GPUDevice: limits 属性

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

注意:此功能在 Web Workers 中可用。

GPUDevice 接口的只读属性 limits 返回一个 GPUSupportedLimits 对象,该对象描述了设备支持的限制。所有限制值都将包含在内,并且在创建设备时(即调用 GPUAdapter.requestDevice() 时)请求的限制将在这些值中得到反映。

注意: 即使底层硬件支持,并非所有限制都会按预期报告。有关更多详细信息,请参阅 GPUAdapter.limits

一个 GPUSupportedLimits 对象实例。

示例

在下面的代码中,我们查询 GPUAdapter.limits 属性的 maxBindGroups 值,看看它是否等于或大于 6。我们的理论示例应用程序理想情况下需要 6 个绑定组,因此如果返回值大于等于 6,我们会将最大限制设置为 6 并添加到 requiredLimits 对象中。

然后,我们通过将预期限制的值记录到控制台来检查该预期限制是否已设置在生成的设备上。

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 requiredLimits = {};

  // App ideally needs 6 bind groups, so we'll try to request what the app needs
  if (adapter.limits.maxBindGroups >= 6) {
    requiredLimits.maxBindGroups = 6;
  }

  const device = await adapter.requestDevice({
    requiredLimits,
  });

  console.log(device.limits.maxBindGroups);

  // …
}

规范

规范
WebGPU
# dom-gpudevice-limits

浏览器兼容性

另见