Skip to content

Commit 7e65479

Browse files
committed
Fix semaphores usage
1 parent a9f4790 commit 7e65479

File tree

7 files changed

+16
-11
lines changed

7 files changed

+16
-11
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ public void loadMeshes(MeshData[] meshDataList) {
7676
}
7777

7878
public void render(Window window, Scene scene) {
79+
// TODO: MISSING SEMAPHORE TO PREVENT LIGHTING STAGE TO START
7980
if (window.isResized() || swapChain.acquireNextImage()) {
8081
window.resetResized();
8182
resize(window, scene);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class GeometryAttachments {
1111
public static final int NUMBER_ATTACHMENTS = 4;
1212
public static final int NUMBER_COLOR_ATTACHMENTS = NUMBER_ATTACHMENTS - 1;
1313
private Attachment[] attachments;
14+
private int depthAttachmentPos;
1415

1516
public GeometryAttachments(Device device, int width, int height) {
1617
attachments = new Attachment[NUMBER_ATTACHMENTS];
@@ -38,6 +39,7 @@ public GeometryAttachments(Device device, int width, int height) {
3839
attachment = new Attachment(device, width, height,
3940
VK_FORMAT_D32_SFLOAT, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
4041
attachments[i] = attachment;
42+
depthAttachmentPos = i;
4143
}
4244

4345
public void cleanup() {
@@ -49,6 +51,6 @@ public Attachment[] getAttachments() {
4951
}
5052

5153
public Attachment getDepthAttachment() {
52-
return attachments[3];
54+
return attachments[depthAttachmentPos];
5355
}
5456
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public GeometryFrameBuffer(SwapChain swapChain) {
2525
int height = extent2D.height();
2626

2727
geometryAttachments = new GeometryAttachments(device, width, height);
28-
geometryRenderPass = new GeometryRenderPass(device, geometryAttachments);
28+
geometryRenderPass = new GeometryRenderPass(device, geometryAttachments.getAttachments());
2929

3030
Attachment[] attachments = geometryAttachments.getAttachments();
3131
int numAttachments = attachments.length;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private void createCommandBuffers(CommandPool commandPool, int numImages) {
8989
private void createDescriptorPool() {
9090
EngineProperties engineProps = EngineProperties.getInstance();
9191
List<DescriptorPool.DescriptorTypeCount> descriptorTypeCounts = new ArrayList<>();
92-
descriptorTypeCounts.add(new DescriptorPool.DescriptorTypeCount(2, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER));
92+
descriptorTypeCounts.add(new DescriptorPool.DescriptorTypeCount(swapChain.getNumImages() + 1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER));
9393
descriptorTypeCounts.add(new DescriptorPool.DescriptorTypeCount(engineProps.getMaxMaterials() * 3, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER));
9494
descriptorTypeCounts.add(new DescriptorPool.DescriptorTypeCount(1, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC));
9595
descriptorPool = new DescriptorPool(device, descriptorTypeCounts);
@@ -303,7 +303,7 @@ public void submit(Queue queue) {
303303
queue.submit(stack.pointers(commandBuffer.getVkCommandBuffer()),
304304
stack.longs(syncSemaphores.imgAcquisitionSemaphore().getVkSemaphore()),
305305
stack.ints(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
306-
null, currentFence);
306+
stack.longs(syncSemaphores.geometryCompleteSemaphore().getVkSemaphore()), currentFence);
307307
}
308308
}
309309

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ public class GeometryRenderPass {
1515
private Device device;
1616
private long vkRenderPass;
1717

18-
public GeometryRenderPass(Device device, GeometryAttachments offscreenAttachments) {
18+
public GeometryRenderPass(Device device, Attachment[] attachments) {
1919
this.device = device;
2020
try (MemoryStack stack = MemoryStack.stackPush()) {
21-
Attachment[] attachments = offscreenAttachments.getAttachments();
2221
int numAttachments = attachments.length;
2322
VkAttachmentDescription.Buffer attachmentsDesc = VkAttachmentDescription.callocStack(numAttachments, stack);
2423
int depthAttachmentPos = 0;
@@ -39,7 +38,6 @@ public GeometryRenderPass(Device device, GeometryAttachments offscreenAttachment
3938
}
4039
}
4140

42-
// Create color references
4341
VkAttachmentReference.Buffer colorReferences = VkAttachmentReference.callocStack(GeometryAttachments.NUMBER_COLOR_ATTACHMENTS,
4442
stack);
4543
for (int i = 0; i < GeometryAttachments.NUMBER_COLOR_ATTACHMENTS; i++) {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ public void recordCommandBuffer(Scene scene) {
233233
}
234234

235235
public void resize(SwapChain swapChain, GeometryFrameBuffer geometryFrameBuffer, Scene scene) {
236+
this.swapChain = swapChain;
236237
lightingFrameBuffer.cleanup();
237238
pipeline.cleanup();
238239
attachmentsDescriptorSet.update(geometryFrameBuffer);
@@ -254,9 +255,10 @@ public void submit(Queue queue) {
254255
Fence currentFence = fences[idx];
255256
SwapChain.SyncSemaphores syncSemaphores = swapChain.getSyncSemaphoresList()[idx];
256257
queue.submit(stack.pointers(commandBuffer.getVkCommandBuffer()),
257-
null,
258+
stack.longs(syncSemaphores.geometryCompleteSemaphore().getVkSemaphore()),
258259
stack.ints(VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT),
259-
stack.longs(syncSemaphores.renderCompleteSemaphore().getVkSemaphore()), currentFence);
260+
stack.longs(syncSemaphores.renderCompleteSemaphore().getVkSemaphore()),
261+
currentFence);
260262
}
261263
}
262264

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public SwapChain(Device device, Surface surface, Window window, int requestedIma
6666
numImages = imageViews.length;
6767
syncSemaphoresList = new SyncSemaphores[numImages];
6868
for (int i = 0; i < numImages; i++) {
69-
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device));
69+
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device), new Semaphore(device));
7070
}
7171
currentFrame = 0;
7272
}
@@ -165,6 +165,7 @@ public void cleanup() {
165165
imageViews[i].cleanup();
166166
SyncSemaphores syncSemaphores = syncSemaphoresList[i];
167167
syncSemaphores.imgAcquisitionSemaphore().cleanup();
168+
syncSemaphores.geometryCompleteSemaphore().cleanup();
168169
syncSemaphores.renderCompleteSemaphore().cleanup();
169170
}
170171

@@ -250,6 +251,7 @@ public boolean presentImage(Queue queue) {
250251
public record SurfaceFormat(int imageFormat, int colorSpace) {
251252
}
252253

253-
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore renderCompleteSemaphore) {
254+
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore geometryCompleteSemaphore,
255+
Semaphore renderCompleteSemaphore) {
254256
}
255257
}

0 commit comments

Comments
 (0)