From 7e6cd36d2619539ca63416d2673434fe5c4fd0dc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 27 Jul 2026 23:11:43 +0530 Subject: [PATCH] Wire up the backbuffer sampler --- kitty/shaders/custom/pipeline.slang | 56 ++++++++++++++++++----------- kitty/shaders/custom/types.slang | 3 +- kitty/shaders/slang.py | 2 +- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/kitty/shaders/custom/pipeline.slang b/kitty/shaders/custom/pipeline.slang index 0fb0092e8..2582764b7 100644 --- a/kitty/shaders/custom/pipeline.slang +++ b/kitty/shaders/custom/pipeline.slang @@ -4,37 +4,53 @@ // IMPORTS -static const int LEFT = 0; -static const int TOP = 1; -static const int RIGHT = 2; -static const int BOTTOM = 3; -static const uint FF = 0xff; - -static const uint2 pos_map[4] = { - uint2(RIGHT, TOP), - uint2(RIGHT, BOTTOM), - uint2(LEFT, BOTTOM), - uint2(LEFT, TOP) +public struct BlitOutput { + public float2 texcoord; + public float2 position; }; +#define left 0 +#define top 1 +#define right 2 +#define bottom 3 + +// Static constant array mapping vertex IDs +static const int2 vertex_pos_map[4] = { + {right, top}, + {right, bottom}, + {left, bottom}, + {left, top} +}; + +public BlitOutput get_coords_for_blit(uint vertex_id, float4 src_rect, float4 dest_rect) { + int2 pos = vertex_pos_map[vertex_id]; + BlitOutput output; + output.texcoord = float2(src_rect[pos.x], src_rect[pos.y]); + output.position = float2(dest_rect[pos.x], dest_rect[pos.y]); + return output; +} + struct VertexOutput { + float2 texcoord : TEXCOORD; float4 position : SV_Position; - KittyCustomShaderData d; }; [shader("vertex")] -VertexOutput vertex_main(float4 rect, uint vertex_id : SV_VertexID) { - uint2 pos = pos_map[vertex_id]; - VertexOutput vo; - vo.position = float4(rect[pos.x], rect[pos.y], 0, 1); - return vo; +VertexOutput vertex_main(float4 src_rect, float4 dest_rect, uint vertex_id : SV_VertexID) { + 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 backbuffer; + [shader("fragment")] -float4 pipeline_fragment_main(VertexOutput vo) : SV_Target { - vo.d.pos = vo.position; - float4 color = float4(1, 1, 1, 1); // TODO: load from framebuffer +float4 pipeline_fragment_main(float2 texcoord : TEXCOORD) : SV_Target { + KittyCustomShaderData d; + d.backbuffer = backbuffer; + d.texcoord = texcoord; + d.backbuffer = backbuffer; + float4 color = backbuffer.Sample(texcoord); // PIPELINE return color; } diff --git a/kitty/shaders/custom/types.slang b/kitty/shaders/custom/types.slang index 4b7f236a3..c5d0c5a5f 100644 --- a/kitty/shaders/custom/types.slang +++ b/kitty/shaders/custom/types.slang @@ -6,5 +6,6 @@ public struct KittyCustomShaderData { public uint timestamp; - public float4 pos; + public float2 texcoord; + public Sampler2D backbuffer; } diff --git a/kitty/shaders/slang.py b/kitty/shaders/slang.py index 1469e9f09..937a23ede 100644 --- a/kitty/shaders/slang.py +++ b/kitty/shaders/slang.py @@ -968,7 +968,7 @@ def build_custom_shader_pipeline_ir(slot: str = 'after-window-background', shade entry_points.append(entry_point) mod_src = get_custom_shader_src('pipeline').decode() mod_src = mod_src.replace('// IMPORTS', '\n'.join(f'__include "{w}";' for w in wrappers), 1) - mod_src = mod_src.replace('// PIPELINE', '\n'.join(f'color = {w}(color, vo.d);' for w in entry_points), 1) + mod_src = mod_src.replace('// PIPELINE', '\n'.join(f'color = {w}(color, d);' for w in entry_points), 1) # subprocess.run(['bat', '-P', '-l', 'cpp'], input=mod_src.encode()) slot_key = key(slot, mod_src, shaders_content_key)