GPUComputePassEncoder: dispatchWorkgroupsIndirect() method

Limited availability

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 dispatchWorkgroupsIndirect() của giao diện GPUComputePassEncoder gửi đi một lưới workgroup, được định nghĩa bởi các tham số của GPUBuffer, để thực hiện công việc do GPUComputePipeline hiện tại đảm nhiệm (tức là được đặt thông qua GPUComputePassEncoder.setPipeline()).

Cú pháp

js
dispatchWorkgroupsIndirect(indirectBuffer, indirectOffset)

Tham số

indirectBuffer

Một GPUBuffer chứa các chiều X, Y và Z của lưới workgroup cần gửi. Buffer phải chứa một khối liền kề gồm ba giá trị số nguyên không dấu 32-bit đại diện cho các chiều (tổng cộng 12 byte), theo thứ tự giống như các đối số của GPUComputePassEncoder.dispatchWorkgroups(). Ví dụ:

js
const uint32 = new Uint32Array(3);
uint32[0] = 25; // The X value
uint32[1] = 1; // The Y value
uint32[2] = 1; // The Z value

// Write values into a GPUBuffer
device.queue.writeBuffer(buffer, 0, uint32, 0, uint32.length);
indirectOffset

Khoảng cách tính bằng byte vào trong indirectBuffer nơi dữ liệu chiều bắt đầu.

Note: Các giá trị chiều X, Y và Z truyền vào GPUComputePassEncoder.dispatchWorkgroups()dispatchWorkgroupsIndirect() là số lượng workgroup cần gửi cho mỗi chiều, không phải số lần gọi shader cho mỗi chiều. Điều này khớp với hành vi của các GPU API native hiện đại, nhưng khác với hành vi của OpenCL. Điều này có nghĩa là nếu GPUShaderModule định nghĩa một entry point với @workgroup_size(4, 4), và công việc được gửi bằng dispatchWorkgroupsIndirect(indirectBuffer); với indirectBuffer chỉ định chiều X và Y là 8 và 8, thì entry point sẽ được gọi tổng cộng 1024 lần. 4 * 4 * 8 * 8 = 1024.

Giá trị trả về

Không có (Undefined).

Kiểm tra hợp lệ

Các tiêu chí sau phải được đáp ứng khi gọi dispatchWorkgroupsIndirect(), nếu không GPUValidationError sẽ được tạo ra và GPUComputePassEncoder sẽ trở nên không hợp lệ:

  • GPUBuffer.usage của indirectBuffer có chứa cờ GPUBufferUsage.INDIRECT.
  • indirectOffset cộng với tổng kích thước được chỉ định bởi các chiều X, YZ phải nhỏ hơn hoặc bằng GPUBuffer.size của indirectBuffer.
  • indirectOffset là bội số của 4.

Ví dụ

js
// Set global buffer size
const BUFFER_SIZE = 1000;

// Compute shader; note workgroup size of 64
const shader = `
@group(0) @binding(0)
var<storage, read_write> output: array<f32>;

@compute @workgroup_size(64)

...

`;

// …

// Create GPUCommandEncoder to encode commands to issue to the GPU
const commandEncoder = device.createCommandEncoder();

// Initiate compute pass
const passEncoder = commandEncoder.beginComputePass();

// Issue commands
passEncoder.setPipeline(computePipeline);
passEncoder.setBindGroup(0, bindGroup);

const uint32 = new Uint32Array(3);
// Note workgroupCountX is set based on the global buffer size and the shader workgroup count.
uint32[0] = Math.ceil(BUFFER_SIZE / 64);
uint32[1] = 1;
uint32[2] = 1;

const workgroupDimensions = device.createBuffer({
  size: 12,
  usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.INDIRECT,
});
device.queue.writeBuffer(workgroupDimensions, 0, uint32, 0, uint32.length);

passEncoder.dispatchWorkgroupsIndirect(workgroupDimensions, 0);

// End the render pass
passEncoder.end();

// Copy output buffer to staging buffer
commandEncoder.copyBufferToBuffer(
  output,
  0, // Source offset
  stagingBuffer,
  0, // Destination offset
  BUFFER_SIZE,
);

// End frame by passing array of command buffers to command queue for execution
device.queue.submit([commandEncoder.finish()]);

// …

Thông số kỹ thuật

Specification
WebGPU
# dom-gpucomputepassencoder-dispatchworkgroupsindirect

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

Xem thêm