DPI scaling fixes

This commit is contained in:
Zachary Levy
2026-05-05 14:45:34 -07:00
parent e8ffa28de3
commit 4e15f831de
19 changed files with 509 additions and 368 deletions
+26 -26
View File
@@ -45,7 +45,7 @@ float sdRegularPolygon(vec2 p, float r, float n) {
return length(p) * cos(bn) - r;
}
// Coverage from SDF distance using half-feather width (feather_px * 0.5, pre-computed on CPU).
// Coverage from SDF distance using half-feather width (feather_ppx * 0.5, pre-computed on CPU).
// Produces a symmetric transition centered on d=0: smoothstep(-h, h, d).
float sdf_alpha(float d, float h) {
return 1.0 - smoothstep(-h, h, d);
@@ -80,56 +80,56 @@ void main() {
// SDF path — dispatch on kind
float d = 1e30;
float h = 0.5; // half-feather width; overwritten per shape kind
vec2 half_size = f_params.xy; // used by RRect and as reference size for gradients
float h = 0.5; // half-feather width (physical px); overwritten per shape kind
vec2 half_size_ppx = f_params.xy; // used by RRect and as reference size for gradients
vec2 p_local = f_local_or_uv; // arrives rotated; vertex shader handled .Rotated
vec2 p_local_ppx = f_local_or_uv; // arrives rotated; vertex shader handled .Rotated
if (kind == 1u) {
// RRect — half_feather in params2.z
vec4 corner_radii = vec4(f_params.zw, f_params2.xy);
// RRect — half_feather_ppx in params2.z
vec4 corner_radii_ppx = vec4(f_params.zw, f_params2.xy);
h = f_params2.z;
d = sdRoundedBox(p_local, half_size, corner_radii);
d = sdRoundedBox(p_local_ppx, half_size_ppx, corner_radii_ppx);
}
else if (kind == 2u) {
// NGon — half_feather in params.z
float radius = f_params.x;
// NGon — half_feather_ppx in params.z
float radius_ppx = f_params.x;
float sides = f_params.y;
h = f_params.z;
d = sdRegularPolygon(p_local, radius, sides);
half_size = vec2(radius); // for gradient UV computation
d = sdRegularPolygon(p_local_ppx, radius_ppx, sides);
half_size_ppx = vec2(radius_ppx); // for gradient UV computation
}
else if (kind == 3u) {
// Ellipse — half_feather in params.z
vec2 ab = f_params.xy;
// Ellipse — half_feather_ppx in params.z
vec2 radii_ppx = f_params.xy;
h = f_params.z;
d = sdEllipseApprox(p_local, ab);
half_size = ab; // for gradient UV computation
d = sdEllipseApprox(p_local_ppx, radii_ppx);
half_size_ppx = radii_ppx; // for gradient UV computation
}
else if (kind == 4u) {
// Ring_Arc — half_feather in params2.z
// Ring_Arc — half_feather_ppx in params2.z
// Arc mode from flag bits 5-6: 0 = full, 1 = narrow (≤π), 2 = wide (>π)
float inner = f_params.x;
float outer = f_params.y;
float inner_radius_ppx = f_params.x;
float outer_radius_ppx = f_params.y;
vec2 n_start = f_params.zw;
vec2 n_end = f_params2.xy;
uint arc_bits = (flags >> 5u) & 3u;
h = f_params2.z;
float r = length(p_local);
d = max(inner - r, r - outer);
float r = length(p_local_ppx);
d = max(inner_radius_ppx - r, r - outer_radius_ppx);
if (arc_bits != 0u) {
float d_start = dot(p_local, n_start);
float d_end = dot(p_local, n_end);
float d_start = dot(p_local_ppx, n_start);
float d_end = dot(p_local_ppx, n_end);
float d_wedge = (arc_bits == 1u)
? max(d_start, d_end) // arc ≤ π: intersect half-planes
: min(d_start, d_end); // arc > π: union half-planes
d = max(d, d_wedge);
}
half_size = vec2(outer); // for gradient UV computation
half_size_ppx = vec2(outer_radius_ppx); // for gradient UV computation
}
// --- fwidth-based normalization for correct AA and stroke width ---
@@ -146,18 +146,18 @@ void main() {
if ((flags & 4u) != 0u) {
// Radial gradient (bit 2): t from distance to center
mediump float t = length(p_local / half_size);
mediump float t = length(p_local_ppx / half_size_ppx);
shape_color = gradient_2color(gradient_start, gradient_end, t);
} else {
// Linear gradient: direction pre-computed on CPU as (cos, sin) f16 pair
vec2 direction = unpackHalf2x16(f_effects.z);
mediump float t = dot(p_local / half_size, direction) * 0.5 + 0.5;
mediump float t = dot(p_local_ppx / half_size_ppx, direction) * 0.5 + 0.5;
shape_color = gradient_2color(gradient_start, gradient_end, t);
}
} else if ((flags & 1u) != 0u) {
// Textured (bit 0)
vec4 uv_rect = f_uv_rect;
vec2 local_uv = p_local / half_size * 0.5 + 0.5;
vec2 local_uv = p_local_ppx / half_size_ppx * 0.5 + 0.5;
vec2 uv = mix(uv_rect.xy, uv_rect.zw, local_uv);
shape_color = f_color * texture(tex, uv);
} else {