From 2f12d47e0e01a8f69424613b9f4e7f37a1c81d11 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 1 Jul 2026 08:02:59 +0530 Subject: [PATCH] Port the blit shader --- kitty/shaders/blit.slang | 32 +++++++++++++++++++++++++++ kitty/shaders/graphics.slang | 5 +---- kitty/shaders/linear2srgb.slang | 38 +++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 kitty/shaders/blit.slang create mode 100644 kitty/shaders/linear2srgb.slang diff --git a/kitty/shaders/blit.slang b/kitty/shaders/blit.slang new file mode 100644 index 000000000..0c51cd9d2 --- /dev/null +++ b/kitty/shaders/blit.slang @@ -0,0 +1,32 @@ +#language slang 2026 +// Copyright (C) 2026 Kovid Goyal +// Distributed under terms of the GPLv3 license. + +import blit_common; +import alpha_blend; +import utils; +import linear2srgb; + +struct VSOutput { + float2 texcoord : TEXCOORD; + float4 position : SV_Position; +}; + + +[shader("vertex")] +VSOutput vertex_main( + uint vertex_id : SV_VertexID, + uniform float4 src_rect, + uniform float4 dest_rect, +) { + BlitOutput ans = get_coords_for_blit(vertex_id, src_rect, dest_rect); + return {ans.texcoord, float4(ans.position[0], ans.position[1], 0.0, 1.0)}; +} + +uniform Sampler2D image; + +[shader("fragment")] +float4 fragment_main(float2 texcoord : TEXCOORD) : SV_Target { + float4 color_premul = image.Sample(texcoord); + return vec4_premul(linear2srgb(color_premul.rgb / color_premul.a), color_premul.a); +} diff --git a/kitty/shaders/graphics.slang b/kitty/shaders/graphics.slang index 6b644f2a4..cc088de02 100644 --- a/kitty/shaders/graphics.slang +++ b/kitty/shaders/graphics.slang @@ -24,10 +24,7 @@ VSOutput vertex_main( uniform float4 dest_rect, ) { BlitOutput ans = get_coords_for_blit(vertex_id, src_rect, dest_rect); - VSOutput output; - output.texcoord = ans.texcoord; - output.position = float4(ans.position[0], ans.position[1], 0.0, 1.0); - return output; + return {ans.texcoord, float4(ans.position[0], ans.position[1], 0.0, 1.0)}; } uniform Sampler2D image; diff --git a/kitty/shaders/linear2srgb.slang b/kitty/shaders/linear2srgb.slang new file mode 100644 index 000000000..b178f3fe6 --- /dev/null +++ b/kitty/shaders/linear2srgb.slang @@ -0,0 +1,38 @@ +#language slang 2026 +// Copyright (C) 2026 Kovid Goyal +// Distributed under terms of the GPLv3 license. + +module linear2srgb; + +// Scalar sRGB to Linear conversion +public float srgb2linear(float x) { + float lower = x / 12.92; + float upper = pow((x + 0.055f) / 1.055f, 2.4f); + + // GLSL mix() is replaced by Slang's lerp() + return lerp(lower, upper, step(0.04045f, x)); +} + +// Scalar Linear to sRGB conversion +public float linear2srgb(float x) { + float lower = 12.92 * x; + float upper = 1.055 * pow(x, 1.0f / 2.4f) - 0.055f; + + return lerp(lower, upper, step(0.0031308f, x)); +} + +// Vector Linear to sRGB conversion +public float3 linear2srgb(float3 x) { + float3 lower = 12.92 * x; + float3 upper = 1.055 * pow(x, float3(1.0f / 2.4f)) - 0.055f; + + return lerp(lower, upper, step(float3(0.0031308f), x)); +} + +// Vector sRGB to Linear conversion +public float3 srgb2linear(float3 c) { + // You can call the scalar version per-component, + // or pass the whole vector if you overload it for float3. + return float3(srgb2linear(c.r), srgb2linear(c.g), srgb2linear(c.b)); +} +