Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

9.5 KB
Raw Blame
#pragma shader "Blit"
#pragma version 300 es
#pragma domain PostProcess
// The display stage: with ES_DISPLAY_LUT, a graded look is read off a 32x32x32 table (1024x32: the slice along x is blue, red runs along a slice, green up the picture) by the picture as
// the display will show it, after the output transform and the transfer function. The amount is the pipeline's, not a scene's.
#pragma param u_displayLut texture default(white)
#pragma param u_displayLutAmount float default(0)

#pragma fragment
precision highp float;

in vec2 v_texCoord;
uniform sampler2D u_texture;
#ifdef ES_COPY_DEPTH
uniform highp sampler2D u_sceneDepth;
#endif
out vec4 fragColor;

#ifdef ES_UPSCALE
// The picture is being stretched over more pixels than it has. A bilinear copy softens every edge by the stretch; this is
// Catmull-Rom (Jimenez's five taps of the hardware filter) clamped to what the four nearest texels span, so it sharpens an
// edge without drawing the halo a plain cubic does.
vec4 upscale(vec2 uv) {
    vec2 size = vec2(textureSize(u_texture, 0));
    vec2 pos = uv * size;
    vec2 centre = floor(pos - 0.5) + 0.5;
    vec2 f = pos - centre;
    vec2 f2 = f * f;
    vec2 f3 = f2 * f;
    vec2 w0 = f2 - 0.5 * (f3 + f);
    vec2 w1 = 1.5 * f3 - 2.5 * f2 + 1.0;
    vec2 w3 = 0.5 * (f3 - f2);
    vec2 w2 = 1.0 - w0 - w1 - w3;
    vec2 w12 = w1 + w2;
    vec2 t0 = (centre - 1.0) / size;
    vec2 t3 = (centre + 2.0) / size;
    vec2 t12 = (centre + w2 / w12) / size;
    vec4 sum = texture(u_texture, vec2(t12.x, t0.y)) * (w12.x * w0.y)
             + texture(u_texture, vec2(t0.x, t12.y)) * (w0.x * w12.y)
             + texture(u_texture, t12) * (w12.x * w12.y)
             + texture(u_texture, vec2(t3.x, t12.y)) * (w3.x * w12.y)
             + texture(u_texture, vec2(t12.x, t3.y)) * (w12.x * w3.y);
    sum /= (w12.x * w0.y + w0.x * w12.y + w12.x * w12.y + w3.x * w12.y + w12.x * w3.y);
    ivec2 base = ivec2(centre - 0.5);
    ivec2 hi = ivec2(size) - 1;
    vec4 a = texelFetch(u_texture, clamp(base, ivec2(0), hi), 0);
    vec4 b = texelFetch(u_texture, clamp(base + ivec2(1, 0), ivec2(0), hi), 0);
    vec4 d = texelFetch(u_texture, clamp(base + ivec2(0, 1), ivec2(0), hi), 0);
    vec4 e = texelFetch(u_texture, clamp(base + ivec2(1, 1), ivec2(0), hi), 0);
    return clamp(sum, min(min(a, b), min(d, e)), max(max(a, b), max(d, e)));
}
#endif

#ifdef ES_DISPLAY_LUT
vec3 displayLookup(vec3 c) {
    const float size = 32.0;
    float b = clamp(c.b, 0.0, 1.0) * (size - 1.0);
    float slice0 = floor(b);
    float slice1 = min(slice0 + 1.0, size - 1.0);
    float u = (clamp(c.r, 0.0, 1.0) * (size - 1.0) + 0.5) / (size * size);
    float v = (clamp(c.g, 0.0, 1.0) * (size - 1.0) + 0.5) / size;
    vec3 a = texture(u_displayLut, vec2(u + slice0 / size, v)).rgb;
    vec3 d = texture(u_displayLut, vec2(u + slice1 / size, v)).rgb;
    return mix(a, d, b - slice0);
}
#endif

