files/en-us/web/api/gpurenderpassencoder/index.md
{{APIRef("WebGPU API")}}{{SecureContext_Header}}{{AvailableInWorkers}}
The GPURenderPassEncoder interface of the {{domxref("WebGPU API", "WebGPU API", "", "nocode")}} encodes commands related to controlling the vertex and fragment shader stages, as issued by a {{domxref("GPURenderPipeline")}}. It forms part of the overall encoding activity of a {{domxref("GPUCommandEncoder")}}.
A render pipeline renders graphics to {{domxref("GPUTexture")}} attachments, typically intended for display in a {{htmlelement("canvas")}} element, but it could also render to textures used for other purposes that never appear onscreen. It has two main stages:
A vertex stage, in which a vertex shader takes positioning data fed into the GPU and uses it to position a series of vertices in 3D space by applying specified effects like rotation, translation, or perspective. The vertices are then assembled into primitives such as triangles (the basic building block of rendered graphics) and rasterized by the GPU to figure out what pixels each one should cover on the drawing canvas.
A fragment stage, in which a fragment shader computes the color for each pixel covered by the primitives produced by the vertex shader. These computations frequently use inputs such as images (in the form of textures) that provide surface details and the position and color of virtual lights.
A GPURenderPassEncoder object instance is created via the {{domxref("GPUCommandEncoder.beginRenderPass()")}} property.
{{InheritanceDiagram}}
{{domxref("GPURenderPassEncoder.beginOcclusionQuery", "beginOcclusionQuery()")}}
occlusionQuerySet descriptor property when invoking {{domxref("GPUCommandEncoder.beginRenderPass()")}} to run the render pass).{{domxref("GPURenderPassEncoder.draw", "draw()")}}
{{domxref("GPURenderPassEncoder.drawIndexed", "drawIndexed()")}}
{{domxref("GPURenderPassEncoder.drawIndirect", "drawIndirect()")}}
{{domxref("GPURenderPassEncoder.drawIndexedIndirect", "drawIndexedIndirect()")}}
{{domxref("GPURenderPassEncoder.end", "end()")}}
{{domxref("GPURenderPassEncoder.endOcclusionQuery", "endOcclusionQuery()")}}
{{domxref("GPURenderPassEncoder.executeBundles", "executeBundles()")}}
{{domxref("GPURenderPassEncoder.insertDebugMarker", "insertDebugMarker()")}}
{{domxref("GPURenderPassEncoder.popDebugGroup", "popDebugGroup()")}}
{{domxref("GPURenderPassEncoder.pushDebugGroup", "pushDebugGroup()")}}
{{domxref("GPURenderPassEncoder.setBindGroup", "setBindGroup()")}}
{{domxref("GPURenderPassEncoder.setBlendConstant", "setBlendConstant()")}}
"constant" and "one-minus-constant" blend factors (as set in the descriptor of the {{domxref("GPUDevice.createRenderPipeline()")}} method, in the blend property).{{domxref("GPURenderPassEncoder.setIndexBuffer", "setIndexBuffer()")}}
{{domxref("GPURenderPassEncoder.setPipeline", "setPipeline()")}}
{{domxref("GPURenderPassEncoder.setScissorRect", "setScissorRect()")}}
{{domxref("GPURenderPassEncoder.setStencilReference", "setStencilReference()")}}
"replace" stencil operation (as set in the descriptor of the {{domxref("GPUDevice.createRenderPipeline()")}} method, in the properties defining the various stencil operations).{{domxref("GPURenderPassEncoder.setVertexBuffer", "setVertexBuffer()")}}
{{domxref("GPURenderPassEncoder.setViewport", "setViewport()")}}
In our basic render demo, several commands are recorded via a {{domxref("GPUCommandEncoder")}}. Most of these commands originate from the GPURenderPassEncoder created via {{domxref("GPUCommandEncoder.beginRenderPass()")}}.
// …
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 pass
const renderPassDescriptor = {
colorAttachments: [
{
clearValue: clearColor,
loadOp: "clear",
storeOp: "store",
view: context.getCurrentTexture().createView(),
},
],
};
const passEncoder = commandEncoder.beginRenderPass(renderPassDescriptor);
// Draw the triangle
passEncoder.setPipeline(renderPipeline);
passEncoder.setVertexBuffer(0, vertexBuffer);
passEncoder.draw(3);
// End the render pass
passEncoder.end();
// End frame by passing array of command buffers to command queue for execution
device.queue.submit([commandEncoder.finish()]);
// …
{{Specifications}}
{{Compat}}