| 1 | package com.mojang.renderpearl.backend.vulkan;
|
| 2 |
|
| 3 | import com.mojang.renderpearl.backend.vulkan.VulkanQueue.Submission.SemaphoreOp;
|
| 4 | import com.mojang.renderpearl.backend.vulkan.VulkanQueue.Submission.SubmitStage;
|
| 5 | import com.mojang.renderpearl.util.UncheckedAutoCloseable;
|
| 6 | import it.unimi.dsi.fastutil.objects.ReferenceArrayList;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import org.lwjgl.PointerBuffer;
|
| 10 | import org.lwjgl.system.MemoryStack;
|
| 11 | import org.lwjgl.vulkan.KHRSynchronization2;
|
| 12 | import org.lwjgl.vulkan.VK12;
|
| 13 | import org.lwjgl.vulkan.VkCommandBuffer;
|
| 14 | import org.lwjgl.vulkan.VkCommandBufferSubmitInfo;
|
| 15 | import org.lwjgl.vulkan.VkQueue;
|
| 16 | import org.lwjgl.vulkan.VkSemaphoreSubmitInfo;
|
| 17 | import org.lwjgl.vulkan.VkSubmitInfo2;
|
| 18 | import org.lwjgl.vulkan.VkSubmitInfo2.Buffer;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public record VulkanQueue(VkQueue vkQueue, int queueFamilyIndex) {
|
| 22 | public VulkanQueue(final VulkanDevice device, final int queueFamilyIndex, final int queueIndex) {
|
| 23 | this(new VkQueue(fetchVkQueue(device, queueFamilyIndex, queueIndex), device.vkDevice()), queueFamilyIndex);
|
| 24 | }
|
| 25 |
|
| 26 | private static long fetchVkQueue(final VulkanDevice device, final int queueFamilyIndex, final int queueIndex) {
|
| 27 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 28 | PointerBuffer queueHandlePtr = stack.callocPointer(1);
|
| 29 | VK12.vkGetDeviceQueue(device.vkDevice(), queueFamilyIndex, queueIndex, queueHandlePtr);
|
| 30 | return queueHandlePtr.get(0);
|
| 31 | }
|
| 32 | }
|
| 33 |
|
| 34 | public VulkanQueue.Submission beginSubmit() {
|
| 35 | return new VulkanQueue.Submission();
|
| 36 | }
|
| 37 |
|
| 38 | public void waitIdle() {
|
| 39 | VK12.vkQueueWaitIdle(this.vkQueue);
|
| 40 | }
|
| 41 |
|
| 42 | @Environment(EnvType.CLIENT)
|
| 43 | public class Submission implements UncheckedAutoCloseable {
|
| 44 | private boolean closed = false;
|
| 45 | private final ReferenceArrayList<SubmitStage> stages = new ReferenceArrayList<>();
|
| 46 |
|
| 47 | private Submission() {
|
| 48 | this.stages.add(new SubmitStage());
|
| 49 | }
|
| 50 |
|
| 51 | public void waitSemaphore(final long semaphore, final long value, final long stageMask) {
|
| 52 | if (this.closed) {
|
| 53 | throw new IllegalStateException("Attempt to use closed Submission");
|
| 54 | }
|
| 55 |
|
| 56 | if (!this.stages.top().commandBuffers.isEmpty() || !this.stages.top().signals.isEmpty()) {
|
| 57 | this.stages.add(new SubmitStage());
|
| 58 | }
|
| 59 |
|
| 60 | this.stages.top().waits.add(new SemaphoreOp(semaphore, value, stageMask));
|
| 61 | }
|
| 62 |
|
| 63 | public void executeCommands(final VkCommandBuffer commandBuffer) {
|
| 64 | if (this.closed) {
|
| 65 | throw new IllegalStateException("Attempt to use closed Submission");
|
| 66 | }
|
| 67 |
|
| 68 | if (!this.stages.top().signals.isEmpty()) {
|
| 69 | this.stages.add(new SubmitStage());
|
| 70 | }
|
| 71 |
|
| 72 | this.stages.top().commandBuffers.add(commandBuffer);
|
| 73 | }
|
| 74 |
|
| 75 | public void signalSemaphore(final long semaphore, final long value, final long stageMask) {
|
| 76 | if (this.closed) {
|
| 77 | throw new IllegalStateException("Attempt to use closed Submission");
|
| 78 | }
|
| 79 |
|
| 80 | this.stages.top().signals.add(new SemaphoreOp(semaphore, value, stageMask));
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public void close() {
|
| 85 | try (MemoryStack stack = MemoryStack.stackPush()) {
|
| 86 | Buffer submits = VkSubmitInfo2.calloc(this.stages.size(), stack);
|
| 87 |
|
| 88 | for (int i = 0; i < this.stages.size(); i++) {
|
| 89 | SubmitStage stage = this.stages.get(i);
|
| 90 | submits.position(i).sType$Default();
|
| 91 | if (!stage.waits.isEmpty()) {
|
| 92 | org.lwjgl.vulkan.VkSemaphoreSubmitInfo.Buffer waits = VkSemaphoreSubmitInfo.calloc(stage.waits.size(), stack);
|
| 93 | submits.pWaitSemaphoreInfos(waits);
|
| 94 |
|
| 95 | for (int j = 0; j < stage.waits.size(); j++) {
|
| 96 | SemaphoreOp wait = (SemaphoreOp)stage.waits.get(j);
|
| 97 | waits.position(j).sType$Default();
|
| 98 | waits.semaphore(wait.vkSemaphore);
|
| 99 | waits.value(wait.value);
|
| 100 | waits.stageMask(wait.stageMask);
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | if (!stage.commandBuffers.isEmpty()) {
|
| 105 | org.lwjgl.vulkan.VkCommandBufferSubmitInfo.Buffer buffers = VkCommandBufferSubmitInfo.calloc(stage.commandBuffers.size(), stack);
|
| 106 | submits.pCommandBufferInfos(buffers);
|
| 107 |
|
| 108 | for (int j = 0; j < stage.commandBuffers.size(); j++) {
|
| 109 | VkCommandBuffer vkCommandBuffer = (VkCommandBuffer)stage.commandBuffers.get(j);
|
| 110 | buffers.position(j).sType$Default();
|
| 111 | buffers.commandBuffer(vkCommandBuffer);
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | if (!stage.signals.isEmpty()) {
|
| 116 | org.lwjgl.vulkan.VkSemaphoreSubmitInfo.Buffer signals = VkSemaphoreSubmitInfo.calloc(stage.signals.size(), stack);
|
| 117 | submits.pSignalSemaphoreInfos(signals);
|
| 118 |
|
| 119 | for (int j = 0; j < stage.signals.size(); j++) {
|
| 120 | SemaphoreOp signal = (SemaphoreOp)stage.signals.get(j);
|
| 121 | signals.position(j).sType$Default();
|
| 122 | signals.semaphore(signal.vkSemaphore);
|
| 123 | signals.value(signal.value);
|
| 124 | signals.stageMask(signal.stageMask);
|
| 125 | }
|
| 126 | }
|
| 127 | }
|
| 128 |
|
| 129 | submits.position(0);
|
| 130 | KHRSynchronization2.vkQueueSubmit2KHR(VulkanQueue.this.vkQueue, submits, 0L);
|
| 131 | this.closed = true;
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 | }
|
| 136 |
|