| Flutter Impeller |

runtime_effect_contents.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "impeller/entity/contents/runtime_effect_contents.h"
6
7 #include <future>
8 #include <memory>
9
10 #include "flutter/fml/logging.h"
11 #include "flutter/fml/make_copyable.h"
12 #include "impeller/base/validation.h"
13 #include "impeller/core/formats.h"
14 #include "impeller/core/shader_types.h"
15 #include "impeller/entity/contents/clip_contents.h"
16 #include "impeller/entity/contents/content_context.h"
17 #include "impeller/entity/runtime_effect.vert.h"
18 #include "impeller/renderer/pipeline_library.h"
19 #include "impeller/renderer/render_pass.h"
20 #include "impeller/renderer/sampler_library.h"
21 #include "impeller/renderer/shader_function.h"
22
23 namespace impeller {
24
25 void RuntimeEffectContents::SetRuntimeStage(
26 std::shared_ptr<RuntimeStage> runtime_stage) {
27 runtime_stage_ = std::move(runtime_stage);
28 }
29
30 void RuntimeEffectContents::SetUniformData(
31 std::shared_ptr<std::vector<uint8_t>> uniform_data) {
32 uniform_data_ = std::move(uniform_data);
33 }
34
35 void RuntimeEffectContents::SetTextureInputs(
36 std::vector<TextureInput> texture_inputs) {
37 texture_inputs_ = std::move(texture_inputs);
38 }
39
40 bool RuntimeEffectContents::CanInheritOpacity(const Entity& entity) const {
41 return false;
42 }
43
44 bool RuntimeEffectContents::Render(const ContentContext& renderer,
45 const Entity& entity,
46 RenderPass& pass) const {
47 // TODO(jonahwilliams): FragmentProgram API is not fully wired up on Android.
48 // Disable until this is complete so that integration tests and benchmarks can
49 // run m3 applications.
50 #ifdef FML_OS_ANDROID
51 return true;
52 #else
53
54 auto context = renderer.GetContext();
55 auto library = context->GetShaderLibrary();
56
57 //--------------------------------------------------------------------------
58 /// Get or register shader.
59 ///
60
61 // TODO(113719): Register the shader function earlier.
62
63 std::shared_ptr<const ShaderFunction> function = library->GetFunction(
64 runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
65
66 if (function && runtime_stage_->IsDirty()) {
67 context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
68 library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
69 ShaderStage::kFragment);
70
71 function = nullptr;
72 }
73
74 if (!function) {
75 std::promise<bool> promise;
76 auto future = promise.get_future();
77
78 library->RegisterFunction(
79 runtime_stage_->GetEntrypoint(),
80 ToShaderStage(runtime_stage_->GetShaderStage()),
81 runtime_stage_->GetCodeMapping(),
82 fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
83 promise.set_value(result);
84 }));
85
86 if (!future.get()) {
87 VALIDATION_LOG << "Failed to build runtime effect (entry point: "
88 << runtime_stage_->GetEntrypoint() << ")";
89 return false;
90 }
91
92 function = library->GetFunction(runtime_stage_->GetEntrypoint(),
93 ShaderStage::kFragment);
94 if (!function) {
95 VALIDATION_LOG
96 << "Failed to fetch runtime effect function immediately after "
97 "registering it (entry point: "
98 << runtime_stage_->GetEntrypoint() << ")";
99 return false;
100 }
101
102 runtime_stage_->SetClean();
103 }
104
105 //--------------------------------------------------------------------------
106 /// Resolve geometry.
107 ///
108
109 auto geometry_result =
110 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
111
112 //--------------------------------------------------------------------------
113 /// Get or create runtime stage pipeline.
114 ///
115
116 const auto& caps = context->GetCapabilities();
117 const auto color_attachment_format = caps->GetDefaultColorFormat();
118 const auto stencil_attachment_format = caps->GetDefaultStencilFormat();
119
120 using VS = RuntimeEffectVertexShader;
121 PipelineDescriptor desc;
122 desc.SetLabel("Runtime Stage");
123 desc.AddStageEntrypoint(
124 library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
125 desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
126 ShaderStage::kFragment));
127 auto vertex_descriptor = std::make_shared<VertexDescriptor>();
128 vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
129 VS::kInterleavedBufferLayout);
130 desc.SetVertexDescriptor(std::move(vertex_descriptor));
131 desc.SetColorAttachmentDescriptor(
132 0u, {.format = color_attachment_format, .blending_enabled = true});
133
134 StencilAttachmentDescriptor stencil0;
135 stencil0.stencil_compare = CompareFunction::kEqual;
136 desc.SetStencilAttachmentDescriptors(stencil0);
137 desc.SetStencilPixelFormat(stencil_attachment_format);
138
139 auto options = OptionsFromPassAndEntity(pass, entity);
140 if (geometry_result.prevent_overdraw) {
141 options.stencil_compare = CompareFunction::kEqual;
142 options.stencil_operation = StencilOperation::kIncrementClamp;
143 }
144 options.primitive_type = geometry_result.type;
145 options.ApplyToPipelineDescriptor(desc);
146
147 auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).Get();
148 if (!pipeline) {
149 VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
150 return false;
151 }
152
153 Command cmd;
154 DEBUG_COMMAND_INFO(cmd, "RuntimeEffectContents");
155 cmd.pipeline = pipeline;
156 cmd.stencil_reference = entity.GetStencilDepth();
157 cmd.BindVertices(geometry_result.vertex_buffer);
158
159 //--------------------------------------------------------------------------
160 /// Vertex stage uniforms.
161 ///
162
163 VS::FrameInfo frame_info;
164 frame_info.mvp = geometry_result.transform;
165 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
166
167 //--------------------------------------------------------------------------
168 /// Fragment stage uniforms.
169 ///
170
171 size_t minimum_sampler_index = 100000000;
172 size_t buffer_index = 0;
173 size_t buffer_offset = 0;
174 for (auto uniform : runtime_stage_->GetUniforms()) {
175 // TODO(113715): Populate this metadata once GLES is able to handle
176 // non-struct uniform names.
177 std::shared_ptr<ShaderMetadata> metadata =
178 std::make_shared<ShaderMetadata>();
179
180 switch (uniform.type) {
181 case kSampledImage: {
182 // Sampler uniforms are ordered in the IPLR according to their
183 // declaration and the uniform location reflects the correct offset to
184 // be mapped to - except that it may include all proceeding float
185 // uniforms. For example, a float sampler that comes after 4 float
186 // uniforms may have a location of 4. To convert to the actual offset we
187 // need to find the largest location assigned to a float uniform and
188 // then subtract this from all uniform locations. This is more or less
189 // the same operation we previously performed in the shader compiler.
190 minimum_sampler_index =
191 std::min(minimum_sampler_index, uniform.location);
192 break;
193 }
194 case kFloat: {
195 size_t alignment =
196 std::max(uniform.bit_width / 8, DefaultUniformAlignment());
197 auto buffer_view = pass.GetTransientsBuffer().Emplace(
198 uniform_data_->data() + buffer_offset, uniform.GetSize(),
199 alignment);
200
201 ShaderUniformSlot uniform_slot;
202 uniform_slot.name = uniform.name.c_str();
203 uniform_slot.ext_res_0 = uniform.location;
204 cmd.BindResource(ShaderStage::kFragment, uniform_slot, metadata,
205 buffer_view);
206 buffer_index++;
207 buffer_offset += uniform.GetSize();
208 break;
209 }
210 case kBoolean:
211 case kSignedByte:
212 case kUnsignedByte:
213 case kSignedShort:
214 case kUnsignedShort:
215 case kSignedInt:
216 case kUnsignedInt:
217 case kSignedInt64:
218 case kUnsignedInt64:
219 case kHalfFloat:
220 case kDouble:
221 VALIDATION_LOG << "Unsupported uniform type for " << uniform.name
222 << ".";
223 return true;
224 }
225 }
226
227 size_t sampler_index = 0;
228 for (auto uniform : runtime_stage_->GetUniforms()) {
229 // TODO(113715): Populate this metadata once GLES is able to handle
230 // non-struct uniform names.
231 ShaderMetadata metadata;
232
233 switch (uniform.type) {
234 case kSampledImage: {
235 FML_DCHECK(sampler_index < texture_inputs_.size());
236 auto& input = texture_inputs_[sampler_index];
237
238 auto sampler =
239 context->GetSamplerLibrary()->GetSampler(input.sampler_descriptor);
240
241 SampledImageSlot image_slot;
242 image_slot.name = uniform.name.c_str();
243 image_slot.texture_index = uniform.location - minimum_sampler_index;
244 image_slot.sampler_index = uniform.location - minimum_sampler_index;
245 cmd.BindResource(ShaderStage::kFragment, image_slot, metadata,
246 input.texture, sampler);
247
248 sampler_index++;
249 break;
250 }
251 default:
252 continue;
253 }
254 }
255
256 pass.AddCommand(std::move(cmd));
257
258 if (geometry_result.prevent_overdraw) {
259 auto restore = ClipRestoreContents();
260 restore.SetRestoreCoverage(GetCoverage(entity));
261 return restore.Render(renderer, entity, pass);
262 }
263 return true;
264 #endif // FML_OS_ANDROID
265 }
266
267 } // namespace impeller
impeller::PipelineDescriptor
Definition: pipeline_descriptor.h:30
impeller::kBoolean
@ kBoolean
Definition: runtime_types.h:14
impeller::kFloat
@ kFloat
Definition: runtime_types.h:24
impeller::Command
An object used to specify work to the GPU along with references to resources the GPU will used when d...
Definition: command.h:99
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:31
impeller::RuntimeEffectContents::SetTextureInputs
void SetTextureInputs(std::vector< TextureInput > texture_inputs)
Definition: runtime_effect_contents.cc:35
impeller::ShaderUniformSlot::ext_res_0
size_t ext_res_0
Definition: shader_types.h:83
impeller::RuntimeEffectContents::SetRuntimeStage
void SetRuntimeStage(std::shared_ptr< RuntimeStage > runtime_stage)
Definition: runtime_effect_contents.cc:25
impeller::SampledImageSlot::name
const char * name
Definition: shader_types.h:130
impeller::Entity::GetStencilDepth
uint32_t GetStencilDepth() const
Definition: entity.cc:89
impeller::ShaderUniformSlot
Definition: shader_types.h:81
shader_function.h
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:550
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
impeller::kUnsignedShort
@ kUnsignedShort
Definition: runtime_types.h:18
sampler_library.h
impeller::RuntimeEffectContents::CanInheritOpacity
bool CanInheritOpacity(const Entity &entity) const override
Whether or not this contents can accept the opacity peephole optimization.
Definition: runtime_effect_contents.cc:40
impeller::PipelineDescriptor::SetStencilAttachmentDescriptors
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
Definition: pipeline_descriptor.cc:153
impeller::PipelineDescriptor::SetColorAttachmentDescriptor
PipelineDescriptor & SetColorAttachmentDescriptor(size_t index, ColorAttachmentDescriptor desc)
Definition: pipeline_descriptor.cc:106
impeller::PipelineDescriptor::SetStencilPixelFormat
PipelineDescriptor & SetStencilPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:141
impeller::ShaderMetadata
Definition: shader_types.h:76
formats.h
impeller::kSignedByte
@ kSignedByte
Definition: runtime_types.h:15
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:87
impeller::ColorSourceContents::GetGeometry
const std::shared_ptr< Geometry > & GetGeometry() const
Get the geometry that this contents will use to render.
Definition: color_source_contents.cc:21
impeller::kSampledImage
@ kSampledImage
Definition: runtime_types.h:26
impeller::ShaderUniformSlot::name
const char * name
Definition: shader_types.h:82
impeller::RuntimeEffectContents::SetUniformData
void SetUniformData(std::shared_ptr< std::vector< uint8_t >> uniform_data)
Definition: runtime_effect_contents.cc:30
validation.h
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
runtime_effect_contents.h
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:30
impeller::kSignedInt
@ kSignedInt
Definition: runtime_types.h:19
render_pass.h
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:440
impeller::SampledImageSlot
Definition: shader_types.h:129
impeller::kUnsignedByte
@ kUnsignedByte
Definition: runtime_types.h:16
impeller::kUnsignedInt64
@ kUnsignedInt64
Definition: runtime_types.h:22
clip_contents.h
impeller::Command::BindResource
bool BindResource(ShaderStage stage, const ShaderUniformSlot &slot, const ShaderMetadata &metadata, const BufferView &view) override
Definition: command.cc:33
impeller::ShaderStage::kFragment
@ kFragment
impeller::kHalfFloat
@ kHalfFloat
Definition: runtime_types.h:23
impeller::kUnsignedInt
@ kUnsignedInt
Definition: runtime_types.h:20
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
impeller::ClipRestoreContents
Definition: clip_contents.h:57
impeller::kSignedShort
@ kSignedShort
Definition: runtime_types.h:17
pipeline_library.h
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:27
impeller::PipelineDescriptor::AddStageEntrypoint
PipelineDescriptor & AddStageEntrypoint(std::shared_ptr< const ShaderFunction > function)
Definition: pipeline_descriptor.cc:77
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:152
content_context.h
impeller::SampledImageSlot::sampler_index
size_t sampler_index
Definition: shader_types.h:132
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the screen space bounding rectangle that this contents affects.
Definition: color_source_contents.cc:45
impeller::kDouble
@ kDouble
Definition: runtime_types.h:25
impeller::ShaderStage::kVertex
@ kVertex
impeller::kSignedInt64
@ kSignedInt64
Definition: runtime_types.h:21
impeller::StencilAttachmentDescriptor
Definition: formats.h:544
impeller::HostBuffer::EmplaceUniform
BufferView EmplaceUniform(const UniformType &uniform)
Emplace uniform data onto the host buffer. Ensure that backend specific uniform alignment requirement...
Definition: host_buffer.h:43
impeller::RuntimeEffectContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: runtime_effect_contents.cc:44
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::SampledImageSlot::texture_index
size_t texture_index
Definition: shader_types.h:131
impeller::Command::BindVertices
bool BindVertices(const VertexBuffer &buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:103
impeller::PipelineDescriptor::SetLabel
PipelineDescriptor & SetLabel(std::string label)
Definition: pipeline_descriptor.cc:67
shader_types.h
impeller::RenderPass::AddCommand
bool AddCommand(Command &&command)
Record a command for subsequent encoding to the underlying command buffer. No work is encoded into th...
Definition: render_pass.cc:46
impeller
Definition: aiks_context.cc:10
impeller::ContentContext
Definition: content_context.h:344
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:34
impeller::PipelineDescriptor::SetVertexDescriptor
PipelineDescriptor & SetVertexDescriptor(std::shared_ptr< VertexDescriptor > vertex_descriptor)
Definition: pipeline_descriptor.cc:92