SDL: Finalized the SDL render API colorspaces

From 9591b2cfb4454f7aab7a3b93337a816abb2c84ee Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Fri, 2 Feb 2024 12:09:37 -0800
Subject: [PATCH] Finalized the SDL render API colorspaces

The renderer will always use the sRGB colorspace for drawing, and will default to the sRGB output colorspace. If you want blending in linear space and HDR support, you can select the scRGB output colorspace, which is supported by the direct3d11 and direct3d12
---
 include/SDL3/SDL_pixels.h                |  10 +++
 include/SDL3/SDL_render.h                |  48 +----------
 src/dynapi/SDL_dynapi.sym                |   2 -
 src/dynapi/SDL_dynapi_overrides.h        |   2 -
 src/dynapi/SDL_dynapi_procs.h            |   2 -
 src/render/SDL_render.c                  | 101 +++++------------------
 src/render/SDL_sysrender.h               |   7 +-
 src/render/direct3d11/SDL_render_d3d11.c |  78 ++++++++---------
 src/render/direct3d12/SDL_render_d3d12.c |  63 ++++++++------
 src/render/opengl/SDL_render_gl.c        |  24 ++----
 src/render/software/SDL_render_sw.c      |  17 +++-
 test/testcolorspace.c                    |  89 +++++++++++++-------
 12 files changed, 195 insertions(+), 248 deletions(-)

diff --git a/include/SDL3/SDL_pixels.h b/include/SDL3/SDL_pixels.h
index ecbc129f6df4..f53cd7f644e7 100644
--- a/include/SDL3/SDL_pixels.h
+++ b/include/SDL3/SDL_pixels.h
@@ -558,6 +558,8 @@ typedef enum
 typedef enum
 {
     SDL_COLORSPACE_UNKNOWN,
+
+    /* sRGB is a gamma corrected colorspace, and the default colorspace for SDL rendering and 8-bit RGB surfaces */
     SDL_COLORSPACE_SRGB =   /**< Equivalent to DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709 */
         SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_RGB,
                               SDL_COLOR_RANGE_FULL,
@@ -565,6 +567,8 @@ typedef enum
                               SDL_TRANSFER_CHARACTERISTICS_SRGB,
                               SDL_MATRIX_COEFFICIENTS_UNSPECIFIED,
                               SDL_CHROMA_LOCATION_NONE),
+
+    /* scRGB is a linear colorspace and the default colorspace for floating point surfaces */
     SDL_COLORSPACE_SCRGB =   /**< Equivalent to DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709  */
         SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_RGB,
                               SDL_COLOR_RANGE_FULL,
@@ -572,6 +576,8 @@ typedef enum
                               SDL_TRANSFER_CHARACTERISTICS_LINEAR,
                               SDL_MATRIX_COEFFICIENTS_UNSPECIFIED,
                               SDL_CHROMA_LOCATION_NONE),
+
+    /* HDR10 is a non-linear HDR colorspace and the default colorspace for 10-bit surfaces */
     SDL_COLORSPACE_HDR10 =   /**< Equivalent to DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020  */
         SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_RGB,
                               SDL_COLOR_RANGE_FULL,
@@ -579,6 +585,7 @@ typedef enum
                               SDL_TRANSFER_CHARACTERISTICS_PQ,
                               SDL_MATRIX_COEFFICIENTS_UNSPECIFIED,
                               SDL_CHROMA_LOCATION_NONE),
+
     SDL_COLORSPACE_BT601_LIMITED =  /**< Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601 */
         SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
                               SDL_COLOR_RANGE_LIMITED,
@@ -586,6 +593,7 @@ typedef enum
                               SDL_TRANSFER_CHARACTERISTICS_BT601,
                               SDL_MATRIX_COEFFICIENTS_BT601,
                               SDL_CHROMA_LOCATION_LEFT),
+
     SDL_COLORSPACE_BT601_FULL =     /**< Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P601 */
         SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
                               SDL_COLOR_RANGE_FULL,
@@ -593,6 +601,7 @@ typedef enum
                               SDL_TRANSFER_CHARACTERISTICS_BT601,
                               SDL_MATRIX_COEFFICIENTS_BT601,
                               SDL_CHROMA_LOCATION_LEFT),
+
     SDL_COLORSPACE_BT709_LIMITED =  /**< Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709 */
         SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
                               SDL_COLOR_RANGE_LIMITED,
@@ -600,6 +609,7 @@ typedef enum
                               SDL_TRANSFER_CHARACTERISTICS_BT709,
                               SDL_MATRIX_COEFFICIENTS_BT709,
                               SDL_CHROMA_LOCATION_LEFT),
