GPUDevice:createComputePipelineAsync() 方法
注意:此功能在 Web Workers 中可用。
GPUDevice 介面的 createComputePipelineAsync() 方法返回一個 Promise,該 Promise 在流水線準備好進行計算而無需任何延遲後,會以一個 GPUComputePipeline 物件fulfilled,該物件可用於控制計算著色器階段,並可在 GPUComputePassEncoder 中使用。
注意: 只要有可能,通常建議使用此方法而不是 GPUDevice.createComputePipeline(),因為它能防止 GPU 操作執行因流水線編譯而被阻塞。
語法
js
createComputePipelineAsync(descriptor)
引數
描述符(descriptor)-
請參閱
GPUDevice.createComputePipeline()方法的描述符定義。
返回值
當建立的流水線準備好被使用而無需額外延遲時,返回的 Promise 會以一個 GPUComputePipeline 物件例項 fulfilled。
驗證
如果流水線建立失敗,導致流水線無效,則返回的 Promise 會以一個 GPUPipelineError reject。
- 如果這是由於內部錯誤,則
GPUPipelineError的reason為"internal"。 - 如果這是由於驗證錯誤,則
GPUPipelineError的reason為"validation"。
如果以下任一條件不滿足,則可能發生驗證錯誤:
compute屬性中引用的module使用的工作組儲存大小小於或等於GPUDevice的maxComputeWorkgroupStorageSize限制。module每個工作組使用的計算呼叫次數小於或等於GPUDevice的maxComputeInvocationsPerWorkgroup限制。module的工作組大小小於或等於GPUDevice相應的maxComputeWorkgroupSizeX、maxComputeWorkgroupSizeY或maxComputeWorkgroupSizeZ限制。- 如果省略了
entryPoint屬性,則著色器程式碼包含一個用於瀏覽器作為預設入口點使用的計算著色器入口點函式。
示例
注意:WebGPU 示例 提供了更多示例。
基本示例
以下示例展示了一個過程:
- 使用
GPUDevice.createBindGroupLayout()建立繫結組佈局。 - 將
bindGroupLayout輸入到GPUDevice.createPipelineLayout()中以建立GPUPipelineLayout。 - 在
createComputePipelineAsync()呼叫中立即使用該值建立GPUComputePipeline。
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 |
瀏覽器相容性
載入中…