WebGPU 2.4. Out-of-bounds access in shaders

Shaders can access physical resources either directly (for example, as a “uniform” GPUBufferBinding), or via texture units, which are fixed-function hardware blocks that handle texture coordinate conversions. Validation on the API side can only guarantee that all the inputs to the shader are provided and they have the correct usage and types. The host API side can not guarantee that the data is accessed within bounds if the texture units are not involved.

着色器可以直接访问物理资源(例如,作为”uniform”GPUBufferBinding),也可以通过纹理单元访问物理资源,纹理单元是处理纹理坐标转换的固定功能硬件块。API端的验证只能保证提供了着色器的所有输入,并且它们具有正确的用法和类型。如果涉及纹理单元,则宿主API端无法保证在边界内访问数据。

In order to prevent the shaders from accessing GPU memory an application doesn’t own, the WebGPU implementation may enable a special mode (called “robust buffer access”) in the driver that guarantees that the access is limited to buffer bounds.

为了防止着色器访问应用程序不拥有的GPU内存,WebGPU实现可以在驱动程序中启用一种特殊模式(称为”robust buffer access”),以确保访问仅限于缓冲区边界内。

Alternatively, an implementation may transform the shader code by inserting manual bounds checks. When this path is taken, the out-of-bound checks only apply to array indexing. They aren’t needed for plain field access of shader structures due to the minBindingSize validation on the host side.

或者,WebGPU实现可以通过手动插入边界检查来变换着色器代码。采用此方法时,越界检查仅适用于数组索引。由于主机端的minBindingSize验证,访问着色器结构的普通字段不需要它们。

If the shader attempts to load data outside of physical resource bounds, the implementation is allowed to:
1.return a value at a different location within the resource bounds
2.return a value vector of “(0, 0, 0, X)” with any “X”
3.partially discard the draw or dispatch call

如果着色器尝试加载物理资源边界之外的数据,则允许WebGPU实现:
1.在资源范围内的不同位置返回值
2.返回带有任意”X”的(0,0,0,X)向量
3.部分放弃the draw or dispatch call

If the shader attempts to write data outside of physical resource bounds, the implementation is allowed to:
1.write the value to a different location within the resource bounds
2.discard the write operation
3.partially discard the draw or dispatch call

如果着色器尝试写入物理资源边界之外的数据,则允许WebGPU实现:
1.将值写入资源边界内的其他位置
2.放弃写入操作
3.部分放弃the draw or dispatch call