@@ -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