大网数据广告活动
Web 标准与前端开发

GPURenderPassEncoder: drawIndexedIndirect() method

· 52CE 我爱测

The **`drawIndexedIndirect()`** method of the GPURenderPassEncoder interface draws indexed primitives using parameters read from a GPUBuffer.

WebGPU API

The drawIndexedIndirect() method of the GPURenderPassEncoder interface draws indexed primitives using parameters read from a GPUBuffer.

Syntax

drawIndexedIndirect(indirectBuffer, indirectOffset)

Parameters

  • indirectBuffer

    • : A GPUBuffer containing the indexCount, instanceCount, firstIndex, baseVertex, and firstInstance values needed to carry out the drawing operation. The buffer must contain a tightly packed block of five 32-bit unsigned integer values representing the values (20 bytes total), given in the same order as the arguments for GPURenderPassEncoder.drawIndexed(). So for example:

      const uint32 = new Uint32Array(5);
      uint32[0] = 3; // The indexCount value
      uint32[1] = 1; // The instanceCount value
      uint32[2] = 0; // The firstIndex value
      uint32[3] = 0; // The baseVertex value
      uint32[4] = 0; // The firstInstance value
      
      // Write values into a GPUBuffer
      device.queue.writeBuffer(buffer, 0, uint32, 0, uint32.length);
      

      [!NOTE] The indirect-first-instance feature needs to be enabled for non-zero firstInstance values to be used. If the indirect-first-instance feature is not enabled and firstInstance is not zero, the drawIndexedIndirect() call will be treated as a no-op.

  • indirectOffset

    • : The offset, in bytes, into indirectBuffer where 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 GPURenderPassEncoder becomes invalid:

  • indirectBuffer's GPUBuffer.usage contains the GPUBufferUsage.INDIRECT flag.
  • indirectOffset + the total size specified by the value parameters in the indirectBuffer is less than or equal to the indirectBuffer's GPUBuffer.size.
  • indirectOffset is a multiple of 4.

Examples

// …

// Create GPURenderPassEncoder
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);

// Set pipeline and vertex buffer
passEncoder.setPipeline(renderPipeline);
passEncoder.setVertexBuffer(0, vertexBuffer);
passEncoder.setIndexBuffer(indexBuffer, "uint16");

// Create drawIndexedIndirect values
const uint32 = new Uint32Array(5);
uint32[0] = 3;
uint32[1] = 1;
uint32[2] = 0;
uint32[3] = 0;
uint32[4] = 0;

// Create a GPUBuffer and write the draw values into it
const drawValues = device.createBuffer({
  size: 20,
  usage: GPUBufferUsage.COPY_DST | GPUBufferUsage.INDIRECT,
});
device.queue.writeBuffer(drawValues, 0, uint32, 0, uint32.length);

// Draw the vertices
passEncoder.drawIndexedIndirect(drawValues, 0);

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

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

// …

Specifications

Browser compatibility

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。