diff options
Diffstat (limited to 'runtime/src/present_compose.comp')
| -rw-r--r-- | runtime/src/present_compose.comp | 120 |
1 files changed, 70 insertions, 50 deletions
diff --git a/runtime/src/present_compose.comp b/runtime/src/present_compose.comp index b557a6444..ff3b0a0c0 100644 --- a/runtime/src/present_compose.comp +++ b/runtime/src/present_compose.comp | |||
| @@ -5,12 +5,15 @@ | |||
| 5 | // | 5 | // |
| 6 | // Every plane is read with texelFetch (no sampler: the filtering is done here, so it works on any sampled format and | 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 | 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, | 8 | // hold sRGB or G2.2 values (push constant sdr_eotf), HDR10 planes hold ST.2084 (PQ) Rec.2020 values. An sRGB-format |
| 9 | // so `flag_srgb_source` encodes it again with the sRGB curve and the bytes come out as they went in. | 9 | // source is decoded by the texture unit, so it is encoded again first and its bytes are read as they went in. |
| 10 | // | 10 | // |
| 11 | // Conversions (all provisional, docs section 4.5): SDR -> HDR10: pow 2.2, Rec.709 -> Rec.2020, times the SDR white level | 11 | // Filtering and blending happen in linear light, premultiplied (docs section 4.5): every texel is decoded to linear before |
| 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 | 12 | // it is weighted (SDR: the EOTF, 1.0 = SDR white, Rec.709; HDR10: PQ to nits, Rec.2020). The filtered plane is converted to |
| 13 | // tone mapping). Same space: untouched. Plane 1 is blended after conversion, in the output's encoded space. | 13 | // the working space of the output (HDR10 output: linear Rec.2020 nits, SDR white = sdr_white nits; SDR output: linear |
| 14 | // Rec.709, 1.0 = SDR white), plane 1 is blended over plane 0 there, and the sum is encoded once (PQ, or the inverse of | ||
| 15 | // the SDR EOTF). HDR10 plane -> SDR output: a luminance shoulder (identity below 0.8 of SDR white, extended Reinhard above, peak at | ||
| 16 | // hdr_peak nits mapped to 1.0), then clipped per channel (no gamut mapping). | ||
| 14 | #version 450 | 17 | #version 450 |
| 15 | #extension GL_EXT_samplerless_texture_functions : require | 18 | #extension GL_EXT_samplerless_texture_functions : require |
| 16 | 19 | ||
| @@ -24,6 +27,7 @@ const uint flag_point = 1u; | |||
| 24 | const uint flag_srgb_source = 2u; | 27 | const uint flag_srgb_source = 2u; |
| 25 | const uint flag_hdr10 = 4u; | 28 | const uint flag_hdr10 = 4u; |
| 26 | const uint flag_straight_alpha = 8u; | 29 | const uint flag_straight_alpha = 8u; |
| 30 | const uint flag_opaque = 16u; // set here for plane 0: its alpha is ignored | ||
| 27 | 31 | ||
| 28 | layout(push_constant) uniform Push { | 32 | layout(push_constant) uniform Push { |
| 29 | vec4 dest0; // destination rectangle in output pixels: left, top, right, bottom | 33 | vec4 dest0; // destination rectangle in output pixels: left, top, right, bottom |
| @@ -36,7 +40,9 @@ layout(push_constant) uniform Push { | |||
| 36 | uint out_hdr; // 1: the output holds PQ Rec.2020 values | 40 | uint out_hdr; // 1: the output holds PQ Rec.2020 values |
| 37 | float sdr_white; // nits of SDR white, for the conversions | 41 | float sdr_white; // nits of SDR white, for the conversions |
| 38 | uint width, height; | 42 | uint width, height; |
| 39 | uint pad; | 43 | uint sdr_eotf; // 0: piecewise sRGB, 1: gamma 2.2 |
| 44 | float hdr_peak; // nits of the HDR10 content's peak, the tone mapper's white point | ||
| 45 | uint pad0, pad1, pad2; | ||
| 40 | } pc; | 46 | } pc; |
| 41 | 47 | ||
| 42 | vec3 srgb_encode(vec3 c) { | 48 | vec3 srgb_encode(vec3 c) { |
| @@ -44,32 +50,14 @@ vec3 srgb_encode(vec3 c) { | |||
| 44 | return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, greaterThan(c, vec3(0.0031308))); | 50 | return mix(c * 12.92, 1.055 * pow(c, vec3(1.0 / 2.4)) - 0.055, greaterThan(c, vec3(0.0031308))); |
| 45 | } | 51 | } |
| 46 | 52 | ||
| 47 | // `pre`: straight-alpha data is filtered premultiplied (colour * alpha), else a transparent texel would drag the colour of its neighbours down. | 53 | vec3 srgb_decode(vec3 e) { |
| 48 | vec4 fetch_texel(texture2D t, ivec2 i, bool pre) { | 54 | e = max(e, 0.0); |
| 49 | vec4 c = texelFetch(t, i, 0); | 55 | return mix(e / 12.92, pow((e + 0.055) / 1.055, vec3(2.4)), greaterThan(e, vec3(0.04045))); |
| 50 | return pre ? vec4(c.rgb * c.a, c.a) : c; | ||
| 51 | } | 56 | } |
| 52 | 57 | ||
| 53 | // The plane's value at output pixel `pixel` (its centre), in the plane's own encoding. | 58 | // SDR encoded -> linear (1.0 = SDR white; may exceed 1 for additive premultiplied colour), and back (clipped to 0..1). |
| 54 | vec4 fetch_plane(texture2D t, vec4 dest, vec4 src, uint flags, ivec2 pixel) { | 59 | vec3 sdr_decode(vec3 e) { return pc.sdr_eotf == 1u ? pow(max(e, 0.0), vec3(2.2)) : srgb_decode(e); } |
| 55 | vec2 p = (vec2(pixel) + 0.5 - dest.xy) / (dest.zw - dest.xy); // 0..1 across the destination rectangle | 60 | vec3 sdr_encode(vec3 l) { return pc.sdr_eotf == 1u ? pow(clamp(l, 0.0, 1.0), vec3(1.0 / 2.2)) : srgb_encode(l); } |
| 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 | 61 | ||
| 74 | const float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875; | 62 | const float m1 = 0.1593017578125, m2 = 78.84375, c1 = 0.8359375, c2 = 18.8515625, c3 = 18.6875; |
| 75 | 63 | ||
| @@ -83,6 +71,31 @@ vec3 nits_to_pq(vec3 nits) { | |||
| 83 | return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2)); | 71 | return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2)); |
| 84 | } | 72 | } |
| 85 | 73 | ||
| 74 | // One texel as linear premultiplied colour (plane space) and its alpha. Premultiplied data is divided by alpha for the | ||
| 75 | // decode (the curve applies to the colour, not to colour times alpha) and multiplied again after. | ||
| 76 | vec4 fetch_texel(texture2D t, ivec2 i, uint flags) { | ||
| 77 | vec4 c = texelFetch(t, i, 0); | ||
| 78 | bool opaque = (flags & flag_opaque) != 0u; | ||
| 79 | float a = opaque ? 1.0 : clamp(c.a, 0.0, 1.0); | ||
| 80 | vec3 e = (flags & flag_srgb_source) != 0u ? srgb_encode(c.rgb) : c.rgb; | ||
| 81 | if (!opaque && (flags & flag_straight_alpha) == 0u) e = a > 0.0 ? e / a : vec3(0.0); | ||
| 82 | vec3 lin = (flags & flag_hdr10) != 0u ? pq_to_nits(e) : sdr_decode(e); | ||
| 83 | return vec4(lin * a, a); | ||
| 84 | } | ||
| 85 | |||
| 86 | // The plane's linear premultiplied value at output pixel `pixel` (its centre), in the plane's own space. | ||
| 87 | vec4 fetch_plane(texture2D t, vec4 dest, vec4 src, uint flags, ivec2 pixel) { | ||
| 88 | vec2 p = (vec2(pixel) + 0.5 - dest.xy) / (dest.zw - dest.xy); // 0..1 across the destination rectangle | ||
| 89 | vec2 s = src.xy + p * (src.zw - src.xy); // continuous texel coordinates | ||
| 90 | vec2 lo = src.xy, hi = src.zw - 1.0; | ||
| 91 | if ((flags & flag_point) != 0u) return fetch_texel(t, ivec2(clamp(floor(s), lo, hi)), flags); | ||
| 92 | vec2 f = clamp(s - 0.5, lo, hi); // the texel whose centre is at or before the sample, clamped inside the rectangle | ||
| 93 | vec2 base = floor(f), w = f - base; | ||
| 94 | ivec2 i0 = ivec2(base), i1 = ivec2(min(base + 1.0, hi)); | ||
| 95 | return mix(mix(fetch_texel(t, ivec2(i0.x, i0.y), flags), fetch_texel(t, ivec2(i1.x, i0.y), flags), w.x), | ||
| 96 | mix(fetch_texel(t, ivec2(i0.x, i1.y), flags), fetch_texel(t, ivec2(i1.x, i1.y), flags), w.x), w.y); | ||
| 97 | } | ||
| 98 | |||
| 86 | vec3 bt709_to_bt2020(vec3 v) { | 99 | 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), | 100 | 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)); | 101 | dot(vec3(0.016391, 0.088013, 0.895595), v)); |
| @@ -93,14 +106,32 @@ vec3 bt2020_to_bt709(vec3 v) { | |||
| 93 | dot(vec3(-0.018151, -0.100579, 1.118730), v)); | 106 | dot(vec3(-0.018151, -0.100579, 1.118730), v)); |
| 94 | } | 107 | } |
| 95 | 108 | ||
| 96 | // `c` in the encoding of the plane (hdr: PQ Rec.2020, else G2.2 Rec.709) to the output's. | 109 | // Tone map: a shoulder on luminance. Identity up to the knee (0.8 of SDR white), then extended Reinhard (Reinhard et al. |
| 97 | vec3 to_output(vec3 c, bool plane_hdr) { | 110 | // 2002, eq. 4) on the excess, scaled to be continuous in value and slope at the knee and to map the content peak to 1.0: |
| 111 | // with t = 1 - knee, u = (Y - knee) / t, w = (Lw - knee) / t: Yd = knee + t u (1 + u / w^2) / (1 + u). The colour is scaled | ||
| 112 | // by Yd / Y. `v`: linear Rec.709, 1.0 = SDR white; Lw = hdr_peak / sdr_white (at least 1). | ||
| 113 | vec3 tone_map(vec3 v) { | ||
| 114 | const float knee = 0.8; | ||
| 115 | float y = dot(v, vec3(0.2126, 0.7152, 0.0722)); | ||
| 116 | if (y <= knee) return v; | ||
| 117 | float t = 1.0 - knee; | ||
| 118 | float w = (max(pc.hdr_peak / pc.sdr_white, 1.0) - knee) / t; | ||
| 119 | float u = (y - knee) / t; | ||
| 120 | return v * ((knee + t * u * (1.0 + u / (w * w)) / (1.0 + u)) / y); | ||
| 121 | } | ||
| 122 | |||
| 123 | // A linear premultiplied colour of alpha `a` from the plane's space to the output's working space. Both conversions are | ||
| 124 | // linear except the tone map, which needs the unassociated colour. | ||
| 125 | vec3 to_working(vec3 lin, float a, bool plane_hdr) { | ||
| 98 | bool out_hdr = pc.out_hdr != 0u; | 126 | bool out_hdr = pc.out_hdr != 0u; |
| 99 | if (plane_hdr == out_hdr) return c; | 127 | if (plane_hdr == out_hdr) return lin; |
| 100 | if (!plane_hdr) return nits_to_pq(bt709_to_bt2020(pow(clamp(c, 0.0, 1.0), vec3(2.2))) * pc.sdr_white); | 128 | if (!plane_hdr) return bt709_to_bt2020(lin) * 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)); | 129 | if (a <= 0.0) return vec3(0.0); |
| 130 | return tone_map(max(bt2020_to_bt709(lin / a) / pc.sdr_white, 0.0)) * a; | ||
| 102 | } | 131 | } |
| 103 | 132 | ||
| 133 | vec3 encode_output(vec3 w) { return pc.out_hdr != 0u ? nits_to_pq(max(w, 0.0)) : sdr_encode(w); } | ||
| 134 | |||
| 104 | void main() { | 135 | void main() { |
| 105 | ivec2 pixel = ivec2(gl_GlobalInvocationID.xy); | 136 | ivec2 pixel = ivec2(gl_GlobalInvocationID.xy); |
| 106 | if (pixel.x >= int(pc.width) || pixel.y >= int(pc.height)) return; | 137 | if (pixel.x >= int(pc.width) || pixel.y >= int(pc.height)) return; |
| @@ -108,23 +139,12 @@ void main() { | |||
| 108 | 139 | ||
| 109 | vec3 base = vec3(0.0); // outside plane 0's rectangle: black | 140 | 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))) { | 141 | 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); | 142 | vec4 c = fetch_plane(plane0, pc.dest0, pc.src0, pc.flags0 | flag_opaque, pixel); |
| 112 | base = to_output(c.rgb, (pc.flags0 & flag_hdr10) != 0u); | 143 | base = to_working(c.rgb, 1.0, (pc.flags0 & flag_hdr10) != 0u); |
| 113 | } | 144 | } |
| 114 | if (pc.plane_count > 1u && all(greaterThanEqual(centre, pc.dest1.xy)) && all(lessThan(centre, pc.dest1.zw))) { | 145 | 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); | 146 | vec4 c = fetch_plane(plane1, pc.dest1, pc.src1, pc.flags1, pixel); |
| 116 | bool hdr = (pc.flags1 & flag_hdr10) != 0u; | 147 | base = to_working(c.rgb, c.a, (pc.flags1 & flag_hdr10) != 0u) + (1.0 - c.a) * base; |
| 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 | } | 148 | } |
| 129 | imageStore(out_image, pixel, vec4(base, 1.0)); | 149 | imageStore(out_image, pixel, vec4(encode_output(base), 1.0)); |
| 130 | } | 150 | } |