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
|
// 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(const ShaderInterface* shaderface);
extern bool gpuMatricesDirty(void); // how best to use this here?
Batch* Batch_create(PrimitiveType prim_type, VertexBuffer* verts, ElementList* elem)
{
Batch* batch = calloc(1, sizeof(Batch));
Batch_init(batch, prim_type, verts, elem);
return batch;
}
void Batch_init(Batch* batch, PrimitiveType prim_type, VertexBuffer* verts, ElementList* elem)
{
#if TRUST_NO_ONE
assert(verts != NULL);
#endif
batch->verts[0] = verts;
for (int v = 1; v < BATCH_MAX_VBO_CT; ++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 = READY_TO_DRAW;
}
void Batch_discard(Batch* batch)
{
if (batch->vao_id)
vao_id_free(batch->vao_id);
free(batch);
}
void Batch_discard_all(Batch* batch)
{
for (int v = 0; v < BATCH_MAX_VBO_CT; ++v)
{
if (batch->verts[v] == NULL)
break;
VertexBuffer_discard(batch->verts[v]);
}
if (batch->elem)
ElementList_discard(batch->elem);
Batch_discard(batch);
}
int Batch_add_VertexBuffer(Batch* batch, VertexBuffer* verts)
{
for (unsigned v = 0; v < BATCH_MAX_VBO_CT; ++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 VertexBuffer
#if TRUST_NO_ONE
assert(false);
#endif
return -1;
}
void Batch_set_program(Batch* batch, GLuint program, const ShaderInterface* shaderface)
{
#if TRUST_NO_ONE
assert(glIsProgram(program));
#endif
batch->program = program;
batch->interface = shaderface;
batch->program_dirty = true;
Batch_use_program(batch); // hack! to make Batch_Uniform* simpler
}
static void Batch_update_program_bindings(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 < MAX_VERTEX_ATTRIBS; ++a_idx)
glDisableVertexAttribArray(a_idx);
for (int v = 0; v < BATCH_MAX_VBO_CT; ++v)
{
VertexBuffer* verts = batch->verts[v];
if (verts == NULL)
break;
const VertexFormat* format = &verts->format;
const unsigned attrib_ct = format->attrib_ct;
const unsigned stride = format->stride;
VertexBuffer_use(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 ShaderInput* input = ShaderInterface_attrib(batch->interface, a->name);
if (input == NULL) continue;
glEnableVertexAttribArray(input->location);
switch (a->fetch_mode)
{
case KEEP_FLOAT:
case CONVERT_INT_TO_FLOAT:
glVertexAttribPointer(input->location, a->comp_ct, a->gl_comp_type, GL_FALSE, stride, pointer);
break;
case NORMALIZE_INT_TO_FLOAT:
glVertexAttribPointer(input->location, a->comp_ct, a->gl_comp_type, GL_TRUE, stride, pointer);
break;
case KEEP_INT:
glVertexAttribIPointer(input->location, a->comp_ct, a->gl_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;
}
}
#if TRUST_NO_ONE
#define GET_UNIFORM const ShaderInput* uniform = ShaderInterface_uniform(batch->interface, name); assert(uniform);
#else
#define GET_UNIFORM const ShaderInput* uniform = ShaderInterface_uniform(batch->interface, name);
#endif
void Batch_Uniform1i(Batch* batch, const char* name, int value)
{
GET_UNIFORM
glUniform1i(uniform->location, value);
}
void Batch_Uniform1b(Batch* batch, const char* name, bool value)
{
GET_UNIFORM
glUniform1i(uniform->location, value ? GL_TRUE : GL_FALSE);
}
void Batch_Uniform2f(Batch* batch, const char* name, float x, float y)
{
GET_UNIFORM
glUniform2f(uniform->location, x, y);
}
void Batch_Uniform3f(Batch* batch, const char* name, float x, float y, float z)
{
GET_UNIFORM
glUniform3f(uniform->location, x, y, z);
}
void Batch_Uniform4f(Batch* batch, const char* name, float x, float y, float z, float w)
{
GET_UNIFORM
glUniform4f(uniform->location, x, y, z, w);
}
void Batch_Uniform1f(Batch* batch, const char* name, float x)
{
GET_UNIFORM
glUniform1f(uniform->location, x);
}
void Batch_Uniform3fv(Batch* batch, const char* name, const float data[3])
{
GET_UNIFORM
glUniform3fv(uniform->location, 1, data);
}
void Batch_Uniform4fv(Batch* batch, const char* name, const float data[4])
{
GET_UNIFORM
glUniform4fv(uniform->location, 1, data);
}
static void Batch_prime(Batch* batch)
{
batch->vao_id = vao_id_alloc();
glBindVertexArray(batch->vao_id);
for (int v = 0; v < BATCH_MAX_VBO_CT; ++v)
{
if (batch->verts[v] == NULL)
break;
VertexBuffer_use(batch->verts[v]);
}
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->interface);
if (batch->elem)
{
const ElementList* el = batch->elem;
#if TRACK_INDEX_RANGE
if (el->base_index)
glDrawRangeElementsBaseVertex(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0, el->base_index);
else
glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->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);
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->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->index_type, 0, el->base_index);
else
glDrawRangeElements(batch->gl_prim_type, el->min_index, el->max_index, el->index_ct, el->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);
// Batch_done_using_program(batch);
glBindVertexArray(0);
}
// clement : temp stuff
void Batch_draw_stupid_instanced(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);
// Batch_use_program(batch);
//gpuBindMatrices(batch->program);
if (batch->elem)
{
const ElementList* 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);
// Batch_done_using_program(batch);
glBindVertexArray(0);
}
|