GPUDevice: features 属性
注意:此功能在 Web Workers 中可用。
GPUDevice 接口的只读属性 features 返回一个 GPUSupportedFeatures 对象,该对象描述了设备支持的其他功能。该对象仅包含在创建设备期间(即调用 GPUAdapter.requestDevice() 时)请求的功能。
注意: 即使底层硬件支持某些功能,并非所有支持 WebGPU 的浏览器都会提供这些功能。有关更多详细信息,请参阅 GPUAdapter.features。
值
一个 GPUSupportedFeatures 对象实例。这是一个 类似 Set 的对象。
示例
在以下代码中,我们检查 GPUAdapter 是否可用 texture-compression-astc 功能。如果可用,我们将其添加到 requiredFeatures 数组中,并使用 GPUAdapter.requestDevice() 请求具有该功能要求的设备。
然后,我们将 GPUDevice.features 集合中的所有项目记录到控制台。此集合应该只包含一个项目 — texture-compression-astc — 因为在创建设备时仅请求了此功能。
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 requiredFeatures = [];
if (adapter.features.has("texture-compression-astc")) {
requiredFeatures.push("texture-compression-astc");
}
const device = await adapter.requestDevice({
requiredFeatures,
});
device.features.forEach((value) => {
console.log(value);
});
// …
}
规范
| 规范 |
|---|
| WebGPU # dom-gpudevice-features |
浏览器兼容性
加载中…