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
- 规范 功能索引