From 6e10518f12e4f04c879fca9395dc3ae64aacd0be Mon Sep 17 00:00:00 2001 From: Dzmitry Malyshau Date: Tue, 28 Jul 2020 11:48:49 -0400 Subject: [PATCH] Reformat the alignment errors, add a stride check --- wgpu-core/src/binding_model.rs | 13 +++---------- wgpu-core/src/command/bundle.rs | 5 +---- wgpu-core/src/command/transfer.rs | 10 +++++----- wgpu-core/src/device/mod.rs | 8 +++++++- wgpu-core/src/device/queue.rs | 20 +++++++++----------- wgpu-core/src/pipeline.rs | 2 ++ wgpu-types/src/lib.rs | 2 ++ 7 files changed, 29 insertions(+), 31 deletions(-) diff --git a/wgpu-core/src/binding_model.rs b/wgpu-core/src/binding_model.rs index 265721c97e..cf68f6e058 100644 --- a/wgpu-core/src/binding_model.rs +++ b/wgpu-core/src/binding_model.rs @@ -66,10 +66,7 @@ pub enum CreateBindGroupError { SingleBindingExpected, #[error("unable to create a bind group with a swap chain image")] SwapChainImage, - #[error( - "buffer offset {0} must be a multiple of {}", - wgt::BIND_BUFFER_ALIGNMENT - )] + #[error("buffer offset {0} does not respect `BIND_BUFFER_ALIGNMENT`")] UnalignedBufferOffset(wgt::BufferAddress), #[error("uniform buffer binding range exceeds `max_uniform_buffer_binding_size` limit")] UniformBufferRangeTooLarge, @@ -320,10 +317,7 @@ pub enum PushConstantUploadError { actual: wgt::ShaderStage, unmatched: wgt::ShaderStage, }, - #[error( - "provided push constant offset {0} must be aligned to {}", - wgt::PUSH_CONSTANT_ALIGNMENT - )] + #[error("provided push constant offset {0} does not respect `PUSH_CONSTANT_ALIGNMENT`")] Unaligned(u32), } @@ -448,8 +442,7 @@ pub enum BindError { #[error("number of dynamic offsets ({actual}) doesn't match the number of dynamic bindings in the bind group layout ({expected})")] MismatchedDynamicOffsetCount { actual: usize, expected: usize }, #[error( - "dynamic binding at index {idx}: offset {offset} must be aligned to {}", - wgt::BIND_BUFFER_ALIGNMENT + "dynamic binding at index {idx}: offset {offset} does not respect `BIND_BUFFER_ALIGNMENT`" )] UnalignedDynamicBinding { idx: usize, offset: u32 }, #[error("dynamic binding at index {idx} with offset {offset} would overrun the buffer (limit: {max})")] diff --git a/wgpu-core/src/command/bundle.rs b/wgpu-core/src/command/bundle.rs index e23f7252ee..959d9001ed 100644 --- a/wgpu-core/src/command/bundle.rs +++ b/wgpu-core/src/command/bundle.rs @@ -600,10 +600,7 @@ impl State { pub enum RenderCommandError { #[error("bind group index {index} is greater than the device's requested `max_bind_group` limit {max}")] BindGroupIndexOutOfRange { index: u8, max: u32 }, - #[error( - "dynamic buffer offset {0} is not a multiple of the required buffer alignment {}", - wgt::BIND_BUFFER_ALIGNMENT - )] + #[error("dynamic buffer offset {0} does not respect `BIND_BUFFER_ALIGNMENT`")] UnalignedBufferOffset(u64), #[error("number of buffer offsets ({actual}) does not match the number of dynamic bindings ({expected})")] InvalidDynamicOffsetCount { actual: usize, expected: usize }, diff --git a/wgpu-core/src/command/transfer.rs b/wgpu-core/src/command/transfer.rs index 1c83bca9cc..331c6213bd 100644 --- a/wgpu-core/src/command/transfer.rs +++ b/wgpu-core/src/command/transfer.rs @@ -38,15 +38,15 @@ pub enum TransferError { BufferOverrun, #[error("buffer offset is not aligned to block size")] UnalignedBufferOffset, - #[error("copy size is not a multiple of block size")] + #[error("copy size does not respect `COPY_BUFFER_ALIGNMENT`")] UnalignedCopySize, - #[error("copy width is not a multiple of block size")] + #[error("copy width is not a multiple of block width")] UnalignedCopyWidth, - #[error("copy height is not a multiple of block size")] + #[error("copy height is not a multiple of block height")] UnalignedCopyHeight, - #[error("bytes per row is not a multiple of the required alignment")] + #[error("bytes per row does not respect `COPY_BYTES_PER_ROW_ALIGNMENT`")] UnalignedBytesPerRow, - #[error("number of rows per image is not a multiple of the required alignment")] + #[error("number of rows per image is not a multiple of block height")] UnalignedRowsPerImage, #[error("number of bytes per row is less than the number of bytes in a complete row")] InvalidBytesPerRow, diff --git a/wgpu-core/src/device/mod.rs b/wgpu-core/src/device/mod.rs index 2f4d9db0c3..8f313d4732 100644 --- a/wgpu-core/src/device/mod.rs +++ b/wgpu-core/src/device/mod.rs @@ -2166,6 +2166,12 @@ impl Global { if vb_state.attributes.is_empty() { continue; } + if vb_state.stride % wgt::VERTEX_STRIDE_ALIGNMENT != 0 { + return Err(pipeline::RenderPipelineError::UnalignedVertexStride { + index: i as u32, + stride: vb_state.stride, + }); + } vertex_buffers.alloc().init(hal::pso::VertexBufferDesc { binding: i as u32, stride: vb_state.stride as u32, @@ -3070,7 +3076,7 @@ pub enum BufferAccessError { NotMapped, #[error("not enough memory left")] OutOfMemory, - #[error("buffer map range is not aligned to {}", wgt::COPY_BUFFER_ALIGNMENT)] + #[error("buffer map range does not respect `COPY_BUFFER_ALIGNMENT`")] UnalignedRange, } diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 6f7cf41384..132eb6cb0c 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -579,21 +579,19 @@ pub enum PrepareStageError { #[derive(Clone, Debug, Error)] pub enum QueueWriteBufferError { #[error("write buffer with indices {start}..{end} would overrun buffer of size {size}")] - BufferOverrun { start: u64, end: u64, size: u64 }, + BufferOverrun { + start: wgt::BufferAddress, + end: wgt::BufferAddress, + size: wgt::BufferAddress, + }, #[error(transparent)] MissingBufferUsage(#[from] MissingBufferUsageError), #[error(transparent)] Stage(#[from] PrepareStageError), - #[error( - "buffer offset {0} must be a multiple of {}", - wgt::COPY_BUFFER_ALIGNMENT - )] - UnalignedBufferOffset(u64), - #[error( - "buffer write size {0} must be a multiple of {}", - wgt::COPY_BUFFER_ALIGNMENT - )] - UnalignedDataSize(u64), + #[error("buffer offset {0} does not respect `COPY_BUFFER_ALIGNMENT`")] + UnalignedBufferOffset(wgt::BufferAddress), + #[error("buffer write size {0} does not respect `COPY_BUFFER_ALIGNMENT`")] + UnalignedDataSize(wgt::BufferAddress), } #[derive(Clone, Debug, Error)] diff --git a/wgpu-core/src/pipeline.rs b/wgpu-core/src/pipeline.rs index 21c8757000..e28cb058f6 100644 --- a/wgpu-core/src/pipeline.rs +++ b/wgpu-core/src/pipeline.rs @@ -63,6 +63,8 @@ pub enum RenderPipelineError { IncompatibleOutputFormat { index: u8 }, #[error("invalid sample count {0}")] InvalidSampleCount(u32), + #[error("vertex buffer {index} stride {stride} does not respect `VERTEX_STRIDE_ALIGNMENT`")] + UnalignedVertexStride { index: u32, stride: BufferAddress }, #[error("vertex attribute at location {location} has invalid offset {offset}")] InvalidVertexAttributeOffset { location: wgt::ShaderLocation, diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index 899c3a63d1..17433c3b85 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -27,6 +27,8 @@ pub const COPY_BYTES_PER_ROW_ALIGNMENT: u32 = 256; pub const BIND_BUFFER_ALIGNMENT: BufferAddress = 256; /// Buffer to buffer copy offsets and sizes must be aligned to this number. pub const COPY_BUFFER_ALIGNMENT: BufferAddress = 4; +/// Vertex buffer strides have to be aligned to this number. +pub const VERTEX_STRIDE_ALIGNMENT: BufferAddress = 4; /// Alignment all push constants need pub const PUSH_CONSTANT_ALIGNMENT: u32 = 4;