Blender V4.5
GPU_batch.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 by Mike Erwin. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
18
19#pragma once
20
21#include "BLI_index_range.hh"
22#include "BLI_utildefines.h"
23
24#include "GPU_index_buffer.hh"
25#include "GPU_shader.hh"
26#include "GPU_storage_buffer.hh"
27#include "GPU_vertex_buffer.hh"
28
29struct GPUShader;
30
31constexpr static int GPU_BATCH_VBO_MAX_LEN = 16;
32constexpr static int GPU_BATCH_INST_VBO_MAX_LEN = 2;
33constexpr static int GPU_BATCH_VAO_STATIC_LEN = 3;
34constexpr static int GPU_BATCH_VAO_DYN_ALLOC_COUNT = 16;
35
59
60#define GPU_BATCH_OWNS_NONE GPU_BATCH_INVALID
61
63 "eGPUBatchFlag: Error: status flags are shadowed by the ownership bits!")
64
66
67namespace blender::gpu {
68
77class Batch {
78 public:
86 GPUStorageBuf *resource_id_buf;
90 GPUPrimType prim_type;
92 GPUShader *shader;
93
94 virtual ~Batch() = default;
95
96 virtual void draw(int v_first, int v_count, int i_first, int i_count) = 0;
97 virtual void draw_indirect(GPUStorageBuf *indirect_buf, intptr_t offset) = 0;
98 virtual void multi_draw_indirect(GPUStorageBuf *indirect_buf,
99 int count,
100 intptr_t offset,
101 intptr_t stride) = 0;
102
103 uint32_t vertex_count_get() const
104 {
105 if (elem) {
106 return elem_()->index_len_get();
107 }
108 return verts_(0)->vertex_len;
109 }
110
111 /* Convenience casts. */
112 IndexBuf *elem_() const
113 {
114 return elem;
115 }
116 VertBuf *verts_(const int index) const
117 {
118 return verts[index];
119 }
120 VertBuf *inst_(const int index) const
121 {
122 return inst[index];
123 }
124};
125
126} // namespace blender::gpu
127
128/* -------------------------------------------------------------------- */
131
136blender::gpu::Batch *GPU_batch_calloc();
137
141blender::gpu::Batch *GPU_batch_create_ex(GPUPrimType primitive_type,
142 blender::gpu::VertBuf *vertex_buf,
143 blender::gpu::IndexBuf *index_buf,
144 eGPUBatchFlag owns_flag);
148#define GPU_batch_create(primitive_type, vertex_buf, index_buf) \
149 GPU_batch_create_ex(primitive_type, vertex_buf, index_buf, (eGPUBatchFlag)0)
150
156void GPU_batch_init_ex(blender::gpu::Batch *batch,
157 GPUPrimType primitive_type,
158 blender::gpu::VertBuf *vertex_buf,
159 blender::gpu::IndexBuf *index_buf,
160 eGPUBatchFlag owns_flag);
166#define GPU_batch_init(batch, primitive_type, vertex_buf, index_buf) \
167 GPU_batch_init_ex(batch, primitive_type, vertex_buf, index_buf, (eGPUBatchFlag)0)
168
173void GPU_batch_copy(blender::gpu::Batch *batch_dst, blender::gpu::Batch *batch_src);
174
176
177/* -------------------------------------------------------------------- */
180
186void GPU_batch_clear(blender::gpu::Batch *batch);
187
188void GPU_batch_zero(blender::gpu::Batch *batch);
189
190#define GPU_BATCH_CLEAR_SAFE(batch) \
191 do { \
192 if (batch != nullptr) { \
193 GPU_batch_clear(batch); \
194 GPU_batch_zero(batch); \
195 } \
196 } while (0)
197
202void GPU_batch_discard(blender::gpu::Batch *batch);
203
204#define GPU_BATCH_DISCARD_SAFE(batch) \
205 do { \
206 if (batch != nullptr) { \
207 GPU_batch_discard(batch); \
208 batch = nullptr; \
209 } \
210 } while (0)
211
213
214/* -------------------------------------------------------------------- */
217
222int GPU_batch_vertbuf_add(blender::gpu::Batch *batch,
223 blender::gpu::VertBuf *vertex_buf,
224 bool own_vbo);
225
230int GPU_batch_instbuf_add(blender::gpu::Batch *batch,
231 blender::gpu::VertBuf *vertex_buf,
232 bool own_vbo);
233
238void GPU_batch_instbuf_set(blender::gpu::Batch *batch,
239 blender::gpu::VertBuf *vertex_buf,
240 bool own_vbo);
241
246void GPU_batch_elembuf_set(blender::gpu::Batch *batch,
247 blender::gpu::IndexBuf *index_buf,
248 bool own_ibo);
249
254bool GPU_batch_vertbuf_has(const blender::gpu::Batch *batch,
255 const blender::gpu::VertBuf *vertex_buf);
256
262void GPU_batch_resource_id_buf_set(blender::gpu::Batch *batch, GPUStorageBuf *resource_id_buf);
263
265
266/* -------------------------------------------------------------------- */
273
278/* TODO(fclem): These should be removed and replaced by `GPU_shader_bind()`. */
280 blender::gpu::Batch *batch,
281 GPUShader *shader,
282 const blender::gpu::shader::SpecializationConstants *constants_state = nullptr);
283void GPU_batch_program_set_builtin(blender::gpu::Batch *batch, eGPUBuiltinShader shader_id);
285 eGPUBuiltinShader shader_id,
286 eGPUShaderConfig sh_cfg);
293void GPU_batch_program_set_imm_shader(blender::gpu::Batch *batch);
294
298/* TODO(fclem): These need to be replaced by GPU_shader_uniform_* with explicit shader. */
299#define GPU_batch_uniform_1i(batch, name, x) GPU_shader_uniform_1i((batch)->shader, name, x);
300#define GPU_batch_uniform_1b(batch, name, x) GPU_shader_uniform_1b((batch)->shader, name, x);
301#define GPU_batch_uniform_1f(batch, name, x) GPU_shader_uniform_1f((batch)->shader, name, x);
302#define GPU_batch_uniform_2f(batch, name, x, y) GPU_shader_uniform_2f((batch)->shader, name, x, y);
303#define GPU_batch_uniform_3f(batch, name, x, y, z) \
304 GPU_shader_uniform_3f((batch)->shader, name, x, y, z);
305#define GPU_batch_uniform_4f(batch, name, x, y, z, w) \
306 GPU_shader_uniform_4f((batch)->shader, name, x, y, z, w);
307#define GPU_batch_uniform_2fv(batch, name, val) GPU_shader_uniform_2fv((batch)->shader, name, val);
308#define GPU_batch_uniform_3fv(batch, name, val) GPU_shader_uniform_3fv((batch)->shader, name, val);
309#define GPU_batch_uniform_4fv(batch, name, val) GPU_shader_uniform_4fv((batch)->shader, name, val);
310#define GPU_batch_uniform_2fv_array(batch, name, len, val) \
311 GPU_shader_uniform_2fv_array((batch)->shader, name, len, val);
312#define GPU_batch_uniform_4fv_array(batch, name, len, val) \
313 GPU_shader_uniform_4fv_array((batch)->shader, name, len, val);
314#define GPU_batch_uniform_mat4(batch, name, val) \
315 GPU_shader_uniform_mat4((batch)->shader, name, val);
316#define GPU_batch_uniformbuf_bind(batch, name, ubo) \
317 GPU_uniformbuf_bind(ubo, GPU_shader_get_ubo_binding((batch)->shader, name));
318#define GPU_batch_texture_bind(batch, name, tex) \
319 GPU_texture_bind(tex, GPU_shader_get_sampler_binding((batch)->shader, name));
320
325 blender::gpu::Batch *batch,
326 GPUShader *shader,
327 const blender::gpu::shader::SpecializationConstants *constants = nullptr);
328
330
331/* -------------------------------------------------------------------- */
334
339void GPU_batch_draw(blender::gpu::Batch *batch);
340
352void GPU_batch_draw_range(blender::gpu::Batch *batch, int vertex_first, int vertex_count);
353
366void GPU_batch_draw_instance_range(blender::gpu::Batch *batch,
367 int instance_first,
368 int instance_count);
369
381void GPU_batch_draw_advanced(blender::gpu::Batch *batch,
382 int vertex_first,
383 int vertex_count,
384 int instance_first,
385 int instance_count);
386
398void GPU_batch_draw_indirect(blender::gpu::Batch *batch,
399 GPUStorageBuf *indirect_buf,
400 intptr_t offset);
401
414void GPU_batch_multi_draw_indirect(blender::gpu::Batch *batch,
415 GPUStorageBuf *indirect_buf,
416 int count,
417 intptr_t offset,
418 intptr_t stride);
419
424void GPU_batch_draw_parameter_get(blender::gpu::Batch *batch,
425 int *r_vertex_count,
426 int *r_vertex_first,
427 int *r_base_index,
428 int *r_instance_count);
429
434 GPUPrimType output_prim_type,
435 int vertex_count,
436 int vertex_first,
437 int output_primitive_cout);
438
440
441/* -------------------------------------------------------------------- */
448
453blender::gpu::Batch *GPU_batch_procedural_points_get();
454
459blender::gpu::Batch *GPU_batch_procedural_lines_get();
460
465blender::gpu::Batch *GPU_batch_procedural_triangles_get();
466
471blender::gpu::Batch *GPU_batch_procedural_triangle_strips_get();
472
474
475/* -------------------------------------------------------------------- */
478
479void gpu_batch_init();
480void gpu_batch_exit();
481
#define BLI_STATIC_ASSERT(a, msg)
Definition BLI_assert.h:83
#define ENUM_OPERATORS(_type, _max)
void GPU_batch_elembuf_set(blender::gpu::Batch *batch, blender::gpu::IndexBuf *index_buf, bool own_ibo)
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:51
static constexpr int GPU_BATCH_VBO_MAX_LEN
Definition GPU_batch.hh:31
void GPU_batch_init_ex(blender::gpu::Batch *batch, GPUPrimType primitive_type, blender::gpu::VertBuf *vertex_buf, blender::gpu::IndexBuf *index_buf, eGPUBatchFlag owns_flag)
blender::gpu::Batch * GPU_batch_procedural_triangle_strips_get()
Definition gpu_batch.cc:548
void GPU_batch_discard(blender::gpu::Batch *batch)
void GPU_batch_zero(blender::gpu::Batch *batch)
blender::gpu::Batch * GPU_batch_procedural_lines_get()
Definition gpu_batch.cc:538
void GPU_batch_draw_advanced(blender::gpu::Batch *batch, int vertex_first, int vertex_count, int instance_first, int instance_count)
static constexpr int GPU_BATCH_INST_VBO_MAX_LEN
Definition GPU_batch.hh:32
blender::IndexRange GPU_batch_draw_expanded_parameter_get(GPUPrimType input_prim_type, GPUPrimType output_prim_type, int vertex_count, int vertex_first, int output_primitive_cout)
Definition gpu_batch.cc:358
void GPU_batch_instbuf_set(blender::gpu::Batch *batch, blender::gpu::VertBuf *vertex_buf, bool own_vbo)
void GPU_batch_draw_instance_range(blender::gpu::Batch *batch, int instance_first, int instance_count)
void GPU_batch_draw_indirect(blender::gpu::Batch *batch, GPUStorageBuf *indirect_buf, intptr_t offset)
static constexpr int GPU_BATCH_VAO_STATIC_LEN
Definition GPU_batch.hh:33
void GPU_batch_resource_id_buf_set(blender::gpu::Batch *batch, GPUStorageBuf *resource_id_buf)
static constexpr int GPU_BATCH_VAO_DYN_ALLOC_COUNT
Definition GPU_batch.hh:34
blender::gpu::Batch * GPU_batch_procedural_points_get()
Definition gpu_batch.cc:533
void GPU_batch_program_set_imm_shader(blender::gpu::Batch *batch)
int GPU_batch_instbuf_add(blender::gpu::Batch *batch, blender::gpu::VertBuf *vertex_buf, bool own_vbo)
void GPU_batch_clear(blender::gpu::Batch *batch)
int GPU_batch_vertbuf_add(blender::gpu::Batch *batch, blender::gpu::VertBuf *vertex_buf, bool own_vbo)
void GPU_batch_bind_as_resources(blender::gpu::Batch *batch, GPUShader *shader, const blender::gpu::shader::SpecializationConstants *constants=nullptr)
blender::gpu::Batch * GPU_batch_procedural_triangles_get()
Definition gpu_batch.cc:543
void GPU_batch_multi_draw_indirect(blender::gpu::Batch *batch, GPUStorageBuf *indirect_buf, int count, intptr_t offset, intptr_t stride)
void gpu_batch_init()
Definition gpu_batch.cc:559
void GPU_batch_program_set_builtin(blender::gpu::Batch *batch, eGPUBuiltinShader shader_id)
void GPU_batch_draw(blender::gpu::Batch *batch)
void GPU_batch_set_shader(blender::gpu::Batch *batch, GPUShader *shader, const blender::gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_batch_copy(blender::gpu::Batch *batch_dst, blender::gpu::Batch *batch_src)
void GPU_batch_program_set_builtin_with_config(blender::gpu::Batch *batch, eGPUBuiltinShader shader_id, eGPUShaderConfig sh_cfg)
void GPU_batch_draw_range(blender::gpu::Batch *batch, int vertex_first, int vertex_count)
bool GPU_batch_vertbuf_has(const blender::gpu::Batch *batch, const blender::gpu::VertBuf *vertex_buf)
eGPUBatchFlag
Definition GPU_batch.hh:36
@ GPU_BATCH_INVALID
Definition GPU_batch.hh:38
@ GPU_BATCH_DIRTY
Definition GPU_batch.hh:57
@ GPU_BATCH_INIT
Definition GPU_batch.hh:53
@ GPU_BATCH_OWNS_INDEX
Definition GPU_batch.hh:50
@ GPU_BATCH_OWNS_INST_VBO
Definition GPU_batch.hh:45
@ GPU_BATCH_OWNS_INST_VBO_MAX
Definition GPU_batch.hh:46
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:41
@ GPU_BATCH_OWNS_VBO_ANY
Definition GPU_batch.hh:43
@ GPU_BATCH_OWNS_INST_VBO_ANY
Definition GPU_batch.hh:47
@ GPU_BATCH_OWNS_VBO_MAX
Definition GPU_batch.hh:42
@ GPU_BATCH_BUILDING
Definition GPU_batch.hh:55
void GPU_batch_draw_parameter_get(blender::gpu::Batch *batch, int *r_vertex_count, int *r_vertex_first, int *r_base_index, int *r_instance_count)
void gpu_batch_exit()
Definition gpu_batch.cc:564
GPUPrimType
eGPUBuiltinShader
static float verts[][3]
Batch * GPU_batch_calloc()
Definition gpu_batch.cc:44
struct @064345207361167251075330302113175271221317160336::@113254110077376341056327177062323111323010325277 batch
int count
uint8_t flag
Definition wm_window.cc:139