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
// Copyright 2017 Dasein Phaos aka. Luxko
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! link between the graphics API and the target surface

use comptr::ComPtr;
use winapi::IDXGISwapChain3;
use format::*;
use resource::*;
use error::WinError;

/// link between the graphics API and the target surface
#[derive(Debug)]
pub struct SwapChain {
    pub(crate) ptr: ComPtr<IDXGISwapChain3>,
}

impl SwapChain {
    /// gets the index of this swapchain's current back buffer
    #[inline]
    pub fn get_current_back_buffer_index(&mut self) -> u32 {
        unsafe{
            self.ptr.GetCurrentBackBufferIndex()
        }
    }

    /// get buffer at the given index
    #[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)
            })
        }
    }

    /// attemp to resize the back buffers with given parameters
    #[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)
    }

    // TODO: methods for color spaces

    // TODO: methods for frame latency waitable object

    // TODO: methods for composition swap chain

    /// get the source region size for the swap chain
    #[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))
    }

    /// set the source region size for the swap chain
    #[inline]
    pub fn set_source_size(&mut self, width: u32, height: u32) -> Result<(), WinError> {
        WinError::from_hresult(unsafe {
            self.ptr.SetSourceSize(width, height)
        })
    }

    /// get the background color for the next `present` method of this swapchain
    #[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]
            })
        }
    }

    /// change the background color for the next frame
    #[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)
        })
    }

    // TODO: add `get_core_window`?

    /// get description
    #[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))
        }
    }

    /// get fullscreen description
    #[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))
        }
    }

    /// get the underlying `HWMD` handle for the swapchain object
    #[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)
        }
    }

    // TODO: add method `get_restrict_to_output`

    // TODO: add method `get_containing_output`, `get_fullscreen_state`


    // TODO: add method to get performance statistics about the last render frame

    /// get the number of times that `Present` or `Present1` has been called.
    #[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)
        }
    }

    /// present a rendered back buffer to the target output.
    /// `sync_interval` specifies how to synchronize presentation of a frame
    /// with the verticle blank. valid values include [0..4].
    // TODO: support dirty rectangles
    #[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}
    }
}

/// description of a swapchain
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct SwapChainDesc {
    /// the resolution width, 0 to use the width of the CA of the target window
    pub width: u32,
    /// the resolution height, 0 to use the height of the CA of the target window
    pub height: u32,
    /// the display format
    pub format: DxgiFormat,
    /// whether the full-screen display mode or the back buffer is stereo
    // TODO: note the relationship with swapchain flip mode
    pub stereo: Bool,
    /// multi-sampling scheme description
    pub sample_desc: SampleDesc,
    /// surface usage and CPU access options for the back buffer.
    /// the back buffer can be used for shader input or target output.
    pub buffer_usage: Usage,
    /// number of buffers in the swap chain
    pub buffer_count: u32,
    /// scaling behavior when the back buffer is presented
    pub scaling: Scaling,
    /// presentation model, as well as how the back buffer would be
    /// handled after calling `swapchain.present()`.
    pub swap_effect: SwapEffect,
    /// transparency behavior
    pub alpha_mode: AlphaMode,
    /// misc flags
    pub flags: SwapChainFlags,
}

impl SwapChainDesc {
    /// create a new `SwapChainDesc` with default parameters
    #[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)
        }
    }
}

/// optional description of a fullsceen swapchain
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct FullScreenDesc {
    /// numerator of the refresh rate
    pub refresh_numerator: u32,
    /// denominator of the refresh rate
    pub refresh_denominator: u32,
    /// the method the raster uses to create an image on the surface
    pub scanline_order: ScanlineOrder,
    /// scaling mode
    pub scaling: ModeScaling,
    /// whether the swapchain is windowed
    pub windowed: Bool,
}

impl Default for FullScreenDesc {
    /// default to 60fps, unspecified order and scaling, fullscreen
    #[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 {
        // TODO: double check
        unsafe { ::std::mem::transmute(desc)}
    }
}

