From 52d074d40b638c388afc2756a932f8fbb102dba5 Mon Sep 17 00:00:00 2001 From: LuizDusky Date: Mon, 23 Nov 2026 21:18:16 -0300 Subject: [PATCH] Fix sRGB color conversion in the Metal presenter --- src/graphics/host_gpu/metal/metalPresenter.mm | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/graphics/host_gpu/metal/metalPresenter.mm b/src/graphics/host_gpu/metal/metalPresenter.mm index 663b330..61d8d86 100644 --- a/src/graphics/host_gpu/metal/metalPresenter.mm +++ b/src/graphics/host_gpu/metal/metalPresenter.mm @@ -29,8 +29,16 @@ uint32_t pitch; uint32_t format; uint32_t is_tiled; + uint32_t encode_srgb; }; +static bool TextureReadNeedsSrgbEncoding(MTLPixelFormat format) { + // Metal reads sRGB textures as linear values. The unorm drawable expects + // display-encoded values, so restore the transfer curve just like for linear float targets. + return format == MTLPixelFormatRGBA8Unorm_sRGB || format == MTLPixelFormatBGRA8Unorm_sRGB || + format == MTLPixelFormatRG11B10Float || format == MTLPixelFormatRGBA16Float; +} + struct MetalPresenter::Impl { SDL_Window* window = nullptr; SDL_MetalView metal_view = nullptr; @@ -64,6 +72,7 @@ bool InitPipeline(id device) { " uint pitch;\n" " uint format;\n" " uint is_tiled;\n" + " uint encode_srgb;\n" "};\n" "inline float decode_r11(uint val) {\n" " uint exponent = (val >> 6u) & 0x1Fu;\n" @@ -104,10 +113,7 @@ bool InitPipeline(id device) { " }\n" " float4 c = src.read(uint2(tex_x, tex_y));\n" " float3 rgb = clamp(c.rgb, 0.0, 1.0);\n" - " // Display-referred formats (the game's final composite: 8-bit and RGB10A2 flip images) are already\n" - " // encoded for the display, so they are copied straight; only linear HDR targets (RG11B10F, RGBA16F)\n" - " // get the sRGB transfer function.\n" - " if (u.format == 92u || u.format == 115u) {\n" + " if (u.encode_srgb != 0u) {\n" " rgb = select(rgb * 12.92, 1.055 * pow(rgb, float3(1.0 / 2.4)) - 0.055, rgb > 0.0031308);\n" " }\n" " return float4(rgb, 1.0);\n" @@ -336,6 +342,7 @@ bool InitPipeline(id device) { if (align < 64) align = 64; u.pitch = static_cast((u.width * 4 + align - 1) & ~(align - 1)); u.format = static_cast(src_tex.pixelFormat); + u.encode_srgb = TextureReadNeedsSrgbEncoding(src_tex.pixelFormat); static int s_env_flip_y = -1; if (s_env_flip_y == -1) { const char* env = std::getenv("KYTY_FLIP_Y"); @@ -537,6 +544,7 @@ bool InitPipeline(id device) { u.height = height; u.pitch = static_cast(row_bytes); u.format = static_cast(src.pixelFormat); + u.encode_srgb = TextureReadNeedsSrgbEncoding(src.pixelFormat); u.is_tiled = 0; id cmd = [queue commandBuffer];