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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*! Swap chain management.

    ## Lifecycle

    At the low level, the swap chain is using the new simplified model of gfx-rs.

    A swap chain is a separate object that is backend-dependent but shares the index with
    the parent surface, which is backend-independent. This ensures a 1:1 correspondence
    between them.

    `get_next_image()` requests a new image from the surface. It becomes a part of
    `TextureViewInner::SwapChain` of the resulted view. The view is registered in the HUB
    but not in the device tracker.

    The only operation allowed on the view is to be either a color or a resolve attachment.
    It can only be used in one command buffer, which needs to be submitted before presenting.
    Command buffer tracker knows about the view, but only for the duration of recording.
    The view ID is erased from it at the end, so that it's not merged into the device tracker.

    When a swapchain view is used in `begin_render_pass()`, we assume the start and end image
    layouts purely based on whether or not this view was used in this command buffer before.
    It always starts with `Uninitialized` and ends with `Present`, so that no barriers are
    needed when we need to actually present it.

    In `queue_submit()` we make sure to signal the semaphore whenever we render to a swap
    chain view.

    In `present()` we return the swap chain image back and wait on the semaphore.
!*/

use crate::{
    conv,
    hub::{GfxBackend, Global, Token},
    resource,
    DeviceId,
    Extent3d,
    Features,
    Input,
    LifeGuard,
    Stored,
    SwapChainId,
    TextureViewId,
};
#[cfg(feature = "local")]
use crate::{gfx_select, hub::GLOBAL};

use hal::{self, device::Device as _, queue::CommandQueue as _, window::PresentationSurface as _};

use smallvec::SmallVec;

#[cfg(feature = "local")]
use std::marker::PhantomData;


const FRAME_TIMEOUT_MS: u64 = 1000;
pub const DESIRED_NUM_FRAMES: u32 = 3;

#[derive(Debug)]
pub struct SwapChain<B: hal::Backend> {
    pub(crate) life_guard: LifeGuard,
    pub(crate) device_id: Stored<DeviceId>,
    pub(crate) desc: SwapChainDescriptor,
    pub(crate) num_frames: hal::window::SwapImageIndex,
    pub(crate) semaphore: B::Semaphore,
    pub(crate) acquired_view_id: Option<Stored<TextureViewId>>,
}

#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub enum PresentMode {
    NoVsync = 0,
    Vsync = 1,
}

#[repr(C)]
#[derive(Clone, Debug)]
pub struct SwapChainDescriptor {
    pub usage: resource::TextureUsage,
    pub format: resource::TextureFormat,
    pub width: u32,
    pub height: u32,
    pub present_mode: PresentMode,
}

impl SwapChainDescriptor {
    pub(crate) fn to_hal(
        &self,
        num_frames: u32,
        features: &Features,
    ) -> hal::window::SwapchainConfig {
        let mut config = hal::window::SwapchainConfig::new(
            self.width,
            self.height,
            conv::map_texture_format(self.format, *features),
            num_frames,
        );
        //TODO: check for supported
        config.image_usage = conv::map_texture_usage(self.usage, hal::format::Aspects::COLOR);
        config.composite_alpha_mode = hal::window::CompositeAlphaMode::OPAQUE;
        config.present_mode = match self.present_mode {
            PresentMode::NoVsync => hal::window::PresentMode::IMMEDIATE,
            PresentMode::Vsync => hal::window::PresentMode::FIFO,
        };
        config
    }

    pub fn to_texture_desc(&self) -> resource::TextureDescriptor {
        resource::TextureDescriptor {
            size: Extent3d {
                width: self.width,
                height: self.height,
                depth: 1,
            },
            mip_level_count: 1,
            array_layer_count: 1,
            sample_count: 1,
            dimension: resource::TextureDimension::D2,
            format: self.format,
            usage: self.usage,
        }
    }
}

#[repr(C)]
#[derive(Debug)]
pub struct SwapChainOutput {
    pub view_id: TextureViewId,
}

#[derive(Debug)]
pub enum SwapChainGetNextTextureError {
    GpuProcessingTimeout,
}

