Browse Source

tests: Fix GPU tests crashing when window is minimized

cosmonaut 4 months ago
parent
commit
0f9e86fe1c
2 changed files with 27 additions and 16 deletions
  1. 21 16
      test/testgpu_simple_clear.c
  2. 6 0
      test/testgpu_spinning_cube.c

+ 21 - 16
test/testgpu_simple_clear.c

@@ -87,22 +87,27 @@ SDL_AppResult SDL_AppIterate(void *appstate)
         return SDL_APP_FAILURE;
     }
 
-    const double currentTime = (double)SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency();
-    SDL_GPURenderPass *renderPass;
-    SDL_GPUColorTargetInfo color_target_info;
-    SDL_zero(color_target_info);
-    color_target_info.texture = swapchainTexture;
-    color_target_info.clear_color.r = (float)(0.5 + 0.5 * SDL_sin(currentTime));
-    color_target_info.clear_color.g = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 2 / 3));
-    color_target_info.clear_color.b = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 4 / 3));
-    color_target_info.clear_color.a = 1.0f;
-    color_target_info.load_op = SDL_GPU_LOADOP_CLEAR;
-    color_target_info.store_op = SDL_GPU_STOREOP_STORE;
-
-    renderPass = SDL_BeginGPURenderPass(cmdbuf, &color_target_info, 1, NULL);
-    SDL_EndGPURenderPass(renderPass);
-
-	SDL_SubmitGPUCommandBuffer(cmdbuf);
+    if (swapchainTexture != NULL) {
+        const double currentTime = (double)SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency();
+        SDL_GPURenderPass *renderPass;
+        SDL_GPUColorTargetInfo color_target_info;
+        SDL_zero(color_target_info);
+        color_target_info.texture = swapchainTexture;
+        color_target_info.clear_color.r = (float)(0.5 + 0.5 * SDL_sin(currentTime));
+        color_target_info.clear_color.g = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 2 / 3));
+        color_target_info.clear_color.b = (float)(0.5 + 0.5 * SDL_sin(currentTime + SDL_PI_D * 4 / 3));
+        color_target_info.clear_color.a = 1.0f;
+        color_target_info.load_op = SDL_GPU_LOADOP_CLEAR;
+        color_target_info.store_op = SDL_GPU_STOREOP_STORE;
+
+        renderPass = SDL_BeginGPURenderPass(cmdbuf, &color_target_info, 1, NULL);
+        SDL_EndGPURenderPass(renderPass);
+
+        SDL_SubmitGPUCommandBuffer(cmdbuf);
+    } else {
+        /* Swapchain is unavailable, cancel work */
+        SDL_CancelGPUCommandBuffer(cmdbuf);
+    }
 
     frames++;
 

+ 6 - 0
test/testgpu_spinning_cube.c

@@ -346,6 +346,12 @@ Render(SDL_Window *window, const int windownum)
         quit(2);
     }
 
+    if (swapchainTexture == NULL) {
+        /* Swapchain is unavailable, cancel work */
+        SDL_CancelGPUCommandBuffer(cmd);
+        return;
+    }
+
     /*
     * Do some rotation with Euler angles. It is not a fixed axis as
     * quaternions would be, but the effect is cool.