Flutter Impeller
pipeline.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_H_
6 #define FLUTTER_IMPELLER_RENDERER_PIPELINE_H_
7 
8 #include <future>
9 
17 
18 namespace impeller {
19 
20 class PipelineLibrary;
21 template <typename PipelineDescriptor_>
22 class Pipeline;
23 
24 template <typename T>
26  std::optional<T> descriptor;
27  std::shared_future<std::shared_ptr<Pipeline<T>>> future;
28 
29  const std::shared_ptr<Pipeline<T>> Get() const { return future.get(); }
30 
31  bool IsValid() const { return future.valid(); }
32 };
33 
34 //------------------------------------------------------------------------------
35 /// @brief Describes the fixed function and programmable aspects of
36 /// rendering and compute operations performed by commands submitted
37 /// to the GPU via a command buffer.
38 ///
39 /// A pipeline handle must be allocated upfront and kept alive for
40 /// as long as possible. Do not create a pipeline object within a
41 /// frame workload.
42 ///
43 /// This pipeline object is almost never used directly as it is
44 /// untyped. Use reflected shader information generated by the
45 /// Impeller offline shader compiler to generate a typed pipeline
46 /// object.
47 ///
48 template <typename T>
49 class Pipeline {
50  public:
51  virtual ~Pipeline();
52 
53  virtual bool IsValid() const = 0;
54 
55  //----------------------------------------------------------------------------
56  /// @brief Get the descriptor that was responsible for creating this
57  /// pipeline. It may be copied and modified to create a pipeline
58  /// variant.
59  ///
60  /// @return The descriptor.
61  ///
62  const T& GetDescriptor() const;
63 
64  PipelineFuture<T> CreateVariant(
65  bool async,
66  std::function<void(T& desc)> descriptor_callback) const;
67 
68  protected:
69  const std::weak_ptr<PipelineLibrary> library_;
70 
71  const T desc_;
72 
73  Pipeline(std::weak_ptr<PipelineLibrary> library, T desc);
74 
75  private:
76  Pipeline(const Pipeline&) = delete;
77 
78  Pipeline& operator=(const Pipeline&) = delete;
79 };
80 
81 extern template class Pipeline<PipelineDescriptor>;
82 extern template class Pipeline<ComputePipelineDescriptor>;
83 
85  const Context& context,
86  std::optional<PipelineDescriptor> desc);
87 
89  const Context& context,
90  std::optional<ComputePipelineDescriptor> desc);
91 
92 /// Holds a reference to a Pipeline used for rendering while also maintaining
93 /// the vertex shader and fragment shader types at compile-time.
94 ///
95 /// See also:
96 /// - impeller::ContentContext::Variants - the typical container for
97 /// RenderPipelineHandles.
98 template <class VertexShader_, class FragmentShader_>
100  static_assert(
102  "The output slots for the fragment shader don't have matches in the "
103  "vertex shader's output slots. This will result in a linker error.");
104 
105  public:
106  using VertexShader = VertexShader_;
107  using FragmentShader = FragmentShader_;
109 
110  explicit RenderPipelineHandle(const Context& context)
112  context,
113  Builder::MakeDefaultPipelineDescriptor(context))) {}
114 
115  explicit RenderPipelineHandle(const Context& context,
116  std::optional<PipelineDescriptor> desc)
117  : RenderPipelineHandle(CreatePipelineFuture(context, desc)) {}
118 
120  : pipeline_future_(std::move(future)) {}
121 
122  std::shared_ptr<Pipeline<PipelineDescriptor>> WaitAndGet() {
123  if (did_wait_) {
124  return pipeline_;
125  }
126  did_wait_ = true;
127  if (pipeline_future_.IsValid()) {
128  pipeline_ = pipeline_future_.Get();
129  }
130  return pipeline_;
131  }
132 
133  std::optional<PipelineDescriptor> GetDescriptor() const {
134  return pipeline_future_.descriptor;
135  }
136 
137  private:
138  PipelineFuture<PipelineDescriptor> pipeline_future_;
139  std::shared_ptr<Pipeline<PipelineDescriptor>> pipeline_;
140  bool did_wait_ = false;
141 
143 
144  RenderPipelineHandle& operator=(const RenderPipelineHandle&) = delete;
145 };
146 
147 template <class ComputeShader_>
149  public:
150  using ComputeShader = ComputeShader_;
152 
153  explicit ComputePipelineHandle(const Context& context)
155  context,
156  Builder::MakeDefaultPipelineDescriptor(context))) {}
157 
159  const Context& context,
160  std::optional<ComputePipelineDescriptor> compute_desc)
161  : ComputePipelineHandle(CreatePipelineFuture(context, compute_desc)) {}
162 
165  : pipeline_future_(std::move(future)) {}
166 
167  std::shared_ptr<Pipeline<ComputePipelineDescriptor>> WaitAndGet() {
168  if (did_wait_) {
169  return pipeline_;
170  }
171  did_wait_ = true;
172  if (pipeline_future_.IsValid()) {
173  pipeline_ = pipeline_future_.Get();
174  }
175  return pipeline_;
176  }
177 
178  private:
180  std::shared_ptr<Pipeline<ComputePipelineDescriptor>> pipeline_;
181  bool did_wait_ = false;
182 
184 
185  ComputePipelineHandle& operator=(const ComputePipelineHandle&) = delete;
186 };
187 
188 } // namespace impeller
189 
190 #endif // FLUTTER_IMPELLER_RENDERER_PIPELINE_H_
impeller::ComputePipelineHandle::ComputeShader
ComputeShader_ ComputeShader
Definition: pipeline.h:150
impeller::Pipeline
Describes the fixed function and programmable aspects of rendering and compute operations performed b...
Definition: compute_pipeline_descriptor.h:18
impeller::RenderPipelineHandle::RenderPipelineHandle
RenderPipelineHandle(const Context &context, std::optional< PipelineDescriptor > desc)
Definition: pipeline.h:115
impeller::RenderPipelineHandle::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:107
impeller::PipelineFuture::descriptor
std::optional< T > descriptor
Definition: pipeline.h:26
impeller::Pipeline::CreateVariant
PipelineFuture< T > CreateVariant(bool async, std::function< void(T &desc)> descriptor_callback) const
Definition: pipeline.cc:54
impeller::CreatePipelineFuture
PipelineFuture< PipelineDescriptor > CreatePipelineFuture(const Context &context, std::optional< PipelineDescriptor > desc)
Definition: pipeline.cc:24
impeller::ComputePipelineHandle
Definition: pipeline.h:148
shader_stage_compatibility_checker.h
pipeline_builder.h
impeller::RenderPipelineHandle
Definition: pipeline.h:99
impeller::PipelineFuture::IsValid
bool IsValid() const
Definition: pipeline.h:31
impeller::PipelineFuture
Definition: pipeline.h:25
impeller::Pipeline::Pipeline
Pipeline(std::weak_ptr< PipelineLibrary > library, T desc)
Definition: pipeline.cc:18
impeller::PipelineFuture::future
std::shared_future< std::shared_ptr< Pipeline< T > > > future
Definition: pipeline.h:27
compute_pipeline_descriptor.h
impeller::RenderPipelineHandle::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:106
compute_pipeline_builder.h
impeller::RenderPipelineHandle::GetDescriptor
std::optional< PipelineDescriptor > GetDescriptor() const
Definition: pipeline.h:133
impeller::Pipeline::GetDescriptor
const T & GetDescriptor() const
Get the descriptor that was responsible for creating this pipeline. It may be copied and modified to ...
Definition: pipeline.cc:49
impeller::Pipeline::desc_
const T desc_
Definition: pipeline.h:71
impeller::RenderPipelineHandle::WaitAndGet
std::shared_ptr< Pipeline< PipelineDescriptor > > WaitAndGet()
Definition: pipeline.h:122
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:45
std
Definition: comparable.h:95
impeller::Pipeline::~Pipeline
virtual ~Pipeline()
impeller::ComputePipelineHandle::WaitAndGet
std::shared_ptr< Pipeline< ComputePipelineDescriptor > > WaitAndGet()
Definition: pipeline.h:167
impeller::ComputePipelineHandle::ComputePipelineHandle
ComputePipelineHandle(const Context &context, std::optional< ComputePipelineDescriptor > compute_desc)
Definition: pipeline.h:158
impeller::ComputePipelineHandle::ComputePipelineHandle
ComputePipelineHandle(const Context &context)
Definition: pipeline.h:153
context.h
impeller::PipelineFuture::Get
const std::shared_ptr< Pipeline< T > > Get() const
Definition: pipeline.h:29
pipeline_descriptor.h
impeller::PipelineBuilder
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
Definition: pipeline_builder.h:30
impeller::Pipeline::IsValid
virtual bool IsValid() const =0
impeller
Definition: aiks_blend_unittests.cc:18
impeller::Pipeline::library_
const std::weak_ptr< PipelineLibrary > library_
Definition: pipeline.h:69
impeller::ShaderStageCompatibilityChecker
Definition: shader_stage_compatibility_checker.h:18
impeller::RenderPipelineHandle::RenderPipelineHandle
RenderPipelineHandle(const Context &context)
Definition: pipeline.h:110
impeller::ComputePipelineHandle::ComputePipelineHandle
ComputePipelineHandle(PipelineFuture< ComputePipelineDescriptor > future)
Definition: pipeline.h:163
impeller::ComputePipelineBuilder
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
Definition: compute_pipeline_builder.h:25
impeller::RenderPipelineHandle::RenderPipelineHandle
RenderPipelineHandle(PipelineFuture< PipelineDescriptor > future)
Definition: pipeline.h:119