Skip to content

Commit

Permalink
Merge #3762
Browse files Browse the repository at this point in the history
3762: Low level interop with Vulkan backend r=kvark a=zarik5

Related issues: #3698 #3761 blaind/xrbevy#1

This PR exposes some Vulkan-backend-specific details needed for interop with other low level APIs. In particular this PR aims to implement a minimum viable API to allow OpenXR integration as a separate crate.

This PR is nowhere complete. I opened it to get some feedback; this is my first PR on a project the size of gfx-hal.

This is the first of multiple PRs needed for OpenXR support in wgpu.

PR checklist:
- [ ] `make` succeeds (on *nix)
- [ ] `make reftests` succeeds
- [ ] tested examples with the following backends: Vulkan

EDIT: To make things clear, this is another take on #3761. Me and @blaind are working together on this route.

Co-authored-by: zarik5 <[email protected]>
  • Loading branch information
bors[bot] and zarik5 committed May 20, 2021
2 parents e16eacb + 6a0ff8b commit cfcc224
Show file tree
Hide file tree
Showing 3 changed files with 445 additions and 329 deletions.
74 changes: 44 additions & 30 deletions src/backend/vulkan/src/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1216,37 +1216,12 @@ impl d::Device<B> for super::Device {
let is_cube = image
.flags
.intersects(vk::ImageCreateFlags::CUBE_COMPATIBLE);
let mut image_view_info;
let mut info = vk::ImageViewCreateInfo::builder()
.flags(vk::ImageViewCreateFlags::empty())
.image(image.raw)
.view_type(match conv::map_view_kind(kind, image.ty, is_cube) {
Some(ty) => ty,
None => return Err(image::ViewCreationError::BadKind(kind)),
})
.format(conv::map_format(format))
.components(conv::map_swizzle(swizzle))
.subresource_range(conv::map_subresource_range(&range));

if self.shared.image_view_usage {
image_view_info = vk::ImageViewUsageCreateInfo::builder()
.usage(conv::map_image_usage(usage))
.build();
info = info.push_next(&mut image_view_info);
}

let result = self.shared.raw.create_image_view(&info, None);
let view_type = match conv::map_view_kind(kind, image.ty, is_cube) {
Some(ty) => ty,
None => return Err(image::ViewCreationError::BadKind(kind)),
};

match result {
Ok(raw) => Ok(n::ImageView {
image: image.raw,
raw,
range,
}),
Err(vk::Result::ERROR_OUT_OF_HOST_MEMORY) => Err(d::OutOfMemory::Host.into()),
Err(vk::Result::ERROR_OUT_OF_DEVICE_MEMORY) => Err(d::OutOfMemory::Device.into()),
_ => unreachable!(),
}
self.image_view_from_raw(image.raw, view_type, format, swizzle, usage, range)
}

unsafe fn create_descriptor_pool<T>(
Expand Down Expand Up @@ -2030,6 +2005,45 @@ impl super::Device {

Ok((swapchain, images))
}

pub unsafe fn image_view_from_raw(
&self,
raw_image: vk::Image,
view_type: vk::ImageViewType,
format: format::Format,
swizzle: format::Swizzle,
usage: image::Usage,
range: image::SubresourceRange,
) -> Result<n::ImageView, image::ViewCreationError> {
let mut image_view_info;
let mut info = vk::ImageViewCreateInfo::builder()
.flags(vk::ImageViewCreateFlags::empty())
.image(raw_image)
.view_type(view_type)
.format(conv::map_format(format))
.components(conv::map_swizzle(swizzle))
.subresource_range(conv::map_subresource_range(&range));

if self.shared.image_view_usage {
image_view_info = vk::ImageViewUsageCreateInfo::builder()
.usage(conv::map_image_usage(usage))
.build();
info = info.push_next(&mut image_view_info);
}

let result = self.shared.raw.create_image_view(&info, None);

match result {
Ok(raw) => Ok(n::ImageView {
image: raw_image,
raw,
range,
}),
Err(vk::Result::ERROR_OUT_OF_HOST_MEMORY) => Err(d::OutOfMemory::Host.into()),
Err(vk::Result::ERROR_OUT_OF_DEVICE_MEMORY) => Err(d::OutOfMemory::Device.into()),
_ => unreachable!(),
}
}
}

#[test]
Expand Down
Loading

0 comments on commit cfcc224

Please sign in to comment.