GPUCommandEncoder: beginRenderPass() method
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.
The beginRenderPass() method of the
GPUCommandEncoder interface starts encoding a render pass, returning a GPURenderPassEncoder that can be used to control rendering.
Syntax
beginRenderPass(descriptor)
Parameters
descriptor-
An object containing the following properties:
colorAttachments-
An array of objects (see Color attachment object structure) defining the color attachments that will be output to when executing this render pass.
depthStencilAttachmentOptional-
An object (see Depth/stencil attachment object structure) defining the depth/stencil attachment that will be output to and tested against when executing this render pass.
labelOptional-
A string providing a label that can be used to identify the object, for example in
GPUErrormessages or console warnings. maxDrawCountOptional-
A number indicating the maximum number of draw calls that will be done in the render pass. This is used by some implementations to size work injected before the render pass. You should keep the default value — 50000000 — unless you know that more draw calls will be done.
occlusionQuerySetOptional-
The
GPUQuerySetthat will store the occlusion query results for this pass. timestampWritesOptional-
An array of objects defining where and when timestamp query values will be written for this pass. These objects have the following properties:
location: An enumerated value specifying when the timestamp will be executed. Available values are:"beginning": The timestamp is executed along with the other encoded commands in the compute pass once the correspondingGPUCommandBufferis submitted."end": The timestamp is executed as part of a separate list of timestamp attachments once the pass ends.
queryIndex: A number specifying the index position in thequerySetthat the timestamp will be written to.querySet: TheGPUQuerySetthat the timestamp will be written to.
Color attachment object structure
Color attachment objects can have the following properties:
clearValueOptional-
A color value to clear the
viewtexture to, prior to executing the render pass. This value is ignored ifloadOpis not set to"clear".clearValuetakes an array or object representing the four color componentsr,g,b, andaas decimals.What follows is a sample array:
jsclearValue: [0.0, 0.5, 1.0, 1.0];The object equivalent would look like this:
jsclearValue: { r: 0.0, g: 0.5, b: 1.0, a: 1.0 }If
clearValueis omitted, it defaults to{r: 0, g: 0, b: 0, a: 0}. loadOp-
An enumerated value indicating the load operation to perform on
viewprior to executing the render pass. Possible values are:"clear": Loads theclearValuefor this attachment into the render pass."load": Loads the existing value for this attachment into the render pass.
Note: It is recommended to always use
"clear"in cases where the initial value doesn't matter, as it will give better performance on some devices such as mobiles. storeOp-
An enumerated value indicating the store operation to perform on
viewafter executing the render pass. Possible values are:"discard": Discards the resulting value of the render pass for this attachment."store": Stores the resulting value of the render pass for this attachment.
resolveTargetOptional-
A
GPUTextureViewobject representing the texture subresource that will receive the resolved output for this color attachment ifviewis multisampled. view-
A
GPUTextureViewobject representing the texture subresource that will be output to for this color attachment.Note: Each color or depth/stencil attachment must be a unique texture subresource, and texture subresources used as attachments cannot be used inside the render pass.
Depth/stencil attachment object structure
The depthStencilAttachment object can have the following properties:
depthClearValueOptional-
A number indicating the value to clear
view's depth component prior to executing the render pass. This is ignored ifdepthLoadOpis not set to"clear".The value must be between 0.0 and 1.0, inclusive.
depthLoadOpOptional-
An enumerated value indicating the load operation to perform on
view's depth component prior to executing the render pass. Possible values are:"clear": Loads theclearValuefor this attachment into the render pass."load": Loads the existing value for this attachment into the render pass.
Note: It is recommended to always use
"clear"in cases where the initial value doesn't matter, as it will give better performance on some devices such as mobiles. depthReadOnlyOptional-
A boolean. Setting the value to
truecauses the depth component ofviewto be read-only. IfdepthReadOnlyis omitted, it defaults tofalse. depthStoreOpOptional-
An enumerated value indicating the store operation to perform on
view's depth component after executing the render pass. Possible values are:"discard": Discards the resulting value of the render pass for this attachment."store": Stores the resulting value of the render pass for this attachment.
stencilClearValueOptional-
A number indicating the value to clear
view's stencil component to prior to executing the render pass. This is ignored ifstencilLoadOpis not set to"clear".If
stencilClearValueis omitted, it defaults to 0. stencilLoadOpOptional-
An enumerated value indicating the load operation to perform on
view's stencil component prior to executing the render pass. Possible values are:"clear": Loads theclearValuefor this attachment into the render pass."load": Loads the existing value for this attachment into the render pass.
Note: It is recommended to always use
"clear"in cases where the initial value doesn't matter, as it will give better performance on some devices such as mobiles. stencilReadOnlyOptional-
A boolean. Setting the value to
truecauses the stencil component ofviewto be read-only. IfstencilReadOnlyis omitted, it defaults tofalse. stencilStoreOpOptional-
An enumerated value indicating the store operation to perform on
view's stencil component after executing the render pass. Possible values are:"discard": Discards the resulting value of the render pass for this attachment."store": Stores the resulting value of the render pass for this attachment.
view-
A
GPUTextureViewobject representing the texture subresource that will be output to and read from for this depth/stencil attachment.
Return value
A GPURenderPassEncoder object instance.
Validation
The following criteria must be met when calling beginRenderPass(), otherwise a GPUValidationError is generated and an invalid GPURenderPassEncoder is returned.
General:
colorAttachments.lengthis less than or equal to theGPUDevice'smaxColorAttachmentslimit.- If
colorAttachmentscontains onlynullvalues,depthStencilAttachmentis provided. - All
views incolorAttachmentsanddepthStencilAttachmenthave equalGPUTexture.sampleCountvalues and render extents (GPUTexture.height,GPUTexture.width, andGPUTexture.depthOrArrayLayers). - If
occlusionQuerySetis set, the referencedGPUQuerySethas atypeof"occlusion".
For color attachment objects
- The
viewis renderable, and theview's format (i.e. specified in the descriptor of the originatingGPUTexture.createView()call) is a color renderable format. - If
resolveTargetis provided:- The
view's originatingGPUTexture'ssampleCountis greater than 1. - The
resolveTarget's originatingGPUTexture'ssampleCountis 1. resolveTargetis renderable.- The sizes of the subresources that
viewandresolveTargetprovide a view of match. view's andresolveTarget's formats match.
- The
- Color attachments bytes per sample is less than or equal to the
GPUDevice'smaxColorAttachmentBytesPerSamplelimit.
For depth/stencil attachment objects:
- The
viewis renderable, and its format is a depth-or-stencil format. - If
depthLoadOpis set to"clear", a validdepthClearValueis provided. - If
view's format is a combined depth-or-stencil format,depthReadOnlymatchesstencilReadOnly. - If
view's format has a depth aspect, anddepthReadOnlyisfalse,depthLoadOpanddepthStoreOpare provided. - If
view's format has a depth aspect, anddepthReadOnlyistrue,depthLoadOpanddepthStoreOpare not provided. - If
view's format has a stencil aspect, andstencilReadOnlyisfalse,stencilLoadOpandstencilStoreOpare provided. - If
view's format has a stencil aspect, andstencilReadOnlyistrue,stencilLoadOpandstencilStoreOpare not provided.
For timestamp queries:
- The
timestamp-queryfeature is enabled in theGPUDevice. - No two
timestampWritesobjects have the samelocation. In effect, this means that you can only run two timestamp queries per render pass. - For each timestamp query, the
querySetGPUQuerySet.typeis"timestamp", and thequeryIndexvalue is less than theGPUQuerySet.count. - No two
timestampWritesobjects have the samequeryIndexandquerySetpair.
Examples
In our basic render demo, a number of commands are recorded via a GPUCommandEncoder. These commands originate from the GPURenderPassEncoder created via beginRenderPass() :
// ...
// 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();
device.queue.submit([commandEncoder.finish()]);
// ...
Specifications
| Specification |
|---|
| WebGPU # dom-gpucommandencoder-beginrenderpass |
Browser compatibility
BCD tables only load in the browser
See also
- The WebGPU API