Blender V5.3
GPU_vertex_format.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 by Mike Erwin. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#pragma once
12
13#include "BLI_assert.hh"
16#include "BLI_string_ref.hh"
17#include "BLI_sys_types.hh"
18
19#include "GPU_format.hh"
20
21namespace blender {
22
23namespace gpu {
24
25enum class VertAttrType : uint8_t {
27
28#define DECLARE(a, b, c, blender_enum, d, e, f, g, h) blender_enum = int(DataFormat::blender_enum),
29
30#define GPU_VERTEX_FORMAT_EXPAND(impl) \
31 SNORM_8_8_8_8_(impl) \
32\
33 SNORM_16_16_(impl) \
34 SNORM_16_16_16_16_(impl) \
35\
36 UNORM_8_8_8_8_(impl) \
37\
38 UNORM_16_16_(impl) \
39 UNORM_16_16_16_16_(impl) \
40\
41 SINT_8_8_8_8_(impl) \
42\
43 SINT_16_16_(impl) \
44 SINT_16_16_16_16_(impl) \
45\
46 SINT_32_(impl) \
47 SINT_32_32_(impl) \
48 SINT_32_32_32_(impl) \
49 SINT_32_32_32_32_(impl) \
50\
51 UINT_8_8_8_8_(impl) \
52\
53 UINT_16_16_(impl) \
54 UINT_16_16_16_16_(impl) \
55\
56 UINT_32_(impl) \
57 UINT_32_32_(impl) \
58 UINT_32_32_32_(impl) \
59 UINT_32_32_32_32_(impl) \
60\
61 SFLOAT_16_16_(impl) \
62 SFLOAT_16_16_16_16_(impl) \
63\
64 SFLOAT_32_(impl) \
65 SFLOAT_32_32_(impl) \
66 SFLOAT_32_32_32_(impl) \
67 SFLOAT_32_32_32_32_(impl) \
68\
69 SNORM_10_10_10_2_(impl) \
70 UNORM_10_10_10_2_(impl) \
71\
72 /* UFLOAT_11_11_10_(impl) Available on Metal (and maybe VK) but not on GL. */ \
73 /* UFLOAT_9_9_9_EXP_5_(impl) Available on Metal (and maybe VK) but not on GL. */
74
76#undef DECLARE
77
78#define DECLARE(a, b, c, blender_enum, d, e, f, g, h) \
79 blender_enum##_DEPRECATED = int(DataFormat::blender_enum),
80
81/* Deprecated formats. To be removed in 5.0. Needed for python shaders. */
82#define GPU_VERTEX_DEPRECATED_FORMAT_EXPAND(impl) \
83 SNORM_8_(impl) \
84 SNORM_8_8_(impl) \
85 SNORM_8_8_8_(impl) \
86 SNORM_16_(impl) \
87 SNORM_16_16_16_(impl) \
88 UNORM_8_(impl) \
89 UNORM_8_8_(impl) \
90 UNORM_8_8_8_(impl) \
91 UNORM_16_(impl) \
92 UNORM_16_16_16_(impl) \
93 SINT_8_(impl) \
94 SINT_8_8_(impl) \
95 SINT_8_8_8_(impl) \
96 SINT_16_(impl) \
97 SINT_16_16_16_(impl) \
98 UINT_8_(impl) \
99 UINT_8_8_(impl) \
100 UINT_8_8_8_(impl) \
101 UINT_16_(impl) \
102 UINT_16_16_16_(impl)
103
105
106#undef DECLARE
107};
108
109/* TODO: Should reuse GPU_VERTEX_FORMAT_EXPAND, but we need to have `s/unorm` types first. */
110#define GPU_VERTEX_FORMAT_EXPAND_TYPED(impl) \
111 SINT_8_8_8_8_(impl) \
112\
113 SINT_16_16_(impl) \
114 SINT_16_16_16_16_(impl) \
115\
116 SINT_32_(impl) \
117 SINT_32_32_(impl) \
118 SINT_32_32_32_(impl) \
119 SINT_32_32_32_32_(impl) \
120\
121 UINT_8_8_8_8_(impl) \
122\
123 UINT_16_16_(impl) \
124 UINT_16_16_16_16_(impl) \
125\
126 UINT_32_(impl) \
127 UINT_32_32_(impl) \
128 UINT_32_32_32_(impl) \
129 UINT_32_32_32_32_(impl) \
130\
131 SFLOAT_32_(impl) \
132 SFLOAT_32_32_(impl) \
133 SFLOAT_32_32_32_(impl) \
134 SFLOAT_32_32_32_32_(impl) \
135\
136 /* UFLOAT_11_11_10_(impl) Available on Metal (and maybe VK) but not on GL. */ \
137 /* UFLOAT_9_9_9_EXP_5_(impl) Available on Metal (and maybe VK) but not on GL. */
138
139/* Must be implemented for each type used in vertex format.
140 * Should contain the associated VertAttrType just like below. */
141template<typename T> struct AttrType {};
142
143#define ATTR_TYPE_MAPPING(_type, b, c, blender_enum, d, e, f, g, h) \
144 template<> struct AttrType<_type> { \
145 static constexpr VertAttrType type = VertAttrType::blender_enum; \
146 };
147
149
150#undef ATTR_TYPE_MAPPING
151
153{
154 return DataFormat(int(format));
155}
156
157class Shader;
158
159} // namespace gpu
160
161constexpr static int GPU_VERT_ATTR_MAX_LEN = 16;
162constexpr static int GPU_VERT_ATTR_MAX_NAMES = 6;
163constexpr static int GPU_VERT_ATTR_NAMES_BUF_LEN = 256;
164constexpr static int GPU_VERT_FORMAT_MAX_NAMES = 63; /* More than enough, actual max is ~30. */
165/* Computed as GPU_VERT_ATTR_NAMES_BUF_LEN / 30 (actual max format name). */
166constexpr static int GPU_MAX_SAFE_ATTR_NAME = 12;
167
183
187 GPU_FETCH_INT_TO_FLOAT_UNIT, /* 127 (ubyte) -> 0.5 (and so on for other int types) */
188 /* Warning! adjust GPUVertAttr if changing. */
189};
190
192 /* To replace fetch_mode, comp_type, comp_len, size. */
193 struct Type {
195
196 size_t size() const
197 {
199 };
200
201 int comp_len() const
202 {
204 }
205
209 /* from beginning of vertex, in bytes */
211 /* up to GPU_VERT_ATTR_MAX_NAMES */
214};
215
217 "We use uchar as index inside the name buffer "
218 "so GPU_VERT_ATTR_NAMES_BUF_LEN needs to be "
219 "smaller than GPUVertFormat->name_offset and "
220 "GPUVertAttr->names maximum value");
221
242
243#define GPU_VERTEX_FORMAT_ADD_ATTR(attr) \
244 format.attribute_add(#attr, gpu::AttrType<decltype(attr)>::type, offsetof(VertT, attr)); \
245 BLI_STATIC_ASSERT(offsetof(VertT, attr) < 255, #attr " has offset greater than 255") \
246 BLI_STATIC_ASSERT(offsetof(VertT, attr) % 4 == 0, #attr " is not aligned to 4 bytes")
247
248#define _ATTR_EXPAND1(a) GPU_VERTEX_FORMAT_ADD_ATTR(a)
249#define _ATTR_EXPAND2(a, b) \
250 _ATTR_EXPAND1(a) \
251 GPU_VERTEX_FORMAT_ADD_ATTR(b)
252#define _ATTR_EXPAND3(a, b, c) \
253 _ATTR_EXPAND2(a, b) \
254 GPU_VERTEX_FORMAT_ADD_ATTR(c)
255#define _ATTR_EXPAND4(a, b, c, d) \
256 _ATTR_EXPAND3(a, b, c) \
257 GPU_VERTEX_FORMAT_ADD_ATTR(d)
258#define _ATTR_EXPAND5(a, b, c, d, e) \
259 _ATTR_EXPAND4(a, b, c, d) \
260 GPU_VERTEX_FORMAT_ADD_ATTR(e)
261#define _ATTR_EXPAND6(a, b, c, d, e, f) \
262 _ATTR_EXPAND5(a, b, c, d, e) \
263 GPU_VERTEX_FORMAT_ADD_ATTR(f)
264#define _ATTR_EXPAND7(a, b, c, d, e, f, g) \
265 _ATTR_EXPAND6(a, b, c, d, e, f) \
266 GPU_VERTEX_FORMAT_ADD_ATTR(g)
267#define _ATTR_EXPAND8(a, b, c, d, e, f, g, h) \
268 _ATTR_EXPAND7(a, b, c, d, e, f, g) \
269 GPU_VERTEX_FORMAT_ADD_ATTR(h)
270#define _ATTR_EXPAND9(a, b, c, d, e, f, g, h, i) \
271 _ATTR_EXPAND8(a, b, c, d, e, f, g, h) \
272 GPU_VERTEX_FORMAT_ADD_ATTR(i)
273#define _ATTR_EXPAND10(a, b, c, d, e, f, g, h, i, j) \
274 _ATTR_EXPAND9(a, b, c, d, e, f, g, h, i) \
275 GPU_VERTEX_FORMAT_ADD_ATTR(j)
276#define _ATTR_EXPAND11(a, b, c, d, e, f, g, h, i, j, k) \
277 _ATTR_EXPAND10(a, b, c, d, e, f, g, h, i, j) \
278 GPU_VERTEX_FORMAT_ADD_ATTR(k)
279#define _ATTR_EXPAND12(a, b, c, d, e, f, g, h, i, j, k, l) \
280 _ATTR_EXPAND11(a, b, c, d, e, f, g, h, i, j, k) \
281 GPU_VERTEX_FORMAT_ADD_ATTR(l)
282#define _ATTR_EXPAND13(a, b, c, d, e, f, g, h, i, j, k, l, m) \
283 _ATTR_EXPAND12(a, b, c, d, e, f, g, h, i, j, k, l) \
284 GPU_VERTEX_FORMAT_ADD_ATTR(m)
285#define _ATTR_EXPAND14(a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
286 _ATTR_EXPAND13(a, b, c, d, e, f, g, h, i, j, k, l, m) \
287 GPU_VERTEX_FORMAT_ADD_ATTR(n)
288#define _ATTR_EXPAND15(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
289 _ATTR_EXPAND14(a, b, c, d, e, f, g, h, i, j, k, l, m, n) \
290 GPU_VERTEX_FORMAT_ADD_ATTR(o)
291#define _ATTR_EXPAND16(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p) \
292 _ATTR_EXPAND15(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o) \
293 GPU_VERTEX_FORMAT_ADD_ATTR(p)
294/* We only support up to GPU_VERT_ATTR_MAX_LEN attribute per format. */
295
296#define GPU_VERTEX_FORMAT_ADD_ATTR_EXPAND(...) VA_NARGS_CALL_OVERLOAD(_ATTR_EXPAND, __VA_ARGS__)
297
298#define GPU_VERTEX_FORMAT_FUNC(_VertT, ...) \
299 static GPUVertFormat &format() \
300 { \
301 using VertT = _VertT; \
302 static GPUVertFormat format = {}; \
303 if (format.attr_len == 0) { \
304 GPU_VERTEX_FORMAT_ADD_ATTR_EXPAND(__VA_ARGS__) \
305 format.stride = sizeof(VertT); \
306 BLI_STATIC_ASSERT(sizeof(VertT) < 1024, "Vertex format is too big") \
307 format.packed = true; \
308 BLI_STATIC_ASSERT_ALIGN(VertT, 4) \
309 } \
310 return format; \
311 }
312
313namespace gpu {
314
320
321template<> struct GenericVertexFormat<int8_t> {
322 /* This is a workaround to reinterpret int8_t into padded vertex format to be able to upload it
323 * on any GPU. The shaders then need to read uint32_t and use shifts and mask to decode in
324 * individual bytes. */
327};
328
329template<> struct GenericVertexFormat<uint8_t> {
330 /* This is a workaround to reinterpret uint8_t into padded vertex format to be able to upload it
331 * on any GPU. The shaders then need to read uint32_t and use shifts and mask to decode in
332 * individual bytes. */
335};
336
337template<> struct GenericVertexFormat<bool> {
338 /* This is a workaround to reinterpret bool into padded vertex format to be able to upload it
339 * on any GPU. The shaders then need to read uint32_t and use shifts and mask to decode in
340 * individual bytes. */
343};
344
345} // namespace gpu
346
348void GPU_vertformat_copy(GPUVertFormat *dest, const GPUVertFormat &src);
350
352/* Legacy/unsafe version.
353 * TODO: Replace by vertex_format_combine. */
356
358
364
376void GPU_vertformat_multiload_enable(GPUVertFormat *format, int load_count);
377
393
395
397 const GPUVertAttr *attr,
398 uint n_idx)
399{
400 return format->names + attr->names[n_idx];
401}
402
407void GPU_vertformat_attr_rename(GPUVertFormat *format, int attr, const char *new_name);
408
413void GPU_vertformat_safe_attr_name(StringRef attr_name, char *r_safe_name, uint max_len);
414
415} // namespace blender
#define DECLARE(a, b, c, blender_enum, d, e, f, g, h)
#define ATTR_TYPE_MAPPING(_type, b, c, blender_enum, d, e, f, g, h)
#define GPU_VERTEX_DEPRECATED_FORMAT_EXPAND(impl)
#define GPU_VERTEX_FORMAT_EXPAND(impl)
#define GPU_VERTEX_FORMAT_EXPAND_TYPED(impl)
#define BLI_INLINE
#define T
int format_component_len(const DataFormat format)
int to_bytesize(const DataFormat format)
constexpr DataFormat to_data_format(TextureFormat format)
BMesh Iterators.
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
void GPU_vertformat_alias_add(GPUVertFormat *, StringRef alias)
unsigned int uint
Definition BKE_text.h:55
int GPU_vertformat_attr_id_get(const GPUVertFormat *, StringRef name)
static constexpr int GPU_MAX_SAFE_ATTR_NAME
static constexpr int GPU_VERT_FORMAT_MAX_NAMES
void GPU_vertformat_from_shader(GPUVertFormat *format, const gpu::Shader *shader)
BLI_INLINE const char * GPU_vertformat_attr_name_get(const GPUVertFormat *format, const GPUVertAttr *attr, uint n_idx)
void GPU_vertformat_deinterleave(GPUVertFormat *format)
GPUVertFormat GPU_vertformat_from_attribute(StringRef name, gpu::VertAttrType type)
static constexpr int GPU_VERT_ATTR_NAMES_BUF_LEN
@ GPU_FETCH_INT_TO_FLOAT_UNIT
unsigned int uint32_t
Definition BKE_text.h:81
void GPU_vertformat_safe_attr_name(StringRef attr_name, char *r_safe_name, uint max_len)
unsigned char uchar
Definition BKE_text.h:58
static constexpr int GPU_VERT_ATTR_MAX_NAMES
uint GPU_vertformat_attr_add_legacy(GPUVertFormat *, StringRef name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
unsigned char uint8_t
Definition BKE_text.h:79
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void GPU_vertformat_copy(GPUVertFormat *dest, const GPUVertFormat &src)
void GPU_vertformat_clear(GPUVertFormat *)
static constexpr int GPU_VERT_ATTR_MAX_LEN
void GPU_vertformat_multiload_enable(GPUVertFormat *format, int load_count)
void GPU_vertformat_attr_rename(GPUVertFormat *format, int attr, const char *new_name)
signed char int8_t
Definition BKE_text.h:76
@ Invalid
Definition types.hh:42
GPUVertFetchMode fetch_mode() const
GPUVertCompType comp_type() const
struct blender::GPUVertAttr::Type type
uchar names[GPU_VERT_ATTR_MAX_NAMES]
uint attribute_add(StringRef name, gpu::VertAttrType type, size_t offset=-1)
GPUVertAttr attrs[GPU_VERT_ATTR_MAX_LEN]
char names[GPU_VERT_ATTR_NAMES_BUF_LEN]
GPU_VERTEX_FORMAT_FUNC(GenericVertexFormat, attr)
GPU_VERTEX_FORMAT_FUNC(GenericVertexFormat, attr)
GPU_VERTEX_FORMAT_FUNC(GenericVertexFormat, attr)
GPU_VERTEX_FORMAT_FUNC(GenericVertexFormat, attr)