| 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
17 namespace impeller {
18
19 class PipelineLibrary;
20 template <typename PipelineDescriptor_>
21 class Pipeline;
22
23 template <typename T>
24 struct PipelineFuture {
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 Pipeline(std::weak_ptr<PipelineLibrary> library, T desc);
68
69 private:
70 const std::weak_ptr<PipelineLibrary> library_;
71 const T desc_;
72
73 Pipeline(const Pipeline&) = delete;
74
75 Pipeline& operator=(const Pipeline&) = delete;
76 };
77
78 extern template class Pipeline<PipelineDescriptor>;
79 extern template class Pipeline<ComputePipelineDescriptor>;
80
81 PipelineFuture<PipelineDescriptor> CreatePipelineFuture(
82 const Context& context,
83 std::optional<PipelineDescriptor> desc);
84
85 PipelineFuture<ComputePipelineDescriptor> CreatePipelineFuture(
86 const Context& context,
87 std::optional<ComputePipelineDescriptor> desc);
88
89 template <class VertexShader_, class FragmentShader_>
90 class RenderPipelineT {
91 public:
92 using VertexShader = VertexShader_;
93 using FragmentShader = FragmentShader_;
94 using Builder = PipelineBuilder<VertexShader, FragmentShader>;
95
96 explicit RenderPipelineT(const Context& context)
97 : RenderPipelineT(CreatePipelineFuture(
98 context,
99 Builder::MakeDefaultPipelineDescriptor(context))) {}
100
101 explicit RenderPipelineT(const Context& context,
102 std::optional<PipelineDescriptor> desc)
103 : RenderPipelineT(CreatePipelineFuture(context, desc)) {}
104
105 explicit RenderPipelineT(PipelineFuture<PipelineDescriptor> future)
106 : pipeline_future_(std::move(future)) {}
107
108 std::shared_ptr<Pipeline<PipelineDescriptor>> WaitAndGet() {
109 if (did_wait_) {
110 return pipeline_;
111 }
112 did_wait_ = true;
113 if (pipeline_future_.IsValid()) {
114 pipeline_ = pipeline_future_.Get();
115 }
116 return pipeline_;
117 }
118
119 std::optional<PipelineDescriptor> GetDescriptor() const {
120 return pipeline_future_.descriptor;
121 }
122
123 private:
124 PipelineFuture<PipelineDescriptor> pipeline_future_;
125 std::shared_ptr<Pipeline<PipelineDescriptor>> pipeline_;
126 bool did_wait_ = false;
127
128 RenderPipelineT(const RenderPipelineT&) = delete;
129
130 RenderPipelineT& operator=(const RenderPipelineT&) = delete;
131 };
132
133 template <class ComputeShader_>
134 class ComputePipelineT {
135 public:
136 using ComputeShader = ComputeShader_;
137 using Builder = ComputePipelineBuilder<ComputeShader>;
138
139 explicit ComputePipelineT(const Context& context)
140 : ComputePipelineT(CreatePipelineFuture(
141 context,
142 Builder::MakeDefaultPipelineDescriptor(context))) {}
143
144 explicit ComputePipelineT(
145 const Context& context,
146 std::optional<ComputePipelineDescriptor> compute_desc)
147 : ComputePipelineT(CreatePipelineFuture(context, compute_desc)) {}
148
149 explicit ComputePipelineT(PipelineFuture<ComputePipelineDescriptor> future)
150 : pipeline_future_(std::move(future)) {}
151
152 std::shared_ptr<Pipeline<ComputePipelineDescriptor>> WaitAndGet() {
153 if (did_wait_) {
154 return pipeline_;
155 }
156 did_wait_ = true;
157 if (pipeline_future_.IsValid()) {
158 pipeline_ = pipeline_future_.Get();
159 }
160 return pipeline_;
161 }
162
163 private:
164 PipelineFuture<ComputePipelineDescriptor> pipeline_future_;
165 std::shared_ptr<Pipeline<ComputePipelineDescriptor>> pipeline_;
166 bool did_wait_ = false;
167
168 ComputePipelineT(const ComputePipelineT&) = delete;
169
170 ComputePipelineT& operator=(const ComputePipelineT&) = delete;
171 };
172
173 } // namespace impeller
174
175 #endif // FLUTTER_IMPELLER_RENDERER_PIPELINE_H_
impeller::ComputePipelineT::ComputePipelineT
ComputePipelineT(const Context &context, std::optional< ComputePipelineDescriptor > compute_desc)
Definition: pipeline.h:144
impeller::RenderPipelineT::RenderPipelineT
RenderPipelineT(const Context &context)
Definition: pipeline.h:96
impeller::PipelineFuture::descriptor
std::optional< T > descriptor
Definition: pipeline.h:25
impeller::RenderPipelineT::WaitAndGet
std::shared_ptr< Pipeline< PipelineDescriptor > > WaitAndGet()
Definition: pipeline.h:108
impeller::ComputePipelineT::ComputePipelineT
ComputePipelineT(PipelineFuture< ComputePipelineDescriptor > future)
Definition: pipeline.h:149
impeller::CreatePipelineFuture
PipelineFuture< PipelineDescriptor > CreatePipelineFuture(const Context &context, std::optional< PipelineDescriptor > desc)
Definition: pipeline.cc:24
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:93
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:101
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:119
impeller::ComputePipelineT
Definition: pipeline.h:134
compute_pipeline_builder.h
impeller::ComputePipelineT::ComputePipelineT
ComputePipelineT(const Context &context)
Definition: pipeline.h:139
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::RenderPipelineT::RenderPipelineT
RenderPipelineT(PipelineFuture< PipelineDescriptor > future)
Definition: pipeline.h:105
impeller::ComputePipelineT::ComputeShader
ComputeShader_ ComputeShader
Definition: pipeline.h:136
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:47
std
Definition: comparable.h:95
impeller::ComputePipelineT::WaitAndGet
std::shared_ptr< Pipeline< ComputePipelineDescriptor > > WaitAndGet()
Definition: pipeline.h:152
impeller::Pipeline::~Pipeline
virtual ~Pipeline()
impeller::RenderPipelineT
Definition: pipeline.h:90
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:92
impeller::Pipeline::IsValid
virtual bool IsValid() const =0
impeller
Definition: aiks_context.cc:10
impeller::ComputePipelineBuilder
An optional (but highly recommended) utility for creating pipelines from reflected shader information...
Definition: compute_pipeline_builder.h:25