Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Jun 16, 2023
1 parent f34bca4 commit ecfa7a2
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 103 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ log = "*"
pollster = "*"
rand = "*"
wgpu = "*"
winit = { git = "https://github.com/rust-windowing/winit", rev = "6300cf915e21089481398c96cc38808764de0a9c" }
winit = { git = "https://github.com/rust-windowing/winit", rev = "47488909353bb6d8e52801b8824b0e2352e4ab63" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
console_error_panic_hook = "*"
Expand Down
21 changes: 20 additions & 1 deletion site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,30 @@
html, body { width: 100%; height: 100%; background: black; }
canvas { display:block; height: 100vh; width: 100vw; }
canvas:focus { outline: none !important; }
#fallback {
color: white;
display: flex;
flex-direction: column;
align-items: center;
width: 100%;
height: 100%;
margin-left: auto;
margin-right: auto;
text-align: center;
}
#fallback a {
color: white;
}
</style>
</head>

<body>
<canvas id="webgpu-canvas" width="100" height="100" tabindex="0"/>
<canvas id="webgpu-canvas" width="100" height="100" tabindex="0"></canvas>
<div id="fallback" style="display:none">
<h1>wgpu Game of life</h1>
<p>This is a game of life simulation done using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGPU_API">WebGPU</a>.</p>
<p>See <a href="https://github.com/fornwall/wgpu-game-of-life">source code</a>.</p>
</div>
</body>

</html>
Expand Down
17 changes: 10 additions & 7 deletions site/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { resizeCanvasToDisplaySize } from "./resize.js";
import init, { run } from "./generated/wgpu_game_of_life.js";

init().then(() => {
const canvas = document.getElementById("webgpu-canvas");
canvas.focus();
run();
//setTimeout(() => resizeCanvasToDisplaySize(canvas), 0);
});
const canvas = document.getElementById("webgpu-canvas");
canvas.focus();

try {
await init();
await run();
} catch (e) {
canvas.remove();
console.log('error', e);
document.getElementById('fallback').style.display = 'flex';
}
32 changes: 0 additions & 32 deletions site/resize.js

This file was deleted.

9 changes: 2 additions & 7 deletions src/event_loop.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::{Duration, Instant};

use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::ControlFlow,
Expand Down Expand Up @@ -46,17 +44,14 @@ pub fn handle_event_loop(event: &Event<()>, state: &mut State, control_flow: &mu
WindowEvent::ScaleFactorChanged { new_inner_size, .. } => {
state.resize(**new_inner_size);
}
WindowEvent::CursorMoved {
device_id: _,
position,
} => {
WindowEvent::CursorMoved { position, .. } => {
state.cursor_position = *position;
}
WindowEvent::MouseInput {
state: winit::event::ElementState::Pressed,
..
} => {
error!("Mouse pressed");
error!("Mouse pressed: {:?}", state.cursor_position);
}
_ => {}
}
Expand Down
7 changes: 3 additions & 4 deletions src/game-of-life.compute.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
@binding(2) @group(0) var<storage, read> size: vec2<u32>;

fn getIndex(x: i32, y: i32) -> u32 {
let h = i32(size.x); // TODO: Should come from override, must mach lib.rs
let w = i32(size.y); // TODO: Should come from override, must match lib.rs

let w = i32(size.x);
let h = i32(size.y);
return u32((y % h) * w + (x % w));
}

Expand All @@ -27,5 +26,5 @@ fn main(@builtin(global_invocation_id) grid: vec3<u32>) {
// Day and night: https://conwaylife.com/wiki/OCA:Day_%26_Night
// select(u32(n == 3u || n == 6u || n == 7u || n == 8u), u32(n == 3u || n == 4u || n == 6u || n == 7u || n == 8u), getCell(x, y) == 1u);
// Highlife:
//select(u32(n == 3u || n == 6u), u32(n == 2u || n == 3u), getCell(x, y) == 1u);
// select(u32(n == 3u || n == 6u), u32(n == 2u || n == 3u), getCell(x, y) == 1u);
}
4 changes: 2 additions & 2 deletions src/game-of-life.render.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ struct Out {
fn vertex_main(@builtin(instance_index) i: u32, @location(0) cell: u32, @location(1) pos: vec2<u32>) -> Out {
let w = size.x;
let h = size.y;
let x = (f32(i % w + pos.x) / f32(w) - 0.5) * 2. * f32(w) / f32(max(w, h));
let y = (f32((i - (i % w)) / w + pos.y) / f32(h) - 0.5) * 2. * f32(h) / f32(max(w, h));
let x = (f32(i % w + pos.x) / f32(w) - 0.5) * 2. * f32(w) / f32(w);
let y = (f32((i - (i % w)) / w + pos.y) / f32(h) - 0.5) * 2. * f32(h) / f32(h);

let c = f32(i) / f32(size.x * size.y);
let r = f32(cell) * f32(i % w) / f32(w);
Expand Down
Loading

0 comments on commit ecfa7a2

Please sign in to comment.