1// Compositor GPU compute shaders (WGSL).
2// Coordinate convention: Fusion uses normalized UV (0,0) = bottom-left, (1,1) = top-right,
3// whereas textures store row 0 at the top. Helpers `pix_to_fu` / `fu_to_uv` bridge cleanly.
4
5@group(0) @binding(0) var src_a: texture_2d<f32>;
6@group(0) @binding(1) var src_b: texture_2d<f32>;
7@group(0) @binding(2) var mask_tex: texture_2d<f32>;
8@group(0) @binding(3) var samp_lin: sampler;
9@group(0) @binding(4) var dst_tex: texture_storage_2d<rgba8unorm, write>;
10
11// Replaced at pipeline build time for Float16/Float32 HDR pipelines (clamped for Int8/Int16).
12const HDR_FLOAT: bool = false;
13
14fn store_dst(p: vec2<i32>, col: vec4<f32>) {
15 if (HDR_FLOAT) {
16 textureStore(dst_tex, p, col);
17 } else {
18 textureStore(dst_tex, p, clamp(col, vec4<f32>(0.0), vec4<f32>(1.0)));
19 }
20}
21
22struct Params {
23 // 16 vec4<f32> = 256 bytes of uniform parameters per dispatch.
24 p: array<vec4<f32>, 16>,
25};
26@group(0) @binding(5) var<uniform> u: Params;
27
28// Helper: output pixel (gid.xy, top-down) -> Fusion UV (0..1, bottom-up).
29fn pix_to_fu(gid: vec2<u32>, dims: vec2<u32>) -> vec2<f32> {
30 let f = vec2<f32>(dims);
31 return vec2<f32>((f32(gid.x) + 0.5) / f.x, 1.0 - (f32(gid.y) + 0.5) / f.y);
32}
33
34// Helper: Fusion UV (bottom-up) -> texture UV (top-down).
35fn fu_to_uv(fu: vec2<f32>) -> vec2<f32> {
36 return vec2<f32>(fu.x, 1.0 - fu.y);
37}
38
39fn sample_tex(tex: texture_2d<f32>, uv: vec2<f32>, nearest: bool) -> vec4<f32> {
40 if (nearest) {
41 let isdims = vec2<i32>(textureDimensions(tex));
42 let sx = clamp(i32(floor(uv.x * f32(isdims.x))), 0, isdims.x - 1);
43 let sy = clamp(i32(floor(uv.y * f32(isdims.y))), 0, isdims.y - 1);
44 return textureLoad(tex, vec2<i32>(sx, sy), 0);
45 }
46 return textureSampleLevel(tex, samp_lin, uv, 0.0);
47}
48
49// Helper: Sample texture with Fusion Edges mode: 0=Canvas, 1=Wrap, 2=Duplicate, 3=Mirror.
50// When `dod` (`origin_x, origin_y, canvas_w, canvas_h`) has `canvas_w > 0.5`, maps `fu`
51// (normalized to `canvas`) into the texture's expanded Domain of Definition!
52fn sample_dod_edges_mode(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32, dod: vec4<f32>, nearest: bool) -> vec4<f32> {
53 var uv: vec2<f32>;
54 if (dod.z > 0.5 && dod.w > 0.5) {
55 let tdims = vec2<f32>(textureDimensions(tex));
56 let canvas_px = vec2<f32>(fu.x * dod.z, (1.0 - fu.y) * dod.w);
57 uv = (canvas_px - dod.xy) / tdims;
58 } else {
59 uv = fu_to_uv(fu);
60 }
61 if (edges == 0) {
62 if (uv.x < 0.0 || uv.x > 1.0 || uv.y < 0.0 || uv.y > 1.0) {
63 return vec4<f32>(0.0);
64 }
65 return sample_tex(tex, uv, nearest);
66 } else if (edges == 1) {
67 uv = fract(uv);
68 return sample_tex(tex, uv, nearest);
69 } else if (edges == 2) {
70 uv = clamp(uv, vec2<f32>(0.0), vec2<f32>(1.0));
71 return sample_tex(tex, uv, nearest);
72 } else {
73 let m = abs(fract(uv * 0.5 + 0.5) * 2.0 - 1.0);
74 return sample_tex(tex, m, nearest);
75 }
76}
77
78fn sample_dod_edges(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32, dod: vec4<f32>) -> vec4<f32> {
79 return sample_dod_edges_mode(tex, fu, edges, dod, false);
80}
81
82fn sample_edges(tex: texture_2d<f32>, fu: vec2<f32>, edges: i32) -> vec4<f32> {
83 return sample_dod_edges(tex, fu, edges, vec4<f32>(0.0));
84}
85
86fn read_mask_uv(uv: vec2<f32>, has_mask: f32, invert: f32) -> f32 {
87 if (has_mask < 0.5) {
88 return 1.0;
89 }
90 var m = 0.0;
91 if (uv.x >= 0.0 && uv.x <= 1.0 && uv.y >= 0.0 && uv.y <= 1.0) {
92 m = textureSampleLevel(mask_tex, samp_lin, uv, 0.0).a;
93 }
94 return select(m, 1.0 - m, invert > 0.5);
95}
96
97fn read_mask(gid: vec2<u32>, dims: vec2<u32>, has_mask: f32, invert: f32) -> f32 {
98 return read_mask_uv((vec2<f32>(gid) + 0.5) / vec2<f32>(dims), has_mask, invert);
99}
100fn fg_hash(p: vec2<i32>, seed: i32) -> f32 {
101 var n = u32(p.x * 374761393 + p.y * 668265263 + seed * 1442695041);
102 n = (n ^ (n >> 13u)) * 1274126177u;
103 n = n ^ (n >> 16u);
104 return f32(n & 0x00ffffffu) / 16777216.0;
105}