Blender V5.3
GPU_index_buffer.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_span.hh"
14
15#include "GPU_primitive.hh"
16
17namespace blender {
18
19#define GPU_TRACK_INDEX_RANGE 1
20
21namespace gpu {
22
24constexpr uint32_t RESTART_INDEX = 0xFFFFFFFF;
25
30
31inline size_t to_bytesize(GPUIndexBufType type)
32{
33 return (type == GPU_INDEX_U32) ? sizeof(uint32_t) : sizeof(uint16_t);
34}
35
42class IndexBuf {
43 protected:
53 bool is_init_ = false;
55 bool is_subrange_ = false;
57 bool is_empty_ = false;
58
59 union {
61 void *data_ = nullptr;
63 IndexBuf *src_;
64 };
65
66 public:
68 virtual ~IndexBuf();
69
70 void init(uint indices_len,
71 uint32_t *indices,
72 uint min_index,
73 uint max_index,
74 GPUPrimType prim_type,
75 bool uses_restart_indices);
76 void init_subrange(IndexBuf *elem_src, uint start, uint length);
77 void init_build_on_device(uint index_len);
78
79 /* Returns render index count (not precise). */
81 {
82 /* Return 0 to bypass drawing for index buffers full of restart indices.
83 * They can lead to graphical glitches on some systems. (See #96892) */
84 return is_empty_ ? 0 : index_len_;
85 }
87 {
88 return index_start_;
89 }
91 {
92 return index_base_;
93 }
94 bool is_32bit() const
95 {
96 return index_type_ == GPU_INDEX_U32;
97 }
99 {
100 return index_type_;
101 }
102 /* Return size in byte of the drawable data buffer range. Actual buffer size might be bigger. */
103 size_t size_get() const
104 {
106 };
107
108 bool is_init() const
109 {
110 return is_init_;
111 };
112
113 virtual void upload_data() = 0;
114
115 virtual void bind_as_ssbo(uint binding) = 0;
116
117 virtual void read(uint32_t *data) const = 0;
118
119 virtual void update_sub(uint start, uint len, const void *data) = 0;
120
133 virtual void copy_sub(IndexBuf &source_buf,
134 uint source_first_index,
135 uint dest_first_index,
136 uint index_len) = 0;
137
138 private:
139 inline void squeeze_indices_short(uint min_idx,
140 uint max_idx,
141 GPUPrimType prim_type,
142 bool clamp_indices_in_range);
143 virtual void strip_restart_indices() = 0;
144};
145
147{
148 switch (prim_type) {
149 case GPU_PRIM_POINTS:
150 return 1;
151 case GPU_PRIM_LINES:
152 return 2;
153 case GPU_PRIM_TRIS:
154 return 3;
156 return 4;
158 return 6;
159 /* IMPORTANT: These last two expects no restart primitive.
160 * Asserting for this would be too slow. Just don't be stupid.
161 * This is needed for polylines but should be deprecated.
162 * See GPU_batch_draw_expanded_parameter_get */
164 return 1; /* Minus one for the whole length. */
166 return 1;
167 default:
168 return -1;
169 }
170}
171
172} // namespace gpu
173
174gpu::IndexBuf *GPU_indexbuf_calloc();
175
188
190void GPU_indexbuf_init_ex(GPUIndexBufBuilder *, GPUPrimType, uint index_len, uint vertex_len);
191
193void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len);
195
197
199
200/*
201 * Thread safe.
202 *
203 * Function inspired by the reduction directives of multi-thread work API's.
204 */
205void GPU_indexbuf_join(GPUIndexBufBuilder *builder, const GPUIndexBufBuilder *builder_from);
206
209
214
218
219/* Skip primitive rendering at the given index. */
223
226 uint index_min,
227 uint index_max,
228 bool uses_restart_indices);
231 uint index_min,
232 uint index_max,
233 bool uses_restart_indices,
234 gpu::IndexBuf *elem);
235
244 const uint32_t *data,
245 int32_t data_len,
246 int32_t index_min,
247 int32_t index_max,
248 bool uses_restart_indices);
249
253void GPU_indexbuf_bind_as_ssbo(gpu::IndexBuf *elem, int binding);
254
256 uint curves_num,
257 uint verts_per_curve);
258
259/* Upload data to the GPU (if not built on the device) and bind the buffer to its default target.
260 */
262
263/* Partially update the gpu::IndexBuf which was already sent to the device, or built
264 * directly on the device. The data needs to be compatible with potential compression applied to
265 * the original indices when the index buffer was built, i.e., if the data was compressed to use
266 * shorts instead of ints, shorts should passed here. */
267void GPU_indexbuf_update_sub(gpu::IndexBuf *elem, uint start, uint len, const void *data);
268
276 gpu::IndexBuf *src,
277 uint source_first_index,
278 uint dest_first_index,
279 uint index_len);
280
281/* Create a sub-range of an existing index-buffer. */
284 gpu::IndexBuf *elem_src,
285 uint start,
286 uint length);
287
294
296
298
300
301/* Macros */
302
303#define GPU_INDEXBUF_DISCARD_SAFE(elem) \
304 do { \
305 if (elem != nullptr) { \
306 GPU_indexbuf_discard(elem); \
307 elem = nullptr; \
308 } \
309 } while (0)
310
311namespace gpu {
312
314 public:
316 {
318 }
319};
320
321using IndexBufPtr = std::unique_ptr<IndexBuf, IndexBufDeleter>;
322
323} // namespace gpu
324} // namespace blender
uint32_t index_start_get() const
uint32_t index_base_get() const
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)
GPUIndexBufType index_type() const
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)
uint32_t index_len_get() const
virtual void bind_as_ssbo(uint binding)=0
float length(VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
std::unique_ptr< IndexBuf, IndexBufDeleter > IndexBufPtr
int to_bytesize(const DataFormat format)
constexpr uint32_t RESTART_INDEX
int indices_per_primitive(GPUPrimType prim_type)
BMesh Iterators.
bool GPU_indexbuf_is_init(gpu::IndexBuf *elem)
void GPU_indexbuf_set_point_restart(GPUIndexBufBuilder *builder, uint elem)
gpu::IndexBuf * GPU_indexbuf_build_on_device(uint index_len)
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
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_TRIS_ADJ
@ GPU_PRIM_LINES_ADJ
@ GPU_PRIM_LINE_STRIP
@ GPU_PRIM_LINE_LOOP
void GPU_indexbuf_join(GPUIndexBufBuilder *builder, const GPUIndexBufBuilder *builder_from)
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_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)
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)
BMesh const char void * data
MutableSpan< uint32_t > GPU_indexbuf_get_data(GPUIndexBufBuilder *)
void GPU_indexbuf_set_point_vert(GPUIndexBufBuilder *builder, uint elem, uint v1)
gpu::IndexBuf * GPU_indexbuf_calloc()
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 *)
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)