SDL: Treat SRGB render targets the same as SRGB swapchains

From 91b5e623b4b1642012cfaa7873920b083b420c50 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 16 Oct 2026 14:22:20 -0700
Subject: [PATCH] Treat SRGB render targets the same as SRGB swapchains

Previously if the renderer swap chain was using a linear colorspace, then sRGB render targets would also do linear blending. Instead we want an sRGB render target to behave like an sRGB swap chain, blending in sRGB space.
---
 src/render/direct3d11/SDL_render_d3d11.c | 67 ++++++++++---------
 src/render/direct3d12/SDL_render_d3d12.c | 67 ++++++++++---------
 src/render/gpu/SDL_render_gpu.c          | 32 ++++++---
 src/render/metal/SDL_render_metal.m      | 21 ++++--
 src/render/vulkan/SDL_render_vulkan.c    | 73 +++++++++++----------
 test/testcolorspace.c                    | 83 ++++++++++++++++++++++++
 6 files changed, 234 insertions(+), 109 deletions(-)

diff --git a/src/render/direct3d11/SDL_render_d3d11.c b/src/render/direct3d11/SDL_render_d3d11.c
index f5f11e4fd8c27..ad836b7c649a1 100644
--- a/src/render/direct3d11/SDL_render_d3d11.c
+++ b/src/render/direct3d11/SDL_render_d3d11.c
@@ -124,6 +124,7 @@ typedef struct
     ID3D11Texture2D *mainTexture;
     ID3D11ShaderResourceView *mainTextureResourceView;
     ID3D11RenderTargetView *mainTextureRenderTargetView;
+    bool srgb_view;
     ID3D11Texture2D *stagingTexture;
     int lockedTexturePositionX;
     int lockedTexturePositionY;
@@ -293,6 +294,18 @@ static DXGI_FORMAT SDLPixelFormatToDXGITextureFormat(Uint32 format, Uint32 outpu
     }
 }
 
+static bool DXGIFormatIsSRGB(DXGI_FORMAT format)
+{
+    switch (format) {
+    case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
+    case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
+    case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
+        return true;
+    default:
+        return false;
+    }
+}
+
 static DXGI_FORMAT SDLPixelFormatToDXGIMainResourceViewFormat(Uint32 format, Uint32 colorspace)
 {
     switch (format) {
@@ -1208,7 +1221,9 @@ static bool D3D11_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
     D3D11_RenderData *rendererData = (D3D11_RenderData *)renderer->internal;
     D3D11_TextureData *textureData;
     HRESULT result;
-    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGITextureFormat(texture->format, renderer->output_colorspace);
+    // Render targets use formats matching their own colorspace, so an sRGB target behaves like an sRGB swap chain
+    const Uint32 format_colorspace = (texture->access == SDL_TEXTUREACCESS_TARGET) ? texture->colorspace : renderer->output_colorspace;
+    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGITextureFormat(texture->format, format_colorspace);
     D3D11_TEXTURE2D_DESC textureDesc;
     D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
 
@@ -1227,6 +1242,7 @@ static bool D3D11_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
     }
 
     texture->internal = textureData;
+    textureData->srgb_view = DXGIFormatIsSRGB(textureFormat);
 
     SDL_zero(textureDesc);
     textureDesc.Width = texture->w;
@@ -1368,7 +1384,7 @@ static bool D3D11_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
         }
     }
     SDL_zero(resourceViewDesc);
-    resourceViewDesc.Format = SDLPixelFormatToDXGIMainResourceViewFormat(texture->format, renderer->output_colorspace);
+    resourceViewDesc.Format = SDLPixelFormatToDXGIMainResourceViewFormat(texture->format, format_colorspace);
     resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
     resourceViewDesc.Texture2D.MostDetailedMip = 0;
     resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
@@ -2172,33 +2188,21 @@ static ID3D11RenderTargetView *D3D11_GetCurrentRenderTargetView(SDL_Renderer *re
     }
 }
 
