testautomation_blit.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * SDL_BlitSurface bit-perfect rendering test suite written by Isaac Aronson
  3. */
  4. /* Suppress C4996 VS compiler warnings for unlink() */
  5. #if defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
  6. #define _CRT_SECURE_NO_DEPRECATE
  7. #endif
  8. #if defined(_MSC_VER) && !defined(_CRT_NONSTDC_NO_DEPRECATE)
  9. #define _CRT_NONSTDC_NO_DEPRECATE
  10. #endif
  11. #include <stdio.h>
  12. #ifndef _MSC_VER
  13. #include <unistd.h>
  14. #else
  15. /* Suppress uint64 to uint32 conversion warning within the PRNG engine */
  16. #pragma warning( disable : 4244 )
  17. #endif
  18. #include <sys/stat.h>
  19. #include <SDL3/SDL.h>
  20. #include <SDL3/SDL_test.h>
  21. #include "testautomation_images.h"
  22. /* ====== xoroshiro128+ PRNG engine for deterministic blit input ===== */
  23. Uint64 rotl(Uint64 x, int k) { return (x << k) | (x >> (-k & 63)); }
  24. Uint64 next(Uint64 state[2]) {
  25. Uint64 s0 = state[0], s1 = state[1];
  26. Uint64 result = rotl((s0 + s1) * 9, 29) + s0;
  27. state[0] = s0 ^ rotl(s1, 29);
  28. state[1] = s0 ^ s1 << 9;
  29. return result;
  30. }
  31. static Uint64 rngState[2] = {1, 2};
  32. Uint32 getRandomUint32() {
  33. return (Uint32)next(rngState);
  34. }
  35. /* ================= Test Case Helper Functions ================== */
  36. /*
  37. * Resets PRNG state to initialize tests using PRNG
  38. */
  39. void blitSetUp(void *arg) {
  40. rngState[0] = 1;
  41. rngState[1] = 2;
  42. }
  43. /*
  44. * Generates a stream of PRNG pixel data given length
  45. */
  46. Uint32 *getNextRandomBuffer(const int width, const int height) {
  47. Uint32* buf = SDL_malloc(sizeof(Uint32) * width * height);
  48. int i;
  49. for (i = 0; i < width * height; i++) {
  50. buf[i] = getRandomUint32();
  51. }
  52. return buf;
  53. }
  54. /*
  55. * Generates a small 15 x 15px surface of PRNG pixel data
  56. */
  57. SDL_Surface* getRandomBlitChunk(Uint32 *pixels, SDL_PixelFormatEnum format) {
  58. return SDL_CreateSurfaceFrom(pixels, 15, 15, 15 * 4, format);
  59. }
  60. /*
  61. * Generates a 800 x 600 surface of PRNG pixel data
  62. */
  63. SDL_Surface* getRandomSVGASurface(Uint32 *pixels, SDL_PixelFormatEnum format) {
  64. return SDL_CreateSurfaceFrom(pixels, 800, 600, 800 * 4, format);
  65. }
  66. /*
  67. * Calculates the FNV-1a hash of input pixel data
  68. */
  69. Uint32 FNVHash(Uint32* buf, int length) {
  70. const Uint32 fnv_prime = 0x811C9DC5;
  71. Uint32 hash = 0;
  72. int i;
  73. for (i = 0; i < length; buf++, i++)
  74. {
  75. hash *= fnv_prime;
  76. hash ^= (*buf);
  77. }
  78. return hash;
  79. }
  80. /*
  81. * Wraps the FNV-1a hash for an input surface's pixels
  82. */
  83. Uint32 hashSurfacePixels(SDL_Surface * surface) {
  84. Uint64 buffer_size = surface->w * surface->h;
  85. return FNVHash(surface->pixels, buffer_size);
  86. }
  87. /* ================= Test Case Implementation ================== */
  88. /**
  89. * Tests rendering a rainbow gradient background onto a blank surface, then rendering a sprite with complex geometry and
  90. * transparency on top of said surface, and comparing the result to known accurate renders with a hash.
  91. */
  92. int blit_testExampleApplicationRender(void *arg) {
  93. const int width = 32;
  94. const int height = 32;
  95. const Uint32 correct_hash = 0xe345d7a7;
  96. SDL_Surface* dest_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_ARGB8888);
  97. SDL_Surface* rainbow_background = SDLTest_ImageBlendingBackground();
  98. SDL_Surface* gearbrain_sprite = SDLTest_ImageBlendingSprite();
  99. // Blit background into "screen"
  100. SDL_BlitSurface(rainbow_background, NULL, dest_surface, NULL);
  101. // Blit example game sprite onto "screen"
  102. SDL_BlitSurface(gearbrain_sprite, NULL, dest_surface, NULL);
  103. // Check result
  104. const Uint32 hash = hashSurfacePixels(dest_surface);
  105. SDLTest_AssertCheck(hash == correct_hash,
  106. "Should render identically, expected hash 0x%" SDL_PRIx32 ", got 0x%" SDL_PRIx32,
  107. correct_hash, hash);
  108. // Clean up
  109. SDL_DestroySurface(rainbow_background);
  110. SDL_DestroySurface(gearbrain_sprite);
  111. SDL_DestroySurface(dest_surface);
  112. return TEST_COMPLETED;
  113. }
  114. /**
  115. * Tests rendering PRNG noise onto a surface of PRNG noise, while also testing color shift operations between the
  116. * different source and destination pixel formats, without an alpha shuffle, at SVGA resolution. Compares to known
  117. * accurate renders with a hash.
  118. */
  119. int blit_testRandomToRandomSVGA(void *arg) {
  120. const int width = 800;
  121. const int height = 600;
  122. const Uint32 correct_hash = 0x42140c5f;
  123. // Allocate random buffers
  124. Uint32 *dest_pixels = getNextRandomBuffer(width, height);
  125. Uint32 *src_pixels = getNextRandomBuffer(width, height);
  126. // Create surfaces of different pixel formats
  127. SDL_Surface* dest_surface = getRandomSVGASurface(dest_pixels, SDL_PIXELFORMAT_BGRA8888);
  128. SDL_Surface* src_surface = getRandomSVGASurface(src_pixels, SDL_PIXELFORMAT_RGBA8888);
  129. // Blit surfaces
  130. SDL_BlitSurface(src_surface, NULL, dest_surface, NULL);
  131. // Check result
  132. const Uint32 hash = hashSurfacePixels(dest_surface);
  133. SDLTest_AssertCheck(hash == correct_hash,
  134. "Should render identically, expected hash 0x%" SDL_PRIx32 ", got 0x%" SDL_PRIx32,
  135. correct_hash, hash);
  136. // Clean up
  137. SDL_DestroySurface(dest_surface);
  138. SDL_DestroySurface(src_surface);
  139. SDL_free(dest_pixels);
  140. SDL_free(src_pixels);
  141. return TEST_COMPLETED;
  142. }
  143. /**
  144. * Tests rendering small chunks of 15 by 15px PRNG noise onto an initially blank SVGA surface, while also testing color
  145. * shift operations between the different source and destination pixel formats, including an alpha shuffle. Compares to
  146. * known accurate renders with a hash.
  147. */
  148. int blit_testRandomToRandomSVGAMultipleIterations(void *arg) {
  149. const int width = 800;
  150. const int height = 600;
  151. int i;
  152. const Uint32 correct_hash = 0x5d26be78;
  153. // Create blank source surface
  154. SDL_Surface* dest_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_ABGR8888);
  155. // Perform 250k random blits into random areas of the blank surface
  156. for (i = 0; i < 250000; i++) {
  157. Uint32 *buf = getNextRandomBuffer(15, 15);
  158. SDL_Surface *sourceSurface = getRandomBlitChunk(buf, SDL_PIXELFORMAT_RGBA8888);
  159. SDL_Rect dest_rect;
  160. int location = (int)getRandomUint32();
  161. dest_rect.x = location % (width - 15 - 1);
  162. dest_rect.y = location % (height - 15 - 1);
  163. SDL_BlitSurface(sourceSurface, NULL, dest_surface, &dest_rect);
  164. SDL_DestroySurface(sourceSurface);
  165. SDL_free(buf);
  166. }
  167. // Check result
  168. const Uint32 hash = hashSurfacePixels(dest_surface);
  169. // Clean up
  170. SDL_DestroySurface(dest_surface);
  171. SDLTest_AssertCheck(hash == correct_hash,
  172. "Should render identically, expected hash 0x%" SDL_PRIx32 ", got 0x%" SDL_PRIx32,
  173. correct_hash, hash);
  174. return TEST_COMPLETED;
  175. }
  176. static const SDLTest_TestCaseReference blitTest1 = {
  177. (SDLTest_TestCaseFp)blit_testExampleApplicationRender, "blit_testExampleApplicationRender",
  178. "Test example application render.", TEST_ENABLED
  179. };
  180. static const SDLTest_TestCaseReference blitTest2 = {
  181. (SDLTest_TestCaseFp)blit_testRandomToRandomSVGA, "blit_testRandomToRandomSVGA",
  182. "Test SVGA noise render.", TEST_ENABLED
  183. };
  184. static const SDLTest_TestCaseReference blitTest3 = {
  185. (SDLTest_TestCaseFp)blit_testRandomToRandomSVGAMultipleIterations, "blit_testRandomToRandomSVGAMultipleIterations",
  186. "Test SVGA noise render (250k iterations).", TEST_ENABLED
  187. };
  188. static const SDLTest_TestCaseReference *blitTests[] = {
  189. &blitTest1, &blitTest2, &blitTest3, NULL
  190. };
  191. SDLTest_TestSuiteReference blitTestSuite = {
  192. "Blending",
  193. blitSetUp,
  194. blitTests,
  195. NULL
  196. };