Skip to content

Commit 3aedbd5

Browse files
committed
Snap quad primitives to the pixel grid (once again!)
Let's hope this approach handles common cases better.
1 parent 7c5a4bc commit 3aedbd5

3 files changed

Lines changed: 15 additions & 18 deletions

File tree

wgpu/src/shader/quad/gradient.wgsl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ fn gradient_vs_main(input: GradientVertexInput) -> GradientVertexOutput {
3333
var pos: vec2<f32> = input.position_and_scale.xy * globals.scale;
3434
var scale: vec2<f32> = input.position_and_scale.zw * globals.scale;
3535

36+
var pos_snap: vec2<f32> = vec2<f32>(round(pos.x + 0.01) - pos.x, round(pos.y + 0.01) - pos.y);
37+
var scale_snap: vec2<f32> = vec2<f32>(round(scale.x + 0.01) - scale.x, round(scale.y + 0.01) - scale.y);
38+
3639
var min_border_radius = min(input.position_and_scale.z, input.position_and_scale.w) * 0.5;
3740
var border_radius: vec4<f32> = vec4<f32>(
3841
min(input.border_radius.x, min_border_radius),
@@ -42,10 +45,10 @@ fn gradient_vs_main(input: GradientVertexInput) -> GradientVertexOutput {
4245
);
4346

4447
var transform: mat4x4<f32> = mat4x4<f32>(
45-
vec4<f32>(scale.x + 1.0, 0.0, 0.0, 0.0),
46-
vec4<f32>(0.0, scale.y + 1.0, 0.0, 0.0),
48+
vec4<f32>(scale.x + scale_snap.x + 1.0, 0.0, 0.0, 0.0),
49+
vec4<f32>(0.0, scale.y + scale_snap.y + 1.0, 0.0, 0.0),
4750
vec4<f32>(0.0, 0.0, 1.0, 0.0),
48-
vec4<f32>(pos - vec2<f32>(0.5, 0.5), 0.0, 1.0)
51+
vec4<f32>(pos + pos_snap - vec2<f32>(0.5, 0.5), 0.0, 1.0)
4952
);
5053

5154
out.position = globals.transform * transform * vec4<f32>(vertex_position(input.vertex_index), 0.0, 1.0);
@@ -55,7 +58,7 @@ fn gradient_vs_main(input: GradientVertexInput) -> GradientVertexOutput {
5558
out.colors_4 = input.colors_4;
5659
out.offsets = input.offsets;
5760
out.direction = input.direction * globals.scale;
58-
out.position_and_scale = vec4<f32>(pos, scale);
61+
out.position_and_scale = vec4<f32>(pos + pos_snap, scale + scale_snap);
5962
out.border_color = premultiply(input.border_color);
6063
out.border_radius = border_radius * globals.scale;
6164
out.border_width = input.border_width * globals.scale;

wgpu/src/shader/quad/solid.wgsl

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,9 @@ fn solid_vs_main(input: SolidVertexInput) -> SolidVertexOutput {
3030

3131
var pos: vec2<f32> = (input.pos + min(input.shadow_offset, vec2<f32>(0.0, 0.0)) - input.shadow_blur_radius) * globals.scale;
3232
var scale: vec2<f32> = (input.scale + vec2<f32>(abs(input.shadow_offset.x), abs(input.shadow_offset.y)) + input.shadow_blur_radius * 2.0) * globals.scale;
33-
var snap: vec2<f32> = vec2<f32>(0.0, 0.0);
3433

35-
if input.scale.x == 1.0 {
36-
snap.x = round(pos.x) - pos.x;
37-
}
38-
39-
if input.scale.y == 1.0 {
40-
snap.y = round(pos.y) - pos.y;
41-
}
34+
var pos_snap: vec2<f32> = vec2<f32>(round(pos.x + 0.01) - pos.x, round(pos.y + 0.01) - pos.y);
35+
var scale_snap: vec2<f32> = vec2<f32>(round(scale.x + 0.01) - scale.x, round(scale.y + 0.01) - scale.y);
4236

4337
var min_border_radius = min(input.scale.x, input.scale.y) * 0.5;
4438
var border_radius: vec4<f32> = vec4<f32>(
@@ -49,17 +43,17 @@ fn solid_vs_main(input: SolidVertexInput) -> SolidVertexOutput {
4943
);
5044

5145
var transform: mat4x4<f32> = mat4x4<f32>(
52-
vec4<f32>(scale.x + 1.0, 0.0, 0.0, 0.0),
53-
vec4<f32>(0.0, scale.y + 1.0, 0.0, 0.0),
46+
vec4<f32>(scale.x + scale_snap.x + 1.0, 0.0, 0.0, 0.0),
47+
vec4<f32>(0.0, scale.y + scale_snap.y + 1.0, 0.0, 0.0),
5448
vec4<f32>(0.0, 0.0, 1.0, 0.0),
55-
vec4<f32>(pos - vec2<f32>(0.5, 0.5) + snap, 0.0, 1.0)
49+
vec4<f32>(pos + pos_snap - vec2<f32>(0.5, 0.5), 0.0, 1.0)
5650
);
5751

5852
out.position = globals.transform * transform * vec4<f32>(vertex_position(input.vertex_index), 0.0, 1.0);
5953
out.color = premultiply(input.color);
6054
out.border_color = premultiply(input.border_color);
61-
out.pos = input.pos * globals.scale + snap;
62-
out.scale = input.scale * globals.scale;
55+
out.pos = input.pos * globals.scale + pos_snap;
56+
out.scale = input.scale * globals.scale + scale_snap;
6357
out.border_radius = border_radius * globals.scale;
6458
out.border_width = input.border_width * globals.scale;
6559
out.shadow_color = premultiply(input.shadow_color);

widget/src/toggler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ where
424424
.style(&self.class, self.last_status.unwrap_or(Status::Disabled));
425425

426426
let border_radius = bounds.height / BORDER_RADIUS_RATIO;
427-
let space = SPACE_RATIO * bounds.height;
427+
let space = (SPACE_RATIO * bounds.height).round();
428428

429429
let toggler_background_bounds = Rectangle {
430430
x: bounds.x + space,

0 commit comments

Comments
 (0)