Skip to content

Commit dc097e9

Browse files
committed
Swapchain refactor
1 parent 237c105 commit dc097e9

File tree

11 files changed

+136
-93
lines changed

11 files changed

+136
-93
lines changed

bookcontents/chapter-04/chapter-04.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,19 @@ public class ImageView {
364364
}
365365
```
366366

367+
Going back to the `Swapchain`class we need also to create a `cleanup` method to free the resources:
368+
```java
369+
public class SwapChain {
370+
...
371+
public void cleanup() {
372+
LOGGER.debug("Destroying Vulkan SwapChain");
373+
Arrays.stream(imageViews).forEach(ImageView::cleanup);
374+
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
375+
}
376+
...
377+
}
378+
```
379+
367380
Now we can use the `Swapchain` class in our `Render`class:
368381

369382
```java

bookcontents/chapter-05/chapter-05.md

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -531,25 +531,38 @@ public class SwapChain {
531531
...
532532
syncSemaphoresList = new SyncSemaphores[numImages];
533533
for (int i = 0; i < numImages; i++) {
534-
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device));
534+
syncSemaphoresList[i] = new SyncSemaphores(device);
535535
}
536536
currentFrame = 0;
537537
...
538538
}
539539
...
540+
public void cleanup() {
541+
...
542+
Arrays.stream(syncSemaphoresList).forEach(SyncSemaphores::cleanup);
543+
...
544+
}
545+
...
540546
}
541547
```
542548

543-
We create to types of semaphores:
549+
The `SyncSemaphores` record defines to types of semaphores:
544550
- `imgAcquisitionSemaphore`: It will be used to signal image acquisition.
545551
- `renderCompleteSemaphore`: It will be used to signal that the command submitted have been completed.
546552

547-
These semaphores are stored together under a record:
548-
549553
```java
550554
public class SwapChain {
551555
...
552556
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore renderCompleteSemaphore) {
557+
558+
public SyncSemaphores(Device device) {
559+
this(new Semaphore(device), new Semaphore(device));
560+
}
561+
562+
public void cleanup() {
563+
imgAcquisitionSemaphore.cleanup();
564+
renderCompleteSemaphore.cleanup();
565+
}
553566
}
554567
...
555568
}

bookcontents/chapter-10/chapter-10.md

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -685,28 +685,19 @@ There's a subtle, but important, change in this method with respect to the simil
685685

686686
```java
687687
public class SwapChain {
688-
...
689-
public SwapChain(Device device, Surface surface, Window window, int requestedImages, boolean vsync) {
690-
...
691-
for (int i = 0; i < numImages; i++) {
692-
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device));
693-
}
694-
...
695-
}
696-
...
697-
public void cleanup() {
698-
...
699-
for (int i = 0; i < size; i++) {
700-
...
701-
syncSemaphores.geometryCompleteSemaphore().cleanup();
702-
...
703-
}
704-
...
705-
}
706688
...
707689
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore geometryCompleteSemaphore,
708690
Semaphore renderCompleteSemaphore) {
709-
}
691+
692+
public SyncSemaphores(Device device) {
693+
this(new Semaphore(device), new Semaphore(device), new Semaphore(device));
694+
}
695+
696+
public void cleanup() {
697+
imgAcquisitionSemaphore.cleanup();
698+
geometryCompleteSemaphore.cleanup();
699+
renderCompleteSemaphore.cleanup();
700+
}
710701
...
711702
}
712703
```

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.vulkanb.eng.Window;
77

88
import java.nio.*;
9+
import java.util.Arrays;
910

1011
import static org.lwjgl.vulkan.VK11.*;
1112
import static org.vulkanb.eng.graph.vk.VulkanUtils.vkCheck;
@@ -130,11 +131,7 @@ public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR su
130131

131132
public void cleanup() {
132133
LOGGER.debug("Destroying Vulkan SwapChain");
133-
int size = imageViews != null ? imageViews.length : 0;
134-
for (int i = 0; i < size; i++) {
135-
imageViews[i].cleanup();
136-
}
137-
134+
Arrays.stream(imageViews).forEach(ImageView::cleanup);
138135
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
139136
}
140137

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.vulkanb.eng.Window;
77

88
import java.nio.*;
9+
import java.util.Arrays;
910

