Blender V5.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
18
19#pragma once
20
21#include "BLI_enum_flags.hh"
22#include "BLI_index_range.hh"
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
29namespace blender {
30
31namespace gpu {
32class Shader;
33} // namespace gpu
34
35constexpr static int GPU_BATCH_VBO_MAX_LEN = 16;
36constexpr static int GPU_BATCH_VAO_STATIC_LEN = 3;
37constexpr static int GPU_BATCH_VAO_DYN_ALLOC_COUNT = 16;
38
57
58#define GPU_BATCH_OWNS_NONE GPU_BATCH_INVALID
59
61 "GPUBatchFlag: Error: status flags are shadowed by the ownership bits!")
62
64
65namespace gpu {
66
75class Batch {
76 public:
80 gpu::IndexBuf *elem;
82 int32_t procedural_vertices;
84 GPUBatchFlag flag;
86 GPUPrimType prim_type;
89
90 virtual ~Batch() = default;
91
92 virtual void draw(int v_first, int v_count, int i_first, int i_count) = 0;
93 virtual void draw_indirect(gpu::StorageBuf *indirect_buf, intptr_t offset) = 0;
94 virtual void multi_draw_indirect(gpu::StorageBuf *indirect_buf,
95 int count,
96 intptr_t offset,
97 intptr_t stride) = 0;
98
100 {
101 if (elem) {
102 return elem_()->index_len_get();
103 }
104 return verts_(0)->vertex_len;
105 }
106
107 /* Convenience casts. */
108 IndexBuf *elem_() const
109 {
110 return elem;
111 }
112 VertBuf *verts_(const int index) const
113 {
114 return verts[index];
115 }
116};
117
118} // namespace gpu
119
120/* -------------------------------------------------------------------- */
123
128gpu::Batch *GPU_batch_calloc();
129
133gpu::Batch *GPU_batch_create_ex(GPUPrimType primitive_type,
134 gpu::VertBuf *vertex_buf,
135 gpu::IndexBuf *index_buf,
136 GPUBatchFlag owns_flag);
137
138gpu::Batch *GPU_batch_create_procedural(GPUPrimType primitive_type, int32_t vertex_count);
139
143#define GPU_batch_create(primitive_type, vertex_buf, index_buf) \
144 GPU_batch_create_ex(primitive_type, vertex_buf, index_buf, (GPUBatchFlag)0)
145
151void GPU_batch_init_ex(gpu::Batch *batch,
152 GPUPrimType primitive_type,
153 gpu::VertBuf *vertex_buf,
154 gpu::IndexBuf *index_buf,
155 GPUBatchFlag owns_flag);
161#define GPU_batch_init(batch, primitive_type, vertex_buf, index_buf) \
162 GPU_batch_init_ex(batch, primitive_type, vertex_buf, index_buf, (GPUBatchFlag)0)
163
168void GPU_batch_copy(gpu::Batch *batch_dst, gpu::Batch *batch_src);
169
171
172/* -------------------------------------------------------------------- */
175
181void GPU_batch_clear(gpu::Batch *batch);
182
183void GPU_batch_zero(gpu::Batch *batch);
184
185#define GPU_BATCH_CLEAR_SAFE(batch) \
186 do { \
187 if (batch != nullptr) { \
188 GPU_batch_clear(batch); \
189 GPU_batch_zero(batch); \
190 } \
191 } while (0)
192
197void GPU_batch_discard(gpu::Batch *batch);
198
199#define GPU_BATCH_DISCARD_SAFE(batch) \
200 do { \
201 if (batch != nullptr) { \
202 GPU_batch_discard(batch); \
203 batch = nullptr; \
204 } \
205 } while (0)
206
208
209/* -------------------------------------------------------------------- */
212
217int GPU_batch_vertbuf_add(gpu::Batch *batch, gpu::VertBuf *vertex_buf, bool own_vbo);
218
223void GPU_batch_elembuf_set(gpu::Batch *batch, gpu::IndexBuf *index_buf, bool own_ibo);
224
229bool GPU_batch_vertbuf_has(const gpu::Batch *batch, const gpu::VertBuf *vertex_buf);
230
232
233/* -------------------------------------------------------------------- */
240
245/* TODO(fclem): These should be removed and replaced by `GPU_shader_bind()`. */
246void GPU_batch_set_shader(gpu::Batch *batch,
248 const gpu::shader::SpecializationConstants *constants_state = nullptr);
249void GPU_batch_program_set_builtin(gpu::Batch *batch, GPUBuiltinShader shader_id);
251 GPUBuiltinShader shader_id,
252 GPUShaderConfig sh_cfg);
259void GPU_batch_program_set_imm_shader(gpu::Batch *batch);
260
264/* TODO(fclem): These need to be replaced by GPU_shader_uniform_* with explicit shader. */
265#define GPU_batch_uniform_1i(batch, name, x) GPU_shader_uniform_1i((batch)->shader, name, x);
266#define GPU_batch_uniform_1b(batch, name, x) GPU_shader_uniform_1b((batch)->shader, name, x);
267#define GPU_batch_uniform_1f(batch, name, x) GPU_shader_uniform_1f((batch)->shader, name, x);
268#define GPU_batch_uniform_2f(batch, name, x, y) GPU_shader_uniform_2f((batch)->shader, name, x, y);
269#define GPU_batch_uniform_3f(batch, name, x, y, z) \
270 GPU_shader_uniform_3f((batch)->shader, name, x, y, z);
271#define GPU_batch_uniform_4f(batch, name, x, y, z, w) \
272 GPU_shader_uniform_4f((batch)->shader, name, x, y, z, w);
273#define GPU_batch_uniform_2fv(batch, name, val) GPU_shader_uniform_2fv((batch)->shader, name, val);
274#define GPU_batch_uniform_3fv(batch, name, val) GPU_shader_uniform_3fv((batch)->shader, name, val);
275#define GPU_batch_uniform_4fv(batch, name, val) GPU_shader_uniform_4fv((batch)->shader, name, val);
276#define GPU_batch_uniform_2fv_array(batch, name, len, val) \
277 GPU_shader_uniform_2fv_array((batch)->shader, name, len, val);
278#define GPU_batch_uniform_4fv_array(batch, name, len, val) \
279 GPU_shader_uniform_4fv_array((batch)->shader, name, len, val);
280#define GPU_batch_uniform_mat4(batch, name, val) \
281 GPU_shader_uniform_mat4((batch)->shader, name, val);
282#define GPU_batch_uniformbuf_bind(batch, name, ubo) \
283 GPU_uniformbuf_bind(ubo, GPU_shader_get_ubo_binding((batch)->shader, name));
284#define GPU_batch_texture_bind(batch, name, tex) \
285 GPU_texture_bind(tex, GPU_shader_get_sampler_binding((batch)->shader, name));
286
290void GPU_batch_bind_as_resources(gpu::Batch *batch,
292 const gpu::shader::SpecializationConstants *constants = nullptr);
293
295
296/* -------------------------------------------------------------------- */
299
304void GPU_batch_draw(gpu::Batch *batch);
305
317void GPU_batch_draw_range(gpu::Batch *batch, int vertex_first, int vertex_count);
318
331void GPU_batch_draw_instance_range(gpu::Batch *batch, int instance_first, int instance_count);
332
345 gpu::Batch *batch, int vertex_first, int vertex_count, int instance_first, int instance_count);
346
358void GPU_batch_draw_indirect(gpu::Batch *batch, gpu::StorageBuf *indirect_buf, intptr_t offset);
359
373 gpu::Batch *batch, gpu::StorageBuf *indirect_buf, int count, intptr_t offset, intptr_t stride);
374
379void GPU_batch_draw_parameter_get(gpu::Batch *batch,
380 int *r_vertex_count,
381 int *r_vertex_first,
382 int *r_base_index,
383 int *r_instance_count);
384
389 GPUPrimType output_prim_type,
390 int vertex_count,
391 int vertex_first,
392 int output_primitive_cout);
393
395
396/* -------------------------------------------------------------------- */
403
409
415
421
427
429
430/* -------------------------------------------------------------------- */
433
434void gpu_batch_init();
435void gpu_batch_exit();
436
438
439} // namespace blender
ENUM_OPERATORS(Layer::Flags)
int vertex_count_get(const Object &object)
Definition sculpt.cc:163
BMesh Iterators.
void GPU_batch_draw_indirect(gpu::Batch *batch, gpu::StorageBuf *indirect_buf, intptr_t offset)
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
gpu::Batch * GPU_batch_create_procedural(GPUPrimType primitive_type, int32_t vertex_count)
Definition gpu_batch.cc:85
gpu::Batch * GPU_batch_create_ex(GPUPrimType primitive_type, gpu::VertBuf *vertex_buf, gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
Definition gpu_batch.cc:53
gpu::Batch * GPU_batch_calloc()
Definition gpu_batch.cc:46
void GPU_batch_bind_as_resources(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants=nullptr)
void GPU_batch_program_set_builtin_with_config(gpu::Batch *batch, GPUBuiltinShader shader_id, GPUShaderConfig sh_cfg)
gpu::Batch * GPU_batch_procedural_points_get()
Definition gpu_batch.cc:513
static constexpr int GPU_BATCH_VBO_MAX_LEN
Definition GPU_batch.hh:35
void GPU_batch_discard(gpu::Batch *batch)
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
void GPU_batch_draw_range(gpu::Batch *batch, int vertex_first, int vertex_count)
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:329
void GPU_batch_copy(gpu::Batch *batch_dst, gpu::Batch *batch_src)
unsigned int uint32_t
Definition BKE_text.h:81
void GPU_batch_init_ex(gpu::Batch *batch, GPUPrimType primitive_type, gpu::VertBuf *vertex_buf, gpu::IndexBuf *index_buf, GPUBatchFlag owns_flag)
gpu::Batch * GPU_batch_procedural_triangle_strips_get()
Definition gpu_batch.cc:528
void GPU_batch_draw_instance_range(gpu::Batch *batch, int instance_first, int instance_count)
void GPU_batch_zero(gpu::Batch *batch)
void GPU_batch_program_set_imm_shader(gpu::Batch *batch)
void GPU_batch_draw(gpu::Batch *batch)
void GPU_batch_multi_draw_indirect(gpu::Batch *batch, gpu::StorageBuf *indirect_buf, int count, intptr_t offset, intptr_t stride)
@ GPU_BATCH_INVALID
Definition GPU_batch.hh:41
@ GPU_BATCH_DIRTY
Definition GPU_batch.hh:55
@ GPU_BATCH_OWNS_VBO_MAX
Definition GPU_batch.hh:45
@ GPU_BATCH_OWNS_VBO
Definition GPU_batch.hh:44
@ GPU_BATCH_BUILDING
Definition GPU_batch.hh:53
@ GPU_BATCH_OWNS_INDEX
Definition GPU_batch.hh:48
@ GPU_BATCH_INIT
Definition GPU_batch.hh:51
@ GPU_BATCH_OWNS_VBO_ANY
Definition GPU_batch.hh:46
void GPU_batch_clear(gpu::Batch *batch)
void GPU_batch_program_set_builtin(gpu::Batch *batch, GPUBuiltinShader shader_id)
gpu::Batch * GPU_batch_procedural_triangles_get()
Definition gpu_batch.cc:523
static constexpr int GPU_BATCH_VAO_DYN_ALLOC_COUNT
Definition GPU_batch.hh:37
bool GPU_batch_vertbuf_has(const gpu::Batch *batch, const gpu::VertBuf *vertex_buf)
signed int int32_t
Definition BKE_text.h:78
void GPU_batch_draw_advanced(gpu::Batch *batch, int vertex_first, int vertex_count, int instance_first, int instance_count)
static constexpr int GPU_BATCH_VAO_STATIC_LEN
Definition GPU_batch.hh:36
void gpu_batch_exit()
Definition gpu_batch.cc:544
void GPU_batch_elembuf_set(gpu::Batch *batch, gpu::IndexBuf *index_buf, bool own_ibo)
void gpu_batch_init()
Definition gpu_batch.cc:539
void GPU_batch_draw_parameter_get(gpu::Batch *batch, int *r_vertex_count, int *r_vertex_first, int *r_base_index, int *r_instance_count)
int GPU_batch_vertbuf_add(gpu::Batch *batch, gpu::VertBuf *vertex_buf, bool own_vbo)
gpu::Batch * GPU_batch_procedural_lines_get()
Definition gpu_batch.cc:518
_W64 int intptr_t
Definition BKE_text.h:119