GPUSupportedFeatures
注意:此功能在 Web Workers 中可用。
GPUSupportedFeatures 介面是 WebGPU API 的一部分,它是一個 類似 Set 的物件,用於描述 GPUAdapter 支援的額外功能。
當前介面卡的 GPUSupportedFeatures 物件可以透過 GPUAdapter.features 屬性進行訪問——使用此屬性可以測試當前設定支援哪些功能。要建立啟用特定功能的 GPUDevice,你需要在 GPUAdapter.requestDevice() 描述符的 requiredFeatures 陣列中指定它。
你應該注意到,即使底層硬體支援某些功能,並非所有瀏覽器都將在支援 WebGPU 的所有瀏覽器中提供這些功能。這可能是由於底層系統、瀏覽器或介面卡的限制。例如:
- 底層系統可能無法保證以與特定瀏覽器相容的方式公開某個功能。
- 瀏覽器廠商可能尚未找到一種安全的方式來實現對該功能的支援,或者可能還沒有著手進行。
如果你希望在 WebGPU 應用程式中利用某個特定的附加功能,建議進行徹底的測試。
可用功能
WebGPU 中定義了以下附加功能。請注意,可用的功能集在不同的實現和物理裝置上可能會有所不同,並且會隨著時間的推移而改變。
| 功能名稱 | 描述 |
|---|---|
bgra8unorm-storage |
啟用後,允許 bgra8unorm-格式的 GPUTexture 使用 STORAGE_BINDING 用法。 |
clip-distances |
啟用後,允許在 WGSL 中使用 clip_distances。 |
core-features-and-limits |
啟用後,表示 GPUAdapter / GPUDevice 能夠使用所有核心 WebGPU 功能和限制,這使得應用程式可以支援具有 Direct 3D 12、Metal 和 Vulkan 平臺圖形 API 的裝置(請注意,Safari 僅支援 Metal)。這也稱為“核心”WebGPU。目前,所有介面卡都可用“核心”WebGPU,並且即使未請求,它也會自動在所有裝置上啟用。此功能將使介面卡和裝置能夠區分“核心”WebGPU 和“相容模式”,後者將提供對舊圖形 API(如 Direct 3D 11 和 OpenGL ES)的支援,但會犧牲效能和功能集。 |
depth-clip-control |
允許停用 深度剪裁。如果啟用了 depth-clip-control,則在建立 GPURenderPipeline 時,primitive 物件(包含在 createRenderPipeline() 或 createRenderPipelineAsync() 描述符中)將具有 unclippedDepth 屬性。primitive 描述了管道如何從其頂點輸入構建和柵格化圖元。將 unclipped-depth 設定為 true 可停用深度剪裁。 |
depth32float-stencil8 |
允許建立具有 depth32float-stencil8 格式的紋理。如果啟用了 depth32float-stencil8,則在建立 GPUTexture 時,可以在 createTexture() 描述符的 format 屬性中使用 depth32float-stencil8 值。 |
dual-source-blending |
啟用後,允許在 WGSL 中使用 dual_source_blending,它使用兩個畫素著色器輸出(@blend_src(0) 和 @blend_src(1))作為與 @location(0) 處的單個顏色附件混合操作的輸入。在 WebGPU 中,dual-source-blending 啟用了以下混合因子操作(在建立 createRenderPipeline() 和 createRenderPipelineAsync() 描述符的 dstFactor 和 srcFactor 屬性中指定):src1、one-minus-src1、src1-alpha 和 one-minus-src1-alpha。 |
float32-blendable |
啟用後,允許 混合 r32float、rg32float 和 rgba32float 格式 的 GPUTexture。 |
float32-filterable |
啟用後,允許 過濾 r32float、rg32float 和 rgba32float 格式 的 GPUTexture。 |
indirect-first-instance |
啟用後,允許在 GPURenderPassEncoder 和 GPURenderBundleEncoder 例項上可用的 drawIndirect() 和 drawIndexedIndirect() 方法的 indirectBuffer 屬性中使用非零的 firstInstance 值。 |
primitive-index |
啟用後,允許在 WGSL 中使用 primitive_index 著色器內建值。這個內建值在支援的硬體上為片段著色器提供每個圖元的索引,類似於現有的 vertex_index 和 instance_index 內建值。primitive_index 內建值對於高階圖形技術(如虛擬化幾何)很有用。 |
rg11b10ufloat-renderable |
啟用後,允許 rg11b10ufloat 格式 的 GPUTexture 使用 RENDER_ATTACHMENT 用法,以及它們的混合和多重取樣。 |
shader-f16 |
啟用後,允許在 WGSL 中使用半精度浮點型別 f16。 |
subgroups |
啟用後,允許在 WGSL 中使用 子組。子組支援 SIMD 級別的並行,允許工作組內的執行緒通訊和執行集體數學運算(如計算數字總和),並提供了一種高效的跨執行緒資料共享方法。請注意,subgroupMinSize 和 subgroupMaxSize 屬性在例如你有一個需要特定大小子組的硬編碼演算法時可能很有用。當請求同時具有 shader-f16 和 subgroups 功能的 GPUDevice 時,可以使用 f16 值與子組。 |
texture-compression-bc |
允許建立二維 BC 壓縮紋理。如果啟用了 texture-compression-bc,則在建立 GPUTexture 時,可以在 createTexture() 描述符的 format 屬性中使用以下值:bc1-rgba-unorm、bc1-rgba-unorm-srgb、bc2-rgba-unorm、bc2-rgba-unorm-srgb、bc3-rgba-unorm、bc3-rgba-unorm-srgb、bc4-r-unorm、bc4-r-snorm、bc5-rg-unorm、bc5-rg-snorm、bc6h-rgb-ufloat、bc6h-rgb-float、bc7-rgba-unorm 和 bc7-rgba-unorm-srgb。 |
texture-compression-bc-sliced-3d |
允許建立三維 BC 壓縮紋理。如果啟用了 texture-compression-bc-sliced-3d,則必須同時啟用 texture-compression-bc,因為它允許在前兩個維度中使用 BC 紋理格式(參見上面的 texture-compression-bc)。texture-compression-bc-sliced-3d 允許在第三個維度中使用相同的紋理。 |
texture-compression-astc |
允許建立二維 ASTC 壓縮紋理。如果啟用了 texture-compression-astc,則在建立 GPUTexture 時,可以在 createTexture() 描述符的 format 屬性中使用以下值:astc-4x4-unorm、astc-4x4-unorm-srgb、astc-5x4-unorm、astc-5x4-unorm-srgb、astc-5x5-unorm、astc-5x5-unorm-srgb、astc-6x5-unorm、astc-6x5-unorm-srgb、astc-6x6-unorm、astc-6x6-unorm-srgb、astc-8x5-unorm、astc-8x5-unorm-srgb、astc-8x6-unorm、astc-8x6-unorm-srgb、astc-8x8-unorm、astc-8x8-unorm-srgb、astc-10x5-unorm、astc-10x5-unorm-srgb、astc-10x6-unorm、astc-10x6-unorm-srgb、astc-10x8-unorm、astc-10x8-unorm-srgb、astc-10x10-unorm、astc-10x10-unorm-srgb、astc-12x10-unorm、astc-12x10-unorm-srgb 和 astc-12x12-unorm``astc-12x12-unorm-srgb。 |
texture-compression-astc-sliced-3d |
允許建立三維 ASTC 壓縮紋理。如果啟用了 texture-compression-astc-sliced-3d,則必須同時啟用 texture-compression-astc,因為它允許在前兩個維度中使用 ASTC 紋理格式(參見上面的 texture-compression-astc)。texture-compression-astc-sliced-3d 允許在第三個維度中使用相同的紋理。 |
texture-compression-etc2 |
允許建立二維 ETC2 壓縮紋理。如果啟用了 texture-compression-etc2,則在建立 GPUTexture 時,可以在 createTexture() 描述符的 format 屬性中使用以下值:etc2-rgb8unorm、etc2-rgb8unorm-srgb、etc2-rgb8a1unorm、etc2-rgb8a1unorm-srgb、etc2-rgba8unorm、etc2-rgba8unorm-srgb、eac-r11unorm、eac-r11snorm、eac-rg11unorm 和 eac-rg11snorm。 |
timestamp-query |
允許執行時間戳查詢,用於測量計算和渲染通道所需的時間。如果啟用了 timestamp-query,則在建立 GPUQuerySet 時,可以在 type 屬性中設定 timestamp 值(使用 createQuerySet() 描述符)。此外,在建立 GPUComputePassEncoder 和 GPURenderPassEncoder 時,可以在 beginComputePass() 和 beginRenderPass() 描述符中設定 timestampWrites 屬性。GPUQuerySet 物件將在 timestampWrites 屬性包含的物件中引用,以指定時間戳應寫入的位置。 |
例項屬性
以下屬性對所有隻讀的 類似 Set 的物件都可用(下面的連結指向 Set 全域性物件參考頁)。
size-
返回集合中的值的數量。
例項方法
以下方法對所有隻讀的 類似 Set 的物件都可用(下面的連結指向 Set 全域性物件參考頁)。
示例
js
async function init() {
if (!navigator.gpu) {
throw Error("WebGPU not supported.");
}
const adapter = await navigator.gpu.requestAdapter();
if (!adapter) {
throw Error("Couldn't request WebGPU adapter.");
}
const adapterFeatures = adapter.features;
// Return the size of the set
console.log(adapterFeatures.size);
// Check whether a feature is supported by the adapter
console.log(adapterFeatures.has("texture-compression-astc"));
// Iterate through all the set values using values()
const valueIterator = adapterFeatures.values();
for (const value of valueIterator) {
console.log(value);
}
// Iterate through all the set values using keys()
const keyIterator = adapterFeatures.keys();
for (const value of keyIterator) {
console.log(value);
}
// Iterate through all the set values using entries()
const entryIterator = adapterFeatures.entries();
for (const entry of entryIterator) {
console.log(entry[0]);
}
// Iterate through all the set values using forEach()
adapterFeatures.forEach((value) => {
console.log(value);
});
// …
}
規範
| 規範 |
|---|
| WebGPU # gpusupportedfeatures |
瀏覽器相容性
載入中…
另見
- WebGPU API
- 規範 功能索引