[metal] Fix metal erroring on an array_stride of 0 (#3538)

Co-authored-by: Connor Fitzgerald <connorwadefitzgerald@gmail.com>
This commit is contained in:
Teodor TanasoaiaandConnor Fitzgerald authored and GitHub committed 2023-04-05 22:40:15 +00:00
1 parent a13ed91626
commit 6918cf33d4
10 files changed
+61 -47

No files matched your search

+4
View File
@@ -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)
+4 -2
View File
@@ -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)]
+1 -1
View File
@@ -664,7 +664,7 @@ impl<G: GlobalIdentityHandlerFactory> Global<G> {
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;
+1 -1
View File
@@ -395,7 +395,7 @@ impl<A: hal::Api> Example<A> {
buffer
};
let local_alignment = hal::auxil::align_to(
let local_alignment = wgt::math::align_to(
mem::size_of::<Locals>() as u32,
capabilities.limits.min_uniform_buffer_offset_alignment,
);
-11
View File
@@ -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 {
+1 -1
View File
@@ -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)
},
},
}
+17 -2
View File
@@ -932,8 +932,23 @@ impl crate::Device<super::Api> 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
+1
View File
@@ -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
+31
View File
@@ -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<T>(value: T, alignment: T) -> T
where
T: Add<Output = T> + Copy + Default + PartialEq<T> + Rem<Output = T> + Sub<Output = T>,
{
let remainder = value % alignment;
if remainder == T::default() {
value
} else {
value + alignment - remainder
}
}
+1 -29
View File
@@ -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<T>(value: T, alignment: T) -> T
where
T: Add<Output = T> + Copy + Default + PartialEq<T> + Rem<Output = T> + Sub<Output = T>,
{
let remainder = value % alignment;
if remainder == T::default() {
value
} else {
value + alignment - remainder
}
}