Welcome to mirror list, hosted at ThFree Co, Russian Federation.

batch.c « src « gawain « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 17e450846138b224d539f34092558e21ea4f6981 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442

// Gawain geometry batch
//
// This code is part of the Gawain library, with modifications
// specific to integration with Blender.
//
// Copyright 2016 Mike Erwin
//
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of
// the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

#include "batch.h"
#include "buffer_id.h"
#include "primitive_private.h"
#include <stdlib.h>

// necessary functions from matrix API
extern void gpuBindMatrices(const Gwn_ShaderInterface* shaderface);
extern bool gpuMatricesDirty(void); // how best to use this here?

Gwn_Batch* GWN_batch_create(Gwn_PrimType prim_type, Gwn_VertBuf* verts, Gwn_IndexBuf* elem)
	{
	Gwn_Batch* batch = calloc(1, sizeof(Gwn_Batch));

	GWN_batch_init(batch, prim_type, verts, elem);

	return batch;
	}

void GWN_batch_init(Gwn_Batch* batch, Gwn_PrimType prim_type, Gwn_VertBuf* verts, Gwn_IndexBuf* elem)
	{
#if TRUST_NO_ONE
	assert(verts != NULL);
#endif

	batch->verts[0] = verts;
	for (int v = 1; v < GWN_BATCH_VBO_MAX_LEN; ++v)
		batch->verts[v] = NULL;
	batch->elem = elem;
	batch->prim_type = prim_type;
	batch->gl_prim_type = convert_prim_type_to_gl(prim_type);
	batch->phase = GWN_BATCH_READY_TO_DRAW;
	}

void GWN_batch_discard(Gwn_Batch* batch)
	{
	if (batch->vao_id)
		GWN_vao_free(batch->vao_id);

	free(batch);
	}

void GWN_batch_discard_all(Gwn_Batch* batch)
	{
	for (int v = 0; v < GWN_BATCH_VBO_MAX_LEN; ++v)
		{
		if (batch->verts[v] == NULL)
			break;
		GWN_vertbuf_discard(batch->verts[v]);
		}

	if (batch->elem)
		GWN_indexbuf_discard(batch->elem);

	GWN_batch_discard(batch);
	}

int GWN_batch_vertbuf_add(Gwn_Batch* batch, Gwn_VertBuf* verts)
	{
	for (unsigned v = 0; v < GWN_BATCH_VBO_MAX_LEN; ++v)
		{
		if (batch->verts[v] == NULL)
			{
#if TRUST_NO_ONE
			// for now all VertexBuffers must have same vertex_ct
			assert(verts->vertex_ct == batch->verts[0]->vertex_ct);
			// in the near future we will enable instanced attribs which have their own vertex_ct
#endif
			batch->verts[v] = verts;
			// TODO: mark dirty so we can keep attrib bindings up-to-date
			return v;
			}
		}
	
	// we only make it this far if there is no room for another Gwn_VertBuf
#if TRUST_NO_ONE
	assert(false);
#endif
	return -1;
	}

void GWN_batch_program_set(Gwn_Batch* batch, GLuint program, const Gwn_ShaderInterface* shaderface)
	{
#if TRUST_NO_ONE
	assert(glIsProgram(program));
#endif

	batch->program = program;
	batch->interface = shaderface;
	batch->program_dirty = true;

	GWN_batch_program_use_begin(batch); // hack! to make Batch_Uniform* simpler
	}

static void Batch_update_program_bindings(Gwn_Batch* batch)
	{
	// disable all as a precaution
	// why are we not using prev_attrib_enabled_bits?? see immediate.c
	for (unsigned a_idx = 0; a_idx < GWN_VERT_ATTR_MAX_LEN; ++a_idx)
		glDisableVertexAttribArray(a_idx);

	for (int v = 0; v < GWN_BATCH_VBO_MAX_LEN; ++v)
		{
		Gwn_VertBuf* verts = batch->verts[v];
		if (verts == NULL)
			break;

		const Gwn_VertFormat* format = &verts->format;

		const unsigned attrib_ct = format->attrib_ct;
		const unsigned stride = format->stride;

		GWN_vertbuf_use(verts);

		for (unsigned a_idx = 0; a_idx < attrib_ct; ++a_idx)
			{
			const Gwn_VertAttr* a = format->attribs + a_idx;

			const GLvoid* pointer = (const GLubyte*)0 + a->offset;

			for (unsigned n_idx = 0; n_idx < a->name_ct; ++n_idx)
				{
				const Gwn_ShaderInput* input = GWN_shaderinterface_attr(batch->interface, a->name[n_idx]);

				if (input == NULL) continue;

				glEnableVertexAttribArray(input->location);

				switch (a->fetch_mode)
					{
					case GWN_FETCH_FLOAT:
					case GWN_FETCH_INT_TO_FLOAT:
						glVertexAttribPointer(input->location, a->comp_ct, a->gl_comp_type, GL_FALSE, stride, pointer);
						break;
					case GWN_FETCH_INT_TO_FLOAT_UNIT:
						glVertexAttribPointer(input->location, a->comp_ct, a->gl_comp_type, GL_TRUE, stride, pointer);
						break;
					case GWN_FETCH_INT:
						glVertexAttribIPointer(input->location, a->comp_ct, a->gl_comp_type, stride, pointer);
					}
				}
			}
		}

	batch->program_dirty = false;
	}

