| 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/runtime_types.h"
15 #include "impeller/core/shader_types.h"
16 #include "impeller/entity/contents/clip_contents.h"
17 #include "impeller/entity/contents/content_context.h"
18 #include "impeller/entity/runtime_effect.vert.h"
19 #include "impeller/renderer/pipeline_library.h"
20 #include "impeller/renderer/render_pass.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 static ShaderType GetShaderType(RuntimeUniformType type) {
45 switch (type) {
46 case kSampledImage:
47 return ShaderType::kSampledImage;
48 case kFloat:
49 return ShaderType::kFloat;
50 case kBoolean:
51 case kSignedByte:
52 case kUnsignedByte:
53 case kSignedShort:
54 case kUnsignedShort:
55 case kSignedInt:
56 case kUnsignedInt:
57 case kSignedInt64:
58 case kUnsignedInt64:
59 case kHalfFloat:
60 case kDouble:
61 VALIDATION_LOG << "Unsupported uniform type.";
62 return ShaderType::kVoid;
63 }
64 }
65
66 static std::shared_ptr<ShaderMetadata> MakeShaderMetadata(
67 const RuntimeUniformDescription& uniform) {
68 auto metadata = std::make_shared<ShaderMetadata>();
69 metadata->name = uniform.name;
70 metadata->members.emplace_back(ShaderStructMemberMetadata{
71 .type = GetShaderType(uniform.type),
72 .size = uniform.GetSize(),
73 .byte_length = uniform.bit_width / 8,
74 });
75
76 return metadata;
77 }
78
79 bool RuntimeEffectContents::Render(const ContentContext& renderer,
80 const Entity& entity,
81 RenderPass& pass) const {
82 // TODO(jonahwilliams): FragmentProgram API is not fully wired up on Android.
83 // Disable until this is complete so that integration tests and benchmarks can
84 // run m3 applications.
85 if (renderer.GetContext()->GetBackendType() ==
86 Context::BackendType::kVulkan) {
87 FML_DLOG(WARNING)
88 << "Fragment programs not supported on Vulkan. Content dropped.";
89 return true;
90 }
91 auto context = renderer.GetContext();
92 auto library = context->GetShaderLibrary();
93
94 //--------------------------------------------------------------------------
95 /// Get or register shader.
96 ///
97
98 // TODO(113719): Register the shader function earlier.
99
100 std::shared_ptr<const ShaderFunction> function = library->GetFunction(
101 runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
102
103 if (function && runtime_stage_->IsDirty()) {
104 context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
105 library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
106 ShaderStage::kFragment);
107
108 function = nullptr;
109 }
110
111 if (!function) {
112 std::promise<bool> promise;
113 auto future = promise.get_future();
114
115 library->RegisterFunction(
116 runtime_stage_->GetEntrypoint(),
117 ToShaderStage(runtime_stage_->GetShaderStage()),
118 runtime_stage_->GetCodeMapping(),
119 fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
120 promise.set_value(result);
121 }));
122
123 if (!future.get()) {
124 VALIDATION_LOG << "Failed to build runtime effect (entry point: "
125 << runtime_stage_->GetEntrypoint() << ")";
126 return false;
127 }
128
129 function = library->GetFunction(runtime_stage_->GetEntrypoint(),
130 ShaderStage::kFragment);
131 if (!function) {
132 VALIDATION_LOG
133 << "Failed to fetch runtime effect function immediately after "
134 "registering it (entry point: "
135 << runtime_stage_->GetEntrypoint() << ")";
136 return false;
137 }
138
139 runtime_stage_->SetClean();
140 }
141
142 //--------------------------------------------------------------------------
143 /// Resolve geometry.
144 ///
145
146 auto geometry_result =
147 GetGeometry()->GetPositionBuffer(renderer, entity, pass);
148
149 //--------------------------------------------------------------------------
150 /// Get or create runtime stage pipeline.
151 ///
152
153 const auto& caps = context->GetCapabilities();
154 const auto color_attachment_format = caps->GetDefaultColorFormat();
155 const auto stencil_attachment_format = caps->GetDefaultStencilFormat();
156
157 using VS = RuntimeEffectVertexShader;
158 PipelineDescriptor desc;
159 desc.SetLabel("Runtime Stage");
160 desc.AddStageEntrypoint(
161 library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
162 desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
163 ShaderStage::kFragment));
164 auto vertex_descriptor = std::make_shared<VertexDescriptor>();
165 vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
166 VS::kInterleavedBufferLayout);
167 desc.SetVertexDescriptor(std::move(vertex_descriptor));
168 desc.SetColorAttachmentDescriptor(
169 0u, {.format = color_attachment_format, .blending_enabled = true});
170
171 StencilAttachmentDescriptor stencil0;
172 stencil0.stencil_compare = CompareFunction::kEqual;
173 desc.SetStencilAttachmentDescriptors(stencil0);
174 desc.SetStencilPixelFormat(stencil_attachment_format);
175
176 auto options = OptionsFromPassAndEntity(pass, entity);
177 if (geometry_result.prevent_overdraw) {
178 options.stencil_compare = CompareFunction::kEqual;
179 options.stencil_operation = StencilOperation::kIncrementClamp;
180 }
181 options.primitive_type = geometry_result.type;
182 options.ApplyToPipelineDescriptor(desc);
183
184 auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).Get();
185 if (!pipeline) {
186 VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
187 return false;
188 }
189
190 Command cmd;
191 DEBUG_COMMAND_INFO(cmd, "RuntimeEffectContents");
192 cmd.pipeline = pipeline;
193 cmd.stencil_reference = entity.GetClipDepth();
194 cmd.BindVertices(std::move(geometry_result.vertex_buffer));
195
196 //--------------------------------------------------------------------------
197 /// Vertex stage uniforms.
198 ///
199
200 VS::FrameInfo frame_info;
201 frame_info.mvp = geometry_result.transform;
202 VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));
203
204 //--------------------------------------------------------------------------
205 /// Fragment stage uniforms.
206 ///
207
208 size_t minimum_sampler_index = 100000000;
209 size_t buffer_index = 0;
210 size_t buffer_offset = 0;
211 for (const auto& uniform : runtime_stage_->GetUniforms()) {
212 std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
213
214 switch (uniform.type) {
215 case kSampledImage: {
216 // Sampler uniforms are ordered in the IPLR according to their
217 // declaration and the uniform location reflects the correct offset to
218 // be mapped to - except that it may include all proceeding float
219 // uniforms. For example, a float sampler that comes after 4 float
220 // uniforms may have a location of 4. To convert to the actual offset we
221 // need to find the largest location assigned to a float uniform and
222 // then subtract this from all uniform locations. This is more or less
223 // the same operation we previously performed in the shader compiler.
224 minimum_sampler_index =
225 std::min(minimum_sampler_index, uniform.location);
226 break;
227 }
228 case kFloat: {
229 size_t alignment =
230 std::max(uniform.bit_width / 8, DefaultUniformAlignment());
231 auto buffer_view = pass.GetTransientsBuffer().Emplace(
232 uniform_data_->data() + buffer_offset, uniform.GetSize(),
233 alignment);
234
235 ShaderUniformSlot uniform_slot;
236 uniform_slot.name = uniform.name.c_str();
237 uniform_slot.ext_res_0 = uniform.location;
238 cmd.BindResource(ShaderStage::kFragment, uniform_slot, metadata,
239 buffer_view);
240 buffer_index++;
241 buffer_offset += uniform.GetSize();
242 break;
243 }
244 case kBoolean:
245 case kSignedByte:
246 case kUnsignedByte:
247 case kSignedShort:
248 case kUnsignedShort:
249 case kSignedInt:
250 case kUnsignedInt:
251 case kSignedInt64:
252 case kUnsignedInt64:
253 case kHalfFloat:
254 case kDouble:
255 VALIDATION_LOG << "Unsupported uniform type for " << uniform.name
256 << ".";
257 return true;
258 }
259 }
260
261 size_t sampler_index = 0;
262 for (const auto& uniform : runtime_stage_->GetUniforms()) {
263 std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
264
265 switch (uniform.type) {
266 case kSampledImage: {
267 FML_DCHECK(sampler_index < texture_inputs_.size());
268 auto& input = texture_inputs_[sampler_index];
269
270 auto sampler =
271 context->GetSamplerLibrary()->GetSampler(input.sampler_descriptor);
272
273 SampledImageSlot image_slot;
274 image_slot.name = uniform.name.c_str();
275 image_slot.texture_index = uniform.location - minimum_sampler_index;
276 cmd.BindResource(ShaderStage::kFragment, image_slot, *metadata,
277 input.texture, sampler);
278
279 sampler_index++;
280 break;
281 }
282 default:
283 continue;
284 }
285 }
286
287 pass.AddCommand(std::move(cmd));
288
289 if (geometry_result.prevent_overdraw) {
290 auto restore = ClipRestoreContents();
291 restore.SetRestoreCoverage(GetCoverage(entity));
292 return restore.Render(renderer, entity, pass);
293 }
294 return true;
295 }
296
297 } // namespace impeller
impeller::PipelineDescriptor
Definition: pipeline_descriptor.h:29
impeller::ShaderStructMemberMetadata
Definition: shader_types.h:63
impeller::RuntimeUniformDescription::GetSize
size_t GetSize() const
Computes the total number of bytes that this uniform requires.
Definition: runtime_types.cc:9
impeller::RuntimeUniformDescription
Definition: runtime_types.h:48
impeller::kBoolean
@ kBoolean
Definition: runtime_types.h:22
impeller::kFloat
@ kFloat
Definition: runtime_types.h:32
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:92
DEBUG_COMMAND_INFO
#define DEBUG_COMMAND_INFO(obj, arg)
Definition: command.h:28
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
ext_res_0 is the Metal binding value.
Definition: shader_types.h:86
impeller::GetShaderType
static ShaderType GetShaderType(RuntimeUniformType type)
Definition: runtime_effect_contents.cc:44
impeller::RuntimeEffectContents::SetRuntimeStage
void SetRuntimeStage(std::shared_ptr< RuntimeStage > runtime_stage)
Definition: runtime_effect_contents.cc:25
impeller::SampledImageSlot::name
const char * name
The name of the uniform slot.
Definition: shader_types.h:100
impeller::ShaderUniformSlot
Metadata required to bind a buffer.
Definition: shader_types.h:81
impeller::MakeShaderMetadata
static std::shared_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
Definition: runtime_effect_contents.cc:66
shader_function.h
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:593
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:15
impeller::kUnsignedShort
@ kUnsignedShort
Definition: runtime_types.h:26
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::ShaderType
ShaderType
Definition: shader_types.h:41
impeller::PipelineDescriptor::SetStencilAttachmentDescriptors
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
Definition: pipeline_descriptor.cc:159
impeller::PipelineDescriptor::SetColorAttachmentDescriptor
PipelineDescriptor & SetColorAttachmentDescriptor(size_t index, ColorAttachmentDescriptor desc)
Definition: pipeline_descriptor.cc:112
impeller::PipelineDescriptor::SetStencilPixelFormat
PipelineDescriptor & SetStencilPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:147
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
formats.h
impeller::kSignedByte
@ kSignedByte
Definition: runtime_types.h:23
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:85
impeller::StencilOperation::kIncrementClamp
@ kIncrementClamp
Increment the current stencil value by 1. Clamp it to the maximum.
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:34
impeller::ShaderType::kVoid
@ kVoid
impeller::ShaderUniformSlot::name
const char * name
The name of the uniform slot.
Definition: shader_types.h:83
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_types.h
runtime_effect_contents.h
impeller::ShaderType::kFloat
@ kFloat
impeller::Entity
Definition: entity.h:21
impeller::OptionsFromPassAndEntity
ContentContextOptions OptionsFromPassAndEntity(const RenderPass &pass, const Entity &entity)
Definition: contents.cc:28
impeller::kSignedInt
@ kSignedInt
Definition: runtime_types.h:27
render_pass.h
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:479
impeller::RuntimeUniformType
RuntimeUniformType
Definition: runtime_types.h:21
impeller::RuntimeUniformDescription::name
std::string name
Definition: runtime_types.h:49
impeller::SampledImageSlot
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:98
impeller::Command::BindVertices
bool BindVertices(VertexBuffer buffer)
Specify the vertex and index buffer to use for this command.
Definition: command.cc:15
impeller::ShaderType::kSampledImage
@ kSampledImage
impeller::kUnsignedByte
@ kUnsignedByte
Definition: runtime_types.h:24
impeller::kUnsignedInt64
@ kUnsignedInt64
Definition: runtime_types.h:30
clip_contents.h
impeller::ShaderStage::kFragment
@ kFragment
impeller::kHalfFloat
@ kHalfFloat
Definition: runtime_types.h:31
impeller::kUnsignedInt
@ kUnsignedInt
Definition: runtime_types.h:28
impeller::RuntimeUniformDescription::type
RuntimeUniformType type
Definition: runtime_types.h:51
impeller::ClipRestoreContents
Definition: clip_contents.h:60
impeller::kSignedShort
@ kSignedShort
Definition: runtime_types.h:25
pipeline_library.h
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:29
impeller::Entity::GetClipDepth
uint32_t GetClipDepth() const
Definition: entity.cc:93
impeller::PipelineDescriptor::AddStageEntrypoint
PipelineDescriptor & AddStageEntrypoint(std::shared_ptr< const ShaderFunction > function)
Definition: pipeline_descriptor.cc:83
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:67
impeller::Context::BackendType::kVulkan
@ kVulkan
impeller::Command::stencil_reference
uint32_t stencil_reference
Definition: command.h:122
content_context.h
impeller::RuntimeUniformDescription::bit_width
size_t bit_width
Definition: runtime_types.h:53
impeller::ColorSourceContents::GetCoverage
std::optional< Rect > GetCoverage(const Entity &entity) const override
Get the area of the render pass that will be affected when this contents is rendered.
Definition: color_source_contents.cc:45
impeller::kDouble
@ kDouble
Definition: runtime_types.h:33
impeller::ShaderStage::kVertex
@ kVertex
impeller::kSignedInt64
@ kSignedInt64
Definition: runtime_types.h:29
impeller::StencilAttachmentDescriptor
Definition: formats.h:587
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:41
impeller::RuntimeEffectContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: runtime_effect_contents.cc:79
impeller::SampledImageSlot::texture_index
size_t texture_index
ext_res_0 is the Metal binding value.
Definition: shader_types.h:103
impeller::ShaderStructMemberMetadata::type
ShaderType type
Definition: shader_types.h:64
impeller::Command::pipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > pipeline
Definition: command.h:96
impeller::PipelineDescriptor::SetLabel
PipelineDescriptor & SetLabel(std::string label)
Definition: pipeline_descriptor.cc:73
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:67
impeller
Definition: aiks_context.cc:10
impeller::ContentContext
Definition: content_context.h:332
impeller::Command::BindResource
bool BindResource(ShaderStage stage, const ShaderUniformSlot &slot, const ShaderMetadata &metadata, BufferView view) override
Definition: command.cc:25
impeller::RenderPass::GetTransientsBuffer
HostBuffer & GetTransientsBuffer()
Definition: render_pass.cc:55
impeller::PipelineDescriptor::SetVertexDescriptor
PipelineDescriptor & SetVertexDescriptor(std::shared_ptr< VertexDescriptor > vertex_descriptor)
Definition: pipeline_descriptor.cc:98