Skip to content

A lightweight Clip Studio Paint alternative with real-time multiplayer collaborative drawing. Built with Tauri and React.

14.3 KB
Raw Blame
//! Non-destructive GPU Layer Styles WGSL shaders:
//! 1. `SEPARABLE_ALPHA_BLUR_WGSL`: Two-pass separable Gaussian blur (Horizontal + Vertical)
//!    for smooth, artifact-free Drop Shadow and Outer Glow alpha halos.
//! 2. `STROKE_SDF_HORIZ_WGSL` & `STROKE_SDF_VERT_WGSL`: Fast 2-pass separable exact Euclidean
//!    distance transform (SDF) for artifact-free circular `stroke` outlines (replaces 144-tap radial loop).
//! 3. `LAYER_STYLES_COMPOSITE_WGSL`: Evaluates `stroke` (`outside` / `inside` / `center`),
//!    `dropShadow`, and `outerGlow` in exact Photoshop/InkStudio stacking order.

pub const SEPARABLE_ALPHA_BLUR_WGSL: &str = r#"
struct BlurUniforms {
    viewport_size_phys: vec2<f32>,
    direction: vec2<f32>, // (1, 0) for horizontal pass, (0, 1) for vertical pass
    params: vec4<f32>,    // x = radius_px
};

@group(0) @binding(0) var<uniform> u: BlurUniforms;
@group(0) @binding(1) var src_tex: texture_2d<f32>;
@group(0) @binding(2) var linear_sampler: sampler;

struct VsOut {
    @builtin(position) pos: vec4<f32>,
    @location(0) uv: vec2<f32>,
};

@vertex
fn vs_main(@builtin(vertex_index) vid: u32) -> VsOut {
    let x = f32((vid << 1u) & 2u);
    let y = f32(vid & 2u);
    var out: VsOut;
    out.uv = vec2<f32>(x, y);
    out.pos = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
    return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
    let tex_dims = vec2<f32>(textureDimensions(src_tex));
    let texel = vec2<f32>(1.0) / max(vec2<f32>(1.0), tex_dims);
    let base_uv = in.pos.xy * texel;
    let min_uv = vec2<f32>(0.5) * texel;
    let max_uv = max(min_uv, (u.viewport_size_phys - vec2<f32>(0.5)) * texel);

    let radius = max(0.0, u.params.x);
    if (radius <= 0.25) {
        let a = textureSampleLevel(src_tex, linear_sampler, clamp(base_uv, min_uv, max_uv), 0.0).a;
        return vec4<f32>(0.0, 0.0, 0.0, a);
    }

    // Clamp tap count to 16 symmetric pairs (33 taps) with adaptive stride for huge blur radii
    let sigma = max(0.5, radius * 0.45);
    let inv_two_sigma2 = 0.5 / (sigma * sigma);
    let max_taps = 16;
    let step_px = max(1.0, radius / f32(max_taps));
    let dir_uv = u.direction * texel;

    var sum_a = textureSampleLevel(src_tex, linear_sampler, clamp(base_uv, min_uv, max_uv), 0.0).a;
    var sum_w = 1.0;

    for (var i: i32 = 1; i <= max_taps; i = i + 1) {
        let dist = f32(i) * step_px;
        if (dist > radius + step_px * 0.5) {
            break;
        }
        let w = exp(-(dist * dist) * inv_two_sigma2);
        let off = dir_uv * dist;
        let uv_pos = base_uv + off;
        let uv_neg = base_uv - off;
        let a_pos = select(
            0.0,
            textureSampleLevel(src_tex, linear_sampler, uv_pos, 0.0).a,
            uv_pos.x >= 0.0 && uv_pos.y >= 0.0 && uv_pos.x <= max_uv.x + texel.x * 0.5 && uv_pos.y <= max_uv.y + texel.y * 0.5,
        );
        let a_neg = select(
            0.0,
            textureSampleLevel(src_tex, linear_sampler, uv_neg, 0.0).a,
            uv_neg.x >= 0.0 && uv_neg.y >= 0.0 && uv_neg.x <= max_uv.x + texel.x * 0.5 && uv_neg.y <= max_uv.y + texel.y * 0.5,
        );
        sum_a += (a_pos + a_neg) * w;
        sum_w += 2.0 * w;
    }

    let blurred_a = clamp(sum_a / max(0.0001, sum_w), 0.0, 1.0);
    return vec4<f32>(0.0, 0.0, 0.0, blurred_a);
}
"#;

