From 5dbe87d0c02a9c625e5a2b8395b742021e0945ca Mon Sep 17 00:00:00 2001 From: Jay Oster Date: Wed, 3 Mar 2021 10:45:14 -0800 Subject: [PATCH] Fix matrix when creating a scaling renderer (#143) - This issue can be seen when creating a window and pixel buffer that have differing sizes; the image will be stretched to fill the window. - If the window supports the resize callback to correct the image aspect ratio, resizing the window will correct the matrix immediately, adding the black border as expected. This is a jarring effect when the texture size ratio is not an integer. - This bug also causes issues with the new `resize_buffer()` API. --- src/builder.rs | 1 + src/renderers.rs | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index 03b0621..ece169a 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -309,6 +309,7 @@ pub(crate) fn create_backing_texture( device, &texture_view, &texture_extent, + surface_size, render_texture_format, ); diff --git a/src/renderers.rs b/src/renderers.rs index 55f490f..0caad67 100644 --- a/src/renderers.rs +++ b/src/renderers.rs @@ -1,3 +1,4 @@ +use crate::SurfaceSize; use ultraviolet::Mat4; use wgpu::util::DeviceExt; @@ -17,6 +18,7 @@ impl ScalingRenderer { device: &wgpu::Device, texture_view: &wgpu::TextureView, texture_size: &wgpu::Extent3d, + surface_size: &SurfaceSize, render_texture_format: wgpu::TextureFormat, ) -> Self { let vs_module = device.create_shader_module(&wgpu::include_spirv!("../shaders/vert.spv")); @@ -39,11 +41,9 @@ impl ScalingRenderer { }); // Create uniform buffer - // TODO: This should also have the width / height of the of the window surface, - // so that it won't break when the window is created with a different size. let matrix = ScalingMatrix::new( (texture_size.width as f32, texture_size.height as f32), - (texture_size.width as f32, texture_size.height as f32), + (surface_size.width as f32, surface_size.height as f32), ); let transform_bytes = matrix.as_bytes(); let uniform_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {