@@ -139,7 +139,7 @@ public void signalSemaphore(final long vkSemaphore) {
139139 this .submissionBuilder .signalSemaphore (vkSemaphore );
140140 }
141141
142- private void memoryBarrier (final MemoryStack stack ) {
142+ public void memoryBarrier (final MemoryStack stack ) {
143143 memoryBarrier (this .commandBuffer (), stack );
144144 }
145145
@@ -196,6 +196,7 @@ public void submit() {
196196
197197 boolean hasDepth = depthAttachment != null ;
198198 Vk11GpuTextureView [] attachmentViews = new Vk11GpuTextureView [colorCount + (hasDepth ? 1 : 0 )];
199+ boolean [] clears = new boolean [attachmentViews .length ];
199200
200201 VkClearValue .Buffer clearValues = VkClearValue .calloc (attachmentViews .length , memoryStack );
201202 int [] colorFormats = new int [colorCount ];
@@ -212,15 +213,29 @@ public void submit() {
212213 textureView .disableTransferMode (memoryStack , currentCommandBuffer );
213214 attachmentViews [i ] = textureView ;
214215 colorFormats [i ] = Vk11Const .toVk (textureView .texture ().getFormat ());
216+ float [] pendingClear = textureView .texture ().consumeClear ();
215217 if (attachment .clearValue ().isPresent ()) {
216218 Vk11Utils .putArgb (clearValues .get (i ).color (), attachment .clearValue ().get ());
219+ clears [i ] = true ;
220+ }else if (pendingClear != null ) {
221+ VkClearColorValue clearColorValue = clearValues .get (i ).color ();
222+ clearColorValue .float32 (0 , pendingClear [0 ]);
223+ clearColorValue .float32 (1 , pendingClear [1 ]);
224+ clearColorValue .float32 (2 , pendingClear [2 ]);
225+ clearColorValue .float32 (3 , pendingClear [3 ]);
226+ clears [i ] = true ;
217227 }
218228 }
219229 if (hasDepth ) {
220230 attachmentViews [colorCount ] = (Vk11GpuTextureView ) depthAttachment .textureView ();
221231 depthFormat = Vk11Const .toVk (depthAttachment .textureView ().texture ().getFormat ());
232+ float [] pendingClearValue = attachmentViews [colorCount ].texture ().consumeClear ();
222233 if (depthAttachment .clearValue ().isPresent ()) {
223234 clearValues .get (colorCount ).depthStencil ().depth ((float ) depthAttachment .clearValue ().getAsDouble ());
235+ clears [colorCount ] = true ;
236+ } else if (pendingClearValue != null ) {
237+ clearValues .get (colorCount ).depthStencil ().set (pendingClearValue [0 ], 0 );
238+ clears [colorCount ] = true ;
224239 }
225240 }
226241
@@ -234,7 +249,7 @@ public void submit() {
234249 break ;
235250 }
236251
237- long renderPass = this .device .renderPassCache ().getOrCreateRenderPass (colorFormats , hasDepth , depthFormat );
252+ long renderPass = this .device .renderPassCache ().getOrCreateRenderPass (clears , colorFormats , hasDepth , depthFormat );
238253
239254 long framebuffer = this .device .framebufferCache ().getOrCreateFramebuffer (renderPass , width , height , attachmentViews );
240255
@@ -284,36 +299,9 @@ public void submitRenderPass() {
284299 submit ();
285300 }
286301 }
287-
288- private void clearColorTextureUnsynced (final MemoryStack stack , final GpuTexture colorTexture , final Vector4fc clearColor ) {
289- org .lwjgl .vulkan .VkClearColorValue vkClearColor = Vk11Utils .putArgb (org .lwjgl .vulkan .VkClearColorValue .calloc (stack ), clearColor );
290- VkImageSubresourceRange subresourceRange = VkImageSubresourceRange .calloc (stack );
291- subresourceRange .baseMipLevel (0 );
292- subresourceRange .levelCount (colorTexture .getMipLevels ());
293- subresourceRange .baseArrayLayer (0 );
294- subresourceRange .layerCount (1 );
295- subresourceRange .aspectMask (VK10 .VK_IMAGE_ASPECT_COLOR_BIT );
296- VK10 .vkCmdClearColorImage (this .commandBuffer (), ((Vk11GpuTexture )colorTexture ).vkImage (), VK10 .VK_IMAGE_LAYOUT_GENERAL , vkClearColor , subresourceRange );
297- }
298-
299- public void clearDepthTextureUnsynced (final MemoryStack stack , final GpuTexture depthTexture , final double clearDepth ) {
300- VkClearDepthStencilValue vkClearDepth = VkClearDepthStencilValue .calloc (stack ).depth ((float )clearDepth );
301- VkImageSubresourceRange subresourceRange = VkImageSubresourceRange .calloc (stack );
302- subresourceRange .baseMipLevel (0 );
303- subresourceRange .levelCount (depthTexture .getMipLevels ());
304- subresourceRange .baseArrayLayer (0 );
305- subresourceRange .layerCount (1 );
306- subresourceRange .aspectMask (VK10 .VK_IMAGE_ASPECT_DEPTH_BIT );
307- VK10 .vkCmdClearDepthStencilImage (this .commandBuffer (), ((Vk11GpuTexture )depthTexture ).vkImage (), VK10 .VK_IMAGE_LAYOUT_GENERAL , vkClearDepth , subresourceRange );
308- }
309-
310302 @ Override
311303 public void clearColorTexture (final @ NotNull GpuTexture colorTexture , final @ NotNull Vector4fc clearColor ) {
312- try (MemoryStack stack = MemoryStack .stackPush ()) {
313- ((Vk11GpuTexture ) colorTexture ).postTransferBarrier (stack , this .commandBuffer ());
314- this .clearColorTextureUnsynced (stack , colorTexture , clearColor );
315- this .memoryBarrier (stack );
316- }
304+ ((Vk11GpuTexture ) colorTexture ).insertClear (clearColor .x (), clearColor .y (), clearColor .z (), clearColor .w ());
317305 }
318306
319307 @ Override
@@ -323,12 +311,8 @@ public void clearColorAndDepthTextures(
323311 final @ NotNull GpuTexture depthTexture ,
324312 final double clearDepth
325313 ) {
326- try (MemoryStack stack = MemoryStack .stackPush ()) {
327- ((Vk11GpuTexture ) colorTexture ).postTransferBarrier (stack , this .commandBuffer ());
328- this .clearColorTextureUnsynced (stack , colorTexture , clearColor );
329- this .clearDepthTextureUnsynced (stack , depthTexture , clearDepth );
330- this .memoryBarrier (stack );
331- }
314+ ((Vk11GpuTexture ) colorTexture ).insertClear (clearColor .x (), clearColor .y (), clearColor .z (), clearColor .w ());
315+ ((Vk11GpuTexture ) depthTexture ).insertClear ((float ) clearDepth );
332316 }
333317
334318 @ Override
@@ -380,10 +364,7 @@ public void clearColorAndDepthTextures(
380364
381365 @ Override
382366 public void clearDepthTexture (final @ NotNull GpuTexture depthTexture , final double clearDepth ) {
383- try (MemoryStack stack = MemoryStack .stackPush ()) {
384- this .clearDepthTextureUnsynced (stack , depthTexture , clearDepth );
385- this .memoryBarrier (stack );
386- }
367+ ((Vk11GpuTexture ) depthTexture ).insertClear ((float ) clearDepth );
387368 }
388369
389370 @ Override
@@ -425,8 +406,10 @@ public void writeToTexture(
425406 final int height
426407 ) {
427408 GpuBufferSlice stagingBuffer = this .transientMemory .uploadStaging (source , 1L , 16 );
428-
409+ Vk11GpuTexture vTex = ( Vk11GpuTexture ) destination ;
429410 try (MemoryStack stack = MemoryStack .stackPush ()) {
411+ vTex .postTransferBarrier (stack , commandBuffer ());
412+ vTex .insertPendingClear (stack , this );
430413 VkBufferImageCopy .Buffer region = VkBufferImageCopy .calloc (1 , stack );
431414 region .bufferOffset (stagingBuffer .offset ());
432415 region .bufferRowLength (width );
@@ -438,7 +421,7 @@ public void writeToTexture(
438421 imageSubresource .layerCount (1 );
439422 region .imageOffset ().set (destX , destY , 0 );
440423 region .imageExtent ().set (width , height , 1 );
441- VK10 .vkCmdCopyBufferToImage (this .commandBuffer (), ((Vk11GpuBuffer )stagingBuffer .buffer ()).vkBuffer (), (( Vk11GpuTexture ) destination ) .vkImage (), VK10 .VK_IMAGE_LAYOUT_GENERAL , region );
424+ VK10 .vkCmdCopyBufferToImage (this .commandBuffer (), ((Vk11GpuBuffer )stagingBuffer .buffer ()).vkBuffer (), vTex .vkImage (), VK10 .VK_IMAGE_LAYOUT_GENERAL , region );
442425 this .memoryBarrier (stack );
443426 }
444427 }
@@ -461,8 +444,11 @@ public void copyBufferToTexture(
461444 int texelSize = destination .getFormat ().blockSize ();
462445 long skipTexels = sourceX + (long )sourceY * sourceWidth ;
463446 long skipBytes = skipTexels * texelSize ;
447+ Vk11GpuTexture vTex = (Vk11GpuTexture ) destination ;
464448
465449 try (MemoryStack stack = MemoryStack .stackPush ()) {
450+ vTex .postTransferBarrier (stack , commandBuffer ());
451+ vTex .insertPendingClear (stack , this );
466452 VkBufferImageCopy .Buffer region = VkBufferImageCopy .calloc (1 , stack );
467453 region .bufferOffset (source .offset () + skipBytes );
468454 region .bufferRowLength (sourceWidth );
@@ -474,7 +460,7 @@ public void copyBufferToTexture(
474460 imageSubresource .layerCount (1 );
475461 region .imageOffset ().set (destinationX , destinationY , 0 );
476462 region .imageExtent ().set (copyWidth , copyHeight , 1 );
477- VK10 .vkCmdCopyBufferToImage (this .commandBuffer (), ((Vk11GpuBuffer )source .buffer ()).vkBuffer (), (( Vk11GpuTexture ) destination ) .vkImage (), VK10 .VK_IMAGE_LAYOUT_GENERAL , region );
463+ VK10 .vkCmdCopyBufferToImage (this .commandBuffer (), ((Vk11GpuBuffer )source .buffer ()).vkBuffer (), vTex .vkImage (), VK10 .VK_IMAGE_LAYOUT_GENERAL , region );
478464 this .memoryBarrier (stack );
479465 }
480466 }
@@ -537,6 +523,7 @@ public void copyTextureToTexture(
537523 Vk11GpuTexture vk11Dst = (Vk11GpuTexture )destination ;
538524
539525 try (MemoryStack stack = MemoryStack .stackPush ()) {
526+ vk11Dst .insertPendingClear (stack , this );
540527 VkImageSubresourceLayers subresourceLayers = VkImageSubresourceLayers .calloc (stack );
541528 subresourceLayers .mipLevel (mipLevel );
542529 subresourceLayers .baseArrayLayer (0 );
0 commit comments