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
use std::{future::Future, pin::Pin, sync::Arc};

use glam::UVec2;
use rend3::{
    types::{Handedness, SampleCount, Surface, TextureFormat},
    InstanceAdapterDevice, Renderer,
};
use rend3_routine::base::BaseRenderGraph;
use wgpu::Instance;
use winit::{
    dpi::PhysicalSize,
    event::WindowEvent,
    event_loop::{ControlFlow, EventLoop, EventLoopWindowTarget},
    window::{Window, WindowBuilder, WindowId},
};

mod assets;
mod grab;
#[cfg(target_arch = "wasm32")]
mod resize_observer;

pub use assets::*;
pub use grab::*;

pub use parking_lot::{Mutex, MutexGuard};
pub type Event<'a, T> = winit::event::Event<'a, UserResizeEvent<T>>;

/// User event which the framework uses to resize on wasm.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum UserResizeEvent<T: 'static> {
    /// Used to fire off resizing on wasm
    Resize {
        window_id: WindowId,
        size: PhysicalSize<u32>,
    },
    /// Custom user event type
    Other(T),
}

pub trait App<T: 'static = ()> {
    /// The handedness of the coordinate system of the renderer.
    const HANDEDNESS: Handedness;

    fn register_logger(&mut self) {
        #[cfg(target_arch = "wasm32")]
        console_log::init().unwrap();

        #[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
        env_logger::init();
    }

    fn register_panic_hook(&mut self) {
        #[cfg(target_arch = "wasm32")]
        std::panic::set_hook(Box::new(console_error_panic_hook::hook));
    }

    fn create_window(&mut self, builder: WindowBuilder) -> (EventLoop<UserResizeEvent<T>>, Window) {
        profiling::scope!("creating window");

        let event_loop = EventLoop::with_user_event();
        let window = builder.build(&event_loop).expect("Could not build window");

        #[cfg(target_arch = "wasm32")]
        {
            use winit::platform::web::WindowExtWebSys;

            let canvas = window.canvas();
            let style = canvas.style();
            style.set_property("width", "100%").unwrap();
            style.set_property("height", "100%").unwrap();

            web_sys::window()
                .and_then(|win| win.document())
                .and_then(|doc| doc.body())
                .and_then(|body| body.append_child(&canvas).ok())
                .expect("couldn't append canvas to document body");
        }

        (event_loop, window)
    }

    fn create_iad<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = anyhow::Result<InstanceAdapterDevice>> + 'a>> {
        Box::pin(async move { Ok(rend3::create_iad(None, None, None, None).await?) })
    }

    fn create_base_rendergraph(&mut self, renderer: &Renderer) -> BaseRenderGraph {
        BaseRenderGraph::new(renderer)
    }

    /// Determines the sample count used, this may change dynamically. This
    /// function is what the framework actually calls, so overriding this
    /// will always use the right values.
    ///
    /// It is called on main events cleared and things are remade if this
    /// changes.
    fn sample_count(&self) -> SampleCount;

    /// Determines the scale factor used
    fn scale_factor(&self) -> f32 {
        1.0
    }

    fn setup(
        &mut self,
        window: &Window,
        renderer: &Arc<Renderer>,
        routines: &Arc<DefaultRoutines>,
        surface_format: rend3::types::TextureFormat,
    ) {
        let _ = (window, renderer, routines, surface_format);
    }

    /// RedrawRequested/RedrawEventsCleared will only be fired if the window
    /// size is non-zero. As such you should always render
    /// in RedrawRequested and use MainEventsCleared for things that need to
    /// keep running when minimized.
    #[allow(clippy::too_many_arguments)]
    fn handle_event(
        &mut self,
        window: &Window,
        renderer: &Arc<rend3::Renderer>,
        routines: &Arc<DefaultRoutines>,
        base_rendergraph: &BaseRenderGraph,
        surface: Option<&Arc<Surface>>,
        resolution: UVec2,
        event: Event<'_, T>,
        control_flow: impl FnOnce(winit::event_loop::ControlFlow),
    ) {
        let _ = (
            window,
            renderer,
            routines,
            base_rendergraph,
            resolution,
            surface,
            event,
            control_flow,
        );
    }
}

