Flutter Impeller
pipeline_descriptor.cc
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 
6 
7 #include <memory>
8 #include <utility>
9 
11 #include "impeller/core/formats.h"
15 
16 namespace impeller {
17 
19 
21 
22 // Comparable<PipelineDescriptor>
23 std::size_t PipelineDescriptor::GetHash() const {
24  auto seed = fml::HashCombine();
25  fml::HashCombineSeed(seed, label_);
26  fml::HashCombineSeed(seed, sample_count_);
27  for (const auto& entry : entrypoints_) {
28  fml::HashCombineSeed(seed, entry.first);
29  if (auto second = entry.second) {
30  fml::HashCombineSeed(seed, second->GetHash());
31  }
32  }
33  for (const auto& des : color_attachment_descriptors_) {
34  fml::HashCombineSeed(seed, des.first);
35  fml::HashCombineSeed(seed, des.second.Hash());
36  }
37  if (vertex_descriptor_) {
38  fml::HashCombineSeed(seed, vertex_descriptor_->GetHash());
39  }
40  fml::HashCombineSeed(seed, depth_pixel_format_);
41  fml::HashCombineSeed(seed, stencil_pixel_format_);
42  fml::HashCombineSeed(seed, depth_attachment_descriptor_);
43  fml::HashCombineSeed(seed, front_stencil_attachment_descriptor_);
44  fml::HashCombineSeed(seed, back_stencil_attachment_descriptor_);
45  fml::HashCombineSeed(seed, winding_order_);
46  fml::HashCombineSeed(seed, cull_mode_);
47  fml::HashCombineSeed(seed, primitive_type_);
48  fml::HashCombineSeed(seed, polygon_mode_);
49  return seed;
50 }
51 
52 // Comparable<PipelineDescriptor>
54  return label_ == other.label_ && sample_count_ == other.sample_count_ &&
55  DeepCompareMap(entrypoints_, other.entrypoints_) &&
56  color_attachment_descriptors_ == other.color_attachment_descriptors_ &&
57  DeepComparePointer(vertex_descriptor_, other.vertex_descriptor_) &&
58  stencil_pixel_format_ == other.stencil_pixel_format_ &&
59  depth_pixel_format_ == other.depth_pixel_format_ &&
60  depth_attachment_descriptor_ == other.depth_attachment_descriptor_ &&
61  front_stencil_attachment_descriptor_ ==
62  other.front_stencil_attachment_descriptor_ &&
63  back_stencil_attachment_descriptor_ ==
64  other.back_stencil_attachment_descriptor_ &&
65  winding_order_ == other.winding_order_ &&
66  cull_mode_ == other.cull_mode_ &&
67  primitive_type_ == other.primitive_type_ &&
68  polygon_mode_ == other.polygon_mode_ &&
69  specialization_constants_ == other.specialization_constants_;
70 }
71 
73  label_ = std::string(label);
74  return *this;
75 }
76 
78  sample_count_ = samples;
79  return *this;
80 }
81 
83  std::shared_ptr<const ShaderFunction> function) {
84  if (!function) {
85  return *this;
86  }
87 
88  if (function->GetStage() == ShaderStage::kUnknown) {
89  return *this;
90  }
91 
92  entrypoints_[function->GetStage()] = std::move(function);
93 
94  return *this;
95 }
96 
98  std::shared_ptr<VertexDescriptor> vertex_descriptor) {
99  vertex_descriptor_ = std::move(vertex_descriptor);
100  return *this;
101 }
102 
104  size_t max = 0;
105  for (const auto& color : color_attachment_descriptors_) {
106  max = std::max(color.first, max);
107  }
108  return max;
109 }
110 
112  size_t index,
114  color_attachment_descriptors_[index] = desc;
115  return *this;
116 }
117 
119  std::map<size_t /* index */, ColorAttachmentDescriptor> descriptors) {
120  color_attachment_descriptors_ = std::move(descriptors);
121  return *this;
122 }
123 
126  auto found = color_attachment_descriptors_.find(index);
127  return found == color_attachment_descriptors_.end() ? nullptr
128  : &found->second;
129 }
130 
133  // Legacy renderers may only render to a single color attachment at index 0u.
134  if (color_attachment_descriptors_.size() != 1u) {
135  return nullptr;
136  }
137  return GetColorAttachmentDescriptor(0u);
138 }
139 
141  PixelFormat format) {
142  depth_pixel_format_ = format;
143  return *this;
144 }
145 
147  PixelFormat format) {
148  stencil_pixel_format_ = format;
149  return *this;
150 }
151 
153  std::optional<DepthAttachmentDescriptor> desc) {
154  depth_attachment_descriptor_ = desc;
155  return *this;
156 }
157 
159  std::optional<StencilAttachmentDescriptor> front_and_back) {
160  return SetStencilAttachmentDescriptors(front_and_back, front_and_back);
161 }
162 
164  std::optional<StencilAttachmentDescriptor> front,
165  std::optional<StencilAttachmentDescriptor> back) {
166  front_stencil_attachment_descriptor_ = front;
167  back_stencil_attachment_descriptor_ = back;
168  return *this;
169 }
170 
172  back_stencil_attachment_descriptor_.reset();
173  front_stencil_attachment_descriptor_.reset();
175 }
176 
178  depth_attachment_descriptor_.reset();
180 }
181 
183  if (color_attachment_descriptors_.find(index) ==
184  color_attachment_descriptors_.end()) {
185  return;
186  }
187 
188  color_attachment_descriptors_.erase(index);
189 }
190 
192  color_attachment_descriptors_.clear();
193  depth_attachment_descriptor_.reset();
194  front_stencil_attachment_descriptor_.reset();
195  back_stencil_attachment_descriptor_.reset();
196 }
197 
199  return stencil_pixel_format_;
200 }
201 
202 std::optional<StencilAttachmentDescriptor>
204  return front_stencil_attachment_descriptor_;
205 }
206 
207 std::optional<DepthAttachmentDescriptor>
209  return depth_attachment_descriptor_;
210 }
211 
212 const std::map<size_t /* index */, ColorAttachmentDescriptor>&
214  return color_attachment_descriptors_;
215 }
216 
217 const std::shared_ptr<VertexDescriptor>&
219  return vertex_descriptor_;
220 }
221 
222 const std::map<ShaderStage, std::shared_ptr<const ShaderFunction>>&
224  return entrypoints_;
225 }
226 
227 std::shared_ptr<const ShaderFunction> PipelineDescriptor::GetEntrypointForStage(
228  ShaderStage stage) const {
229  if (auto found = entrypoints_.find(stage); found != entrypoints_.end()) {
230  return found->second;
231  }
232  return nullptr;
233 }
234 
235 std::string_view PipelineDescriptor::GetLabel() const {
236  return label_;
237 }
238 
240  return depth_pixel_format_;
241 }
242 
243 std::optional<StencilAttachmentDescriptor>
245  return back_stencil_attachment_descriptor_;
246 }
247 
249  return front_stencil_attachment_descriptor_.has_value() ||
250  back_stencil_attachment_descriptor_.has_value();
251 }
252 
254  cull_mode_ = mode;
255 }
256 
258  return cull_mode_;
259 }
260 
262  winding_order_ = order;
263 }
264 
266  return winding_order_;
267 }
268 
270  primitive_type_ = type;
271 }
272 
274  return primitive_type_;
275 }
276 
278  polygon_mode_ = mode;
279 }
280 
282  return polygon_mode_;
283 }
284 
286  std::vector<Scalar> values) {
287  specialization_constants_ = std::move(values);
288 }
289 
291  const {
292  return specialization_constants_;
293 }
294 
295 } // namespace impeller
GLenum type
PipelineDescriptor & SetStencilPixelFormat(PixelFormat format)
PipelineDescriptor & SetDepthStencilAttachmentDescriptor(std::optional< DepthAttachmentDescriptor > desc)
std::string_view GetLabel() const
std::size_t GetHash() const override
PixelFormat GetDepthPixelFormat() const
PipelineDescriptor & SetVertexDescriptor(std::shared_ptr< VertexDescriptor > vertex_descriptor)
void SetPolygonMode(PolygonMode mode)
PrimitiveType GetPrimitiveType() const
std::optional< DepthAttachmentDescriptor > GetDepthStencilAttachmentDescriptor() const
PipelineDescriptor & AddStageEntrypoint(std::shared_ptr< const ShaderFunction > function)
bool IsEqual(const PipelineDescriptor &other) const override
const std::map< ShaderStage, std::shared_ptr< const ShaderFunction > > & GetStageEntrypoints() const
PixelFormat GetStencilPixelFormat() const
const ColorAttachmentDescriptor * GetLegacyCompatibleColorAttachment() const
PipelineDescriptor & SetLabel(std::string_view label)
void SetSpecializationConstants(std::vector< Scalar > values)
const std::vector< Scalar > & GetSpecializationConstants() const
PipelineDescriptor & SetDepthPixelFormat(PixelFormat format)
PipelineDescriptor & SetStencilAttachmentDescriptors(std::optional< StencilAttachmentDescriptor > front_and_back)
const ColorAttachmentDescriptor * GetColorAttachmentDescriptor(size_t index) const
WindingOrder GetWindingOrder() const
PipelineDescriptor & SetColorAttachmentDescriptor(size_t index, ColorAttachmentDescriptor desc)
PipelineDescriptor & SetSampleCount(SampleCount samples)
void SetPrimitiveType(PrimitiveType type)
std::shared_ptr< const ShaderFunction > GetEntrypointForStage(ShaderStage stage) const
const std::map< size_t, ColorAttachmentDescriptor > & GetColorAttachmentDescriptors() const
std::optional< StencilAttachmentDescriptor > GetBackStencilAttachmentDescriptor() const
const std::shared_ptr< VertexDescriptor > & GetVertexDescriptor() const
std::optional< StencilAttachmentDescriptor > GetFrontStencilAttachmentDescriptor() const
void SetWindingOrder(WindingOrder order)
void ClearColorAttachment(size_t index)
PipelineDescriptor & SetColorAttachmentDescriptors(std::map< size_t, ColorAttachmentDescriptor > descriptors)
PrimitiveType
Decides how backend draws pixels based on input vertices.
Definition: formats.h:355
PixelFormat
The Pixel formats supported by Impeller. The naming convention denotes the usage of the component,...
Definition: formats.h:99
WindingOrder
Definition: formats.h:22
bool DeepComparePointer(const std::shared_ptr< ComparableType > &lhs, const std::shared_ptr< ComparableType > &rhs)
Definition: comparable.h:57
PolygonMode
Definition: formats.h:392
bool DeepCompareMap(const std::map< Key, std::shared_ptr< ComparableType >> &lhs, const std::map< Key, std::shared_ptr< ComparableType >> &rhs)
Definition: comparable.h:74
SampleCount
Definition: formats.h:298
Describe the color attachment that will be used with this pipeline.
Definition: formats.h:522