WebGPU 開發人員功能

François Beaufort
François Beaufort

發布日期:2025 年 6 月 3 日

Chrome 的 WebGPU API 實作項目包含僅供開發和測試的功能。這些功能不屬於標準 WebGPU 規格。請勿在正式環境中使用這些功能。

本文詳細說明如何啟用 WebGPU 開發人員功能,並提供完整清單。

修課條件

如要在 Chrome 中啟用 WebGPU 開發人員功能,請按照下列步驟操作:

  1. chrome://flags/#enable-webgpu-developer-features 啟用「WebGPU 開發人員功能」旗標
  2. 重新啟動 Chrome 瀏覽器。

停用時間戳記查詢量化

時間戳記查詢可讓 WebGPU 應用程式在運算和算繪傳遞期間,準確測量 (精確度達奈秒單位) GPU 指令的執行時間。這些查詢對於分析 GPU 工作負載效能和行為至關重要。詳情請參閱「在運算和算繪階段中查詢時間戳記」。

基於時間攻擊考量,時間戳記查詢會以 100 微秒的解析度量化,在精確度和安全性之間取得平衡。啟用「WebGPU 開發人員功能」旗標後,系統會自動停用這項量化功能。

擴充轉接程式資訊

如要深入瞭解使用的轉接程式,GPUAdapterInfo 會公開下列屬性:

  • device 屬性 (標準化) 是供應商專屬的轉接程式 ID。
  • description 屬性 (標準化) 是使用者可判讀的字串,提供轉接器詳細資料。
  • driver 屬性 (非標準化) 是使用者可理解的字串,用於說明司機。
  • backend 屬性 (非標準化) 會指出圖形後端,例如 "WebGPU""D3D11""D3D12""metal""vulkan""openGL""openGLES""null"
  • type 屬性 (非標準化) 會識別 GPU 類型:"discrete GPU""integrated GPU""CPU""unknown"
  • d3dShaderModel 屬性 (非標準化) 會指定支援的 D3D Shader Model 號碼上限,例如 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');
}