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 
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:
53  return ShaderType::kStruct;
54  }
55 }
56 
57 static 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  }
75  ContentContextOptions options;
77  renderer.GetContext()->GetCapabilities()->GetDefaultColorFormat();
78  CreatePipeline(renderer, options, /*async=*/true);
79  return true;
80 }
81 
82 bool 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 
136 std::shared_ptr<Pipeline<PipelineDescriptor>>
137 RuntimeEffectContents::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());
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 {
271  FML_UNREACHABLE();
272  }
273  }
274  size_t alignment = std::max(sizeof(float) * uniform_buffer.size(),
276 
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;
305  DescriptorType::kSampledImage, image_slot,
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 =
322  [&](ContentContextOptions options) {
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
impeller::ShaderStructMemberMetadata
Definition: shader_types.h:63
impeller::RuntimeUniformDescription::GetSize
size_t GetSize() const
Computes the total number of bytes that this uniform requires.
Definition: runtime_types.cc:9
impeller::RuntimeUniformDescription
Definition: runtime_types.h:39
impeller::ColorSourceContents::PipelineBuilderCallback
std::function< std::shared_ptr< Pipeline< PipelineDescriptor > >(ContentContextOptions)> PipelineBuilderCallback
Definition: color_source_contents.h:114
impeller::kFloat
@ kFloat
Definition: runtime_types.h:23
impeller::ContentContext::ClearCachedRuntimeEffectPipeline
void ClearCachedRuntimeEffectPipeline(const std::string &unique_entrypoint_name) const
Definition: content_context.cc:579
impeller::RuntimeEffectContents::SetTextureInputs
void SetTextureInputs(std::vector< TextureInput > texture_inputs)
Definition: runtime_effect_contents.cc:37
impeller::ShaderUniformSlot::ext_res_0
size_t ext_res_0
ext_res_0 is the Metal binding value.
Definition: shader_types.h:86
impeller::GetShaderType
static ShaderType GetShaderType(RuntimeUniformType type)
Definition: runtime_effect_contents.cc:46
impeller::RuntimeEffectContents::SetRuntimeStage
void SetRuntimeStage(std::shared_ptr< RuntimeStage > runtime_stage)
Definition: runtime_effect_contents.cc:27
impeller::SampledImageSlot::name
const char * name
The name of the uniform slot.
Definition: shader_types.h:100
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer, size_t alignment=0)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:95
impeller::ShaderUniformSlot
Metadata required to bind a buffer.
Definition: shader_types.h:81
impeller::MakeShaderMetadata
static std::shared_ptr< ShaderMetadata > MakeShaderMetadata(const RuntimeUniformDescription &uniform)
Definition: runtime_effect_contents.cc:57
shader_function.h
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:14
impeller::RuntimeEffectContents::CanInheritOpacity
bool CanInheritOpacity(const Entity &entity) const override
Whether or not this contents can accept the opacity peephole optimization.
Definition: runtime_effect_contents.cc:42
impeller::ShaderType
ShaderType
Definition: shader_types.h:41
impeller::ShaderMetadata
Definition: shader_types.h:72
formats.h
impeller::kSampledImage
@ kSampledImage
Definition: runtime_types.h:24
impeller::ShaderUniformSlot::name
const char * name
The name of the uniform slot.
Definition: shader_types.h:83
buffer_view
BufferView buffer_view
Definition: blit_command_gles.cc:127
impeller::RuntimeEffectContents::SetUniformData
void SetUniformData(std::shared_ptr< std::vector< uint8_t >> uniform_data)
Definition: runtime_effect_contents.cc:32
impeller::ContentContext::GetCachedRuntimeEffectPipeline
std::shared_ptr< Pipeline< PipelineDescriptor > > GetCachedRuntimeEffectPipeline(const std::string &unique_entrypoint_name, const ContentContextOptions &options, const std::function< std::shared_ptr< Pipeline< PipelineDescriptor >>()> &create_callback) const
Definition: content_context.cc:566
validation.h
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
runtime_types.h
vertex_descriptor.h
runtime_effect_contents.h
impeller::RenderPass::BindResource
virtual bool BindResource(ShaderStage stage, DescriptorType type, const ShaderUniformSlot &slot, const ShaderMetadata &metadata, BufferView view) override
Definition: render_pass.cc:138
impeller::ShaderType::kFloat
@ kFloat
impeller::VS
SolidFillVertexShader VS
Definition: stroke_path_geometry.cc:16
impeller::ShaderUniformSlot::binding
size_t binding
The Vulkan binding value.
Definition: shader_types.h:92
impeller::Entity
Definition: entity.h:20
impeller::ContentContextOptions::color_attachment_pixel_format
PixelFormat color_attachment_pixel_format
Definition: content_context.h:312
render_pass.h
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:553
impeller::RuntimeUniformType
RuntimeUniformType
Definition: runtime_types.h:22
impeller::RuntimeUniformDescription::name
std::string name
Definition: runtime_types.h:40
impeller::SampledImageSlot
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:98
type
GLenum type
Definition: blit_command_gles.cc:126
impeller::ShaderType::kSampledImage
@ kSampledImage
capabilities.h
impeller::ShaderStage::kFragment
@ kFragment
impeller::RuntimeUniformDescription::type
RuntimeUniformType type
Definition: runtime_types.h:44
impeller::kStruct
@ kStruct
Definition: runtime_types.h:25
impeller::DescriptorType::kSampledImage
@ kSampledImage
impeller::DescriptorType::kUniformBuffer
@ kUniformBuffer
pipeline_library.h
impeller::RenderPass
Render passes encode render commands directed as one specific render target into an underlying comman...
Definition: render_pass.h:33
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::Context::BackendType::kVulkan
@ kVulkan
content_context.h
impeller::BufferView
Definition: buffer_view.h:15
impeller::ShaderType::kStruct
@ kStruct
impeller::RuntimeEffectContents::BootstrapShader
bool BootstrapShader(const ContentContext &renderer) const
Load the runtime effect and ensure a default PSO is initialized.
Definition: runtime_effect_contents.cc:70
impeller::RuntimeUniformDescription::bit_width
size_t bit_width
Definition: runtime_types.h:46
impeller::ColorSourceContents::BindFragmentCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
Definition: color_source_contents.h:109
impeller::ShaderStage::kVertex
@ kVertex
impeller::RuntimeEffectContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: runtime_effect_contents.cc:188
impeller::SampledImageSlot::texture_index
size_t texture_index
ext_res_0 is the Metal binding value.
Definition: shader_types.h:103
impeller::ShaderStructMemberMetadata::type
ShaderType type
Definition: shader_types.h:64
impeller::SampledImageSlot::binding
size_t binding
The Vulkan binding value.
Definition: shader_types.h:109
impeller::ContentContextOptions
Definition: content_context.h:254
shader_types.h
impeller
Definition: aiks_blend_unittests.cc:18
impeller::ContentContext
Definition: content_context.h:366
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:752