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

js
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ộ, GPUPipelineError sẽ có reason"internal".
  • Nếu nguyên nhân là lỗi kiểm tra hợp lệ, GPUPipelineError sẽ có reason"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ính compute sử dụng nhỏ hơn hoặc bằng giới hạn maxComputeWorkgroupStorageSize của GPUDevice.
  • module sử dụng số lượng compute invocation trên mỗi workgroup nhỏ hơn hoặc bằng maxComputeInvocationsPerWorkgroup của GPUDevice.
  • Kích thước workgroup của module nhỏ hơn hoặc bằng maxComputeWorkgroupSizeX, maxComputeWorkgroupSizeY, hoặc maxComputeWorkgroupSizeZ tương ứng của GPUDevice.
  • Nếu thuộc tính entryPoint bị 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:

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",
    },
  });

  // …
}

Đặc tả

Thông số kỹ thuật
WebGPU
# dom-gpudevice-createcomputepipelineasync

Tương thích trình duyệt

Xem thêm