Roots are responsible for resource allocation and management. Whether you’d like to wrap an existing WebGPU buffer
with a typed shell or create a brand new buffer, roots are the place to start.
You can create a root using the tgpu.init function.
It requests a GPU device with default requirements. An optional parameter
can be passed in with special requirements for the GPU device.
The Window.navigator read-only property returns a reference to the Navigator object, which has methods and properties about the application running the script.
The requestDevice() method of the GPUAdapter interface returns a Promise that fulfills with a GPUDevice object, which is the primary interface for communicating with the GPU.
The GPUDevice interface of the WebGPU API represents a logical GPU device. This is the main interface through which the majority of WebGPU functionality is accessed.
Available only in secure contexts.
The HTMLCanvasElement.getContext() method returns a drawing context on the canvas, or null if the context identifier is not supported, or the canvas has already been set to a different context mode.
The GPUCanvasContext interface of the WebGPU API represents the WebGPU rendering context of a element, returned via an HTMLCanvasElement.getContext() call with a contextType of "webgpu".
Available only in secure contexts.
The Window.navigator read-only property returns a reference to the Navigator object, which has methods and properties about the application running the script.
for displaying 8-bit depth, standard dynamic range
content on this system. Must only return
GPUTextureFormat
"rgba8unorm" or
GPUTextureFormat
"bgra8unorm".
The returned value can be passed as the
GPUCanvasConfiguration#format
to
GPUCanvasContext#configure
calls on a
GPUCanvasContext
to ensure the associated
canvas is able to display its contents efficiently.
Note: Canvases which are not displayed to the screen may or may not benefit from using this
format.
Resources independent from the device, such as bind group layouts, functions, variables, TypeGPU slots etc., are created using dedicated tgpu.* methods.
Resources requiring a device, such as bind groups, buffers, samplers, pipelines, etc., are created using root.create* methods.
Treat roots as their own separate universes, meaning resources created from the same root can interact with each other, while
resources created by seperate roots can have a hard time interacting. This usually means creating just one root at the start
of the program is the safest bet, but there are exceptions.
If you cannot control the lifetime of the GPU device you are to use for computing/rendering, but are instead given the device in a lifecycle hook (e.g., react-native-webgpu),
you can create a new root each time, as long as you recreate every resource as well.
It is common practice to pass a GPUDevice to classes or functions for them to allocate their required resources. At first glance, this poses a problem when trying to
incorporate TypeGPU, since we would need to pass a root around instead of a device for all functionality that wants to move towards a typed API. We can create a global mapping
between devices and roots to solve this.
You can copy and paste the utility below that implements a basic global cache for roots.