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