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

浏览器兼容性

另见