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 
8 #include <format>
9 
11 #include "impeller/core/formats.h"
16 
17 namespace impeller {
18 
19 //------------------------------------------------------------------------------
20 /// @brief An optional (but highly recommended) utility for creating
21 /// pipelines from reflected shader information.
22 ///
23 /// @tparam VertexShader_ The reflected vertex shader information. Found
24 /// in a generated header file called
25 /// <shader_name>.vert.h.
26 /// @tparam FragmentShader_ The reflected fragment shader information.
27 /// Found in a generated header file called
28 /// <shader_name>.frag.h.
29 ///
30 template <class VertexShader_, class FragmentShader_>
32  public:
33  using VertexShader = VertexShader_;
34  using FragmentShader = FragmentShader_;
35 
36  static constexpr size_t kVertexBufferIndex =
38 
39  //----------------------------------------------------------------------------
40  /// @brief Create a default pipeline descriptor using the combination
41  /// reflected shader information. The descriptor can be configured
42  /// further before a pipeline state object is created using it.
43  ///
44  /// @param[in] context The context
45  ///
46  /// @return If the combination of reflected shader information is
47  /// compatible and the requisite functions can be found in the
48  /// context, a pipeline descriptor.
49  ///
50  static std::optional<PipelineDescriptor> MakeDefaultPipelineDescriptor(
51  const Context& context,
52  const std::vector<Scalar>& constants = {}) {
53  PipelineDescriptor desc;
54  desc.SetSpecializationConstants(constants);
55  if (InitializePipelineDescriptorDefaults(context, desc)) {
56  return {std::move(desc)};
57  }
58  return std::nullopt;
59  }
60 
61  [[nodiscard]] static bool InitializePipelineDescriptorDefaults(
62  const Context& context,
63  PipelineDescriptor& desc) {
64  // Setup debug instrumentation.
65  desc.SetLabel(std::format("{} Pipeline", FragmentShader::kLabel));
66 
67  // Resolve pipeline entrypoints.
68  {
69  auto vertex_function = context.GetShaderLibrary()->GetFunction(
70  VertexShader::kEntrypointName, ShaderStage::kVertex);
71  auto fragment_function = context.GetShaderLibrary()->GetFunction(
72  FragmentShader::kEntrypointName, ShaderStage::kFragment);
73 
74  if (!vertex_function || !fragment_function) {
75  VALIDATION_LOG << "Could not resolve pipeline entrypoint(s) '"
76  << VertexShader::kEntrypointName << "' and '"
77  << FragmentShader::kEntrypointName
78  << "' for pipeline named '" << VertexShader::kLabel
79  << "'.";
80  return false;
81  }
82 
83  desc.AddStageEntrypoint(std::move(vertex_function));
84  desc.AddStageEntrypoint(std::move(fragment_function));
85  }
86 
87  // Setup the vertex descriptor from reflected information.
88  {
89  auto vertex_descriptor = std::make_shared<VertexDescriptor>();
90  vertex_descriptor->SetStageInputs(VertexShader::kAllShaderStageInputs,
91  VertexShader::kInterleavedBufferLayout);
92  vertex_descriptor->RegisterDescriptorSetLayouts(
93  VertexShader::kDescriptorSetLayouts);
94  vertex_descriptor->RegisterDescriptorSetLayouts(
95  FragmentShader::kDescriptorSetLayouts);
96  desc.SetVertexDescriptor(std::move(vertex_descriptor));
97  }
98 
99  // Setup fragment shader output descriptions.
100  {
101  // Configure the sole color attachments pixel format. This is by
102  // convention.
104  color0.format = context.GetCapabilities()->GetDefaultColorFormat();
105  color0.blending_enabled = true;
106  desc.SetColorAttachmentDescriptor(0u, color0);
107  }
108 
109  // Setup default depth buffer descriptions.
110  {
114  desc.SetDepthPixelFormat(
115  context.GetCapabilities()->GetDefaultDepthStencilFormat());
116  }
117 
118  // Setup default stencil buffer descriptions.
119  {
122  desc.SetStencilAttachmentDescriptors(stencil0);
124  context.GetCapabilities()->GetDefaultDepthStencilFormat());
125  }
126 
127  return true;
128  }
129 };
130 
131 } // namespace impeller
132 
133 #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
@ 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)
#define VALIDATION_LOG
Definition: validation.h:91