1pub const WGSL_PATH_COMPUTE_FLATTEN_SHADER: &str = r#"
5struct RawCurveInput {
6 p0: vec2<f32>,
7 p1: vec2<f32>,
8 p2: vec2<f32>,
9 p3: vec2<f32>,
10 stroke_width: f32,
11 flags: u32,
12 z_index: f32,
13 _pad: f32,
14 color: vec4<f32>,
15};
16
17struct GpuPathSegment {
18 p0: vec2<f32>,
19 p1: vec2<f32>,
20 p2: vec2<f32>,
21 stroke_width: f32,
22 flags: u32,
23 color: vec4<f32>,
24 z_index: f32,
25 _pad0: f32,
26 _pad1: f32,
27 _pad2: f32,
28};
29
30struct ComputeUniforms {
31 viewport_size: vec2<f32>,
32 tolerance: f32,
33 curve_count: u32,
34};
35
36@group(0) @binding(0) var<uniform> uniforms: ComputeUniforms;
37@group(0) @binding(1) var<storage, read> input_curves: array<RawCurveInput>;
38@group(0) @binding(2) var<storage, read_write> output_segments: array<GpuPathSegment>;
39
40@compute @workgroup_size(64)
41fn cs_flatten_curves(@builtin(global_invocation_id) global_id: vec3<u32>) {
42 let index = global_id.x;
43 if (index >= uniforms.curve_count) {
44 return;
45 }
46 let curve = input_curves[index];
47
48 // Compute quadratic approximation control point: C = (3*P1 - P0 + 3*P2 - P3) / 4
49 let c = (3.0 * curve.p1 - curve.p0 + 3.0 * curve.p2 - curve.p3) * 0.25;
50
51 var out: GpuPathSegment;
52 out.p0 = curve.p0;
53 out.p1 = c;
54 out.p2 = curve.p3;
55 out.stroke_width = curve.stroke_width;
56 out.flags = curve.flags;
57 out.color = curve.color;
58 out.z_index = curve.z_index;
59 out._pad0 = 0.0;
60 out._pad1 = 0.0;
61 out._pad2 = 0.0;
62
63 output_segments[index] = out;
64}
65"#;
66
67pub const WGSL_ANALYTICAL_PATH_RASTER_SHADER: &str = r#"
69struct VertexInput {
70 @location(0) position: vec2<f32>, // Unit quad [-1, -1] to [1, 1]
71};
72
73struct InstanceInput {
74 @location(1) p0: vec2<f32>,
75 @location(2) p1: vec2<f32>,
76 @location(3) p2: vec2<f32>,
77 @location(4) stroke_width: f32,
78 @location(5) flags: u32,
79 @location(6) color: vec4<f32>,
80 @location(7) z_index: f32,
81};
82
83struct VertexOutput {
84 @builtin(position) clip_position: vec4<f32>,
85 @location(0) pixel_coords: vec2<f32>,
86 @location(1) p0: vec2<f32>,
87 @location(2) p1: vec2<f32>,
88 @location(3) p2: vec2<f32>,
89 @location(4) stroke_width: f32,
90 @location(5) @interpolate(flat) flags: u32,
91 @location(6) color: vec4<f32>,
92};
93
94struct Uniforms {
95 viewport_size: vec2<f32>,
96 _padding: vec2<f32>,
97};
98
99@group(0) @binding(0) var<uniform> uniforms: Uniforms;
100
101@vertex
102fn vs_main(model: VertexInput, instance: InstanceInput) -> VertexOutput {
103 var out: VertexOutput;
104
105 // Compute axis-aligned bounding box of the quadratic curve expanded by stroke width
106 let min_xy = min(min(instance.p0, instance.p1), instance.p2) - vec2<f32>(instance.stroke_width + 1.0);
107 let max_xy = max(max(instance.p0, instance.p1), instance.p2) + vec2<f32>(instance.stroke_width + 1.0);
108 let size = max_xy - min_xy;
109
110 // Transform unit quad to pixel coordinates
111 let norm_pos = model.position * 0.5 + 0.5;
112 let pixel_coords = min_xy + norm_pos * size;
113
114 let ndc_x = (pixel_coords.x / uniforms.viewport_size.x) * 2.0 - 1.0;
115 let ndc_y = 1.0 - (pixel_coords.y / uniforms.viewport_size.y) * 2.0;
116
117 out.clip_position = vec4<f32>(ndc_x, ndc_y, 0.0, 1.0);
118 out.pixel_coords = pixel_coords;
119 out.p0 = instance.p0;
120 out.p1 = instance.p1;
121 out.p2 = instance.p2;
122 out.stroke_width = instance.stroke_width;
123 out.flags = instance.flags;
124 out.color = instance.color;
125
126 return out;
127}
128
129// Distance from point P to quadratic Bézier curve segment B(t)
130fn sd_quadratic_bezier(p: vec2<f32>, p0: vec2<f32>, p1: vec2<f32>, p2: vec2<f32>) -> f32 {
131 let a = p0 - 2.0 * p1 + p2;
132 let b = 2.0 * (p1 - p0);
133 let c = p0 - p;
134
135 let dot_aa = dot(a, a);
136 let dot_ab = dot(a, b);
137 let dot_ac = dot(a, c);
138 let dot_bb = dot(b, b);
139 let dot_bc = dot(b, c);
140
141 var min_dist_sq = min(dot(p - p0, p - p0), dot(p - p2, p - p2));
142
143 // Approximate closest t via clamped parameter sample
144 let t_approx = clamp(-dot_ac / max(dot_aa, 1e-4), 0.0, 1.0);
145 let u = 1.0 - t_approx;
146 let pt = u * u * p0 + 2.0 * u * t_approx * p1 + t_approx * t_approx * p2;
147 min_dist_sq = min(min_dist_sq, dot(p - pt, p - pt));
148
149 return sqrt(min_dist_sq);
150}
151
152@fragment
153fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
154 let dist = sd_quadratic_bezier(in.pixel_coords, in.p0, in.p1, in.p2);
155
156 let is_stroke = (in.flags & 2u) != 0u;
157 var alpha = 0.0;
158
159 if (is_stroke) {
160 let half_width = max(in.stroke_width * 0.5, 0.5);
161 let stroke_dist = abs(dist) - half_width;
162 alpha = clamp(0.5 - stroke_dist, 0.0, 1.0);
163 } else {
164 // Fill mode: distance field threshold
165 alpha = clamp(0.5 - dist, 0.0, 1.0);
166 }
167
168 if (alpha <= 0.0) {
169 discard;
170 }
171
172 return vec4<f32>(in.color.rgb, in.color.a * alpha);
173}
174"#;
175
176pub const WGSL_CONCENTRIC_FILLET_CORNER_SHADER: &str = r#"
178fn sdf_squircle(p: vec2<f32>, half_size: vec2<f32>, radius: f32, n: f32) -> f32 {
179 let straight = max(half_size - vec2<f32>(radius, radius), vec2<f32>(0.0, 0.0));
180 let q = abs(p) - straight;
181 if (q.x <= 0.0 && q.y <= 0.0) {
182 return max(q.x, q.y) - radius;
183 } else if (q.x > 0.0 && q.y > 0.0) {
184 let norm_n = pow(pow(q.x, n) + pow(q.y, n), 1.0 / n);
185 return norm_n - radius;
186 } else {
187 return max(q.x, q.y) - radius;
188 }
189}
190
191fn concentric_fillet_clip(
192 pixel_pos: vec2<f32>,
193 outer_half: vec2<f32>,
194 outer_radius: f32,
195 bezel_thickness: f32,
196 exponent: f32
197) -> vec4<f32> {
198 let inner_half = max(outer_half - vec2<f32>(bezel_thickness, bezel_thickness), vec2<f32>(0.0, 0.0));
199 let inner_radius = max(outer_radius - bezel_thickness, 0.0);
200
201 let d_outer = sdf_squircle(pixel_pos, outer_half, outer_radius, exponent);
202 let d_inner = sdf_squircle(pixel_pos, inner_half, inner_radius, exponent);
203
204 let screen_coverage = clamp(0.5 - d_inner, 0.0, 1.0);
205 let chassis_coverage = clamp(0.5 - d_outer, 0.0, 1.0);
206 let bezel_mask = clamp(0.5 - max(d_outer, -d_inner), 0.0, 1.0);
207
208 return vec4<f32>(screen_coverage, chassis_coverage, bezel_mask, d_inner);
209}
210"#;