| 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 "impeller/base/strings.h"
9 #include "impeller/base/validation.h"
10 #include "impeller/core/formats.h"
11 #include "impeller/renderer/context.h"
12 #include "impeller/renderer/pipeline_descriptor.h"
13 #include "impeller/renderer/shader_library.h"
14 #include "impeller/renderer/vertex_descriptor.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_>
30 struct PipelineBuilder {
31 public:
32 using VertexShader = VertexShader_;
33 using FragmentShader = FragmentShader_;
34
35 static constexpr size_t kVertexBufferIndex =
36 VertexDescriptor::kReservedVertexBufferIndex;
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.
102 ColorAttachmentDescriptor color0;
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 {
110 DepthAttachmentDescriptor depth0;
111 depth0.depth_compare = CompareFunction::kAlways;
112 desc.SetDepthStencilAttachmentDescriptor(depth0);
113 desc.SetDepthPixelFormat(
114 context.GetCapabilities()->GetDefaultDepthStencilFormat());
115 }
116
117 // Setup default stencil buffer descriptions.
118 {
119 StencilAttachmentDescriptor stencil0;
120 stencil0.stencil_compare = CompareFunction::kEqual;
121 desc.SetStencilAttachmentDescriptors(stencil0);
122 desc.SetStencilPixelFormat(
123 context.GetCapabilities()->GetDefaultDepthStencilFormat());
124 }
125
126 return true;
127 }
128 };
129
130 } // namespace impeller
131
132 #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::SPrintF
std::string SPrintF(const char *format,...)
Definition: strings.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
strings.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:30
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:49
impeller::PipelineBuilder::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline_builder.h:33
impeller::PipelineBuilder::kVertexBufferIndex
static constexpr size_t kVertexBufferIndex
Definition: pipeline_builder.h:35
impeller::PipelineBuilder::InitializePipelineDescriptorDefaults
static bool InitializePipelineDescriptorDefaults(const Context &context, PipelineDescriptor &desc)
Definition: pipeline_builder.h:60
impeller::PipelineBuilder::VertexShader
VertexShader_ VertexShader
Definition: pipeline_builder.h:32
impeller::StencilAttachmentDescriptor
Definition: formats.h:610
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:616
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68
validation.h
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:91
vertex_descriptor.h