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 namespace {
28 constexpr char kPaddingType = 0;
29 constexpr char kFloatType = 1;
30 } // namespace
31 
32 // static
34  const std::shared_ptr<const std::vector<uint8_t>>& input_data,
35  HostBuffer& 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  size_t alignment = std::max(sizeof(float) * uniform_buffer.size(),
53  minimum_uniform_alignment);
54 
55  return host_buffer.Emplace(
56  reinterpret_cast<const void*>(uniform_buffer.data()),
57  sizeof(float) * uniform_buffer.size(), alignment);
58 }
59 
61  std::shared_ptr<RuntimeStage> runtime_stage) {
62  runtime_stage_ = std::move(runtime_stage);
63 }
64 
66  std::shared_ptr<std::vector<uint8_t>> uniform_data) {
67  uniform_data_ = std::move(uniform_data);
68 }
69 
71  std::vector<TextureInput> texture_inputs) {
72  texture_inputs_ = std::move(texture_inputs);
73 }
74 
76  switch (type) {
77  case kSampledImage:
79  case kFloat:
80  return ShaderType::kFloat;
81  case kStruct:
82  return ShaderType::kStruct;
83  }
84 }
85 
86 static std::unique_ptr<ShaderMetadata> MakeShaderMetadata(
87  const RuntimeUniformDescription& uniform) {
88  std::unique_ptr<ShaderMetadata> metadata = std::make_unique<ShaderMetadata>();
89  metadata->name = uniform.name;
90  metadata->members.emplace_back(ShaderStructMemberMetadata{
91  .type = GetShaderType(uniform.type), //
92  .size = uniform.dimensions.rows * uniform.dimensions.cols *
93  (uniform.bit_width / 8u), //
94  .byte_length =
95  (uniform.bit_width / 8u) * uniform.array_elements.value_or(1), //
96  .array_elements = uniform.array_elements //
97  });
98 
99  return metadata;
100 }
101 
103  const ContentContext& renderer) const {
104  if (!RegisterShader(renderer)) {
105  return false;
106  }
107  ContentContextOptions options;
109  renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
110  CreatePipeline(renderer, options, /*async=*/true);
111  return true;
112 }
113 
114 bool RuntimeEffectContents::RegisterShader(
115  const ContentContext& renderer) const {
116  const std::shared_ptr<Context>& context = renderer.GetContext();
117  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
118 
119  std::shared_ptr<const ShaderFunction> function = library->GetFunction(
120  runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
121 
122  //--------------------------------------------------------------------------
123  /// Resolve runtime stage function.
124  ///
125 
126  if (function && runtime_stage_->IsDirty()) {
127  renderer.ClearCachedRuntimeEffectPipeline(runtime_stage_->GetEntrypoint());
128  context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
129  library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
131 
132  function = nullptr;
133  }
134 
135  if (!function) {
136  std::promise<bool> promise;
137  auto future = promise.get_future();
138 
139  library->RegisterFunction(
140  runtime_stage_->GetEntrypoint(),
141  ToShaderStage(runtime_stage_->GetShaderStage()),
142  runtime_stage_->GetCodeMapping(),
143  fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
144  promise.set_value(result);
145  }));
146 
147  if (!future.get()) {
148  VALIDATION_LOG << "Failed to build runtime effect (entry point: "
149  << runtime_stage_->GetEntrypoint() << ")";
150  return false;
151  }
152 
153  function = library->GetFunction(runtime_stage_->GetEntrypoint(),
155  if (!function) {
157  << "Failed to fetch runtime effect function immediately after "
158  "registering it (entry point: "
159  << runtime_stage_->GetEntrypoint() << ")";
160  return false;
161  }
162 
163  runtime_stage_->SetClean();
164  }
165  return true;
166 }
167 
168 std::shared_ptr<Pipeline<PipelineDescriptor>>
169 RuntimeEffectContents::CreatePipeline(const ContentContext& renderer,
170  ContentContextOptions options,
171  bool async) const {
172  const std::shared_ptr<Context>& context = renderer.GetContext();
173  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
174  const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
175  const PixelFormat color_attachment_format = caps->GetDefaultColorFormat();
176  const PixelFormat stencil_attachment_format =
177  caps->GetDefaultDepthStencilFormat();
178 
179  using VS = RuntimeEffectVertexShader;
180 
181  PipelineDescriptor desc;
182  desc.SetLabel("Runtime Stage");
183  desc.AddStageEntrypoint(
184  library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
185  desc.AddStageEntrypoint(library->GetFunction(runtime_stage_->GetEntrypoint(),
187 
188  std::shared_ptr<VertexDescriptor> vertex_descriptor =
189  std::make_shared<VertexDescriptor>();
190  vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
191  VS::kInterleavedBufferLayout);
192  vertex_descriptor->RegisterDescriptorSetLayouts(VS::kDescriptorSetLayouts);
193  vertex_descriptor->RegisterDescriptorSetLayouts(
194  runtime_stage_->GetDescriptorSetLayouts().data(),
195  runtime_stage_->GetDescriptorSetLayouts().size());
196  desc.SetVertexDescriptor(std::move(vertex_descriptor));
197  desc.SetColorAttachmentDescriptor(
198  0u, {.format = color_attachment_format, .blending_enabled = true});
199 
200  desc.SetStencilAttachmentDescriptors(StencilAttachmentDescriptor{});
201  desc.SetStencilPixelFormat(stencil_attachment_format);
202 
203  desc.SetDepthStencilAttachmentDescriptor(DepthAttachmentDescriptor{});
204  desc.SetDepthPixelFormat(stencil_attachment_format);
205 
206  options.ApplyToPipelineDescriptor(desc);
207  if (async) {
208  context->GetPipelineLibrary()->GetPipeline(desc, async);
209  return nullptr;
210  }
211 
212  auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc, async).Get();
213  if (!pipeline) {
214  VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
215  return nullptr;
216  }
217 
218  return pipeline;
219 }
220 
222  const Entity& entity,
223  RenderPass& pass) const {
224  const std::shared_ptr<Context>& context = renderer.GetContext();
225  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
226 
227  //--------------------------------------------------------------------------
228  /// Get or register shader. Flutter will do this when the runtime effect
229  /// is first loaded, but this check is added to supporting testing of the
230  /// Aiks API and non-flutter usage of Impeller.
231  ///
232  if (!RegisterShader(renderer)) {
233  return false;
234  }
235 
236  //--------------------------------------------------------------------------
237  /// Fragment stage uniforms.
238  ///
239  BindFragmentCallback bind_callback = [this, &renderer,
240  &context](RenderPass& pass) {
241  size_t buffer_index = 0;
242  size_t buffer_offset = 0;
243  size_t sampler_location = 0;
244  size_t buffer_location = 0;
245 
246  // Uniforms are ordered in the IPLR according to their
247  // declaration and the uniform location reflects the correct offset to
248  // be mapped to - except that it may include all proceeding
249  // uniforms of a different type. For example, a texture sampler that comes
250  // after 4 float uniforms may have a location of 4. Since we know that
251  // the declarations are already ordered, we can track the uniform location
252  // ourselves.
253  auto& host_buffer = renderer.GetTransientsBuffer();
254  for (const auto& uniform : runtime_stage_->GetUniforms()) {
255  std::unique_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
256  switch (uniform.type) {
257  case kSampledImage: {
258  FML_DCHECK(sampler_location < texture_inputs_.size());
259  auto& input = texture_inputs_[sampler_location];
260 
261  raw_ptr<const Sampler> sampler =
262  context->GetSamplerLibrary()->GetSampler(
263  input.sampler_descriptor);
264 
265  SampledImageSlot image_slot;
266  image_slot.name = uniform.name.c_str();
267  image_slot.binding = uniform.binding;
268  image_slot.texture_index = sampler_location;
270  DescriptorType::kSampledImage, image_slot,
271  std::move(metadata), input.texture, sampler);
272  sampler_location++;
273  break;
274  }
275  case kFloat: {
276  FML_DCHECK(renderer.GetContext()->GetBackendType() !=
278  << "Uniform " << uniform.name
279  << " had unexpected type kFloat for Vulkan backend.";
280 
281  size_t alignment = std::max(uniform.bit_width / 8,
282  host_buffer.GetMinimumUniformAlignment());
284  host_buffer.Emplace(uniform_data_->data() + buffer_offset,
285  uniform.GetSize(), alignment);
286 
287  ShaderUniformSlot uniform_slot;
288  uniform_slot.name = uniform.name.c_str();
289  uniform_slot.ext_res_0 = buffer_location;
291  DescriptorType::kUniformBuffer, uniform_slot,
292  std::move(metadata), std::move(buffer_view));
293  buffer_index++;
294  buffer_offset += uniform.GetSize();
295  buffer_location++;
296  break;
297  }
298  case kStruct: {
299  FML_DCHECK(renderer.GetContext()->GetBackendType() ==
301  ShaderUniformSlot uniform_slot;
302  uniform_slot.binding = uniform.location;
303  uniform_slot.name = uniform.name.c_str();
304 
305  pass.BindResource(
307  uniform_slot, nullptr,
308  EmplaceVulkanUniform(uniform_data_, host_buffer, uniform,
309  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 & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
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.
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 EmplaceVulkanUniform(const std::shared_ptr< const std::vector< uint8_t >> &input_data, HostBuffer &host_buffer, const RuntimeUniformDescription &uniform, size_t minimum_uniform_alignment)
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)
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
RuntimeUniformDimensions dimensions
Definition: runtime_types.h:47
std::vector< uint8_t > struct_layout
Definition: runtime_types.h:50
std::optional< size_t > array_elements
Definition: runtime_types.h:49
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