Blender V5.0
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_string_ref.hh"
12#include "BLI_vector.hh"
13
35
56
71
76
77namespace blender::gpu::shader {
78
79enum class Type : int8_t {
80 /* Types supported natively across all GPU back-ends. */
96 /* Additionally supported types to enable data optimization and native
97 * support in some GPU back-ends.
98 * NOTE: These types must be representable in all APIs. E.g. `VEC3_101010I2` is aliased as vec3
99 * in the GL back-end, as implicit type conversions from packed normal attribute data to vec3 is
100 * supported. UCHAR/CHAR types are natively supported in Metal and can be used to avoid
101 * additional data conversions for `GPU_COMP_U8` vertex attributes. */
119};
120
122{
123 switch (type) {
124 case Type::float_t:
125 case Type::uint_t:
126 case Type::int_t:
127 case Type::bool_t:
128 return 1;
129 case Type::float2_t:
130 case Type::uint2_t:
131 case Type::int2_t:
132 return 2;
133 case Type::float3_t:
134 case Type::uint3_t:
135 case Type::int3_t:
136 return 3;
137 case Type::float4_t:
138 case Type::uint4_t:
139 case Type::int4_t:
140 return 4;
141 case Type::float3x3_t:
142 return 9;
143 case Type::float4x4_t:
144 return 16;
145 /* Alias special types. */
146 case Type::uchar_t:
147 case Type::ushort_t:
148 case Type::char_t:
149 case Type::short_t:
150 return 1;
151 case Type::uchar2_t:
152 case Type::ushort2_t:
153 case Type::char2_t:
154 case Type::short2_t:
155 return 2;
156 case Type::uchar3_t:
157 case Type::ushort3_t:
158 case Type::char3_t:
159 case Type::short3_t:
160 return 3;
161 case Type::uchar4_t:
162 case Type::ushort4_t:
163 case Type::char4_t:
164 case Type::short4_t:
165 return 4;
167 return 3;
168 }
170 return -1;
171}
172
174 struct Value {
175 union {
176 uint32_t u;
178 float f;
179 };
180
181 bool operator==(const Value &other) const
182 {
183 return u == other.u;
184 }
185
187 {
188 return uint64_t(u);
189 }
190 };
191
195
197
199 {
200 this->value.u = value;
201 }
202
204 {
205 this->value.i = value;
206 }
207
209 {
210 this->value.f = value;
211 }
212
214 {
215 this->value.u = value ? 1 : 0;
216 }
217
219 {
220 return this->type == b.type && this->name == b.name && this->value == b.value;
221 }
222};
223
230 /* Current values set by `GPU_shader_constant_*()` call. The backend can choose to interpret
231 * that however it wants (i.e: bind another shader instead). */
233
234 void set_value(int index, uint32_t value)
235 {
236 BLI_assert_msg(types[index] == Type::uint_t, "Mismatch between interface and constant type");
237 values[index].u = value;
238 }
239
240 void set_value(int index, int value)
241 {
242 BLI_assert_msg(types[index] == Type::int_t, "Mismatch between interface and constant type");
243 values[index].i = value;
244 }
245
246 void set_value(int index, float value)
247 {
248 BLI_assert_msg(types[index] == Type::float_t, "Mismatch between interface and constant type");
249 values[index].f = value;
250 }
251
252 void set_value(int index, bool value)
253 {
254 BLI_assert_msg(types[index] == Type::bool_t, "Mismatch between interface and constant type");
255 values[index].u = value ? 1 : 0;
256 }
257
258 bool is_empty() const
259 {
260 return types.is_empty();
261 }
262};
263
267 /* Reusing value type. */
269
271 {
272 return this->type == b.type && this->name == b.name && this->value == b.value;
273 }
274};
275
276} // namespace blender::gpu::shader
#define BLI_assert_unreachable()
Definition BLI_assert.h:93
#define BLI_assert_msg(a, msg)
Definition BLI_assert.h:53
#define BLI_INLINE
GPUFrontFace
@ GPU_COUNTERCLOCKWISE
@ GPU_CLOCKWISE
@ GPU_LOADACTION_LOAD
@ GPU_LOADACTION_DONT_CARE
@ GPU_LOADACTION_CLEAR
@ GPU_STOREACTION_STORE
@ GPU_STOREACTION_DONT_CARE
GPUAttachmentState
@ GPU_ATTACHMENT_WRITE
@ GPU_ATTACHMENT_READ
@ GPU_ATTACHMENT_IGNORE
unsigned long long int uint64_t
BLI_INLINE int to_component_count(const Type &type)
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