Flutter Engine
The Flutter Engine
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 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
81extern template class Pipeline<PipelineDescriptor>;
82extern 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.
98template <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)
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
147template <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_
ComputePipelineHandle(PipelineFuture< ComputePipelineDescriptor > future)
Definition: pipeline.h:163
ComputePipelineHandle(const Context &context, std::optional< ComputePipelineDescriptor > compute_desc)
Definition: pipeline.h:158
ComputePipelineHandle(const Context &context)
Definition: pipeline.h:153
ComputeShader_ ComputeShader
Definition: pipeline.h:150
std::shared_ptr< Pipeline< ComputePipelineDescriptor > > WaitAndGet()
Definition: pipeline.h:167
To do anything rendering related with Impeller, you need a context.
Definition: context.h:45
Describes the fixed function and programmable aspects of rendering and compute operations performed b...
Definition: pipeline.h:49
PipelineFuture< T > CreateVariant(bool async, std::function< void(T &desc)> descriptor_callback) const
Definition: pipeline.cc:54
const std::weak_ptr< PipelineLibrary > library_
Definition: pipeline.h:69
virtual bool IsValid() const =0
Pipeline(std::weak_ptr< PipelineLibrary > library, T desc)
Definition: pipeline.cc:18
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:115
std::optional< PipelineDescriptor > GetDescriptor() const
Definition: pipeline.h:133
RenderPipelineHandle(PipelineFuture< PipelineDescriptor > future)
Definition: pipeline.h:119
std::shared_ptr< Pipeline< PipelineDescriptor > > WaitAndGet()
Definition: pipeline.h:122
RenderPipelineHandle(const Context &context)
Definition: pipeline.h:110
FragmentShader_ FragmentShader
Definition: pipeline.h:107
Dart_NativeFunction function
Definition: fuchsia.cc:51
PipelineFuture< PipelineDescriptor > CreatePipelineFuture(const Context &context, std::optional< PipelineDescriptor > desc)
Definition: pipeline.cc:24
Definition: ref_ptr.h:256
#define T
Definition: precompiler.cc:65
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