Flutter Engine
The Flutter Engine
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
6
7#include <algorithm>
8#include <future>
9#include <memory>
10
11#include "flutter/fml/logging.h"
12#include "flutter/fml/make_copyable.h"
18#include "impeller/entity/runtime_effect.vert.h"
24
25namespace impeller {
26
28 std::shared_ptr<RuntimeStage> runtime_stage) {
29 runtime_stage_ = std::move(runtime_stage);
30}
31
33 std::shared_ptr<std::vector<uint8_t>> uniform_data) {
34 uniform_data_ = std::move(uniform_data);
35}
36
38 std::vector<TextureInput> texture_inputs) {
39 texture_inputs_ = std::move(texture_inputs);
40}
41
43 return false;
44}
45
47 switch (type) {
48 case kSampledImage:
50 case kFloat:
51 return ShaderType::kFloat;
52 case kStruct:
54 }
55}
56
57static std::shared_ptr<ShaderMetadata> MakeShaderMetadata(
58 const RuntimeUniformDescription& uniform) {
59 auto metadata = std::make_shared<ShaderMetadata>();
60 metadata->name = uniform.name;
61 metadata->members.emplace_back(ShaderStructMemberMetadata{
62 .type = GetShaderType(uniform.type),
63 .size = uniform.GetSize(),
64 .byte_length = uniform.bit_width / 8,
65 });
66
67 return metadata;
68}
69
71 const ContentContext& renderer) const {
72 if (!RegisterShader(renderer)) {
73 return false;
74 }
76 options.color_attachment_pixel_format =
77 renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
78 CreatePipeline(renderer, options, /*async=*/true);
79 return true;
80}
81
82bool RuntimeEffectContents::RegisterShader(
83 const ContentContext& renderer) const {
84 const std::shared_ptr<Context>& context = renderer.GetContext();
85 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
86
87 std::shared_ptr<const ShaderFunction> function = library->GetFunction(
88 runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
89
90 //--------------------------------------------------------------------------
91 /// Resolve runtime stage function.
92 ///
93
94 if (function && runtime_stage_->IsDirty()) {
95 renderer.ClearCachedRuntimeEffectPipeline(runtime_stage_->GetEntrypoint());
96 context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
97 library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
99
100 function = nullptr;
101 }
102
103 if (!function) {
104 std::promise<bool> promise;
105 auto future = promise.get_future();
106
107 library->RegisterFunction(
108 runtime_stage_->GetEntrypoint(),
109 ToShaderStage(runtime_stage_->GetShaderStage()),
110 runtime_stage_->GetCodeMapping(),
111 fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
112 promise.set_value(result);
113 }));
114
115 if (!future.get()) {
116 VALIDATION_LOG << "Failed to build runtime effect (entry point: "
117 << runtime_stage_->GetEntrypoint() << ")";
118 return false;
119 }
120
121 function = library->GetFunction(runtime_stage_->GetEntrypoint(),
123 if (!function) {
125 << "Failed to fetch runtime effect function immediately after "
126 "registering it (entry point: "
127 << runtime_stage_->GetEntrypoint() << ")";
128 return false;
129 }
130
131 runtime_stage_->SetClean();
132 }
133 return true;
134}
135
136std::shared_ptr<Pipeline<PipelineDescriptor>>
137RuntimeEffectContents::CreatePipeline(const ContentContext& renderer,
138 ContentContextOptions options,
139 bool async) const {
140 const std::shared_ptr<Context>& context = renderer.GetContext();
141 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
142 const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
143 const auto color_attachment_format = caps->GetDefaultColorFormat();
144 const auto stencil_attachment_format = caps->GetDefaultDepthStencilFormat();
145
146 using VS = RuntimeEffectVertexShader;
147
148 PipelineDescriptor desc;
149 desc.SetLabel("Runtime Stage");
150 desc.AddStageEntrypoint(
151 library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
152 desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
154
155 std::shared_ptr<VertexDescriptor> vertex_descriptor =
156 std::make_shared<VertexDescriptor>();
157 vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
158 VS::kInterleavedBufferLayout);
159 vertex_descriptor->RegisterDescriptorSetLayouts(VS::kDescriptorSetLayouts);
160 vertex_descriptor->RegisterDescriptorSetLayouts(
161 runtime_stage_->GetDescriptorSetLayouts().data(),
162 runtime_stage_->GetDescriptorSetLayouts().size());
163 desc.SetVertexDescriptor(std::move(vertex_descriptor));
164 desc.SetColorAttachmentDescriptor(
165 0u, {.format = color_attachment_format, .blending_enabled = true});
166
167 desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
168 desc.SetStencilPixelFormat(stencil_attachment_format);
169
170 desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
171 desc.SetDepthPixelFormat(stencil_attachment_format);
172
173 options.ApplyToPipelineDescriptor(desc);
174 if (async) {
175 context->GetPipelineLibrary()->GetPipeline(desc, async);
176 return nullptr;
177 }
178
179 auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc, async).Get();
180 if (!pipeline) {
181 VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
182 return nullptr;
183 }
184
185 return pipeline;
186}
187
189 const Entity& entity,
190 RenderPass& pass) const {
191 const std::shared_ptr<Context>& context = renderer.GetContext();
192 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
193
194 //--------------------------------------------------------------------------
195 /// Get or register shader. Flutter will do this when the runtime effect
196 /// is first loaded, but this check is added to supporting testing of the
197 /// Aiks API and non-flutter usage of Impeller.
198 ///
199 if (!RegisterShader(renderer)) {
200 return false;
201 }
202
203 //--------------------------------------------------------------------------
204 /// Fragment stage uniforms.
205 ///
206 BindFragmentCallback bind_callback = [this, &renderer,
207 &context](RenderPass& pass) {
208 size_t minimum_sampler_index = 100000000;
209 size_t buffer_index = 0;
210 size_t buffer_offset = 0;
211
212 for (const auto& uniform : runtime_stage_->GetUniforms()) {
213 std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
214
215 switch (uniform.type) {
216 case kSampledImage: {
217 // Sampler uniforms are ordered in the IPLR according to their
218 // declaration and the uniform location reflects the correct offset to
219 // be mapped to - except that it may include all proceeding float
220 // uniforms. For example, a float sampler that comes after 4 float
221 // uniforms may have a location of 4. To convert to the actual offset
222 // we need to find the largest location assigned to a float uniform
223 // and then subtract this from all uniform locations. This is more or
224 // less the same operation we previously performed in the shader
225 // compiler.
226 minimum_sampler_index =
227 std::min(minimum_sampler_index, uniform.location);
228 break;
229 }
230 case kFloat: {
231 FML_DCHECK(renderer.GetContext()->GetBackendType() !=
233 << "Uniform " << uniform.name
234 << " had unexpected type kFloat for Vulkan backend.";
235
236 size_t alignment =
237 std::max(uniform.bit_width / 8, DefaultUniformAlignment());
238 BufferView buffer_view = renderer.GetTransientsBuffer().Emplace(
239 uniform_data_->data() + buffer_offset, uniform.GetSize(),
240 alignment);
241
242 ShaderUniformSlot uniform_slot;
243 uniform_slot.name = uniform.name.c_str();
244 uniform_slot.ext_res_0 = uniform.location;
246 DescriptorType::kUniformBuffer, uniform_slot,
247 metadata, std::move(buffer_view));
248 buffer_index++;
249 buffer_offset += uniform.GetSize();
250 break;
251 }
252 case kStruct: {
253 FML_DCHECK(renderer.GetContext()->GetBackendType() ==
255 ShaderUniformSlot uniform_slot;
256 uniform_slot.name = uniform.name.c_str();
257 uniform_slot.binding = uniform.location;
258
259 // TODO(jonahwilliams): rewrite this to emplace directly into
260 // HostBuffer.
261 std::vector<float> uniform_buffer;
262 uniform_buffer.reserve(uniform.struct_layout.size());
263 size_t uniform_byte_index = 0u;
264 for (const auto& byte_type : uniform.struct_layout) {
265 if (byte_type == 0) {
266 uniform_buffer.push_back(0.f);
267 } else if (byte_type == 1) {
268 uniform_buffer.push_back(reinterpret_cast<float*>(
269 uniform_data_->data())[uniform_byte_index++]);
270 } else {
272 }
273 }
274 size_t alignment = std::max(sizeof(float) * uniform_buffer.size(),
276
277 BufferView buffer_view = renderer.GetTransientsBuffer().Emplace(
278 reinterpret_cast<const void*>(uniform_buffer.data()),
279 sizeof(float) * uniform_buffer.size(), alignment);
281 DescriptorType::kUniformBuffer, uniform_slot,
282 ShaderMetadata{}, std::move(buffer_view));
283 }
284 }
285 }
286
287 size_t sampler_index = 0;
288 for (const auto& uniform : runtime_stage_->GetUniforms()) {
289 std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
290
291 switch (uniform.type) {
292 case kSampledImage: {
293 FML_DCHECK(sampler_index < texture_inputs_.size());
294 auto& input = texture_inputs_[sampler_index];
295
296 const std::unique_ptr<const Sampler>& sampler =
297 context->GetSamplerLibrary()->GetSampler(
298 input.sampler_descriptor);
299
300 SampledImageSlot image_slot;
301 image_slot.name = uniform.name.c_str();
302 image_slot.binding = uniform.binding;
303 image_slot.texture_index = uniform.location - minimum_sampler_index;
306 *metadata, input.texture, sampler);
307
308 sampler_index++;
309 break;
310 }
311 default:
312 continue;
313 }
314 }
315 return true;
316 };
317
318 /// Now that the descriptor set layouts are known, get the pipeline.
319 using VS = RuntimeEffectVertexShader;
320
321 PipelineBuilderCallback pipeline_callback =
323 // Pipeline creation callback for the cache handler to call.
324 return renderer.GetCachedRuntimeEffectPipeline(
325 runtime_stage_->GetEntrypoint(), options, [&]() {
326 return CreatePipeline(renderer, options, /*async=*/false);
327 });
328 };
329
330 return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
331 pipeline_callback,
332 VS::FrameInfo{}, bind_callback);
333}
334
335} // namespace impeller
const char * options
GLenum type
BufferView buffer_view
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
virtual bool BindResource(ShaderStage stage, DescriptorType type, const ShaderUniformSlot &slot, const ShaderMetadata &metadata, BufferView view) override
Definition: render_pass.cc:138
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
bool BootstrapShader(const ContentContext &renderer) const
Load the runtime effect and ensure a default PSO is initialized.
void SetRuntimeStage(std::shared_ptr< RuntimeStage > runtime_stage)
void SetTextureInputs(std::vector< TextureInput > texture_inputs)
void SetUniformData(std::shared_ptr< std::vector< uint8_t > > uniform_data)
bool CanInheritOpacity(const Entity &entity) const override
Whether or not this contents can accept the opacity peephole optimization.
GAsyncResult * result
#define FML_UNREACHABLE()
Definition: logging.h:109
#define FML_DCHECK(condition)
Definition: logging.h:103
Dart_NativeFunction function
Definition: fuchsia.cc:51
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57
static std::shared_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
SolidFillVertexShader VS
static ShaderType GetShaderType(RuntimeUniformType type)
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
size_t GetSize() const
Computes the total number of bytes that this uniform requires.
Definition: runtime_types.cc:9
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:98
size_t texture_index
ext_res_0 is the Metal binding value.
Definition: shader_types.h:103
const char * name
The name of the uniform slot.
Definition: shader_types.h:100
size_t binding
The Vulkan binding value.
Definition: shader_types.h:109
Metadata required to bind a buffer.
Definition: shader_types.h:81
size_t binding
The Vulkan binding value.
Definition: shader_types.h:92
size_t ext_res_0
ext_res_0 is the Metal binding value.
Definition: shader_types.h:86
const char * name
The name of the uniform slot.
Definition: shader_types.h:83
#define VALIDATION_LOG
Definition: validation.h:73