-// Returns true if the texture sampler converts this format from sRGB to linear
-// This needs to match the formats with sRGB variants in dxgi_format_map
-static bool SamplerConvertsSRGB(Uint32 format, Uint32 output_colorspace)
+// Returns the input type for sRGB textures, based on whether the sampler converts them to linear
+static float GetSRGBInputType(SDL_Renderer *renderer, bool srgb_view)
 {
-    if (output_colorspace != SDL_COLORSPACE_SRGB_LINEAR &&
-        output_colorspace != SDL_COLORSPACE_HDR10) {
-        return false;
-    }
-
-    switch (format) {
-    case SDL_PIXELFORMAT_ARGB8888:
-    case SDL_PIXELFORMAT_ABGR8888:
-    case SDL_PIXELFORMAT_XRGB8888:
-        return true;
-    default:
-        return false;
-    }
-}
-
-static float GetSRGBInputType(SDL_Renderer *renderer, Uint32 format)
-{
-    if (SDL_RenderingLinearSpace(renderer) &&
-        !SamplerConvertsSRGB(format, renderer->output_colorspace)) {
-        // The shader needs to convert from sRGB to linear
-        return INPUTTYPE_SRGB;
+    if (SDL_RenderingLinearSpace(renderer)) {
+        if (!srgb_view) {
+            // The shader needs to convert from sRGB to linear
+            return INPUTTYPE_SRGB;
+        }
+    } else {
+        if (srgb_view) {
+            // The sampler converts to linear, so the shader needs to convert back to sRGB
+            return INPUTTYPE_SCRGB;
+        }
     }
-    // The sampler will convert from sRGB to linear on load if working in linear colorspace
+    // The texture is already in the colorspace we're rendering in
     return INPUTTYPE_UNSPECIFIED;
 }
 
@@ -2230,8 +2234,11 @@ static void D3D11_SetupShaderConstants(SDL_Renderer *renderer, const SDL_RenderC
                 SDL_assert(!"Unknown scale mode");
                 break;
             }
-            // The palette is stored in an RGBA32 texture
-            constants->input_type = GetSRGBInputType(renderer, SDL_PIXELFORMAT_RGBA32);
+            {
+                // The palette is stored in an RGBA32 texture
+                const DXGI_FORMAT paletteFormat = SDLPixelFormatToDXGITextureFormat(SDL_PIXELFORMAT_RGBA32, renderer->output_colorspace);
+                constants->input_type = GetSRGBInputType(renderer, DXGIFormatIsSRGB(paletteFormat));
+            }
             break;
         case SDL_PIXELFORMAT_YV12:
         case SDL_PIXELFORMAT_IYUV:
@@ -2267,7 +2274,7 @@ static void D3D11_SetupShaderConstants(SDL_Renderer *renderer, const SDL_RenderC
             } else if (texture->colorspace == SDL_COLORSPACE_HDR10) {
                 constants->input_type = INPUTTYPE_HDR10;
             } else {
-                constants->input_type = GetSRGBInputType(renderer, texture->format);
+                constants->input_type = GetSRGBInputType(renderer, textureData->srgb_view);
             }
             break;
         }
diff --git a/src/render/direct3d12/SDL_render_d3d12.c b/src/render/direct3d12/SDL_render_d3d12.c
index 8da17346a08d2..b79b19c0793ff 100644
--- a/src/render/direct3d12/SDL_render_d3d12.c
+++ b/src/render/direct3d12/SDL_render_d3d12.c
@@ -124,6 +124,7 @@ typedef struct
     SIZE_T mainSRVIndex;
     D3D12_CPU_DESCRIPTOR_HANDLE mainTextureRenderTargetView;
     DXGI_FORMAT mainTextureFormat;
+    bool srgb_view;
     ID3D12Resource *stagingBuffer;
     D3D12_RESOURCE_STATES stagingResourceState;
     const float *YCbCr_matrix;
@@ -353,6 +354,18 @@ static DXGI_FORMAT SDLPixelFormatToDXGITextureFormat(SDL_PixelFormat format, Uin
     }
 }
 
