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 void RuntimeEffectContents::SetRuntimeStage(
72  std::shared_ptr<RuntimeStage> runtime_stage) {
73  runtime_stage_ = std::move(runtime_stage);
74 }
75 
76 void RuntimeEffectContents::SetUniformData(
77  std::shared_ptr<std::vector<uint8_t>> uniform_data) {
78  uniform_data_ = std::move(uniform_data);
79 }
80 
81 void RuntimeEffectContents::SetTextureInputs(
82  std::vector<TextureInput> texture_inputs) {
83  texture_inputs_ = std::move(texture_inputs);
84 }
85 
87  switch (type) {
88  case kSampledImage:
90  case kFloat:
91  return ShaderType::kFloat;
92  case kStruct:
93  return ShaderType::kStruct;
94  }
95 }
96 
97 static std::unique_ptr<ShaderMetadata> MakeShaderMetadata(
98  const RuntimeUniformDescription& uniform) {
99  std::unique_ptr<ShaderMetadata> metadata = std::make_unique<ShaderMetadata>();
100  metadata->name = uniform.name;
101 
102  // If the element is not an array, then the runtime stage flatbuffer will
103  // represent the unspecified array_elements as the default value of 0.
104  std::optional<size_t> array_elements;
105  if (uniform.array_elements.value_or(0) > 0) {
106  array_elements = uniform.array_elements;
107  }
108 
109  size_t member_size = uniform.dimensions.rows * uniform.dimensions.cols *
110  (uniform.bit_width / 8u);
111  metadata->members.emplace_back(ShaderStructMemberMetadata{
112  .type = GetShaderType(uniform.type), //
113  .size = member_size, //
114  .byte_length = member_size * array_elements.value_or(1), //
115  .array_elements = array_elements //
116  });
117 
118  return metadata;
119 }
120 
121 bool RuntimeEffectContents::BootstrapShader(
122  const ContentContext& renderer) const {
123  if (!RegisterShader(renderer)) {
124  return false;
125  }
126  ContentContextOptions options;
128  renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
129  CreatePipeline(renderer, options, /*async=*/true);
130  return true;
131 }
132 
133 bool RuntimeEffectContents::RegisterShader(
134  const ContentContext& renderer) const {
135  const std::shared_ptr<Context>& context = renderer.GetContext();
136  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
137 
138  std::shared_ptr<const ShaderFunction> function = library->GetFunction(
139  runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
140 
141  //--------------------------------------------------------------------------
142  /// Resolve runtime stage function.
143  ///
144 
145  if (function && runtime_stage_->IsDirty()) {
146  renderer.ClearCachedRuntimeEffectPipeline(runtime_stage_->GetEntrypoint());
147  context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
148  library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
149  ShaderStage::kFragment);
150 
151  function = nullptr;
152  }
153 
154  if (!function) {
155  std::promise<bool> promise;
156  auto future = promise.get_future();
157 
158  library->RegisterFunction(
159  runtime_stage_->GetEntrypoint(),
160  ToShaderStage(runtime_stage_->GetShaderStage()),
161  runtime_stage_->GetCodeMapping(),
162  fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
163  promise.set_value(result);
164  }));
165 
166  if (!future.get()) {
167  VALIDATION_LOG << "Failed to build runtime effect (entry point: "
168  << runtime_stage_->GetEntrypoint() << ")";
169  return false;
170  }
171 
172  function = library->GetFunction(runtime_stage_->GetEntrypoint(),
173  ShaderStage::kFragment);
174  if (!function) {
176  << "Failed to fetch runtime effect function immediately after "
177  "registering it (entry point: "
178  << runtime_stage_->GetEntrypoint() << ")";
179  return false;
180  }
181 
182  runtime_stage_->SetClean();
183  }
184  return true;
185 }
186 
187 std::shared_ptr<Pipeline<PipelineDescriptor>>
188 RuntimeEffectContents::CreatePipeline(const ContentContext& renderer,
189  ContentContextOptions options,
190  bool async) const {
191  const std::shared_ptr<Context>& context = renderer.GetContext();
192  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
193  const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
194  const PixelFormat color_attachment_format = caps->GetDefaultColorFormat();
195  const PixelFormat stencil_attachment_format =
196  caps->GetDefaultDepthStencilFormat();
197 
198  using VS = RuntimeEffectVertexShader;
199 
200  PipelineDescriptor desc;
201  desc.SetLabel("Runtime Stage");
202  desc.AddStageEntrypoint(
203  library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
204  desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
205  ShaderStage::kFragment));
206 
207  std::shared_ptr<VertexDescriptor> vertex_descriptor =
208  std::make_shared<VertexDescriptor>();
209  vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
210  VS::kInterleavedBufferLayout);
211  vertex_descriptor->RegisterDescriptorSetLayouts(VS::kDescriptorSetLayouts);
212  vertex_descriptor->RegisterDescriptorSetLayouts(
213  runtime_stage_->GetDescriptorSetLayouts().data(),
214  runtime_stage_->GetDescriptorSetLayouts().size());
215  desc.SetVertexDescriptor(std::move(vertex_descriptor));
216  desc.SetColorAttachmentDescriptor(
217  0u, {.format = color_attachment_format, .blending_enabled = true});
218 
219  desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
220  desc.SetStencilPixelFormat(stencil_attachment_format);
221 
222  desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
223  desc.SetDepthPixelFormat(stencil_attachment_format);
224 
225  options.ApplyToPipelineDescriptor(desc);
226  if (async) {
227  context->GetPipelineLibrary()->GetPipeline(desc, async);
228  return nullptr;
229  }
230 
231  auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc, async).Get();
232  if (!pipeline) {
233  VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
234  return nullptr;
235  }
236 
237  return pipeline;
238 }
239 
240 bool RuntimeEffectContents::Render(const ContentContext& renderer,
241  const Entity& entity,
242  RenderPass& pass) const {
243  const std::shared_ptr<Context>& context = renderer.GetContext();
244  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
245 
246  //--------------------------------------------------------------------------
247  /// Get or register shader. Flutter will do this when the runtime effect
248  /// is first loaded, but this check is added to supporting testing of the
249  /// Aiks API and non-flutter usage of Impeller.
250  ///
251  if (!RegisterShader(renderer)) {
252  return false;
253  }
254 
255  //--------------------------------------------------------------------------
256  /// Fragment stage uniforms.
257  ///
258  BindFragmentCallback bind_callback = [this, &renderer,
259  &context](RenderPass& pass) {
260  size_t buffer_index = 0;
261  size_t buffer_offset = 0;
262  size_t sampler_location = 0;
263  size_t buffer_location = 0;
264 
265  // Uniforms are ordered in the IPLR according to their
266  // declaration and the uniform location reflects the correct offset to
267  // be mapped to - except that it may include all proceeding
268  // uniforms of a different type. For example, a texture sampler that comes
269  // after 4 float uniforms may have a location of 4. Since we know that
270  // the declarations are already ordered, we can track the uniform location
271  // ourselves.
272  auto& data_host_buffer = renderer.GetTransientsDataBuffer();
273  for (const auto& uniform : runtime_stage_->GetUniforms()) {
274  std::unique_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
275  switch (uniform.type) {
276  case kSampledImage: {
277  FML_DCHECK(sampler_location < texture_inputs_.size());
278  auto& input = texture_inputs_[sampler_location];
279 
280  raw_ptr<const Sampler> sampler =
281  context->GetSamplerLibrary()->GetSampler(
282  input.sampler_descriptor);
283 
284  SampledImageSlot image_slot;
285  image_slot.name = uniform.name.c_str();
286  image_slot.binding = uniform.binding;
287  image_slot.texture_index = sampler_location;
288  pass.BindDynamicResource(ShaderStage::kFragment,
289  DescriptorType::kSampledImage, image_slot,
290  std::move(metadata), input.texture, sampler);
291  sampler_location++;
292  break;
293  }
294  case kFloat: {
295  FML_DCHECK(renderer.GetContext()->GetBackendType() !=
296  Context::BackendType::kVulkan)
297  << "Uniform " << uniform.name
298  << " had unexpected type kFloat for Vulkan backend.";
299 
300  BufferView buffer_view = EmplaceUniform(
301  uniform_data_->data() + buffer_offset, data_host_buffer, uniform);
302 
303  ShaderUniformSlot uniform_slot;
304  uniform_slot.name = uniform.name.c_str();
305  uniform_slot.ext_res_0 = buffer_location;
306  pass.BindDynamicResource(ShaderStage::kFragment,
307  DescriptorType::kUniformBuffer, uniform_slot,
308  std::move(metadata), std::move(buffer_view));
309  buffer_index++;
310  buffer_offset += uniform.GetDartSize();
311  buffer_location++;
312  break;
313  }
314  case kStruct: {
315  FML_DCHECK(renderer.GetContext()->GetBackendType() ==
316  Context::BackendType::kVulkan);
317  ShaderUniformSlot uniform_slot;
318  uniform_slot.binding = uniform.location;
319  uniform_slot.name = uniform.name.c_str();
320 
321  pass.BindResource(
322  ShaderStage::kFragment, DescriptorType::kUniformBuffer,
323  uniform_slot, nullptr,
324  EmplaceUniform(uniform_data_->data(), data_host_buffer, uniform));
325  }
326  }
327  }
328 
329  return true;
330  };
331 
332  /// Now that the descriptor set layouts are known, get the pipeline.
333  using VS = RuntimeEffectVertexShader;
334 
335  PipelineBuilderCallback pipeline_callback =
336  [&](ContentContextOptions options) {
337  // Pipeline creation callback for the cache handler to call.
338  return renderer.GetCachedRuntimeEffectPipeline(
339  runtime_stage_->GetEntrypoint(), options, [&]() {
340  return CreatePipeline(renderer, options, /*async=*/false);
341  });
342  };
343 
344  return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
345  pipeline_callback,
346  VS::FrameInfo{}, bind_callback);
347 }
348 
349 } // namespace impeller
GLenum type
BufferView buffer_view
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
static BufferView EmplaceUniform(const uint8_t *source_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform)
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: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:91