
Run WebGPU on Older Devices: Understanding Compatibility Mode
WebGPU compatibility mode is designed to allow your applications to run on a wider range of devices, especially older ones, by adhering to certain limitations. This article will guide you through the specifics of compatibility mode, its restrictions, and how to adapt your code to make the most of it.
What is WebGPU Compatibility Mode and Why Use It?
WebGPU compatibility mode enables your WebGPU applications to function on devices that might not fully support the core WebGPU specifications.
- Expanded Reach: By developing with compatibility mode in mind, you can target a broader audience, including users with older hardware.
- Nearly Universal WebGL2 Conversion: Most WebGL2 programs can be converted to run in this mode.
To enable compatibility mode in Chrome Canary (version 136.0.7063.0 or newer), activate the “enable-unsafe-webgpu” flag in chrome://flags/#enable-unsafe-webgpu
.
Adhering to compatibility mode limits ensures your app is also a valid "core" WebGPU application
Major Restrictions in Compatibility Mode
One of the primary limitations is the restricted use of storage buffers in vertex shaders.
- Limited Storage Buffers: Approximately 45% of older devices do not support storage buffers in vertex shaders.
If you're using storage buffers for vertex data, consider alternative solutions like vertex buffers, which are universally supported.
Medium-Level Restrictions: Texture Management
Compatibility mode imposes constraints on texture usage, specifically in how textures are viewed and manipulated.
Single ViewDimension Requirement
In standard WebGPU, a texture can have multiple view dimensions. For example:
Compatibility mode restricts you to a single viewDimension
per texture. A 2D texture with one layer defaults to a '2d' view, while multiple layers default to a '2d-array' view. For cubemaps, specify the textureBindingViewDimension
when creating the texture:
Attempting to use a different view type will result in a validation error. For example:
No Layer Subsets in BindGroups
Core WebGPU allows selecting subsets of texture layers, but compatibility mode restricts this:
These restrictions, while present, rarely impact most programs directly.
Generating Mipmaps in Compatibility Mode: A Detailed Example
Mipmap generation, a common use case, requires adaptation due to the aforementioned restrictions. The standard approach of using different view dimensions for each layer won’t work. Instead, you need to work with textures using their original view dimension and select layers within the shader.
Adapting the WGSL Shader
Here's the modified WGSL code that supports different texture types:
Key points:
- Multiple Fragment Shaders: The code uses different fragment shaders for 2D, 2D-array, cube, and cube-array textures.
- Instance Indexing:
@builtin(instance_index)
passes the layer index to the shader. - Cubemap Conversion: The cubemap code converts UV coordinates into a 3D cubemap coordinate, ensuring compatibility.
JavaScript Adjustments
In your JavaScript code, you'll need to:
- Determine Texture View Dimension: If the user doesn't provide a
textureBindingViewDimension
, guess it based on the texture’s properties. - Select the Appropriate Fragment Shader: Based on the
textureBindingViewDimension
.
Alter the generateMips function
Conclusion
WebGPU compatibility mode offers a pathway to support a wider array of devices, particularly older ones. By understanding and adapting to its restrictions, like those concerning storage buffers and texture management, you can ensure your WebGPU applications reach a broader audience without sacrificing functionality.