GPURenderPassEncoder: setViewport() 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.
ThesetViewport() method of theGPURenderPassEncoder interface sets the viewport used during the rasterization stage to linearly map from normalized device coordinates to viewport coordinates.
In this article
Syntax
setViewport(x, y, width, height, minDepth, maxDepth)Parameters
xA number representing the minimum X value of the viewport, in pixels.
yA number representing the minimum Y value of the viewport, in pixels.
widthA number representing the width of the viewport, in pixels.
heightA number representing the height of the viewport, in pixels.
minDepthA number representing the minimum depth value of the viewport.
maxDepthA number representing the maximum depth value of the viewport.
Note:If asetViewport() call is not made, the default values are(0, 0, attachment width, attachment height, 0, 1) for each render pass.
Return value
None (Undefined).
Validation
The following criteria must be met when callingsetViewport(), otherwise aGPUValidationError is generated and theGPURenderPassEncoder becomes invalid:
x,y,width, andheightare all greater than or equal to 0.x+widthis less than or equal to the width of the render pass's render attachments (see note below).y+heightis less than or equal to the height of the render pass's render attachments (see note below).minDepthandmaxDepthare both inside the range 0.0–1.0 inclusive.minDepthis less thanmaxDepth.
Note:See the color and depth/stencil attachments specified in the descriptor ofGPUCommandEncoder.beginRenderPass(); the width and height are based on that of theGPUTexture that theirviews originate from.
Examples
>Basic snippet
In a typical canvas render, the following could be used to halve the width and height of the rendered graphics:
passEncoder.setViewport(0, 0, canvas.width / 2, canvas.height / 2, 0, 1);In context
In the WebGPU SamplesreversedZ example,setViewport is used several times to set the viewport for the different render passes. Study the example code listing for the full context.
For example:
// …colorPass.setViewport( (canvas.width * m) / 2, 0, canvas.width / 2, canvas.height, 0, 1,);// …Specifications
| Specification |
|---|
| WebGPU> # dom-gpurenderpassencoder-setviewport> |
Browser compatibility
See also
- TheWebGPU API