testcolorspace.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  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.h>
  11. #include <SDL3/SDL_test.h>
  12. #include <SDL3/SDL_main.h>
  13. #ifdef SDL_PLATFORM_EMSCRIPTEN
  14. #include <emscripten/emscripten.h>
  15. #endif
  16. #define WINDOW_WIDTH 640
  17. #define WINDOW_HEIGHT 480
  18. #define TEXT_START_X 6.0f
  19. #define TEXT_START_Y 6.0f
  20. #define TEXT_LINE_ADVANCE FONT_CHARACTER_SIZE * 2
  21. static SDL_Window *window;
  22. static SDL_Renderer *renderer;
  23. static const char *renderer_name;
  24. static SDL_Colorspace colorspace = SDL_COLORSPACE_SRGB;
  25. static const char *colorspace_name = "sRGB";
  26. static int renderer_count = 0;
  27. static int renderer_index = 0;
  28. static int stage_index = 0;
  29. static int done;
  30. static float HDR_headroom = 1.0f;
  31. enum
  32. {
  33. StageClearBackground,
  34. StageDrawBackground,
  35. StageTextureBackground,
  36. StageTargetBackground,
  37. StageBlendDrawing,
  38. StageBlendTexture,
  39. StageGradientDrawing,
  40. StageGradientTexture,
  41. StageCount
  42. };
  43. static void FreeRenderer(void)
  44. {
  45. SDLTest_CleanupTextDrawing();
  46. SDL_DestroyRenderer(renderer);
  47. renderer = NULL;
  48. }
  49. static void UpdateHDRState(void)
  50. {
  51. SDL_PropertiesID props;
  52. bool HDR_enabled;
  53. props = SDL_GetWindowProperties(window);
  54. HDR_enabled = SDL_GetBooleanProperty(props, SDL_PROP_WINDOW_HDR_ENABLED_BOOLEAN, false);
  55. SDL_Log("HDR %s", HDR_enabled ? "enabled" : "disabled");
  56. if (HDR_enabled) {
  57. props = SDL_GetRendererProperties(renderer);
  58. if (SDL_GetNumberProperty(props, SDL_PROP_RENDERER_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB) != SDL_COLORSPACE_SRGB_LINEAR) {
  59. SDL_Log("Run with --colorspace linear to display HDR colors");
  60. }
  61. HDR_headroom = SDL_GetFloatProperty(props, SDL_PROP_RENDERER_HDR_HEADROOM_FLOAT, 1.0f);
  62. }
  63. }
  64. static void CreateRenderer(void)
  65. {
  66. SDL_PropertiesID props;
  67. props = SDL_CreateProperties();
  68. SDL_SetPointerProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window);
  69. SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, SDL_GetRenderDriver(renderer_index));
  70. SDL_SetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, colorspace);
  71. renderer = SDL_CreateRendererWithProperties(props);
  72. SDL_DestroyProperties(props);
  73. if (!renderer) {
  74. SDL_Log("Couldn't create renderer: %s", SDL_GetError());
  75. return;
  76. }
  77. renderer_name = SDL_GetRendererName(renderer);
  78. SDL_Log("Created renderer %s", renderer_name);
  79. UpdateHDRState();
  80. }
  81. static void NextRenderer( void )
  82. {
  83. if (renderer_count <= 0) {
  84. return;
  85. }
  86. ++renderer_index;
  87. if (renderer_index == renderer_count) {
  88. renderer_index = 0;
  89. }
  90. FreeRenderer();
  91. CreateRenderer();
  92. }
  93. static void PrevRenderer(void)
  94. {
  95. if (renderer_count <= 0) {
  96. return;
  97. }
  98. --renderer_index;
  99. if (renderer_index == -1) {
  100. renderer_index += renderer_count;
  101. }
  102. FreeRenderer();
  103. CreateRenderer();
  104. }
  105. static void NextStage(void)
  106. {
  107. ++stage_index;
  108. if (stage_index == StageCount) {
  109. stage_index = 0;
  110. }
  111. }
  112. static void PrevStage(void)
  113. {
  114. --stage_index;
  115. if (stage_index == -1) {
  116. stage_index += StageCount;
  117. }
  118. }
  119. static bool ReadPixel(int x, int y, SDL_Color *c)
  120. {
  121. SDL_Surface *surface;
  122. SDL_Rect r;
  123. bool result = false;
  124. r.x = x;
  125. r.y = y;
  126. r.w = 1;
  127. r.h = 1;
  128. surface = SDL_RenderReadPixels(renderer, &r);
  129. if (surface) {
  130. /* Don't tonemap back to SDR, our source content was SDR */
  131. SDL_SetStringProperty(SDL_GetSurfaceProperties(surface), SDL_PROP_SURFACE_TONEMAP_OPERATOR_STRING, "*=1");
  132. if (SDL_ReadSurfacePixel(surface, 0, 0, &c->r, &c->g, &c->b, &c->a)) {
  133. result = true;
  134. } else {
  135. SDL_Log("Couldn't read pixel: %s", SDL_GetError());
  136. }
  137. SDL_DestroySurface(surface);
  138. } else {
  139. SDL_Log("Couldn't read back pixels: %s", SDL_GetError());
  140. }
  141. return result;
  142. }
  143. static void DrawText(float x, float y, const char *fmt, ...)
  144. {
  145. char *text;
  146. va_list ap;
  147. va_start(ap, fmt);
  148. SDL_vasprintf(&text, fmt, ap);
  149. va_end(ap);
  150. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  151. SDLTest_DrawString(renderer, x + 1.0f, y + 1.0f, text);
  152. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  153. SDLTest_DrawString(renderer, x, y, text);
  154. SDL_free(text);
  155. }
  156. static void RenderClearBackground(void)
  157. {
  158. /* Draw a 50% gray background.
  159. * This will be darker when using sRGB colors and lighter using linear colors
  160. */
  161. SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255);
  162. SDL_RenderClear(renderer);
  163. /* Check the renderered pixels */
  164. SDL_Color c;
  165. if (!ReadPixel(0, 0, &c)) {
  166. return;
  167. }
  168. float x = TEXT_START_X;
  169. float y = TEXT_START_Y;
  170. DrawText(x, y, "%s %s", renderer_name, colorspace_name);
  171. y += TEXT_LINE_ADVANCE;
  172. DrawText(x, y, "Test: Clear 50%% Gray Background");
  173. y += TEXT_LINE_ADVANCE;
  174. DrawText(x, y, "Background color written: 0x808080, read: 0x%.2x%.2x%.2x", c.r, c.g, c.b);
  175. y += TEXT_LINE_ADVANCE;
  176. if (c.r != 128) {
  177. DrawText(x, y, "Incorrect background color, unknown reason");
  178. y += TEXT_LINE_ADVANCE;
  179. }
  180. }
  181. static void RenderDrawBackground(void)
  182. {
  183. /* Draw a 50% gray background.
  184. * This will be darker when using sRGB colors and lighter using linear colors
  185. */
  186. SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255);
  187. SDL_RenderFillRect(renderer, NULL);
  188. /* Check the renderered pixels */
  189. SDL_Color c;
  190. if (!ReadPixel(0, 0, &c)) {
  191. return;
  192. }
  193. float x = TEXT_START_X;
  194. float y = TEXT_START_Y;
  195. DrawText(x, y, "%s %s", renderer_name, colorspace_name);
  196. y += TEXT_LINE_ADVANCE;
  197. DrawText(x, y, "Test: Draw 50%% Gray Background");
  198. y += TEXT_LINE_ADVANCE;
  199. DrawText(x, y, "Background color written: 0x808080, read: 0x%.2x%.2x%.2x", c.r, c.g, c.b);
  200. y += TEXT_LINE_ADVANCE;
  201. if (c.r != 128) {
  202. DrawText(x, y, "Incorrect background color, unknown reason");
  203. y += TEXT_LINE_ADVANCE;
  204. }
  205. }
  206. static SDL_Texture *CreateGrayTexture(void)
  207. {
  208. SDL_Texture *texture;
  209. Uint8 pixels[4];
  210. /* Floating point textures are in the linear colorspace by default */
  211. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1);
  212. if (!texture) {
  213. return NULL;
  214. }
  215. pixels[0] = 128;
  216. pixels[1] = 128;
  217. pixels[2] = 128;
  218. pixels[3] = 255;
  219. SDL_UpdateTexture(texture, NULL, pixels, sizeof(pixels));
  220. return texture;
  221. }
  222. static void RenderTextureBackground(void)
  223. {
  224. /* Fill the background with a 50% gray texture.
  225. * This will be darker when using sRGB colors and lighter using linear colors
  226. */
  227. SDL_Texture *texture = CreateGrayTexture();
  228. SDL_RenderTexture(renderer, texture, NULL, NULL);
  229. SDL_DestroyTexture(texture);
  230. /* Check the renderered pixels */
  231. SDL_Color c;
  232. if (!ReadPixel(0, 0, &c)) {
  233. return;
  234. }
  235. float x = TEXT_START_X;
  236. float y = TEXT_START_Y;
  237. DrawText(x, y, "%s %s", renderer_name, colorspace_name);
  238. y += TEXT_LINE_ADVANCE;
  239. DrawText(x, y, "Test: Fill 50%% Gray Texture");
  240. y += TEXT_LINE_ADVANCE;
  241. DrawText(x, y, "Background color written: 0x808080, read: 0x%.2x%.2x%.2x", c.r, c.g, c.b);
  242. y += TEXT_LINE_ADVANCE;
  243. if (c.r != 128) {
  244. DrawText(x, y, "Incorrect background color, unknown reason");
  245. y += TEXT_LINE_ADVANCE;
  246. }
  247. }
  248. static void RenderTargetBackground(void)
  249. {
  250. /* Fill the background with a 50% gray texture.
  251. * This will be darker when using sRGB colors and lighter using linear colors
  252. */
  253. SDL_Texture *target = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, 1, 1);
  254. SDL_Texture *texture = CreateGrayTexture();
  255. /* Fill the render target with the gray texture */
  256. SDL_SetRenderTarget(renderer, target);
  257. SDL_RenderTexture(renderer, texture, NULL, NULL);
  258. SDL_DestroyTexture(texture);
  259. /* Fill the output with the render target */
  260. SDL_SetRenderTarget(renderer, NULL);
  261. SDL_RenderTexture(renderer, target, NULL, NULL);
  262. SDL_DestroyTexture(target);
  263. /* Check the renderered pixels */
  264. SDL_Color c;
  265. if (!ReadPixel(0, 0, &c)) {
  266. return;
  267. }
  268. float x = TEXT_START_X;
  269. float y = TEXT_START_Y;
  270. DrawText(x, y, "%s %s", renderer_name, colorspace_name);
  271. y += TEXT_LINE_ADVANCE;
  272. DrawText(x, y, "Test: Fill 50%% Gray Render Target");
  273. y += TEXT_LINE_ADVANCE;
  274. DrawText(x, y, "Background color written: 0x808080, read: 0x%.2x%.2x%.2x", c.r, c.g, c.b);
  275. y += TEXT_LINE_ADVANCE;
  276. if (c.r != 128) {
  277. DrawText(x, y, "Incorrect background color, unknown reason");
  278. y += TEXT_LINE_ADVANCE;
  279. }
  280. }
  281. static void RenderBlendDrawing(void)
  282. {
  283. SDL_Color a = { 238, 70, 166, 255 }; /* red square */
  284. SDL_Color b = { 147, 255, 0, 255 }; /* green square */
  285. SDL_FRect rect;
  286. /* Draw a green square blended over a red square
  287. * This will have different effects based on whether sRGB colorspaces and sRGB vs linear blending is used.
  288. */
  289. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  290. SDL_RenderClear(renderer);
  291. rect.x = WINDOW_WIDTH / 3;
  292. rect.y = 0;
  293. rect.w = WINDOW_WIDTH / 3;
  294. rect.h = WINDOW_HEIGHT;
  295. SDL_SetRenderDrawColor(renderer, a.r, a.g, a.b, a.a);
  296. SDL_RenderFillRect(renderer, &rect);
  297. rect.x = 0;
  298. rect.y = WINDOW_HEIGHT / 3;
  299. rect.w = WINDOW_WIDTH;
  300. rect.h = WINDOW_HEIGHT / 6;
  301. SDL_SetRenderDrawColor(renderer, b.r, b.g, b.b, b.a);
  302. SDL_RenderFillRect(renderer, &rect);
  303. SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
  304. SDL_SetRenderDrawColor(renderer, b.r, b.g, b.b, 128);
  305. rect.y += WINDOW_HEIGHT / 6;
  306. SDL_RenderFillRect(renderer, &rect);
  307. SDL_Color ar, br, cr;
  308. if (!ReadPixel(WINDOW_WIDTH / 2, 0, &ar) ||
  309. !ReadPixel(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 3, &br) ||
  310. !ReadPixel(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, &cr)) {
  311. return;
  312. }
  313. float x = TEXT_START_X;
  314. float y = TEXT_START_Y;
  315. DrawText(x, y, "%s %s", renderer_name, colorspace_name);
  316. y += TEXT_LINE_ADVANCE;
  317. DrawText(x, y, "Test: Draw Blending");
  318. y += TEXT_LINE_ADVANCE;
  319. if (cr.r == 199 && cr.g == 193 && cr.b == 121) {
  320. DrawText(x, y, "Correct blend color, blending in linear space");
  321. } else if ((cr.r == 192 && cr.g == 163 && cr.b == 83) ||
  322. (cr.r == 191 && cr.g == 162 && cr.b == 82)) {
  323. DrawText(x, y, "Correct blend color, blending in sRGB space");
  324. } else if (cr.r == 214 && cr.g == 156 && cr.b == 113) {
  325. DrawText(x, y, "Incorrect blend color, blending in PQ space");
  326. } else {
  327. DrawText(x, y, "Incorrect blend color, unknown reason");
  328. }
  329. y += TEXT_LINE_ADVANCE;
  330. }
  331. static void RenderBlendTexture(void)
  332. {
  333. SDL_Color color_a = { 238, 70, 166, 255 }; /* red square */
  334. SDL_Color color_b = { 147, 255, 0, 255 }; /* green square */
  335. SDL_Texture *a;
  336. SDL_Texture *b;
  337. SDL_FRect rect;
  338. /* Draw a green square blended over a red square
  339. * This will have different effects based on whether sRGB colorspaces and sRGB vs linear blending is used.
  340. */
  341. a = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1);
  342. SDL_UpdateTexture(a, NULL, &color_a, sizeof(color_a));
  343. b = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_STATIC, 1, 1);
  344. SDL_UpdateTexture(b, NULL, &color_b, sizeof(color_b));
  345. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  346. SDL_RenderClear(renderer);
  347. rect.x = WINDOW_WIDTH / 3;
  348. rect.y = 0;
  349. rect.w = WINDOW_WIDTH / 3;
  350. rect.h = WINDOW_HEIGHT;
  351. SDL_RenderTexture(renderer, a, NULL, &rect);
  352. rect.x = 0;
  353. rect.y = WINDOW_HEIGHT / 3;
  354. rect.w = WINDOW_WIDTH;
  355. rect.h = WINDOW_HEIGHT / 6;
  356. SDL_RenderTexture(renderer, b, NULL, &rect);
  357. rect.y += WINDOW_HEIGHT / 6;
  358. SDL_SetTextureBlendMode(b, SDL_BLENDMODE_BLEND);
  359. SDL_SetTextureAlphaModFloat(b, 128 / 255.0f);
  360. SDL_RenderTexture(renderer, b, NULL, &rect);
  361. SDL_Color ar, br, cr;
  362. if (!ReadPixel(WINDOW_WIDTH / 2, 0, &ar) ||
  363. !ReadPixel(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 3, &br) ||
  364. !ReadPixel(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, &cr)) {
  365. return;
  366. }
  367. float x = TEXT_START_X;
  368. float y = TEXT_START_Y;
  369. DrawText(x, y, "%s %s", renderer_name, colorspace_name);
  370. y += TEXT_LINE_ADVANCE;
  371. DrawText(x, y, "Test: Texture Blending");
  372. y += TEXT_LINE_ADVANCE;
  373. if (cr.r == 199 && cr.g == 193 && cr.b == 121) {
  374. DrawText(x, y, "Correct blend color, blending in linear space");
  375. } else if ((cr.r == 192 && cr.g == 163 && cr.b == 83) ||
  376. (cr.r == 191 && cr.g == 162 && cr.b == 82)) {
  377. DrawText(x, y, "Correct blend color, blending in sRGB space");
  378. } else {
  379. DrawText(x, y, "Incorrect blend color, unknown reason");
  380. }
  381. y += TEXT_LINE_ADVANCE;
  382. SDL_DestroyTexture(a);
  383. SDL_DestroyTexture(b);
  384. }
  385. static void DrawGradient(float x, float y, float width, float height, float start, float end)
  386. {
  387. float xy[8];
  388. const int xy_stride = 2 * sizeof(float);
  389. SDL_FColor color[4];
  390. const int color_stride = sizeof(SDL_FColor);
  391. const int num_vertices = 4;
  392. const int indices[6] = { 0, 1, 2, 0, 2, 3 };
  393. const int num_indices = 6;
  394. const int size_indices = 4;
  395. float minx, miny, maxx, maxy;
  396. SDL_FColor min_color = { start, start, start, 1.0f };
  397. SDL_FColor max_color = { end, end, end, 1.0f };
  398. minx = x;
  399. miny = y;
  400. maxx = minx + width;
  401. maxy = miny + height;
  402. xy[0] = minx;
  403. xy[1] = miny;
  404. xy[2] = maxx;
  405. xy[3] = miny;
  406. xy[4] = maxx;
  407. xy[5] = maxy;
  408. xy[6] = minx;
  409. xy[7] = maxy;
  410. color[0] = min_color;
  411. color[1] = max_color;
  412. color[2] = max_color;
  413. color[3] = min_color;
  414. SDL_RenderGeometryRaw(renderer, NULL, xy, xy_stride, color, color_stride, NULL, 0, num_vertices, indices, num_indices, size_indices);
  415. }
  416. static void RenderGradientDrawing(void)
  417. {
  418. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  419. SDL_RenderClear(renderer);
  420. float x = TEXT_START_X;
  421. float y = TEXT_START_Y;
  422. DrawText(x, y, "%s %s", renderer_name, colorspace_name);
  423. y += TEXT_LINE_ADVANCE;
  424. DrawText(x, y, "Test: Draw SDR and HDR gradients");
  425. y += TEXT_LINE_ADVANCE;
  426. y += TEXT_LINE_ADVANCE;
  427. DrawText(x, y, "SDR gradient");
  428. y += TEXT_LINE_ADVANCE;
  429. DrawGradient(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, 1.0f);
  430. y += 64.0f;
  431. y += TEXT_LINE_ADVANCE;
  432. y += TEXT_LINE_ADVANCE;
  433. if (HDR_headroom > 1.0f) {
  434. DrawText(x, y, "HDR gradient");
  435. } else {
  436. DrawText(x, y, "No HDR headroom, HDR and SDR gradient are the same");
  437. }
  438. y += TEXT_LINE_ADVANCE;
  439. /* Drawing is in the sRGB colorspace, so we need to use the color scale, which is applied in linear space, to get into high dynamic range */
  440. SDL_SetRenderColorScale(renderer, HDR_headroom);
  441. DrawGradient(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, 1.0f);
  442. SDL_SetRenderColorScale(renderer, 1.0f);
  443. y += 64.0f;
  444. }
  445. static SDL_Texture *CreateGradientTexture(int width, float start, float end)
  446. {
  447. SDL_Texture *texture;
  448. float *pixels;
  449. /* Floating point textures are in the linear colorspace by default */
  450. texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA128_FLOAT, SDL_TEXTUREACCESS_STATIC, width, 1);
  451. if (!texture) {
  452. return NULL;
  453. }
  454. pixels = (float *)SDL_malloc(width * sizeof(float) * 4);
  455. if (pixels) {
  456. int i;
  457. float length = (end - start);
  458. for (i = 0; i < width; ++i) {
  459. float v = (start + (length * i) / width);
  460. pixels[i * 4 + 0] = v;
  461. pixels[i * 4 + 1] = v;
  462. pixels[i * 4 + 2] = v;
  463. pixels[i * 4 + 3] = 1.0f;
  464. }
  465. SDL_UpdateTexture(texture, NULL, pixels, width * sizeof(float) * 4);
  466. SDL_free(pixels);
  467. }
  468. return texture;
  469. }
  470. static void DrawGradientTexture(float x, float y, float width, float height, float start, float end)
  471. {
  472. SDL_FRect rect = { x, y, width, height };
  473. SDL_Texture *texture = CreateGradientTexture((int)width, start, end);
  474. SDL_RenderTexture(renderer, texture, NULL, &rect);
  475. SDL_DestroyTexture(texture);
  476. }
  477. static void RenderGradientTexture(void)
  478. {
  479. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  480. SDL_RenderClear(renderer);
  481. float x = TEXT_START_X;
  482. float y = TEXT_START_Y;
  483. DrawText(x, y, "%s %s", renderer_name, colorspace_name);
  484. y += TEXT_LINE_ADVANCE;
  485. DrawText(x, y, "Test: Texture SDR and HDR gradients");
  486. y += TEXT_LINE_ADVANCE;
  487. y += TEXT_LINE_ADVANCE;
  488. DrawText(x, y, "SDR gradient");
  489. y += TEXT_LINE_ADVANCE;
  490. DrawGradientTexture(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, 1.0f);
  491. y += 64.0f;
  492. y += TEXT_LINE_ADVANCE;
  493. y += TEXT_LINE_ADVANCE;
  494. if (HDR_headroom > 1.0f) {
  495. DrawText(x, y, "HDR gradient");
  496. } else {
  497. DrawText(x, y, "No HDR headroom, HDR and SDR gradient are the same");
  498. }
  499. y += TEXT_LINE_ADVANCE;
  500. /* The gradient texture is in the linear colorspace, so we can use the HDR_headroom value directly */
  501. DrawGradientTexture(x, y, WINDOW_WIDTH - 2 * x, 64.0f, 0.0f, HDR_headroom);
  502. y += 64.0f;
  503. }
  504. static void loop(void)
  505. {
  506. SDL_Event event;
  507. /* Check for events */
  508. while (SDL_PollEvent(&event)) {
  509. if (event.type == SDL_EVENT_KEY_DOWN) {
  510. switch (event.key.key) {
  511. case SDLK_ESCAPE:
  512. done = 1;
  513. break;
  514. case SDLK_SPACE:
  515. case SDLK_RIGHT:
  516. NextStage();
  517. break;
  518. case SDLK_LEFT:
  519. PrevStage();
  520. break;
  521. case SDLK_DOWN:
  522. NextRenderer();
  523. break;
  524. case SDLK_UP:
  525. PrevRenderer();
  526. break;
  527. default:
  528. break;
  529. }
  530. } else if (event.type == SDL_EVENT_WINDOW_HDR_STATE_CHANGED) {
  531. UpdateHDRState();
  532. } else if (event.type == SDL_EVENT_QUIT) {
  533. done = 1;
  534. }
  535. }
  536. if (renderer) {
  537. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  538. SDL_RenderClear(renderer);
  539. switch (stage_index) {
  540. case StageClearBackground:
  541. RenderClearBackground();
  542. break;
  543. case StageDrawBackground:
  544. RenderDrawBackground();
  545. break;
  546. case StageTextureBackground:
  547. RenderTextureBackground();
  548. break;
  549. case StageTargetBackground:
  550. RenderTargetBackground();
  551. break;
  552. case StageBlendDrawing:
  553. RenderBlendDrawing();
  554. break;
  555. case StageBlendTexture:
  556. RenderBlendTexture();
  557. break;
  558. case StageGradientDrawing:
  559. RenderGradientDrawing();
  560. break;
  561. case StageGradientTexture:
  562. RenderGradientTexture();
  563. break;
  564. }
  565. SDL_RenderPresent(renderer);
  566. }
  567. SDL_Delay(100);
  568. #ifdef SDL_PLATFORM_EMSCRIPTEN
  569. if (done) {
  570. emscripten_cancel_main_loop();
  571. }
  572. #endif
  573. }
  574. static void LogUsage(const char *argv0)
  575. {
  576. SDL_Log("Usage: %s [--renderer renderer] [--colorspace colorspace]", argv0);
  577. }
  578. int main(int argc, char *argv[])
  579. {
  580. int return_code = 1;
  581. int i;
  582. for (i = 1; i < argc; ++i) {
  583. if (SDL_strcmp(argv[i], "--renderer") == 0) {
  584. if (argv[i + 1]) {
  585. renderer_name = argv[i + 1];
  586. ++i;
  587. } else {
  588. LogUsage(argv[0]);
  589. goto quit;
  590. }
  591. } else if (SDL_strcmp(argv[i], "--colorspace") == 0) {
  592. if (argv[i + 1]) {
  593. colorspace_name = argv[i + 1];
  594. if (SDL_strcasecmp(colorspace_name, "sRGB") == 0) {
  595. colorspace = SDL_COLORSPACE_SRGB;
  596. } else if (SDL_strcasecmp(colorspace_name, "linear") == 0) {
  597. colorspace = SDL_COLORSPACE_SRGB_LINEAR;
  598. /* Not currently supported
  599. } else if (SDL_strcasecmp(colorspace_name, "HDR10") == 0) {
  600. colorspace = SDL_COLORSPACE_HDR10;
  601. */
  602. } else {
  603. SDL_Log("Unknown colorspace %s", argv[i + 1]);
  604. goto quit;
  605. }
  606. ++i;
  607. } else {
  608. LogUsage(argv[0]);
  609. goto quit;
  610. }
  611. } else {
  612. LogUsage(argv[0]);
  613. goto quit;
  614. }
  615. }
  616. window = SDL_CreateWindow("SDL colorspace test", WINDOW_WIDTH, WINDOW_HEIGHT, 0);
  617. if (!window) {
  618. SDL_Log("Couldn't create window: %s", SDL_GetError());
  619. return_code = 2;
  620. goto quit;
  621. }
  622. renderer_count = SDL_GetNumRenderDrivers();
  623. SDL_Log("There are %d render drivers:", renderer_count);
  624. for (i = 0; i < renderer_count; ++i) {
  625. const char *name = SDL_GetRenderDriver(i);
  626. if (renderer_name && SDL_strcasecmp(renderer_name, name) == 0) {
  627. renderer_index = i;
  628. }
  629. SDL_Log(" %s", name);
  630. }
  631. CreateRenderer();
  632. /* Main render loop */
  633. done = 0;
  634. #ifdef SDL_PLATFORM_EMSCRIPTEN
  635. emscripten_set_main_loop(loop, 0, 1);
  636. #else
  637. while (!done) {
  638. loop();
  639. }
  640. #endif
  641. return_code = 0;
  642. quit:
  643. SDL_DestroyRenderer(renderer);
  644. SDL_DestroyWindow(window);
  645. SDL_Quit();
  646. return return_code;
  647. }