void GWN_batch_program_use_begin(Gwn_Batch* batch)
	{
	// 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;
		}
	}

void GWN_batch_program_use_end(Gwn_Batch* batch)
	{
	if (batch->program_in_use)
		{
		glUseProgram(0);
		batch->program_in_use = false;
		}
	}

#if TRUST_NO_ONE
  #define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(batch->interface, name); assert(uniform);
#else
  #define GET_UNIFORM const Gwn_ShaderInput* uniform = GWN_shaderinterface_uniform(batch->interface, name);
#endif

void GWN_batch_uniform_1i(Gwn_Batch* batch, const char* name, int value)
	{
	GET_UNIFORM
	glUniform1i(uniform->location, value);
	}

void GWN_batch_uniform_1b(Gwn_Batch* batch, const char* name, bool value)
	{
	GET_UNIFORM
	glUniform1i(uniform->location, value ? GL_TRUE : GL_FALSE);
	}

void GWN_batch_uniform_2f(Gwn_Batch* batch, const char* name, float x, float y)
	{
	GET_UNIFORM
	glUniform2f(uniform->location, x, y);
	}

void GWN_batch_uniform_3f(Gwn_Batch* batch, const char* name, float x, float y, float z)
	{
	GET_UNIFORM
	glUniform3f(uniform->location, x, y, z);
	}

void GWN_batch_uniform_4f(Gwn_Batch* batch, const char* name, float x, float y, float z, float w)
	{
	GET_UNIFORM
	glUniform4f(uniform->location, x, y, z, w);
	}

void GWN_batch_uniform_1f(Gwn_Batch* batch, const char* name, float x)
	{
	GET_UNIFORM
	glUniform1f(uniform->location, x);
	}

void GWN_batch_uniform_3fv(Gwn_Batch* batch, const char* name, const float data[3])
	{
	GET_UNIFORM
	glUniform3fv(uniform->location, 1, data);
	}

void GWN_batch_uniform_4fv(Gwn_Batch* batch, const char* name, const float data[4])
	{
	GET_UNIFORM
	glUniform4fv(uniform->location, 1, data);
	}

static void Batch_prime(Gwn_Batch* batch)
	{
	batch->vao_id = GWN_vao_alloc();
	glBindVertexArray(batch->vao_id);

	for (int v = 0; v < GWN_BATCH_VBO_MAX_LEN; ++v)
		{
		if (batch->verts[v] == NULL)
			break;
		GWN_vertbuf_use(batch->verts[v]);
		}

	if (batch->elem)
		GWN_indexbuf_use(batch->elem);

	// vertex attribs and element list remain bound to this VAO
	}

void GWN_batch_draw(Gwn_Batch* batch)
	{
#if TRUST_NO_ONE
	assert(batch->phase == GWN_BATCH_READY_TO_DRAW);
	assert(glIsProgram(batch->program));
#endif

	if (batch->vao_id)
		glBindVertexArray(batch->vao_id);
	else
		Batch_prime(batch);

	if (batch->program_dirty)
		Batch_update_program_bindings(batch);

	GWN_batch_program_use_begin(batch);

	gpuBindMatrices(batch->interface);

	if (batch->elem)
		{
		const Gwn_IndexBuf* el = batch->elem;

#if GWN_TRACK_INDEX_RANGE
		if (el->base_index)
			glDrawRangeElementsBaseVertex(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->gl_index_type, 0, el->base_index);
		else
			glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->gl_index_type, 0);
#else
		glDrawElements(batch->gl_prim_type, el->index_ct, GL_UNSIGNED_INT, 0);
#endif
		}
	else
		glDrawArrays(batch->gl_prim_type, 0, batch->verts[0]->vertex_ct);

	GWN_batch_program_use_end(batch);
	glBindVertexArray(0);
	}



// clement : temp stuff
void GWN_batch_draw_stupid(Gwn_Batch* batch)
	{
	if (batch->vao_id)
		glBindVertexArray(batch->vao_id);
	else
		Batch_prime(batch);

	if (batch->program_dirty)
		Batch_update_program_bindings(batch);

	// GWN_batch_program_use_begin(batch);

	//gpuBindMatrices(batch->program);

	if (batch->elem)
		{
		const Gwn_IndexBuf* el = batch->elem;

#if GWN_TRACK_INDEX_RANGE
		if (el->base_index)
			glDrawRangeElementsBaseVertex(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->gl_index_type, 0, el->base_index);
		else
			glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->gl_index_type, 0);
#else
		glDrawElements(batch->gl_prim_type, el->index_ct, GL_UNSIGNED_INT, 0);
#endif
		}
	else
		glDrawArrays(batch->gl_prim_type, 0, batch->verts[0]->vertex_ct);

	// GWN_batch_program_use_end(batch);
	glBindVertexArray(0);
	}

