Skip to content

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

20.3 KB
Raw Blame
//! Camera-projected GPU live stroke rasterizer, sparse GPU stroke tile projector,
//! and full 32-mode in-stack active-layer merger.
//!
//! 1. `SPARSE_STROKE_PROJECT_WGSL`: Projects active `256×256` `Rgba16Float` sparse GPU
//!    stroke tiles from `GpuBrushEngine::active_stroke_atlas()` into `live_stroke_surface`
//!    in a single instanced draw call (zero 8K FBO!).
//! 2. `LIVE_DAB_VIEWPORT_WGSL`: Legacy radial live dab shader retained for Phase 3 tests.
//! 3. `LIVE_STROKE_MERGE_WGSL`: Merges `active_layer_base_surface` + `live_stroke_surface`
//!    in-stack with ALL 32 `CspBrushCombineMode`s (all 28 blend modes + `add`, `behind`,
//!    `clear`, `compare-density`), `brush_opacity`, `is_eraser`, `is_bg_eraser`,
//!    `alpha_locked`, expression color, screentone, layer color tint, and layer mask
//!    so live strokes are 100% WYSIWYG beneath upper layers & blend modes!

use super::blend_modes::BLEND_MODES_WGSL;
use super::screentone::SCREENTONE_WGSL;

/// Instanced projector drawing sparse `256×256` `R8Unorm` layer mask tiles from
/// `GpuTilePool::mask_atlas` directly into a viewport-resolution `R8Unorm` mask target.
///
/// Uses `textureLoad` bilinear (or nearest when `u._pad > 0.5`) matching `sample_sparse_mask`
/// in `layer_tile.rs` identically so 4K–8K masks stay razor-sharp with zero pen-down snap!
pub const SPARSE_MASK_PROJECT_WGSL: &str = r#"
struct SparseMaskProjectUniforms {
    viewport_size_phys: vec2<f32>,
    doc_size: vec2<f32>,
    pan_css: vec2<f32>,
    zoom: f32,
    rotation_rad: f32,
    flip: vec2<f32>,
    dpr: f32,
    use_nearest: f32,
};

@group(0) @binding(0) var<uniform> u: SparseMaskProjectUniforms;
@group(0) @binding(1) var mask_atlas: texture_2d_array<f32>;

struct SparseTileInst {
    @location(0) tile_origin_doc: vec2<f32>,
    @location(1) slot_f: f32,
    @location(2) _pad: f32,
};

struct VsOut {
    @builtin(position) pos: vec4<f32>,
    @location(0) uv: vec2<f32>,
    @location(1) doc_coord: vec2<f32>,
    @location(2) @interpolate(flat) slot_i: i32,
};

@vertex
fn vs_main(@builtin(vertex_index) vid: u32, inst: SparseTileInst) -> VsOut {
    var uv: vec2<f32>;
    switch (vid) {
        case 0u: { uv = vec2<f32>(0.0, 0.0); }
        case 1u: { uv = vec2<f32>(1.0, 0.0); }
        case 2u: { uv = vec2<f32>(0.0, 1.0); }
        case 3u: { uv = vec2<f32>(0.0, 1.0); }
        case 4u: { uv = vec2<f32>(1.0, 0.0); }
        default: { uv = vec2<f32>(1.0, 1.0); }
    }

    let doc_pos = inst.tile_origin_doc + uv * 256.0;
    let centered = (doc_pos - u.doc_size * 0.5) * u.flip;
    let cos_r = cos(u.rotation_rad);
    let sin_r = sin(u.rotation_rad);
    let rotated = vec2<f32>(
        centered.x * cos_r - centered.y * sin_r,
        centered.x * sin_r + centered.y * cos_r,
    );
    let screen_phys = (u.pan_css + rotated * u.zoom) * u.dpr;
    let ndc = vec2<f32>(
        (2.0 * screen_phys.x / max(1.0, u.viewport_size_phys.x)) - 1.0,
        1.0 - (2.0 * screen_phys.y / max(1.0, u.viewport_size_phys.y)),
    );

    var out: VsOut;
    out.pos = vec4<f32>(ndc, 0.0, 1.0);
    out.uv = uv;
    out.doc_coord = doc_pos;
    out.slot_i = i32(round(inst.slot_f));
    return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
    if (
        in.doc_coord.x < 0.0 ||
        in.doc_coord.y < 0.0 ||
        in.doc_coord.x >= u.doc_size.x ||
        in.doc_coord.y >= u.doc_size.y
    ) {
        discard;
    }
    var m: f32;
    if (u.use_nearest > 0.5) {
        let coord = clamp(vec2<i32>(floor(in.uv * 256.0)), vec2<i32>(0), vec2<i32>(255));
        m = textureLoad(mask_atlas, coord, in.slot_i, 0).r;
    } else {
        let p = in.uv * 256.0 - vec2<f32>(0.5);
        let p_floor = floor(p);
        let p_i = vec2<i32>(p_floor);
        let i0 = clamp(p_i, vec2<i32>(0), vec2<i32>(255));
        let i1 = clamp(p_i + vec2<i32>(1), vec2<i32>(0), vec2<i32>(255));
        let f = clamp(p - p_floor, vec2<f32>(0.0), vec2<f32>(1.0));
        let m00 = textureLoad(mask_atlas, i0, in.slot_i, 0).r;
        let m10 = textureLoad(mask_atlas, vec2<i32>(i1.x, i0.y), in.slot_i, 0).r;
        let m01 = textureLoad(mask_atlas, vec2<i32>(i0.x, i1.y), in.slot_i, 0).r;
        let m11 = textureLoad(mask_atlas, i1, in.slot_i, 0).r;
        m = mix(mix(m00, m10, f.x), mix(m01, m11, f.x), f.y);
    }
    return vec4<f32>(m, 0.0, 0.0, 1.0);
}
"#;

