Blender V5.3
gpu_vertex_format.cc
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#include "GPU_vertex_format.hh"
12#include "BLI_assert.hh"
13#include "BLI_math_base_c.hh"
14#include "GPU_capabilities.hh"
15
17#include "gpu_shader_private.hh"
19
20#include <cstddef>
21#include <cstring>
22
23#include "BLI_hash_mm2a.hh"
24#include "BLI_string.hh"
25#include "BLI_utildefines.hh"
26
27namespace blender {
28
29#define PACK_DEBUG 0
30
31#if PACK_DEBUG
32# include <stdio.h>
33#endif
34
35namespace gpu {
36
37/* Used to combine legacy enums into new vertex attribute type. */
39 GPUVertFetchMode fetch_mode,
40 uint32_t component_len)
41{
42 switch (component_type) {
43 case GPU_COMP_I8: {
44 switch (fetch_mode) {
46 switch (component_len) {
47 case 1:
48 return VertAttrType::SNORM_8_DEPRECATED;
49 case 2:
50 return VertAttrType::SNORM_8_8_DEPRECATED;
51 case 3:
52 return VertAttrType::SNORM_8_8_8_DEPRECATED;
53 case 4:
54 return VertAttrType::SNORM_8_8_8_8;
55 }
56 break;
57 case GPU_FETCH_INT:
58 switch (component_len) {
59 case 1:
60 return VertAttrType::SINT_8_DEPRECATED;
61 case 2:
62 return VertAttrType::SINT_8_8_DEPRECATED;
63 case 3:
64 return VertAttrType::SINT_8_8_8_DEPRECATED;
65 case 4:
66 return VertAttrType::SINT_8_8_8_8;
67 }
68 break;
69 default:
70 break;
71 }
72 break;
73 }
74 case GPU_COMP_U8: {
75 switch (fetch_mode) {
77 switch (component_len) {
78 case 1:
79 return VertAttrType::UNORM_8_DEPRECATED;
80 case 2:
81 return VertAttrType::UNORM_8_8_DEPRECATED;
82 case 3:
83 return VertAttrType::UNORM_8_8_8_DEPRECATED;
84 case 4:
85 return VertAttrType::UNORM_8_8_8_8;
86 }
87 break;
88 case GPU_FETCH_INT:
89 switch (component_len) {
90 case 1:
91 return VertAttrType::UINT_8_DEPRECATED;
92 case 2:
93 return VertAttrType::UINT_8_8_DEPRECATED;
94 case 3:
95 return VertAttrType::UINT_8_8_8_DEPRECATED;
96 case 4:
97 return VertAttrType::UINT_8_8_8_8;
98 }
99 break;
100 default:
101 break;
102 }
103 break;
104 }
105 case GPU_COMP_I16: {
106 switch (fetch_mode) {
108 switch (component_len) {
109 case 1:
110 return VertAttrType::SNORM_16_DEPRECATED;
111 case 2:
112 return VertAttrType::SNORM_16_16;
113 case 3:
114 return VertAttrType::SNORM_16_16_16_DEPRECATED;
115 case 4:
116 return VertAttrType::SNORM_16_16_16_16;
117 }
118 break;
119 case GPU_FETCH_INT:
120 switch (component_len) {
121 case 1:
122 return VertAttrType::SINT_16_DEPRECATED;
123 case 2:
124 return VertAttrType::SINT_16_16;
125 case 3:
126 return VertAttrType::SINT_16_16_16_DEPRECATED;
127 case 4:
128 return VertAttrType::SINT_16_16_16_16;
129 }
130 break;
131 default:
132 break;
133 }
134 break;
135 }
136 case GPU_COMP_U16: {
137 switch (fetch_mode) {
139 switch (component_len) {
140 case 1:
141 return VertAttrType::UNORM_16_DEPRECATED;
142 case 2:
143 return VertAttrType::UNORM_16_16;
144 case 3:
145 return VertAttrType::UNORM_16_16_16_DEPRECATED;
146 case 4:
147 return VertAttrType::UNORM_16_16_16_16;
148 }
149 break;
150 case GPU_FETCH_INT:
151 switch (component_len) {
152 case 1:
153 return VertAttrType::UINT_16_DEPRECATED;
154 case 2:
155 return VertAttrType::UINT_16_16;
156 case 3:
157 return VertAttrType::UINT_16_16_16_DEPRECATED;
158 case 4:
159 return VertAttrType::UINT_16_16_16_16;
160 }
161 break;
162 default:
163 break;
164 }
165 break;
166 }
167 case GPU_COMP_I32: {
168 switch (fetch_mode) {
169 case GPU_FETCH_INT:
170 switch (component_len) {
171 case 1:
172 return VertAttrType::SINT_32;
173 case 2:
174 return VertAttrType::SINT_32_32;
175 case 3:
176 return VertAttrType::SINT_32_32_32;
177 case 4:
178 return VertAttrType::SINT_32_32_32_32;
179 }
180 break;
181 default:
182 break;
183 }
184 break;
185 }
186 case GPU_COMP_U32: {
187 switch (fetch_mode) {
188 case GPU_FETCH_INT:
189 switch (component_len) {
190 case 1:
191 return VertAttrType::UINT_32;
192 case 2:
193 return VertAttrType::UINT_32_32;
194 case 3:
195 return VertAttrType::UINT_32_32_32;
196 case 4:
197 return VertAttrType::UINT_32_32_32_32;
198 }
199 break;
200 default:
201 break;
202 }
203 break;
204 }
205 case GPU_COMP_F32: {
206 switch (fetch_mode) {
207 case GPU_FETCH_FLOAT:
208 switch (component_len) {
209 case 1:
210 return VertAttrType::SFLOAT_32;
211 case 2:
212 return VertAttrType::SFLOAT_32_32;
213 case 3:
214 return VertAttrType::SFLOAT_32_32_32;
215 case 4:
216 return VertAttrType::SFLOAT_32_32_32_32;
217 }
218 break;
219 default:
220 break;
221 }
222 break;
223 }
224 case GPU_COMP_I10: {
225 switch (fetch_mode) {
227 return VertAttrType::SNORM_10_10_10_2;
228 default:
229 break;
230 }
231 break;
232 }
233 case GPU_COMP_MAX:
234 break;
235 }
236
238};
239
241{
242 switch (attr_type) {
243 case VertAttrType::SNORM_8_8_8_8:
244 case VertAttrType::SNORM_16_16:
245 case VertAttrType::SNORM_16_16_16_16:
246 case VertAttrType::UNORM_8_8_8_8:
247 case VertAttrType::UNORM_16_16:
248 case VertAttrType::UNORM_16_16_16_16:
249 case VertAttrType::SNORM_10_10_10_2:
250 case VertAttrType::UNORM_10_10_10_2:
251 return true;
252 default:
253 return false;
254 }
255};
256
258{
259 switch (attr_type) {
260 case VertAttrType::SFLOAT_32:
261 case VertAttrType::SFLOAT_32_32:
262 case VertAttrType::SFLOAT_32_32_32:
263 case VertAttrType::SFLOAT_32_32_32_32:
264 return true;
265 default:
266 return false;
267 }
268};
269
270} // namespace gpu
271
272using namespace gpu;
273using namespace gpu::shader;
274
276{
277 if (is_fetch_float(this->format)) {
278 return GPU_FETCH_FLOAT;
279 }
280 if (is_fetch_normalized(this->format)) {
282 }
283 return GPU_FETCH_INT;
284}
285
287{
288 switch (this->format) {
289 case VertAttrType::SNORM_8_8_8_8:
290 case VertAttrType::SINT_8_8_8_8:
291 return GPU_COMP_I8;
292 case VertAttrType::SNORM_16_16:
293 case VertAttrType::SNORM_16_16_16_16:
294 case VertAttrType::SINT_16_16:
295 case VertAttrType::SINT_16_16_16_16:
296 return GPU_COMP_I16;
297 case VertAttrType::SINT_32:
298 case VertAttrType::SINT_32_32:
299 case VertAttrType::SINT_32_32_32:
300 case VertAttrType::SINT_32_32_32_32:
301 return GPU_COMP_I32;
302 case VertAttrType::UNORM_8_8_8_8:
303 case VertAttrType::UINT_8_8_8_8:
304 return GPU_COMP_U8;
305 case VertAttrType::UNORM_16_16:
306 case VertAttrType::UNORM_16_16_16_16:
307 case VertAttrType::UINT_16_16:
308 case VertAttrType::UINT_16_16_16_16:
309 return GPU_COMP_U16;
310 case VertAttrType::UINT_32:
311 case VertAttrType::UINT_32_32:
312 case VertAttrType::UINT_32_32_32:
313 case VertAttrType::UINT_32_32_32_32:
314 return GPU_COMP_U32;
315 case VertAttrType::SFLOAT_32:
316 case VertAttrType::SFLOAT_32_32:
317 case VertAttrType::SFLOAT_32_32_32:
318 case VertAttrType::SFLOAT_32_32_32_32:
319 return GPU_COMP_F32;
320 case VertAttrType::SNORM_10_10_10_2:
321 case VertAttrType::UNORM_10_10_10_2:
322 return GPU_COMP_I10;
323 default: /* TODO(fclem): This avoids warning caused by deprecated formats. */
324 case VertAttrType::Invalid:
325 break;
326 }
328 return GPU_COMP_I8;
329}
330
332{
333#ifndef NDEBUG
334 memset(format, 0, sizeof(GPUVertFormat));
335#else
336 format->attr_len = 0;
337 format->packed = false;
338 format->name_offset = 0;
339 format->name_len = 0;
340 format->deinterleaved = false;
341
342 for (uint i = 0; i < GPU_VERT_ATTR_MAX_LEN; i++) {
343 format->attrs[i].name_len = 0;
344 }
345#endif
346}
347
349{
350 /* copy regular struct fields */
351 memcpy(dest, &src, sizeof(GPUVertFormat));
352}
353
355{
356 BLI_assert(!this->packed);
357 VertexFormat_pack(this);
358}
359
360uint vertex_buffer_size(const GPUVertFormat *format, uint vertex_len)
361{
362 BLI_assert(format->packed && format->stride > 0);
363 return format->stride * vertex_len;
364}
365
366static uchar copy_attr_name(GPUVertFormat *format, const StringRef name)
367{
368 const uchar name_offset = format->name_offset;
369 /* Subtract one to make sure there's enough space for the last null terminator. */
370 const int64_t available = GPU_VERT_ATTR_NAMES_BUF_LEN - name_offset - 1;
371 const int64_t chars_to_copy = std::min(name.size(), available);
372
373 name.substr(0, available).copy_unsafe(format->names + name_offset);
374 BLI_assert((format->name_offset + chars_to_copy + 1) <= GPU_VERT_ATTR_NAMES_BUF_LEN);
375 format->name_offset += chars_to_copy + 1;
376
377 return name_offset;
378}
379
381 const StringRef name,
382 GPUVertCompType comp_type,
383 uint comp_len,
384 GPUVertFetchMode fetch_mode)
385{
386 return format->attribute_add(name, vertex_format_combine(comp_type, fetch_mode, comp_len));
387}
388
390 const StringRef name,
392{
393 return format->attribute_add(name, type);
394}
395
397{
398 BLI_assert(this->name_len < GPU_VERT_FORMAT_MAX_NAMES); /* there's room for more */
399 BLI_assert(this->attr_len < GPU_VERT_ATTR_MAX_LEN); /* there's room for more */
400 BLI_assert(!this->packed); /* packed means frozen/locked */
402
403 this->name_len++; /* Multi-name support. */
404
405 const uint attr_id = this->attr_len++;
406 GPUVertAttr *attr = &this->attrs[attr_id];
407 attr->names[attr->name_len++] = copy_attr_name(this, name);
408 if (offset != -1) {
409 attr->offset = offset; /* Offset computed externally. */
410 }
411 else {
412 attr->offset = 0; /* offsets & stride are calculated later (during pack) */
413 }
414 attr->type.format = type;
415 return attr_id;
416}
417
419{
420 GPUVertAttr *attr = &format->attrs[format->attr_len - 1];
421 BLI_assert(format->name_len < GPU_VERT_FORMAT_MAX_NAMES); /* there's room for more */
423 format->name_len++; /* Multi-name support. */
424 attr->names[attr->name_len++] = copy_attr_name(format, alias);
425}
426
428{
429 GPUVertFormat format{};
430 format.attribute_add(name, type);
431 return format;
432}
433
435{
436 /* Sanity check. Maximum can be upgraded if needed. */
437 BLI_assert(load_count > 1 && load_count < 5);
438 /* We need a packed format because of format->stride. */
439 if (!format->packed) {
440 VertexFormat_pack(format);
441 }
442
443 BLI_assert((format->name_len + 1) * load_count < GPU_VERT_FORMAT_MAX_NAMES);
444 BLI_assert(format->attr_len * load_count <= GPU_VERT_ATTR_MAX_LEN);
445 BLI_assert(format->name_offset * load_count < GPU_VERT_ATTR_NAMES_BUF_LEN);
446
447 const GPUVertAttr *attr = format->attrs;
448 int attr_len = format->attr_len;
449 for (int i = 0; i < attr_len; i++, attr++) {
450 const char *attr_name = GPU_vertformat_attr_name_get(format, attr, 0);
451 for (int j = 1; j < load_count; j++) {
452 char load_name[/*MAX_CUSTOMDATA_LAYER_NAME*/ 68];
453 SNPRINTF(load_name, "%s%d", attr_name, j);
454 GPUVertAttr *dst_attr = &format->attrs[format->attr_len++];
455 *dst_attr = *attr;
456
457 dst_attr->names[0] = copy_attr_name(format, load_name);
458 dst_attr->name_len = 1;
459 dst_attr->offset += format->stride * j;
460 }
461 }
462}
463
465{
466 for (int i = 0; i < format->attr_len; i++) {
467 const GPUVertAttr *attr = &format->attrs[i];
468 for (int j = 0; j < attr->name_len; j++) {
469 const char *attr_name = GPU_vertformat_attr_name_get(format, attr, j);
470 if (name == attr_name) {
471 return i;
472 }
473 }
474 }
475 return -1;
476}
477
478void GPU_vertformat_attr_rename(GPUVertFormat *format, int attr_id, const char *new_name)
479{
480 BLI_assert(attr_id > -1 && attr_id < format->attr_len);
481 GPUVertAttr *attr = &format->attrs[attr_id];
482 char *attr_name = const_cast<char *>(GPU_vertformat_attr_name_get(format, attr, 0));
483 BLI_assert(strlen(attr_name) == strlen(new_name));
484 int i = 0;
485 while (attr_name[i] != '\0') {
486 attr_name[i] = new_name[i];
487 i++;
488 }
489 attr->name_len = 1;
490}
491
492/* Encode 8 original bytes into 11 safe bytes. */
493static void safe_bytes(char out[11], const char data[8])
494{
495 const char safe_chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
496
497 uint64_t in = *reinterpret_cast<uint64_t *>(const_cast<char *>(data));
498 for (int i = 0; i < 11; i++) {
499 out[i] = safe_chars[in % 62lu];
500 in /= 62lu;
501 }
502}
503
504void GPU_vertformat_safe_attr_name(const StringRef attr_name, char *r_safe_name, uint /*max_len*/)
505{
506 char data[8] = {0};
507 uint len = attr_name.size();
508
509 if (len > 8) {
510 /* Start with the first 4 chars of the name. */
511 memcpy(data, attr_name.data(), 4);
512 /* We use a hash to identify each data layer based on its name.
513 * NOTE: This is still prone to hash collision but the risks are very low. */
514 /* Start hashing after the first 2 chars. */
515 const StringRef to_hash = attr_name.drop_prefix(4);
516 *reinterpret_cast<uint *>(&data[4]) = BLI_hash_mm2(
517 reinterpret_cast<const uchar *>(to_hash.data()), to_hash.size(), 0);
518 }
519 else {
520 /* Copy the whole name. Collision is barely possible
521 * (hash would have to be equal to the last 4 bytes). */
522 memcpy(data, attr_name.data(), std::min<int>(8, len));
523 }
524 /* Convert to safe bytes characters. */
525 safe_bytes(r_safe_name, data);
526 /* End the string */
527 r_safe_name[11] = '\0';
528
530#if 0 /* For debugging */
531 printf("%s > %lx > %s\n", attr_name, *(uint64_t *)data, r_safe_name);
532#endif
533}
534
536{
537 /* Ideally we should change the stride and offset here. This would allow
538 * us to use GPU_vertbuf_attr_set / GPU_vertbuf_attr_fill. But since
539 * we use only 11 bits for attr->offset this limits the size of the
540 * buffer considerably. So instead we do the conversion when creating
541 * bindings in create_bindings(). */
542 format->deinterleaved = true;
543}
544
546{
547 const uint mod = offset % alignment;
548 return (mod == 0) ? 0 : (alignment - mod);
549}
550
551#if PACK_DEBUG
552static void show_pack(uint a_idx, uint size, uint pad)
553{
554 const char c = 'A' + a_idx;
555 for (uint i = 0; i < pad; i++) {
556 putchar('-');
557 }
558 for (uint i = 0; i < size; i++) {
559 putchar(c);
560 }
561}
562#endif
563
565{
566 uint offset = 0;
567 for (uint a_idx = 0; a_idx < format->attr_len; a_idx++) {
568 GPUVertAttr *a = &format->attrs[a_idx];
569 a->offset = offset;
570 offset += a->type.size();
571 }
572 format->stride = offset;
573 format->packed = true;
574}
575
576static uint component_size_get(const Type gpu_type)
577{
578 switch (gpu_type) {
579 case Type::float2_t:
580 case Type::int2_t:
581 case Type::uint2_t:
582 return 2;
583 case Type::float3_t:
584 case Type::int3_t:
585 case Type::uint3_t:
586 return 3;
587 case Type::float4_t:
588 case Type::int4_t:
589 case Type::uint4_t:
590 return 4;
591 case Type::float3x3_t:
592 return 12;
593 case Type::float4x4_t:
594 return 16;
595 default:
596 return 1;
597 }
598}
599
601 GPUVertCompType *r_comp_type,
602 GPUVertFetchMode *r_fetch_mode)
603{
604 switch (gpu_type) {
605 case Type::float_t:
606 case Type::float2_t:
607 case Type::float3_t:
608 case Type::float4_t:
609 case Type::float3x3_t:
610 case Type::float4x4_t:
611 *r_comp_type = GPU_COMP_F32;
612 *r_fetch_mode = GPU_FETCH_FLOAT;
613 break;
614 case Type::int_t:
615 case Type::int2_t:
616 case Type::int3_t:
617 case Type::int4_t:
618 *r_comp_type = GPU_COMP_I32;
619 *r_fetch_mode = GPU_FETCH_INT;
620 break;
621 case Type::uint_t:
622 case Type::uint2_t:
623 case Type::uint3_t:
624 case Type::uint4_t:
625 *r_comp_type = GPU_COMP_U32;
626 *r_fetch_mode = GPU_FETCH_INT;
627 break;
628 default:
629 BLI_assert(0);
630 }
631}
632
634{
635 GPU_vertformat_clear(format);
636
638 int location_test = 0, attrs_added = 0;
639 while (attrs_added < attr_len) {
640 char name[256];
641 Type gpu_type;
643 shader, location_test++, name, reinterpret_cast<int *>(&gpu_type)))
644 {
645 continue;
646 }
647
648 GPUVertCompType comp_type;
649 GPUVertFetchMode fetch_mode;
650 recommended_fetch_mode_and_comp_type(gpu_type, &comp_type, &fetch_mode);
651
652 int comp_len = component_size_get(gpu_type);
653
654 GPU_vertformat_attr_add_legacy(format, name, comp_type, comp_len, fetch_mode);
655 attrs_added++;
656 }
657}
658
659} // namespace blender
#define SNPRINTF(dst, format,...)
int pad[32 - sizeof(int)]
#define BLI_assert_unreachable()
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition btDbvt.cpp:52
constexpr StringRef drop_prefix(int64_t n) const
constexpr int64_t size() const
void copy_unsafe(char *dst) const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr const char * data() const
VecBase< float, D > constexpr mod(VecOp< float, D >, VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
int size(const AssetLibraryReference *library_reference)
static VertAttrType vertex_format_combine(GPUVertCompType component_type, GPUVertFetchMode fetch_mode, uint32_t component_len)
bool is_fetch_normalized(VertAttrType attr_type)
bool is_fetch_float(VertAttrType attr_type)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static uint component_size_get(const Type gpu_type)
unsigned __int64 uint64_t
Definition BKE_text.h:91
__int64 int64_t
Definition BKE_text.h:90
uint padding(uint offset, uint alignment)
void GPU_vertformat_alias_add(GPUVertFormat *, StringRef alias)
unsigned int uint
Definition BKE_text.h:55
static void safe_bytes(char out[11], const char data[8])
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)
BMesh const char void * data
static uchar copy_attr_name(GPUVertFormat *format, const StringRef name)
uint32_t BLI_hash_mm2(const unsigned char *data, size_t len, uint32_t seed)
Definition hash_mm2a.cc:102
void VertexFormat_pack(GPUVertFormat *format)
uint GPU_shader_get_attribute_len(const gpu::Shader *shader)
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
static void recommended_fetch_mode_and_comp_type(Type gpu_type, GPUVertCompType *r_comp_type, GPUVertFetchMode *r_fetch_mode)
void GPU_vertformat_multiload_enable(GPUVertFormat *format, int load_count)
void GPU_vertformat_attr_rename(GPUVertFormat *format, int attr, const char *new_name)
bool GPU_shader_get_attribute_info(const gpu::Shader *shader, int attr_location, char r_name[256], int *r_type)
uint vertex_buffer_size(const GPUVertFormat *format, uint vertex_len)
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]