From cdb6bd56a15a0200f8713e48f0a3948f8f7987e7 Mon Sep 17 00:00:00 2001 From: Imbris Date: Sun, 29 Nov 2020 21:09:33 -0500 Subject: [PATCH] No crash on startup --- voxygen/src/render/buffer.rs | 4 ++-- voxygen/src/render/texture.rs | 21 ++++++++++++++++++--- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/voxygen/src/render/buffer.rs b/voxygen/src/render/buffer.rs index 8d128d486f..42eb72bda9 100644 --- a/voxygen/src/render/buffer.rs +++ b/voxygen/src/render/buffer.rs @@ -35,7 +35,7 @@ impl DynamicBuffer { buf: device.create_buffer(&wgpu::BufferDescriptor { label: None, mapped_at_creation: false, - size: (len / std::mem::size_of::()) as u64, + size: len as u64 * std::mem::size_of::() as u64, usage: usage | wgpu::BufferUsage::COPY_DST, }), len, @@ -48,7 +48,7 @@ impl DynamicBuffer { if !vals.is_empty() { queue.write_buffer( &self.buf, - (offset / std::mem::size_of::()) as u64, + offset as u64 * std::mem::size_of::() as u64, bytemuck::cast_slice(vals), ) } diff --git a/voxygen/src/render/texture.rs b/voxygen/src/render/texture.rs index 96bd52cafd..32296b33c6 100644 --- a/voxygen/src/render/texture.rs +++ b/voxygen/src/render/texture.rs @@ -18,6 +18,19 @@ impl Texture { filter_method: Option, address_mode: Option, ) -> Result { + let format = match &image { + DynamicImage::ImageLuma8(_) => wgpu::TextureFormat::R8Unorm, + DynamicImage::ImageLumaA8(_) => panic!("ImageLuma8 unsupported"), + DynamicImage::ImageRgb8(_) => panic!("ImageRgb8 unsupported"), + DynamicImage::ImageRgba8(_) => wgpu::TextureFormat::Rgba8UnormSrgb, + DynamicImage::ImageBgr8(_) => panic!("ImageBgr8 unsupported"), + DynamicImage::ImageBgra8(_) => panic!("ImageBgra8 unsupported"), + DynamicImage::ImageLuma16(_) => panic!("ImageLuma16 unsupported"), + DynamicImage::ImageLumaA16(_) => panic!("ImageLumaA16 unsupported"), + DynamicImage::ImageRgb16(_) => panic!("ImageRgb16 unsupported"), + DynamicImage::ImageRgba16(_) => panic!("ImageRgba16 unsupported"), + }; + // TODO: Actually handle images that aren't in rgba format properly. let buffer = image.as_flat_samples_u8().ok_or_else(|| { RenderError::CustomError( @@ -25,6 +38,8 @@ impl Texture { ) })?; + let bytes_per_pixel = u32::from(buffer.layout.channels); + let size = Extent3d { width: image.width(), height: image.height(), @@ -37,7 +52,7 @@ impl Texture { mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, - format: wgpu::TextureFormat::Rgba8UnormSrgb, + format, usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, }); @@ -53,7 +68,7 @@ impl Texture { buffer.as_slice(), wgpu::TextureDataLayout { offset: 0, - bytes_per_row: image.width() * 4, + bytes_per_row: image.width() * bytes_per_pixel, rows_per_image: image.height(), }, wgpu::Extent3d { @@ -76,7 +91,7 @@ impl Texture { let view = tex.create_view(&wgpu::TextureViewDescriptor { label: None, - format: Some(wgpu::TextureFormat::Rgba8UnormSrgb), + format: Some(format), dimension: Some(wgpu::TextureViewDimension::D2), aspect: wgpu::TextureAspect::All, base_mip_level: 0,