testgpu_spinning_cube.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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 <stdlib.h>
  11. #ifdef __EMSCRIPTEN__
  12. #include <emscripten/emscripten.h>
  13. #endif
  14. #include <SDL3/SDL_test_common.h>
  15. #include <SDL3/SDL_gpu.h>
  16. #include <SDL3/SDL_main.h>
  17. /* Regenerate the shaders with testgpu/build-shaders.sh */
  18. #include "testgpu/testgpu_spirv.h"
  19. #include "testgpu/testgpu_dxil.h"
  20. #include "testgpu/testgpu_metallib.h"
  21. #define TESTGPU_SUPPORTED_FORMATS (SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXBC | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB)
  22. #define CHECK_CREATE(var, thing) { if (!(var)) { SDL_Log("Failed to create %s: %s\n", thing, SDL_GetError()); quit(2); } }
  23. static Uint32 frames = 0;
  24. typedef struct RenderState
  25. {
  26. SDL_GPUBuffer *buf_vertex;
  27. SDL_GPUGraphicsPipeline *pipeline;
  28. SDL_GPUSampleCount sample_count;
  29. } RenderState;
  30. typedef struct WindowState
  31. {
  32. int angle_x, angle_y, angle_z;
  33. SDL_GPUTexture *tex_depth, *tex_msaa, *tex_resolve;
  34. Uint32 prev_drawablew, prev_drawableh;
  35. } WindowState;
  36. static SDL_GPUDevice *gpu_device = NULL;
  37. static RenderState render_state;
  38. static SDLTest_CommonState *state = NULL;
  39. static WindowState *window_states = NULL;
  40. static void shutdownGPU(void)
  41. {
  42. if (window_states) {
  43. int i;
  44. for (i = 0; i < state->num_windows; i++) {
  45. WindowState *winstate = &window_states[i];
  46. SDL_ReleaseGPUTexture(gpu_device, winstate->tex_depth);
  47. SDL_ReleaseGPUTexture(gpu_device, winstate->tex_msaa);
  48. SDL_ReleaseGPUTexture(gpu_device, winstate->tex_resolve);
  49. SDL_ReleaseWindowFromGPUDevice(gpu_device, state->windows[i]);
  50. }
  51. SDL_free(window_states);
  52. window_states = NULL;
  53. }
  54. SDL_ReleaseGPUBuffer(gpu_device, render_state.buf_vertex);
  55. SDL_ReleaseGPUGraphicsPipeline(gpu_device, render_state.pipeline);
  56. SDL_DestroyGPUDevice(gpu_device);
  57. SDL_zero(render_state);
  58. gpu_device = NULL;
  59. }
  60. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  61. static void
  62. quit(int rc)
  63. {
  64. shutdownGPU();
  65. SDLTest_CommonQuit(state);
  66. exit(rc);
  67. }
  68. /*
  69. * Simulates desktop's glRotatef. The matrix is returned in column-major
  70. * order.
  71. */
  72. static void
  73. rotate_matrix(float angle, float x, float y, float z, float *r)
  74. {
  75. float radians, c, s, c1, u[3], length;
  76. int i, j;
  77. radians = angle * SDL_PI_F / 180.0f;
  78. c = SDL_cosf(radians);
  79. s = SDL_sinf(radians);
  80. c1 = 1.0f - SDL_cosf(radians);
  81. length = (float)SDL_sqrt(x * x + y * y + z * z);
  82. u[0] = x / length;
  83. u[1] = y / length;
  84. u[2] = z / length;
  85. for (i = 0; i < 16; i++) {
  86. r[i] = 0.0;
  87. }
  88. r[15] = 1.0;
  89. for (i = 0; i < 3; i++) {
  90. r[i * 4 + (i + 1) % 3] = u[(i + 2) % 3] * s;
  91. r[i * 4 + (i + 2) % 3] = -u[(i + 1) % 3] * s;
  92. }
  93. for (i = 0; i < 3; i++) {
  94. for (j = 0; j < 3; j++) {
  95. r[i * 4 + j] += c1 * u[i] * u[j] + (i == j ? c : 0.0f);
  96. }
  97. }
  98. }
  99. /*
  100. * Simulates gluPerspectiveMatrix
  101. */
  102. static void
  103. perspective_matrix(float fovy, float aspect, float znear, float zfar, float *r)
  104. {
  105. int i;
  106. float f;
  107. f = 1.0f/SDL_tanf(fovy * 0.5f);
  108. for (i = 0; i < 16; i++) {
  109. r[i] = 0.0;
  110. }
  111. r[0] = f / aspect;
  112. r[5] = f;
  113. r[10] = (znear + zfar) / (znear - zfar);
  114. r[11] = -1.0f;
  115. r[14] = (2.0f * znear * zfar) / (znear - zfar);
  116. r[15] = 0.0f;
  117. }
  118. /*
  119. * Multiplies lhs by rhs and writes out to r. All matrices are 4x4 and column
  120. * major. In-place multiplication is supported.
  121. */
  122. static void
  123. multiply_matrix(float *lhs, float *rhs, float *r)
  124. {
  125. int i, j, k;
  126. float tmp[16];
  127. for (i = 0; i < 4; i++) {
  128. for (j = 0; j < 4; j++) {
  129. tmp[j * 4 + i] = 0.0;
  130. for (k = 0; k < 4; k++) {
  131. tmp[j * 4 + i] += lhs[k * 4 + i] * rhs[j * 4 + k];
  132. }
  133. }
  134. }
  135. for (i = 0; i < 16; i++) {
  136. r[i] = tmp[i];
  137. }
  138. }
  139. typedef struct VertexData
  140. {
  141. float x, y, z; /* 3D data. Vertex range -0.5..0.5 in all axes. Z -0.5 is near, 0.5 is far. */
  142. float red, green, blue; /* intensity 0 to 1 (alpha is always 1). */
  143. } VertexData;
  144. static const VertexData vertex_data[] = {
  145. /* Front face. */
  146. /* Bottom left */
  147. { -0.5, 0.5, -0.5, 1.0, 0.0, 0.0 }, /* red */
  148. { 0.5, -0.5, -0.5, 0.0, 0.0, 1.0 }, /* blue */
  149. { -0.5, -0.5, -0.5, 0.0, 1.0, 0.0 }, /* green */
  150. /* Top right */
  151. { -0.5, 0.5, -0.5, 1.0, 0.0, 0.0 }, /* red */
  152. { 0.5, 0.5, -0.5, 1.0, 1.0, 0.0 }, /* yellow */
  153. { 0.5, -0.5, -0.5, 0.0, 0.0, 1.0 }, /* blue */
  154. /* Left face */
  155. /* Bottom left */
  156. { -0.5, 0.5, 0.5, 1.0, 1.0, 1.0 }, /* white */
  157. { -0.5, -0.5, -0.5, 0.0, 1.0, 0.0 }, /* green */
  158. { -0.5, -0.5, 0.5, 0.0, 1.0, 1.0 }, /* cyan */
  159. /* Top right */
  160. { -0.5, 0.5, 0.5, 1.0, 1.0, 1.0 }, /* white */
  161. { -0.5, 0.5, -0.5, 1.0, 0.0, 0.0 }, /* red */
  162. { -0.5, -0.5, -0.5, 0.0, 1.0, 0.0 }, /* green */
  163. /* Top face */
  164. /* Bottom left */
  165. { -0.5, 0.5, 0.5, 1.0, 1.0, 1.0 }, /* white */
  166. { 0.5, 0.5, -0.5, 1.0, 1.0, 0.0 }, /* yellow */
  167. { -0.5, 0.5, -0.5, 1.0, 0.0, 0.0 }, /* red */
  168. /* Top right */
  169. { -0.5, 0.5, 0.5, 1.0, 1.0, 1.0 }, /* white */
  170. { 0.5, 0.5, 0.5, 0.0, 0.0, 0.0 }, /* black */
  171. { 0.5, 0.5, -0.5, 1.0, 1.0, 0.0 }, /* yellow */
  172. /* Right face */
  173. /* Bottom left */
  174. { 0.5, 0.5, -0.5, 1.0, 1.0, 0.0 }, /* yellow */
  175. { 0.5, -0.5, 0.5, 1.0, 0.0, 1.0 }, /* magenta */
  176. { 0.5, -0.5, -0.5, 0.0, 0.0, 1.0 }, /* blue */
  177. /* Top right */
  178. { 0.5, 0.5, -0.5, 1.0, 1.0, 0.0 }, /* yellow */
  179. { 0.5, 0.5, 0.5, 0.0, 0.0, 0.0 }, /* black */
  180. { 0.5, -0.5, 0.5, 1.0, 0.0, 1.0 }, /* magenta */
  181. /* Back face */
  182. /* Bottom left */
  183. { 0.5, 0.5, 0.5, 0.0, 0.0, 0.0 }, /* black */
  184. { -0.5, -0.5, 0.5, 0.0, 1.0, 1.0 }, /* cyan */
  185. { 0.5, -0.5, 0.5, 1.0, 0.0, 1.0 }, /* magenta */
  186. /* Top right */
  187. { 0.5, 0.5, 0.5, 0.0, 0.0, 0.0 }, /* black */
  188. { -0.5, 0.5, 0.5, 1.0, 1.0, 1.0 }, /* white */
  189. { -0.5, -0.5, 0.5, 0.0, 1.0, 1.0 }, /* cyan */
  190. /* Bottom face */
  191. /* Bottom left */
  192. { -0.5, -0.5, -0.5, 0.0, 1.0, 0.0 }, /* green */
  193. { 0.5, -0.5, 0.5, 1.0, 0.0, 1.0 }, /* magenta */
  194. { -0.5, -0.5, 0.5, 0.0, 1.0, 1.0 }, /* cyan */
  195. /* Top right */
  196. { -0.5, -0.5, -0.5, 0.0, 1.0, 0.0 }, /* green */
  197. { 0.5, -0.5, -0.5, 0.0, 0.0, 1.0 }, /* blue */
  198. { 0.5, -0.5, 0.5, 1.0, 0.0, 1.0 } /* magenta */
  199. };
  200. static SDL_GPUTexture*
  201. CreateDepthTexture(Uint32 drawablew, Uint32 drawableh)
  202. {
  203. SDL_GPUTextureCreateInfo createinfo;
  204. SDL_GPUTexture *result;
  205. createinfo.type = SDL_GPU_TEXTURETYPE_2D;
  206. createinfo.format = SDL_GPU_TEXTUREFORMAT_D16_UNORM;
  207. createinfo.width = drawablew;
  208. createinfo.height = drawableh;
  209. createinfo.layer_count_or_depth = 1;
  210. createinfo.num_levels = 1;
  211. createinfo.sample_count = render_state.sample_count;
  212. createinfo.usage = SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET;
  213. createinfo.props = 0;
  214. result = SDL_CreateGPUTexture(gpu_device, &createinfo);
  215. CHECK_CREATE(result, "Depth Texture")
  216. return result;
  217. }
  218. static SDL_GPUTexture*
  219. CreateMSAATexture(Uint32 drawablew, Uint32 drawableh)
  220. {
  221. SDL_GPUTextureCreateInfo createinfo;
  222. SDL_GPUTexture *result;
  223. if (render_state.sample_count == SDL_GPU_SAMPLECOUNT_1) {
  224. return NULL;
  225. }
  226. createinfo.type = SDL_GPU_TEXTURETYPE_2D;
  227. createinfo.format = SDL_GetGPUSwapchainTextureFormat(gpu_device, state->windows[0]);
  228. createinfo.width = drawablew;
  229. createinfo.height = drawableh;
  230. createinfo.layer_count_or_depth = 1;
  231. createinfo.num_levels = 1;
  232. createinfo.sample_count = render_state.sample_count;
  233. createinfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET;
  234. createinfo.props = 0;
  235. result = SDL_CreateGPUTexture(gpu_device, &createinfo);
  236. CHECK_CREATE(result, "MSAA Texture")
  237. return result;
  238. }
  239. static SDL_GPUTexture *
  240. CreateResolveTexture(Uint32 drawablew, Uint32 drawableh)
  241. {
  242. SDL_GPUTextureCreateInfo createinfo;
  243. SDL_GPUTexture *result;
  244. if (render_state.sample_count == SDL_GPU_SAMPLECOUNT_1) {
  245. return NULL;
  246. }
  247. createinfo.type = SDL_GPU_TEXTURETYPE_2D;
  248. createinfo.format = SDL_GetGPUSwapchainTextureFormat(gpu_device, state->windows[0]);
  249. createinfo.width = drawablew;
  250. createinfo.height = drawableh;
  251. createinfo.layer_count_or_depth = 1;
  252. createinfo.num_levels = 1;
  253. createinfo.sample_count = SDL_GPU_SAMPLECOUNT_1;
  254. createinfo.usage = SDL_GPU_TEXTUREUSAGE_COLOR_TARGET | SDL_GPU_TEXTUREUSAGE_SAMPLER;
  255. createinfo.props = 0;
  256. result = SDL_CreateGPUTexture(gpu_device, &createinfo);
  257. CHECK_CREATE(result, "Resolve Texture")
  258. return result;
  259. }
  260. static void
  261. Render(SDL_Window *window, const int windownum)
  262. {
  263. WindowState *winstate = &window_states[windownum];
  264. SDL_GPUTexture *swapchainTexture;
  265. SDL_GPUColorTargetInfo color_target;
  266. SDL_GPUDepthStencilTargetInfo depth_target;
  267. float matrix_rotate[16], matrix_modelview[16], matrix_perspective[16], matrix_final[16];
  268. SDL_GPUCommandBuffer *cmd;
  269. SDL_GPURenderPass *pass;
  270. SDL_GPUBufferBinding vertex_binding;
  271. SDL_GPUBlitInfo blit_info;
  272. Uint32 drawablew, drawableh;
  273. /* Acquire the swapchain texture */
  274. cmd = SDL_AcquireGPUCommandBuffer(gpu_device);
  275. if (!cmd) {
  276. SDL_Log("Failed to acquire command buffer :%s", SDL_GetError());
  277. quit(2);
  278. }
  279. if (!SDL_WaitAndAcquireGPUSwapchainTexture(cmd, state->windows[windownum], &swapchainTexture, &drawablew, &drawableh)) {
  280. SDL_Log("Failed to acquire swapchain texture: %s", SDL_GetError());
  281. quit(2);
  282. }
  283. if (swapchainTexture == NULL) {
  284. /* Swapchain is unavailable, cancel work */
  285. SDL_CancelGPUCommandBuffer(cmd);
  286. return;
  287. }
  288. /*
  289. * Do some rotation with Euler angles. It is not a fixed axis as
  290. * quaternions would be, but the effect is cool.
  291. */
  292. rotate_matrix((float)winstate->angle_x, 1.0f, 0.0f, 0.0f, matrix_modelview);
  293. rotate_matrix((float)winstate->angle_y, 0.0f, 1.0f, 0.0f, matrix_rotate);
  294. multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
  295. rotate_matrix((float)winstate->angle_z, 0.0f, 1.0f, 0.0f, matrix_rotate);
  296. multiply_matrix(matrix_rotate, matrix_modelview, matrix_modelview);
  297. /* Pull the camera back from the cube */
  298. matrix_modelview[14] -= 2.5f;
  299. perspective_matrix(45.0f, (float)drawablew/drawableh, 0.01f, 100.0f, matrix_perspective);
  300. multiply_matrix(matrix_perspective, matrix_modelview, (float*) &matrix_final);
  301. winstate->angle_x += 3;
  302. winstate->angle_y += 2;
  303. winstate->angle_z += 1;
  304. if(winstate->angle_x >= 360) winstate->angle_x -= 360;
  305. if(winstate->angle_x < 0) winstate->angle_x += 360;
  306. if(winstate->angle_y >= 360) winstate->angle_y -= 360;
  307. if(winstate->angle_y < 0) winstate->angle_y += 360;
  308. if(winstate->angle_z >= 360) winstate->angle_z -= 360;
  309. if(winstate->angle_z < 0) winstate->angle_z += 360;
  310. /* Resize the depth buffer if the window size changed */
  311. if (winstate->prev_drawablew != drawablew || winstate->prev_drawableh != drawableh) {
  312. SDL_ReleaseGPUTexture(gpu_device, winstate->tex_depth);
  313. SDL_ReleaseGPUTexture(gpu_device, winstate->tex_msaa);
  314. SDL_ReleaseGPUTexture(gpu_device, winstate->tex_resolve);
  315. winstate->tex_depth = CreateDepthTexture(drawablew, drawableh);
  316. winstate->tex_msaa = CreateMSAATexture(drawablew, drawableh);
  317. winstate->tex_resolve = CreateResolveTexture(drawablew, drawableh);
  318. }
  319. winstate->prev_drawablew = drawablew;
  320. winstate->prev_drawableh = drawableh;
  321. /* Set up the pass */
  322. SDL_zero(color_target);
  323. color_target.clear_color.a = 1.0f;
  324. if (winstate->tex_msaa) {
  325. color_target.load_op = SDL_GPU_LOADOP_CLEAR;
  326. color_target.store_op = SDL_GPU_STOREOP_RESOLVE;
  327. color_target.texture = winstate->tex_msaa;
  328. color_target.resolve_texture = winstate->tex_resolve;
  329. color_target.cycle = true;
  330. color_target.cycle_resolve_texture = true;
  331. } else {
  332. color_target.load_op = SDL_GPU_LOADOP_CLEAR;
  333. color_target.store_op = SDL_GPU_STOREOP_STORE;
  334. color_target.texture = swapchainTexture;
  335. }
  336. SDL_zero(depth_target);
  337. depth_target.clear_depth = 1.0f;
  338. depth_target.load_op = SDL_GPU_LOADOP_CLEAR;
  339. depth_target.store_op = SDL_GPU_STOREOP_DONT_CARE;
  340. depth_target.stencil_load_op = SDL_GPU_LOADOP_DONT_CARE;
  341. depth_target.stencil_store_op = SDL_GPU_STOREOP_DONT_CARE;
  342. depth_target.texture = winstate->tex_depth;
  343. depth_target.cycle = true;
  344. /* Set up the bindings */
  345. vertex_binding.buffer = render_state.buf_vertex;
  346. vertex_binding.offset = 0;
  347. /* Draw the cube! */
  348. SDL_PushGPUVertexUniformData(cmd, 0, matrix_final, sizeof(matrix_final));
  349. pass = SDL_BeginGPURenderPass(cmd, &color_target, 1, &depth_target);
  350. SDL_BindGPUGraphicsPipeline(pass, render_state.pipeline);
  351. SDL_BindGPUVertexBuffers(pass, 0, &vertex_binding, 1);
  352. SDL_DrawGPUPrimitives(pass, 36, 1, 0, 0);
  353. SDL_EndGPURenderPass(pass);
  354. /* Blit MSAA resolve target to swapchain, if needed */
  355. if (render_state.sample_count > SDL_GPU_SAMPLECOUNT_1) {
  356. SDL_zero(blit_info);
  357. blit_info.source.texture = winstate->tex_resolve;
  358. blit_info.source.w = drawablew;
  359. blit_info.source.h = drawableh;
  360. blit_info.destination.texture = swapchainTexture;
  361. blit_info.destination.w = drawablew;
  362. blit_info.destination.h = drawableh;
  363. blit_info.load_op = SDL_GPU_LOADOP_DONT_CARE;
  364. blit_info.filter = SDL_GPU_FILTER_LINEAR;
  365. SDL_BlitGPUTexture(cmd, &blit_info);
  366. }
  367. /* Submit the command buffer! */
  368. SDL_SubmitGPUCommandBuffer(cmd);
  369. ++frames;
  370. }
  371. static SDL_GPUShader*
  372. load_shader(bool is_vertex)
  373. {
  374. SDL_GPUShaderCreateInfo createinfo;
  375. createinfo.num_samplers = 0;
  376. createinfo.num_storage_buffers = 0;
  377. createinfo.num_storage_textures = 0;
  378. createinfo.num_uniform_buffers = is_vertex ? 1 : 0;
  379. createinfo.props = 0;
  380. SDL_GPUShaderFormat format = SDL_GetGPUShaderFormats(gpu_device);
  381. if (format & SDL_GPU_SHADERFORMAT_DXIL) {
  382. createinfo.format = SDL_GPU_SHADERFORMAT_DXIL;
  383. createinfo.code = is_vertex ? D3D12_CubeVert : D3D12_CubeFrag;
  384. createinfo.code_size = is_vertex ? SDL_arraysize(D3D12_CubeVert) : SDL_arraysize(D3D12_CubeFrag);
  385. createinfo.entrypoint = is_vertex ? "VSMain" : "PSMain";
  386. } else if (format & SDL_GPU_SHADERFORMAT_METALLIB) {
  387. createinfo.format = SDL_GPU_SHADERFORMAT_METALLIB;
  388. createinfo.code = is_vertex ? cube_vert_metallib : cube_frag_metallib;
  389. createinfo.code_size = is_vertex ? cube_vert_metallib_len : cube_frag_metallib_len;
  390. createinfo.entrypoint = is_vertex ? "vs_main" : "fs_main";
  391. } else {
  392. createinfo.format = SDL_GPU_SHADERFORMAT_SPIRV;
  393. createinfo.code = is_vertex ? cube_vert_spv : cube_frag_spv;
  394. createinfo.code_size = is_vertex ? cube_vert_spv_len : cube_frag_spv_len;
  395. createinfo.entrypoint = "main";
  396. }
  397. createinfo.stage = is_vertex ? SDL_GPU_SHADERSTAGE_VERTEX : SDL_GPU_SHADERSTAGE_FRAGMENT;
  398. return SDL_CreateGPUShader(gpu_device, &createinfo);
  399. }
  400. static void
  401. init_render_state(int msaa)
  402. {
  403. SDL_GPUCommandBuffer *cmd;
  404. SDL_GPUTransferBuffer *buf_transfer;
  405. void *map;
  406. SDL_GPUTransferBufferLocation buf_location;
  407. SDL_GPUBufferRegion dst_region;
  408. SDL_GPUCopyPass *copy_pass;
  409. SDL_GPUBufferCreateInfo buffer_desc;
  410. SDL_GPUTransferBufferCreateInfo transfer_buffer_desc;
  411. SDL_GPUGraphicsPipelineCreateInfo pipelinedesc;
  412. SDL_GPUColorTargetDescription color_target_desc;
  413. Uint32 drawablew, drawableh;
  414. SDL_GPUVertexAttribute vertex_attributes[2];
  415. SDL_GPUVertexBufferDescription vertex_buffer_desc;
  416. SDL_GPUShader *vertex_shader;
  417. SDL_GPUShader *fragment_shader;
  418. int i;
  419. gpu_device = SDL_CreateGPUDevice(
  420. TESTGPU_SUPPORTED_FORMATS,
  421. true,
  422. state->gpudriver
  423. );
  424. CHECK_CREATE(gpu_device, "GPU device");
  425. /* Claim the windows */
  426. for (i = 0; i < state->num_windows; i++) {
  427. SDL_ClaimWindowForGPUDevice(
  428. gpu_device,
  429. state->windows[i]
  430. );
  431. }
  432. /* Create shaders */
  433. vertex_shader = load_shader(true);
  434. CHECK_CREATE(vertex_shader, "Vertex Shader")
  435. fragment_shader = load_shader(false);
  436. CHECK_CREATE(fragment_shader, "Fragment Shader")
  437. /* Create buffers */
  438. buffer_desc.usage = SDL_GPU_BUFFERUSAGE_VERTEX;
  439. buffer_desc.size = sizeof(vertex_data);
  440. buffer_desc.props = 0;
  441. render_state.buf_vertex = SDL_CreateGPUBuffer(
  442. gpu_device,
  443. &buffer_desc
  444. );
  445. CHECK_CREATE(render_state.buf_vertex, "Static vertex buffer")
  446. SDL_SetGPUBufferName(gpu_device, render_state.buf_vertex, "космонавт");
  447. transfer_buffer_desc.usage = SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD;
  448. transfer_buffer_desc.size = sizeof(vertex_data);
  449. transfer_buffer_desc.props = 0;
  450. buf_transfer = SDL_CreateGPUTransferBuffer(
  451. gpu_device,
  452. &transfer_buffer_desc
  453. );
  454. CHECK_CREATE(buf_transfer, "Vertex transfer buffer")
  455. /* We just need to upload the static data once. */
  456. map = SDL_MapGPUTransferBuffer(gpu_device, buf_transfer, false);
  457. SDL_memcpy(map, vertex_data, sizeof(vertex_data));
  458. SDL_UnmapGPUTransferBuffer(gpu_device, buf_transfer);
  459. cmd = SDL_AcquireGPUCommandBuffer(gpu_device);
  460. copy_pass = SDL_BeginGPUCopyPass(cmd);
  461. buf_location.transfer_buffer = buf_transfer;
  462. buf_location.offset = 0;
  463. dst_region.buffer = render_state.buf_vertex;
  464. dst_region.offset = 0;
  465. dst_region.size = sizeof(vertex_data);
  466. SDL_UploadToGPUBuffer(copy_pass, &buf_location, &dst_region, false);
  467. SDL_EndGPUCopyPass(copy_pass);
  468. SDL_SubmitGPUCommandBuffer(cmd);
  469. SDL_ReleaseGPUTransferBuffer(gpu_device, buf_transfer);
  470. /* Determine which sample count to use */
  471. render_state.sample_count = SDL_GPU_SAMPLECOUNT_1;
  472. if (msaa && SDL_GPUTextureSupportsSampleCount(
  473. gpu_device,
  474. SDL_GetGPUSwapchainTextureFormat(gpu_device, state->windows[0]),
  475. SDL_GPU_SAMPLECOUNT_4)) {
  476. render_state.sample_count = SDL_GPU_SAMPLECOUNT_4;
  477. }
  478. /* Set up the graphics pipeline */
  479. SDL_zero(pipelinedesc);
  480. SDL_zero(color_target_desc);
  481. color_target_desc.format = SDL_GetGPUSwapchainTextureFormat(gpu_device, state->windows[0]);
  482. pipelinedesc.target_info.num_color_targets = 1;
  483. pipelinedesc.target_info.color_target_descriptions = &color_target_desc;
  484. pipelinedesc.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM;
  485. pipelinedesc.target_info.has_depth_stencil_target = true;
  486. pipelinedesc.depth_stencil_state.enable_depth_test = true;
  487. pipelinedesc.depth_stencil_state.enable_depth_write = true;
  488. pipelinedesc.depth_stencil_state.compare_op = SDL_GPU_COMPAREOP_LESS_OR_EQUAL;
  489. pipelinedesc.multisample_state.sample_count = render_state.sample_count;
  490. pipelinedesc.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
  491. pipelinedesc.vertex_shader = vertex_shader;
  492. pipelinedesc.fragment_shader = fragment_shader;
  493. vertex_buffer_desc.slot = 0;
  494. vertex_buffer_desc.input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX;
  495. vertex_buffer_desc.instance_step_rate = 0;
  496. vertex_buffer_desc.pitch = sizeof(VertexData);
  497. vertex_attributes[0].buffer_slot = 0;
  498. vertex_attributes[0].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3;
  499. vertex_attributes[0].location = 0;
  500. vertex_attributes[0].offset = 0;
  501. vertex_attributes[1].buffer_slot = 0;
  502. vertex_attributes[1].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3;
  503. vertex_attributes[1].location = 1;
  504. vertex_attributes[1].offset = sizeof(float) * 3;
  505. pipelinedesc.vertex_input_state.num_vertex_buffers = 1;
  506. pipelinedesc.vertex_input_state.vertex_buffer_descriptions = &vertex_buffer_desc;
  507. pipelinedesc.vertex_input_state.num_vertex_attributes = 2;
  508. pipelinedesc.vertex_input_state.vertex_attributes = (SDL_GPUVertexAttribute*) &vertex_attributes;
  509. pipelinedesc.props = 0;
  510. render_state.pipeline = SDL_CreateGPUGraphicsPipeline(gpu_device, &pipelinedesc);
  511. CHECK_CREATE(render_state.pipeline, "Render Pipeline")
  512. /* These are reference-counted; once the pipeline is created, you don't need to keep these. */
  513. SDL_ReleaseGPUShader(gpu_device, vertex_shader);
  514. SDL_ReleaseGPUShader(gpu_device, fragment_shader);
  515. /* Set up per-window state */
  516. window_states = (WindowState *) SDL_calloc(state->num_windows, sizeof (WindowState));
  517. if (!window_states) {
  518. SDL_Log("Out of memory!\n");
  519. quit(2);
  520. }
  521. for (i = 0; i < state->num_windows; i++) {
  522. WindowState *winstate = &window_states[i];
  523. /* create a depth texture for the window */
  524. SDL_GetWindowSizeInPixels(state->windows[i], (int*) &drawablew, (int*) &drawableh);
  525. winstate->tex_depth = CreateDepthTexture(drawablew, drawableh);
  526. winstate->tex_msaa = CreateMSAATexture(drawablew, drawableh);
  527. winstate->tex_resolve = CreateResolveTexture(drawablew, drawableh);
  528. /* make each window different */
  529. winstate->angle_x = (i * 10) % 360;
  530. winstate->angle_y = (i * 20) % 360;
  531. winstate->angle_z = (i * 30) % 360;
  532. }
  533. }
  534. static int done = 0;
  535. void loop(void)
  536. {
  537. SDL_Event event;
  538. int i;
  539. /* Check for events */
  540. while (SDL_PollEvent(&event) && !done) {
  541. SDLTest_CommonEvent(state, &event, &done);
  542. }
  543. if (!done) {
  544. for (i = 0; i < state->num_windows; ++i) {
  545. Render(state->windows[i], i);
  546. }
  547. }
  548. #ifdef __EMSCRIPTEN__
  549. else {
  550. emscripten_cancel_main_loop();
  551. }
  552. #endif
  553. }
  554. int
  555. main(int argc, char *argv[])
  556. {
  557. int msaa;
  558. int i;
  559. const SDL_DisplayMode *mode;
  560. Uint64 then, now;
  561. /* Initialize params */
  562. msaa = 0;
  563. /* Initialize test framework */
  564. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  565. if (!state) {
  566. return 1;
  567. }
  568. for (i = 1; i < argc;) {
  569. int consumed;
  570. consumed = SDLTest_CommonArg(state, i);
  571. if (consumed == 0) {
  572. if (SDL_strcasecmp(argv[i], "--msaa") == 0) {
  573. ++msaa;
  574. consumed = 1;
  575. } else {
  576. consumed = -1;
  577. }
  578. }
  579. if (consumed < 0) {
  580. static const char *options[] = { "[--msaa]", NULL };
  581. SDLTest_CommonLogUsage(state, argv[0], options);
  582. quit(1);
  583. }
  584. i += consumed;
  585. }
  586. state->skip_renderer = 1;
  587. state->window_flags |= SDL_WINDOW_RESIZABLE;
  588. if (!SDLTest_CommonInit(state)) {
  589. quit(2);
  590. return 0;
  591. }
  592. mode = SDL_GetCurrentDisplayMode(SDL_GetDisplayForWindow(state->windows[0]));
  593. SDL_Log("Screen bpp: %d\n", SDL_BITSPERPIXEL(mode->format));
  594. init_render_state(msaa);
  595. /* Main render loop */
  596. frames = 0;
  597. then = SDL_GetTicks();
  598. done = 0;
  599. #ifdef __EMSCRIPTEN__
  600. emscripten_set_main_loop(loop, 0, 1);
  601. #else
  602. while (!done) {
  603. loop();
  604. }
  605. #endif
  606. /* Print out some timing information */
  607. now = SDL_GetTicks();
  608. if (now > then) {
  609. SDL_Log("%2.2f frames per second\n",
  610. ((double) frames * 1000) / (now - then));
  611. }
  612. #if !defined(__ANDROID__)
  613. quit(0);
  614. #endif
  615. return 0;
  616. }
  617. /* vi: set ts=4 sw=4 expandtab: */