diff options
| author | edwin042331-hue <edwin042331@gmail.com> | 2026-11-22 16:03:25 -0400 |
|---|---|---|
| committer | edwin042331-hue <edwin042331@gmail.com> | 2026-11-22 16:04:21 -0400 |
| commit | aa780e3c7f43668c2d2e9be58234c794703f705e (patch) | |
| tree | 026a32e2877b728a6b7593836f99b39f230c66da /runtime/src/present_compose.comp | |
| parent | 355e67cbc45b163f96ee7b0091174ebde5c64e45 (diff) | |
| parent | f847748cc76bd3777f9c070c4034eb2fc1c04e64 (diff) | |
Merge branch 'main' into perf/wave64-emulate-dead-scc-exec-fold
Diffstat (limited to 'runtime/src/present_compose.comp')
| -rw-r--r-- | runtime/src/present_compose.comp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/runtime/src/present_compose.comp b/runtime/src/present_compose.comp new file mode 100644 index 000000000..b557a6444 --- /dev/null +++ b/runtime/src/present_compose.comp | |||
| @@ -0,0 +1,130 @@ | |||
| 1 | // PresentX plane composition (docs/d3d12_vulkan.md section 4.5): one or two planes, each scaled from a source rectangle to | ||
| 2 | // a destination rectangle of the output, plane 1 blended over plane 0, written to an RGBA16F image the presenter then | ||
| 3 | // copies to the swapchain. present_compose_spv.h is this file compiled with | ||
| 4 | // `glslc --target-env=vulkan1.1 -O -mfmt=c` (see the header for the command); keep them together. | ||
| 5 | // | ||
| 6 | // Every plane is read with texelFetch (no sampler: the filtering is done here, so it works on any sampled format and | ||
| 7 | // only reads texels inside the source rectangle). Values are the encoded ones of the plane's colour space: SDR planes | ||
| 8 | // hold G2.2 values, HDR10 planes hold ST.2084 (PQ) Rec.2020 values. An sRGB-format source is decoded by the texture unit, | ||
| 9 | // so `flag_srgb_source` encodes it again with the sRGB curve and the bytes come out as they went in. | ||
| 10 | // | ||
| 11 | // Conversions (all provisional, docs section 4.5): SDR -> HDR10: pow 2.2, Rec.709 -> Rec.2020, times the SDR white level | ||
| 12 | // in nits, PQ. HDR10 -> SDR: PQ, Rec.2020 -> Rec.709, divided by the white level, clipped to 0..1, pow 1/2.2 (no | ||
| 13 | // tone mapping). Same space: untouched. Plane 1 is blended after conversion, in the output's encoded space. | ||
| 14 | #version 450 | ||
| 15 | #extension GL_EXT_samplerless_texture_functions : require | ||
| 16 | |||
| 17 | layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in; | ||
| 18 | |||
| 19 | layout(set = 0, binding = 0) uniform texture2D plane0; | ||
| 20 | layout(set = 0, binding = 1) uniform texture2D plane1; | ||
| 21 | layout(set = 0, binding = 2, rgba16f) uniform writeonly image2D out_image; | ||
| 22 | |||
| 23 | const uint flag_point = 1u; | ||
| 24 | const uint flag_srgb_source = 2u; | ||
| 25 | const uint flag_hdr10 = 4u; | ||
| 26 | const uint flag_straight_alpha = 8u; | ||
| 27 | |||
| 28 | layout(push_constant) uniform Push { | ||
| 29 | vec4 dest0; // destination rectangle in output pixels: left, top, right, bottom | ||
| 30 | vec4 dest1; | ||
| 31 | vec4 src0; // source rectangle in texels | ||
| 32 | vec4 src1; | ||
| 33 | uint flags0; | ||
| 34 | uint flags1; | ||
| 35 | uint plane_count; | ||
| 36 | uint out_hdr; // 1: the output holds PQ Rec.2020 values | ||
| 37 | float sdr_white; // nits of SDR white, for the conversions | ||
| 38 | uint width, height; | ||
| 39 | uint pad; | ||
| 40 | } pc; | ||
| 41 | |||
| 42 | vec3 srgb_encode(vec3 c) { | ||
| 43 | c = clamp(c, 0.0, 1.0); | ||
| 44 | return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, greaterThan(c, vec3(0.0031308))); | ||
| 45 | } | ||
| 46 | |||
| 47 | // `pre`: straight-alpha data is filtered premultiplied (colour * alpha), else a transparent texel would drag the colour of its neighbours down. | ||
| 48 | vec4 fetch_texel(texture2D t, ivec2 i, bool pre) { | ||
| 49 | vec4 c = texelFetch(t, i, 0); | ||
| 50 | return pre ? vec4(c.rgb * c.a, c.a) : c; | ||
| 51 | } | ||
| 52 | |||
| 53 | // The plane's value at output pixel `pixel` (its centre), in the plane's own encoding. | ||
| 54 | vec4 fetch_plane(texture2D t, vec4 dest, vec4 src, uint flags, ivec2 pixel) { | ||
| 55 | vec2 p = (vec2(pixel) + 0.5 - dest.xy) / (dest.zw - dest.xy); // 0..1 across the destination rectangle | ||
| 56 | vec2 s = src.xy + p * (src.zw - src.xy); // continuous texel coordinates | ||
| 57 | vec2 lo = src.xy, hi = src.zw - 1.0; | ||
| 58 | bool pre = (flags & flag_straight_alpha) != 0u; | ||
| 59 | vec4 c; | ||
| 60 | if ((flags & flag_point) != 0u) { | ||
| 61 | c = fetch_texel(t, ivec2(clamp(floor(s), lo, hi)), false); | ||
| 62 | } else { | ||
| 63 | vec2 f = clamp(s - 0.5, lo, hi); // the texel whose centre is at or before the sample, clamped inside the rectangle | ||
| 64 | vec2 base = floor(f), w = f - base; | ||
| 65 | ivec2 i0 = ivec2(base), i1 = ivec2(min(base + 1.0, hi)); | ||
| 66 | c = mix(mix(fetch_texel(t, ivec2(i0.x, i0.y), pre), fetch_texel(t, ivec2(i1.x, i0.y), pre), w.x), | ||
| 67 | mix(fetch_texel(t, ivec2(i0.x, i1.y), pre), fetch_texel(t, ivec2(i1.x, i1.y), pre), w.x), w.y); | ||
| 68 | if (pre) c.rgb = c.a > 0.0 ? c.rgb / c.a : vec3(0.0); // back to unassociated colour | ||
| 69 | } | ||
| 70 | if ((flags & flag_srgb_source) != 0u) c.rgb = srgb_encode(c.rgb); | ||
| 71 | return c; | ||
| 72 | } | ||
| 73 | |||
| 74 | const float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875; | ||
| 75 | |||
| 76 | vec3 pq_to_nits(vec3 e) { | ||
| 77 | vec3 ep = pow(clamp(e, 0.0, 1.0), vec3(1.0 / m2)); | ||
| 78 | return 10000.0 * pow(max(ep - c1, 0.0) / (c2 - c3 * ep), vec3(1.0 / m1)); | ||
| 79 | } | ||
| 80 | |||
| 81 | vec3 nits_to_pq(vec3 nits) { | ||
| 82 | vec3 y = pow(clamp(nits / 10000.0, 0.0, 1.0), vec3(m1)); | ||
| 83 | return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2)); | ||
| 84 | } | ||
| 85 | |||
| 86 | vec3 bt709_to_bt2020(vec3 v) { | ||
| 87 | return vec3(dot(vec3(0.627404, 0.329283, 0.043313), v), dot(vec3(0.069097, 0.919540, 0.011362), v), | ||
| 88 | dot(vec3(0.016391, 0.088013, 0.895595), v)); | ||
| 89 | } | ||
| 90 | |||
| 91 | vec3 bt2020_to_bt709(vec3 v) { | ||
| 92 | return vec3(dot(vec3(1.660491, -0.587641, -0.072850), v), dot(vec3(-0.124550, 1.132900, -0.008349), v), | ||
| 93 | dot(vec3(-0.018151, -0.100579, 1.118730), v)); | ||
| 94 | } | ||
| 95 | |||
| 96 | // `c` in the encoding of the plane (hdr: PQ Rec.2020, else G2.2 Rec.709) to the output's. | ||
| 97 | vec3 to_output(vec3 c, bool plane_hdr) { | ||
| 98 | bool out_hdr = pc.out_hdr != 0u; | ||
| 99 | if (plane_hdr == out_hdr) return c; | ||
| 100 | if (!plane_hdr) return nits_to_pq(bt709_to_bt2020(pow(clamp(c, 0.0, 1.0), vec3(2.2))) * pc.sdr_white); | ||
| 101 | return pow(clamp(bt2020_to_bt709(pq_to_nits(c)) / pc.sdr_white, 0.0, 1.0), vec3(1.0 / 2.2)); | ||
| 102 | } | ||
| 103 | |||
| 104 | void main() { | ||
| 105 | ivec2 pixel = ivec2(gl_GlobalInvocationID.xy); | ||
| 106 | if (pixel.x >= int(pc.width) || pixel.y >= int(pc.height)) return; | ||
| 107 | vec2 centre = vec2(pixel) + 0.5; | ||
| 108 | |||
| 109 | vec3 base = vec3(0.0); // outside plane 0's rectangle: black | ||
| 110 | if (all(greaterThanEqual(centre, pc.dest0.xy)) && all(lessThan(centre, pc.dest0.zw))) { | ||
| 111 | vec4 c = fetch_plane(plane0, pc.dest0, pc.src0, pc.flags0 & ~flag_straight_alpha, pixel); | ||
| 112 | base = to_output(c.rgb, (pc.flags0 & flag_hdr10) != 0u); | ||
| 113 | } | ||
| 114 | if (pc.plane_count > 1u && all(greaterThanEqual(centre, pc.dest1.xy)) && all(lessThan(centre, pc.dest1.zw))) { | ||
| 115 | vec4 c = fetch_plane(plane1, pc.dest1, pc.src1, pc.flags1, pixel); | ||
| 116 | bool hdr = (pc.flags1 & flag_hdr10) != 0u; | ||
| 117 | float a = clamp(c.a, 0.0, 1.0); | ||
| 118 | bool straight = (pc.flags1 & flag_straight_alpha) != 0u; | ||
| 119 | vec3 over; | ||
| 120 | if (hdr == (pc.out_hdr != 0u)) { | ||
| 121 | over = c.rgb; // already in the output's space: no conversion, so no un-premultiplying either | ||
| 122 | } else { | ||
| 123 | vec3 colour = straight || a <= 0.0 ? c.rgb : c.rgb / a; // convert unassociated colour | ||
| 124 | over = to_output(colour, hdr); | ||
| 125 | if (!straight) over *= a; | ||
| 126 | } | ||
| 127 | base = straight ? mix(base, over, a) : over + (1.0 - a) * base; | ||
| 128 | } | ||
| 129 | imageStore(out_image, pixel, vec4(base, 1.0)); | ||
| 130 | } | ||