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 <future>
8 #include <memory>
9 
10 #include "flutter/fml/logging.h"
11 #include "flutter/fml/make_copyable.h"
13 #include "impeller/core/formats.h"
17 #include "impeller/entity/runtime_effect.vert.h"
22 
23 namespace impeller {
24 
26  std::shared_ptr<RuntimeStage> runtime_stage) {
27  runtime_stage_ = std::move(runtime_stage);
28 }
29 
31  std::shared_ptr<std::vector<uint8_t>> uniform_data) {
32  uniform_data_ = std::move(uniform_data);
33 }
34 
36  std::vector<TextureInput> texture_inputs) {
37  texture_inputs_ = std::move(texture_inputs);
38 }
39 
41  return false;
42 }
43 
45  switch (type) {
46  case kSampledImage:
48  case kFloat:
49  return ShaderType::kFloat;
50  case kStruct:
51  return ShaderType::kStruct;
52  }
53 }
54 
55 static std::shared_ptr<ShaderMetadata> MakeShaderMetadata(
56  const RuntimeUniformDescription& uniform) {
57  auto metadata = std::make_shared<ShaderMetadata>();
58  metadata->name = uniform.name;
59  metadata->members.emplace_back(ShaderStructMemberMetadata{
60  .type = GetShaderType(uniform.type),
61  .size = uniform.GetSize(),
62  .byte_length = uniform.bit_width / 8,
63  });
64 
65  return metadata;
66 }
67 
69  const Entity& entity,
70  RenderPass& pass) const {
71  const std::shared_ptr<Context>& context = renderer.GetContext();
72  const std::shared_ptr<ShaderLibrary>& library = context->GetShaderLibrary();
73 
74  //--------------------------------------------------------------------------
75  /// Get or register shader.
76  ///
77 
78  // TODO(113719): Register the shader function earlier.
79 
80  std::shared_ptr<const ShaderFunction> function = library->GetFunction(
81  runtime_stage_->GetEntrypoint(), ShaderStage::kFragment);
82 
83  //--------------------------------------------------------------------------
84  /// Resolve runtime stage function.
85  ///
86 
87  if (function && runtime_stage_->IsDirty()) {
88  renderer.ClearCachedRuntimeEffectPipeline(runtime_stage_->GetEntrypoint());
89  context->GetPipelineLibrary()->RemovePipelinesWithEntryPoint(function);
90  library->UnregisterFunction(runtime_stage_->GetEntrypoint(),
92 
93  function = nullptr;
94  }
95 
96  if (!function) {
97  std::promise<bool> promise;
98  auto future = promise.get_future();
99 
100  library->RegisterFunction(
101  runtime_stage_->GetEntrypoint(),
102  ToShaderStage(runtime_stage_->GetShaderStage()),
103  runtime_stage_->GetCodeMapping(),
104  fml::MakeCopyable([promise = std::move(promise)](bool result) mutable {
105  promise.set_value(result);
106  }));
107 
108  if (!future.get()) {
109  VALIDATION_LOG << "Failed to build runtime effect (entry point: "
110  << runtime_stage_->GetEntrypoint() << ")";
111  return false;
112  }
113 
114  function = library->GetFunction(runtime_stage_->GetEntrypoint(),
116  if (!function) {
118  << "Failed to fetch runtime effect function immediately after "
119  "registering it (entry point: "
120  << runtime_stage_->GetEntrypoint() << ")";
121  return false;
122  }
123 
124  runtime_stage_->SetClean();
125  }
126 
127  //--------------------------------------------------------------------------
128  /// Set up the command. Defer setting up the pipeline until the descriptor set
129  /// layouts are known from the uniforms.
130  ///
131 
132  const std::shared_ptr<const Capabilities>& caps = context->GetCapabilities();
133  const auto color_attachment_format = caps->GetDefaultColorFormat();
134  const auto stencil_attachment_format = caps->GetDefaultDepthStencilFormat();
135 
136  using VS = RuntimeEffectVertexShader;
137 
138  //--------------------------------------------------------------------------
139  /// Fragment stage uniforms.
140  ///
141 
142  std::vector<DescriptorSetLayout> descriptor_set_layouts;
143 
144  BindFragmentCallback bind_callback = [this, &renderer, &context,
145  &descriptor_set_layouts](
146  RenderPass& pass) {
147  descriptor_set_layouts.clear();
148 
149  size_t minimum_sampler_index = 100000000;
150  size_t buffer_index = 0;
151  size_t buffer_offset = 0;
152 
153  for (const auto& uniform : runtime_stage_->GetUniforms()) {
154  std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
155 
156  switch (uniform.type) {
157  case kSampledImage: {
158  // Sampler uniforms are ordered in the IPLR according to their
159  // declaration and the uniform location reflects the correct offset to
160  // be mapped to - except that it may include all proceeding float
161  // uniforms. For example, a float sampler that comes after 4 float
162  // uniforms may have a location of 4. To convert to the actual offset
163  // we need to find the largest location assigned to a float uniform
164  // and then subtract this from all uniform locations. This is more or
165  // less the same operation we previously performed in the shader
166  // compiler.
167  minimum_sampler_index =
168  std::min(minimum_sampler_index, uniform.location);
169  break;
170  }
171  case kFloat: {
172  FML_DCHECK(renderer.GetContext()->GetBackendType() !=
174  << "Uniform " << uniform.name
175  << " had unexpected type kFloat for Vulkan backend.";
176  size_t alignment =
177  std::max(uniform.bit_width / 8, DefaultUniformAlignment());
178  auto buffer_view = renderer.GetTransientsBuffer().Emplace(
179  uniform_data_->data() + buffer_offset, uniform.GetSize(),
180  alignment);
181 
182  ShaderUniformSlot uniform_slot;
183  uniform_slot.name = uniform.name.c_str();
184  uniform_slot.ext_res_0 = uniform.location;
186  DescriptorType::kUniformBuffer, uniform_slot,
187  metadata, buffer_view);
188  buffer_index++;
189  buffer_offset += uniform.GetSize();
190  break;
191  }
192  case kStruct: {
193  FML_DCHECK(renderer.GetContext()->GetBackendType() ==
195  descriptor_set_layouts.emplace_back(DescriptorSetLayout{
196  static_cast<uint32_t>(uniform.location),
199  });
200  ShaderUniformSlot uniform_slot;
201  uniform_slot.name = uniform.name.c_str();
202  uniform_slot.binding = uniform.location;
203 
204  std::vector<float> uniform_buffer;
205  uniform_buffer.reserve(uniform.struct_layout.size());
206  size_t uniform_byte_index = 0u;
207  for (const auto& byte_type : uniform.struct_layout) {
208  if (byte_type == 0) {
209  uniform_buffer.push_back(0.f);
210  } else if (byte_type == 1) {
211  uniform_buffer.push_back(reinterpret_cast<float*>(
212  uniform_data_->data())[uniform_byte_index++]);
213  } else {
214  FML_UNREACHABLE();
215  }
216  }
217 
218  size_t alignment = std::max(sizeof(float) * uniform_buffer.size(),
220 
221  auto buffer_view = renderer.GetTransientsBuffer().Emplace(
222  reinterpret_cast<const void*>(uniform_buffer.data()),
223  sizeof(float) * uniform_buffer.size(), alignment);
225  DescriptorType::kUniformBuffer, uniform_slot,
226  ShaderMetadata{}, buffer_view);
227  }
228  }
229  }
230 
231  size_t sampler_index = 0;
232  for (const auto& uniform : runtime_stage_->GetUniforms()) {
233  std::shared_ptr<ShaderMetadata> metadata = MakeShaderMetadata(uniform);
234 
235  switch (uniform.type) {
236  case kSampledImage: {
237  FML_DCHECK(sampler_index < texture_inputs_.size());
238  auto& input = texture_inputs_[sampler_index];
239 
240  const std::unique_ptr<const Sampler>& sampler =
241  context->GetSamplerLibrary()->GetSampler(
242  input.sampler_descriptor);
243 
244  SampledImageSlot image_slot;
245  image_slot.name = uniform.name.c_str();
246 
247  uint32_t sampler_binding_location = 0u;
248  if (!descriptor_set_layouts.empty()) {
249  sampler_binding_location =
250  descriptor_set_layouts.back().binding + 1;
251  }
252 
253  descriptor_set_layouts.emplace_back(DescriptorSetLayout{
254  sampler_binding_location,
257  });
258 
259  image_slot.binding = sampler_binding_location;
260  image_slot.texture_index = uniform.location - minimum_sampler_index;
262  DescriptorType::kSampledImage, image_slot,
263  *metadata, input.texture, sampler);
264 
265  sampler_index++;
266  break;
267  }
268  default:
269  continue;
270  }
271  }
272  return true;
273  };
274 
275  /// Now that the descriptor set layouts are known, get the pipeline.
276 
277  PipelineBuilderCallback pipeline_callback = [&](ContentContextOptions
278  options) {
279  // Pipeline creation callback for the cache handler to call.
280  auto create_callback =
281  [&]() -> std::shared_ptr<Pipeline<PipelineDescriptor>> {
282  PipelineDescriptor desc;
283  desc.SetLabel("Runtime Stage");
284  desc.AddStageEntrypoint(
285  library->GetFunction(VS::kEntrypointName, ShaderStage::kVertex));
286  desc.AddStageEntrypoint(library->GetFunction(
287  runtime_stage_->GetEntrypoint(), ShaderStage::kFragment));
288  auto vertex_descriptor = std::make_shared<VertexDescriptor>();
289  vertex_descriptor->SetStageInputs(VS::kAllShaderStageInputs,
290  VS::kInterleavedBufferLayout);
291  vertex_descriptor->RegisterDescriptorSetLayouts(
292  VS::kDescriptorSetLayouts);
293  vertex_descriptor->RegisterDescriptorSetLayouts(
294  descriptor_set_layouts.data(), descriptor_set_layouts.size());
295  desc.SetVertexDescriptor(std::move(vertex_descriptor));
297  0u, {.format = color_attachment_format, .blending_enabled = true});
298 
300  desc.SetStencilPixelFormat(stencil_attachment_format);
301 
303  desc.SetDepthPixelFormat(stencil_attachment_format);
304 
305  options.ApplyToPipelineDescriptor(desc);
306  auto pipeline = context->GetPipelineLibrary()->GetPipeline(desc).Get();
307  if (!pipeline) {
308  VALIDATION_LOG << "Failed to get or create runtime effect pipeline.";
309  return nullptr;
310  }
311 
312  return pipeline;
313  };
314  return renderer.GetCachedRuntimeEffectPipeline(
315  runtime_stage_->GetEntrypoint(), options, create_callback);
316  };
317 
318  return ColorSourceContents::DrawGeometry<VS>(renderer, entity, pass,
319  pipeline_callback,
320  VS::FrameInfo{}, bind_callback);
321 }
322 
323 } // namespace impeller
impeller::PipelineDescriptor
Definition: pipeline_descriptor.h:24
impeller::Pipeline< PipelineDescriptor >
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:113
impeller::kFloat
@ kFloat
Definition: runtime_types.h:23
impeller::ContentContext::ClearCachedRuntimeEffectPipeline
void ClearCachedRuntimeEffectPipeline(const std::string &unique_entrypoint_name) const
Definition: content_context.cc:590
impeller::RuntimeEffectContents::SetTextureInputs
void SetTextureInputs(std::vector< TextureInput > texture_inputs)
Definition: runtime_effect_contents.cc:35
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:44
impeller::RuntimeEffectContents::SetRuntimeStage
void SetRuntimeStage(std::shared_ptr< RuntimeStage > runtime_stage)
Definition: runtime_effect_contents.cc:25
impeller::SampledImageSlot::name
const char * name
The name of the uniform slot.
Definition: shader_types.h:100
impeller::DescriptorSetLayout
Definition: shader_types.h:162
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:55
shader_function.h
impeller::DefaultUniformAlignment
constexpr size_t DefaultUniformAlignment()
Definition: platform.h:15
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:40
impeller::ShaderType
ShaderType
Definition: shader_types.h:41
impeller::PipelineDescriptor::SetStencilAttachmentDescriptors
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
Definition: pipeline_descriptor.cc:157
impeller::PipelineDescriptor::SetColorAttachmentDescriptor
PipelineDescriptor & SetColorAttachmentDescriptor(size_t index, ColorAttachmentDescriptor desc)
Definition: pipeline_descriptor.cc:110
impeller::PipelineDescriptor::SetStencilPixelFormat
PipelineDescriptor & SetStencilPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:145
impeller::ShaderMetadata
Definition: shader_types.h:72
formats.h
impeller::HostBuffer::Emplace
BufferView Emplace(const BufferType &buffer)
Emplace non-uniform data (like contiguous vertices) onto the host buffer.
Definition: host_buffer.h:94
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
impeller::RuntimeEffectContents::SetUniformData
void SetUniformData(std::shared_ptr< std::vector< uint8_t >> uniform_data)
Definition: runtime_effect_contents.cc:30
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:577
validation.h
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
runtime_types.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:15
impeller::ShaderUniformSlot::binding
size_t binding
The Vulkan binding value.
Definition: shader_types.h:92
impeller::Entity
Definition: entity.h:21
render_pass.h
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:564
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
impeller::ShaderType::kSampledImage
@ kSampledImage
capabilities.h
impeller::ShaderStage::kFragment
@ kFragment
impeller::RuntimeUniformDescription::type
RuntimeUniformType type
Definition: runtime_types.h:42
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
impeller::PipelineDescriptor::AddStageEntrypoint
PipelineDescriptor & AddStageEntrypoint(std::shared_ptr< const ShaderFunction > function)
Definition: pipeline_descriptor.cc:81
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:73
impeller::Context::BackendType::kVulkan
@ kVulkan
content_context.h
impeller::ShaderType::kStruct
@ kStruct
impeller::RuntimeUniformDescription::bit_width
size_t bit_width
Definition: runtime_types.h:44
impeller::ColorSourceContents::BindFragmentCallback
std::function< bool(RenderPass &pass)> BindFragmentCallback
Definition: color_source_contents.h:108
impeller::ShaderStage::kVertex
@ kVertex
impeller::DepthAttachmentDescriptor
Definition: formats.h:572
impeller::StencilAttachmentDescriptor
Definition: formats.h:592
impeller::PipelineDescriptor::SetDepthPixelFormat
PipelineDescriptor & SetDepthPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:139
impeller::RuntimeEffectContents::Render
bool Render(const ContentContext &renderer, const Entity &entity, RenderPass &pass) const override
Definition: runtime_effect_contents.cc:68
impeller::SampledImageSlot::texture_index
size_t texture_index
ext_res_0 is the Metal binding value.
Definition: shader_types.h:103
impeller::PipelineDescriptor::SetDepthStencilAttachmentDescriptor
PipelineDescriptor & SetDepthStencilAttachmentDescriptor(std::optional< DepthAttachmentDescriptor > desc)
Definition: pipeline_descriptor.cc:151
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::PipelineDescriptor::SetLabel
PipelineDescriptor & SetLabel(std::string label)
Definition: pipeline_descriptor.cc:71
impeller::ContentContextOptions
Definition: content_context.h:288
shader_types.h
impeller
Definition: aiks_blur_unittests.cc:20
impeller::ContentContext
Definition: content_context.h:392
impeller::ContentContext::GetTransientsBuffer
HostBuffer & GetTransientsBuffer() const
Retrieve the currnent host buffer for transient storage.
Definition: content_context.h:833
impeller::PipelineDescriptor::SetVertexDescriptor
PipelineDescriptor & SetVertexDescriptor(std::shared_ptr< VertexDescriptor > vertex_descriptor)
Definition: pipeline_descriptor.cc:96