oxidx/
entry.rs

1use windows::Win32::Graphics::Direct3D12::{D3D12CreateDevice, D3D12GetDebugInterface};
2use windows::Win32::Graphics::Dxgi::CreateDXGIFactory2;
3
4use crate::dx::{Adapter3, Debug, Device, Factory4};
5use crate::error::DxError;
6use crate::types::{FactoryCreationFlags, FeatureLevel};
7
8/// Creates a DXGI 1.3 factory that you can use to generate other DXGI objects.
9///
10/// For more information: [`CreateDXGIFactory2 function`](https://learn.microsoft.com/en-us/windows/win32/api/dxgi1_3/nf-dxgi1_3-createdxgifactory2)
11pub fn create_factory4(flags: FactoryCreationFlags) -> Result<Factory4, DxError> {
12    unsafe {
13        let inner = CreateDXGIFactory2(flags.as_raw()).map_err(DxError::from)?;
14
15        Ok(Factory4(inner))
16    }
17}
18
19/// Creates a device that represents the display adapter.
20///
21/// For more information: [`D3D12CreateDevice function`](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12createdevice)
22pub fn create_device<'a>(
23    adapter: impl Into<Option<&'a Adapter3>>,
24    feature_level: FeatureLevel,
25) -> Result<Device, DxError> {
26    unsafe {
27        let mut inner = None;
28
29        if let Some(adapter) = adapter.into() {
30            D3D12CreateDevice(&adapter.0, feature_level.as_raw(), &mut inner)
31                .map_err(DxError::from)?;
32        } else {
33            D3D12CreateDevice(None, feature_level.as_raw(), &mut inner).map_err(DxError::from)?;
34        }
35
36        let inner = inner.unwrap();
37
38        Ok(Device(inner))
39    }
40}
41
42/// Gets a debug interface.
43///
44/// For more information: [`D3D12GetDebugInterface function`](https://learn.microsoft.com/en-us/windows/win32/api/d3d12/nf-d3d12-d3d12getdebuginterface)
45pub fn create_debug() -> Result<Debug, DxError> {
46    unsafe {
47        let mut inner = None;
48
49        D3D12GetDebugInterface(&mut inner).map_err(DxError::from)?;
50        let inner = inner.unwrap();
51
52        Ok(Debug(inner))
53    }
54}
55
56#[cfg(test)]
57mod test {
58    use crate::{dx::Factory7, types::FactoryCreationFlags};
59
60    use super::*;
61
62    #[test]
63    fn create_factory4_test() {
64        let factory = create_factory4(FactoryCreationFlags::Debug);
65
66        assert!(factory.is_ok())
67    }
68
69    #[test]
70    fn create_device_test() {
71        let device = create_device(None, FeatureLevel::Level11);
72        assert!(device.is_ok());
73    }
74
75    #[test]
76    fn as_ref_factory_test() {
77        fn test(factory: impl AsRef<Factory4>) {
78            let _ = factory.as_ref().enum_adapters(0).unwrap();
79        }
80
81        let factory = create_factory4(FactoryCreationFlags::empty()).unwrap();
82        let factory: Factory7 = factory.try_into().unwrap();
83
84        test(&factory);
85    }
86}