Precision in WebGPU shaders
WebGL shaders allow the use of shader precision qualifiers such as lowp
and mediump
. WebGPU uses a different approach with explicit types such as f32
. Some devices support a lower-precision f16
type if they support the shader-f16
feature. To help make it easy to use the f16
type, Construct requests to use the shader-f16
feature where supported, and defines a type in WebGPU shaders named f16or32
, which is f16
when shader-f16
is supported, otherwise it is f32
.
Currently all of Construct's built-in inputs, outputs and library functions use the f32
type exclusively for broadest compatibility. However shaders can make use of the f16or32
type for their internal calculations, converting to f32
where necessary, to help improve shader performance, especially as f32
is high precision with a higher performance cost compared to lowp
or mediump
precision in GLSL.
Compatibility differences with WebGL shaders
Due to API differences between WebGL and WebGPU, the WebGL src/srcOrigin/dest uniforms use an inverted Y direction. This means instead of ranging from 0-1 for top-to-bottom, they range from 1-0.
Sometimes this does not have any impact on the effect. However in some cases it does, depending on the kinds of calculation done in the shader. When porting a GLSL shader to WGSL, you may need to emulate the inverted Y direction in WGSL to achieve the same effect. For example the Lens2 effect uses the following code pattern in WGSL to emulate the inverted Y direction:
// At start of shader: get normalized source co-ordinates
// and then invert Y direction to match WebGL
var tex : vec2<f32> = c3_srcToNorm(input.fragUV);
tex.y = 1.0 - tex.y;
// ... rest of effect ...
// At end of shader: invert Y direction again and then
// calculate background sampling position
p.y = 1.0 - p.y;
var output : FragmentOutput;
output.color = textureSample(textureBack, samplerBack, mix(c3Params.destStart, c3Params.destEnd, p));
If you are writing a new effect, consider writing the WebGPU shader first, and then if necessary applying the Y inversion in the WebGL shader instead. As WebGPU is the newer technology, in the long term the WebGL renderer may eventually be retired, in which case it is better to have a natural code style in the WGSL shader.