Skip to content

Commit

Permalink
move align_to function to wgpu_types crate
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Mar 4, 2023
1 parent e1d67ba commit cca15cb
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 57 deletions.
6 changes: 4 additions & 2 deletions wgpu-core/src/command/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion wgpu-core/src/device/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,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;
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/examples/halmark/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,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,
);
Expand Down
22 changes: 0 additions & 22 deletions wgpu-hal/src/auxil/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,28 +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,
}
}
}

pub fn align_to_u64(value: u64, alignment: u64) -> u64 {
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 {
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/dx12/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl crate::BufferTextureCopy {
(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)
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion wgpu-hal/src/metal/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ impl crate::Device<super::Api> for super::Device {
.map(|attribute| attribute.offset + attribute.format.size())
.max()
.unwrap_or(0);
buffer_desc.set_stride(crate::auxil::align_to_u64(stride, 4));
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 {
Expand Down
1 change: 1 addition & 0 deletions wgpu-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions wgpu-types/src/math.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
30 changes: 1 addition & 29 deletions wgpu/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod encoder;
mod indirect;
mod init;

use std::ops::{Add, Rem, Sub};
use std::sync::Arc;
use std::{
borrow::Cow,
Expand All @@ -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.
///
Expand Down Expand Up @@ -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
}
}

0 comments on commit cca15cb

Please sign in to comment.