Skip to content
Open
Show file tree
Hide file tree
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
25 changes: 7 additions & 18 deletions apps/typegpu-docs/src/examples/algorithms/probability/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,32 +79,21 @@ 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<d.v3f[]> {
getPipeline(distribution: TgpuFn<() => d.Vec3f>) {
let pipeline = this.#pipelineCache.get(distribution);
if (!pipeline) {
pipeline = this.#root
.with(this.#distributionSlot, distribution)
.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<d.v3f[]> {
this.getPipeline(distribution)
.with(this.#bindGroup)
.dispatchWorkgroups(Math.ceil(this.#count / 64));

return await this.#samplesBuffer.read();
}
Expand Down
10 changes: 7 additions & 3 deletions apps/typegpu-docs/src/examples/algorithms/probability/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,20 @@ 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 }));
c.distributions.forEach((dist) =>
root.device.createShaderModule({
code: tgpu.resolve([executor.getPipeline(getPRNG(dist).prng)]),
}),
);
},
},
});

export function onCleanup() {
plotter.destroy();
root.destroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
let needNewBuffer = false;
if (samples.length !== this.#count) {
Expand Down
Loading