Flutter Engine
 
Loading...
Searching...
No Matches
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"
18#include "impeller/entity/runtime_effect.vert.h"
24
25namespace impeller {
26
27namespace {
28constexpr char kPaddingType = 0;
29constexpr char kFloatType = 1;
30} // namespace
31
32// static
34 const std::shared_ptr<const std::vector<uint8_t>>& input_data,
35 HostBuffer& data_host_buffer,
36 const RuntimeUniformDescription& uniform,
37 size_t minimum_uniform_alignment) {
38 // TODO(jonahwilliams): rewrite this to emplace directly into
39 // HostBuffer.
40 std::vector<float> uniform_buffer;
41 uniform_buffer.reserve(uniform.struct_layout.size());
42 size_t uniform_byte_index = 0u;
43 for (char byte_type : uniform.struct_layout) {
44 if (byte_type == kPaddingType) {
45 uniform_buffer.push_back(0.f);
46 } else {
47 FML_DCHECK(byte_type == kFloatType);
48 uniform_buffer.push_back(reinterpret_cast<const float*>(
49 input_data->data())[uniform_byte_index++]);
50 }
51 }
52
53 return data_host_buffer.Emplace(
54 reinterpret_cast<const void*>(uniform_buffer.data()),
55 sizeof(float) * uniform_buffer.size(), minimum_uniform_alignment);
56}
57
59 std::shared_ptr<RuntimeStage> runtime_stage) {
60 runtime_stage_ = std::move(runtime_stage);
61}
62
64 std::shared_ptr<std::vector<uint8_t>> uniform_data) {
65 uniform_data_ = std::move(uniform_data);
66}
67
69 std::vector<TextureInput> texture_inputs) {
70 texture_inputs_ = std::move(texture_inputs);
71}
72
74 switch (type) {
75 case kSampledImage:
77 case kFloat:
78 return ShaderType::kFloat;
79 case kStruct:
81 }
82}
83
84static std::unique_ptr<ShaderMetadata> MakeShaderMetadata(
85 const RuntimeUniformDescription& uniform) {
86 std::unique_ptr<ShaderMetadata> metadata = std::make_unique<ShaderMetadata>();
87 metadata->name = uniform.name;
88 metadata->members.emplace_back(ShaderStructMemberMetadata{
89 .type = GetShaderType(uniform.type), //
90 .size = uniform.dimensions.rows * uniform.dimensions.cols *
91 (uniform.bit_width / 8u), //
92 .byte_length =
93 (uniform.bit_width / 8u) * uniform.array_elements.value_or(1), //
94 .array_elements = uniform.array_elements //
95 });
96
97 return metadata;
98}
99
101 const ContentContext& renderer) const {
102 if (!RegisterShader(renderer)) {
103 return false;
104 }
105 ContentContextOptions options;
107 renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
108 CreatePipeline(renderer, options, /*async=*/true);
109 return true;
110}
111
112bool RuntimeEffectContents::RegisterShader(
113 const ContentContext& renderer) const {
114 const std::shared_ptr<Context>& context = renderer.GetContext();
115 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
116
117 std::shared_ptr<const ShaderFunction> function = library->GetFunction(
118 runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
119
120 //--------------------------------------------------------------------------
121 /// Resolve runtime stage function.
122 ///
123
124 if (function && runtime_stage_->IsDirty()) {
125 renderer.ClearCachedRuntimeEffectPipeline(runtime_stage_->GetEntrypoint());
126 context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
127 library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
129
130 function = nullptr;
131 }
132
133 if (!function) {
134 std::promise<bool> promise;
135 auto future = promise.get_future();
136
137 library->RegisterFunction(
138 runtime_stage_->GetEntrypoint(),
139 ToShaderStage(runtime_stage_->GetShaderStage()),
140 runtime_stage_->GetCodeMapping(),
141 fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
142 promise.set_value(result);
143 }));
144
145 if (!future.get()) {
146 VALIDATION_LOG << "Failed to build runtime effect (entry point: "
147 << runtime_stage_->GetEntrypoint() << ")";
148 return false;
149 }
150
151 function = library->GetFunction(runtime_stage_->GetEntrypoint(),
153 if (!function) {
155 << "Failed to fetch runtime effect function immediately after "
156 "registering it (entry point: "
157 << runtime_stage_->GetEntrypoint() << ")";
158 return false;
159 }
160
161 runtime_stage_->SetClean();
162 }
163 return true;
164}
165
166std::shared_ptr<Pipeline<PipelineDescriptor>>
167RuntimeEffectContents::CreatePipeline(const ContentContext& renderer,
168 ContentContextOptions options,
169 bool async) const {
170 const std::shared_ptr<Context>& context = renderer.GetContext();
171 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
172 const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
173 const PixelFormat color_attachment_format = caps->GetDefaultColorFormat();
174 const PixelFormat stencil_attachment_format =
175 caps->GetDefaultDepthStencilFormat();
176
177 using VS = RuntimeEffectVertexShader;
178
179 PipelineDescriptor desc;
180 desc.SetLabel("Runtime Stage");
181 desc.AddStageEntrypoint(
182 library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
183 desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
185
186 std::shared_ptr<VertexDescriptor> vertex_descriptor =
187 std::make_shared<VertexDescriptor>();
188 vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
189 VS::kInterleavedBufferLayout);
190 vertex_descriptor->RegisterDescriptorSetLayouts(VS::kDescriptorSetLayouts);
191 vertex_descriptor->RegisterDescriptorSetLayouts(
192 runtime_stage_->GetDescriptorSetLayouts().data(),
193 runtime_stage_->GetDescriptorSetLayouts().size());
194 desc.SetVertexDescriptor(std::move(vertex_descriptor));
195 desc.SetColorAttachmentDescriptor(
196 0u, {.format = color_attachment_format, .blending_enabled = true});
197
198 desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
199 desc.SetStencilPixelFormat(stencil_attachment_format);
200
201 desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
202 desc.SetDepthPixelFormat(stencil_attachment_format);
203
204 options.ApplyToPipelineDescriptor(desc);
205 if (async) {
206 context->GetPipelineLibrary()->GetPipeline(desc, async);
207 return nullptr;
208 }
209
210 auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc, async).Get();
211 if (!pipeline) {
212 VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
213 return nullptr;
214 }
215
216 return pipeline;
217}
218
220 const Entity& entity,
221 RenderPass& pass) const {
222 const std::shared_ptr<Context>& context = renderer.GetContext();
223 const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
224
225 //--------------------------------------------------------------------------
226 /// Get or register shader. Flutter will do this when the runtime effect
227 /// is first loaded, but this check is added to supporting testing of the
228 /// Aiks API and non-flutter usage of Impeller.
229 ///
230 if (!RegisterShader(renderer)) {
231 return false;
232 }
233
234 //--------------------------------------------------------------------------
235 /// Fragment stage uniforms.
236 ///
237 BindFragmentCallback bind_callback = [this, &renderer,
238 &context](RenderPass& pass) {
239 size_t buffer_index = 0;
240 size_t buffer_offset = 0;
241 size_t sampler_location = 0;
242 size_t buffer_location = 0;
243
244 // Uniforms are ordered in the IPLR according to their
245 // declaration and the uniform location reflects the correct offset to
246 // be mapped to - except that it may include all proceeding
247 // uniforms of a different type. For example, a texture sampler that comes
248 // after 4 float uniforms may have a location of 4. Since we know that
249 // the declarations are already ordered, we can track the uniform location
250 // ourselves.
251 auto& data_host_buffer = renderer.GetTransientsDataBuffer();
252 for (const auto& uniform : runtime_stage_->GetUniforms()) {
253 std::unique_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
254 switch (uniform.type) {
255 case kSampledImage: {
256 FML_DCHECK(sampler_location < texture_inputs_.size());
257 auto& input = texture_inputs_[sampler_location];
258
259 raw_ptr<const Sampler> sampler =
260 context->GetSamplerLibrary()->GetSampler(
261 input.sampler_descriptor);
262
263 SampledImageSlot image_slot;
264 image_slot.name = uniform.name.c_str();
265 image_slot.binding = uniform.binding;
266 image_slot.texture_index = sampler_location;
269 std::move(metadata), input.texture, sampler);
270 sampler_location++;
271 break;
272 }
273 case kFloat: {
274 FML_DCHECK(renderer.GetContext()->GetBackendType() !=
276 << "Uniform " << uniform.name
277 << " had unexpected type kFloat for Vulkan backend.";
278
279 size_t alignment =
280 std::max(uniform.bit_width / 8,
281 data_host_buffer.GetMinimumUniformAlignment());
283 data_host_buffer.Emplace(uniform_data_->data() + buffer_offset,
284 uniform.GetSize(), alignment);
285
286 ShaderUniformSlot uniform_slot;
287 uniform_slot.name = uniform.name.c_str();
288 uniform_slot.ext_res_0 = buffer_location;
290 DescriptorType::kUniformBuffer, uniform_slot,
291 std::move(metadata), std::move(buffer_view));
292 buffer_index++;
293 buffer_offset += uniform.GetSize();
294 buffer_location++;
295 break;
296 }
297 case kStruct: {
298 FML_DCHECK(renderer.GetContext()->GetBackendType() ==
300 ShaderUniformSlot uniform_slot;
301 uniform_slot.binding = uniform.location;
302 uniform_slot.name = uniform.name.c_str();
303
305 DescriptorType::kUniformBuffer, uniform_slot,
306 nullptr,
308 uniform_data_, data_host_buffer, uniform,
309 data_host_buffer.GetMinimumUniformAlignment()));
310 }
311 }
312 }
313
314 return true;
315 };
316
317 /// Now that the descriptor set layouts are known, get the pipeline.
318 using VS = RuntimeEffectVertexShader;
319
320 PipelineBuilderCallback pipeline_callback =
321 [&](ContentContextOptions options) {
322 // Pipeline creation callback for the cache handler to call.
323 return renderer.GetCachedRuntimeEffectPipeline(
324 runtime_stage_->GetEntrypoint(), options, [&]() {
325 return CreatePipeline(renderer, options, /*async=*/false);
326 });
327 };
328
329 return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
330 pipeline_callback,
331 VS::FrameInfo{}, bind_callback);
332}
333
334} // namespace impeller
GLenum type
BufferView buffer_view
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
void ClearCachedRuntimeEffectPipeline(const std::string &unique_entrypoint_name) const
PipelineRef GetCachedRuntimeEffectPipeline(const std::string &unique_entrypoint_name, const ContentContextOptions &options, const std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >()> &create_callback) const
std::shared_ptr< Context > GetContext() const
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition host_buffer.h:92
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition render_pass.h:30
virtual bool BindDynamicResource(ShaderStage stage, DescriptorType type, const SampledImageSlot &slot, std::unique_ptr< ShaderMetadata > metadata, std::shared_ptr< const Texture > texture, raw_ptr< const Sampler >)
Bind with dynamically generated shader metadata.
virtual bool BindResource(ShaderStage stage, DescriptorType type, const ShaderUniformSlot &slot, const ShaderMetadata *metadata, BufferView view) override
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)
static BufferView EmplaceVulkanUniform(const std::shared_ptr< const std::vector< uint8_t > > &input_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform, size_t minimum_uniform_alignment)
A wrapper around a raw ptr that adds additional unopt mode only checks.
Definition raw_ptr.h:15
static int input(yyscan_t yyscanner)
#define FML_DCHECK(condition)
Definition logging.h:122
Dart_NativeFunction function
Definition fuchsia.cc:50
internal::CopyableLambda< T > MakeCopyable(T lambda)
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition formats.h:99
static ShaderType GetShaderType(RuntimeUniformType type)
static std::unique_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
LinePipeline::VertexShader VS
RuntimeUniformDimensions dimensions
std::vector< uint8_t > struct_layout
std::optional< size_t > array_elements
Metadata required to bind a combined texture and sampler.
size_t texture_index
ext_res_0 is the Metal binding value.
const char * name
The name of the uniform slot.
size_t binding
The Vulkan binding value.
Metadata required to bind a buffer.
size_t binding
The Vulkan binding value.
size_t ext_res_0
ext_res_0 is the Metal binding value.
const char * name
The name of the uniform slot.
#define VALIDATION_LOG
Definition validation.h:91