🛠️🐜 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.

805 lines
23 KiB

  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(__NACL__) \
  19. || defined(__WINDOWS__) || defined(__LINUX__)
  20. #define HAVE_OPENGLES2
  21. #endif
  22. #ifdef HAVE_OPENGLES2
  23. #include "SDL_opengles2.h"
  24. typedef struct GLES2_Context
  25. {
  26. #define SDL_PROC(ret,func,params) ret (APIENTRY *func) params;
  27. #include "../src/render/opengles2/SDL_gles2funcs.h"
  28. #undef SDL_PROC
  29. } GLES2_Context;
  30. static SDL_Surface *g_surf_sdf = NULL;
  31. GLenum g_texture;
  32. GLenum g_texture_type = GL_TEXTURE_2D;
  33. GLfloat g_verts[24];
  34. typedef enum
  35. {
  36. GLES2_ATTRIBUTE_POSITION = 0,
  37. GLES2_ATTRIBUTE_TEXCOORD = 1,
  38. GLES2_ATTRIBUTE_ANGLE = 2,
  39. GLES2_ATTRIBUTE_CENTER = 3,
  40. } GLES2_Attribute;
  41. typedef enum
  42. {
  43. GLES2_UNIFORM_PROJECTION,
  44. GLES2_UNIFORM_TEXTURE,
  45. GLES2_UNIFORM_COLOR,
  46. } GLES2_Uniform;
  47. GLuint g_uniform_locations[16];
  48. static SDLTest_CommonState *state;
  49. static SDL_GLContext *context = NULL;
  50. static int depth = 16;
  51. static GLES2_Context ctx;
  52. static int LoadContext(GLES2_Context * data)
  53. {
  54. #if SDL_VIDEO_DRIVER_UIKIT
  55. #define __SDL_NOGETPROCADDR__
  56. #elif SDL_VIDEO_DRIVER_ANDROID
  57. #define __SDL_NOGETPROCADDR__
  58. #elif SDL_VIDEO_DRIVER_PANDORA
  59. #define __SDL_NOGETPROCADDR__
  60. #endif
  61. #if defined __SDL_NOGETPROCADDR__
  62. #define SDL_PROC(ret,func,params) data->func=func;
  63. #else
  64. #define SDL_PROC(ret,func,params) \
  65. do { \
  66. data->func = SDL_GL_GetProcAddress(#func); \
  67. if ( ! data->func ) { \
  68. return SDL_SetError("Couldn't load GLES2 function %s: %s", #func, SDL_GetError()); \
  69. } \
  70. } while ( 0 );
  71. #endif /* __SDL_NOGETPROCADDR__ */
  72. #include "../src/render/opengles2/SDL_gles2funcs.h"
  73. #undef SDL_PROC
  74. return 0;
  75. }
  76. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  77. static void
  78. quit(int rc)
  79. {
  80. int i;
  81. if (context != NULL) {
  82. for (i = 0; i < state->num_windows; i++) {
  83. if (context[i]) {
  84. SDL_GL_DeleteContext(context[i]);
  85. }
  86. }
  87. SDL_free(context);
  88. }
  89. SDLTest_CommonQuit(state);
  90. exit(rc);
  91. }
  92. #define GL_CHECK(x) \
  93. x; \
  94. { \
  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__); \
  98. quit(1); \
  99. } \
  100. }
  101. /*
  102. * Create shader, load in source, compile, dump debug as necessary.
  103. *
  104. * shader: Pointer to return created shader ID.
  105. * source: Passed-in shader source code.
  106. * shader_type: Passed to GL, e.g. GL_VERTEX_SHADER.
  107. */
  108. void
  109. process_shader(GLuint *shader, const char * source, GLint shader_type)
  110. {
  111. GLint status = GL_FALSE;
  112. const char *shaders[1] = { NULL };
  113. char buffer[1024];
  114. GLsizei length;
  115. /* Create shader and load into GL. */
  116. *shader = GL_CHECK(ctx.glCreateShader(shader_type));
  117. shaders[0] = source;
  118. GL_CHECK(ctx.glShaderSource(*shader, 1, shaders, NULL));
  119. /* Clean up shader source. */
  120. shaders[0] = NULL;
  121. /* Try compiling the shader. */
  122. GL_CHECK(ctx.glCompileShader(*shader));
  123. GL_CHECK(ctx.glGetShaderiv(*shader, GL_COMPILE_STATUS, &status));
  124. /* Dump debug info (source and log) if compilation failed. */
  125. if(status != GL_TRUE) {
  126. ctx.glGetProgramInfoLog(*shader, sizeof(buffer), &length, &buffer[0]);
  127. buffer[length] = '\0';
  128. SDL_Log("Shader compilation failed: %s", buffer);fflush(stderr);
  129. quit(-1);
  130. }
  131. }
  132. /* Notes on a_angle:
  133. * It is a vector containing sine and cosine for rotation matrix
  134. * To get correct rotation for most cases when a_angle is disabled cosine
  135. * value is decremented by 1.0 to get proper output with 0.0 which is default value
  136. */
  137. static const Uint8 GLES2_VertexSrc_Default_[] = " \
  138. uniform mat4 u_projection; \
  139. attribute vec2 a_position; \
  140. attribute vec2 a_texCoord; \
  141. attribute vec2 a_angle; \
  142. attribute vec2 a_center; \
  143. varying vec2 v_texCoord; \
  144. \
  145. void main() \
  146. { \
  147. float s = a_angle[0]; \
  148. float c = a_angle[1] + 1.0; \
  149. mat2 rotationMatrix = mat2(c, -s, s, c); \
  150. vec2 position = rotationMatrix * (a_position - a_center) + a_center; \
  151. v_texCoord = a_texCoord; \
  152. gl_Position = u_projection * vec4(position, 0.0, 1.0);\
  153. gl_PointSize = 1.0; \
  154. } \
  155. ";
  156. static const Uint8 GLES2_FragmentSrc_TextureABGRSrc_[] = " \
  157. precision mediump float; \
  158. uniform sampler2D u_texture; \
  159. uniform vec4 u_color; \
  160. varying vec2 v_texCoord; \
  161. \
  162. void main() \
  163. { \
  164. gl_FragColor = texture2D(u_texture, v_texCoord); \
  165. gl_FragColor *= u_color; \
  166. } \
  167. ";
  168. /* RGB to ABGR conversion */
  169. static const Uint8 GLES2_FragmentSrc_TextureABGRSrc_SDF[] = " \
  170. #extension GL_OES_standard_derivatives : enable\n\
  171. \
  172. precision mediump float; \
  173. uniform sampler2D u_texture; \
  174. uniform vec4 u_color; \
  175. varying vec2 v_texCoord; \
  176. \
  177. void main() \
  178. { \
  179. vec4 abgr = texture2D(u_texture, v_texCoord); \
  180. \
  181. float sigDist = abgr.a; \
  182. \
  183. float w = fwidth( sigDist );\
  184. float alpha = clamp(smoothstep(0.5 - w, 0.5 + w, sigDist), 0.0, 1.0); \
  185. \
  186. gl_FragColor = vec4(abgr.rgb, abgr.a * alpha); \
  187. gl_FragColor.rgb *= gl_FragColor.a; \
  188. gl_FragColor *= u_color; \
  189. } \
  190. ";
  191. /* RGB to ABGR conversion DEBUG */
  192. static const char *GLES2_FragmentSrc_TextureABGRSrc_SDF_dbg = " \
  193. #extension GL_OES_standard_derivatives : enable\n\
  194. \
  195. precision mediump float; \
  196. uniform sampler2D u_texture; \
  197. uniform vec4 u_color; \
  198. varying vec2 v_texCoord; \
  199. \
  200. void main() \
  201. { \
  202. vec4 abgr = texture2D(u_texture, v_texCoord); \
  203. \
  204. float a = abgr.a; \
  205. gl_FragColor = vec4(a, a, a, 1.0); \
  206. } \
  207. ";
  208. static float g_val = 1.0f;
  209. static int g_use_SDF = 1;
  210. static int g_use_SDF_debug = 0;
  211. static float g_angle = 0.0f;
  212. static float matrix_mvp[4][4];
  213. typedef struct shader_data
  214. {
  215. GLuint shader_program, shader_frag, shader_vert;
  216. GLint attr_position;
  217. GLint attr_color, attr_mvp;
  218. } shader_data;
  219. static void
  220. Render(unsigned int width, unsigned int height, shader_data* data)
  221. {
  222. float *verts = g_verts;
  223. ctx.glViewport(0, 0, 640, 480);
  224. GL_CHECK(ctx.glClear(GL_COLOR_BUFFER_BIT));
  225. GL_CHECK(ctx.glUniformMatrix4fv(g_uniform_locations[GLES2_UNIFORM_PROJECTION], 1, GL_FALSE, (const float *)matrix_mvp));
  226. GL_CHECK(ctx.glUniform4f(g_uniform_locations[GLES2_UNIFORM_COLOR], 1.0f, 1.0f, 1.0f, 1.0f));
  227. GL_CHECK(ctx.glVertexAttribPointer(GLES2_ATTRIBUTE_ANGLE, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (verts + 16)));
  228. GL_CHECK(ctx.glVertexAttribPointer(GLES2_ATTRIBUTE_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) (verts + 8)));
  229. GL_CHECK(ctx.glVertexAttribPointer(GLES2_ATTRIBUTE_POSITION, 2, GL_FLOAT, GL_FALSE, 0, (const GLvoid *) verts));
  230. GL_CHECK(ctx.glDrawArrays(GL_TRIANGLE_STRIP, 0, 4));
  231. }
  232. void renderCopy_angle(float degree_angle)
  233. {
  234. const float radian_angle = (float)(3.141592 * degree_angle) / 180.0;
  235. const GLfloat s = (GLfloat) SDL_sin(radian_angle);
  236. const GLfloat c = (GLfloat) SDL_cos(radian_angle) - 1.0f;
  237. GLfloat *verts = g_verts + 16;
  238. *(verts++) = s;
  239. *(verts++) = c;
  240. *(verts++) = s;
  241. *(verts++) = c;
  242. *(verts++) = s;
  243. *(verts++) = c;
  244. *(verts++) = s;
  245. *(verts++) = c;
  246. }
  247. void renderCopy_position(SDL_Rect *srcrect, SDL_Rect *dstrect)
  248. {
  249. GLfloat minx, miny, maxx, maxy;
  250. GLfloat minu, maxu, minv, maxv;
  251. GLfloat *verts = g_verts;
  252. minx = dstrect->x;
  253. miny = dstrect->y;
  254. maxx = dstrect->x + dstrect->w;
  255. maxy = dstrect->y + dstrect->h;
  256. minu = (GLfloat) srcrect->x / g_surf_sdf->w;
  257. maxu = (GLfloat) (srcrect->x + srcrect->w) / g_surf_sdf->w;
  258. minv = (GLfloat) srcrect->y / g_surf_sdf->h;
  259. maxv = (GLfloat) (srcrect->y + srcrect->h) / g_surf_sdf->h;
  260. *(verts++) = minx;
  261. *(verts++) = miny;
  262. *(verts++) = maxx;
  263. *(verts++) = miny;
  264. *(verts++) = minx;
  265. *(verts++) = maxy;
  266. *(verts++) = maxx;
  267. *(verts++) = maxy;
  268. *(verts++) = minu;
  269. *(verts++) = minv;
  270. *(verts++) = maxu;
  271. *(verts++) = minv;
  272. *(verts++) = minu;
  273. *(verts++) = maxv;
  274. *(verts++) = maxu;
  275. *(verts++) = maxv;
  276. }
  277. int done;
  278. Uint32 frames;
  279. shader_data *datas;
  280. void loop()
  281. {
  282. SDL_Event event;
  283. int i;
  284. int status;
  285. /* Check for events */
  286. ++frames;
  287. while (SDL_PollEvent(&event) && !done) {
  288. switch (event.type) {
  289. case SDL_KEYDOWN:
  290. {
  291. const int sym = event.key.keysym.sym;
  292. if (sym == SDLK_TAB) {
  293. SDL_Log("Tab");
  294. }
  295. if (sym == SDLK_LEFT) g_val -= 0.05;
  296. if (sym == SDLK_RIGHT) g_val += 0.05;
  297. if (sym == SDLK_UP) g_angle -= 1;
  298. if (sym == SDLK_DOWN) g_angle += 1;
  299. break;
  300. }
  301. case SDL_WINDOWEVENT:
  302. switch (event.window.event) {
  303. case SDL_WINDOWEVENT_RESIZED:
  304. for (i = 0; i < state->num_windows; ++i) {
  305. if (event.window.windowID == SDL_GetWindowID(state->windows[i])) {
  306. int w, h;
  307. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  308. if (status) {
  309. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  310. break;
  311. }
  312. /* Change view port to the new window dimensions */
  313. SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
  314. ctx.glViewport(0, 0, w, h);
  315. state->window_w = event.window.data1;
  316. state->window_h = event.window.data2;
  317. /* Update window content */
  318. Render(event.window.data1, event.window.data2, &datas[i]);
  319. SDL_GL_SwapWindow(state->windows[i]);
  320. break;
  321. }
  322. }
  323. break;
  324. }
  325. }
  326. SDLTest_CommonEvent(state, &event, &done);
  327. }
  328. matrix_mvp[3][0] = -1.0f;
  329. matrix_mvp[3][3] = 1.0f;
  330. matrix_mvp[0][0] = 2.0f / 640.0;
  331. matrix_mvp[1][1] = -2.0f / 480.0;
  332. matrix_mvp[3][1] = 1.0f;
  333. if (0)
  334. {
  335. float *f = (float *) matrix_mvp;
  336. SDL_Log("-----------------------------------");
  337. SDL_Log("[ %f, %f, %f, %f ]", *f++, *f++, *f++, *f++);
  338. SDL_Log("[ %f, %f, %f, %f ]", *f++, *f++, *f++, *f++);
  339. SDL_Log("[ %f, %f, %f, %f ]", *f++, *f++, *f++, *f++);
  340. SDL_Log("[ %f, %f, %f, %f ]", *f++, *f++, *f++, *f++);
  341. SDL_Log("-----------------------------------");
  342. }
  343. renderCopy_angle(g_angle);
  344. {
  345. int w, h;
  346. SDL_Rect rs, rd;
  347. SDL_GL_GetDrawableSize(state->windows[0], &w, &h);
  348. rs.x = 0; rs.y = 0; rs.w = g_surf_sdf->w; rs.h = g_surf_sdf->h;
  349. rd.w = g_surf_sdf->w * g_val; rd.h = g_surf_sdf->h * g_val;
  350. rd.x = (w - rd.w) / 2; rd.y = (h - rd.h) / 2;
  351. renderCopy_position(&rs, &rd);
  352. }
  353. if (!done) {
  354. for (i = 0; i < state->num_windows; ++i) {
  355. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  356. if (status) {
  357. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  358. /* Continue for next window */
  359. continue;
  360. }
  361. Render(state->window_w, state->window_h, &datas[i]);
  362. SDL_GL_SwapWindow(state->windows[i]);
  363. }
  364. }
  365. #ifdef __EMSCRIPTEN__
  366. else {
  367. emscripten_cancel_main_loop();
  368. }
  369. #endif
  370. }
  371. int
  372. main(int argc, char *argv[])
  373. {
  374. int fsaa, accel;
  375. int value;
  376. int i;
  377. SDL_DisplayMode mode;
  378. Uint32 then, now;
  379. int status;
  380. shader_data *data;
  381. /* Initialize parameters */
  382. fsaa = 0;
  383. accel = 0;
  384. /* Initialize test framework */
  385. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  386. if (!state) {
  387. return 1;
  388. }
  389. for (i = 1; i < argc;) {
  390. int consumed;
  391. consumed = SDLTest_CommonArg(state, i);
  392. if (consumed == 0) {
  393. if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
  394. ++fsaa;
  395. consumed = 1;
  396. } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
  397. ++accel;
  398. consumed = 1;
  399. } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
  400. i++;
  401. if (!argv[i]) {
  402. consumed = -1;
  403. } else {
  404. depth = SDL_atoi(argv[i]);
  405. consumed = 1;
  406. }
  407. } else {
  408. consumed = -1;
  409. }
  410. }
  411. if (consumed < 0) {
  412. static const char *options[] = { "[--fsaa]", "[--accel]", "[--zdepth %d]", NULL };
  413. SDLTest_CommonLogUsage(state, argv[0], options);
  414. quit(1);
  415. }
  416. i += consumed;
  417. }
  418. /* Set OpenGL parameters */
  419. state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
  420. state->gl_red_size = 5;
  421. state->gl_green_size = 5;
  422. state->gl_blue_size = 5;
  423. state->gl_depth_size = depth;
  424. state->gl_major_version = 2;
  425. state->gl_minor_version = 0;
  426. state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
  427. if (fsaa) {
  428. state->gl_multisamplebuffers=1;
  429. state->gl_multisamplesamples=fsaa;
  430. }
  431. if (accel) {
  432. state->gl_accelerated=1;
  433. }
  434. if (!SDLTest_CommonInit(state)) {
  435. quit(2);
  436. return 0;
  437. }
  438. context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(context));
  439. if (context == NULL) {
  440. SDL_Log("Out of memory!\n");
  441. quit(2);
  442. }
  443. /* Create OpenGL ES contexts */
  444. for (i = 0; i < state->num_windows; i++) {
  445. context[i] = SDL_GL_CreateContext(state->windows[i]);
  446. if (!context[i]) {
  447. SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
  448. quit(2);
  449. }
  450. }
  451. /* Important: call this *after* creating the context */
  452. if (LoadContext(&ctx) < 0) {
  453. SDL_Log("Could not load GLES2 functions\n");
  454. quit(2);
  455. return 0;
  456. }
  457. SDL_memset(matrix_mvp, 0, sizeof (matrix_mvp));
  458. {
  459. SDL_Surface *tmp;
  460. char *f;
  461. g_use_SDF = 1;
  462. g_use_SDF_debug = 0;
  463. if (g_use_SDF) {
  464. f = "testgles2_sdf_img_sdf.bmp";
  465. } else {
  466. f = "testgles2_sdf_img_normal.bmp";
  467. }
  468. SDL_Log("SDF is %s", g_use_SDF ? "enabled" : "disabled");
  469. /* Load SDF BMP image */
  470. #if 1
  471. tmp = SDL_LoadBMP(f);
  472. if (tmp == NULL) {
  473. SDL_Log("missing image file: %s", f);
  474. exit(-1);
  475. } else {
  476. SDL_Log("Load image file: %s", f);
  477. }
  478. #else
  479. /* Generate SDF image using SDL_ttf */
  480. #include "SDL_ttf.h"
  481. char *font_file = "./font/DroidSansFallback.ttf";
  482. char *str = "Abcde";
  483. SDL_Color color = { 0, 0,0, 255};
  484. TTF_Init();
  485. TTF_Font *font = TTF_OpenFont(font_file, 72);
  486. if (font == NULL) {
  487. SDL_Log("Cannot open font %s", font_file);
  488. }
  489. TTF_SetFontSDF(font, g_use_SDF);
  490. SDL_Surface *tmp = TTF_RenderUTF8_Blended(font, str, color);
  491. SDL_Log("err: %s", SDL_GetError());
  492. if (tmp == NULL) {
  493. SDL_Log("can't render text");
  494. return -1;
  495. }
  496. SDL_SaveBMP(tmp, f);
  497. TTF_CloseFont(font);
  498. TTF_Quit();
  499. #endif
  500. g_surf_sdf = SDL_ConvertSurfaceFormat(tmp, SDL_PIXELFORMAT_ABGR8888, 0);
  501. SDL_SetSurfaceBlendMode(g_surf_sdf, SDL_BLENDMODE_BLEND);
  502. }
  503. if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
  504. SDL_GL_SetSwapInterval(1);
  505. } else {
  506. SDL_GL_SetSwapInterval(0);
  507. }
  508. SDL_GetCurrentDisplayMode(0, &mode);
  509. SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
  510. SDL_Log("\n");
  511. SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
  512. SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
  513. SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
  514. SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
  515. SDL_Log("\n");
  516. status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
  517. if (!status) {
  518. SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
  519. } else {
  520. SDL_Log( "Failed to get SDL_GL_RED_SIZE: %s\n",
  521. SDL_GetError());
  522. }
  523. status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
  524. if (!status) {
  525. SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
  526. } else {
  527. SDL_Log( "Failed to get SDL_GL_GREEN_SIZE: %s\n",
  528. SDL_GetError());
  529. }
  530. status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
  531. if (!status) {
  532. SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
  533. } else {
  534. SDL_Log( "Failed to get SDL_GL_BLUE_SIZE: %s\n",
  535. SDL_GetError());
  536. }
  537. status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
  538. if (!status) {
  539. SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
  540. } else {
  541. SDL_Log( "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
  542. SDL_GetError());
  543. }
  544. if (fsaa) {
  545. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
  546. if (!status) {
  547. SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
  548. } else {
  549. SDL_Log( "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
  550. SDL_GetError());
  551. }
  552. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
  553. if (!status) {
  554. SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
  555. value);
  556. } else {
  557. SDL_Log( "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
  558. SDL_GetError());
  559. }
  560. }
  561. if (accel) {
  562. status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
  563. if (!status) {
  564. SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
  565. } else {
  566. SDL_Log( "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
  567. SDL_GetError());
  568. }
  569. }
  570. datas = (shader_data *)SDL_calloc(state->num_windows, sizeof(shader_data));
  571. /* Set rendering settings for each context */
  572. for (i = 0; i < state->num_windows; ++i) {
  573. int w, h;
  574. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  575. if (status) {
  576. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  577. /* Continue for next window */
  578. continue;
  579. }
  580. {
  581. int format = GL_RGBA;
  582. int type = GL_UNSIGNED_BYTE;
  583. GL_CHECK(ctx.glGenTextures(1, &g_texture));
  584. ctx.glActiveTexture(GL_TEXTURE0);
  585. ctx.glPixelStorei(GL_PACK_ALIGNMENT, 1);
  586. ctx.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  587. ctx.glBindTexture(g_texture_type, g_texture);
  588. ctx.glTexParameteri(g_texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  589. ctx.glTexParameteri(g_texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  590. ctx.glTexParameteri(g_texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  591. ctx.glTexParameteri(g_texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  592. GL_CHECK(ctx.glTexImage2D(g_texture_type, 0, format, g_surf_sdf->w, g_surf_sdf->h, 0, format, type, NULL));
  593. GL_CHECK(ctx.glTexSubImage2D(g_texture_type, 0, 0 /* xoffset */, 0 /* yoffset */, g_surf_sdf->w, g_surf_sdf->h, format, type, g_surf_sdf->pixels));
  594. }
  595. SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
  596. ctx.glViewport(0, 0, w, h);
  597. data = &datas[i];
  598. /* Shader Initialization */
  599. process_shader(&data->shader_vert, GLES2_VertexSrc_Default_, GL_VERTEX_SHADER);
  600. if (g_use_SDF) {
  601. if (g_use_SDF_debug == 0) {
  602. process_shader(&data->shader_frag, GLES2_FragmentSrc_TextureABGRSrc_SDF, GL_FRAGMENT_SHADER);
  603. } else {
  604. process_shader(&data->shader_frag, GLES2_FragmentSrc_TextureABGRSrc_SDF_dbg, GL_FRAGMENT_SHADER);
  605. }
  606. } else {
  607. process_shader(&data->shader_frag, GLES2_FragmentSrc_TextureABGRSrc_, GL_FRAGMENT_SHADER);
  608. }
  609. /* Create shader_program (ready to attach shaders) */
  610. data->shader_program = GL_CHECK(ctx.glCreateProgram());
  611. /* Attach shaders and link shader_program */
  612. GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
  613. GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
  614. GL_CHECK(ctx.glLinkProgram(data->shader_program));
  615. ctx.glBindAttribLocation(data->shader_program, GLES2_ATTRIBUTE_POSITION, "a_position");
  616. ctx.glBindAttribLocation(data->shader_program, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
  617. ctx.glBindAttribLocation(data->shader_program, GLES2_ATTRIBUTE_ANGLE, "a_angle");
  618. ctx.glBindAttribLocation(data->shader_program, GLES2_ATTRIBUTE_CENTER, "a_center");
  619. /* Predetermine locations of uniform variables */
  620. g_uniform_locations[GLES2_UNIFORM_PROJECTION] = ctx.glGetUniformLocation(data->shader_program, "u_projection");
  621. g_uniform_locations[GLES2_UNIFORM_TEXTURE] = ctx.glGetUniformLocation(data->shader_program, "u_texture");
  622. g_uniform_locations[GLES2_UNIFORM_COLOR] = ctx.glGetUniformLocation(data->shader_program, "u_color");
  623. GL_CHECK(ctx.glUseProgram(data->shader_program));
  624. ctx.glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_ANGLE);
  625. ctx.glDisableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_CENTER);
  626. ctx.glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
  627. ctx.glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
  628. ctx.glUniform1i(g_uniform_locations[GLES2_UNIFORM_TEXTURE], 0); /* always texture unit 0. */
  629. ctx.glActiveTexture(GL_TEXTURE0);
  630. ctx.glBindTexture(g_texture_type, g_texture);
  631. GL_CHECK(ctx.glClearColor(1, 1, 1, 1));
  632. // SDL_BLENDMODE_BLEND
  633. GL_CHECK(ctx.glEnable(GL_BLEND));
  634. ctx.glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  635. ctx.glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
  636. }
  637. /* Main render loop */
  638. frames = 0;
  639. then = SDL_GetTicks();
  640. done = 0;
  641. #ifdef __EMSCRIPTEN__
  642. emscripten_set_main_loop(loop, 0, 1);
  643. #else
  644. while (!done) {
  645. loop();
  646. }
  647. #endif
  648. /* Print out some timing information */
  649. now = SDL_GetTicks();
  650. if (now > then) {
  651. SDL_Log("%2.2f frames per second\n",
  652. ((double) frames * 1000) / (now - then));
  653. }
  654. #if !defined(__ANDROID__) && !defined(__NACL__)
  655. quit(0);
  656. #endif
  657. return 0;
  658. }
  659. #else /* HAVE_OPENGLES2 */
  660. int
  661. main(int argc, char *argv[])
  662. {
  663. SDL_Log("No OpenGL ES support on this system\n");
  664. return 1;
  665. }
  666. #endif /* HAVE_OPENGLES2 */
  667. /* vi: set ts=4 sw=4 expandtab: */