testgles2_sdf.c 24 KB

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