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 #pragma once
6 
7 #include "flutter/fml/logging.h"
8 #include "flutter/fml/macros.h"
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  PipelineDescriptor desc;
53  if (InitializePipelineDescriptorDefaults(context, desc)) {
54  return {std::move(desc)};
55  }
56  return std::nullopt;
57  }
58 
59  [[nodiscard]] static bool InitializePipelineDescriptorDefaults(
60  const Context& context,
61  PipelineDescriptor& desc) {
62  // Setup debug instrumentation.
63  desc.SetLabel(SPrintF("%s Pipeline", FragmentShader::kLabel.data()));
64 
65  // Resolve pipeline entrypoints.
66  {
67  auto vertex_function = context.GetShaderLibrary()->GetFunction(
68  VertexShader::kEntrypointName, ShaderStage::kVertex);
69  auto fragment_function = context.GetShaderLibrary()->GetFunction(
70  FragmentShader::kEntrypointName, ShaderStage::kFragment);
71 
72  if (!vertex_function || !fragment_function) {
73  VALIDATION_LOG << "Could not resolve pipeline entrypoint(s) '"
74  << VertexShader::kEntrypointName << "' and '"
75  << FragmentShader::kEntrypointName
76  << "' for pipeline named '" << VertexShader::kLabel
77  << "'.";
78  return false;
79  }
80 
81  desc.AddStageEntrypoint(std::move(vertex_function));
82  desc.AddStageEntrypoint(std::move(fragment_function));
83  }
84 
85  // Setup the vertex descriptor from reflected information.
86  {
87  auto vertex_descriptor = std::make_shared<VertexDescriptor>();
88  vertex_descriptor->SetStageInputs(VertexShader::kAllShaderStageInputs,
89  VertexShader::kInterleavedBufferLayout);
90  vertex_descriptor->RegisterDescriptorSetLayouts(
91  VertexShader::kDescriptorSetLayouts);
92  vertex_descriptor->RegisterDescriptorSetLayouts(
93  FragmentShader::kDescriptorSetLayouts);
94  desc.SetVertexDescriptor(std::move(vertex_descriptor));
95  }
96 
97  // Setup fragment shader output descriptions.
98  {
99  // Configure the sole color attachments pixel format. This is by
100  // convention.
102  color0.format = context.GetCapabilities()->GetDefaultColorFormat();
103  color0.blending_enabled = true;
104  desc.SetColorAttachmentDescriptor(0u, color0);
105  }
106 
107  // Setup default stencil buffer descriptions.
108  {
111  desc.SetStencilAttachmentDescriptors(stencil0);
113  context.GetCapabilities()->GetDefaultStencilFormat());
114  }
115 
116  return true;
117  }
118 };
119 
120 } // namespace impeller
impeller::PipelineDescriptor
Definition: pipeline_descriptor.h:30
impeller::StencilAttachmentDescriptor::stencil_compare
CompareFunction stencil_compare
Definition: formats.h:550
impeller::PipelineDescriptor::SetStencilAttachmentDescriptors
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
Definition: pipeline_descriptor.cc:153
impeller::PipelineDescriptor::SetColorAttachmentDescriptor
PipelineDescriptor & SetColorAttachmentDescriptor(size_t index, ColorAttachmentDescriptor desc)
Definition: pipeline_descriptor.cc:106
impeller::PipelineDescriptor::SetStencilPixelFormat
PipelineDescriptor & SetStencilPixelFormat(PixelFormat format)
Definition: pipeline_descriptor.cc:141
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:33
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:452
impeller::PipelineBuilder::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline_builder.h:34
impeller::ShaderStage::kFragment
@ kFragment
strings.h
impeller::PipelineDescriptor::AddStageEntrypoint
PipelineDescriptor & AddStageEntrypoint(std::shared_ptr< const ShaderFunction > function)
Definition: pipeline_descriptor.cc:77
VALIDATION_LOG
#define VALIDATION_LOG
Definition: validation.h:60
impeller::PipelineBuilder::MakeDefaultPipelineDescriptor
static std::optional< PipelineDescriptor > MakeDefaultPipelineDescriptor(const Context &context)
Create a default pipeline descriptor using the combination reflected shader information....
Definition: pipeline_builder.h:50
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:544
context.h
impeller::CompareFunction::kEqual
@ kEqual
Comparison test passes if new_value == current_value.
pipeline_descriptor.h
impeller::PipelineBuilder
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
Definition: pipeline_builder.h:31
impeller::PipelineDescriptor::SetLabel
PipelineDescriptor & SetLabel(std::string label)
Definition: pipeline_descriptor.cc:67
impeller::PipelineBuilder::kVertexBufferIndex
static constexpr size_t kVertexBufferIndex
Definition: pipeline_builder.h:36
impeller
Definition: aiks_context.cc:10
impeller::ColorAttachmentDescriptor::blending_enabled
bool blending_enabled
Definition: formats.h:453
impeller::PipelineBuilder::InitializePipelineDescriptorDefaults
static bool InitializePipelineDescriptorDefaults(const Context &context, PipelineDescriptor &desc)
Definition: pipeline_builder.h:59
impeller::PipelineDescriptor::SetVertexDescriptor
PipelineDescriptor & SetVertexDescriptor(std::shared_ptr< VertexDescriptor > vertex_descriptor)
Definition: pipeline_descriptor.cc:92
impeller::ColorAttachmentDescriptor
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:451