🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

832 lines
22 KiB

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