/// Pass 1 of separable 2-pass exact Euclidean Distance Transform (Horizontal 1D distance scan).
/// Outputs `rg = (dx_to_inside, dx_to_outside)` in physical pixels.
pub const STROKE_SDF_HORIZ_WGSL: &str = r#"
struct BlurUniforms {
    viewport_size_phys: vec2<f32>,
    direction: vec2<f32>,
    params: vec4<f32>, // x = radius_px
};

@group(0) @binding(0) var<uniform> u: BlurUniforms;
@group(0) @binding(1) var src_tex: texture_2d<f32>;
@group(0) @binding(2) var linear_sampler: sampler;

struct VsOut {
    @builtin(position) pos: vec4<f32>,
    @location(0) uv: vec2<f32>,
};

@vertex
fn vs_main(@builtin(vertex_index) vid: u32) -> VsOut {
    let x = f32((vid << 1u) & 2u);
    let y = f32(vid & 2u);
    var out: VsOut;
    out.uv = vec2<f32>(x, y);
    out.pos = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
    return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
    let vp_w = max(1, i32(u.viewport_size_phys.x));
    let vp_h = max(1, i32(u.viewport_size_phys.y));
    let px = clamp(vec2<i32>(in.pos.xy), vec2<i32>(0), vec2<i32>(vp_w - 1, vp_h - 1));
    let center_in = textureLoad(src_tex, px, 0).a >= 0.5;

    let radius = clamp(u.params.x, 0.5, 128.0);
    let max_steps = 48;
    let step_px = max(1.0, ceil(radius) / f32(max_steps));
    let steps = min(max_steps, i32(ceil(radius / step_px)) + 1);

    let inf_d = 1024.0;
    if (center_in) {
        var dx_out = inf_d;
        for (var i: i32 = 1; i <= steps; i = i + 1) {
            let d = f32(i) * step_px;
            let di = i32(round(d));
            let xr = px.x + di;
            let xl = px.x - di;
            let out_r = xr >= vp_w || textureLoad(src_tex, vec2<i32>(xr, px.y), 0).a < 0.5;
            let out_l = xl < 0 || textureLoad(src_tex, vec2<i32>(xl, px.y), 0).a < 0.5;
            if (out_r || out_l) {
                dx_out = d;
                break;
            }
        }
        return vec4<f32>(0.0, dx_out, 0.0, 1.0);
    } else {
        var dx_in = inf_d;
        for (var i: i32 = 1; i <= steps; i = i + 1) {
            let d = f32(i) * step_px;
            let di = i32(round(d));
            let xr = px.x + di;
            let xl = px.x - di;
            let in_r = xr < vp_w && textureLoad(src_tex, vec2<i32>(xr, px.y), 0).a >= 0.5;
            let in_l = xl >= 0 && textureLoad(src_tex, vec2<i32>(xl, px.y), 0).a >= 0.5;
            if (in_r || in_l) {
                dx_in = d;
                break;
            }
        }
        return vec4<f32>(dx_in, 0.0, 0.0, 1.0);
    }
}
"#;

/// Pass 2 of separable 2-pass exact Euclidean Distance Transform (Vertical parabolic minimization).
/// Outputs `rg = (euclid_dist_to_inside, euclid_dist_to_outside)` in physical pixels.
pub const STROKE_SDF_VERT_WGSL: &str = r#"
struct BlurUniforms {
    viewport_size_phys: vec2<f32>,
    direction: vec2<f32>,
    params: vec4<f32>, // x = radius_px
};

@group(0) @binding(0) var<uniform> u: BlurUniforms;
@group(0) @binding(1) var src_tex: texture_2d<f32>;
@group(0) @binding(2) var linear_sampler: sampler;

struct VsOut {
    @builtin(position) pos: vec4<f32>,
    @location(0) uv: vec2<f32>,
};

