working wasm build

This commit is contained in:
Lauri Räsänen 2023-01-28 12:23:43 +02:00
parent dc3bd2a433
commit 1dc6565a26
6 changed files with 125 additions and 37 deletions

68
Cargo.lock generated
View file

@ -940,6 +940,40 @@ dependencies = [
"tiff", "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]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.9.2" version = "1.9.2"
@ -1055,6 +1089,26 @@ version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79" 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]] [[package]]
name = "libloading" name = "libloading"
version = "0.7.4" version = "0.7.4"
@ -1516,6 +1570,12 @@ dependencies = [
"toml_edit", "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]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.50" version = "1.0.50"
@ -1618,12 +1678,19 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1382d1f0a252c4bf97dc20d979a2fdd05b024acd7c2ed0f7595d7817666a157" checksum = "f1382d1f0a252c4bf97dc20d979a2fdd05b024acd7c2ed0f7595d7817666a157"
[[package]]
name = "rle-decode-fast"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3582f63211428f83597b51b2ddb88e2a91a9d52d12831f9d08f5e624e8977422"
[[package]] [[package]]
name = "rust-embed" name = "rust-embed"
version = "6.4.2" version = "6.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "283ffe2f866869428c92e0d61c2f35dfb4355293cdfdc48f49e895c15f1333d1" checksum = "283ffe2f866869428c92e0d61c2f35dfb4355293cdfdc48f49e895c15f1333d1"
dependencies = [ dependencies = [
"include-flate",
"rust-embed-impl", "rust-embed-impl",
"rust-embed-utils", "rust-embed-utils",
"walkdir", "walkdir",
@ -2270,6 +2337,7 @@ dependencies = [
"glob", "glob",
"gltf", "gltf",
"image 0.24.5", "image 0.24.5",
"instant",
"log", "log",
"pollster", "pollster",
"regex", "regex",

View file

@ -24,8 +24,9 @@ tobj = { version = "3.2.1", features = ["async"] }
gltf = "1.0.0" gltf = "1.0.0"
wgpu-types = "0.14.1" wgpu-types = "0.14.1"
regex = "1.7.1" regex = "1.7.1"
rust-embed = "6.4.2" rust-embed = { version = "6.4.2", features = ["debug-embed", "compression"]}
cfg-if = "1" cfg-if = "1"
instant = "0.1.12"
[target.'cfg(target_arch = "wasm32")'.dependencies] [target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "0.1.6" console_error_panic_hook = "0.1.6"

View file

@ -9,8 +9,9 @@ A small [wgpu](https://github.com/gfx-rs/wgpu) renderer written in [Rust](https:
- (G: Smith's Schlick-GGX) - (G: Smith's Schlick-GGX)
- (D: Trowbridge-Reitz GGX) - (D: Trowbridge-Reitz GGX)
- glTF models - glTF models
- 1 realtime light - 1 realtime pointlight
- Simple wgsl preprocessor for includes - Simple wgsl preprocessor for includes
- Works on WASM
Things I would like to add: Things I would like to add:
- Shadow mapping - Shadow mapping
@ -19,13 +20,17 @@ Things I would like to add:
- `src/core/state.rs` is a mess; separate input handling, pipeline, passes - `src/core/state.rs` is a mess; separate input handling, pipeline, passes
- SSAO - SSAO
- Bloom - Bloom
- AA
- Texture filtering
- Immediate mode UI (dear imgui, egui) - Immediate mode UI (dear imgui, egui)
- Editing material properties, lights at runtime through UI - Editing material properties, lights at runtime through UI
- Some type of GI (DDGI, VXGI) - Some type of GI (DDGI, VXGI)
## References ## References
- [Learn Wgpu](https://sotrh.github.io/learn-wgpu/) - [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/) - [Learn OpenGL](https://learnopengl.com/)
- PBR
## Assets ## Assets
- Sponza - Sponza

View file

@ -55,7 +55,11 @@ impl State {
.request_device( .request_device(
&wgpu::DeviceDescriptor { &wgpu::DeviceDescriptor {
features: wgpu::Features::empty(), 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, label: None,
}, },
None, // Trace path None, // Trace path

View file

@ -1,5 +1,3 @@
use std::time::Instant;
use super::state::State; use super::state::State;
use winit::{ use winit::{
event::*, event::*,
@ -14,14 +12,9 @@ pub async fn run() {
.with_maximized(true) .with_maximized(true)
.build(&event_loop) .build(&event_loop)
.unwrap(); .unwrap();
let mut state = State::new(&window).await;
let mut last_render = Instant::now();
#[cfg(target_arch = "wasm32")] #[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 // Winit prevents sizing with CSS, so we have to set
// the size manually when on web. // the size manually when on web.
use winit::dpi::PhysicalSize; use winit::dpi::PhysicalSize;
@ -37,14 +30,12 @@ pub async fn run() {
}) })
.expect("Couldn't append canvas to document body."); .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
event_loop.run(move |event, _, control_flow| { 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) => { WindowEvent::Resized(physical_size) => {
state.resize(*physical_size); state.resize(*physical_size);
} }
@ -75,23 +71,18 @@ pub async fn run() {
state.resize(**new_inner_size); state.resize(**new_inner_size);
} }
WindowEvent::Focused(focused) => { WindowEvent::Focused(focused) => {
window lock_cursor(&window, *focused);
.set_cursor_grab(if *focused { is_focused = *focused;
winit::window::CursorGrabMode::Confined
} else {
winit::window::CursorGrabMode::None
})
.unwrap();
window.set_cursor_visible(!*focused);
} }
_ => {} _ => {}
} }
} }
} }
Event::RedrawRequested(window_id) if window_id == window.id() => { Event::RedrawRequested(window_id) if window_id == window.id() => {
let now = Instant::now(); let now = instant::Instant::now();
let dt = now - last_render; let dt = now - last_render;
last_render = now; last_render = now;
if is_focused {
state.update(dt); state.update(dt);
match state.render() { match state.render() {
Ok(_) => {} Ok(_) => {}
@ -103,6 +94,7 @@ pub async fn run() {
Err(e) => eprintln!("{:?}", e), Err(e) => eprintln!("{:?}", e),
} }
} }
}
Event::MainEventsCleared => { Event::MainEventsCleared => {
// RedrawRequested will only trigger once, unless we manually // RedrawRequested will only trigger once, unless we manually
// request it. // request it.
@ -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);
}
}

View file

@ -11,7 +11,7 @@ pub fn run() {
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
{ {
std::panic::set_hook(Box::new(console_error_panic_hook::hook)); 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()); wasm_bindgen_futures::spawn_local(core::window::run());
} }
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]