-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtinygp_gl.h
329 lines (282 loc) · 11.1 KB
/
tinygp_gl.h
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
#ifndef TINYGP_GL_H_INCLUDED
#define TINYGP_GL_H_INCLUDED
#include "tinygp.h"
#include <GLES2/gl2.h>
#include <stdbool.h>
#include <stdio.h>
#ifndef TGPGL_OFFSETOF
#define TGPGL_OFFSETOF offsetof
#include <stddef.h>
#endif
/**** header *****/
#if defined(GL_ES_VERSION_3_0) && GL_ES_VERSION_3_0
#define TGPGL_GLES3
#elif defined(GL_ES_VERSION_2_0) && GL_ES_VERSION_2_0
#define TGPGL_GLES2
#endif
#define TGPGL_GLSL_VERSION_STR_SIZE 32
typedef struct {
tgp_context* tgpctx;
GLuint gl_version;
char glsl_version_str[TGPGL_GLSL_VERSION_STR_SIZE];
GLuint vbo, elements;
GLuint shader_handle;
GLint attrib_location_tex;
GLint attrib_location_vtx_pos;
GLint attrib_location_vtx_uv;
GLint attrib_location_vtx_color;
GLuint white_texture;
} tgpgl_context;
TGPDEF void tgpgl_init_context(tgpgl_context* ctx, tgp_context* tgpctx);
TGPDEF void tgpgl_destroy_context(tgpgl_context* ctx);
TGPDEF void tgpgl_render(tgpgl_context* ctx);
/**** implementation *****/
// #ifdef TINYGPGL_IMPLEMENTATION
static bool tgpgl_check_shader(tgpgl_context* ctx, GLuint handle,
const char* desc) {
GLint status = 0, log_length = 0;
glGetShaderiv(handle, GL_COMPILE_STATUS, &status);
glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &log_length);
if ((GLboolean)status == GL_FALSE) {
fprintf(stderr,
"error: tgpgl_create_device_objects(): failed to compile %s! "
"GLSL %s",
desc, ctx->glsl_version_str);
}
if (log_length > 1) {
char buf[log_length];
glGetShaderInfoLog(handle, log_length, NULL, buf);
fprintf(stderr, "%s\n", buf);
}
return (GLboolean)status == GL_TRUE;
}
static bool tgpgl_check_program(tgpgl_context* ctx, GLuint handle,
const char* desc) {
GLint status = 0, log_length = 0;
glGetProgramiv(handle, GL_LINK_STATUS, &status);
glGetProgramiv(handle, GL_INFO_LOG_LENGTH, &log_length);
if ((GLboolean)status == GL_FALSE) {
fprintf(stderr,
"error: tgpgl_create_device_objects(): failed to link %s! "
"GLSL %s",
desc, ctx->glsl_version_str);
}
if (log_length > 1) {
char buf[log_length];
glGetProgramInfoLog(handle, log_length, NULL, buf);
fprintf(stderr, "%s\n", buf);
}
return (GLboolean)status == GL_TRUE;
}
static void tgpgl_create_device_objects(tgpgl_context* ctx) {
TINYGP_ASSERT(ctx != NULL);
static const GLchar* vertex_shader_glsl_120 =
"attribute vec2 coord;\n"
"attribute vec2 uv;\n"
"attribute vec4 color;\n"
"varying vec2 fragUV;\n"
"varying vec4 fragColor;\n"
"void main() {\n"
" fragUV = uv;\n"
" fragColor = color;\n"
" gl_Position = vec4(coord.xy, 0.0, 1.0);\n"
"}\n";
static const GLchar* fragment_shader_glsl_120 =
"#ifdef GL_ES\n"
"precision mediump float;\n"
"#endif\n"
"uniform sampler2D tex;\n"
"varying vec2 fragUV;\n"
"varying vec4 fragColor;\n"
"void main() {\n"
" gl_FragColor = fragColor * texture2D(tex, fragUV.st);\n"
"}\n";
// parse GLSL version
int glsl_version = 130;
sscanf(ctx->glsl_version_str, "#version %d", &glsl_version);
// select matching GLSL shader
const GLchar* vertex_shader = NULL;
const GLchar* fragment_shader = NULL;
if (glsl_version < 130) {
vertex_shader = vertex_shader_glsl_120;
fragment_shader = fragment_shader_glsl_120;
} else {
TINYGP_ASSERT(false && "not implemented");
}
// create shaders
const GLchar* vertex_shader_with_version[2] = {ctx->glsl_version_str,
vertex_shader};
GLuint vert_handle = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vert_handle, 2, vertex_shader_with_version, NULL);
glCompileShader(vert_handle);
tgpgl_check_shader(ctx, vert_handle, "vertex shader");
const GLchar* fragment_shader_with_version[2] = {ctx->glsl_version_str,
fragment_shader};
GLuint frag_handle = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(frag_handle, 2, fragment_shader_with_version, NULL);
glCompileShader(frag_handle);
tgpgl_check_shader(ctx, frag_handle, "fragment shader");
// link shaders
ctx->shader_handle = glCreateProgram();
glAttachShader(ctx->shader_handle, vert_handle);
glAttachShader(ctx->shader_handle, frag_handle);
glLinkProgram(ctx->shader_handle);
tgpgl_check_program(ctx, ctx->shader_handle, "shader program");
// the shaders are now linked into our program, we can get rid of them
glDetachShader(ctx->shader_handle, vert_handle);
glDetachShader(ctx->shader_handle, frag_handle);
glDeleteShader(vert_handle);
glDeleteShader(frag_handle);
// find attributes
ctx->attrib_location_tex = glGetUniformLocation(ctx->shader_handle, "tex");
ctx->attrib_location_vtx_pos =
glGetAttribLocation(ctx->shader_handle, "coord");
ctx->attrib_location_vtx_uv = glGetAttribLocation(ctx->shader_handle, "uv");
ctx->attrib_location_vtx_color =
glGetAttribLocation(ctx->shader_handle, "color");
// create buffers
glGenBuffers(1, &ctx->vbo);
glGenBuffers(1, &ctx->elements);
// create a white texture
uint8_t data[4 * 4 * 4];
for (int i = 0; i < 4 * 4 * 4; i++) {
data[i] = 255;
}
glGenTextures(1, &ctx->white_texture);
glBindTexture(GL_TEXTURE_2D, ctx->white_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 4, 4, 0, GL_RGBA, GL_UNSIGNED_BYTE,
data);
}
static inline void tgpgl_destroy_device_objects(tgpgl_context* ctx) {
glDeleteBuffers(1, &ctx->vbo);
glDeleteBuffers(1, &ctx->elements);
glDeleteProgram(ctx->shader_handle);
glDeleteTextures(1, &ctx->white_texture);
}
TGPDEF void tgpgl_init_context(tgpgl_context* ctx, tgp_context* tgpctx) {
TINYGP_ASSERT(ctx != NULL && tgpctx != NULL);
memset(ctx, 0, sizeof(*ctx));
ctx->tgpctx = tgpctx;
// find out GL version
#if !defined(TGPGL_GLES2)
GLint major = 0;
GLint minor = 0;
glGetIntegerv(GL_MAJOR_VERSION, &major);
glGetIntegerv(GL_MINOR_VERSION, &minor);
if (major == 0 && minor == 0) {
// in desktop GL 2.x, the GL_VERSION string starts with major.minor
const char* gl_version = (const char*)glGetString(GL_VERSION);
sscanf(gl_version, "%d.%d", &major, &minor);
}
ctx->gl_version = (GLuint)(major * 100 + minor * 10);
#else
ctx->gl_version = 200;
#endif
// determine the GLSL version string (will be prepended to the shader
// sources)
#if defined(TGPGL_GLES2)
const char* glsl_version = "#version 100";
#elif defined(TGPGL_GLES3)
const char* glsl_version = "#version 300 es";
#elif defined(__APPLE__)
const char* glsl_version = "#version 150";
#else
const char* glsl_version = "#version 130";
#endif
TINYGP_ASSERT(strlen(glsl_version) + 2 < TGPGL_GLSL_VERSION_STR_SIZE);
strcpy(ctx->glsl_version_str, glsl_version);
strcat(ctx->glsl_version_str, "\n");
tgpgl_create_device_objects(ctx);
}
TGPDEF void tgpgl_destroy_context(tgpgl_context* ctx) {
if (ctx != NULL) {
tgpgl_destroy_device_objects(ctx);
free(ctx);
}
}
static void tgpgl_setup_render_state(tgpgl_context* ctx) {
TINYGP_ASSERT(ctx != NULL);
// enable alpha blending, disable face culling, disable depth testing,
// enable scissor
glEnable(GL_BLEND);
glBlendEquation(GL_FUNC_ADD);
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE,
GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
glEnable(GL_SCISSOR_TEST);
// TODO: set glPolygonMode() and GL_PRIMITIVE_RESTART
glUseProgram(ctx->shader_handle);
glUniform1i(ctx->attrib_location_tex, 0);
// bind vertex and index buffers
glBindBuffer(GL_ARRAY_BUFFER, ctx->vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ctx->elements);
// setup attributes for tgp_vertex
glEnableVertexAttribArray(ctx->attrib_location_vtx_pos);
glEnableVertexAttribArray(ctx->attrib_location_vtx_uv);
glEnableVertexAttribArray(ctx->attrib_location_vtx_color);
glVertexAttribPointer(ctx->attrib_location_vtx_pos, 2, GL_FLOAT, GL_FALSE,
sizeof(tgp_vertex),
(GLvoid*)TGPGL_OFFSETOF(tgp_vertex, position));
glVertexAttribPointer(ctx->attrib_location_vtx_uv, 2, GL_FLOAT, GL_FALSE,
sizeof(tgp_vertex),
(GLvoid*)TGPGL_OFFSETOF(tgp_vertex, texcoord));
glVertexAttribPointer(ctx->attrib_location_vtx_color, 4, GL_FLOAT, GL_FALSE,
sizeof(tgp_vertex),
(GLvoid*)TGPGL_OFFSETOF(tgp_vertex, color));
}
TGPDEF void tgpgl_render(tgpgl_context* ctx) {
TINYGP_ASSERT(ctx != NULL);
tgp_context* tgpctx = ctx->tgpctx;
// setup desired GL state
tgpgl_setup_render_state(ctx);
// render draw commands
uint32_t i = 0;
tgp_command cmd;
int num_drawcalls = 0;
while (tgp_get_command_p(tgpctx, &cmd, i++)) {
switch (cmd.type) {
case TGP_COMMAND_CLEAR:
glClearColor(cmd.data.clear.r, cmd.data.clear.g, cmd.data.clear.b,
cmd.data.clear.a);
glClear(GL_COLOR_BUFFER_BIT);
break;
case TGP_COMMAND_VIEWPORT:
glViewport(cmd.data.viewport.x, cmd.data.viewport.y,
cmd.data.viewport.w, cmd.data.viewport.h);
break;
case TGP_COMMAND_SCISSOR:
glScissor(cmd.data.scissor.x, cmd.data.scissor.y,
cmd.data.scissor.w, cmd.data.scissor.h);
break;
case TGP_COMMAND_DRAW: {
tgp_draw_command draw = cmd.data.draw;
printf("drawcall %d: ", ++num_drawcalls);
// upload vertex/index buffers
GLsizeiptr vtxbuf_size = sizeof(tgp_vertex) * draw.num_vertices;
GLsizeiptr idxbuf_size = sizeof(tgp_index) * draw.num_indices;
glBufferData(GL_ARRAY_BUFFER, vtxbuf_size,
&tgpctx->vertices[draw.vtx_offset], GL_STREAM_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, idxbuf_size,
&tgpctx->indices[draw.idx_offset], GL_STREAM_DRAW);
for (int i = 0; i < draw.num_indices; i++) {
printf("%d ", tgpctx->indices[draw.idx_offset + i]);
}
printf("\n");
// draw
glBindTexture(GL_TEXTURE_2D, ctx->white_texture);
glDrawElements(
GL_TRIANGLES, draw.num_indices,
sizeof(tgp_index) == 2 ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT,
(void*)(intptr_t)(draw.idx_offset * sizeof(tgp_index)));
break;
}
case TGP_COMMAND_NONE: break;
}
}
}
// #endif // TINYGPGL_IMPLEMENTATION
#endif // TINYGP_GL_H_INCLUDED