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 |
浏览器兼容性
加载中…