Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic D3D12 bindings #146

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 93 additions & 2 deletions openxr/src/graphics/d3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub enum D3D11 {}

impl Graphics for D3D11 {
type Requirements = Requirements;
type SessionCreateInfo = SessionCreateInfo;
type SessionCreateInfo = SessionCreateInfoD3D11;
type Format = u32;
type SwapchainImage = *mut ID3D11Texture2D;

Expand Down Expand Up @@ -87,13 +87,104 @@ impl Graphics for D3D11 {
}
}


/// The D3D12 graphics API
///
/// See [`XR_KHR_d3d12_enable`] for safety details.
///
/// [`XR_KHR_d3d_enable`]: https://www.khronos.org/registry/OpenXR/specs/1.0/html/xrspec.html#XR_KHR_D3D12_enable
pub enum D3D12 {}

impl Graphics for D3D12 {
type Requirements = Requirements;
type SessionCreateInfo = SessionCreateInfoD3D12;
type Format = u32;
type SwapchainImage = *mut ID3D12Resource;

fn raise_format(x: i64) -> u32 {
x as _
}
fn lower_format(x: u32) -> i64 {
x.into()
}

fn requirements(inst: &Instance, system: SystemId) -> Result<Requirements> {
let out = unsafe {
let mut x = sys::GraphicsRequirementsD3D12KHR::out(ptr::null_mut());
cvt((inst.d3d12().get_d3d12_graphics_requirements)(
inst.as_raw(),
system,
x.as_mut_ptr(),
))?;
x.assume_init()
};
Ok(Requirements {
adapter_luid: out.adapter_luid,
min_feature_level: out.min_feature_level,
})
}

unsafe fn create_session(
instance: &Instance,
system: SystemId,
info: &Self::SessionCreateInfo,
) -> Result<sys::Session> {
let binding = sys::GraphicsBindingD3D12KHR {
ty: sys::GraphicsBindingD3D12KHR::TYPE,
next: ptr::null(),
device: info.device,
queue: info.queue,
};
let info = sys::SessionCreateInfo {
ty: sys::SessionCreateInfo::TYPE,
next: &binding as *const _ as *const _,
create_flags: Default::default(),
system_id: system,
};
let mut out = sys::Session::NULL;
cvt((instance.fp().create_session)(
instance.as_raw(),
&info,
&mut out,
))?;
Ok(out)
}

fn enumerate_swapchain_images(
swapchain: &Swapchain<Self>,
) -> Result<Vec<Self::SwapchainImage>> {
let images = get_arr_init(
sys::SwapchainImageD3D12KHR {
ty: sys::SwapchainImageD3D12KHR::TYPE,
next: ptr::null_mut(),
texture: ptr::null_mut(),
},
|capacity, count, buf| unsafe {
(swapchain.instance().fp().enumerate_swapchain_images)(
swapchain.as_raw(),
capacity,
count,
buf as *mut _,
)
},
)?;
Ok(images.into_iter().map(|x| x.texture).collect())
}
}

#[derive(Copy, Clone)]
pub struct Requirements {
pub adapter_luid: LUID,
pub min_feature_level: D3D_FEATURE_LEVEL,
}

#[derive(Copy, Clone)]
pub struct SessionCreateInfo {
pub struct SessionCreateInfoD3D11 {
pub device: *mut ID3D11Device,
}

#[derive(Copy, Clone)]
pub struct SessionCreateInfoD3D12 {
pub device: *mut ID3D12Device,
pub queue: *mut ID3D12CommandQueue,
}
2 changes: 2 additions & 0 deletions openxr/src/graphics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ pub trait Graphics: Sized {
pub mod d3d;
#[cfg(windows)]
pub use d3d::D3D11;
#[cfg(windows)]
pub use d3d::D3D12;

pub mod vulkan;
pub use vulkan::Vulkan;
Expand Down
7 changes: 7 additions & 0 deletions openxr/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,13 @@ impl Instance {
.as_ref()
.expect("KHR_d3d11_enable not loaded")
}
#[cfg(windows)]
pub(crate) fn d3d12(&self) -> &raw::D3d12EnableKHR {
self.exts()
.khr_d3d12_enable
.as_ref()
.expect("KHR_d3d12_enable not loaded")
}
pub(crate) fn visibility_mask(&self) -> &raw::VisibilityMaskKHR {
self.exts()
.khr_visibility_mask
Expand Down
Loading