+
     SDL_COLORSPACE_BT709_FULL =     /**< Equivalent to DXGI_COLOR_SPACE_YCBCR_STUDIO_G22_LEFT_P709 */
         SDL_DEFINE_COLORSPACE(SDL_COLOR_TYPE_YCBCR,
                               SDL_COLOR_RANGE_FULL,
diff --git a/include/SDL3/SDL_render.h b/include/SDL3/SDL_render.h
index 5ad57210cb58..4b04982801f7 100644
--- a/include/SDL3/SDL_render.h
+++ b/include/SDL3/SDL_render.h
@@ -240,15 +240,9 @@ extern DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window *window, co
  *   is displayed, if you want a software renderer without a window
  * - `SDL_PROP_RENDERER_CREATE_NAME_STRING`: the name of the rendering driver
  *   to use, if a specific one is desired
- * - `SDL_PROP_RENDERER_CREATE_INPUT_COLORSPACE_NUMBER`: an SDL_ColorSpace
- *   value describing the colorspace for input colors, defaults to
- *   SDL_COLORSPACE_SRGB
  * - `SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER`: an SDL_ColorSpace
  *   value describing the colorspace for output to the display, defaults to
- *   SDL_COLORSPACE_SRGB
- * - `SDL_PROP_RENDERER_CREATE_COLORSPACE_CONVERSION_BOOLEAN`: true if you
- *   want conversion between the input colorspace and the output colorspace,
- *   defaults to SDL_TRUE
+ *   SDL_COLORSPACE_SRGB. The direct3d11 and direct3d12 renderers support SDL_COLORSPACE_SCRGB, which is a linear color space and supports HDR output.
  * - `SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_BOOLEAN`: true if you want
  *   present synchronized with the refresh rate
  *
@@ -273,9 +267,7 @@ extern DECLSPEC SDL_Renderer * SDLCALL SDL_CreateRendererWithProperties(SDL_Prop
 #define SDL_PROP_RENDERER_CREATE_WINDOW_POINTER                 "window"
 #define SDL_PROP_RENDERER_CREATE_SURFACE_POINTER                "surface"
 #define SDL_PROP_RENDERER_CREATE_NAME_STRING                    "name"
-#define SDL_PROP_RENDERER_CREATE_INPUT_COLORSPACE_NUMBER        "input_colorspace"
 #define SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER       "output_colorspace"
-#define SDL_PROP_RENDERER_CREATE_COLORSPACE_CONVERSION_BOOLEAN  "colorspace_conversion"
 #define SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_BOOLEAN          "present_vsync"
 
 /**
@@ -1360,44 +1352,6 @@ extern DECLSPEC int SDLCALL SDL_SetRenderScale(SDL_Renderer *renderer, float sca
  */
 extern DECLSPEC int SDLCALL SDL_GetRenderScale(SDL_Renderer *renderer, float *scaleX, float *scaleY);
 
-/**
- * Set the colorspace used for drawing operations
- *
- * The default colorspace for drawing operations is SDL_COLORSPACE_SRGB, but
- * you can change it to other colorspaces such as SDL_COLORSPACE_SCRGB for HDR
- * rendering.
- *
- * This does not affect the colorspace of textures, which is specified via
- * properties when the texture is created and does not change.
- *
- * \param renderer the rendering context
- * \param colorspace an SDL_ColorSpace value describing the colorspace for
- *                   drawing operations
- * \returns 0 on success or a negative error code on failure; call
- *          SDL_GetError() for more information.
- *
- * \since This function is available since SDL 3.0.0.
- *
- * \sa SDL_GetRenderDrawColorspace
- */
-extern DECLSPEC int SDLCALL SDL_SetRenderDrawColorspace(SDL_Renderer *renderer, SDL_Colorspace colorspace);
-
-/**
- * Get the colorspace used for drawing operations
- *
- * \param renderer the rendering context
- * \param colorspace a pointer filled in with an SDL_ColorSpace value
- *                   describing the colorspace for drawing operations
- * \returns 0 on success or a negative error code on failure; call
- *          SDL_GetError() for more information.
- *
- * \since This function is available since SDL 3.0.0.
- *
- * \sa SDL_SetRenderDrawColorspace
- */
-extern DECLSPEC int SDLCALL SDL_GetRenderDrawColorspace(SDL_Renderer *renderer, SDL_Colorspace *colorspace);
-
-
 /**
  * Set the color used for drawing operations.
  *
diff --git a/src/dynapi/SDL_dynapi.sym b/src/dynapi/SDL_dynapi.sym
index c9d6cceebd0e..1dc5a5405ef2 100644
--- a/src/dynapi/SDL_dynapi.sym
+++ b/src/dynapi/SDL_dynapi.sym
@@ -969,8 +969,6 @@ SDL3_0.0.0 {
     SDL_SetSurfaceColorspace;
     SDL_GetSurfaceColorspace;
     SDL_ConvertSurfaceFormatAndColorspace;
-    SDL_SetRenderDrawColorspace;
-    SDL_GetRenderDrawColorspace;
     # extra symbols go here (don't modify this line)
   local: *;
 };
diff --git a/src/dynapi/SDL_dynapi_overrides.h b/src/dynapi/SDL_dynapi_overrides.h
index 51530f6701aa..42a2e4846f92 100644
--- a/src/dynapi/SDL_dynapi_overrides.h
+++ b/src/dynapi/SDL_dynapi_overrides.h
@@ -994,5 +994,3 @@
 #define SDL_SetSurfaceColorspace SDL_SetSurfaceColorspace_REAL
 #define SDL_GetSurfaceColorspace SDL_GetSurfaceColorspace_REAL
 #define SDL_ConvertSurfaceFormatAndColorspace SDL_ConvertSurfaceFormatAndColorspace_REAL
-#define SDL_SetRenderDrawColorspace SDL_SetRenderDrawColorspace_REAL
-#define SDL_GetRenderDrawColorspace SDL_GetRenderDrawColorspace_REAL
diff --git a/src/dynapi/SDL_dynapi_procs.h b/src/dynapi/SDL_dynapi_procs.h
index 8eb2f14d9890..b6221bb51d90 100644
--- a/src/dynapi/SDL_dynapi_procs.h
+++ b/src/dynapi/SDL_dynapi_procs.h
@@ -1019,5 +1019,3 @@ SDL_DYNAPI_PROC(int,SDL_ConvertPixelsAndColorspace,(int a, int b, Uint32 c, SDL_
 SDL_DYNAPI_PROC(int,SDL_SetSurfaceColorspace,(SDL_Surface *a, SDL_Colorspace b),(a,b),return)
 SDL_DYNAPI_PROC(int,SDL_GetSurfaceColorspace,(SDL_Surface *a, SDL_Colorspace *b),(a,b),return)
 SDL_DYNAPI_PROC(SDL_Surface*,SDL_ConvertSurfaceFormatAndColorspace,(SDL_Surface *a, Uint32 b, SDL_Colorspace c),(a,b,c),return)
-SDL_DYNAPI_PROC(int,SDL_SetRenderDrawColorspace,(SDL_Renderer *a, SDL_Colorspace b),(a,b),return)
-SDL_DYNAPI_PROC(int,SDL_GetRenderDrawColorspace,(SDL_Renderer *a, SDL_Colorspace *b),(a,b),return)
diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c
index 2fc35c15aa6c..491e102bdc41 100644
--- a/src/render/SDL_render.c
+++ b/src/render/SDL_render.c
@@ -128,9 +128,7 @@ char SDL_texture_magic;
 
 void SDL_SetupRendererColorspace(SDL_Renderer *renderer, SDL_PropertiesID props)
 {
-    renderer->input_colorspace = (SDL_Colorspace)SDL_GetNumberProperty(props, SDL_PROP_RENDERER_CREATE_INPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB);
     renderer->output_colorspace = (SDL_Colorspace)SDL_GetNumberProperty(props, SDL_PROP_RENDERER_CREATE_OUTPUT_COLORSPACE_NUMBER, SDL_COLORSPACE_SRGB);
-    renderer->colorspace_conversion = SDL_GetBooleanProperty(props, SDL_PROP_RENDERER_CREATE_COLORSPACE_CONVERSION_BOOLEAN, SDL_TRUE);
 }
 
 static float sRGBtoLinear(float v)
@@ -143,46 +141,33 @@ static float sRGBfromLinear(float v)
     return v <= 0.0031308f ? (v * 12.92f) : (SDL_powf(v, 1.0f / 2.4f) * 1.055f - 0.055f);
 }
 
-void SDL_ConvertToLinear(SDL_Renderer *renderer, SDL_FColor *color)
+SDL_bool SDL_RenderingLinearSpace(SDL_Renderer *renderer)
 {
-    if (!renderer->colorspace_conversion) {
-        return;
-    }
+    SDL_Colorspace colorspace;
 
-    switch (SDL_COLORSPACETRANSFER(renderer->input_colorspace)) {
-    case SDL_TRANSFER_CHARACTERISTICS_SRGB:
-        color->r = sRGBtoLinear(color->r);
-        color->g = sRGBtoLinear(color->g);
-        color->b = sRGBtoLinear(color->b);
-        break;
-    case SDL_TRANSFER_CHARACTERISTICS_LINEAR:
-        /* No conversion needed */
-        break;
-    default:
-        /* Unsupported */
-        break;
+    if (renderer->target) {
+        colorspace = renderer->target->colorspace;
+    } else {
+        colorspace = renderer->output_colorspace;
     }
+    if (colorspace == SDL_COLORSPACE_SCRGB) {
+        return SDL_TRUE;
+    }
+    return SDL_FALSE;
 }
 
-void SDL_ConvertFromLinear(SDL_Renderer *renderer, SDL_FColor *color)
+void SDL_ConvertToLinear(SDL_FColor *color)
 {
-    if (!renderer->colorspace_conversion) {
-        return;
-    }
+    color->r = sRGBtoLinear(color->r);
+    color->g = sRGBtoLinear(color->g);
+    color->b = sRGBtoLinear(color->b);
+}
 
-    switch (SDL_COLORSPACETRANSFER(renderer->input_colorspace)) {
-    case SDL_TRANSFER_CHARACTERISTICS_SRGB:
-        color->r = sRGBfromLinear(color->r);
-        color->g = sRGBfromLinear(color->g);
-        color->b = sRGBfromLinear(color->b);
-        break;
-    case SDL_TRANSFER_CHARACTERISTICS_LINEAR:
-        /* No conversion needed */
-        break;
-    default:
-        /* Unsupported */
-        break;
-    }
+void SDL_ConvertFromLinear(SDL_FColor *color)
+{
+    color->r = sRGBfromLinear(color->r);
+    color->g = sRGBfromLinear(color->g);
+    color->b = sRGBfromLinear(color->b);
 }
 
 static SDL_INLINE void DebugLogRenderCommands(const SDL_RenderCommand *cmd)
@@ -1549,7 +1534,6 @@ int SDL_SetTextureColorModFloat(SDL_Texture *texture, float r, float g, float b)
     texture->color.r = r;
     texture->color.g = g;
     texture->color.b = b;
-    SDL_ConvertToLinear(texture->renderer, &texture->color);
     if (texture->native) {
         return SDL_SetTextureColorModFloat(texture->native, r, g, b);
     }
@@ -1583,7 +1567,6 @@ int SDL_GetTextureColorModFloat(SDL_Texture *texture, float *r, float *g, float
     CHECK_TEXTURE_MAGIC(texture, -1);
 
     color = texture->color;
-    SDL_ConvertFromLinear(texture->renderer, &color);
 
     if (r) {
         *r = color.r;
@@ -2785,30 +2768,6 @@ int SDL_GetRenderScale(SDL_Renderer *renderer, float *scaleX, float *scaleY)
     return 0;
 }
 
-int SDL_SetRenderDrawColorspace(SDL_Renderer *renderer, SDL_Colorspace colorspace)
-{
-    CHECK_RENDERER_MAGIC(renderer, -1);
-
-    if (colorspace != SDL_COLORSPACE_SRGB &&
-        colorspace != SDL_COLORSPACE_SCRGB) {
-        return SDL_SetError("Unsupported colorspace");
-    }
-
-    renderer->input_colorspace = colorspace;
-    return 0;
-}
-
-int SDL_GetRenderDrawColorspace(SDL_Renderer *renderer, SDL_Colorspace *colorspace)
-{
-    CHECK_RENDERER_MAGIC(renderer, -1);
-
-    if (colorspace) {
-        *colorspace = renderer->input_colorspace;
-    }
-    return 0;
-}
-
-
 int SDL_SetRenderDrawColor(SDL_Renderer *renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
 {
     const float fR = (float)r / 255.0f;
@@ -2827,7 +2786,6 @@ int SDL_SetRenderDrawColorFloat(SDL_Renderer *renderer, float r, float g, float
     renderer->color.g = g;
     renderer->color.b = b;
     renderer->color.a = a;
-    SDL_ConvertToLinear(renderer, &renderer->color);
     return 0;
 }
 
@@ -2861,7 +2819,6 @@ int SDL_GetRenderDrawColorFloat(SDL_Renderer *renderer, float *r, float *g, floa
     CHECK_RENDERER_MAGIC(renderer, -1);
 
     color = renderer->color;
-    SDL_ConvertFromLinear(renderer, &color);
 
     if (r) {
         *r = color.r;
@@ -4163,24 +4120,6 @@ int SDL_RenderGeometryRaw(SDL_Renderer *renderer,
                                         indices, num_indices, size_indices);
     }
 
-    /* Transform the colors if necessary */
-    if (renderer->colorspace_conversion &&
-        SDL_COLORSPACETRANSFER(renderer->input_colorspace) == SDL_TRANSFER_CHARACTERISTICS_SRGB) {
-        int num_colors = (color_stride > 0) ? num_vertices : 1;
-        updated_colors = SDL_small_alloc(SDL_FColor, num_colors, &isstack);
-        if (!updated_colors) {
-            return -1;
-        }
-        for (i = 0; i < num_colors; ++i) {
-            updated_colors[i] = *(const SDL_FColor *)(((const Uint8 *)color) + i * color_stride);
-            SDL_ConvertToLinear(renderer, &updated_colors[i]);
-        }
-        color = updated_colors;
-        if (color_stride > 0) {
-            color_stride = sizeof(SDL_FColor);
-        }
-    }
-
     retval = QueueCmdGeometry(renderer, texture,
                               xy, xy_stride, color, color_stride, uv, uv_stride,
                               num_vertices,
diff --git a/src/render/SDL_sysrender.h b/src/render/SDL_sysrender.h
index 58536876f3f6..0894fd3db406 100644
--- a/src/render/SDL_sysrender.h
+++ b/src/render/SDL_sysrender.h
@@ -250,9 +250,7 @@ struct SDL_Renderer
     SDL_Texture *target;
     SDL_Mutex *target_mutex;
 
-    SDL_Colorspace input_colorspace;
     SDL_Colorspace output_colorspace;
-    SDL_bool colorspace_conversion;
 
     SDL_FColor color;        /**< Color for drawing operations values */
     SDL_BlendMode blendMode; /**< The drawing blend mode */
@@ -303,8 +301,9 @@ extern SDL_RenderDriver VITA_GXM_RenderDriver;
 extern void SDL_SetupRendererColorspace(SDL_Renderer *renderer, SDL_PropertiesID props);
 
 /* Colorspace conversion functions */
-extern void SDL_ConvertToLinear(SDL_Renderer *renderer, SDL_FColor *color);
-extern void SDL_ConvertFromLinear(SDL_Renderer *renderer, SDL_FColor *color);
+extern SDL_bool SDL_RenderingLinearSpace(SDL_Renderer *renderer);
+extern void SDL_ConvertToLinear(SDL_FColor *color);
+extern void SDL_ConvertFromLinear(SDL_FColor *color);
 
 /* Blend mode functions */
 extern SDL_BlendFactor SDL_GetBlendModeSrcColorFactor(SDL_BlendMode blendMode);
diff --git a/src/render/direct3d11/SDL_render_d3d11.c b/src/render/direct3d11/SDL_render_d3d11.c
index c5f103835e29..3c61a192e3cf 100644
--- a/src/render/direct3d11/SDL_render_d3d11.c
+++ b/src/render/direct3d11/SDL_render_d3d11.c
@@ -214,18 +214,18 @@ Uint32 D3D11_DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat)
     }
 }
 
-static DXGI_FORMAT SDLPixelFormatToDXGITextureFormat(Uint32 format, Uint32 colorspace, SDL_bool colorspace_conversion)
+static DXGI_FORMAT SDLPixelFormatToDXGITextureFormat(Uint32 format, Uint32 colorspace)
 {
     switch (format) {
     case SDL_PIXELFORMAT_RGBA64_FLOAT:
         return DXGI_FORMAT_R16G16B16A16_FLOAT;
     case SDL_PIXELFORMAT_ARGB8888:
-        if (colorspace_conversion && colorspace == SDL_COLORSPACE_SRGB) {
+        if (colorspace == SDL_COLORSPACE_SCRGB) {
             return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
         }
         return DXGI_FORMAT_B8G8R8A8_UNORM;
     case SDL_PIXELFORMAT_XRGB8888:
-        if (colorspace_conversion && colorspace == SDL_COLORSPACE_SRGB) {
+        if (colorspace == SDL_COLORSPACE_SCRGB) {
             return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
         }
         return DXGI_FORMAT_B8G8R8X8_UNORM;
@@ -240,18 +240,18 @@ static DXGI_FORMAT SDLPixelFormatToDXGITextureFormat(Uint32 format, Uint32 color
     }
 }
 
-static DXGI_FORMAT SDLPixelFormatToDXGIMainResourceViewFormat(Uint32 format, Uint32 colorspace, SDL_bool colorspace_conversion)
+static DXGI_FORMAT SDLPixelFormatToDXGIMainResourceViewFormat(Uint32 format, Uint32 colorspace)
 {
     switch (format) {
     case SDL_PIXELFORMAT_RGBA64_FLOAT:
         return DXGI_FORMAT_R16G16B16A16_FLOAT;
     case SDL_PIXELFORMAT_ARGB8888:
-        if (colorspace_conversion && colorspace == SDL_COLORSPACE_SRGB) {
+        if (colorspace == SDL_COLORSPACE_SCRGB) {
             return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
         }
         return DXGI_FORMAT_B8G8R8A8_UNORM;
     case SDL_PIXELFORMAT_XRGB8888:
-        if (colorspace_conversion && colorspace == SDL_COLORSPACE_SRGB) {
+        if (colorspace == SDL_COLORSPACE_SCRGB) {
             return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
         }
         return DXGI_FORMAT_B8G8R8X8_UNORM;
@@ -523,6 +523,7 @@ static HRESULT D3D11_CreateDeviceResources(SDL_Renderer *renderer)
     if (SDL_GetHintBoolean(SDL_HINT_RENDER_DIRECT3D11_DEBUG, SDL_FALSE)) {
         creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
     }
+        creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
 
     /* Create a single-threaded device unless the app requests otherwise. */
     if (!SDL_GetHintBoolean(SDL_HINT_RENDER_DIRECT3D_THREADSAFE, SDL_FALSE)) {
@@ -893,23 +894,31 @@ static HRESULT D3D11_CreateSwapChain(SDL_Renderer *renderer, int w, int h)
 
     IDXGISwapChain3 *swapChain3 = NULL;
     if (SUCCEEDED(IDXGISwapChain1_QueryInterface(data->swapChain, &SDL_IID_IDXGISwapChain2, (void **)&swapChain3))) {
-        DXGI_COLOR_SPACE_TYPE ColorSpace;
+        UINT colorspace_support = 0;
+        DXGI_COLOR_SPACE_TYPE colorspace;
         switch (renderer->output_colorspace) {
         case SDL_COLORSPACE_SCRGB:
-            ColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;
+            colorspace = DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;
             break;
         case SDL_COLORSPACE_HDR10:
-            ColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020;
+            colorspace = DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020;
             break;
         default:
             /* sRGB */
-            ColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;
+            colorspace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;
             break;
         }
-        result = IDXGISwapChain3_SetColorSpace1(swapChain3, ColorSpace);
-        if (FAILED(result)) {
-            WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain3::SetColorSpace1"), result);
-            goto done;
+        if (SUCCEEDED(IDXGISwapChain3_CheckColorSpaceSupport(swapChain3, colorspace, &colorspace_support)) &&
+            (colorspace_support & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT)) {
+            result = IDXGISwapChain3_SetColorSpace1(swapChain3, colorspace);
+            if (FAILED(result)) {
+                WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain3::SetColorSpace1"), result);
+                goto done;
+            }
+        } else if (colorspace != DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709) {
+            /* Not the default, we're not going to be able to present in this colorspace */
+            SDL_SetError("Unsupported output colorspace");
+            result = DXGI_ERROR_UNSUPPORTED;
         }
     }
 
@@ -1050,27 +1059,9 @@ static HRESULT D3D11_CreateWindowSizeDependentResources(SDL_Renderer *renderer)
     }
 
     /* Create a render target view of the swap chain back buffer. */
-    D3D11_RENDER_TARGET_VIEW_DESC desc;
-    SDL_zero(desc);
-    switch (renderer->output_colorspace) {
-    case SDL_COLORSPACE_SCRGB:
-        desc.Format = DXGI_FORMAT_R16G16B16A16_FLOAT;
-        break;
-    case SDL_COLORSPACE_HDR10:
-        desc.Format = DXGI_FORMAT_R10G10B10A2_UNORM;
-        break;
-    default:
-        if (renderer->colorspace_conversion && renderer->output_colorspace == SDL_COLORSPACE_SRGB) {
-            desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
-        } else {
-            desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
-        }
-        break;
-    }
-    desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
     result = ID3D11Device_CreateRenderTargetView(data->d3dDevice,
                                                  (ID3D11Resource *)backBuffer,
-                                                 &desc,
+                                                 NULL,
                                                  &data->mainRenderTargetView);
     if (FAILED(result)) {
         WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("ID3D11Device::CreateRenderTargetView"), result);
@@ -1163,7 +1154,7 @@ static int D3D11_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL
     D3D11_RenderData *rendererData = (D3D11_RenderData *)renderer->driverdata;
     D3D11_TextureData *textureData;
     HRESULT result;
-    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGITextureFormat(texture->format, texture->colorspace, renderer->colorspace_conversion);
+    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGITextureFormat(texture->format, renderer->output_colorspace);
     D3D11_TEXTURE2D_DESC textureDesc;
     D3D11_SHADER_RESOURCE_VIEW_DESC resourceViewDesc;
 
@@ -1262,7 +1253,7 @@ static int D3D11_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL
     }
 #endif /* SDL_HAVE_YUV */
     SDL_zero(resourceViewDesc);
-    resourceViewDesc.Format = SDLPixelFormatToDXGIMainResourceViewFormat(texture->format, texture->colorspace, renderer->colorspace_conversion);
+    resourceViewDesc.Format = SDLPixelFormatToDXGIMainResourceViewFormat(texture->format, renderer->output_colorspace);
     resourceViewDesc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
     resourceViewDesc.Texture2D.MostDetailedMip = 0;
     resourceViewDesc.Texture2D.MipLevels = textureDesc.MipLevels;
@@ -1774,6 +1765,7 @@ static int D3D11_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd,
 {
     VertexPositionColor *verts = (VertexPositionColor *)SDL_AllocateRenderVertices(renderer, count * sizeof(VertexPositionColor), 0, &cmd->data.draw.first);
     int i;
+    SDL_bool convert_color = SDL_RenderingLinearSpace(renderer);
 
     if (!verts) {
         return -1;
@@ -1787,6 +1779,9 @@ static int D3D11_QueueDrawPoints(SDL_Renderer *renderer, SDL_RenderCommand *cmd,
         verts->tex.x = 0.0f;
         verts->tex.y = 0.0f;
         verts->color = cmd->data.draw.color;
+        if (convert_color) {
+            SDL_ConvertToLinear(&verts->color);
+        }
         verts++;
     }
 
@@ -1801,6 +1796,7 @@ static int D3D11_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, S
     int i;
     int count = indices ? num_indices : num_vertices;
     VertexPositionColor *verts = (VertexPositionColor *)SDL_AllocateRenderVertices(renderer, count * sizeof(VertexPositionColor), 0, &cmd->data.draw.first);
+    SDL_bool convert_color = SDL_RenderingLinearSpace(renderer);
 
     if (!verts) {
         return -1;
@@ -1827,6 +1823,9 @@ static int D3D11_QueueGeometry(SDL_Renderer *renderer, SDL_RenderCommand *cmd, S
         verts->pos.x = xy_[0] * scale_x;
         verts->pos.y = xy_[1] * scale_y;
         verts->color = *(SDL_FColor *)((char *)color + j * color_stride);
+        if (convert_color) {
+            SDL_ConvertToLinear(&verts->color);
+        }
 
         if (texture) {
             float *uv_ = (float *)((char *)uv + j * uv_stride);
@@ -2270,7 +2269,12 @@ static int D3D11_RunCommandQueue(SDL_Renderer *renderer, SDL_RenderCommand *cmd,
 
         case SDL_RENDERCMD_CLEAR:
         {
-            ID3D11DeviceContext_ClearRenderTargetView(rendererData->d3dContext, D3D11_GetCurrentRenderTargetView(renderer), &cmd->data.color.color.r);
+            SDL_bool convert_color = SDL_RenderingLinearSpace(renderer);
+            SDL_FColor color = cmd->data.color.color;
+            if (convert_color) {
+                SDL_ConvertToLinear(&color);
+            }
+            ID3D11DeviceContext_ClearRenderTargetView(rendererData->d3dContext, D3D11_GetCurrentRenderTargetView(renderer), &color.r);
             break;
         }
 
@@ -2419,7 +2423,7 @@ static int D3D11_RenderReadPixels(SDL_Renderer *renderer, const SDL_Rect *rect,
         textureMemory.pData,
         textureMemory.RowPitch,
         format,
-        renderer->input_colorspace,
+        SDL_COLORSPACE_SRGB,
         pixels,
         pitch);
 
diff --git a/src/render/direct3d12/SDL_render_d3d12.c b/src/render/direct3d12/SDL_render_d3d12.c
index bfd5da1abf90..0d6c40590166 100644
--- a/src/render/direct3d12/SDL_render_d3d12.c
+++ b/src/render/direct3d12/SDL_render_d3d12.c
@@ -288,18 +288,18 @@ Uint32 D3D12_DXGIFormatToSDLPixelFormat(DXGI_FORMAT dxgiFormat)
     }
 }
 
-static DXGI_FORMAT SDLPixelFormatToDXGITextureFormat(Uint32 format, Uint32 colorspace, SDL_bool colorspace_conversion)
+static DXGI_FORMAT SDLPixelFormatToDXGITextureFormat(Uint32 format, Uint32 colorspace)
 {
     switch (format) {
     case SDL_PIXELFORMAT_RGBA64_FLOAT:
         return DXGI_FORMAT_R16G16B16A16_FLOAT;
     case SDL_PIXELFORMAT_ARGB8888:
-        if (colorspace_conversion && colorspace == SDL_COLORSPACE_SRGB) {
+        if (colorspace == SDL_COLORSPACE_SCRGB) {
             return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
         }
         return DXGI_FORMAT_B8G8R8A8_UNORM;
     case SDL_PIXELFORMAT_XRGB8888:
-        if (colorspace_conversion && colorspace == SDL_COLORSPACE_SRGB) {
+        if (colorspace == SDL_COLORSPACE_SCRGB) {
             return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
         }
         return DXGI_FORMAT_B8G8R8X8_UNORM;
@@ -314,18 +314,18 @@ static DXGI_FORMAT SDLPixelFormatToDXGITextureFormat(Uint32 format, Uint32 color
     }
 }
 
-static DXGI_FORMAT SDLPixelFormatToDXGIMainResourceViewFormat(Uint32 format, Uint32 colorspace, SDL_bool colorspace_conversion)
+static DXGI_FORMAT SDLPixelFormatToDXGIMainResourceViewFormat(Uint32 format, Uint32 colorspace)
 {
     switch (format) {
     case SDL_PIXELFORMAT_RGBA64_FLOAT:
         return DXGI_FORMAT_R16G16B16A16_FLOAT;
     case SDL_PIXELFORMAT_ARGB8888:
-        if (colorspace_conversion && colorspace == SDL_COLORSPACE_SRGB) {
+        if (colorspace == SDL_COLORSPACE_SCRGB) {
             return DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
         }
         return DXGI_FORMAT_B8G8R8A8_UNORM;
     case SDL_PIXELFORMAT_XRGB8888:
-        if (colorspace_conversion && colorspace == SDL_COLORSPACE_SRGB) {
+        if (colorspace == SDL_COLORSPACE_SCRGB) {
             return DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
         }
         return DXGI_FORMAT_B8G8R8X8_UNORM;
@@ -1200,11 +1200,7 @@ static HRESULT D3D12_CreateSwapChain(SDL_Renderer *renderer, int w, int h)
         break;
     default:
         swapChainDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM; /* This is the most common swap chain format. */
-        if (renderer->colorspace_conversion && renderer->output_colorspace == SDL_COLORSPACE_SRGB) {
-            data->renderTargetFormat = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
-        } else {
-            data->renderTargetFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
-        }
+        data->renderTargetFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
         break;
     }
     swapChainDesc.Stereo = FALSE;
@@ -1255,23 +1251,31 @@ static HRESULT D3D12_CreateSwapChain(SDL_Renderer *renderer, int w, int h)
     data->swapEffect = swapChainDesc.SwapEffect;
     data->swapFlags = swapChainDesc.Flags;
 
-    DXGI_COLOR_SPACE_TYPE ColorSpace;
+    UINT colorspace_support = 0;
+    DXGI_COLOR_SPACE_TYPE colorspace;
     switch (renderer->output_colorspace) {
     case SDL_COLORSPACE_SCRGB:
-        ColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;
+        colorspace = DXGI_COLOR_SPACE_RGB_FULL_G10_NONE_P709;
         break;
     case SDL_COLORSPACE_HDR10:
-        ColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020;
+        colorspace = DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020;
         break;
     default:
         /* sRGB */
-        ColorSpace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;
+        colorspace = DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709;
         break;
     }
-    result = D3D_CALL(data->swapChain, SetColorSpace1, ColorSpace);
-    if (FAILED(result)) {
-        WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain3::SetColorSpace1"), result);
-        goto done;
+    if (SUCCEEDED(D3D_CALL(data->swapChain, CheckColorSpaceSupport, colorspace, &colorspace_support)) &&
+        (colorspace_support & DXGI_SWAP_CHAIN_COLOR_SPACE_SUPPORT_FLAG_PRESENT)) {
+        result = D3D_CALL(data->swapChain, SetColorSpace1, colorspace);
+        if (FAILED(result)) {
+            WIN_SetErrorFromHRESULT(SDL_COMPOSE_ERROR("IDXGISwapChain3::SetColorSpace1"), result);
+            goto done;
+        }
+    } else {
+        /* Not the default, we're not going to be able to present in this colorspace */
+        SDL_SetError("Unsupported output colorspace");
+        result = DXGI_ERROR_UNSUPPORTED;
     }
 
 done:
@@ -1511,7 +1515,7 @@ static int D3D12_CreateTexture(SDL_Renderer *renderer, SDL_Texture *texture, SDL
     D3D12_RenderData *rendererData = (D3D12_RenderData *)renderer->driverdata;
     D3D12_TextureData *textureData;
     HRESULT result;
-    DXGI_FORMAT textureFormat = SDLPixelFormatToDXGITextureFormat(texture->format,  texture

(Patch may be truncated, please check the link at the top of this post.)