GPUDevice: phương thức createComputePipelineAsync()
Khả dụng hạn chế
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
Note: This feature is available in Web Workers.
Phương thức createComputePipelineAsync() của giao diện GPUDevice trả về một Promise sẽ được hoàn tất với một GPUComputePipeline, có thể điều khiển giai đoạn compute shader và được dùng trong GPUComputePassEncoder khi pipeline có thể dùng mà không gây chặn.
Note:
Nói chung nên dùng phương thức này thay cho GPUDevice.createComputePipeline() khi có thể, vì nó tránh chặn việc thực thi thao tác GPU trong lúc biên dịch pipeline.
Cú pháp
createComputePipelineAsync(descriptor)
Tham số
descriptor-
Xem định nghĩa descriptor cho phương thức
GPUDevice.createComputePipeline().
Giá trị trả về
Một Promise sẽ được hoàn tất với một thể hiện GPUComputePipeline khi pipeline được tạo đã sẵn sàng để dùng mà không cần chờ thêm.
Kiểm tra hợp lệ
Nếu việc tạo pipeline thất bại và pipeline kết quả trở nên không hợp lệ, promise trả về sẽ bị từ chối với một GPUPipelineError:
- Nếu nguyên nhân là lỗi nội bộ,
GPUPipelineErrorsẽ córeasonlà"internal". - Nếu nguyên nhân là lỗi kiểm tra hợp lệ,
GPUPipelineErrorsẽ córeasonlà"validation".
Lỗi kiểm tra hợp lệ có thể xảy ra nếu bất kỳ điều nào sau đây là sai:
- Kích thước bộ nhớ lưu trữ workgroup mà
moduleđược tham chiếu bên trong thuộc tínhcomputesử dụng nhỏ hơn hoặc bằng giới hạnmaxComputeWorkgroupStorageSizecủaGPUDevice. modulesử dụng số lượng compute invocation trên mỗi workgroup nhỏ hơn hoặc bằngmaxComputeInvocationsPerWorkgroupcủaGPUDevice.- Kích thước workgroup của
modulenhỏ hơn hoặc bằngmaxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY, hoặcmaxComputeWorkgroupSizeZtương ứng củaGPUDevice. - Nếu thuộc tính
entryPointbị bỏ qua, mã shader phải chứa đúng một hàm entry point compute shader để trình duyệt dùng làm entry point mặc định.
Ví dụ
Note: Các mẫu WebGPU có thêm nhiều ví dụ khác.
Ví dụ cơ bản
Ví dụ sau mô tả một quy trình:
- Tạo một bind group layout bằng
GPUDevice.createBindGroupLayout(). - Đưa
bindGroupLayoutvàoGPUDevice.createPipelineLayout()để tạo mộtGPUPipelineLayout. - Dùng ngay giá trị đó trong một lời gọi
createComputePipelineAsync()để tạo mộtGPUComputePipeline.
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",
},
});
// …
}
Đặc tả
| Thông số kỹ thuật |
|---|
| WebGPU> # dom-gpudevice-createcomputepipelineasync> |