GPUSupportedFeatures

可用性有限

此特性不是基線特性,因為它在一些最廣泛使用的瀏覽器中不起作用。

安全上下文: 此功能僅在安全上下文(HTTPS)中可用,且支援此功能的瀏覽器數量有限。

注意:此功能在 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() 描述符的 dstFactorsrcFactor 屬性中指定):src1one-minus-src1src1-alphaone-minus-src1-alpha
float32-blendable 啟用後,允許 混合 r32floatrg32floatrgba32float 格式GPUTexture
float32-filterable 啟用後,允許 過濾 r32floatrg32floatrgba32float 格式GPUTexture
indirect-first-instance 啟用後,允許在 GPURenderPassEncoderGPURenderBundleEncoder 例項上可用的 drawIndirect()drawIndexedIndirect() 方法的 indirectBuffer 屬性中使用非零的 firstInstance 值。
primitive-index 啟用後,允許在 WGSL 中使用 primitive_index 著色器內建值。這個內建值在支援的硬體上為片段著色器提供每個圖元的索引,類似於現有的 vertex_indexinstance_index 內建值。primitive_index 內建值對於高階圖形技術(如虛擬化幾何)很有用。
rg11b10ufloat-renderable 啟用後,允許 rg11b10ufloat 格式GPUTexture 使用 RENDER_ATTACHMENT 用法,以及它們的混合和多重取樣。
shader-f16 啟用後,允許在 WGSL 中使用半精度浮點型別 f16
subgroups 啟用後,允許在 WGSL 中使用 子組。子組支援 SIMD 級別的並行,允許工作組內的執行緒通訊和執行集體數學運算(如計算數字總和),並提供了一種高效的跨執行緒資料共享方法。請注意,subgroupMinSizesubgroupMaxSize 屬性在例如你有一個需要特定大小子組的硬編碼演算法時可能很有用。當請求同時具有 shader-f16subgroups 功能的 GPUDevice 時,可以使用 f16 值與子組。
texture-compression-bc 允許建立二維 BC 壓縮紋理。如果啟用了 texture-compression-bc,則在建立 GPUTexture 時,可以在 createTexture() 描述符的 format 屬性中使用以下值:bc1-rgba-unormbc1-rgba-unorm-srgbbc2-rgba-unormbc2-rgba-unorm-srgbbc3-rgba-unormbc3-rgba-unorm-srgbbc4-r-unormbc4-r-snormbc5-rg-unormbc5-rg-snormbc6h-rgb-ufloatbc6h-rgb-floatbc7-rgba-unormbc7-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-unormastc-4x4-unorm-srgbastc-5x4-unormastc-5x4-unorm-srgbastc-5x5-unormastc-5x5-unorm-srgbastc-6x5-unormastc-6x5-unorm-srgbastc-6x6-unormastc-6x6-unorm-srgbastc-8x5-unormastc-8x5-unorm-srgbastc-8x6-unormastc-8x6-unorm-srgbastc-8x8-unormastc-8x8-unorm-srgbastc-10x5-unormastc-10x5-unorm-srgbastc-10x6-unormastc-10x6-unorm-srgbastc-10x8-unormastc-10x8-unorm-srgbastc-10x10-unormastc-10x10-unorm-srgbastc-12x10-unormastc-12x10-unorm-srgbastc-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-rgb8unormetc2-rgb8unorm-srgbetc2-rgb8a1unormetc2-rgb8a1unorm-srgbetc2-rgba8unormetc2-rgba8unorm-srgbeac-r11unormeac-r11snormeac-rg11unormeac-rg11snorm
timestamp-query 允許執行時間戳查詢,用於測量計算和渲染通道所需的時間。如果啟用了 timestamp-query,則在建立 GPUQuerySet 時,可以在 type 屬性中設定 timestamp 值(使用 createQuerySet() 描述符)。此外,在建立 GPUComputePassEncoderGPURenderPassEncoder 時,可以在 beginComputePass()beginRenderPass() 描述符中設定 timestampWrites 屬性。GPUQuerySet 物件將在 timestampWrites 屬性包含的物件中引用,以指定時間戳應寫入的位置。

例項屬性

以下屬性對所有隻讀的 類似 Set 的物件都可用(下面的連結指向 Set 全域性物件參考頁)。

size

返回集合中的值的數量。

例項方法

以下方法對所有隻讀的 類似 Set 的物件都可用(下面的連結指向 Set 全域性物件參考頁)。

has()

返回一個布林值,斷言集合中是否存在具有給定值的元素。

values()

返回一個新的迭代器物件,該物件按照插入順序產生集合中每個元素的

keys()

values() 的別名。

entries()

返回一個新的迭代器物件,該物件包含集合中每個元素的[value, value] 陣列,按照插入順序排列。

forEach()

按照插入順序,對集合中的每個值呼叫一次提供的回撥函式。

示例

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

瀏覽器相容性

另見