From 466016a374aad425464ee66f4e600e0c1e0b554a Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 26 Mar 2021 11:07:16 +0100 Subject: [PATCH] Snap sprites to pixel grid in `Renderer` Previously, we were doing so in the `SpriteCache` but that would cause floating point errors that caused glyphs to sometimes be positioned midway through a pixel. --- gpui/src/platform/mac/renderer.rs | 4 +++- gpui/src/platform/mac/sprite_cache.rs | 7 ++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/gpui/src/platform/mac/renderer.rs b/gpui/src/platform/mac/renderer.rs index a79ce15869..f679edca2b 100644 --- a/gpui/src/platform/mac/renderer.rs +++ b/gpui/src/platform/mac/renderer.rs @@ -293,11 +293,13 @@ impl Renderer { glyph.origin, scene.scale_factor(), ) { + // Snap sprite to pixel grid. + let origin = (glyph.origin * scene.scale_factor()).floor() + sprite.offset.to_f32(); sprites_by_atlas .entry(sprite.atlas_id) .or_insert_with(Vec::new) .push(shaders::GPUISprite { - origin: (glyph.origin * scene.scale_factor() + sprite.offset).to_float2(), + origin: origin.to_float2(), size: sprite.size.to_float2(), atlas_origin: sprite.atlas_origin.to_float2(), color: glyph.color.to_uchar4(), diff --git a/gpui/src/platform/mac/sprite_cache.rs b/gpui/src/platform/mac/sprite_cache.rs index 3d718a25f0..7f73494e5f 100644 --- a/gpui/src/platform/mac/sprite_cache.rs +++ b/gpui/src/platform/mac/sprite_cache.rs @@ -23,7 +23,7 @@ struct GlyphDescriptor { pub struct GlyphSprite { pub atlas_id: usize, pub atlas_origin: Vector2I, - pub offset: Vector2F, + pub offset: Vector2I, pub size: Vector2I, } @@ -109,13 +109,10 @@ impl SpriteCache { bounds }); - // Snap sprite to pixel grid. - let offset = glyph_bounds.origin().to_f32() - - vec2f(target_position.x().fract(), target_position.y().fract()); Some(GlyphSprite { atlas_id: atlasses.len() - 1, atlas_origin: atlas_bounds.origin(), - offset, + offset: glyph_bounds.origin(), size: glyph_bounds.size(), }) })