@@ -228,106 +228,74 @@ public void submit() {
228228 return this .transientMemory ;
229229 }
230230
231- @ Override
232- public @ NotNull RenderPassBackend createRenderPass (final RenderPassDescriptor descriptor ) {
233-
234- List <RenderPassDescriptor .Attachment <@ NotNull Optional <Vector4fc >>> colorAttachments = descriptor .colorAttachments ();
235- Vk11GpuTextureView [] colorTextures = new Vk11GpuTextureView [colorAttachments .size ()];
236-
237- MemoryStack transStack = MemoryStack .stackGet ();
238- for (int i = 0 ; i < colorAttachments .size (); i ++) {
239- RenderPassDescriptor .Attachment <@ NotNull Optional <Vector4fc >> attachment = colorAttachments .get (i );
240- if (attachment == null ) {
241- colorTextures [i ] = null ;
242- continue ;
231+ @ Override
232+ public @ NotNull RenderPassBackend createRenderPass (final RenderPassDescriptor descriptor ) {
233+ try (MemoryStack memoryStack = MemoryStack .stackPush ()) {
234+ List <RenderPassDescriptor .Attachment <@ NotNull Optional <Vector4fc >>> colorAttachments = descriptor .colorAttachments ();
235+ RenderPassDescriptor .Attachment <@ NotNull OptionalDouble > depthAttachment = descriptor .depthAttachment ();
236+ int colorCount = colorAttachments .size ();
237+
238+ boolean hasDepth = depthAttachment != null ;
239+ Vk11GpuTextureView [] attachmentViews = new Vk11GpuTextureView [colorCount + (hasDepth ? 1 : 0 )];
240+
241+ VkClearValue .Buffer clearValues = VkClearValue .calloc (attachmentViews .length , memoryStack );
242+ int [] colorFormats = new int [colorCount ];
243+ int depthFormat = VK10 .VK_FORMAT_UNDEFINED ;
244+
245+ for (int i = 0 ; i < colorCount ; i ++) {
246+ RenderPassDescriptor .Attachment <@ NotNull Optional <Vector4fc >> attachment = colorAttachments .get (i );
247+ if (attachment == null ) {
248+ attachmentViews [i ] = null ;
249+ colorFormats [i ] = VK10 .VK_FORMAT_UNDEFINED ;
250+ continue ;
251+ }
252+ Vk11GpuTextureView textureView = (Vk11GpuTextureView )attachment .textureView ();
253+ textureView .disableTransferMode (memoryStack , currentCommandBuffer );
254+ attachmentViews [i ] = textureView ;
255+ colorFormats [i ] = Vk11Const .toVk (textureView .texture ().getFormat ());
256+ if (attachment .clearValue ().isPresent ()) {
257+ Vk11Utils .putArgb (clearValues .get (i ).color (), attachment .clearValue ().get ());
258+ }
259+ }
260+ if (hasDepth ) {
261+ attachmentViews [colorCount ] = (Vk11GpuTextureView ) depthAttachment .textureView ();
262+ depthFormat = Vk11Const .toVk (depthAttachment .textureView ().texture ().getFormat ());
263+ if (depthAttachment .clearValue ().isPresent ()) {
264+ clearValues .get (colorCount ).depthStencil ().depth ((float ) depthAttachment .clearValue ().getAsDouble ());
265+ }
243266 }
244- Vk11GpuTextureView textureView = (Vk11GpuTextureView )attachment .textureView ();
245- textureView .disableTransferMode (transStack , currentCommandBuffer );
246- colorTextures [i ] = textureView ;
247- }
248-
249- RenderPassDescriptor .Attachment <@ NotNull OptionalDouble > depthAttachment = descriptor .depthAttachment ();
250- this .device .instance ().debug ().beginDebugGroup (this .commandBuffer (), descriptor .label ());
251-
252- int width = 0 ;
253- int height = 0 ;
254- if (!colorAttachments .isEmpty ()) {
255- for (RenderPassDescriptor .Attachment <@ NotNull Optional <Vector4fc >> colorAttachment : colorAttachments ) {
256- if (colorAttachment != null ) {
257- GpuTextureView colorTexture = colorAttachment .textureView ();
258- width = colorTexture .getWidth (0 );
259- height = colorTexture .getHeight (0 );
260- }
261- }
262- } else if (depthAttachment != null ) {
263- width = depthAttachment .textureView ().getWidth (0 );
264- height = depthAttachment .textureView ().getHeight (0 );
265- }
266-
267- try (MemoryStack stack = MemoryStack .stackPush ()) {
268- // Build color formats list
269- List <Integer > colorFormats = new java .util .ArrayList <>();
270- for (Vk11GpuTextureView cv : colorTextures ) {
271- if (cv != null ) {
272- colorFormats .add (Vk11Const .toVk (cv .texture ().getFormat ()));
273- } else {
274- colorFormats .add (VK10 .VK_FORMAT_UNDEFINED );
275- }
276- }
277267
278- boolean hasDepth = depthAttachment != null ;
279- int depthFormat = hasDepth ? Vk11Const .toVk (depthAttachment .textureView ().texture ().getFormat ()) : VK10 .VK_FORMAT_UNDEFINED ;
268+ this .device .instance ().debug ().beginDebugGroup (this .commandBuffer (), descriptor .label ());
280269
281- long renderPass = this .device .renderPassCache ().getOrCreateRenderPass (colorFormats , hasDepth , depthFormat );
270+ int width = 0 , height = 0 ;
271+ for (Vk11GpuTextureView view : attachmentViews ) {
272+ if (view == null ) continue ;
273+ width = view .getWidth (0 );
274+ height = view .getHeight (0 );
275+ break ;
276+ }
282277
283- // Build image views array for framebuffer
284- int viewCount = colorTextures .length + (hasDepth ? 1 : 0 );
285- long [] imageViews = new long [viewCount ];
286- for (int i = 0 ; i < colorTextures .length ; i ++) {
287- imageViews [i ] = colorTextures [i ] != null ? colorTextures [i ].vkImageView () : 0L ;
288- }
289- if (hasDepth ) {
290- imageViews [colorTextures .length ] = ((Vk11GpuTextureView )depthAttachment .textureView ()).vkImageView ();
291- }
278+ long renderPass = this .device .renderPassCache ().getOrCreateRenderPass (colorFormats , hasDepth , depthFormat );
292279
293- long framebuffer = this .device .renderPassCache ().getOrCreateFramebuffer (renderPass , width , height , imageViews );
294-
295- // Build clear values
296- org .lwjgl .vulkan .VkClearValue .Buffer clearValues = VkClearValue .calloc (viewCount , stack );
297- for (int i = 0 ; i < colorTextures .length ; i ++) {
298- if (colorTextures [i ] != null ) {
299- RenderPassDescriptor .Attachment <@ NotNull Optional <Vector4fc >> attachment = colorAttachments .get (i );
300- if (attachment .clearValue ().isPresent ()) {
301- Vector4fc color = attachment .clearValue ().get ();
302- System .out .println ("Clear value: " +color );
303- Vk11Utils .putArgb (clearValues .get (i ).color (), color );
304- }
305- }
306- }
307- if (hasDepth ) {
308- OptionalDouble clearDepth = depthAttachment .clearValue ();
309- if (clearDepth .isPresent ()) {
310- clearValues .get (colorTextures .length ).depthStencil (VkClearDepthStencilValue .calloc (stack ).depth ((float )clearDepth .getAsDouble ()));
311- }
312- }
280+ long framebuffer = this .device .framebufferCache ().getOrCreateFramebuffer (renderPass , width , height , attachmentViews );
313281
314- // Begin render pass
315- org . lwjgl . vulkan . VkRenderPassBeginInfo renderPassBeginInfo = org . lwjgl . vulkan . VkRenderPassBeginInfo .calloc (stack ).sType$Default ();
316- renderPassBeginInfo .renderPass (renderPass );
317- renderPassBeginInfo .framebuffer (framebuffer );
318- renderPassBeginInfo .renderArea ().offset ().set (descriptor .renderArea != null ? descriptor .renderArea .x () : 0 , descriptor .renderArea != null ? descriptor .renderArea .y () : 0 );
319- renderPassBeginInfo .renderArea ().extent ().set (width , height );
320- renderPassBeginInfo .pClearValues (clearValues );
321- renderPassBeginInfo .clearValueCount (viewCount );
282+ // Begin render pass
283+ VkRenderPassBeginInfo renderPassBeginInfo = VkRenderPassBeginInfo .calloc (memoryStack ).sType$Default ();
284+ renderPassBeginInfo .renderPass (renderPass );
285+ renderPassBeginInfo .framebuffer (framebuffer );
286+ renderPassBeginInfo .renderArea ().offset ().set (descriptor .renderArea != null ? descriptor .renderArea .x () : 0 , descriptor .renderArea != null ? descriptor .renderArea .y () : 0 );
287+ renderPassBeginInfo .renderArea ().extent ().set (width , height );
288+ renderPassBeginInfo .pClearValues (clearValues );
289+ renderPassBeginInfo .clearValueCount (attachmentViews . length );
322290
323- VK10 .vkCmdBeginRenderPass (this .commandBuffer (), renderPassBeginInfo , VK10 .VK_SUBPASS_CONTENTS_INLINE );
324- }
291+ VK10 .vkCmdBeginRenderPass (this .commandBuffer (), renderPassBeginInfo , VK10 .VK_SUBPASS_CONTENTS_INLINE );
325292
326- this .currentRenderPass = new Vk11RenderPass (
327- this .device , this , this .commandBuffer (), descriptor .renderArea , width , height , depthAttachment != null , descriptor .label ()
328- );
329- return this .currentRenderPass ;
330- }
293+ this .currentRenderPass = new Vk11RenderPass (
294+ this .device , this , this .commandBuffer (), descriptor .renderArea , width , height , hasDepth , descriptor .label ()
295+ );
296+ return this .currentRenderPass ;
297+ }
298+ }
331299
332300 private boolean poolCapacityLow () {
333301 for (Vk11DescriptorPool pool : descriptorPools ) {
0 commit comments