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

batch.c « gawain « gpu « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 99c133011777999b88aaa1c12fe1425957a7c7c0 (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

// 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 <stdlib.h>

// necessary functions from matrix API
extern void gpuBindMatrices(GLuint program);
extern bool gpuMatricesDirty(void); // how best to use this here?

Batch* Batch_create(PrimitiveType prim_type, VertexBuffer* verts, ElementList* elem)
	{
#if TRUST_NO_ONE
	assert(verts != NULL);
	assert(prim_type == PRIM_POINTS || prim_type == PRIM_LINES || prim_type == PRIM_TRIANGLES);
	// we will allow other primitive types in a future update
#endif

	Batch* batch = calloc(1, sizeof(Batch));

	batch->verts = verts;
	batch->elem = elem;
	batch->prim_type = prim_type;
	batch->phase = READY_TO_DRAW;

	return batch;
	}

void Batch_discard(Batch* batch)
	{
	if (batch->vao_id)
		vao_id_free(batch->vao_id);

	free(batch);
	}

void Batch_discard_all(Batch* batch)
	{
	VertexBuffer_discard(batch->verts);
	if (batch->elem)
		ElementList_discard(batch->elem);
	Batch_discard(batch);
	}

void Batch_set_program(Batch* batch, GLuint program)
	{
#if TRUST_NO_ONE
	assert(glIsProgram(program));
#endif

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

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

static void Batch_update_program_bindings(Batch* batch)
	{
	const VertexFormat* format = &batch->verts->format;

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

	// disable all as a precaution
	// why are we not using prev_attrib_enabled_bits?? see immediate.c
	for (unsigned a_idx = 0; a_idx < MAX_VERTEX_ATTRIBS; ++a_idx)
		glDisableVertexAttribArray(a_idx);

	VertexBuffer_use(batch->verts);

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

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

		const GLint loc = glGetAttribLocation(batch->program, a->name);

#if TRUST_NO_ONE
		assert(loc != -1);
#endif

		glEnableVertexAttribArray(loc);

		switch (a->fetch_mode)
			{
			case KEEP_FLOAT:
			case CONVERT_INT_TO_FLOAT:
				glVertexAttribPointer(loc, a->comp_ct, a->comp_type, GL_FALSE, stride, pointer);
				break;
			case NORMALIZE_INT_TO_FLOAT:
				glVertexAttribPointer(loc, a->comp_ct, a->comp_type, GL_TRUE, stride, pointer);
				break;
			case KEEP_INT:
				glVertexAttribIPointer(loc, a->comp_ct, a->comp_type, stride, pointer);
			}
		}

	batch->program_dirty = false;
	}

void Batch_use_program(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 Batch_done_using_program(Batch* batch)
	{
	if (batch->program_in_use)
		{
		glUseProgram(0);
		batch->program_in_use = false;
		}
	}

void Batch_Uniform1b(Batch* batch, const char* name, bool value)
	{
	int loc = glGetUniformLocation(batch->program, name);

#if TRUST_NO_ONE
	assert(loc != -1);
#endif

	glUniform1i(loc, value ? GL_TRUE : GL_FALSE);
	}

void Batch_Uniform2f(Batch* batch, const char* name, float x, float y)
	{
	int loc = glGetUniformLocation(batch->program, name);

#if TRUST_NO_ONE
	assert(loc != -1);
#endif

	glUniform2f(loc, x, y);
	}

void Batch_Uniform4f(Batch* batch, const char* name, float x, float y, float z, float w)
	{
	int loc = glGetUniformLocation(batch->program, name);

#if TRUST_NO_ONE
	assert(loc != -1);
#endif

	glUniform4f(loc, x, y, z, w);
	}

void Batch_Uniform1f(Batch* batch, const char* name, float x)
	{
	int loc = glGetUniformLocation(batch->program, name);

#if TRUST_NO_ONE
	assert(loc != -1);
#endif

	glUniform1f(loc, x);
	}

void Batch_Uniform3fv(Batch* batch, const char* name, const float data[3])
	{
	int loc = glGetUniformLocation(batch->program, name);

#if TRUST_NO_ONE
	assert(loc != -1);
#endif

	glUniform3fv(loc, 1, data);
	}

void Batch_Uniform4fv(Batch* batch, const char* name, const float data[4])
	{
	int loc = glGetUniformLocation(batch->program, name);

#if TRUST_NO_ONE
	assert(loc != -1);
#endif

	glUniform4fv(loc, 1, data);
	}

static void Batch_prime(Batch* batch)
	{
	batch->vao_id = vao_id_alloc();
	glBindVertexArray(batch->vao_id);

	VertexBuffer_use(batch->verts);

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

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

void Batch_draw(Batch* batch)
	{
#if TRUST_NO_ONE
	assert(batch->phase == 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);

	Batch_use_program(batch);

	gpuBindMatrices(batch->program);

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

#if TRACK_INDEX_RANGE
		if (el->base_index)
			glDrawRangeElementsBaseVertex(batch->prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0, el->base_index);
		else
			glDrawRangeElements(batch->prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0);
#else
		glDrawElements(batch->prim_type, el->index_ct, GL_UNSIGNED_INT, 0);
#endif
		}
	else
		glDrawArrays(batch->prim_type, 0, batch->verts->vertex_ct);

	Batch_done_using_program(batch);
	glBindVertexArray(0);
	}

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

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

	// Batch_use_program(batch);

	//gpuBindMatrices(batch->program);

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

#if TRACK_INDEX_RANGE
		if (el->base_index)
			glDrawRangeElementsBaseVertex(batch->prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0, el->base_index);
		else
			glDrawRangeElements(batch->prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0);
#else
		glDrawElements(batch->prim_type, el->index_ct, GL_UNSIGNED_INT, 0);
#endif
		}
	else
		glDrawArrays(batch->prim_type, 0, batch->verts->vertex_ct);

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

/* clement : temp stuff */
void Batch_draw_stupid_instanced(Batch* batch, unsigned int instance_vbo, int instance_count)
{
	if (batch->vao_id)
		glBindVertexArray(batch->vao_id);
	else
		Batch_prime(batch);

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

	const GLint loc = glGetAttribLocation(batch->program, "InstanceModelMatrix");

#if TRUST_NO_ONE
	assert(loc != -1);
#endif

	glBindBuffer(GL_ARRAY_BUFFER, instance_vbo);
	glEnableVertexAttribArray(loc);
	glVertexAttribPointer(loc + 0, 4, GL_FLOAT, GL_FALSE, sizeof(float)*4*4, (GLvoid*)0);
	glEnableVertexAttribArray(loc + 1);
	glVertexAttribPointer(loc + 1, 4, GL_FLOAT, GL_FALSE, sizeof(float)*4*4, (GLvoid*)(sizeof(float)*4));
	glEnableVertexAttribArray(loc + 2);
	glVertexAttribPointer(loc + 2, 4, GL_FLOAT, GL_FALSE, sizeof(float)*4*4, (GLvoid*)(2 * sizeof(float)*4));
	glEnableVertexAttribArray(loc + 3);
	glVertexAttribPointer(loc + 3, 4, GL_FLOAT, GL_FALSE, sizeof(float)*4*4, (GLvoid*)(3 * sizeof(float)*4));
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glVertexAttribDivisor(loc + 0, 1);
	glVertexAttribDivisor(loc + 1, 1);
	glVertexAttribDivisor(loc + 2, 1);
	glVertexAttribDivisor(loc + 3, 1);

	// Batch_use_program(batch);

	//gpuBindMatrices(batch->program);

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

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

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