From 12b9cf1898ba17c7b0461ba07d8894c4c96ec4fb Mon Sep 17 00:00:00 2001
From: Stenzek <stenzek@gmail.com>
Date: Sun, 31 Dec 2023 19:40:45 +1000
Subject: [PATCH] GPU: Fix crash when trying to postprocess surfaceless

---
 src/core/gpu.cpp            | 6 ++++--
 src/util/postprocessing.cpp | 1 +
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/core/gpu.cpp b/src/core/gpu.cpp
index 224f65c7a..eb289f062 100644
--- a/src/core/gpu.cpp
+++ b/src/core/gpu.cpp
@@ -1716,8 +1716,10 @@ bool GPU::RenderDisplay(GPUTexture* target, const Common::Rectangle<s32>& draw_r
   const GPUTexture::Format hdformat = target ? target->GetFormat() : g_gpu_device->GetWindowFormat();
   const u32 target_width = target ? target->GetWidth() : g_gpu_device->GetWindowWidth();
   const u32 target_height = target ? target->GetHeight() : g_gpu_device->GetWindowHeight();
-  const bool really_postfx = (postfx && HasDisplayTexture() && PostProcessing::IsActive() &&
-                              PostProcessing::CheckTargets(hdformat, target_width, target_height));
+  const bool really_postfx =
+    (postfx && HasDisplayTexture() && PostProcessing::IsActive() && !g_gpu_device->GetWindowInfo().IsSurfaceless() &&
+     hdformat != GPUTexture::Format::Unknown && target_width > 0 && target_height > 0 &&
+     PostProcessing::CheckTargets(hdformat, target_width, target_height));
   const Common::Rectangle<s32> real_draw_rect =
     g_gpu_device->UsesLowerLeftOrigin() ? GPUDevice::FlipToLowerLeft(draw_rect, target_height) : draw_rect;
   if (really_postfx)
diff --git a/src/util/postprocessing.cpp b/src/util/postprocessing.cpp
index 67fa86943..5b6823537 100644
--- a/src/util/postprocessing.cpp
+++ b/src/util/postprocessing.cpp
@@ -681,6 +681,7 @@ bool PostProcessing::CheckTargets(GPUTexture::Format target_format, u32 target_w
       !(s_output_texture = g_gpu_device->FetchTexture(target_width, target_height, 1, 1, 1,
                                                       GPUTexture::Type::RenderTarget, target_format)))
   {
+    DestroyTextures();
     return false;
   }