@@ -253,8 +253,8 @@ RenderTarget RenderTarget::CreateOffscreen(
253253 target.SetColorAttachment (color0, 0u );
254254
255255 if (stencil_attachment_config.has_value ()) {
256- target.SetupStencilAttachment (context, allocator, size, false , label,
257- stencil_attachment_config.value ());
256+ target.SetupDepthStencilAttachments (context, allocator, size, false , label,
257+ stencil_attachment_config.value ());
258258 } else {
259259 target.SetStencilAttachment (std::nullopt );
260260 }
@@ -343,43 +343,56 @@ RenderTarget RenderTarget::CreateOffscreenMSAA(
343343 // Create MSAA stencil texture.
344344
345345 if (stencil_attachment_config.has_value ()) {
346- target.SetupStencilAttachment (context, allocator, size, true , label,
347- stencil_attachment_config.value ());
346+ target.SetupDepthStencilAttachments (context, allocator, size, true , label,
347+ stencil_attachment_config.value ());
348348 } else {
349349 target.SetStencilAttachment (std::nullopt );
350350 }
351351
352352 return target;
353353}
354354
355- void RenderTarget::SetupStencilAttachment (
355+ void RenderTarget::SetupDepthStencilAttachments (
356356 const Context& context,
357357 RenderTargetAllocator& allocator,
358358 ISize size,
359359 bool msaa,
360360 const std::string& label,
361361 AttachmentConfig stencil_attachment_config) {
362- TextureDescriptor stencil_tex0;
363- stencil_tex0.storage_mode = stencil_attachment_config.storage_mode ;
362+ TextureDescriptor depth_stencil_texture_desc;
363+ depth_stencil_texture_desc.storage_mode =
364+ stencil_attachment_config.storage_mode ;
364365 if (msaa) {
365- stencil_tex0 .type = TextureType::kTexture2DMultisample ;
366- stencil_tex0 .sample_count = SampleCount::kCount4 ;
366+ depth_stencil_texture_desc .type = TextureType::kTexture2DMultisample ;
367+ depth_stencil_texture_desc .sample_count = SampleCount::kCount4 ;
367368 }
368- stencil_tex0.format = context.GetCapabilities ()->GetDefaultStencilFormat ();
369- stencil_tex0.size = size;
370- stencil_tex0.usage =
369+ depth_stencil_texture_desc.format =
370+ context.GetCapabilities ()->GetDefaultDepthStencilFormat ();
371+ depth_stencil_texture_desc.size = size;
372+ depth_stencil_texture_desc.usage =
371373 static_cast <TextureUsageMask>(TextureUsage::kRenderTarget );
372374
375+ auto depth_stencil_texture =
376+ allocator.CreateTexture (depth_stencil_texture_desc);
377+ if (!depth_stencil_texture) {
378+ return ; // Error messages are handled by `Allocator::CreateTexture`.
379+ }
380+
381+ DepthAttachment depth0;
382+ depth0.load_action = stencil_attachment_config.load_action ;
383+ depth0.store_action = stencil_attachment_config.store_action ;
384+ depth0.clear_depth = 0u ;
385+ depth0.texture = depth_stencil_texture;
386+
373387 StencilAttachment stencil0;
374388 stencil0.load_action = stencil_attachment_config.load_action ;
375389 stencil0.store_action = stencil_attachment_config.store_action ;
376390 stencil0.clear_stencil = 0u ;
377- stencil0.texture = allocator. CreateTexture (stencil_tex0) ;
391+ stencil0.texture = depth_stencil_texture ;
378392
379- if (!stencil0.texture ) {
380- return ; // Error messages are handled by `Allocator::CreateTexture`.
381- }
382- stencil0.texture ->SetLabel (SPrintF (" %s Stencil Texture" , label.c_str ()));
393+ stencil0.texture ->SetLabel (
394+ SPrintF (" %s Depth+Stencil Texture" , label.c_str ()));
395+ SetDepthAttachment (std::move (depth0));
383396 SetStencilAttachment (std::move (stencil0));
384397}
385398
@@ -399,6 +412,9 @@ size_t RenderTarget::GetTotalAttachmentCount() const {
399412 if (stencil_.has_value ()) {
400413 count++;
401414 }
415+ if (depth_.has_value ()) {
416+ count++;
417+ }
402418 return count;
403419}
404420
0 commit comments