WebGPU brings modern GPU capabilities to the web, enabling high-performance graphics and compute workloads that were previously impossible in browsers.
WebGPU vs WebGL
- Modern API: Based on Vulkan/Metal/D3D12
- Compute Shaders: General-purpose GPU computing
- Better Performance: Lower driver overhead
- Explicit Control: Manage GPU resources directly
Getting Started
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const canvas = document.querySelector("canvas");
const context = canvas.getContext("webgpu");
context.configure({
device,
format: navigator.gpu.getPreferredCanvasFormat(),
});
// Create shader module
const shaderModule = device.createShaderModule({
code: `
@vertex fn vs(@builtin(vertex_index) i: u32) -> @builtin(position) vec4f {
const pos = array(vec2f(0, 1), vec2f(-1, -1), vec2f(1, -1));
return vec4f(pos[i], 0, 1);
}
@fragment fn fs() -> @location(0) vec4f {
return vec4f(1, 0, 0, 1);
}
`
});
Use Cases
- 3D visualization and games
- AI/ML inference in browser
- Video processing
- Scientific computing
Comments (0)
Leave a Comment
No comments yet. Be the first to share your thoughts!