Blender  V2.93
gpu_batch_utils.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include "MEM_guardedalloc.h"
22 
23 #include "BLI_math.h"
24 #include "BLI_polyfill_2d.h"
25 #include "BLI_rect.h"
26 #include "BLI_sort_utils.h"
27 #include "BLI_utildefines.h"
28 
29 #include "GPU_batch.h"
30 #include "GPU_batch_utils.h" /* own include */
31 
32 /* -------------------------------------------------------------------- */
46  uint polys_flat_len,
47  const rctf *rect)
48 {
49  const uchar(*polys)[2] = (const void *)polys_flat;
50  const uint polys_len = polys_flat_len / 2;
51  BLI_assert(polys_flat_len == polys_len * 2);
52 
53  /* Over alloc in both cases */
54  float(*verts)[2] = MEM_mallocN(sizeof(*verts) * polys_len, __func__);
55  float(*verts_step)[2] = verts;
56  uint(*tris)[3] = MEM_mallocN(sizeof(*tris) * polys_len, __func__);
57  uint(*tris_step)[3] = tris;
58 
59  const float range_uchar[2] = {
60  (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
61  (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
62  };
63  const float min_uchar[2] = {
64  (rect ? rect->xmin : -1.0f),
65  (rect ? rect->ymin : -1.0f),
66  };
67 
68  uint i_poly = 0;
69  uint i_vert = 0;
70  while (i_poly != polys_len) {
71  for (uint j = 0; j < 2; j++) {
72  verts[i_vert][j] = min_uchar[j] + ((float)polys[i_poly][j] * range_uchar[j]);
73  }
74  i_vert++;
75  i_poly++;
76  if (polys[i_poly - 1][0] == polys[i_poly][0] && polys[i_poly - 1][1] == polys[i_poly][1]) {
77  const uint verts_step_len = (&verts[i_vert]) - verts_step;
78  BLI_assert(verts_step_len >= 3);
79  const uint tris_len = (verts_step_len - 2);
80  BLI_polyfill_calc(verts_step, verts_step_len, -1, tris_step);
81  /* offset indices */
82  if (verts_step != verts) {
83  uint *t = tris_step[0];
84  const uint offset = (verts_step - verts);
85  uint tot = tris_len * 3;
86  while (tot--) {
87  *t += offset;
88  t++;
89  }
90  BLI_assert(t == tris_step[tris_len]);
91  }
92  verts_step += verts_step_len;
93  tris_step += tris_len;
94  i_poly++;
95  /* ignore the duplicate point */
96  }
97  }
98 
99  /* We have vertices and tris, make a batch from this. */
100  static GPUVertFormat format = {0};
101  static struct {
102  uint pos;
103  } attr_id;
104  if (format.attr_len == 0) {
106  }
107 
108  const uint verts_len = (verts_step - verts);
109  const uint tris_len = (tris_step - tris);
111  GPU_vertbuf_data_alloc(vbo, verts_len);
112 
113  GPUVertBufRaw pos_step;
114  GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
115 
116  for (uint i = 0; i < verts_len; i++) {
117  copy_v2_v2(GPU_vertbuf_raw_step(&pos_step), verts[i]);
118  }
119 
120  GPUIndexBufBuilder elb;
121  GPU_indexbuf_init(&elb, GPU_PRIM_TRIS, tris_len, verts_len);
122  for (uint i = 0; i < tris_len; i++) {
123  GPU_indexbuf_add_tri_verts(&elb, UNPACK3(tris[i]));
124  }
125  GPUIndexBuf *indexbuf = GPU_indexbuf_build(&elb);
126 
127  MEM_freeN(tris);
128  MEM_freeN(verts);
129 
130  return GPU_batch_create_ex(
132 }
133 
135  uint polys_flat_len,
136  const rctf *rect)
137 {
138  const uchar(*polys)[2] = (const void *)polys_flat;
139  const uint polys_len = polys_flat_len / 2;
140  BLI_assert(polys_flat_len == polys_len * 2);
141 
142  /* Over alloc */
143  /* Lines are pairs of (x, y) byte locations packed into an int32_t. */
144  int32_t *lines = MEM_mallocN(sizeof(*lines) * polys_len, __func__);
145  int32_t *lines_step = lines;
146 
147  const float range_uchar[2] = {
148  (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
149  (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
150  };
151  const float min_uchar[2] = {
152  (rect ? rect->xmin : -1.0f),
153  (rect ? rect->ymin : -1.0f),
154  };
155 
156  uint i_poly_prev = 0;
157  uint i_poly = 0;
158  while (i_poly != polys_len) {
159  i_poly++;
160  if (polys[i_poly - 1][0] == polys[i_poly][0] && polys[i_poly - 1][1] == polys[i_poly][1]) {
161  const uchar(*polys_step)[2] = polys + i_poly_prev;
162  const uint polys_step_len = i_poly - i_poly_prev;
163  BLI_assert(polys_step_len >= 2);
164  for (uint i_prev = polys_step_len - 1, i = 0; i < polys_step_len; i_prev = i++) {
165  union {
166  uint16_t as_u16[2];
167  uint32_t as_u32;
168  } data;
169  data.as_u16[0] = *((const uint16_t *)polys_step[i_prev]);
170  data.as_u16[1] = *((const uint16_t *)polys_step[i]);
171  if (data.as_u16[0] > data.as_u16[1]) {
172  SWAP(uint16_t, data.as_u16[0], data.as_u16[1]);
173  }
174  *lines_step = data.as_u32;
175  lines_step++;
176  }
177  i_poly++;
178  i_poly_prev = i_poly;
179  /* ignore the duplicate point */
180  }
181  }
182 
183  uint lines_len = lines_step - lines;
184 
185  /* Hide Lines (we could make optional) */
186  {
187  qsort(lines, lines_len, sizeof(int32_t), BLI_sortutil_cmp_int);
188  lines_step = lines;
189  for (uint i_prev = 0, i = 1; i < lines_len; i_prev = i++) {
190  if (lines[i] != lines[i_prev]) {
191  *lines_step++ = lines[i_prev];
192  }
193  else {
194  i++;
195  }
196  }
197  *lines_step++ = lines[lines_len - 1];
198  lines_len = lines_step - lines;
199  }
200 
201  /* We have vertices and tris, make a batch from this. */
202  static GPUVertFormat format = {0};
203  static struct {
204  uint pos;
205  } attr_id;
206  if (format.attr_len == 0) {
208  }
209 
211  const uint vbo_len_capacity = lines_len * 2;
212  GPU_vertbuf_data_alloc(vbo, vbo_len_capacity);
213 
214  GPUVertBufRaw pos_step;
215  GPU_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
216 
217  for (uint i = 0; i < lines_len; i++) {
218  union {
219  uint8_t as_u8_pair[2][2];
220  uint32_t as_u32;
221  } data;
222  data.as_u32 = lines[i];
223  for (uint k = 0; k < 2; k++) {
224  float *pos_v2 = GPU_vertbuf_raw_step(&pos_step);
225  for (uint j = 0; j < 2; j++) {
226  pos_v2[j] = min_uchar[j] + ((float)data.as_u8_pair[k][j] * range_uchar[j]);
227  }
228  }
229  }
230  BLI_assert(vbo_len_capacity == GPU_vertbuf_raw_used(&pos_step));
231  MEM_freeN(lines);
233 }
234 
typedef float(TangentPoint)[2]
#define BLI_assert(a)
Definition: BLI_assert.h:58
MINLINE void copy_v2_v2(float r[2], const float a[2])
void BLI_polyfill_calc(const float(*coords)[2], const unsigned int coords_tot, const int coords_sign, unsigned int(*r_tris)[3])
Definition: polyfill_2d.c:905
int BLI_sortutil_cmp_int(const void *a_, const void *b_)
Definition: sort_utils.c:68
unsigned char uchar
Definition: BLI_sys_types.h:86
unsigned int uint
Definition: BLI_sys_types.h:83
#define SWAP(type, a, b)
#define UNPACK3(a)
GPUBatch
Definition: GPU_batch.h:93
GPUBatch * GPU_batch_create_ex(GPUPrimType prim, GPUVertBuf *vert, GPUIndexBuf *elem, eGPUBatchFlag owns_flag)
Definition: gpu_batch.cc:60
@ GPU_BATCH_OWNS_INDEX
Definition: GPU_batch.h:54
@ GPU_BATCH_OWNS_VBO
Definition: GPU_batch.h:45
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:36
@ GPU_PRIM_TRIS
Definition: GPU_primitive.h:37
#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 @612::@615 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)
format
Definition: logImageCore.h:47
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:41
void *(* MEM_mallocN)(size_t len, const char *str)
Definition: mallocn.c:47
unsigned short uint16_t
Definition: stdint.h:82
unsigned int uint32_t
Definition: stdint.h:83
signed int int32_t
Definition: stdint.h:80
unsigned char uint8_t
Definition: stdint.h:81
float xmax
Definition: DNA_vec_types.h:85
float xmin
Definition: DNA_vec_types.h:85
float ymax
Definition: DNA_vec_types.h:86
float ymin
Definition: DNA_vec_types.h:86