Skip to content

Commit

Permalink
Update samples to work with TypeScript strict mode (part 1) (#278)
Browse files Browse the repository at this point in the history
* chore: adding types

* suggestions

* chore: strict false, temporary

---------

Co-authored-by: Kai Ninomiya <[email protected]>
  • Loading branch information
nash1111 and kainino0x authored Jul 20, 2023
1 parent 9f6d860 commit 5b0d24c
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 30 deletions.
13 changes: 12 additions & 1 deletion src/components/SampleLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ const SampleLayout: React.FunctionComponent<
};
try {
const canvas = canvasRef.current;
if (!canvas) {
throw new Error('The canvas is not available');
}
const p = props.init({
canvas,
pageState,
Expand Down Expand Up @@ -190,7 +193,9 @@ const SampleLayout: React.FunctionComponent<
<p>{props.description}</p>
{error ? (
<>
<p>Is WebGPU Enabled?</p>
<p>
Something went wrong. Do your browser and device support WebGPU?
</p>
<p>{`${error}`}</p>
</>
) : null}
Expand Down Expand Up @@ -253,3 +258,9 @@ export const makeSample: (
) => JSX.Element = (props) => {
return <SampleLayout {...props} />;
};

export function assert(condition: unknown, msg?: string): asserts condition {
if (!condition) {
throw new Error(msg);
}
}
6 changes: 5 additions & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { pages } from './samples/[slug]';

const title = 'WebGPU Samples';

type PageType = {
[key: string]: React.ComponentType & { render: { preload: () => void } };
};

const MainLayout: React.FunctionComponent<AppProps> = ({
Component,
pageProps,
Expand Down Expand Up @@ -71,7 +75,7 @@ const MainLayout: React.FunctionComponent<AppProps> = ({
key={slug}
className={className}
onMouseOver={() => {
pages[slug].render.preload();
(pages as PageType)[slug].render.preload();
}}
>
<Link
Expand Down
12 changes: 10 additions & 2 deletions src/pages/samples/[slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ type Props = {
slug: string;
};

export const pages = {
type PageComponentType = {
[key: string]: React.ComponentType;
};

export const pages: PageComponentType = {
helloTriangle: dynamic(() => import('../../sample/helloTriangle/main')),
helloTriangleMSAA: dynamic(
() => import('../../sample/helloTriangleMSAA/main')
Expand Down Expand Up @@ -59,9 +63,13 @@ export const getStaticPaths: GetStaticPaths<PathParams> = async () => {
export const getStaticProps: GetStaticProps<Props, PathParams> = async ({
params,
}) => {
if (!params) {
return { notFound: true };
}

return {
props: {
...params,
slug: params.slug,
},
};
};
Expand Down
47 changes: 28 additions & 19 deletions src/sample/animometer/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { makeSample, SampleInit } from '../../components/SampleLayout';
import { assert, makeSample, SampleInit } from '../../components/SampleLayout';

import animometerWGSL from './animometer.wgsl';

const init: SampleInit = async ({ canvas, pageState, gui }) => {
const adapter = await navigator.gpu.requestAdapter();
assert(adapter, 'requestAdapter returned null');
const device = await adapter.requestDevice();

if (!pageState.active) return;
Expand All @@ -17,7 +18,11 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {

const perfDisplay = document.createElement('pre');
perfDisplayContainer.appendChild(perfDisplay);
canvas.parentNode.appendChild(perfDisplayContainer);
if (canvas.parentNode) {
canvas.parentNode.appendChild(perfDisplayContainer);
} else {
console.error('canvas.parentNode is null');
}

const params = new URLSearchParams(window.location.search);
const settings = {
Expand Down Expand Up @@ -272,16 +277,16 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
}
}

let startTime = undefined;
let startTime: number | undefined = undefined;
const uniformTime = new Float32Array([0]);

const renderPassDescriptor: GPURenderPassDescriptor = {
const renderPassDescriptor = {
colorAttachments: [
{
view: undefined, // Assigned later
view: undefined as any, // Assigned later

Check warning on line 286 in src/sample/animometer/main.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Unexpected any. Specify a different type
clearValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 },
loadOp: 'clear',
storeOp: 'store',
loadOp: 'clear' as const,
storeOp: 'store' as const,
},
],
};
Expand All @@ -292,7 +297,7 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
recordRenderPass(renderBundleEncoder);
const renderBundle = renderBundleEncoder.finish();

return function doDraw(timestamp) {
return function doDraw(timestamp: number) {
if (startTime === undefined) {
startTime = timestamp;
}
Expand Down Expand Up @@ -322,19 +327,23 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
const updateSettings = () => {
doDraw = configure();
};
gui
.add(settings, 'numTriangles', 0, 200000)
.step(1)
.onFinishChange(updateSettings);
gui.add(settings, 'renderBundles');
gui.add(settings, 'dynamicOffsets');

let previousFrameTimestamp = undefined;
let jsTimeAvg = undefined;
let frameTimeAvg = undefined;
if (gui === undefined) {
console.error('GUI not initialized');
} else {
gui
.add(settings, 'numTriangles', 0, 200000)
.step(1)
.onFinishChange(updateSettings);
gui.add(settings, 'renderBundles');
gui.add(settings, 'dynamicOffsets');
}

let previousFrameTimestamp: number | undefined = undefined;
let jsTimeAvg: number | undefined = undefined;
let frameTimeAvg: number | undefined = undefined;
let updateDisplay = true;

function frame(timestamp) {
function frame(timestamp: number) {
// Sample is no longer the active page.
if (!pageState.active) return;

Expand Down
18 changes: 12 additions & 6 deletions src/sample/computeBoids/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { makeSample, SampleInit } from '../../components/SampleLayout';
import { assert, makeSample, SampleInit } from '../../components/SampleLayout';

import spriteWGSL from './sprite.wgsl';
import updateSpritesWGSL from './updateSprites.wgsl';

const init: SampleInit = async ({ canvas, pageState, gui }) => {
const adapter = await navigator.gpu.requestAdapter();
assert(adapter, 'requestAdapter returned null');
const device = await adapter.requestDevice();

if (!pageState.active) return;
Expand Down Expand Up @@ -85,13 +86,13 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {
},
});

const renderPassDescriptor: GPURenderPassDescriptor = {
const renderPassDescriptor = {
colorAttachments: [
{
view: undefined, // Assigned later
view: undefined as any, // Assigned later

Check warning on line 92 in src/sample/computeBoids/main.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Unexpected any. Specify a different type
clearValue: { r: 0.0, g: 0.0, b: 0.0, a: 1.0 },
loadOp: 'clear',
storeOp: 'store',
loadOp: 'clear' as const,
storeOp: 'store' as const,
},
],
};
Expand Down Expand Up @@ -144,7 +145,12 @@ const init: SampleInit = async ({ canvas, pageState, gui }) => {

updateSimParams();
Object.keys(simParams).forEach((k) => {
gui.add(simParams, k).onFinishChange(updateSimParams);
const key = k as keyof typeof simParams;
if (gui === undefined) {
console.error('GUI not initialized');
} else {
gui.add(simParams, key).onFinishChange(updateSimParams);
}
});

const numParticles = 1500;
Expand Down
3 changes: 2 additions & 1 deletion src/sample/resizeCanvas/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeSample, SampleInit } from '../../components/SampleLayout';
import { assert, makeSample, SampleInit } from '../../components/SampleLayout';

import triangleVertWGSL from '../../shaders/triangle.vert.wgsl';
import redFragWGSL from '../../shaders/red.frag.wgsl';
Expand All @@ -7,6 +7,7 @@ import styles from './animatedCanvasSize.module.css';

const init: SampleInit = async ({ canvas, pageState }) => {
const adapter = await navigator.gpu.requestAdapter();
assert(adapter, 'requestAdapter returned null');
const device = await adapter.requestDevice();

if (!pageState.active) return;
Expand Down
3 changes: 3 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@ declare module '*.wgsl' {
const shader: string;
export default shader;
}

declare module 'stats-js';
declare module 'stanford-dragon/4';

0 comments on commit 5b0d24c

Please sign in to comment.