pub fn lock<T>(lock: &parking_lot::Mutex<T>) -> parking_lot::MutexGuard<'_, T> {
    #[cfg(target_arch = "wasm32")]
    let guard = lock.try_lock().expect("Could not lock mutex on single-threaded wasm. Do not hold locks open while an .await causes you to yield execution.");
    #[cfg(not(target_arch = "wasm32"))]
    let guard = lock.lock();

    guard
}

pub struct DefaultRoutines {
    pub pbr: Mutex<rend3_routine::pbr::PbrRoutine>,
    pub skybox: Mutex<rend3_routine::skybox::SkyboxRoutine>,
    pub tonemapping: Mutex<rend3_routine::tonemapping::TonemappingRoutine>,
}

#[cfg(not(target_arch = "wasm32"))]
fn winit_run<F, T>(event_loop: winit::event_loop::EventLoop<T>, event_handler: F) -> !
where
    F: FnMut(winit::event::Event<'_, T>, &EventLoopWindowTarget<T>, &mut ControlFlow) + 'static,
    T: 'static,
{
    event_loop.run(event_handler)
}

#[cfg(target_arch = "wasm32")]
fn winit_run<F, T>(event_loop: EventLoop<T>, event_handler: F)
where
    F: FnMut(winit::event::Event<'_, T>, &EventLoopWindowTarget<T>, &mut ControlFlow) + 'static,
    T: 'static,
{
    use wasm_bindgen::{prelude::*, JsCast};

    let winit_closure = Closure::once_into_js(move || event_loop.run(event_handler));

    // make sure to handle JS exceptions thrown inside start.
    // Otherwise wasm_bindgen_futures Queue would break and never handle any tasks
    // again. This is required, because winit uses JS exception for control flow
    // to escape from `run`.
    if let Err(error) = call_catch(&winit_closure) {
        let is_control_flow_exception = error
            .dyn_ref::<js_sys::Error>()
            .map_or(false, |e| e.message().includes("Using exceptions for control flow", 0));

        if !is_control_flow_exception {
            web_sys::console::error_1(&error);
        }
    }

    #[wasm_bindgen]
    extern "C" {
        #[wasm_bindgen(catch, js_namespace = Function, js_name = "prototype.call.call")]
        fn call_catch(this: &JsValue) -> Result<(), JsValue>;
    }
}

pub async fn async_start<A: App + 'static>(mut app: A, window_builder: WindowBuilder) {
    app.register_logger();
    app.register_panic_hook();

    // Create the window invisible until we are rendering
    let (event_loop, window) = app.create_window(window_builder.with_visible(false));
    let window_size = window.inner_size();

    let iad = app.create_iad().await.unwrap();

    // The one line of unsafe needed. We just need to guarentee that the window
    // outlives the use of the surface.
    //
    // Android has to defer the surface until `Resumed` is fired. This doesn't fire
    // on other platforms though :|
    let mut surface = if cfg!(target_os = "android") {
        None
    } else {
        Some(Arc::new(unsafe { iad.instance.create_surface(&window) }))
    };

    // Make us a renderer.
    let renderer = rend3::Renderer::new(
        iad.clone(),
        A::HANDEDNESS,
        Some(window_size.width as f32 / window_size.height as f32),
    )
    .unwrap();

    // Get the preferred format for the surface.
    //
    // Assume android supports Rgba8Srgb, as it has 100% device coverage
    let format = surface.as_ref().map_or(TextureFormat::Rgba8UnormSrgb, |s| {
        let format = s.get_preferred_format(&iad.adapter).unwrap();

        // Configure the surface to be ready for rendering.
        rend3::configure_surface(
            s,
            &iad.device,
            format,
            glam::UVec2::new(window_size.width, window_size.height),
            rend3::types::PresentMode::Mailbox,
        );

        format
    });

    let base_rendergraph = app.create_base_rendergraph(&renderer);
    let mut data_core = renderer.data_core.lock();
    let routines = Arc::new(DefaultRoutines {
        pbr: Mutex::new(rend3_routine::pbr::PbrRoutine::new(
            &renderer,
            &mut data_core,
            &base_rendergraph.interfaces,
        )),
        skybox: Mutex::new(rend3_routine::skybox::SkyboxRoutine::new(
            &renderer,
            &base_rendergraph.interfaces,
        )),
        tonemapping: Mutex::new(rend3_routine::tonemapping::TonemappingRoutine::new(
            &renderer,
            &base_rendergraph.interfaces,
            format,
        )),
    });
    drop(data_core);

    app.setup(&window, &renderer, &routines, format);

    #[cfg(target_arch = "wasm32")]
    let _observer = resize_observer::ResizeObserver::new(&window, event_loop.create_proxy());

    // We're ready, so lets make things visible
    window.set_visible(true);

    let mut suspended = cfg!(target_os = "android");
    let mut last_user_control_mode = ControlFlow::Poll;
    let mut stored_surface_info = StoredSurfaceInfo {
        size: glam::UVec2::new(window_size.width, window_size.height),
        scale_factor: app.scale_factor(),
        sample_count: app.sample_count(),
    };

    winit_run(event_loop, move |event, _event_loop, control_flow| {
        let event = match event {
            Event::UserEvent(UserResizeEvent::Resize { size, window_id }) => Event::WindowEvent {
                window_id,
                event: WindowEvent::Resized(size),
            },
            e => e,
        };

        if let Some(suspend) = handle_surface(
            &app,
            &window,
            &event,
            &iad.instance,
            &mut surface,
            &renderer,
            format,
            &mut stored_surface_info,
        ) {
            suspended = suspend;
        }

        // We move to Wait when we get suspended so we don't spin at 50k FPS.
        match event {
            Event::Suspended => {
                *control_flow = ControlFlow::Wait;
            }
            Event::Resumed => {
                *control_flow = last_user_control_mode;
            }
            _ => {}
        }

        // We need to block all updates
        if let Event::RedrawRequested(_) | Event::RedrawEventsCleared | Event::MainEventsCleared = event {
            if suspended {
                return;
            }
        }

        app.handle_event(
            &window,
            &renderer,
            &routines,
            &base_rendergraph,
            surface.as_ref(),
            stored_surface_info.size,
            event,
            |c: ControlFlow| {
                *control_flow = c;
                last_user_control_mode = c;
            },
        )
    });
}

struct StoredSurfaceInfo {
    size: UVec2,
    scale_factor: f32,
    sample_count: SampleCount,
}

#[allow(clippy::too_many_arguments)]
fn handle_surface<A: App, T: 'static>(
    app: &A,
    window: &Window,
    event: &Event<T>,
    instance: &Instance,
    surface: &mut Option<Arc<Surface>>,
    renderer: &Arc<Renderer>,
    format: rend3::types::TextureFormat,
    surface_info: &mut StoredSurfaceInfo,
) -> Option<bool> {
    match *event {
        Event::Resumed => {
            *surface = Some(Arc::new(unsafe { instance.create_surface(window) }));
            Some(false)
        }
        Event::Suspended => {
            *surface = None;
            Some(true)
        }
        Event::WindowEvent {
            event: winit::event::WindowEvent::Resized(size),
            ..
        } => {
            log::debug!("resize {:?}", size);
            let size = UVec2::new(size.width, size.height);

            if size.x == 0 || size.y == 0 {
                return Some(false);
            }

            surface_info.size = size;
            surface_info.scale_factor = app.scale_factor();
            surface_info.sample_count = app.sample_count();

            // Reconfigure the surface for the new size.
            rend3::configure_surface(
                surface.as_ref().unwrap(),
                &renderer.device,
                format,
                glam::UVec2::new(size.x, size.y),
                rend3::types::PresentMode::Mailbox,
            );
            // Tell the renderer about the new aspect ratio.
            renderer.set_aspect_ratio(size.x as f32 / size.y as f32);
            Some(false)
        }
        _ => None,
    }
}

pub fn start<A: App + 'static>(app: A, window_builder: WindowBuilder) {
    #[cfg(target_arch = "wasm32")]
    {
        wasm_bindgen_futures::spawn_local(async_start(app, window_builder));
    }

    #[cfg(not(target_arch = "wasm32"))]
    {
        pollster::block_on(async_start(app, window_builder));
    }
}