/// parameters for swapchain resizing
#[derive(Clone, Copy, Debug)]
pub struct SwapChainResizeDesc {
    /// new resolution width, 0 to use the width of the CA of the target window
    pub width: u32,
    /// new resolution height, 0 to use the height of the CA of the target window
    pub height: u32,
    /// new format, `DXGI_FORMAT_UNKNOWN` to preserve exisiting format
    pub format: DxgiFormat,
    /// new buffer counts, 0 to preserve existing counts
    pub buffer_count: u32,
    /// new flags
    pub flags: SwapChainFlags,
    // TODO: add nodes support
    // TODO: add present queue support
}

impl SwapChainResizeDesc {
    /// construct a new resize description with `flags` and default paramters
    #[inline]
    pub fn new(flags: SwapChainFlags) -> SwapChainResizeDesc {
        SwapChainResizeDesc{
            width: 0, height: 0, format: DXGI_FORMAT_UNKNOWN,
            buffer_count: 0, flags
        }
    }
}

/// multi-sampling scheme description. Default to count 1 and quality 0,
/// representing no aa
#[repr(C)]
#[derive(Clone, Copy, Debug)]
pub struct SampleDesc {
    /// the number of multisamples per pixel
    pub count: u32,
    /// the image quality level
    pub quality: u32,
}

impl Default for SampleDesc {
    fn default() -> SampleDesc {
        SampleDesc{count: 1, quality: 0}
    }
}

