Flutter Impeller
pipeline_builder.h
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 
5 #ifndef FLUTTER_IMPELLER_RENDERER_PIPELINE_BUILDER_H_
6 #define FLUTTER_IMPELLER_RENDERER_PIPELINE_BUILDER_H_
7 
10 #include "impeller/core/formats.h"
15 
16 namespace impeller {
17 
18 //------------------------------------------------------------------------------
19 /// @brief An optional (but highly recommended) utility for creating
20 /// pipelines from reflected shader information.
21 ///
22 /// @tparam VertexShader_ The reflected vertex shader information. Found
23 /// in a generated header file called
24 /// <shader_name>.vert.h.
25 /// @tparam FragmentShader_ The reflected fragment shader information.
26 /// Found in a generated header file called
27 /// <shader_name>.frag.h.
28 ///
29 template <class VertexShader_, class FragmentShader_>
31  public:
32  using VertexShader = VertexShader_;
33  using FragmentShader = FragmentShader_;
34 
35  static constexpr size_t kVertexBufferIndex =
37 
38  //----------------------------------------------------------------------------
39  /// @brief Create a default pipeline descriptor using the combination
40  /// reflected shader information. The descriptor can be configured
41  /// further before a pipeline state object is created using it.
42  ///
43  /// @param[in] context The context
44  ///
45  /// @return If the combination of reflected shader information is
46  /// compatible and the requisite functions can be found in the
47  /// context, a pipeline descriptor.
48  ///
49  static std::optional<PipelineDescriptor> MakeDefaultPipelineDescriptor(
50  const Context& context,
51  const std::vector<Scalar>& constants = {}) {
52  PipelineDescriptor desc;
53  desc.SetSpecializationConstants(constants);
54  if (InitializePipelineDescriptorDefaults(context, desc)) {
55  return {std::move(desc)};
56  }
57  return std::nullopt;
58  }
59 
60  [[nodiscard]] static bool InitializePipelineDescriptorDefaults(
61  const Context& context,
62  PipelineDescriptor& desc) {
63  // Setup debug instrumentation.
64  desc.SetLabel(SPrintF("%s Pipeline", FragmentShader::kLabel.data()));
65 
66  // Resolve pipeline entrypoints.
67  {
68  auto vertex_function = context.GetShaderLibrary()->GetFunction(
69  VertexShader::kEntrypointName, ShaderStage::kVertex);
70  auto fragment_function = context.GetShaderLibrary()->GetFunction(
71  FragmentShader::kEntrypointName, ShaderStage::kFragment);
72 
73  if (!vertex_function || !fragment_function) {
74  VALIDATION_LOG << "Could not resolve pipeline entrypoint(s) '"
75  << VertexShader::kEntrypointName << "' and '"
76  << FragmentShader::kEntrypointName
77  << "' for pipeline named '" << VertexShader::kLabel
78  << "'.";
79  return false;
80  }
81 
82  desc.AddStageEntrypoint(std::move(vertex_function));
83  desc.AddStageEntrypoint(std::move(fragment_function));
84  }
85 
86  // Setup the vertex descriptor from reflected information.
87  {
88  auto vertex_descriptor = std::make_shared<VertexDescriptor>();
89  vertex_descriptor->SetStageInputs(VertexShader::kAllShaderStageInputs,
90  VertexShader::kInterleavedBufferLayout);
91  vertex_descriptor->RegisterDescriptorSetLayouts(
92  VertexShader::kDescriptorSetLayouts);
93  vertex_descriptor->RegisterDescriptorSetLayouts(
94  FragmentShader::kDescriptorSetLayouts);
95  desc.SetVertexDescriptor(std::move(vertex_descriptor));
96  }
97 
98  // Setup fragment shader output descriptions.
99  {
100  // Configure the sole color attachments pixel format. This is by
101  // convention.
103  color0.format = context.GetCapabilities()->GetDefaultColorFormat();
104  color0.blending_enabled = true;
105  desc.SetColorAttachmentDescriptor(0u, color0);
106  }
107 
108  // Setup default depth buffer descriptions.
109  {
113  desc.SetDepthPixelFormat(
114  context.GetCapabilities()->GetDefaultDepthStencilFormat());
115  }
116 
117  // Setup default stencil buffer descriptions.
118  {
121  desc.SetStencilAttachmentDescriptors(stencil0);
123  context.GetCapabilities()->GetDefaultDepthStencilFormat());
124  }
125 
126  return true;
127  }
128 };
129 
130 } // namespace impeller
131 
132 #endif // FLUTTER_IMPELLER_RENDERER_PIPELINE_BUILDER_H_
To do anything rendering related with Impeller, you need a context.
Definition: context.h:65
virtual std::shared_ptr< ShaderLibrary > GetShaderLibrary() const =0
Returns the library of shaders used to specify the programmable stages of a pipeline.
virtual const std::shared_ptr< const Capabilities > & GetCapabilities() const =0
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
PipelineDescriptor & SetStencilPixelFormat(PixelFormat format)
PipelineDescriptor & SetDepthStencilAttachmentDescriptor(std::optional< DepthAttachmentDescriptor > desc)
PipelineDescriptor & SetVertexDescriptor(std::shared_ptr< VertexDescriptor > vertex_descriptor)
PipelineDescriptor & AddStageEntrypoint(std::shared_ptr< const ShaderFunction > function)
PipelineDescriptor & SetLabel(std::string_view label)
void SetSpecializationConstants(std::vector< Scalar > values)
PipelineDescriptor & SetDepthPixelFormat(PixelFormat format)
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
PipelineDescriptor & SetColorAttachmentDescriptor(size_t index, ColorAttachmentDescriptor desc)
static constexpr size_t kReservedVertexBufferIndex
std::string SPrintF(const char *format,...)
Definition: strings.cc:12
@ kEqual
Comparison test passes if new_value == current_value.
@ kAlways
Comparison test passes always passes.
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:518
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
static std::optional< PipelineDescriptor > MakeDefaultPipelineDescriptor(const Context &context, const std::vector< Scalar > &constants={})
Create a default pipeline descriptor using the combination reflected shader information....
FragmentShader_ FragmentShader
static constexpr size_t kVertexBufferIndex
static bool InitializePipelineDescriptorDefaults(const Context &context, PipelineDescriptor &desc)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68
#define VALIDATION_LOG
Definition: validation.h:91