Skip to content

Commit a9f4790

Browse files
committed
Changed synchronization
1 parent 3a2eb77 commit a9f4790

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

booksamples/chapter-10/src/main/java/org/vulkanb/eng/graph/geometry/GeometryRenderActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private void createDescriptorSets(int numImages) {
117117

118118
viewMatricesDescriptorSets = new MatrixDescriptorSet[numImages];
119119
viewMatricesBuffer = new VulkanBuffer[numImages];
120-
materialsBuffer = new VulkanBuffer(device, materialDescriptorSetLayout.getMaterialSize() * engineProps.getMaxMaterials(), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
120+
materialsBuffer = new VulkanBuffer(device, (long) materialDescriptorSetLayout.getMaterialSize() * engineProps.getMaxMaterials(), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
121121
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
122122
materialsDescriptorSet = new MaterialDescriptorSet(descriptorPool, materialDescriptorSetLayout,
123123
materialsBuffer, 0);
@@ -299,11 +299,11 @@ public void submit(Queue queue) {
299299
int idx = swapChain.getCurrentFrame();
300300
CommandBuffer commandBuffer = commandBuffers[idx];
301301
Fence currentFence = fences[idx];
302-
SwapChain.SyncSemaphores syncSemaphores = swapChain.getSyncSemaphoresList();
302+
SwapChain.SyncSemaphores syncSemaphores = swapChain.getSyncSemaphoresList()[idx];
303303
queue.submit(stack.pointers(commandBuffer.getVkCommandBuffer()),
304304
stack.longs(syncSemaphores.imgAcquisitionSemaphore().getVkSemaphore()),
305305
stack.ints(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
306-
stack.longs(syncSemaphores.geometryCompleteSemaphore().getVkSemaphore()), currentFence);
306+
null, currentFence);
307307
}
308308
}
309309

booksamples/chapter-10/src/main/java/org/vulkanb/eng/graph/lighting/LightingRenderActivity.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,11 @@ public void submit(Queue queue) {
252252
int idx = swapChain.getCurrentFrame();
253253
CommandBuffer commandBuffer = commandBuffers[idx];
254254
Fence currentFence = fences[idx];
255-
SwapChain.SyncSemaphores syncSemaphores = swapChain.getSyncSemaphoresList();
255+
SwapChain.SyncSemaphores syncSemaphores = swapChain.getSyncSemaphoresList()[idx];
256256
queue.submit(stack.pointers(commandBuffer.getVkCommandBuffer()),
257-
stack.longs(syncSemaphores.geometryCompleteSemaphore().getVkSemaphore()),
257+
null,
258258
stack.ints(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
259-
stack.longs(syncSemaphores.lightingCompleteSemaphore().getVkSemaphore()), currentFence);
259+
stack.longs(syncSemaphores.renderCompleteSemaphore().getVkSemaphore()), currentFence);
260260
}
261261
}
262262

booksamples/chapter-10/src/main/java/org/vulkanb/eng/graph/vk/SwapChain.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class SwapChain {
1818
private ImageView[] imageViews;
1919
private SurfaceFormat surfaceFormat;
2020
private VkExtent2D swapChainExtent;
21-
private SyncSemaphores syncSemaphoresList;
21+
private SyncSemaphores[] syncSemaphoresList;
2222
private long vkSwapChain;
2323

2424
public SwapChain(Device device, Surface surface, Window window, int requestedImages, boolean vsync) {
@@ -63,7 +63,11 @@ public SwapChain(Device device, Surface surface, Window window, int requestedIma
6363
vkSwapChain = lp.get(0);
6464

6565
imageViews = createImageViews(stack, device, vkSwapChain, surfaceFormat.imageFormat);
66-
syncSemaphoresList = new SyncSemaphores(new Semaphore(device), new Semaphore(device), new Semaphore(device));
66+
numImages = imageViews.length;
67+
syncSemaphoresList = new SyncSemaphores[numImages];
68+
for (int i = 0; i < numImages; i++) {
69+
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device));
70+
}
6771
currentFrame = 0;
6872
}
6973
}
@@ -73,7 +77,7 @@ public boolean acquireNextImage() {
7377
try (MemoryStack stack = MemoryStack.stackPush()) {
7478
IntBuffer ip = stack.mallocInt(1);
7579
int err = KHRSwapchain.vkAcquireNextImageKHR(device.getVkDevice(), vkSwapChain, ~0L,
76-
syncSemaphoresList.imgAcquisitionSemaphore().getVkSemaphore(), MemoryUtil.NULL, ip);
80+
syncSemaphoresList[currentFrame].imgAcquisitionSemaphore().getVkSemaphore(), MemoryUtil.NULL, ip);
7781
if (err == KHRSwapchain.VK_ERROR_OUT_OF_DATE_KHR) {
7882
resize = true;
7983
} else if (err == KHRSwapchain.VK_SUBOPTIMAL_KHR) {
@@ -159,8 +163,10 @@ public void cleanup() {
159163
int size = imageViews != null ? imageViews.length : 0;
160164
for (int i = 0; i < size; i++) {
161165
imageViews[i].cleanup();
166+
SyncSemaphores syncSemaphores = syncSemaphoresList[i];
167+
syncSemaphores.imgAcquisitionSemaphore().cleanup();
168+
syncSemaphores.renderCompleteSemaphore().cleanup();
162169
}
163-
syncSemaphoresList.cleanup();
164170

165171
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
166172
}
@@ -209,7 +215,7 @@ public VkExtent2D getSwapChainExtent() {
209215
return swapChainExtent;
210216
}
211217

212-
public SyncSemaphores getSyncSemaphoresList() {
218+
public SyncSemaphores[] getSyncSemaphoresList() {
213219
return syncSemaphoresList;
214220
}
215221

@@ -223,7 +229,7 @@ public boolean presentImage(Queue queue) {
223229
VkPresentInfoKHR present = VkPresentInfoKHR.callocStack(stack)
224230
.sType(KHRSwapchain.VK_STRUCTURE_TYPE_PRESENT_INFO_KHR)
225231
.pWaitSemaphores(stack.longs(
226-
syncSemaphoresList.lightingCompleteSemaphore().getVkSemaphore()))
232+
syncSemaphoresList[currentFrame].renderCompleteSemaphore().getVkSemaphore()))
227233
.swapchainCount(1)
228234
.pSwapchains(stack.longs(vkSwapChain))
229235
.pImageIndices(stack.ints(currentFrame));
@@ -244,12 +250,6 @@ public boolean presentImage(Queue queue) {
244250
public record SurfaceFormat(int imageFormat, int colorSpace) {
245251
}
246252

247-
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore geometryCompleteSemaphore,
248-
Semaphore lightingCompleteSemaphore) {
249-
public void cleanup() {
250-
imgAcquisitionSemaphore().cleanup();
251-
geometryCompleteSemaphore().cleanup();
252-
lightingCompleteSemaphore().cleanup();
253-
}
253+
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore renderCompleteSemaphore) {
254254
}
255255
}

0 commit comments

Comments
 (0)