Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
18namespace impeller {
19
20class PipelineLibrary;
21template <typename PipelineDescriptor_>
22class Pipeline;
23
24template <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///
48template <typename T>
49class 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
65 std::function<void(T& desc)> descriptor_callback) const;
66
67 protected:
68 const std::weak_ptr<PipelineLibrary> library_;
69
70 const T desc_;
71
72 Pipeline(std::weak_ptr<PipelineLibrary> library, T desc);
73
74 private:
75 Pipeline(const Pipeline&) = delete;
76
77 Pipeline& operator=(const Pipeline&) = delete;
78};
79
80extern template class Pipeline<PipelineDescriptor>;
81extern template class Pipeline<ComputePipelineDescriptor>;
82
84 const Context& context,
85 std::optional<PipelineDescriptor> desc);
86
88 const Context& context,
89 std::optional<ComputePipelineDescriptor> desc);
90
91/// Holds a reference to a Pipeline used for rendering while also maintaining
92/// the vertex shader and fragment shader types at compile-time.
93///
94/// See also:
95/// - impeller::ContentContext::Variants - the typical container for
96/// RenderPipelineHandles.
97template <class VertexShader_, class FragmentShader_>
99 static_assert(
101 "The output slots for the fragment shader don't have matches in the "
102 "vertex shader's output slots. This will result in a linker error.");
103
104 public:
105 using VertexShader = VertexShader_;
106 using FragmentShader = FragmentShader_;
108
109 explicit RenderPipelineHandle(const Context& context)
111 context,
112 Builder::MakeDefaultPipelineDescriptor(context))) {}
113
114 explicit RenderPipelineHandle(const Context& context,
115 std::optional<PipelineDescriptor> desc)
116 : RenderPipelineHandle(CreatePipelineFuture(context, desc)) {}
117
119 : pipeline_future_(std::move(future)) {}
120
121 std::shared_ptr<Pipeline<PipelineDescriptor>> WaitAndGet() {
122 if (did_wait_) {
123 return pipeline_;
124 }
125 did_wait_ = true;
126 if (pipeline_future_.IsValid()) {
127 pipeline_ = pipeline_future_.Get();
128 }
129 return pipeline_;
130 }
131
132 std::optional<PipelineDescriptor> GetDescriptor() const {
133 return pipeline_future_.descriptor;
134 }
135
136 private:
137 PipelineFuture<PipelineDescriptor> pipeline_future_;
138 std::shared_ptr<Pipeline<PipelineDescriptor>> pipeline_;
139 bool did_wait_ = false;
140
142
143 RenderPipelineHandle& operator=(const RenderPipelineHandle&) = delete;
144};
145
146template <class ComputeShader_>
148 public:
149 using ComputeShader = ComputeShader_;
151
152 explicit ComputePipelineHandle(const Context& context)
154 context,
155 Builder::MakeDefaultPipelineDescriptor(context))) {}
156
158 const Context& context,
159 std::optional<ComputePipelineDescriptor> compute_desc)
160 : ComputePipelineHandle(CreatePipelineFuture(context, compute_desc)) {}
161
164 : pipeline_future_(std::move(future)) {}
165
166 std::shared_ptr<Pipeline<ComputePipelineDescriptor>> WaitAndGet() {
167 if (did_wait_) {
168 return pipeline_;
169 }
170 did_wait_ = true;
171 if (pipeline_future_.IsValid()) {
172 pipeline_ = pipeline_future_.Get();
173 }
174 return pipeline_;
175 }
176
177 private:
179 std::shared_ptr<Pipeline<ComputePipelineDescriptor>> pipeline_;
180 bool did_wait_ = false;
181
183
184 ComputePipelineHandle& operator=(const ComputePipelineHandle&) = delete;
185};
186
187} // namespace impeller
188
189#endif // FLUTTER_IMPELLER_RENDERER_PIPELINE_H_
ComputePipelineHandle(PipelineFuture< ComputePipelineDescriptor > future)
Definition pipeline.h:162
ComputePipelineHandle(const Context &context, std::optional< ComputePipelineDescriptor > compute_desc)
Definition pipeline.h:157
ComputePipelineHandle(const Context &context)
Definition pipeline.h:152
std::shared_ptr< Pipeline< ComputePipelineDescriptor > > WaitAndGet()
Definition pipeline.h:166
To do anything rendering related with Impeller, you need a context.
Definition context.h:46
Describes the fixed function and programmable aspects of rendering and compute operations performed b...
Definition pipeline.h:49
PipelineFuture< T > CreateVariant(std::function< void(T &desc)> descriptor_callback) const
Definition pipeline.cc:54
const std::weak_ptr< PipelineLibrary > library_
Definition pipeline.h:68
virtual bool IsValid() const =0
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
RenderPipelineHandle(const Context &context, std::optional< PipelineDescriptor > desc)
Definition pipeline.h:114
std::optional< PipelineDescriptor > GetDescriptor() const
Definition pipeline.h:132
RenderPipelineHandle(PipelineFuture< PipelineDescriptor > future)
Definition pipeline.h:118
std::shared_ptr< Pipeline< PipelineDescriptor > > WaitAndGet()
Definition pipeline.h:121
RenderPipelineHandle(const Context &context)
Definition pipeline.h:109
FragmentShader_ FragmentShader
Definition pipeline.h:106
PipelineFuture< PipelineDescriptor > CreatePipelineFuture(const Context &context, std::optional< PipelineDescriptor > desc)
Definition pipeline.cc:24
Definition ref_ptr.h:256
#define T
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
const std::shared_ptr< Pipeline< T > > Get() const
Definition pipeline.h:29
std::shared_future< std::shared_ptr< Pipeline< T > > > future
Definition pipeline.h:27
bool IsValid() const
Definition pipeline.h:31
std::optional< T > descriptor
Definition pipeline.h:26