Blender V4.3
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
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_uniform_buffer.hh"
28#include "GPU_vertex_buffer.hh"
29
30struct GPUShader;
31
32#define GPU_BATCH_VBO_MAX_LEN 16
33#define GPU_BATCH_INST_VBO_MAX_LEN 2
34#define GPU_BATCH_VAO_STATIC_LEN 3
35#define GPU_BATCH_VAO_DYN_ALLOC_COUNT 16
36
60
61#define GPU_BATCH_OWNS_NONE GPU_BATCH_INVALID
62
64 "eGPUBatchFlag: Error: status flags are shadowed by the ownership bits!")
65
67
68namespace blender::gpu {
69
78class Batch {
79 public:
87 GPUStorageBuf *resource_id_buf;
91 GPUPrimType prim_type;
93 GPUShader *shader;
94
95 virtual ~Batch() = default;
96
97 virtual void draw(int v_first, int v_count, int i_first, int i_count) = 0;
98 virtual void draw_indirect(GPUStorageBuf *indirect_buf, intptr_t offset) = 0;
99 virtual void multi_draw_indirect(GPUStorageBuf *indirect_buf,
100 int count,
101 intptr_t offset,
102 intptr_t stride) = 0;
103
104 uint32_t vertex_count_get() const
105 {
106 if (elem) {
107 return elem_()->index_len_get();
108 }
109 return verts_(0)->vertex_len;
110 }
111
112 /* Convenience casts. */
113 IndexBuf *elem_() const
114 {
115 return elem;
116 }
117 VertBuf *verts_(const int index) const
118 {
119 return verts[index];
120 }
121 VertBuf *inst_(const int index) const
122 {
123 return inst[index];
124 }
125};
126
127} // namespace blender::gpu
128
129/* -------------------------------------------------------------------- */
137blender::gpu::Batch *GPU_batch_calloc();
138
142blender::gpu::Batch *GPU_batch_create_ex(GPUPrimType primitive_type,
143 blender::gpu::VertBuf *vertex_buf,
144 blender::gpu::IndexBuf *index_buf,
145 eGPUBatchFlag owns_flag);
149#define GPU_batch_create(primitive_type, vertex_buf, index_buf) \
150 GPU_batch_create_ex(primitive_type, vertex_buf, index_buf, (eGPUBatchFlag)0)
151
157void GPU_batch_init_ex(blender::gpu::Batch *batch,
158 GPUPrimType primitive_type,
159 blender::gpu::VertBuf *vertex_buf,
160 blender::gpu::IndexBuf *index_buf,
161 eGPUBatchFlag owns_flag);
167#define GPU_batch_init(batch, primitive_type, vertex_buf, index_buf) \
168 GPU_batch_init_ex(batch, primitive_type, vertex_buf, index_buf, (eGPUBatchFlag)0)
169
174void GPU_batch_copy(blender::gpu::Batch *batch_dst, blender::gpu::Batch *batch_src);
175
178/* -------------------------------------------------------------------- */
187void GPU_batch_clear(blender::gpu::Batch *batch);
188
189void GPU_batch_zero(blender::gpu::Batch *batch);
190
191#define GPU_BATCH_CLEAR_SAFE(batch) \
192 do { \
193 if (batch != nullptr) { \
194 GPU_batch_clear(batch); \
195 GPU_batch_zero(batch); \
196 } \
197 } while (0)
198
203void GPU_batch_discard(blender::gpu::Batch *batch);
204
205#define GPU_BATCH_DISCARD_SAFE(batch) \
206 do { \
207 if (batch != nullptr) { \
208 GPU_batch_discard(batch); \
209 batch = nullptr; \
210 } \
211 } while (0)
212
215/* -------------------------------------------------------------------- */
223int GPU_batch_vertbuf_add(blender::gpu::Batch *batch,
224 blender::gpu::VertBuf *vertex_buf,
225 bool own_vbo);
226
231int GPU_batch_instbuf_add(blender::gpu::Batch *batch,
232 blender::gpu::VertBuf *vertex_buf,
233 bool own_vbo);
234
239void GPU_batch_instbuf_set(blender::gpu::Batch *batch,
240 blender::gpu::VertBuf *vertex_buf,
241 bool own_vbo);
242
247void GPU_batch_elembuf_set(blender::gpu::Batch *batch,
248 blender::gpu::IndexBuf *index_buf,
249 bool own_ibo);
250
255bool GPU_batch_vertbuf_has(const blender::gpu::Batch *batch,
256 const blender::gpu::VertBuf *vertex_buf);
257
263void GPU_batch_resource_id_buf_set(blender::gpu::Batch *batch, GPUStorageBuf *resource_id_buf);
264
267/* -------------------------------------------------------------------- */
279/* TODO(fclem): These should be removed and replaced by `GPU_shader_bind()`. */
280void GPU_batch_set_shader(blender::gpu::Batch *batch, GPUShader *shader);
281void GPU_batch_program_set_builtin(blender::gpu::Batch *batch, eGPUBuiltinShader shader_id);
283 eGPUBuiltinShader shader_id,
284 eGPUShaderConfig sh_cfg);
291void GPU_batch_program_set_imm_shader(blender::gpu::Batch *batch);
292
296/* TODO(fclem): These need to be replaced by GPU_shader_uniform_* with explicit shader. */
297#define GPU_batch_uniform_1i(batch, name, x) GPU_shader_uniform_1i((batch)->shader, name, x);
298#define GPU_batch_uniform_1b(batch, name, x) GPU_shader_uniform_1b((batch)->shader, name, x);
299#define GPU_batch_uniform_1f(batch, name, x) GPU_shader_uniform_1f((batch)->shader, name, x);
300#define GPU_batch_uniform_2f(batch, name, x, y) GPU_shader_uniform_2f((batch)->shader, name, x, y);
301#define GPU_batch_uniform_3f(batch, name, x, y, z) \
302 GPU_shader_uniform_3f((batch)->shader, name, x, y, z);
303#define GPU_batch_uniform_4f(batch, name, x, y, z, w) \
304 GPU_shader_uniform_4f((batch)->shader, name, x, y, z, w);
305#define GPU_batch_uniform_2fv(batch, name, val) GPU_shader_uniform_2fv((batch)->shader, name, val);
306#define GPU_batch_uniform_3fv(batch, name, val) GPU_shader_uniform_3fv((batch)->shader, name, val);
307#define GPU_batch_uniform_4fv(batch, name, val) GPU_shader_uniform_4fv((batch)->shader, name, val);
308#define GPU_batch_uniform_2fv_array(batch, name, len, val) \
309 GPU_shader_uniform_2fv_array((batch)->shader, name, len, val);
310#define GPU_batch_uniform_4fv_array(batch, name, len, val) \
311 GPU_shader_uniform_4fv_array((batch)->shader, name, len, val);
312#define GPU_batch_uniform_mat4(batch, name, val) \
313 GPU_shader_uniform_mat4((batch)->shader, name, val);
314#define GPU_batch_uniformbuf_bind(batch, name, ubo) \
315 GPU_uniformbuf_bind(ubo, GPU_shader_get_ubo_binding((batch)->shader, name));
316#define GPU_batch_texture_bind(batch, name, tex) \
317 GPU_texture_bind(tex, GPU_shader_get_sampler_binding((batch)->shader, name));
318
322void GPU_batch_bind_as_resources(blender::gpu::Batch *batch, GPUShader *shader);
323
326/* -------------------------------------------------------------------- */
334void GPU_batch_draw(blender::gpu::Batch *batch);
335
347void GPU_batch_draw_range(blender::gpu::Batch *batch, int vertex_first, int vertex_count);
348
361void GPU_batch_draw_instance_range(blender::gpu::Batch *batch,
362 int instance_first,
363 int instance_count);
364
376void GPU_batch_draw_advanced(blender::gpu::Batch *batch,
377 int vertex_first,
378 int vertex_count,
379 int instance_first,
380 int instance_count);
381
393void GPU_batch_draw_indirect(blender::gpu::Batch *batch,
394 GPUStorageBuf *indirect_buf,
395 intptr_t offset);
396
409void GPU_batch_multi_draw_indirect(blender::gpu::Batch *batch,
410 GPUStorageBuf *indirect_buf,
411 int count,
412 intptr_t offset,
413 intptr_t stride);
414
419void GPU_batch_draw_parameter_get(blender::gpu::Batch *batch,
420 int *r_vertex_count,
421 int *r_vertex_first,
422 int *r_base_index,
423 int *r_instance_count);
424
429 GPUPrimType expanded_prim_type,
430 int vertex_count,
431 int vertex_first);
432
435/* -------------------------------------------------------------------- */
439void gpu_batch_init();
440void gpu_batch_exit();
441
#define BLI_STATIC_ASSERT(a, msg)
Definition BLI_assert.h:87
#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:56
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)
void GPU_batch_discard(blender::gpu::Batch *batch)
void GPU_batch_zero(blender::gpu::Batch *batch)
void GPU_batch_draw_advanced(blender::gpu::Batch *batch, int vertex_first, int vertex_count, int instance_first, int instance_count)
#define GPU_BATCH_INST_VBO_MAX_LEN
Definition GPU_batch.hh:33
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)
void GPU_batch_resource_id_buf_set(blender::gpu::Batch *batch, GPUStorageBuf *resource_id_buf)
blender::IndexRange GPU_batch_draw_expanded_parameter_get(const blender::gpu::Batch *batch, GPUPrimType expanded_prim_type, int vertex_count, int vertex_first)
Definition gpu_batch.cc:355
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)
#define GPU_BATCH_VBO_MAX_LEN
Definition GPU_batch.hh:32
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:474
void GPU_batch_set_shader(blender::gpu::Batch *batch, GPUShader *shader)
void GPU_batch_program_set_builtin(blender::gpu::Batch *batch, eGPUBuiltinShader shader_id)
void GPU_batch_draw(blender::gpu::Batch *batch)
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:37
@ GPU_BATCH_INVALID
Definition GPU_batch.hh:39
@ GPU_BATCH_DIRTY
Definition GPU_batch.hh:58
@ GPU_BATCH_INIT
Definition GPU_batch.hh:54
@ GPU_BATCH_OWNS_INDEX
Definition GPU_batch.hh:51
@ GPU_BATCH_OWNS_INST_VBO
Definition GPU_batch.hh:46
@ GPU_BATCH_OWNS_INST_VBO_MAX
Definition GPU_batch.hh:47
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:42
@ GPU_BATCH_OWNS_VBO_ANY
Definition GPU_batch.hh:44
@ GPU_BATCH_OWNS_INST_VBO_ANY
Definition GPU_batch.hh:48
@ GPU_BATCH_OWNS_VBO_MAX
Definition GPU_batch.hh:43
@ GPU_BATCH_BUILDING
Definition GPU_batch.hh:56
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:479
void GPU_batch_bind_as_resources(blender::gpu::Batch *batch, GPUShader *shader)
GPUPrimType
eGPUBuiltinShader
struct GPUShader GPUShader
uint32_t index_len_get() const
static float verts[][3]
Batch * GPU_batch_calloc()
Definition gpu_batch.cc:49
struct @620::@622 batch
int count
unsigned int uint32_t
Definition stdint.h:80
_W64 int intptr_t
Definition stdint.h:118
uint8_t flag
Definition wm_window.cc:138