The graphics device manages the underlying graphics context. It is responsible for submitting render state changes and graphics primitives to the hardware. A graphics device is tied to a specific canvas HTML element. It is valid to have more than one canvas element per page and create a new graphics device against each.

Hierarchy (view full)

Constructors

  • Creates a new WebglGraphicsDevice instance.

    Parameters

    • canvas: HTMLCanvasElement

      The canvas to which the graphics device will render.

    • Optionaloptions: {
          alpha: boolean;
          antialias: boolean;
          depth: boolean;
          desynchronized: boolean;
          failIfMajorPerformanceCaveat: boolean;
          gl: WebGLRenderingContext | WebGL2RenderingContext;
          powerPreference: "default" | "high-performance" | "low-power";
          preferWebGl2: boolean;
          premultipliedAlpha: boolean;
          preserveDrawingBuffer: boolean;
          stencil: boolean;
          xrCompatible: boolean;
      } = {}

      Options passed when creating the WebGL context.

      • alpha: boolean

        Boolean that indicates if the canvas contains an alpha buffer. Defaults to true.

      • antialias: boolean

        Boolean that indicates whether or not to perform anti-aliasing if possible. Defaults to true.

      • depth: boolean

        Boolean that indicates that the drawing buffer is requested to have a depth buffer of at least 16 bits. Defaults to true.

      • desynchronized: boolean

        Boolean that hints the user agent to reduce the latency by desynchronizing the canvas paint cycle from the event loop. Defaults to false.

      • failIfMajorPerformanceCaveat: boolean

        Boolean that indicates if a context will be created if the system performance is low or if no hardware GPU is available. Defaults to false.

      • gl: WebGLRenderingContext | WebGL2RenderingContext

        The rendering context to use. If not specified, a new context will be created.

      • powerPreference: "default" | "high-performance" | "low-power"

        A hint to the user agent indicating what configuration of GPU is suitable for the WebGL context. Possible values are:

        • 'default': Let the user agent decide which GPU configuration is most suitable. This is the default value.
        • 'high-performance': Prioritizes rendering performance over power consumption.
        • 'low-power': Prioritizes power saving over rendering performance.

        Defaults to 'default'.

      • preferWebGl2: boolean

        Boolean that indicates if a WebGl2 context should be preferred. Defaults to true.

      • premultipliedAlpha: boolean

        Boolean that indicates that the page compositor will assume the drawing buffer contains colors with pre-multiplied alpha. Defaults to true.

      • preserveDrawingBuffer: boolean

        If the value is true the buffers will not be cleared and will preserve their values until cleared or overwritten by the author. Defaults to false.

      • stencil: boolean

        Boolean that indicates that the drawing buffer is requested to have a stencil buffer of at least 8 bits. Defaults to true.

      • xrCompatible: boolean

        Boolean that hints to the user agent to use a compatible graphics adapter for an immersive XR device.

    Returns WebglGraphicsDevice

Properties

The canvas DOM element that provides the underlying WebGL context used by the graphics device.

gpuProfiler: GpuProfiler

The GPU profiler.

insideRenderPass: boolean = false
isWebGPU: boolean = false

True if the deviceType is WebGPU

maxAnisotropy: number

The maximum supported texture anisotropy setting.

maxColorAttachments: number = 1

The maximum supported number of color buffers attached to a render target.

maxCubeMapSize: number

The maximum supported dimension of a cube map.

maxTextureSize: number

The maximum supported dimension of a texture.

maxVolumeSize: number

The maximum supported dimension of a 3D texture (any axis).

precision: string

The highest shader precision supported by this graphics device. Can be 'hiphp', 'mediump' or 'lowp'.

samples: number

The number of hardware anti-aliasing samples used by the frame buffer.

scope: ScopeSpace

The scope namespace for shader attributes and variables.

supportsCompute: boolean = false

True if the device supports compute shaders.

supportsInstancing: boolean

True if hardware instancing is supported.

supportsMrt: boolean = false

True if Multiple Render Targets feature is supported. This refers to the ability to render to multiple color textures with a single draw call.

supportsStorageTextureRead: boolean = false

True if the device can read from StorageTexture in the compute shader. By default, the storage texture can be only used with the write operation. When a shader uses this feature, it's recommended to use a requires directive to signal the potential for non-portability at the top of the WGSL shader code:

requires readonly_and_readwrite_storage_textures;
supportsVolumeTextures: boolean = false

True if the device supports volume textures.

textureFloatFilterable: boolean = false

True if filtering can be applied when sampling float textures.

textureHalfFloatFilterable: boolean = false

True if filtering can be applied when sampling 16-bit float textures.

Accessors

