Skip to content

Commit

Permalink
WIP: IO safety
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Sep 2, 2023
1 parent 0652aee commit 5ecc662
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 31 deletions.
19 changes: 14 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@ name: CI
jobs:
check:
name: Check
runs-on: ubuntu-latest
strategy:
matrix:
os: [windows-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- run: cargo check --workspace --all-targets --all-features

check_msrv:
name: Check ash MSRV (1.60.0)
runs-on: ubuntu-latest
name: Check ash MSRV (1.63.0)
strategy:
matrix:
os: [windows-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: dtolnay/rust-toolchain@1.60.0
- uses: dtolnay/rust-toolchain@1.63.0
- run: cargo check -p ash -p ash-rewrite --all-features

check_ash_window_msrv:
name: Check ash-window MSRV (1.64.0)
runs-on: ubuntu-latest
strategy:
matrix:
os: [windows-latest, ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- uses: dtolnay/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion ash/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "../README.md"
keywords = ["vulkan", "graphic"]
documentation = "https://docs.rs/ash"
edition = "2021"
rust-version = "1.60.0"
rust-version = "1.63.0"

[dependencies]
libloading = { version = "0.7", optional = true }
Expand Down
16 changes: 11 additions & 5 deletions ash/src/extensions/ext/acquire_drm_display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::vk;
use crate::{Entry, Instance};
use std::ffi::CStr;
use std::mem;
use std::os::unix::io::{AsRawFd, BorrowedFd};

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_EXT_acquire_drm_display.html>
#[derive(Clone)]
Expand All @@ -24,23 +25,28 @@ impl AcquireDrmDisplay {
pub unsafe fn acquire_drm_display(
&self,
physical_device: vk::PhysicalDevice,
drm_fd: i32,
drm_fd: BorrowedFd<'_>,
display: vk::DisplayKHR,
) -> VkResult<()> {
(self.fp.acquire_drm_display_ext)(physical_device, drm_fd, display).result()
(self.fp.acquire_drm_display_ext)(physical_device, drm_fd.as_raw_fd(), display).result()
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetDrmDisplayEXT.html>
#[inline]
pub unsafe fn get_drm_display(
&self,
physical_device: vk::PhysicalDevice,
drm_fd: i32,
drm_fd: BorrowedFd<'_>,
connector_id: u32,
) -> VkResult<vk::DisplayKHR> {
let mut display = mem::MaybeUninit::uninit();
(self.fp.get_drm_display_ext)(physical_device, drm_fd, connector_id, display.as_mut_ptr())
.assume_init_on_success(display)
(self.fp.get_drm_display_ext)(
physical_device,
drm_fd.as_raw_fd(),
connector_id,
display.as_mut_ptr(),
)
.assume_init_on_success(display)
}

pub const NAME: &'static CStr = vk::ExtAcquireDrmDisplayFn::NAME;
Expand Down
10 changes: 8 additions & 2 deletions ash/src/extensions/khr/external_fence_fd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// TODO: This bound is only needed for MSRV 1.63 where Fd wrappers are not available in os::fd yet
#![cfg(unix)]

use crate::prelude::*;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
// use std::os::fd::OwnedFd;
use std::os::unix::io::{FromRawFd, OwnedFd};

#[derive(Clone)]
pub struct ExternalFenceFd {
Expand All @@ -27,9 +32,10 @@ impl ExternalFenceFd {

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetFenceFdKHR.html>
#[inline]
pub unsafe fn get_fence_fd(&self, get_info: &vk::FenceGetFdInfoKHR) -> VkResult<i32> {
pub unsafe fn get_fence_fd(&self, get_info: &vk::FenceGetFdInfoKHR) -> VkResult<OwnedFd> {
let mut fd = -1;
(self.fp.get_fence_fd_khr)(self.handle, get_info, &mut fd).result_with_success(fd)
(self.fp.get_fence_fd_khr)(self.handle, get_info, &mut fd).result()?;
Ok(OwnedFd::from_raw_fd(fd))
}

pub const NAME: &'static CStr = vk::KhrExternalFenceFdFn::NAME;
Expand Down
9 changes: 6 additions & 3 deletions ash/src/extensions/khr/external_fence_win32.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![cfg(windows)]

use crate::prelude::*;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use std::os::windows::io::{FromRawHandle, OwnedHandle};
use std::ptr;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_KHR_external_fence_win32.html>
Expand Down Expand Up @@ -35,10 +38,10 @@ impl ExternalFenceWin32 {
pub unsafe fn get_fence_win32_handle(
&self,
get_info: &vk::FenceGetWin32HandleInfoKHR,
) -> VkResult<vk::HANDLE> {
) -> VkResult<OwnedHandle> {
let mut handle = ptr::null_mut();
(self.fp.get_fence_win32_handle_khr)(self.handle, get_info, &mut handle)
.result_with_success(handle)
(self.fp.get_fence_win32_handle_khr)(self.handle, get_info, &mut handle).result()?;
Ok(OwnedHandle::from_raw_handle(handle))
}

pub const NAME: &'static CStr = vk::KhrExternalFenceWin32Fn::NAME;
Expand Down
20 changes: 15 additions & 5 deletions ash/src/extensions/khr/external_memory_fd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// TODO: This bound is only needed for MSRV 1.63 where Fd wrappers are not available in os::fd yet
#![cfg(unix)]

use crate::prelude::*;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use std::os::unix::io::{AsRawFd, BorrowedFd, FromRawFd, OwnedFd};

/// <https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/VK_KHR_external_memory_fd.html>
#[derive(Clone)]
Expand All @@ -22,21 +26,27 @@ impl ExternalMemoryFd {

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetMemoryFdKHR.html>
#[inline]
pub unsafe fn get_memory_fd(&self, get_fd_info: &vk::MemoryGetFdInfoKHR) -> VkResult<i32> {
pub unsafe fn get_memory_fd(&self, get_fd_info: &vk::MemoryGetFdInfoKHR) -> VkResult<OwnedFd> {
let mut fd = -1;
(self.fp.get_memory_fd_khr)(self.handle, get_fd_info, &mut fd).result_with_success(fd)
(self.fp.get_memory_fd_khr)(self.handle, get_fd_info, &mut fd).result()?;
Ok(OwnedFd::from_raw_fd(fd))
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetMemoryFdPropertiesKHR.html>
#[inline]
pub unsafe fn get_memory_fd_properties(
&self,
handle_type: vk::ExternalMemoryHandleTypeFlags,
fd: i32,
fd: BorrowedFd<'_>,
memory_fd_properties: &mut vk::MemoryFdPropertiesKHR,
) -> VkResult<()> {
(self.fp.get_memory_fd_properties_khr)(self.handle, handle_type, fd, memory_fd_properties)
.result()
(self.fp.get_memory_fd_properties_khr)(
self.handle,
handle_type,
fd.as_raw_fd(),
memory_fd_properties,
)
.result()
}

pub const NAME: &'static CStr = vk::KhrExternalMemoryFdFn::NAME;
Expand Down
13 changes: 8 additions & 5 deletions ash/src/extensions/khr/external_memory_win32.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![cfg(windows)]

use crate::prelude::*;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use std::os::windows::io::{AsRawHandle, BorrowedHandle, FromRawHandle, OwnedHandle};
use std::ptr;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_KHR_external_memory_win32.html>
Expand All @@ -26,24 +29,24 @@ impl ExternalMemoryWin32 {
pub unsafe fn get_memory_win32_handle(
&self,
create_info: &vk::MemoryGetWin32HandleInfoKHR,
) -> VkResult<vk::HANDLE> {
) -> VkResult<OwnedHandle> {
let mut handle = ptr::null_mut();
(self.fp.get_memory_win32_handle_khr)(self.handle, create_info, &mut handle)
.result_with_success(handle)
(self.fp.get_memory_win32_handle_khr)(self.handle, create_info, &mut handle).result()?;
Ok(OwnedHandle::from_raw_handle(handle))
}

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetMemoryWin32HandlePropertiesKHR.html>
#[inline]
pub unsafe fn get_memory_win32_handle_properties(
&self,
handle_type: vk::ExternalMemoryHandleTypeFlags,
handle: vk::HANDLE,
handle: BorrowedHandle<'_>,
memory_win32_handle_properties: &mut vk::MemoryWin32HandlePropertiesKHR,
) -> VkResult<()> {
(self.fp.get_memory_win32_handle_properties_khr)(
self.handle,
handle_type,
handle,
handle.as_raw_handle(),
memory_win32_handle_properties,
)
.result()
Expand Down
12 changes: 10 additions & 2 deletions ash/src/extensions/khr/external_semaphore_fd.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// TODO: This bound is only needed for MSRV 1.63 where Fd wrappers are not available in os::fd yet
#![cfg(unix)]

use crate::prelude::*;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use std::os::unix::io::{FromRawFd, OwnedFd};

#[derive(Clone)]
pub struct ExternalSemaphoreFd {
Expand Down Expand Up @@ -30,9 +34,13 @@ impl ExternalSemaphoreFd {

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/vkGetSemaphoreFdKHR.html>
#[inline]
pub unsafe fn get_semaphore_fd(&self, get_info: &vk::SemaphoreGetFdInfoKHR) -> VkResult<i32> {
pub unsafe fn get_semaphore_fd(
&self,
get_info: &vk::SemaphoreGetFdInfoKHR,
) -> VkResult<OwnedFd> {
let mut fd = -1;
(self.fp.get_semaphore_fd_khr)(self.handle, get_info, &mut fd).result_with_success(fd)
(self.fp.get_semaphore_fd_khr)(self.handle, get_info, &mut fd).result()?;
Ok(OwnedFd::from_raw_fd(fd))
}

pub const NAME: &'static CStr = vk::KhrExternalSemaphoreFdFn::NAME;
Expand Down
9 changes: 6 additions & 3 deletions ash/src/extensions/khr/external_semaphore_win32.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#![cfg(windows)]

use crate::prelude::*;
use crate::vk;
use crate::{Device, Instance};
use std::ffi::CStr;
use std::mem;
use std::os::windows::io::{FromRawHandle, OwnedHandle};
use std::ptr;

/// <https://www.khronos.org/registry/vulkan/specs/1.3-extensions/man/html/VK_KHR_external_semaphore_win32.html>
Expand Down Expand Up @@ -35,10 +38,10 @@ impl ExternalSemaphoreWin32 {
pub unsafe fn get_semaphore_win32_handle(
&self,
get_info: &vk::SemaphoreGetWin32HandleInfoKHR,
) -> VkResult<vk::HANDLE> {
) -> VkResult<OwnedHandle> {
let mut handle = ptr::null_mut();
(self.fp.get_semaphore_win32_handle_khr)(self.handle, get_info, &mut handle)
.result_with_success(handle)
(self.fp.get_semaphore_win32_handle_khr)(self.handle, get_info, &mut handle).result()?;
Ok(OwnedHandle::from_raw_handle(handle))
}

pub const NAME: &'static CStr = vk::KhrExternalSemaphoreWin32Fn::NAME;
Expand Down
9 changes: 9 additions & 0 deletions ash/src/extensions/khr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,20 @@ pub use self::display::Display;
pub use self::display_swapchain::DisplaySwapchain;
pub use self::draw_indirect_count::DrawIndirectCount;
pub use self::dynamic_rendering::DynamicRendering;
// TODO: This bound is only needed for MSRV 1.63 where Fd wrappers are not available in os::fd yet
#[cfg(unix)]
pub use self::external_fence_fd::ExternalFenceFd;
#[cfg(windows)]
pub use self::external_fence_win32::ExternalFenceWin32;
// TODO: This bound is only needed for MSRV 1.63 where Fd wrappers are not available in os::fd yet
#[cfg(unix)]
pub use self::external_memory_fd::ExternalMemoryFd;
#[cfg(windows)]
pub use self::external_memory_win32::ExternalMemoryWin32;
// TODO: This bound is only needed for MSRV 1.63 where Fd wrappers are not available in os::fd yet
#[cfg(unix)]
pub use self::external_semaphore_fd::ExternalSemaphoreFd;
#[cfg(windows)]
pub use self::external_semaphore_win32::ExternalSemaphoreWin32;
pub use self::get_memory_requirements2::GetMemoryRequirements2;
pub use self::get_physical_device_properties2::GetPhysicalDeviceProperties2;
Expand Down

0 comments on commit 5ecc662

Please sign in to comment.