Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 4f1b19f

Browse files
committed
[Impeller] Switch from transient stencil to depth+stencil buffer.
1 parent 11b80d6 commit 4f1b19f

File tree

4 files changed

+44
-28
lines changed

4 files changed

+44
-28
lines changed

impeller/entity/entity_pass.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ bool EntityPass::Render(ContentContext& renderer,
441441
// Setup a new root stencil with an optimal configuration if one wasn't
442442
// provided by the caller.
443443
else {
444-
root_render_target.SetupStencilAttachment(
444+
root_render_target.SetupDepthStencilAttachments(
445445
*renderer.GetContext(), *renderer.GetRenderTargetCache(),
446446
color0.texture->GetSize(),
447447
renderer.GetContext()->GetCapabilities()->SupportsOffscreenMSAA(),

impeller/renderer/render_target.cc

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

impeller/renderer/render_target.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ class RenderTarget final {
106106

107107
bool IsValid() const;
108108

109-
void SetupStencilAttachment(const Context& context,
110-
RenderTargetAllocator& allocator,
111-
ISize size,
112-
bool msaa,
113-
const std::string& label = "Offscreen",
114-
AttachmentConfig stencil_attachment_config =
115-
kDefaultStencilAttachmentConfig);
109+
void SetupDepthStencilAttachments(const Context& context,
110+
RenderTargetAllocator& allocator,
111+
ISize size,
112+
bool msaa,
113+
const std::string& label = "Offscreen",
114+
AttachmentConfig stencil_attachment_config =
115+
kDefaultStencilAttachmentConfig);
116116

117117
SampleCount GetSampleCount() const;
118118

impeller/renderer/renderer_unittests.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,9 +1162,9 @@ TEST_P(RendererTest, StencilMask) {
11621162
stencil_config.storage_mode = StorageMode::kHostVisible;
11631163
auto render_target_allocator =
11641164
RenderTargetAllocator(context->GetResourceAllocator());
1165-
render_target.SetupStencilAttachment(*context, render_target_allocator,
1166-
render_target.GetRenderTargetSize(),
1167-
true, "stencil", stencil_config);
1165+
render_target.SetupDepthStencilAttachments(
1166+
*context, render_target_allocator,
1167+
render_target.GetRenderTargetSize(), true, "stencil", stencil_config);
11681168
// Fill the stencil buffer with an checkerboard pattern.
11691169
const auto target_width = render_target.GetRenderTargetSize().width;
11701170
const auto target_height = render_target.GetRenderTargetSize().height;

0 commit comments

Comments
 (0)