CommandEncoder
Quick answer
CommandEncoder records an ordered stream of commands: it begins render and compute passes, copies buffers and textures, and resolves queries, then finish() seals an immutable CommandBuffer. Create one per frame with device.createCommandEncoder and finish it before submitting. It mirrors the WebGPU GPUCommandEncoder interface.
device. createCommandEncoder ( props ?) → CommandEncoder
Key points
- Methods cover beginRenderPass, beginComputePass, copyBufferToTexture and finish.
- copyTextureToBuffer reads pixels back into a mappable Buffer.
- pushDebugGroup and popDebugGroup label commands for GPU debuggers.
Example
const encoder = device.createCommandEncoder({ id: 'frame' });
/* 第一趟:离屏渲染 */
const offscreen = encoder.beginRenderPass({
framebuffer: fbo,
clearColor: [0, 0, 0, 1]
});
offscreen.setPipeline(offscreenPipeline);
offscreen.setVertexArray(quadArray);
offscreen.draw({ vertexCount: 6 });
offscreen.end();
/* 第二趟:画到画布 */
const screen = encoder.beginRenderPass({
clearColor: [0.04, 0.06, 0.09, 1]
});
screen.setPipeline(postPipeline);
screen.setVertexArray(quadArray);
screen.setBindings({ uScene: colorTexture });
screen.draw({ vertexCount: 6 });
screen.end();
const commandBuffer = encoder.finish();
device.submit(commandBuffer);const encoder = device.createCommandEncoder();
const pass = encoder.beginRenderPass({
framebuffer: fbo,
clearColor: [0.1, 0.2, 0.3, 1]
});
pass.end();
encoder.copyTextureToBuffer(copyOptions);
device.submit(encoder.finish());
const pixels = await readbackBuffer.readAsync();
Notes
- Ending the encoder while a pass is still open makes finish() throw.
- An encoder is single-use; calling finish() twice on it fails.
English edition written for Atlaslog. Code is taken from the original Chinese page and runs unchanged.
Atlaslog (2026). CommandEncoder. https://www.jingweizhi.com/en/lumagl/api/command-encoder.html (accessed 2026-11-03).