Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -4223,12 +4223,16 @@ will forward to. The queue-side calls ignore which queue they came on (one `VkQu
working space, plane 1 is blended over plane 0 there, and the sum is encoded once:

1. Decode. SDR planes: the SDR EOTF (below) gives linear Rec.709 with 1.0 = SDR white. HDR10 planes: PQ inverse to linear
nits (Rec.2020). Premultiplied data is divided by alpha for the decode and multiplied again after, so what is filtered
and blended is linear premultiplied colour; straight-alpha colour is premultiplied after the decode, so a transparent
texel adds no colour of its own (found in review by @Flyander).
nits (Rec.2020). For premultiplied UNORM, encoded RGB up to alpha is divided by alpha before decoding and multiplied
again afterwards. Encoded RGB above alpha is decoded as additive light, including all RGB when alpha is zero. Both
contributions are filtered separately so coverage cannot attenuate the additive light. Premultiplied
`*_UNORM_SRGB` planes are already hardware-decoded to linear premultiplied light by `texelFetch` (no encoded-space
division by alpha). Straight-alpha colour is premultiplied after the decode, so a transparent texel adds no colour of
its own (found in review by @Flyander).
2. Convert, still linear. HDR10 output: SDR to Rec.2020 (the Rec.709 to Rec.2020 matrix from the primaries; rows sum to 1)
times the SDR white level in nits. SDR output: HDR10 to Rec.709 (the inverse matrix), divided by the SDR white level, then
tone mapped (below). A plane already in the output's space is not converted.
tone mapped (below). Tone mapping unpremultiplies only the covered light and maps the filtered additive light
independently. A plane already in the output's space is not converted.
3. Blend `over + (1 - alpha) * base` (premultiplied; straight alpha was premultiplied in step 1); black outside plane 0.
4. Encode once: PQ of the nits for an HDR10 output (argument clamped to 0..10000 nits), the inverse SDR EOTF (clipped to
0..1) for an SDR one.
Expand Down
73 changes: 50 additions & 23 deletions runtime/src/present_compose.comp
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,54 @@ vec3 nits_to_pq(vec3 nits) {
return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3(m2));
}

// One texel as linear premultiplied colour (plane space) and its alpha. Premultiplied data is divided by alpha for the
// decode (the curve applies to the colour, not to colour times alpha) and multiplied again after.
vec4 fetch_texel(texture2D t, ivec2 i, uint flags) {
struct PlaneSample {
vec4 covered;
vec3 additive;
};

PlaneSample fetch_texel(texture2D t, ivec2 i, uint flags) {
vec4 c = texelFetch(t, i, 0);
bool opaque = (flags & flag_opaque) != 0u;
float a = opaque ? 1.0 : clamp(c.a, 0.0, 1.0);
vec3 e = (flags & flag_srgb_source) != 0u ? srgb_encode(c.rgb) : c.rgb;
if (!opaque && (flags & flag_straight_alpha) == 0u) e = a > 0.0 ? e / a : vec3(0.0);
vec3 lin = (flags & flag_hdr10) != 0u ? pq_to_nits(e) : sdr_decode(e);
return vec4(lin * a, a);
bool srgb_src = (flags & flag_srgb_source) != 0u;
bool hdr = (flags & flag_hdr10) != 0u;
bool premul = !opaque && (flags & flag_straight_alpha) == 0u;
if (srgb_src && premul && !hdr) {
vec3 lin = max(c.rgb, 0.0);
if (pc.sdr_eotf == 1u) {
vec3 covered = a > 0.0 ? sdr_decode(srgb_encode(min(lin, vec3(a)) / a)) * a : vec3(0.0);
vec3 additive = sdr_decode(srgb_encode(max(lin - a, 0.0)));
return PlaneSample(vec4(covered, a), additive);
}
return PlaneSample(vec4(lin, a), vec3(0.0));
}
vec3 e = srgb_src ? srgb_encode(c.rgb) : c.rgb;
if (premul) {
vec3 covered = a > 0.0 ? min(e, vec3(a)) / a : vec3(0.0);
vec3 excess = max(e - a, 0.0);
return PlaneSample(vec4((hdr ? pq_to_nits(covered) : sdr_decode(covered)) * a, a),
hdr ? pq_to_nits(excess) : sdr_decode(excess));
}
vec3 lin = hdr ? pq_to_nits(e) : sdr_decode(e);
return PlaneSample(vec4(lin * a, a), vec3(0.0));
}

PlaneSample mix_samples(PlaneSample a, PlaneSample b, float weight) {
return PlaneSample(mix(a.covered, b.covered, weight), mix(a.additive, b.additive, weight));
}

// The plane's linear premultiplied value at output pixel `pixel` (its centre), in the plane's own space.
vec4 fetch_plane(texture2D t, vec4 dest, vec4 src, uint flags, ivec2 pixel) {
vec2 p = (vec2(pixel) + 0.5 - dest.xy) / (dest.zw - dest.xy); // 0..1 across the destination rectangle
vec2 s = src.xy + p * (src.zw - src.xy); // continuous texel coordinates
PlaneSample fetch_plane(texture2D t, vec4 dest, vec4 src, uint flags, ivec2 pixel) {
vec2 p = (vec2(pixel) + 0.5 - dest.xy) / (dest.zw - dest.xy);
vec2 s = src.xy + p * (src.zw - src.xy);
vec2 lo = src.xy, hi = src.zw - 1.0;
if ((flags & flag_point) != 0u) return fetch_texel(t, ivec2(clamp(floor(s), lo, hi)), flags);
vec2 f = clamp(s - 0.5, lo, hi); // the texel whose centre is at or before the sample, clamped inside the rectangle
vec2 f = clamp(s - 0.5, lo, hi);
vec2 base = floor(f), w = f - base;
ivec2 i0 = ivec2(base), i1 = ivec2(min(base + 1.0, hi));
return mix(mix(fetch_texel(t, ivec2(i0.x, i0.y), flags), fetch_texel(t, ivec2(i1.x, i0.y), flags), w.x),
mix(fetch_texel(t, ivec2(i0.x, i1.y), flags), fetch_texel(t, ivec2(i1.x, i1.y), flags), w.x), w.y);
return mix_samples(mix_samples(fetch_texel(t, ivec2(i0.x, i0.y), flags),
fetch_texel(t, ivec2(i1.x, i0.y), flags), w.x),
mix_samples(fetch_texel(t, ivec2(i0.x, i1.y), flags),
fetch_texel(t, ivec2(i1.x, i1.y), flags), w.x), w.y);
}

vec3 bt709_to_bt2020(vec3 v) {
Expand All @@ -120,14 +145,16 @@ vec3 tone_map(vec3 v) {
return v * ((knee + t * u * (1.0 + u / (w * w)) / (1.0 + u)) / y);
}

// A linear premultiplied colour of alpha `a` from the plane's space to the output's working space. Both conversions are
// linear except the tone map, which needs the unassociated colour.
vec3 to_working(vec3 lin, float a, bool plane_hdr) {
vec3 to_working(PlaneSample sample_value, bool plane_hdr) {
bool out_hdr = pc.out_hdr != 0u;
vec3 lin = sample_value.covered.rgb + sample_value.additive;
if (plane_hdr == out_hdr) return lin;
if (!plane_hdr) return bt709_to_bt2020(lin) * pc.sdr_white;
if (a <= 0.0) return vec3(0.0);
return tone_map(max(bt2020_to_bt709(lin / a) / pc.sdr_white, 0.0)) * a;
float a = sample_value.covered.a;
vec3 covered = a > 0.0 ? tone_map(max(bt2020_to_bt709(sample_value.covered.rgb / a) / pc.sdr_white, 0.0)) * a
: vec3(0.0);
vec3 additive = tone_map(max(bt2020_to_bt709(sample_value.additive) / pc.sdr_white, 0.0));
return covered + additive;
}

vec3 encode_output(vec3 w) { return pc.out_hdr != 0u ? nits_to_pq(max(w, 0.0)) : sdr_encode(w); }
Expand All @@ -139,12 +166,12 @@ void main() {

vec3 base = vec3(0.0); // outside plane 0's rectangle: black
if (all(greaterThanEqual(centre, pc.dest0.xy)) && all(lessThan(centre, pc.dest0.zw))) {
vec4 c = fetch_plane(plane0, pc.dest0, pc.src0, pc.flags0 | flag_opaque, pixel);
base = to_working(c.rgb, 1.0, (pc.flags0 & flag_hdr10) != 0u);
PlaneSample c = fetch_plane(plane0, pc.dest0, pc.src0, pc.flags0 | flag_opaque, pixel);
base = to_working(c, (pc.flags0 & flag_hdr10) != 0u);
}
if (pc.plane_count > 1u && all(greaterThanEqual(centre, pc.dest1.xy)) && all(lessThan(centre, pc.dest1.zw))) {
vec4 c = fetch_plane(plane1, pc.dest1, pc.src1, pc.flags1, pixel);
base = to_working(c.rgb, c.a, (pc.flags1 & flag_hdr10) != 0u) + (1.0 - c.a) * base;
PlaneSample c = fetch_plane(plane1, pc.dest1, pc.src1, pc.flags1, pixel);
base = to_working(c, (pc.flags1 & flag_hdr10) != 0u) + (1.0 - c.covered.a) * base;
}
imageStore(out_image, pixel, vec4(encode_output(base), 1.0));
}
Loading
Loading