WebXR Layers API Level 1

Editor’s Draft,

This version:
https://immersive-web.github.io/layers/
Latest published version:
https://www.w3.org/TR/webxrlayers-1/
Previous Versions:
Issue Tracking:
GitHub
Inline In Spec
Editors:
(Facebook)

Abstract

This specification describes support for various layer types used in a WebXR session.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This document was published by the Immersive Web Working Group as an Editors' Draft. This document is intended to become a W3C Recommendation. Feedback and comments on this specification are welcome. Please use Github issues. Discussions may also be found in the public-immersive-web@w3.org archives.

Publication as an Editors' Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 1 March 2019 W3C Process Document.

1. Introduction

The spec adds support of `composition` layers to the WebXR spec. The benefits of layers are as follows:

how to do hit testing? Should it be done by the UA?

1.1. Terminology

1.2. Application flow

If an author wants to use GL layers, they have to go through these steps:

  1. For any layer type other than XRProjectionLayer request support through requiredFeatures or optionalFeatures in requestSession().

  2. Create an XRWebGLBinding or XRMediaBinding.

  3. Create layers with these objects.

  4. Add the layers to XRRenderStateInit and call updateRenderState().

  5. During requestAnimationFrame() for webgl layers, draw content each WebGL layer.

2. Initialization

If an application wants to create layers other than of type XRProjectionLayer during a session, the session MUST be requested with an appropriate feature descriptor. The string "layers" is introduced by this module as a new valid feature descriptor for the WebXR Layers feature.

Layers of type XRProjectionLayer MUST always be supported, regardless if the feature descriptor was requested.

