This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[Impeller] Implement RuntimeEffectContents #36866
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
730474c
Add DlRuntimeEffect
bdero 4f633ee
Include fix
bdero 99a9b58
Runtime effect
bdero d6ddae3
Make RuntimeEffect work for Metal
bdero c7fbde9
Newlines
bdero 08227c8
Address comments
bdero cba7506
kConicalGradient unimplemented
bdero ecf021a
Correct prevent overdraw
bdero da4b6bc
Remove example.frag.metal.iplr from fixture list
bdero 3310ce8
Remove unused emplace
bdero File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "impeller/entity/contents/runtime_effect_contents.h" | ||
|
|
||
| #include <future> | ||
| #include <memory> | ||
|
|
||
| #include "flutter/fml/logging.h" | ||
| #include "flutter/fml/make_copyable.h" | ||
| #include "impeller/base/validation.h" | ||
| #include "impeller/entity/contents/clip_contents.h" | ||
| #include "impeller/entity/contents/content_context.h" | ||
| #include "impeller/entity/position_no_color.vert.h" | ||
| #include "impeller/renderer/formats.h" | ||
| #include "impeller/renderer/pipeline_library.h" | ||
| #include "impeller/renderer/render_pass.h" | ||
| #include "impeller/renderer/shader_function.h" | ||
| #include "impeller/renderer/shader_types.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| void RuntimeEffectContents::SetRuntimeStage( | ||
| std::shared_ptr<RuntimeStage> runtime_stage) { | ||
| runtime_stage_ = std::move(runtime_stage); | ||
| } | ||
|
|
||
| void RuntimeEffectContents::SetUniformData(std::vector<uint8_t> uniform_data) { | ||
| uniform_data_ = std::move(uniform_data); | ||
| } | ||
|
|
||
| bool RuntimeEffectContents::Render(const ContentContext& renderer, | ||
| const Entity& entity, | ||
| RenderPass& pass) const { | ||
| auto context = renderer.GetContext(); | ||
| auto library = context->GetShaderLibrary(); | ||
|
|
||
| //-------------------------------------------------------------------------- | ||
| /// Get or register shader. | ||
| /// | ||
|
|
||
| // TODO(113719): Register the shader function earlier. | ||
|
|
||
| std::shared_ptr<const ShaderFunction> function = library->GetFunction( | ||
| runtime_stage_->GetEntrypoint(), ShaderStage::kFragment); | ||
|
|
||
| if (!function) { | ||
| std::promise<bool> promise; | ||
| auto future = promise.get_future(); | ||
|
|
||
| library->RegisterFunction( | ||
| runtime_stage_->GetEntrypoint(), | ||
| ToShaderStage(runtime_stage_->GetShaderStage()), | ||
| runtime_stage_->GetCodeMapping(), | ||
| fml::MakeCopyable([promise = std::move(promise)](bool result) mutable { | ||
| promise.set_value(result); | ||
| })); | ||
|
|
||
| if (!future.get()) { | ||
| VALIDATION_LOG << "Failed to build runtime effect (entry point: " | ||
| << runtime_stage_->GetEntrypoint() << ")"; | ||
| return false; | ||
| } | ||
|
|
||
| function = library->GetFunction(runtime_stage_->GetEntrypoint(), | ||
| ShaderStage::kFragment); | ||
| if (!function) { | ||
| VALIDATION_LOG | ||
| << "Failed to fetch runtime effect function immediately after " | ||
| "registering it (entry point: " | ||
| << runtime_stage_->GetEntrypoint() << ")"; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| //-------------------------------------------------------------------------- | ||
| /// Resolve geometry. | ||
| /// | ||
|
|
||
| auto geometry_result = GetGeometry()->GetPositionBuffer( | ||
| context->GetResourceAllocator(), pass.GetTransientsBuffer(), | ||
| renderer.GetTessellator(), pass.GetRenderTargetSize()); | ||
|
|
||
| //-------------------------------------------------------------------------- | ||
| /// Get or create runtime stage pipeline. | ||
| /// | ||
|
|
||
| using VS = PositionNoColorVertexShader; | ||
| PipelineDescriptor desc; | ||
| desc.SetLabel("Runtime Stage"); | ||
| desc.AddStageEntrypoint( | ||
| library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex)); | ||
| desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(), | ||
| ShaderStage::kFragment)); | ||
| auto vertex_descriptor = std::make_shared<VertexDescriptor>(); | ||
| if (!vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs)) { | ||
| VALIDATION_LOG << "Failed to set stage inputs for runtime effect pipeline."; | ||
| } | ||
| desc.SetVertexDescriptor(std::move(vertex_descriptor)); | ||
| desc.SetColorAttachmentDescriptor(0u, {.format = PixelFormat::kDefaultColor}); | ||
| desc.SetStencilAttachmentDescriptors({}); | ||
| desc.SetStencilPixelFormat(PixelFormat::kDefaultStencil); | ||
|
|
||
| auto options = OptionsFromPassAndEntity(pass, entity); | ||
| if (geometry_result.prevent_overdraw) { | ||
| options.stencil_compare = CompareFunction::kEqual; | ||
| options.stencil_operation = StencilOperation::kIncrementClamp; | ||
| } | ||
| options.ApplyToPipelineDescriptor(desc); | ||
|
|
||
| auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).get(); | ||
| if (!pipeline) { | ||
| VALIDATION_LOG << "Failed to get or create runtime effect pipeline."; | ||
| return false; | ||
| } | ||
|
|
||
| Command cmd; | ||
| cmd.label = "RuntimeEffectContents"; | ||
| cmd.pipeline = pipeline; | ||
| cmd.stencil_reference = entity.GetStencilDepth(); | ||
| cmd.BindVertices(geometry_result.vertex_buffer); | ||
| cmd.primitive_type = geometry_result.type; | ||
|
|
||
| //-------------------------------------------------------------------------- | ||
| /// Vertex stage uniforms. | ||
| /// | ||
|
|
||
| VS::VertInfo frame_info; | ||
| frame_info.mvp = Matrix::MakeOrthographic(pass.GetRenderTargetSize()) * | ||
| entity.GetTransformation(); | ||
| VS::BindVertInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info)); | ||
|
|
||
| //-------------------------------------------------------------------------- | ||
| /// Fragment stage uniforms. | ||
| /// | ||
|
|
||
| size_t buffer_index = 0; | ||
| for (auto uniform : runtime_stage_->GetUniforms()) { | ||
| // TODO(113715): Populate this metadata once GLES is able to handle | ||
| // non-struct uniform names. | ||
| ShaderMetadata metadata; | ||
|
|
||
| size_t alignment = | ||
| std::max(uniform.bit_width / 8, DefaultUniformAlignment()); | ||
| auto buffer_view = pass.GetTransientsBuffer().Emplace( | ||
| &uniform_data_[uniform.location * sizeof(float)], uniform.GetSize(), | ||
| alignment); | ||
|
|
||
| ShaderUniformSlot slot; | ||
| slot.name = uniform.name.c_str(); | ||
| slot.ext_res_0 = buffer_index; | ||
| cmd.BindResource(ShaderStage::kFragment, slot, metadata, buffer_view); | ||
|
|
||
| buffer_index++; | ||
| } | ||
|
|
||
| pass.AddCommand(std::move(cmd)); | ||
|
|
||
| if (geometry_result.prevent_overdraw) { | ||
| return ClipRestoreContents().Render(renderer, entity, pass); | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| } // namespace impeller | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <functional> | ||
| #include <memory> | ||
|
|
||
| #include "impeller/entity/contents/color_source_contents.h" | ||
| #include "impeller/runtime_stage/runtime_stage.h" | ||
|
|
||
| namespace impeller { | ||
|
|
||
| class RuntimeEffectContents final : public ColorSourceContents { | ||
| public: | ||
| void SetRuntimeStage(std::shared_ptr<RuntimeStage> runtime_stage); | ||
|
|
||
| void SetUniformData(std::vector<uint8_t> uniform_data); | ||
|
|
||
| // |Contents| | ||
| bool Render(const ContentContext& renderer, | ||
| const Entity& entity, | ||
| RenderPass& pass) const override; | ||
|
|
||
| private: | ||
| std::shared_ptr<RuntimeStage> runtime_stage_; | ||
| std::vector<uint8_t> uniform_data_; | ||
| }; | ||
|
|
||
| } // namespace impeller |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <impeller/transform.glsl> | ||
|
|
||
| uniform VertInfo { | ||
| mat4 mvp; | ||
| } vert_info; | ||
|
|
||
| in vec2 position; | ||
| out vec2 v_position; | ||
|
|
||
| void main() { | ||
| gl_Position = vert_info.mvp * vec4(position, 0.0, 1.0); | ||
| v_position = IPVec2TransformPosition(vert_info.mvp, position); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be doing this immediately after reading the shader in from the asset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but I'd like to defer figuring out the threading and g3 dependency situation to a later patch. I assume we're already building all of Impeller though, so maybe g3 isn't as much a concern.
Added an issue: flutter/flutter#113719