1011
import static org.lwjgl.vulkan.VK11.*;
1112
import static org.vulkanb.eng.graph.vk.VulkanUtils.vkCheck;
@@ -66,7 +67,7 @@ public SwapChain(Device device, Surface surface, Window window, int requestedIma
6667
numImages = imageViews.length;
6768
syncSemaphoresList = new SyncSemaphores[numImages];
6869
for (int i = 0; i < numImages; i++) {
69-
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device));
70+
syncSemaphoresList[i] = new SyncSemaphores(device);
7071
}
7172
currentFrame = 0;
7273
}
@@ -159,15 +160,8 @@ public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR su
159160
public void cleanup() {
160161
LOGGER.debug("Destroying Vulkan SwapChain");
161162
swapChainExtent.free();
162-
163-
int size = imageViews != null ? imageViews.length : 0;
164-
for (int i = 0; i < size; i++) {
165-
imageViews[i].cleanup();
166-
SyncSemaphores syncSemaphores = syncSemaphoresList[i];
167-
syncSemaphores.imgAcquisitionSemaphore().cleanup();
168-
syncSemaphores.renderCompleteSemaphore().cleanup();
169-
}
170-
163+
Arrays.stream(imageViews).forEach(ImageView::cleanup);
164+
Arrays.stream(syncSemaphoresList).forEach(SyncSemaphores::cleanup);
171165
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
172166
}
173167

@@ -251,5 +245,14 @@ public record SurfaceFormat(int imageFormat, int colorSpace) {
251245
}
252246

253247
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore renderCompleteSemaphore) {
248+
249+
public SyncSemaphores(Device device) {
250+
this(new Semaphore(device), new Semaphore(device));
251+
}
252+
253+
public void cleanup() {
254+
imgAcquisitionSemaphore.cleanup();
255+
renderCompleteSemaphore.cleanup();
256+
}
254257
}
255-
}
258+
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.vulkanb.eng.Window;
77

88
import java.nio.*;
9+
import java.util.Arrays;
910

1011
import static org.lwjgl.vulkan.VK11.*;
1112
import static org.vulkanb.eng.graph.vk.VulkanUtils.vkCheck;
@@ -66,7 +67,7 @@ public SwapChain(Device device, Surface surface, Window window, int requestedIma
6667
numImages = imageViews.length;
6768
syncSemaphoresList = new SyncSemaphores[numImages];
6869
for (int i = 0; i < numImages; i++) {
69-
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device));
70+
syncSemaphoresList[i] = new SyncSemaphores(device);
7071
}
7172
currentFrame = 0;
7273
}
@@ -159,15 +160,8 @@ public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR su
159160
public void cleanup() {
160161
LOGGER.debug("Destroying Vulkan SwapChain");
161162
swapChainExtent.free();
162-
163-
int size = imageViews != null ? imageViews.length : 0;
164-
for (int i = 0; i < size; i++) {
165-
imageViews[i].cleanup();
166-
SyncSemaphores syncSemaphores = syncSemaphoresList[i];
167-
syncSemaphores.imgAcquisitionSemaphore().cleanup();
168-
syncSemaphores.renderCompleteSemaphore().cleanup();
169-
}
170-
163+
Arrays.stream(imageViews).forEach(ImageView::cleanup);
164+
Arrays.stream(syncSemaphoresList).forEach(SyncSemaphores::cleanup);
171165
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
172166
}
173167

@@ -251,5 +245,14 @@ public record SurfaceFormat(int imageFormat, int colorSpace) {
251245
}
252246

253247
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore renderCompleteSemaphore) {
248+
249+
public SyncSemaphores(Device device) {
250+
this(new Semaphore(device), new Semaphore(device));
251+
}
252+
253+
public void cleanup() {
254+
imgAcquisitionSemaphore.cleanup();
255+
renderCompleteSemaphore.cleanup();
256+
}
254257
}
255-
}
258+
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.vulkanb.eng.Window;
77

88
import java.nio.*;
9+
import java.util.Arrays;
910

1011
import static org.lwjgl.vulkan.VK11.*;
1112
import static org.vulkanb.eng.graph.vk.VulkanUtils.vkCheck;
@@ -66,7 +67,7 @@ public SwapChain(Device device, Surface surface, Window window, int requestedIma
6667
numImages = imageViews.length;
6768
syncSemaphoresList = new SyncSemaphores[numImages];
6869
for (int i = 0; i < numImages; i++) {
69-
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device));
70+
syncSemaphoresList[i] = new SyncSemaphores(device);
7071
}
7172
currentFrame = 0;
7273
}
@@ -159,15 +160,8 @@ public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR su
159160
public void cleanup() {
160161
LOGGER.debug("Destroying Vulkan SwapChain");
161162
swapChainExtent.free();
162-
163-
int size = imageViews != null ? imageViews.length : 0;
164-
for (int i = 0; i < size; i++) {
165-
imageViews[i].cleanup();
166-
SyncSemaphores syncSemaphores = syncSemaphoresList[i];
167-
syncSemaphores.imgAcquisitionSemaphore().cleanup();
168-
syncSemaphores.renderCompleteSemaphore().cleanup();
169-
}
170-
163+
Arrays.stream(imageViews).forEach(ImageView::cleanup);
164+
Arrays.stream(syncSemaphoresList).forEach(SyncSemaphores::cleanup);
171165
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
172166
}
173167

@@ -251,5 +245,14 @@ public record SurfaceFormat(int imageFormat, int colorSpace) {
251245
}
252246

