Port the blit shader

This commit is contained in:
Kovid Goyal
2026-07-01 08:02:59 +05:30
parent 740d2275dc
commit c268e3a2e2
3 changed files with 71 additions and 4 deletions

32
kitty/shaders/blit.slang Normal file
View File

@@ -0,0 +1,32 @@
#language slang 2026
// Copyright (C) 2026 Kovid Goyal <kovid at kovidgoyal.net>
// 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);
}

View File

@@ -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;

View File

@@ -0,0 +1,38 @@
#language slang 2026
// Copyright (C) 2026 Kovid Goyal <kovid at kovidgoyal.net>
// 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));
}