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

817 lines
24 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. #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. char *path = NULL;
  382. /* Initialize parameters */
  383. fsaa = 0;
  384. accel = 0;
  385. /* Initialize test framework */
  386. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  387. if (!state) {
  388. return 1;
  389. }
  390. for (i = 1; i < argc;) {
  391. int consumed;
  392. consumed = SDLTest_CommonArg(state, i);
  393. if (consumed == 0) {
  394. if (SDL_strcasecmp(argv[i], "--fsaa") == 0) {
  395. ++fsaa;
  396. consumed = 1;
  397. } else if (SDL_strcasecmp(argv[i], "--accel") == 0) {
  398. ++accel;
  399. consumed = 1;
  400. } else if (SDL_strcasecmp(argv[i], "--zdepth") == 0) {
  401. i++;
  402. if (!argv[i]) {
  403. consumed = -1;
  404. } else {
  405. depth = SDL_atoi(argv[i]);
  406. consumed = 1;
  407. }
  408. } else {
  409. consumed = -1;
  410. }
  411. }
  412. if (consumed < 0) {
  413. static const char *options[] = { "[--fsaa]", "[--accel]", "[--zdepth %d]", NULL };
  414. SDLTest_CommonLogUsage(state, argv[0], options);
  415. quit(1);
  416. }
  417. i += consumed;
  418. }
  419. /* Set OpenGL parameters */
  420. state->window_flags |= SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
  421. state->gl_red_size = 5;
  422. state->gl_green_size = 5;
  423. state->gl_blue_size = 5;
  424. state->gl_depth_size = depth;
  425. state->gl_major_version = 2;
  426. state->gl_minor_version = 0;
  427. state->gl_profile_mask = SDL_GL_CONTEXT_PROFILE_ES;
  428. if (fsaa) {
  429. state->gl_multisamplebuffers=1;
  430. state->gl_multisamplesamples=fsaa;
  431. }
  432. if (accel) {
  433. state->gl_accelerated=1;
  434. }
  435. if (!SDLTest_CommonInit(state)) {
  436. quit(2);
  437. return 0;
  438. }
  439. context = (SDL_GLContext *)SDL_calloc(state->num_windows, sizeof(*context));
  440. if (context == NULL) {
  441. SDL_Log("Out of memory!\n");
  442. quit(2);
  443. }
  444. /* Create OpenGL ES contexts */
  445. for (i = 0; i < state->num_windows; i++) {
  446. context[i] = SDL_GL_CreateContext(state->windows[i]);
  447. if (!context[i]) {
  448. SDL_Log("SDL_GL_CreateContext(): %s\n", SDL_GetError());
  449. quit(2);
  450. }
  451. }
  452. /* Important: call this *after* creating the context */
  453. if (LoadContext(&ctx) < 0) {
  454. SDL_Log("Could not load GLES2 functions\n");
  455. quit(2);
  456. return 0;
  457. }
  458. SDL_memset(matrix_mvp, 0, sizeof (matrix_mvp));
  459. {
  460. SDL_Surface *tmp;
  461. char *f;
  462. g_use_SDF = 1;
  463. g_use_SDF_debug = 0;
  464. if (g_use_SDF) {
  465. f = "testgles2_sdf_img_sdf.bmp";
  466. } else {
  467. f = "testgles2_sdf_img_normal.bmp";
  468. }
  469. SDL_Log("SDF is %s", g_use_SDF ? "enabled" : "disabled");
  470. /* Load SDF BMP image */
  471. #if 1
  472. path = GetNearbyFilename(f);
  473. if (path == NULL)
  474. path = SDL_strdup(f);
  475. if (path == NULL) {
  476. SDL_Log("out of memory\n");
  477. exit(-1);
  478. }
  479. tmp = SDL_LoadBMP(path);
  480. if (tmp == NULL) {
  481. SDL_Log("missing image file: %s", path);
  482. exit(-1);
  483. } else {
  484. SDL_Log("Load image file: %s", path);
  485. }
  486. SDL_free(path);
  487. #else
  488. /* Generate SDF image using SDL_ttf */
  489. #include "SDL_ttf.h"
  490. char *font_file = "./font/DroidSansFallback.ttf";
  491. char *str = "Abcde";
  492. SDL_Color color = { 0, 0,0, 255};
  493. TTF_Init();
  494. TTF_Font *font = TTF_OpenFont(font_file, 72);
  495. if (font == NULL) {
  496. SDL_Log("Cannot open font %s", font_file);
  497. }
  498. TTF_SetFontSDF(font, g_use_SDF);
  499. SDL_Surface *tmp = TTF_RenderUTF8_Blended(font, str, color);
  500. SDL_Log("err: %s", SDL_GetError());
  501. if (tmp == NULL) {
  502. SDL_Log("can't render text");
  503. return -1;
  504. }
  505. SDL_SaveBMP(tmp, f);
  506. TTF_CloseFont(font);
  507. TTF_Quit();
  508. #endif
  509. g_surf_sdf = SDL_ConvertSurfaceFormat(tmp, SDL_PIXELFORMAT_ABGR8888, 0);
  510. SDL_SetSurfaceBlendMode(g_surf_sdf, SDL_BLENDMODE_BLEND);
  511. }
  512. if (state->render_flags & SDL_RENDERER_PRESENTVSYNC) {
  513. SDL_GL_SetSwapInterval(1);
  514. } else {
  515. SDL_GL_SetSwapInterval(0);
  516. }
  517. SDL_GetCurrentDisplayMode(0, &mode);
  518. SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode.format));
  519. SDL_Log("\n");
  520. SDL_Log("Vendor : %s\n", ctx.glGetString(GL_VENDOR));
  521. SDL_Log("Renderer : %s\n", ctx.glGetString(GL_RENDERER));
  522. SDL_Log("Version : %s\n", ctx.glGetString(GL_VERSION));
  523. SDL_Log("Extensions : %s\n", ctx.glGetString(GL_EXTENSIONS));
  524. SDL_Log("\n");
  525. status = SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value);
  526. if (!status) {
  527. SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d\n", 5, value);
  528. } else {
  529. SDL_Log( "Failed to get SDL_GL_RED_SIZE: %s\n",
  530. SDL_GetError());
  531. }
  532. status = SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value);
  533. if (!status) {
  534. SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d\n", 5, value);
  535. } else {
  536. SDL_Log( "Failed to get SDL_GL_GREEN_SIZE: %s\n",
  537. SDL_GetError());
  538. }
  539. status = SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value);
  540. if (!status) {
  541. SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d\n", 5, value);
  542. } else {
  543. SDL_Log( "Failed to get SDL_GL_BLUE_SIZE: %s\n",
  544. SDL_GetError());
  545. }
  546. status = SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value);
  547. if (!status) {
  548. SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d\n", depth, value);
  549. } else {
  550. SDL_Log( "Failed to get SDL_GL_DEPTH_SIZE: %s\n",
  551. SDL_GetError());
  552. }
  553. if (fsaa) {
  554. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value);
  555. if (!status) {
  556. SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d\n", value);
  557. } else {
  558. SDL_Log( "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s\n",
  559. SDL_GetError());
  560. }
  561. status = SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value);
  562. if (!status) {
  563. SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d\n", fsaa,
  564. value);
  565. } else {
  566. SDL_Log( "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s\n",
  567. SDL_GetError());
  568. }
  569. }
  570. if (accel) {
  571. status = SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value);
  572. if (!status) {
  573. SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested 1, got %d\n", value);
  574. } else {
  575. SDL_Log( "Failed to get SDL_GL_ACCELERATED_VISUAL: %s\n",
  576. SDL_GetError());
  577. }
  578. }
  579. datas = (shader_data *)SDL_calloc(state->num_windows, sizeof(shader_data));
  580. /* Set rendering settings for each context */
  581. for (i = 0; i < state->num_windows; ++i) {
  582. int w, h;
  583. status = SDL_GL_MakeCurrent(state->windows[i], context[i]);
  584. if (status) {
  585. SDL_Log("SDL_GL_MakeCurrent(): %s\n", SDL_GetError());
  586. /* Continue for next window */
  587. continue;
  588. }
  589. {
  590. int format = GL_RGBA;
  591. int type = GL_UNSIGNED_BYTE;
  592. GL_CHECK(ctx.glGenTextures(1, &g_texture));
  593. ctx.glActiveTexture(GL_TEXTURE0);
  594. ctx.glPixelStorei(GL_PACK_ALIGNMENT, 1);
  595. ctx.glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  596. ctx.glBindTexture(g_texture_type, g_texture);
  597. ctx.glTexParameteri(g_texture_type, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  598. ctx.glTexParameteri(g_texture_type, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  599. ctx.glTexParameteri(g_texture_type, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  600. ctx.glTexParameteri(g_texture_type, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  601. GL_CHECK(ctx.glTexImage2D(g_texture_type, 0, format, g_surf_sdf->w, g_surf_sdf->h, 0, format, type, NULL));
  602. 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));
  603. }
  604. SDL_GL_GetDrawableSize(state->windows[i], &w, &h);
  605. ctx.glViewport(0, 0, w, h);
  606. data = &datas[i];
  607. /* Shader Initialization */
  608. process_shader(&data->shader_vert, GLES2_VertexSrc_Default_, GL_VERTEX_SHADER);
  609. if (g_use_SDF) {
  610. if (g_use_SDF_debug == 0) {
  611. process_shader(&data->shader_frag, GLES2_FragmentSrc_TextureABGRSrc_SDF, GL_FRAGMENT_SHADER);
  612. } else {
  613. process_shader(&data->shader_frag, GLES2_FragmentSrc_TextureABGRSrc_SDF_dbg, GL_FRAGMENT_SHADER);
  614. }
  615. } else {
  616. process_shader(&data->shader_frag, GLES2_FragmentSrc_TextureABGRSrc_, GL_FRAGMENT_SHADER);
  617. }
  618. /* Create shader_program (ready to attach shaders) */
  619. data->shader_program = GL_CHECK(ctx.glCreateProgram());
  620. /* Attach shaders and link shader_program */
  621. GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_vert));
  622. GL_CHECK(ctx.glAttachShader(data->shader_program, data->shader_frag));
  623. GL_CHECK(ctx.glLinkProgram(data->shader_program));
  624. ctx.glBindAttribLocation(data->shader_program, GLES2_ATTRIBUTE_POSITION, "a_position");
  625. ctx.glBindAttribLocation(data->shader_program, GLES2_ATTRIBUTE_TEXCOORD, "a_texCoord");
  626. ctx.glBindAttribLocation(data->shader_program, GLES2_ATTRIBUTE_ANGLE, "a_angle");
  627. ctx.glBindAttribLocation(data->shader_program, GLES2_ATTRIBUTE_CENTER, "a_center");
  628. /* Predetermine locations of uniform variables */
  629. g_uniform_locations[GLES2_UNIFORM_PROJECTION] = ctx.glGetUniformLocation(data->shader_program, "u_projection");
  630. g_uniform_locations[GLES2_UNIFORM_TEXTURE] = ctx.glGetUniformLocation(data->shader_program, "u_texture");
  631. g_uniform_locations[GLES2_UNIFORM_COLOR] = ctx.glGetUniformLocation(data->shader_program, "u_color");
  632. GL_CHECK(ctx.glUseProgram(data->shader_program));
  633. ctx.glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_ANGLE);
  634. ctx.glDisableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_CENTER);
  635. ctx.glEnableVertexAttribArray(GLES2_ATTRIBUTE_POSITION);
  636. ctx.glEnableVertexAttribArray((GLenum) GLES2_ATTRIBUTE_TEXCOORD);
  637. ctx.glUniform1i(g_uniform_locations[GLES2_UNIFORM_TEXTURE], 0); /* always texture unit 0. */
  638. ctx.glActiveTexture(GL_TEXTURE0);
  639. ctx.glBindTexture(g_texture_type, g_texture);
  640. GL_CHECK(ctx.glClearColor(1, 1, 1, 1));
  641. // SDL_BLENDMODE_BLEND
  642. GL_CHECK(ctx.glEnable(GL_BLEND));
  643. ctx.glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
  644. ctx.glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
  645. }
  646. /* Main render loop */
  647. frames = 0;
  648. then = SDL_GetTicks();
  649. done = 0;
  650. #ifdef __EMSCRIPTEN__
  651. emscripten_set_main_loop(loop, 0, 1);
  652. #else
  653. while (!done) {
  654. loop();
  655. }
  656. #endif
  657. /* Print out some timing information */
  658. now = SDL_GetTicks();
  659. if (now > then) {
  660. SDL_Log("%2.2f frames per second\n",
  661. ((double) frames * 1000) / (now - then));
  662. }
  663. #if !defined(__ANDROID__) && !defined(__NACL__)
  664. quit(0);
  665. #endif
  666. return 0;
  667. }
  668. #else /* HAVE_OPENGLES2 */
  669. int
  670. main(int argc, char *argv[])
  671. {
  672. SDL_Log("No OpenGL ES support on this system\n");
  673. return 1;
  674. }
  675. #endif /* HAVE_OPENGLES2 */
  676. /* vi: set ts=4 sw=4 expandtab: */