@@ -261,37 +261,46 @@ RenderTarget RenderTargetAllocator::CreateOffscreen(
261261 int mip_count,
262262 const std::string& label,
263263 RenderTarget::AttachmentConfig color_attachment_config,
264- std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config) {
264+ std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
265+ const std::shared_ptr<Texture>& existing_color_texture,
266+ const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
265267 if (size.IsEmpty ()) {
266268 return {};
267269 }
268270
269271 RenderTarget target;
270- PixelFormat pixel_format = context.GetCapabilities ()->GetDefaultColorFormat ();
271- TextureDescriptor color_tex0;
272- color_tex0.storage_mode = color_attachment_config.storage_mode ;
273- color_tex0.format = pixel_format;
274- color_tex0.size = size;
275- color_tex0.mip_count = mip_count;
276- color_tex0.usage = static_cast <uint64_t >(TextureUsage::kRenderTarget ) |
277- static_cast <uint64_t >(TextureUsage::kShaderRead );
272+
273+ std::shared_ptr<Texture> color0_tex;
274+ if (existing_color_texture) {
275+ color0_tex = existing_color_texture;
276+ } else {
277+ PixelFormat pixel_format =
278+ context.GetCapabilities ()->GetDefaultColorFormat ();
279+ TextureDescriptor color0_tex_desc;
280+ color0_tex_desc.storage_mode = color_attachment_config.storage_mode ;
281+ color0_tex_desc.format = pixel_format;
282+ color0_tex_desc.size = size;
283+ color0_tex_desc.mip_count = mip_count;
284+ color0_tex_desc.usage = static_cast <uint64_t >(TextureUsage::kRenderTarget ) |
285+ static_cast <uint64_t >(TextureUsage::kShaderRead );
286+ color0_tex = allocator_->CreateTexture (color0_tex_desc);
287+ if (!color0_tex) {
288+ return {};
289+ }
290+ }
291+ color0_tex->SetLabel (SPrintF (" %s Color Texture" , label.c_str ()));
278292
279293 ColorAttachment color0;
280294 color0.clear_color = color_attachment_config.clear_color ;
281295 color0.load_action = color_attachment_config.load_action ;
282296 color0.store_action = color_attachment_config.store_action ;
283- color0.texture = allocator_->CreateTexture (color_tex0);
284-
285- if (!color0.texture ) {
286- return {};
287- }
288- color0.texture ->SetLabel (SPrintF (" %s Color Texture" , label.c_str ()));
297+ color0.texture = color0_tex;
289298 target.SetColorAttachment (color0, 0u );
290299
291300 if (stencil_attachment_config.has_value ()) {
292- target.SetupDepthStencilAttachments (context, *allocator_, size, false ,
293- label,
294- stencil_attachment_config.value ());
301+ target.SetupDepthStencilAttachments (
302+ context, *allocator_, size, false , label,
303+ stencil_attachment_config.value (), existing_depth_stencil_texture );
295304 } else {
296305 target.SetStencilAttachment (std::nullopt );
297306 target.SetDepthAttachment (std::nullopt );
@@ -306,7 +315,10 @@ RenderTarget RenderTargetAllocator::CreateOffscreenMSAA(
306315 int mip_count,
307316 const std::string& label,
308317 RenderTarget::AttachmentConfigMSAA color_attachment_config,
309- std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config) {
318+ std::optional<RenderTarget::AttachmentConfig> stencil_attachment_config,
319+ const std::shared_ptr<Texture>& existing_color_msaa_texture,
320+ const std::shared_ptr<Texture>& existing_color_resolve_texture,
321+ const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
310322 if (size.IsEmpty ()) {
311323 return {};
312324 }
@@ -315,45 +327,50 @@ RenderTarget RenderTargetAllocator::CreateOffscreenMSAA(
315327 PixelFormat pixel_format = context.GetCapabilities ()->GetDefaultColorFormat ();
316328
317329 // Create MSAA color texture.
318-
319- TextureDescriptor color0_tex_desc;
320- color0_tex_desc.storage_mode = color_attachment_config.storage_mode ;
321- color0_tex_desc.type = TextureType::kTexture2DMultisample ;
322- color0_tex_desc.sample_count = SampleCount::kCount4 ;
323- color0_tex_desc.format = pixel_format;
324- color0_tex_desc.size = size;
325- color0_tex_desc.usage = static_cast <uint64_t >(TextureUsage::kRenderTarget );
326-
327- if (context.GetCapabilities ()->SupportsImplicitResolvingMSAA ()) {
328- // See below ("SupportsImplicitResolvingMSAA") for more details.
329- color0_tex_desc.storage_mode = StorageMode::kDevicePrivate ;
330- }
331-
332- auto color0_msaa_tex = allocator_->CreateTexture (color0_tex_desc);
333- if (!color0_msaa_tex) {
334- VALIDATION_LOG << " Could not create multisample color texture." ;
335- return {};
330+ std::shared_ptr<Texture> color0_msaa_tex;
331+ if (existing_color_msaa_texture) {
332+ color0_msaa_tex = existing_color_msaa_texture;
333+ } else {
334+ TextureDescriptor color0_tex_desc;
335+ color0_tex_desc.storage_mode = color_attachment_config.storage_mode ;
336+ color0_tex_desc.type = TextureType::kTexture2DMultisample ;
337+ color0_tex_desc.sample_count = SampleCount::kCount4 ;
338+ color0_tex_desc.format = pixel_format;
339+ color0_tex_desc.size = size;
340+ color0_tex_desc.usage = static_cast <uint64_t >(TextureUsage::kRenderTarget );
341+ if (context.GetCapabilities ()->SupportsImplicitResolvingMSAA ()) {
342+ // See below ("SupportsImplicitResolvingMSAA") for more details.
343+ color0_tex_desc.storage_mode = StorageMode::kDevicePrivate ;
344+ }
345+ color0_msaa_tex = allocator_->CreateTexture (color0_tex_desc);
346+ if (!color0_msaa_tex) {
347+ VALIDATION_LOG << " Could not create multisample color texture." ;
348+ return {};
349+ }
336350 }
337351 color0_msaa_tex->SetLabel (
338352 SPrintF (" %s Color Texture (Multisample)" , label.c_str ()));
339353
340354 // Create color resolve texture.
341-
342- TextureDescriptor color0_resolve_tex_desc;
343- color0_resolve_tex_desc.storage_mode =
344- color_attachment_config.resolve_storage_mode ;
345- color0_resolve_tex_desc.format = pixel_format;
346- color0_resolve_tex_desc.size = size;
347- color0_resolve_tex_desc.compression_type = CompressionType::kLossy ;
348- color0_resolve_tex_desc.usage =
349- static_cast <uint64_t >(TextureUsage::kRenderTarget ) |
350- static_cast <uint64_t >(TextureUsage::kShaderRead );
351- color0_resolve_tex_desc.mip_count = mip_count;
352-
353- auto color0_resolve_tex = allocator_->CreateTexture (color0_resolve_tex_desc);
354- if (!color0_resolve_tex) {
355- VALIDATION_LOG << " Could not create color texture." ;
356- return {};
355+ std::shared_ptr<Texture> color0_resolve_tex;
356+ if (existing_color_resolve_texture) {
357+ color0_resolve_tex = existing_color_resolve_texture;
358+ } else {
359+ TextureDescriptor color0_resolve_tex_desc;
360+ color0_resolve_tex_desc.storage_mode =
361+ color_attachment_config.resolve_storage_mode ;
362+ color0_resolve_tex_desc.format = pixel_format;
363+ color0_resolve_tex_desc.size = size;
364+ color0_resolve_tex_desc.compression_type = CompressionType::kLossy ;
365+ color0_resolve_tex_desc.usage =
366+ static_cast <uint64_t >(TextureUsage::kRenderTarget ) |
367+ static_cast <uint64_t >(TextureUsage::kShaderRead );
368+ color0_resolve_tex_desc.mip_count = mip_count;
369+ color0_resolve_tex = allocator_->CreateTexture (color0_resolve_tex_desc);
370+ if (!color0_resolve_tex) {
371+ VALIDATION_LOG << " Could not create color texture." ;
372+ return {};
373+ }
357374 }
358375 color0_resolve_tex->SetLabel (SPrintF (" %s Color Texture" , label.c_str ()));
359376
@@ -383,7 +400,8 @@ RenderTarget RenderTargetAllocator::CreateOffscreenMSAA(
383400
384401 if (stencil_attachment_config.has_value ()) {
385402 target.SetupDepthStencilAttachments (context, *allocator_, size, true , label,
386- stencil_attachment_config.value ());
403+ stencil_attachment_config.value (),
404+ existing_depth_stencil_texture);
387405 } else {
388406 target.SetDepthAttachment (std::nullopt );
389407 target.SetStencilAttachment (std::nullopt );
@@ -398,24 +416,28 @@ void RenderTarget::SetupDepthStencilAttachments(
398416 ISize size,
399417 bool msaa,
400418 const std::string& label,
401- RenderTarget::AttachmentConfig stencil_attachment_config) {
402- TextureDescriptor depth_stencil_texture_desc;
403- depth_stencil_texture_desc.storage_mode =
404- stencil_attachment_config.storage_mode ;
405- if (msaa) {
406- depth_stencil_texture_desc.type = TextureType::kTexture2DMultisample ;
407- depth_stencil_texture_desc.sample_count = SampleCount::kCount4 ;
408- }
409- depth_stencil_texture_desc.format =
410- context.GetCapabilities ()->GetDefaultDepthStencilFormat ();
411- depth_stencil_texture_desc.size = size;
412- depth_stencil_texture_desc.usage =
413- static_cast <TextureUsageMask>(TextureUsage::kRenderTarget );
414-
415- auto depth_stencil_texture =
416- allocator.CreateTexture (depth_stencil_texture_desc);
417- if (!depth_stencil_texture) {
418- return ; // Error messages are handled by `Allocator::CreateTexture`.
419+ RenderTarget::AttachmentConfig stencil_attachment_config,
420+ const std::shared_ptr<Texture>& existing_depth_stencil_texture) {
421+ std::shared_ptr<Texture> depth_stencil_texture;
422+ if (existing_depth_stencil_texture) {
423+ depth_stencil_texture = existing_depth_stencil_texture;
424+ } else {
425+ TextureDescriptor depth_stencil_texture_desc;
426+ depth_stencil_texture_desc.storage_mode =
427+ stencil_attachment_config.storage_mode ;
428+ if (msaa) {
429+ depth_stencil_texture_desc.type = TextureType::kTexture2DMultisample ;
430+ depth_stencil_texture_desc.sample_count = SampleCount::kCount4 ;
431+ }
432+ depth_stencil_texture_desc.format =
433+ context.GetCapabilities ()->GetDefaultDepthStencilFormat ();
434+ depth_stencil_texture_desc.size = size;
435+ depth_stencil_texture_desc.usage =
436+ static_cast <TextureUsageMask>(TextureUsage::kRenderTarget );
437+ depth_stencil_texture = allocator.CreateTexture (depth_stencil_texture_desc);
438+ if (!depth_stencil_texture) {
439+ return ; // Error messages are handled by `Allocator::CreateTexture`.
440+ }
419441 }
420442
421443 DepthAttachment depth0;
0 commit comments