Skip to content

Commit 435e0b4

Browse files
committed
Move all swapchain semaphore logic into Vk11GpuSurface
1 parent e46ccaf commit 435e0b4

2 files changed

Lines changed: 32 additions & 60 deletions

File tree

src/main/java/git/artdeell/artvk/Vk11CommandEncoder.java

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,19 @@ public class Vk11CommandEncoder implements CommandEncoderBackend, Destroyable {
2929
public static final int MAX_SUBMITS_IN_FLIGHT = 3;
3030
private final Vk11Device device;
3131
private final Vk11TransientMemory transientMemory;
32-
private final long[] acquireSemaphores = new long[MAX_SUBMITS_IN_FLIGHT];
33-
private final long[] presentSemaphores = new long[MAX_SUBMITS_IN_FLIGHT];
3432
private final Vk11Fence[] frameFences = new Vk11Fence[MAX_SUBMITS_IN_FLIGHT];
3533
private int currentSubmitIndex = 0;
3634
private Vk11Queue.Submission submissionBuilder;
3735
private final DestructionQueue<Destroyable> destroyQueue = new DestructionQueue<>(MAX_SUBMITS_IN_FLIGHT, Destroyable::destroy);
3836
private final Vk11CommandPool[] commandPools = new Vk11CommandPool[MAX_SUBMITS_IN_FLIGHT];
3937
private @Nullable VkCommandBuffer currentCommandBuffer;
4038
private @Nullable Vk11RenderPass currentRenderPass;
41-
private long submittedPresentSemaphore;
4239
private final java.util.ArrayList<Vk11DescriptorPool> descriptorPools = new java.util.ArrayList<>();
4340

4441
public Vk11CommandEncoder(final Vk11Device device) {
4542
this.device = device;
4643
this.transientMemory = new Vk11TransientMemory(device, this);
4744

48-
try (MemoryStack stack = MemoryStack.stackPush()) {
49-
VkSemaphoreCreateInfo semaphoreCreateInfo = VkSemaphoreCreateInfo.calloc(stack).sType$Default();
50-
LongBuffer semaphoreHandlePtr = stack.callocLong(1);
51-
52-
for (int i = 0; i < this.acquireSemaphores.length; i++) {
53-
Vk11Utils.crashIfFailure(
54-
VK10.vkCreateSemaphore(device.vkDevice(), semaphoreCreateInfo, null, semaphoreHandlePtr), "Failed to create acquire VkSemaphore"
55-
);
56-
this.acquireSemaphores[i] = semaphoreHandlePtr.get(0);
57-
}
58-
59-
for (int i = 0; i < this.presentSemaphores.length; i++) {
60-
Vk11Utils.crashIfFailure(
61-
VK10.vkCreateSemaphore(device.vkDevice(), semaphoreCreateInfo, null, semaphoreHandlePtr), "Failed to create present VkSemaphore"
62-
);
63-
this.presentSemaphores[i] = semaphoreHandlePtr.get(0);
64-
}
65-
}
66-
6745
for (int i = 0; i < MAX_SUBMITS_IN_FLIGHT; i++) {
6846
this.frameFences[i] = new Vk11Fence(device, true);
6947
this.commandPools[i] = new Vk11CommandPool(device, device.graphicsQueue());
@@ -85,8 +63,6 @@ public void destroy() {
8563
for (int i = 0; i < MAX_SUBMITS_IN_FLIGHT; i++) {
8664
this.commandPools[i].destroy();
8765
this.frameFences[i].destroy();
88-
VK10.vkDestroySemaphore(this.device.vkDevice(), this.acquireSemaphores[i], null);
89-
VK10.vkDestroySemaphore(this.device.vkDevice(), this.presentSemaphores[i], null);
9066
}
9167
}
9268

@@ -202,7 +178,6 @@ public void requireFencesComplete(long... fences) {
202178
public void submit() {
203179
this.endCommandBuffer();
204180
this.transientMemory.endSubmit();
205-
this.submittedPresentSemaphore = this.presentSemaphores[this.currentSubmitIndex];
206181
long lastFence = this.frameFences[this.currentSubmitIndex].vkFence();
207182

208183
requireFencesComplete(lastFence);
@@ -637,20 +612,6 @@ public long getTimestampNow() {
637612
}
638613
}
639614

640-
public long acquireSemaphore() {
641-
assert acquireSemaphores.length == MAX_SUBMITS_IN_FLIGHT;
642-
return this.acquireSemaphores[this.currentSubmitIndex];
643-
}
644-
645-
public long presentSemaphore() {
646-
assert presentSemaphores.length == MAX_SUBMITS_IN_FLIGHT;
647-
return this.presentSemaphores[this.currentSubmitIndex];
648-
}
649-
650-
public long submittedPresentSemaphore() {
651-
return this.submittedPresentSemaphore;
652-
}
653-
654615
public int currentSubmitIndex() {
655616
return this.currentSubmitIndex;
656617
}

src/main/java/git/artdeell/artvk/Vk11GpuSurface.java

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,7 @@
2121
import org.lwjgl.glfw.GLFWVulkan;
2222
import org.lwjgl.system.MemoryStack;
2323
import org.lwjgl.system.MemoryUtil;
24-
import org.lwjgl.vulkan.KHRSurface;
25-
import org.lwjgl.vulkan.KHRSwapchain;
26-
import org.lwjgl.vulkan.VK10;
27-
import org.lwjgl.vulkan.VkCommandBuffer;
28-
import org.lwjgl.vulkan.VkExtent2D;
29-
import org.lwjgl.vulkan.VkImageBlit;
30-
import org.lwjgl.vulkan.VkImageMemoryBarrier;
31-
import org.lwjgl.vulkan.VkImageSubresourceLayers;
32-
import org.lwjgl.vulkan.VkImageSubresourceRange;
33-
import org.lwjgl.vulkan.VkMemoryBarrier;
34-
import org.lwjgl.vulkan.VkOffset3D;
35-
import org.lwjgl.vulkan.VkPresentInfoKHR;
36-
import org.lwjgl.vulkan.VkQueue;
37-
import org.lwjgl.vulkan.VkSurfaceCapabilitiesKHR;
38-
import org.lwjgl.vulkan.VkSurfaceFormatKHR;
39-
import org.lwjgl.vulkan.VkSwapchainCreateInfoKHR;
24+
import org.lwjgl.vulkan.*;
4025
import org.lwjgl.vulkan.VkSurfaceFormatKHR.Buffer;
4126

4227
@Environment(EnvType.CLIENT)
@@ -50,6 +35,9 @@ public class Vk11GpuSurface implements GpuSurfaceBackend {
5035
private int swapchainWidth;
5136
private int swapchainHeight;
5237
private final LongList swapchainImages = new LongArrayList();
38+
private long[] acquireSemaphores = null;
39+
private long[] presentSemaphores = null;
40+
private int currentAcquireSemaphore = 0;
5341
private int currentImageIndex = NO_CURRENT_IMAGE;
5442
private @Nullable SurfaceException eatenException = null;
5543
private boolean swapchainSuboptimal;
@@ -143,6 +131,8 @@ private void destroySwapchain() {
143131
if (this.swapchain != 0L) {
144132
this.device.graphicsQueue().waitIdle();
145133
KHRSwapchain.vkDestroySwapchainKHR(this.device.vkDevice(), this.swapchain, null);
134+
destroySemaphores(presentSemaphores);
135+
destroySemaphores(acquireSemaphores);
146136
this.swapchain = 0L;
147137
}
148138
}
@@ -204,6 +194,12 @@ public void configure(final GpuSurface.Configuration config) throws SurfaceExcep
204194
this.swapchainImages.add(swapchainImagesPtr.get(i));
205195
}
206196

197+
acquireSemaphores = new long[swapchainImages.size()];
198+
createSemaphores(acquireSemaphores);
199+
200+
presentSemaphores = new long[swapchainImages.size()];
201+
createSemaphores(presentSemaphores);
202+
207203
this.swapchainSuboptimal = false;
208204
this.swapchainOutOfDate = false;
209205
this.swapchainWidth = config.width();
@@ -235,8 +231,8 @@ public void acquireNextTexture() throws SurfaceException {
235231
try (MemoryStack stack = MemoryStack.stackPush()) {
236232
IntBuffer frameIndexPtr = stack.callocInt(1);
237233
frameIndexPtr.put(0, NO_CURRENT_IMAGE);
238-
Vk11CommandEncoder commandEncoder = this.device.createCommandEncoder();
239-
long acquireSemaphore = commandEncoder.acquireSemaphore();
234+
currentAcquireSemaphore = (currentAcquireSemaphore + 1) % acquireSemaphores.length;
235+
long acquireSemaphore = acquireSemaphores[currentAcquireSemaphore];
240236
int result = KHRSwapchain.vkAcquireNextImageKHR(this.device.vkDevice(), this.swapchain, 50000000000L, acquireSemaphore, 0L, frameIndexPtr);
241237
if (result == VK10.VK_TIMEOUT) {
242238
throw new IllegalStateException("GPU timeout attempting to acquire next frame");
@@ -358,8 +354,8 @@ public void blitFromTexture(final @NotNull CommandEncoderBackend commandEncoder,
358354
);
359355
}
360356

361-
vk11CommandEncoder.waitSemaphore(vk11CommandEncoder.acquireSemaphore(), 0L, VK10.VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
362-
vk11CommandEncoder.signalSemaphore(vk11CommandEncoder.presentSemaphore(), 0L, VK10.VK_PIPELINE_STAGE_TRANSFER_BIT);
357+
vk11CommandEncoder.waitSemaphore(acquireSemaphores[currentAcquireSemaphore], 0L, VK10.VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
358+
vk11CommandEncoder.signalSemaphore(presentSemaphores[currentImageIndex], 0L, VK10.VK_PIPELINE_STAGE_TRANSFER_BIT);
363359
}
364360

365361
@Override
@@ -370,7 +366,7 @@ public void present() {
370366

371367
try (MemoryStack stack = MemoryStack.stackPush()) {
372368
VkPresentInfoKHR presentInfo = VkPresentInfoKHR.calloc(stack).sType$Default();
373-
presentInfo.pWaitSemaphores(stack.longs(this.device.createCommandEncoder().submittedPresentSemaphore()));
369+
presentInfo.pWaitSemaphores(stack.longs(this.presentSemaphores[this.currentImageIndex]));
374370
presentInfo.swapchainCount(1);
375371
presentInfo.pSwapchains(stack.longs(this.swapchain));
376372
presentInfo.pImageIndices(stack.ints(this.currentImageIndex));
@@ -387,4 +383,19 @@ public void present() {
387383
}
388384
}
389385
}
386+
387+
private void createSemaphores( long[] semaphores) {
388+
try (MemoryStack stack = MemoryStack.stackPush()) {
389+
VkSemaphoreCreateInfo createInfo = VkSemaphoreCreateInfo.calloc(stack).sType$Default();
390+
LongBuffer ptr = stack.callocLong(1);
391+
for(int i = 0; i < semaphores.length; i++) {
392+
Vk11Utils.crashIfFailure(VK10.vkCreateSemaphore(device.vkDevice(), createInfo, null, ptr), "Failed to create semaphore");
393+
semaphores[i] = ptr.get(0);
394+
}
395+
}
396+
}
397+
398+
private void destroySemaphores(long[] semaphores) {
399+
for(long s : semaphores) VK10.vkDestroySemaphore(device.vkDevice(), s, null);
400+
}
390401
}

0 commit comments

Comments
 (0)