@@ -255,8 +255,8 @@ RenderTarget RenderTarget::CreateOffscreen(
255255 target.SetColorAttachment (color0, 0u );
256256
257257 if (stencil_attachment_config.has_value ()) {
258- target.SetupStencilAttachment (context, allocator, size, false , label,
259- stencil_attachment_config.value ());
258+ target.SetupDepthStencilAttachments (context, allocator, size, false , label,
259+ stencil_attachment_config.value ());
260260 } else {
261261 target.SetStencilAttachment (std::nullopt );
262262 }
@@ -347,43 +347,56 @@ RenderTarget RenderTarget::CreateOffscreenMSAA(
347347 // Create MSAA stencil texture.
348348
349349 if (stencil_attachment_config.has_value ()) {
350- target.SetupStencilAttachment (context, allocator, size, true , label,
351- stencil_attachment_config.value ());
350+ target.SetupDepthStencilAttachments (context, allocator, size, true , label,
351+ stencil_attachment_config.value ());
352352 } else {
353353 target.SetStencilAttachment (std::nullopt );
354354 }
355355
356356 return target;
357357}
358358
359- void RenderTarget::SetupStencilAttachment (
359+ void RenderTarget::SetupDepthStencilAttachments (
360360 const Context& context,
361361 RenderTargetAllocator& allocator,
362362 ISize size,
363363 bool msaa,
364364 const std::string& label,
365365 AttachmentConfig stencil_attachment_config) {
366- TextureDescriptor stencil_tex0;
367- stencil_tex0.storage_mode = stencil_attachment_config.storage_mode ;
366+ TextureDescriptor depth_stencil_texture_desc;
367+ depth_stencil_texture_desc.storage_mode =
368+ stencil_attachment_config.storage_mode ;
368369 if (msaa) {
369- stencil_tex0 .type = TextureType::kTexture2DMultisample ;
370- stencil_tex0 .sample_count = SampleCount::kCount4 ;
370+ depth_stencil_texture_desc .type = TextureType::kTexture2DMultisample ;
371+ depth_stencil_texture_desc .sample_count = SampleCount::kCount4 ;
371372 }
372- stencil_tex0.format = context.GetCapabilities ()->GetDefaultStencilFormat ();
373- stencil_tex0.size = size;
374- stencil_tex0.usage =
373+ depth_stencil_texture_desc.format =
374+ context.GetCapabilities ()->GetDefaultDepthStencilFormat ();
375+ depth_stencil_texture_desc.size = size;
376+ depth_stencil_texture_desc.usage =
375377 static_cast <TextureUsageMask>(TextureUsage::kRenderTarget );
376378
379+ auto depth_stencil_texture =
380+ allocator.CreateTexture (depth_stencil_texture_desc);
381+ if (!depth_stencil_texture) {
382+ return ; // Error messages are handled by `Allocator::CreateTexture`.
383+ }
384+
385+ DepthAttachment depth0;
386+ depth0.load_action = stencil_attachment_config.load_action ;
387+ depth0.store_action = stencil_attachment_config.store_action ;
388+ depth0.clear_depth = 0u ;
389+ depth0.texture = depth_stencil_texture;
390+
377391 StencilAttachment stencil0;
378392 stencil0.load_action = stencil_attachment_config.load_action ;
379393 stencil0.store_action = stencil_attachment_config.store_action ;
380394 stencil0.clear_stencil = 0u ;
381- stencil0.texture = allocator. CreateTexture (stencil_tex0) ;
395+ stencil0.texture = depth_stencil_texture ;
382396
383- if (!stencil0.texture ) {
384- return ; // Error messages are handled by `Allocator::CreateTexture`.
385- }
386- stencil0.texture ->SetLabel (SPrintF (" %s Stencil Texture" , label.c_str ()));
397+ stencil0.texture ->SetLabel (
398+ SPrintF (" %s Depth+Stencil Texture" , label.c_str ()));
399+ SetDepthAttachment (std::move (depth0));
387400 SetStencilAttachment (std::move (stencil0));
388401}
389402
@@ -403,6 +416,9 @@ size_t RenderTarget::GetTotalAttachmentCount() const {
403416 if (stencil_.has_value ()) {
404417 count++;
405418 }
419+ if (depth_.has_value ()) {
420+ count++;
421+ }
406422 return count;
407423}
408424
0 commit comments