Blender V5.3
gpu_batch_utils.cc
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#include "MEM_guardedalloc.h"
10
11#include "DNA_vec_types.h"
12
13#include "BLI_math_vector_c.hh"
15#include "BLI_polyfill_2d.hh"
16#include "BLI_sort_utils.hh"
17#include "BLI_utildefines.hh"
18
19#include "GPU_batch.hh"
20#include "GPU_batch_utils.hh" /* own include */
21
22namespace blender {
23
24/* -------------------------------------------------------------------- */
27
28gpu::Batch *GPU_batch_tris_from_poly_2d_encoded(const uchar *polys_flat,
29 uint polys_flat_len,
30 const rctf *rect)
31{
32 const uchar(*polys)[2] = reinterpret_cast<const uchar(*)[2]>(polys_flat);
33 const uint polys_len = polys_flat_len / 2;
34 BLI_assert(polys_flat_len == polys_len * 2);
35
36 /* Over alloc in both cases */
37 float (*verts)[2] = MEM_new_array_uninitialized<float[2]>(polys_len, __func__);
38 float (*verts_step)[2] = verts;
39 uint(*tris)[3] = MEM_new_array_uninitialized<uint[3]>(polys_len, __func__);
40 uint(*tris_step)[3] = tris;
41
42 const float range_uchar[2] = {
43 (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
44 (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
45 };
46 const float min_uchar[2] = {
47 (rect ? rect->xmin : -1.0f),
48 (rect ? rect->ymin : -1.0f),
49 };
50
51 uint i_poly = 0;
52 uint i_vert = 0;
53 while (i_poly != polys_len) {
54 for (uint j = 0; j < 2; j++) {
55 verts[i_vert][j] = min_uchar[j] + (float(polys[i_poly][j]) * range_uchar[j]);
56 }
57 i_vert++;
58 i_poly++;
59 if (polys[i_poly - 1][0] == polys[i_poly][0] && polys[i_poly - 1][1] == polys[i_poly][1]) {
60 const uint verts_step_len = (&verts[i_vert]) - verts_step;
61 BLI_assert(verts_step_len >= 3);
62 const uint tris_len = (verts_step_len - 2);
63 BLI_polyfill_calc(verts_step, verts_step_len, -1, tris_step);
64 /* offset indices */
65 if (verts_step != verts) {
66 uint *t = tris_step[0];
67 const uint offset = (verts_step - verts);
68 uint tot = tris_len * 3;
69 while (tot--) {
70 *t += offset;
71 t++;
72 }
73 BLI_assert(t == tris_step[tris_len]);
74 }
75 verts_step += verts_step_len;
76 tris_step += tris_len;
77 i_poly++;
78 /* ignore the duplicate point */
79 }
80 }
81
82 /* We have vertices and tris, make a batch from this. */
83 static GPUVertFormat format = {0};
84 static struct {
85 uint pos;
86 } attr_id;
87 if (format.attr_len == 0) {
88 attr_id.pos = GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32);
89 }
90
91 const uint verts_len = (verts_step - verts);
92 const uint tris_len = (tris_step - tris);
93 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
94 GPU_vertbuf_data_alloc(*vbo, verts_len);
95
96 GPUVertBufRaw pos_step;
97 GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
98
99 for (uint i = 0; i < verts_len; i++) {
100 copy_v2_v2(static_cast<float *>(GPU_vertbuf_raw_step(&pos_step)), verts[i]);
101 }
102
104 GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, tris_len, verts_len);
105 for (uint i = 0; i < tris_len; i++) {
106 GPU_indexbuf_add_tri_verts(&elb, UNPACK3(tris[i]));
107 }
108 gpu::IndexBuf *indexbuf = GPU_indexbuf_build(&elb);
109
110 MEM_delete(tris);
111 MEM_delete(verts);
112
113 return GPU_batch_create_ex(
115}
116
117gpu::Batch *GPU_batch_wire_from_poly_2d_encoded(const uchar *polys_flat,
118 uint polys_flat_len,
119 const rctf *rect)
120{
121 const uchar(*polys)[2] = reinterpret_cast<const uchar(*)[2]>(polys_flat);
122 const uint polys_len = polys_flat_len / 2;
123 BLI_assert(polys_flat_len == polys_len * 2);
124
125 /* Over alloc */
126 /* Lines are pairs of (x, y) byte locations packed into an int32_t. */
127 int32_t *lines = MEM_new_array_uninitialized<int32_t>(polys_len, __func__);
128 int32_t *lines_step = lines;
129
130 const float range_uchar[2] = {
131 (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
132 (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
133 };
134 const float min_uchar[2] = {
135 (rect ? rect->xmin : -1.0f),
136 (rect ? rect->ymin : -1.0f),
137 };
138
139 uint i_poly_prev = 0;
140 uint i_poly = 0;
141 while (i_poly != polys_len) {
142 i_poly++;
143 if (polys[i_poly - 1][0] == polys[i_poly][0] && polys[i_poly - 1][1] == polys[i_poly][1]) {
144 const uchar(*polys_step)[2] = polys + i_poly_prev;
145 const uint polys_step_len = i_poly - i_poly_prev;
146 BLI_assert(polys_step_len >= 2);
147 for (uint i_prev = polys_step_len - 1, i = 0; i < polys_step_len; i_prev = i++) {
148 union {
149 uint16_t as_u16[2];
150 uint32_t as_u32;
151 } data;
152 data.as_u16[0] = *(reinterpret_cast<const uint16_t *>(polys_step[i_prev]));
153 data.as_u16[1] = *(reinterpret_cast<const uint16_t *>(polys_step[i]));
154 if (data.as_u16[0] > data.as_u16[1]) {
155 std::swap(data.as_u16[0], data.as_u16[1]);
156 }
157 *lines_step = data.as_u32;
158 lines_step++;
159 }
160 i_poly++;
161 i_poly_prev = i_poly;
162 /* ignore the duplicate point */
163 }
164 }
165
166 uint lines_len = lines_step - lines;
167
168 /* Hide Lines (we could make optional) */
169 {
170 qsort(lines, lines_len, sizeof(int32_t), BLI_sortutil_cmp_int);
171 lines_step = lines;
172 for (uint i_prev = 0, i = 1; i < lines_len; i_prev = i++) {
173 if (lines[i] != lines[i_prev]) {
174 *lines_step++ = lines[i_prev];
175 }
176 else {
177 i++;
178 }
179 }
180 *lines_step++ = lines[lines_len - 1];
181 lines_len = lines_step - lines;
182 }
183
184 /* We have vertices and tris, make a batch from this. */
185 static GPUVertFormat format = {0};
186 static struct {
187 uint pos;
188 } attr_id;
189 if (format.attr_len == 0) {
190 attr_id.pos = GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32);
191 }
192
193 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
194 const uint vbo_len_capacity = lines_len * 2;
195 GPU_vertbuf_data_alloc(*vbo, vbo_len_capacity);
196
197 GPUVertBufRaw pos_step;
198 GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
199
200 for (uint i = 0; i < lines_len; i++) {
201 union {
202 uint8_t as_u8_pair[2][2];
203 uint32_t as_u32;
204 } data;
205 data.as_u32 = lines[i];
206 for (uint k = 0; k < 2; k++) {
207 float *pos_v2 = static_cast<float *>(GPU_vertbuf_raw_step(&pos_step));
208 for (uint j = 0; j < 2; j++) {
209 pos_v2[j] = min_uchar[j] + (float(data.as_u8_pair[k][j]) * range_uchar[j]);
210 }
211 }
212 }
213 BLI_assert(vbo_len_capacity == GPU_vertbuf_raw_used(&pos_step));
214 MEM_delete(lines);
216}
217
219
220/* -------------------------------------------------------------------- */
223
225{
226 static const std::array<float3, 8> bone_box_verts = {
227 float3{1.0f, -1.0f, 1.0f},
228 float3{1.0f, -1.0f, -1.0f},
229 float3{-1.0f, -1.0f, -1.0f},
230 float3{-1.0f, -1.0f, 1.0f},
231 float3{1.0f, 1.0f, 1.0f},
232 float3{1.0f, 1.0f, -1.0f},
233 float3{-1.0f, 1.0f, -1.0f},
234 float3{-1.0f, 1.0f, 1.0f},
235 };
236
237 static const std::array<int3, 12> bone_box_solid_tris = {
238 int3{0, 2, 1}, /* bottom */
239 int3{0, 3, 2},
240
241 int3{0, 1, 5}, /* sides */
242 int3{0, 5, 4},
243
244 int3{1, 2, 6},
245 int3{1, 6, 5},
246
247 int3{2, 3, 7},
248 int3{2, 7, 6},
249
250 int3{3, 0, 4},
251 int3{3, 4, 7},
252
253 int3{4, 5, 6}, /* top */
254 int3{4, 6, 7},
255 };
256
257 GPUVertFormat format = {0};
258 GPU_vertformat_attr_add(&format, "pos", gpu::VertAttrType::SFLOAT_32_32_32);
259 gpu::VertBuf *vbo = GPU_vertbuf_create_with_format(format);
260
261 const int tri_len = bone_box_solid_tris.size();
262 const int vert_len = bone_box_verts.size();
263
264 GPU_vertbuf_data_alloc(*vbo, vert_len);
265
267 GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, tri_len, vert_len);
268
269 int v = 0;
270 for (int i = 0; i < vert_len; i++) {
271 GPU_vertbuf_vert_set(vbo, v++, &bone_box_verts[i]);
272 }
273
274 for (int i = 0; i < tri_len; i++) {
275 const int3 tri_indices = bone_box_solid_tris[i];
276 GPU_indexbuf_add_tri_verts(&elb, tri_indices[0], tri_indices[1], tri_indices[2]);
277 }
278
279 return GPU_batch_create_ex(
281}
282
284
285} // namespace blender
Read Blender memory allocation and freeing.
#define UNPACK3(a)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
GPU_INLINE uint GPU_vertbuf_raw_used(const GPUVertBufRaw *a)
gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, gpu::VertBuf *vertex_buf, gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:53
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
unsigned int uint
Definition BKE_text.h:55
void * MEM_new_array_uninitialized(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
MINLINE void copy_v2_v2(float r[2], const float a[2])
void BLI_polyfill_calc(const float(*coords)[2], unsigned int coords_num, int coords_sign, unsigned int(*r_tris)[3])
gpu::Batch * GPU_batch_unit_cube() ATTR_WARN_UNUSED_RESULT
ATTR_WARN_UNUSED_RESULT const BMVert * v
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
unsigned short uint16_t
Definition BKE_text.h:80
unsigned int uint32_t
Definition BKE_text.h:81
gpu::IndexBuf * GPU_indexbuf_build(GPUIndexBufBuilder *)
VecBase< int32_t, 3 > int3
unsigned char uchar
Definition BKE_text.h:58
BMesh const char void * data
gpu::Batch * GPU_batch_wire_from_poly_2d_encoded(const uchar *polys_flat, uint polys_flat_len, const rctf *rect) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
unsigned char uint8_t
Definition BKE_text.h:79
gpu::Batch * GPU_batch_tris_from_poly_2d_encoded(const uchar *polys_flat, uint polys_flat_len, const rctf *rect) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:44
@ GPU_BATCH_OWNS_INDEX
Definition GPU_batch.hh:48
void GPU_indexbuf_add_tri_verts(GPUIndexBufBuilder *, uint v1, uint v2, uint v3)
void GPU_vertbuf_attr_get_raw_data(gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access)
uint GPU_vertformat_attr_add(GPUVertFormat *format, StringRef name, gpu::VertAttrType type)
void GPU_vertbuf_vert_set(gpu::VertBuf *verts, uint v_idx, const void *data)
void GPU_vertbuf_data_alloc(gpu::VertBuf &verts, uint v_len)
signed int int32_t
Definition BKE_text.h:78
int BLI_sortutil_cmp_int(const void *a_, const void *b_)
Definition sort_utils.cc:55
VecBase< float, 3 > float3
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float