void main() {
#ifdef ES_COPY_DEPTH
    // How far each pixel of the opaque scene is from the eye, in world units, for a surface that measures the column of water or
    // glass in front of it. A sky pixel is as far as the copy can say (the largest number its 16-bit floats hold), and says so in g: its colour in the copy is not the sky's (that is painted later), so a
    // reflection must not take it for something met. Alpha 0.5 is the same "a real copy" mark as the colour copy's.
    ivec2 depthSize = textureSize(u_sceneDepth, 0);
    float rawDepth = texelFetch(u_sceneDepth, clamp(ivec2(v_texCoord * vec2(depthSize)), ivec2(0), depthSize - ivec2(1)), 0).r;
    vec3 scenePoint = worldFromDepth(v_texCoord, rawDepth);
    float eyeDistance = u_camera.w > 0.5 ? length(scenePoint - u_camera.xyz) : -dot(scenePoint, u_camera.xyz);
    fragColor = vec4(rawDepth >= 1.0 ? 65504.0 : eyeDistance, rawDepth >= 1.0 ? 1.0 : 0.0, 0.0, 0.5);
    return;
#endif
#ifdef ES_UPSCALE
    vec4 c = upscale(v_texCoord);
#else
    vec4 c = texture(u_texture, v_texCoord);
#endif
    // The editor view's exposure (u_time.z, stops): zero outside the editor, where
    // exp2(0) = 1 leaves every pixel as it was. Before the curve, like any exposure.
#ifdef ES_COPY_EXACT
    // The picture as it is, alpha included: the opaque scene kept aside and put back around the blended draws. Its alpha is how much
    // of each pixel geometry covers (the sky reads it), so a copy that marked it would leave every pixel half covered.
    fragColor = c;
    return;
#endif
#ifdef ES_COPY
    // A copy of the picture for a surface that looks through to it. Alpha 0.5 says "a real copy": the texture param a shader falls
    // back to when the frame took none is white, whose alpha is 1.
    fragColor = vec4(c.rgb, 0.5);
    return;
#endif
#ifdef ES_RGBM
    // A probe capture's face packed for an 8-bit readback: RGBM over 8. Alpha 0 is a pixel nothing was drawn on (the sky, which
    // post-processing paints), left for the reader to fill.
    if (c.a <= 0.001) { fragColor = vec4(0.0); return; }
    vec3 h = clamp(c.rgb, vec3(0.0), vec3(8.0));
    float m = clamp(max(max(h.r, h.g), max(h.b, 1e-6)) / 8.0, 1.0 / 255.0, 1.0);
    m = ceil(m * 255.0) / 255.0;
    fragColor = vec4(h / (m * 8.0), m);
    return;
#endif
    c = vec4(c.rgb * exp2(u_time.z), c.a);
#ifdef ES_TONEMAP
    // The output transform: scene radiance can exceed 1.0 on an HDR target,
    // and this is the one place a chain maps it down. Before the OETF, which
    // is a transfer function and not a look.
    c = vec4(acesFilmic(c.rgb), c.a);
#endif
#ifdef ES_TONEMAP_NEUTRAL
    c = vec4(pbrNeutral(c.rgb), c.a);
#endif
#ifdef ES_LINEAR
    // The one mandatory OETF: the chain upstream is linear (sRGB attachments
    // decode on sample), the canvas backbuffer is plain UNORM.
    vec3 shown = linearToSrgb(c.rgb);
#else
    vec3 shown = c.rgb;
#endif
#ifdef ES_DISPLAY_LUT
    // What the display will show, graded: the table is authored against the picture as it looks, so it reads the encoded colour.
    shown = mix(shown, displayLookup(shown), clamp(u_displayLutAmount, 0.0, 1.0));
#endif
    fragColor = vec4(shown, c.a);
}
#pragma end

