GPUDevice: createCommandEncoder() method
WebGPU API
WebGPU API
The **`createCommandEncoder()`** method of the
GPUDevice interface creates a GPUCommandEncoder, used to encode commands to be issued to the GPU.
## Syntax
```js-nolint
createCommandEncoder()
createCommandEncoder(descriptor)
```
### Parameters
- `descriptor`
- : An object containing the following properties:
- `label`
- : A string providing a label that can be used to identify the object, for example in GPUError messages or console warnings.
### Return value
A GPUCommandEncoder object instance.
## Examples
In our [basic render demo](https://mdn.github.io/dom-examples/webgpu-render-demo/), several commands are recorded via a GPUCommandEncoder created via `createCommandEncoder()`:
```js
// …
// Create GPUCommandEncoder
const commandEncoder = device.createCommandEncoder();
// Create GPURenderPassDescriptor to tell WebGPU which texture to draw into, then initiate render pass
const renderPassDescriptor = {
colorAttachments: [
{
clearValue: clearColor,
loadOp: "clear",
storeOp: "store",
view: context.getCurrentTexture().createView(),
},
],
};
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
// Draw a triangle
passEncoder.setPipeline(renderPipeline);
passEncoder.setVertexBuffer(0, vertexBuffer);
passEncoder.draw(3);
// End the render pass
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.
```js
device.queue.submit([commandEncoder.finish()]);
```
> [!NOTE]
> Study the [WebGPU samples](https://webgpu.github.io/webgpu-samples/) to find more command encoding examples.
## Specifications
## Browser compatibility
## See also
- The [WebGPU API](/docs/Web/API/WebGPU_API)
Cite this
"GPUDevice: createCommandEncoder() method". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/API/GPUDevice/createCommandEncoder JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy