Skip to content
Draft
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
27 changes: 13 additions & 14 deletions apps/typegpu-docs/src/examples/algorithms/bitonic-sort/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import tgpu, { d, std } from 'typegpu';
import tgpu, { common, d, std } from 'typegpu';
import {
type BitonicSorter,
type BitonicSorterOptions,
createBitonicSorter,
decomposeWorkgroups,
} from '@typegpu/sort';
import { randf } from '@typegpu/noise';
import { fullScreenTriangle } from 'typegpu/common';
import { defineControls } from '../../common/defineControls.ts';

const maxBufferSize = await navigator.gpu.requestAdapter().then((adapter) => {
Expand All @@ -32,8 +31,6 @@ const querySet = hasTimestampQuery ? root.createQuerySet('timestamp', 2) : null;
const canvas = document.querySelector('canvas') as HTMLCanvasElement;
const context = root.configureContext({ canvas });

const presentationFormat = navigator.gpu.getPreferredCanvasFormat();

const maxSide = Math.floor(Math.sqrt(maxBufferSize / 4));
const minLog = 2; // log_2(4)
const maxLog = Math.floor(Math.log2(maxSide));
Expand Down Expand Up @@ -75,17 +72,11 @@ const state = {
const WORKGROUP_SIZE = 256;

const renderLayout = tgpu.bindGroupLayout({
data: {
storage: d.arrayOf(d.u32),
access: 'readonly',
},
data: { storage: d.arrayOf(d.u32), access: 'readonly' },
});

const initLayout = tgpu.bindGroupLayout({
data: {
storage: d.arrayOf(d.u32),
access: 'mutable',
},
data: { storage: d.arrayOf(d.u32), access: 'mutable' },
});

const initSeed = root.createUniform(d.f32, 0);
Expand Down Expand Up @@ -135,9 +126,8 @@ const initKernel = tgpu.computeFn({
});

const renderPipeline = root.createRenderPipeline({
vertex: fullScreenTriangle,
vertex: common.fullScreenTriangle,
fragment: fragmentFn,
targets: { format: presentationFormat },
});

const initPipeline = root.createComputePipeline({ compute: initKernel });
Expand Down Expand Up @@ -246,6 +236,14 @@ async function sort() {
hideOverlay();
}

const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
onResize() {
render();
},
});

// #region Example controls & Cleanup

const sortOrderKeys = Object.keys(sortOrders) as SortOrderKey[];
Expand Down Expand Up @@ -275,6 +273,7 @@ export function onCleanup() {
for (const s of Object.values(sorters)) {
s.destroy();
}
detachAutoResizer();
root.destroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,11 @@ applyGridSize(...GRID_SIZES[gridSizeKey]);
applyTrack(generateGridTrack(trackSeed, ...GRID_SIZES[gridSizeKey]));
startSimulation();

const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
});

// #region Example controls & Cleanup

