GPUDevice: createCommandEncoder() 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.
ThecreateCommandEncoder() method of theGPUDevice interface creates aGPUCommandEncoder, used to encode commands to be issued to the GPU.
In this article
Syntax
createCommandEncoder()createCommandEncoder(descriptor)Parameters
descriptorOptionalAn object containing the following properties:
labelOptionalA string providing a label that can be used to identify the object, for example in
GPUErrormessages or console warnings.
Return value
AGPUCommandEncoder object instance.
Examples
In ourbasic render demo, several commands are recorded via aGPUCommandEncoder created viacreateCommandEncoder():
// …// Create GPUCommandEncoderconst 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 a trianglepassEncoder.setPipeline(renderPipeline);passEncoder.setVertexBuffer(0, vertexBuffer);passEncoder.draw(3);// End the render passpassEncoder.end();// …The commands encoded by theGPUCommandEncoder are recoded into aGPUCommandBuffer using theGPUCommandEncoder.finish() method. The command buffer is then passed into the queue via asubmit() call, ready to be processed by the GPU.
device.queue.submit([commandEncoder.finish()]);Note:Study theWebGPU samples to find more command encoding examples.
Specifications
| Specification |
|---|
| WebGPU> # dom-gpudevice-createcommandencoder> |
Browser compatibility
See also
- TheWebGPU API