// clement : temp stuff
void GWN_batch_draw_stupid_instanced(Gwn_Batch* batch, unsigned int instance_vbo, int instance_count,
                                 int attrib_nbr, int attrib_stride, int attrib_size[16], int attrib_loc[16])
	{
	if (batch->vao_id)
		glBindVertexArray(batch->vao_id);
	else
		Batch_prime(batch);

	if (batch->program_dirty)
		Batch_update_program_bindings(batch);

	glBindBuffer(GL_ARRAY_BUFFER, instance_vbo);
	int ptr_ofs = 0;
	for (int i = 0; i < attrib_nbr; ++i)
		{
		int size = attrib_size[i];
		int loc = attrib_loc[i];
		int atr_ofs = 0;

		while (size > 0)
			{
			glEnableVertexAttribArray(loc + atr_ofs);
			glVertexAttribPointer(loc + atr_ofs, (size > 4) ? 4 : size, GL_FLOAT, GL_FALSE,
			                      sizeof(float) * attrib_stride, (GLvoid*)(sizeof(float) * ptr_ofs));
			glVertexAttribDivisor(loc + atr_ofs, 1);
			atr_ofs++;
			ptr_ofs += (size > 4) ? 4 : size;
			size -= 4;
			}
		}
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	// GWN_batch_program_use_begin(batch);

	//gpuBindMatrices(batch->program);

	if (batch->elem)
		{
		const Gwn_IndexBuf* el = batch->elem;

		glDrawElementsInstanced(batch->gl_prim_type, el->index_ct, GL_UNSIGNED_INT, 0, instance_count);
		}
	else
		glDrawArraysInstanced(batch->gl_prim_type, 0, batch->verts[0]->vertex_ct, instance_count);

	// GWN_batch_program_use_end(batch);
	glBindVertexArray(0);
	}

void GWN_batch_draw_stupid_instanced_with_batch(Gwn_Batch* batch_instanced, Gwn_Batch* batch_instancing)
	{
	if (batch_instanced->vao_id)
		glBindVertexArray(batch_instanced->vao_id);
	else
		Batch_prime(batch_instanced);

	if (batch_instanced->program_dirty)
		Batch_update_program_bindings(batch_instanced);

	Gwn_VertBuf* verts = batch_instancing->verts[0];

	const Gwn_VertFormat* format = &verts->format;

	const unsigned attrib_ct = format->attrib_ct;
	const unsigned stride = format->stride;

	GWN_vertbuf_use(verts);

	for (unsigned a_idx = 0; a_idx < attrib_ct; ++a_idx)
		{
		const Gwn_VertAttr* a = format->attribs + a_idx;

		const GLvoid* pointer = (const GLubyte*)0 + a->offset;

		for (unsigned n_idx = 0; n_idx < a->name_ct; ++n_idx)
			{
			const Gwn_ShaderInput* input = GWN_shaderinterface_attr(batch_instanced->interface, a->name[n_idx]);

			if (input == NULL) continue;

			glEnableVertexAttribArray(input->location);
			glVertexAttribDivisor(input->location, 1);

			switch (a->fetch_mode)
				{
				case GWN_FETCH_FLOAT:
				case GWN_FETCH_INT_TO_FLOAT:
					glVertexAttribPointer(input->location, a->comp_ct, a->gl_comp_type, GL_FALSE, stride, pointer);
					break;
				case GWN_FETCH_INT_TO_FLOAT_UNIT:
					glVertexAttribPointer(input->location, a->comp_ct, a->gl_comp_type, GL_TRUE, stride, pointer);
					break;
				case GWN_FETCH_INT:
					glVertexAttribIPointer(input->location, a->comp_ct, a->gl_comp_type, stride, pointer);
				}
			}
		}

	// GWN_batch_program_use_begin(batch);

	//gpuBindMatrices(batch->program);

	if (batch_instanced->elem)
		{
		const Gwn_IndexBuf* el = batch_instanced->elem;

		glDrawElementsInstanced(batch_instanced->gl_prim_type, el->index_ct, GL_UNSIGNED_INT, 0, verts->vertex_ct);
		}
	else
		glDrawArraysInstanced(batch_instanced->gl_prim_type, 0, batch_instanced->verts[0]->vertex_ct, verts->vertex_ct);

	// GWN_batch_program_use_end(batch);
	glBindVertexArray(0);
	}