Blender V5.3
GPU_common_types.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#pragma once
10
11#include "BLI_enum_flags.hh"
12#include "BLI_string_ref.hh"
13#include "BLI_vector.hh"
14
15namespace blender {
16
38
59
74
79
80namespace gpu {
81
89
90} // namespace gpu
91
92namespace gpu::shader {
93
94enum class Type : int8_t {
95 /* Types supported natively across all GPU back-ends. */
96 float_t = 0,
97 float2_t,
98 float3_t,
99 float4_t,
100 float3x3_t,
101 float4x4_t,
102 uint_t,
103 uint2_t,
104 uint3_t,
105 uint4_t,
106 int_t,
107 int2_t,
108 int3_t,
109 int4_t,
110 bool_t,
111 /* Additionally supported types to enable data optimization and native
112 * support in some GPU back-ends.
113 * NOTE: These types must be representable in all APIs. E.g. `VEC3_101010I2` is aliased as vec3
114 * in the GL back-end, as implicit type conversions from packed normal attribute data to vec3 is
115 * supported. UCHAR/CHAR types are natively supported in Metal and can be used to avoid
116 * additional data conversions for `GPU_COMP_U8` vertex attributes. */
118 uchar_t,
119 uchar2_t,
120 uchar3_t,
121 uchar4_t,
122 char_t,
123 char2_t,
124 char3_t,
125 char4_t,
126 ushort_t,
127 ushort2_t,
128 ushort3_t,
129 ushort4_t,
130 short_t,
131 short2_t,
132 short3_t,
133 short4_t
134};
135
137{
138 switch (type) {
139 case Type::float_t:
140 case Type::uint_t:
141 case Type::int_t:
142 case Type::bool_t:
143 return 1;
144 case Type::float2_t:
145 case Type::uint2_t:
146 case Type::int2_t:
147 return 2;
148 case Type::float3_t:
149 case Type::uint3_t:
150 case Type::int3_t:
151 return 3;
152 case Type::float4_t:
153 case Type::uint4_t:
154 case Type::int4_t:
155 return 4;
156 case Type::float3x3_t:
157 return 9;
158 case Type::float4x4_t:
159 return 16;
160 /* Alias special types. */
161 case Type::uchar_t:
162 case Type::ushort_t:
163 case Type::char_t:
164 case Type::short_t:
165 return 1;
166 case Type::uchar2_t:
167 case Type::ushort2_t:
168 case Type::char2_t:
169 case Type::short2_t:
170 return 2;
171 case Type::uchar3_t:
172 case Type::ushort3_t:
173 case Type::char3_t:
174 case Type::short3_t:
175 return 3;
176 case Type::uchar4_t:
177 case Type::ushort4_t:
178 case Type::char4_t:
179 case Type::short4_t:
180 return 4;
182 return 3;
183 }
185 return -1;
186}
187
189 struct Value {
190 union {
191 uint32_t u;
192 int32_t i;
193 float f;
194 };
195
196 bool operator==(const Value &other) const
197 {
198 return u == other.u;
199 }
200
202 {
203 return uint64_t(u);
204 }
205 };
206
210
212
214 {
215 this->value.u = value;
216 }
217
218 SpecializationConstant(const char *name, int value) : type(Type::int_t), name(name)
219 {
220 this->value.i = value;
221 }
222
223 SpecializationConstant(const char *name, float value) : type(Type::float_t), name(name)
224 {
225 this->value.f = value;
226 }
227
228 SpecializationConstant(const char *name, bool value) : type(Type::bool_t), name(name)
229 {
230 this->value.u = value ? 1 : 0;
231 }
232
234 {
235 return this->type == b.type && this->name == b.name && this->value == b.value;
236 }
237};
238
245 /* Current values set by `GPU_shader_constant_*()` call. The backend can choose to interpret
246 * that however it wants (i.e: bind another shader instead). */
248
249 void set_value(int index, uint32_t value)
250 {
251 BLI_assert_msg(types[index] == Type::uint_t, "Mismatch between interface and constant type");
252 values[index].u = value;
253 }
254
255 void set_value(int index, int value)
256 {
257 BLI_assert_msg(types[index] == Type::int_t, "Mismatch between interface and constant type");
258 values[index].i = value;
259 }
260
261 void set_value(int index, float value)
262 {
263 BLI_assert_msg(types[index] == Type::float_t, "Mismatch between interface and constant type");
264 values[index].f = value;
265 }
266
267 void set_value(int index, bool value)
268 {
269 BLI_assert_msg(types[index] == Type::bool_t, "Mismatch between interface and constant type");
270 values[index].u = value ? 1 : 0;
271 }
272
273 bool is_empty() const
274 {
275 return types.is_empty();
276 }
277};
278
282 /* Reusing value type. */
284
286 {
287 return this->type == b.type && this->name == b.name && this->value == b.value;
288 }
289};
290
291} // namespace gpu::shader
292} // namespace blender
#define BLI_assert_unreachable()
#define BLI_INLINE
#define BLI_assert_msg(a, msg)
BLI_INLINE int to_component_count(const Type &type)
ENUM_OPERATORS(ShaderStage)
BMesh Iterators.
unsigned __int64 uint64_t
Definition BKE_text.h:91
@ GPU_LOADACTION_DONT_CARE
@ GPU_STOREACTION_DONT_CARE
unsigned int uint32_t
Definition BKE_text.h:81
unsigned char uint8_t
Definition BKE_text.h:79
signed int int32_t
Definition BKE_text.h:78
signed char int8_t
Definition BKE_text.h:76
bool operator==(const CompilationConstant &b) const
SpecializationConstant(const char *name, int value)
SpecializationConstant(const char *name, float value)
bool operator==(const SpecializationConstant &b) const
SpecializationConstant(const char *name, uint32_t value)
SpecializationConstant(const char *name, bool value)
Vector< SpecializationConstant::Value, 8 > values