diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e17dacb4..31d54440a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -166,6 +166,10 @@ By @cwfitzgerald in [#3610](https://github.com/gfx-rs/wgpu/pull/3610). - Fix `Vertex buffer is not big enough for the draw call.` for ANGLE/Web when rendering with instance attributes on a single instance. By @wumpf in [#3597](https://github.com/gfx-rs/wgpu/pull/3597) - Fix `copy_external_image_to_texture`, `copy_texture_to_texture` and `copy_buffer_to_texture` not taking the specified index into account if the target texture is a cube map, 2D texture array or cube map array. By @daxpedda [#3641](https://github.com/gfx-rs/wgpu/pull/3641) +#### Metal + +- Fix metal erroring on an `array_stride` of 0. By @teoxoy in [#3538](https://github.com/gfx-rs/wgpu/pull/3538) + #### General - `copyTextureToTexture` src/dst aspects must both refer to all aspects of src/dst format. By @teoxoy in [#3431](https://github.com/gfx-rs/wgpu/pull/3431) diff --git a/wgpu-core/src/command/clear.rs b/wgpu-core/src/command/clear.rs index 9cf99ac1d..dafdbb33c 100644 --- a/wgpu-core/src/command/clear.rs +++ b/wgpu-core/src/command/clear.rs @@ -12,9 +12,11 @@ use crate::{ track::{TextureSelector, TextureTracker}, }; -use hal::{auxil::align_to, CommandEncoder as _}; +use hal::CommandEncoder as _; use thiserror::Error; -use wgt::{BufferAddress, BufferSize, BufferUsages, ImageSubresourceRange, TextureAspect}; +use wgt::{ + math::align_to, BufferAddress, BufferSize, BufferUsages, ImageSubresourceRange, TextureAspect, +}; /// Error encountered while attempting a clear. #[derive(Clone, Debug, Error)] diff --git a/wgpu-core/src/device/queue.rs b/wgpu-core/src/device/queue.rs index 0f9ec753c..e957424b4 100644 --- a/wgpu-core/src/device/queue.rs +++ b/wgpu-core/src/device/queue.rs @@ -664,7 +664,7 @@ impl Global { let bytes_per_row_alignment = get_lowest_common_denom(device.alignments.buffer_copy_pitch.get() as u32, block_size); let stage_bytes_per_row = - hal::auxil::align_to(block_size * width_blocks, bytes_per_row_alignment); + wgt::math::align_to(block_size * width_blocks, bytes_per_row_alignment); let block_rows_in_copy = (size.depth_or_array_layers - 1) * block_rows_per_image + height_blocks; diff --git a/wgpu-hal/examples/halmark/main.rs b/wgpu-hal/examples/halmark/main.rs index 2810d160b..588de6106 100644 --- a/wgpu-hal/examples/halmark/main.rs +++ b/wgpu-hal/examples/halmark/main.rs @@ -395,7 +395,7 @@ impl Example { buffer }; - let local_alignment = hal::auxil::align_to( + let local_alignment = wgt::math::align_to( mem::size_of::() as u32, capabilities.limits.min_uniform_buffer_offset_alignment, ); diff --git a/wgpu-hal/src/auxil/mod.rs b/wgpu-hal/src/auxil/mod.rs index d37b5b0ac..f0aa6a4a8 100644 --- a/wgpu-hal/src/auxil/mod.rs +++ b/wgpu-hal/src/auxil/mod.rs @@ -54,17 +54,6 @@ pub fn map_naga_stage(stage: naga::ShaderStage) -> wgt::ShaderStages { } } -pub fn align_to(value: u32, alignment: u32) -> u32 { - if alignment.is_power_of_two() { - (value + alignment - 1) & !(alignment - 1) - } else { - match value % alignment { - 0 => value, - other => value - other + alignment, - } - } -} - impl crate::CopyExtent { pub fn map_extent_to_copy_size(extent: &wgt::Extent3d, dim: wgt::TextureDimension) -> Self { Self { diff --git a/wgpu-hal/src/dx12/command.rs b/wgpu-hal/src/dx12/command.rs index c63411015..4786a61bf 100644 --- a/wgpu-hal/src/dx12/command.rs +++ b/wgpu-hal/src/dx12/command.rs @@ -43,7 +43,7 @@ impl crate::BufferTextureCopy { .unwrap(); (self.size.width / block_width) * block_size }); - crate::auxil::align_to(actual, d3d12_ty::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) + wgt::math::align_to(actual, d3d12_ty::D3D12_TEXTURE_DATA_PITCH_ALIGNMENT) }, }, } diff --git a/wgpu-hal/src/metal/device.rs b/wgpu-hal/src/metal/device.rs index 11244059a..b86f1e355 100644 --- a/wgpu-hal/src/metal/device.rs +++ b/wgpu-hal/src/metal/device.rs @@ -932,8 +932,23 @@ impl crate::Device for super::Device { self.shared.private_caps.max_vertex_buffers as u64 - 1 - i as u64; let buffer_desc = vertex_descriptor.layouts().object_at(buffer_index).unwrap(); - buffer_desc.set_stride(vb.array_stride); - buffer_desc.set_step_function(conv::map_step_mode(vb.step_mode)); + // Metal expects the stride to be the actual size of the attributes. + // The semantics of array_stride == 0 can be achieved by setting + // the step function to constant and rate to 0. + if vb.array_stride == 0 { + let stride = vb + .attributes + .iter() + .map(|attribute| attribute.offset + attribute.format.size()) + .max() + .unwrap_or(0); + buffer_desc.set_stride(wgt::math::align_to(stride, 4)); + buffer_desc.set_step_function(metal::MTLVertexStepFunction::Constant); + buffer_desc.set_step_rate(0); + } else { + buffer_desc.set_stride(vb.array_stride); + buffer_desc.set_step_function(conv::map_step_mode(vb.step_mode)); + } for at in vb.attributes { let attribute_desc = vertex_descriptor diff --git a/wgpu-types/src/lib.rs b/wgpu-types/src/lib.rs index b4ee44906..8c001e7c0 100644 --- a/wgpu-types/src/lib.rs +++ b/wgpu-types/src/lib.rs @@ -16,6 +16,7 @@ use std::path::PathBuf; use std::{num::NonZeroU32, ops::Range}; pub mod assertions; +pub mod math; // Use this macro instead of the one provided by the bitflags_serde_shim crate // because the latter produces an error when deserializing bits that are not diff --git a/wgpu-types/src/math.rs b/wgpu-types/src/math.rs new file mode 100644 index 000000000..593e5d810 --- /dev/null +++ b/wgpu-types/src/math.rs @@ -0,0 +1,31 @@ +//! Utilitary math functions. + +use std::ops::{Add, Rem, Sub}; + +/// +/// Aligns a `value` to an `alignment`. +/// +/// Returns the first number greater than or equal to `value` that is also a +/// multiple of `alignment`. If `value` is already a multiple of `alignment`, +/// `value` will be returned. +/// +/// # Examples +/// +/// ``` +/// # use wgpu_types::math::align_to; +/// assert_eq!(align_to(253, 16), 256); +/// assert_eq!(align_to(256, 16), 256); +/// assert_eq!(align_to(0, 16), 0); +/// ``` +/// +pub fn align_to(value: T, alignment: T) -> T +where + T: Add + Copy + Default + PartialEq + Rem + Sub, +{ + let remainder = value % alignment; + if remainder == T::default() { + value + } else { + value + alignment - remainder + } +} diff --git a/wgpu/src/util/mod.rs b/wgpu/src/util/mod.rs index 138205645..5c814e0c5 100644 --- a/wgpu/src/util/mod.rs +++ b/wgpu/src/util/mod.rs @@ -9,7 +9,6 @@ mod encoder; mod indirect; mod init; -use std::ops::{Add, Rem, Sub}; use std::sync::Arc; use std::{ borrow::Cow, @@ -22,6 +21,7 @@ pub use device::{BufferInitDescriptor, DeviceExt}; pub use encoder::RenderEncoder; pub use indirect::*; pub use init::*; +pub use wgt::math::*; /// Treat the given byte slice as a SPIR-V module. /// @@ -141,31 +141,3 @@ impl std::ops::Deref for DownloadBuffer { self.1.slice() } } - -/// -/// Aligns a `value` to an `alignment`. -/// -/// Returns the first number greater than or equal to `value` that is also a -/// multiple of `alignment`. If `value` is already a multiple of `alignment`, -/// `value` will be returned. -/// -/// # Examples -/// -/// ``` -/// # use wgpu::util::align_to; -/// assert_eq!(align_to(253, 16), 256); -/// assert_eq!(align_to(256, 16), 256); -/// assert_eq!(align_to(0, 16), 0); -/// ``` -/// -pub fn align_to(value: T, alignment: T) -> T -where - T: Add + Copy + Default + PartialEq + Rem + Sub, -{ - let remainder = value % alignment; - if remainder == T::default() { - value - } else { - value + alignment - remainder - } -}