| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 4 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 5 | import com.mojang.renderpearl.backend.common.BaseGpuBuffer;
|
| 6 | import com.mojang.renderpearl.backend.vulkan.VulkanGpuBuffer.Direct.1;
|
| 7 | import java.nio.ByteBuffer;
|
| 8 | import java.nio.LongBuffer;
|
| 9 | import java.util.function.Supplier;
|
| 10 | import net.fabricmc.api.EnvType;
|
| 11 | import net.fabricmc.api.Environment;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 | import org.lwjgl.PointerBuffer;
|
| 14 | import org.lwjgl.system.MemoryStack;
|
| 15 | import org.lwjgl.system.MemoryUtil;
|
| 16 | import org.lwjgl.util.vma.Vma;
|
| 17 | import org.lwjgl.util.vma.VmaAllocationCreateInfo;
|
| 18 | import org.lwjgl.vulkan.VkBufferCreateInfo;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public abstract class VulkanGpuBuffer extends BaseGpuBuffer implements Destroyable {
|
| 22 | private final long vkBuffer;
|
| 23 |
|
| 24 | public VulkanGpuBuffer(final long vkBuffer, final @GpuBuffer.Usage int usage, final long size) {
|
| 25 | super(usage, size);
|
| 26 | this.vkBuffer = vkBuffer;
|
| 27 | }
|
| 28 |
|
| 29 | public long vkBuffer() {
|
| 30 | return this.vkBuffer;
|
| 31 | }
|
| 32 |
|
| 33 | @Environment(EnvType.CLIENT)
|
| 34 | public static class Direct extends VulkanGpuBuffer {
|
| 35 | private boolean closed;
|
| 36 | protected final VulkanDevice device;
|
| 37 | private final long vmaAllocation;
|
| 38 | private int mappingRefCount;
|
| 39 |
|
| 40 | public Direct(
|
| 41 | final VulkanDevice device,
|
| 42 | final @Nullable Supplier<String> label,
|
| 43 | final @GpuBuffer.Usage int usage,
|
| 44 | final long size,
|
| 45 | final boolean forceHostVisibleAllocation
|
| 46 | ) {
|
| 47 | this.device = device;
|
| 48 |
|
| 49 | long vkBuffer;
|
| 50 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 51 | VkBufferCreateInfo bufferCreateInfo = VkBufferCreateInfo.calloc(stack).sType$Default();
|
| 52 | bufferCreateInfo.size(size);
|
| 53 | bufferCreateInfo.usage(VulkanConst.bufferUsageToVk(usage));
|
| 54 | bufferCreateInfo.sharingMode(0);
|
| 55 | bufferCreateInfo.pQueueFamilyIndices(null);
|
| 56 | VmaAllocationCreateInfo allocCreateInfo = VmaAllocationCreateInfo.calloc(stack);
|
| 57 | allocCreateInfo.usage(8);
|
| 58 | if (forceHostVisibleAllocation) {
|
| 59 | allocCreateInfo.requiredFlags(allocCreateInfo.requiredFlags() | 2);
|
| 60 | }
|
| 61 |
|
| 62 | if (VulkanUtils.hasAnyBit(usage, 3)) {
|
| 63 | allocCreateInfo.requiredFlags(allocCreateInfo.requiredFlags() | 2 | 4);
|
| 64 | if (VulkanUtils.hasAnyBit(usage, 1)) {
|
| 65 | allocCreateInfo.preferredFlags(allocCreateInfo.preferredFlags() | 8);
|
| 66 | allocCreateInfo.flags(allocCreateInfo.flags() | 2048);
|
| 67 | } else {
|
| 68 | allocCreateInfo.flags(allocCreateInfo.flags() | 1024);
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | if (VulkanUtils.hasAnyBit(usage, 5)) {
|
| 73 | allocCreateInfo.usage(9);
|
| 74 | }
|
| 75 |
|
| 76 | LongBuffer bufferPtr = stack.callocLong(1);
|
| 77 | PointerBuffer allocPtr = stack.callocPointer(1);
|
| 78 | int result = Vma.vmaCreateBuffer(device.vma(), bufferCreateInfo, allocCreateInfo, bufferPtr, allocPtr, null);
|
| 79 | VulkanUtils.crashIfFailure(device, result, "Failed to allocate VkBuffer");
|
| 80 | vkBuffer = bufferPtr.get(0);
|
| 81 | this.vmaAllocation = allocPtr.get(0);
|
| 82 | if (label != null) {
|
| 83 | device.instance().debug().setObjectName(device.vkDevice(), 9, vkBuffer, label);
|
| 84 | }
|
| 85 | }
|
| 86 |
|
| 87 | super(vkBuffer, usage, size);
|
| 88 | this.closed = false;
|
| 89 | this.mappingRefCount = 0;
|
| 90 | }
|
| 91 |
|
| 92 | @Override
|
| 93 | public void destroy() {
|
| 94 | Vma.vmaDestroyBuffer(this.device.vma(), this.vkBuffer(), this.vmaAllocation);
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public boolean isClosed() {
|
| 99 | return this.closed;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public void close() {
|
| 104 | if (!this.closed) {
|
| 105 | this.closed = true;
|
| 106 | if (this.mappingRefCount != 0) {
|
| 107 | throw new IllegalStateException("Attempt to close a mapped buffer");
|
| 108 | }
|
| 109 |
|
| 110 | this.device.createCommandEncoder().queueForDestroy(this);
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | @Override
|
| 115 | public GpuBufferSlice.MappedView map(final long offset, final long length, final boolean read, final boolean write) {
|
| 116 | if (this.isClosed()) {
|
| 117 | throw new IllegalStateException("Buffer already closed");
|
| 118 | }
|
| 119 |
|
| 120 | if (!read && !write) {
|
| 121 | throw new IllegalArgumentException("At least read or write must be true");
|
| 122 | }
|
| 123 |
|
| 124 | if (read && (this.usage() & 1) == 0) {
|
| 125 | throw new IllegalStateException("Buffer is not readable");
|
| 126 | }
|
| 127 |
|
| 128 | if (write && (this.usage() & 2) == 0) {
|
| 129 | throw new IllegalStateException("Buffer is not writable");
|
| 130 | }
|
| 131 |
|
| 132 | if (offset + length > this.size()) {
|
| 133 | throw new IllegalArgumentException(
|
| 134 | "Cannot map more data than this buffer can hold (attempting to map " + length + " bytes at offset " + offset + " from " + this.size() + " size buffer)"
|
| 135 | );
|
| 136 | }
|
| 137 |
|
| 138 | if (length > 2147483647L) {
|
| 139 | throw new IllegalArgumentException("Mapping buffer slice larger than 2GB is not supported");
|
| 140 | }
|
| 141 |
|
| 142 | if (offset >= 0L && length >= 0L) {
|
| 143 | this.mappingRefCount++;
|
| 144 |
|
| 145 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 146 | PointerBuffer pointer = stack.callocPointer(1);
|
| 147 | VulkanUtils.crashIfFailure(this.device, Vma.vmaMapMemory(this.device.vma(), this.vmaAllocation, pointer), "Failed to map buffer");
|
| 148 | ByteBuffer byteBuffer = MemoryUtil.memByteBuffer(pointer.get(0) + offset, (int)length);
|
| 149 | return new GpuBufferSlice.MappedView(this.slice(offset, length), byteBuffer, new 1(this));
|
| 150 | }
|
| 151 | } else {
|
| 152 | throw new IllegalArgumentException("Offset or length must be positive integer values");
|
| 153 | }
|
| 154 | }
|
| 155 | }
|
| 156 | }
|
| 157 |
|