The following code requests layers as an optional feature.
navigator.xr.requestSession('immersive-vr', {
    optionalFeatures: ['layers']
  }

Layers are only supported for XRSessions created with XRSessionMode of "immersive-vr" or "immersive-ar". "inline" sessions MUST not support layers.

The "layers" feature descriptor has a feature requirement that it cannot be enabled when there is an active immersive session.

NOTE: This means that executing the request(permissionDesc) API with "layers" will not enable layers support for the current active session.

3. Layer types

3.1. Mono and stereo layers

A stereo layer MUST supply a different XRSubImage to render to for each view.

A mono layer MUST supply a single XRSubImage which is shown to each view.

The XR Compositor MUST ensure that layers are presented correctly in stereo to the observer.

3.2. XRLayerLayout

The XRLayerLayout enum defines the layout of the layer.
enum XRLayerLayout {
  "default",
  "mono",
  "stereo",
  "stereo-left-right",
  "stereo-top-bottom"
};

NOTE: If an XRCompositionLayer is created with a "default" or "stereo" XRLayerLayout, it is highly recommended that it is allocated with an "texture-array" texture type.

Note: The "stereo-left-right" and "stereo-top-bottom" layouts are designed to minimize draw calls for content that is already in stereo (for example stereo videos or images). Experiences that don’t require such assets types should use the "default" or "stereo" layout.

3.3. XRCompositionLayer

XRCompositionLayer defines a set of common attributes and behaviors across certain layer types.
interface XRCompositionLayer : XRLayer {
  readonly attribute XRLayerLayout layout;

  attribute boolean blendTextureSourceAlpha;
  attribute boolean? chromaticAberrationCorrection;
  attribute float? fixedFoveation;

  readonly attribute boolean needsRedraw;

  void destroy();
};

The layout attribute returns the layout of the layer.

The blendTextureSourceAlpha attribute enables the layer’s texture alpha channel.

The chromaticAberrationCorrection attribute is a hint for the XR Compositor to enable optical chromatic aberration correction for the layer. If the user agent or device does not support this attribute, they should return null on getting, and setting should be a no-op.

The fixedFoveation attribute controls the amount of foveation used by the XR Compositor. If the user agent or device does not support this attribute, they should return null on getting, and setting should be a no-op. Setting fixedFoveation to a value less than 0 will set it to 0 and setting it to a value higher than 1 will set it to 1. 0 sets the minimum amount of foveation while 1 set the maximum. It is up to the user agent how the XR Compositor interprets these values.

The needsRedraw attribute signals that the XRCompositionLayer should be rerendered in the next XR animation frame. It MAY be set when the underlying resources of a layer are lost or when the XR Compositor can no longer reproject the layer. Failing to redraw the content in the next XR animation frame might cause flickering or other side effects.

When the underlying resources of a layer are lost for an XRCompositionLayer layer, the user agent MUST run the following steps:
  1. Set layer’s needsRedraw to true.

  2. If layer is not an XRProjectionLayer, queue a task to fire an event named redraw on layer.

destroy() will delete the underlying attachments. If there are no attachments, this function does nothing.

To intialize a composition layer with a XRSession session and an optional instance of a WebGLRenderingContext or a WebGL2RenderingContext context, the user agent MUST run the following steps:

  1. Set this session to session.

  2. If context is defined, set this context to context.

  3. Set this blendTextureSourceAlpha to true.

  4. Initialize this chromaticAberrationCorrection as follows:

    If the user agent supports chromatic aberration correction
    Set this chromaticAberrationCorrection to true or false depending on the user agent’s preference.
    Otherwise
    Set this chromaticAberrationCorrection to null.
  5. Initialize this fixedFoveation as follows:

    If the user agent supports fixed foveation
    Set this fixedFoveation to a value between 0 and 1 depending on the user agent’s preference.
    Otherwise
    Set this fixedFoveation to null.

When calling destroy(), the user agent MUST run the following steps:

  1. Set this colorTextures array to an empty array.

  2. Set this depthStencilTextures array to an empty array.

  3. Destroy the underlying GL attachments.

Each XRCompositionLayer has a context object which is an instance of either null or a WebGLRenderingContext or a WebGL2RenderingContext and a media object which is an instance of null or a HTMLVideoElement.

Each XRCompositionLayer has an associated session, which is the XRSession it was created with.

When setting the space on a layer with XRSpace space and XRCompositionLayer layer, the user agent MUST run the following steps to validate if the space is valid:

  1. If space is null, throw TypeError and abort these steps.

  2. If space’s session is not equal to the layer’s session, throw TypeError and abort these steps.

XRCompositionLayer has an internal boolean isStatic that indicates that the author can only draw to this layer when needsRedraw is true.

NOTE: if isStatic is true the author can only draw into the layer once after creation or once after a redraw event. This allows the UA to only allocate a single GPU buffer.

3.4. XRProjectionLayer

An XRProjectionLayer is a layer that fills the entire view of the observer. Projection layers should be refreshed close to the device’s native frame rate.
interface XRProjectionLayer : XRCompositionLayer {
  readonly attribute boolean ignoreDepthValues;
};

The ignoreDepthValues attribute, if true, indicates that the XR Compositor MUST NOT make use of values in the depth buffer attachment when rendering. When the attribute is false it indicates that the content of the depth buffer attachment will be used by the XR Compositor and is expected to be representative of the scene rendered into the layer.

3.5. XRQuadLayer

An XRQuadLayer renders a layer that takes up a flat rectangular space in the virtual environment. Only the front of the layer MUST be visible; the back face MUST not be drawn by the XR Compositor.

A XRQuadLayer has no thicknes. It is a two-dimensional object positioned and oriented in 3D space. The position of a quad refers to the center of the quad.

interface XRQuadLayer : XRCompositionLayer {
  attribute XRSpace space;
  attribute XRRigidTransform transform;

  attribute float width;
  attribute float height;

  // Events
  attribute EventHandler onredraw;
};

The transform attributes sets and returns the offset and orientation relative to the space attribute. The transform and space attributes establish the spatial relationship of the layer within the user’s physical environment. When setting the space, first run the steps for setting the space on a layer.

The width and height attributes set and return the width and height of the layer in meters.

When initializing an XRQuadLayer layer with an XRQuadLayerInit init, the user agent MUST run the following steps:
  1. Initialize layer’s width to init’s width.

  2. Initialize layer’s height to init’s height.

  3. Let layer’s space be the init’s space.

  4. Initialize layer’s transform as follows:

    If init’s transform is set
    Let layer’s transform be a new XRRigidTransform in the relevant realm of layer initialized with position and orientation of init’s transform.
    Otherwise
    Let layer’s transform be a new XRRigidTransform in the relevant realm of layer initialized with a DOMPointInit position of { x: 0.0, y: 0.0, z: 0.0, w: 1.0 }.
  5. Initialize layer’s isStatic to init’s isStatic

The onredraw attribute is an Event handler IDL attribute for the redraw event type.

3.6. XRCylinderLayer

An XRCylinderLayer renders a layer that takes up a curved rectangular space in the virtual environment. Only the front of the layer MUST be visible; the back face MUST not be drawn by the XR Compositor.

A XRCylinderLayer has no thicknes. It is a two-dimensional object positioned and oriented in 3D space. The position of the cylinder refers to the center of the quad.

interface XRCylinderLayer : XRCompositionLayer {
  attribute XRSpace space;
  attribute XRRigidTransform transform;

  attribute float radius;
  attribute float centralAngle;
  attribute float aspectRatio;

  // Events
  attribute EventHandler onredraw;
};

The transform attribute sets and returns the offset and orientation relative to the space attribute. The transform and space attributes establish the spatial relationship of the layer within the user’s physical environment. When setting the space, first run the steps for setting the space on a layer.

The radius attribute controls the radius in meters of the cylinder.

The centralAngle attribute controls the angle in radians of the visible section of the cylinder. It grows symmetrically around the 0 angle.

The aspectRatio attribute controls the ratio of the visible cylinder section.

When initializing an XRCylinderLayer layer with an XRCylinderLayerInit init, the user agent MUST run the following steps:
  1. Initialize layer’s radius to init’s radius.

  2. Initialize layer’s centralAngle to init’s centralAngle.

  3. Initialize layer’s aspectRatio to init’s aspectRatio.

  4. Let layer’s space be the init’s space.

  5. Initialize layer’s transform as follows:

    If init’s transform is set
    Let layer’s transform be a new XRRigidTransform in the relevant realm of layer initialized with position and orientation of init’s transform.
    Otherwise
    Let layer’s transform be a new XRRigidTransform in the relevant realm of layer initialized with a DOMPointInit position of { x: 0.0, y: 0.0, z: 0.0, w: 1.0 }.
  6. Initialize layer’s isStatic to init’s isStatic

The onredraw attribute is an Event handler IDL attribute for the redraw event type.

3.7. XREquirectLayer

An XREquirectLayer renders a layer where the XR Compositor MUST map an equirectangular coded data onto the inside of a sphere.

this section needs clarification

interface XREquirectLayer : XRCompositionLayer {
  attribute XRSpace space;
  attribute XRRigidTransform transform;

  attribute float radius;
  attribute float centralHorizontalAngle;
  attribute float upperVerticalAngle;
  attribute float lowerVerticalAngle;

  // Events
  attribute EventHandler onredraw;
};

The transform attribute sets and returns the offset and orientation relative to space. The transform attribute and the space establish the spatial relationship of the layer within the user’s physical environment.

The radius attribute is the non-negative radius in meters of the sphere. Values of zero or infinity are treated as an infinite sphere.

Setting radius to a value less than 0 will set it to 0.

The centralHorizontalAngle, upperVerticalAngle and lowerVerticalAngle attributes set and return how the texture is mapped to the sphere.

Setting centralHorizontalAngle to a value less than 0 will set it to 0 and setting it to a value higher than 2π will set it to 2π.

Setting upperVerticalAngle or lowerVerticalAngle to a value less than -π/2 will set it to -π/2 and setting it to a value higher than π/2 will set it to π/2.

When assigning an XRSpace to the space attribute, first run the following steps.

When setting the space on an equirect layer with XRSpace space and XREquirectLayer layer, the user agent MUST run the following steps to validate if the space is valid:

  1. If init’s space is not an instance of type XRReferenceSpace, throw TypeError and abort these steps.

  2. If init’s space has a type of "viewer", throw TypeError and abort these steps.

  3. Run setting the space on a layer with space and layer.

When initializing an XREquirectLayer layer with an XREquirectLayerInit init, the user agent MUST run the following steps:
  1. Initialize layer’s radius to init’s radius.

  2. Initialize layer’s centralHorizontalAngle to init’s centralHorizontalAngle.

  3. Initialize layer’s upperVerticalAngle to init’s upperVerticalAngle.

  4. Initialize layer’s lowerVerticalAngle to init’s lowerVerticalAngle.

  5. Let layer’s space be the init’s space.

  6. Initialize layer’s transform as follows:

    If init’s transform is set
    Let layer’s transform be a new XRRigidTransform in the relevant realm of layer initialized with position and orientation of init’s transform.
    Otherwise
    Let layer’s transform be a new XRRigidTransform in the relevant realm of layer.
  7. Initialize layer’s isStatic to init’s isStatic

The onredraw attribute is an Event handler IDL attribute for the redraw event type.

3.8. XRCubeLayer

A XRCubeLayer renders a layer where the XR Compositor renders directly from a cubemap.

this section needs clarification

interface XRCubeLayer : XRCompositionLayer {
  attribute XRSpace space;
  attribute DOMPointReadOnly orientation;

  // Events
  attribute EventHandler onredraw;
};

The orientation attribute sets and returns the orientation relative to the space attribute. The orientation and space attributes establish the spatial relationship of the layer within the user’s physical environment. When placing the XRCubeLayer only the orientation of the space is considered. The cube layer will always be rendered with the view point at the center.

When assigning an XRSpace to the space attribute, first run the following steps.

When setting the space on an cube layer with XRSpace space and XRCubeLayer layer, the user agent MUST run the following steps to validate if the space is valid:

  1. If init’s space is not an instance of type XRReferenceSpace, throw TypeError and abort these steps.

  2. If init’s space has a type of "viewer", throw TypeError and abort these steps.

  3. Run setting the space on a layer with space and layer.

The onredraw attribute is an Event handler IDL attribute for the redraw event type.

4. Spaces

XRProjectionLayer and XRWebGLLayer don’t have associated an XRSpace because they render to the full frame.

XRCubeLayer and XREquirectLayer MUST only support XRReferenceSpaces that are not of type "viewer".

XRQuadLayer and XRCylinderLayer MUST support all XRSpace types.

Generally, developers should not use of "viewer" space to stabilize layers, as this will almost always defeat positional or rotational reprojection and result in a loss in stability of the rendered content relative to the world. The exception being small UI elements like a gaze cursor or targeting reticle.

Following are some best practices of spaces to use with a layer type:

5. Rendering

5.1. XRSubImage

The XRSubImage object represents what viewport of the GPU texture to use.
interface XRSubImage {
  [SameObject] readonly attribute XRViewport viewport;
};

NOTE: this class is designed to accomodate future extensions

The viewport attribute returns the XRViewport to use when rendering the sub image.

5.2. XRWebGLSubImage

The XRWebGLSubImage object is used during rendering of the layer.
interface XRWebGLSubImage : XRSubImage {
  [SameObject] readonly attribute WebGLTexture colorTexture;
  [SameObject] readonly attribute WebGLTexture? depthStencilTexture;
  readonly attribute unsigned long? imageIndex;
  readonly attribute unsigned long textureWidth;
  readonly attribute unsigned long textureHeight;
};

The colorTexture attribute returns the color WebGLTexture for the XRCompositionLayer.

The depthStencilTexture attribute returns the depth/stencil WebGLTexture for the XRCompositionLayer. If the layer was created without depth/stencil, this attribute returns null.

The imageIndex attribute returns the offset into the texture array. Valid only for layers that were requested with texture-array.

The colorTexture and depthStencilTexture objects MUST only be used during the XR animation frame of the current session and MUST be made invalid once the XR animation frame completes.

NOTE: the colorTexture and depthStencilTexture objects are allocated when the layer is contructed using the allocate color textures and allocate depth textures algoritms. The side effect of this pre-allocation is that calling getSubImage() and getViewSubImage() with the same parameters will always return the same texture objects.

In addition, they MUST behave as though they were allocated with texStorage2D or texStorage3D, as appropriate, even when using a WebGL 1.0 context.

NOTE: Changes to the dimensions or format of the colorTexture or depthStencilTexture objects are not allowed. GL commands may only alter the texel values and texture parameters. Using any of the following commands with the WebGLTexture will result in an INVALID_OPERATION error being generated, even if it does not affect the dimensions or format: TexImage*, CompressedTexImage*, CopyTexImage* and TexStorage*. The "Immutable-Format Texture Images" section in the OpenGL ES 3.0 spec defines these limitations in more detail.

The textureWidth and textureHeight attributes return the width and height in pixels of the GL attachments, respectively.

5.3. XRTextureType

The XRTextureType enum defines what type of texture is allocated.
enum XRTextureType {
  "texture",
  "texture-array"
};

6. GPU layer and view creation

6.1. Overview

When a layer is created it is backed by a GPU resource, typically a texture, provided by one of the Web platform’s graphics APIs. In order to specify which API is providing the layer’s GPU resources an XRWebGLBinding for the API in question must be created. Each graphics API may have unique requirements that must be satisfied before a context can be used in the creation of a layer. For example, a WebGLRenderingContext must have its xrCompatible flag set prior to being passed to the constructor of the XRWebGLBinding instance.

Any interaction between the XRSession the graphics API, such as allocating or retrieving textures, will go through this XRWebGLBinding instance, and the exact mechanics of the interaction will typically be API specific. This allows the rest of the WebXR API to be graphics API agnostic and more easily adapt to future advances in rendering techniques.

Once an XRWebGLBinding instance has been acquired, it can be used to create a variety of XRCompositionLayer. Any layers created by that instance will then be able to query the associated GPU resources each frame, generally expected to be the native API’s texture interface.

The various layer types are created with the create____Layer series of methods on the XRWebGLBinding instance. Information about the graphics resources required, such as whether or not to allocate a depth buffer or alpha channel, are passed in at layer creation time and will be immutable for the lifetime of the layer. The method will return the associated XRCompositionLayer type.

Some layer types may not be supported by the XRSession. If a layer type isn’t supported the method will throw an exception. XRProjectionLayer MUST be supported by all XRSessions.

6.2. XRProjectionLayerInit

The XRProjectionLayerInit dictionary represents a set of configurable values that describe how an XRProjectionLayer is initialized.
dictionary XRProjectionLayerInit {
  boolean depth = true;
  boolean stencil = false;
  boolean alpha = true;
  double scaleFactor = 1.0;
};

The depth attribute defines if the XRProjectionLayer will have a depth buffer.

The stencil attribute defines if the XRProjectionLayer will have a stencil buffer.

The alpha attribute defines if the XRProjectionLayer will have an alpha channel.

The scaleFactor attribute defines the value that the session’s recommended WebGL framebuffer resolution MUST be multiplied by determining the resolution of the layer’s attachments.

6.3. XRLayerInit

The XRLayerInit dictionary represents a set of common configurable values for XRQuadLayer, XRCylinderLayer, XREquirectLayer and XRCubeLayer .
dictionary XRLayerInit {
  required XRSpace space;
  required unsigned long viewPixelWidth;
  required unsigned long viewPixelHeight;
  XRLayerLayout layout = "mono";
  boolean depth = false;
  boolean stencil = false;
  boolean alpha = true;
};

The space attribute defines the spatial relationship with the user’s physical environment.

The viewPixelWidth and viewPixelHeight attributes define the rectangular dimensions of the XRCompositionLayer.

The layout attribute defines the layout of the layer.

The depth attribute defines if the XRCompositionLayer view will have a depth buffer.

The stencil attribute defines if the XRCompositionLayer view will have a stencil buffer.

The alpha attribute defines if the XRCompositionLayer view will have an alpha channel.

6.4. XRQuadLayerInit

The XRQuadLayerInit dictionary represents a set of configurable values that describe how an XRQuadLayer is initialized.

dictionary XRQuadLayerInit : XRLayerInit {
  XRRigidTransform? transform;
  float width = 1.0;
  float height = 1.0;
  boolean isStatic = false;
};

6.5. XRCylinderLayerInit

The XRCylinderLayerInit dictionary represents a set of configurable values that describe how an XRCylinderLayer is initialized.

dictionary XRCylinderLayerInit : XRLayerInit {
  XRRigidTransform? transform;
  float radius = 2.0;
  float centralAngle = 0.78539;
  float aspectRatio = 2.0;
  boolean isStatic = false;
};

The default value of centralAngle is π / 4.

6.6. XREquirectLayerInit

The XREquirectLayerInit dictionary represents a set of configurable values that describe how an XREquirectLayer is initialized.

dictionary XREquirectLayerInit : XRLayerInit {
  XRRigidTransform? transform;
  float radius = 0;
  float centralHorizontalAngle = 6.28318;
  float upperVerticalAngle = 1.570795;
  float lowerVerticalAngle = -1.570795;
  boolean isStatic = false;
};

The default value of centralHorizontalAngle is 2π. The default value of upperVerticalAngle is π/2. The default value of lowerVerticalAngle is -π/2.

6.7. XRCubeLayerInit

The XRCubeLayerInit dictionary represents a set of configurable values that describe how an XRCubeLayer is initialized.

dictionary XRCubeLayerInit : XRLayerInit {
  DOMPointReadOnly? orientation;
  boolean isStatic = false;
};

6.8. XRWebGLBinding

The XRWebGLBinding object is used to create layers that have a GPU backend.
interface XRWebGLBinding {
  constructor(XRSession session, XRWebGLRenderingContext context);

  readonly attribute double nativeProjectionScaleFactor;

  XRProjectionLayer createProjectionLayer(XRTextureType textureType,
                                          optional XRProjectionLayerInit init);
  XRQuadLayer createQuadLayer(XRTextureType textureType,
                              optional XRQuadLayerInit init);
  XRCylinderLayer createCylinderLayer(XRTextureType textureType,
                                      optional XRCylinderLayerInit init);
  XREquirectLayer createEquirectLayer(XRTextureType textureType,
                                      optional XREquirectLayerInit init);
  XRCubeLayer createCubeLayer(optional XRCubeLayerInit init);

  XRWebGLSubImage getSubImage(XRCompositionLayer layer, XRFrame frame, optional XREye eye = "none");
  XRWebGLSubImage getViewSubImage(XRProjectionLayer layer, XRView view);
};

the init dictionaries shouldn’t be optional. This is bikeshed issue 1566.

Each XRWebGLBinding has a context object of type XRWebGLRenderingContext which is an instance of either a WebGLRenderingContext or a WebGL2RenderingContext.

Each XRWebGLBinding has an associated session, which is the XRSession it was created with.

NOTE: It is possible to create more than one XRWebGLBinding. Any layer created with an instance of XRWebGLBinding can be used with another instance of XRWebGLBinding as long as both were created with the same session and the same context. The lifetime of layers or instances of XRWebGLSubImage is not tied to the lifetime of the XRWebGLBinding that created them.

Each XRCompositionLayer created through XRWebGLBinding has an internal colorTextures array which is an array of WebGLTextures for color textures and an internal depthStencilTextures which is an array of WebGLTextures for depth/stencil textures.

Each XRProjectionLayer created through XRWebGLBinding has an internal colorTextures for secondary views array which is an array of WebGLTextures for color textures and an internal depthStencilTextures for secondary views array which is an array of WebGLTextures for depth/stencil textures that are used to render the secondary views.

The XRWebGLBinding(session, context) constructor MUST perform the following steps when invoked:

  1. Let binding be a new XRWebGLBinding in the relevant realm of session.

  2. If session’s ended value is true, throw an InvalidStateError and abort these steps.

  3. If context is lost, throw an InvalidStateError and abort these steps.

  4. If session is not an immersive session, throw an InvalidStateError and abort these steps.

  5. If context’s XR compatible boolean is false, throw an InvalidStateError and abort these steps.

  6. Initialize binding’s context to context.

  7. Initialize binding’s session to session.

  8. Return binding.

The nativeProjectionScaleFactor function returns the value that the session’s recommended WebGL framebuffer resolution MUST be multiplied by to yield the session’s native WebGL framebuffer resolution.

special case UA behavior if the size causes the layout to change (ie if the requested width exceeds a limit with "stereo-left-right")

To determine the layout attribute using an XRTextureType textureType, an XRWebGLRenderingContext context and an XRLayerLayout layout, the user agent MUST run the following steps:

  1. If context is not an WebGL2RenderingContext and textureType is "texture-array", throw TypeError and abort these steps.

  2. If textureType is "texture-array" and not all of the session’s views in the list of views have the same recommended WebGL texture resolution, throw an NotSupportedError and abort these steps.

  3. If layout is "mono", return layout and abort these steps.

  4. If layout is "default", run the following steps:

    1. If the size of list of views is 1, return "mono" and abort these steps.

    2. If textureType is "texture-array", return layout and abort these steps.

  5. If layout is "default" or "stereo", run the following steps:

    1. If the user agent prefers "stereo-left-right" layout, return "stereo-left-right" and abort these steps.

    2. If the user agent prefers "stereo-top-bottom" layout, return "stereo-top-bottom" and abort these steps.

  6. return layout.

To determine the maximum scalefactor using an XRSession session, an XRWebGLRenderingContext context and an XRLayerLayout layout, the user agent MUST run the following steps:

  1. Let largest width be the largest width of the recommended WebGL texture resolution from the session’s list of views excluding the secondary views.

  2. Let largest height be the largest height of the recommended WebGL texture resolution from the session’s list of views excluding the secondary views.

  3. If layout is "stereo-left-right" layout, multiply largest width by 2.

  4. If layout is "stereo-top-bottom" layout, multiply largest height by 2.

  5. Let largest view dimension be the largest of largest width or largest height.

  6. Let largest texture dimension be the largest dimension of a WebGLTexture created by context.

  7. return largest texture dimension divided by largest view dimension.

To allocate color textures for projection layers using an XRProjectionLayer layer, an XRTextureType textureType, a boolean alpha and a float scaleFactor, the user agent MUST run the following steps:

  1. Let array be a new array in the relevant realm of context.

  2. Let context be layer’s context.

  3. Let session be layer’s session.

  4. Let numViews be the number of the session’s list of views excluding the secondary views.

  5. Let view be the first entry in the session’s list of views that is not a secondary views.

  6. Let width be the width of view’s recommended WebGL texture resolution multiplied by scaleFactor.

  7. Let height be the height of view’s recommended WebGL texture resolution multiplied by scaleFactor.

  8. If layer’s layout is "mono" or "default":

    If textureType is "texture-array":
    If the session’s views in the list of views don’t all have the same recommended WebGL texture resolution excluding the secondary views, throw an NotSupportedError and abort these steps.
    Initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D_ARRAY texture with numViews layers using context, alpha, width and height.
    Return array and abort these steps.
    Otherwise
    For each view in the session’s list of views:
    1. If view is a secondary view, continue.

    2. Let width be the width of view’s recommended WebGL texture resolution multiplied by scaleFactor.

    3. Let height be the height of view’s recommended WebGL texture resolution multiplied by scaleFactor.

    4. let texture be a new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D texture with context, alpha, width and height.

    5. Append texture to array.

    Return array and abort these steps.
  9. If the session’s views in the list of views don’t all have the same recommended WebGL texture resolution excluding the secondary views, throw an NotSupportedError and abort these steps.

  10. If layer’s layout is stereo-left-right, initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a textureType texture using context , alpha, numViews multiplied by width and height.

  11. If layer’s layout is stereo-top-bottom, initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a textureType texture using context , alpha, width and numViews multiplied by height.

  12. return array.

To allocate depth textures for projection layers using an XRProjectionLayer layer, an XRTextureType textureType, a boolean depth, a boolean stencil and a float scaleFactor, the user agent MUST run the following steps:

  1. let array be a new array in the relevant realm of context.

  2. let context be layer’s context.

  3. let session be layer’s session.

  4. If depth and stencil are not set, return array and abort these steps.

  5. let depthsupport be true if context is a WebGL2RenderingContext or the WEBGL_depth_texture extension is enabled in context.

  6. If depth or stencil are set and depthsupport is false, throw TypeError and abort these steps.

  7. let numViews be the number of the session’s list of views excluding the secondary views.

  8. Let view be the first entry in the session’s list of views that is not a secondary view.

  9. Let width be the width of view’s recommended WebGL texture resolution multiplied by scaleFactor.

  10. Let height be the height of view’s recommended WebGL texture resolution multiplied by scaleFactor.

  11. If layer’s layout is "mono" or "default":

    If textureType is "texture-array":
    Initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D_ARRAY texture with numViews layers using context, stencil, width and height.
    Return array and abort these steps.
    Otherwise
    For each view in the session’s list of views:
    1. If view is a secondary view, continue.

    2. Let width be the width of view’s recommended WebGL texture resolution multiplied by scaleFactor.

    3. Let height be the height of view’s recommended WebGL texture resolution multiplied by scaleFactor.

    4. let texture be a new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D texture with context, stencil, width and height.

    5. Append texture to array.

    Return array and abort these steps.
  12. If the session’s views in the list of views don’t all have the same recommended WebGL texture resolution excluding the secondary views, throw an NotSupportedError and abort these steps.

  13. If layer’s layout is stereo-left-right, initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a textureType texture using context , stencil, numViews multiplied by width and height.

  14. If layer’s layout is stereo-top-bottom, initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a textureType texture using context , stencil, width and numViews multiplied by height.

  15. return array.

To allocate the color textures for the secondary views using an XRProjectionLayer layer, an XRTextureType textureType, a boolean alpha and a float scaleFactor, the user agent MUST run the following steps:

  1. Let context be layer’s context.

  2. Let session be layer’s session.

  3. Let array be a new array in the relevant realm of context.

  4. For each view in the session’s list of views:

    1. If view is not a secondary view, continue.

    2. Let width be the width of view’s recommended WebGL texture resolution multiplied by scaleFactor.

    3. Let height be the height of view’s recommended WebGL texture resolution multiplied by scaleFactor.

    4. Initialize texture as follows:

      If textureType is "texture-array":
      Let texture be a new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D_ARRAY texture with context, alpha, width and height.
      Otherwise
      Let texture be a new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D texture with context, alpha, width and height.
    5. Append texture to array.

  5. Return array and abort these steps.

To allocate the depth textures for the secondary views using an XRProjectionLayer layer, an XRTextureType textureType, a boolean depth, a boolean stencil and a float scaleFactor, the user agent MUST run the following steps:

  1. Let context be layer’s context.

  2. Let session be layer’s session.

  3. If depth and stencil are not set, return null and abort these steps.

  4. let depthsupport be true if context is a WebGL2RenderingContext or the WEBGL_depth_texture extension is enabled in context.

  5. If depth or stencil are set and depthsupport is false, throw TypeError and abort these steps.

  6. Let array be a new array in the relevant realm of context.

  7. For each view in the session’s list of views:

    1. If view is not a secondary view, continue.

    2. Let width be the width of view’s recommended WebGL texture resolution multiplied by scaleFactor.

    3. Let height be the height of view’s recommended WebGL texture resolution multiplied by scaleFactor.

    4. Initialize texture as follows:

      If textureType is "texture-array":
      Let texture be a new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D_ARRAY texture with context, stencil, width and height.
      Otherwise
      Let texture be a new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D texture with context, stencil, width and height.
    5. Append texture to array.

  8. Return array and abort these steps.

the scaleFactor needs to be recalculated for the secondary views.

To allocate color textures using an XRCompositionLayer layer, an XRTextureType textureType and an XRLayerInit init, the user agent MUST run the following steps:

  1. let array be a new array in the relevant realm of context.

  2. let context be layer’s context.

  3. If layer’s layout is "mono":

    If textureType is "texture-array":
    Initialize array with 1 new instance of WebGLTexture in the relevant realm of this context created as a TEXTURE_2D_ARRAY texture with 1 internal texture using context and init’s alpha, viewPixelWidth and viewPixelHeight values.
    Otherwise
    Initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D texture with context and init’s alpha, viewPixelWidth and viewPixelHeight values.
  4. If layer’s layout is "stereo":

    If textureType is "texture-array":
    Initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D_ARRAY texture with 2 layers using context and init’s alpha, viewPixelWidth and viewPixelHeight values.
    Return array and abort these steps.
    Otherwise
    Initialize array with 2 new instances of WebGLTexture in the relevant realm of context created as a TEXTURE_2D texture with context and init’s alpha, viewPixelWidth and viewPixelHeight values.
    Return array and abort these steps.
  5. If layer’s layout is stereo-left-right, initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a textureType texture using context and init’s alpha, double of viewPixelWidth and viewPixelHeight values.

  6. If layer’s layout is stereo-top-bottom, initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a textureType texture using context and init’s alpha, viewPixelWidth and double of viewPixelHeight values.

  7. return array.

To allocate depth textures using an XRCompositionLayer layer, an XRTextureType textureType and an XRLayerInit init, the user agent MUST run the following steps:

  1. let array be a new array in the relevant realm of context.

  2. let context be layer’s context.

  3. If init’s depth and stencil are not set, return array and abort these steps.

  4. let depthsupport be true if context is a WebGL2RenderingContext or the WEBGL_depth_texture extension is enabled in context.

  5. If init’s depth or stencil are set and depthsupport is false, throw TypeError and abort these steps.

  6. If layer’s layout is "mono":

    If textureType is "texture-array":
    Initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D_ARRAY texture with 1 internal texture using context and init’s stencil, viewPixelWidth and viewPixelHeight values.
    Otherwise
    Initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D texture with context and init’s stencil, viewPixelWidth and viewPixelHeight values.
  7. If layer’s layout is "stereo":

    If textureType is "texture-array":
    Initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a TEXTURE_2D_ARRAY texture with 2 layers using context and init’s stencil, viewPixelWidth and viewPixelHeight values.
    Return array and abort these steps.
    Otherwise
    Initialize array with 2 new instances of WebGLTexture in the relevant realm of context created as a TEXTURE_2D texture with context and init’s stencil, viewPixelWidth and viewPixelHeight values.
    Return array and abort these steps.
  8. If layer’s layout is stereo-left-right, initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a textureType texture using context and init’s stencil, double of viewPixelWidth and viewPixelHeight values.

  9. If layer’s layout is stereo-top-bottom, initialize array with 1 new instance of WebGLTexture in the relevant realm of context created as a textureType texture using context and init’s stencil, viewPixelWidth and double of viewPixelHeight values.

  10. return array.

The createProjectionLayer(XRTextureType textureType, optional XRProjectionLayerInit init) method creates a new XRProjectionLayer layer.

When this method is invoked, the user agent MUST run the following steps:

  1. Let session be this session.

  2. Let context be this context.

  3. Let layer be a new XRProjectionLayer in the relevant realm of this.

  4. If session’s ended value is true, throw InvalidStateError and abort these steps.

  5. If context is lost, throw InvalidStateError and abort these steps.

  6. Run intialize a composition layer on layer with session and context.

  7. Initialize layer’s isStatic to false.

  8. Initialize layer’s ignoreDepthValues as follows:

    If init’s depth value is false and the XR Compositor will make use of depth values
    Initialize layer’s ignoreDepthValues to false
    Otherwise
    Initialize layer’s ignoreDepthValues to true
  9. let layout be the result of determining the layout attribute with textureType, context and "default".

  10. Let maximum scalefactor be the result of determining the maximum scalefactor with session, context and layout.

  11. If scaleFactor is larger than maximum scalefactor, set scaleFactor to maximum scalefactor.

  12. Initialize layer’s layout to layout.

  13. Initialize layer’s needsRedraw to true.

  14. let layer’s colorTextures be the result of allocating color textures for projection layers with layer, textureType, init’s alpha and init’s scaleFactor.

  15. let layer’s depthStencilTextures be the result of allocating depth textures for projection layers with layer, textureType, init’s depth, init’s stencil and init’s scaleFactor.

  16. Initialize the colortextures for secondary views as follows:

    If the session was created with "secondary-views" enabled
    Let colortextures for secondary views be the result of allocate the color textures for the secondary views with layer, textureType, init’s alpha and init’s scaleFactor.
    Otherwise
    Let colortextures for secondary views be null.
  17. Initialize the depthstenciltextures for secondary views as follows:

    If the session was created with "secondary-views" enabled
    Let depthstenciltextures for secondary views be the result of allocate the depth textures for the secondary views with layer, textureType, init’s depth, init’s stencil and init’s scaleFactor.
    Otherwise
    Let depthstenciltextures for secondary views be null.
  18. Allocate and initialize resources compatible with session’s XR device, including GPU accessible memory buffers, as required to support the compositing of layer.

  19. If layer’s resources were unable to be created for any reason, throw an OperationError and abort these steps.

  20. Return layer.

define "opaque" WebGLTexture

The createQuadLayer(XRTextureType textureType, XRQuadLayerInit init) method creates a new XRQuadLayer layer.

When this method is invoked, the user agent MUST run the following steps:

  1. Let session be this session.

  2. If session was not created with "layers" enabled, throw an NotSupportedError and abort these steps.

  3. Let context be this context.

  4. If session’s ended value is true, throw InvalidStateError and abort these steps.

  5. If context is lost, throw InvalidStateError and abort these steps.

  6. If layout is "default", throw TypeError and abort these steps.

  7. Let layer be a new XRQuadLayer in the relevant realm of this.

  8. Run intialize a composition layer on layer with session and context.

  9. Run initialize a quad layer with layer and init.

  10. let layout be the result of determining the layout attribute with textureType, context and init’s layout.

  11. Initialize layer’s layout to layout.

  12. Initialize layer’s needsRedraw to true.

  13. let layer’s colorTextures be the result of allocating color textures with layer, textureType and init.

  14. let layer’s depthStencilTextures be the result of allocating depth textures with layer, textureType and init.

  15. Allocate and initialize resources compatible with session’s XR device, including GPU accessible memory buffers, as required to support the compositing of layer.

  16. If layer’s resources were unable to be created for any reason, throw an OperationError and abort these steps.

  17. return layer.

The createCylinderLayer(XRTextureType textureType, XRCylinderLayerInit init) method creates a new XRCylinderLayer layer.

When this method is invoked, the user agent MUST run the following steps:

  1. Let session be this session.

  2. If session was not created with "layers" enabled, throw an NotSupportedError and abort these steps.

  3. Let context be this context.

  4. If session’s ended value is true, throw InvalidStateError and abort these steps.

  5. If context is lost, throw InvalidStateError and abort these steps.

  6. If layout is "default", throw TypeError and abort these steps.

  7. Let layer be a new XRCylinderLayer in the relevant realm of this.

  8. Run intialize a composition layer on layer with session and context.

  9. Run initialize a cylinder layer with layer and init.

  10. let layout be the result of determining the layout attribute with textureType, context and init’s layout.

  11. Initialize layer’s layout to layout.

  12. Initialize layer’s needsRedraw to true.

  13. let layer’s colorTextures be the result of allocating color textures with layer, textureType and init.

  14. let layer’s depthStencilTextures be the result of allocating depth textures with layer, textureType and init.

  15. Allocate and initialize resources compatible with session’s XR device, including GPU accessible memory buffers, as required to support the compositing of layer.

  16. If layer’s resources were unable to be created for any reason, throw an OperationError and abort these steps.

  17. return layer.

The createEquirectLayer(XRTextureType textureType, XREquirectLayerLayerInit init) method creates a new XREquirectLayer layer.

When this method is invoked, the user agent MUST run the following steps:

  1. Let session be this session.

  2. If session was not created with "layers" enabled, throw an NotSupportedError and abort these steps.

  3. Let context be this context.

  4. If session’s ended value is true, throw InvalidStateError and abort these steps.

  5. If context is lost, throw InvalidStateError and abort these steps.

  6. If layout is "default", throw TypeError and abort these steps.

  7. If init’s space is not an instance of type XRReferenceSpace, throw TypeError and abort these steps.

  8. If init’s space has a type of "viewer", throw TypeError and abort these steps.

  9. Let layer be a new XREquirectLayer in the relevant realm of this.

  10. Run intialize a composition layer on layer with session and context.

  11. Run initialize a equirect layer with layer and init.

  12. let layout be the result of determining the layout attribute with textureType, context and init’s layout.

  13. Initialize layer’s layout to layout.

  14. Initialize layer’s needsRedraw to true.

  15. let layer’s colorTextures be the result of allocating color textures with layer, textureType and init.

  16. let layer’s depthStencilTextures be the result of allocating depth textures with layer, textureType and init.

  17. Allocate and initialize resources compatible with session’s XR device, including GPU accessible memory buffers, as required to support the compositing of layer.

  18. If layer’s resources were unable to be created for any reason, throw an OperationError and abort these steps.

  19. return layer.

The createCubeLayer(XRCubeLayerInit init) method creates a new XRCubeLayer layer.

When this method is invoked, the user agent MUST run the following steps:

  1. Let session be this session.

  2. If session was not created with "layers" enabled, throw an NotSupportedError and abort these steps.

  3. Let context be this context.

  4. If session’s ended value is true, throw InvalidStateError and abort these steps.

  5. If context is not a WebGL2RenderingContext context, throw InvalidStateError and abort these steps.

  6. If context is lost, throw InvalidStateError and abort these steps.

  7. If init’s space is not an instance of type XRReferenceSpace, throw TypeError and abort these steps.

  8. If init’s space has a type of "viewer", throw TypeError and abort these steps.

  9. Let layer be a new XRCubeLayer in the relevant realm of this.

  10. Run intialize a composition layer on layer with session and context.

  11. Let layer’s space be the init’s space.

  12. Initialize layer’s isStatic to init’s isStatic

  13. Initialize layer’s orientation as follows:

    If init’s orientation is set
    Let layer’s orientation be the result of running fromPoint with init’s orientation.
    Otherwise
    Let layer’s orientation be a new DOMPointReadOnly in the relevant realm of this.
  14. let layout be init’s layout.

  15. Initialize layer’s needsRedraw to true.

  16. If layout is "default" or "stereo-left-right" or "stereo-top-bottom", throw TypeError and abort these steps.

  17. Let layer’s colorTextures be a new array in the relevant realm of this XRCubeLayer.

  18. Initialize layer’s colorTextures as follows, based on the value of layout:

    "mono":
    Initialize colorTextures with 1 new instance of WebGLTexture in the relevant realm of this XRCubeLayer created as a TEXTURE_CUBE_MAP texture with context and init’s alpha, viewPixelWidth and viewPixelHeight values.
    Otherwise
    Initialize colorTextures with 2 new instances of WebGLTexture in the relevant realm of this XRCubeLayer created as a TEXTURE_CUBE_MAP texture with context and init’s alpha, viewPixelWidth and viewPixelHeight values.
  19. Let layer’s depthStencilTextures be a new array in the relevant realm of this XRCubeLayer.

  20. If init’s depth and stencil are set, initialize layer’s depthStencilTextures as follows:

    If context is not a WebGL2RenderingContext and the WEBGL_depth_texture extension is not enabled in context.
    Throw TypeError and abort these steps.
    Else if layout is "mono":
    Initialize depthStencilTextures with 1 new instance of WebGLTexture in the relevant realm of this XRCubeLayer created as a TEXTURE_CUBE_MAP texture with context and init’s alpha, viewPixelWidth and viewPixelHeight values.
    Otherwise
    Initialize depthStencilTextures with 2 new instances of WebGLTexture in the relevant realm of this XRCubeLayer created as a TEXTURE_CUBE_MAP texture with context and init’s alpha, viewPixelWidth and viewPixelHeight values.
  21. Allocate and initialize resources compatible with session’s XR device, including GPU accessible memory buffers, as required to support the compositing of layer.

  22. If layer’s resources were unable to be created for any reason, throw an OperationError and abort these steps.

  23. return layer.

Define how cubemap sizes are determined.

How should space be handled. Can you walk to the edge of a cubemap?

determine the initial state of orientation.

To validate the state of the XRWebGLSubImage creation function of an XRWebGLBinding binding with parameters of XRCompositionLayer layer and XRFrame frame, the user agent MUST run the following steps:

  1. If frame’s session is not equal to layer’s session, return false and abort these steps.

  2. If frame’s active boolean is false, return false and abort these steps.

  3. If frame’s animationFrame boolean is false, return false and abort these steps.

  4. If binding’s session is not equal to layer’s session, return false and abort these steps.

  5. If binding’s context is not equal to layer’s context, return false and abort these steps.

  6. If the layer’s colorTextures array is empty or missing, return false and abort these steps.

  7. If the layer’s isStatic is true and layer’s needsRedraw is false, return false and abort these steps.

  8. return true.

To initialize the viewport of an XRViewport viewport with a WebGLTexture texture, a XRLayerLayout layout, an integer offset and a integer num, the user agent MUST run the following steps:

  1. Set viewport’s x to 0.

  2. Set viewport’s y to 0.

  3. Set viewport’s width to the pixel width of texture.

  4. Set viewport’s height to the pixelh eight of texture.

  5. Update viewport as follows:

    If layout is "stereo-left-right":
    Set viewport’s x to the pixel width of texture multiplied by offset and divided by num.
    Set viewport’s width to half the pixel width of subimage’s texture divided by num.
    Else if layout is "stereo-top-bottom":
    Set viewport’s y to the pixel height of texture multiplied by offset and divided by num.
    Set viewport’s height to the pixel height of subimage’s texture divided by num.
The getSubImage(XRCompositionLayer layer, XRFrame frame, optional XREye eye = "none") method creates a new XRWebGLSubImage.

When this method is invoked on an XRWebGLBinding binding, it MUST run the following steps:

  1. Initialize subimage as follows:

    If getSubImage() was called previously with the same binding, layer and eye, the user agent MAY:
    Let subimage be the same XRWebGLSubImage object as returned by an earlier call with the same arguments.
    Otherwise
    Let subimage be a new XRWebGLSubImage in the relevant realm of this.
    Let subimage’s viewport be a new XRViewport in the relevant realm of this.
  2. If layer’s type is XRProjectionLayer, throw a TypeError and abort these steps.

  3. If layer’s layout attribute is "default", throw a TypeError and abort these steps.

  4. Let index be 0.

  5. If layer’s layout attribute is "stereo":

    1. If eye is "none", throw a TypeError and abort these steps.

    2. If eye is "right", set index to 1.

  6. If validate the state of the XRWebGLSubImage creation function with layer and frame is false, throw an InvalidStateError and abort these steps.

  7. Initialize subimage’s colorTexture as follows:

    If the layer was created with a textureType of "texture-array"
    Initialize subimage’s colorTexture with the first element of the layer’s colorTextures array.
    Initialize subimage’s imageIndex with index.
    Otherwise
    Initialize subimage’s colorTexture with the element at offset index of the layer’s colorTextures array.
  8. Initialize subimage’s depthStencilTexture as follows:

    If the layer’s depthStencilTextures is an empty array.
    Continue to the next entry.
    Else if the layer was created with a textureType of "texture-array"
    Initialize subimage’s depthStencilTexture with the first element of layer’s depthStencilTextures array.
    Otherwise
    Initialize subimage’s depthStencilTexture with the element at offset index of the layer’s depthStencilTextures array.
  9. Set subimage’s textureWidth to the pixel width of subimage’s colorTexture.

  10. Set subimage’s textureHeight to the pixel height of subimage’s colorTexture.

  11. Let index be 0.

  12. If eye is "right", set index to 1.

  13. Let num be 1.

  14. If eye is not "none" and layer’s layout attribute is "stereo-left-right" or "stereo-top-bottom", set num to 2.

  15. Run initialize the viewport on subimage’s viewport with subimage’s colorTexture, layer’s layout, index and num.

  16. Queue a task to set needsRedraw to false.

  17. return subimage.

The getViewSubImage(XRProjectionLayer layer, XRView view) method creates a new XRWebGLSubImage.

When this method is invoked on an XRWebGLBinding binding, it MUST run the following steps:

  1. Initialize subimage as follows:

    If getViewSubImage() was called previously with the same binding, layer and view, the user agent MAY:
    Let subimage be the same XRWebGLSubImage object as returned by an earlier call with the same arguments.
    Otherwise
    Let subimage be a new XRWebGLSubImage in the relevant realm of this.
    Let subimage’s viewport be a new XRViewport in the relevant realm of this.
  2. Let frame be view’s frame.

  3. Let session be this session.

  4. If validate the state of the XRWebGLSubImage creation function with layer and frame is false, throw an InvalidStateError and abort these steps.

  5. If view’s active flag is false, throw an InvalidStateError and abort these steps.

  6. Initialize index as follows:

    If view is a secondary view from session’s list of views
    Let index be the offset of view’s view in session’s list of views excluding the primary views.
    Otherwise
    Let index be the offset of view’s view in session’s list of views excluding the secondary views.
  7. Initialize subimage’s colorTexture as follows:

    If view is a secondary view from session’s list of views
    Initialize subimage’s colorTexture with the element at offset index of the layer’s colorTextures for secondary views.
    Initialize subimage’s imageIndex with 0.
    Else if the layer was created with a textureType of "texture-array"
    Initialize subimage’s colorTexture with the first element of the layer’s colorTextures array.
    Initialize subimage’s imageIndex with index.
    Otherwise
    Initialize subimage’s colorTexture with the element at offset index of the layer’s colorTextures array.
    Initialize subimage’s imageIndex with 0.
  8. Initialize subimage’s depthStencilTexture as follows:

    If the layer’s depthStencilTextures is an empty array.
    Initialize subimage’s depthStencilTexture with null.
    Else if view is a secondary view from session’s list of views
    Initialize subimage’s colorTexture with the element at offset index of the layer’s depthStencilTextures for secondary views.
    Else if the layer was created with a textureType of "texture-array"
    Initialize subimage’s depthStencilTexture with the first element of layer’s depthStencilTextures array.
    Otherwise
    Initialize subimage’s depthStencilTexture with the element at offset index of the layer’s depthStencilTextures array.
  9. Set subimage’s textureWidth to the pixel width of subimage’s colorTexture.

  10. Set subimage’s textureHeight to the pixel height of subimage’s colorTexture.

  11. Run initialize the viewport on subimage’s viewport with subimage’s colorTexture, layer’s layout, index and the number of the session’s list of views.

  12. Set needsRedraw to false.

  13. return subimage

When an XRLayer is a member of the layers array, it MUST be presented to the immersive XR device immediately after an XR animation frame completes, but only if at least one of the following has occurred since the previous XR animation frame:

Before the opaque framebuffer or colorTexture texture are presented to the immersive XR device the user agent MUST ensure that all rendering operations have been flushed.

7. Video layer creation

7.1. XRMediaLayerInit

The XRMediaLayerInit dictionary represents a set of configurable values that describe how an XRCompositionLayer containing a video is initialized.
dictionary XRMediaLayerInit {
  required XRSpace space;
  XRLayerLayout layout = "mono";
  boolean invertStereo = false;
};

The space attribute defines the spatial relationship with the user’s physical environment.

The layout attribute defines the layout of the video in the XRCompositionLayer.

The invertStereo attribute defines if the natural location of each view in the video should be inverted.

7.2. XRMediaBinding

The XRMediaBinding object is used to create layers that display the content of an HTMLVideoElement.
interface XRMediaBinding {
  constructor(XRSession session);

  XRQuadLayer createQuadVideoLayer(HTMLVideoElement video,
                                   optional XRMediaLayerInit init = {});
  XRCylinderLayer createCylinderVideoLayer(HTMLVideoElement video,
                                           optional XRMediaLayerInit init = {});
  XREquirectLayer createEquirectVideoLayer(HTMLVideoElement video,
                                           optional XRMediaLayerInit init = {});
};

Each XRMediaBinding has an associated session, which is the XRSession it was created with.

NOTE: It is possible to create more than one XRMediaBinding. The lifetime of a layer is not tied to the lifetime of the XRMediaBinding that created it.

Each layer created through XRMediaBinding has an internal HTMLVideoElement media. If the layer is part of the session’s renderState, it will display the current frame of the video. The layer is update at the native framerate of the XR device or the video, whichever is less.

NOTE: only the video frames will be displayed in the layer. Video controls should be implemented by the author and must be drawn in another layer.

more clarification is needed on how the video is blitted to the layers.

When an XRCompositionLayer layer with a HTMLVideoElement media needs to be rendered, the user agent MUST run the following steps:
  1. Let usability be the result of checking the usability of media.

  2. If usability is bad, then fill the layer with opaque black and abort these steps.

  3. Fill the layer with the content of the media element.

add a better algorithm to describe the drawing.

The XRMediaBinding(XRSession session) constructor MUST perform the following steps when invoked:

  1. If session’s ended value is true, throw an InvalidStateError and abort these steps.

  2. If session is not an immersive session, throw an InvalidStateError and abort these steps.

  3. Let binding be a new XRMediaBindingin the relevant realm of session.

  4. Initialize binding’s session to session.

  5. Return binding.

The createQuadLayer(HTMLVideoElement video, optional XRMediaLayerInit init) method creates a new XRQuadLayer layer.

When this method is invoked, the user agent MUST run the following steps:

  1. If the device does not support the creation of an XRQuadLayer, throw an NotSupportedError and abort these steps.

  2. Let session be this session.

  3. If session’s ended value is true, throw InvalidStateError and abort these steps.

  4. Let layer be a new XRQuadLayer in the relevant realm of this.

  5. Run intialize a composition layer on layer with session.

  6. Initialize layer’s media to video.

  7. Initialize layer’s needsRedraw to false.

  8. Run initialize a quad layer with layer and init.

  9. Allocate and initialize resources compatible with session’s XR device, including GPU accessible memory buffers, as required to support the compositing of layer.

  10. If layer’s resources were unable to be created for any reason, throw an OperationError and abort these steps.

  11. return layer.

The createCylinderLayer(HTMLVideoElement video, optional XRMediaLayerInit init) method creates a new XRCylinderLayer layer.

When this method is invoked, the user agent MUST run the following steps:

  1. If the device does not support the creation of an XRCylinderLayer, throw an NotSupportedError and abort these steps.

  2. Let session be this session.

  3. If session’s ended value is true, throw InvalidStateError and abort these steps.

  4. Let layer be a new XRCylinderLayer in the relevant realm of this.

  5. Run intialize a composition layer on layer with session.

  6. Initialize layer’s media to video.

  7. Initialize layer’s needsRedraw to false.

  8. Run initialize a cylinder layer with layer and init.

  9. Allocate and initialize resources compatible with session’s XR device, including GPU accessible memory buffers, as required to support the compositing of layer.

  10. If layer’s resources were unable to be created for any reason, throw an OperationError and abort these steps.

  11. return layer.

The createEquirectLayer(HTMLVideoElement video, optional XRMediaLayerInit init) method creates a new XREquirectLayer layer.

When this method is invoked, the user agent MUST run the following steps:

  1. If the device does not support the creation of an XREquirectLayer, throw an NotSupportedError and abort these steps.

  2. Let session be this session.

  3. If session’s ended value is true, throw InvalidStateError and abort these steps.

  4. If init’s space is not an instance of type XRReferenceSpace, throw InvalidStateError and abort these steps.

  5. If init’s space has a type of "viewer", throw InvalidStateError and abort these steps.

  6. Let layer be a new XREquirectLayer in the relevant realm of this.

  7. Run intialize a composition layer on layer with session.

  8. Initialize layer’s media to video.

  9. Initialize layer’s needsRedraw to false.

  10. Run initialize a equirect layer with layer and init.

  11. Allocate and initialize resources compatible with session’s XR device, including GPU accessible memory buffers, as required to support the compositing of layer.

  12. If layer’s resources were unable to be created for any reason, throw an OperationError and abort these steps.

  13. return layer.

define how the XREquirectLayer's parameters affect the video display.

8. Events

8.1. XRLayerEvent

XRLayerEvent is fired to indicate changes to the state of an XRLayer.

[SecureContext, Exposed=Window]
interface XRLayerEvent : Event {
  constructor(DOMString type, XRLayerEventInit eventInitDict);
  [SameObject] readonly attribute XRLayer layer;
};

dictionary XRLayerEventInit : EventInit {
  required XRLayer layer;
};

The layer attribute indicates the XRLayer that generated the event.

8.2. Event Types

The user agent MUST provide the following new events. Registration for and firing of the events must follow the usual behavior of DOM4 Events.

The user agent MAY fire a redraw event on the XRLayer object when the underlying resources of a layer are lost or when the XR Compositor can no longer reproject the layer.

The author SHOULD redraw the content of the layer at the next XR animation frame. The event must be of type XRLayerEvent.

9. WebXR Device API Integration

9.1. XRRenderState changes

This module extends the XRRenderStateInit and XRRenderState interfaces with a new optional array layers containing instances of XRLayer.
[SecureContext, Exposed=Window] partial interface XRRenderState {
  [SameObject] readonly attribute FrozenArray<XRLayer> layers;
};

The layers attribute returns an array containing the instances of XRLayer that are displayed by the XR Compositor.

This module replaces the steps given by initialize the render state. Instead when an XRRenderState object state is created for an XRSession session, the user agent MUST initialize the render state by running the following steps:
  1. Initialize state by running the original steps to initialize the render state.

  2. Initialize state’s layers with a new empty array in the relevant realm of session.

9.2. updateRenderState changes

This module replaces the steps given by "update the pending layers state" from the WebXR specification. Instead when the user agent will update the pending layers state with XRSession session and XRRenderStateInit newState, it must run the following steps:

  1. If newState’s baseLayer is set:

    1. If the session was created with "layers" enabled, throw an NotSupportedError and abort these steps.

    2. If newState’s layers is not empty, throw a TypeError and abort these steps.

  2. If newState’s layers contains duplicate instances, throw an TypeError and abort these steps.

  3. For each layer in newState’s layers:

    1. If layer is an XRCompositionLayer and layer’s session is different from session, throw an TypeError and abort these steps.

    2. If layer is an XRWebGLLayer and layer’s session is different from session, throw an TypeError and abort these steps.

  4. Let activeState be session’s active render state.

  5. If session’s pending render state is null, set it to a copy of activeState.

  6. If newState’s layers array is set, set session’s pending render state's layers to newState’s layers.

9.3. XRCompositor changes

The XR Compositor MUST be extended so all XRLayer instances from the layers array are composited at the same time. All other requirements for WebXR MUST continue to apply.

If the XR Compositor is rendering to a view with an XREye of "none" and drawing an XRCompositionLayer which is NOT an XRProjectionLayer and does NOT have a layout of "mono", the XR Compositor MUST render that layer as if the view had an XREye of "left".

NOTE: This means that the side for the right eye of the layer is ignored. This enables authors to use the same assets for stereoscopic and monoscopic devices.

9.4. XRView changes

Each view MUST define a recommended WebGL texture resolution which represents a best estimate of the WebGL texture resolution large enough to contain the view.

9.5. Animation frames changes

This module replaces the steps given by "check the layers state" from the WebXR specification. Instead to check the layers state with renderState state, the user agent MUST run the following steps:
  1. If state’s baseLayer is not null, return true.

  2. If state’s layers is not empty, return true.

  3. return false.

10. Security and Privacy Considerations

10.1. Timing of the composition

Composition timing MUST be independent of the content that is rendered. Moreover, content in a layer MUST not be observable in other layers.

If possible, composition of layers should happen outside the browser to reduce risk of timing attacks or other security vulnerabilities.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[DOM]
Anne van Kesteren. DOM Standard. Living Standard. URL: https://dom.spec.whatwg.org/
[GEOMETRY-1]
Simon Pieters; Chris Harrelson. Geometry Interfaces Module Level 1. 4 December 2018. CR. URL: https://www.w3.org/TR/geometry-1/
[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[WEBGL-2]
Dean Jackson; Jeff Gilbert. WebGL 2.0 Specification. 12 August 2017. URL: https://www.khronos.org/registry/webgl/specs/latest/2.0/
[WebIDL]
Boris Zbarsky. Web IDL. 15 December 2016. ED. URL: https://heycam.github.io/webidl/
[WEBXR]
Brandon Jones; Nell Waliczek. WebXR Device API. 10 October 2019. WD. URL: https://www.w3.org/TR/webxr/
[WEBXR-AR-MODULE-1]
Brandon Jones; Nell Waliczek; Manish Goregaokar. WebXR Augmented Reality Module - Level 1. 10 October 2019. WD. URL: https://www.w3.org/TR/webxr-ar-module-1/

Informative References

[PERMISSIONS-REQUEST]
Requesting Permissions. cg-draft. URL: https://wicg.github.io/permissions-request/

IDL Index

enum XRLayerLayout {
  "default",
  "mono",
  "stereo",
  "stereo-left-right",
  "stereo-top-bottom"
};

interface XRCompositionLayer : XRLayer {
  readonly attribute XRLayerLayout layout;

  attribute boolean blendTextureSourceAlpha;
  attribute boolean? chromaticAberrationCorrection;
  attribute float? fixedFoveation;

  readonly attribute boolean needsRedraw;

  void destroy();
};

interface XRProjectionLayer : XRCompositionLayer {
  readonly attribute boolean ignoreDepthValues;
};

interface XRQuadLayer : XRCompositionLayer {
  attribute XRSpace space;
  attribute XRRigidTransform transform;

  attribute float width;
  attribute float height;

  // Events
  attribute EventHandler onredraw;
};

interface XRCylinderLayer : XRCompositionLayer {
  attribute XRSpace space;
  attribute XRRigidTransform transform;

  attribute float radius;
  attribute float centralAngle;
  attribute float aspectRatio;

  // Events
  attribute EventHandler onredraw;
};

interface XREquirectLayer : XRCompositionLayer {
  attribute XRSpace space;
  attribute XRRigidTransform transform;

  attribute float radius;
  attribute float centralHorizontalAngle;
  attribute float upperVerticalAngle;
  attribute float lowerVerticalAngle;

  // Events
  attribute EventHandler onredraw;
};

interface XRCubeLayer : XRCompositionLayer {
  attribute XRSpace space;
  attribute DOMPointReadOnly orientation;

  // Events
  attribute EventHandler onredraw;
};

interface XRSubImage {
  [SameObject] readonly attribute XRViewport viewport;
};

interface XRWebGLSubImage : XRSubImage {
  [SameObject] readonly attribute WebGLTexture colorTexture;
  [SameObject] readonly attribute WebGLTexture? depthStencilTexture;
  readonly attribute unsigned long? imageIndex;
  readonly attribute unsigned long textureWidth;
  readonly attribute unsigned long textureHeight;
};

enum XRTextureType {
  "texture",
  "texture-array"
};

dictionary XRProjectionLayerInit {
  boolean depth = true;
  boolean stencil = false;
  boolean alpha = true;
  double scaleFactor = 1.0;
};

dictionary XRLayerInit {
  required XRSpace space;
  required unsigned long viewPixelWidth;
  required unsigned long viewPixelHeight;
  XRLayerLayout layout = "mono";
  boolean depth = false;
  boolean stencil = false;
  boolean alpha = true;
};

dictionary XRQuadLayerInit : XRLayerInit {
  XRRigidTransform? transform;
  float width = 1.0;
  float height = 1.0;
  boolean isStatic = false;
};

dictionary XRCylinderLayerInit : XRLayerInit {
  XRRigidTransform? transform;
  float radius = 2.0;
  float centralAngle = 0.78539;
  float aspectRatio = 2.0;
  boolean isStatic = false;
};

dictionary XREquirectLayerInit : XRLayerInit {
  XRRigidTransform? transform;
  float radius = 0;
  float centralHorizontalAngle = 6.28318;
  float upperVerticalAngle = 1.570795;
  float lowerVerticalAngle = -1.570795;
  boolean isStatic = false;
};

dictionary XRCubeLayerInit : XRLayerInit {
  DOMPointReadOnly? orientation;
  boolean isStatic = false;
};

interface XRWebGLBinding {
  constructor(XRSession session, XRWebGLRenderingContext context);

  readonly attribute double nativeProjectionScaleFactor;

  XRProjectionLayer createProjectionLayer(XRTextureType textureType,
                                          optional XRProjectionLayerInit init);
  XRQuadLayer createQuadLayer(XRTextureType textureType,
                              optional XRQuadLayerInit init);
  XRCylinderLayer createCylinderLayer(XRTextureType textureType,
                                      optional XRCylinderLayerInit init);
  XREquirectLayer createEquirectLayer(XRTextureType textureType,
                                      optional XREquirectLayerInit init);
  XRCubeLayer createCubeLayer(optional XRCubeLayerInit init);

  XRWebGLSubImage getSubImage(XRCompositionLayer layer, XRFrame frame, optional XREye eye = "none");
  XRWebGLSubImage getViewSubImage(XRProjectionLayer layer, XRView view);
};

dictionary XRMediaLayerInit {
  required XRSpace space;
  XRLayerLayout layout = "mono";
  boolean invertStereo = false;
};

interface XRMediaBinding {
  constructor(XRSession session);

  XRQuadLayer createQuadVideoLayer(HTMLVideoElement video,
                                   optional XRMediaLayerInit init = {});
  XRCylinderLayer createCylinderVideoLayer(HTMLVideoElement video,
                                           optional XRMediaLayerInit init = {});
  XREquirectLayer createEquirectVideoLayer(HTMLVideoElement video,
                                           optional XRMediaLayerInit init = {});
};


[SecureContext, Exposed=Window]
interface XRLayerEvent : Event {
  constructor(DOMString type, XRLayerEventInit eventInitDict);
  [SameObject] readonly attribute XRLayer layer;
};

dictionary XRLayerEventInit : EventInit {
  required XRLayer layer;
};

[SecureContext, Exposed=Window] partial interface XRRenderState {
  [SameObject] readonly attribute FrozenArray<XRLayer> layers;
};

Issues Index

how to do hit testing? Should it be done by the UA?
this section needs clarification
this section needs clarification
the init dictionaries shouldn’t be optional. This is bikeshed issue 1566.
special case UA behavior if the size causes the layout to change (ie if the requested width exceeds a limit with "stereo-left-right")
the scaleFactor needs to be recalculated for the secondary views.
define "opaque" WebGLTexture
Define how cubemap sizes are determined.
How should space be handled. Can you walk to the edge of a cubemap?
determine the initial state of orientation.
more clarification is needed on how the video is blitted to the layers.
add a better algorithm to describe the drawing.
define how the XREquirectLayer's parameters affect the video display.