Debian Codegraph @

test/testgles2.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11 */
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <math.h>
16
17 #ifdef __EMSCRIPTEN__
18 #include <emscripten/emscripten.h>
19 #endif
20
21 #include "SDL_test_common.h"
22
23 #if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__NACL__) \
24 || defined(__WINDOWS__) || defined(__LINUX__)
25 #ifndef HAVE_OPENGLES2
26 #define HAVE_OPENGLES2
27 #endif
28 #endif
29
30 #ifdef HAVE_OPENGLES2
31
32 #include "SDL_opengles2.h"
33
34 typedef struct GLES2_Context
35 {
36 #define SDL_PROC(ret, func, params) ret (APIENTRY *func) params;
37 #if 0
38 #include "../src/render/opengles2/SDL_gles2funcs.h"
39 #else
40 #include "gles2funcs.h"
41 #endif
42 #undef SDL_PROC
43 } GLES2_Context;
44
45 typedef struct shader_data
46 {
47 GLuint shader_program, shader_frag, shader_vert;
48
49 GLint attr_position;
50 GLint attr_color, attr_mvp;
51
52 int angle_x, angle_y, angle_z;
53
54 GLuint position_buffer;
55 GLuint color_buffer;
56 } shader_data;
57
58 typedef struct thread_data
59 {
60 SDL_Thread *thread;
61 int done;
62 int index;
63 } thread_data;
64
65 static SDLTest_CommonState *state;
66 static SDL_GLContext *context = NULL;
67 static int depth = 16;
68 static GLES2_Context ctx;
69
70 static int LoadContext(GLES2_Context *data)
71 {
72 #ifdef SDL_VIDEO_DRIVER_UIKIT
73 #define __SDL_NOGETPROCADDR__
74 #elif defined(SDL_VIDEO_DRIVER_ANDROID)
75 #define __SDL_NOGETPROCADDR__
76 #elif defined(SDL_VIDEO_DRIVER_PANDORA)
77 #define __SDL_NOGETPROCADDR__
78 #endif
79
80 #if defined __SDL_NOGETPROCADDR__
81 #define SDL_PROC(ret, func, params) data->func = func;
82 #else
83 #define SDL_PROC(ret, func, params) \
84 do { \
85 data->func = SDL_GL_GetProcAddress(#func); \
86 if (!data->func) { \
87 return SDL_SetError("Couldn't load GLES2 function %s: %s", #func, SDL_GetError()); \
88 } \
89 } while (0);
90 #endif /* __SDL_NOGETPROCADDR__ */
91
92 #if 0
93 #include "../src/render/opengles2/SDL_gles2funcs.h"
94 #else
95 #include "gles2funcs.h"
96 #endif
97 #undef SDL_PROC
98 return 0;
99 }
100
101 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
102 static void
103 quit(int rc)
104 {
105 int i;
106
107 if (context) {
108 for (i = 0; i < state->num_windows; i++) {
109 if (context[i]) {
110 SDL_GL_DeleteContext(context[i]);
111 }
112 }
113
114 SDL_free(context);
115 }
116
117 SDLTest_CommonQuit(state);
118 exit(rc);
119 }
120
121 #define GL_CHECK(x) \
122 x; \
123 { \
124 GLenum glError = ctx.glGetError(); \
125 if (glError != GL_NO_ERROR) { \
126 SDL_Log("glGetError() = %i (0x%.8x) at line %i\n", glError, glError, __LINE__); \
127 quit(1); \
128 } \
129 }
130
131 /*
132 * Simulates desktop's glRotatef. The matrix is returned in column-major
133 * order.
134 */
135 static void
136 rotate_matrix(float angle, float x, float y, float z, float *r)
137 {
138 float radians, c, s, c1, u[3], length;
139 int i, j;
140
141 radians = (float)(angle * M_PI) / 180.0f;
142
143 c = SDL_cosf(radians);
144 s = SDL_sinf(radians);
145
146 c1 = 1.0f - SDL_cosf(radians);
147
148 length = (float)SDL_sqrt(x * x + y * y + z * z);
149
150 u[0] = x / length;
151 u[1] = y / length;
152 u[2] = z / length;
153
154 for (i = 0; i < 16; i++) {
155 r[i] = 0.0;
156 }
157
158 r[15] = 1.0;
159
160 for (i = 0; i < 3; i++) {
161 r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
162 r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
163 }
164
165 for (i = 0; i < 3; i++) {
166 for (j = 0; j < 3; j++) {
167 r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0f);
168 }
169 }
170 }
171
172 /*
173 * Simulates gluPerspectiveMatrix
174 */
175 static void
176 perspective_matrix(float fovy, float aspect, float znear, float zfar, float *r)
177 {
178 int i;
179 float f;
180
181 f = 1.0f / SDL_tanf(fovy * 0.5f);
182
183 for (i = 0; i < 16; i++) {
184 r[i] = 0.0;
185 }
186
187 r[0] = f / aspect;
188 r[5] = f;
189 r[10] = (znear + zfar) / (znear - zfar);
190 r[11] = -1.0f;
191 r[14] = (2.0f * znear * zfar) / (znear - zfar);
192 r[15] = 0.0f;
193 }
194
195 /*
196 * Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
197 * major. In-place multiplication is supported.
198 */
199 static void
200 multiply_matrix(const float *lhs, const float *rhs, float *r)
201 {
202 int i, j, k;
203 float tmp[16];
204
205 for (i = 0; i < 4; i++) {
206 for (j = 0; j < 4; j++) {
207 tmp[j * 4 + i] = 0.0;
208
209 for (k = 0; k < 4; k++) {
210 tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
211 }
212 }
213 }
214
215 for (i = 0; i < 16; i++) {
216 r[i] = tmp[i];
217 }
218 }
219
220 /*
221 * Create shader, load in source, compile, dump debug as necessary.
222 *
223 * shader: Pointer to return created shader ID.
224 * source: Passed-in shader source code.
225 * shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
226 */
227 static void
228 process_shader(GLuint *shader, const char *source, GLint shader_type)
229 {
230 GLint status = GL_FALSE;
231 const char *shaders[1] = { NULL };
232 char buffer[1024];
233 GLsizei length = 0;
234
235 /* Create shader and load into GL. */
236 *shader = GL_CHECK(ctx.glCreateShader(shader_type));
237
238 shaders[0] = source;
239
240 GL_CHECK(ctx.glShaderSource(*shader, 1, shaders, NULL));
241
242 /* Clean up shader source. */
243 shaders[0] = NULL;
244
245 /* Try compiling the shader. */
246 GL_CHECK(ctx.glCompileShader(*shader));
247 GL_CHECK(ctx.glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
248
249 /* Dump debug info (source and log) if compilation failed. */
250 if (status != GL_TRUE) {
251 ctx.glGetShaderInfoLog(*shader, sizeof(buffer), &length, &buffer[0]);
252 buffer[length] = '\0';
253 SDL_Log("Shader compilation failed: %s", buffer);
254 fflush(stderr);
255 quit(-1);
256 }
257 }
258
259 static void
260 link_program(struct shader_data *data)
261 {
262 GLint status = GL_FALSE;
263 char buffer[1024];
264 GLsizei length = 0;
265
266 GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
267 GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
268 GL_CHECK(ctx.glLinkProgram(data->shader_program));
269 GL_CHECK(ctx.glGetProgramiv(data->shader_program, GL_LINK_STATUS, &status));
270
271 if (status != GL_TRUE) {
272 ctx.glGetProgramInfoLog(data->shader_program, sizeof(buffer), &length, &buffer[0]);
273 buffer[length] = '\0';
274 SDL_Log("Program linking failed: %s", buffer);
275 fflush(stderr);
276 quit(-1);
277 }
278 }
279
280 /* 3D data. Vertex range -0.5..0.5 in all axes.
281 * Z -0.5 is near, 0.5 is far. */
282 const float _vertices[] = {
283 /* Front face. */
284 /* Bottom left */
285 -0.5,
286 0.5,
287 -0.5,
288 0.5,
289 -0.5,
290 -0.5,
291 -0.5,
292 -0.5,
293 -0.5,
294 /* Top right */
295 -0.5,
296 0.5,
297 -0.5,
298 0.5,
299 0.5,
300 -0.5,
301 0.5,
302 -0.5,
303 -0.5,
304 /* Left face */
305 /* Bottom left */
306 -0.5,
307 0.5,
308 0.5,
309 -0.5,
310 -0.5,
311 -0.5,
312 -0.5,
313 -0.5,
314 0.5,
315 /* Top right */
316 -0.5,
317 0.5,
318 0.5,
319 -0.5,
320 0.5,
321 -0.5,
322 -0.5,
323 -0.5,
324 -0.5,
325 /* Top face */
326 /* Bottom left */
327 -0.5,
328 0.5,
329 0.5,
330 0.5,
331 0.5,
332 -0.5,
333 -0.5,
334 0.5,
335 -0.5,
336 /* Top right */
337 -0.5,
338 0.5,
339 0.5,
340 0.5,
341 0.5,
342 0.5,
343 0.5,
344 0.5,
345 -0.5,
346 /* Right face */
347 /* Bottom left */
348 0.5,
349 0.5,
350 -0.5,
351 0.5,
352 -0.5,
353 0.5,
354 0.5,
355 -0.5,
356 -0.5,
357 /* Top right */
358 0.5,
359 0.5,
360 -0.5,
361 0.5,
362 0.5,
363 0.5,
364 0.5,
365 -0.5,
366 0.5,
367 /* Back face */
368 /* Bottom left */
369 0.5,
370 0.5,
371 0.5,
372 -0.5,
373 -0.5,
374 0.5,
375 0.5,
376 -0.5,
377 0.5,
378 /* Top right */
379 0.5,
380 0.5,
381 0.5,
382 -0.5,
383 0.5,
384 0.5,
385 -0.5,
386 -0.5,
387 0.5,
388 /* Bottom face */
389 /* Bottom left */
390 -0.5,
391 -0.5,
392 -0.5,
393 0.5,
394 -0.5,
395 0.5,
396 -0.5,
397 -0.5,
398 0.5,
399 /* Top right */
400 -0.5,
401 -0.5,
402 -0.5,
403 0.5,
404 -0.5,
405 -0.5,
406 0.5,
407 -0.5,
408 0.5,
409 };
410
411 const float _colors[] = {
412 /* Front face */
413 /* Bottom left */
414 1.0, 0.0, 0.0, /* red */
415 0.0, 0.0, 1.0, /* blue */
416 0.0, 1.0, 0.0, /* green */
417 /* Top right */
418 1.0, 0.0, 0.0, /* red */
419 1.0, 1.0, 0.0, /* yellow */
420 0.0, 0.0, 1.0, /* blue */
421 /* Left face */
422 /* Bottom left */
423 1.0, 1.0, 1.0, /* white */
424 0.0, 1.0, 0.0, /* green */
425 0.0, 1.0, 1.0, /* cyan */
426 /* Top right */
427 1.0, 1.0, 1.0, /* white */
428 1.0, 0.0, 0.0, /* red */
429 0.0, 1.0, 0.0, /* green */
430 /* Top face */
431 /* Bottom left */
432 1.0, 1.0, 1.0, /* white */
433 1.0, 1.0, 0.0, /* yellow */
434 1.0, 0.0, 0.0, /* red */
435 /* Top right */
436 1.0, 1.0, 1.0, /* white */
437 0.0, 0.0, 0.0, /* black */
438 1.0, 1.0, 0.0, /* yellow */
439 /* Right face */
440 /* Bottom left */
441 1.0, 1.0, 0.0, /* yellow */
442 1.0, 0.0, 1.0, /* magenta */
443 0.0, 0.0, 1.0, /* blue */
444 /* Top right */
445 1.0, 1.0, 0.0, /* yellow */
446 0.0, 0.0, 0.0, /* black */
447 1.0, 0.0, 1.0, /* magenta */
448 /* Back face */
449 /* Bottom left */
450 0.0, 0.0, 0.0, /* black */
451 0.0, 1.0, 1.0, /* cyan */
452 1.0, 0.0, 1.0, /* magenta */
453 /* Top right */
454 0.0, 0.0, 0.0, /* black */
455 1.0, 1.0, 1.0, /* white */
456 0.0, 1.0, 1.0, /* cyan */
457 /* Bottom face */
458 /* Bottom left */
459 0.0, 1.0, 0.0, /* green */
460 1.0, 0.0, 1.0, /* magenta */
461 0.0, 1.0, 1.0, /* cyan */
462 /* Top right */
463 0.0, 1.0, 0.0, /* green */
464 0.0, 0.0, 1.0, /* blue */
465 1.0, 0.0, 1.0, /* magenta */
466 };
467
468 const char *_shader_vert_src =
469 " attribute vec4 av4position; "
470 " attribute vec3 av3color; "
471 " uniform mat4 mvp; "
472 " varying vec3 vv3color; "
473 " void main() { "
474 " vv3color = av3color; "
475 " gl_Position = mvp * av4position; "
476 " } ";
477
478 const char *_shader_frag_src =
479 " precision lowp float; "
480 " varying vec3 vv3color; "
481 " void main() { "
482 " gl_FragColor = vec4(vv3color, 1.0); "
483 " } ";
484
485 static void
486 Render(unsigned int width, unsigned int height, shader_data *data)
487 {
488 float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
489
490 /*
491 * Do some rotation with Euler angles. It is not a fixed axis as
492 * quaterions would be, but the effect is cool.
493 */
494 rotate_matrix((float)data->angle_x, 1.0f, 0.0f, 0.0f, matrix_modelview);
495 rotate_matrix((float)data->angle_y, 0.0f, 1.0f, 0.0f, matrix_rotate);
496
497 multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
498
499 rotate_matrix((float)data->angle_z, 0.0f, 1.0f, 0.0f, matrix_rotate);
500
501 multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
502
503 /* Pull the camera back from the cube */
504 matrix_modelview[14] -= 2.5;
505
506 perspective_matrix(45.0f, (float)width / height, 0.01f, 100.0f, matrix_perspective);
507 multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
508
509 GL_CHECK(ctx.glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
510
511 data->angle_x += 3;
512 data->angle_y += 2;
513 data->angle_z += 1;
514
515 if (data->angle_x >= 360) {
516 data->angle_x -= 360;
517 }
518 if (data->angle_x < 0) {
519 data->angle_x += 360;
520 }
521 if (data->angle_y >= 360) {
522 data->angle_y -= 360;
523 }
524 if (data->angle_y < 0) {
525 data->angle_y += 360;
526 }
527 if (data->angle_z >= 360) {
528 data->angle_z -= 360;
529 }
530 if (data->angle_z < 0) {
531 data->angle_z += 360;
532 }
533
534 GL_CHECK(ctx.glViewport(0, 0, width, height));
535 GL_CHECK(ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
536 GL_CHECK(ctx.glDrawArrays(GL_TRIANGLES, 0, 36));
537 }
538
539 int done;
540 Uint32 frames;
541 shader_data *datas;
542 thread_data *threads;
543
544 static void
545 render_window(int index)
546 {
547 int w, h, status;
548
549 if (!state->windows[index]) {
550 return;
551 }
552
553 status = SDL_GL_MakeCurrent(state->windows[index], context[index]);
554 if (status) {
555 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
556 return;
557 }
558
559 SDL_GL_GetDrawableSize(state->windows[index], &w, &h);
560 Render(w, h, &datas[index]);
561 SDL_GL_SwapWindow(state->windows[index]);
562 ++frames;
563 }
564
565 #ifndef __EMSCRIPTEN__
566 static int SDLCALL
567 render_thread_fn(void *render_ctx)
568 {
569 thread_data *thread = render_ctx;
570
571 while (!done && !thread->done && state->windows[thread->index]) {
572 render_window(thread->index);
573 }
574
575 SDL_GL_MakeCurrent(state->windows[thread->index], NULL);
576 return 0;
577 }
578
579 static void
580 loop_threaded(void)
581 {
582 SDL_Event event;
583 int i;
584
585 /* Wait for events */
586 while (SDL_WaitEvent(&event) && !done) {
587 if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE) {
588 SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
589 if (window) {
590 for (i = 0; i < state->num_windows; ++i) {
591 if (window == state->windows[i]) {
592 /* Stop the render thread when the window is closed */
593 threads[i].done = 1;
594 if (threads[i].thread) {
595 SDL_WaitThread(threads[i].thread, NULL);
596 threads[i].thread = NULL;
597 }
598 break;
599 }
600 }
601 }
602 }
603 SDLTest_CommonEvent(state, &event, &done);
604 }
605 }
606 #endif
607
608 static void
609 loop(void)
610 {
611 SDL_Event event;
612 int i;
613
614 /* Check for events */
615 while (SDL_PollEvent(&event) && !done) {
616 SDLTest_CommonEvent(state, &event, &done);
617 }
618 if (!done) {
619 for (i = 0; i < state->num_windows; ++i) {
620 render_window(i);
621 }
622 }
623 #ifdef __EMSCRIPTEN__
624 else {
625 emscripten_cancel_main_loop();
626 }
627 #endif
628 }
629
630 int main(int argc, char *argv[])
631 {
632 int fsaa, accel, threaded;
633 int value;
634 int i;
635 SDL_DisplayMode mode;
636 Uint32 then, now;
637 int status;
638 shader_data *data;
639
640 /* Initialize parameters */
641 fsaa = 0;
642 accel = 0;
643 threaded = 0;
644
645 /* Initialize test framework */
646 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
647 if (!state) {
648 return 1;
649 }
650 for (i = 1; i < argc;) {
651 int consumed;
652
653 consumed = SDLTest_CommonArg(state, i);
654 if (consumed == 0) {
655 if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
656 ++fsaa;
657 consumed = 1;
658 } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
659 ++accel;
660 consumed = 1;
661 } else if (SDL_strcasecmp(argv[i], "--threaded") == 0) {
662 ++threaded;
663 consumed = 1;
664 } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
665 i++;
666 if (!argv[i]) {
667 consumed = -1;
668 } else {
669 depth = SDL_atoi(argv[i]);
670 consumed = 1;
671 }
672 } else {
673 consumed = -1;
674 }
675 }
676 if (consumed < 0) {
677 static const char *options[] = { "[--fsaa]", "[--accel]", "[--zdepth %d]", "[--threaded]", NULL };
678 SDLTest_CommonLogUsage(state, argv[0], options);
679 quit(1);
680 }
681 i += consumed;
682 }
683
684 /* Set OpenGL parameters */
685 state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
686 state->gl_red_size = 5;
687 state->gl_green_size = 5;
688 state->gl_blue_size = 5;
689 state->gl_depth_size = depth;
690 state->gl_major_version = 2;
691 state->gl_minor_version = 0;
692 state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
693
694 if (fsaa) {
695 state->gl_multisamplebuffers = 1;
696 state->gl_multisamplesamples = fsaa;
697 }
698 if (accel) {
699 state->gl_accelerated = 1;
700 }
701 if (!SDLTest_CommonInit(state)) {
702 quit(2);
703 return 0;
704 }
705
706 context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(*context));
707 if (!context) {
708 SDL_Log("Out of memory!\n");
709 quit(2);
710 }
711
712 /* Create OpenGL ES contexts */
713 for (i = 0; i < state->num_windows; i++) {
714 context[i] = SDL_GL_CreateContext(state->windows[i]);
715 if (!context[i]) {
716 SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
717 quit(2);
718 }
719 }
720
721 /* Important: call this *after* creating the context */
722 if (LoadContext(&ctx) < 0) {
723 SDL_Log("Could not load GLES2 functions\n");
724 quit(2);
725 return 0;
726 }
727
728 if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
729 SDL_GL_SetSwapInterval(1);
730 } else {
731 SDL_GL_SetSwapInterval(0);
732 }
733
734 SDL_GetCurrentDisplayMode(0, &mode);
735 SDL_Log("Threaded : %s\n", threaded ? "yes" : "no");
736 SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
737 SDL_Log("\n");
738 SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
739 SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
740 SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
741 SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
742 SDL_Log("\n");
743
744 status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
745 if (!status) {
746 SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
747 } else {
748 SDL_Log("Failed to get SDL_GL_RED_SIZE: %s\n",
749 SDL_GetError());
750 }
751 status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
752 if (!status) {
753 SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
754 } else {
755 SDL_Log("Failed to get SDL_GL_GREEN_SIZE: %s\n",
756 SDL_GetError());
757 }
758 status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
759 if (!status) {
760 SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
761 } else {
762 SDL_Log("Failed to get SDL_GL_BLUE_SIZE: %s\n",
763 SDL_GetError());
764 }
765 status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
766 if (!status) {
767 SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
768 } else {
769 SDL_Log("Failed to get SDL_GL_DEPTH_SIZE: %s\n",
770 SDL_GetError());
771 }
772 if (fsaa) {
773 status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
774 if (!status) {
775 SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
776 } else {
777 SDL_Log("Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
778 SDL_GetError());
779 }
780 status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
781 if (!status) {
782 SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
783 value);
784 } else {
785 SDL_Log("Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
786 SDL_GetError());
787 }
788 }
789 if (accel) {
790 status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
791 if (!status) {
792 SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
793 } else {
794 SDL_Log("Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
795 SDL_GetError());
796 }
797 }
798
799 datas = (shader_data *)SDL_calloc(state->num_windows, sizeof(shader_data));
800
801 /* Set rendering settings for each context */
802 for (i = 0; i < state->num_windows; ++i) {
803
804 int w, h;
805 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
806 if (status) {
807 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
808
809 /* Continue for next window */
810 continue;
811 }
812 SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
813 ctx.glViewport(0, 0, w, h);
814
815 data = &datas[i];
816 data->angle_x = 0;
817 data->angle_y = 0;
818 data->angle_z = 0;
819
820 /* Shader Initialization */
821 process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER);
822 process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER);
823
824 /* Create shader_program (ready to attach shaders) */
825 data->shader_program = GL_CHECK(ctx.glCreateProgram());
826
827 /* Attach shaders and link shader_program */
828 link_program(data);
829
830 /* Get attribute locations of non-fixed attributes like color and texture coordinates. */
831 data->attr_position = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av4position"));
832 data->attr_color = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av3color"));
833
834 /* Get uniform locations */
835 data->attr_mvp = GL_CHECK(ctx.glGetUniformLocation(data->shader_program, "mvp"));
836
837 GL_CHECK(ctx.glUseProgram(data->shader_program));
838
839 /* Enable attributes for position, color and texture coordinates etc. */
840 GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_position));
841 GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_color));
842
843 /* Populate attributes for position, color and texture coordinates etc. */
844
845 GL_CHECK(ctx.glGenBuffers(1, &data->position_buffer));
846 GL_CHECK(ctx.glBindBuffer(GL_ARRAY_BUFFER, data->position_buffer));
847 GL_CHECK(ctx.glBufferData(GL_ARRAY_BUFFER, sizeof(_vertices), _vertices, GL_STATIC_DRAW));
848 GL_CHECK(ctx.glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, 0));
849 GL_CHECK(ctx.glBindBuffer(GL_ARRAY_BUFFER, 0));
850
851 GL_CHECK(ctx.glGenBuffers(1, &data->color_buffer));
852 GL_CHECK(ctx.glBindBuffer(GL_ARRAY_BUFFER, data->color_buffer));
853 GL_CHECK(ctx.glBufferData(GL_ARRAY_BUFFER, sizeof(_colors), _colors, GL_STATIC_DRAW));
854 GL_CHECK(ctx.glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, 0));
855 GL_CHECK(ctx.glBindBuffer(GL_ARRAY_BUFFER, 0));
856
857 GL_CHECK(ctx.glEnable(GL_CULL_FACE));
858 GL_CHECK(ctx.glEnable(GL_DEPTH_TEST));
859
860 SDL_GL_MakeCurrent(state->windows[i], NULL);
861 }
862
863 /* Main render loop */
864 frames = 0;
865 then = SDL_GetTicks();
866 done = 0;
867
868 #ifdef __EMSCRIPTEN__
869 emscripten_set_main_loop(loop, 0, 1);
870 #else
871 if (threaded) {
872 threads = (thread_data *)SDL_calloc(state->num_windows, sizeof(thread_data));
873
874 /* Start a render thread for each window */
875 for (i = 0; i < state->num_windows; ++i) {
876 threads[i].index = i;
877 threads[i].thread = SDL_CreateThread(render_thread_fn, "RenderThread", &threads[i]);
878 }
879
880 while (!done) {
881 loop_threaded();
882 }
883
884 /* Join the remaining render threads (if any) */
885 for (i = 0; i < state->num_windows; ++i) {
886 threads[i].done = 1;
887 if (threads[i].thread) {
888 SDL_WaitThread(threads[i].thread, NULL);
889 }
890 }
891 } else {
892 while (!done) {
893 loop();
894 }
895 }
896 #endif
897
898 /* Print out some timing information */
899 now = SDL_GetTicks();
900 if (now > then) {
901 SDL_Log("%2.2f frames per second\n",
902 ((double)frames * 1000) / (now - then));
903 }
904 #if !defined(__ANDROID__) && !defined(__NACL__)
905 quit(0);
906 #endif
907 return 0;
908 }
909
910 #else /* HAVE_OPENGLES2 */
911
912 int main(int argc, char *argv[])
913 {
914 SDL_Log("No OpenGL ES support on this system\n");
915 return 1;
916 }
917
918 #endif /* HAVE_OPENGLES2 */
919
920 /* vi: set ts=4 sw=4 expandtab: */