/// Instanced projector drawing active `256×256` `Rgba16Float` (premultiplied) sparse GPU
/// stroke tiles into the viewport `live_stroke_surface` with exact camera transform.
pub const SPARSE_STROKE_PROJECT_WGSL: &str = r#"
struct SparseStrokeProjectUniforms {
    viewport_size_phys: vec2<f32>,
    doc_size: vec2<f32>,
    pan_css: vec2<f32>,
    zoom: f32,
    rotation_rad: f32,
    flip: vec2<f32>,
    dpr: f32,
    _pad: f32,
};

@group(0) @binding(0) var<uniform> u: SparseStrokeProjectUniforms;
@group(0) @binding(1) var stroke_atlas: texture_2d_array<f32>;
@group(0) @binding(2) var stroke_sampler: sampler;

struct SparseTileInst {
    @location(0) tile_origin_doc: vec2<f32>,
    @location(1) slot_f: f32,
    @location(2) _pad: f32,
};

struct VsOut {
    @builtin(position) pos: vec4<f32>,
    @location(0) uv: vec2<f32>,
    @location(1) doc_coord: vec2<f32>,
    @location(2) @interpolate(flat) slot_i: i32,
};

@vertex
fn vs_main(@builtin(vertex_index) vid: u32, inst: SparseTileInst) -> VsOut {
    var uv: vec2<f32>;
    switch (vid) {
        case 0u: { uv = vec2<f32>(0.0, 0.0); }
        case 1u: { uv = vec2<f32>(1.0, 0.0); }
        case 2u: { uv = vec2<f32>(0.0, 1.0); }
        case 3u: { uv = vec2<f32>(0.0, 1.0); }
        case 4u: { uv = vec2<f32>(1.0, 0.0); }
        default: { uv = vec2<f32>(1.0, 1.0); }
    }

    let doc_pos = inst.tile_origin_doc + uv * 256.0;
    let centered = (doc_pos - u.doc_size * 0.5) * u.flip;
    let cos_r = cos(u.rotation_rad);
    let sin_r = sin(u.rotation_rad);
    let rotated = vec2<f32>(
        centered.x * cos_r - centered.y * sin_r,
        centered.x * sin_r + centered.y * cos_r,
    );
    let screen_phys = (u.pan_css + rotated * u.zoom) * u.dpr;
    let ndc = vec2<f32>(
        (2.0 * screen_phys.x / max(1.0, u.viewport_size_phys.x)) - 1.0,
        1.0 - (2.0 * screen_phys.y / max(1.0, u.viewport_size_phys.y)),
    );

    var out: VsOut;
    out.pos = vec4<f32>(ndc, 0.0, 1.0);
    out.uv = uv;
    out.doc_coord = doc_pos;
    out.slot_i = i32(round(inst.slot_f));
    return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
    if (
        in.doc_coord.x < 0.0 ||
        in.doc_coord.y < 0.0 ||
        in.doc_coord.x >= u.doc_size.x ||
        in.doc_coord.y >= u.doc_size.y
    ) {
        discard;
    }
    // When u._pad > 0.5, tiles carry straight RGBA16F complete layer replacements (Phase 5 Natural Paint / Color Mixing):
    // Use halo-free premultiplied 2x2 bilinear interpolation (matching layer_tile.rs) instead of
    // hardware bilinear sampling straight-alpha tiles before premultiplying (which creates dark edge halos).
    if (u._pad > 0.5) {
        let p = in.uv * 256.0 - vec2<f32>(0.5);
        let p_floor = floor(p);
        let p_i = vec2<i32>(p_floor);
        let i0 = clamp(p_i, vec2<i32>(0), vec2<i32>(255));
        let i1 = clamp(p_i + vec2<i32>(1), vec2<i32>(0), vec2<i32>(255));
        let f = clamp(p - p_floor, vec2<f32>(0.0), vec2<f32>(1.0));

        let c00 = textureLoad(stroke_atlas, i0, in.slot_i, 0);
        let c10 = textureLoad(stroke_atlas, vec2<i32>(i1.x, i0.y), in.slot_i, 0);
        let c01 = textureLoad(stroke_atlas, vec2<i32>(i0.x, i1.y), in.slot_i, 0);
        let c11 = textureLoad(stroke_atlas, i1, in.slot_i, 0);

        let a00 = clamp(c00.a, 0.0, 1.0);
        let a10 = clamp(c10.a, 0.0, 1.0);
        let a01 = clamp(c01.a, 0.0, 1.0);
        let a11 = clamp(c11.a, 0.0, 1.0);

        let p00 = vec4<f32>(clamp(c00.rgb, vec3<f32>(0.0), vec3<f32>(1.0)) * a00, a00);
        let p10 = vec4<f32>(clamp(c10.rgb, vec3<f32>(0.0), vec3<f32>(1.0)) * a10, a10);
        let p01 = vec4<f32>(clamp(c01.rgb, vec3<f32>(0.0), vec3<f32>(1.0)) * a01, a01);
        let p11 = vec4<f32>(clamp(c11.rgb, vec3<f32>(0.0), vec3<f32>(1.0)) * a11, a11);

        let top = mix(p00, p10, f.x);
        let bot = mix(p01, p11, f.x);
        return mix(top, bot, f.y);
    }
    let sample_uv = clamp(in.uv, vec2<f32>(0.5 / 256.0), vec2<f32>(255.5 / 256.0));
    let px = textureSampleLevel(stroke_atlas, stroke_sampler, sample_uv, in.slot_i, 0.0);
    if (px.a <= 0.0005) {
        discard;
    }
    return px;
}
"#;

