diff options
Diffstat (limited to 'contrib/SDL-3.2.8/test/testgpu_simple_clear.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testgpu_simple_clear.c | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testgpu_simple_clear.c b/contrib/SDL-3.2.8/test/testgpu_simple_clear.c new file mode 100644 index 0000000..41016ee --- /dev/null +++ b/contrib/SDL-3.2.8/test/testgpu_simple_clear.c | |||
| @@ -0,0 +1,128 @@ | |||
| 1 | /* | ||
| 2 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 3 | |||
| 4 | This software is provided 'as-is', without any express or implied | ||
| 5 | warranty. In no event will the authors be held liable for any damages | ||
| 6 | arising from the use of this software. | ||
| 7 | |||
| 8 | Permission is granted to anyone to use this software for any purpose, | ||
| 9 | including commercial applications, and to alter it and redistribute it | ||
| 10 | freely. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #define SDL_MAIN_USE_CALLBACKS 1 | ||
| 14 | #include <SDL3/SDL.h> | ||
| 15 | #include <SDL3/SDL_main.h> | ||
| 16 | #include <SDL3/SDL_test.h> | ||
| 17 | |||
| 18 | /* we don't actually use any shaders in this one, so just give us lots of options for backends. */ | ||
| 19 | #define TESTGPU_SUPPORTED_FORMATS (SDL_GPU_SHADERFORMAT_SPIRV | SDL_GPU_SHADERFORMAT_DXBC | SDL_GPU_SHADERFORMAT_DXIL | SDL_GPU_SHADERFORMAT_METALLIB) | ||
| 20 | |||
| 21 | static SDLTest_CommonState *state; | ||
| 22 | static SDL_GPUDevice *gpu_device; | ||
| 23 | static Uint64 then = 0; | ||
| 24 | static Uint64 frames = 0; | ||
| 25 | |||
| 26 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
| 27 | { | ||
| 28 | const SDL_DisplayMode *mode; | ||
| 29 | int dw, dh; | ||
| 30 | |||
| 31 | /* Initialize test framework */ | ||
| 32 | state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); | ||
| 33 | if (!state) { | ||
| 34 | return SDL_APP_FAILURE; | ||
| 35 | } | ||
| 36 | |||
| 37 | state->skip_renderer = 1; | ||
| 38 | |||
| 39 | if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) { | ||
| 40 | SDLTest_CommonQuit(state); | ||
| 41 | return SDL_APP_FAILURE; | ||
| 42 | } | ||
| 43 | |||
| 44 | gpu_device = SDL_CreateGPUDevice(TESTGPU_SUPPORTED_FORMATS, true, NULL); | ||
| 45 | if (!gpu_device) { | ||
| 46 | SDL_Log("SDL_CreateGPUDevice failed: %s", SDL_GetError()); | ||
| 47 | return SDL_APP_FAILURE; | ||
| 48 | } | ||
| 49 | |||
| 50 | if (!SDL_ClaimWindowForGPUDevice(gpu_device, state->windows[0])) { | ||
| 51 | SDL_Log("SDL_ClaimWindowForGPUDevice failed: %s", SDL_GetError()); | ||
| 52 | return SDL_APP_FAILURE; | ||
| 53 | } | ||
| 54 | |||
| 55 | mode = SDL_GetCurrentDisplayMode(SDL_GetPrimaryDisplay()); | ||
| 56 | if (mode) { | ||
| 57 | SDL_Log("Screen BPP : %d", SDL_BITSPERPIXEL(mode->format)); | ||
| 58 | } | ||
| 59 | SDL_GetWindowSize(state->windows[0], &dw, &dh); | ||
| 60 | SDL_Log("Window Size : %d,%d", dw, dh); | ||
| 61 | SDL_GetWindowSizeInPixels(state->windows[0], &dw, &dh); | ||
| 62 | SDL_Log("Draw Size : %d,%d", dw, dh); | ||
| 63 | SDL_Log("%s", ""); | ||
| 64 | |||
| 65 | then = SDL_GetTicks(); | ||
| 66 | |||
| 67 | return SDL_APP_CONTINUE; | ||
| 68 | } | ||
| 69 | |||
| 70 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 71 | { | ||
| 72 | return SDLTest_CommonEventMainCallbacks(state, event); | ||
| 73 | } | ||
| 74 | |||
| 75 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 76 | { | ||
| 77 | SDL_GPUCommandBuffer *cmdbuf = SDL_AcquireGPUCommandBuffer(gpu_device); | ||
| 78 | |||
| 79 | if (cmdbuf == NULL) { | ||
| 80 | SDL_Log("SDL_AcquireGPUCommandBuffer failed: %s", SDL_GetError()); | ||
| 81 | return SDL_APP_FAILURE; | ||
| 82 | } | ||
| 83 | |||
| 84 | SDL_GPUTexture *swapchainTexture; | ||
| 85 | if (!SDL_WaitAndAcquireGPUSwapchainTexture(cmdbuf, state->windows[0], &swapchainTexture, NULL, NULL)) { | ||
| 86 | SDL_Log("SDL_AcquireGPUSwapchainTexture failed: %s", SDL_GetError()); | ||
| 87 | return SDL_APP_FAILURE; | ||
| 88 | } | ||
| 89 | |||
| 90 | if (swapchainTexture != NULL) { | ||
| 91 | const double currentTime = (double)SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency(); | ||
| 92 | SDL_GPURenderPass *renderPass; | ||
| 93 | SDL_GPUColorTargetInfo color_target_info; | ||
| 94 | SDL_zero(color_target_info); | ||
| 95 | color_target_info.texture = swapchainTexture; | ||
| 96 | color_target_info.clear_color.r = (float)(0.5 + 0.5 * SDL_sin(currentTime)); | ||
| 97 | color_target_info.clear_color.g = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 2 / 3)); | ||
| 98 | color_target_info.clear_color.b = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 4 / 3)); | ||
| 99 | color_target_info.clear_color.a = 1.0f; | ||
| 100 | color_target_info.load_op = SDL_GPU_LOADOP_CLEAR; | ||
| 101 | color_target_info.store_op = SDL_GPU_STOREOP_STORE; | ||
| 102 | |||
| 103 | renderPass = SDL_BeginGPURenderPass(cmdbuf, &color_target_info, 1, NULL); | ||
| 104 | SDL_EndGPURenderPass(renderPass); | ||
| 105 | |||
| 106 | SDL_SubmitGPUCommandBuffer(cmdbuf); | ||
| 107 | } else { | ||
| 108 | /* Swapchain is unavailable, cancel work */ | ||
| 109 | SDL_CancelGPUCommandBuffer(cmdbuf); | ||
| 110 | } | ||
| 111 | |||
| 112 | frames++; | ||
| 113 | |||
| 114 | return SDL_APP_CONTINUE; | ||
| 115 | } | ||
| 116 | |||
| 117 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 118 | { | ||
| 119 | /* Print out some timing information */ | ||
| 120 | const Uint64 now = SDL_GetTicks(); | ||
| 121 | if (now > then) { | ||
| 122 | SDL_Log("%2.2f frames per second", ((double)frames * 1000) / (now - then)); | ||
| 123 | } | ||
| 124 | |||
| 125 | SDL_ReleaseWindowFromGPUDevice(gpu_device, state->windows[0]); | ||
| 126 | SDL_DestroyGPUDevice(gpu_device); | ||
| 127 | SDLTest_CommonQuit(state); | ||
| 128 | } | ||
