Skip to main content

bedrock_vk/
extensions.rs

1use crate::*;
2
3// Spreading single value to all dimensions
4impl VkExtent2D {
5    pub const fn spread1(value: u32) -> Self {
6        Self {
7            width: value,
8            height: value,
9        }
10    }
11}
12impl VkExtent3D {
13    pub const fn spread1(value: u32) -> Self {
14        Self {
15            width: value,
16            height: value,
17            depth: value,
18        }
19    }
20
21    pub const fn new1(width: u32) -> Self {
22        Self {
23            width,
24            height: 1,
25            depth: 1,
26        }
27    }
28
29    pub const fn new2(width: u32, height: u32) -> Self {
30        Self {
31            width,
32            height,
33            depth: 1,
34        }
35    }
36
37    pub const fn new(width: u32, height: u32, depth: u32) -> Self {
38        Self { width, height, depth }
39    }
40
41    pub const fn as_2d_ref(&self) -> &VkExtent2D {
42        unsafe { std::mem::transmute(self) }
43    }
44}
45impl VkOffset2D {
46    pub const fn spread1(value: i32) -> Self {
47        Self { x: value, y: value }
48    }
49}
50impl VkOffset3D {
51    pub const fn spread1(value: i32) -> Self {
52        Self {
53            x: value,
54            y: value,
55            z: value,
56        }
57    }
58
59    pub const fn new1(x: i32) -> Self {
60        Self { x, y: 0, z: 0 }
61    }
62
63    pub const fn new2(x: i32, y: i32) -> Self {
64        Self { x, y, z: 0 }
65    }
66
67    pub const fn new(x: i32, y: i32, z: i32) -> Self {
68        Self { x, y, z }
69    }
70
71    pub const fn as_2d_ref(&self) -> &VkOffset2D {
72        unsafe { std::mem::transmute(self) }
73    }
74}
75
76// into conversion to larger dimension //
77impl VkExtent2D {
78    pub const fn with_depth(self, depth: u32) -> VkExtent3D {
79        VkExtent3D {
80            width: self.width,
81            height: self.height,
82            depth,
83        }
84    }
85}
86impl VkOffset2D {
87    pub const fn with_z(self, z: i32) -> VkOffset3D {
88        VkOffset3D {
89            x: self.x,
90            y: self.y,
91            z,
92        }
93    }
94}
95
96// AsRef for self //
97impl AsRef<VkExtent3D> for VkExtent3D {
98    fn as_ref(&self) -> &Self {
99        self
100    }
101}
102impl AsRef<VkExtent2D> for VkExtent2D {
103    fn as_ref(&self) -> &Self {
104        self
105    }
106}
107impl AsRef<VkOffset3D> for VkOffset3D {
108    fn as_ref(&self) -> &Self {
109        self
110    }
111}
112impl AsRef<VkOffset2D> for VkOffset2D {
113    fn as_ref(&self) -> &Self {
114        self
115    }
116}
117
118// AsRef Conversion to smaller-dimension
119impl AsRef<VkExtent2D> for VkExtent3D {
120    fn as_ref(&self) -> &VkExtent2D {
121        unsafe { core::mem::transmute(self) }
122    }
123}
124impl AsRef<VkOffset2D> for VkOffset3D {
125    fn as_ref(&self) -> &VkOffset2D {
126        unsafe { core::mem::transmute(self) }
127    }
128}
129
130// From conversion to smaller-dimension
131impl From<VkOffset3D> for VkOffset2D {
132    fn from(value: VkOffset3D) -> Self {
133        Self { x: value.x, y: value.y }
134    }
135}
136
137impl From<VkExtent3D> for VkExtent2D {
138    fn from(value: VkExtent3D) -> Self {
139        Self {
140            width: value.width,
141            height: value.height,
142        }
143    }
144}
145
146// Swizzling
147impl VkExtent3D {
148    pub const fn wh(&self) -> VkExtent2D {
149        VkExtent2D {
150            width: self.width,
151            height: self.height,
152        }
153    }
154
155    pub const fn wd(&self) -> VkExtent2D {
156        VkExtent2D {
157            width: self.width,
158            height: self.depth,
159        }
160    }
161
162    pub const fn hw(&self) -> VkExtent2D {
163        VkExtent2D {
164            width: self.height,
165            height: self.width,
166        }
167    }
168
169    pub const fn hd(&self) -> VkExtent2D {
170        VkExtent2D {
171            width: self.height,
172            height: self.depth,
173        }
174    }
175
176    pub const fn dw(&self) -> VkExtent2D {
177        VkExtent2D {
178            width: self.depth,
179            height: self.width,
180        }
181    }
182
183    pub const fn dh(&self) -> VkExtent2D {
184        VkExtent2D {
185            width: self.depth,
186            height: self.height,
187        }
188    }
189}
190impl VkOffset3D {
191    pub const fn xy(&self) -> VkOffset2D {
192        VkOffset2D { x: self.x, y: self.y }
193    }
194
195    pub const fn xz(&self) -> VkOffset2D {
196        VkOffset2D { x: self.x, y: self.z }
197    }
198
199    pub const fn yx(&self) -> VkOffset2D {
200        VkOffset2D { x: self.y, y: self.x }
201    }
202
203    pub const fn yz(&self) -> VkOffset2D {
204        VkOffset2D { x: self.y, y: self.z }
205    }
206
207    pub const fn zx(&self) -> VkOffset2D {
208        VkOffset2D { x: self.z, y: self.x }
209    }
210
211    pub const fn zy(&self) -> VkOffset2D {
212        VkOffset2D { x: self.z, y: self.y }
213    }
214}
215
216/// Utility Constants
217impl VkExtent2D {
218    pub const ONE: Self = Self::spread1(1);
219}
220impl VkExtent3D {
221    pub const ONE: Self = Self::spread1(1);
222}
223impl VkOffset2D {
224    pub const ZERO: Self = Self::spread1(0);
225}
226impl VkOffset3D {
227    pub const ZERO: Self = Self::spread1(0);
228}
229
230/// Viewport and Rect Util Functions
231impl VkExtent2D {
232    pub const fn into_rect(self, offset: VkOffset2D) -> VkRect2D {
233        VkRect2D { offset, extent: self }
234    }
235}
236impl From<VkViewport> for VkRect2D {
237    fn from(vp: VkViewport) -> Self {
238        VkRect2D {
239            offset: VkOffset2D {
240                x: vp.x as _,
241                y: vp.y as _,
242            },
243            extent: VkExtent2D {
244                width: vp.width as _,
245                height: vp.height as _,
246            },
247        }
248    }
249}
250impl VkRect2D {
251    pub const fn make_viewport(&self, depth_range: std::ops::Range<f32>) -> VkViewport {
252        VkViewport {
253            x: self.offset.x as _,
254            y: self.offset.y as _,
255            width: self.extent.width as _,
256            height: self.extent.height as _,
257            minDepth: depth_range.start,
258            maxDepth: depth_range.end,
259        }
260    }
261}
262impl VkViewport {
263    pub const fn from_rect_with_depth_range(rect: &VkRect2D, depth_range: core::ops::Range<f32>) -> Self {
264        rect.make_viewport(depth_range)
265    }
266
267    pub const fn set_offset(&mut self, offset: &VkOffset2D) -> &mut Self {
268        self.x = offset.x as _;
269        self.y = offset.y as _;
270        self
271    }
272    pub const fn set_extent(&mut self, extent: &VkExtent2D) -> &mut Self {
273        self.width = extent.width as _;
274        self.height = extent.height as _;
275        self
276    }
277    pub const fn set_depth_range(&mut self, range: core::ops::Range<f32>) -> &mut Self {
278        self.minDepth = range.start;
279        self.maxDepth = range.end;
280        self
281    }
282}