From 1169cf72ec435a495e1449bdf222bc0ccfd6f62a Mon Sep 17 00:00:00 2001 From: Micha White Date: Tue, 4 Oct 2022 09:59:44 -0400 Subject: Small optimization --- src/renderer.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'src/renderer.rs') diff --git a/src/renderer.rs b/src/renderer.rs index 14a7b6a..516b1b1 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -64,6 +64,7 @@ pub struct Renderer { texture_size: wgpu::Extent3d, diffuse_texture: wgpu::Texture, diffuse_bind_group: wgpu::BindGroup, + image: image::RgbaImage, window: Window, } @@ -299,6 +300,13 @@ impl Renderer { }, ], }); + // TODO make this all resizable + let image = image::RgbaImage::from_vec( + texture_size.width, + texture_size.height, + vec![0; 4 * texture_size.width as usize * texture_size.height as usize], + ) + .unwrap(); let render_pipeline_layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { @@ -330,6 +338,7 @@ impl Renderer { texture_size, diffuse_texture, diffuse_bind_group, + image, window, }) } @@ -468,18 +477,10 @@ impl Renderer { // TODO optimize this fn fill_textures(&mut self) { - // create a base image - let mut image = image::RgbaImage::from_vec( - self.texture_size.width, - self.texture_size.height, - vec![0; 4 * self.texture_size.width as usize * self.texture_size.height as usize], - ) - .unwrap(); - // put the packed texture into the base image let Ok(atlases) = self.textures.atlases() else { return }; let Some(atlas) = atlases.first() else { return }; - image.copy_from(atlas, 0, 0).unwrap(); + self.image.copy_from(atlas, 0, 0).unwrap(); // copy that to the gpu self.queue.write_texture( @@ -489,7 +490,7 @@ impl Renderer { origin: wgpu::Origin3d::ZERO, aspect: wgpu::TextureAspect::All, }, - image.as_bytes(), + self.image.as_bytes(), wgpu::ImageDataLayout { offset: 0, bytes_per_row: NonZeroU32::new(self.texture_size.width * 4), -- cgit v1.2.3