GPURenderBundleEncoder: drawIndirect() method
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The drawIndirect() method of the
GPURenderBundleEncoder interface draws primitives using parameters read from a GPUBuffer.
Note: This method is functionally identical to its equivalent on GPURenderPassEncoder — drawIndirect().
Syntax
js
drawIndirect(indirectBuffer, indirectOffset)
Parameters
indirectBuffer-
A
GPUBuffercontaining thevertexCount,instanceCount,firstVertex, andfirstInstancevalues needed to carry out the drawing operation. The buffer must contain a tightly packed block of four 32-bit unsigned integer values representing the values (16 bytes total), given in the same order as the arguments forGPURenderBundleEncoder.draw(). So for example:js
const uint32 = new Uint32Array(4); uint32[0] = 3; // The vertexCount value uint32[1] = 1; // The instanceCount value uint32[2] = 0; // The firstVertex value uint32[3] = 0; // The firstInstance value // Write values into a GPUBuffer device.queue.writeBuffer(buffer, 0, uint32, 0, uint32.length); indirectOffset-
The offset, in bytes, into
indirectBufferwhere the value data begins.
Return value
None (Undefined).
Validation
The following criteria must be met when calling drawIndirect(), otherwise a GPUValidationError is generated and the GPURenderBundleEncoder becomes invalid:
indirectBuffer'sGPUBuffer.usagecontains theGPUBufferUsage.INDIRECTflag.indirectOffset+ the total size specified by the value parameters in theindirectBufferis less than or equal to theindirectBuffer'sGPUBuffer.size.indirectOffsetis a multiple of 4.
Examples
js
// ...
// Create GPURenderBundleEncoder
const bundleEncoder = device.createRenderBundleEncoder(descriptor);
// Set pipeline and vertex buffer
bundleEncoder.setPipeline(renderPipeline);
bundleEncoder.setVertexBuffer(0, vertexBuffer);
// Create drawIndirect values
const uint32 = new Uint32Array(4);
uint32[0] = 3;
uint32[1] = 1;
uint32[2] = 0;
uint32[3] = 0;
// Create a GPUBuffer and write the draw values into it
const drawValues = device.createBuffer({
size: 16,
usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.INDIRECT,
});
device.queue.writeBuffer(drawValues, 0, uint32, 0, uint32.length);
// Draw the vertices
bundleEncoder.drawIndirect(drawValues, 0);
// End the bundle recording
const renderBundle = bundleEncoder.finish();
// ...
Specifications
| Specification |
|---|
| WebGPU # dom-gpurendercommandsmixin-drawindirect |
Browser compatibility
BCD tables only load in the browser
See also
- The WebGPU API