+static bool DXGIFormatIsSRGB(DXGI_FORMAT format)
+{
+    switch (format) {
+    case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
+    case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
+    case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
+        return true;
+    default:
+        return false;
+    }
+}
+
 static DXGI_FORMAT SDLPixelFormatToDXGIMainResourceViewFormat(SDL_PixelFormat format, Uint32 colorspace)
 {
     switch (format) {
@@ -1622,7 +1635,9 @@ static bool D3D12_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
     D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->internal;
     D3D12_TextureData *textureData;
     HRESULT result;
-    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGITextureFormat(texture->format, renderer->output_colorspace);
+    // Render targets use formats matching their own colorspace, so an sRGB target behaves like an sRGB swap chain
+    const Uint32 format_colorspace = (texture->access == SDL_TEXTUREACCESS_TARGET) ? texture->colorspace : renderer->output_colorspace;
+    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGITextureFormat(texture->format, format_colorspace);
     D3D12_RESOURCE_DESC textureDesc;
     D3D12_HEAP_PROPERTIES heapProps;
     D3D12_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
@@ -1641,6 +1656,7 @@ static bool D3D12_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
     }
 
     texture->internal = textureData;
+    textureData->srgb_view = DXGIFormatIsSRGB(textureFormat);
     textureData->mainTextureFormat = textureFormat;
 
     SDL_zero(textureDesc);
@@ -1807,7 +1823,7 @@ static bool D3D12_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
     }
     SDL_zero(resourceViewDesc);
     resourceViewDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
-    resourceViewDesc.Format = SDLPixelFormatToDXGIMainResourceViewFormat(texture->format, renderer->output_colorspace);
+    resourceViewDesc.Format = SDLPixelFormatToDXGIMainResourceViewFormat(texture->format, format_colorspace);
     resourceViewDesc.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D;
     resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
 
@@ -2634,33 +2650,21 @@ static bool D3D12_UpdateViewport(SDL_Renderer *renderer)
     return true;
 }
 
-// Returns true if the texture sampler converts this format from sRGB to linear
-// This needs to match the formats with sRGB variants in dxgi_format_map
-static bool SamplerConvertsSRGB(Uint32 format, Uint32 output_colorspace)
+// Returns the input type for sRGB textures, based on whether the sampler converts them to linear
+static float GetSRGBInputType(SDL_Renderer *renderer, bool srgb_view)
 {
-    if (output_colorspace != SDL_COLORSPACE_SRGB_LINEAR &&
-        output_colorspace != SDL_COLORSPACE_HDR10) {
-        return false;
-    }
-
-    switch (format) {
-    case SDL_PIXELFORMAT_ARGB8888:
-    case SDL_PIXELFORMAT_ABGR8888:
-    case SDL_PIXELFORMAT_XRGB8888:
-        return true;
-    default:
-        return false;
-    }
-}
-
-static float GetSRGBInputType(SDL_Renderer *renderer, Uint32 format)
-{
-    if (SDL_RenderingLinearSpace(renderer) &&
-        !SamplerConvertsSRGB(format, renderer->output_colorspace)) {
-        // The shader needs to convert from sRGB to linear
-        return INPUTTYPE_SRGB;
+    if (SDL_RenderingLinearSpace(renderer)) {
+        if (!srgb_view) {
+            // The shader needs to convert from sRGB to linear
+            return INPUTTYPE_SRGB;
+        }
+    } else {
+        if (srgb_view) {
+            // The sampler converts to linear, so the shader needs to convert back to sRGB
+            return INPUTTYPE_SCRGB;
+        }
     }
-    // The sampler will convert from sRGB to linear on load if working in linear colorspace
+    // The texture is already in the colorspace we're rendering in
     return INPUTTYPE_UNSPECIFIED;
 }
 
@@ -2692,8 +2696,11 @@ static void D3D12_SetupShaderConstants(SDL_Renderer *renderer, const SDL_RenderC
                 SDL_assert(!"Unknown scale mode");
                 break;
             }
-            // The palette is stored in an RGBA32 texture
-            constants->input_type = GetSRGBInputType(renderer, SDL_PIXELFORMAT_RGBA32);
+            {
+                // The palette is stored in an RGBA32 texture
+                const DXGI_FORMAT paletteFormat = SDLPixelFormatToDXGITextureFormat(SDL_PIXELFORMAT_RGBA32, renderer->output_colorspace);
+                constants->input_type = GetSRGBInputType(renderer, DXGIFormatIsSRGB(paletteFormat));
+            }
             break;
         case SDL_PIXELFORMAT_YV12:
         case SDL_PIXELFORMAT_IYUV:
@@ -2729,7 +2736,7 @@ static void D3D12_SetupShaderConstants(SDL_Renderer *renderer, const SDL_RenderC
             } else if (texture->colorspace == SDL_COLORSPACE_HDR10) {
                 constants->input_type = INPUTTYPE_HDR10;
             } else {
-                constants->input_type = GetSRGBInputType(renderer, texture->format);
+                constants->input_type = GetSRGBInputType(renderer, textureData->srgb_view);
             }
             break;
         }
