| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import java.nio.LongBuffer;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import org.lwjgl.PointerBuffer;
|
| 7 | import org.lwjgl.system.MemoryStack;
|
| 8 | import org.lwjgl.system.MemoryUtil;
|
| 9 | import org.lwjgl.vulkan.VK12;
|
| 10 | import org.lwjgl.vulkan.VkCommandBuffer;
|
| 11 | import org.lwjgl.vulkan.VkCommandBufferAllocateInfo;
|
| 12 | import org.lwjgl.vulkan.VkCommandPoolCreateInfo;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class VulkanCommandPool implements Destroyable {
|
| 16 | private static final int BUFFER_ALLOC_COUNT = 32;
|
| 17 | private static final int HANDLE_BUFFER_BLOCK_SIZE = 512;
|
| 18 | private final VulkanDevice device;
|
| 19 | private final long commandPool;
|
| 20 | private PointerBuffer allocatedBuffers;
|
| 21 |
|
| 22 | public VulkanCommandPool(final VulkanDevice device, final VulkanQueue queue) {
|
| 23 | this.device = device;
|
| 24 |
|
| 25 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 26 | VkCommandPoolCreateInfo commandPoolCreateInfo = VkCommandPoolCreateInfo.calloc(stack).sType$Default();
|
| 27 | commandPoolCreateInfo.flags(1);
|
| 28 | commandPoolCreateInfo.queueFamilyIndex(queue.queueFamilyIndex());
|
| 29 | LongBuffer commandPoolHandlePtr = stack.callocLong(1);
|
| 30 | VulkanUtils.crashIfFailure(
|
| 31 | device, VK12.vkCreateCommandPool(device.vkDevice(), commandPoolCreateInfo, null, commandPoolHandlePtr), "Failed to create VkCommandPool"
|
| 32 | );
|
| 33 | this.commandPool = commandPoolHandlePtr.get(0);
|
| 34 | }
|
| 35 |
|
| 36 | this.allocatedBuffers = MemoryUtil.memAllocPointer(512);
|
| 37 | this.allocatedBuffers.limit(0);
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public void destroy() {
|
| 42 | this.release();
|
| 43 | this.allocatedBuffers.free();
|
| 44 | VK12.vkDestroyCommandPool(this.device.vkDevice(), this.commandPool, null);
|
| 45 | }
|
| 46 |
|
| 47 | public void release() {
|
| 48 | this.allocatedBuffers.rewind();
|
| 49 | if (this.allocatedBuffers.hasRemaining()) {
|
| 50 | VK12.vkFreeCommandBuffers(this.device.vkDevice(), this.commandPool, this.allocatedBuffers);
|
| 51 | this.allocatedBuffers.clear();
|
| 52 | MemoryUtil.memSet(this.allocatedBuffers, 0);
|
| 53 | }
|
| 54 |
|
| 55 | VK12.vkResetCommandPool(this.device.vkDevice(), this.commandPool, 1);
|
| 56 | this.allocatedBuffers.limit(0);
|
| 57 | }
|
| 58 |
|
| 59 | public void reset() {
|
| 60 | VK12.vkResetCommandPool(this.device.vkDevice(), this.commandPool, 0);
|
| 61 | this.allocatedBuffers.rewind();
|
| 62 | }
|
| 63 |
|
| 64 | private void allocateMoreBuffers() {
|
| 65 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 66 | if (this.allocatedBuffers.capacity() - this.allocatedBuffers.limit() < 32) {
|
| 67 | PointerBuffer newBuffer = MemoryUtil.memRealloc(this.allocatedBuffers, this.allocatedBuffers.capacity() + 512);
|
| 68 | newBuffer.limit(this.allocatedBuffers.limit());
|
| 69 | this.allocatedBuffers = newBuffer;
|
| 70 | }
|
| 71 |
|
| 72 | VkCommandBufferAllocateInfo allocateInfo = VkCommandBufferAllocateInfo.calloc(stack).sType$Default();
|
| 73 | allocateInfo.commandPool(this.commandPool);
|
| 74 | allocateInfo.level(0);
|
| 75 | allocateInfo.commandBufferCount(32);
|
| 76 | this.allocatedBuffers.limit(this.allocatedBuffers.limit() + 32);
|
| 77 | PointerBuffer buffers = this.allocatedBuffers.slice(0, 32);
|
| 78 | VulkanUtils.crashIfFailure(this.device, VK12.vkAllocateCommandBuffers(this.device.vkDevice(), allocateInfo, buffers), "Failed to allocate VkCommandBuffers");
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | public VkCommandBuffer allocateBuffer() {
|
| 83 | if (!this.allocatedBuffers.hasRemaining()) {
|
| 84 | this.allocateMoreBuffers();
|
| 85 | }
|
| 86 |
|
| 87 | return new VkCommandBuffer(this.allocatedBuffers.get(), this.device.vkDevice());
|
| 88 | }
|
| 89 | }
|
| 90 |
|