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

Commit f95db8c

Browse files
committed
[Impeller] Switch from transient stencil to depth+stencil buffer.
1 parent 299bbd5 commit f95db8c

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
@@ -442,7 +442,7 @@ bool EntityPass::Render(ContentContext& renderer,
442442
// Setup a new root stencil with an optimal configuration if one wasn't
443443
// provided by the caller.
444444
else {
445-
root_render_target.SetupStencilAttachment(
445+
root_render_target.SetupDepthStencilAttachments(
446446
*renderer.GetContext(), *renderer.GetRenderTargetCache(),
447447
color0.texture->GetSize(),
448448
renderer.GetContext()->GetCapabilities()->SupportsOffscreenMSAA(),

impeller/renderer/render_target.cc

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

impeller/renderer/render_target.h

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

110110
bool IsValid() const;
111111

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

120120
SampleCount GetSampleCount() const;
121121

impeller/renderer/renderer_unittests.cc

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

0 commit comments

Comments
 (0)