2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2016 by Mike Erwin.
19 * All rights reserved.
21 * Contributor(s): Blender Foundation
23 * ***** END GPL LICENSE BLOCK *****
26 /** \file blender/gpu/intern/gpu_batch.c
30 * Contains VAOs + VBOs + Shader representing a drawable entity.
33 #include "MEM_guardedalloc.h"
35 #include "GPU_batch.h"
36 #include "GPU_batch_presets.h"
37 #include "GPU_matrix.h"
38 #include "GPU_shader.h"
40 #include "gpu_batch_private.h"
41 #include "gpu_context_private.h"
42 #include "gpu_primitive_private.h"
43 #include "gpu_shader_private.h"
48 static void batch_update_program_bindings(GPUBatch *batch, uint v_first);
50 void GPU_batch_vao_cache_clear(GPUBatch *batch)
52 if (batch->context == NULL) {
55 if (batch->is_dynamic_vao_count) {
56 for (int i = 0; i < batch->dynamic_vaos.count; ++i) {
57 if (batch->dynamic_vaos.vao_ids[i]) {
58 GPU_vao_free(batch->dynamic_vaos.vao_ids[i], batch->context);
60 if (batch->dynamic_vaos.interfaces[i]) {
61 GPU_shaderinterface_remove_batch_ref((GPUShaderInterface *)batch->dynamic_vaos.interfaces[i], batch);
64 MEM_freeN(batch->dynamic_vaos.interfaces);
65 MEM_freeN(batch->dynamic_vaos.vao_ids);
68 for (int i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i) {
69 if (batch->static_vaos.vao_ids[i]) {
70 GPU_vao_free(batch->static_vaos.vao_ids[i], batch->context);
72 if (batch->static_vaos.interfaces[i]) {
73 GPU_shaderinterface_remove_batch_ref((GPUShaderInterface *)batch->static_vaos.interfaces[i], batch);
77 batch->is_dynamic_vao_count = false;
78 for (int i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i) {
79 batch->static_vaos.vao_ids[i] = 0;
80 batch->static_vaos.interfaces[i] = NULL;
82 gpu_context_remove_batch(batch->context, batch);
83 batch->context = NULL;
86 GPUBatch *GPU_batch_create_ex(
87 GPUPrimType prim_type, GPUVertBuf *verts, GPUIndexBuf *elem,
90 GPUBatch *batch = MEM_callocN(sizeof(GPUBatch), "GPUBatch");
91 GPU_batch_init_ex(batch, prim_type, verts, elem, owns_flag);
95 void GPU_batch_init_ex(
96 GPUBatch *batch, GPUPrimType prim_type, GPUVertBuf *verts, GPUIndexBuf *elem,
100 assert(verts != NULL);
103 batch->verts[0] = verts;
104 for (int v = 1; v < GPU_BATCH_VBO_MAX_LEN; ++v) {
105 batch->verts[v] = NULL;
109 batch->gl_prim_type = convert_prim_type_to_gl(prim_type);
110 batch->phase = GPU_BATCH_READY_TO_DRAW;
111 batch->is_dynamic_vao_count = false;
112 batch->owns_flag = owns_flag;
113 batch->free_callback = NULL;
116 /* This will share the VBOs with the new batch. */
117 void GPU_batch_copy(GPUBatch *batch_dst, GPUBatch *batch_src)
119 GPU_batch_init_ex(batch_dst, GPU_PRIM_POINTS, batch_src->verts[0], batch_src->elem, 0);
121 batch_dst->gl_prim_type = batch_src->gl_prim_type;
122 for (int v = 1; v < GPU_BATCH_VBO_MAX_LEN; ++v) {
123 batch_dst->verts[v] = batch_src->verts[v];
127 void GPU_batch_clear(GPUBatch *batch)
129 if (batch->free_callback) {
130 batch->free_callback(batch, batch->callback_data);
133 if (batch->owns_flag & GPU_BATCH_OWNS_INDEX) {
134 GPU_indexbuf_discard(batch->elem);
136 if (batch->owns_flag & GPU_BATCH_OWNS_INSTANCES) {
137 GPU_vertbuf_discard(batch->inst);
139 if ((batch->owns_flag & ~GPU_BATCH_OWNS_INDEX) != 0) {
140 for (int v = 0; v < GPU_BATCH_VBO_MAX_LEN; ++v) {
141 if (batch->verts[v] == NULL) {
144 if (batch->owns_flag & (1 << v)) {
145 GPU_vertbuf_discard(batch->verts[v]);
149 GPU_batch_vao_cache_clear(batch);
152 void GPU_batch_discard(GPUBatch *batch)
154 GPU_batch_clear(batch);
158 void GPU_batch_callback_free_set(GPUBatch *batch, void (*callback)(GPUBatch *, void *), void *user_data)
160 batch->free_callback = callback;
161 batch->callback_data = user_data;
164 void GPU_batch_instbuf_set(GPUBatch *batch, GPUVertBuf *inst, bool own_vbo)
167 assert(inst != NULL);
169 /* redo the bindings */
170 GPU_batch_vao_cache_clear(batch);
172 if (batch->inst != NULL && (batch->owns_flag & GPU_BATCH_OWNS_INSTANCES)) {
173 GPU_vertbuf_discard(batch->inst);
178 batch->owns_flag |= GPU_BATCH_OWNS_INSTANCES;
181 batch->owns_flag &= ~GPU_BATCH_OWNS_INSTANCES;
185 /* Returns the index of verts in the batch. */
186 int GPU_batch_vertbuf_add_ex(
187 GPUBatch *batch, GPUVertBuf *verts,
190 /* redo the bindings */
191 GPU_batch_vao_cache_clear(batch);
193 for (uint v = 0; v < GPU_BATCH_VBO_MAX_LEN; ++v) {
194 if (batch->verts[v] == NULL) {
196 /* for now all VertexBuffers must have same vertex_len */
197 assert(verts->vertex_len == batch->verts[0]->vertex_len);
199 batch->verts[v] = verts;
200 /* TODO: mark dirty so we can keep attrib bindings up-to-date */
202 batch->owns_flag |= (1 << v);
207 /* we only make it this far if there is no room for another GPUVertBuf */
214 static GLuint batch_vao_get(GPUBatch *batch)
216 /* Search through cache */
217 if (batch->is_dynamic_vao_count) {
218 for (int i = 0; i < batch->dynamic_vaos.count; ++i)
219 if (batch->dynamic_vaos.interfaces[i] == batch->interface)
220 return batch->dynamic_vaos.vao_ids[i];
223 for (int i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i)
224 if (batch->static_vaos.interfaces[i] == batch->interface)
225 return batch->static_vaos.vao_ids[i];
228 /* Set context of this batch.
229 * It will be bound to it until GPU_batch_vao_cache_clear is called.
230 * Until then it can only be drawn with this context. */
231 if (batch->context == NULL) {
232 batch->context = GPU_context_active_get();
233 gpu_context_add_batch(batch->context, batch);
237 /* Make sure you are not trying to draw this batch in another context. */
238 assert(batch->context == GPU_context_active_get());
242 /* Cache miss, time to add a new entry! */
244 if (!batch->is_dynamic_vao_count) {
245 int i; /* find first unused slot */
246 for (i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i)
247 if (batch->static_vaos.vao_ids[i] == 0)
250 if (i < GPU_BATCH_VAO_STATIC_LEN) {
251 batch->static_vaos.interfaces[i] = batch->interface;
252 batch->static_vaos.vao_ids[i] = new_vao = GPU_vao_alloc();
255 /* Not enough place switch to dynamic. */
256 batch->is_dynamic_vao_count = true;
257 /* Erase previous entries, they will be added back if drawn again. */
258 for (int j = 0; j < GPU_BATCH_VAO_STATIC_LEN; ++j) {
259 GPU_shaderinterface_remove_batch_ref((GPUShaderInterface *)batch->static_vaos.interfaces[j], batch);
260 GPU_vao_free(batch->static_vaos.vao_ids[j], batch->context);
262 /* Init dynamic arrays and let the branch below set the values. */
263 batch->dynamic_vaos.count = GPU_BATCH_VAO_DYN_ALLOC_COUNT;
264 batch->dynamic_vaos.interfaces = MEM_callocN(batch->dynamic_vaos.count * sizeof(GPUShaderInterface *), "dyn vaos interfaces");
265 batch->dynamic_vaos.vao_ids = MEM_callocN(batch->dynamic_vaos.count * sizeof(GLuint), "dyn vaos ids");
269 if (batch->is_dynamic_vao_count) {
270 int i; /* find first unused slot */
271 for (i = 0; i < batch->dynamic_vaos.count; ++i)
272 if (batch->dynamic_vaos.vao_ids[i] == 0)
275 if (i == batch->dynamic_vaos.count) {
276 /* Not enough place, realloc the array. */
277 i = batch->dynamic_vaos.count;
278 batch->dynamic_vaos.count += GPU_BATCH_VAO_DYN_ALLOC_COUNT;
279 batch->dynamic_vaos.interfaces = MEM_recallocN(batch->dynamic_vaos.interfaces, sizeof(GPUShaderInterface *) * batch->dynamic_vaos.count);
280 batch->dynamic_vaos.vao_ids = MEM_recallocN(batch->dynamic_vaos.vao_ids, sizeof(GLuint) * batch->dynamic_vaos.count);
282 batch->dynamic_vaos.interfaces[i] = batch->interface;
283 batch->dynamic_vaos.vao_ids[i] = new_vao = GPU_vao_alloc();
286 GPU_shaderinterface_add_batch_ref((GPUShaderInterface *)batch->interface, batch);
289 assert(new_vao != 0);
292 /* We just got a fresh VAO we need to initialize it. */
293 glBindVertexArray(new_vao);
294 batch_update_program_bindings(batch, 0);
295 glBindVertexArray(0);
300 void GPU_batch_program_set_no_use(GPUBatch *batch, uint32_t program, const GPUShaderInterface *shaderface)
303 assert(glIsProgram(shaderface->program));
304 assert(batch->program_in_use == 0);
306 batch->interface = shaderface;
307 batch->program = program;
308 batch->vao_id = batch_vao_get(batch);
311 void GPU_batch_program_set(GPUBatch *batch, uint32_t program, const GPUShaderInterface *shaderface)
313 GPU_batch_program_set_no_use(batch, program, shaderface);
314 GPU_batch_program_use_begin(batch); /* hack! to make Batch_Uniform* simpler */
317 void gpu_batch_remove_interface_ref(GPUBatch *batch, const GPUShaderInterface *interface)
319 if (batch->is_dynamic_vao_count) {
320 for (int i = 0; i < batch->dynamic_vaos.count; ++i) {
321 if (batch->dynamic_vaos.interfaces[i] == interface) {
322 GPU_vao_free(batch->dynamic_vaos.vao_ids[i], batch->context);
323 batch->dynamic_vaos.vao_ids[i] = 0;
324 batch->dynamic_vaos.interfaces[i] = NULL;
325 break; /* cannot have duplicates */
331 for (i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i) {
332 if (batch->static_vaos.interfaces[i] == interface) {
333 GPU_vao_free(batch->static_vaos.vao_ids[i], batch->context);
334 batch->static_vaos.vao_ids[i] = 0;
335 batch->static_vaos.interfaces[i] = NULL;
336 break; /* cannot have duplicates */
342 static void create_bindings(
343 GPUVertBuf *verts, const GPUShaderInterface *interface,
344 uint v_first, const bool use_instancing)
346 const GPUVertFormat *format = &verts->format;
348 const uint attr_len = format->attr_len;
349 const uint stride = format->stride;
351 GPU_vertbuf_use(verts);
353 for (uint a_idx = 0; a_idx < attr_len; ++a_idx) {
354 const GPUVertAttr *a = format->attribs + a_idx;
355 const GLvoid *pointer = (const GLubyte *)0 + a->offset + v_first * stride;
357 for (uint n_idx = 0; n_idx < a->name_len; ++n_idx) {
358 const GPUShaderInput *input = GPU_shaderinterface_attr(interface, a->name[n_idx]);
360 if (input == NULL) continue;
362 if (a->comp_len == 16 || a->comp_len == 12 || a->comp_len == 8) {
364 assert(a->fetch_mode == GPU_FETCH_FLOAT);
365 assert(a->gl_comp_type == GL_FLOAT);
367 for (int i = 0; i < a->comp_len / 4; ++i) {
368 glEnableVertexAttribArray(input->location + i);
369 glVertexAttribDivisor(input->location + i, (use_instancing) ? 1 : 0);
370 glVertexAttribPointer(input->location + i, 4, a->gl_comp_type, GL_FALSE, stride,
371 (const GLubyte *)pointer + i * 16);
375 glEnableVertexAttribArray(input->location);
376 glVertexAttribDivisor(input->location, (use_instancing) ? 1 : 0);
378 switch (a->fetch_mode) {
379 case GPU_FETCH_FLOAT:
380 case GPU_FETCH_INT_TO_FLOAT:
381 glVertexAttribPointer(input->location, a->comp_len, a->gl_comp_type, GL_FALSE, stride, pointer);
383 case GPU_FETCH_INT_TO_FLOAT_UNIT:
384 glVertexAttribPointer(input->location, a->comp_len, a->gl_comp_type, GL_TRUE, stride, pointer);
387 glVertexAttribIPointer(input->location, a->comp_len, a->gl_comp_type, stride, pointer);
395 static void batch_update_program_bindings(GPUBatch *batch, uint v_first)
397 for (int v = 0; v < GPU_BATCH_VBO_MAX_LEN && batch->verts[v] != NULL; ++v) {
398 create_bindings(batch->verts[v], batch->interface, (batch->inst) ? 0 : v_first, false);
401 create_bindings(batch->inst, batch->interface, v_first, true);
404 GPU_indexbuf_use(batch->elem);
408 void GPU_batch_program_use_begin(GPUBatch *batch)
410 /* NOTE: use_program & done_using_program are fragile, depend on staying in sync with
411 * the GL context's active program. use_program doesn't mark other programs as "not used". */
412 /* TODO: make not fragile (somehow) */
414 if (!batch->program_in_use) {
415 glUseProgram(batch->program);
416 batch->program_in_use = true;
420 void GPU_batch_program_use_end(GPUBatch *batch)
422 if (batch->program_in_use) {
426 batch->program_in_use = false;
431 # define GET_UNIFORM const GPUShaderInput *uniform = GPU_shaderinterface_uniform(batch->interface, name); assert(uniform);
433 # define GET_UNIFORM const GPUShaderInput *uniform = GPU_shaderinterface_uniform(batch->interface, name);
436 void GPU_batch_uniform_1ui(GPUBatch *batch, const char *name, int value)
439 glUniform1ui(uniform->location, value);
442 void GPU_batch_uniform_1i(GPUBatch *batch, const char *name, int value)
445 glUniform1i(uniform->location, value);
448 void GPU_batch_uniform_1b(GPUBatch *batch, const char *name, bool value)
451 glUniform1i(uniform->location, value ? GL_TRUE : GL_FALSE);
454 void GPU_batch_uniform_2f(GPUBatch *batch, const char *name, float x, float y)
457 glUniform2f(uniform->location, x, y);
460 void GPU_batch_uniform_3f(GPUBatch *batch, const char *name, float x, float y, float z)
463 glUniform3f(uniform->location, x, y, z);
466 void GPU_batch_uniform_4f(GPUBatch *batch, const char *name, float x, float y, float z, float w)
469 glUniform4f(uniform->location, x, y, z, w);
472 void GPU_batch_uniform_1f(GPUBatch *batch, const char *name, float x)
475 glUniform1f(uniform->location, x);
478 void GPU_batch_uniform_2fv(GPUBatch *batch, const char *name, const float data[2])
481 glUniform2fv(uniform->location, 1, data);
484 void GPU_batch_uniform_3fv(GPUBatch *batch, const char *name, const float data[3])
487 glUniform3fv(uniform->location, 1, data);
490 void GPU_batch_uniform_4fv(GPUBatch *batch, const char *name, const float data[4])
493 glUniform4fv(uniform->location, 1, data);
496 void GPU_batch_uniform_2fv_array(GPUBatch *batch, const char *name, const int len, const float *data)
499 glUniform2fv(uniform->location, len, data);
502 void GPU_batch_uniform_4fv_array(GPUBatch *batch, const char *name, const int len, const float *data)
505 glUniform4fv(uniform->location, len, data);
508 void GPU_batch_uniform_mat4(GPUBatch *batch, const char *name, const float data[4][4])
511 glUniformMatrix4fv(uniform->location, 1, GL_FALSE, (const float *)data);
514 static void primitive_restart_enable(const GPUIndexBuf *el)
516 // TODO(fclem) Replace by GL_PRIMITIVE_RESTART_FIXED_INDEX when we have ogl 4.3
517 glEnable(GL_PRIMITIVE_RESTART);
518 GLuint restart_index = (GLuint)0xFFFFFFFF;
520 #if GPU_TRACK_INDEX_RANGE
521 if (el->index_type == GPU_INDEX_U8)
522 restart_index = (GLuint)0xFF;
523 else if (el->index_type == GPU_INDEX_U16)
524 restart_index = (GLuint)0xFFFF;
527 glPrimitiveRestartIndex(restart_index);
530 static void primitive_restart_disable(void)
532 glDisable(GL_PRIMITIVE_RESTART);
535 static void *elem_offset(const GPUIndexBuf *el, int v_first)
537 #if GPU_TRACK_INDEX_RANGE
538 if (el->index_type == GPU_INDEX_U8)
539 return (GLubyte *)0 + v_first;
540 else if (el->index_type == GPU_INDEX_U16)
541 return (GLushort *)0 + v_first;
544 return (GLuint *)0 + v_first;
547 void GPU_batch_draw(GPUBatch *batch)
550 assert(batch->phase == GPU_BATCH_READY_TO_DRAW);
551 assert(batch->verts[0]->vbo_id != 0);
553 GPU_batch_program_use_begin(batch);
554 GPU_matrix_bind(batch->interface); // external call.
556 GPU_batch_draw_range_ex(batch, 0, 0, false);
558 GPU_batch_program_use_end(batch);
561 void GPU_batch_draw_range_ex(GPUBatch *batch, int v_first, int v_count, bool force_instance)
564 assert(!(force_instance && (batch->inst == NULL)) || v_count > 0); // we cannot infer length if force_instance
567 const bool do_instance = (force_instance || batch->inst);
569 // If using offset drawing, use the default VAO and redo bindings.
570 if (v_first != 0 && do_instance) {
571 glBindVertexArray(GPU_vao_default());
572 batch_update_program_bindings(batch, v_first);
575 glBindVertexArray(batch->vao_id);
579 /* Infer length if vertex count is not given */
581 v_count = batch->inst->vertex_len;
585 const GPUIndexBuf *el = batch->elem;
587 if (el->use_prim_restart) {
588 primitive_restart_enable(el);
590 #if GPU_TRACK_INDEX_RANGE
591 glDrawElementsInstancedBaseVertex(batch->gl_prim_type,
598 glDrawElementsInstanced(batch->gl_prim_type, el->index_len, GL_UNSIGNED_INT, 0, v_count);
600 if (el->use_prim_restart) {
601 primitive_restart_disable();
605 glDrawArraysInstanced(batch->gl_prim_type, 0, batch->verts[0]->vertex_len, v_count);
609 /* Infer length if vertex count is not given */
611 v_count = (batch->elem) ? batch->elem->index_len : batch->verts[0]->vertex_len;
615 const GPUIndexBuf *el = batch->elem;
617 if (el->use_prim_restart) {
618 primitive_restart_enable(el);
621 void *v_first_ofs = elem_offset(el, v_first);
623 #if GPU_TRACK_INDEX_RANGE
624 if (el->base_index) {
625 glDrawRangeElementsBaseVertex(
635 glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, v_count, el->gl_index_type, v_first_ofs);
638 glDrawElements(batch->gl_prim_type, v_count, GL_UNSIGNED_INT, v_first_ofs);
640 if (el->use_prim_restart) {
641 primitive_restart_disable();
645 glDrawArrays(batch->gl_prim_type, v_first, v_count);
649 /* Performance hog if you are drawing with the same vao multiple time.
650 * Only activate for debugging. */
651 // glBindVertexArray(0);
654 /* just draw some vertices and let shader place them where we want. */
655 void GPU_draw_primitive(GPUPrimType prim_type, int v_count)
657 /* we cannot draw without vao ... annoying ... */
658 glBindVertexArray(GPU_vao_default());
660 GLenum type = convert_prim_type_to_gl(prim_type);
661 glDrawArrays(type, 0, v_count);
663 /* Performance hog if you are drawing with the same vao multiple time.
664 * Only activate for debugging.*/
665 // glBindVertexArray(0);
669 /* -------------------------------------------------------------------- */
673 void GPU_batch_program_set_builtin(GPUBatch *batch, GPUBuiltinShader shader_id)
675 GPUShader *shader = GPU_shader_get_builtin_shader(shader_id);
676 GPU_batch_program_set(batch, shader->program, shader->interface);
681 /* -------------------------------------------------------------------- */
685 void gpu_batch_init(void)
687 gpu_batch_presets_init();
690 void gpu_batch_exit(void)
692 gpu_batch_presets_exit();