upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/src/present_compose.comp
diff options
context:
space:
mode:
authorAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 16:51:05 +0100
committerAlxcBTW <187763517+AlxcBTW@users.noreply.github.com>2026-11-22 16:51:05 +0100
commit76262214e87312a1bac6f77e1fe61b5a899ad67b (patch)
tree7bee9b774e452e04c3ca964f90024c93d9803437 /runtime/src/present_compose.comp
parent4eb6155d3ac4f9d3fadb0d095c4ce13e76783615 (diff)
PresentX planes (M7): compose 1-2 planes on Vulkan with placement, filter, alpha overlay and HDR10/SDR conversion
A plain single plane keeps the old blit. A plane with pDestPlacementBase, a point/linear ScaleFilter, the HDR10 colour space, or a second overlay plane goes through VulkanPresenter::present_planes: a compute pass (present_compose.comp, SPIR-V embedded) into an RGBA16F image that is copied to the swapchain. HDR10 uses an ST.2084 swapchain when the surface lists one, else is clipped to SDR. An SDR plane 1 next to an HDR10 plane 0 is the GameDVR copy (public HDR sample) unless flagged an overlay. The layouts, flag values and alpha convention are ours; xbox_present_test checks every case against a CPU model. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src/present_compose.comp')
-rw-r--r--runtime/src/present_compose.comp124
1 files changed, 124 insertions, 0 deletions
diff --git a/runtime/src/present_compose.comp b/runtime/src/present_compose.comp
new file mode 100644
index 000000000..303730cef
--- /dev/null
+++ b/runtime/src/present_compose.comp
@@ -0,0 +1,124 @@
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
17layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
18
19layout(set = 0, binding = 0) uniform texture2D plane0;
20layout(set = 0, binding = 1) uniform texture2D plane1;
21layout(set = 0, binding = 2, rgba16f) uniform writeonly image2D out_image;
22
23const uint flag_point = 1u;
24const uint flag_srgb_source = 2u;
25const uint flag_hdr10 = 4u;
26const uint flag_straight_alpha = 8u;
27
28layout(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
42vec3 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
47vec4 fetch_texel(texture2D t, ivec2 i) { return texelFetch(t, i, 0); }
48
49// The plane's value at output pixel `pixel` (its centre), in the plane's own encoding.
50vec4 fetch_plane(texture2D t, vec4 dest, vec4 src, uint flags, ivec2 pixel) {
51 vec2 p = (vec2(pixel) + 0.5 - dest.xy) / (dest.zw - dest.xy); // 0..1 across the destination rectangle
52 vec2 s = src.xy + p * (src.zw - src.xy); // continuous texel coordinates
53 vec2 lo = src.xy, hi = src.zw - 1.0;
54 vec4 c;
55 if ((flags & flag_point) != 0u) {
56 c = fetch_texel(t, ivec2(clamp(floor(s), lo, hi)));
57 } else {
58 vec2 f = clamp(s - 0.5, lo, hi); // the texel whose centre is at or before the sample, clamped inside the rectangle
59 vec2 base = floor(f), w = f - base;
60 ivec2 i0 = ivec2(base), i1 = ivec2(min(base + 1.0, hi));
61 c = mix(mix(fetch_texel(t, ivec2(i0.x, i0.y)), fetch_texel(t, ivec2(i1.x, i0.y)), w.x),
62 mix(fetch_texel(t, ivec2(i0.x, i1.y)), fetch_texel(t, ivec2(i1.x, i1.y)), w.x), w.y);
63 }
64 if ((flags & flag_srgb_source) != 0u) c.rgb = srgb_encode(c.rgb);
65 return c;
66}
67
68const float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875;
69
70vec3 pq_to_nits(vec3 e) {
71 vec3 ep = pow(clamp(e, 0.0, 1.0), vec3(1.0 / m2));
72 return 10000.0 * pow(max(ep - c1, 0.0) / (c2 - c3 * ep), vec3(1.0 / m1));
73}
74
75vec3 nits_to_pq(vec3 nits) {
76 vec3 y = pow(clamp(nits / 10000.0, 0.0, 1.0), vec3(m1));
77 return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2));
78}
79
80vec3 bt709_to_bt2020(vec3 v) {
81 return vec3(dot(vec3(0.627404, 0.329283, 0.043313), v), dot(vec3(0.069097, 0.919540, 0.011362), v),
82 dot(vec3(0.016391, 0.088013, 0.895595), v));
83}
84
85vec3 bt2020_to_bt709(vec3 v) {
86 return vec3(dot(vec3(1.660491, -0.587641, -0.072850), v), dot(vec3(-0.124550, 1.132900, -0.008349), v),
87 dot(vec3(-0.018151, -0.100579, 1.118730), v));
88}
89
90// `c` in the encoding of the plane (hdr: PQ Rec.2020, else G2.2 Rec.709) to the output's.
91vec3 to_output(vec3 c, bool plane_hdr) {
92 bool out_hdr = pc.out_hdr != 0u;
93 if (plane_hdr == out_hdr) return c;
94 if (!plane_hdr) return nits_to_pq(bt709_to_bt2020(pow(clamp(c, 0.0, 1.0), vec3(2.2))) * pc.sdr_white);
95 return pow(clamp(bt2020_to_bt709(pq_to_nits(c)) / pc.sdr_white, 0.0, 1.0), vec3(1.0 / 2.2));
96}
97
98void main() {
99 ivec2 pixel = ivec2(gl_GlobalInvocationID.xy);
100 if (pixel.x >= int(pc.width) || pixel.y >= int(pc.height)) return;
101 vec2 centre = vec2(pixel) + 0.5;
102
103 vec3 base = vec3(0.0); // outside plane 0's rectangle: black
104 if (all(greaterThanEqual(centre, pc.dest0.xy)) && all(lessThan(centre, pc.dest0.zw))) {
105 vec4 c = fetch_plane(plane0, pc.dest0, pc.src0, pc.flags0, pixel);
106 base = to_output(c.rgb, (pc.flags0 & flag_hdr10) != 0u);
107 }
108 if (pc.plane_count > 1u && all(greaterThanEqual(centre, pc.dest1.xy)) && all(lessThan(centre, pc.dest1.zw))) {
109 vec4 c = fetch_plane(plane1, pc.dest1, pc.src1, pc.flags1, pixel);
110 bool hdr = (pc.flags1 & flag_hdr10) != 0u;
111 float a = clamp(c.a, 0.0, 1.0);
112 bool straight = (pc.flags1 & flag_straight_alpha) != 0u;
113 vec3 over;
114 if (hdr == (pc.out_hdr != 0u)) {
115 over = c.rgb; // already in the output's space: no conversion, so no un-premultiplying either
116 } else {
117 vec3 colour = straight || a <= 0.0 ? c.rgb : c.rgb / a; // convert unassociated colour
118 over = to_output(colour, hdr);
119 if (!straight) over *= a;
120 }
121 base = straight ? mix(base, over, a) : over + (1.0 - a) * base;
122 }
123 imageStore(out_image, pixel, vec4(base, 1.0));
124}