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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use comptr::ComPtr;
use winapi::IDXGISwapChain3;
use format::*;
use resource::*;
use error::WinError;
#[derive(Debug)]
pub struct SwapChain {
pub(crate) ptr: ComPtr<IDXGISwapChain3>,
}
impl SwapChain {
#[inline]
pub fn get_current_back_buffer_index(&mut self) -> u32 {
unsafe{
self.ptr.GetCurrentBackBufferIndex()
}
}
#[inline]
pub fn get_buffer(&mut self, index: u32) -> Result<RawResource, WinError> {
unsafe {
let mut ret = ::std::mem::uninitialized();
let hr = self.ptr.GetBuffer(
index, & ::dxguid::IID_ID3D12Resource,
&mut ret as *mut *mut _ as *mut *mut _
);
WinError::from_hresult_or_ok(hr, || RawResource{
ptr: ComPtr::new(ret)
})
}
}
#[inline]
pub fn resize_buffers(&mut self, params: SwapChainResizeDesc) -> Result<(), WinError> {
let hr = unsafe {
self.ptr.ResizeBuffers1(
params.buffer_count,
params.width,
params.height,
params.format,
params.flags.bits(),
::std::ptr::null(),
::std::ptr::null_mut()
)
};
WinError::from_hresult(hr)
}
#[inline]
pub fn get_source_size(&mut self) -> Result<(u32, u32), WinError> {
let mut width = 0;
let mut height = 0;
let hr = unsafe {
self.ptr.GetSourceSize(&mut width, &mut height)
};
WinError::from_hresult_or_ok(hr, || (width, height))
}
#[inline]
pub fn set_source_size(&mut self, width: u32, height: u32) -> Result<(), WinError> {
WinError::from_hresult(unsafe {
self.ptr.SetSourceSize(width, height)
})
}
#[inline]
pub fn get_background_color(&mut self) -> Result<[f32; 4], WinError> {
unsafe {
let mut ret = ::std::mem::uninitialized();
let hr = self.ptr.GetBackgroundColor(&mut ret);
WinError::from_hresult_or_ok(hr, || {
[ret.r, ret.g, ret.b, ret.a]
})
}
}
#[inline]
pub fn set_background_color(&mut self, r: f32, g: f32, b: f32, a: f32) -> Result<(), WinError> {
let rgba = ::winapi::DXGI_RGBA{r, g, b, a};
WinError::from_hresult(unsafe {
self.ptr.SetBackgroundColor(&rgba)
})
}
#[inline]
pub fn get_desc(&mut self) -> Result<SwapChainDesc, WinError> {
unsafe {
let mut ret = ::std::mem::uninitialized();
let hr = self.ptr.GetDesc1(&mut ret);
WinError::from_hresult_or_ok(hr, || ::std::mem::transmute(ret))
}
}
#[inline]
pub fn get_fullscreen_desc(&mut self) -> Result<FullScreenDesc, WinError> {
unsafe {
let mut ret = ::std::mem::uninitialized();
let hr = self.ptr.GetFullscreenDesc(&mut ret);
WinError::from_hresult_or_ok(hr, || ::std::mem::transmute(ret))
}
}
#[inline]
pub fn get_hwnd(&mut self) -> Result<::winapi::HWND, WinError> {
unsafe {
let mut ret = ::std::mem::uninitialized();
let hr = self.ptr.GetHwnd(&mut ret);
WinError::from_hresult_or_ok(hr, || ret)
}
}
#[inline]
pub fn get_last_present_count(&mut self) -> Result<u32, WinError> {
unsafe {
let mut ret = 0;
let hr = self.ptr.GetLastPresentCount(&mut ret);
WinError::from_hresult_or_ok(hr, || ret)
}
}
#[inline]
pub fn present(
&mut self, sync_interval: u32, flags: PresentFlags
) -> Result<(), WinError> {
debug_assert!(sync_interval<=4);
WinError::from_hresult(unsafe {
self.ptr.Present(sync_interval, flags.bits())
})
}
}
impl From<ComPtr<IDXGISwapChain3>> for SwapChain {
#[inline]
fn from(ptr: ComPtr<IDXGISwapChain3>) -> SwapChain {
SwapChain{ptr}
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct SwapChainDesc {
pub width: u32,
pub height: u32,
pub format: DxgiFormat,
pub stereo: Bool,
pub sample_desc: SampleDesc,
pub buffer_usage: Usage,
pub buffer_count: u32,
pub scaling: Scaling,
pub swap_effect: SwapEffect,
pub alpha_mode: AlphaMode,
pub flags: SwapChainFlags,
}
impl SwapChainDesc {
#[inline]
pub fn new(format: DxgiFormat) -> SwapChainDesc {
SwapChainDesc{
width: 0,
height: 0,
format,
stereo: false.into(),
sample_desc: Default::default(),
buffer_usage: USAGE_RENDER_TARGET_OUTPUT,
buffer_count: 2,
scaling: Default::default(),
swap_effect: Default::default(),
alpha_mode: Default::default(),
flags: Default::default(),
}
}
}
impl From<SwapChainDesc> for ::winapi::DXGI_SWAP_CHAIN_DESC1 {
#[inline]
fn from(desc: SwapChainDesc) -> Self {
unsafe {
::std::mem::transmute(desc)
}
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct FullScreenDesc {
pub refresh_numerator: u32,
pub refresh_denominator: u32,
pub scanline_order: ScanlineOrder,
pub scaling: ModeScaling,
pub windowed: Bool,
}
impl Default for FullScreenDesc {
#[inline]
fn default() -> FullScreenDesc {
FullScreenDesc{
refresh_numerator: 60,
refresh_denominator: 1,
scanline_order: Default::default(),
scaling: Default::default(),
windowed: false.into()
}
}
}
impl From<FullScreenDesc> for ::winapi::DXGI_SWAP_CHAIN_FULLSCREEN_DESC {
#[inline]
fn from(desc: FullScreenDesc) -> Self {
unsafe { ::std::mem::transmute(desc)}
}
}
#[derive(Clone, Copy, Debug)]
pub struct SwapChainResizeDesc {
pub width: u32,
pub height: u32,
pub format: DxgiFormat,
pub buffer_count: u32,
pub flags: SwapChainFlags,
}
impl SwapChainResizeDesc {
#[inline]
pub fn new(flags: SwapChainFlags) -> SwapChainResizeDesc {
SwapChainResizeDesc{
width: 0, height: 0, format: DXGI_FORMAT_UNKNOWN,
buffer_count: 0, flags
}
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct SampleDesc {
pub count: u32,
pub quality: u32,
}
impl Default for SampleDesc {
fn default() -> SampleDesc {
SampleDesc{count: 1, quality: 0}
}
}
bitflags!{
#[repr(C)]
pub struct Scaling: u32 {
const SCALING_STRETCH = 0;
const SCALING_NONE = 1;
const SCALING_ASPECT_RATIO_STRETCH = 2;
}
}
impl From<Scaling> for ::winapi::DXGI_SCALING {
fn from(scaling: Scaling) -> Self {
::winapi::DXGI_SCALING(scaling.bits())
}
}
impl Default for Scaling {
#[inline]
fn default() -> Scaling {
SCALING_STRETCH
}
}
bitflags!{
#[repr(C)]
pub struct SwapEffect: u32 {
const SWAP_EFFECT_DISCARD = 0;
const SWAP_EFFECT_SEQUENTIAL = 1;
const SWAP_EFFECT_FLIP_SEQUENTIAL = 3;
const SWAP_EFFECT_FLIP_DISCARD = 4;
}
}
impl Default for SwapEffect {
fn default() -> SwapEffect {
SWAP_EFFECT_FLIP_DISCARD
}
}
bitflags!{
#[repr(C)]
pub struct AlphaMode: u32 {
const ALPHA_MODE_UNSPECIFIED = 0;
const ALPHA_MODE_PREMULTIPLIED = 1;
const ALPHA_MODE_STRAIGHT = 2;
const ALPHA_MODE_IGNORE = 3;
}
}
impl Default for AlphaMode {
fn default() -> AlphaMode {
ALPHA_MODE_UNSPECIFIED
}
}
bitflags!{
#[repr(C)]
pub struct SwapChainFlags: u32 {
const SWAP_CHAIN_FLAG_NONE = 0;
const SWAP_CHAIN_FLAG_NONPREROTATED = 1;
const SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH = 2;
const SWAP_CHAIN_FLAG_GDI_COMPATIBLE = 4;
const SWAP_CHAIN_FLAG_RESTRICTED_CONTENT = 8;
const SWAP_CHAIN_FLAG_RESTRICT_SHARED_RESOURCE_DRIVER = 16;
const SWAP_CHAIN_FLAG_DISPLAY_ONLY = 32;
const SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT = 64;
const SWAP_CHAIN_FLAG_FOREGROUND_LAYER = 128;
const SWAP_CHAIN_FLAG_FULLSCREEN_VIDEO = 256;
const SWAP_CHAIN_FLAG_YUV_VIDEO = 512;
const SWAP_CHAIN_FLAG_HW_PROTECTED = 1024;
const SWAP_CHAIN_FLAG_ALLOW_TEARING = 2048;
}
}
impl Default for SwapChainFlags {
#[inline]
fn default() -> Self {
SWAP_CHAIN_FLAG_NONE
}
}
bitflags!{
#[repr(C)]
pub struct PresentFlags: u32 {
const PRESENT_FLAG_NONE = 0;
const PRESENT_FLAG_DO_NOT_SEQUENCE = 0x2;
const PRESENT_FLAG_TEST = 0x1;
const PRESENT_FLAG_RESTART = 0x4;
const PRESENT_FLAG_DO_NOT_WAIT = 0x8;
const PRESENT_FLAG_RESTRICT_TO_OUTPUT = 0x10;
const PRESENT_FLAG_STEREO_PREFER_RIGHT = 0x20;
const PRESENT_FLAG_STEREO_TEMPORARY_MONO = 0x40;
const PRESENT_FLAG_ALLOW_TEARING = 0x200;
}
}
impl Default for PresentFlags {
#[inline]
fn default() -> Self {
PRESENT_FLAG_NONE
}
}
bitflags!{
#[repr(C)]
pub struct ScanlineOrder: u32 {
const SCANLINE_ORDER_UNSPECIFIED = 0;
const SCANLINE_ORDER_PROGRESSIVE = 1;
const SCANLINE_ORDER_UPPER_FIELD_FIRST = 2;
const SCANLINE_ORDER_LOWER_FIELD_FIRST = 3;
}
}
impl Default for ScanlineOrder {
#[inline]
fn default() -> Self {
SCANLINE_ORDER_UNSPECIFIED
}
}
bitflags!{
#[repr(C)]
pub struct ModeScaling: u32 {
const MODE_SCALING_UNSPECIFIED = 0;
const MODE_SCALING_CENTERED = 1;
const MODE_SCALING_STRETCHED = 2;
}
}
impl Default for ModeScaling {
#[inline]
fn default() -> Self {
MODE_SCALING_UNSPECIFIED
}
}