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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
|
use std::{borrow::Borrow, fmt, mem, os::raw::c_void};
use winapi::{
shared::{
dxgi, dxgi1_4, dxgi1_5, dxgitype,
minwindef::{BOOL, FALSE, TRUE},
windef::{HWND, RECT},
winerror,
},
um::{d3d12, synchapi, winbase, winnt::HANDLE, winuser::GetClientRect},
};
use crate::{conv, resource as r, Backend, Device, Instance, PhysicalDevice, QueueFamily};
use hal::{device::Device as _, format as f, image as i, window as w};
impl Instance {
pub fn create_surface_from_hwnd(&self, hwnd: *mut c_void) -> Surface {
Surface {
factory: self.factory,
wnd_handle: hwnd as *mut _,
presentation: None,
}
}
}
#[derive(Debug)]
struct Presentation {
swapchain: Swapchain,
format: f::Format,
size: w::Extent2D,
mode: w::PresentMode,
}
pub struct Surface {
pub(crate) factory: native::WeakPtr<dxgi1_4::IDXGIFactory4>,
pub(crate) wnd_handle: HWND,
presentation: Option<Presentation>,
}
impl fmt::Debug for Surface {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("Surface")
}
}
unsafe impl Send for Surface {}
unsafe impl Sync for Surface {}
impl Surface {
pub(crate) unsafe fn present(&mut self, image: SwapchainImage) -> Result<(), w::PresentError> {
let present = self.presentation.as_mut().unwrap();
let sc = &mut present.swapchain;
sc.acquired_count -= 1;
let expected_index = sc.inner.GetCurrentBackBufferIndex() as w::SwapImageIndex;
if image.index != expected_index {
log::warn!(
"Expected frame {}, got frame {}",
expected_index,
image.index
);
return Err(w::PresentError::OutOfDate);
}
let (interval, flags) = match present.mode {
w::PresentMode::IMMEDIATE => (0, dxgi::DXGI_PRESENT_ALLOW_TEARING),
w::PresentMode::FIFO => (1, 0),
_ => (1, 0), // Surface was created with an unsupported present mode, fall back to FIFO
};
sc.inner.Present(interval, flags);
Ok(())
}
}
impl w::Surface<Backend> for Surface {
fn supports_queue_family(&self, queue_family: &QueueFamily) -> bool {
match queue_family {
&QueueFamily::Present => true,
_ => false,
}
}
fn capabilities(&self, _physical_device: &PhysicalDevice) -> w::SurfaceCapabilities {
let current_extent = unsafe {
let mut rect: RECT = mem::zeroed();
if GetClientRect(self.wnd_handle as *mut _, &mut rect as *mut RECT) == 0 {
panic!("GetClientRect failed");
}
Some(w::Extent2D {
width: (rect.right - rect.left) as u32,
height: (rect.bottom - rect.top) as u32,
})
};
let allow_tearing = unsafe {
let (f5, hr) = self.factory.cast::<dxgi1_5::IDXGIFactory5>();
if winerror::SUCCEEDED(hr) {
let mut allow_tearing: BOOL = FALSE;
let hr = f5.CheckFeatureSupport(
dxgi1_5::DXGI_FEATURE_PRESENT_ALLOW_TEARING,
&mut allow_tearing as *mut _ as *mut _,
mem::size_of::<BOOL>() as _,
);
f5.destroy();
winerror::SUCCEEDED(hr) && allow_tearing == TRUE
} else {
false
}
};
let mut present_modes = w::PresentMode::FIFO;
if allow_tearing {
present_modes |= w::PresentMode::IMMEDIATE;
}
w::SurfaceCapabilities {
present_modes,
composite_alpha_modes: w::CompositeAlphaMode::OPAQUE, //TODO
image_count: 2..=16, // we currently use a flip effect which supports 2..=16 buffers
current_extent,
extents: w::Extent2D {
width: 16,
height: 16,
}..=w::Extent2D {
width: 4096,
height: 4096,
},
max_image_layers: 1,
usage: i::Usage::COLOR_ATTACHMENT | i::Usage::TRANSFER_SRC | i::Usage::TRANSFER_DST,
}
}
fn supported_formats(&self, _physical_device: &PhysicalDevice) -> Option<Vec<f::Format>> {
Some(vec![
f::Format::Bgra8Srgb,
f::Format::Bgra8Unorm,
f::Format::Rgba8Srgb,
f::Format::Rgba8Unorm,
f::Format::A2b10g10r10Unorm,
f::Format::Rgba16Sfloat,
])
}
}
#[derive(Debug)]
pub struct SwapchainImage {
index: w::SwapImageIndex,
image: r::Image,
view: r::ImageView,
}
impl Borrow<r::Image> for SwapchainImage {
fn borrow(&self) -> &r::Image {
&self.image
}
}
impl Borrow<r::ImageView> for SwapchainImage {
fn borrow(&self) -> &r::ImageView {
&self.view
}
}
impl w::PresentationSurface<Backend> for Surface {
type SwapchainImage = SwapchainImage;
unsafe fn configure_swapchain(
&mut self,
device: &Device,
config: w::SwapchainConfig,
) -> Result<(), w::CreationError> {
assert!(i::Usage::COLOR_ATTACHMENT.contains(config.image_usage));
let swapchain = match self.presentation.take() {
Some(present) => {
if present.format == config.format && present.size == config.extent {
self.presentation = Some(present);
return Ok(());
}
// can't have image resources in flight used by GPU
device.wait_idle().unwrap();
let mut flags = dxgi::DXGI_SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT;
if config.present_mode.contains(w::PresentMode::IMMEDIATE) {
flags |= dxgi::DXGI_SWAP_CHAIN_FLAG_ALLOW_TEARING;
}
let inner = present.swapchain.release_resources();
let result = inner.ResizeBuffers(
config.image_count,
config.extent.width,
config.extent.height,
conv::map_format_nosrgb(config.format).unwrap(),
flags,
);
if result != winerror::S_OK {
error!("ResizeBuffers failed with 0x{:x}", result as u32);
return Err(w::CreationError::WindowInUse(hal::device::WindowInUse));
}
inner
}
None => {
let (swapchain, _) =
device.create_swapchain_impl(&config, self.wnd_handle, self.factory.clone())?;
swapchain
}
};
// Disable automatic Alt+Enter handling by DXGI.
const DXGI_MWA_NO_WINDOW_CHANGES: u32 = 1;
const DXGI_MWA_NO_ALT_ENTER: u32 = 2;
self.factory.MakeWindowAssociation(
self.wnd_handle,
DXGI_MWA_NO_WINDOW_CHANGES | DXGI_MWA_NO_ALT_ENTER,
);
self.presentation = Some(Presentation {
swapchain: device.wrap_swapchain(swapchain, &config),
format: config.format,
size: config.extent,
mode: config.present_mode,
});
Ok(())
}
unsafe fn unconfigure_swapchain(&mut self, device: &Device) {
if let Some(mut present) = self.presentation.take() {
let _ = present.swapchain.wait(winbase::INFINITE);
let _ = device.wait_idle(); //TODO: this shouldn't be needed,
// but it complains that the queue is still used otherwise
let inner = present.swapchain.release_resources();
inner.destroy();
}
}
unsafe fn acquire_image(
&mut self,
timeout_ns: u64,
) -> Result<(SwapchainImage, Option<w::Suboptimal>), w::AcquireError> {
let present = self.presentation.as_mut().unwrap();
let sc = &mut present.swapchain;
sc.wait((timeout_ns / 1_000_000) as u32)?;
let base_index = sc.inner.GetCurrentBackBufferIndex() as usize;
let index = (base_index + sc.acquired_count) % sc.resources.len();
sc.acquired_count += 1;
let resource = sc.resources[index];
let kind = i::Kind::D2(present.size.width, present.size.height, 1, 1);
let base_format = present.format.base_format();
let dxgi_format = conv::map_format(present.format).unwrap();
let rtv = sc.rtv_heap.at(index as _, 0).cpu;
let descriptor = d3d12::D3D12_RESOURCE_DESC {
Dimension: d3d12::D3D12_RESOURCE_DIMENSION_TEXTURE2D,
Alignment: 0,
Width: present.size.width as _,
Height: present.size.height as _,
DepthOrArraySize: 1,
MipLevels: 1,
Format: dxgi_format,
SampleDesc: dxgitype::DXGI_SAMPLE_DESC {
Count: 1,
Quality: 0,
},
Layout: d3d12::D3D12_TEXTURE_LAYOUT_UNKNOWN,
Flags: d3d12::D3D12_RESOURCE_FLAG_NONE, //TODO?
};
let image = r::ImageBound {
resource,
place: r::Place::Swapchain {},
surface_type: base_format.0,
kind,
mip_levels: 1,
usage: sc.usage,
default_view_format: None,
view_caps: i::ViewCapabilities::empty(),
descriptor,
clear_cv: Vec::new(), //TODO
clear_dv: Vec::new(),
clear_sv: Vec::new(),
requirements: hal::memory::Requirements {
size: 0,
alignment: 1,
type_mask: 0,
},
};
let swapchain_image = SwapchainImage {
index: index as _,
image: r::Image::Bound(image),
view: r::ImageView {
resource,
handle_srv: None,
handle_rtv: r::RenderTargetHandle::Swapchain(rtv),
handle_uav: None,
handle_dsv: None,
dxgi_format,
num_levels: 1,
mip_levels: (0, 1),
layers: (0, 1),
kind,
},
};
Ok((swapchain_image, None))
}
}
#[derive(Debug)]
pub struct Swapchain {
pub(crate) inner: native::WeakPtr<dxgi1_4::IDXGISwapChain3>,
#[allow(dead_code)]
pub(crate) rtv_heap: r::DescriptorHeap,
// need to associate raw image pointers with the swapchain so they can be properly released
// when the swapchain is destroyed
pub(crate) resources: Vec<native::Resource>,
pub(crate) waitable: HANDLE,
pub(crate) usage: i::Usage,
pub(crate) acquired_count: usize,
}
impl Swapchain {
pub(crate) unsafe fn release_resources(self) -> native::WeakPtr<dxgi1_4::IDXGISwapChain3> {
for resource in &self.resources {
resource.destroy();
}
self.rtv_heap.destroy();
self.inner
}
pub(crate) fn wait(&mut self, timeout_ms: u32) -> Result<(), w::AcquireError> {
match unsafe { synchapi::WaitForSingleObject(self.waitable, timeout_ms) } {
winbase::WAIT_ABANDONED | winbase::WAIT_FAILED => {
Err(w::AcquireError::DeviceLost(hal::device::DeviceLost))
}
winbase::WAIT_OBJECT_0 => Ok(()),
winerror::WAIT_TIMEOUT => Err(w::AcquireError::Timeout),
hr => panic!("Unexpected wait status 0x{:X}", hr),
}
}
}
unsafe impl Send for Swapchain {}
unsafe impl Sync for Swapchain {}
|