The setVertexBuffer() method of the GPURenderPassEncoder interface sets or unsets the current GPUBuffer for the given slot that will provide vertex data for subsequent drawing commands.
setVertexBuffer(slot, buffer, offset, size)
The following criteria must be met when calling setVertexBuffer(), otherwise a GPUValidationError is generated and the GPURenderPassEncoder becomes invalid:
-
buffer's GPUBuffer.usage contains the GPUBufferUsage.VERTEX flag. -
slot is less than the GPUDevice's maxVertexBuffers limit. -
offset + size is less than or equal to the buffer's GPUBuffer.size. -
offset is a multiple of 4.
In our basic render demo, several commands are recorded via a GPUCommandEncoder. Most of these commands originate from the GPURenderPassEncoder created via GPUCommandEncoder.beginRenderPass(). setVertexBuffer() is used as appropriate to set the source of vertex data.
const renderPipeline = device.createRenderPipeline(pipelineDescriptor);
const commandEncoder = device.createCommandEncoder();
const renderPassDescriptor = {
colorAttachments: [
{
clearValue: clearColor,
loadOp: "clear",
storeOp: "store",
view: context.getCurrentTexture().createView(),
},
],
};
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
passEncoder.setPipeline(renderPipeline);
passEncoder.setVertexBuffer(0, vertexBuffer);
passEncoder.draw(3);
passEncoder.end();
device.queue.submit([commandEncoder.finish()]);
passEncoder.setVertexBuffer(0, vertexBuffer);
passEncoder.setVertexBuffer(0, null);