Skip to content

Commit

Permalink
wasm32-unknown-unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
caiiiycuk committed Dec 9, 2021
1 parent 664e25b commit 6bc3562
Show file tree
Hide file tree
Showing 19 changed files with 790 additions and 44 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
name: Check
on:
push:
branches: [master]
branches: [master, vange-rs-web]
pull_request:
branches: [master]
branches: [master, vange-rs-web]

jobs:
build:
Expand All @@ -14,7 +14,9 @@ jobs:
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v2
- run: rustup target add wasm32-unknown-unknown
- run: cargo check
- if: matrix.os == 'ubuntu-latest'
run: cargo check --features glsl
- run: cargo check --bin road --features nodata --target wasm32-unknown-unknown
- run: cargo test
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@
.fuse_hidden*
.DS_Store
last-shader.wgsl

# web version
web/assets/res
web/assets/road.css
web/road*
res_linux
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
default = []
glsl = ["glsl-to-spirv", "wgpu/spirv"]
profile = ["profiling/profile-with-tracy"]
nodata = []

[[bin]]
name = "road"
Expand Down Expand Up @@ -60,13 +61,13 @@ rust-ini = "0.17"
serde = "1.0"
serde_derive = "1.0"
serde_scan = "0.4"
wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "c8d572a", features = [] }
wgpu = { git = "https://github.com/gfx-rs/wgpu", rev = "c8d572a", features = ["webgl"] }
# binaries
env_logger = "0.8"
getopts = "0.2"
obj = "0.10"
png = "0.16"
winit = "0.26"
winit = { version = "0.26", features = [] }

[dev-dependencies]
naga = { git = "https://github.com/gfx-rs/naga", rev = "c69f676", features = ["wgsl-in"] }
Expand All @@ -82,5 +83,11 @@ default-features = false
#wgpu-core = { path = "../wgpu/wgpu-core" }
#wgpu-types = { path = "../wgpu/wgpu-types" }

[patch."https://github.com/gfx-rs/naga"]
#naga = { path = "../naga" }
[target.'cfg(target_arch = "wasm32")'.dependencies]
web-sys = { version = "0.3.55", features = [] }
wasm-bindgen = "0.2.78"
wasm-bindgen-futures = "0.4"
console_error_panic_hook = { version = "0.1.7" }

[target.'cfg(target_arch = "wasm32")'.dependencies.getrandom]
features = ["js"]
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,43 @@ Controls:

<img alt="game" src="etc/shots/Road11-pause.png" width="25%">

### Web version

Stage | State | Note |
-------------------- | ------------------ | ----------- |
Support FS | :white_check_mark: | [memfs]() |
WebGL Initialization | :white_check_mark: ||
Loading worlds | :white_check_mark: ||
Loading heights | :white_check_mark: ||
Loading data | :white_check_mark: ||
Loading flood | :white_check_mark: ||
Render | :white_check_mark: | WebGPU not tested |

**Resources**

To build game **you must** put resources of original game in `res_linux/data` folder.

**Debug build**

```sh
cargo build --target wasm32-unknown-unknown --bin road --verbose && \
wasm-bindgen --out-dir web/ --target web --keep-debug target/wasm32-unknown-unknown/debug/road.wasm
```

**Release build**
```sh
cargo build --target wasm32-unknown-unknown --bin road --verbose --release && \
wasm-bindgen --out-dir web/ --target web target/wasm32-unknown-unknown/release/road.wasm
```

**Build web page**
```sh
cd web
./build.sh
```

After building you can run game just by serving `web` folder.