diff --git a/src/render/gpu/SDL_render_gpu.c b/src/render/gpu/SDL_render_gpu.c
index af89a39f258fc..2226ea635ec55 100644
--- a/src/render/gpu/SDL_render_gpu.c
+++ b/src/render/gpu/SDL_render_gpu.c
@@ -301,7 +301,9 @@ static bool GPU_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL_
         format = SDL_GetGPUTextureFormatFromPixelFormat(texture->format);
         break;
     }
-    if (renderer->output_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
+    // Render targets use formats matching their own colorspace, so an sRGB target behaves like an sRGB swap chain
+    const SDL_Colorspace format_colorspace = (texture->access == SDL_TEXTUREACCESS_TARGET) ? texture->colorspace : renderer->output_colorspace;
+    if (format_colorspace == SDL_COLORSPACE_SRGB_LINEAR) {
         switch (format) {
         case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM:
             format = SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB;
@@ -875,15 +877,24 @@ static SDL_GPUSampler *GetSampler(GPU_RenderData *data, SDL_PixelFormat format,
     return data->samplers[key];
 }
 
+// Returns the input type for sRGB textures, based on whether the sampler converts them to linear
 static float GetSRGBInputType(SDL_Renderer *renderer, SDL_GPUTextureFormat format)
 {
-    if (SDL_RenderingLinearSpace(renderer) &&
-        format != SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB &&
-        format != SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB) {
-        // The shader needs to convert from sRGB to linear
-        return INPUTTYPE_SRGB;
+    const bool srgb_view = (format == SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB ||
+                            format == SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB);
+
+    if (SDL_RenderingLinearSpace(renderer)) {
+        if (!srgb_view) {
+            // The shader needs to convert from sRGB to linear
+            return INPUTTYPE_SRGB;
+        }
+    } else {
+        if (srgb_view) {
+            // The sampler converts to linear, so the shader needs to convert back to sRGB
+            return INPUTTYPE_SCRGB;
+        }
     }
-    // The sampler will convert from sRGB to linear on load if working in linear colorspace
+    // The texture is already in the colorspace we're rendering in
     return INPUTTYPE_UNSPECIFIED;
 }
 
@@ -912,8 +923,11 @@ static void CalculateAdvancedShaderConstants(SDL_Renderer *renderer, const SDL_R
             SDL_assert(!"Unknown scale mode");
             break;
         }
-        // The palette is stored in an RGBA32 texture
-        constants->input_type = GetSRGBInputType(renderer, SDL_GetGPUTextureFormatFromPixelFormat(SDL_PIXELFORMAT_RGBA32));
+        {
+            // The palette is stored in an RGBA32 texture
+            const SDL_GPUTextureFormat palette_format = SDL_GetGPUTextureFormatFromPixelFormat(SDL_PIXELFORMAT_RGBA32);
+            constants->input_type = GetSRGBInputType(renderer, palette_format);
+        }
         break;
     case SDL_PIXELFORMAT_YV12:
     case SDL_PIXELFORMAT_IYUV:
diff --git a/src/render/metal/SDL_render_metal.m b/src/render/metal/SDL_render_metal.m
index ebeb847ec188a..f6c6b5647b210 100644
--- a/src/render/metal/SDL_render_metal.m
+++ b/src/render/metal/SDL_render_metal.m
@@ -719,16 +719,20 @@ static bool METAL_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SD
             }
         }
 
+        // Render targets use formats matching their own colorspace, so an sRGB target behaves like an sRGB swap chain
+        const SDL_Colorspace format_colorspace = (texture->access == SDL_TEXTUREACCESS_TARGET) ? texture->colorspace : renderer->output_colorspace;
+        const bool use_srgb = (format_colorspace == SDL_COLORSPACE_SRGB_LINEAR || format_colorspace == SDL_COLORSPACE_HDR10);
+
         switch (texture->format) {
         case SDL_PIXELFORMAT_ABGR8888:
-            if (UseSRGBTextureFormats(renderer)) {
+            if (use_srgb) {
                 pixfmt = MTLPixelFormatRGBA8Unorm_sRGB;
             } else {
                 pixfmt = MTLPixelFormatRGBA8Unorm;
             }
             break;
         case SDL_PIXELFORMAT_ARGB8888:
-            if (UseSRGBTextureFormats(renderer)) {
+            if (use_srgb) {
                 pixfmt = MTLPixelFormatBGRA8Unorm_sRGB;
             } else {
                 pixfmt = MTLPixelFormatBGRA8Unorm;
@@ -1489,7 +1493,7 @@ static bool METAL_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd,
     PixelShaderConstants shader_constants;
 } METAL_DrawStateCache;
 
-static float GetSRGBInputType(const SDL_Texture *texture, float texture_type)
+static float GetSRGBInputType(SDL_Renderer *renderer, const SDL_Texture *texture, float texture_type)
 {
     if (texture_type == TEXTURETYPE_YUV ||
         texture_type == TEXTURETYPE_NV12 ||
@@ -1510,8 +1514,13 @@ static float GetSRGBInputType(const SDL_Texture *texture, float texture_type)
     switch (mtltexture.pixelFormat) {
     case MTLPixelFormatRGBA8Unorm_sRGB:
     case MTLPixelFormatBGRA8Unorm_sRGB:
-        // The sampler will convert from sRGB to linear on load
-        return INPUTTYPE_UNSPECIFIED;
+        if (SDL_RenderingLinearSpace(renderer)) {
+            // The sampler will convert from sRGB to linear on load
+            return INPUTTYPE_UNSPECIFIED;
+        } else {
+            // The sampler converts to linear, so the shader needs to convert back to sRGB
+            return INPUTTYPE_SCRGB;
+        }
     default:
         // The shader needs to convert from sRGB if necessary
         return INPUTTYPE_SRGB;
@@ -1587,7 +1596,7 @@ static void SetupShaderConstants(SDL_Renderer *renderer, const SDL_RenderCommand
             constants->input_type = INPUTTYPE_HDR10;
             break;
         default:
-            constants->input_type = GetSRGBInputType(texture, constants->texture_type);
+            constants->input_type = GetSRGBInputType(renderer, texture, constants->texture_type);
             break;
         }
 
diff --git a/src/render/vulkan/SDL_render_vulkan.c b/src/render/vulkan/SDL_render_vulkan.c
index 66f1c2e5e6fa0..d1a09a4b6d0e4 100644
--- a/src/render/vulkan/SDL_render_vulkan.c
+++ b/src/render/vulkan/SDL_render_vulkan.c
@@ -322,6 +322,7 @@ typedef struct
     VULKAN_Buffer stagingBuffer;
     int width;
     int height;
+    bool srgb_view;
     const float *YCbCr_matrix;
 
     // YCbCr conversion support
@@ -599,6 +600,18 @@ static VkFormat VULKAN_GetVkImageFormat(SDL_PixelFormat format, Uint32 output_co
     }
 }
 
+static bool VkFormatIsSRGB(VkFormat format)
+{
+    switch (format) {
+    case VK_FORMAT_B8G8R8A8_SRGB:
+    case VK_FORMAT_R8G8B8A8_SRGB:
+    case VK_FORMAT_A8B8G8R8_SRGB_PACK32:
+        return true;
+    default:
+        return false;
+    }
+}
+
 static VkFormat VULKAN_GetVkImageViewFormat(SDL_PixelFormat format, int plane, Uint32 output_colorspace)
 {
     switch (format) {
@@ -3037,7 +3050,9 @@ static bool VULKAN_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, S
     VkResult result;
     int numImages = VULKAN_GetFormatImageCount(texture->format);
     int numImageViews = VULKAN_GetFormatImageViewCount(texture->format);
-    VkFormat textureFormat = VULKAN_GetVkImageFormat(texture->format, renderer->output_colorspace);
+    // Render targets use formats matching their own colorspace, so an sRGB target behaves like an sRGB swap chain
+    const Uint32 format_colorspace = (texture->access == SDL_TEXTUREACCESS_TARGET) ? texture->colorspace : renderer->output_colorspace;
+    VkFormat textureFormat = VULKAN_GetVkImageFormat(texture->format, format_colorspace);
     VkFormat chromaFormat = textureFormat;
     uint32_t width = texture->w;
     uint32_t height = texture->h;
@@ -3056,6 +3071,7 @@ static bool VULKAN_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, S
         return false;
     }
     texture->internal = textureData;
+    textureData->srgb_view = VkFormatIsSRGB(textureFormat);
 
     if (texture->format == SDL_PIXELFORMAT_EXTERNAL_OES) {
         SDL_zero(externalBufferInfo);
@@ -3227,8 +3243,8 @@ static bool VULKAN_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, S
         numImages = 2;
         chromaWidth = (width / 2);
         chromaHeight = (height / 2);
-        textureFormat = VULKAN_GetVkImageViewFormat(texture->format, 0, renderer->output_colorspace);
-        chromaFormat = VULKAN_GetVkImageViewFormat(texture->format, 1, renderer->output_colorspace);
+        textureFormat = VULKAN_GetVkImageViewFormat(texture->format, 0, format_colorspace);
+        chromaFormat = VULKAN_GetVkImageViewFormat(texture->format, 1, format_colorspace);
         imageCreateProperties[1] = SDL_PROP_TEXTURE_CREATE_VULKAN_TEXTURE_UV_NUMBER;
     }
 
@@ -3248,7 +3264,7 @@ static bool VULKAN_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, S
     bool usePlaneViews = (numImageViews > numImages);
     int imageIndex = 0;
     for (int i = 0; i < numImageViews; ++i) {
-        VkFormat imageViewFormat = VULKAN_GetVkImageViewFormat(texture->format, i, renderer->output_colorspace);
+        VkFormat imageViewFormat = VULKAN_GetVkImageViewFormat(texture->format, i, format_colorspace);
         int imagePlane = usePlaneViews ? i : -1;
         result = VULKAN_AllocateImageView(rendererData, textureData->images[imageIndex].image, imagePlane, imageViewFormat, usage, imageViewSwizzle, samplerYcbcrConversion, &textureData->imageViews[i]);
         if (result != VK_SUCCESS) {
@@ -3995,35 +4011,21 @@ static bool VULKAN_UpdateClipRect(SDL_Renderer *renderer)
     return true;
 }
 
-// Returns true if the texture sampler converts this format from sRGB to linear
-// This needs to match the formats with sRGB variants in vk_format_map
-static bool SamplerConvertsSRGB(Uint32 format, Uint32 output_colorspace)
+// Returns the input type for sRGB textures, based on whether the sampler converts them to linear
+static float GetSRGBInputType(SDL_Renderer *renderer, bool srgb_view)
 {
-    if (output_colorspace != SDL_COLORSPACE_SRGB_LINEAR &&
-        output_colorspace != SDL_COLORSPACE_HDR10) {
-        return false;
-    }
-
-    switch (format) {
-    case SDL_PIXELFORMAT_BGRA32:
-    case SDL_PIXELFORMAT_RGBA32:
-#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-    case SDL_PIXELFORMAT_ABGR8888:
-#endif
-        return true;
-    default:
-        return false;
-    }
-}
-
-static float GetSRGBInputType(SDL_Renderer *renderer, Uint32 format)
-{
-    if (SDL_RenderingLinearSpace(renderer) &&
-        !SamplerConvertsSRGB(format, renderer->output_colorspace)) {
-        // The shader needs to convert from sRGB to linear
-        return INPUTTYPE_SRGB;
+    if (SDL_RenderingLinearSpace(renderer)) {
+        if (!srgb_view) {
+            // The shader needs to convert from sRGB to linear
+            return INPUTTYPE_SRGB;
+        }
+    } else {
+        if (srgb_view) {
+            // The sampler converts to linear, so the shader needs to convert back to sRGB
+            return INPUTTYPE_SCRGB;
+        }
     }
-    // The sampler will convert from sRGB to linear on load if working in linear colorspace
+    // The texture is already in the colorspace we're rendering in
     return INPUTTYPE_UNSPECIFIED;
 }
 
@@ -4056,8 +4058,11 @@ static void VULKAN_SetupShaderConstants(SDL_Renderer *renderer, const SDL_Render
                 SDL_assert(!"Unknown scale mode");
                 break;
             }
-            // The palette is stored in an RGBA32 texture
-            constants->input_type = GetSRGBInputType(renderer, SDL_PIXELFORMAT_RGBA32);
+            {
+                // The palette is stored in an RGBA32 texture
+                const VkFormat paletteFormat = VULKAN_GetVkImageFormat(SDL_PIXELFORMAT_RGBA32, renderer->output_colorspace);
+                constants->input_type = GetSRGBInputType(renderer, VkFormatIsSRGB(paletteFormat));
+            }
             break;
         case SDL_PIXELFORMAT_YV12:
         case SDL_PIXELFORMAT_IYUV:
@@ -4094,7 +4099,7 @@ static void VULKAN_SetupShaderConstants(SDL_Renderer *renderer, const SDL_Render
                        SDL_COLORSPACETRANSFER(texture->colorspace) == SDL_TRANSFER_CHARACTERISTICS_PQ) {
                 constants->input_type = INPUTTYPE_HDR10;
             } else {
-                constants->input_type = GetSRGBInputType(renderer, texture->format);
+                constants->input_type = GetSRGBInputType(renderer, textureData->srgb_view);
             }
             break;
         }
diff --git a/test/testcolorspace.c b/test/testcolorspace.c
index 7aa140e8ec34a..82ff644a7014f 100644
--- a/test/testcolorspace.c
+++ b/test/testcolorspace.c
@@ -53,6 +53,7 @@ enum
     StageDrawBackground,
     StageTextureBackground,
     StageTargetBackground,
+    StageTargetDrawing,
     StageTextureFormats,
     StageBlendDrawing,
     StageBlendTexture,
@@ -421,6 +422,85 @@ static bool ColorMatches(const SDL_Color *c, int r, int g, int b, int epsilon)
     return false;
 }
 
+static void RenderTargetDrawing(void)
+{
+    /* Clear one render target to 50% gray, fill another with 50% gray, and blend white at 50% over black in a third.
+     * The window is split into thirds showing each of the targets, which should all be 50% gray.
+     * The render targets are sRGB, so they should behave like an sRGB swap chain,
+     * including blending in sRGB space, whatever the output colorspace.
+     */
+    SDL_Texture *cleared = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, 1, 1);
+    SDL_Texture *filled = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, 1, 1);
+    SDL_Texture *blended = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, SDL_TEXTUREACCESS_TARGET, 1, 1);
+    SDL_FRect rect;
+
+    SDL_SetRenderTarget(renderer, cleared);
+    SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255);
+    SDL_RenderClear(renderer);
+
+    SDL_SetRenderTarget(renderer, filled);
+    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
+    SDL_RenderClear(renderer);
+    SDL_SetRenderDrawColor(renderer, 128, 128, 128, 255);
+    SDL_RenderFillRect(renderer, NULL);
+
+    SDL_SetRenderTarget(renderer, blended);
+    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
+    SDL_RenderClear(renderer);
+    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
+    SDL_SetRenderDrawColor(renderer, 255, 255, 255, 128);
+    SDL_RenderFillRect(renderer, NULL);
+    SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
+
+    /* Fill the output with the render targets */
+    SDL_SetRenderTarget(renderer, NULL);
+    rect.x = 0.0f;
+    rect.y = 0.0f;
+    rect.w = WINDOW_WIDTH / 3;
+    rect.h = WINDOW_HEIGHT;
+    SDL_RenderTexture(renderer, cleared, NULL, &rect);
+    rect.x += rect.w;
+    SDL_RenderTexture(renderer, filled, NULL, &rect);
+    rect.x += rect.w;
+    SDL_RenderTexture(renderer, blended, NULL, &rect);
+    SDL_DestroyTexture(cleared);
+    SDL_DestroyTexture(filled);
+    SDL_DestroyTexture(blended);
+
+    /* Check the rendered pixels */
+    SDL_Color c1, c2, c3;
+    if (!ReadPixel(WINDOW_WIDTH / 6, WINDOW_HEIGHT / 2, &c1) ||
+        !ReadPixel(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2, &c2) ||
+        !ReadPixel((WINDOW_WIDTH * 5) / 6, WINDOW_HEIGHT / 2, &c3)) {
+        return;
+    }
+
+    float x = TEXT_START_X;
+    float y = TEXT_START_Y;
+    DrawText(x, y, "%s %s", renderer_name, colorspace_name);
+    y += TEXT_LINE_ADVANCE;
+    DrawText(x, y, "Test: Clear, Fill and Blend in Render Targets");
+    y += TEXT_LINE_ADVANCE;
+    DrawText(x, y, "Cleared target written: 0x808080, read: 0x%.2x%.2x%.2x", c1.r, c1.g, c1.b);
+    y += TEXT_LINE_ADVANCE;
+    DrawText(x, y, "Filled target written: 0x808080, read: 0x%.2x%.2x%.2x", c2.r, c2.g, c2.b);
+    y += TEXT_LINE_ADVANCE;
+    DrawText(x, y, "Blended target written: 0x808080, read: 0x%.2x%.2x%.2x", c3.r, c3.g, c3.b);
+    y += TEXT_LINE_ADVANCE;
+    if (c1.r != 128 || c2.r != 128) {
+        DrawText(x, y, "Incorrect render target color");
+        y += TEXT_LINE_ADVANCE;
+    }
+    if (ColorMatches(&c3, 128, 128, 128, 1)) {
+        DrawText(x, y, "Correct blend color, blending in sRGB space");
+    } else if (ColorMatches(&c3, 188, 188, 188, 1)) {
+        DrawText(x, y, "Incorrect blend color, blending in linear space");
+    } else {
+        DrawText(x, y, "Incorrect blend color, unknown reason");
+    }
+    y += TEXT_LINE_ADVANCE;
+}
+
 static SDL_Texture *CreateGrayFormatTexture(SDL_PixelFormat format, SDL_Color *expected)
 {
     SDL_Surface *surface;
@@ -947,6 +1027,9 @@ SDL_AppResult SDL_AppIterate(void *appstate)
     case StageTargetBackground:
         RenderTargetBackground();
         break;
+    case StageTargetDrawing:
+        RenderTargetDrawing();
+        break;
     case StageTextureFormats:
         RenderTextureFormats();
         break;