Blender  V3.3
GPU_batch.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2016 by Mike Erwin. All rights reserved. */
3 
11 #pragma once
12 
13 #include "BLI_utildefines.h"
14 
15 #include "GPU_index_buffer.h"
16 #include "GPU_shader.h"
17 #include "GPU_uniform_buffer.h"
18 #include "GPU_vertex_buffer.h"
19 
20 #define GPU_BATCH_VBO_MAX_LEN 16
21 #define GPU_BATCH_INST_VBO_MAX_LEN 2
22 #define GPU_BATCH_VAO_STATIC_LEN 3
23 #define GPU_BATCH_VAO_DYN_ALLOC_COUNT 16
24 
25 typedef enum eGPUBatchFlag {
28 
30  GPU_BATCH_OWNS_VBO = (1 << 0),
40 
42  GPU_BATCH_INIT = (1 << 26),
44  GPU_BATCH_BUILDING = (1 << 26),
46  GPU_BATCH_DIRTY = (1 << 27),
48 
49 #define GPU_BATCH_OWNS_NONE GPU_BATCH_INVALID
50 
52  "eGPUBatchFlag: Error: status flags are shadowed by the ownership bits!")
53 
55 
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59 
65 typedef struct GPUBatch {
71  GPUIndexBuf *elem;
73  eGPUBatchFlag flag;
75  GPUPrimType prim_type;
77  struct GPUShader *shader;
79 
82  GPUVertBuf *vert,
83  GPUIndexBuf *elem,
84  eGPUBatchFlag owns_flag);
86  GPUPrimType prim,
87  GPUVertBuf *vert,
88  GPUIndexBuf *elem,
89  eGPUBatchFlag owns_flag);
93 void GPU_batch_copy(GPUBatch *batch_dst, GPUBatch *batch_src);
94 
95 #define GPU_batch_create(prim, verts, elem) GPU_batch_create_ex(prim, verts, elem, 0)
96 #define GPU_batch_init(batch, prim, verts, elem) GPU_batch_init_ex(batch, prim, verts, elem, 0)
97 
101 void GPU_batch_clear(GPUBatch *);
102 
107 
111 void GPU_batch_instbuf_set(GPUBatch *, GPUVertBuf *, bool own_vbo); /* Instancing */
115 void GPU_batch_elembuf_set(GPUBatch *batch, GPUIndexBuf *elem, bool own_ibo);
116 
117 int GPU_batch_instbuf_add_ex(GPUBatch *, GPUVertBuf *, bool own_vbo);
121 int GPU_batch_vertbuf_add_ex(GPUBatch *, GPUVertBuf *, bool own_vbo);
123 
124 #define GPU_batch_vertbuf_add(batch, verts) GPU_batch_vertbuf_add_ex(batch, verts, false)
125 
136  eGPUBuiltinShader shader_id,
137  eGPUShaderConfig sh_cfg);
138 
139 /* Will only work after setting the batch program. */
140 /* TODO(fclem): These need to be replaced by GPU_shader_uniform_* with explicit shader. */
141 
142 #define GPU_batch_uniform_1i(batch, name, x) GPU_shader_uniform_1i((batch)->shader, name, x);
143 #define GPU_batch_uniform_1b(batch, name, x) GPU_shader_uniform_1b((batch)->shader, name, x);
144 #define GPU_batch_uniform_1f(batch, name, x) GPU_shader_uniform_1f((batch)->shader, name, x);
145 #define GPU_batch_uniform_2f(batch, name, x, y) GPU_shader_uniform_2f((batch)->shader, name, x, y);
146 #define GPU_batch_uniform_3f(batch, name, x, y, z) \
147  GPU_shader_uniform_3f((batch)->shader, name, x, y, z);
148 #define GPU_batch_uniform_4f(batch, name, x, y, z, w) \
149  GPU_shader_uniform_4f((batch)->shader, name, x, y, z, w);
150 #define GPU_batch_uniform_2fv(batch, name, val) GPU_shader_uniform_2fv((batch)->shader, name, val);
151 #define GPU_batch_uniform_3fv(batch, name, val) GPU_shader_uniform_3fv((batch)->shader, name, val);
152 #define GPU_batch_uniform_4fv(batch, name, val) GPU_shader_uniform_4fv((batch)->shader, name, val);
153 #define GPU_batch_uniform_2fv_array(batch, name, len, val) \
154  GPU_shader_uniform_2fv_array((batch)->shader, name, len, val);
155 #define GPU_batch_uniform_4fv_array(batch, name, len, val) \
156  GPU_shader_uniform_4fv_array((batch)->shader, name, len, val);
157 #define GPU_batch_uniform_mat4(batch, name, val) \
158  GPU_shader_uniform_mat4((batch)->shader, name, val);
159 #define GPU_batch_uniformbuf_bind(batch, name, ubo) \
160  GPU_uniformbuf_bind(ubo, GPU_shader_get_uniform_block_binding((batch)->shader, name));
161 #define GPU_batch_texture_bind(batch, name, tex) \
162  GPU_texture_bind(tex, GPU_shader_get_texture_binding((batch)->shader, name));
163 
165 void GPU_batch_draw_range(GPUBatch *batch, int v_first, int v_count);
169 void GPU_batch_draw_instanced(GPUBatch *batch, int i_count);
170 
174 void GPU_batch_draw_advanced(GPUBatch *, int v_first, int v_count, int i_first, int i_count);
175 
176 #if 0 /* future plans */
177 
178 /* Can multiple batches share a #GPUVertBuf? Use ref count? */
179 
180 /* We often need a batch with its own data, to be created and discarded together. */
181 /* WithOwn variants reduce number of system allocations. */
182 
183 typedef struct BatchWithOwnVertexBuffer {
184  GPUBatch batch;
185  GPUVertBuf verts; /* link batch.verts to this */
186 } BatchWithOwnVertexBuffer;
187 
188 typedef struct BatchWithOwnElementList {
189  GPUBatch batch;
190  GPUIndexBuf elem; /* link batch.elem to this */
191 } BatchWithOwnElementList;
192 
193 typedef struct BatchWithOwnVertexBufferAndElementList {
194  GPUBatch batch;
195  GPUIndexBuf elem; /* link batch.elem to this */
196  GPUVertBuf verts; /* link batch.verts to this */
197 } BatchWithOwnVertexBufferAndElementList;
198 
199 GPUBatch *create_BatchWithOwnVertexBuffer(GPUPrimType, GPUVertFormat *, uint v_len, GPUIndexBuf *);
200 GPUBatch *create_BatchWithOwnElementList(GPUPrimType, GPUVertBuf *, uint prim_len);
201 GPUBatch *create_BatchWithOwnVertexBufferAndElementList(GPUPrimType,
202  GPUVertFormat *,
203  uint v_len,
204  uint prim_len);
205 /* verts: shared, own */
206 /* elem: none, shared, own */
207 GPUBatch *create_BatchInGeneral(GPUPrimType, VertexBufferStuff, ElementListStuff);
208 
209 #endif /* future plans */
210 
211 void gpu_batch_init(void);
212 void gpu_batch_exit(void);
213 
214 /* Macros */
215 
216 #define GPU_BATCH_DISCARD_SAFE(batch) \
217  do { \
218  if (batch != NULL) { \
219  GPU_batch_discard(batch); \
220  batch = NULL; \
221  } \
222  } while (0)
223 
224 #define GPU_BATCH_CLEAR_SAFE(batch) \
225  do { \
226  if (batch != NULL) { \
227  GPU_batch_clear(batch); \
228  memset(batch, 0, sizeof(*(batch))); \
229  } \
230  } while (0)
231 
232 #define GPU_BATCH_DISCARD_ARRAY_SAFE(_batch_array, _len) \
233  do { \
234  if (_batch_array != NULL) { \
235  BLI_assert(_len > 0); \
236  for (int _i = 0; _i < _len; _i++) { \
237  GPU_BATCH_DISCARD_SAFE(_batch_array[_i]); \
238  } \
239  MEM_freeN(_batch_array); \
240  } \
241  } while (0)
242 
243 #ifdef __cplusplus
244 }
245 #endif
unsigned int uint
Definition: BLI_sys_types.h:67
#define ENUM_OPERATORS(_type, _max)
GPUBatch * GPU_batch_calloc(void)
Definition: gpu_batch.cc:36
GPUBatch
Definition: GPU_batch.h:78
void GPU_batch_draw_instanced(GPUBatch *batch, int i_count)
Definition: gpu_batch.cc:235
void GPU_batch_copy(GPUBatch *batch_dst, GPUBatch *batch_src)
Definition: gpu_batch.cc:76
BLI_STATIC_ASSERT(GPU_BATCH_OWNS_INDEX< GPU_BATCH_INIT, "eGPUBatchFlag: Error: status flags are shadowed by the ownership bits!") typedef struct GPUBatch
Definition: GPU_batch.h:51
void GPU_batch_set_shader(GPUBatch *batch, GPUShader *shader)
Definition: gpu_batch.cc:211
void GPU_batch_init_ex(GPUBatch *batch, GPUPrimType prim, GPUVertBuf *vert, GPUIndexBuf *elem, eGPUBatchFlag owns_flag)
Definition: gpu_batch.cc:53
void GPU_batch_draw_range(GPUBatch *batch, int v_first, int v_count)
Definition: gpu_batch.cc:229
void gpu_batch_init(void)
Definition: gpu_batch.cc:303
void GPU_batch_elembuf_set(GPUBatch *batch, GPUIndexBuf *elem, bool own_ibo)
Definition: gpu_batch.cc:135
void GPU_batch_discard(GPUBatch *)
Definition: gpu_batch.cc:109
void GPU_batch_program_set_builtin_with_config(GPUBatch *batch, eGPUBuiltinShader shader_id, eGPUShaderConfig sh_cfg)
Definition: gpu_batch.cc:279
void GPU_batch_draw_advanced(GPUBatch *, int v_first, int v_count, int i_first, int i_count)
Definition: gpu_batch.cc:243
int GPU_batch_vertbuf_add_ex(GPUBatch *, GPUVertBuf *, bool own_vbo)
Definition: gpu_batch.cc:171
#define GPU_BATCH_INST_VBO_MAX_LEN
Definition: GPU_batch.h:21
void GPU_batch_program_set_imm_shader(GPUBatch *batch)
Definition: gpu_batch.cc:292
#define GPU_BATCH_VBO_MAX_LEN
Definition: GPU_batch.h:20
void GPU_batch_program_set_builtin(GPUBatch *batch, eGPUBuiltinShader shader_id)
Definition: gpu_batch.cc:287
void GPU_batch_clear(GPUBatch *)
Definition: gpu_batch.cc:87
int GPU_batch_instbuf_add_ex(GPUBatch *, GPUVertBuf *, bool own_vbo)
Definition: gpu_batch.cc:148
bool GPU_batch_vertbuf_has(GPUBatch *, GPUVertBuf *)
Definition: gpu_batch.cc:193
void gpu_batch_exit(void)
Definition: gpu_batch.cc:308
void GPU_batch_instbuf_set(GPUBatch *, GPUVertBuf *, bool own_vbo)
Definition: gpu_batch.cc:122
GPUBatch * GPU_batch_create_ex(GPUPrimType prim, GPUVertBuf *vert, GPUIndexBuf *elem, eGPUBatchFlag owns_flag)
Definition: gpu_batch.cc:43
void GPU_batch_draw(GPUBatch *batch)
Definition: gpu_batch.cc:223
eGPUBatchFlag
Definition: GPU_batch.h:25
@ GPU_BATCH_INVALID
Definition: GPU_batch.h:27
@ GPU_BATCH_DIRTY
Definition: GPU_batch.h:46
@ GPU_BATCH_INIT
Definition: GPU_batch.h:42
@ GPU_BATCH_OWNS_INDEX
Definition: GPU_batch.h:39
@ GPU_BATCH_OWNS_INST_VBO
Definition: GPU_batch.h:34
@ GPU_BATCH_OWNS_INST_VBO_MAX
Definition: GPU_batch.h:35
@ GPU_BATCH_OWNS_VBO
Definition: GPU_batch.h:30
@ GPU_BATCH_OWNS_VBO_ANY
Definition: GPU_batch.h:32
@ GPU_BATCH_OWNS_INST_VBO_ANY
Definition: GPU_batch.h:36
@ GPU_BATCH_OWNS_VBO_MAX
Definition: GPU_batch.h:31
@ GPU_BATCH_BUILDING
Definition: GPU_batch.h:44
struct GPUIndexBuf GPUIndexBuf
GPUPrimType
Definition: GPU_primitive.h:18
struct GPUShader GPUShader
Definition: GPU_shader.h:20
eGPUShaderConfig
Definition: GPU_shader.h:364
eGPUBuiltinShader
Definition: GPU_shader.h:189
struct GPUVertBuf GPUVertBuf
static float verts[][3]
struct @653::@655 batch