No crash on startup

This commit is contained in:
Imbris 2020-11-29 21:09:33 -05:00
parent 366e8e8e04
commit cdb6bd56a1
2 changed files with 20 additions and 5 deletions

View File

@ -35,7 +35,7 @@ impl<T: Copy + Pod> DynamicBuffer<T> {
buf: device.create_buffer(&wgpu::BufferDescriptor { buf: device.create_buffer(&wgpu::BufferDescriptor {
label: None, label: None,
mapped_at_creation: false, mapped_at_creation: false,
size: (len / std::mem::size_of::<T>()) as u64, size: len as u64 * std::mem::size_of::<T>() as u64,
usage: usage | wgpu::BufferUsage::COPY_DST, usage: usage | wgpu::BufferUsage::COPY_DST,
}), }),
len, len,
@ -48,7 +48,7 @@ impl<T: Copy + Pod> DynamicBuffer<T> {
if !vals.is_empty() { if !vals.is_empty() {
queue.write_buffer( queue.write_buffer(
&self.buf, &self.buf,
(offset / std::mem::size_of::<T>()) as u64, offset as u64 * std::mem::size_of::<T>() as u64,
bytemuck::cast_slice(vals), bytemuck::cast_slice(vals),
) )
} }

View File

@ -18,6 +18,19 @@ impl Texture {
filter_method: Option<wgpu::FilterMode>, filter_method: Option<wgpu::FilterMode>,
address_mode: Option<wgpu::AddressMode>, address_mode: Option<wgpu::AddressMode>,
) -> Result<Self, RenderError> { ) -> Result<Self, RenderError> {
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. // TODO: Actually handle images that aren't in rgba format properly.
let buffer = image.as_flat_samples_u8().ok_or_else(|| { let buffer = image.as_flat_samples_u8().ok_or_else(|| {
RenderError::CustomError( RenderError::CustomError(
@ -25,6 +38,8 @@ impl Texture {
) )
})?; })?;
let bytes_per_pixel = u32::from(buffer.layout.channels);
let size = Extent3d { let size = Extent3d {
width: image.width(), width: image.width(),
height: image.height(), height: image.height(),
@ -37,7 +52,7 @@ impl Texture {
mip_level_count: 1, mip_level_count: 1,
sample_count: 1, sample_count: 1,
dimension: wgpu::TextureDimension::D2, dimension: wgpu::TextureDimension::D2,
format: wgpu::TextureFormat::Rgba8UnormSrgb, format,
usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST, usage: wgpu::TextureUsage::SAMPLED | wgpu::TextureUsage::COPY_DST,
}); });
@ -53,7 +68,7 @@ impl Texture {
buffer.as_slice(), buffer.as_slice(),
wgpu::TextureDataLayout { wgpu::TextureDataLayout {
offset: 0, offset: 0,
bytes_per_row: image.width() * 4, bytes_per_row: image.width() * bytes_per_pixel,
rows_per_image: image.height(), rows_per_image: image.height(),
}, },
wgpu::Extent3d { wgpu::Extent3d {
@ -76,7 +91,7 @@ impl Texture {
let view = tex.create_view(&wgpu::TextureViewDescriptor { let view = tex.create_view(&wgpu::TextureViewDescriptor {
label: None, label: None,
format: Some(wgpu::TextureFormat::Rgba8UnormSrgb), format: Some(format),
dimension: Some(wgpu::TextureViewDimension::D2), dimension: Some(wgpu::TextureViewDimension::D2),
aspect: wgpu::TextureAspect::All, aspect: wgpu::TextureAspect::All,
base_mip_level: 0, base_mip_level: 0,