Blender V5.3
gpu_index_buffer.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 "MEM_guardedalloc.h"
12
13#include "BLI_array_utils.hh"
14#include "BLI_math_base_c.hh"
15#include "BLI_utildefines.hh"
16
17#include "gpu_backend.hh"
18
19#include "GPU_index_buffer.hh"
20
21#include "GPU_capabilities.hh"
22#include "GPU_compute.hh"
23#include "GPU_platform.hh"
24#include "GPU_state.hh"
25
26#include <algorithm> /* For `min/max`. */
27#include <cstring>
28
29namespace blender {
30
31/* -------------------------------------------------------------------- */
34
35using namespace blender::gpu;
36
38 GPUPrimType prim_type,
39 uint index_len,
40 uint vertex_len)
41{
42 builder->max_allowed_index = vertex_len - 1;
43 builder->max_index_len = index_len;
44 builder->index_len = 0; // start empty
45 builder->index_min = UINT32_MAX;
46 builder->index_max = 0;
47 builder->prim_type = prim_type;
48
49#ifdef __APPLE__
50 /* Only encode restart indices for restart-compatible primitive types.
51 * Resolves out-of-bounds read error on macOS. Using 0-index will ensure
52 * degenerative primitives when skipping primitives is required and will
53 * incur no additional performance cost for rendering. */
55 /* We will still use restart-indices for point primitives and then
56 * patch these during IndexBuf::init, as we cannot benefit from degenerative
57 * primitives to eliminate these. */
58 builder->restart_index_value = (is_restart_compatible(prim_type) ||
59 prim_type == GPU_PRIM_POINTS) ?
61 0;
62 }
63 else {
65 }
66#else
68#endif
69 builder->uses_restart_indices = false;
70 builder->data = MEM_new_array_uninitialized<uint>(builder->max_index_len, "IndexBuf data");
71}
72
74 GPUPrimType prim_type,
75 uint prim_len,
76 uint vertex_len)
77{
78 int verts_per_prim = GPU_indexbuf_primitive_len(prim_type);
79 BLI_assert(verts_per_prim != -1);
80 GPU_indexbuf_init_ex(builder, prim_type, prim_len * uint(verts_per_prim), vertex_len);
81}
82
84{
86 GPU_indexbuf_init_build_on_device(elem_, index_len);
87 return elem_;
88}
89
91{
92 IndexBuf *elem_ = elem;
93 elem_->init_build_on_device(index_len);
94}
95
97{
98 return {builder->data, builder->max_index_len};
99}
100
101void GPU_indexbuf_join(GPUIndexBufBuilder *builder_to, const GPUIndexBufBuilder *builder_from)
102{
103 BLI_assert(builder_to->data == builder_from->data);
104 builder_to->index_len = max_uu(builder_to->index_len, builder_from->index_len);
105 builder_to->index_min = min_uu(builder_to->index_min, builder_from->index_min);
106 builder_to->index_max = max_uu(builder_to->index_max, builder_from->index_max);
107}
108
110{
111 BLI_assert(builder->data != nullptr);
112 BLI_assert(builder->index_len < builder->max_index_len);
113 BLI_assert(v <= builder->max_allowed_index);
114 builder->data[builder->index_len++] = v;
115 builder->index_min = std::min(builder->index_min, v);
116 builder->index_max = std::max(builder->index_max, v);
117}
118
120{
121 BLI_assert(builder->data != nullptr);
122 BLI_assert(builder->index_len < builder->max_index_len);
123 builder->data[builder->index_len++] = builder->restart_index_value;
124 builder->uses_restart_indices = true;
125}
126
132
140
142{
144 BLI_assert(v1 != v2 && v2 != v3 && v3 != v1);
148}
149
151 GPUIndexBufBuilder *builder, uint v1, uint v2, uint v3, uint v4)
152{
154 BLI_assert(v2 != v3); /* only the line need diff indices */
159}
160
162{
164 BLI_assert(elem < builder->max_index_len);
165 builder->data[elem++] = v1;
166 builder->index_min = std::min(builder->index_min, v1);
167 builder->index_max = std::max(builder->index_max, v1);
168 builder->index_len = std::max(builder->index_len, elem);
169}
170
172{
174 BLI_assert(v1 != v2);
175 BLI_assert(v1 <= builder->max_allowed_index);
176 BLI_assert(v2 <= builder->max_allowed_index);
177 BLI_assert((elem + 1) * 2 <= builder->max_index_len);
178 uint idx = elem * 2;
179 builder->data[idx++] = v1;
180 builder->data[idx++] = v2;
181 builder->index_min = std::min({builder->index_min, v1, v2});
182 builder->index_max = std::max({builder->index_max, v1, v2});
183 builder->index_len = std::max(builder->index_len, idx);
184}
185
187{
189 BLI_assert(v1 != v2 && v2 != v3 && v3 != v1);
190 BLI_assert(v1 <= builder->max_allowed_index);
191 BLI_assert(v2 <= builder->max_allowed_index);
192 BLI_assert(v3 <= builder->max_allowed_index);
193 BLI_assert((elem + 1) * 3 <= builder->max_index_len);
194 uint idx = elem * 3;
195 builder->data[idx++] = v1;
196 builder->data[idx++] = v2;
197 builder->data[idx++] = v3;
198
199 builder->index_min = std::min({builder->index_min, v1, v2, v3});
200 builder->index_max = std::max({builder->index_max, v1, v2, v3});
201 builder->index_len = std::max(builder->index_len, idx);
202}
203
205{
207 BLI_assert(elem < builder->max_index_len);
208 builder->data[elem++] = builder->restart_index_value;
209 builder->index_len = std::max(builder->index_len, elem);
210 builder->uses_restart_indices = true;
211}
212
214{
216 BLI_assert((elem + 1) * 2 <= builder->max_index_len);
217 uint idx = elem * 2;
218 builder->data[idx++] = builder->restart_index_value;
219 builder->data[idx++] = builder->restart_index_value;
220 builder->index_len = std::max(builder->index_len, idx);
221 builder->uses_restart_indices = true;
222}
223
225{
227 BLI_assert((elem + 1) * 3 <= builder->max_index_len);
228 uint idx = elem * 3;
229 builder->data[idx++] = builder->restart_index_value;
230 builder->data[idx++] = builder->restart_index_value;
231 builder->data[idx++] = builder->restart_index_value;
232 builder->index_len = std::max(builder->index_len, idx);
233 builder->uses_restart_indices = true;
234}
235
237 uint curves_num,
238 uint verts_per_curve)
239{
240 uint64_t dispatch_x_dim = verts_per_curve;
242 dispatch_x_dim += 1;
243 }
244 uint64_t grid_x, grid_y, grid_z;
245 uint64_t max_grid_x = GPU_max_work_group_count(0), max_grid_y = GPU_max_work_group_count(1),
246 max_grid_z = GPU_max_work_group_count(2);
247 grid_x = min_uu(max_grid_x, (dispatch_x_dim + 15) / 16);
248 grid_y = (curves_num + 15) / 16;
249 if (grid_y <= max_grid_y) {
250 grid_z = 1;
251 }
252 else {
253 grid_y = grid_z = uint64_t(ceil(sqrt(double(grid_y))));
254 grid_y = min_uu(grid_y, max_grid_y);
255 grid_z = min_uu(grid_z, max_grid_z);
256 }
257 bool tris = (prim_type == GPU_PRIM_TRIS);
258 bool lines = (prim_type == GPU_PRIM_LINES);
263 IndexBuf *ibo = GPU_indexbuf_build_on_device(curves_num * dispatch_x_dim);
264 int resolution;
265 if (tris) {
266 resolution = 6;
267 }
268 else if (lines) {
269 resolution = 2;
270 }
271 else {
272 resolution = 1;
273 }
274 GPU_shader_uniform_1i(shader, "elements_per_curve", dispatch_x_dim / resolution);
275 GPU_shader_uniform_1i(shader, "ncurves", curves_num);
277 GPU_compute_dispatch(shader, grid_x, grid_y, grid_z);
278
281 return ibo;
282}
283
285
286/* -------------------------------------------------------------------- */
289
290namespace gpu {
291
293{
294 if (!is_subrange_) {
295 MEM_SAFE_DELETE_VOID(data_);
296 }
297}
298
299void IndexBuf::init(uint indices_len,
300 uint32_t *indices,
301 uint min_index,
302 uint max_index,
303 GPUPrimType prim_type,
304 bool uses_restart_indices)
305{
306 is_init_ = true;
307 data_ = indices;
308 index_start_ = 0;
309 index_len_ = indices_len;
310 is_empty_ = min_index > max_index;
311
312 /* Patch index buffer to remove restart indices from
313 * non-restart-compatible primitive types. Restart indices
314 * are situationally added to selectively hide vertices.
315 * Metal does not support restart-indices for non-restart-compatible
316 * types, as such we should remove these indices.
317 *
318 * We only need to perform this for point primitives, as
319 * line primitives/triangle primitives can use index 0 for all
320 * vertices to create a degenerative primitive, where all
321 * vertices share the same index and skip rendering via HW
322 * culling. */
323 if (prim_type == GPU_PRIM_POINTS && uses_restart_indices) {
324 this->strip_restart_indices();
325 }
326
327#if GPU_TRACK_INDEX_RANGE
328 /* Everything remains 32 bit while building to keep things simple.
329 * Find min/max after, then convert to smallest index type possible. */
330 uint range = min_index < max_index ? max_index - min_index : 0;
331 /* count the primitive restart index. */
332 range += 1;
333
334 if (range <= 0xFFFF) {
336 bool do_clamp_indices = false;
337# ifdef __APPLE__
338 /* NOTE: For the Metal Backend, we use degenerative primitives to hide vertices
339 * which are not restart compatible. When this is done, we need to ensure
340 * that compressed index ranges clamp all index values within the valid
341 * range, rather than maximally clamping against the USHORT restart index
342 * value of 0xFFFFu, as this will cause an out-of-bounds read during
343 * vertex assembly. */
344 do_clamp_indices = GPU_type_matches_ex(
346# endif
347 this->squeeze_indices_short(min_index, max_index, prim_type, do_clamp_indices);
348 }
349#endif
350}
351
353{
354 is_init_ = true;
355 index_start_ = 0;
356 index_len_ = index_len;
358 data_ = nullptr;
359}
360
362{
363 /* We don't support nested sub-ranges. */
364 BLI_assert(elem_src && elem_src->is_subrange_ == false);
366
367 is_init_ = true;
368 is_subrange_ = true;
369 src_ = elem_src;
370 index_start_ = start;
372 index_base_ = elem_src->index_base_;
373 index_type_ = elem_src->index_type_;
374}
375
376void IndexBuf::squeeze_indices_short(uint min_idx,
377 uint max_idx,
378 GPUPrimType prim_type,
379 bool clamp_indices_in_range)
380{
381 /* data will never be *larger* than builder->data...
382 * converting in place to avoid extra allocation */
383 uint16_t *ushort_idx = static_cast<uint16_t *>(data_);
384 const uint32_t *uint_idx = static_cast<uint32_t *>(data_);
385
386 if (max_idx >= 0xFFFF) {
387 index_base_ = min_idx;
388 /* NOTE: When using restart_index=0 for degenerative primitives indices,
389 * the compressed index will go below zero and wrap around when min_idx > 0.
390 * In order to ensure the resulting index is still within range, we instead
391 * clamp index to the maximum within the index range.
392 *
393 * `clamp_max_idx` represents the maximum possible index to clamp against. If primitive is
394 * restart-compatible, we can just clamp against the primitive-restart value, otherwise, we
395 * must assign to a valid index within the range.
396 *
397 * NOTE: For OpenGL we skip this by disabling clamping, as we still need to use
398 * restart index values for point primitives to disable rendering. */
399 uint16_t clamp_max_idx = (is_restart_compatible(prim_type) || !clamp_indices_in_range) ?
400 0xFFFFu :
401 (max_idx - min_idx);
402 for (uint i = 0; i < index_len_; i++) {
403 ushort_idx[i] = std::min<uint16_t>(clamp_max_idx, uint_idx[i] - min_idx);
404 }
405 }
406 else {
407 index_base_ = 0;
408 for (uint i = 0; i < index_len_; i++) {
409 ushort_idx[i] = uint16_t(uint_idx[i]);
410 }
411 }
412}
413
414} // namespace gpu
415
417
418/* -------------------------------------------------------------------- */
421
426
428{
430 GPU_indexbuf_build_in_place(builder, elem);
431 return elem;
432}
433
435{
437 GPU_indexbuf_create_subrange_in_place(elem, elem_src, start, length);
438 return elem;
439}
440
442{
443 BLI_assert(builder->data != nullptr);
444 /* Transfer data ownership to IndexBuf.
445 * It will be uploaded upon first use. */
446 elem->init(builder->index_len,
447 builder->data,
448 builder->index_min,
449 builder->index_max,
450 builder->prim_type,
451 builder->uses_restart_indices);
452 builder->data = nullptr;
453}
454
456 const uint index_min,
457 const uint index_max,
458 const bool uses_restart_indices,
459 IndexBuf *elem)
460{
461 BLI_assert(builder->data != nullptr);
462 /* Transfer data ownership to IndexBuf.
463 * It will be uploaded upon first use. */
464 elem->init(builder->max_index_len,
465 builder->data,
466 index_min,
467 index_max,
468 builder->prim_type,
469 uses_restart_indices);
470 builder->data = nullptr;
471}
472
474 const uint index_min,
475 const uint index_max,
476 const bool uses_restart_indices)
477{
479 GPU_indexbuf_build_in_place_ex(builder, index_min, index_max, uses_restart_indices, elem);
480 return elem;
481}
482
484 const uint32_t *data,
485 const int32_t data_len,
486 const int32_t index_min,
487 const int32_t index_max,
488 const bool uses_restart_indices)
489{
490 const uint32_t indices_num = data_len * indices_per_primitive(prim_type);
491 /* TODO: The need for this copy is meant to be temporary. The data should be uploaded directly to
492 * the GPU here rather than copied to an array owned by the IBO first. */
493 uint32_t *copy = MEM_new_array_uninitialized<uint32_t>(indices_num, __func__);
494 threading::memory_bandwidth_bound_task(sizeof(uint32_t) * indices_num * 2, [&]() {
495 array_utils::copy(Span(data, indices_num), MutableSpan(copy, indices_num));
496 });
498 ibo->init(indices_num, copy, index_min, index_max, prim_type, uses_restart_indices);
499 return ibo;
500}
501
503 IndexBuf *elem_src,
504 uint start,
505 uint length)
506{
507 elem->init_subrange(elem_src, start, length);
508}
509
511{
512 elem->read(data);
513}
514
516{
517 delete elem;
518}
519
521{
522 return elem->is_init();
523}
524
526{
527 return indices_per_primitive(prim_type);
528}
529
531{
532 elem->upload_data();
533}
534
535void GPU_indexbuf_bind_as_ssbo(IndexBuf *elem, int binding)
536{
537 elem->bind_as_ssbo(binding);
538}
539
540void GPU_indexbuf_update_sub(IndexBuf *elem, uint start, uint len, const void *data)
541{
542 elem->update_sub(start, len, data);
543}
544
546 IndexBuf *dst, IndexBuf *src, uint source_first_index, uint dest_first_index, uint index_len)
547{
548 dst->copy_sub(*src, source_first_index, dest_first_index, index_len);
549}
550
552
553} // namespace blender
#define UINT32_MAX
Read Blender memory allocation and freeing.
#define ELEM(...)
SIMD_FORCE_INLINE btScalar length() const
Return the length of the vector.
Definition btVector3.h:257
constexpr MutableSpan()=default
constexpr Span()=default
virtual IndexBuf * indexbuf_alloc()=0
static GPUBackend * get()
virtual void upload_data()=0
virtual void read(uint32_t *data) const =0
GPUIndexBufType index_type_
virtual void update_sub(uint start, uint len, const void *data)=0
void init_build_on_device(uint index_len)
virtual void copy_sub(IndexBuf &source_buf, uint source_first_index, uint dest_first_index, uint index_len)=0
Copy part of source index buffer into this index buffer.
void init_subrange(IndexBuf *elem_src, uint start, uint length)
virtual void bind_as_ssbo(uint binding)=0
void init(uint indices_len, uint32_t *indices, uint min_index, uint max_index, GPUPrimType prim_type, bool uses_restart_indices)
#define ceil
float length(VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void copy(const GVArray &src, GMutableSpan dst, exec_mode::Mode mode=exec_mode::parallel)
constexpr uint32_t RESTART_INDEX
int indices_per_primitive(GPUPrimType prim_type)
void memory_bandwidth_bound_task(const int64_t approximate_bytes_touched, const Function &function)
Definition BLI_task.hh:251
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool GPU_indexbuf_is_init(gpu::IndexBuf *elem)
unsigned __int64 uint64_t
Definition BKE_text.h:91
void GPU_indexbuf_set_point_restart(GPUIndexBufBuilder *builder, uint elem)
gpu::IndexBuf * GPU_indexbuf_build_on_device(uint index_len)
bool GPU_type_matches_ex(GPUDeviceType device, GPUOSType os, GPUDriverType driver, GPUBackendType backend)
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
void GPU_indexbuf_init_ex(GPUIndexBufBuilder *, GPUPrimType, uint index_len, uint vertex_len)
unsigned int uint
Definition BKE_text.h:55
bool is_restart_compatible(GPUPrimType type)
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
void GPU_indexbuf_use(gpu::IndexBuf *elem)
void GPU_indexbuf_add_line_verts(GPUIndexBufBuilder *, uint v1, uint v2)
void GPU_indexbuf_update_sub(gpu::IndexBuf *elem, uint start, uint len, const void *data)
void GPU_indexbuf_add_primitive_restart(GPUIndexBufBuilder *)
@ GPU_PRIM_TRI_STRIP
@ GPU_PRIM_LINES_ADJ
@ GPU_PRIM_LINE_STRIP
void GPU_indexbuf_join(GPUIndexBufBuilder *builder, const GPUIndexBufBuilder *builder_from)
MINLINE uint max_uu(uint a, uint b)
void GPU_indexbuf_add_line_adj_verts(GPUIndexBufBuilder *, uint v1, uint v2, uint v3, uint v4)
ATTR_WARN_UNUSED_RESULT const BMVert * v
void GPU_compute_dispatch(gpu::Shader *shader, uint groups_x_len, uint groups_y_len, uint groups_z_len, const gpu::shader::SpecializationConstants *constants_state=nullptr)
int GPU_shader_get_ssbo_binding(gpu::Shader *shader, const char *name)
void GPU_indexbuf_bind_as_ssbo(gpu::IndexBuf *elem, int binding)
unsigned short uint16_t
Definition BKE_text.h:80
void GPU_indexbuf_set_line_verts(GPUIndexBufBuilder *builder, uint elem, uint v1, uint v2)
void GPU_indexbuf_set_tri_restart(GPUIndexBufBuilder *builder, uint elem)
int GPU_max_work_group_count(int index)
void GPU_indexbuf_add_point_vert(GPUIndexBufBuilder *, uint v)
unsigned int uint32_t
Definition BKE_text.h:81
gpu::IndexBuf * GPU_indexbuf_build(GPUIndexBufBuilder *)
void GPU_indexbuf_build_in_place_ex(GPUIndexBufBuilder *builder, uint index_min, uint index_max, bool uses_restart_indices, gpu::IndexBuf *elem)
void GPU_memory_barrier(GPUBarrier barrier)
Definition gpu_state.cc:305
BMesh const char void * data
void GPU_shader_unbind()
MutableSpan< uint32_t > GPU_indexbuf_get_data(GPUIndexBufBuilder *)
sqrt(x)+1/max(0
void GPU_indexbuf_set_point_vert(GPUIndexBufBuilder *builder, uint elem, uint v1)
gpu::IndexBuf * GPU_indexbuf_calloc()
void GPU_shader_bind(gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_shader_uniform_1i(gpu::Shader *sh, const char *name, int value)
@ GPU_BARRIER_ELEMENT_ARRAY
Definition GPU_state.hh:55
gpu::IndexBuf * GPU_indexbuf_build_curves_on_device(GPUPrimType prim_type, uint curves_num, uint verts_per_curve)
void GPU_indexbuf_add_tri_verts(GPUIndexBufBuilder *, uint v1, uint v2, uint v3)
void GPU_indexbuf_init_build_on_device(gpu::IndexBuf *elem, uint index_len)
gpu::IndexBuf * GPU_indexbuf_build_from_memory(GPUPrimType prim_type, const uint32_t *data, int32_t data_len, int32_t index_min, int32_t index_max, bool uses_restart_indices)
void GPU_indexbuf_read(gpu::IndexBuf *elem, uint32_t *data)
void GPU_indexbuf_build_in_place(GPUIndexBufBuilder *, gpu::IndexBuf *)
gpu::Shader * GPU_shader_get_builtin_shader(GPUBuiltinShader shader)
void GPU_indexbuf_set_tri_verts(GPUIndexBufBuilder *builder, uint elem, uint v1, uint v2, uint v3)
void GPU_indexbuf_create_subrange_in_place(gpu::IndexBuf *elem, gpu::IndexBuf *elem_src, uint start, uint length)
ATTR_WARN_UNUSED_RESULT const BMVert * v2
void GPU_indexbuf_copy_sub(gpu::IndexBuf *dst, gpu::IndexBuf *src, uint source_first_index, uint dest_first_index, uint index_len)
Copy index data from part of a source index buffer into this index buffer.
void GPU_indexbuf_discard(gpu::IndexBuf *elem)
gpu::IndexBuf * GPU_indexbuf_build_ex(GPUIndexBufBuilder *builder, uint index_min, uint index_max, bool uses_restart_indices)
signed int int32_t
Definition BKE_text.h:78
gpu::IndexBuf * GPU_indexbuf_create_subrange(gpu::IndexBuf *elem_src, uint start, uint length)
void GPU_indexbuf_add_generic_vert(GPUIndexBufBuilder *, uint v)
int GPU_indexbuf_primitive_len(GPUPrimType prim_type)
void GPU_indexbuf_set_line_restart(GPUIndexBufBuilder *builder, uint elem)
MINLINE uint min_uu(uint a, uint b)