pub const LIVE_DAB_VIEWPORT_WGSL: &str = r#"
struct LiveDabUniforms {
    viewport_size_phys: vec2<f32>,
    doc_size: vec2<f32>,
    pan_css: vec2<f32>,
    zoom: f32,
    rotation_rad: f32,
    flip: vec2<f32>,
    dpr: f32,
    _pad: f32,
};

@group(0) @binding(0) var<uniform> u: LiveDabUniforms;

struct DabInstance {
    @location(0) center_doc: vec2<f32>,
    @location(1) radius_doc: f32,
    @location(2) hardness: f32,
    @location(3) color_rgba: vec4<f32>,
};

struct VsOut {
    @builtin(position) pos: vec4<f32>,
    @location(0) local_doc: vec2<f32>,
    @location(1) doc_coord: vec2<f32>,
    @location(2) radius_doc: f32,
    @location(3) hardness: f32,
    @location(4) color_rgba: vec4<f32>,
};

@vertex
fn vs_main(@builtin(vertex_index) vid: u32, inst: DabInstance) -> VsOut {
    var corner: vec2<f32>;
    switch (vid) {
        case 0u: { corner = vec2<f32>(-1.0, -1.0); }
        case 1u: { corner = vec2<f32>( 1.0, -1.0); }
        case 2u: { corner = vec2<f32>(-1.0,  1.0); }
        case 3u: { corner = vec2<f32>(-1.0,  1.0); }
        case 4u: { corner = vec2<f32>( 1.0, -1.0); }
        default: { corner = vec2<f32>( 1.0,  1.0); }
    }

    let extent = inst.radius_doc + max(1.5, 1.5 / max(0.05, u.zoom));
    let local_doc = corner * extent;
    let doc_pos = inst.center_doc + local_doc;

    let centered = (doc_pos - u.doc_size * 0.5) * u.flip;
    let cos_r = cos(u.rotation_rad);
    let sin_r = sin(u.rotation_rad);
    let rotated = vec2<f32>(
        centered.x * cos_r - centered.y * sin_r,
        centered.x * sin_r + centered.y * cos_r,
    );
    let screen_phys = (u.pan_css + rotated * u.zoom) * u.dpr;
    let ndc = vec2<f32>(
        (2.0 * screen_phys.x / max(1.0, u.viewport_size_phys.x)) - 1.0,
        1.0 - (2.0 * screen_phys.y / max(1.0, u.viewport_size_phys.y)),
    );

    var out: VsOut;
    out.pos = vec4<f32>(ndc, 0.0, 1.0);
    out.local_doc = local_doc;
    out.doc_coord = doc_pos;
    out.radius_doc = inst.radius_doc;
    out.hardness = inst.hardness;
    out.color_rgba = inst.color_rgba;
    return out;
}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {
    if (
        in.doc_coord.x < 0.0 ||
        in.doc_coord.y < 0.0 ||
        in.doc_coord.x > u.doc_size.x ||
        in.doc_coord.y > u.doc_size.y
    ) {
        discard;
    }

    let dist = length(in.local_doc);
    let r = max(0.35, in.radius_doc);
    if (dist > r + 1.0) {
        discard;
    }

    let h = clamp(in.hardness, 0.0, 1.0);
    let inner_r = r * h * 0.96;
    let aa_edge = max(0.75, r - inner_r);
    let falloff = 1.0 - smoothstep(max(0.0, r - aa_edge), r + 0.5, dist);
    let alpha = clamp(in.color_rgba.a * falloff, 0.0, 1.0);
    if (alpha < 0.002) {
        discard;
    }
    return vec4<f32>(in.color_rgba.rgb * alpha, alpha);
}
"#;

