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 
6 
7 #include <algorithm>
8 #include <future>
9 #include <memory>
10 
11 #include "flutter/fml/logging.h"
12 #include "flutter/fml/make_copyable.h"
14 #include "impeller/core/formats.h"
18 #include "impeller/entity/runtime_effect.vert.h"
24 
25 namespace impeller {
26 
27 // static
29  const uint8_t* source_data,
30  HostBuffer& data_host_buffer,
31  const RuntimeUniformDescription& uniform) {
32  size_t minimum_uniform_alignment =
33  data_host_buffer.GetMinimumUniformAlignment();
34  size_t alignment = std::max(uniform.bit_width / 8, minimum_uniform_alignment);
35 
36  if (uniform.padding_layout.empty()) {
37  return data_host_buffer.Emplace(source_data, uniform.GetGPUSize(),
38  alignment);
39  }
40 
41  // If the uniform has a padding layout, we need to repack the data.
42  // We can do this by using the EmplaceProc to write directly to the
43  // HostBuffer.
44  return data_host_buffer.Emplace(
45  uniform.GetGPUSize(), alignment,
46  [&uniform, source_data](uint8_t* destination) {
47  size_t count = uniform.array_elements.value_or(1);
48  if (count == 0) {
49  // Make sure to run at least once.
50  count = 1;
51  }
52  size_t uniform_byte_index = 0u;
53  size_t struct_float_index = 0u;
54  auto* float_destination = reinterpret_cast<float*>(destination);
55  auto* float_source = reinterpret_cast<const float*>(source_data);
56 
57  for (size_t i = 0; i < count; i++) {
58  for (RuntimePaddingType byte_type : uniform.padding_layout) {
59  if (byte_type == RuntimePaddingType::kPadding) {
60  float_destination[struct_float_index++] = 0.f;
61  } else {
62  FML_DCHECK(byte_type == RuntimePaddingType::kFloat);
63  float_destination[struct_float_index++] =
64  float_source[uniform_byte_index++];
65  }
66  }
67  }
68  });
69 }
70 
71 RuntimeEffectContents::RuntimeEffectContents(const Geometry* geometry)
72  : geometry_(geometry) {}
73 
75 
77  return geometry_;
78 }
79 
81  std::shared_ptr<RuntimeStage> runtime_stage) {
82  runtime_stage_ = std::move(runtime_stage);
83 }
84 
86  std::shared_ptr<std::vector<uint8_t>> uniform_data) {
87  uniform_data_ = std::move(uniform_data);
88 }
89 
91  std::vector<TextureInput> texture_inputs) {
92  texture_inputs_ = std::move(texture_inputs);
93 }
94 
96  switch (type) {
97  case kSampledImage:
99  case kFloat:
100  return ShaderType::kFloat;
101  case kStruct:
102  return ShaderType::kStruct;
103  }
104 }
105 
106 static std::unique_ptr<ShaderMetadata> MakeShaderMetadata(
107  const RuntimeUniformDescription& uniform) {
108  std::unique_ptr<ShaderMetadata> metadata = std::make_unique<ShaderMetadata>();
109  metadata->name = uniform.name;
110 
111  // If the element is not an array, then the runtime stage flatbuffer will
112  // represent the unspecified array_elements as the default value of 0.
113  std::optional<size_t> array_elements;
114  if (uniform.array_elements.value_or(0) > 0) {
115  array_elements = uniform.array_elements;
116  }
117 
118  size_t member_size = uniform.dimensions.rows * uniform.dimensions.cols *
119  (uniform.bit_width / 8u);
120 
121  ShaderType shader_type = GetShaderType(uniform.type);
122  std::optional<ShaderFloatType> float_type;
123  if (shader_type == ShaderType::kFloat) {
124  if (uniform.dimensions.cols == 1) {
125  switch (uniform.dimensions.rows) {
126  case 1:
127  float_type = ShaderFloatType::kFloat;
128  break;
129  case 2:
130  float_type = ShaderFloatType::kVec2;
131  break;
132  case 3:
133  float_type = ShaderFloatType::kVec3;
134  break;
135  case 4:
136  float_type = ShaderFloatType::kVec4;
137  break;
138  }
139  } else if (uniform.dimensions.rows == uniform.dimensions.cols) {
140  switch (uniform.dimensions.rows) {
141  case 2:
142  float_type = ShaderFloatType::kMat2;
143  break;
144  case 3:
145  float_type = ShaderFloatType::kMat3;
146  break;
147  case 4:
148  float_type = ShaderFloatType::kMat4;
149  break;
150  }
151  }
152  }
153 
154  metadata->members.emplace_back(ShaderStructMemberMetadata{
155  .type = shader_type, //
156  .size = member_size, //
157  .byte_length = member_size * array_elements.value_or(1), //
158  .array_elements = array_elements, //
159  .float_type = float_type, //
160  });
161 
162  return metadata;
163 }
164 
166  const ContentContext& renderer) const {
167  if (!RegisterShader(renderer)) {
168  return false;
169  }
170  ContentContextOptions options;
172  renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
173  CreatePipeline(renderer, options, /*async=*/true);
174  return true;
175 }
176 
177 bool RuntimeEffectContents::RegisterShader(
178  const ContentContext& renderer) const {
179  const std::shared_ptr<Context>& context = renderer.GetContext();
180  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
181 
182  std::shared_ptr<const ShaderFunction> function = library->GetFunction(
183  runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
184 
185  //--------------------------------------------------------------------------
186  /// Resolve runtime stage function.
187  ///
188 
189  if (function && runtime_stage_->IsDirty()) {
190  renderer.ClearCachedRuntimeEffectPipeline(runtime_stage_->GetEntrypoint());
191  context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
192  library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
194 
195  function = nullptr;
196  }
197 
198  if (!function) {
199  std::promise<bool> promise;
200  auto future = promise.get_future();
201 
202  library->RegisterFunction(
203  runtime_stage_->GetEntrypoint(),
204  ToShaderStage(runtime_stage_->GetShaderStage()),
205  runtime_stage_->GetCodeMapping(),
206  fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
207  promise.set_value(result);
208  }));
209 
210  if (!future.get()) {
211  VALIDATION_LOG << "Failed to build runtime effect (entry point: "
212  << runtime_stage_->GetEntrypoint() << ")";
213  return false;
214  }
215 
216  function = library->GetFunction(runtime_stage_->GetEntrypoint(),
218  if (!function) {
220  << "Failed to fetch runtime effect function immediately after "
221  "registering it (entry point: "
222  << runtime_stage_->GetEntrypoint() << ")";
223  return false;
224  }
225 
226  runtime_stage_->SetClean();
227  }
228  return true;
229 }
230 
231 std::shared_ptr<Pipeline<PipelineDescriptor>>
232 RuntimeEffectContents::CreatePipeline(const ContentContext& renderer,
233  ContentContextOptions options,
234  bool async) const {
235  const std::shared_ptr<Context>& context = renderer.GetContext();
236  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
237  const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
238  const PixelFormat color_attachment_format = caps->GetDefaultColorFormat();
239  const PixelFormat stencil_attachment_format =
240  caps->GetDefaultDepthStencilFormat();
241 
242  using VS = RuntimeEffectVertexShader;
243 
244  PipelineDescriptor desc;
245  desc.SetLabel("Runtime Stage");
246  desc.AddStageEntrypoint(
247  library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
248  desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
250 
251  std::shared_ptr<VertexDescriptor> vertex_descriptor =
252  std::make_shared<VertexDescriptor>();
253  vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
254  VS::kInterleavedBufferLayout);
255  vertex_descriptor->RegisterDescriptorSetLayouts(VS::kDescriptorSetLayouts);
256  vertex_descriptor->RegisterDescriptorSetLayouts(
257  runtime_stage_->GetDescriptorSetLayouts().data(),
258  runtime_stage_->GetDescriptorSetLayouts().size());
259  desc.SetVertexDescriptor(std::move(vertex_descriptor));
260  desc.SetColorAttachmentDescriptor(
261  0u, {.format = color_attachment_format, .blending_enabled = true});
262 
263  desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
264  desc.SetStencilPixelFormat(stencil_attachment_format);
265 
266  desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
267  desc.SetDepthPixelFormat(stencil_attachment_format);
268 
269  options.ApplyToPipelineDescriptor(desc);
270  if (async) {
271  context->GetPipelineLibrary()->GetPipeline(desc, async);
272  return nullptr;
273  }
274 
275  auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc, async).Get();
276  if (!pipeline) {
277  VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
278  return nullptr;
279  }
280 
281  return pipeline;
282 }
283 
285  const Entity& entity,
286  RenderPass& pass) const {
287  const std::shared_ptr<Context>& context = renderer.GetContext();
288  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
289 
290  //--------------------------------------------------------------------------
291  /// Get or register shader. Flutter will do this when the runtime effect
292  /// is first loaded, but this check is added to supporting testing of the
293  /// Aiks API and non-flutter usage of Impeller.
294  ///
295  if (!RegisterShader(renderer)) {
296  return false;
297  }
298 
299  //--------------------------------------------------------------------------
300  /// Fragment stage uniforms.
301  ///
302  BindFragmentCallback bind_callback = [this, &renderer,
303  &context](RenderPass& pass) {
304  size_t buffer_index = 0;
305  size_t buffer_offset = 0;
306  size_t sampler_location = 0;
307  size_t buffer_location = 0;
308 
309  // Uniforms are ordered in the IPLR according to their
310  // declaration and the uniform location reflects the correct offset to
311  // be mapped to - except that it may include all proceeding
312  // uniforms of a different type. For example, a texture sampler that comes
313  // after 4 float uniforms may have a location of 4. Since we know that
314  // the declarations are already ordered, we can track the uniform location
315  // ourselves.
316  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
317  for (const auto& uniform : runtime_stage_->GetUniforms()) {
318  std::unique_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
319  switch (uniform.type) {
320  case kSampledImage: {
321  FML_DCHECK(sampler_location < texture_inputs_.size());
322  auto& input = texture_inputs_[sampler_location];
323 
324  raw_ptr<const Sampler> sampler =
325  context->GetSamplerLibrary()->GetSampler(
326  input.sampler_descriptor);
327 
328  SampledImageSlot image_slot;
329  image_slot.name = uniform.name.c_str();
330  image_slot.binding = uniform.binding;
331  image_slot.texture_index = sampler_location;
333  DescriptorType::kSampledImage, image_slot,
334  std::move(metadata), input.texture, sampler);
335  sampler_location++;
336  break;
337  }
338  case kFloat: {
339  FML_DCHECK(renderer.GetContext()->GetBackendType() !=
341  << "Uniform " << uniform.name
342  << " had unexpected type kFloat for Vulkan backend.";
343 
344  BufferView buffer_view = EmplaceUniform(
345  uniform_data_->data() + buffer_offset, data_host_buffer, uniform);
346 
347  ShaderUniformSlot uniform_slot;
348  uniform_slot.name = uniform.name.c_str();
349  uniform_slot.ext_res_0 = buffer_location;
351  DescriptorType::kUniformBuffer, uniform_slot,
352  std::move(metadata), std::move(buffer_view));
353  buffer_index++;
354  buffer_offset += uniform.GetDartSize();
355  buffer_location++;
356  break;
357  }
358  case kStruct: {
359  FML_DCHECK(renderer.GetContext()->GetBackendType() ==
361  ShaderUniformSlot uniform_slot;
362  uniform_slot.binding = uniform.location;
363  uniform_slot.name = uniform.name.c_str();
364 
365  pass.BindResource(
367  uniform_slot, nullptr,
368  EmplaceUniform(uniform_data_->data(), data_host_buffer, uniform));
369  }
370  }
371  }
372 
373  return true;
374  };
375 
376  /// Now that the descriptor set layouts are known, get the pipeline.
377  using VS = RuntimeEffectVertexShader;
378 
379  PipelineBuilderCallback pipeline_callback =
380  [&](ContentContextOptions options) {
381  // Pipeline creation callback for the cache handler to call.
382  return renderer.GetCachedRuntimeEffectPipeline(
383  runtime_stage_->GetEntrypoint(), options, [&]() {
384  return CreatePipeline(renderer, options, /*async=*/false);
385  });
386  };
387 
388  return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
389  pipeline_callback,
390  VS::FrameInfo{}, bind_callback);
391 }
392 
393 } // namespace impeller
std::function< PipelineRef(ContentContextOptions)> PipelineBuilderCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
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
HostBuffer & GetTransientsDataBuffer() const
Retrieve the current host buffer for transient storage of other non-index data.
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
size_t GetMinimumUniformAlignment() const
Retrieve the minimum uniform buffer alignment in bytes.
Definition: host_buffer.cc:241
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.
Definition: render_pass.cc:270
virtual bool BindResource(ShaderStage stage, DescriptorType type, const ShaderUniformSlot &slot, const ShaderMetadata *metadata, BufferView view) override
Definition: render_pass.cc:225
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)
static BufferView EmplaceUniform(const uint8_t *source_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform)
const Geometry * GetGeometry() const override
Get the geometry that this contents will use to render.
void SetUniformData(std::shared_ptr< std::vector< uint8_t >> uniform_data)
A wrapper around a raw ptr that adds additional unopt mode only checks.
Definition: raw_ptr.h:15
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
static std::unique_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
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)
LinePipeline::VertexShader VS
size_t GetGPUSize() const
Computes the total number of bytes that this uniform requires for representation in the GPU.
RuntimeUniformDimensions dimensions
Definition: runtime_types.h:58
std::vector< RuntimePaddingType > padding_layout
Definition: runtime_types.h:61
std::optional< size_t > array_elements
Definition: runtime_types.h:60
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:111
size_t texture_index
ext_res_0 is the Metal binding value.
Definition: shader_types.h:116
const char * name
The name of the uniform slot.
Definition: shader_types.h:113
size_t binding
The Vulkan binding value.
Definition: shader_types.h:122
Metadata required to bind a buffer.
Definition: shader_types.h:94
size_t binding
The Vulkan binding value.
Definition: shader_types.h:105
size_t ext_res_0
ext_res_0 is the Metal binding value.
Definition: shader_types.h:99
const char * name
The name of the uniform slot.
Definition: shader_types.h:96
#define VALIDATION_LOG
Definition: validation.h:91