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