Skip to content

Free open-source image editor for the browser

1.9 KB
Raw Blame
import { linearToSrgb3, srgbToLinear3 } from "@vgpu/wgsl-std/color";
import { blendColors } from "../../core/image/blend.wgsl";
import { srgbToRec2020 } from "../../core/image/color.wgsl";

// Lays premultiplied sRGB paint over the image with a Photoshop blend, mixing by the paint's alpha on
// encoded values as Photoshop does. Unpainted pixels pass through, and so does HDR headroom under thin
// paint. The raster covers the source, sampled by position so any proxy resolution reads the same paint.
// The layer's open stroke, not in the raster yet, lays over it from the stroke buffer's coverage.
struct Params {
  blend: u32,
  // 0 when the stroke buffer holds no stroke of this layer, 1 when it paints `strokeColor`, 2 when it erases.
  stroke: u32,
  strokeColor: vec3f,
}
@group(0) @binding(0) var source: texture_2d<f32>;
@group(0) @binding(1) var paint: texture_2d<f32>;
@group(0) @binding(2) var stroke: texture_2d<f32>;
@group(0) @binding(3) var paintSampler: sampler;
@group(0) @binding(4) var<uniform> params: Params;

@fragment fn fs_main(@builtin(position) position: vec4f) -> @location(0) vec4f {
  let input = textureLoad(source, vec2i(position.xy), 0);
  let uv = position.xy / vec2f(textureDimensions(source));
  var painted = textureSampleLevel(paint, paintSampler, uv, 0.0);
  if (params.stroke != 0u) {
    let coverage = textureSampleLevel(stroke, paintSampler, uv, 0.0).r;
    let laid = select(vec4f(0.0), vec4f(params.strokeColor, 1.0), params.stroke == 1u);
    painted = mix(painted, laid, coverage);
  }
  if (painted.a <= 0.0) {
    return input;
  }
  let color = linearToSrgb3(clamp(srgbToRec2020 * srgbToLinear3(painted.rgb / painted.a), vec3f(0.0), vec3f(1.0)));
  let base = linearToSrgb3(max(input.rgb, vec3f(0.0)));
  let blended = clamp(blendColors(min(base, vec3f(1.0)), color, params.blend), vec3f(0.0), vec3f(1.0));
  return vec4f(srgbToLinear3(mix(base, blended, painted.a)), input.a);
}