testgles2.c 22 KB

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