@vertex
fn vs_main(@builtin(vertex_index) vid: u32) -> VsOut {
    let x = f32((vid << 1u) & 2u);
    let y = f32(vid & 2u);
    var out: VsOut;
    out.uv = vec2<f32>(x, y);
    out.pos = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
    return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
    let vp_w = max(1, i32(u.viewport_size_phys.x));
    let vp_h = max(1, i32(u.viewport_size_phys.y));
    let px = clamp(vec2<i32>(in.pos.xy), vec2<i32>(0), vec2<i32>(vp_w - 1, vp_h - 1));

    let radius = clamp(u.params.x, 0.5, 128.0);
    let max_steps = 48;
    let step_px = max(1.0, ceil(radius) / f32(max_steps));
    let steps = min(max_steps, i32(ceil(radius / step_px)) + 1);

    let c = textureLoad(src_tex, px, 0).rg;
    var min_in_sq = c.x * c.x;
    var min_out_sq = c.y * c.y;

    for (var i: i32 = 1; i <= steps; i = i + 1) {
        let dy = f32(i) * step_px;
        let dy2 = dy * dy;
        if (dy2 >= min_in_sq && dy2 >= min_out_sq) {
            break;
        }
        let dyi = i32(round(dy));

        let yp = px.y + dyi;
        if (yp < vp_h) {
            let hp = textureLoad(src_tex, vec2<i32>(px.x, yp), 0).rg;
            min_in_sq = min(min_in_sq, hp.x * hp.x + dy2);
            min_out_sq = min(min_out_sq, hp.y * hp.y + dy2);
        } else {
            min_out_sq = min(min_out_sq, dy2);
        }

        let yn = px.y - dyi;
        if (yn >= 0) {
            let hn = textureLoad(src_tex, vec2<i32>(px.x, yn), 0).rg;
            min_in_sq = min(min_in_sq, hn.x * hn.x + dy2);
            min_out_sq = min(min_out_sq, hn.y * hn.y + dy2);
        } else {
            min_out_sq = min(min_out_sq, dy2);
        }
    }

    return vec4<f32>(sqrt(min_in_sq), sqrt(min_out_sq), 0.0, 1.0);
}
"#;

pub const LAYER_STYLES_COMPOSITE_WGSL: &str = r#"
struct LayerStylesUniforms {
    viewport_size_phys: vec2<f32>,
    shadow_offset_px: vec2<f32>,
    // params0 = [layer_opacity, stroke_size_px, glow_blur_px, glow_spread]
    params0: vec4<f32>,
    // params1 = [shadow_blur_px, shadow_spread, _pad, _pad]
    params1: vec4<f32>,
    // flags = [stroke_enabled, stroke_position (0=out,1=in,2=center), glow_enabled, shadow_enabled]
    flags: vec4<u32>,
    stroke_color: vec4<f32>, // rgb + stroke opacity
    glow_color: vec4<f32>,   // rgb + glow opacity
    shadow_color: vec4<f32>, // rgb + shadow opacity
};

@group(0) @binding(0) var<uniform> u: LayerStylesUniforms;
@group(0) @binding(1) var layer_tex: texture_2d<f32>;
@group(0) @binding(2) var shadow_blur_tex: texture_2d<f32>;
@group(0) @binding(3) var glow_blur_tex: texture_2d<f32>;
@group(0) @binding(4) var linear_sampler: sampler;
@group(0) @binding(5) var stroke_sdf_tex: texture_2d<f32>;

struct VsOut {
    @builtin(position) pos: vec4<f32>,
    @location(0) uv: vec2<f32>,
};

@vertex
fn vs_main(@builtin(vertex_index) vid: u32) -> VsOut {
    let x = f32((vid << 1u) & 2u);
    let y = f32(vid & 2u);
    var out: VsOut;
    out.uv = vec2<f32>(x, y);
    out.pos = vec4<f32>(x * 2.0 - 1.0, 1.0 - y * 2.0, 0.0, 1.0);
    return out;
}

