Skip to content

Commit c7f88e9

Browse files
committed
Add conversions from Extent2D to Extent3D and Rect2D
These two conversions occur all the time in Vulkan applications. For example, Extent2D -> Extent3D occurs whenever you need to make an image the same size as a surface and Extent2D -> Rect2D occurs whenever you fill out a scissors or render area from a surface resolution.
1 parent cebfd54 commit c7f88e9

File tree

4 files changed

+30
-32
lines changed

4 files changed

+30
-32
lines changed

ash/src/vk/prelude.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::vk;
2+
13
/// Holds 24 bits in the least significant bits of memory,
24
/// and 8 bytes in the most significant bits of that memory,
35
/// occupying a single [`u32`] in total. This is commonly used in
@@ -31,3 +33,22 @@ impl super::ColorComponentFlags {
3133
/// Contraction of [`Self::R`] | [`Self::G`] | [`Self::B`] | [`Self::A`]
3234
pub const RGBA: Self = Self(Self::R.0 | Self::G.0 | Self::B.0 | Self::A.0);
3335
}
36+
37+
impl From<vk::Extent2D> for vk::Extent3D {
38+
fn from(value: vk::Extent2D) -> Self {
39+
Self {
40+
width: value.width,
41+
height: value.height,
42+
depth: 1,
43+
}
44+
}
45+
}
46+
47+
impl From<vk::Extent2D> for vk::Rect2D {
48+
fn from(extent: vk::Extent2D) -> Self {
49+
Self {
50+
offset: Default::default(),
51+
extent,
52+
}
53+
}
54+
}

examples/src/bin/texture.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,8 @@ fn main() {
264264
let image = image::load_from_memory(include_bytes!("../../assets/rust.png"))
265265
.unwrap()
266266
.to_rgba8();
267-
let image_dimensions = image.dimensions();
267+
let (width, height) = image.dimensions();
268+
let image_dimensions = vk::Extent2D { width, height };
268269
let image_data = image.into_raw();
269270
let image_buffer_info = vk::BufferCreateInfo {
270271
size: (std::mem::size_of::<u8>() * image_data.len()) as u64,
@@ -313,11 +314,7 @@ fn main() {
313314
let texture_create_info = vk::ImageCreateInfo {
314315
image_type: vk::ImageType::TYPE_2D,
315316
format: vk::Format::R8G8B8A8_UNORM,
316-
extent: vk::Extent3D {
317-
width: image_dimensions.0,
318-
height: image_dimensions.1,
319-
depth: 1,
320-
},
317+
extent: image_dimensions.into(),
321318
mip_levels: 1,
322319
array_layers: 1,
323320
samples: vk::SampleCountFlags::TYPE_1,
@@ -388,11 +385,7 @@ fn main() {
388385
.layer_count(1)
389386
.build(),
390387
)
391-
.image_extent(vk::Extent3D {
392-
width: image_dimensions.0,
393-
height: image_dimensions.1,
394-
depth: 1,
395-
});
388+
.image_extent(image_dimensions.into());
396389

397390
device.cmd_copy_buffer_to_image(
398391
texture_command_buffer,
@@ -623,10 +616,7 @@ fn main() {
623616
min_depth: 0.0,
624617
max_depth: 1.0,
625618
}];
626-
let scissors = [vk::Rect2D {
627-
extent: base.surface_resolution,
628-
..Default::default()
629-
}];
619+
let scissors = [base.surface_resolution.into()];
630620
let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
631621
.scissors(&scissors)
632622
.viewports(&viewports);
@@ -727,10 +717,7 @@ fn main() {
727717
let render_pass_begin_info = vk::RenderPassBeginInfo::builder()
728718
.render_pass(renderpass)
729719
.framebuffer(framebuffers[present_index as usize])
730-
.render_area(vk::Rect2D {
731-
offset: vk::Offset2D { x: 0, y: 0 },
732-
extent: base.surface_resolution,
733-
})
720+
.render_area(base.surface_resolution.into())
734721
.clear_values(&clear_values);
735722

736723
record_submit_commandbuffer(

examples/src/bin/triangle.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,7 @@ fn main() {
277277
min_depth: 0.0,
278278
max_depth: 1.0,
279279
}];
280-
let scissors = [vk::Rect2D {
281-
offset: vk::Offset2D { x: 0, y: 0 },
282-
extent: base.surface_resolution,
283-
}];
280+
let scissors = [base.surface_resolution.into()];
284281
let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
285282
.scissors(&scissors)
286283
.viewports(&viewports);
@@ -380,10 +377,7 @@ fn main() {
380377
let render_pass_begin_info = vk::RenderPassBeginInfo::builder()
381378
.render_pass(renderpass)
382379
.framebuffer(framebuffers[present_index as usize])
383-
.render_area(vk::Rect2D {
384-
offset: vk::Offset2D { x: 0, y: 0 },
385-
extent: base.surface_resolution,
386-
})
380+
.render_area(base.surface_resolution.into())
387381
.clear_values(&clear_values);
388382

389383
record_submit_commandbuffer(

examples/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,7 @@ impl ExampleBase {
425425
let depth_image_create_info = vk::ImageCreateInfo::builder()
426426
.image_type(vk::ImageType::TYPE_2D)
427427
.format(vk::Format::D16_UNORM)
428-
.extent(vk::Extent3D {
429-
width: surface_resolution.width,
430-
height: surface_resolution.height,
431-
depth: 1,
432-
})
428+
.extent(surface_resolution.into())
433429
.mip_levels(1)
434430
.array_layers(1)
435431
.samples(vk::SampleCountFlags::TYPE_1)

0 commit comments

Comments
 (0)