| 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
10 #include "impeller/base/validation.h"
11 #include "impeller/core/formats.h"
12 #include "impeller/renderer/context.h"
13 #include "impeller/renderer/pipeline_descriptor.h"
14 #include "impeller/renderer/shader_library.h"
15 #include "impeller/renderer/vertex_descriptor.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_>
31 struct PipelineBuilder {
32 public:
33 using VertexShader = VertexShader_;
34 using FragmentShader = FragmentShader_;
35
36 static constexpr size_t kVertexBufferIndex =
37 VertexDescriptor::kReservedVertexBufferIndex;
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.
103 ColorAttachmentDescriptor color0;
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 {
111 DepthAttachmentDescriptor depth0;
112 depth0.depth_compare = CompareFunction::kAlways;
113 desc.SetDepthStencilAttachmentDescriptor(depth0);
114 desc.SetDepthPixelFormat(
115 context.GetCapabilities()->GetDefaultDepthStencilFormat());
116 }
117
118 // Setup default stencil buffer descriptions.
119 {
120 StencilAttachmentDescriptor stencil0;
121 stencil0.stencil_compare = CompareFunction::kEqual;
122 desc.SetStencilAttachmentDescriptors(stencil0);
123 desc.SetStencilPixelFormat(
124 context.GetCapabilities()->GetDefaultDepthStencilFormat());
125 }
126
127 return true;
128 }
129 };
130
131 } // namespace impeller
132
133 #endif // FLUTTER_IMPELLER_RENDERER_PIPELINE_BUILDER_H_
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:65
impeller::Context::GetShaderLibrary
virtual std::shared_ptr< ShaderLibrary > GetShaderLibrary() const =0
Returns the library of shaders used to specify the programmable stages of a pipeline.
impeller::Context::GetCapabilities
virtual const std::shared_ptr< const Capabilities > & GetCapabilities() const =0
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
impeller::PipelineDescriptor
Definition: pipeline_descriptor.h:24
impeller::PipelineDescriptor::SetStencilPixelFormat
PipelineDescriptor & SetStencilPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:145
impeller::PipelineDescriptor::SetDepthStencilAttachmentDescriptor
PipelineDescriptor & SetDepthStencilAttachmentDescriptor(std::optional< DepthAttachmentDescriptor > desc)
Definition: pipeline_descriptor.cc:151
impeller::PipelineDescriptor::SetVertexDescriptor
PipelineDescriptor & SetVertexDescriptor(std::shared_ptr< VertexDescriptor > vertex_descriptor)
Definition: pipeline_descriptor.cc:96
impeller::PipelineDescriptor::AddStageEntrypoint
PipelineDescriptor & AddStageEntrypoint(std::shared_ptr< const ShaderFunction > function)
Definition: pipeline_descriptor.cc:81
impeller::PipelineDescriptor::SetLabel
PipelineDescriptor & SetLabel(std::string_view label)
Definition: pipeline_descriptor.cc:71
impeller::PipelineDescriptor::SetSpecializationConstants
void SetSpecializationConstants(std::vector< Scalar > values)
Definition: pipeline_descriptor.cc:284
impeller::PipelineDescriptor::SetDepthPixelFormat
PipelineDescriptor & SetDepthPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:139
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::VertexDescriptor::kReservedVertexBufferIndex
static constexpr size_t kReservedVertexBufferIndex
Definition: vertex_descriptor.h:25
formats.h
impeller
Definition: allocation.cc:12
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
impeller::CompareFunction::kAlways
@ kAlways
Comparison test passes always passes.
impeller::ShaderStage::kFragment
@ kFragment
impeller::ShaderStage::kVertex
@ kVertex
pipeline_descriptor.h
context.h
shader_library.h
impeller::ColorAttachmentDescriptor
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:518
impeller::ColorAttachmentDescriptor::blending_enabled
bool blending_enabled
Definition: formats.h:520
impeller::ColorAttachmentDescriptor::format
PixelFormat format
Definition: formats.h:519
impeller::DepthAttachmentDescriptor
Definition: formats.h:590
impeller::DepthAttachmentDescriptor::depth_compare
CompareFunction depth_compare
Definition: formats.h:594
impeller::PipelineBuilder
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
Definition: pipeline_builder.h:31
impeller::PipelineBuilder::MakeDefaultPipelineDescriptor
static std::optional< PipelineDescriptor > MakeDefaultPipelineDescriptor(const Context &context, const std::vector< Scalar > &constants={})
Create a default pipeline descriptor using the combination reflected shader information....
Definition: pipeline_builder.h:50
impeller::PipelineBuilder::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline_builder.h:34
impeller::PipelineBuilder::kVertexBufferIndex
static constexpr size_t kVertexBufferIndex
Definition: pipeline_builder.h:36
impeller::PipelineBuilder::InitializePipelineDescriptorDefaults
static bool InitializePipelineDescriptorDefaults(const Context &context, PipelineDescriptor &desc)
Definition: pipeline_builder.h:61
impeller::PipelineBuilder::VertexShader
VertexShader_ VertexShader
Definition: pipeline_builder.h:33
impeller::StencilAttachmentDescriptor
Definition: formats.h:610
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:616
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
vertex_descriptor.h