wgpu_types/
origin_extent.rs1#[cfg(any(feature = "serde", test))]
2use serde::{Deserialize, Serialize};
3
4use crate::TextureDimension;
5
6#[repr(C)]
11#[derive(Clone, Copy, PartialEq, Eq, Hash)]
12#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
13#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
14pub struct Origin2d {
15 #[allow(missing_docs)]
16 pub x: u32,
17 #[allow(missing_docs)]
18 pub y: u32,
19}
20
21impl Origin2d {
22 pub const ZERO: Self = Self { x: 0, y: 0 };
24
25 #[must_use]
27 pub fn to_3d(self, z: u32) -> Origin3d {
28 Origin3d {
29 x: self.x,
30 y: self.y,
31 z,
32 }
33 }
34}
35
36impl core::fmt::Debug for Origin2d {
37 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38 (self.x, self.y).fmt(f)
39 }
40}
41
42#[repr(C)]
47#[derive(Clone, Copy, PartialEq, Eq, Hash)]
48#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
49#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
50pub struct Origin3d {
51 pub x: u32,
53 pub y: u32,
55 pub z: u32,
57}
58
59impl Origin3d {
60 pub const ZERO: Self = Self { x: 0, y: 0, z: 0 };
62
63 #[must_use]
65 pub fn to_2d(self) -> Origin2d {
66 Origin2d {
67 x: self.x,
68 y: self.y,
69 }
70 }
71}
72
73impl Default for Origin3d {
74 fn default() -> Self {
75 Self::ZERO
76 }
77}
78
79impl core::fmt::Debug for Origin3d {
80 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
81 (self.x, self.y, self.z).fmt(f)
82 }
83}
84
85#[repr(C)]
90#[derive(Clone, Copy, PartialEq, Eq, Hash)]
91#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
92#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
93pub struct Extent3d {
94 pub width: u32,
96 pub height: u32,
98 #[cfg_attr(feature = "serde", serde(default = "default_depth"))]
100 pub depth_or_array_layers: u32,
101}
102
103impl core::fmt::Debug for Extent3d {
104 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
105 (self.width, self.height, self.depth_or_array_layers).fmt(f)
106 }
107}
108
109#[cfg(feature = "serde")]
110fn default_depth() -> u32 {
111 1
112}
113
114impl Default for Extent3d {
115 fn default() -> Self {
116 Self {
117 width: 1,
118 height: 1,
119 depth_or_array_layers: 1,
120 }
121 }
122}
123
124impl Extent3d {
125 #[must_use]
133 pub fn physical_size(&self, format: crate::TextureFormat) -> Self {
134 let (block_width, block_height) = format.block_dimensions();
135
136 let width = self.width.div_ceil(block_width) * block_width;
137 let height = self.height.div_ceil(block_height) * block_height;
138
139 Self {
140 width,
141 height,
142 depth_or_array_layers: self.depth_or_array_layers,
143 }
144 }
145
146 #[must_use]
151 pub fn max_mips(&self, dim: TextureDimension) -> u32 {
152 match dim {
153 TextureDimension::D1 => 1,
154 TextureDimension::D2 => {
155 let max_dim = self.width.max(self.height);
156 32 - max_dim.leading_zeros()
157 }
158 TextureDimension::D3 => {
159 let max_dim = self.width.max(self.height.max(self.depth_or_array_layers));
160 32 - max_dim.leading_zeros()
161 }
162 }
163 }
164
165 #[doc(hidden)]
177 #[must_use]
178 pub fn mip_level_size(&self, level: u32, dim: TextureDimension) -> Self {
179 Self {
180 width: u32::max(1, self.width >> level),
181 height: match dim {
182 TextureDimension::D1 => 1,
183 _ => u32::max(1, self.height >> level),
184 },
185 depth_or_array_layers: match dim {
186 TextureDimension::D1 => 1,
187 TextureDimension::D2 => self.depth_or_array_layers,
188 TextureDimension::D3 => u32::max(1, self.depth_or_array_layers >> level),
189 },
190 }
191 }
192}