Goals
- Exposes capability of Metal, D3D12, and Vulkan
- Web-native API
- Modern Features
- Built with extensibility in mind
Features
- Compute Shaders
- Indirect drawing
- Render Bundles
- External textures (for more efficient video)
- More flexible Canvas integration
- Improved debugging features
- No global state!
Drawing a triangle
Initialization
Buffers
Shaders
- new shader syntax
wgsl
(wig · sel) - WebGPU Shading language
Pipelines
- Is like a webgl program
- Contains
- shape of the vertex attributes
- render target formats
- blending
Drawing
- draw time vs setup time
- all state initialization in webgpu happens during setup time
- this help webgpu to do optimizations without any assumptions
Core concepts
-
GPUAdapter
- Adapter are the GPU’s available to the device.
- Can be software! (SwiftShader)
- Only returns one at a time, but can request an adapter that meets certain criteria.
{ powerPreference: 'low-power'
- Gives basic description of the GPU
{ vendor: "nvidia", architecture: "turing" }
- Lists the features and limits available to the GPUDevice.
-
GPUDevice
- Primary interface for the API
- Creates resources like Textures, Buffers, Pipelines, etc.
- Has a GPUQueue for executing commands
- Roughly equivalent to a WebGLRenderingContext
Features
- Roughly equivalent to WEBGL’s extensions
- Typically things not supported on all implementations/systems
- “texture-compression-bc”
- “timestamp-query”
- Adapter list which ones are available
Limits
- Numeric limits of GPU capabilities
maxTextureDimension2D
maxBindGroups
maxVertexBuffers
- Each has a baseline that all WEBGPU implementations must support.
- Adaptor reports the actual system limits.
- Devices will only have access to the default limits unless otherwise specified when requesting the Device.
-
GPUCanvasContext
- Allows WebGPU to render to the canvas
- Must be configured after creation to associate it with a device.
- Multiple canvases can be configured to use the same device
- Can be reconfigured as needed
- Provides a texture to render into
-
Resource types
- All have immutable “shape” after creation, but the contents can be changed
- GPUBuffer
- Specifies size and usage
- Uniforms, Vertices, Indices, General data
- GPUTexture - Specifies 1D/2D/3D, size, mips, samples, format, usage. ```js const texture = device.createTexture({ size: { width: 64, height: 64 }, mipLevelCount: 4, format: 'rgba8unorm', usage: GPUTextureUsage.TEXTURE_BINDING, }); ``` - GPUTextureView - Subset of a texture for sampling, render targets. - Specifies usage as cube map, array texture, etc. ```js const textureView = texture.createView({ baseMipLevel: 1, mipLevelCount: 1, }); ``` - GPUSampler - Specifies Texture filtering/wrapping behaviour ```js const sampler = device.createSampler({ magFilter: "nearest", minFilter: "linear", mipmapFilter: "linear", addressModU: "repeat", addressModeV: "clamp-to-edge", })
-
Shading language - WGSL
-
Pipelines
- Comes in GPURenderPipeline and GPUComputePipeline flavors
- Links WGSL shaders (via GPUShaderModules)
- GPURenderPipeline sets majority of relevant state for rendering
- Immutable after creation
-
Queue
- Device has a default GPUQueue
- Used to submit commands for the GPU.
-
BindGroups
- Exposing resources to shaders
- Values from your program get into shaders via
- A binding
- vertex attribute
- The binding need to be declared in two places
- Binding are each associated with a group, and a binding number of your choice
- Values from your program get into shaders via
- Exposing resources to shaders
Performance
- Minimize the number of pipelines
- More pipelines more state switching less performance
- Create pipelines in advance
PipelineAsync
variant
- Use RenderBundles
- Render bundles are pre recorded, partial, reusable, render passes.
- They can contain most rendering commands
- Can be “replayed” as part of an actual render pass later on.
Native Debugging tools
- renderdocs
- pix
Resources
- WebGPU Samples - Written by members of the Chrome team
- Raw WebGPU - A great introductory tutorial
- All the cores, none of the canvas - Compute-focused introduction
- Efficiently rendering glTF models - Focuses on efficient WebGPU patterns
- WebGPU best practices - Some targeted best practice advice for specific APIs
- WebGPU Spec, WGSL Spec - For a bit of light reading
- Babylon.js, Three.js, - Libraries with WebGPU support**
Videos