Flutter Impeller
shader_types.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_CORE_SHADER_TYPES_H_
6 #define FLUTTER_IMPELLER_CORE_SHADER_TYPES_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 #include <optional>
11 #include <string_view>
12 #include <vector>
13 
14 #include "flutter/fml/hash_combine.h"
15 #include "flutter/fml/logging.h"
17 #include "impeller/geometry/half.h"
19 
20 namespace impeller {
21 
22 enum class ShaderStage {
23  kUnknown,
24  kVertex,
25  kFragment,
26  kCompute,
27 };
28 
30  switch (stage) {
32  return ShaderStage::kVertex;
36  return ShaderStage::kCompute;
37  }
38  FML_UNREACHABLE();
39 }
40 
41 enum class ShaderType {
42  kUnknown,
43  kVoid,
44  kBoolean,
49  kSignedInt,
54  kHalfFloat,
55  kFloat,
56  kDouble,
57  kStruct,
58  kImage,
60  kSampler,
61 };
62 
63 // This is a separate type from ShaderType because ShaderType is used for
64 // OpenGLES's attrib type which doesn't map to things like vec4.
65 enum class ShaderFloatType {
66  kFloat,
67  kVec2,
68  kVec3,
69  kVec4,
70  kMat2,
71  kMat3,
72  kMat4,
73 };
74 
77  std::string name;
78  size_t offset;
79  size_t size;
80  size_t byte_length;
81  std::optional<size_t> array_elements;
82  std::optional<ShaderFloatType> float_type;
83 };
84 
86  // This must match the uniform name in the shader program.
87  std::string name;
88  std::vector<ShaderStructMemberMetadata> members;
89 };
90 
91 /// @brief Metadata required to bind a buffer.
92 ///
93 /// OpenGL binding requires the usage of the separate shader metadata struct.
95  /// @brief The name of the uniform slot.
96  const char* name;
97 
98  /// @brief `ext_res_0` is the Metal binding value.
99  size_t ext_res_0;
100 
101  /// @brief The Vulkan descriptor set index.
102  size_t set;
103 
104  /// @brief The Vulkan binding value.
105  size_t binding;
106 };
107 
108 /// @brief Metadata required to bind a combined texture and sampler.
109 ///
110 /// OpenGL binding requires the usage of the separate shader metadata struct.
112  /// @brief The name of the uniform slot.
113  const char* name;
114 
115  /// @brief `ext_res_0` is the Metal binding value.
117 
118  /// @brief The Vulkan descriptor set index.
119  size_t set;
120 
121  /// @brief The Vulkan binding value.
122  size_t binding;
123 };
124 
126  const char* name;
127  size_t location;
128  size_t set;
129  size_t binding;
131  size_t bit_width;
132  size_t vec_size;
133  size_t columns;
134  size_t offset;
136 
137  constexpr size_t GetHash() const {
138  return fml::HashCombine(name, location, set, binding, type, bit_width,
140  }
141 
142  constexpr bool operator==(const ShaderStageIOSlot& other) const {
143  return name == other.name && //
144  location == other.location && //
145  set == other.set && //
146  binding == other.binding && //
147  type == other.type && //
148  bit_width == other.bit_width && //
149  vec_size == other.vec_size && //
150  columns == other.columns && //
151  offset == other.offset && //
153  ;
154  }
155 };
156 
158  size_t stride;
159  size_t binding;
160 
161  constexpr size_t GetHash() const { return fml::HashCombine(stride, binding); }
162 
163  constexpr bool operator==(const ShaderStageBufferLayout& other) const {
164  return stride == other.stride && //
165  binding == other.binding;
166  }
167 };
168 
169 // These enum values were chosen to match the same values
170 // in the VK Descriptor Type enum.
171 enum class DescriptorType {
172  kSampler = 0,
173  kSampledImage = 1,
174  kImage = 2,
175  kUniformBuffer = 6,
176  kStorageBuffer = 7,
177  kInputAttachment = 10,
178 };
179 
181  uint32_t binding;
184 };
185 
186 template <size_t Size>
187 struct Padding {
188  private:
189  uint8_t pad_[Size];
190 };
191 
192 /// @brief Struct used for padding uniform buffer array elements.
193 template <typename T,
194  size_t Size,
195  class = std::enable_if_t<std::is_standard_layout_v<T>>>
196 struct Padded {
197  T value;
199 
200  Padded(T p_value) : value(p_value) {}; // NOLINT(google-explicit-constructor)
201 };
202 
203 inline constexpr Vector4 ToVector(Color color) {
204  return {color.red, color.green, color.blue, color.alpha};
205 }
206 
207 } // namespace impeller
208 
209 #endif // FLUTTER_IMPELLER_CORE_SHADER_TYPES_H_
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:203
TSize< Scalar > Size
Definition: size.h:159
Scalar blue
Definition: color.h:138
Scalar alpha
Definition: color.h:143
Scalar red
Definition: color.h:128
Scalar green
Definition: color.h:133
Struct used for padding uniform buffer array elements.
Definition: shader_types.h:196
Padded(T p_value)
Definition: shader_types.h:200
Padding< Size > _PADDING_
Definition: shader_types.h:198
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:111
size_t texture_index
ext_res_0 is the Metal binding value.
Definition: shader_types.h:116
size_t set
The Vulkan descriptor set index.
Definition: shader_types.h:119
const char * name
The name of the uniform slot.
Definition: shader_types.h:113
size_t binding
The Vulkan binding value.
Definition: shader_types.h:122
std::vector< ShaderStructMemberMetadata > members
Definition: shader_types.h:88
constexpr size_t GetHash() const
Definition: shader_types.h:161
constexpr bool operator==(const ShaderStageBufferLayout &other) const
Definition: shader_types.h:163
constexpr bool operator==(const ShaderStageIOSlot &other) const
Definition: shader_types.h:142
constexpr size_t GetHash() const
Definition: shader_types.h:137
std::optional< ShaderFloatType > float_type
Definition: shader_types.h:82
std::optional< size_t > array_elements
Definition: shader_types.h:81
Metadata required to bind a buffer.
Definition: shader_types.h:94
size_t binding
The Vulkan binding value.
Definition: shader_types.h:105
size_t ext_res_0
ext_res_0 is the Metal binding value.
Definition: shader_types.h:99
size_t set
The Vulkan descriptor set index.
Definition: shader_types.h:102
const char * name
The name of the uniform slot.
Definition: shader_types.h:96