| 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.api.buffers.TransientMemory;
|
| 6 | import com.mojang.renderpearl.backend.util.TransientBlockAllocator;
|
| 7 | import com.mojang.renderpearl.backend.util.TransientBlockAllocator.Allocator.Block;
|
| 8 | import com.mojang.renderpearl.backend.util.TransientBlockAllocator.Allocator.CpuBlock;
|
| 9 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 10 | import it.unimi.dsi.fastutil.ints.IntComparator;
|
| 11 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 12 | import java.nio.ByteBuffer;
|
| 13 | import java.nio.LongBuffer;
|
| 14 | import java.util.List;
|
| 15 | import java.util.stream.IntStream;
|
| 16 | import net.fabricmc.api.EnvType;
|
| 17 | import net.fabricmc.api.Environment;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 | import org.lwjgl.PointerBuffer;
|
| 21 | import org.lwjgl.system.MemoryStack;
|
| 22 | import org.lwjgl.system.MemoryUtil;
|
| 23 | import org.lwjgl.util.vma.Vma;
|
| 24 | import org.lwjgl.util.vma.VmaAllocationCreateInfo;
|
| 25 | import org.lwjgl.util.vma.VmaAllocationInfo;
|
| 26 | import org.lwjgl.vulkan.VK12;
|
| 27 | import org.lwjgl.vulkan.VkBufferCopy;
|
| 28 | import org.lwjgl.vulkan.VkBufferCreateInfo;
|
| 29 | import org.lwjgl.vulkan.VkCommandBuffer;
|
| 30 | import org.lwjgl.vulkan.VkDevice;
|
| 31 | import org.lwjgl.vulkan.VkMemoryHeap;
|
| 32 | import org.lwjgl.vulkan.VkMemoryType;
|
| 33 | import org.lwjgl.vulkan.VkPhysicalDeviceMemoryProperties;
|
| 34 | import org.lwjgl.vulkan.VkBufferCopy.Buffer;
|
| 35 |
|
| 36 | @Environment(EnvType.CLIENT)
|
| 37 | public class VulkanTransientMemory implements TransientMemory, Destroyable {
|
| 38 | private static final long BLOCK_SIZE = 524288L;
|
| 39 | private static final long MAX_CPU_ALIGNMENT = 16L;
|
| 40 | private static final long MAX_GPU_ALIGNMENT = Long.highestOneBit(Long.MAX_VALUE);
|
| 41 | private static final int BUFFER_USAGE_BITS = 471;
|
| 42 | private final VulkanDevice device;
|
| 43 | private final VulkanCommandEncoder encoder;
|
| 44 | private final boolean useDeviceMemoryForMappedGpuStaging;
|
| 45 | private final TransientBlockAllocator<CpuBlock> cpuBlockAllocator = new TransientBlockAllocator(524288L, 16L, CpuBlock.memalloc());
|
| 46 | private final TransientBlockAllocator<VulkanTransientMemory.VulkanAllocation> stagingBlockAllocator;
|
| 47 | private final TransientBlockAllocator<VulkanTransientMemory.VulkanAllocation> gpuBlockAllocator;
|
| 48 | private final TransientBlockAllocator<VulkanTransientMemory.TransferPair> gpuMappedBlockAllocator;
|
| 49 | private final int expectedCpuMemoryHeap;
|
| 50 | private final int expectedGpuMemoryHeap;
|
| 51 | private final int[] memoryTypeToHeapMap = new int[32];
|
| 52 | private long submitIndex = 0L;
|
| 53 | private boolean anyCommandRecorded = false;
|
| 54 | private @Nullable VkCommandBuffer commandBuffer;
|
| 55 |
|
| 56 | VulkanTransientMemory(final VulkanDevice device, final VulkanCommandEncoder encoder) {
|
| 57 | this.device = device;
|
| 58 | this.encoder = encoder;
|
| 59 |
|
| 60 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 61 | VkDevice vkDevice = device.vkDevice();
|
| 62 | VkPhysicalDeviceMemoryProperties memoryProperties = VkPhysicalDeviceMemoryProperties.calloc(stack);
|
| 63 | VK12.vkGetPhysicalDeviceMemoryProperties(vkDevice.getPhysicalDevice(), memoryProperties);
|
| 64 | int heapCount = memoryProperties.memoryHeapCount();
|
| 65 | int typeCount = memoryProperties.memoryTypeCount();
|
| 66 |
|
| 67 | for (int i = 0; i < typeCount; i++) {
|
| 68 | this.memoryTypeToHeapMap[i] = memoryProperties.memoryTypes(i).heapIndex();
|
| 69 | }
|
| 70 |
|
| 71 | int largestDeviceLocalHeapIndex = -1;
|
| 72 | long largestDeviceLocalHeapSize = -1L;
|
| 73 |
|
| 74 | for (int i = 0; i < heapCount; i++) {
|
| 75 | VkMemoryHeap heapProperties = memoryProperties.memoryHeaps(i);
|
| 76 | if (VulkanUtils.hasAnyBit(heapProperties.flags(), 1) && heapProperties.size() >= largestDeviceLocalHeapSize) {
|
| 77 | largestDeviceLocalHeapIndex = i;
|
| 78 | largestDeviceLocalHeapSize = heapProperties.size();
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | assert largestDeviceLocalHeapIndex != -1;
|
| 83 | boolean largestHeapIsHostVisibleAndCoherent = false;
|
| 84 |
|
| 85 | for (int i = 0; i < typeCount; i++) {
|
| 86 | VkMemoryType typeProperties = memoryProperties.memoryTypes(i);
|
| 87 | if (typeProperties.heapIndex() == largestDeviceLocalHeapIndex && VulkanUtils.hasAllBits(typeProperties.propertyFlags(), 6)) {
|
| 88 | largestHeapIsHostVisibleAndCoherent = true;
|
| 89 | break;
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | this.useDeviceMemoryForMappedGpuStaging = largestHeapIsHostVisibleAndCoherent;
|
| 94 | int selectedCpuHeap = -1;
|
| 95 | if (this.useDeviceMemoryForMappedGpuStaging) {
|
| 96 | selectedCpuHeap = largestDeviceLocalHeapIndex;
|
| 97 | } else {
|
| 98 | for (int i = 0; i < typeCount; i++) {
|
| 99 | VkMemoryType typeProperties = memoryProperties.memoryTypes(i);
|
| 100 | if (VulkanUtils.hasAllBits(typeProperties.propertyFlags(), 6)) {
|
| 101 | VkMemoryHeap heapProperties = memoryProperties.memoryHeaps(typeProperties.heapIndex());
|
| 102 | if (!VulkanUtils.hasAnyBit(heapProperties.flags(), 1)) {
|
| 103 | selectedCpuHeap = typeProperties.heapIndex();
|
| 104 | break;
|
| 105 | }
|
| 106 | }
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | if (selectedCpuHeap == -1) {
|
| 111 | throw new IllegalStateException("Could not select heap for CPU allocations");
|
| 112 | }
|
| 113 |
|
| 114 | this.expectedCpuMemoryHeap = selectedCpuHeap;
|
| 115 | this.expectedGpuMemoryHeap = largestDeviceLocalHeapIndex;
|
| 116 | }
|
| 117 |
|
| 118 | this.stagingBlockAllocator = new TransientBlockAllocator<>(
|
| 119 | 524288L, MAX_GPU_ALIGNMENT, TransientBlockAllocator.Allocator.create(size -> this.allocateVulkanBlock(size, true), this::freeVulkanBlock)
|
| 120 | );
|
| 121 | this.gpuBlockAllocator = new TransientBlockAllocator<>(
|
| 122 | 524288L, MAX_GPU_ALIGNMENT, TransientBlockAllocator.Allocator.create(size -> this.allocateVulkanBlock(size, false), this::queueFreeVulkanBlock)
|
| 123 | );
|
| 124 | this.gpuMappedBlockAllocator = new TransientBlockAllocator<>(
|
| 125 | 524288L,
|
| 126 | MAX_GPU_ALIGNMENT,
|
| 127 | TransientBlockAllocator.Allocator.create(this::allocateGpuMappedVulkanBlock, this::freeGpuMappedVulkanBlock),
|
| 128 | this::recordGpuMappedCopy
|
| 129 | );
|
| 130 | }
|
| 131 |
|
| 132 | @Override
|
| 133 | public void destroy() {
|
| 134 | this.cpuBlockAllocator.close();
|
| 135 | this.stagingBlockAllocator.close();
|
| 136 | this.gpuBlockAllocator.close();
|
| 137 | this.gpuMappedBlockAllocator.close();
|
| 138 | }
|
| 139 |
|
| 140 | public void beginSubmit() {
|
| 141 | assert this.commandBuffer == null;
|
| 142 | this.commandBuffer = this.encoder.allocateAndBeginTransientCommandBuffer();
|
| 143 | this.encoder.execute(this.commandBuffer);
|
| 144 | this.anyCommandRecorded = false;
|
| 145 | }
|
| 146 |
|
| 147 | public void endSubmit() {
|
| 148 | this.cpuBlockAllocator.rotate().run();
|
| 149 | this.encoder.queueForDestroy(this.stagingBlockAllocator.rotate()::run);
|
| 150 | if (this.useDeviceMemoryForMappedGpuStaging) {
|
| 151 | this.encoder.queueForDestroy(this.gpuBlockAllocator.rotate()::run);
|
| 152 | } else {
|
| 153 | this.gpuBlockAllocator.rotate().run();
|
| 154 | }
|
| 155 |
|
| 156 | this.gpuMappedBlockAllocator.rotate();
|
| 157 | assert this.commandBuffer != null;
|
| 158 | if (this.anyCommandRecorded) {
|
| 159 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 160 | VulkanCommandEncoder.memoryBarrier(this.commandBuffer, stack);
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | VK12.vkEndCommandBuffer(this.commandBuffer);
|
| 165 | this.commandBuffer = null;
|
| 166 | this.submitIndex++;
|
| 167 | }
|
| 168 |
|
| 169 | private void recordGpuMappedCopy(final VulkanTransientMemory.TransferPair block) {
|
| 170 | if (block.cpu() != block.gpu()) {
|
| 171 | assert block.cpu().size == block.gpu().size;
|
| 172 |
|
| 173 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 174 | Buffer region = VkBufferCopy.calloc(1, stack);
|
| 175 | region.srcOffset(0L);
|
| 176 | region.dstOffset(0L);
|
| 177 | region.size(block.cpu().size);
|
| 178 | assert this.commandBuffer != null;
|
| 179 | VK12.vkCmdCopyBuffer(this.commandBuffer, block.cpu().vkBuffer, block.gpu().vkBuffer, region);
|
| 180 | this.anyCommandRecorded = true;
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 |
|
| 185 | private VulkanTransientMemory.VulkanAllocation allocateVulkanBlock(final long size, final boolean staging) {
|
| 186 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 187 | VkBufferCreateInfo bufferCreateInfo = VkBufferCreateInfo.calloc(stack).sType$Default();
|
| 188 | bufferCreateInfo.size(size);
|
| 189 | bufferCreateInfo.usage(471);
|
| 190 | bufferCreateInfo.sharingMode(0);
|
| 191 | bufferCreateInfo.pQueueFamilyIndices(null);
|
| 192 | VmaAllocationCreateInfo allocCreateInfo = VmaAllocationCreateInfo.calloc(stack);
|
| 193 | int expectedHeap;
|
| 194 | if (staging && !this.useDeviceMemoryForMappedGpuStaging) {
|
| 195 | allocCreateInfo.usage(9);
|
| 196 | expectedHeap = this.expectedCpuMemoryHeap;
|
| 197 | } else {
|
| 198 | allocCreateInfo.usage(8);
|
| 199 | expectedHeap = this.expectedGpuMemoryHeap;
|
| 200 | }
|
| 201 |
|
| 202 | if (this.useDeviceMemoryForMappedGpuStaging || staging) {
|
| 203 | allocCreateInfo.requiredFlags(6);
|
| 204 | allocCreateInfo.flags(1024);
|
| 205 | }
|
| 206 |
|
| 207 | LongBuffer bufferPtr = stack.callocLong(1);
|
| 208 | PointerBuffer allocPtr = stack.callocPointer(1);
|
| 209 | VmaAllocationInfo allocationInfo = VmaAllocationInfo.calloc(stack);
|
| 210 | int result = Vma.vmaCreateBuffer(this.device.vma(), bufferCreateInfo, allocCreateInfo, bufferPtr, allocPtr, allocationInfo);
|
| 211 | VulkanUtils.crashIfFailure(this.device, result, "Failed to allocate VkBuffer");
|
| 212 | PointerBuffer hostPtrPtr = stack.callocPointer(1);
|
| 213 | if (staging || this.useDeviceMemoryForMappedGpuStaging) {
|
| 214 | VulkanUtils.crashIfFailure(this.device, Vma.vmaMapMemory(this.device.vma(), allocPtr.get(0), hostPtrPtr), "Failed to map buffer");
|
| 215 | }
|
| 216 |
|
| 217 | this.device.instance().debug().setObjectName(this.device.vkDevice(), 9, bufferPtr.get(0), "Vulkan Transient Memory Buffer");
|
| 218 | int allocatedHeap = this.memoryTypeToHeapMap[allocationInfo.memoryType()];
|
| 219 | return new VulkanTransientMemory.VulkanAllocation(bufferPtr.get(0), allocPtr.get(0), hostPtrPtr.get(0), size, allocatedHeap != expectedHeap);
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | private void queueFreeVulkanBlock(final VulkanTransientMemory.VulkanAllocation allocation) {
|
| 224 | this.encoder.queueForDestroy(() -> this.freeVulkanBlock(allocation));
|
| 225 | }
|
| 226 |
|
| 227 | private void freeVulkanBlock(final VulkanTransientMemory.VulkanAllocation allocation) {
|
| 228 | Vma.vmaDestroyBuffer(this.device.vma(), allocation.vkBuffer, allocation.vmaAllocation);
|
| 229 | }
|
| 230 |
|
| 231 | private VulkanTransientMemory.TransferPair allocateGpuMappedVulkanBlock(final long size) {
|
| 232 | assert size >= 524288L;
|
| 233 | assert size >= this.gpuBlockAllocator.blockSize();
|
| 234 | if (this.useDeviceMemoryForMappedGpuStaging) {
|
| 235 | TransientBlockAllocator.Allocation<VulkanTransientMemory.VulkanAllocation> block = this.gpuBlockAllocator.allocate(size, 16L, size, 1L);
|
| 236 | assert block.offset() == 0L;
|
| 237 | return new VulkanTransientMemory.TransferPair(block.block(), block.block());
|
| 238 | } else {
|
| 239 | assert size >= this.stagingBlockAllocator.blockSize();
|
| 240 | TransientBlockAllocator.Allocation<VulkanTransientMemory.VulkanAllocation> stagingBlock = this.stagingBlockAllocator.allocate(size, 16L, size, 1L);
|
| 241 | TransientBlockAllocator.Allocation<VulkanTransientMemory.VulkanAllocation> gpuBlock = this.gpuBlockAllocator.allocate(size, 16L, size, 1L);
|
| 242 | return new VulkanTransientMemory.TransferPair(stagingBlock.block(), gpuBlock.block());
|
| 243 | }
|
| 244 | }
|
| 245 |
|
| 246 | private void freeGpuMappedVulkanBlock(final VulkanTransientMemory.TransferPair allocations) {
|
| 247 | }
|
| 248 |
|
| 249 | @Override
|
| 250 | public ByteBuffer allocateCpu(final long size, final long alignment, final long minimumAllocation, final long elementSize) {
|
| 251 | assert size <= 2147483647L;
|
| 252 | TransientBlockAllocator.Allocation<CpuBlock> alloc = this.cpuBlockAllocator.allocate(size, alignment, minimumAllocation, elementSize);
|
| 253 | return MemoryUtil.memByteBuffer(alloc.block().address() + alloc.offset(), (int)alloc.size());
|
| 254 | }
|
| 255 |
|
| 256 | @Override
|
| 257 | public GpuBufferSlice.MappedView allocateStaging(
|
| 258 | final long size, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 259 | ) {
|
| 260 | assert size <= 2147483647L;
|
| 261 | TransientBlockAllocator.Allocation<VulkanTransientMemory.VulkanAllocation> alloc = this.stagingBlockAllocator
|
| 262 | .allocate(size, alignment, minimumAllocation, elementSize);
|
| 263 | VulkanTransientMemory.TransientGpuBuffer apiBuffer = new VulkanTransientMemory.TransientGpuBuffer(
|
| 264 | alloc.block().vkBuffer, usage, (int)alloc.block().size, this.submitIndex
|
| 265 | );
|
| 266 | ByteBuffer cpuBuffer = MemoryUtil.memByteBuffer(alloc.block().hostPtr + alloc.offset(), (int)alloc.size());
|
| 267 | return new GpuBufferSlice.MappedView(new GpuBufferSlice(apiBuffer, alloc.offset(), alloc.size()), cpuBuffer, () -> {});
|
| 268 | }
|
| 269 |
|
| 270 | @Override
|
| 271 | public GpuBufferSlice allocateGpu(
|
| 272 | final long size, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 273 | ) {
|
| 274 | assert size <= 2147483647L;
|
| 275 | TransientBlockAllocator.Allocation<VulkanTransientMemory.VulkanAllocation> alloc = this.gpuBlockAllocator
|
| 276 | .allocate(size, alignment, minimumAllocation, elementSize);
|
| 277 | VulkanTransientMemory.TransientGpuBuffer apiBuffer = new VulkanTransientMemory.TransientGpuBuffer(
|
| 278 | alloc.block().vkBuffer, usage, (int)alloc.block().size, this.submitIndex
|
| 279 | );
|
| 280 | return new GpuBufferSlice(apiBuffer, alloc.offset(), alloc.size());
|
| 281 | }
|
| 282 |
|
| 283 | @Override
|
| 284 | public GpuBufferSlice.MappedView allocateGpuMapped(
|
| 285 | final long size, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 286 | ) {
|
| 287 | assert size <= 2147483647L;
|
| 288 | TransientBlockAllocator.Allocation<VulkanTransientMemory.TransferPair> alloc = this.gpuMappedBlockAllocator
|
| 289 | .allocate(size, alignment, minimumAllocation, elementSize);
|
| 290 | VulkanTransientMemory.TransientGpuBuffer apiBuffer = new VulkanTransientMemory.TransientGpuBuffer(
|
| 291 | alloc.block().gpu().vkBuffer, usage, (int)alloc.block().gpu().size, this.submitIndex
|
| 292 | );
|
| 293 | ByteBuffer cpuBuffer = MemoryUtil.memByteBuffer(alloc.block().cpu().hostPtr + alloc.offset(), (int)alloc.size());
|
| 294 | return new GpuBufferSlice.MappedView(new GpuBufferSlice(apiBuffer, alloc.offset(), alloc.size()), cpuBuffer, () -> {});
|
| 295 | }
|
| 296 |
|
| 297 | @Override
|
| 298 | public GpuBufferSlice uploadStaging(
|
| 299 | final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 300 | ) {
|
| 301 | return this.upload(data, alignment, usage, minimumAllocation, elementSize, true);
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | public GpuBufferSlice uploadGpu(
|
| 306 | final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage, final long minimumAllocation, final long elementSize
|
| 307 | ) {
|
| 308 | return this.upload(data, alignment, usage, minimumAllocation, elementSize, false);
|
| 309 | }
|
| 310 |
|
| 311 | public GpuBufferSlice upload(
|
| 312 | final List<ByteBuffer> data,
|
| 313 | final long alignment,
|
| 314 | final @GpuBuffer.Usage int usage,
|
| 315 | final long minimumAllocation,
|
| 316 | final long elementSize,
|
| 317 | final boolean staging
|
| 318 | ) {
|
| 319 | long totalSize = 0L;
|
| 320 |
|
| 321 | for (ByteBuffer buffer : data) {
|
| 322 | totalSize += buffer.remaining();
|
| 323 | totalSize = Mth.roundToward(totalSize, alignment);
|
| 324 | }
|
| 325 |
|
| 326 | try (GpuBufferSlice.MappedView mapped = staging
|
| 327 | ? this.allocateStaging(totalSize, alignment, usage, minimumAllocation, elementSize)
|
| 328 | : this.allocateGpuMapped(totalSize, alignment, usage, minimumAllocation, elementSize)) {
|
| 329 | long mappedPtr = MemoryUtil.memAddress(mapped.data());
|
| 330 | long offset = 0L;
|
| 331 |
|
| 332 | for (ByteBuffer buffer : data) {
|
| 333 | MemoryUtil.memCopy(MemoryUtil.memAddress(buffer), mappedPtr + offset, Math.min(mapped.slice().length() - offset, buffer.remaining()));
|
| 334 | offset += buffer.remaining();
|
| 335 | offset = Mth.roundToward(offset, alignment);
|
| 336 | if (offset >= mapped.slice().length()) {
|
| 337 | break;
|
| 338 | }
|
| 339 | }
|
| 340 |
|
| 341 | return mapped.slice();
|
| 342 | }
|
| 343 | }
|
| 344 |
|
| 345 | @Override
|
| 346 | public List<GpuBufferSlice> multiUploadStaging(final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage) {
|
| 347 | return this.multiUpload(data, alignment, usage, true);
|
| 348 | }
|
| 349 |
|
| 350 | @Override
|
| 351 | public List<GpuBufferSlice> multiUploadGpu(final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage) {
|
| 352 | return this.multiUpload(data, alignment, usage, false);
|
| 353 | }
|
| 354 |
|
| 355 | public List<GpuBufferSlice> multiUpload(final List<ByteBuffer> data, final long alignment, final @GpuBuffer.Usage int usage, final boolean staging) {
|
| 356 | ReferenceArrayList<GpuBufferSlice> uploadedBuffers = new ReferenceArrayList<>();
|
| 357 | uploadedBuffers.size(data.size());
|
| 358 | TransientBlockAllocator<?> allocatorInUse = staging ? this.stagingBlockAllocator : this.gpuMappedBlockAllocator;
|
| 359 | IntArrayList sortedDataIndices = IntArrayList.toList(IntStream.range(0, data.size()));
|
| 360 | sortedDataIndices.sort(IntComparator.comparing(index -> data.get(index).remaining()));
|
| 361 |
|
| 362 | while (!sortedDataIndices.isEmpty()) {
|
| 363 | boolean allocatedAnything = false;
|
| 364 |
|
| 365 | for (int i = sortedDataIndices.size() - 1; i >= 0; i--) {
|
| 366 | int bufferIndex = sortedDataIndices.getInt(i);
|
| 367 | ByteBuffer currentBuffer = data.get(bufferIndex);
|
| 368 | if (allocatorInUse.canAllocateInCurrentBlock(currentBuffer.remaining(), alignment)) {
|
| 369 | sortedDataIndices.removeInt(i);
|
| 370 |
|
| 371 | try (GpuBufferSlice.MappedView view = staging
|
| 372 | ? this.allocateStaging(currentBuffer.remaining(), alignment, usage)
|
| 373 | : this.allocateGpuMapped(currentBuffer.remaining(), alignment, usage)) {
|
| 374 | MemoryUtil.memCopy(currentBuffer, view.data());
|
| 375 | uploadedBuffers.set(bufferIndex, view.slice());
|
| 376 | }
|
| 377 |
|
| 378 | allocatedAnything = true;
|
| 379 | break;
|
| 380 | }
|
| 381 | }
|
| 382 |
|
| 383 | if (!allocatedAnything) {
|
| 384 | int bufferIndex = sortedDataIndices.popInt();
|
| 385 | ByteBuffer currentBuffer = data.get(bufferIndex);
|
| 386 |
|
| 387 | try (GpuBufferSlice.MappedView view = this.allocateGpuMapped(currentBuffer.remaining(), alignment, usage)) {
|
| 388 | MemoryUtil.memCopy(currentBuffer, view.data());
|
| 389 | uploadedBuffers.set(bufferIndex, view.slice());
|
| 390 | }
|
| 391 | }
|
| 392 | }
|
| 393 |
|
| 394 | return uploadedBuffers;
|
| 395 | }
|
| 396 |
|
| 397 | @Environment(EnvType.CLIENT)
|
| 398 | private record TransferPair(VulkanTransientMemory.VulkanAllocation cpu, VulkanTransientMemory.VulkanAllocation gpu) implements Block {
|
| 399 | public boolean suboptimal() {
|
| 400 | return this.cpu.suboptimal() || this.gpu.suboptimal();
|
| 401 | }
|
| 402 | }
|
| 403 |
|
| 404 | @Environment(EnvType.CLIENT)
|
| 405 | private class TransientGpuBuffer extends VulkanGpuBuffer {
|
| 406 | private boolean closed = false;
|
| 407 | private final long bufferSubmitIndex;
|
| 408 |
|
| 409 | public TransientGpuBuffer(final long vkBuffer, final @GpuBuffer.Usage int usage, final int size, final long bufferSubmitIndex) {
|
| 410 | super(vkBuffer, usage, size);
|
| 411 | this.bufferSubmitIndex = bufferSubmitIndex;
|
| 412 | }
|
| 413 |
|
| 414 | @Override
|
| 415 | public void destroy() {
|
| 416 | }
|
| 417 |
|
| 418 | @Override
|
| 419 | public GpuBufferSlice.MappedView map(final long offset, final long length, final boolean read, final boolean write) {
|
| 420 | throw new IllegalStateException("Cannot map transient buffer");
|
| 421 | }
|
| 422 |
|
| 423 | @Override
|
| 424 | public boolean isClosed() {
|
| 425 | if (this.closed) {
|
| 426 | return true;
|
| 427 | }
|
| 428 |
|
| 429 | this.closed = this.bufferSubmitIndex < VulkanTransientMemory.this.submitIndex;
|
| 430 | return this.closed;
|
| 431 | }
|
| 432 |
|
| 433 | @Override
|
| 434 | public void close() {
|
| 435 | this.closed = true;
|
| 436 | }
|
| 437 |
|
| 438 | @Override
|
| 439 | public GpuBufferSlice slice(final long offset, final long length) {
|
| 440 | throw new IllegalStateException("Cannot slice transient buffer");
|
| 441 | }
|
| 442 |
|
| 443 | @Override
|
| 444 | public GpuBufferSlice slice() {
|
| 445 | throw new IllegalStateException("Cannot slice transient buffer");
|
| 446 | }
|
| 447 | }
|
| 448 |
|
| 449 | @Environment(EnvType.CLIENT)
|
| 450 | private record VulkanAllocation(long vkBuffer, long vmaAllocation, long hostPtr, long size, boolean suboptimal) implements Block {
|
| 451 | }
|
| 452 | }
|
| 453 |
|