Methods

  • Clears the frame buffer of the currently set render target.

    Parameters

    • Optionaloptions: {
          color: number[];
          depth: number;
          flags: number;
          stencil: number;
      }

      Optional options object that controls the behavior of the clear operation defined as follows:

      • color: number[]

        The color to clear the color buffer to in the range 0 to 1 for each component.

      • depth: number

        The depth value to clear the depth buffer to in the range 0 to 1. Defaults to 1.

      • flags: number

        The buffers to clear (the types being color, depth and stencil). Can be any bitwise combination of:

      • stencil: number

        The stencil value to clear the stencil buffer to. Defaults to 0.

    Returns void

    // Clear color buffer to black and depth buffer to 1
    device.clear();

    // Clear just the color buffer to red
    device.clear({
    color: [1, 0, 0, 1],
    flags: pc.CLEARFLAG_COLOR
    });

    // Clear color buffer to yellow and depth to 1.0
    device.clear({
    color: [1, 1, 0, 1],
    depth: 1,
    flags: pc.CLEARFLAG_COLOR | pc.CLEARFLAG_DEPTH
    });
  • Copies source render target into destination render target. Mostly used by post-effects.

    Parameters

    • Optionalsource: RenderTarget

      The source render target. Defaults to frame buffer.

    • Optionaldest: RenderTarget

      The destination render target. Defaults to frame buffer.

    • Optionalcolor: boolean

      If true, will copy the color buffer. Defaults to false.

    • Optionaldepth: boolean

      If true, will copy the depth buffer. Defaults to false.

    Returns boolean

    True if the copy was successful, false otherwise.

  • Submits a graphical primitive to the hardware for immediate rendering.

    Parameters

    • primitive: {
          base: number;
          count: number;
          indexed: boolean;
          type: number;
      }

      Primitive object describing how to submit current vertex/index buffers.

    • OptionalnumInstances: number

      The number of instances to render when using ANGLE_instanced_arrays. Defaults to 1.

    • OptionalkeepBuffers: boolean

      Optionally keep the current set of vertex / index buffers / VAO. This is used when rendering of multiple views, for example under WebXR.

    Returns void

    // Render a single, unindexed triangle
    device.draw({
    type: pc.PRIMITIVE_TRIANGLES,
    base: 0,
    count: 3,
    indexed: false
    });
  • Fire an event, all additional arguments are passed on to the event listener.

    Parameters

    • name: string

      Name of event to fire.

    • Optionalarg1: any

      First argument that is passed to the event handler.

    • Optionalarg2: any

      Second argument that is passed to the event handler.

    • Optionalarg3: any

      Third argument that is passed to the event handler.

    • Optionalarg4: any

      Fourth argument that is passed to the event handler.

    • Optionalarg5: any

      Fifth argument that is passed to the event handler.

    • Optionalarg6: any

      Sixth argument that is passed to the event handler.

    • Optionalarg7: any

      Seventh argument that is passed to the event handler.

    • Optionalarg8: any

      Eighth argument that is passed to the event handler.

    Returns EventHandler

    Self for chaining.

    obj.fire('test', 'This is the message');
    
  • Test if there are any handlers bound to an event name.

    Parameters

    • name: string

      The name of the event to test.

    Returns boolean

    True if the object has handlers bound to the specified event name.

    obj.on('test', function () { }); // bind an event to 'test'
    obj.hasEvent('test'); // returns true
    obj.hasEvent('hello'); // returns false
  • Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.

    Parameters

    • Optionalname: string

      Name of the event to unbind.

    • Optionalcallback: HandleEventCallback

      Function to be unbound.

    • Optionalscope: object

      Scope that was used as the this when the event is fired.

    Returns EventHandler

    Self for chaining.

    const handler = function () {
    };
    obj.on('test', handler);

    obj.off(); // Removes all events
    obj.off('test'); // Removes all events called 'test'
    obj.off('test', handler); // Removes all handler functions, called 'test'
    obj.off('test', handler, this); // Removes all handler functions, called 'test' with scope this
  • Attach an event handler to an event.

    Parameters

    • name: string

      Name of the event to bind the callback to.

    • callback: HandleEventCallback

      Function that is called when event is fired. Note the callback is limited to 8 arguments.

    • Optionalscope: object = ...

      Object to use as 'this' when the event is fired, defaults to current this.

    Returns EventHandle

    Can be used for removing event in the future.

    obj.on('test', function (a, b) {
    console.log(a + b);
    });
    obj.fire('test', 1, 2); // prints 3 to the console
    const evt = obj.on('test', function (a, b) {
    console.log(a + b);
    });
    // some time later
    evt.off();
  • Attach an event handler to an event. This handler will be removed after being fired once.

    Parameters

    • name: string

      Name of the event to bind the callback to.

    • callback: HandleEventCallback

      Function that is called when event is fired. Note the callback is limited to 8 arguments.

    • Optionalscope: object = ...

      Object to use as 'this' when the event is fired, defaults to current this.

    Returns EventHandle

    • can be used for removing event in the future.
    obj.once('test', function (a, b) {
    console.log(a + b);
    });
    obj.fire('test', 1, 2); // prints 3 to the console
    obj.fire('test', 1, 2); // not going to get handled
  • Sets the specified render target on the device. If null is passed as a parameter, the back buffer becomes the current target for all rendering operations.

    Parameters

    Returns void

    // Set a render target to receive all rendering output
    device.setRenderTarget(renderTarget);

    // Set the back buffer to receive all rendering output
    device.setRenderTarget(null);
  • Set the active scissor rectangle on the specified device.

    Parameters

    • x: number

      The pixel space x-coordinate of the bottom left corner of the scissor rectangle.

    • y: number

      The pixel space y-coordinate of the bottom left corner of the scissor rectangle.

    • w: number

      The width of the scissor rectangle in pixels.

    • h: number

      The height of the scissor rectangle in pixels.

    Returns void

  • Sets the active shader to be used during subsequent draw calls.

    Parameters

    • shader: Shader

      The shader to assign to the device.

    • asyncCompile: boolean = false

      If true, rendering will be skipped until the shader is compiled, otherwise the rendering will wait for the shader compilation to finish. Defaults to false.

    Returns void

  • Set the active rectangle for rendering on the specified device.

    Parameters

    • x: number

      The pixel space x-coordinate of the bottom left corner of the viewport.

    • y: number

      The pixel space y-coordinate of the bottom left corner of the viewport.

    • w: number

      The width of the viewport in pixels.

    • h: number

      The height of the viewport in pixels.

    Returns void