#pragma fragment wgsl
#ifdef ES_UPSCALE
fn upscale(uv : vec2f) -> vec4f {
    let size = vec2f(textureDimensions(t0, 0));
    let pos = uv * size;
    let centre = floor(pos - vec2f(0.5)) + vec2f(0.5);
    let f = pos - centre;
    let f2 = f * f;
    let f3 = f2 * f;
    let w0 = f2 - 0.5 * (f3 + f);
    let w1 = 1.5 * f3 - 2.5 * f2 + vec2f(1.0);
    let w3 = 0.5 * (f3 - f2);
    let w2 = vec2f(1.0) - w0 - w1 - w3;
    let w12 = w1 + w2;
    let tapLo = (centre - vec2f(1.0)) / size;
    let tapHi = (centre + vec2f(2.0)) / size;
    let tapMid = (centre + w2 / w12) / size;
    var sum = textureSampleLevel(t0, s0, vec2f(tapMid.x, tapLo.y), 0.0) * (w12.x * w0.y)
            + textureSampleLevel(t0, s0, vec2f(tapLo.x, tapMid.y), 0.0) * (w0.x * w12.y)
            + textureSampleLevel(t0, s0, tapMid, 0.0) * (w12.x * w12.y)
            + textureSampleLevel(t0, s0, vec2f(tapHi.x, tapMid.y), 0.0) * (w3.x * w12.y)
            + textureSampleLevel(t0, s0, vec2f(tapMid.x, tapHi.y), 0.0) * (w12.x * w3.y);
    sum = sum / (w12.x * w0.y + w0.x * w12.y + w12.x * w12.y + w3.x * w12.y + w12.x * w3.y);
    let base = vec2i(centre - vec2f(0.5));
    let hi = vec2i(size) - vec2i(1);
    let a = textureLoad(t0, clamp(base, vec2i(0), hi), 0);
    let b = textureLoad(t0, clamp(base + vec2i(1, 0), vec2i(0), hi), 0);
    let d = textureLoad(t0, clamp(base + vec2i(0, 1), vec2i(0), hi), 0);
    let e = textureLoad(t0, clamp(base + vec2i(1, 1), vec2i(0), hi), 0);
    return clamp(sum, min(min(a, b), min(d, e)), max(max(a, b), max(d, e)));
}
#endif

#ifdef ES_DISPLAY_LUT
fn displayLookup(c : vec3f) -> vec3f {
    let size = 32.0;
    let b = clamp(c.b, 0.0, 1.0) * (size - 1.0);
    let slice0 = floor(b);
    let slice1 = min(slice0 + 1.0, size - 1.0);
    let u = (clamp(c.r, 0.0, 1.0) * (size - 1.0) + 0.5) / (size * size);
    let v = (clamp(c.g, 0.0, 1.0) * (size - 1.0) + 0.5) / size;
    let a = textureSampleLevel(u_displayLut, u_displayLut_s, vec2f(u + slice0 / size, v), 0.0).rgb;
    let d = textureSampleLevel(u_displayLut, u_displayLut_s, vec2f(u + slice1 / size, v), 0.0).rgb;
    return mix(a, d, b - slice0);
}
#endif

@fragment fn fs_main(v : VSOut) -> @location(0) vec4f {
#ifdef ES_COPY_DEPTH
    let depthSize = vec2i(textureDimensions(t7, 0));
    let rawDepth = textureLoad(t7, clamp(vec2i(v.v_texCoord * vec2f(depthSize)), vec2i(0), depthSize - vec2i(1)), 0);
    let scenePoint = worldFromDepth(v.v_texCoord, rawDepth);
    var eyeDistance = -dot(scenePoint, frame.camera.xyz);
    if (frame.camera.w > 0.5) { eyeDistance = length(scenePoint - frame.camera.xyz); }
    return vec4f(select(eyeDistance, 65504.0, rawDepth >= 1.0), select(0.0, 1.0, rawDepth >= 1.0), 0.0, 0.5);
#endif
#ifdef ES_UPSCALE
    var c = upscale(v.v_texCoord);
#else
    var c = textureSampleLevel(t0, s0, v.v_texCoord, 0.0);
#endif
#ifdef ES_COPY_EXACT
    return c;
#endif
#ifdef ES_COPY
    return vec4f(c.rgb, 0.5);
#endif
#ifdef ES_RGBM
    if (c.a <= 0.001) { return vec4f(0.0); }
    let h = clamp(c.rgb, vec3f(0.0), vec3f(8.0));
    var m = clamp(max(max(h.r, h.g), max(h.b, 1e-6)) / 8.0, 1.0 / 255.0, 1.0);
    m = ceil(m * 255.0) / 255.0;
    return vec4f(h / (m * 8.0), m);
#endif
    c = vec4f(c.rgb * exp2(tc.u_time.z), c.a);
#ifdef ES_TONEMAP
    c = vec4f(acesFilmic(c.rgb), c.a);
#endif
#ifdef ES_TONEMAP_NEUTRAL
    c = vec4f(pbrNeutral(c.rgb), c.a);
#endif
#ifdef ES_LINEAR
    var shown = linearToSrgb(c.rgb);
#else
    var shown = c.rgb;
#endif
#ifdef ES_DISPLAY_LUT
    shown = mix(shown, displayLookup(shown), clamp(mc.u_displayLutAmount, 0.0, 1.0));
#endif
    return vec4f(shown, c.a);
}
#pragma end