pub fn swap_chain_get_next_texture<B: GfxBackend>(
    global: &Global,
    swap_chain_id: SwapChainId,
    view_id_in: Input<TextureViewId>,
) -> Result<SwapChainOutput, SwapChainGetNextTextureError> {
    let hub = B::hub(global);
    let mut token = Token::root();

    let (mut surface_guard, mut token) = global.surfaces.write(&mut token);
    let surface = &mut surface_guard[swap_chain_id.to_surface_id()];
    let (device_guard, mut token) = hub.devices.read(&mut token);
    let (mut swap_chain_guard, mut token) = hub.swap_chains.write(&mut token);
    let sc = &mut swap_chain_guard[swap_chain_id];
    let device = &device_guard[sc.device_id.value];

    let (image, _) = {
        let suf = B::get_surface_mut(surface);
        match unsafe { suf.acquire_image(FRAME_TIMEOUT_MS * 1_000_000) } {
            Ok(surface_image) => surface_image,
            Err(hal::window::AcquireError::Timeout) => {
                return Err(SwapChainGetNextTextureError::GpuProcessingTimeout);
            }
            Err(e) => {
                log::warn!("acquire_image() failed ({:?}), reconfiguring swapchain", e);
                let desc = sc.desc.to_hal(sc.num_frames, &device.features);
                unsafe {
                    suf.configure_swapchain(&device.raw, desc).unwrap();
                    suf.acquire_image(FRAME_TIMEOUT_MS * 1_000_000).unwrap()
                }
            }
        }
    };

    let view = resource::TextureView {
        inner: resource::TextureViewInner::SwapChain {
            image,
            source_id: Stored {
                value: swap_chain_id,
                ref_count: sc.life_guard.ref_count.clone(),
            },
            framebuffers: SmallVec::new(),
        },
        format: sc.desc.format,
        extent: hal::image::Extent {
            width: sc.desc.width,
            height: sc.desc.height,
            depth: 1,
        },
        samples: 1,
        range: hal::image::SubresourceRange {
            aspects: hal::format::Aspects::COLOR,
            layers: 0 .. 1,
            levels: 0 .. 1,
        },
        life_guard: LifeGuard::new(),
    };
    let ref_count = view.life_guard.ref_count.clone();
    let (view_id, _) = hub.texture_views.new_identity(view_id_in);
    hub.texture_views.register(view_id, view, &mut token);

    assert!(
        sc.acquired_view_id.is_none(),
        "Swap chain image is already acquired"
    );
    sc.acquired_view_id = Some(Stored {
        value: view_id,
        ref_count,
    });

    Ok(SwapChainOutput { view_id })
}

#[cfg(feature = "local")]
#[no_mangle]
pub extern "C" fn wgpu_swap_chain_get_next_texture(swap_chain_id: SwapChainId) -> SwapChainOutput {
    gfx_select!(swap_chain_id => swap_chain_get_next_texture(&*GLOBAL, swap_chain_id, PhantomData)).unwrap_or(SwapChainOutput {
        view_id: TextureViewId::ERROR,
    })
}

pub fn swap_chain_present<B: GfxBackend>(global: &Global, swap_chain_id: SwapChainId) {
    let hub = B::hub(global);
    let mut token = Token::root();

    let (mut surface_guard, mut token) = global.surfaces.write(&mut token);
    let surface = &mut surface_guard[swap_chain_id.to_surface_id()];
    let (mut device_guard, mut token) = hub.devices.write(&mut token);
    let (mut swap_chain_guard, mut token) = hub.swap_chains.write(&mut token);
    let sc = &mut swap_chain_guard[swap_chain_id];
    let device = &mut device_guard[sc.device_id.value];

    let view_id = sc
        .acquired_view_id
        .take()
        .expect("Swap chain image is not acquired");
    let (view, _) = hub.texture_views.unregister(view_id.value, &mut token);
    let (image, framebuffers) = match view.inner {
        resource::TextureViewInner::Native { .. } => unreachable!(),
        resource::TextureViewInner::SwapChain {
            image, framebuffers, ..
        } => (image, framebuffers),
    };

    let err = unsafe {
        let queue = &mut device.queue_group.queues[0];
        queue.present_surface(B::get_surface_mut(surface), image, Some(&sc.semaphore))
    };
    if let Err(e) = err {
        log::warn!("present failed: {:?}", e);
    }

    for fbo in framebuffers {
        unsafe {
            device.raw.destroy_framebuffer(fbo);
        }
    }
}

#[cfg(feature = "local")]
#[no_mangle]
pub extern "C" fn wgpu_swap_chain_present(swap_chain_id: SwapChainId) {
    gfx_select!(swap_chain_id => swap_chain_present(&*GLOBAL, swap_chain_id))
}