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

[Impeller] Switch from transient stencil-only to depth+stencil buffer. #47987

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion impeller/entity/entity_pass.cc
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ bool EntityPass::Render(ContentContext& renderer,
// Setup a new root stencil with an optimal configuration if one wasn't
// provided by the caller.
else {
root_render_target.SetupStencilAttachment(
root_render_target.SetupDepthStencilAttachments(
*renderer.GetContext(), *renderer.GetRenderTargetCache(),
color0.texture->GetSize(),
renderer.GetContext()->GetCapabilities()->SupportsOffscreenMSAA(),
Expand Down
2 changes: 1 addition & 1 deletion impeller/renderer/backend/gles/blit_command_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static std::optional<GLuint> ConfigureFBO(
gl.BindFramebuffer(fbo_type, fbo);

if (!TextureGLES::Cast(*texture).SetAsFramebufferAttachment(
fbo_type, TextureGLES::AttachmentPoint::kColor0)) {
fbo_type, TextureGLES::AttachmentType::kColor0)) {
VALIDATION_LOG << "Could not attach texture to framebuffer.";
DeleteFBO(gl, fbo, fbo_type);
return std::nullopt;
Expand Down
6 changes: 3 additions & 3 deletions impeller/renderer/backend/gles/render_pass_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,20 @@ struct RenderPassData {

if (auto color = TextureGLES::Cast(pass_data.color_attachment.get())) {
if (!color->SetAsFramebufferAttachment(
GL_FRAMEBUFFER, TextureGLES::AttachmentPoint::kColor0)) {
GL_FRAMEBUFFER, TextureGLES::AttachmentType::kColor0)) {
return false;
}
}

if (auto depth = TextureGLES::Cast(pass_data.depth_attachment.get())) {
if (!depth->SetAsFramebufferAttachment(
GL_FRAMEBUFFER, TextureGLES::AttachmentPoint::kDepth)) {
GL_FRAMEBUFFER, TextureGLES::AttachmentType::kDepth)) {
return false;
}
}
if (auto stencil = TextureGLES::Cast(pass_data.stencil_attachment.get())) {
if (!stencil->SetAsFramebufferAttachment(
GL_FRAMEBUFFER, TextureGLES::AttachmentPoint::kStencil)) {
GL_FRAMEBUFFER, TextureGLES::AttachmentType::kStencil)) {
return false;
}
}
Expand Down
71 changes: 49 additions & 22 deletions impeller/renderer/backend/gles/texture_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <optional>
#include <utility>

#include "flutter/fml/logging.h"
#include "flutter/fml/mapping.h"
#include "flutter/fml/trace_event.h"
#include "impeller/base/allocation.h"
Expand All @@ -17,13 +18,37 @@

namespace impeller {

static bool IsDepthStencilFormat(PixelFormat format) {
switch (format) {
case PixelFormat::kS8UInt:
case PixelFormat::kD24UnormS8Uint:
case PixelFormat::kD32FloatS8UInt:
return true;
case PixelFormat::kUnknown:
case PixelFormat::kA8UNormInt:
case PixelFormat::kR8UNormInt:
case PixelFormat::kR8G8UNormInt:
case PixelFormat::kR8G8B8A8UNormInt:
case PixelFormat::kR8G8B8A8UNormIntSRGB:
case PixelFormat::kB8G8R8A8UNormInt:
case PixelFormat::kB8G8R8A8UNormIntSRGB:
case PixelFormat::kR32G32B32A32Float:
case PixelFormat::kR16G16B16A16Float:
case PixelFormat::kB10G10R10XR:
case PixelFormat::kB10G10R10XRSRGB:
case PixelFormat::kB10G10R10A10XR:
return false;
}
FML_UNREACHABLE();
}

static TextureGLES::Type GetTextureTypeFromDescriptor(
const TextureDescriptor& desc) {
const auto usage = static_cast<TextureUsageMask>(desc.usage);
const auto render_target =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);
const auto is_msaa = desc.sample_count == SampleCount::kCount4;
if (usage == render_target && desc.format == PixelFormat::kS8UInt) {
if (usage == render_target && IsDepthStencilFormat(desc.format)) {
return is_msaa ? TextureGLES::Type::kRenderBufferMultisampled
: TextureGLES::Type::kRenderBuffer;
}
Expand Down Expand Up @@ -457,19 +482,20 @@ TextureGLES::Type TextureGLES::GetType() const {
return type_;
}

static GLenum ToAttachmentPoint(TextureGLES::AttachmentPoint point) {
static GLenum ToAttachmentType(TextureGLES::AttachmentType point) {
switch (point) {
case TextureGLES::AttachmentPoint::kColor0:
case TextureGLES::AttachmentType::kColor0:
return GL_COLOR_ATTACHMENT0;
case TextureGLES::AttachmentPoint::kDepth:
case TextureGLES::AttachmentType::kDepth:
return GL_DEPTH_ATTACHMENT;
case TextureGLES::AttachmentPoint::kStencil:
case TextureGLES::AttachmentType::kStencil:
return GL_STENCIL_ATTACHMENT;
}
}

bool TextureGLES::SetAsFramebufferAttachment(GLenum target,
AttachmentPoint point) const {
bool TextureGLES::SetAsFramebufferAttachment(
GLenum target,
AttachmentType attachment_type) const {
if (!IsValid()) {
return false;
}
Expand All @@ -482,29 +508,30 @@ bool TextureGLES::SetAsFramebufferAttachment(GLenum target,

switch (type_) {
case Type::kTexture:
gl.FramebufferTexture2D(target, // target
ToAttachmentPoint(point), // attachment
GL_TEXTURE_2D, // textarget
handle.value(), // texture
0 // level
gl.FramebufferTexture2D(target, // target
ToAttachmentType(attachment_type), // attachment
GL_TEXTURE_2D, // textarget
handle.value(), // texture
0 // level
);
break;
case Type::kTextureMultisampled:
gl.FramebufferTexture2DMultisampleEXT(
target, // target
ToAttachmentPoint(point), // attachment
GL_TEXTURE_2D, // textarget
handle.value(), // texture
0, // level
4 // samples
target, // target
ToAttachmentType(attachment_type), // attachment
GL_TEXTURE_2D, // textarget
handle.value(), // texture
0, // level
4 // samples
);
break;
case Type::kRenderBuffer:
case Type::kRenderBufferMultisampled:
gl.FramebufferRenderbuffer(target, // target
ToAttachmentPoint(point), // attachment
GL_RENDERBUFFER, // render-buffer target
handle.value() // render-buffer
gl.FramebufferRenderbuffer(
target, // target
ToAttachmentType(attachment_type), // attachment
GL_RENDERBUFFER, // render-buffer target
handle.value() // render-buffer
);
break;
}
Expand Down
7 changes: 4 additions & 3 deletions impeller/renderer/backend/gles/texture_gles.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ class TextureGLES final : public Texture,

[[nodiscard]] bool GenerateMipmap();

enum class AttachmentPoint {
enum class AttachmentType {
kColor0,
kDepth,
kStencil,
};
[[nodiscard]] bool SetAsFramebufferAttachment(GLenum target,
AttachmentPoint point) const;
[[nodiscard]] bool SetAsFramebufferAttachment(
GLenum target,
AttachmentType attachment_type) const;

Type GetType() const;

Expand Down
50 changes: 33 additions & 17 deletions impeller/renderer/render_target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ RenderTarget RenderTarget::CreateOffscreen(
target.SetColorAttachment(color0, 0u);

if (stencil_attachment_config.has_value()) {
target.SetupStencilAttachment(context, allocator, size, false, label,
stencil_attachment_config.value());
target.SetupDepthStencilAttachments(context, allocator, size, false, label,
stencil_attachment_config.value());
} else {
target.SetStencilAttachment(std::nullopt);
}
Expand Down Expand Up @@ -347,43 +347,56 @@ RenderTarget RenderTarget::CreateOffscreenMSAA(
// Create MSAA stencil texture.

if (stencil_attachment_config.has_value()) {
target.SetupStencilAttachment(context, allocator, size, true, label,
stencil_attachment_config.value());
target.SetupDepthStencilAttachments(context, allocator, size, true, label,
stencil_attachment_config.value());
} else {
target.SetStencilAttachment(std::nullopt);
}

return target;
}

void RenderTarget::SetupStencilAttachment(
void RenderTarget::SetupDepthStencilAttachments(
const Context& context,
RenderTargetAllocator& allocator,
ISize size,
bool msaa,
const std::string& label,
AttachmentConfig stencil_attachment_config) {
TextureDescriptor stencil_tex0;
stencil_tex0.storage_mode = stencil_attachment_config.storage_mode;
TextureDescriptor depth_stencil_texture_desc;
depth_stencil_texture_desc.storage_mode =
stencil_attachment_config.storage_mode;
if (msaa) {
stencil_tex0.type = TextureType::kTexture2DMultisample;
stencil_tex0.sample_count = SampleCount::kCount4;
depth_stencil_texture_desc.type = TextureType::kTexture2DMultisample;
depth_stencil_texture_desc.sample_count = SampleCount::kCount4;
}
stencil_tex0.format = context.GetCapabilities()->GetDefaultStencilFormat();
stencil_tex0.size = size;
stencil_tex0.usage =
depth_stencil_texture_desc.format =
context.GetCapabilities()->GetDefaultDepthStencilFormat();
depth_stencil_texture_desc.size = size;
depth_stencil_texture_desc.usage =
static_cast<TextureUsageMask>(TextureUsage::kRenderTarget);

auto depth_stencil_texture =
allocator.CreateTexture(depth_stencil_texture_desc);
if (!depth_stencil_texture) {
return; // Error messages are handled by `Allocator::CreateTexture`.
}

DepthAttachment depth0;
depth0.load_action = stencil_attachment_config.load_action;
depth0.store_action = stencil_attachment_config.store_action;
depth0.clear_depth = 0u;
depth0.texture = depth_stencil_texture;

StencilAttachment stencil0;
stencil0.load_action = stencil_attachment_config.load_action;
stencil0.store_action = stencil_attachment_config.store_action;
stencil0.clear_stencil = 0u;
stencil0.texture = allocator.CreateTexture(stencil_tex0);
stencil0.texture = depth_stencil_texture;

if (!stencil0.texture) {
return; // Error messages are handled by `Allocator::CreateTexture`.
}
stencil0.texture->SetLabel(SPrintF("%s Stencil Texture", label.c_str()));
stencil0.texture->SetLabel(
SPrintF("%s Depth+Stencil Texture", label.c_str()));
SetDepthAttachment(std::move(depth0));
SetStencilAttachment(std::move(stencil0));
}

Expand All @@ -403,6 +416,9 @@ size_t RenderTarget::GetTotalAttachmentCount() const {
if (stencil_.has_value()) {
count++;
}
if (depth_.has_value()) {
count++;
}
return count;
}

Expand Down
14 changes: 7 additions & 7 deletions impeller/renderer/render_target.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ class RenderTarget final {

bool IsValid() const;

void SetupStencilAttachment(const Context& context,
RenderTargetAllocator& allocator,
ISize size,
bool msaa,
const std::string& label = "Offscreen",
AttachmentConfig stencil_attachment_config =
kDefaultStencilAttachmentConfig);
void SetupDepthStencilAttachments(const Context& context,
RenderTargetAllocator& allocator,
ISize size,
bool msaa,
const std::string& label = "Offscreen",
AttachmentConfig stencil_attachment_config =
kDefaultStencilAttachmentConfig);

SampleCount GetSampleCount() const;

Expand Down
20 changes: 17 additions & 3 deletions impeller/renderer/renderer_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1161,9 +1161,9 @@ TEST_P(RendererTest, StencilMask) {
stencil_config.storage_mode = StorageMode::kHostVisible;
auto render_target_allocator =
RenderTargetAllocator(context->GetResourceAllocator());
render_target.SetupStencilAttachment(*context, render_target_allocator,
render_target.GetRenderTargetSize(),
true, "stencil", stencil_config);
render_target.SetupDepthStencilAttachments(
*context, render_target_allocator,
render_target.GetRenderTargetSize(), true, "stencil", stencil_config);
// Fill the stencil buffer with an checkerboard pattern.
const auto target_width = render_target.GetRenderTargetSize().width;
const auto target_height = render_target.GetRenderTargetSize().height;
Expand Down Expand Up @@ -1289,6 +1289,20 @@ TEST_P(RendererTest, CanLookupRenderTargetProperties) {
render_target.GetRenderTargetSize());
}

TEST_P(RendererTest,
RenderTargetCreateOffscreenMSAASetsDefaultDepthStencilFormat) {
auto context = GetContext();
auto render_target_cache = std::make_shared<RenderTargetAllocator>(
GetContext()->GetResourceAllocator());

RenderTarget render_target = RenderTarget::CreateOffscreenMSAA(
*context, *render_target_cache, {100, 100}, /*mip_count=*/1);
EXPECT_EQ(render_target.GetDepthAttachment()
->texture->GetTextureDescriptor()
.format,
GetContext()->GetCapabilities()->GetDefaultDepthStencilFormat());
}

} // namespace testing
} // namespace impeller

Expand Down