Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
fornwall committed Jun 27, 2023
1 parent 0abfccf commit 2462746
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
12 changes: 7 additions & 5 deletions site/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<meta name="description" content="Game of Life simulation using WebGPU." />
<meta property="og:description" content="Game of Life simulationg using WebGPU." />
<meta property="og:url" content="https://wgpu-game-of-life.fornwall.net" />
<meta property="og:type" content="website" />
<meta property="og:title" content="Game of Life WebGPU" />
<meta property="og:description" content="A simulation of Game of Life using the WebGPU API." />
<meta property="og:image" content="https://wgpu-game-of-life.fornwall.net/static/screenshot.png" />
<title>Game of Life</title>
<style>
Expand Down Expand Up @@ -112,8 +113,9 @@

<dialog id="about">
<h1>wgpu-game-of-life</h1>
<p id="webgpu-not-working" style="display: none"><strong>⚠️ NOTE:️</strong> WebGPU is not working in your
browser - see <a href="https://caniuse.com/webgpu">caniuse.com/webgpu</a>.</p>
<p id="webgpu-not-working" style="display: none">⚠️ <strong>NOTE:️</strong> <a
href="https://caniuse.com/webgpu">WebGPU is not working in your
browser</a> ⚠️</p>
<p>This is a <a href="https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life">Game of Life</a> simulation done
using the <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGPU_API">WebGPU API</a>.</p>
<p>See <a href="https://github.com/fornwall/wgpu-game-of-life">source code</a>.</p>
Expand Down Expand Up @@ -158,4 +160,4 @@ <h2>About game of life</h2>
</div>
</body>

</html>
</html>
7 changes: 6 additions & 1 deletion src/game-of-life.compute.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
@binding(2) @group(0) var<uniform> size: vec2<u32>;
@binding(3) @group(0) var<uniform> rule: vec2<i32>;

fn modulo_euclidean(a: i32, b: i32) -> i32 {
let m = a % b;
return m + select(0, b, m < 0);
}

fn getIndex(x: i32, y: i32) -> u32 {
let w = i32(size.x);
let h = i32(size.y);
return u32((y % h) * w + (x % w));
return u32(modulo_euclidean(y, h) * w + modulo_euclidean(x, w));
}

fn getCell(x: i32, y: i32) -> u32 {
Expand Down
8 changes: 2 additions & 6 deletions src/game-of-life.render.wgsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ fn vertex_main(@builtin(instance_index) i: u32, @location(0) cell: u32, @locatio
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 max_age_for_color: u32 = 10u;
let max_age_for_color: u32 = 20u;
let intensity = 1.0 - f32(min(cell, max_age_for_color)) / f32(max_age_for_color);

let rgb = select(vec3(0., 0., 0.), spectral_bruton(intensity), cell > 0u);
Expand All @@ -26,19 +26,15 @@ fn fragment_main(@location(0) cell: vec3<f32>) -> @location(0) vec4<f32> {

fn spectral_bruton(w: f32) -> vec3<f32> {
if w < 0.15 {
// return vec3<f32>(-(w - 0.15) / 0.15, 0.0, 1.0);
return vec3<f32>(0.0, 1.0, -(w - 0.325) / (0.325 - 0.275));
} else if w >= 0.15 && w < 0.275 {
//return vec3<f32>(0.0, (w - 0.15) / (0.275 - 0.15), 1.0);
return vec3<f32>(0.0, 1.0, -(w - 0.325) / (0.325 - 0.275));
} else if w >= 0.275 && w < 0.325 {
return vec3<f32>(0.0, 1.0, -(w - 0.325) / (0.325 - 0.275));
} else if w >= 0.325 && w < 0.5 {
return vec3<f32>((w - 0.325) / (0.5 - 0.325), 1.0, 0.0);
} else if w >= 0.5 && w < 0.6625 {
} else if w >= 0.5 {
return vec3<f32>(1.0, -(w - 0.6625) / (0.6625 - 0.5), 0.0);
} else if w >= 0.6625 {
return vec3<f32>(1.0, 0.0, 0.0);
} else {
return vec3<f32>(0.0, 0.0, 0.0);
}
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ impl State {

let cells_width = match grid_size {
Some(v) if [128, 256, 512, 1024, 2048].iter().any(|&e| e == v) => v,
_ => 256,
_ => 512,
};
let cells_height = cells_width;

Expand Down Expand Up @@ -617,7 +617,7 @@ impl State {

let generations_per_second = match generations_per_second {
Some(value) if value > 0 && value < 100 => value,
_ => 4,
_ => 8,
};

let computer_factory = ComputerFactory::new(&device);
Expand Down Expand Up @@ -694,6 +694,7 @@ impl State {
pub fn set_rule_idx(&mut self, new_rule_idx: u32) {
self.rule_idx = new_rule_idx;
let rule = &rules::RULES[self.rule_idx as usize];
self.initial_density = rule.initial_density;
self.queue.write_buffer(
&self.rule_buffer,
0,
Expand Down
10 changes: 10 additions & 0 deletions src/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use wasm_bindgen::prelude::*;
pub struct Rule {
pub born: u16,
pub survives: u16,
pub initial_density: u8,
name: &'static str,
}

Expand Down Expand Up @@ -35,46 +36,55 @@ pub static RULES: [Rule; 9] = [
born: 0b1000,
survives: 0b1100,
name: "Conway's Life",
initial_density: 12,
},
Rule {
born: 0b0_0000_1000,
survives: 0b0_0011_1110,
name: "Maze",
initial_density: 3,
},
Rule {
born: 0b0_0000_1000,
survives: 0b0_0001_1110,
name: "Mazectric",
initial_density: 3,
},
Rule {
born: 0b0_1010_1010,
survives: 0b0_1010_1010,
name: "Replicator",
initial_density: 8,
},
Rule {
born: 0b1_1100_1000,
survives: 0b1_1101_1000,
name: "Day & Night",
initial_density: 30,
},
Rule {
born: 0b0_0010_1000,
survives: 0b1_1011_1100,
name: "Land Rush",
initial_density: 4,
},
Rule {
born: 0b0_0100_1000,
survives: 0b1_1011_1100,
name: "Land Rush 2",
initial_density: 4,
},
Rule {
born: 0b1_1100_1000,
survives: 0b1_1110_1100,
name: "Stains",
initial_density: 8,
},
Rule {
born: 0b1_1110_0000,
survives: 0b1_1111_0000,
name: "Vote",
initial_density: 50,
},
];

Expand Down

0 comments on commit 2462746

Please sign in to comment.