GPUDevice:createComputePipelineAsync() 方法

可用性有限

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

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

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

GPUDevice 接口的 createComputePipelineAsync() 方法返回一个 Promise,该 Promise 在流水线准备好进行计算而无需任何延迟后,会以一个 GPUComputePipeline 对象fulfilled,该对象可用于控制计算着色器阶段,并可在 GPUComputePassEncoder 中使用。

注意: 只要有可能,通常建议使用此方法而不是 GPUDevice.createComputePipeline(),因为它能防止 GPU 操作执行因流水线编译而被阻塞。

语法

js
createComputePipelineAsync(descriptor)

参数

描述符(descriptor)

请参阅 GPUDevice.createComputePipeline() 方法的描述符定义。

返回值

当创建的流水线准备好被使用而无需额外延迟时,返回的 Promise 会以一个 GPUComputePipeline 对象实例 fulfilled。

验证

如果流水线创建失败,导致流水线无效,则返回的 Promise 会以一个 GPUPipelineError reject。

  • 如果这是由于内部错误,则 GPUPipelineErrorreason"internal"
  • 如果这是由于验证错误,则 GPUPipelineErrorreason"validation"

如果以下任一条件不满足,则可能发生验证错误:

  • compute 属性中引用的 module 使用的工作组存储大小小于或等于 GPUDevicemaxComputeWorkgroupStorageSize 限制
  • module 每个工作组使用的计算调用次数小于或等于 GPUDevicemaxComputeInvocationsPerWorkgroup 限制
  • module 的工作组大小小于或等于 GPUDevice 相应的 maxComputeWorkgroupSizeXmaxComputeWorkgroupSizeYmaxComputeWorkgroupSizeZ 限制
  • 如果省略了 entryPoint 属性,则着色器代码包含一个用于浏览器作为默认入口点使用的计算着色器入口点函数。

示例

注意:WebGPU 示例 提供了更多示例。

基本示例

以下示例展示了一个过程:

js
async function init() {
  // …

  const bindGroupLayout = device.createBindGroupLayout({
    entries: [
      {
        binding: 0,
        visibility: GPUShaderStage.COMPUTE,
        buffer: {
          type: "storage",
        },
      },
    ],
  });

  const computePipeline = await device.createComputePipelineAsync({
    layout: device.createPipelineLayout({
      bindGroupLayouts: [bindGroupLayout],
    }),
    compute: {
      module: shaderModule,
      entryPoint: "main",
    },
  });

  // …
}

规范

规范
WebGPU
# dom-gpudevice-createcomputepipelineasync

浏览器兼容性

另见