pub fn build_live_stroke_merge_wgsl() -> String {
    format!(
        r#"
struct LiveStrokeMergeUniforms {{
    viewport_size_phys: vec2<f32>,
    doc_size: vec2<f32>,
    pan_css: vec2<f32>,
    zoom: f32,
    rotation_rad: f32,
    flip: vec2<f32>,
    dpr: f32,
    layer_opacity: f32,
    // flags0 = [is_eraser, alpha_locked, mask_enabled, combine_mode]
    flags0: vec4<u32>,
    // expr_flags = [expression_mode, mono_express_black, mono_express_white, layer_color_enabled]
    expr_flags: vec4<u32>,
    // tone_flags = [tone_enabled, tone_dot_shape, tone_density_type, is_bg_eraser]
    tone_flags: vec4<u32>,
    // tone_params = [tone_frequency, tone_angle_rad, tone_dpi, tone_density]
    tone_params: vec4<f32>,
    layer_color_and_mono: vec4<f32>,
    layer_sub_color: vec4<f32>,
    // brush_params = [bg_r, bg_g, bg_b, brush_opacity]
    brush_params: vec4<f32>,
}};

@group(0) @binding(0) var<uniform> u: LiveStrokeMergeUniforms;
@group(0) @binding(1) var base_layer_tex: texture_2d<f32>;
@group(0) @binding(2) var stroke_tex: texture_2d<f32>;
@group(0) @binding(3) var doc_mask_tex: texture_2d<f32>;
@group(0) @binding(4) var linear_sampler: sampler;
@group(0) @binding(5) var sel_vp_mask_tex: texture_2d<f32>;

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

fn phys_to_doc(phys: vec2<f32>) -> vec2<f32> {{
    let safe_dpr = max(0.05, u.dpr);
    let safe_zoom = max(0.0001, u.zoom);
    let css = phys / safe_dpr;
    let rel = (css - u.pan_css) / safe_zoom;
    let c = cos(u.rotation_rad);
    let s = sin(u.rotation_rad);
    let unrot = vec2<f32>(
        rel.x * c + rel.y * s,
        -rel.x * s + rel.y * c,
    );
    return unrot * u.flip + u.doc_size * 0.5;
}}

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

{BLEND_MODES_WGSL}

{SCREENTONE_WGSL}

@fragment
fn fs_main(in: VsOut) -> @location(0) vec4<f32> {{
    let d_doc_x = dpdx(in.doc_coord);
    let d_doc_y = dpdy(in.doc_coord);

    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 base_premul = textureLoad(base_layer_tex, px_coord, 0);
    let stroke_raw = textureLoad(stroke_tex, px_coord, 0);
    var sel_a = 1.0;
    if (u.layer_sub_color.w > 0.5) {{
        sel_a = textureLoad(sel_vp_mask_tex, px_coord, 0).r;
    }}
    let brush_opacity = clamp(u.brush_params.w, 0.0, 1.0);
    let stroke_a = clamp(stroke_raw.a * brush_opacity * sel_a, 0.0, 1.0);

    var merged_premul = base_premul;
    let combine_mode = u.flags0.w;
    let is_eraser = u.flags0.x != 0u || combine_mode == 30u;
    let alpha_locked = u.flags0.y != 0u;
    let is_bg_eraser = u.tone_flags.w != 0u;

    if (combine_mode == 33u) {{
        // Live Layer Mask editing mode: keep layer RGB artwork intact and modulate mask alpha below!
        merged_premul = base_premul;
    }} else if (combine_mode == 32u) {{
        // Phase 5 Direct Layer Replacement mode (Natural Paint & Color Mixing):
        // `stroke_tex` already holds Base + Mixed Stroke in premultiplied Rgba16Float!
        merged_premul = stroke_raw;
    }} else if (stroke_a > 0.0005) {{
        let src_rgb = select(
            vec3<f32>(0.0),
            clamp(stroke_raw.rgb / max(1e-5, stroke_raw.a), vec3<f32>(0.0), vec3<f32>(1.0)),
            stroke_raw.a > 0.0005,
        );
        let dst_a = clamp(base_premul.a, 0.0, 1.0);
        let dst_rgb = select(
            vec3<f32>(0.0),
            clamp(base_premul.rgb / max(1e-5, dst_a), vec3<f32>(0.0), vec3<f32>(1.0)),
            dst_a > 0.0005,
        );

        if (is_bg_eraser) {{
            let bg_rgb = u.brush_params.xyz;
            if (alpha_locked) {{
                if (dst_a > 0.0005) {{
                    let out_rgb = mix(dst_rgb, bg_rgb, stroke_a);
                    merged_premul = vec4<f32>(out_rgb * dst_a, dst_a);
                }}
            }} else {{
                let out_a = clamp(stroke_a + dst_a * (1.0 - stroke_a), 0.0, 1.0);
                merged_premul = vec4<f32>(bg_rgb * stroke_a + base_premul.rgb * (1.0 - stroke_a), out_a);
            }}
        }} else if (is_eraser) {{
            if (!alpha_locked) {{
                let keep = clamp(1.0 - stroke_a, 0.0, 1.0);
                merged_premul = base_premul * keep;
            }}
        }} else if (combine_mode == 29u) {{
            // Behind (destination-over)
            if (!alpha_locked) {{
                let inv_da = 1.0 - dst_a;
                let out_a = clamp(dst_a + stroke_a * inv_da, 0.0, 1.0);
                merged_premul = vec4<f32>(base_premul.rgb + src_rgb * stroke_a * inv_da, out_a);
            }}
        }} else if (combine_mode == 31u) {{
            // Compare Density
            let target_a = select(max(dst_a, stroke_a), dst_a, alpha_locked);
            if (target_a > 0.0005) {{
                let blend_t = select(stroke_a / max(1e-5, target_a), stroke_a, alpha_locked);
                let out_rgb = mix(dst_rgb, src_rgb, clamp(blend_t, 0.0, 1.0));
                merged_premul = vec4<f32>(out_rgb * target_a, target_a);
            }}
        }} else if (combine_mode == 28u) {{
            // Add
            let out_a = select(clamp(stroke_a + dst_a * (1.0 - stroke_a), 0.0, 1.0), dst_a, alpha_locked);
            let add_rgb = clamp(base_premul.rgb + src_rgb * stroke_a, vec3<f32>(0.0), vec3<f32>(out_a));
            merged_premul = vec4<f32>(add_rgb, out_a);
        }} else {{
            let blended_rgb = select(
                evaluate_blend_mode(dst_rgb, src_rgb, combine_mode),
                src_rgb,
                combine_mode == 0u || dst_a <= 0.0005,
            );
            if (alpha_locked) {{
                if (dst_a > 0.0005) {{
                    let out_rgb = mix(dst_rgb, blended_rgb, stroke_a);
                    merged_premul = vec4<f32>(out_rgb * dst_a, dst_a);
                }}
            }} else {{
                let out_a = clamp(stroke_a + dst_a * (1.0 - stroke_a), 0.0, 1.0);
                let premul =
                    blended_rgb * (stroke_a * dst_a)
                    + src_rgb * (stroke_a * (1.0 - dst_a))
                    + dst_rgb * (dst_a * (1.0 - stroke_a));
                merged_premul = vec4<f32>(premul, out_a);
            }}
        }}
    }}

    if (merged_premul.a < 0.0005) {{
        return vec4<f32>(0.0);
    }}

    var final_color = vec4<f32>(
        clamp(merged_premul.rgb / merged_premul.a, vec3<f32>(0.0), vec3<f32>(1.0)),
        clamp(merged_premul.a, 0.0, 1.0),
    );

    // Apply expression color (0: color, 1: gray, 2: monochrome)
    let expr_mode = u.expr_flags.x;
    if (expr_mode == 1u) {{
        let g_lum = dot(final_color.rgb, vec3<f32>(0.299, 0.587, 0.114));
        final_color = vec4<f32>(vec3<f32>(g_lum), final_color.a);
    }} else if (expr_mode == 2u) {{
        let m_lum = dot(final_color.rgb, vec3<f32>(0.299, 0.587, 0.114));
        var mono_alpha = final_color.a;
        if (u.zoom >= 0.99) {{
            mono_alpha = step(0.5, final_color.a);
        }}
        if (m_lum < u.layer_color_and_mono.w) {{
            if (u.expr_flags.y != 0u) {{
                final_color = vec4<f32>(0.0, 0.0, 0.0, mono_alpha);
            }} else {{
                return vec4<f32>(0.0);
            }}
        }} else {{
            if (u.expr_flags.z != 0u) {{
                final_color = vec4<f32>(1.0, 1.0, 1.0, mono_alpha);
            }} else {{
                return vec4<f32>(0.0);
            }}
        }}
    }}

    var mask_a = 1.0;
    if (u.flags0.z != 0u) {{
        let mask_uv = clamp(in.pos.xy / max(vec2<f32>(1.0), u.viewport_size_phys), vec2<f32>(0.0), vec2<f32>(1.0));
        mask_a = textureSampleLevel(doc_mask_tex, linear_sampler, mask_uv, 0.0).r;
    }}
    if (combine_mode == 33u && stroke_a > 0.0005) {{
        let stroke_lum = select(
            0.0,
            clamp(stroke_raw.r / max(1e-5, stroke_raw.a), 0.0, 1.0),
            stroke_raw.a > 0.0005,
        );
        mask_a = mix(mask_a, stroke_lum, stroke_a);
    }}

    if (u.tone_flags.x != 0u) {{
        final_color.a *= mask_a * u.layer_opacity;
        final_color = evaluate_screentone(
            final_color,
            in.doc_coord,
            d_doc_x,
            d_doc_y,
            u.tone_params.x,
            u.tone_params.y,
            u.tone_params.z,
            u.tone_flags.y,
            u.tone_params.w,
            u.tone_flags.z,
            u.zoom,
        );
        if (u.expr_flags.w != 0u) {{
            let lum = dot(final_color.rgb, vec3<f32>(0.299, 0.587, 0.114));
            final_color = vec4<f32>(mix(u.layer_color_and_mono.rgb, u.layer_sub_color.rgb, lum), final_color.a);
        }}
    }} else {{
        if (u.expr_flags.w != 0u) {{
            let lum = dot(final_color.rgb, vec3<f32>(0.299, 0.587, 0.114));
            final_color = vec4<f32>(mix(u.layer_color_and_mono.rgb, u.layer_sub_color.rgb, lum), final_color.a);
        }}
        final_color.a *= mask_a * u.layer_opacity;
    }}

    if (final_color.a < 0.0005) {{
        return vec4<f32>(0.0);
    }}
    return vec4<f32>(final_color.rgb * final_color.a, final_color.a);
}}
"#
    )
}