Blender  V3.3
gpu_batch_utils.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include "MEM_guardedalloc.h"
8 
9 #include "BLI_math.h"
10 #include "BLI_polyfill_2d.h"
11 #include "BLI_sort_utils.h"
12 #include "BLI_utildefines.h"
13 
14 #include "GPU_batch.h"
15 #include "GPU_batch_utils.h" /* own include */
16 
17 /* -------------------------------------------------------------------- */
22  uint polys_flat_len,
23  const rctf *rect)
24 {
25  const uchar(*polys)[2] = (const void *)polys_flat;
26  const uint polys_len = polys_flat_len / 2;
27  BLI_assert(polys_flat_len == polys_len * 2);
28 
29  /* Over alloc in both cases */
30  float(*verts)[2] = MEM_mallocN(sizeof(*verts) * polys_len, __func__);
31  float(*verts_step)[2] = verts;
32  uint(*tris)[3] = MEM_mallocN(sizeof(*tris) * polys_len, __func__);
33  uint(*tris_step)[3] = tris;
34 
35  const float range_uchar[2] = {
36  (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
37  (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
38  };
39  const float min_uchar[2] = {
40  (rect ? rect->xmin : -1.0f),
41  (rect ? rect->ymin : -1.0f),
42  };
43 
44  uint i_poly = 0;
45  uint i_vert = 0;
46  while (i_poly != polys_len) {
47  for (uint j = 0; j < 2; j++) {
48  verts[i_vert][j] = min_uchar[j] + ((float)polys[i_poly][j] * range_uchar[j]);
49  }
50  i_vert++;
51  i_poly++;
52  if (polys[i_poly - 1][0] == polys[i_poly][0] && polys[i_poly - 1][1] == polys[i_poly][1]) {
53  const uint verts_step_len = (&verts[i_vert]) - verts_step;
54  BLI_assert(verts_step_len >= 3);
55  const uint tris_len = (verts_step_len - 2);
56  BLI_polyfill_calc(verts_step, verts_step_len, -1, tris_step);
57  /* offset indices */
58  if (verts_step != verts) {
59  uint *t = tris_step[0];
60  const uint offset = (verts_step - verts);
61  uint tot = tris_len * 3;
62  while (tot--) {
63  *t += offset;
64  t++;
65  }
66  BLI_assert(t == tris_step[tris_len]);
67  }
68  verts_step += verts_step_len;
69  tris_step += tris_len;
70  i_poly++;
71  /* ignore the duplicate point */
72  }
73  }
74 
75  /* We have vertices and tris, make a batch from this. */
76  static GPUVertFormat format = {0};
77  static struct {
78  uint pos;
79  } attr_id;
80  if (format.attr_len == 0) {
82  }
83 
84  const uint verts_len = (verts_step - verts);
85  const uint tris_len = (tris_step - tris);
87  GPU_vertbuf_data_alloc(vbo, verts_len);
88 
89  GPUVertBufRaw pos_step;
90  GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
91 
92  for (uint i = 0; i < verts_len; i++) {
93  copy_v2_v2(GPU_vertbuf_raw_step(&pos_step), verts[i]);
94  }
95 
97  GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, tris_len, verts_len);
98  for (uint i = 0; i < tris_len; i++) {
99  GPU_indexbuf_add_tri_verts(&elb, UNPACK3(tris[i]));
100  }
101  GPUIndexBuf *indexbuf = GPU_indexbuf_build(&elb);
102 
103  MEM_freeN(tris);
104  MEM_freeN(verts);
105 
106  return GPU_batch_create_ex(
108 }
109 
111  uint polys_flat_len,
112  const rctf *rect)
113 {
114  const uchar(*polys)[2] = (const void *)polys_flat;
115  const uint polys_len = polys_flat_len / 2;
116  BLI_assert(polys_flat_len == polys_len * 2);
117 
118  /* Over alloc */
119  /* Lines are pairs of (x, y) byte locations packed into an int32_t. */
120  int32_t *lines = MEM_mallocN(sizeof(*lines) * polys_len, __func__);
121  int32_t *lines_step = lines;
122 
123  const float range_uchar[2] = {
124  (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
125  (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
126  };
127  const float min_uchar[2] = {
128  (rect ? rect->xmin : -1.0f),
129  (rect ? rect->ymin : -1.0f),
130  };
131 
132  uint i_poly_prev = 0;
133  uint i_poly = 0;
134  while (i_poly != polys_len) {
135  i_poly++;
136  if (polys[i_poly - 1][0] == polys[i_poly][0] && polys[i_poly - 1][1] == polys[i_poly][1]) {
137  const uchar(*polys_step)[2] = polys + i_poly_prev;
138  const uint polys_step_len = i_poly - i_poly_prev;
139  BLI_assert(polys_step_len >= 2);
140  for (uint i_prev = polys_step_len - 1, i = 0; i < polys_step_len; i_prev = i++) {
141  union {
142  uint16_t as_u16[2];
143  uint32_t as_u32;
144  } data;
145  data.as_u16[0] = *((const uint16_t *)polys_step[i_prev]);
146  data.as_u16[1] = *((const uint16_t *)polys_step[i]);
147  if (data.as_u16[0] > data.as_u16[1]) {
148  SWAP(uint16_t, data.as_u16[0], data.as_u16[1]);
149  }
150  *lines_step = data.as_u32;
151  lines_step++;
152  }
153  i_poly++;
154  i_poly_prev = i_poly;
155  /* ignore the duplicate point */
156  }
157  }
158 
159  uint lines_len = lines_step - lines;
160 
161  /* Hide Lines (we could make optional) */
162  {
163  qsort(lines, lines_len, sizeof(int32_t), BLI_sortutil_cmp_int);
164  lines_step = lines;
165  for (uint i_prev = 0, i = 1; i < lines_len; i_prev = i++) {
166  if (lines[i] != lines[i_prev]) {
167  *lines_step++ = lines[i_prev];
168  }
169  else {
170  i++;
171  }
172  }
173  *lines_step++ = lines[lines_len - 1];
174  lines_len = lines_step - lines;
175  }
176 
177  /* We have vertices and tris, make a batch from this. */
178  static GPUVertFormat format = {0};
179  static struct {
180  uint pos;
181  } attr_id;
182  if (format.attr_len == 0) {
184  }
185 
187  const uint vbo_len_capacity = lines_len * 2;
188  GPU_vertbuf_data_alloc(vbo, vbo_len_capacity);
189 
190  GPUVertBufRaw pos_step;
191  GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
192 
193  for (uint i = 0; i < lines_len; i++) {
194  union {
195  uint8_t as_u8_pair[2][2];
196  uint32_t as_u32;
197  } data;
198  data.as_u32 = lines[i];
199  for (uint k = 0; k < 2; k++) {
200  float *pos_v2 = GPU_vertbuf_raw_step(&pos_step);
201  for (uint j = 0; j < 2; j++) {
202  pos_v2[j] = min_uchar[j] + ((float)data.as_u8_pair[k][j] * range_uchar[j]);
203  }
204  }
205  }
206  BLI_assert(vbo_len_capacity == GPU_vertbuf_raw_used(&pos_step));
207  MEM_freeN(lines);
209 }
210 
typedef float(TangentPoint)[2]
#define BLI_assert(a)
Definition: BLI_assert.h:46
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])
Definition: polyfill_2d.c:875
int BLI_sortutil_cmp_int(const void *a_, const void *b_)
Definition: sort_utils.c:52
unsigned char uchar
Definition: BLI_sys_types.h:70
unsigned int uint
Definition: BLI_sys_types.h:67
#define SWAP(type, a, b)
#define UNPACK3(a)
GPUBatch
Definition: GPU_batch.h:78
GPUBatch * GPU_batch_create_ex(GPUPrimType prim, GPUVertBuf *vert, GPUIndexBuf *elem, eGPUBatchFlag owns_flag)
Definition: gpu_batch.cc:43
@ GPU_BATCH_OWNS_INDEX
Definition: GPU_batch.h:39
@ GPU_BATCH_OWNS_VBO
Definition: GPU_batch.h:30
struct GPUIndexBuf GPUIndexBuf
void GPU_indexbuf_init(GPUIndexBufBuilder *, GPUPrimType, uint prim_len, uint vertex_len)
GPUIndexBuf * GPU_indexbuf_build(GPUIndexBufBuilder *)
void GPU_indexbuf_add_tri_verts(GPUIndexBufBuilder *, uint v1, uint v2, uint v3)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
@ GPU_PRIM_LINES
Definition: GPU_primitive.h:20
@ GPU_PRIM_TRIS
Definition: GPU_primitive.h:21
#define GPU_vertbuf_create_with_format(format)
struct GPUVertBuf GPUVertBuf
GPU_INLINE uint GPU_vertbuf_raw_used(GPUVertBufRaw *a)
void GPU_vertbuf_data_alloc(GPUVertBuf *, uint v_len)
GPU_INLINE void * GPU_vertbuf_raw_step(GPUVertBufRaw *a)
void GPU_vertbuf_attr_get_raw_data(GPUVertBuf *, uint a_idx, GPUVertBufRaw *access)
@ 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.
static float verts[][3]
uint pos
struct @653::@656 attr_id
GPUBatch * GPU_batch_tris_from_poly_2d_encoded(const uchar *polys_flat, uint polys_flat_len, const rctf *rect)
GPUBatch * GPU_batch_wire_from_poly_2d_encoded(const uchar *polys_flat, uint polys_flat_len, const rctf *rect)
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
format
Definition: logImageCore.h:38
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:33
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
Definition: DNA_vec_types.h:69
float xmin
Definition: DNA_vec_types.h:69
float ymax
Definition: DNA_vec_types.h:70
float ymin
Definition: DNA_vec_types.h:70