GPURenderPassEncoder: draw() 方法

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

注意:此功能在 Web Workers 中可用。

draw() 方法是 GPURenderPassEncoder 接口的一部分,它会根据通过 setVertexBuffer() 提供给它的顶点缓冲区来绘制图元。

语法

js
draw(vertexCount)
draw(vertexCount, instanceCount)
draw(vertexCount, instanceCount, firstVertex)
draw(vertexCount, instanceCount, firstVertex, firstInstance)

参数

vertexCount

一个定义要绘制的顶点数量的数字。

instanceCount 可选

一个定义要绘制的实例数量的数字。如果省略,instanceCount 默认为 1。

firstVertex 可选

一个定义顶点缓冲区偏移量的数字(以顶点为单位),表示从何处开始绘制。如果省略,firstVertex 默认为 0。

firstInstance 可选

一个定义要绘制的第一个实例的数字。如果省略,firstInstance 默认为 0。

返回值

无 (Undefined)。

示例

在我们的 基础渲染演示 中,通过 GPUCommandEncoder 记录了多个命令。这些命令大多数都源自通过 GPUCommandEncoder.beginRenderPass() 创建的 GPURenderPassEncoderdraw() 用于指定绘制三个顶点来创建我们的三角形。

js
// …

const renderPipeline = device.createRenderPipeline(pipelineDescriptor);

// Create GPUCommandEncoder to issue commands to the GPU
// Note: render pass descriptor, command encoder, etc. are destroyed after use, fresh one needed for each frame.
const commandEncoder = device.createCommandEncoder();

// Create GPURenderPassDescriptor to tell WebGPU which texture to draw into, then initiate render pass
const renderPassDescriptor = {
  colorAttachments: [
    {
      clearValue: clearColor,
      loadOp: "clear",
      storeOp: "store",
      view: context.getCurrentTexture().createView(),
    },
  ],
};

const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);

// Draw the triangle
passEncoder.setPipeline(renderPipeline);
passEncoder.setVertexBuffer(0, vertexBuffer);
passEncoder.draw(3);

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

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

// …

规范

规范
WebGPU
# dom-gpurendercommandsmixin-draw

浏览器兼容性

另见