~raph/dx11-toy

dx11-toy/src/dxgi.rs -rw-r--r-- 1.8 KiB
c3700a74Raph Levien Move shaders into file 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::ptr::{null, null_mut};

use winapi::shared::{dxgi, dxgi1_2, windef};
use winapi::um::d3d11;
use winapi::Interface;

use wio::com::ComPtr;

use crate::d3d11::{D3D11Device, D3D11Texture2D};
use crate::util::{wrap, wrap_unit, Error};

pub struct DXGIFactory2(ComPtr<dxgi1_2::IDXGIFactory2>);
pub struct DXGISwapChain1(ComPtr<dxgi1_2::IDXGISwapChain1>);

impl DXGIFactory2 {
    pub fn create() -> Result<DXGIFactory2, Error> {
        unsafe {
            let mut ptr = null_mut();
            let hr = dxgi::CreateDXGIFactory1(
                &dxgi1_2::IDXGIFactory2::uuidof(),
                &mut ptr as *mut _ as *mut _,
            );
            wrap(hr, ptr, DXGIFactory2)
        }
    }

    pub unsafe fn create_swapchain_for_hwnd(
        &self,
        device: &D3D11Device,
        hwnd: windef::HWND,
        desc: &dxgi1_2::DXGI_SWAP_CHAIN_DESC1,
    ) -> Result<DXGISwapChain1, Error> {
        let mut ptr = null_mut();
        let hr = self.0.CreateSwapChainForHwnd(
            device.as_raw() as *mut _,
            hwnd,
            desc,
            null(),
            null_mut(),
            &mut ptr as *mut _ as *mut _,
        );
        wrap(hr, ptr, DXGISwapChain1)
    }
}

impl DXGISwapChain1 {
    pub fn present(&self, sync_interval: u32, flags: u32) -> Result<(), Error> {
        unsafe {
            let hr = self.0.Present(sync_interval, flags);
            wrap_unit(hr)
        }
    }

    pub fn get_buffer(&mut self, buffer: u32) -> Result<D3D11Texture2D, Error> {
        unsafe {
            let mut ptr = null_mut();
            let hr = self.0.GetBuffer(
                buffer,
                &d3d11::ID3D11Texture2D::uuidof(),
                &mut ptr as *mut _ as *mut _,
            );
            wrap(hr, ptr, D3D11Texture2D)
        }
    }
}