Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
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
15#include "flutter/fml/logging.h"
19
20namespace impeller {
21
22enum class ShaderStage {
24 kVertex,
27};
28
40
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.
65enum 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;
81 std::optional<size_t> array_elements;
82 std::optional<ShaderFloatType> float_type;
83};
84
85/// @brief Derive the `ShaderFloatType` from the base `ShaderType` and
86/// the (vec_size, columns) dimensions reported by SPIR-V Cross.
87///
88/// `vec_size` is the component count of a single column (the vector length
89/// for non-matrix types, the row count for matrices). `columns` is 1 for
90/// vectors and N for an NxN matrix. Returns `std::nullopt` for non-float
91/// types and for shapes that don't map to a `ShaderFloatType`.
92constexpr std::optional<ShaderFloatType> DeriveShaderFloatType(ShaderType type,
93 size_t vec_size,
94 size_t columns) {
95 if (type != ShaderType::kFloat) {
96 return std::nullopt;
97 }
98 if (columns == 1) {
99 switch (vec_size) {
100 case 1:
102 case 2:
104 case 3:
106 case 4:
108 default:
109 return std::nullopt;
110 }
111 }
112 if (vec_size == columns) {
113 switch (vec_size) {
114 case 2:
116 case 3:
118 case 4:
120 default:
121 return std::nullopt;
122 }
123 }
124 return std::nullopt;
125}
126
128 // This must match the uniform name in the shader program.
129 std::string name;
130 std::vector<ShaderStructMemberMetadata> members;
131};
132
133/// @brief Metadata required to bind a buffer.
134///
135/// OpenGL binding requires the usage of the separate shader metadata struct.
137 /// @brief The name of the uniform slot.
138 const char* name;
139
140 /// @brief `ext_res_0` is the Metal binding value.
141 size_t ext_res_0;
142
143 /// @brief The Vulkan descriptor set index.
144 size_t set;
145
146 /// @brief The Vulkan binding value.
147 size_t binding;
148};
149
150/// @brief Metadata required to bind a combined texture and sampler.
151///
152/// OpenGL binding requires the usage of the separate shader metadata struct.
154 /// @brief The name of the uniform slot.
155 const char* name;
156
157 /// @brief `ext_res_0` is the Metal binding value.
159
160 /// @brief The Vulkan descriptor set index.
161 size_t set;
162
163 /// @brief The Vulkan binding value.
164 size_t binding;
165};
166
168 const char* name;
169 size_t location;
170 size_t set;
171 size_t binding;
173 size_t bit_width;
174 size_t vec_size;
175 size_t columns;
176 size_t offset;
178
179 constexpr size_t GetHash() const {
182 }
183
184 constexpr bool operator==(const ShaderStageIOSlot& other) const {
185 return name == other.name && //
186 location == other.location && //
187 set == other.set && //
188 binding == other.binding && //
189 type == other.type && //
190 bit_width == other.bit_width && //
191 vec_size == other.vec_size && //
192 columns == other.columns && //
193 offset == other.offset && //
195 ;
196 }
197};
198
200 size_t stride;
201 size_t binding;
202
203 constexpr size_t GetHash() const { return fml::HashCombine(stride, binding); }
204
205 constexpr bool operator==(const ShaderStageBufferLayout& other) const {
206 return stride == other.stride && //
207 binding == other.binding;
208 }
209};
210
211// These enum values were chosen to match the same values
212// in the VK Descriptor Type enum.
213enum class DescriptorType {
214 kSampler = 0,
215 kSampledImage = 1,
216 kImage = 2,
217 kUniformBuffer = 6,
218 kStorageBuffer = 7,
219 kInputAttachment = 10,
220};
221
227
228template <size_t Size>
229struct Padding {
230 private:
231 uint8_t pad_[Size];
232};
233
234/// @brief Struct used for padding uniform buffer array elements.
235template <typename T,
236 size_t Size,
237 class = std::enable_if_t<std::is_standard_layout_v<T>>>
238struct Padded {
241
242 Padded(T p_value) : value(p_value) {}; // NOLINT(google-explicit-constructor)
243};
244
245inline constexpr Vector4 ToVector(Color color) {
246 return {color.red, color.green, color.blue, color.alpha};
247}
248
249} // namespace impeller
250
251#endif // FLUTTER_IMPELLER_CORE_SHADER_TYPES_H_
uint32_t vec_size
uint32_t columns
#define FML_UNREACHABLE()
Definition logging.h:128
constexpr std::size_t HashCombine()
constexpr std::optional< ShaderFloatType > DeriveShaderFloatType(ShaderType type, size_t vec_size, size_t columns)
Derive the ShaderFloatType from the base ShaderType and the (vec_size, columns) dimensions reported b...
constexpr ShaderStage ToShaderStage(RuntimeShaderStage stage)
constexpr Vector4 ToVector(Color color)
TSize< Scalar > Size
Definition size.h:159
impeller::ShaderType type
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.
Padded(T p_value)
Padding< Size > _PADDING_
Metadata required to bind a combined texture and sampler.
size_t texture_index
ext_res_0 is the Metal binding value.
size_t set
The Vulkan descriptor set index.
const char * name
The name of the uniform slot.
size_t binding
The Vulkan binding value.
std::vector< ShaderStructMemberMetadata > members
constexpr size_t GetHash() const
constexpr bool operator==(const ShaderStageBufferLayout &other) const
constexpr bool operator==(const ShaderStageIOSlot &other) const
constexpr size_t GetHash() const
std::optional< ShaderFloatType > float_type
std::optional< size_t > array_elements
Metadata required to bind a buffer.
size_t binding
The Vulkan binding value.
size_t ext_res_0
ext_res_0 is the Metal binding value.
size_t set
The Vulkan descriptor set index.
const char * name
The name of the uniform slot.