#[cfg(any(feature = "serde", test))]
use serde::{Deserialize, Serialize};
use crate::TextureDimension;
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Origin2d {
pub x: u32,
pub y: u32,
}
impl Origin2d {
pub const ZERO: Self = Self { x: 0, y: 0 };
#[must_use]
pub fn to_3d(self, z: u32) -> Origin3d {
Origin3d {
x: self.x,
y: self.y,
z,
}
}
}
impl core::fmt::Debug for Origin2d {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(self.x, self.y).fmt(f)
}
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Origin3d {
pub x: u32,
pub y: u32,
pub z: u32,
}
impl Origin3d {
pub const ZERO: Self = Self { x: 0, y: 0, z: 0 };
#[must_use]
pub fn to_2d(self) -> Origin2d {
Origin2d {
x: self.x,
y: self.y,
}
}
}
impl Default for Origin3d {
fn default() -> Self {
Self::ZERO
}
}
impl core::fmt::Debug for Origin3d {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(self.x, self.y, self.z).fmt(f)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Rect<T> {
pub x: T,
pub y: T,
pub w: T,
pub h: T,
}
impl Rect<u32> {
#[must_use]
pub fn intersect(&self, other: &Self) -> Option<Self> {
let left = self.x.max(other.x);
let top = self.y.max(other.y);
let right = self
.x
.saturating_add(self.w)
.min(other.x.saturating_add(other.w));
let bottom = self
.y
.saturating_add(self.h)
.min(other.y.saturating_add(other.h));
if right <= left || bottom <= top {
return None;
}
Some(Self {
x: left,
y: top,
w: right - left,
h: bottom - top,
})
}
}
#[repr(C)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct Extent3d {
pub width: u32,
pub height: u32,
#[cfg_attr(feature = "serde", serde(default = "default_depth"))]
pub depth_or_array_layers: u32,
}
impl core::fmt::Debug for Extent3d {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
(self.width, self.height, self.depth_or_array_layers).fmt(f)
}
}
#[cfg(feature = "serde")]
fn default_depth() -> u32 {
1
}
impl Default for Extent3d {
fn default() -> Self {
Self {
width: 1,
height: 1,
depth_or_array_layers: 1,
}
}
}
impl Extent3d {
#[must_use]
pub fn physical_size(&self, format: crate::TextureFormat) -> Self {
let (block_width, block_height) = format.block_dimensions();
let width = self.width.div_ceil(block_width) * block_width;
let height = self.height.div_ceil(block_height) * block_height;
Self {
width,
height,
depth_or_array_layers: self.depth_or_array_layers,
}
}
#[must_use]
pub fn max_mips(&self, dim: TextureDimension) -> u32 {
match dim {
TextureDimension::D1 => 1,
TextureDimension::D2 => {
let max_dim = self.width.max(self.height);
32 - max_dim.leading_zeros()
}
TextureDimension::D3 => {
let max_dim = self.width.max(self.height.max(self.depth_or_array_layers));
32 - max_dim.leading_zeros()
}
}
}
#[doc(hidden)]
#[must_use]
pub fn mip_level_size(&self, level: u32, dim: TextureDimension) -> Self {
Self {
width: u32::max(1, self.width.unbounded_shr(level)),
height: match dim {
TextureDimension::D1 => 1,
_ => u32::max(1, self.height.unbounded_shr(level)),
},
depth_or_array_layers: match dim {
TextureDimension::D1 => 1,
TextureDimension::D2 => self.depth_or_array_layers,
TextureDimension::D3 => {
u32::max(1, self.depth_or_array_layers.unbounded_shr(level))
}
},
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn rect(x: u32, y: u32, w: u32, h: u32) -> Rect<u32> {
Rect { x, y, w, h }
}
#[test]
fn intersect() {
let surface = rect(0, 0, 800, 600);
assert_eq!(
rect(10, 20, 100, 50).intersect(&surface),
Some(rect(10, 20, 100, 50))
);
assert_eq!(surface.intersect(&surface), Some(surface));
assert_eq!(
rect(750, 570, 100, 50).intersect(&surface),
Some(rect(750, 570, 50, 30))
);
assert_eq!(rect(900, 0, 100, 50).intersect(&surface), None);
assert_eq!(rect(0, 600, 100, 50).intersect(&surface), None);
assert_eq!(rect(10, 10, 0, 0).intersect(&surface), None);
}
#[test]
fn intersect_does_not_overflow() {
let surface = rect(0, 0, 800, 600);
assert_eq!(
rect(u32::MAX, u32::MAX, u32::MAX, u32::MAX).intersect(&surface),
None
);
assert_eq!(
rect(10, 10, u32::MAX, u32::MAX).intersect(&surface),
Some(rect(10, 10, 790, 590))
);
}
}