From 1dc6565a263b685a38adb04ab9036d9cdc9a5d96 Mon Sep 17 00:00:00 2001 From: nullprop Date: Sat, 28 Jan 2023 12:23:43 +0200 Subject: [PATCH] working wasm build --- Cargo.lock | 68 +++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +- README.md | 7 ++++- src/core/state.rs | 6 +++- src/core/window.rs | 76 ++++++++++++++++++++++++++-------------------- src/lib.rs | 2 +- 6 files changed, 125 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7a4faef..778f1a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -940,6 +940,40 @@ dependencies = [ "tiff", ] +[[package]] +name = "include-flate" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfdcb449c721557c1cf89bbd3412bf33fa963289e26e9badbd824a960912e148" +dependencies = [ + "include-flate-codegen-exports", + "lazy_static", + "libflate", +] + +[[package]] +name = "include-flate-codegen" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a7d6e1419fa3129eb0802b4c99603c0d425c79fb5d76191d5a20d0ab0d664e8" +dependencies = [ + "libflate", + "proc-macro-hack", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "include-flate-codegen-exports" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75657043ffe3d8280f1cb8aef0f505532b392ed7758e0baeac22edadcee31a03" +dependencies = [ + "include-flate-codegen", + "proc-macro-hack", +] + [[package]] name = "indexmap" version = "1.9.2" @@ -1055,6 +1089,26 @@ version = "0.2.139" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" +[[package]] +name = "libflate" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05605ab2bce11bcfc0e9c635ff29ef8b2ea83f29be257ee7d730cac3ee373093" +dependencies = [ + "adler32", + "crc32fast", + "libflate_lz77", +] + +[[package]] +name = "libflate_lz77" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39a734c0493409afcd49deee13c006a04e3586b9761a03543c6272c9c51f2f5a" +dependencies = [ + "rle-decode-fast", +] + [[package]] name = "libloading" version = "0.7.4" @@ -1516,6 +1570,12 @@ dependencies = [ "toml_edit", ] +[[package]] +name = "proc-macro-hack" +version = "0.5.20+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068" + [[package]] name = "proc-macro2" version = "1.0.50" @@ -1618,12 +1678,19 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1382d1f0a252c4bf97dc20d979a2fdd05b024acd7c2ed0f7595d7817666a157" +[[package]] +name = "rle-decode-fast" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3582f63211428f83597b51b2ddb88e2a91a9d52d12831f9d08f5e624e8977422" + [[package]] name = "rust-embed" version = "6.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "283ffe2f866869428c92e0d61c2f35dfb4355293cdfdc48f49e895c15f1333d1" dependencies = [ + "include-flate", "rust-embed-impl", "rust-embed-utils", "walkdir", @@ -2270,6 +2337,7 @@ dependencies = [ "glob", "gltf", "image 0.24.5", + "instant", "log", "pollster", "regex", diff --git a/Cargo.toml b/Cargo.toml index 57e952d..b6f9907 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,8 +24,9 @@ tobj = { version = "3.2.1", features = ["async"] } gltf = "1.0.0" wgpu-types = "0.14.1" regex = "1.7.1" -rust-embed = "6.4.2" +rust-embed = { version = "6.4.2", features = ["debug-embed", "compression"]} cfg-if = "1" +instant = "0.1.12" [target.'cfg(target_arch = "wasm32")'.dependencies] console_error_panic_hook = "0.1.6" diff --git a/README.md b/README.md index a48017d..31bbdea 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,9 @@ A small [wgpu](https://github.com/gfx-rs/wgpu) renderer written in [Rust](https: - (G: Smith's Schlick-GGX) - (D: Trowbridge-Reitz GGX) - glTF models -- 1 realtime light +- 1 realtime pointlight - Simple wgsl preprocessor for includes +- Works on WASM Things I would like to add: - Shadow mapping @@ -19,13 +20,17 @@ Things I would like to add: - `src/core/state.rs` is a mess; separate input handling, pipeline, passes - SSAO - Bloom +- AA +- Texture filtering - Immediate mode UI (dear imgui, egui) - Editing material properties, lights at runtime through UI - Some type of GI (DDGI, VXGI) ## References - [Learn Wgpu](https://sotrh.github.io/learn-wgpu/) +- [wgpu examples](https://github.com/gfx-rs/wgpu/blob/master/wgpu/examples) - [Learn OpenGL](https://learnopengl.com/) + - PBR ## Assets - Sponza diff --git a/src/core/state.rs b/src/core/state.rs index 1450471..79b1b11 100644 --- a/src/core/state.rs +++ b/src/core/state.rs @@ -55,7 +55,11 @@ impl State { .request_device( &wgpu::DeviceDescriptor { features: wgpu::Features::empty(), - limits: wgpu::Limits::default(), + limits: if cfg!(target_arch = "wasm32") { + wgpu::Limits::downlevel_webgl2_defaults() + } else { + wgpu::Limits::default() + }, label: None, }, None, // Trace path diff --git a/src/core/window.rs b/src/core/window.rs index 136256d..ebd9c6f 100644 --- a/src/core/window.rs +++ b/src/core/window.rs @@ -1,5 +1,3 @@ -use std::time::Instant; - use super::state::State; use winit::{ event::*, @@ -14,14 +12,9 @@ pub async fn run() { .with_maximized(true) .build(&event_loop) .unwrap(); - let mut state = State::new(&window).await; - let mut last_render = Instant::now(); #[cfg(target_arch = "wasm32")] { - std::panic::set_hook(Box::new(console_error_panic_hook::hook)); - console_log::init().expect("could not initialize logger"); - // Winit prevents sizing with CSS, so we have to set // the size manually when on web. use winit::dpi::PhysicalSize; @@ -37,14 +30,12 @@ pub async fn run() { }) .expect("Couldn't append canvas to document body."); } - #[cfg(not(target_arch = "wasm"))] - { - window - .set_cursor_grab(winit::window::CursorGrabMode::Confined) - .unwrap(); - } - window.set_cursor_visible(false); + lock_cursor(&window, true); + + let mut state = State::new(&window).await; + let mut last_render = instant::Instant::now(); + let mut is_focused = true; // Event loop event_loop.run(move |event, _, control_flow| { @@ -67,7 +58,12 @@ pub async fn run() { .. }, .. - } => *control_flow = ControlFlow::Exit, + } => { + #[cfg(not(target_arch = "wasm32"))] + { + *control_flow = ControlFlow::Exit; + } + } WindowEvent::Resized(physical_size) => { state.resize(*physical_size); } @@ -75,32 +71,28 @@ pub async fn run() { state.resize(**new_inner_size); } WindowEvent::Focused(focused) => { - window - .set_cursor_grab(if *focused { - winit::window::CursorGrabMode::Confined - } else { - winit::window::CursorGrabMode::None - }) - .unwrap(); - window.set_cursor_visible(!*focused); + lock_cursor(&window, *focused); + is_focused = *focused; } _ => {} } } } Event::RedrawRequested(window_id) if window_id == window.id() => { - let now = Instant::now(); + let now = instant::Instant::now(); let dt = now - last_render; last_render = now; - state.update(dt); - match state.render() { - Ok(_) => {} - // Reconfigure the surface if lost - Err(wgpu::SurfaceError::Lost) => state.resize(state.size), - // The system is out of memory, we should probably quit - Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit, - // All other errors (Outdated, Timeout) should be resolved by the next frame - Err(e) => eprintln!("{:?}", e), + if is_focused { + state.update(dt); + match state.render() { + Ok(_) => {} + // Reconfigure the surface if lost + Err(wgpu::SurfaceError::Lost) => state.resize(state.size), + // The system is out of memory, we should probably quit + Err(wgpu::SurfaceError::OutOfMemory) => *control_flow = ControlFlow::Exit, + // All other errors (Outdated, Timeout) should be resolved by the next frame + Err(e) => eprintln!("{:?}", e), + } } } Event::MainEventsCleared => { @@ -112,3 +104,21 @@ pub async fn run() { } }); } + +fn lock_cursor(window: &winit::window::Window, lock: bool) { + if lock { + window + .set_cursor_grab(if cfg!(target_arch = "wasm32") { + winit::window::CursorGrabMode::Locked + } else { + winit::window::CursorGrabMode::Confined + }) + .unwrap(); + window.set_cursor_visible(false); + } else { + window + .set_cursor_grab(winit::window::CursorGrabMode::None) + .unwrap(); + window.set_cursor_visible(true); + } +} diff --git a/src/lib.rs b/src/lib.rs index 8d92b12..6f1283f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,7 @@ pub fn run() { #[cfg(target_arch = "wasm32")] { std::panic::set_hook(Box::new(console_error_panic_hook::hook)); - console_log::init_with_level(log::Level::Warn).expect("Couldn't initialize logger"); + console_log::init_with_level(log::Level::Info).expect("Couldn't initialize logger"); wasm_bindgen_futures::spawn_local(core::window::run()); } #[cfg(not(target_arch = "wasm32"))]