From 0929425477228bae5f134f2acf85a8ce6866fafb Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Wed, 1 Jul 2020 10:42:34 -0400 Subject: [PATCH] Require mapped buffers to be aligned --- wgpu-core/src/device/mod.rs | 18 +++++++++++++----- wgpu-core/src/device/queue.rs | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index b4b9834686..5da3f44f50 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -22,10 +22,7 @@ use hal::{ window::{PresentationSurface as _, Surface as _}, }; use parking_lot::{Mutex, MutexGuard}; -use wgt::{ - BufferAddress, BufferSize, InputStepMode, TextureDimension, TextureFormat, - BIND_BUFFER_ALIGNMENT, -}; +use wgt::{BufferAddress, BufferSize, InputStepMode, TextureDimension, TextureFormat}; use std::{ collections::hash_map::Entry, ffi, iter, marker::PhantomData, mem, ops::Range, ptr, slice, @@ -670,6 +667,14 @@ impl Global { log::info!("Create buffer {:?} with ID {:?}", desc, id_in); + if desc.mapped_at_creation { + assert_eq!( + desc.size % wgt::COPY_BUFFER_ALIGNMENT, + 0, + "Buffers that are mapped at creation have to be aligned to COPY_BUFFER_ALIGNMENT" + ); + } + let (device_guard, mut token) = hub.devices.read(&mut token); let device = &device_guard[device_id]; let mut buffer = device.create_buffer(device_id, desc, gfx_memory::Kind::General); @@ -1535,7 +1540,7 @@ impl Global { }; assert_eq!( - bb.offset % BIND_BUFFER_ALIGNMENT, + bb.offset % wgt::BIND_BUFFER_ALIGNMENT, 0, "Buffer offset {} must be a multiple of BIND_BUFFER_ALIGNMENT", bb.offset @@ -2792,6 +2797,9 @@ impl Global { HostMap::Write => (wgt::BufferUsage::MAP_WRITE, resource::BufferUse::MAP_WRITE), }; + assert_eq!(range.start % wgt::COPY_BUFFER_ALIGNMENT, 0); + assert_eq!(range.end % wgt::COPY_BUFFER_ALIGNMENT, 0); + let (device_id, ref_count) = { let (mut buffer_guard, _) = hub.buffers.write(&mut token); let buffer = &mut buffer_guard[buffer_id]; diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 2e1818e84b..1a7d4cadcc 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -188,7 +188,7 @@ impl Global { data_size % wgt::COPY_BUFFER_ALIGNMENT, 0, "Buffer write size {} must be a multiple of {}", - buffer_offset, + data_size, wgt::COPY_BUFFER_ALIGNMENT, ); assert_eq!(