files/en-us/web/api/xrwebgllayer/index.md
{{SecureContext_Header}}{{APIRef("WebXR Device API")}}{{SeeCompatTable}}
The XRWebGLLayer interface of the WebXR Device API provides a linkage between the WebXR device (or simulated XR device, in the case of an inline session) and a WebGL context used to render the scene for display on the device. In particular, it provides access to the WebGL framebuffer and viewport to ease access to the context.
Although XRWebGLLayer is currently the only type of framebuffer layer supported by WebGL, it's entirely possible that future updates to the WebXR specification may allow for other layer types and corresponding image sources.
{{InheritanceDiagram}}
XRWebGLLayer object for use by the specified {{domxref("XRSession")}}, using a particular {{domxref("WebGLRenderingContext")}} or {{domxref("WebGL2RenderingContext")}} as the destination context.XRWebGLLayer's framebuffer.This snippet, taken from Drawing a frame in our "Movement and motion" WebXR example, shows how the XRWebGLLayer is obtained from the {{domxref("XRSession")}} object's rendering state and is then bound as the current rendering WebGL framebuffer by calling the WebGL {{domxref("WebGLRenderingContext.bindFrameBuffer", "bindFrameBuffer()")}} function.
let glLayer = xrSession.renderState.baseLayer;
gl.bindFrameBuffer(gl.FRAMEBUFFER, glLayer.framebuffer);
Each time the GPU is ready to render the scene to the XR device, the XR runtime calls the function you specified when you called the {{domxref("XRSession")}} method {{domxref("XRSession.requestAnimationFrame", "requestAnimationFrame()")}} to ask to render the frame.
That function receives as input an {{domxref("XRFrame")}} which encapsulates the data needed to render the frame. This information includes the pose (an {{domxref("XRViewerPose")}} object) that describes the position and facing direction of the viewer within the scene as well as a list of {{domxref("XRView")}} objects, each representing one perspective on the scene. In current WebXR implementations, there will never be more than two entries in this list: one describing the position and viewing angle of the left eye and another doing the same for the right.
let pose = xrFrame.getViewerPose(xrReferenceSpace);
if (pose) {
const glLayer = xrSession.renderState.baseLayer;
gl.bindFrameBuffer(gl.FRAMEBUFFER, glLayer.framebuffer);
for (const view of pose.views) {
const viewport = glLayer.getViewport(view);
gl.viewport(viewport.x, viewport.y, viewport.width, viewport.height);
/* Render the view */
}
}
{{Specifications}}
{{Compat}}