renderdoc/
handles.rs

1//! Portable wrapper types around platform specific window and device handles.
2
3use std::os::raw::c_void;
4
5#[cfg(windows)]
6use wio::com::ComPtr;
7
8/// Raw mutable pointer to the OS-provided window handle.
9pub type WindowHandle = *const c_void;
10
11/// Raw mutable pointer to the API's root handle.
12///
13/// For example, this could be a pointer to an `ID3D11Device`, `HGLRC`/`GLXContext`,
14/// `ID3D12Device`, etc.
15#[derive(Clone, Debug, Eq, Hash, PartialEq)]
16pub struct DevicePointer(pub(crate) *const c_void);
17
18impl From<*const c_void> for DevicePointer {
19    fn from(ptr: *const c_void) -> Self {
20        DevicePointer(ptr)
21    }
22}
23
24impl From<*mut c_void> for DevicePointer {
25    fn from(ptr: *mut c_void) -> Self {
26        DevicePointer(ptr)
27    }
28}
29
30#[cfg(windows)]
31impl From<winapi::shared::windef::HGLRC> for DevicePointer {
32    fn from(ctx: winapi::shared::windef::HGLRC) -> Self {
33        DevicePointer(ctx as *mut _ as *const c_void)
34    }
35}
36
37#[cfg(windows)]
38impl From<*mut winapi::um::d3d11::ID3D11Device> for DevicePointer {
39    fn from(ctx: *mut winapi::um::d3d11::ID3D11Device) -> Self {
40        DevicePointer(ctx as *mut _ as *const c_void)
41    }
42}
43
44#[cfg(windows)]
45impl From<wio::com::ComPtr<winapi::um::d3d11::ID3D11Device>> for DevicePointer {
46    fn from(ctx: wio::com::ComPtr<winapi::um::d3d11::ID3D11Device>) -> Self {
47        DevicePointer(ctx.as_raw() as *mut _ as *const c_void)
48    }
49}
50
51#[cfg(windows)]
52impl From<*mut winapi::um::d3d12::ID3D12Device> for DevicePointer {
53    fn from(ctx: *mut winapi::um::d3d12::ID3D12Device) -> Self {
54        DevicePointer(ctx as *mut _ as *const c_void)
55    }
56}
57
58#[cfg(windows)]
59impl From<wio::com::ComPtr<winapi::um::d3d12::ID3D12Device>> for DevicePointer {
60    fn from(ctx: wio::com::ComPtr<winapi::um::d3d12::ID3D12Device>) -> Self {
61        DevicePointer(ctx.as_raw() as *mut _ as *const c_void)
62    }
63}
64
65#[cfg(feature = "glutin")]
66impl<'a, T: glutin::context::AsRawContext> From<&'a T> for DevicePointer {
67    fn from(ctx: &'a T) -> Self {
68        use glutin::context::RawContext;
69
70        #[cfg(unix)]
71        match ctx.raw_context() {
72            RawContext::Egl(egl) => DevicePointer::from(egl),
73            RawContext::Glx(glx) => DevicePointer::from(glx),
74        }
75
76        #[cfg(windows)]
77        match ctx.raw_context() {
78            RawContext::Egl(egl) => DevicePointer::from(egl),
79            RawContext::Wgl(wgl) => DevicePointer::from(wgl),
80        }
81    }
82}