pub const WGSL: &str = r#"
// ---- sg HDR output (sg_abi::hdr) ----
struct SgOutput { mode: u32, paper_white_nits: f32, peak_nits: f32, _pad: f32 };
const SG_OUT_SDR_SRGB_FORMAT: u32 = 0u;
const SG_OUT_SDR_UNORM: u32 = 1u;
const SG_OUT_SCRGB: u32 = 2u;
const SG_OUT_PQ: u32 = 3u;
fn sg_pq_oetf(nits: vec3f) -> vec3f {
let m1 = 2610.0 / 16384.0;
let m2 = 2523.0 / 4096.0 * 128.0;
let c1 = 3424.0 / 4096.0;
let c2 = 2413.0 / 4096.0 * 32.0;
let c3 = 2392.0 / 4096.0 * 32.0;
let y = pow(clamp(nits / 10000.0, vec3f(0.0), vec3f(1.0)), vec3f(m1));
return pow((c1 + c2 * y) / (1.0 + c3 * y), vec3f(m2));
}
fn sg_srgb_oetf(x: vec3f) -> vec3f {
let c = clamp(x, vec3f(0.0), vec3f(1.0));
return select(1.055 * pow(c, vec3f(1.0 / 2.4)) - 0.055, 12.92 * c, c <= vec3f(0.0031308));
}
fn sg_rolloff(x: f32, knee: f32, peak: f32) -> f32 {
if (x <= knee) { return max(x, 0.0); }
let r = peak - knee;
return knee + r * (1.0 - exp(-(x - knee) / r));
}
fn sg_tonemap(rgb: vec3f, knee: f32, peak: f32) -> vec3f {
let m = max(rgb.r, max(rgb.g, rgb.b));
if (m <= knee) { return max(rgb, vec3f(0.0)); }
return max(rgb * (sg_rolloff(m, knee, peak) / m), vec3f(0.0));
}
fn sg_bt709_to_bt2020(c: vec3f) -> vec3f {
return vec3f(
dot(vec3f(0.627404, 0.329282, 0.0433136), c),
dot(vec3f(0.069097, 0.919540, 0.0113612), c),
dot(vec3f(0.0163916, 0.0880132, 0.895595), c));
}
// Scene-linear BT.709 in paper-white units -> the value to write to the surface.
fn sg_encode_output(rgb: vec3f, o: SgOutput) -> vec4f {
let h = o.peak_nits / o.paper_white_nits;
switch o.mode {
case 0u: { return vec4f(sg_tonemap(rgb, 0.8, 1.0), 1.0); }
case 1u: { return vec4f(sg_srgb_oetf(sg_tonemap(rgb, 0.8, 1.0)), 1.0); }
case 2u: { return vec4f(sg_tonemap(rgb, 0.75 * h, h) * (o.paper_white_nits / 80.0), 1.0); }
default: { return vec4f(sg_pq_oetf(max(sg_bt709_to_bt2020(sg_tonemap(rgb, 0.75 * h, h)), vec3f(0.0)) * o.paper_white_nits), 1.0); }
}
}
"#;Expand description
WGSL for guests: paste (or concat!) into a shader module and bind an
SgOutput uniform built by Output::uniform.