Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 6 additions & 27 deletions apps/typegpu-docs/src/examples/simulation/game-of-life/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ import { sdLine, sdRoundedBox2d } from '@typegpu/sdf';
import { setupInput } from './input.ts';
import { defineControls } from '../../common/defineControls.ts';

const root = await tgpu.init({
device: { optionalFeatures: ['timestamp-query'] },
});
const hasTimestamp = root.enabledFeatures.has('timestamp-query');
const root = await tgpu.init();
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
const context = root.configureContext({ canvas, alphaMode: 'premultiplied' });

Expand Down Expand Up @@ -141,30 +138,14 @@ const handleDrawBitpacked = root.createGuardedComputePipeline((px, py) => {
std.textureStore(computeLayout.$.next, d.vec2u(px, py), d.vec4u(result, 0, 0, 0));
});

const perf = { sum: 0, count: 0, window: 120 };

function createPipeline(name: string, compute: TgpuComputeFn) {
const base = root.with(gameSizeAccessor, gameSizeUniform).createComputePipeline({ compute });

return !hasTimestamp
? base
: base.withPerformanceCallback((start, end) => {
perf.sum += Number(end - start) / 1_000_00;
perf.count++;
if (perf.count >= perf.window) {
console.log(
`${name}: ${(perf.sum / perf.count).toFixed(3)}ms average over ${perf.window} frames`,
);
perf.sum = 0;
perf.count = 0;
}
});
function createPipeline(compute: TgpuComputeFn) {
return root.with(gameSizeAccessor, gameSizeUniform).createComputePipeline({ compute });
}

const computePipelines = {
tiled: createPipeline('Tiled', tiledCompute),
naive: createPipeline('Naive', naiveCompute),
bitpacked: createPipeline('Bitpacked', bitpackedCompute),
tiled: createPipeline(tiledCompute),
naive: createPipeline(naiveCompute),
bitpacked: createPipeline(bitpackedCompute),
};

const sampleRegular = (sampleUv: d.v2f, gs: number): number => {
Expand Down Expand Up @@ -435,8 +416,6 @@ export const controls = defineControls({
const wasBitpacked = chosenPipeline === 'bitpacked';
const isBitpacked = value === 'bitpacked';
chosenPipeline = value;
perf.sum = 0;
perf.count = 0;
if (wasBitpacked !== isBitpacked) {
recreateResources(gameSize);
}
Expand Down
Loading