async function x()
{
if (!navigator.gpu) {
console.error("WebGPU is not available.");
throw new Error("WebGPU support is not available");
}
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
console.error("Failed to request Adapter.");
return;
}
let device = await adapter.requestDevice();
if (!device) {
console.error("Failed to request Device.");
return;
}
const wgpu_canvas = <HTMLCanvasElement>document.getElementById("wgpu");
const context = canvas.getContext('webgpu');
const canvasConfig = {
device: device,
format: navigator.gpu.getPreferredCanvasFormat(),
usage:
GPUTextureUsage.RENDER_ATTACHMENT,
alphaMode: 'opaque'
};
context.configure(canvasConfig);
}```
#can't get webgpu context work
39 messages · Page 1 of 1 (latest)
why I have that error ?
it's ts file , following that tutorial, can't find where is the problem ,and how he uses that without issues there ?
Looks like a typo
@woeful meadow what do you mean typo
async function wgpu_init()
{
const adapter = await navigator.gpu.requestAdapter
({
featureLevel: "compatibility",
});
const device = await adapter?.requestDevice();
const canvas = document.querySelector("wgpu") as HTMLCanvasElement;
const context = canvas.getContext("webgpu") as GPUCanvasContext;
const devicePixelRatio = window.devicePixelRatio;
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
context?.configure
({
device,
format: presentationFormat,
})
}```
any ideas what's up with this ? something happens in canvas.getContext
I took that code from webgpu samples , that works in my browser but I can't make it work in vscode ...
why why 🙁
why device errors
Looks fixed in the new code, you were using canvas in this code which wasn't defined (I assume you meant wgu_canvas)
yes .. I took a new code
it's named correctly
const canvas = document.querySelector("wgpu") as HTMLCanvasElement;
is that correct ?
const context = canvas.getContext("webgpu") as GPUCanvasContext;
Also querySelector takes a CSS selector so to query by ID with it you need a # prefix
Or just use getElementById
const canvas = document.getElementById("wgpu") as HTMLCanvasElement;
ok now ?
but device still fails ..
Whats the error message?
Oh you need to assert that its not null with the ! operator (postfix) iirc
I'd recommend learning a bit more about typescript 
which element I need to assert with ! there ?