Blender V4.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
9#include "MEM_guardedalloc.h"
10
11#include "BLI_math_base.h"
12#include "BLI_polyfill_2d.h"
13#include "BLI_rect.h"
14#include "BLI_sort_utils.h"
15#include "BLI_utildefines.h"
16
17#include "GPU_batch.hh"
18#include "GPU_batch_utils.hh" /* own include */
19
20/* -------------------------------------------------------------------- */
24blender::gpu::Batch *GPU_batch_tris_from_poly_2d_encoded(const uchar *polys_flat,
25 uint polys_flat_len,
26 const rctf *rect)
27{
28 const uchar(*polys)[2] = static_cast<const uchar(*)[2]>((const void *)polys_flat);
29 const uint polys_len = polys_flat_len / 2;
30 BLI_assert(polys_flat_len == polys_len * 2);
31
32 /* Over alloc in both cases */
33 float(*verts)[2] = static_cast<float(*)[2]>(MEM_mallocN(sizeof(*verts) * polys_len, __func__));
34 float(*verts_step)[2] = verts;
35 uint(*tris)[3] = static_cast<uint(*)[3]>(MEM_mallocN(sizeof(*tris) * polys_len, __func__));
36 uint(*tris_step)[3] = tris;
37
38 const float range_uchar[2] = {
39 (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
40 (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
41 };
42 const float min_uchar[2] = {
43 (rect ? rect->xmin : -1.0f),
44 (rect ? rect->ymin : -1.0f),
45 };
46
47 uint i_poly = 0;
48 uint i_vert = 0;
49 while (i_poly != polys_len) {
50 for (uint j = 0; j < 2; j++) {
51 verts[i_vert][j] = min_uchar[j] + (float(polys[i_poly][j]) * range_uchar[j]);
52 }
53 i_vert++;
54 i_poly++;
55 if (polys[i_poly - 1][0] == polys[i_poly][0] && polys[i_poly - 1][1] == polys[i_poly][1]) {
56 const uint verts_step_len = (&verts[i_vert]) - verts_step;
57 BLI_assert(verts_step_len >= 3);
58 const uint tris_len = (verts_step_len - 2);
59 BLI_polyfill_calc(verts_step, verts_step_len, -1, tris_step);
60 /* offset indices */
61 if (verts_step != verts) {
62 uint *t = tris_step[0];
63 const uint offset = (verts_step - verts);
64 uint tot = tris_len * 3;
65 while (tot--) {
66 *t += offset;
67 t++;
68 }
69 BLI_assert(t == tris_step[tris_len]);
70 }
71 verts_step += verts_step_len;
72 tris_step += tris_len;
73 i_poly++;
74 /* ignore the duplicate point */
75 }
76 }
77
78 /* We have vertices and tris, make a batch from this. */
79 static GPUVertFormat format = {0};
80 static struct {
81 uint pos;
82 } attr_id;
83 if (format.attr_len == 0) {
85 }
86
87 const uint verts_len = (verts_step - verts);
88 const uint tris_len = (tris_step - tris);
90 GPU_vertbuf_data_alloc(*vbo, verts_len);
91
92 GPUVertBufRaw pos_step;
93 GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
94
95 for (uint i = 0; i < verts_len; i++) {
96 copy_v2_v2(static_cast<float *>(GPU_vertbuf_raw_step(&pos_step)), verts[i]);
97 }
98
100 GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, tris_len, verts_len);
101 for (uint i = 0; i < tris_len; i++) {
102 GPU_indexbuf_add_tri_verts(&elb, UNPACK3(tris[i]));
103 }
105
106 MEM_freeN(tris);
108
109 return GPU_batch_create_ex(
111}
112
113blender::gpu::Batch *GPU_batch_wire_from_poly_2d_encoded(const uchar *polys_flat,
114 uint polys_flat_len,
115 const rctf *rect)
116{
117 const uchar(*polys)[2] = static_cast<const uchar(*)[2]>((const void *)polys_flat);
118 const uint polys_len = polys_flat_len / 2;
119 BLI_assert(polys_flat_len == polys_len * 2);
120
121 /* Over alloc */
122 /* Lines are pairs of (x, y) byte locations packed into an int32_t. */
123 int32_t *lines = static_cast<int32_t *>(MEM_mallocN(sizeof(*lines) * polys_len, __func__));
124 int32_t *lines_step = lines;
125
126 const float range_uchar[2] = {
127 (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
128 (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
129 };
130 const float min_uchar[2] = {
131 (rect ? rect->xmin : -1.0f),
132 (rect ? rect->ymin : -1.0f),
133 };
134
135 uint i_poly_prev = 0;
136 uint i_poly = 0;
137 while (i_poly != polys_len) {
138 i_poly++;
139 if (polys[i_poly - 1][0] == polys[i_poly][0] && polys[i_poly - 1][1] == polys[i_poly][1]) {
140 const uchar(*polys_step)[2] = polys + i_poly_prev;
141 const uint polys_step_len = i_poly - i_poly_prev;
142 BLI_assert(polys_step_len >= 2);
143 for (uint i_prev = polys_step_len - 1, i = 0; i < polys_step_len; i_prev = i++) {
144 union {
145 uint16_t as_u16[2];
146 uint32_t as_u32;
147 } data;
148 data.as_u16[0] = *((const uint16_t *)polys_step[i_prev]);
149 data.as_u16[1] = *((const uint16_t *)polys_step[i]);
150 if (data.as_u16[0] > data.as_u16[1]) {
151 std::swap(data.as_u16[0], data.as_u16[1]);
152 }
153 *lines_step = data.as_u32;
154 lines_step++;
155 }
156 i_poly++;
157 i_poly_prev = i_poly;
158 /* ignore the duplicate point */
159 }
160 }
161
162 uint lines_len = lines_step - lines;
163
164 /* Hide Lines (we could make optional) */
165 {
166 qsort(lines, lines_len, sizeof(int32_t), BLI_sortutil_cmp_int);
167 lines_step = lines;
168 for (uint i_prev = 0, i = 1; i < lines_len; i_prev = i++) {
169 if (lines[i] != lines[i_prev]) {
170 *lines_step++ = lines[i_prev];
171 }
172 else {
173 i++;
174 }
175 }
176 *lines_step++ = lines[lines_len - 1];
177 lines_len = lines_step - lines;
178 }
179
180 /* We have vertices and tris, make a batch from this. */
181 static GPUVertFormat format = {0};
182 static struct {
183 uint pos;
184 } attr_id;
185 if (format.attr_len == 0) {
187 }
188
190 const uint vbo_len_capacity = lines_len * 2;
191 GPU_vertbuf_data_alloc(*vbo, vbo_len_capacity);
192
193 GPUVertBufRaw pos_step;
194 GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
195
196 for (uint i = 0; i < lines_len; i++) {
197 union {
198 uint8_t as_u8_pair[2][2];
199 uint32_t as_u32;
200 } data;
201 data.as_u32 = lines[i];
202 for (uint k = 0; k < 2; k++) {
203 float *pos_v2 = static_cast<float *>(GPU_vertbuf_raw_step(&pos_step));
204 for (uint j = 0; j < 2; j++) {
205 pos_v2[j] = min_uchar[j] + (float(data.as_u8_pair[k][j]) * range_uchar[j]);
206 }
207 }
208 }
209 BLI_assert(vbo_len_capacity == GPU_vertbuf_raw_used(&pos_step));
210 MEM_freeN(lines);
212}
213
#define BLI_assert(a)
Definition BLI_assert.h:50
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])
int BLI_sortutil_cmp_int(const void *a_, const void *b_)
Definition sort_utils.c:53
unsigned char uchar
unsigned int uint
#define UNPACK3(a)
blender::gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, eGPUBatchFlag owns_flag)
Definition gpu_batch.cc:56
@ GPU_BATCH_OWNS_INDEX
Definition GPU_batch.hh:51
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:42
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
blender::gpu::IndexBuf * GPU_indexbuf_build(GPUIndexBufBuilder *)
void GPU_indexbuf_add_tri_verts(GPUIndexBufBuilder *, uint v1, uint v2, uint v3)
@ GPU_PRIM_LINES
@ GPU_PRIM_TRIS
void GPU_vertbuf_attr_get_raw_data(blender::gpu::VertBuf *, uint a_idx, GPUVertBufRaw *access)
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
#define GPU_vertbuf_create_with_format(format)
GPU_INLINE uint GPU_vertbuf_raw_used(const GPUVertBufRaw *a)
void GPU_vertbuf_data_alloc(blender::gpu::VertBuf &verts, uint v_len)
@ GPU_FETCH_FLOAT
uint GPU_vertformat_attr_add(GPUVertFormat *, const char *name, GPUVertCompType, uint comp_len, GPUVertFetchMode)
@ GPU_COMP_F32
Read Guarded memory(de)allocation.
draw_view in_light_buf[] float
static float verts[][3]
struct @620::@623 attr_id
blender::gpu::Batch * GPU_batch_tris_from_poly_2d_encoded(const uchar *polys_flat, uint polys_flat_len, const rctf *rect)
blender::gpu::Batch * GPU_batch_wire_from_poly_2d_encoded(const uchar *polys_flat, uint polys_flat_len, const rctf *rect)
format
void *(* MEM_mallocN)(size_t len, const char *str)
Definition mallocn.cc:44
void MEM_freeN(void *vmemh)
Definition mallocn.cc:105
unsigned short uint16_t
Definition stdint.h:79
unsigned int uint32_t
Definition stdint.h:80
signed int int32_t
Definition stdint.h:77
unsigned char uint8_t
Definition stdint.h:78
float xmax
float xmin
float ymax
float ymin