Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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"
17
18namespace 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///
31template <class VertexShader_, class FragmentShader_>
33 public:
34 using VertexShader = VertexShader_;
35 using FragmentShader = FragmentShader_;
36
37 static constexpr size_t kVertexBufferIndex =
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 = {}) {
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.
105 color0.format = context.GetCapabilities()->GetDefaultColorFormat();
106 color0.blending_enabled = true;
107 desc.SetColorAttachmentDescriptor(0u, color0);
108 }
109
110 // Setup default depth buffer descriptions.
111 {
114 desc.SetDepthStencilAttachmentDescriptor(depth0);
115 desc.SetDepthPixelFormat(
116 context.GetCapabilities()->GetDefaultDepthStencilFormat());
117 }
118
119 // Setup default stencil buffer descriptions.
120 {
123 desc.SetStencilAttachmentDescriptors(stencil0);
124 desc.SetStencilPixelFormat(
125 context.GetCapabilities()->GetDefaultDepthStencilFormat());
126 }
127
128 return true;
129 }
130};
131
132} // namespace impeller
133
134#endif // FLUTTER_IMPELLER_RENDERER_PIPELINE_BUILDER_H_
To do anything rendering related with Impeller, you need a context.
Definition context.h:46
virtual const std::shared_ptr< const Capabilities > & GetCapabilities() const =0
Get the capabilities of Impeller context. All optionally supported feature of the platform,...
virtual std::shared_ptr< ShaderLibrary > GetShaderLibrary() const =0
Returns the library of shaders used to specify the programmable stages of a pipeline.
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:500
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
static constexpr size_t kVertexBufferIndex
static std::optional< PipelineDescriptor > MakeDefaultPipelineDescriptor(const Context &context, const std::vector< Scalar > &constants={})
Create a default pipeline descriptor using the combination reflected shader information....
static bool InitializePipelineDescriptorDefaults(const Context &context, PipelineDescriptor &desc)
#define VALIDATION_LOG
Definition validation.h:73