bitflags!{
    /// scaling behavor when the back buffer got presented. 
    #[repr(C)]
    pub struct Scaling: u32 {
        /// back buffer content would be scaled to fill the presentation target
        const SCALING_STRETCH = 0;
        /// back buffer content would appear without scaling, with top edge
        /// aligned with the presentation target.
        const SCALING_NONE = 1;
        /// back buffer content would be scaled to fit the presentation target,
        /// while preserving the aspect ratio, centered with black borders
        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!{
    /// presentation model, as well as how the back buffer would be
    /// handled after calling `swapchain.present()`.
    /// [more info](https://msdn.microsoft.com/en-us/library/windows/desktop/bb173077%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396)
    #[repr(C)]
    pub struct SwapEffect: u32 {
        /// bitblt, back buffer content would be discarded after presented
        const SWAP_EFFECT_DISCARD = 0;
        /// bitblt, back buffer content would persist after presented,
        /// cannot be used with multisampling
        const SWAP_EFFECT_SEQUENTIAL = 1;
        /// flip, back buffer content would persist after presented,
        /// cannot be used with multisampling
        const SWAP_EFFECT_FLIP_SEQUENTIAL = 3;
        /// flip, back buffer content would be discared after presented,
        /// cannot be used with multisampling and partial presentation
        const SWAP_EFFECT_FLIP_DISCARD = 4;
    }
}

impl Default for SwapEffect {
    fn default() -> SwapEffect {
        SWAP_EFFECT_FLIP_DISCARD
    }
}

bitflags!{
    /// transparency behavior of a surface
    #[repr(C)]
    pub struct AlphaMode: u32 {
        /// transparency behavior is not specified
        const ALPHA_MODE_UNSPECIFIED = 0;
        /// each color channel is premultiplied by the alpha value
        const ALPHA_MODE_PREMULTIPLIED = 1;
        /// each color channel is not premultiplied by the alpha value
        const ALPHA_MODE_STRAIGHT = 2;
        /// alpha channel would be ignored
        const ALPHA_MODE_IGNORE = 3;
    }
}

impl Default for AlphaMode {
    fn default() -> AlphaMode {
        ALPHA_MODE_UNSPECIFIED
    }
}

bitflags!{
    /// misc flags for swapchain behavior
    #[repr(C)]
    pub struct SwapChainFlags: u32 {
        const SWAP_CHAIN_FLAG_NONE = 0;
        /// turn off fullscreen automatic rotation
        const SWAP_CHAIN_FLAG_NONPREROTATED = 1;
        /// allow switch between fullscreen and windowed with `resize_target`
        const SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH = 2;
        /// allow `get_dc` on the 0th back buffer
        const SWAP_CHAIN_FLAG_GDI_COMPATIBLE = 4;
        /// OS would support creation only when driver and hardware protection is used?
        const SWAP_CHAIN_FLAG_RESTRICTED_CONTENT = 8;
        const SWAP_CHAIN_FLAG_RESTRICT_SHARED_RESOURCE_DRIVER = 16;
        /// the presented content would only be avaiable for local display
        const SWAP_CHAIN_FLAG_DISPLAY_ONLY = 32;
        /// ensure rendering does not begin while a frame is still being resented
        const SWAP_CHAIN_FLAG_FRAME_LATENCY_WAITABLE_OBJECT = 64;
        /// create a swapchain in the foreground layer for multi-plane rendering
        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;
        /// enable displays that support variable refresh rates to function
        /// properly when the application presents a swapchain tied to a full
        /// screen borderless window.
        const SWAP_CHAIN_FLAG_ALLOW_TEARING = 2048;
    }
}

impl Default for SwapChainFlags {
    #[inline]
    fn default() -> Self {
        SWAP_CHAIN_FLAG_NONE
    }
}

bitflags!{
    /// options for frame presentation
    #[repr(C)]
    pub struct PresentFlags: u32 {
        /// present a frame from each buffer (starting from the current one)
        /// to the output
        const PRESENT_FLAG_NONE = 0;
        /// present a frame from current buffer to the output.
        /// this flag allows vsync instead of typical sequencing
        const PRESENT_FLAG_DO_NOT_SEQUENCE = 0x2;
        /// don't present to the output. intended for use only when switching from idle
        const PRESENT_FLAG_TEST = 0x1;
        /// make the runtime discard outstanding queued frames
        const PRESENT_FLAG_RESTART = 0x4;
        /// make the invocation fail if the calling thread would be blocked
        const PRESENT_FLAG_DO_NOT_WAIT = 0x8;
        /// indicates that presentation content will be shown only on the particular output. The content will not be visible on other outputs.
        const PRESENT_FLAG_RESTRICT_TO_OUTPUT = 0x10;
        /// stereo prefers right-eye viewing instead of right
        const PRESENT_FLAG_STEREO_PREFER_RIGHT = 0x20;
        /// Indicates that the presentation should use the left buffer as a mono buffer.
        const PRESENT_FLAG_STEREO_TEMPORARY_MONO = 0x40;
        // TODO: const PRESENT_USE_DURATION = 0x100;
        /// allow tearing for variable refresh rate displays.
        ///
        /// this flag can be used when:
        /// - the swapchain was reated with the `ALLOW_TEARING` flag
        /// - the `sync_interval` is `0`
        /// - fullscreen borderless window, disabling automatic Alt+Enter...
        const PRESENT_FLAG_ALLOW_TEARING = 0x200;
    }
}

impl Default for PresentFlags {
    #[inline]
    fn default() -> Self {
        PRESENT_FLAG_NONE
    }
}

bitflags!{
    /// method the raster uses to create an image on the surface
    #[repr(C)]
    pub struct ScanlineOrder: u32 {
        const SCANLINE_ORDER_UNSPECIFIED = 0;
        /// image is created from the first scanline to the last without skipping any
        const SCANLINE_ORDER_PROGRESSIVE = 1;
        /// image is created beginning with the upper field
        const SCANLINE_ORDER_UPPER_FIELD_FIRST = 2;
        /// image is created beginning with the lower field
        const SCANLINE_ORDER_LOWER_FIELD_FIRST = 3;
    }
}

impl Default for ScanlineOrder {
    #[inline]
    fn default() -> Self {
        SCANLINE_ORDER_UNSPECIFIED
    }
}

bitflags!{
    /// scaling behavior for an image on a monitor
    #[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
    }
}