Constant PRELUDE

Source
pub const PRELUDE: &str = r#"#version 330 core
precision highp float;

in vec2 v_uv;
out vec4 out_color;

uniform sampler2D tex;
uniform vec2 u_res;
uniform float u_time;
uniform float u_scale;
uniform float p0;
uniform float p1;
uniform float p2;
uniform float p3;
uniform float p4;
uniform float p5;
uniform float p6;
uniform float p7;
uniform sampler2D u_mask;
uniform int u_has_mask;

float luma(vec3 c) { return dot(c, vec3(0.2126, 0.7152, 0.0722)); }

float hash12(vec2 p) {
    vec3 q = fract(vec3(p.xyx) * 0.1031);
    q += dot(q, q.yzx + 33.33);
    return fract((q.x + q.y) * q.z);
}

float hash11(float x) { return hash12(vec2(x, x * 1.7 + 0.37)); }

vec3 rgb2hsl(vec3 c) {
    float mx = max(c.r, max(c.g, c.b));
    float mn = min(c.r, min(c.g, c.b));
    float l = (mx + mn) * 0.5;
    float d = mx - mn;
    float h = 0.0;
    float s = 0.0;
    if (d > 1e-6) {
        s = (l > 0.5) ? d / max(2.0 - mx - mn, 1e-6) : d / max(mx + mn, 1e-6);
        if (mx == c.r) {
            h = (c.g - c.b) / d + ((c.g < c.b) ? 6.0 : 0.0);
        } else if (mx == c.g) {
            h = (c.b - c.r) / d + 2.0;
        } else {
            h = (c.r - c.g) / d + 4.0;
        }
        h /= 6.0;
    }
    return vec3(h, s, l);
}

float hue2rgb(float p, float q, float t) {
    t = fract(t);
    if (t < 1.0 / 6.0) { return p + (q - p) * 6.0 * t; }
    if (t < 0.5) { return q; }
    if (t < 2.0 / 3.0) { return p + (q - p) * (2.0 / 3.0 - t) * 6.0; }
    return p;
}

vec3 hsl2rgb(vec3 c) {
    if (c.y <= 0.0) { return vec3(c.z); }
    float q = (c.z < 0.5) ? c.z * (1.0 + c.y) : c.z + c.y - c.z * c.y;
    float p = 2.0 * c.z - q;
    return vec3(hue2rgb(p, q, c.x + 1.0 / 3.0), hue2rgb(p, q, c.x), hue2rgb(p, q, c.x - 1.0 / 3.0));
}

/// Sample `t` with the uv clamped to the half-texel border of a `u_res`-sized image.
vec4 sample_clamped(sampler2D t, vec2 uv) {
    vec2 half_texel = 0.5 / max(u_res, vec2(1.0));
    return texture(t, clamp(uv, half_texel, 1.0 - half_texel));
}

float mask_at(vec2 uv) { return (u_has_mask == 0) ? 1.0 : texture(u_mask, uv).r; }

/// Blend an effect result back over the untouched source through the mask.
vec4 apply_mask(vec4 src, vec4 fx, vec2 uv) { return mix(src, fx, mask_at(uv)); }
"#;
Expand description

Shared header: version, precision, varyings, the uniform block and helpers (rgb<->hsl, luma, hash noise, clamped sampling, mask application).