In Deno 1.8, unstable/experimental support for WebGPU was added.
With WebGPU landing in Chrome Stable 113, I decided it was about time I start my foray into graphics programming. I do understand that no other browser has this feature enabled by default (yet).
Here is a basic example I tried getting up and running:
document.addEventListener("DOMContentLoaded", (): void => {
const _game: Game = new Game();
});
class Game {
constructor() {
const canvas: HTMLElement | null = document.getElementById("webgpu-canvas");
if (canvas === null) {
throw Error("Failed to get canvas.");
}
if (!(canvas instanceof HTMLCanvasElement)) {
throw Error("Canvas was unexpectedly not an HTMLCanvasElement.");
}
// dimensions
const aspectRatio: number = 16 / 9;
const width = 1000;
const height: number = width / aspectRatio;
// handle hdpi
const devicePixelRatio: number = window.devicePixelRatio;
canvas.width = canvas.width * devicePixelRatio;
canvas.height = canvas.height * devicePixelRatio;
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
if (!navigator.gpu) {
throw Error("WebGPU not supported on this browser.");
}
}
}
Here is the command I'm using to bundle my code: deno bundle --config deno.jsonc ui/static/script/run.ts bin/static/script/run.js
And here is the associated deno.jsonc
(make note - I'm targeting the browser):
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "dom.asynciterable", "deno.ns"]
},
"lint": {
"include": ["ui/static/script/"]
},
"fmt": {
"include": ["ui/static/script/"],
"lineWidth": 120
}
}
When I try bundling this basic example, I get this error:
Warning "deno bundle" is deprecated and will be removed in the future.
Use alternative bundlers like "deno_emit", "esbuild" or "rollup" instead.
Check file:///<private-path>/ui/static/script/run.ts
error: TS2339 [ERROR]: Property 'gpu' does not exist on type 'Navigator'.
if (!navigator.gpu) {
~~~
at file:///<private-path>/ui/static/script/run.ts:27:20
I've tried a couple things:
- Adding
--unstable
afterdeno bundle
- (Yes - I'm aware
deno bundle
may be deprecated soon. The Deno community did not like this decision, and it seems like it is still up for debate.)
- (Yes - I'm aware
- Adding
"deno.unstable"
to thedeno.jsonc
field#/compilerOptions/lib
Both of which seem to have zero affect.
How do I get access to WebGPU when developing Typescript with Deno?