Skip to content

Commit

Permalink
Add conversions from Extent2D to Extent3D and Rect2D (#557)
Browse files Browse the repository at this point in the history
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.

Co-authored-by: Steve Wooster <s.f.m.wooster@gmail.com>
  • Loading branch information
swooster and swooster authored Jan 22, 2022
1 parent cebfd54 commit fbcc45f
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 32 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - ReleaseDate

### Added

- Added conversions from `Extent2D` to `Extent3D` and `Rect2D` (#557)

## [0.35.1] - 2022-01-18

### Added
Expand Down
21 changes: 21 additions & 0 deletions ash/src/vk/prelude.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::vk;

/// Holds 24 bits in the least significant bits of memory,
/// and 8 bytes in the most significant bits of that memory,
/// occupying a single [`u32`] in total. This is commonly used in
Expand Down Expand Up @@ -31,3 +33,22 @@ impl super::ColorComponentFlags {
/// Contraction of [`Self::R`] | [`Self::G`] | [`Self::B`] | [`Self::A`]
pub const RGBA: Self = Self(Self::R.0 | Self::G.0 | Self::B.0 | Self::A.0);
}

impl From<vk::Extent2D> for vk::Extent3D {
fn from(value: vk::Extent2D) -> Self {
Self {
width: value.width,
height: value.height,
depth: 1,
}
}
}

impl From<vk::Extent2D> for vk::Rect2D {
fn from(extent: vk::Extent2D) -> Self {
Self {
offset: Default::default(),
extent,
}
}
}
25 changes: 6 additions & 19 deletions examples/src/bin/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ fn main() {
let image = image::load_from_memory(include_bytes!("../../assets/rust.png"))
.unwrap()
.to_rgba8();
let image_dimensions = image.dimensions();
let (width, height) = image.dimensions();
let image_extent = vk::Extent2D { width, height };
let image_data = image.into_raw();
let image_buffer_info = vk::BufferCreateInfo {
size: (std::mem::size_of::<u8>() * image_data.len()) as u64,
Expand Down Expand Up @@ -313,11 +314,7 @@ fn main() {
let texture_create_info = vk::ImageCreateInfo {
image_type: vk::ImageType::TYPE_2D,
format: vk::Format::R8G8B8A8_UNORM,
extent: vk::Extent3D {
width: image_dimensions.0,
height: image_dimensions.1,
depth: 1,
},
extent: image_extent.into(),
mip_levels: 1,
array_layers: 1,
samples: vk::SampleCountFlags::TYPE_1,
Expand Down Expand Up @@ -388,11 +385,7 @@ fn main() {
.layer_count(1)
.build(),
)
.image_extent(vk::Extent3D {
width: image_dimensions.0,
height: image_dimensions.1,
depth: 1,
});
.image_extent(image_extent.into());

device.cmd_copy_buffer_to_image(
texture_command_buffer,
Expand Down Expand Up @@ -623,10 +616,7 @@ fn main() {
min_depth: 0.0,
max_depth: 1.0,
}];
let scissors = [vk::Rect2D {
extent: base.surface_resolution,
..Default::default()
}];
let scissors = [base.surface_resolution.into()];
let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
.scissors(&scissors)
.viewports(&viewports);
Expand Down Expand Up @@ -727,10 +717,7 @@ fn main() {
let render_pass_begin_info = vk::RenderPassBeginInfo::builder()
.render_pass(renderpass)
.framebuffer(framebuffers[present_index as usize])
.render_area(vk::Rect2D {
offset: vk::Offset2D { x: 0, y: 0 },
extent: base.surface_resolution,
})
.render_area(base.surface_resolution.into())
.clear_values(&clear_values);

record_submit_commandbuffer(
Expand Down
10 changes: 2 additions & 8 deletions examples/src/bin/triangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,7 @@ fn main() {
min_depth: 0.0,
max_depth: 1.0,
}];
let scissors = [vk::Rect2D {
offset: vk::Offset2D { x: 0, y: 0 },
extent: base.surface_resolution,
}];
let scissors = [base.surface_resolution.into()];
let viewport_state_info = vk::PipelineViewportStateCreateInfo::builder()
.scissors(&scissors)
.viewports(&viewports);
Expand Down Expand Up @@ -380,10 +377,7 @@ fn main() {
let render_pass_begin_info = vk::RenderPassBeginInfo::builder()
.render_pass(renderpass)
.framebuffer(framebuffers[present_index as usize])
.render_area(vk::Rect2D {
offset: vk::Offset2D { x: 0, y: 0 },
extent: base.surface_resolution,
})
.render_area(base.surface_resolution.into())
.clear_values(&clear_values);

record_submit_commandbuffer(
Expand Down
6 changes: 1 addition & 5 deletions examples/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,11 +425,7 @@ impl ExampleBase {
let depth_image_create_info = vk::ImageCreateInfo::builder()
.image_type(vk::ImageType::TYPE_2D)
.format(vk::Format::D16_UNORM)
.extent(vk::Extent3D {
width: surface_resolution.width,
height: surface_resolution.height,
depth: 1,
})
.extent(surface_resolution.into())
.mip_levels(1)
.array_layers(1)
.samples(vk::SampleCountFlags::TYPE_1)
Expand Down

0 comments on commit fbcc45f

Please sign in to comment.