| 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
10 #include "compute_pipeline_descriptor.h"
11 #include "impeller/renderer/compute_pipeline_builder.h"
12 #include "impeller/renderer/compute_pipeline_descriptor.h"
13 #include "impeller/renderer/context.h"
14 #include "impeller/renderer/pipeline_builder.h"
15 #include "impeller/renderer/pipeline_descriptor.h"
16 #include "impeller/renderer/shader_stage_compatibility_checker.h"
17
18 namespace impeller {
19
20 class PipelineLibrary;
21 template <typename PipelineDescriptor_>
22 class Pipeline;
23
24 template <typename T>
25 struct PipelineFuture {
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
84 PipelineFuture<PipelineDescriptor> CreatePipelineFuture(
85 const Context& context,
86 std::optional<PipelineDescriptor> desc);
87
88 PipelineFuture<ComputePipelineDescriptor> CreatePipelineFuture(
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_>
99 class RenderPipelineHandle {
100 static_assert(
101 ShaderStageCompatibilityChecker<VertexShader_, FragmentShader_>::Check(),
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_;
108 using Builder = PipelineBuilder<VertexShader, FragmentShader>;
109
110 explicit RenderPipelineHandle(const Context& context)
111 : RenderPipelineHandle(CreatePipelineFuture(
112 context,
113 Builder::MakeDefaultPipelineDescriptor(context))) {}
114
115 explicit RenderPipelineHandle(const Context& context,
116 std::optional<PipelineDescriptor> desc)
117 : RenderPipelineHandle(CreatePipelineFuture(context, desc)) {}
118
119 explicit RenderPipelineHandle(PipelineFuture<PipelineDescriptor> future)
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
142 RenderPipelineHandle(const RenderPipelineHandle&) = delete;
143
144 RenderPipelineHandle& operator=(const RenderPipelineHandle&) = delete;
145 };
146
147 template <class ComputeShader_>
148 class ComputePipelineHandle {
149 public:
150 using ComputeShader = ComputeShader_;
151 using Builder = ComputePipelineBuilder<ComputeShader>;
152
153 explicit ComputePipelineHandle(const Context& context)
154 : ComputePipelineHandle(CreatePipelineFuture(
155 context,
156 Builder::MakeDefaultPipelineDescriptor(context))) {}
157
158 explicit ComputePipelineHandle(
159 const Context& context,
160 std::optional<ComputePipelineDescriptor> compute_desc)
161 : ComputePipelineHandle(CreatePipelineFuture(context, compute_desc)) {}
162
163 explicit ComputePipelineHandle(
164 PipelineFuture<ComputePipelineDescriptor> future)
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:
179 PipelineFuture<ComputePipelineDescriptor> pipeline_future_;
180 std::shared_ptr<Pipeline<ComputePipelineDescriptor>> pipeline_;
181 bool did_wait_ = false;
182
183 ComputePipelineHandle(const ComputePipelineHandle&) = delete;
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:46
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: allocation.cc:12
impeller::Pipeline::library_
const std::weak_ptr< PipelineLibrary > library_
Definition: pipeline.h:69
impeller::ShaderStageCompatibilityChecker
Checks, at C++ compile-time, if the two pipeline stages are compatible.
Definition: shader_stage_compatibility_checker.h:39
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