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

Commit 4a398d3

Browse files
authored
[Impeller] Reland: Switch from transient stencil-only to depth+stencil buffer. (#49838)
Part of flutter/flutter#138460. In preparation for [draw order optimization](flutter/flutter#114402) and [StC](flutter/flutter#123671). Use a transient depth+stencil texture instead of a stencil-only texture. Doing this in isolation to detect/weed out any bugs with handling the attachment.
1 parent d653559 commit 4a398d3

17 files changed

+169
-81
lines changed

impeller/aiks/aiks_unittests.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3557,6 +3557,8 @@ TEST_P(AiksTest, GaussianBlurWithoutDecalSupport) {
35573557
.WillRepeatedly(::testing::Return(false));
35583558
FLT_FORWARD(mock_capabilities, old_capabilities, GetDefaultColorFormat);
35593559
FLT_FORWARD(mock_capabilities, old_capabilities, GetDefaultStencilFormat);
3560+
FLT_FORWARD(mock_capabilities, old_capabilities,
3561+
GetDefaultDepthStencilFormat);
35603562
FLT_FORWARD(mock_capabilities, old_capabilities, SupportsOffscreenMSAA);
35613563
FLT_FORWARD(mock_capabilities, old_capabilities,
35623564
SupportsImplicitResolvingMSAA);

impeller/entity/contents/content_context.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,8 @@ void ContentContextOptions::ApplyToPipelineDescriptor(
136136
}
137137
desc.SetColorAttachmentDescriptor(0u, color0);
138138

139-
if (!has_stencil_attachment) {
139+
if (!has_depth_stencil_attachments) {
140+
desc.ClearDepthAttachment();
140141
desc.ClearStencilAttachments();
141142
}
142143

impeller/entity/contents/content_context.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ struct ContentContextOptions {
285285
StencilOperation stencil_operation = StencilOperation::kKeep;
286286
PrimitiveType primitive_type = PrimitiveType::kTriangle;
287287
PixelFormat color_attachment_pixel_format = PixelFormat::kUnknown;
288-
bool has_stencil_attachment = true;
288+
bool has_depth_stencil_attachments = true;
289289
bool wireframe = false;
290290
bool is_for_rrect_blur_clear = false;
291291

@@ -301,7 +301,7 @@ struct ContentContextOptions {
301301

302302
return (o.is_for_rrect_blur_clear ? 1llu : 0llu) << 0 |
303303
(o.wireframe ? 1llu : 0llu) << 1 |
304-
(o.has_stencil_attachment ? 1llu : 0llu) << 2 |
304+
(o.has_depth_stencil_attachments ? 1llu : 0llu) << 2 |
305305
// enums
306306
static_cast<uint64_t>(o.color_attachment_pixel_format) << 16 |
307307
static_cast<uint64_t>(o.primitive_type) << 24 |
@@ -322,7 +322,8 @@ struct ContentContextOptions {
322322
lhs.primitive_type == rhs.primitive_type &&
323323
lhs.color_attachment_pixel_format ==
324324
rhs.color_attachment_pixel_format &&
325-
lhs.has_stencil_attachment == rhs.has_stencil_attachment &&
325+
lhs.has_depth_stencil_attachments ==
326+
rhs.has_depth_stencil_attachments &&
326327
lhs.wireframe == rhs.wireframe &&
327328
lhs.is_for_rrect_blur_clear == rhs.is_for_rrect_blur_clear;
328329
}

impeller/entity/contents/contents.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ ContentContextOptions OptionsFromPass(const RenderPass& pass) {
2121
ContentContextOptions opts;
2222
opts.sample_count = pass.GetSampleCount();
2323
opts.color_attachment_pixel_format = pass.GetRenderTargetPixelFormat();
24-
opts.has_stencil_attachment = pass.HasStencilAttachment();
24+
opts.has_depth_stencil_attachments = pass.HasStencilAttachment();
2525
return opts;
2626
}
2727

impeller/entity/entity_pass.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,8 @@ bool EntityPass::Render(ContentContext& renderer,
431431
// If a root stencil was provided by the caller, then verify that it has a
432432
// configuration which can be used to render this pass.
433433
auto stencil_attachment = root_render_target.GetStencilAttachment();
434-
if (stencil_attachment.has_value()) {
434+
auto depth_attachment = root_render_target.GetDepthAttachment();
435+
if (stencil_attachment.has_value() && depth_attachment.has_value()) {
435436
auto stencil_texture = stencil_attachment->texture;
436437
if (!stencil_texture) {
437438
VALIDATION_LOG << "The root RenderTarget must have a stencil texture.";
@@ -450,7 +451,7 @@ bool EntityPass::Render(ContentContext& renderer,
450451
// Setup a new root stencil with an optimal configuration if one wasn't
451452
// provided by the caller.
452453
else {
453-
root_render_target.SetupStencilAttachment(
454+
root_render_target.SetupDepthStencilAttachments(
454455
*renderer.GetContext(), *renderer.GetRenderTargetCache(),
455456
color0.texture->GetSize(),
456457
renderer.GetContext()->GetCapabilities()->SupportsOffscreenMSAA(),

impeller/entity/entity_unittests.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2619,9 +2619,9 @@ TEST_P(EntityTest, SpecializationConstantsAreAppliedToVariants) {
26192619
ContentContext(GetContext(), TypographerContextSkia::Make());
26202620

26212621
auto default_color_burn = content_context.GetBlendColorBurnPipeline(
2622-
{.has_stencil_attachment = false});
2622+
{.has_depth_stencil_attachments = false});
26232623
auto alt_color_burn = content_context.GetBlendColorBurnPipeline(
2624-
{.has_stencil_attachment = true});
2624+
{.has_depth_stencil_attachments = true});
26252625

26262626
ASSERT_NE(default_color_burn, alt_color_burn);
26272627
ASSERT_EQ(default_color_burn->GetDescriptor().GetSpecializationConstants(),
@@ -2690,7 +2690,7 @@ TEST_P(EntityTest, ContentContextOptionsHasReasonableHashFunctions) {
26902690
opts.blend_mode = BlendMode::kColorBurn;
26912691
auto hash_b = ContentContextOptions::Hash{}(opts);
26922692

2693-
opts.has_stencil_attachment = false;
2693+
opts.has_depth_stencil_attachments = false;
26942694
auto hash_c = ContentContextOptions::Hash{}(opts);
26952695

26962696
opts.primitive_type = PrimitiveType::kPoint;

impeller/renderer/backend/gles/blit_command_gles.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static std::optional<GLuint> ConfigureFBO(
4141
gl.BindFramebuffer(fbo_type, fbo);
4242

4343
if (!TextureGLES::Cast(*texture).SetAsFramebufferAttachment(
44-
fbo_type, TextureGLES::AttachmentPoint::kColor0)) {
44+
fbo_type, TextureGLES::AttachmentType::kColor0)) {
4545
VALIDATION_LOG << "Could not attach texture to framebuffer.";
4646
DeleteFBO(gl, fbo, fbo_type);
4747
return std::nullopt;

impeller/renderer/backend/gles/render_pass_gles.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,20 @@ struct RenderPassData {
186186

187187
if (auto color = TextureGLES::Cast(pass_data.color_attachment.get())) {
188188
if (!color->SetAsFramebufferAttachment(
189-
GL_FRAMEBUFFER, TextureGLES::AttachmentPoint::kColor0)) {
189+
GL_FRAMEBUFFER, TextureGLES::AttachmentType::kColor0)) {
190190
return false;
191191
}
192192
}
193193

194194
if (auto depth = TextureGLES::Cast(pass_data.depth_attachment.get())) {
195195
if (!depth->SetAsFramebufferAttachment(
196-
GL_FRAMEBUFFER, TextureGLES::AttachmentPoint::kDepth)) {
196+
GL_FRAMEBUFFER, TextureGLES::AttachmentType::kDepth)) {
197197
return false;
198198
}
199199
}
200200
if (auto stencil = TextureGLES::Cast(pass_data.stencil_attachment.get())) {
201201
if (!stencil->SetAsFramebufferAttachment(
202-
GL_FRAMEBUFFER, TextureGLES::AttachmentPoint::kStencil)) {
202+
GL_FRAMEBUFFER, TextureGLES::AttachmentType::kStencil)) {
203203
return false;
204204
}
205205
}

impeller/renderer/backend/gles/surface_gles.cc

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,24 +40,34 @@ std::unique_ptr<Surface> SurfaceGLES::WrapFBO(
4040
color0.load_action = LoadAction::kClear;
4141
color0.store_action = StoreAction::kStore;
4242

43-
TextureDescriptor stencil0_tex;
44-
stencil0_tex.type = TextureType::kTexture2D;
45-
stencil0_tex.format = color_format;
46-
stencil0_tex.size = fbo_size;
47-
stencil0_tex.usage =
43+
TextureDescriptor depth_stencil_texture_desc;
44+
depth_stencil_texture_desc.type = TextureType::kTexture2D;
45+
depth_stencil_texture_desc.format = color_format;
46+
depth_stencil_texture_desc.size = fbo_size;
47+
depth_stencil_texture_desc.usage =
4848
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
49-
stencil0_tex.sample_count = SampleCount::kCount1;
49+
depth_stencil_texture_desc.sample_count = SampleCount::kCount1;
50+
51+
auto depth_stencil_tex = std::make_shared<TextureGLES>(
52+
gl_context.GetReactor(), depth_stencil_texture_desc,
53+
TextureGLES::IsWrapped::kWrapped);
54+
55+
DepthAttachment depth0;
56+
depth0.clear_depth = 0;
57+
depth0.texture = depth_stencil_tex;
58+
depth0.load_action = LoadAction::kClear;
59+
depth0.store_action = StoreAction::kDontCare;
5060

5161
StencilAttachment stencil0;
5262
stencil0.clear_stencil = 0;
53-
stencil0.texture = std::make_shared<TextureGLES>(
54-
gl_context.GetReactor(), stencil0_tex, TextureGLES::IsWrapped::kWrapped);
63+
stencil0.texture = depth_stencil_tex;
5564
stencil0.load_action = LoadAction::kClear;
5665
stencil0.store_action = StoreAction::kDontCare;
5766

5867
RenderTarget render_target_desc;
5968

6069
render_target_desc.SetColorAttachment(color0, 0u);
70+
render_target_desc.SetDepthAttachment(depth0);
6171
render_target_desc.SetStencilAttachment(stencil0);
6272

6373
#ifdef IMPELLER_DEBUG

impeller/renderer/backend/gles/texture_gles.cc

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <optional>
88
#include <utility>
99

10+
#include "flutter/fml/logging.h"
1011
#include "flutter/fml/mapping.h"
1112
#include "flutter/fml/trace_event.h"
1213
#include "impeller/base/allocation.h"
@@ -17,13 +18,37 @@
1718

1819
namespace impeller {
1920

21+
static bool IsDepthStencilFormat(PixelFormat format) {
22+
switch (format) {
23+
case PixelFormat::kS8UInt:
24+
case PixelFormat::kD24UnormS8Uint:
25+
case PixelFormat::kD32FloatS8UInt:
26+
return true;
27+
case PixelFormat::kUnknown:
28+
case PixelFormat::kA8UNormInt:
29+
case PixelFormat::kR8UNormInt:
30+
case PixelFormat::kR8G8UNormInt:
31+
case PixelFormat::kR8G8B8A8UNormInt:
32+
case PixelFormat::kR8G8B8A8UNormIntSRGB:
33+
case PixelFormat::kB8G8R8A8UNormInt:
34+
case PixelFormat::kB8G8R8A8UNormIntSRGB:
35+
case PixelFormat::kR32G32B32A32Float:
36+
case PixelFormat::kR16G16B16A16Float:
37+
case PixelFormat::kB10G10R10XR:
38+
case PixelFormat::kB10G10R10XRSRGB:
39+
case PixelFormat::kB10G10R10A10XR:
40+
return false;
41+
}
42+
FML_UNREACHABLE();
43+
}
44+
2045
static TextureGLES::Type GetTextureTypeFromDescriptor(
2146
const TextureDescriptor& desc) {
2247
const auto usage = static_cast<TextureUsageMask>(desc.usage);
2348
const auto render_target =
2449
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
2550
const auto is_msaa = desc.sample_count == SampleCount::kCount4;
26-
if (usage == render_target && desc.format == PixelFormat::kS8UInt) {
51+
if (usage == render_target && IsDepthStencilFormat(desc.format)) {
2752
return is_msaa ? TextureGLES::Type::kRenderBufferMultisampled
2853
: TextureGLES::Type::kRenderBuffer;
2954
}
@@ -457,19 +482,20 @@ TextureGLES::Type TextureGLES::GetType() const {
457482
return type_;
458483
}
459484

460-
static GLenum ToAttachmentPoint(TextureGLES::AttachmentPoint point) {
485+
static GLenum ToAttachmentType(TextureGLES::AttachmentType point) {
461486
switch (point) {
462-
case TextureGLES::AttachmentPoint::kColor0:
487+
case TextureGLES::AttachmentType::kColor0:
463488
return GL_COLOR_ATTACHMENT0;
464-
case TextureGLES::AttachmentPoint::kDepth:
489+
case TextureGLES::AttachmentType::kDepth:
465490
return GL_DEPTH_ATTACHMENT;
466-
case TextureGLES::AttachmentPoint::kStencil:
491+
case TextureGLES::AttachmentType::kStencil:
467492
return GL_STENCIL_ATTACHMENT;
468493
}
469494
}
470495

471-
bool TextureGLES::SetAsFramebufferAttachment(GLenum target,
472-
AttachmentPoint point) const {
496+
bool TextureGLES::SetAsFramebufferAttachment(
497+
GLenum target,
498+
AttachmentType attachment_type) const {
473499
if (!IsValid()) {
474500
return false;
475501
}
@@ -482,29 +508,30 @@ bool TextureGLES::SetAsFramebufferAttachment(GLenum target,
482508

483509
switch (type_) {
484510
case Type::kTexture:
485-
gl.FramebufferTexture2D(target, // target
486-
ToAttachmentPoint(point), // attachment
487-
GL_TEXTURE_2D, // textarget
488-
handle.value(), // texture
489-
0 // level
511+
gl.FramebufferTexture2D(target, // target
512+
ToAttachmentType(attachment_type), // attachment
513+
GL_TEXTURE_2D, // textarget
514+
handle.value(), // texture
515+
0 // level
490516
);
491517
break;
492518
case Type::kTextureMultisampled:
493519
gl.FramebufferTexture2DMultisampleEXT(
494-
target, // target
495-
ToAttachmentPoint(point), // attachment
496-
GL_TEXTURE_2D, // textarget
497-
handle.value(), // texture
498-
0, // level
499-
4 // samples
520+
target, // target
521+
ToAttachmentType(attachment_type), // attachment
522+
GL_TEXTURE_2D, // textarget
523+
handle.value(), // texture
524+
0, // level
525+
4 // samples
500526
);
501527
break;
502528
case Type::kRenderBuffer:
503529
case Type::kRenderBufferMultisampled:
504-
gl.FramebufferRenderbuffer(target, // target
505-
ToAttachmentPoint(point), // attachment
506-
GL_RENDERBUFFER, // render-buffer target
507-
handle.value() // render-buffer
530+
gl.FramebufferRenderbuffer(
531+
target, // target
532+
ToAttachmentType(attachment_type), // attachment
533+
GL_RENDERBUFFER, // render-buffer target
534+
handle.value() // render-buffer
508535
);
509536
break;
510537
}

0 commit comments

Comments
 (0)