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

瀏覽器相容性

另見