From 2348776791e9dd17afeb12a06ba42a4a71917bcf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 30 Oct 2026 21:34:41 -0300 Subject: sketchbook: a drawing layer on top, host fixes for replay and migration --- src/vector/shaders/cover.frag | 65 ---------------------------------------- src/vector/shaders/paint.frag | 69 +++++++++++++++++++++++++++++++++++++++++++ src/vector/shaders/paint.vert | 15 ++++++++++ 3 files changed, 84 insertions(+), 65 deletions(-) delete mode 100644 src/vector/shaders/cover.frag create mode 100644 src/vector/shaders/paint.frag create mode 100644 src/vector/shaders/paint.vert (limited to 'src/vector/shaders') diff --git a/src/vector/shaders/cover.frag b/src/vector/shaders/cover.frag deleted file mode 100644 index a57a916..0000000 --- a/src/vector/shaders/cover.frag +++ /dev/null @@ -1,65 +0,0 @@ -#version 450 -#extension GL_GOOGLE_include_directive : require -// Paints the covered pixels. Mirrors vector/Paint.zig: `param` gives the -// gradient parameter, `stop_color` the color at it. The uniform block is -// generated from vector/layouts.zig. -layout(location = 0) out vec4 out_color; - -#include "Paint.glsl" - -float offset_at(int i) { - return paint.offsets[i / 4][i % 4]; -} - -vec4 premultiply(vec4 c) { - return vec4(c.rgb * c.a, c.a); -} - -float param(vec2 q) { - vec2 g = vec2(dot(paint.row0.xyz, vec3(q, 1.0)), dot(paint.row1.xyz, vec3(q, 1.0))); - vec2 p0 = paint.points.xy; - vec2 p1 = paint.points.zw; - float t = 0.0; - if (paint.row0.w < 1.5) { - vec2 dir = p1 - p0; - float len2 = dot(dir, dir); - t = len2 > 0.0 ? dot(g - p0, dir) / len2 : 0.0; - } else { - // Where the ray from the focus through g meets the circle. - vec2 dq = g - p1; - vec2 cf = p1 - p0; - float a = dot(dq, dq); - if (a > 0.0) { - float b = dot(dq, cf); - float c = dot(cf, cf) - paint.extra.x * paint.extra.x; - float s = (-b + sqrt(max(b * b - a * c, 0.0))) / a; - t = s > 0.0 ? 1.0 / s : 1.0; - } - } - if (paint.row1.w < 0.5) return clamp(t, 0.0, 1.0); - if (paint.row1.w < 1.5) { - float r = mod(t, 2.0); - return r > 1.0 ? 2.0 - r : r; - } - return t - floor(t); -} - -vec4 stop_color(float t) { - int count = int(paint.extra.z); - if (count == 0) return vec4(0.0); - if (t <= offset_at(0)) return premultiply(paint.colors[0]); - for (int i = 1; i < count; i++) { - float b = offset_at(i); - if (t <= b) { - float a = offset_at(i - 1); - float u = b > a ? (t - a) / (b - a) : 1.0; - return premultiply(mix(paint.colors[i - 1], paint.colors[i], u)); - } - } - return premultiply(paint.colors[count - 1]); -} - -void main() { - vec4 c = paint.row0.w < 0.5 ? paint.color : stop_color(param(gl_FragCoord.xy)); - out_color = c * paint.extra.y; -} diff --git a/src/vector/shaders/paint.frag b/src/vector/shaders/paint.frag new file mode 100644 index 0000000..97eb551 --- /dev/null +++ b/src/vector/shaders/paint.frag @@ -0,0 +1,69 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// Paints a fill: its solid color, or its gradient times its alpha. A draw +// holds many fills, so each vertex says which (paint.vert). Mirrors +// vector/Paint.zig: `param` gives the gradient parameter, `stop_color` the +// color at it. The Paint struct is generated from vector/layouts.zig. +layout(location = 0) flat in vec4 fill_color; +layout(location = 1) flat in int fill_gradient; +layout(location = 0) out vec4 out_color; + +#include "Paint.glsl" +layout(set = 2, binding = 0, std430) readonly buffer Paints { Paint paints[]; }; +#define paint paints[fill_gradient] + +float offset_at(int i) { + return paint.offsets[i / 4][i % 4]; +} + +vec4 premultiply(vec4 c) { + return vec4(c.rgb * c.a, c.a); +} + +float param(vec2 q) { + vec2 g = vec2(dot(paint.row0.xyz, vec3(q, 1.0)), dot(paint.row1.xyz, vec3(q, 1.0))); + vec2 p0 = paint.points.xy; + vec2 p1 = paint.points.zw; + float t = 0.0; + if (paint.row0.w < 1.5) { + vec2 dir = p1 - p0; + float len2 = dot(dir, dir); + t = len2 > 0.0 ? dot(g - p0, dir) / len2 : 0.0; + } else { + // Where the ray from the focus through g meets the circle. + vec2 dq = g - p1; + vec2 cf = p1 - p0; + float a = dot(dq, dq); + if (a > 0.0) { + float b = dot(dq, cf); + float c = dot(cf, cf) - paint.extra.x * paint.extra.x; + float s = (-b + sqrt(max(b * b - a * c, 0.0))) / a; + t = s > 0.0 ? 1.0 / s : 1.0; + } + } + if (paint.row1.w < 0.5) return clamp(t, 0.0, 1.0); + if (paint.row1.w < 1.5) { + float r = mod(t, 2.0); + return r > 1.0 ? 2.0 - r : r; + } + return t - floor(t); +} + +vec4 stop_color(float t) { + int count = int(paint.extra.z); + if (count == 0) return vec4(0.0); + if (t <= offset_at(0)) return premultiply(paint.colors[0]); + for (int i = 1; i < count; i++) { + float b = offset_at(i); + if (t <= b) { + float a = offset_at(i - 1); + float u = b > a ? (t - a) / (b - a) : 1.0; + return premultiply(mix(paint.colors[i - 1], paint.colors[i], u)); + } + } + return premultiply(paint.colors[count - 1]); +} + +void main() { + out_color = fill_gradient < 0 ? fill_color : stop_color(param(gl_FragCoord.xy)) * fill_color; +} diff --git a/src/vector/shaders/paint.vert b/src/vector/shaders/paint.vert new file mode 100644 index 0000000..89c2c82 --- /dev/null +++ b/src/vector/shaders/paint.vert @@ -0,0 +1,15 @@ +#version 450 +#extension GL_GOOGLE_include_directive : require +// Painted triangles: device pixels (y down) to normalized device +// coordinates (y up), as in path.vert, with the fill's paint passed on. +#include "Painted.glsl" +#include "Viewport.glsl" + +layout(location = 0) flat out vec4 fill_color; +layout(location = 1) flat out int fill_gradient; + +void main() { + gl_Position = vec4(position.x / viewport.size.x * 2.0 - 1.0, 1.0 - position.y / viewport.size.y * 2.0, 0.0, 1.0); + fill_color = color; + fill_gradient = int(gradient); +} -- cgit v1.3