From ce236013c96b7678a0364c720601274f9c649bd3 Mon Sep 17 00:00:00 2001 From: Szymon Szulc Date: Wed, 29 Apr 2026 13:37:14 +0200 Subject: [PATCH 1/2] cleanup --- .../algorithms/probability/executor.ts | 25 ++++++------------- .../examples/algorithms/probability/index.ts | 6 +++-- .../algorithms/probability/plotter.ts | 4 +++ 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/apps/typegpu-docs/src/examples/algorithms/probability/executor.ts b/apps/typegpu-docs/src/examples/algorithms/probability/executor.ts index d436cf6d2b..0fc704cda4 100644 --- a/apps/typegpu-docs/src/examples/algorithms/probability/executor.ts +++ b/apps/typegpu-docs/src/examples/algorithms/probability/executor.ts @@ -79,23 +79,7 @@ export class Executor { }); } - cachedPipeline(distribution: TgpuFn<() => d.Vec3f>) { - if (!import.meta.env.DEV) { - throw new Error('Function only for testing purposes'); - } - - if (!this.#pipelineCache.has(distribution)) { - const pipeline = this.#root - .with(this.#distributionSlot, distribution) - .createComputePipeline({ compute: this.#dataMoreWorkersFunc }); - this.#pipelineCache.set(distribution, pipeline); - } - - // oxlint-disable-next-line typescript/no-non-null-assertion -- just checked it above - return this.#pipelineCache.get(distribution)!; - } - - async executeMoreWorkers(distribution: TgpuFn<() => d.Vec3f>): Promise { + getPipeline(distribution: TgpuFn<() => d.Vec3f>) { let pipeline = this.#pipelineCache.get(distribution); if (!pipeline) { pipeline = this.#root @@ -103,8 +87,13 @@ export class Executor { .createComputePipeline({ compute: this.#dataMoreWorkersFunc }); this.#pipelineCache.set(distribution, pipeline); } + return pipeline; + } - pipeline.with(this.#bindGroup).dispatchWorkgroups(Math.ceil(this.#count / 64)); + async executeMoreWorkers(distribution: TgpuFn<() => d.Vec3f>): Promise { + this.getPipeline(distribution) + .with(this.#bindGroup) + .dispatchWorkgroups(Math.ceil(this.#count / 64)); return await this.#samplesBuffer.read(); } diff --git a/apps/typegpu-docs/src/examples/algorithms/probability/index.ts b/apps/typegpu-docs/src/examples/algorithms/probability/index.ts index 6c4c807686..51c7c77a78 100644 --- a/apps/typegpu-docs/src/examples/algorithms/probability/index.ts +++ b/apps/typegpu-docs/src/examples/algorithms/probability/index.ts @@ -89,16 +89,18 @@ export const controls = defineControls({ await replot(currentDistribution); }, }, + // this is the only place where some niche distributions are tested 'Test Resolution': import.meta.env.DEV && { onButtonClick() { c.distributions - .map((dist) => tgpu.resolve([executor.cachedPipeline(getPRNG(dist).prng)])) - .map((r) => root.device.createShaderModule({ code: r })); + .map((dist) => tgpu.resolve([executor.getPipeline(getPRNG(dist).prng)])) + .forEach((r) => root.device.createShaderModule({ code: r })); }, }, }); export function onCleanup() { + plotter.destroy(); root.destroy(); } diff --git a/apps/typegpu-docs/src/examples/algorithms/probability/plotter.ts b/apps/typegpu-docs/src/examples/algorithms/probability/plotter.ts index ceff9ff0ad..a1e7da29e4 100644 --- a/apps/typegpu-docs/src/examples/algorithms/probability/plotter.ts +++ b/apps/typegpu-docs/src/examples/algorithms/probability/plotter.ts @@ -43,6 +43,10 @@ export class Plotter { this.#core.renderer.transitionTime = 1; // it is number from [0, 1] indicating the state of the animation - 1 means current } + destroy() { + this.#core.stop(); + } + async plot(samples: d.v3f[], prng: PRNG, animate = false): Promise { let needNewBuffer = false; if (samples.length !== this.#count) { From 82c04bafa23eb056d6d486d57e91410321b6cd26 Mon Sep 17 00:00:00 2001 From: Szymon Szulc Date: Wed, 29 Apr 2026 13:57:44 +0200 Subject: [PATCH 2/2] review fix --- .../src/examples/algorithms/probability/index.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/typegpu-docs/src/examples/algorithms/probability/index.ts b/apps/typegpu-docs/src/examples/algorithms/probability/index.ts index 51c7c77a78..2bbc4f7fc5 100644 --- a/apps/typegpu-docs/src/examples/algorithms/probability/index.ts +++ b/apps/typegpu-docs/src/examples/algorithms/probability/index.ts @@ -92,9 +92,11 @@ export const controls = defineControls({ // this is the only place where some niche distributions are tested 'Test Resolution': import.meta.env.DEV && { onButtonClick() { - c.distributions - .map((dist) => tgpu.resolve([executor.getPipeline(getPRNG(dist).prng)])) - .forEach((r) => root.device.createShaderModule({ code: r })); + c.distributions.forEach((dist) => + root.device.createShaderModule({ + code: tgpu.resolve([executor.getPipeline(getPRNG(dist).prng)]), + }), + ); }, }, });