Skip to content

Commit

Permalink
move align_to functions to wgpu_types crate
Browse files Browse the repository at this point in the history
  • Loading branch information
teoxoy committed Mar 2, 2023
1 parent af18a58 commit 145a85a
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 64 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 @@ -925,7 +925,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_u64(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
57 changes: 57 additions & 0 deletions wgpu-types/src/math.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//! Utilitary math functions.
///
/// 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: u32, alignment: u32) -> u32 {
if alignment.is_power_of_two() {
let mask = alignment - 1;
(value + mask) & !mask
} else {
match value % alignment {
0 => value,
other => value - other + alignment,
}
}
}

///
/// 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_u64;
/// assert_eq!(align_to_u64(253, 16), 256);
/// assert_eq!(align_to_u64(256, 16), 256);
/// assert_eq!(align_to_u64(0, 16), 0);
/// ```
///
pub fn align_to_u64(value: u64, alignment: u64) -> u64 {
if alignment.is_power_of_two() {
let mask = alignment - 1;
(value + mask) & !mask
} else {
match value % alignment {
0 => value,
other => value - other + alignment,
}
}
}
4 changes: 2 additions & 2 deletions wgpu/examples/shadow/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{borrow::Cow, f32::consts, iter, mem, ops::Range, rc::Rc};
mod framework;

use bytemuck::{Pod, Zeroable};
use wgpu::util::{align_to, DeviceExt};
use wgpu::util::DeviceExt;

#[repr(C)]
#[derive(Clone, Copy, Pod, Zeroable)]
Expand Down Expand Up @@ -292,7 +292,7 @@ impl framework::Example for Example {
let uniform_alignment = {
let alignment =
device.limits().min_uniform_buffer_offset_alignment as wgpu::BufferAddress;
align_to(entity_uniform_size, alignment)
wgt::math::align_to_u64(entity_uniform_size, alignment)
};
// Note: dynamic uniform offsets also have to be aligned to `Limits::min_uniform_buffer_offset_alignment`.
let entity_uniform_buf = device.create_buffer(&wgpu::BufferDescriptor {
Expand Down
6 changes: 3 additions & 3 deletions wgpu/src/util/belt.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
util::align_to, Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferUsages,
BufferViewMut, CommandEncoder, Device, MapMode,
Buffer, BufferAddress, BufferDescriptor, BufferSize, BufferUsages, BufferViewMut,
CommandEncoder, Device, MapMode,
};
use std::fmt;
use std::sync::{mpsc, Arc};
Expand Down Expand Up @@ -114,7 +114,7 @@ impl StagingBelt {

encoder.copy_buffer_to_buffer(&chunk.buffer, chunk.offset, target, offset, size.get());
let old_offset = chunk.offset;
chunk.offset = align_to(chunk.offset + size.get(), crate::MAP_ALIGNMENT);
chunk.offset = wgt::math::align_to_u64(chunk.offset + size.get(), crate::MAP_ALIGNMENT);

self.active_chunks.push(chunk);
self.active_chunks
Expand Down
29 changes: 0 additions & 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 Down Expand Up @@ -141,31 +140,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
}
}
5 changes: 3 additions & 2 deletions wgpu/tests/shader_primitive_index/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::common::{initialize_test, TestParameters, TestingContext};
use std::num::NonZeroU32;
use wasm_bindgen_test::*;
use wgpu::util::{align_to, DeviceExt};
use wgpu::util::DeviceExt;

//
// These tests render two triangles to a 2x2 render target. The first triangle
Expand Down Expand Up @@ -205,7 +205,8 @@ fn capture_rgba_u8_texture(
color_texture: wgpu::Texture,
texture_size: wgpu::Extent3d,
) -> Vec<u8> {
let bytes_per_row = align_to(4 * texture_size.width, wgpu::COPY_BYTES_PER_ROW_ALIGNMENT);
let bytes_per_row =
wgt::math::align_to(4 * texture_size.width, wgpu::COPY_BYTES_PER_ROW_ALIGNMENT);
let buffer_size = bytes_per_row * texture_size.height;
let output_buffer = ctx
.device
Expand Down

0 comments on commit 145a85a

Please sign in to comment.