| 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 #pragma once
6
7 #include <cstddef>
8 #include <optional>
9 #include <string_view>
10 #include <vector>
11
12 #include "flutter/fml/hash_combine.h"
13 #include "flutter/fml/logging.h"
14 #include "impeller/core/runtime_types.h"
15 #include "impeller/geometry/half.h"
16 #include "impeller/geometry/matrix.h"
17
18 namespace impeller {
19
20 enum class ShaderStage {
21 kUnknown,
22 kVertex,
23 kFragment,
24 kTessellationControl,
25 kTessellationEvaluation,
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 case RuntimeShaderStage::kTessellationControl:
38 return ShaderStage::kTessellationControl;
39 case RuntimeShaderStage::kTessellationEvaluation:
40 return ShaderStage::kTessellationEvaluation;
41 }
42 FML_UNREACHABLE();
43 }
44
45 enum class ShaderType {
46 kUnknown,
47 kVoid,
48 kBoolean,
49 kSignedByte,
50 kUnsignedByte,
51 kSignedShort,
52 kUnsignedShort,
53 kSignedInt,
54 kUnsignedInt,
55 kSignedInt64,
56 kUnsignedInt64,
57 kAtomicCounter,
58 kHalfFloat,
59 kFloat,
60 kDouble,
61 kStruct,
62 kImage,
63 kSampledImage,
64 kSampler,
65 };
66
67 struct ShaderStructMemberMetadata {
68 ShaderType type;
69 std::string name;
70 size_t offset;
71 size_t size;
72 size_t byte_length;
73 std::optional<size_t> array_elements;
74 };
75
76 struct ShaderMetadata {
77 std::string name;
78 std::vector<ShaderStructMemberMetadata> members;
79 };
80
81 struct ShaderUniformSlot {
82 const char* name;
83 size_t ext_res_0;
84 size_t set;
85 size_t binding;
86 };
87
88 struct ShaderStageIOSlot {
89 const char* name;
90 size_t location;
91 size_t set;
92 size_t binding;
93 ShaderType type;
94 size_t bit_width;
95 size_t vec_size;
96 size_t columns;
97 size_t offset;
98
99 constexpr size_t GetHash() const {
100 return fml::HashCombine(name, location, set, binding, type, bit_width,
101 vec_size, columns, offset);
102 }
103
104 constexpr bool operator==(const ShaderStageIOSlot& other) const {
105 return name == other.name && //
106 location == other.location && //
107 set == other.set && //
108 binding == other.binding && //
109 type == other.type && //
110 bit_width == other.bit_width && //
111 vec_size == other.vec_size && //
112 columns == other.columns && //
113 offset == other.offset;
114 }
115 };
116
117 struct ShaderStageBufferLayout {
118 size_t stride;
119 size_t binding;
120
121 constexpr size_t GetHash() const { return fml::HashCombine(stride, binding); }
122
123 constexpr bool operator==(const ShaderStageBufferLayout& other) const {
124 return stride == other.stride && //
125 binding == other.binding;
126 }
127 };
128
129 struct SampledImageSlot {
130 const char* name;
131 size_t texture_index;
132 size_t sampler_index;
133 size_t binding;
134 size_t set;
135
136 constexpr bool HasTexture() const { return texture_index < 32u; }
137
138 constexpr bool HasSampler() const { return sampler_index < 32u; }
139 };
140
141 enum class DescriptorType {
142 kUniformBuffer,
143 kStorageBuffer,
144 kSampledImage,
145 kImage,
146 kSampler,
147 };
148
149 struct DescriptorSetLayout {
150 uint32_t binding;
151 DescriptorType descriptor_type;
152 ShaderStage shader_stage;
153 };
154
155 template <size_t Size>
156 struct Padding {
157 private:
158 uint8_t pad_[Size];
159 };
160
161 /// @brief Struct used for padding uniform buffer array elements.
162 template <typename T,
163 size_t Size,
164 class = std::enable_if_t<std::is_standard_layout_v<T>>>
165 struct Padded {
166 T value;
167 Padding<Size> _PADDING_;
168
169 Padded(T p_value) : value(p_value){}; // NOLINT(google-explicit-constructor)
170 };
171
172 inline constexpr Vector4 ToVector(Color color) {
173 return {color.red, color.green, color.blue, color.alpha};
174 }
175
176 } // namespace impeller
impeller::ShaderStageIOSlot::binding
size_t binding
Definition: shader_types.h:92
impeller::ShaderStructMemberMetadata
Definition: shader_types.h:67
impeller::ShaderStageIOSlot::name
const char * name
Definition: shader_types.h:89
impeller::ShaderStageIOSlot
Definition: shader_types.h:88
impeller::ShaderUniformSlot::ext_res_0
size_t ext_res_0
Definition: shader_types.h:83
impeller::SampledImageSlot::name
const char * name
Definition: shader_types.h:130
impeller::DescriptorSetLayout
Definition: shader_types.h:149
impeller::Padded
Struct used for padding uniform buffer array elements.
Definition: shader_types.h:165
impeller::ShaderUniformSlot
Definition: shader_types.h:81
impeller::ShaderType::kAtomicCounter
@ kAtomicCounter
impeller::ShaderStageIOSlot::columns
size_t columns
Definition: shader_types.h:96
impeller::RuntimeShaderStage
RuntimeShaderStage
Definition: runtime_types.h:29
impeller::ShaderType::kDouble
@ kDouble
impeller::ShaderStageIOSlot::offset
size_t offset
Definition: shader_types.h:97
impeller::Padded::_PADDING_
Padding< Size > _PADDING_
Definition: shader_types.h:167
impeller::ShaderStage::kUnknown
@ kUnknown
impeller::ShaderType
ShaderType
Definition: shader_types.h:45
impeller::Color
Definition: color.h:122
impeller::ShaderMetadata
Definition: shader_types.h:76
impeller::Vector4
Definition: vector.h:229
impeller::ShaderStageBufferLayout::binding
size_t binding
Definition: shader_types.h:119
impeller::Padded::value
T value
Definition: shader_types.h:166
impeller::ShaderType::kSignedInt64
@ kSignedInt64
impeller::ShaderStructMemberMetadata::byte_length
size_t byte_length
Definition: shader_types.h:72
impeller::ShaderType::kImage
@ kImage
impeller::Color::alpha
Scalar alpha
Definition: color.h:141
impeller::ShaderStage
ShaderStage
Definition: shader_types.h:20
impeller::DescriptorSetLayout::shader_stage
ShaderStage shader_stage
Definition: shader_types.h:152
impeller::Padded::Padded
Padded(T p_value)
Definition: shader_types.h:169
impeller::ShaderType::kVoid
@ kVoid
impeller::ShaderUniformSlot::name
const char * name
Definition: shader_types.h:82
impeller::DescriptorType::kImage
@ kImage
impeller::ShaderStageBufferLayout
Definition: shader_types.h:117
impeller::Size
TSize< Scalar > Size
Definition: size.h:135
impeller::ShaderType::kSignedByte
@ kSignedByte
impeller::ShaderMetadata::name
std::string name
Definition: shader_types.h:77
impeller::Color::green
Scalar green
Definition: color.h:131
impeller::ShaderType::kUnsignedShort
@ kUnsignedShort
impeller::ToShaderStage
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
Definition: shader_types.h:29
runtime_types.h
impeller::ShaderType::kUnsignedInt64
@ kUnsignedInt64
impeller::RuntimeShaderStage::kVertex
@ kVertex
impeller::ShaderStructMemberMetadata::offset
size_t offset
Definition: shader_types.h:70
impeller::ShaderType::kUnsignedByte
@ kUnsignedByte
impeller::ShaderType::kFloat
@ kFloat
matrix.h
impeller::ShaderUniformSlot::binding
size_t binding
Definition: shader_types.h:85
impeller::RuntimeShaderStage::kFragment
@ kFragment
impeller::ShaderStageIOSlot::bit_width
size_t bit_width
Definition: shader_types.h:94
impeller::Padding
Definition: shader_types.h:156
impeller::ShaderType::kBoolean
@ kBoolean
impeller::ShaderStageIOSlot::set
size_t set
Definition: shader_types.h:91
impeller::RuntimeShaderStage::kTessellationEvaluation
@ kTessellationEvaluation
impeller::ShaderType::kSampler
@ kSampler
impeller::ShaderStructMemberMetadata::size
size_t size
Definition: shader_types.h:71
impeller::ShaderStructMemberMetadata::array_elements
std::optional< size_t > array_elements
Definition: shader_types.h:73
impeller::DescriptorType::kSampler
@ kSampler
impeller::SampledImageSlot
Definition: shader_types.h:129
impeller::Color::red
Scalar red
Definition: color.h:126
impeller::ShaderType::kSampledImage
@ kSampledImage
impeller::ShaderStage::kTessellationEvaluation
@ kTessellationEvaluation
impeller::ShaderStage::kFragment
@ kFragment
impeller::ShaderStageIOSlot::GetHash
constexpr size_t GetHash() const
Definition: shader_types.h:99
impeller::DescriptorType::kSampledImage
@ kSampledImage
impeller::ShaderStructMemberMetadata::name
std::string name
Definition: shader_types.h:69
impeller::SampledImageSlot::HasSampler
constexpr bool HasSampler() const
Definition: shader_types.h:138
impeller::DescriptorType::kUniformBuffer
@ kUniformBuffer
impeller::ShaderStageIOSlot::location
size_t location
Definition: shader_types.h:90
impeller::ShaderType::kUnsignedInt
@ kUnsignedInt
impeller::ShaderStageIOSlot::operator==
constexpr bool operator==(const ShaderStageIOSlot &other) const
Definition: shader_types.h:104
impeller::ShaderType::kStruct
@ kStruct
half.h
impeller::ShaderType::kSignedShort
@ kSignedShort
impeller::ShaderType::kHalfFloat
@ kHalfFloat
impeller::ShaderType::kSignedInt
@ kSignedInt
impeller::ShaderStageIOSlot::type
ShaderType type
Definition: shader_types.h:93
impeller::SampledImageSlot::sampler_index
size_t sampler_index
Definition: shader_types.h:132
impeller::ShaderStage::kVertex
@ kVertex
impeller::ToVector
constexpr Vector4 ToVector(Color color)
Definition: shader_types.h:172
impeller::SampledImageSlot::set
size_t set
Definition: shader_types.h:134
impeller::SampledImageSlot::HasTexture
constexpr bool HasTexture() const
Definition: shader_types.h:136
impeller::RuntimeShaderStage::kCompute
@ kCompute
impeller::ShaderStageBufferLayout::GetHash
constexpr size_t GetHash() const
Definition: shader_types.h:121
impeller::ShaderUniformSlot::set
size_t set
Definition: shader_types.h:84
impeller::SampledImageSlot::texture_index
size_t texture_index
Definition: shader_types.h:131
impeller::ShaderStageBufferLayout::stride
size_t stride
Definition: shader_types.h:118
impeller::ShaderStage::kCompute
@ kCompute
impeller::ShaderStructMemberMetadata::type
ShaderType type
Definition: shader_types.h:68
impeller::ShaderStage::kTessellationControl
@ kTessellationControl
impeller::ShaderType::kUnknown
@ kUnknown
impeller::SampledImageSlot::binding
size_t binding
Definition: shader_types.h:133
impeller::Color::blue
Scalar blue
Definition: color.h:136
impeller::DescriptorSetLayout::descriptor_type
DescriptorType descriptor_type
Definition: shader_types.h:151
impeller::RuntimeShaderStage::kTessellationControl
@ kTessellationControl
impeller::ShaderMetadata::members
std::vector< ShaderStructMemberMetadata > members
Definition: shader_types.h:78
impeller::ShaderStageBufferLayout::operator==
constexpr bool operator==(const ShaderStageBufferLayout &other) const
Definition: shader_types.h:123
impeller
Definition: aiks_context.cc:10
impeller::ShaderStageIOSlot::vec_size
size_t vec_size
Definition: shader_types.h:95
impeller::DescriptorSetLayout::binding
uint32_t binding
Definition: shader_types.h:150
impeller::DescriptorType::kStorageBuffer
@ kStorageBuffer
impeller::DescriptorType
DescriptorType
Definition: shader_types.h:141