-
Notifications
You must be signed in to change notification settings - Fork 570
/
Copy pathdcomp.rs
70 lines (61 loc) · 2.07 KB
/
dcomp.rs
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
66
67
68
69
70
// Copyright 2018 the Druid Authors
// SPDX-License-Identifier: Apache-2.0
//! Safe-ish wrappers for DirectComposition and related interfaces.
// This module could become a general wrapper for DirectComposition, but
// for now we're just using what we need to get a swapchain up.
use std::ptr::{null, null_mut};
use tracing::error;
use winapi::shared::winerror::SUCCEEDED;
use winapi::um::d3d11::*;
use winapi::um::d3dcommon::{D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP};
use winapi::um::winnt::HRESULT;
use winapi::Interface;
use wio::com::ComPtr;
unsafe fn wrap<T, U, F>(hr: HRESULT, ptr: *mut T, f: F) -> Result<U, HRESULT>
where
F: Fn(ComPtr<T>) -> U,
T: Interface,
{
if SUCCEEDED(hr) {
Ok(f(ComPtr::from_raw(ptr)))
} else {
Err(hr)
}
}
pub struct D3D11Device(ComPtr<ID3D11Device>);
impl D3D11Device {
/// Creates a new device with basic defaults.
pub(crate) fn new_simple() -> Result<D3D11Device, HRESULT> {
let mut hr = 0;
unsafe {
let mut d3d11_device: *mut ID3D11Device = null_mut();
// Note: could probably set single threaded in flags for small performance boost.
let flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
// Prefer hardware but use warp if it's the only driver available.
for driver_type in &[D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP] {
hr = D3D11CreateDevice(
null_mut(),
*driver_type,
null_mut(),
flags,
null(),
0,
D3D11_SDK_VERSION,
&mut d3d11_device,
null_mut(),
null_mut(),
);
if SUCCEEDED(hr) {
break;
}
}
if !SUCCEEDED(hr) {
error!("D3D11CreateDevice: 0x{:x}", hr);
}
wrap(hr, d3d11_device, D3D11Device)
}
}
pub(crate) fn raw_ptr(&mut self) -> *mut ID3D11Device {
self.0.as_raw()
}
}