Skip to main content

Commands, passes, and presentation

A command encoder records ordered GPU work. Draw inside a render pass, dispatch inside a compute pass, and record transfers outside either pass. End each pass before using its results in the next one.

Draw to a canvas​

Create a device with createCanvasContext, then begin a pass without a framebuffer to target its default canvas:

const renderPass = device.beginRenderPass({clearColor: [0, 0, 0, 1]});
model.draw(renderPass);
renderPass.end();
device.submit();

When using AnimationLoop, omit device.submit() inside onRender: the loop submits after the callback returns. Ending a pass finishes encoding; it does not itself submit WebGPU work. See Issuing GPU commands for explicit command encoders.

A CanvasContext connects the device to an HTMLCanvasElement or OffscreenCanvas. When targeting a particular context, obtain its current framebuffer each frame with canvasContext.getCurrentFramebuffer() and pass it to beginRenderPass({framebuffer}). Do not cache a swap-chain framebuffer across frames. See Multiple canvases for presentation to several canvases.

Draw to a texture​

A Framebuffer groups color and optional depth/stencil attachments. Format strings create framebuffer-owned textures:

const framebuffer = device.createFramebuffer({
width: 512,
height: 512,
colorAttachments: ['rgba8unorm'],
depthStencilAttachment: 'depth24plus'
});

const renderPass = device.beginRenderPass({framebuffer});
model.draw(renderPass);
renderPass.end();
device.submit();

// The framebuffer owns the textures created from format strings.
framebuffer.destroy();

To sample the result in a later pass, supply a caller-owned texture created with Texture.RENDER_ATTACHMENT | Texture.SAMPLE usage instead. Destroy it separately when finished. Sampler filters control subsequent sampling; they do not make a texture renderable. Check format support through the device before choosing optional render-target formats.

For a new size, use framebuffer.clone({width, height}) and destroy the old framebuffer. The clone owns its new attachment textures and does not copy their contents; see Framebuffer ownership. Canvas framebuffer sizing is managed by its canvas context.

Clear or preserve attachments​

Clear values are pass properties, not framebuffer properties. The default values are opaque black [0, 0, 0, 1], depth 1, and stencil 0. Set clearColor, clearDepth, or clearStencil to boolean false to preserve that attachment. Use clearColors for separate values on multiple color attachments. See RenderPassProps for exact types and read-only options.

Reusable Draw Commands​

WebGPU supportedWebGL 2 not supported

Use a RenderBundleEncoder when the same WebGPU draw commands run repeatedly and only already-bound buffer or texture contents change. It records draw commands without starting a render pass. finish() returns an immutable RenderBundle, and a normal RenderPass replays that bundle.

const renderBundleEncoder = device.createRenderBundleEncoder({
colorAttachmentFormats: [device.preferredColorFormat]
});

model.draw(renderBundleEncoder);
const renderBundle = renderBundleEncoder.finish();

const renderPass = device.beginRenderPass({clearColor: [0, 0, 0, 1]});
renderPass.executeBundles([renderBundle]);
renderPass.end();
device.submit();

Rebuild a render bundle when its draw sequence, bound resources, pipeline compatibility, or attachment formats change. Multisampled render bundles are not currently supported.

Change a viewport between draws​

renderPass.setParameters({viewport: [0, 0, 800, 600]});
model.draw(renderPass);
renderPass.setParameters({viewport: [600, 0, 200, 150]});
model.draw(renderPass);

Viewport and scissor coordinates use drawing-buffer pixels, not CSS pixels. Use scissorRect to restrict writes; changing a viewport alone does not clip to its rectangle.

Run compute​

computePipeline.setBindings({data: storageBuffer});
const computePass = device.beginComputePass();
computePass.setPipeline(computePipeline);
computePass.dispatch(Math.ceil(itemCount / 64));
computePass.end();
device.submit();

Compute is WebGPU-only. Use GPU Graph when several stages need dependency scheduling.

Commands Outside Passes​

Not every command belongs inside a pass. CommandEncoder also records operations such as:

  • copyBufferToBuffer()
  • copyBufferToTexture()
  • copyTextureToBuffer()
  • copyTextureToTexture()
  • resolveQuerySet()
  • writeTimestamp()

