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