### Mechous viewer/debugger
`car` binary allows to see the mechos with items selected by the configuration. It also shows the debug collision info.
```bash
Expand Down
77 changes: 58 additions & 19 deletions bin/boilerplate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ use vangers::{
render::{ScreenTargets, DEPTH_FORMAT},
};

use futures::executor::{LocalPool, LocalSpawner};
use futures::executor::{block_on, LocalPool, LocalSpawner};
use log::info;
use winit::{
event,
event_loop::{ControlFlow, EventLoop},
window::{Window, WindowBuilder},
};

#[cfg(target_arch = "wasm32")]
use crate::web;

pub trait Application {
fn on_key(&mut self, input: event::KeyboardInput) -> bool;
fn on_mouse_wheel(&mut self, _delta: event::MouseScrollDelta) {}
Expand Down Expand Up @@ -53,12 +56,15 @@ pub struct HarnessOptions {
}

impl Harness {
#[cfg(not(target_arch = "wasm32"))]
pub fn init(options: HarnessOptions) -> (Self, config::Settings) {
env_logger::init();
let mut task_pool = LocalPool::new();
block_on(Harness::init_async(options))
}

pub async fn init_async(options: HarnessOptions) -> (Self, config::Settings) {
info!("Loading the settings");
let settings = config::Settings::load("config/settings.ron");

let extent = wgpu::Extent3d {
width: settings.window.size[0],
height: settings.window.size[1],
Expand All @@ -76,19 +82,25 @@ impl Harness {
.unwrap();
let surface = unsafe { instance.create_surface(&window) };

info!("Initializing the device");
let adapter = task_pool
.run_until(instance.request_adapter(&wgpu::RequestAdapterOptions {
info!("Initializing the device:adapter");
let adapter = instance
.request_adapter(&wgpu::RequestAdapterOptions {
power_preference: wgpu::PowerPreference::HighPerformance,
compatible_surface: Some(&surface),
force_fallback_adapter: false,
}))
.expect("Unable to initialize GPU via the selected backend.");
})
.await
.expect("Unable to initialize GPU via the selected backend (adapter).");

let downlevel_caps = adapter.get_downlevel_properties();
let adapter_limits = adapter.limits();

#[cfg(target_arch = "wasm32")]
let mut limits = wgpu::Limits::downlevel_webgl2_defaults();

#[cfg(not(target_arch = "wasm32"))]
let mut limits = wgpu::Limits::downlevel_defaults();

if options.uses_level {
let desired_height = 16 << 10;
limits.max_texture_dimension_2d =
Expand All @@ -102,8 +114,10 @@ impl Harness {
desired_height
};
}
let (device, queue) = task_pool
.run_until(adapter.request_device(

info!("Initializing the device:request");
let (device, queue) = adapter
.request_device(
&wgpu::DeviceDescriptor {
label: None,
features: wgpu::Features::empty(),
Expand All @@ -114,8 +128,9 @@ impl Harness {
} else {
Some(std::path::Path::new(&settings.render.wgpu_trace_path))
},
))
.unwrap();
)
.await
.expect("Unable to initialize GPU via the selected backend (request).");

let config = wgpu::SurfaceConfiguration {
usage: wgpu::TextureUsages::RENDER_ATTACHMENT,
Expand All @@ -141,7 +156,7 @@ impl Harness {
.create_view(&wgpu::TextureViewDescriptor::default());

let harness = Harness {
task_pool,
task_pool: LocalPool::new(),
event_loop,
window,
device,
Expand All @@ -158,9 +173,15 @@ impl Harness {
}

pub fn main_loop<A: 'static + Application>(self, mut app: A) {
#[cfg(not(target_arch = "wasm32"))]
use std::time;

#[cfg(target_arch = "wasm32")]
let mut last_time = web::now();

#[cfg(not(target_arch = "wasm32"))]
let mut last_time = time::Instant::now();

let mut needs_reload = false;
let Harness {
mut task_pool,
Expand All @@ -181,6 +202,9 @@ impl Harness {
*control_flow = ControlFlow::Poll;
task_pool.run_until_stalled();

#[cfg(target_arch = "wasm32")]
web::bind_once(&mut app);

match event {
event::Event::WindowEvent {
event: event::WindowEvent::Resized(size),
Expand Down Expand Up @@ -241,13 +265,28 @@ impl Harness {
},
event::Event::MainEventsCleared => {
let spawner = task_pool.spawner();
let duration = time::Instant::now() - last_time;
last_time += duration;
let delta = duration.as_secs() as f32 + duration.subsec_nanos() as f32 * 1.0e-9;

let update_command_buffers = app.update(&device, delta, &spawner);
if !update_command_buffers.is_empty() {
queue.submit(update_command_buffers);
let delta: f32;

#[cfg(target_arch = "wasm32")]
{
let duration = web::now() - last_time;
last_time += duration;
delta = (duration / 1000.0) as f32;
}

#[cfg(not(target_arch = "wasm32"))]
{
let duration = time::Instant::now() - last_time;
last_time += duration;
delta = duration.as_secs() as f32 + duration.subsec_nanos() as f32 * 1.0e-9;
}

if delta > 0.0 {
let update_command_buffers = app.update(&device, delta, &spawner);
if !update_command_buffers.is_empty() {
queue.submit(update_command_buffers);
}
}

match surface.get_current_texture() {
Expand Down
9 changes: 8 additions & 1 deletion bin/road/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,14 +811,21 @@ impl Application for Game {
}

{
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;

let clipper = Clipper::new(&self.cam);
let max_quant = self.max_quant;
let common = &self.db.common;
let level = &self.level;

self.agents.par_iter_mut().for_each(|a| {
#[cfg(not(target_arch = "wasm32"))]
let agent_iter = self.agents.par_iter_mut();

#[cfg(target_arch = "wasm32")]
let agent_iter = self.agents.iter_mut();

agent_iter.for_each(|a| {
let mut dt = physics_dt;
a.cpu_apply_control(input_factor, common);

Expand Down
Loading

0 comments on commit 6bc3562

Please sign in to comment.