WebGPU 新功能 (Chrome 153-154)

François Beaufort
François Beaufort

發布日期:2026 年 9 月 15 日

WGSL buffer_view 擴充功能

WGSL 語言擴充功能 buffer_view 可讓您將統一、儲存或工作群組變數的資料解讀為多種不同型別。從傳統的型別雙關用法,到將變數分割為多個邏輯變數,這項功能用途廣泛。

為確保安全及簡化實作程序,您只能使用下列內建函式存取重新解譯的型別:

  • bufferView<T>() 會將特定位元組偏移的記憶體解讀為指向型別 T 的指標。
  • bufferArrayView<T>() 會建立具有明確元素計數的繫結陣列指標。
  • bufferLength() 會傳回緩衝區的位元組大小。

您可以使用 navigator.gpu.wgslLanguageFeatures 在 JavaScript 中偵測這項語言擴充功能。建議在 WGSL 著色器程式碼頂端使用 requires 指令,透過 requires buffer_view 標示可能無法移植。請參閱以下 WGSL 範例和出貨意願

requires buffer_view;

//  +-------------------+-----------------------------+----------------------------+
//  | Byte 0 .. 3       | Byte 4 .. (4 + N - 1)       | Byte (4 + N) .. End        |
//  +-------------------+-----------------------------+----------------------------+
//  | indices_size (u32)| indices (array<u32, count>) | vertices (array<f32>)      |
//  +-------------------+-----------------------------+----------------------------+
@group(0) @binding(0) var<storage> indices_and_vertices : buffer;

fn unpack_and_process_geometry() {
  let indices_size = *bufferView<u32>(&indices_and_vertices, 0);

  let indices = bufferArrayView<array<u32>>(&indices_and_vertices, 4, indices_size);
  let vertices = bufferView<array<f32>>(&indices_and_vertices, indices_size + 4);
  ...
}

如要瞭解詳情,請參閱緩衝區檢視提案,並試用 Wireframe WebGPU 範例

WGSL swizzle_assignment 擴充功能

為回應最受歡迎的社群功能要求,WGSL 擴充功能 swizzle_assignment 可讓您直接寫入向量 swizzle,以便在單一陳述式中更新多個元件。這項功能大幅減少了樣板程式碼,提升著色器可讀性,同時讓 WGSL 與其他著色語言保持一致。

您可以使用 navigator.gpu.wgslLanguageFeatures 在 JavaScript 中偵測這項語言擴充功能。建議在 WGSL 著色器程式碼頂端使用 requires 指令,透過 requires swizzle_assignment 標示可能無法移植。請參閱以下 WGSL 範例和出貨意願

if (!navigator.gpu.wgslLanguageFeatures.has("swizzle_assignment")) {
  throw new Error(`WGSL swizzle assignment is not available`);
}

const adapter = await navigator.gpu.requestAdapter();
const device = await adapter.requestDevice();

const shaderModule = device.createShaderModule({ code: `
  requires swizzle_assignment;

  @vertex
  fn main(@builtin(vertex_index) vertex_index: u32) -> @builtin(position) vec4f {
    var position = vec4f(0.0, 0.0, 0.0, 1.0);
    position.xy = vec2f(10.0, 20.0); // Swizzle assignment

    return position;
  }`,
});

WGSL 擴充功能更新

texture-formats-tier1 語言擴充功能雖然在 WGSL 程式碼中受到支援,但先前在 JavaScript 中並未正確對應至 navigator.gpu.wgslLanguageFeatures (如 texture_formats_tier1)。這項對應問題現已解決。請參閱 Chromium CL 8159562

在 WGSL 中啟用 subgroup_size_control 時,系統現在會自動啟用 subgroups 語言擴充功能。詳情請參閱問題 dawn:545285663

黎明更新

新的 wgpu::FeatureName::BufferMapWriteExtendedUsages 實驗功能可讓您使用 wgpu::BufferUsage::MapWrite 搭配任何其他 wgpu::BufferUsage (wgpu::BufferUsage::MapRead 除外) 建立緩衝區,直接寫入可對應的緩衝區 (也可用於 GPU),藉此在 Vulkan 和 D3D12 後端中,消除暫存緩衝區和額外副本。詳情請參閱「Buffer Map Write Extended Usages」和問題 dawn:386255678

您現在可以在任何 webgpu.h 實作項目上使用 webgpu_cpp.h 繫結,不必再透過新的 webgpu_upstream_cpp_headers 繫結至 Dawn 的風味。請參閱 webgpu-headers 問題 #596

Dawn 現在需要 macOS 13.0 以上版本。請參閱 Dawn CL 334415

這僅涵蓋部分重點。詳情請參閱完整修訂清單

WebGPU 最新消息

WebGPU 最新消息」系列涵蓋的所有主題清單。

Chrome 153-154

Chrome 151-152

Chrome 149-150

Chrome 147-148

Chrome 146

Chrome 145

Chrome 144

Chrome 143

Chrome 142

Chrome 141

Chrome 140

Chrome 139

Chrome 138

Chrome 137

Chrome 136

Chrome 135

Chrome 134

Chrome 133

Chrome 132

Chrome 131

Chrome 130

Chrome 129

Chrome 128

Chrome 127

Chrome 126

Chrome 125

Chrome 124

Chrome 123

Chrome 122

Chrome 121

Chrome 120

Chrome 119

Chrome 118

Chrome 117

Chrome 116

Chrome 115

Chrome 114

Chrome 113