| 
									
										
										
										
											2016-09-15 18:41:28 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							|  |  |  |  * of the License, or (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							|  |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  |  * The Original Code is Copyright (C) 2016 by Mike Erwin. | 
					
						
							| 
									
										
										
										
											2016-09-15 18:41:28 +02:00
										 |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 15:42:22 +11:00
										 |  |  | /** \file \ingroup gpu
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  |  * GPU geometry batch | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  |  * Contains VAOs + VBOs + Shader representing a drawable entity. | 
					
						
							| 
									
										
										
										
											2018-01-15 16:21:23 +11:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-31 16:54:58 +02:00
										 |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | #include "GPU_batch.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-15 10:34:31 +02:00
										 |  |  | #include "GPU_batch_presets.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | #include "GPU_matrix.h"
 | 
					
						
							|  |  |  | #include "GPU_shader.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "gpu_batch_private.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-19 15:48:13 +02:00
										 |  |  | #include "gpu_context_private.h"
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | #include "gpu_primitive_private.h"
 | 
					
						
							| 
									
										
										
										
											2016-09-15 18:41:28 +02:00
										 |  |  | #include "gpu_shader_private.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | static void batch_update_program_bindings(GPUBatch *batch, uint v_first); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_vao_cache_clear(GPUBatch *batch) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (batch->context == NULL) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (batch->is_dynamic_vao_count) { | 
					
						
							|  |  |  | 		for (int i = 0; i < batch->dynamic_vaos.count; ++i) { | 
					
						
							|  |  |  | 			if (batch->dynamic_vaos.vao_ids[i]) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 				GPU_vao_free(batch->dynamic_vaos.vao_ids[i], batch->context); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if (batch->dynamic_vaos.interfaces[i]) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 				GPU_shaderinterface_remove_batch_ref((GPUShaderInterface *)batch->dynamic_vaos.interfaces[i], batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2018-07-31 16:54:58 +02:00
										 |  |  | 		MEM_freeN(batch->dynamic_vaos.interfaces); | 
					
						
							|  |  |  | 		MEM_freeN(batch->dynamic_vaos.vao_ids); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		for (int i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i) { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			if (batch->static_vaos.vao_ids[i]) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 				GPU_vao_free(batch->static_vaos.vao_ids[i], batch->context); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			if (batch->static_vaos.interfaces[i]) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 				GPU_shaderinterface_remove_batch_ref((GPUShaderInterface *)batch->static_vaos.interfaces[i], batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	batch->is_dynamic_vao_count = false; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	for (int i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i) { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		batch->static_vaos.vao_ids[i] = 0; | 
					
						
							|  |  |  | 		batch->static_vaos.interfaces[i] = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	gpu_context_remove_batch(batch->context, batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	batch->context = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | GPUBatch *GPU_batch_create_ex( | 
					
						
							|  |  |  |         GPUPrimType prim_type, GPUVertBuf *verts, GPUIndexBuf *elem, | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  |         uint owns_flag) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-31 16:54:58 +02:00
										 |  |  | 	GPUBatch *batch = MEM_callocN(sizeof(GPUBatch), "GPUBatch"); | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_batch_init_ex(batch, prim_type, verts, elem, owns_flag); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	return batch; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | void GPU_batch_init_ex( | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  |         GPUBatch *batch, GPUPrimType prim_type, GPUVertBuf *verts, GPUIndexBuf *elem, | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  |         uint owns_flag) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							|  |  |  | 	assert(verts != NULL); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	batch->verts[0] = verts; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	for (int v = 1; v < GPU_BATCH_VBO_MAX_LEN; ++v) { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		batch->verts[v] = NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	batch->inst = NULL; | 
					
						
							|  |  |  | 	batch->elem = elem; | 
					
						
							|  |  |  | 	batch->gl_prim_type = convert_prim_type_to_gl(prim_type); | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	batch->phase = GPU_BATCH_READY_TO_DRAW; | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	batch->is_dynamic_vao_count = false; | 
					
						
							|  |  |  | 	batch->owns_flag = owns_flag; | 
					
						
							|  |  |  | 	batch->free_callback = NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* This will share the VBOs with the new batch. */ | 
					
						
							| 
									
										
										
										
											2018-12-18 13:08:08 +01:00
										 |  |  | void GPU_batch_copy(GPUBatch *batch_dst, GPUBatch *batch_src) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-18 13:08:08 +01:00
										 |  |  | 	GPU_batch_init_ex(batch_dst, GPU_PRIM_POINTS, batch_src->verts[0], batch_src->elem, 0); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-18 13:08:08 +01:00
										 |  |  | 	batch_dst->gl_prim_type = batch_src->gl_prim_type; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	for (int v = 1; v < GPU_BATCH_VBO_MAX_LEN; ++v) { | 
					
						
							| 
									
										
										
										
											2018-12-18 13:08:08 +01:00
										 |  |  | 		batch_dst->verts[v] = batch_src->verts[v]; | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-14 00:07:59 +01:00
										 |  |  | void GPU_batch_clear(GPUBatch *batch) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-12-18 13:08:08 +01:00
										 |  |  | 	if (batch->free_callback) { | 
					
						
							|  |  |  | 		batch->free_callback(batch, batch->callback_data); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	if (batch->owns_flag & GPU_BATCH_OWNS_INDEX) { | 
					
						
							|  |  |  | 		GPU_indexbuf_discard(batch->elem); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	if (batch->owns_flag & GPU_BATCH_OWNS_INSTANCES) { | 
					
						
							|  |  |  | 		GPU_vertbuf_discard(batch->inst); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	if ((batch->owns_flag & ~GPU_BATCH_OWNS_INDEX) != 0) { | 
					
						
							|  |  |  | 		for (int v = 0; v < GPU_BATCH_VBO_MAX_LEN; ++v) { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			if (batch->verts[v] == NULL) { | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (batch->owns_flag & (1 << v)) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 				GPU_vertbuf_discard(batch->verts[v]); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_batch_vao_cache_clear(batch); | 
					
						
							| 
									
										
										
										
											2018-12-14 00:07:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_batch_discard(GPUBatch *batch) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	GPU_batch_clear(batch); | 
					
						
							| 
									
										
										
										
											2018-07-31 16:54:58 +02:00
										 |  |  | 	MEM_freeN(batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_callback_free_set(GPUBatch *batch, void (*callback)(GPUBatch *, void *), void *user_data) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	batch->free_callback = callback; | 
					
						
							|  |  |  | 	batch->callback_data = user_data; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_instbuf_set(GPUBatch *batch, GPUVertBuf *inst, bool own_vbo) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							|  |  |  | 	assert(inst != NULL); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	/* redo the bindings */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_batch_vao_cache_clear(batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	if (batch->inst != NULL && (batch->owns_flag & GPU_BATCH_OWNS_INSTANCES)) { | 
					
						
							|  |  |  | 		GPU_vertbuf_discard(batch->inst); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	batch->inst = inst; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (own_vbo) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		batch->owns_flag |= GPU_BATCH_OWNS_INSTANCES; | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		batch->owns_flag &= ~GPU_BATCH_OWNS_INSTANCES; | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Returns the index of verts in the batch. */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | int GPU_batch_vertbuf_add_ex( | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  |         GPUBatch *batch, GPUVertBuf *verts, | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  |         bool own_vbo) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	/* redo the bindings */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_batch_vao_cache_clear(batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	for (uint v = 0; v < GPU_BATCH_VBO_MAX_LEN; ++v) { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		if (batch->verts[v] == NULL) { | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							|  |  |  | 			/* for now all VertexBuffers must have same vertex_len */ | 
					
						
							|  |  |  | 			assert(verts->vertex_len == batch->verts[0]->vertex_len); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 			batch->verts[v] = verts; | 
					
						
							| 
									
										
										
										
											2019-01-29 07:46:25 +11:00
										 |  |  | 			/* TODO: mark dirty so we can keep attribute bindings up-to-date */ | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			if (own_vbo) | 
					
						
							|  |  |  | 				batch->owns_flag |= (1 << v); | 
					
						
							|  |  |  | 			return v; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	/* we only make it this far if there is no room for another GPUVertBuf */ | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | #if TRUST_NO_ONE
 | 
					
						
							|  |  |  | 	assert(false); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | static GLuint batch_vao_get(GPUBatch *batch) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	/* Search through cache */ | 
					
						
							|  |  |  | 	if (batch->is_dynamic_vao_count) { | 
					
						
							|  |  |  | 		for (int i = 0; i < batch->dynamic_vaos.count; ++i) | 
					
						
							|  |  |  | 			if (batch->dynamic_vaos.interfaces[i] == batch->interface) | 
					
						
							|  |  |  | 				return batch->dynamic_vaos.vao_ids[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		for (int i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			if (batch->static_vaos.interfaces[i] == batch->interface) | 
					
						
							|  |  |  | 				return batch->static_vaos.vao_ids[i]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Set context of this batch.
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	 * It will be bound to it until GPU_batch_vao_cache_clear is called. | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	 * Until then it can only be drawn with this context. */ | 
					
						
							|  |  |  | 	if (batch->context == NULL) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		batch->context = GPU_context_active_get(); | 
					
						
							|  |  |  | 		gpu_context_add_batch(batch->context, batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		/* Make sure you are not trying to draw this batch in another context. */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		assert(batch->context == GPU_context_active_get()); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Cache miss, time to add a new entry! */ | 
					
						
							|  |  |  | 	GLuint new_vao = 0; | 
					
						
							|  |  |  | 	if (!batch->is_dynamic_vao_count) { | 
					
						
							|  |  |  | 		int i; /* find first unused slot */ | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 		for (i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			if (batch->static_vaos.vao_ids[i] == 0) | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		if (i < GPU_BATCH_VAO_STATIC_LEN) { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			batch->static_vaos.interfaces[i] = batch->interface; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 			batch->static_vaos.vao_ids[i] = new_vao = GPU_vao_alloc(); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			/* Not enough place switch to dynamic. */ | 
					
						
							|  |  |  | 			batch->is_dynamic_vao_count = true; | 
					
						
							|  |  |  | 			/* Erase previous entries, they will be added back if drawn again. */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 			for (int j = 0; j < GPU_BATCH_VAO_STATIC_LEN; ++j) { | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 				GPU_shaderinterface_remove_batch_ref((GPUShaderInterface *)batch->static_vaos.interfaces[j], batch); | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 				GPU_vao_free(batch->static_vaos.vao_ids[j], batch->context); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			/* Init dynamic arrays and let the branch below set the values. */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 			batch->dynamic_vaos.count = GPU_BATCH_VAO_DYN_ALLOC_COUNT; | 
					
						
							| 
									
										
										
										
											2018-07-31 16:54:58 +02:00
										 |  |  | 			batch->dynamic_vaos.interfaces = MEM_callocN(batch->dynamic_vaos.count * sizeof(GPUShaderInterface *), "dyn vaos interfaces"); | 
					
						
							|  |  |  | 			batch->dynamic_vaos.vao_ids = MEM_callocN(batch->dynamic_vaos.count * sizeof(GLuint), "dyn vaos ids"); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (batch->is_dynamic_vao_count) { | 
					
						
							|  |  |  | 		int i; /* find first unused slot */ | 
					
						
							|  |  |  | 		for (i = 0; i < batch->dynamic_vaos.count; ++i) | 
					
						
							|  |  |  | 			if (batch->dynamic_vaos.vao_ids[i] == 0) | 
					
						
							|  |  |  | 				break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (i == batch->dynamic_vaos.count) { | 
					
						
							|  |  |  | 			/* Not enough place, realloc the array. */ | 
					
						
							|  |  |  | 			i = batch->dynamic_vaos.count; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 			batch->dynamic_vaos.count += GPU_BATCH_VAO_DYN_ALLOC_COUNT; | 
					
						
							| 
									
										
										
										
											2018-07-31 16:54:58 +02:00
										 |  |  | 			batch->dynamic_vaos.interfaces = MEM_recallocN(batch->dynamic_vaos.interfaces, sizeof(GPUShaderInterface *) * batch->dynamic_vaos.count); | 
					
						
							|  |  |  | 			batch->dynamic_vaos.vao_ids = MEM_recallocN(batch->dynamic_vaos.vao_ids, sizeof(GLuint) * batch->dynamic_vaos.count); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		batch->dynamic_vaos.interfaces[i] = batch->interface; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		batch->dynamic_vaos.vao_ids[i] = new_vao = GPU_vao_alloc(); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 	GPU_shaderinterface_add_batch_ref((GPUShaderInterface *)batch->interface, batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							|  |  |  | 	assert(new_vao != 0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* We just got a fresh VAO we need to initialize it. */ | 
					
						
							|  |  |  | 	glBindVertexArray(new_vao); | 
					
						
							|  |  |  | 	batch_update_program_bindings(batch, 0); | 
					
						
							|  |  |  | 	glBindVertexArray(0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return new_vao; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_program_set_no_use(GPUBatch *batch, uint32_t program, const GPUShaderInterface *shaderface) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							|  |  |  | 	assert(glIsProgram(shaderface->program)); | 
					
						
							|  |  |  | 	assert(batch->program_in_use == 0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 	batch->interface = shaderface; | 
					
						
							|  |  |  | 	batch->program = program; | 
					
						
							|  |  |  | 	batch->vao_id = batch_vao_get(batch); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_program_set(GPUBatch *batch, uint32_t program, const GPUShaderInterface *shaderface) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_batch_program_set_no_use(batch, program, shaderface); | 
					
						
							|  |  |  | 	GPU_batch_program_use_begin(batch); /* hack! to make Batch_Uniform* simpler */ | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void gpu_batch_remove_interface_ref(GPUBatch *batch, const GPUShaderInterface *interface) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (batch->is_dynamic_vao_count) { | 
					
						
							|  |  |  | 		for (int i = 0; i < batch->dynamic_vaos.count; ++i) { | 
					
						
							|  |  |  | 			if (batch->dynamic_vaos.interfaces[i] == interface) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 				GPU_vao_free(batch->dynamic_vaos.vao_ids[i], batch->context); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 				batch->dynamic_vaos.vao_ids[i] = 0; | 
					
						
							|  |  |  | 				batch->dynamic_vaos.interfaces[i] = NULL; | 
					
						
							|  |  |  | 				break; /* cannot have duplicates */ | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		int i; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		for (i = 0; i < GPU_BATCH_VAO_STATIC_LEN; ++i) { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			if (batch->static_vaos.interfaces[i] == interface) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 				GPU_vao_free(batch->static_vaos.vao_ids[i], batch->context); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 				batch->static_vaos.vao_ids[i] = 0; | 
					
						
							|  |  |  | 				batch->static_vaos.interfaces[i] = NULL; | 
					
						
							|  |  |  | 				break; /* cannot have duplicates */ | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void create_bindings( | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 	GPUVertBuf *verts, const GPUShaderInterface *interface, | 
					
						
							|  |  |  | 	uint v_first, const bool use_instancing) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 	const GPUVertFormat *format = &verts->format; | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	const uint attr_len = format->attr_len; | 
					
						
							|  |  |  | 	const uint stride = format->stride; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_vertbuf_use(verts); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (uint a_idx = 0; a_idx < attr_len; ++a_idx) { | 
					
						
							| 
									
										
										
										
											2019-01-29 07:46:25 +11:00
										 |  |  | 		const GPUVertAttr *a = &format->attrs[a_idx]; | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 		const GLvoid *pointer = (const GLubyte *)0 + a->offset + v_first * stride; | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		for (uint n_idx = 0; n_idx < a->name_len; ++n_idx) { | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 			const GPUShaderInput *input = GPU_shaderinterface_attr(interface, a->name[n_idx]); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (input == NULL) continue; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (a->comp_len == 16 || a->comp_len == 12 || a->comp_len == 8) { | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 				assert(a->fetch_mode == GPU_FETCH_FLOAT); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 				assert(a->gl_comp_type == GL_FLOAT); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 				for (int i = 0; i < a->comp_len / 4; ++i) { | 
					
						
							|  |  |  | 					glEnableVertexAttribArray(input->location + i); | 
					
						
							|  |  |  | 					glVertexAttribDivisor(input->location + i, (use_instancing) ? 1 : 0); | 
					
						
							|  |  |  | 					glVertexAttribPointer(input->location + i, 4, a->gl_comp_type, GL_FALSE, stride, | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 					                      (const GLubyte *)pointer + i * 16); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 			else { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 				glEnableVertexAttribArray(input->location); | 
					
						
							|  |  |  | 				glVertexAttribDivisor(input->location, (use_instancing) ? 1 : 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				switch (a->fetch_mode) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 					case GPU_FETCH_FLOAT: | 
					
						
							|  |  |  | 					case GPU_FETCH_INT_TO_FLOAT: | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 						glVertexAttribPointer(input->location, a->comp_len, a->gl_comp_type, GL_FALSE, stride, pointer); | 
					
						
							|  |  |  | 						break; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 					case GPU_FETCH_INT_TO_FLOAT_UNIT: | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 						glVertexAttribPointer(input->location, a->comp_len, a->gl_comp_type, GL_TRUE, stride, pointer); | 
					
						
							|  |  |  | 						break; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 					case GPU_FETCH_INT: | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 						glVertexAttribIPointer(input->location, a->comp_len, a->gl_comp_type, stride, pointer); | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | static void batch_update_program_bindings(GPUBatch *batch, uint v_first) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	for (int v = 0; v < GPU_BATCH_VBO_MAX_LEN && batch->verts[v] != NULL; ++v) { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		create_bindings(batch->verts[v], batch->interface, (batch->inst) ? 0 : v_first, false); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (batch->inst) { | 
					
						
							|  |  |  | 		create_bindings(batch->inst, batch->interface, v_first, true); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (batch->elem) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		GPU_indexbuf_use(batch->elem); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_program_use_begin(GPUBatch *batch) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	/* NOTE: use_program & done_using_program are fragile, depend on staying in sync with
 | 
					
						
							|  |  |  | 	 *       the GL context's active program. use_program doesn't mark other programs as "not used". */ | 
					
						
							|  |  |  | 	/* TODO: make not fragile (somehow) */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!batch->program_in_use) { | 
					
						
							|  |  |  | 		glUseProgram(batch->program); | 
					
						
							|  |  |  | 		batch->program_in_use = true; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_program_use_end(GPUBatch *batch) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	if (batch->program_in_use) { | 
					
						
							|  |  |  | #if PROGRAM_NO_OPTI
 | 
					
						
							|  |  |  | 		glUseProgram(0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 		batch->program_in_use = false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							| 
									
										
										
										
											2019-01-16 04:41:27 +01:00
										 |  |  | #  define GET_UNIFORM const GPUShaderInput *uniform = GPU_shaderinterface_uniform_ensure(batch->interface, name); assert(uniform);
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2019-01-16 04:41:27 +01:00
										 |  |  | #  define GET_UNIFORM const GPUShaderInput *uniform = GPU_shaderinterface_uniform_ensure(batch->interface, name);
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_1ui(GPUBatch *batch, const char *name, int value) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform1ui(uniform->location, value); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_1i(GPUBatch *batch, const char *name, int value) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform1i(uniform->location, value); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_1b(GPUBatch *batch, const char *name, bool value) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform1i(uniform->location, value ? GL_TRUE : GL_FALSE); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_2f(GPUBatch *batch, const char *name, float x, float y) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform2f(uniform->location, x, y); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_3f(GPUBatch *batch, const char *name, float x, float y, float z) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform3f(uniform->location, x, y, z); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_4f(GPUBatch *batch, const char *name, float x, float y, float z, float w) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform4f(uniform->location, x, y, z, w); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_1f(GPUBatch *batch, const char *name, float x) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform1f(uniform->location, x); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_2fv(GPUBatch *batch, const char *name, const float data[2]) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform2fv(uniform->location, 1, data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_3fv(GPUBatch *batch, const char *name, const float data[3]) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform3fv(uniform->location, 1, data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_4fv(GPUBatch *batch, const char *name, const float data[4]) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform4fv(uniform->location, 1, data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_2fv_array(GPUBatch *batch, const char *name, const int len, const float *data) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform2fv(uniform->location, len, data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_4fv_array(GPUBatch *batch, const char *name, const int len, const float *data) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniform4fv(uniform->location, len, data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_uniform_mat4(GPUBatch *batch, const char *name, const float data[4][4]) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	GET_UNIFORM | 
					
						
							|  |  |  | 	glUniformMatrix4fv(uniform->location, 1, GL_FALSE, (const float *)data); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | static void primitive_restart_enable(const GPUIndexBuf *el) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | 	// TODO(fclem) Replace by GL_PRIMITIVE_RESTART_FIXED_INDEX when we have ogl 4.3
 | 
					
						
							|  |  |  | 	glEnable(GL_PRIMITIVE_RESTART); | 
					
						
							|  |  |  | 	GLuint restart_index = (GLuint)0xFFFFFFFF; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | #if GPU_TRACK_INDEX_RANGE
 | 
					
						
							|  |  |  | 	if (el->index_type == GPU_INDEX_U8) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		restart_index = (GLuint)0xFF; | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	else if (el->index_type == GPU_INDEX_U16) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		restart_index = (GLuint)0xFFFF; | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	glPrimitiveRestartIndex(restart_index); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void primitive_restart_disable(void) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	glDisable(GL_PRIMITIVE_RESTART); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 14:52:02 +02:00
										 |  |  | static void *elem_offset(const GPUIndexBuf *el, int v_first) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | #if GPU_TRACK_INDEX_RANGE
 | 
					
						
							|  |  |  | 	if (el->index_type == GPU_INDEX_U8) | 
					
						
							|  |  |  | 		return (GLubyte *)0 + v_first; | 
					
						
							|  |  |  | 	else if (el->index_type == GPU_INDEX_U16) | 
					
						
							|  |  |  | 		return (GLushort *)0 + v_first; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 		return (GLuint *)0 + v_first; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_draw(GPUBatch *batch) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	assert(batch->phase == GPU_BATCH_READY_TO_DRAW); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	assert(batch->verts[0]->vbo_id != 0); | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_batch_program_use_begin(batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	GPU_matrix_bind(batch->interface); // external call.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_batch_draw_range_ex(batch, 0, 0, false); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_batch_program_use_end(batch); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | void GPU_batch_draw_range_ex(GPUBatch *batch, int v_first, int v_count, bool force_instance) | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | { | 
					
						
							|  |  |  | #if TRUST_NO_ONE
 | 
					
						
							|  |  |  | 	assert(!(force_instance && (batch->inst == NULL)) || v_count > 0); // we cannot infer length if force_instance
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							| 
									
										
										
										
											2018-10-01 14:52:02 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	const bool do_instance = (force_instance || batch->inst); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// If using offset drawing, use the default VAO and redo bindings.
 | 
					
						
							| 
									
										
										
										
											2018-10-01 14:52:02 +02:00
										 |  |  | 	if (v_first != 0 && do_instance) { | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 		glBindVertexArray(GPU_vao_default()); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 		batch_update_program_bindings(batch, v_first); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		glBindVertexArray(batch->vao_id); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (do_instance) { | 
					
						
							|  |  |  | 		/* Infer length if vertex count is not given */ | 
					
						
							|  |  |  | 		if (v_count == 0) { | 
					
						
							|  |  |  | 			v_count = batch->inst->vertex_len; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (batch->elem) { | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 			const GPUIndexBuf *el = batch->elem; | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (el->use_prim_restart) { | 
					
						
							|  |  |  | 				primitive_restart_enable(el); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | #if GPU_TRACK_INDEX_RANGE
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			glDrawElementsInstancedBaseVertex(batch->gl_prim_type, | 
					
						
							|  |  |  | 			                                  el->index_len, | 
					
						
							|  |  |  | 			                                  el->gl_index_type, | 
					
						
							|  |  |  | 			                                  0, | 
					
						
							|  |  |  | 			                                  v_count, | 
					
						
							|  |  |  | 			                                  el->base_index); | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 			glDrawElementsInstanced(batch->gl_prim_type, el->index_len, GL_UNSIGNED_INT, 0, v_count); | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 			if (el->use_prim_restart) { | 
					
						
							|  |  |  | 				primitive_restart_disable(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			glDrawArraysInstanced(batch->gl_prim_type, 0, batch->verts[0]->vertex_len, v_count); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							|  |  |  | 		/* Infer length if vertex count is not given */ | 
					
						
							|  |  |  | 		if (v_count == 0) { | 
					
						
							|  |  |  | 			v_count = (batch->elem) ? batch->elem->index_len : batch->verts[0]->vertex_len; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (batch->elem) { | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 			const GPUIndexBuf *el = batch->elem; | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (el->use_prim_restart) { | 
					
						
							|  |  |  | 				primitive_restart_enable(el); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-01 14:52:02 +02:00
										 |  |  | 			void *v_first_ofs = elem_offset(el, v_first); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | #if GPU_TRACK_INDEX_RANGE
 | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			if (el->base_index) { | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 				glDrawRangeElementsBaseVertex( | 
					
						
							|  |  |  | 				        batch->gl_prim_type, | 
					
						
							|  |  |  | 				        el->min_index, | 
					
						
							|  |  |  | 				        el->max_index, | 
					
						
							|  |  |  | 				        v_count, | 
					
						
							|  |  |  | 				        el->gl_index_type, | 
					
						
							| 
									
										
										
										
											2018-10-01 14:52:02 +02:00
										 |  |  | 				        v_first_ofs, | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | 				        el->base_index); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			else { | 
					
						
							| 
									
										
										
										
											2018-10-01 14:52:02 +02:00
										 |  |  | 				glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, v_count, el->gl_index_type, v_first_ofs); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | #else
 | 
					
						
							| 
									
										
										
										
											2018-10-01 14:52:02 +02:00
										 |  |  | 			glDrawElements(batch->gl_prim_type, v_count, GL_UNSIGNED_INT, v_first_ofs); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 			if (el->use_prim_restart) { | 
					
						
							|  |  |  | 				primitive_restart_disable(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			glDrawArrays(batch->gl_prim_type, v_first, v_count); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Performance hog if you are drawing with the same vao multiple time.
 | 
					
						
							|  |  |  | 	 * Only activate for debugging. */ | 
					
						
							|  |  |  | 	// glBindVertexArray(0);
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* just draw some vertices and let shader place them where we want. */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | void GPU_draw_primitive(GPUPrimType prim_type, int v_count) | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 	/* we cannot draw without vao ... annoying ... */ | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	glBindVertexArray(GPU_vao_default()); | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	GLenum type = convert_prim_type_to_gl(prim_type); | 
					
						
							|  |  |  | 	glDrawArrays(type, 0, v_count); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Performance hog if you are drawing with the same vao multiple time.
 | 
					
						
							|  |  |  | 	 * Only activate for debugging.*/ | 
					
						
							|  |  |  | 	// glBindVertexArray(0);
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-07-17 21:11:23 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 16:21:23 +11:00
										 |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Utilities
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 10:33:14 +11:00
										 |  |  | void GPU_batch_program_set_shader(GPUBatch *batch, GPUShader *shader) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	GPU_batch_program_set(batch, shader->program, shader->interface); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void GPU_batch_program_set_builtin_with_config( | 
					
						
							| 
									
										
										
										
											2019-02-10 11:02:06 +11:00
										 |  |  |         GPUBatch *batch, eGPUBuiltinShader shader_id, eGPUShaderConfig sh_cfg) | 
					
						
							| 
									
										
										
										
											2016-09-15 18:41:28 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2019-02-10 11:02:06 +11:00
										 |  |  | 	GPUShader *shader = GPU_shader_get_builtin_shader_with_config(shader_id, sh_cfg); | 
					
						
							| 
									
										
										
										
											2018-07-18 00:12:21 +02:00
										 |  |  | 	GPU_batch_program_set(batch, shader->program, shader->interface); | 
					
						
							| 
									
										
										
										
											2016-09-15 18:41:28 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2017-02-08 00:38:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-06 10:33:14 +11:00
										 |  |  | void GPU_batch_program_set_builtin(GPUBatch *batch, eGPUBuiltinShader shader_id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	GPU_batch_program_set_builtin_with_config(batch, shader_id, GPU_SHADER_CFG_DEFAULT); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-15 20:38:06 +11:00
										 |  |  | /** \} */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* -------------------------------------------------------------------- */ | 
					
						
							|  |  |  | /** \name Init/Exit
 | 
					
						
							|  |  |  |  * \{ */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-08 00:38:07 +01:00
										 |  |  | void gpu_batch_init(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-15 16:21:23 +11:00
										 |  |  | 	gpu_batch_presets_init(); | 
					
						
							| 
									
										
										
										
											2017-02-08 00:38:07 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void gpu_batch_exit(void) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-01-15 16:21:23 +11:00
										 |  |  | 	gpu_batch_presets_exit(); | 
					
						
							| 
									
										
										
										
											2017-02-08 00:38:07 +01:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2018-01-15 16:21:23 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-18 23:09:31 +10:00
										 |  |  | /** \} */ |