GPURenderPassEncoder: setVertexBuffer() 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 insecure contexts (HTTPS), in some or allsupporting browsers.
Note: This feature is available inWeb Workers.
ThesetVertexBuffer() method of theGPURenderPassEncoder interface sets or unsets the currentGPUBuffer for the given slot that will provide vertex data for subsequent drawing commands.
In this article
Syntax
setVertexBuffer(slot, buffer, offset, size)Parameters
slotA number referencing the vertex buffer slot to set the vertex buffer for.
bufferA
GPUBufferrepresenting the buffer containing the vertex data to use for subsequent drawing commands, ornull, in which case any previously-set buffer in the given slot is unset.offsetOptionalA number representing the offset, in bytes, into
bufferwhere the vertex data begins. If omitted,offsetdefaults to 0.sizeOptionalA number representing the size, in bytes, of the vertex data contained in
buffer. If omitted,sizedefaults to thebuffer'sGPUBuffer.size-offset.
Return value
None (Undefined).
Validation
The following criteria must be met when callingsetVertexBuffer(), otherwise aGPUValidationError is generated and theGPURenderPassEncoder becomes invalid:
buffer'sGPUBuffer.usagecontains theGPUBufferUsage.VERTEXflag.slotis less than theGPUDevice'smaxVertexBufferslimit.offset+sizeis less than or equal to thebuffer'sGPUBuffer.size.offsetis a multiple of 4.
Examples
>Set vertex buffer
In ourbasic render demo, several commands are recorded via aGPUCommandEncoder. Most of these commands originate from theGPURenderPassEncoder created viaGPUCommandEncoder.beginRenderPass().setVertexBuffer() is used as appropriate to set the source of vertex data.
// …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 passconst renderPassDescriptor = { colorAttachments: [ { clearValue: clearColor, loadOp: "clear", storeOp: "store", view: context.getCurrentTexture().createView(), }, ],};const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);// Draw the trianglepassEncoder.setPipeline(renderPipeline);passEncoder.setVertexBuffer(0, vertexBuffer);passEncoder.draw(3);// End the render passpassEncoder.end();// End frame by passing array of command buffers to command queue for executiondevice.queue.submit([commandEncoder.finish()]);// …Unset vertex buffer
// Set vertex buffer in slot 0passEncoder.setVertexBuffer(0, vertexBuffer);// Later, unset vertex buffer in slot 0passEncoder.setVertexBuffer(0, null);Specifications
| Specification |
|---|
| WebGPU> # dom-gpurendercommandsmixin-setvertexbuffer> |
Browser compatibility
See also
- TheWebGPU API