@@ -86,7 +86,7 @@ public static int getCurrentImage() {
8686 private SwapChain swapChain ;
8787
8888 private int framesNum ;
89- private List <VkCommandBuffer > commandBuffers ;
89+ private List <VkCommandBuffer > mainCommandBuffers ;
9090 private ArrayList <Long > imageAvailableSemaphores ;
9191 private ArrayList <Long > renderFinishedSemaphores ;
9292 private ArrayList <Long > inFlightFences ;
@@ -100,6 +100,7 @@ public static int getCurrentImage() {
100100 private static int lastReset = -1 ;
101101 private VkCommandBuffer currentCmdBuffer ;
102102 private boolean recordingCmds = false ;
103+ int recursion = 0 ;
103104
104105 MainPass mainPass ;
105106
@@ -136,11 +137,11 @@ private void init() {
136137 }
137138
138139 private void allocateCommandBuffers () {
139- if (commandBuffers != null ) {
140- commandBuffers .forEach (commandBuffer -> vkFreeCommandBuffers (device , Vulkan .getCommandPool (), commandBuffer ));
140+ if (mainCommandBuffers != null ) {
141+ mainCommandBuffers .forEach (commandBuffer -> vkFreeCommandBuffers (device , Vulkan .getCommandPool (), commandBuffer ));
141142 }
142143
143- commandBuffers = new ArrayList <>(framesNum );
144+ mainCommandBuffers = new ArrayList <>(framesNum );
144145
145146 try (MemoryStack stack = stackPush ()) {
146147 VkCommandBufferAllocateInfo allocInfo = VkCommandBufferAllocateInfo .calloc (stack );
@@ -157,7 +158,7 @@ private void allocateCommandBuffers() {
157158 }
158159
159160 for (int i = 0 ; i < framesNum ; i ++) {
160- commandBuffers .add (new VkCommandBuffer (pCommandBuffers .get (i ), device ));
161+ mainCommandBuffers .add (new VkCommandBuffer (pCommandBuffers .get (i ), device ));
161162 }
162163 }
163164
@@ -232,10 +233,6 @@ public void preInitFrame() {
232233 }
233234
234235 public void beginFrame () {
235- Profiler p = Profiler .getMainProfiler ();
236- p .pop ();
237- p .push ("Frame_fence" );
238-
239236 if (swapChainUpdate ) {
240237 recreateSwapChain ();
241238 swapChainUpdate = false ;
@@ -249,9 +246,22 @@ public void beginFrame() {
249246 }
250247 }
251248
252-
253- if (skipRendering || recordingCmds )
249+ if (skipRendering ) {
254250 return ;
251+ }
252+
253+ this .recursion ++;
254+
255+ // In case this is a recursive call end prev frame
256+ if (this .recursion > 1 ) {
257+ this .endFrame ();
258+ }
259+
260+ this .preInitFrame ();
261+
262+ Profiler p = Profiler .getMainProfiler ();
263+ p .pop ();
264+ p .push ("Frame_fence" );
255265
256266 vkWaitForFences (device , inFlightFences .get (currentFrame ), true , VUtil .UINT64_MAX );
257267
@@ -263,11 +273,10 @@ public void beginFrame() {
263273
264274 resetDescriptors ();
265275
266- currentCmdBuffer = commandBuffers .get (currentFrame );
276+ currentCmdBuffer = mainCommandBuffers .get (currentFrame );
267277 vkResetCommandBuffer (currentCmdBuffer , 0 );
268278
269279 try (MemoryStack stack = stackPush ()) {
270-
271280 IntBuffer pImageIndex = stack .mallocInt (1 );
272281
273282 int vkResult = vkAcquireNextImageKHR (device , swapChain .getId (), VUtil .UINT64_MAX ,
@@ -276,7 +285,7 @@ public void beginFrame() {
276285 if (vkResult == VK_SUBOPTIMAL_KHR || vkResult == VK_ERROR_OUT_OF_DATE_KHR || swapChainUpdate ) {
277286 swapChainUpdate = true ;
278287 skipRendering = true ;
279- beginFrame ();
288+ this . beginFrame ();
280289
281290 return ;
282291 } else if (vkResult != VK_SUCCESS ) {
@@ -285,13 +294,13 @@ public void beginFrame() {
285294
286295 imageIndex = pImageIndex .get (0 );
287296
288- this .beginRenderPass (stack );
297+ this .beginMainRenderPass (stack );
289298 }
290299
291300 p .pop ();
292301 }
293302
294- private void beginRenderPass (MemoryStack stack ) {
303+ private void beginMainRenderPass (MemoryStack stack ) {
295304 VkCommandBufferBeginInfo beginInfo = VkCommandBufferBeginInfo .calloc (stack );
296305 beginInfo .sType (VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO );
297306 beginInfo .flags (VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT );
@@ -313,6 +322,11 @@ public void endFrame() {
313322 if (skipRendering || !recordingCmds )
314323 return ;
315324
325+ if (this .recursion == 0 ) {
326+ return ;
327+ }
328+ this .recursion --;
329+
316330 Profiler p = Profiler .getMainProfiler ();
317331 p .push ("End_rendering" );
318332
@@ -418,7 +432,7 @@ public void flushCmds() {
418432
419433 vkWaitForFences (device , inFlightFences .get (currentFrame ), true , VUtil .UINT64_MAX );
420434
421- this .beginRenderPass (stack );
435+ this .beginMainRenderPass (stack );
422436 }
423437 }
424438
@@ -455,10 +469,17 @@ public void endRenderPass(VkCommandBuffer commandBuffer) {
455469 VkGlFramebuffer .resetBoundFramebuffer ();
456470 }
457471
458- public boolean beginRendering (RenderPass renderPass , Framebuffer framebuffer ) {
459- if (skipRendering || !recordingCmds )
472+ public boolean beginRenderPass (RenderPass renderPass , Framebuffer framebuffer ) {
473+ // TODO: minimizing could trigger this preventing rendering (e.g. texture atlas uploads)
474+ if (skipRendering )
460475 return false ;
461476
477+ if (!recordingCmds ) {
478+ this .beginFrame ();
479+
480+ recordingCmds = true ;
481+ }
482+
462483 if (this .boundFramebuffer != framebuffer ) {
463484 this .endRenderPass (currentCmdBuffer );
464485
@@ -467,7 +488,12 @@ public boolean beginRendering(RenderPass renderPass, Framebuffer framebuffer) {
467488 }
468489
469490 this .boundFramebuffer = framebuffer ;
491+ this .boundRenderPass = renderPass ;
492+
493+ Renderer .setViewportState (0 , 0 , framebuffer .getWidth (), framebuffer .getHeight ());
494+ Renderer .setScissor (0 , 0 , framebuffer .getWidth (), framebuffer .getHeight ());
470495 }
496+
471497 return true ;
472498 }
473499
@@ -517,7 +543,7 @@ private void recreateSwapChain() {
517543 waitFences ();
518544 Vulkan .waitIdle ();
519545
520- commandBuffers .forEach (commandBuffer -> vkResetCommandBuffer (commandBuffer , 0 ));
546+ mainCommandBuffers .forEach (commandBuffer -> vkResetCommandBuffer (commandBuffer , 0 ));
521547 recordingCmds = false ;
522548
523549 swapChain .recreate ();
@@ -671,11 +697,11 @@ public static void clearAttachments(VkCommandBuffer commandBuffer, int attachmen
671697 }
672698
673699 public static void clearAttachments (int attachments , int width , int height ) {
674- clearAttachments (INSTANCE .currentCmdBuffer , attachments , width ,height );
700+ clearAttachments (INSTANCE .currentCmdBuffer , attachments , width , height );
675701 }
676702
677703 public static void clearAttachments (int attachments , int x , int y , int width , int height ) {
678- clearAttachments (INSTANCE .currentCmdBuffer , attachments , x , y , width ,height );
704+ clearAttachments (INSTANCE .currentCmdBuffer , attachments , x , y , width , height );
679705 }
680706
681707 public static void clearAttachments (VkCommandBuffer commandBuffer , int attachments , int width , int height ) {
0 commit comments