[Impeller] Fix runtime effect pipeline depth/stencil. (flutter/engine#49953)

- Setup depth and stencil attachments for RuntimeEffect.
- Add validation check in `EntityPass` to force backends to set things
up the root pass correctly.
- Add checks in `ContentContextOptions::ApplyToPipelineDescriptor` to
validate that callers are holding it right.
This commit is contained in:
Brandon DeRosier
2024-01-23 10:57:52 -08:00
committed by GitHub
parent bf70dc87fb
commit 20e12c94dd
4 changed files with 72 additions and 4 deletions

View File

@@ -142,12 +142,26 @@ void ContentContextOptions::ApplyToPipelineDescriptor(
}
auto maybe_stencil = desc.GetFrontStencilAttachmentDescriptor();
auto maybe_depth = desc.GetDepthStencilAttachmentDescriptor();
FML_DCHECK(has_depth_stencil_attachments == maybe_depth.has_value())
<< "Depth attachment doesn't match expected pipeline state. "
"has_depth_stencil_attachments="
<< has_depth_stencil_attachments;
FML_DCHECK(has_depth_stencil_attachments == maybe_stencil.has_value())
<< "Stencil attachment doesn't match expected pipeline state. "
"has_depth_stencil_attachments="
<< has_depth_stencil_attachments;
if (maybe_stencil.has_value()) {
StencilAttachmentDescriptor stencil = maybe_stencil.value();
stencil.stencil_compare = stencil_compare;
stencil.depth_stencil_pass = stencil_operation;
desc.SetStencilAttachmentDescriptors(stencil);
}
if (maybe_depth.has_value()) {
DepthAttachmentDescriptor depth = maybe_depth.value();
depth.depth_compare = CompareFunction::kAlways;
depth.depth_write_enabled = false;
}
desc.SetPrimitiveType(primitive_type);

View File

@@ -141,7 +141,7 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
const auto color_attachment_format = caps->GetDefaultColorFormat();
const auto stencil_attachment_format = caps->GetDefaultStencilFormat();
const auto stencil_attachment_format = caps->GetDefaultDepthStencilFormat();
using VS = RuntimeEffectVertexShader;
@@ -303,11 +303,12 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer,
desc.SetColorAttachmentDescriptor(
0u, {.format = color_attachment_format, .blending_enabled = true});
StencilAttachmentDescriptor stencil0;
stencil0.stencil_compare = CompareFunction::kEqual;
desc.SetStencilAttachmentDescriptors(stencil0);
desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
desc.SetStencilPixelFormat(stencil_attachment_format);
desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
desc.SetDepthPixelFormat(stencil_attachment_format);
options.ApplyToPipelineDescriptor(desc);
auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).Get();
if (!pipeline) {

View File

@@ -328,6 +328,12 @@ bool EntityPass::Render(ContentContext& renderer,
VALIDATION_LOG << "The root RenderTarget must have a color attachment.";
return false;
}
if (root_render_target.GetDepthAttachment().has_value() !=
root_render_target.GetStencilAttachment().has_value()) {
VALIDATION_LOG << "The root RenderTarget should have a stencil attachment "
"iff it has a depth attachment.";
return false;
}
capture.AddRect("Coverage",
Rect::MakeSize(root_render_target.GetRenderTargetSize()),

View File

@@ -2200,6 +2200,53 @@ TEST_P(EntityTest, RuntimeEffect) {
ASSERT_TRUE(OpenPlaygroundHere(callback));
}
TEST_P(EntityTest, RuntimeEffectCanSuccessfullyRender) {
auto runtime_stages =
OpenAssetAsRuntimeStage("runtime_stage_example.frag.iplr");
auto runtime_stage =
runtime_stages[PlaygroundBackendToRuntimeStageBackend(GetBackend())];
ASSERT_TRUE(runtime_stage);
ASSERT_TRUE(runtime_stage->IsDirty());
auto contents = std::make_shared<RuntimeEffectContents>();
contents->SetGeometry(Geometry::MakeCover());
contents->SetRuntimeStage(runtime_stage);
struct FragUniforms {
Vector2 iResolution;
Scalar iTime;
} frag_uniforms = {
.iResolution = Vector2(GetWindowSize().width, GetWindowSize().height),
.iTime = static_cast<Scalar>(GetSecondsElapsed()),
};
auto uniform_data = std::make_shared<std::vector<uint8_t>>();
uniform_data->resize(sizeof(FragUniforms));
memcpy(uniform_data->data(), &frag_uniforms, sizeof(FragUniforms));
contents->SetUniformData(uniform_data);
Entity entity;
entity.SetContents(contents);
// Create a render target with a depth-stencil, similar to how EntityPass
// does.
RenderTarget target = RenderTarget::CreateOffscreenMSAA(
*GetContext(), *GetContentContext()->GetRenderTargetCache(),
{GetWindowSize().width, GetWindowSize().height}, 1,
"RuntimeEffect Texture");
testing::MockRenderPass pass(GetContext(), target);
ASSERT_TRUE(contents->Render(*GetContentContext(), entity, pass));
ASSERT_EQ(pass.GetCommands().size(), 1u);
const auto& command = pass.GetCommands()[0];
ASSERT_TRUE(command.pipeline->GetDescriptor()
.GetDepthStencilAttachmentDescriptor()
.has_value());
ASSERT_TRUE(command.pipeline->GetDescriptor()
.GetFrontStencilAttachmentDescriptor()
.has_value());
}
TEST_P(EntityTest, RuntimeEffectSetsRightSizeWhenUniformIsStruct) {
if (GetBackend() != PlaygroundBackend::kVulkan) {
GTEST_SKIP() << "Test only applies to Vulkan";