testautomation_blit.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_t x, int k) { return (x << k) | (x >> (-k & 63)); }
  24. Uint64 next(uint64_t 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 unsigned long 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 unsigned long hash = hashSurfacePixels(dest_surface);
  105. SDLTest_AssertCheck(hash == correct_hash,
  106. "Should render identically, expected hash 0x%lx, got 0x%lx", correct_hash, hash);
  107. // Clean up
  108. SDL_DestroySurface(rainbow_background);
  109. SDL_DestroySurface(gearbrain_sprite);
  110. SDL_DestroySurface(dest_surface);
  111. return TEST_COMPLETED;
  112. }
  113. /**
  114. * Tests rendering PRNG noise onto a surface of PRNG noise, while also testing color shift operations between the
  115. * different source and destination pixel formats, without an alpha shuffle, at SVGA resolution. Compares to known
  116. * accurate renders with a hash.
  117. */
  118. int blit_testRandomToRandomSVGA(void *arg) {
  119. const int width = 800;
  120. const int height = 600;
  121. const unsigned long correct_hash = 0x42140c5f;
  122. // Allocate random buffers
  123. Uint32 *dest_pixels = getNextRandomBuffer(width, height);
  124. Uint32 *src_pixels = getNextRandomBuffer(width, height);
  125. // Create surfaces of different pixel formats
  126. SDL_Surface* dest_surface = getRandomSVGASurface(dest_pixels, SDL_PIXELFORMAT_BGRA8888);
  127. SDL_Surface* src_surface = getRandomSVGASurface(src_pixels, SDL_PIXELFORMAT_RGBA8888);
  128. // Blit surfaces
  129. SDL_BlitSurface(src_surface, NULL, dest_surface, NULL);
  130. // Check result
  131. const unsigned long hash = hashSurfacePixels(dest_surface);
  132. SDLTest_AssertCheck(hash == correct_hash,
  133. "Should render identically, expected hash 0x%lx, got 0x%lx", correct_hash, hash);
  134. // Clean up
  135. SDL_DestroySurface(dest_surface);
  136. SDL_DestroySurface(src_surface);
  137. SDL_free(dest_pixels);
  138. SDL_free(src_pixels);
  139. return TEST_COMPLETED;
  140. }
  141. /**
  142. * Tests rendering small chunks of 15 by 15px PRNG noise onto an initially blank SVGA surface, while also testing color
  143. * shift operations between the different source and destination pixel formats, including an alpha shuffle. Compares to
  144. * known accurate renders with a hash.
  145. */
  146. int blit_testRandomToRandomSVGAMultipleIterations(void *arg) {
  147. const int width = 800;
  148. const int height = 600;
  149. int i;
  150. const unsigned long correct_hash = 0x5d26be78;
  151. // Create blank source surface
  152. SDL_Surface* dest_surface = SDL_CreateSurface(width, height, SDL_PIXELFORMAT_ABGR8888);
  153. // Perform 250k random blits into random areas of the blank surface
  154. for (i = 0; i < 250000; i++) {
  155. Uint32 *buf = getNextRandomBuffer(15, 15);
  156. SDL_Surface *sourceSurface = getRandomBlitChunk(buf, SDL_PIXELFORMAT_RGBA8888);
  157. SDL_Rect dest_rect;
  158. int location = (int)getRandomUint32();
  159. dest_rect.x = location % (width - 15 - 1);
  160. dest_rect.y = location % (height - 15 - 1);
  161. SDL_BlitSurface(sourceSurface, NULL, dest_surface, &dest_rect);
  162. SDL_DestroySurface(sourceSurface);
  163. SDL_free(buf);
  164. }
  165. // Check result
  166. const unsigned long hash = hashSurfacePixels(dest_surface);
  167. // Clean up
  168. SDL_DestroySurface(dest_surface);
  169. SDLTest_AssertCheck(hash == correct_hash,
  170. "Should render identically, expected hash 0x%lx, got 0x%lx", correct_hash, hash);
  171. return TEST_COMPLETED;
  172. }
  173. static const SDLTest_TestCaseReference blitTest1 = {
  174. (SDLTest_TestCaseFp)blit_testExampleApplicationRender, "blit_testExampleApplicationRender",
  175. "Test example application render.", TEST_ENABLED
  176. };
  177. static const SDLTest_TestCaseReference blitTest2 = {
  178. (SDLTest_TestCaseFp)blit_testRandomToRandomSVGA, "blit_testRandomToRandomSVGA",
  179. "Test SVGA noise render.", TEST_ENABLED
  180. };
  181. static const SDLTest_TestCaseReference blitTest3 = {
  182. (SDLTest_TestCaseFp)blit_testRandomToRandomSVGAMultipleIterations, "blit_testRandomToRandomSVGAMultipleIterations",
  183. "Test SVGA noise render (250k iterations).", TEST_ENABLED
  184. };
  185. static const SDLTest_TestCaseReference *blitTests[] = {
  186. &blitTest1, &blitTest2, &blitTest3, NULL
  187. };
  188. SDLTest_TestSuiteReference blitTestSuite = {
  189. "Blending",
  190. blitSetUp,
  191. blitTests,
  192. NULL
  193. };