export const controls = defineControls({
Expand Down Expand Up @@ -605,6 +610,7 @@ export const controls = defineControls({

export function onCleanup() {
cancelAnimationFrame(rafHandle);
detachAutoResizer();
root.destroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,8 +342,6 @@ function recreateResources() {
sourceIdx = 0;
}

clearCanvas();

function getTouchPosition(rect: DOMRect, touches: TouchList) {
if (touches.length === 2) {
return {
Expand All @@ -357,17 +355,16 @@ function getTouchPosition(rect: DOMRect, touches: TouchList) {
};
}

// #region Example controls & Cleanup

let resizeTimeout: ReturnType<typeof setTimeout>;
const resizeObserver = new ResizeObserver(() => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
onResize() {
recreateResources();
clearCanvas();
}, 100);
},
});
resizeObserver.observe(canvas);

// #region Example controls & Cleanup

const onMouseDown = (e: MouseEvent) => {
if (e.button !== 0 && e.button !== 2) {
Expand Down Expand Up @@ -475,8 +472,7 @@ export const controls = defineControls({
});

export function onCleanup() {
clearTimeout(resizeTimeout);
resizeObserver.disconnect();
detachAutoResizer();
root.destroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,16 @@ function reset() {
void runFloodAnimated(currentRunId);
}

reset();

// #region Example controls & Cleanup

let resizeTimeout: ReturnType<typeof setTimeout>;
const resizeObserver = new ResizeObserver(() => {
clearTimeout(resizeTimeout);
resizeTimeout = setTimeout(() => {
const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
onResize() {
recreateResources();
reset();
}, 100);
},
});
resizeObserver.observe(canvas);

// #region Example controls & Cleanup

export const controls = defineControls({
'Run Algorithm': {
Expand Down Expand Up @@ -295,8 +292,7 @@ export const controls = defineControls({
});

export function onCleanup() {
clearTimeout(resizeTimeout);
resizeObserver.disconnect();
detachAutoResizer();
root.destroy();
}

Expand Down
45 changes: 25 additions & 20 deletions apps/typegpu-docs/src/examples/geometry/circles/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { circle, circleVertexCount } from '@typegpu/geometry';
import tgpu, { d, std as s } from 'typegpu';
import tgpu, { common, d, std as s } from 'typegpu';

const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
const canvas = document.querySelector('canvas');
Expand Down Expand Up @@ -128,29 +128,34 @@ const pipeline = root.createRenderPipeline({
multisample: { count: multisample ? 4 : 1 },
});

setTimeout(() => {
pipeline
.with(uniformsBindGroup)
.withColorAttachment({
...(multisample
? {
view: msaaTextureView,
resolveTarget: context,
}
: { view: context }),
clearValue: [0, 0, 0, 0],
loadOp: 'clear',
storeOp: 'store',
})
.withPerformanceCallback((a, b) => {
console.log((Number(b - a) * 1e-6).toFixed(3), 'ms');
})
.draw(circleVertexCount(4), circleCount);
}, 100);
const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
onResize() {
pipeline
.with(uniformsBindGroup)
.withColorAttachment({
...(multisample
? {
view: msaaTextureView,
resolveTarget: context,
}
: { view: context }),
clearValue: [0, 0, 0, 0],
loadOp: 'clear',
storeOp: 'store',
})
.withPerformanceCallback((a, b) => {
console.log((Number(b - a) * 1e-6).toFixed(3), 'ms');
})
.draw(circleVertexCount(4), circleCount);
},
});

// #region Example controls & Cleanup

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
lineSegmentWireframeIndices,
startCapSlot,
} from '@typegpu/geometry';
import tgpu, { type ColorAttachment } from 'typegpu';
import tgpu, { common, type ColorAttachment } from 'typegpu';
import {
arrayOf,
builtin,
Expand Down Expand Up @@ -406,6 +406,8 @@ const runAnimationFrame = (timeMs: number) => {
};
runAnimationFrame(0);

const detachAutoResizer = common.attachAutoResizer({ root, canvas });

const fillOptions = {
none: 0,
solid: 1,
Expand Down Expand Up @@ -502,6 +504,7 @@ export const controls = defineControls({
});

export function onCleanup() {
detachAutoResizer();
root.destroy();
cancelAnimationFrame(frameId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ if (isIOS) {

videoFrameCallbackId = video.requestVideoFrameCallback(processVideoFrame);

const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
});

export const controls = defineControls({
'use extended characters': {
initial: false,
Expand Down Expand Up @@ -279,5 +284,6 @@ export function onCleanup() {
}
}

detachAutoResizer();
root.destroy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ async function processVideoFrame(_: number, metadata: VideoFrameCallbackMetadata
}
videoFrameCallbackId = video.requestVideoFrameCallback(processVideoFrame);

const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
});

// #region Example controls & Cleanup

export const controls = defineControls({
Expand Down Expand Up @@ -355,6 +360,7 @@ export function onCleanup() {
adapter.requestDevice = oldRequestDevice;
}

detachAutoResizer();
root.destroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,13 @@ function render() {

renderPipeline.withColorAttachment({ view: context }).draw(3);
}
render();
const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
onResize() {
render();
},
});

// #region Example controls & Cleanup

Expand Down Expand Up @@ -200,6 +206,7 @@ export const controls = defineControls({
});

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ function processVideoFrame(_: number, metadata: VideoFrameCallbackMetadata) {
}
videoFrameCallbackId = video.requestVideoFrameCallback(processVideoFrame);

const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
});

// #region Example controls & Cleanup

export const controls = defineControls({
Expand Down Expand Up @@ -160,6 +165,7 @@ export function onCleanup() {
track.stop();
}
}
detachAutoResizer();
root.destroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ function processVideoFrame(_: number, metadata: VideoFrameCallbackMetadata) {

videoFrameCallbackId = video.requestVideoFrameCallback(processVideoFrame);

const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
});

// #region Example controls & Cleanup

export const controls = defineControls({
Expand Down Expand Up @@ -152,6 +157,7 @@ export function onCleanup() {
track.stop();
}
}
detachAutoResizer();
root.destroy();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,17 @@ export const controls = defineControls({
},
});

const detachAutoResizer = common.attachAutoResizer({
root,
canvas,
onResize() {
render();
},
});

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

// #endregion

render();
Loading
Loading