| 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"
16 #include "impeller/core/runtime_types.h"
17 #include "impeller/geometry/half.h"
18 #include "impeller/geometry/matrix.h"
19
20 namespace impeller {
21
22 enum class ShaderStage {
23 kUnknown,
24 kVertex,
25 kFragment,
26 kCompute,
27 };
28
29 constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage) {
30 switch (stage) {
31 case RuntimeShaderStage::kVertex:
32 return ShaderStage::kVertex;
33 case RuntimeShaderStage::kFragment:
34 return ShaderStage::kFragment;
35 case RuntimeShaderStage::kCompute:
36 return ShaderStage::kCompute;
37 }
38 FML_UNREACHABLE();
39 }
40
41 enum class ShaderType {
42 kUnknown,
43 kVoid,
44 kBoolean,
45 kSignedByte,
46 kUnsignedByte,
47 kSignedShort,
48 kUnsignedShort,
49 kSignedInt,
50 kUnsignedInt,
51 kSignedInt64,
52 kUnsignedInt64,
53 kAtomicCounter,
54 kHalfFloat,
55 kFloat,
56 kDouble,
57 kStruct,
58 kImage,
59 kSampledImage,
60 kSampler,
61 };
62
63 struct ShaderStructMemberMetadata {
64 ShaderType type;
65 std::string name;
66 size_t offset;
67 size_t size;
68 size_t byte_length;
69 std::optional<size_t> array_elements;
70 };
71
72 struct ShaderMetadata {
73 // This must match the uniform name in the shader program.
74 std::string name;
75 std::vector<ShaderStructMemberMetadata> members;
76 };
77
78 /// @brief Metadata required to bind a buffer.
79 ///
80 /// OpenGL binding requires the usage of the separate shader metadata struct.
81 struct ShaderUniformSlot {
82 /// @brief The name of the uniform slot.
83 const char* name;
84
85 /// @brief `ext_res_0` is the Metal binding value.
86 size_t ext_res_0;
87
88 /// @brief The Vulkan descriptor set index.
89 size_t set;
90
91 /// @brief The Vulkan binding value.
92 size_t binding;
93 };
94
95 /// @brief Metadata required to bind a combined texture and sampler.
96 ///
97 /// OpenGL binding requires the usage of the separate shader metadata struct.
98 struct SampledImageSlot {
99 /// @brief The name of the uniform slot.
100 const char* name;
101
102 /// @brief `ext_res_0` is the Metal binding value.
103 size_t texture_index;
104
105 /// @brief The Vulkan descriptor set index.
106 size_t set;
107
108 /// @brief The Vulkan binding value.
109 size_t binding;
110 };
111
112 struct ShaderStageIOSlot {
113 const char* name;
114 size_t location;
115 size_t set;
116 size_t binding;
117 ShaderType type;
118 size_t bit_width;
119 size_t vec_size;
120 size_t columns;
121 size_t offset;
122 bool relaxed_precision;
123
124 constexpr size_t GetHash() const {
125 return fml::HashCombine(name, location, set, binding, type, bit_width,
126 vec_size, columns, offset, relaxed_precision);
127 }
128
129 constexpr bool operator==(const ShaderStageIOSlot& other) const {
130 return name == other.name && //
131 location == other.location && //
132 set == other.set && //
133 binding == other.binding && //
134 type == other.type && //
135 bit_width == other.bit_width && //
136 vec_size == other.vec_size && //
137 columns == other.columns && //
138 offset == other.offset && //
139 relaxed_precision == other.relaxed_precision //
140 ;
141 }
142 };
143
144 struct ShaderStageBufferLayout {
145 size_t stride;
146 size_t binding;
147
148 constexpr size_t GetHash() const { return fml::HashCombine(stride, binding); }
149
150 constexpr bool operator==(const ShaderStageBufferLayout& other) const {
151 return stride == other.stride && //
152 binding == other.binding;
153 }
154 };
155
156 // These enum values were chosen to match the same values
157 // in the VK Descriptor Type enum.
158 enum class DescriptorType {
159 kSampler = 0,
160 kSampledImage = 1,
161 kImage = 2,
162 kUniformBuffer = 6,
163 kStorageBuffer = 7,
164 kInputAttachment = 10,
165 };
166
167 struct DescriptorSetLayout {
168 uint32_t binding;
169 DescriptorType descriptor_type;
170 ShaderStage shader_stage;
171 };
172
173 template <size_t Size>
174 struct Padding {
175 private:
176 uint8_t pad_[Size];
177 };
178
179 /// @brief Struct used for padding uniform buffer array elements.
180 template <typename T,
181 size_t Size,
182 class = std::enable_if_t<std::is_standard_layout_v<T>>>
183 struct Padded {
184 T value;
185 Padding<Size> _PADDING_;
186
187 Padded(T p_value) : value(p_value) {}; // NOLINT(google-explicit-constructor)
188 };
189
190 inline constexpr Vector4 ToVector(Color color) {
191 return {color.red, color.green, color.blue, color.alpha};
192 }
193
194 } // namespace impeller
195
196 #endif // FLUTTER_IMPELLER_CORE_SHADER_TYPES_H_
half.h
matrix.h
impeller
Definition: allocation.cc:12
impeller::RuntimeShaderStage
RuntimeShaderStage
Definition: runtime_types.h:30
impeller::RuntimeShaderStage::kCompute
@ kCompute
impeller::RuntimeShaderStage::kFragment
@ kFragment
impeller::RuntimeShaderStage::kVertex
@ kVertex
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:190
impeller::ShaderStage
ShaderStage
Definition: shader_types.h:22
impeller::ShaderStage::kUnknown
@ kUnknown
impeller::ShaderStage::kCompute
@ kCompute
impeller::ShaderStage::kFragment
@ kFragment
impeller::ShaderStage::kVertex
@ kVertex
impeller::Size
TSize< Scalar > Size
Definition: size.h:159
impeller::ShaderType
ShaderType
Definition: shader_types.h:41
impeller::ShaderType::kSignedInt
@ kSignedInt
impeller::ShaderType::kSignedShort
@ kSignedShort
impeller::ShaderType::kUnknown
@ kUnknown
impeller::ShaderType::kSignedInt64
@ kSignedInt64
impeller::ShaderType::kUnsignedInt64
@ kUnsignedInt64
impeller::ShaderType::kStruct
@ kStruct
impeller::ShaderType::kHalfFloat
@ kHalfFloat
impeller::ShaderType::kImage
@ kImage
impeller::ShaderType::kUnsignedByte
@ kUnsignedByte
impeller::ShaderType::kBoolean
@ kBoolean
impeller::ShaderType::kSignedByte
@ kSignedByte
impeller::ShaderType::kDouble
@ kDouble
impeller::ShaderType::kFloat
@ kFloat
impeller::ShaderType::kUnsignedShort
@ kUnsignedShort
impeller::ShaderType::kAtomicCounter
@ kAtomicCounter
impeller::ShaderType::kSampler
@ kSampler
impeller::ShaderType::kVoid
@ kVoid
impeller::ShaderType::kUnsignedInt
@ kUnsignedInt
impeller::ShaderType::kSampledImage
@ kSampledImage
impeller::DescriptorType
DescriptorType
Definition: shader_types.h:158
impeller::DescriptorType::kImage
@ kImage
impeller::DescriptorType::kUniformBuffer
@ kUniformBuffer
impeller::DescriptorType::kSampler
@ kSampler
impeller::DescriptorType::kInputAttachment
@ kInputAttachment
impeller::DescriptorType::kSampledImage
@ kSampledImage
impeller::DescriptorType::kStorageBuffer
@ kStorageBuffer
runtime_types.h
impeller::Color
Definition: color.h:124
impeller::Color::blue
Scalar blue
Definition: color.h:138
impeller::Color::alpha
Scalar alpha
Definition: color.h:143
impeller::Color::red
Scalar red
Definition: color.h:128
impeller::Color::green
Scalar green
Definition: color.h:133
impeller::DescriptorSetLayout
Definition: shader_types.h:167
impeller::DescriptorSetLayout::descriptor_type
DescriptorType descriptor_type
Definition: shader_types.h:169
impeller::DescriptorSetLayout::shader_stage
ShaderStage shader_stage
Definition: shader_types.h:170
impeller::DescriptorSetLayout::binding
uint32_t binding
Definition: shader_types.h:168
impeller::Padded
Struct used for padding uniform buffer array elements.
Definition: shader_types.h:183
impeller::Padded::value
T value
Definition: shader_types.h:184
impeller::Padded::Padded
Padded(T p_value)
Definition: shader_types.h:187
impeller::Padded::_PADDING_
Padding< Size > _PADDING_
Definition: shader_types.h:185
impeller::Padding
Definition: shader_types.h:174
impeller::SampledImageSlot
Metadata required to bind a combined texture and sampler.
Definition: shader_types.h:98
impeller::SampledImageSlot::texture_index
size_t texture_index
ext_res_0 is the Metal binding value.
Definition: shader_types.h:103
impeller::SampledImageSlot::set
size_t set
The Vulkan descriptor set index.
Definition: shader_types.h:106
impeller::SampledImageSlot::name
const char * name
The name of the uniform slot.
Definition: shader_types.h:100
impeller::SampledImageSlot::binding
size_t binding
The Vulkan binding value.
Definition: shader_types.h:109
impeller::ShaderMetadata
Definition: shader_types.h:72
impeller::ShaderMetadata::name
std::string name
Definition: shader_types.h:74
impeller::ShaderMetadata::members
std::vector< ShaderStructMemberMetadata > members
Definition: shader_types.h:75
impeller::ShaderStageBufferLayout
Definition: shader_types.h:144
impeller::ShaderStageBufferLayout::GetHash
constexpr size_t GetHash() const
Definition: shader_types.h:148
impeller::ShaderStageBufferLayout::operator==
constexpr bool operator==(const ShaderStageBufferLayout &other) const
Definition: shader_types.h:150
impeller::ShaderStageBufferLayout::stride
size_t stride
Definition: shader_types.h:145
impeller::ShaderStageBufferLayout::binding
size_t binding
Definition: shader_types.h:146
impeller::ShaderStageIOSlot
Definition: shader_types.h:112
impeller::ShaderStageIOSlot::set
size_t set
Definition: shader_types.h:115
impeller::ShaderStageIOSlot::bit_width
size_t bit_width
Definition: shader_types.h:118
impeller::ShaderStageIOSlot::operator==
constexpr bool operator==(const ShaderStageIOSlot &other) const
Definition: shader_types.h:129
impeller::ShaderStageIOSlot::relaxed_precision
bool relaxed_precision
Definition: shader_types.h:122
impeller::ShaderStageIOSlot::type
ShaderType type
Definition: shader_types.h:117
impeller::ShaderStageIOSlot::GetHash
constexpr size_t GetHash() const
Definition: shader_types.h:124
impeller::ShaderStageIOSlot::name
const char * name
Definition: shader_types.h:113
impeller::ShaderStageIOSlot::vec_size
size_t vec_size
Definition: shader_types.h:119
impeller::ShaderStageIOSlot::columns
size_t columns
Definition: shader_types.h:120
impeller::ShaderStageIOSlot::offset
size_t offset
Definition: shader_types.h:121
impeller::ShaderStageIOSlot::location
size_t location
Definition: shader_types.h:114
impeller::ShaderStageIOSlot::binding
size_t binding
Definition: shader_types.h:116
impeller::ShaderStructMemberMetadata
Definition: shader_types.h:63
impeller::ShaderStructMemberMetadata::size
size_t size
Definition: shader_types.h:67
impeller::ShaderStructMemberMetadata::byte_length
size_t byte_length
Definition: shader_types.h:68
impeller::ShaderStructMemberMetadata::offset
size_t offset
Definition: shader_types.h:66
impeller::ShaderStructMemberMetadata::type
ShaderType type
Definition: shader_types.h:64
impeller::ShaderStructMemberMetadata::name
std::string name
Definition: shader_types.h:65
impeller::ShaderStructMemberMetadata::array_elements
std::optional< size_t > array_elements
Definition: shader_types.h:69
impeller::ShaderUniformSlot
Metadata required to bind a buffer.
Definition: shader_types.h:81
impeller::ShaderUniformSlot::binding
size_t binding
The Vulkan binding value.
Definition: shader_types.h:92
impeller::ShaderUniformSlot::ext_res_0
size_t ext_res_0
ext_res_0 is the Metal binding value.
Definition: shader_types.h:86
impeller::ShaderUniformSlot::set
size_t set
The Vulkan descriptor set index.
Definition: shader_types.h:89
impeller::ShaderUniformSlot::name
const char * name
The name of the uniform slot.
Definition: shader_types.h:83
impeller::Vector4
Definition: vector.h:232