发布时间:2025 年 6 月 3 日
Chrome 的 WebGPU API 实现包含仅用于开发和测试的功能。这些功能不在标准 WebGPU 规范的范围内,不得在生产环境中使用。
本文档详细介绍了如何启用 WebGPU 开发者功能,并提供了一个全面的列表。
前提条件
如需在 Chrome 中启用 WebGPU 开发者功能,请按以下步骤操作:
- 在
chrome://flags/#enable-webgpu-developer-features
中启用“WebGPU 开发者功能”标志。 - 重启 Chrome 浏览器。
停用时间戳查询量化
借助时间戳查询,WebGPU 应用能够在计算和渲染传递期间准确测量 GPU 命令的执行时间(精确到纳秒)。这些查询对于分析 GPU 工作负载性能和行为至关重要。如需了解详情,请参阅计算和渲染传递中的时间戳查询。
由于存在时间攻击问题,时间戳查询会以 100 微秒的分辨率进行量化,从而在准确性和安全性之间取得良好的平衡。启用“WebGPU 开发者功能”标志后,此量化会自动停用。
扩展适配器信息
为了更深入地了解所使用的适配器,GPUAdapterInfo 会公开以下属性:
device
属性(标准化)是供应商专用适配器标识符。description
属性(标准化)是一个直观易懂的字符串,用于提供适配器详细信息。driver
属性(非标准化)是一个直观易懂的字符串,用于描述驱动程序。backend
属性(非标准化)表示图形后端,例如"WebGPU"
、"D3D11"
、"D3D12"
、"metal"
、"vulkan"
、"openGL"
、"openGLES"
或"null"
。type
属性(非标准化)用于标识 GPU 类型:"discrete GPU"
、"integrated GPU"
、"CPU"
或"unknown"
。d3dShaderModel
属性(非标准化)用于指定支持的最大 D3D 着色器模型编号,例如,62 表示支持 HLSL SM 6.2。vkDriverVersion
属性(非标准化)是供应商指定的 Vulkan 驱动程序版本。powerPreference
属性(非标准化)为"low-power"
或"high-performance"
,具体取决于 GPURequestAdapterOptions 中的 GPUPowerPreference。
为了在开发应用期间预见分配大量内存时会出现的内存限制,GPUAdapterInfo 会公开 memoryHeaps
非标准化信息,例如适配器上可用内存堆的大小和类型。
const adapter = await navigator.gpu.requestAdapter();
for (const { size, properties } of adapter.info.memoryHeaps) {
console.log(size); // memory heap size in bytes
if (properties & GPUHeapProperty.DEVICE_LOCAL) { /* ... */ }
if (properties & GPUHeapProperty.HOST_VISIBLE) { /* ... */ }
if (properties & GPUHeapProperty.HOST_COHERENT) { /* ... */ }
if (properties & GPUHeapProperty.HOST_UNCACHED) { /* ... */ }
if (properties & GPUHeapProperty.HOST_CACHED) { /* ... */ }
}
着色器模块编译选项“严格的数学运算”
GPUShaderModuleDescriptor 包含一个 strictMath
非标准布尔值选项,用于在着色器模块编译期间启用或停用严格的数学精度。Metal 和 Direct3D 支持此选项。启用 strictMath
后,编译器会遵循精确的数学规则。反之,停用它可让编译器通过以下方式优化着色器:
- 忽略 NaN 和 Infinity 值的可能性。
- 将 -0 视为 +0。
- 将除法替换为更快的乘法(乘以倒数)。
- 根据结合性和分配性属性重新排列运算。
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const code = `
// Examines the bit pattern of the floating-point number to
// determine if it represents a NaN according to the IEEE 754 standard.
fn isNan(x : f32) -> bool {
bool ones_exp = (bitcast<u32>(x) & 0x7f8) == 0x7f8;
bool non_zero_sig = (bitcast<u32>(x) & 0x7ffff) != 0;
return ones_exp && non_zero_sig;
}
// ...
`;
// Enable strict math during shader compilation.
const shaderModule = device.createShaderModule({ code, strictMath: true });
使用零拷贝功能导入视频
通过 GPUExternalTexture isZeroCopy
非标准布尔值属性,您可以了解 GPU 是否直接访问了使用 importExternalTexture() 导入的视频,而无需中间复制。
const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();
const video = document.querySelector('video');
const externalTexture = device.importExternalTexture({ source: video });
if (externalTexture.isZeroCopy) {
console.log('Video frame was accessed directly by` the GPU');
}