The createCommandEncoder() method of the GPUDevice interface creates a GPUCommandEncoder, used to encode commands to be issued to the GPU.
createCommandEncoder()
createCommandEncoder(descriptor)
A GPUCommandEncoder object instance.
In our basic render demo, several commands are recorded via a GPUCommandEncoder created via createCommandEncoder():
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();
The commands encoded by the GPUCommandEncoder are recoded into a GPUCommandBuffer using the GPUCommandEncoder.finish() method. The command buffer is then passed into the queue via a submit() call, ready to be processed by the GPU.
device.queue.submit([commandEncoder.finish()]);
Note: Study the WebGPU samples to find more command encoding examples.