From 2a4ebbf19d47fd8e5cdc4d4311e82393ec50ca1d Mon Sep 17 00:00:00 2001 From: Jay Oster Date: Wed, 1 Dec 2021 21:36:56 -0800 Subject: [PATCH] Fix half-pixel offsets in scaling renderer (#231) * Fix half-pixel offsets in scaling renderer - This bug was very subtle. It can be hard to notice! - Context: When the scaling renderer transformation matrix is created, it needs to center the image within the border. - The previous code always evaluated the translation to (0, 0) - This PR makes half-pixel adjustments to the translation when needed, making it impossible to rasterize the texture on a half-pixel boundary. - I spotted this while working on pixel-aspect-ratio support, but it can most easily be witnessed in the `conway` example by grabbing the top or bottom resize handle on the window and slow dragging it up and down by 1-pixel-at-a-time. When you hit a half-pixel bug the entire texture will change slightly; some pixels will become hidden or duplicated. * Bump MSRV --- .github/workflows/ci.yml | 4 ++-- MSRV.md | 1 + src/renderers.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 797c7b8..e29aa92 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: rust: - stable - beta - - 1.52.0 + - 1.53.0 steps: - name: Checkout sources uses: actions/checkout@v2 @@ -73,7 +73,7 @@ jobs: rust: - stable - beta - - 1.52.0 + - 1.53.0 steps: - name: Checkout sources uses: actions/checkout@v2 diff --git a/MSRV.md b/MSRV.md index 45d1261..9f1df2c 100644 --- a/MSRV.md +++ b/MSRV.md @@ -2,6 +2,7 @@ | `pixels` version | `rustc` version | |------------------|-----------------| +| `0.9.0` | `1.53.0` | | `0.8.0` | `1.52.0` | | `0.7.0` | `1.52.0` | | `0.6.0` | `1.52.0` | diff --git a/src/renderers.rs b/src/renderers.rs index 2b75545..79c577a 100644 --- a/src/renderers.rs +++ b/src/renderers.rs @@ -243,8 +243,8 @@ impl ScalingMatrix { // Create a transformation matrix let sw = scaled_width / screen_width; let sh = scaled_height / screen_height; - let tx = (texture_width / screen_width - 1.0).max(0.0); - let ty = (1.0 - texture_height / screen_height).min(0.0); + let tx = (screen_width / 2.0).fract() / screen_width; + let ty = (screen_height / 2.0).fract() / screen_height; #[rustfmt::skip] let transform: [f32; 16] = [ sw, 0.0, 0.0, 0.0,