2 Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
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.
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
18 #include <emscripten/emscripten.h>
21 #include "SDL_test_common.h"
23 #if defined(__IPHONEOS__) || defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__NACL__) \
24 || defined(__WINDOWS__) || defined(__LINUX__)
25 #define HAVE_OPENGLES2
30 #include "SDL_opengles2.h"
32 typedef struct GLES2_Context
34 #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
35 #include "../src/render/opengles2/SDL_gles2funcs.h"
40 static SDLTest_CommonState *state;
41 static SDL_GLContext *context = NULL;
42 static int depth = 16;
43 static GLES2_Context ctx;
45 static int LoadContext(GLES2_Context * data)
47 #if SDL_VIDEO_DRIVER_UIKIT
48 #define __SDL_NOGETPROCADDR__
49 #elif SDL_VIDEO_DRIVER_ANDROID
50 #define __SDL_NOGETPROCADDR__
51 #elif SDL_VIDEO_DRIVER_PANDORA
52 #define __SDL_NOGETPROCADDR__
55 #if defined __SDL_NOGETPROCADDR__
56 #define SDL_PROC(ret,func,params) data->func=func;
58 #define SDL_PROC(ret,func,params) \
60 data->func = SDL_GL_GetProcAddress(#func); \
61 if ( ! data->func ) { \
62 return SDL_SetError("Couldn't load GLES2 function %s: %s", #func, SDL_GetError()); \
65 #endif /* __SDL_NOGETPROCADDR__ */
67 #include "../src/render/opengles2/SDL_gles2funcs.h"
72 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
78 if (context != NULL) {
79 for (i = 0; i < state->num_windows; i++) {
81 SDL_GL_DeleteContext(context[i]);
88 SDLTest_CommonQuit(state);
95 GLenum glError = ctx.glGetError(); \
96 if(glError != GL_NO_ERROR) { \
97 SDL_Log("glGetError() = %i (0x%.8x) at line %i\n", glError, glError, __LINE__); \
103 * Simulates desktop's glRotatef. The matrix is returned in column-major
107 rotate_matrix(float angle, float x, float y, float z, float *r)
109 float radians, c, s, c1, u[3], length;
112 radians = (float)(angle * M_PI) / 180.0f;
114 c = SDL_cosf(radians);
115 s = SDL_sinf(radians);
117 c1 = 1.0f - SDL_cosf(radians);
119 length = (float)SDL_sqrt(x * x + y * y + z * z);
125 for (i = 0; i < 16; i++) {
131 for (i = 0; i < 3; i++) {
132 r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
133 r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
136 for (i = 0; i < 3; i++) {
137 for (j = 0; j < 3; j++) {
138 r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0f);
144 * Simulates gluPerspectiveMatrix
147 perspective_matrix(float fovy, float aspect, float znear, float zfar, float *r)
152 f = 1.0f/SDL_tanf(fovy * 0.5f);
154 for (i = 0; i < 16; i++) {
160 r[10] = (znear + zfar) / (znear - zfar);
162 r[14] = (2.0f * znear * zfar) / (znear - zfar);
167 * Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
168 * major. In-place multiplication is supported.
171 multiply_matrix(float *lhs, float *rhs, float *r)
176 for (i = 0; i < 4; i++) {
177 for (j = 0; j < 4; j++) {
178 tmp[j * 4 + i] = 0.0;
180 for (k = 0; k < 4; k++) {
181 tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
186 for (i = 0; i < 16; i++) {
192 * Create shader, load in source, compile, dump debug as necessary.
194 * shader: Pointer to return created shader ID.
195 * source: Passed-in shader source code.
196 * shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
199 process_shader(GLuint *shader, const char * source, GLint shader_type)
201 GLint status = GL_FALSE;
202 const char *shaders[1] = { NULL };
206 /* Create shader and load into GL. */
207 *shader = GL_CHECK(ctx.glCreateShader(shader_type));
211 GL_CHECK(ctx.glShaderSource(*shader, 1, shaders, NULL));
213 /* Clean up shader source. */
216 /* Try compiling the shader. */
217 GL_CHECK(ctx.glCompileShader(*shader));
218 GL_CHECK(ctx.glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
220 /* Dump debug info (source and log) if compilation failed. */
221 if(status != GL_TRUE) {
222 ctx.glGetProgramInfoLog(*shader, sizeof(buffer), &length, &buffer[0]);
223 buffer[length] = '\0';
224 SDL_Log("Shader compilation failed: %s", buffer);fflush(stderr);
229 /* 3D data. Vertex range -0.5..0.5 in all axes.
230 * Z -0.5 is near, 0.5 is far. */
231 const float _vertices[] =
289 const float _colors[] =
293 1.0, 0.0, 0.0, /* red */
294 0.0, 0.0, 1.0, /* blue */
295 0.0, 1.0, 0.0, /* green */
297 1.0, 0.0, 0.0, /* red */
298 1.0, 1.0, 0.0, /* yellow */
299 0.0, 0.0, 1.0, /* blue */
302 1.0, 1.0, 1.0, /* white */
303 0.0, 1.0, 0.0, /* green */
304 0.0, 1.0, 1.0, /* cyan */
306 1.0, 1.0, 1.0, /* white */
307 1.0, 0.0, 0.0, /* red */
308 0.0, 1.0, 0.0, /* green */
311 1.0, 1.0, 1.0, /* white */
312 1.0, 1.0, 0.0, /* yellow */
313 1.0, 0.0, 0.0, /* red */
315 1.0, 1.0, 1.0, /* white */
316 0.0, 0.0, 0.0, /* black */
317 1.0, 1.0, 0.0, /* yellow */
320 1.0, 1.0, 0.0, /* yellow */
321 1.0, 0.0, 1.0, /* magenta */
322 0.0, 0.0, 1.0, /* blue */
324 1.0, 1.0, 0.0, /* yellow */
325 0.0, 0.0, 0.0, /* black */
326 1.0, 0.0, 1.0, /* magenta */
329 0.0, 0.0, 0.0, /* black */
330 0.0, 1.0, 1.0, /* cyan */
331 1.0, 0.0, 1.0, /* magenta */
333 0.0, 0.0, 0.0, /* black */
334 1.0, 1.0, 1.0, /* white */
335 0.0, 1.0, 1.0, /* cyan */
338 0.0, 1.0, 0.0, /* green */
339 1.0, 0.0, 1.0, /* magenta */
340 0.0, 1.0, 1.0, /* cyan */
342 0.0, 1.0, 0.0, /* green */
343 0.0, 0.0, 1.0, /* blue */
344 1.0, 0.0, 1.0, /* magenta */
347 const char* _shader_vert_src =
348 " attribute vec4 av4position; "
349 " attribute vec3 av3color; "
350 " uniform mat4 mvp; "
351 " varying vec3 vv3color; "
353 " vv3color = av3color; "
354 " gl_Position = mvp * av4position; "
357 const char* _shader_frag_src =
358 " precision lowp float; "
359 " varying vec3 vv3color; "
361 " gl_FragColor = vec4(vv3color, 1.0); "
364 typedef struct shader_data
366 GLuint shader_program, shader_frag, shader_vert;
369 GLint attr_color, attr_mvp;
371 int angle_x, angle_y, angle_z;
376 Render(unsigned int width, unsigned int height, shader_data* data)
378 float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_mvp[16];
381 * Do some rotation with Euler angles. It is not a fixed axis as
382 * quaterions would be, but the effect is cool.
384 rotate_matrix((float)data->angle_x, 1.0f, 0.0f, 0.0f, matrix_modelview);
385 rotate_matrix((float)data->angle_y, 0.0f, 1.0f, 0.0f, matrix_rotate);
387 multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
389 rotate_matrix((float)data->angle_z, 0.0f, 1.0f, 0.0f, matrix_rotate);
391 multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
393 /* Pull the camera back from the cube */
394 matrix_modelview[14] -= 2.5;
396 perspective_matrix(45.0f, (float)width/height, 0.01f, 100.0f, matrix_perspective);
397 multiply_matrix(matrix_perspective, matrix_modelview, matrix_mvp);
399 GL_CHECK(ctx.glUniformMatrix4fv(data->attr_mvp, 1, GL_FALSE, matrix_mvp));
405 if(data->angle_x >= 360) data->angle_x -= 360;
406 if(data->angle_x < 0) data->angle_x += 360;
407 if(data->angle_y >= 360) data->angle_y -= 360;
408 if(data->angle_y < 0) data->angle_y += 360;
409 if(data->angle_z >= 360) data->angle_z -= 360;
410 if(data->angle_z < 0) data->angle_z += 360;
412 GL_CHECK(ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT));
413 GL_CHECK(ctx.glDrawArrays(GL_TRIANGLES, 0, 36));
426 /* Check for events */
428 while (SDL_PollEvent(&event) && !done) {
429 switch (event.type) {
430 case SDL_WINDOWEVENT:
431 switch (event.window.event) {
432 case SDL_WINDOWEVENT_RESIZED:
433 for (i = 0; i < state->num_windows; ++i) {
434 if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
436 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
438 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
441 /* Change view port to the new window dimensions */
442 SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
443 ctx.glViewport(0, 0, w, h);
444 state->window_w = event.window.data1;
445 state->window_h = event.window.data2;
446 /* Update window content */
447 Render(event.window.data1, event.window.data2, &datas[i]);
448 SDL_GL_SwapWindow(state->windows[i]);
455 SDLTest_CommonEvent(state, &event, &done);
458 for (i = 0; i < state->num_windows; ++i) {
459 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
461 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
463 /* Continue for next window */
466 Render(state->window_w, state->window_h, &datas[i]);
467 SDL_GL_SwapWindow(state->windows[i]);
470 #ifdef __EMSCRIPTEN__
472 emscripten_cancel_main_loop();
478 main(int argc, char *argv[])
483 SDL_DisplayMode mode;
488 /* Initialize parameters */
492 /* Initialize test framework */
493 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
497 for (i = 1; i < argc;) {
500 consumed = SDLTest_CommonArg(state, i);
502 if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
505 } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
508 } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
513 depth = SDL_atoi(argv[i]);
521 SDL_Log ("Usage: %s %s [--fsaa] [--accel] [--zdepth %%d]\n", argv[0],
522 SDLTest_CommonUsage(state));
528 /* Set OpenGL parameters */
529 state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_BORDERLESS;
530 state->gl_red_size = 5;
531 state->gl_green_size = 5;
532 state->gl_blue_size = 5;
533 state->gl_depth_size = depth;
534 state->gl_major_version = 2;
535 state->gl_minor_version = 0;
536 state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
539 state->gl_multisamplebuffers=1;
540 state->gl_multisamplesamples=fsaa;
543 state->gl_accelerated=1;
545 if (!SDLTest_CommonInit(state)) {
550 context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(context));
551 if (context == NULL) {
552 SDL_Log("Out of memory!\n");
556 /* Create OpenGL ES contexts */
557 for (i = 0; i < state->num_windows; i++) {
558 context[i] = SDL_GL_CreateContext(state->windows[i]);
560 SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
565 /* Important: call this *after* creating the context */
566 if (LoadContext(&ctx) < 0) {
567 SDL_Log("Could not load GLES2 functions\n");
574 if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
575 SDL_GL_SetSwapInterval(1);
577 SDL_GL_SetSwapInterval(0);
580 SDL_GetCurrentDisplayMode(0, &mode);
581 SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
583 SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
584 SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
585 SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
586 SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
589 status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
591 SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
593 SDL_Log( "Failed to get SDL_GL_RED_SIZE: %s\n",
596 status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
598 SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
600 SDL_Log( "Failed to get SDL_GL_GREEN_SIZE: %s\n",
603 status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
605 SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
607 SDL_Log( "Failed to get SDL_GL_BLUE_SIZE: %s\n",
610 status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
612 SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
614 SDL_Log( "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
618 status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
620 SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
622 SDL_Log( "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
625 status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
627 SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
630 SDL_Log( "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
635 status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
637 SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
639 SDL_Log( "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
644 datas = (shader_data *)SDL_calloc(state->num_windows, sizeof(shader_data));
646 /* Set rendering settings for each context */
647 for (i = 0; i < state->num_windows; ++i) {
650 status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
652 SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
654 /* Continue for next window */
657 SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
658 ctx.glViewport(0, 0, w, h);
661 data->angle_x = 0; data->angle_y = 0; data->angle_z = 0;
663 /* Shader Initialization */
664 process_shader(&data->shader_vert, _shader_vert_src, GL_VERTEX_SHADER);
665 process_shader(&data->shader_frag, _shader_frag_src, GL_FRAGMENT_SHADER);
667 /* Create shader_program (ready to attach shaders) */
668 data->shader_program = GL_CHECK(ctx.glCreateProgram());
670 /* Attach shaders and link shader_program */
671 GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
672 GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
673 GL_CHECK(ctx.glLinkProgram(data->shader_program));
675 /* Get attribute locations of non-fixed attributes like color and texture coordinates. */
676 data->attr_position = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av4position"));
677 data->attr_color = GL_CHECK(ctx.glGetAttribLocation(data->shader_program, "av3color"));
679 /* Get uniform locations */
680 data->attr_mvp = GL_CHECK(ctx.glGetUniformLocation(data->shader_program, "mvp"));
682 GL_CHECK(ctx.glUseProgram(data->shader_program));
684 /* Enable attributes for position, color and texture coordinates etc. */
685 GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_position));
686 GL_CHECK(ctx.glEnableVertexAttribArray(data->attr_color));
688 /* Populate attributes for position, color and texture coordinates etc. */
689 GL_CHECK(ctx.glVertexAttribPointer(data->attr_position, 3, GL_FLOAT, GL_FALSE, 0, _vertices));
690 GL_CHECK(ctx.glVertexAttribPointer(data->attr_color, 3, GL_FLOAT, GL_FALSE, 0, _colors));
692 GL_CHECK(ctx.glEnable(GL_CULL_FACE));
693 GL_CHECK(ctx.glEnable(GL_DEPTH_TEST));
696 /* Main render loop */
698 then = SDL_GetTicks();
701 #ifdef __EMSCRIPTEN__
702 emscripten_set_main_loop(loop, 0, 1);
709 /* Print out some timing information */
710 now = SDL_GetTicks();
712 SDL_Log("%2.2f frames per second\n",
713 ((double) frames * 1000) / (now - then));
715 #if !defined(__ANDROID__) && !defined(__NACL__)
721 #else /* HAVE_OPENGLES2 */
724 main(int argc, char *argv[])
726 SDL_Log("No OpenGL ES support on this system\n");
730 #endif /* HAVE_OPENGLES2 */
732 /* vi: set ts=4 sw=4 expandtab: */