Skip to content

Commit 5fcb4fb

Browse files
authored
docs: Make game of life and increment examples stable (#865)
1 parent dbfd03c commit 5fcb4fb

File tree

3 files changed

+17
-17
lines changed

3 files changed

+17
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
22
"title": "Increment",
3-
"category": "simple",
4-
"tags": ["experimental"]
3+
"category": "simple"
54
}

apps/typegpu-docs/src/content/examples/simulation/game-of-life/index.ts

+15-13
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ const device = root.device;
66

77
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
88
const context = canvas.getContext('webgpu') as GPUCanvasContext;
9-
const devicePixelRatio = window.devicePixelRatio;
10-
canvas.width = canvas.clientWidth * devicePixelRatio;
11-
canvas.height = canvas.clientHeight * devicePixelRatio;
9+
1210
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
1311
context.configure({
1412
device,
1513
format: presentationFormat,
14+
alphaMode: 'premultiplied',
1615
});
1716

1817
let workgroupSize = 16;
@@ -37,6 +36,7 @@ const bindGroupLayoutCompute = tgpu.bindGroupLayout({
3736
access: 'mutable',
3837
},
3938
});
39+
4040
const bindGroupLayoutRender = tgpu.bindGroupLayout({
4141
size: {
4242
uniform: d.vec2u,
@@ -121,20 +121,18 @@ fn frag(@location(0) cell: f32, @builtin(position) pos: vec4f) -> @location(0) v
121121
}
122122
123123
return vec4f(
124-
max(f32(cell) * pos.x / 1024, 0),
125-
max(f32(cell) * pos.y / 1024, 0),
126-
max(f32(cell) * (1 - pos.x / 1024), 0),
127-
1.
124+
pos.x / 2048,
125+
pos.y / 2048,
126+
1 - pos.x / 2048,
127+
0.8
128128
);
129129
}`,
130130
externals: {
131131
...bindGroupLayoutRender.bound,
132132
},
133133
}),
134134
});
135-
let commandEncoder: GPUCommandEncoder;
136135

137-
// compute pipeline
138136
const computePipeline = device.createComputePipeline({
139137
layout: device.createPipelineLayout({
140138
bindGroupLayouts: [root.unwrap(bindGroupLayoutCompute)],
@@ -159,7 +157,7 @@ const resetGameData = () => {
159157
const length = gameWidth * gameHeight;
160158
const cells = Array.from({ length })
161159
.fill(0)
162-
.map((_, i) => (Math.random() < 0.25 ? 1 : 0));
160+
.map(() => (Math.random() < 0.25 ? 1 : 0));
163161

164162
const buffer0 = root
165163
.createBuffer(d.arrayOf(d.u32, length), cells)
@@ -191,14 +189,13 @@ const resetGameData = () => {
191189
colorAttachments: [
192190
{
193191
view,
194-
clearValue: { r: 239 / 255, g: 239 / 255, b: 249 / 255, a: 1 },
195192
loadOp: 'clear',
196193
storeOp: 'store',
197194
},
198195
],
199196
};
200197

201-
commandEncoder = device.createCommandEncoder();
198+
const commandEncoder = device.createCommandEncoder();
202199
const passEncoderCompute = commandEncoder.beginComputePass();
203200

204201
passEncoderCompute.setPipeline(computePipeline);
@@ -271,7 +268,7 @@ const renderPipeline = device.createRenderPipeline({
271268

272269
export const controls = {
273270
size: {
274-
initial: '1024',
271+
initial: '64',
275272
options: [16, 32, 64, 128, 256, 512, 1024].map((x) => x.toString()),
276273
onSelectChange: (value: string) => {
277274
gameWidth = Number.parseInt(value);
@@ -306,9 +303,14 @@ export const controls = {
306303
paused = value;
307304
},
308305
},
306+
307+
Reset: {
308+
onButtonClick: resetGameData,
309+
},
309310
};
310311

311312
export function onCleanup() {
313+
paused = true;
312314
root.destroy();
313315
root.device.destroy();
314316
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
22
"title": "Game of Life",
3-
"category": "simulation",
4-
"tags": ["experimental"]
3+
"category": "simulation"
54
}

0 commit comments

Comments
 (0)