| 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 #pragma once
6
7 #include <future>
8
9 #include "compute_pipeline_descriptor.h"
10 #include "flutter/fml/macros.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 FML_DISALLOW_COPY_AND_ASSIGN(Pipeline);
74 };
75
76 extern template class Pipeline<PipelineDescriptor>;
77 extern template class Pipeline<ComputePipelineDescriptor>;
78
79 PipelineFuture<PipelineDescriptor> CreatePipelineFuture(
80 const Context& context,
81 std::optional<PipelineDescriptor> desc);
82
83 PipelineFuture<ComputePipelineDescriptor> CreatePipelineFuture(
84 const Context& context,
85 std::optional<ComputePipelineDescriptor> desc);
86
87 template <class VertexShader_, class FragmentShader_>
88 class RenderPipelineT {
89 public:
90 using VertexShader = VertexShader_;
91 using FragmentShader = FragmentShader_;
92 using Builder = PipelineBuilder<VertexShader, FragmentShader>;
93
94 explicit RenderPipelineT(const Context& context)
95 : RenderPipelineT(CreatePipelineFuture(
96 context,
97 Builder::MakeDefaultPipelineDescriptor(context))) {}
98
99 explicit RenderPipelineT(const Context& context,
100 std::optional<PipelineDescriptor> desc)
101 : RenderPipelineT(CreatePipelineFuture(context, desc)) {}
102
103 explicit RenderPipelineT(PipelineFuture<PipelineDescriptor> future)
104 : pipeline_future_(std::move(future)) {}
105
106 std::shared_ptr<Pipeline<PipelineDescriptor>> WaitAndGet() {
107 if (did_wait_) {
108 return pipeline_;
109 }
110 did_wait_ = true;
111 if (pipeline_future_.IsValid()) {
112 pipeline_ = pipeline_future_.Get();
113 }
114 return pipeline_;
115 }
116
117 std::optional<PipelineDescriptor> GetDescriptor() const {
118 return pipeline_future_.descriptor;
119 }
120
121 private:
122 PipelineFuture<PipelineDescriptor> pipeline_future_;
123 std::shared_ptr<Pipeline<PipelineDescriptor>> pipeline_;
124 bool did_wait_ = false;
125
126 FML_DISALLOW_COPY_AND_ASSIGN(RenderPipelineT);
127 };
128
129 template <class ComputeShader_>
130 class ComputePipelineT {
131 public:
132 using ComputeShader = ComputeShader_;
133 using Builder = ComputePipelineBuilder<ComputeShader>;
134
135 explicit ComputePipelineT(const Context& context)
136 : ComputePipelineT(CreatePipelineFuture(
137 context,
138 Builder::MakeDefaultPipelineDescriptor(context))) {}
139
140 explicit ComputePipelineT(
141 const Context& context,
142 std::optional<ComputePipelineDescriptor> compute_desc)
143 : ComputePipelineT(CreatePipelineFuture(context, compute_desc)) {}
144
145 explicit ComputePipelineT(PipelineFuture<ComputePipelineDescriptor> future)
146 : pipeline_future_(std::move(future)) {}
147
148 std::shared_ptr<Pipeline<ComputePipelineDescriptor>> WaitAndGet() {
149 if (did_wait_) {
150 return pipeline_;
151 }
152 did_wait_ = true;
153 if (pipeline_future_.IsValid()) {
154 pipeline_ = pipeline_future_.Get();
155 }
156 return pipeline_;
157 }
158
159 private:
160 PipelineFuture<ComputePipelineDescriptor> pipeline_future_;
161 std::shared_ptr<Pipeline<ComputePipelineDescriptor>> pipeline_;
162 bool did_wait_ = false;
163
164 FML_DISALLOW_COPY_AND_ASSIGN(ComputePipelineT);
165 };
166
167 } // namespace impeller
impeller::ComputePipelineT::ComputePipelineT
ComputePipelineT(const Context &context, std::optional< ComputePipelineDescriptor > compute_desc)
Definition: pipeline.h:140
impeller::RenderPipelineT::RenderPipelineT
RenderPipelineT(const Context &context)
Definition: pipeline.h:94
impeller::PipelineFuture::descriptor
std::optional< T > descriptor
Definition: pipeline.h:25
impeller::RenderPipelineT::WaitAndGet
std::shared_ptr< Pipeline< PipelineDescriptor > > WaitAndGet()
Definition: pipeline.h:106
impeller::ComputePipelineT::ComputePipelineT
ComputePipelineT(PipelineFuture< ComputePipelineDescriptor > future)
Definition: pipeline.h:145
impeller::CreatePipelineFuture
PipelineFuture< PipelineDescriptor > CreatePipelineFuture(const Context &context, std::optional< PipelineDescriptor > desc)
Definition: pipeline.cc:24
impeller::RenderPipelineT::FragmentShader
FragmentShader_ FragmentShader
Definition: pipeline.h:91
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:99
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:117
impeller::ComputePipelineT
Definition: pipeline.h:130
compute_pipeline_builder.h
impeller::ComputePipelineT::ComputePipelineT
ComputePipelineT(const Context &context)
Definition: pipeline.h:135
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:103
impeller::ComputePipelineT::ComputeShader
ComputeShader_ ComputeShader
Definition: pipeline.h:132
impeller::Context
To do anything rendering related with Impeller, you need a context.
Definition: context.h:47
std
Definition: comparable.h:98
impeller::ComputePipelineT::WaitAndGet
std::shared_ptr< Pipeline< ComputePipelineDescriptor > > WaitAndGet()
Definition: pipeline.h:148
impeller::Pipeline::~Pipeline
virtual ~Pipeline()
impeller::RenderPipelineT
Definition: pipeline.h:88
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:31
impeller::RenderPipelineT::VertexShader
VertexShader_ VertexShader
Definition: pipeline.h:90
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:28