253247
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore renderCompleteSemaphore) {
248+
249+
public SyncSemaphores(Device device) {
250+
this(new Semaphore(device), new Semaphore(device));
251+
}
252+
253+
public void cleanup() {
254+
imgAcquisitionSemaphore.cleanup();
255+
renderCompleteSemaphore.cleanup();
256+
}
254257
}
255-
}
258+
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.vulkanb.eng.Window;
77

88
import java.nio.*;
9+
import java.util.Arrays;
910

1011
import static org.lwjgl.vulkan.VK11.*;
1112
import static org.vulkanb.eng.graph.vk.VulkanUtils.vkCheck;
@@ -66,7 +67,7 @@ public SwapChain(Device device, Surface surface, Window window, int requestedIma
6667
numImages = imageViews.length;
6768
syncSemaphoresList = new SyncSemaphores[numImages];
6869
for (int i = 0; i < numImages; i++) {
69-
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device));
70+
syncSemaphoresList[i] = new SyncSemaphores(device);
7071
}
7172
currentFrame = 0;
7273
}
@@ -159,15 +160,8 @@ public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR su
159160
public void cleanup() {
160161
LOGGER.debug("Destroying Vulkan SwapChain");
161162
swapChainExtent.free();
162-
163-
int size = imageViews != null ? imageViews.length : 0;
164-
for (int i = 0; i < size; i++) {
165-
imageViews[i].cleanup();
166-
SyncSemaphores syncSemaphores = syncSemaphoresList[i];
167-
syncSemaphores.imgAcquisitionSemaphore().cleanup();
168-
syncSemaphores.renderCompleteSemaphore().cleanup();
169-
}
170-
163+
Arrays.stream(imageViews).forEach(ImageView::cleanup);
164+
Arrays.stream(syncSemaphoresList).forEach(SyncSemaphores::cleanup);
171165
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
172166
}
173167

@@ -251,5 +245,14 @@ public record SurfaceFormat(int imageFormat, int colorSpace) {
251245
}
252246

253247
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore renderCompleteSemaphore) {
248+
249+
public SyncSemaphores(Device device) {
250+
this(new Semaphore(device), new Semaphore(device));
251+
}
252+
253+
public void cleanup() {
254+
imgAcquisitionSemaphore.cleanup();
255+
renderCompleteSemaphore.cleanup();
256+
}
254257
}
255-
}
258+
}

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.vulkanb.eng.Window;
77

88
import java.nio.*;
9+
import java.util.Arrays;
910

1011
import static org.lwjgl.vulkan.VK11.*;
1112
import static org.vulkanb.eng.graph.vk.VulkanUtils.vkCheck;
@@ -66,7 +67,7 @@ public SwapChain(Device device, Surface surface, Window window, int requestedIma
6667
numImages = imageViews.length;
6768
syncSemaphoresList = new SyncSemaphores[numImages];
6869
for (int i = 0; i < numImages; i++) {
69-
syncSemaphoresList[i] = new SyncSemaphores(new Semaphore(device), new Semaphore(device));
70+
syncSemaphoresList[i] = new SyncSemaphores(device);
7071
}
7172
currentFrame = 0;
7273
}
@@ -159,15 +160,8 @@ public VkExtent2D calcSwapChainExtent(Window window, VkSurfaceCapabilitiesKHR su
159160
public void cleanup() {
160161
LOGGER.debug("Destroying Vulkan SwapChain");
161162
swapChainExtent.free();
162-
163-
int size = imageViews != null ? imageViews.length : 0;
164-
for (int i = 0; i < size; i++) {
165-
imageViews[i].cleanup();
166-
SyncSemaphores syncSemaphores = syncSemaphoresList[i];
167-
syncSemaphores.imgAcquisitionSemaphore().cleanup();
168-
syncSemaphores.renderCompleteSemaphore().cleanup();
169-
}
170-
163+
Arrays.stream(imageViews).forEach(ImageView::cleanup);
164+
Arrays.stream(syncSemaphoresList).forEach(SyncSemaphores::cleanup);
171165
KHRSwapchain.vkDestroySwapchainKHR(device.getVkDevice(), vkSwapChain, null);
172166
}
173167

@@ -251,5 +245,14 @@ public record SurfaceFormat(int imageFormat, int colorSpace) {
251245
}
252246

253247
public record SyncSemaphores(Semaphore imgAcquisitionSemaphore, Semaphore renderCompleteSemaphore) {
248+
249+
public SyncSemaphores(Device device) {
250+
this(new Semaphore(device), new Semaphore(device));
251+
}
252+
253+
public void cleanup() {
254+
imgAcquisitionSemaphore.cleanup();
255+
renderCompleteSemaphore.cleanup();
256+
}
254257
}
255-
}
258+
}

0 commit comments

Comments
 (0)