@@ -239,25 +239,8 @@ RenderTarget RenderTarget::CreateOffscreen(
239239 target.SetColorAttachment (color0, 0u );
240240
241241 if (stencil_attachment_config.has_value ()) {
242- TextureDescriptor stencil_tex0;
243- stencil_tex0.storage_mode = stencil_attachment_config->storage_mode ;
244- stencil_tex0.format = context.GetCapabilities ()->GetDefaultStencilFormat ();
245- stencil_tex0.size = size;
246- stencil_tex0.usage =
247- static_cast <TextureUsageMask>(TextureUsage::kRenderTarget );
248-
249- StencilAttachment stencil0;
250- stencil0.load_action = stencil_attachment_config->load_action ;
251- stencil0.store_action = stencil_attachment_config->store_action ;
252- stencil0.clear_stencil = 0u ;
253- stencil0.texture =
254- context.GetResourceAllocator ()->CreateTexture (stencil_tex0);
255-
256- if (!stencil0.texture ) {
257- return {};
258- }
259- stencil0.texture ->SetLabel (SPrintF (" %s Stencil Texture" , label.c_str ()));
260- target.SetStencilAttachment (std::move (stencil0));
242+ target.SetupStencilAttachment (context, size, false , label,
243+ stencil_attachment_config.value ());
261244 } else {
262245 target.SetStencilAttachment (std::nullopt );
263246 }
@@ -331,34 +314,46 @@ RenderTarget RenderTarget::CreateOffscreenMSAA(
331314 // Create MSAA stencil texture.
332315
333316 if (stencil_attachment_config.has_value ()) {
334- TextureDescriptor stencil_tex0;
335- stencil_tex0.storage_mode = stencil_attachment_config->storage_mode ;
336- stencil_tex0.type = TextureType::kTexture2DMultisample ;
337- stencil_tex0.sample_count = SampleCount::kCount4 ;
338- stencil_tex0.format = context.GetCapabilities ()->GetDefaultStencilFormat ();
339- stencil_tex0.size = size;
340- stencil_tex0.usage =
341- static_cast <TextureUsageMask>(TextureUsage::kRenderTarget );
342-
343- StencilAttachment stencil0;
344- stencil0.load_action = stencil_attachment_config->load_action ;
345- stencil0.store_action = stencil_attachment_config->store_action ;
346- stencil0.clear_stencil = 0u ;
347- stencil0.texture =
348- context.GetResourceAllocator ()->CreateTexture (stencil_tex0);
349-
350- if (!stencil0.texture ) {
351- return {};
352- }
353- stencil0.texture ->SetLabel (SPrintF (" %s Stencil Texture" , label.c_str ()));
354- target.SetStencilAttachment (std::move (stencil0));
317+ target.SetupStencilAttachment (context, size, true , label,
318+ stencil_attachment_config.value ());
355319 } else {
356320 target.SetStencilAttachment (std::nullopt );
357321 }
358322
359323 return target;
360324}
361325
326+ void RenderTarget::SetupStencilAttachment (
327+ const Context& context,
328+ ISize size,
329+ bool msaa,
330+ const std::string& label,
331+ AttachmentConfig stencil_attachment_config) {
332+ TextureDescriptor stencil_tex0;
333+ stencil_tex0.storage_mode = stencil_attachment_config.storage_mode ;
334+ if (msaa) {
335+ stencil_tex0.type = TextureType::kTexture2DMultisample ;
336+ stencil_tex0.sample_count = SampleCount::kCount4 ;
337+ }
338+ stencil_tex0.format = context.GetCapabilities ()->GetDefaultStencilFormat ();
339+ stencil_tex0.size = size;
340+ stencil_tex0.usage =
341+ static_cast <TextureUsageMask>(TextureUsage::kRenderTarget );
342+
343+ StencilAttachment stencil0;
344+ stencil0.load_action = stencil_attachment_config.load_action ;
345+ stencil0.store_action = stencil_attachment_config.store_action ;
346+ stencil0.clear_stencil = 0u ;
347+ stencil0.texture =
348+ context.GetResourceAllocator ()->CreateTexture (stencil_tex0);
349+
350+ if (!stencil0.texture ) {
351+ return ; // Error messages are handled by `Allocator::CreateTexture`.
352+ }
353+ stencil0.texture ->SetLabel (SPrintF (" %s Stencil Texture" , label.c_str ()));
354+ SetStencilAttachment (std::move (stencil0));
355+ }
356+
362357size_t RenderTarget::GetTotalAttachmentCount () const {
363358 size_t count = 0u ;
364359 for (const auto & [_, color] : colors_) {
0 commit comments