From 20e12c94dd53212f34478197d6babd5ca21850d0 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Tue, 23 Jan 2024 10:57:52 -0800 Subject: [PATCH] [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. --- .../entity/contents/content_context.cc | 14 ++++++ .../contents/runtime_effect_contents.cc | 9 ++-- .../flutter/impeller/entity/entity_pass.cc | 6 +++ .../impeller/entity/entity_unittests.cc | 47 +++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/impeller/entity/contents/content_context.cc b/engine/src/flutter/impeller/entity/contents/content_context.cc index f3baf7437f..f4c186199a 100644 --- a/engine/src/flutter/impeller/entity/contents/content_context.cc +++ b/engine/src/flutter/impeller/entity/contents/content_context.cc @@ -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); diff --git a/engine/src/flutter/impeller/entity/contents/runtime_effect_contents.cc b/engine/src/flutter/impeller/entity/contents/runtime_effect_contents.cc index 7da74e4a56..896731622e 100644 --- a/engine/src/flutter/impeller/entity/contents/runtime_effect_contents.cc +++ b/engine/src/flutter/impeller/entity/contents/runtime_effect_contents.cc @@ -141,7 +141,7 @@ bool RuntimeEffectContents::Render(const ContentContext& renderer, const std::shared_ptr& 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) { diff --git a/engine/src/flutter/impeller/entity/entity_pass.cc b/engine/src/flutter/impeller/entity/entity_pass.cc index e56e4bb7f9..701910f5e8 100644 --- a/engine/src/flutter/impeller/entity/entity_pass.cc +++ b/engine/src/flutter/impeller/entity/entity_pass.cc @@ -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()), diff --git a/engine/src/flutter/impeller/entity/entity_unittests.cc b/engine/src/flutter/impeller/entity/entity_unittests.cc index fc14e47e4c..a6a21d9bde 100644 --- a/engine/src/flutter/impeller/entity/entity_unittests.cc +++ b/engine/src/flutter/impeller/entity/entity_unittests.cc @@ -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(); + 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(GetSecondsElapsed()), + }; + auto uniform_data = std::make_shared>(); + 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";