#can't get webgpu context work

39 messages · Page 1 of 1 (latest)

brittle wave
#
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);
}```
#

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 ?

woeful meadow
brittle wave
#

@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

woeful meadow
brittle wave
#

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;
woeful meadow
#

Also querySelector takes a CSS selector so to query by ID with it you need a # prefix

#

Or just use getElementById

brittle wave
#
  const canvas = document.getElementById("wgpu") as HTMLCanvasElement;
#

ok now ?

#

but device still fails ..

woeful meadow
#

Whats the error message?

brittle wave
woeful meadow
#

Oh you need to assert that its not null with the ! operator (postfix) iirc

#

I'd recommend learning a bit more about typescript forgderp2

brittle wave
#

which element I need to assert with ! there ?

woeful meadow
#

The device, it currently could be undefined if requestDevice failed (or requestAdapter)

#

But it looks like context.configure expects the device to not be undefined

#

(the type GPUDevice | undefined is not compatible with GPUDevice)

brittle wave
#

ok I will look into it , thanks !

#
  const device = await adapter?.requestDevice();
  if (!device) {
    console.log("device error");
  }
  else
    {
      console.log("device ok");
    }```
#

this says device is ok in console

#

hence , I don't think it's empty

#

but it still errors 🙂