fn compute_stroke_coverage(
    px_coord: vec2<i32>,
    center_alpha: f32,
    radius_px: f32,
    position: u32,
) -> f32 {
    if (radius_px <= 0.0) {
        return 0.0;
    }

    let sdf = textureLoad(stroke_sdf_tex, px_coord, 0).rg;

    // Outside stroke (0)
    if (position == 0u) {
        if (center_alpha >= 0.99) {
            return 0.0;
        }
        let min_inside_dist = sdf.r;
        if (min_inside_dist <= radius_px) {
            return clamp(radius_px - min_inside_dist + 1.0, 0.0, 1.0) * (1.0 - center_alpha);
        }
        return 0.0;
    }

    // Inside stroke (1)
    if (position == 1u) {
        if (center_alpha < 0.01) {
            return 0.0;
        }
        let min_outside_dist = sdf.g;
        if (min_outside_dist <= radius_px) {
            return clamp(radius_px - min_outside_dist + 1.0, 0.0, 1.0) * center_alpha;
        }
        return 0.0;
    }

    // Center stroke (2): half outside, half inside
    let half_r = max(0.5, radius_px * 0.5);
    let dist = select(sdf.g, sdf.r, center_alpha < 0.5);
    if (dist <= half_r) {
        return clamp(half_r - dist + 1.0, 0.0, 1.0);
    }
    return 0.0;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
    let px_coord = clamp(
        vec2<i32>(in.pos.xy),
        vec2<i32>(0),
        max(vec2<i32>(0), vec2<i32>(u.viewport_size_phys) - vec2<i32>(1)),
    );
    let center_sample = textureLoad(layer_tex, px_coord, 0);
    let center_alpha = clamp(center_sample.a, 0.0, 1.0);

    // 1. Drop Shadow (sampled from separable Gaussian blur pass shifted by shadow_offset_px)
    var shadow_alpha = 0.0;
    if (u.flags.w != 0u) {
        let shadow_pos = in.pos.xy - u.shadow_offset_px;
        if (
            shadow_pos.x >= 0.0 && shadow_pos.x <= u.viewport_size_phys.x &&
            shadow_pos.y >= 0.0 && shadow_pos.y <= u.viewport_size_phys.y
        ) {
            var raw_a: f32;
            if (u.params1.x <= 0.25) {
                let l_dims = max(vec2<f32>(1.0), vec2<f32>(textureDimensions(layer_tex)));
                let l_uv = clamp(shadow_pos / l_dims, vec2<f32>(0.5) / l_dims, (u.viewport_size_phys - vec2<f32>(0.5)) / l_dims);
                raw_a = textureSampleLevel(layer_tex, linear_sampler, l_uv, 0.0).a;
            } else {
                let s_dims = max(vec2<f32>(1.0), vec2<f32>(textureDimensions(shadow_blur_tex)));
                let s_uv = clamp(shadow_pos / s_dims, vec2<f32>(0.5) / s_dims, (u.viewport_size_phys - vec2<f32>(0.5)) / s_dims);
                raw_a = textureSampleLevel(shadow_blur_tex, linear_sampler, s_uv, 0.0).a;
            }
            let spread_factor = max(0.01, 1.0 - u.params1.y * 0.95);
            shadow_alpha = clamp(raw_a / spread_factor, 0.0, 1.0) * u.shadow_color.a;
        }
    }

    // 2. Outer Glow (sampled from separable Gaussian blur pass)
    var glow_alpha = 0.0;
    if (u.flags.z != 0u && center_alpha < 0.99) {
        let g_dims = max(vec2<f32>(1.0), vec2<f32>(textureDimensions(glow_blur_tex)));
        let g_uv = clamp(in.pos.xy / g_dims, vec2<f32>(0.5) / g_dims, (u.viewport_size_phys - vec2<f32>(0.5)) / g_dims);
        let raw_g = textureSampleLevel(glow_blur_tex, linear_sampler, g_uv, 0.0).a;
        let spread_factor = max(0.01, 1.0 - u.params0.w * 0.95);
        let glow_val = clamp(raw_g / spread_factor, 0.0, 1.0);
        glow_alpha = glow_val * (1.0 - center_alpha) * u.glow_color.a;
    }

    // 3. Stroke (O(1) lookup from 2-pass separable exact Euclidean SDF)
    var stroke_cov = 0.0;
    if (u.flags.x != 0u) {
        stroke_cov = compute_stroke_coverage(px_coord, center_alpha, u.params0.y, u.flags.y);
    }

    // Standard Photoshop / InkStudio stacking order:
    // Drop Shadow -> Outer Glow -> Outside/Outer-Center Stroke -> Layer Body -> Inside/Inner-Center Stroke
    var composite = vec4<f32>(0.0);

    if (u.flags.w != 0u && shadow_alpha > 0.001) {
        composite = vec4<f32>(u.shadow_color.rgb * shadow_alpha, shadow_alpha);
    }

    if (u.flags.z != 0u && glow_alpha > 0.001) {
        let glow = vec4<f32>(u.glow_color.rgb * glow_alpha, glow_alpha);
        composite = glow + composite * (1.0 - glow.a);
    }

    let is_outside_or_center = (u.flags.y == 0u) || (u.flags.y == 2u && center_alpha < 0.5);
    if (u.flags.x != 0u && is_outside_or_center && stroke_cov > 0.001) {
        let s_a = stroke_cov * u.stroke_color.a;
        let strk = vec4<f32>(u.stroke_color.rgb * s_a, s_a);
        composite = strk + composite * (1.0 - strk.a);
    }

    if (center_sample.a > 0.001) {
        composite = center_sample + composite * (1.0 - center_sample.a);
    }

    let is_inside_or_center = (u.flags.y == 1u) || (u.flags.y == 2u && center_alpha >= 0.5);
    if (u.flags.x != 0u && is_inside_or_center && stroke_cov > 0.001) {
        let s_a = stroke_cov * u.stroke_color.a;
        let strk = vec4<f32>(u.stroke_color.rgb * s_a, s_a);
        composite = strk + composite * (1.0 - strk.a);
    }

    composite *= u.params0.x;
    if (composite.a < 0.0005) {
        discard;
    }
    return composite;
}
"#;