testgl.c 13 KB

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