1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#[cfg(any(feature = "serde", test))]
use serde::{Deserialize, Serialize};
use crate::TextureDimension;
/// Origin of a copy from a 2D image.
///
/// Corresponds to [WebGPU `GPUOrigin2D`](
/// https://gpuweb.github.io/gpuweb/#dictdef-gpuorigin2ddict).
#[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 {
#[allow(missing_docs)]
pub x: u32,
#[allow(missing_docs)]
pub y: u32,
}
impl Origin2d {
/// Zero origin.
pub const ZERO: Self = Self { x: 0, y: 0 };
/// Adds the third dimension to this origin
#[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)
}
}
/// Origin of a copy to/from a texture.
///
/// Corresponds to [WebGPU `GPUOrigin3D`](
/// https://gpuweb.github.io/gpuweb/#dictdef-gpuorigin3ddict).
#[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 {
/// X position of the origin
pub x: u32,
/// Y position of the origin
pub y: u32,
/// Z position of the origin
pub z: u32,
}
impl Origin3d {
/// Zero origin.
pub const ZERO: Self = Self { x: 0, y: 0, z: 0 };
/// Removes the third dimension from this origin
#[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)
}
}
/// Extent of a texture related operation.
///
/// Corresponds to [WebGPU `GPUExtent3D`](
/// https://gpuweb.github.io/gpuweb/#dictdef-gpuextent3ddict).
#[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 {
/// Width of the extent
pub width: u32,
/// Height of the extent
pub height: u32,
/// The depth of the extent or the number of array layers
#[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 {
/// Calculates the [physical size] backing a texture of the given
/// format and extent. This includes padding to the block width
/// and height of the format.
///
/// This is the texture extent that you must upload at when uploading to _mipmaps_ of compressed textures.
///
/// [physical size]: https://gpuweb.github.io/gpuweb/#physical-miplevel-specific-texture-extent
#[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,
}
}
/// Calculates the maximum possible count of mipmaps.
///
/// Treats the depth as part of the mipmaps. If calculating
/// for a 2DArray texture, which does not mipmap depth, set depth to 1.
#[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()
}
}
}
/// Calculates the extent at a given mip level.
///
/// This is a low-level helper for internal use.
///
/// It does *not* account for memory size being a multiple of block size.
///
/// TODO(<https://github.com/gfx-rs/wgpu/issues/8491>): It also does not
/// consider whether an even dimension is required due to chroma
/// subsampling, but it probably should.
///
/// <https://gpuweb.github.io/gpuweb/#logical-miplevel-specific-texture-extent>
#[doc(hidden)]
#[must_use]
pub fn mip_level_size(&self, level: u32, dim: TextureDimension) -> Self {
Self {
width: u32::max(1, self.width >> level),
height: match dim {
TextureDimension::D1 => 1,
_ => u32::max(1, self.height >> 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 >> level),
},
}
}
}