Use these when the operation must be ordered with render or compute work in the same command stream. For simple CPU-driven uploads and readbacks, resource helpers such as Buffer.write() and Texture.writeData() are often simpler; see the buffer and texture guides for those choices.

Resize presentation​

device.canvasContext.setDrawingBufferSize(width, height);
depthTexture.destroy();
depthTexture = device.createTexture({width, height, format: 'depth24plus'});

Recreate size-dependent attachments, not static buffers, shaders, or pipelines.

Multiple canvases​

Use device.createPresentationContext() when one device must present to several canvases. For portable setup, create the device with a default CanvasContext backed by an OffscreenCanvas. On WebGL 2 the views share one GPU-backed target: finish and present each view before drawing the next. On WebGPU each presentation context has its own target.

const leftPresentationContext = device.createPresentationContext({canvas: leftCanvas});
const rightPresentationContext = device.createPresentationContext({canvas: rightCanvas});

const leftFramebuffer = leftPresentationContext.getCurrentFramebuffer();
const leftRenderPass = device.beginRenderPass({framebuffer: leftFramebuffer});
// draw left view
leftRenderPass.end();
leftPresentationContext.present();

const rightFramebuffer = rightPresentationContext.getCurrentFramebuffer();
const rightRenderPass = device.beginRenderPass({framebuffer: rightFramebuffer});
// draw right view
rightRenderPass.end();
rightPresentationContext.present();

High-dynamic-range presentation​

Rendering into an rgba16float offscreen texture does not automatically produce an HDR image on the display. The final WebGPU canvas must also use a floating-point presentation format and explicitly enable extended tone mapping. Otherwise, the browser clamps displayed colors to the standard [0, 1] range even when internal lighting and postprocessing retain brighter values.

Detect whether the current display advertises high dynamic range, then configure the default canvas when creating the device:

import {luma} from '@luma.gl/core/api';
import {webgpuAdapter} from '@luma.gl/core/webgpu';

const supportsHighDynamicRange =
typeof window !== 'undefined' && window.matchMedia('(dynamic-range: high)').matches;

const device = await luma.createDevice({
adapters: [webgpuAdapter],
createCanvasContext: supportsHighDynamicRange
? {
colorFormat: 'rgba16float',
colorSpace: 'display-p3',
toneMapping: 'extended'
}
: true
});

const renderingHighDynamicRange = device.preferredColorFormat === 'rgba16float';

The three canvas settings have separate responsibilities:

  • colorFormat: 'rgba16float' preserves fractional precision and color components above 1.0.
  • toneMapping: 'extended' asks WebGPU to display values above SDR white instead of clipping them to the [0, 1] presentation range.
  • colorSpace: 'display-p3' selects a wider display gamut; wide color and HDR luminance are related but independent capabilities.

Applications must preserve that range throughout their rendering pipeline:

  1. Keep scene lighting, emissive materials, and intermediate postprocessing in floating-point textures such as rgba16float.
  2. Match fullscreen render pipelines and presentation targets to device.preferredColorFormat.
  3. Avoid unconditional clamp(color, 0.0, 1.0) in the final shader when HDR presentation is active. Apply an SDR-safe curve to ordinary colors while preserving brighter specular and emissive highlights.
  4. Retain the normal 8-bit canvas and SDR tone mapping when the display, browser, or backend cannot present HDR.

window.matchMedia('(dynamic-range: high)') identifies display capability, not browser WebGPU canvas support. On browsers exposing GPUCanvasContext.getConfiguration(), inspect configuration.toneMapping?.mode === 'extended' to verify that extended presentation was accepted. Monitor availability can also change when a window moves between displays.

HDR presentation is WebGPU-specific. WebGL applications can still use floating-point intermediate render targets where supported, but their ordinary presentation canvas remains SDR. Floating-point presentation and intermediate textures also consume more GPU memory than 8-bit formats.

The Deferred Rendering: Illumination Lab example demonstrates clustered lighting, floating-point postprocessing, configurable highlight intensity, and automatic HDR/SDR presentation selection.