testgl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. #ifdef HAVE_OPENGL
  13. #include <stdlib.h>
  14. #include <SDL3/SDL_opengl.h>
  15. typedef struct GL_Context
  16. {
  17. #define SDL_PROC(ret, func, params) ret (APIENTRY *func) params;
  18. #include "../src/render/opengl/SDL_glfuncs.h"
  19. #undef SDL_PROC
  20. } GL_Context;
  21. /* Undefine this if you want a flat cube instead of a rainbow cube */
  22. #define SHADED_CUBE
  23. static SDLTest_CommonState *state;
  24. static SDL_GLContext context;
  25. static GL_Context ctx;
  26. static bool suspend_when_occluded;
  27. static bool LoadContext(GL_Context *data)
  28. {
  29. #ifdef SDL_VIDEO_DRIVER_UIKIT
  30. #define __SDL_NOGETPROCADDR__
  31. #elif defined(SDL_VIDEO_DRIVER_ANDROID)
  32. #define __SDL_NOGETPROCADDR__
  33. #endif
  34. #if defined __SDL_NOGETPROCADDR__
  35. #define SDL_PROC(ret, func, params) data->func = func;
  36. #else
  37. #define SDL_PROC(ret, func, params) \
  38. do { \
  39. data->func = (ret (APIENTRY *) params)SDL_GL_GetProcAddress(#func); \
  40. if (!data->func) { \
  41. return SDL_SetError("Couldn't load GL function %s: %s", #func, SDL_GetError()); \
  42. } \
  43. } while (0);
  44. #endif /* __SDL_NOGETPROCADDR__ */
  45. #include "../src/render/opengl/SDL_glfuncs.h"
  46. #undef SDL_PROC
  47. return true;
  48. }
  49. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  50. static void quit(int rc)
  51. {
  52. if (context) {
  53. /* SDL_GL_MakeCurrent(0, NULL); */ /* doesn't do anything */
  54. SDL_GL_DestroyContext(context);
  55. }
  56. SDLTest_CommonQuit(state);
  57. /* Let 'main()' return normally */
  58. if (rc != 0) {
  59. exit(rc);
  60. }
  61. }
  62. static void Render(void)
  63. {
  64. static float color[8][3] = {
  65. { 1.0, 1.0, 0.0 },
  66. { 1.0, 0.0, 0.0 },
  67. { 0.0, 0.0, 0.0 },
  68. { 0.0, 1.0, 0.0 },
  69. { 0.0, 1.0, 1.0 },
  70. { 1.0, 1.0, 1.0 },
  71. { 1.0, 0.0, 1.0 },
  72. { 0.0, 0.0, 1.0 }
  73. };
  74. static float cube[8][3] = {
  75. { 0.5, 0.5, -0.5 },
  76. { 0.5, -0.5, -0.5 },
  77. { -0.5, -0.5, -0.5 },
  78. { -0.5, 0.5, -0.5 },
  79. { -0.5, 0.5, 0.5 },
  80. { 0.5, 0.5, 0.5 },
  81. { 0.5, -0.5, 0.5 },
  82. { -0.5, -0.5, 0.5 }
  83. };
  84. /* Do our drawing, too. */
  85. ctx.glClearColor(0.0, 0.0, 0.0, 0.0 /* used with --transparent */);
  86. ctx.glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  87. ctx.glBegin(GL_QUADS);
  88. #ifdef SHADED_CUBE
  89. ctx.glColor3fv(color[0]);
  90. ctx.glVertex3fv(cube[0]);
  91. ctx.glColor3fv(color[1]);
  92. ctx.glVertex3fv(cube[1]);
  93. ctx.glColor3fv(color[2]);
  94. ctx.glVertex3fv(cube[2]);
  95. ctx.glColor3fv(color[3]);
  96. ctx.glVertex3fv(cube[3]);
  97. ctx.glColor3fv(color[3]);
  98. ctx.glVertex3fv(cube[3]);
  99. ctx.glColor3fv(color[4]);
  100. ctx.glVertex3fv(cube[4]);
  101. ctx.glColor3fv(color[7]);
  102. ctx.glVertex3fv(cube[7]);
  103. ctx.glColor3fv(color[2]);
  104. ctx.glVertex3fv(cube[2]);
  105. ctx.glColor3fv(color[0]);
  106. ctx.glVertex3fv(cube[0]);
  107. ctx.glColor3fv(color[5]);
  108. ctx.glVertex3fv(cube[5]);
  109. ctx.glColor3fv(color[6]);
  110. ctx.glVertex3fv(cube[6]);
  111. ctx.glColor3fv(color[1]);
  112. ctx.glVertex3fv(cube[1]);
  113. ctx.glColor3fv(color[5]);
  114. ctx.glVertex3fv(cube[5]);
  115. ctx.glColor3fv(color[4]);
  116. ctx.glVertex3fv(cube[4]);
  117. ctx.glColor3fv(color[7]);
  118. ctx.glVertex3fv(cube[7]);
  119. ctx.glColor3fv(color[6]);
  120. ctx.glVertex3fv(cube[6]);
  121. ctx.glColor3fv(color[5]);
  122. ctx.glVertex3fv(cube[5]);
  123. ctx.glColor3fv(color[0]);
  124. ctx.glVertex3fv(cube[0]);
  125. ctx.glColor3fv(color[3]);
  126. ctx.glVertex3fv(cube[3]);
  127. ctx.glColor3fv(color[4]);
  128. ctx.glVertex3fv(cube[4]);
  129. ctx.glColor3fv(color[6]);
  130. ctx.glVertex3fv(cube[6]);
  131. ctx.glColor3fv(color[1]);
  132. ctx.glVertex3fv(cube[1]);
  133. ctx.glColor3fv(color[2]);
  134. ctx.glVertex3fv(cube[2]);
  135. ctx.glColor3fv(color[7]);
  136. ctx.glVertex3fv(cube[7]);
  137. #else /* flat cube */
  138. ctx.glColor3f(1.0, 0.0, 0.0);
  139. ctx.glVertex3fv(cube[0]);
  140. ctx.glVertex3fv(cube[1]);
  141. ctx.glVertex3fv(cube[2]);
  142. ctx.glVertex3fv(cube[3]);
  143. ctx.glColor3f(0.0, 1.0, 0.0);
  144. ctx.glVertex3fv(cube[3]);
  145. ctx.glVertex3fv(cube[4]);
  146. ctx.glVertex3fv(cube[7]);
  147. ctx.glVertex3fv(cube[2]);
  148. ctx.glColor3f(0.0, 0.0, 1.0);
  149. ctx.glVertex3fv(cube[0]);
  150. ctx.glVertex3fv(cube[5]);
  151. ctx.glVertex3fv(cube[6]);
  152. ctx.glVertex3fv(cube[1]);
  153. ctx.glColor3f(0.0, 1.0, 1.0);
  154. ctx.glVertex3fv(cube[5]);
  155. ctx.glVertex3fv(cube[4]);
  156. ctx.glVertex3fv(cube[7]);
  157. ctx.glVertex3fv(cube[6]);
  158. ctx.glColor3f(1.0, 1.0, 0.0);
  159. ctx.glVertex3fv(cube[5]);
  160. ctx.glVertex3fv(cube[0]);
  161. ctx.glVertex3fv(cube[3]);
  162. ctx.glVertex3fv(cube[4]);
  163. ctx.glColor3f(1.0, 0.0, 1.0);
  164. ctx.glVertex3fv(cube[6]);
  165. ctx.glVertex3fv(cube[1]);
  166. ctx.glVertex3fv(cube[2]);
  167. ctx.glVertex3fv(cube[7]);
  168. #endif /* SHADED_CUBE */
  169. ctx.glEnd();
  170. ctx.glMatrixMode(GL_MODELVIEW);
  171. ctx.glRotatef(5.0, 1.0, 1.0, 1.0);
  172. }
  173. static void LogSwapInterval(void)
  174. {
  175. int interval = 0;
  176. if (SDL_GL_GetSwapInterval(&interval)) {
  177. SDL_Log("Swap Interval : %d", interval);
  178. } else {
  179. SDL_Log("Swap Interval : %d error: %s", interval, SDL_GetError());
  180. }
  181. }
  182. int main(int argc, char *argv[])
  183. {
  184. int fsaa, accel;
  185. int value;
  186. int i, done;
  187. const SDL_DisplayMode *mode;
  188. SDL_Event event;
  189. Uint64 then, now;
  190. Uint32 frames;
  191. int dw, dh;
  192. int swap_interval = 0;
  193. /* Initialize parameters */
  194. fsaa = 0;
  195. accel = -1;
  196. /* Initialize test framework */
  197. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  198. if (!state) {
  199. return 1;
  200. }
  201. for (i = 1; i < argc;) {
  202. int consumed;
  203. consumed = SDLTest_CommonArg(state, i);
  204. if (consumed == 0) {
  205. if (SDL_strcasecmp(argv[i], "--fsaa") == 0 && i + 1 < argc) {
  206. fsaa = SDL_atoi(argv[i + 1]);
  207. consumed = 2;
  208. } else if (SDL_strcasecmp(argv[i], "--accel") == 0 && i + 1 < argc) {
  209. accel = SDL_atoi(argv[i + 1]);
  210. consumed = 2;
  211. } else if(SDL_strcasecmp(argv[i], "--suspend-when-occluded") == 0) {
  212. suspend_when_occluded = true;
  213. consumed = 1;
  214. } else {
  215. consumed = -1;
  216. }
  217. }
  218. if (consumed < 0) {
  219. static const char *options[] = { "[--fsaa n]", "[--accel n]", "[--suspend-when-occluded]", NULL };
  220. SDLTest_CommonLogUsage(state, argv[0], options);
  221. quit(1);
  222. }
  223. i += consumed;
  224. }
  225. /* Set OpenGL parameters */
  226. state->window_flags |= SDL_WINDOW_OPENGL;
  227. state->gl_red_size = 5;
  228. state->gl_green_size = 5;
  229. state->gl_blue_size = 5;
  230. state->gl_depth_size = 16;
  231. /* For release_behavior to work, at least on Windows, you'll most likely need to set state->gl_major_version = 3 */
  232. /* state->gl_major_version = 3; */
  233. state->gl_release_behavior = 0;
  234. state->gl_double_buffer = 1;
  235. if (fsaa) {
  236. state->gl_multisamplebuffers = 1;
  237. state->gl_multisamplesamples = fsaa;
  238. }
  239. if (accel >= 0) {
  240. state->gl_accelerated = accel;
  241. }
  242. if (!SDLTest_CommonInit(state)) {
  243. quit(2);
  244. }
  245. /* Create OpenGL context */
  246. context = SDL_GL_CreateContext(state->windows[0]);
  247. if (!context) {
  248. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_GL_CreateContext(): %s", SDL_GetError());
  249. quit(2);
  250. }
  251. /* Important: call this *after* creating the context */
  252. if (!LoadContext(&ctx)) {
  253. SDL_Log("Could not load GL functions");
  254. quit(2);
  255. return 0;
  256. }
  257. SDL_GL_SetSwapInterval(state->render_vsync);
  258. swap_interval = state->render_vsync;
  259. mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay());
  260. if (mode) {
  261. SDL_Log("Screen BPP : %d", SDL_BITSPERPIXEL(mode->format));
  262. }
  263. LogSwapInterval();
  264. SDL_GetWindowSize(state->windows[0], &dw, &dh);
  265. SDL_Log("Window Size : %d,%d", dw, dh);
  266. SDL_GetWindowSizeInPixels(state->windows[0], &dw, &dh);
  267. SDL_Log("Draw Size : %d,%d", dw, dh);
  268. SDL_Log("%s", "");
  269. SDL_Log("Vendor : %s", ctx.glGetString(GL_VENDOR));
  270. SDL_Log("Renderer : %s", ctx.glGetString(GL_RENDERER));
  271. SDL_Log("Version : %s", ctx.glGetString(GL_VERSION));
  272. SDL_Log("Extensions : %s", ctx.glGetString(GL_EXTENSIONS));
  273. SDL_Log("%s", "");
  274. if (SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &value)) {
  275. SDL_Log("SDL_GL_RED_SIZE: requested %d, got %d", 5, value);
  276. } else {
  277. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_RED_SIZE: %s", SDL_GetError());
  278. }
  279. if (SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &value)) {
  280. SDL_Log("SDL_GL_GREEN_SIZE: requested %d, got %d", 5, value);
  281. } else {
  282. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_GREEN_SIZE: %s", SDL_GetError());
  283. }
  284. if (SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &value)) {
  285. SDL_Log("SDL_GL_BLUE_SIZE: requested %d, got %d", 5, value);
  286. } else {
  287. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_BLUE_SIZE: %s", SDL_GetError());
  288. }
  289. if (SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &value)) {
  290. SDL_Log("SDL_GL_DEPTH_SIZE: requested %d, got %d", 16, value);
  291. } else {
  292. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_DEPTH_SIZE: %s", SDL_GetError());
  293. }
  294. if (SDL_GL_GetAttribute(SDL_GL_CONTEXT_RELEASE_BEHAVIOR, &value)) {
  295. SDL_Log("SDL_GL_CONTEXT_RELEASE_BEHAVIOR: requested %d, got %d", 0, value);
  296. } else {
  297. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_CONTEXT_RELEASE_BEHAVIOR: %s", SDL_GetError());
  298. }
  299. if (fsaa) {
  300. if (SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &value)) {
  301. SDL_Log("SDL_GL_MULTISAMPLEBUFFERS: requested 1, got %d", value);
  302. } else {
  303. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLEBUFFERS: %s",
  304. SDL_GetError());
  305. }
  306. if (SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &value)) {
  307. SDL_Log("SDL_GL_MULTISAMPLESAMPLES: requested %d, got %d", fsaa,
  308. value);
  309. } else {
  310. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_MULTISAMPLESAMPLES: %s",
  311. SDL_GetError());
  312. }
  313. }
  314. if (accel >= 0) {
  315. if (SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &value)) {
  316. SDL_Log("SDL_GL_ACCELERATED_VISUAL: requested %d, got %d", accel,
  317. value);
  318. } else {
  319. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to get SDL_GL_ACCELERATED_VISUAL: %s",
  320. SDL_GetError());
  321. }
  322. }
  323. /* Set rendering settings */
  324. ctx.glMatrixMode(GL_PROJECTION);
  325. ctx.glLoadIdentity();
  326. ctx.glOrtho(-2.0, 2.0, -2.0, 2.0, -20.0, 20.0);
  327. ctx.glMatrixMode(GL_MODELVIEW);
  328. ctx.glLoadIdentity();
  329. ctx.glEnable(GL_DEPTH_TEST);
  330. ctx.glDepthFunc(GL_LESS);
  331. ctx.glShadeModel(GL_SMOOTH);
  332. /* Main render loop */
  333. frames = 0;
  334. then = SDL_GetTicks();
  335. done = 0;
  336. while (!done) {
  337. bool update_swap_interval = false;
  338. int active_windows = 0;
  339. /* Check for events */
  340. ++frames;
  341. while (SDL_PollEvent(&event)) {
  342. SDLTest_CommonEvent(state, &event, &done);
  343. if (event.type == SDL_EVENT_KEY_DOWN) {
  344. if (event.key.key == SDLK_O) {
  345. swap_interval--;
  346. update_swap_interval = true;
  347. } else if (event.key.key == SDLK_P) {
  348. swap_interval++;
  349. update_swap_interval = true;
  350. }
  351. }
  352. }
  353. if (update_swap_interval) {
  354. SDL_Log("Swap interval to be set to %d", swap_interval);
  355. }
  356. for (i = 0; i < state->num_windows; ++i) {
  357. int w, h;
  358. if (state->windows[i] == NULL ||
  359. (suspend_when_occluded && (SDL_GetWindowFlags(state->windows[i]) & SDL_WINDOW_OCCLUDED))) {
  360. continue;
  361. }
  362. ++active_windows;
  363. SDL_GL_MakeCurrent(state->windows[i], context);
  364. if (update_swap_interval) {
  365. SDL_GL_SetSwapInterval(swap_interval);
  366. LogSwapInterval();
  367. }
  368. SDL_GetWindowSizeInPixels(state->windows[i], &w, &h);
  369. ctx.glViewport(0, 0, w, h);
  370. Render();
  371. SDL_GL_SwapWindow(state->windows[i]);
  372. }
  373. /* If all windows are occluded, throttle event polling to 15hz. */
  374. if (!active_windows) {
  375. SDL_DelayNS(SDL_NS_PER_SECOND / 15);
  376. }
  377. }
  378. /* Print out some timing information */
  379. now = SDL_GetTicks();
  380. if (now > then) {
  381. SDL_Log("%2.2f frames per second",
  382. ((double)frames * 1000) / (now - then));
  383. }
  384. quit(0);
  385. return 0;
  386. }
  387. #else /* HAVE_OPENGL */
  388. int main(int argc, char *argv[])
  389. {
  390. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No OpenGL support on this system");
  391. return 1;
  392. }
  393. #endif /* HAVE_OPENGL */