luma.gl simplifies the usage of WebGL parameters by providing a unified API for setting and getting values. Any GL parameter can be queried or set using getParameters and setParameters (no need to keep track of what underlying WebGL calls are required), and luma.gl also provide setting names that allow the normal WebGL setter functions (like gl.blendEquation or gl.clearColor) to be specified as keys in a setParameters call.
In addition, state queries are done towards cached values and are thus much faster than working directly with the WebGL API, where synchronous WebGL queries can be a performance bottleneck.
The following functions are provided:
getParameters - Returns the values of some or all GL context parameters
setParameters - Sets a the value(s) of the specified GL context parameters
resetParameters - Resets all gl context parameters to default values
Usage
Set a global parameter value using a WebGL GLenum
const value =setParameters(gl,{[gl.DEPTH_TEST]:true});
Set a global parameter value using a luma.gl setting function name
const value =setParameters(gl,{depthTest:true});
Get all gl parameter values (values will be an object map keyed with parameter names)
const values =getParameters(gl);
Set parameters temporarily for a function call (automatically restoring them after the call)
const returnValue =withParameters(gl,{depthTest:true},()={// execute code with new parameters temporarily applied
program.draw(...);// ...// parameters will be restored even the function throws an exceptionif(...){thrownewError('Exception after setting parameters');}// Return value of the function will be returned from `withParameters`returntrue;});// previous parameters are restored here
Functions
getParameters
Gets the values of a gl context parameter.
getParameters(gl, values);
gl {WebGLRenderingContext} - context
values= {Object | GLenum[] | null} - parameters, either as keys in object or elements of array. Defaults to all parameters.
Returns {Object} - object with keys and values corresponding to supplied parameter names and the current values of those parameters.
setParameters
Sets a number of parameters.
setParameters(gl,{key: value,...})
gl {WebGLRenderingContext} - context
key {String} - parameter names, (, either )luma.gl setting name or a GL parameter constants
value {} - parameter value
Returns {} - "normalized" parameter value after assignment
Note:
If both luma.gl setting names and GL parameter constants representing the same value are submitted the results are undefined.
value may be "normalized" (in case a short form is supported). In that case the normalized value is returned.
resetParameters
resetParameters(gl);
Resets all gl context parameters to default values.
gl {WebGLRenderingContext} - context
Returns no value.
Note that technically, resetting context parameters does not fully reset the context, as buffer binding, z buffer values etc are not reset.
withParameters
Executes a function after temporarily setting the parameters. Will restore the parameters to their previously value after the completion of the function, even if the function exits with an exception.
withParameters(gl,{...params}, func);
gl {WebGLRenderingContext} - context
params {Object} - any parameter names accepted by setParameters
// Set viewport to maximum supported sizeconst lineWidthRange =getLimits(gl)[GL.ALIASED_LINE_WIDTH_RANGE];setState(gl,{lineWidth: lineWidthRange[1]});
Note: Line widths will be clamped to [1, GL.ALIASED_LINE_WIDTH_RANGE]. This is different from gl.lineWidth which generates errors on lineWidth 0.
Caution: line aliasing is driver dependent and GL.LINES may not give desired results.
PolygonOffset
Add small offset to fragment depth values (by factor × DZ + r × units)
Useful for rendering hidden-line images, for applying decals to surfaces,
and for rendering solids with highlighted edges.
Sets bit mask enabling writing of individual bits in the stencil planes
GL.STENCIL_BACK_WRITEMASK
GLuint
0xFFFFFFFF
Sets bit mask enabling writing of individual bits in the stencil planes
GL.STENCIL_FUNC
GLenum
GL.ALWAYS
GL.STENCIL_REF
GLint
0
GL.STENCIL_VALUE_MASK
GLuint
0xFFFFFFFF
Sets bit mask
GL.STENCIL_BACK_FUNC
GLenum
GL.ALWAYS
GL.STENCIL_BACK_REF
GLint
0
GL.STENCIL_BACK_VALUE_MASK
GLuint
0xFFFFFFFF
Sets bit mask enabling writing of individual bits in the stencil planes
GL.STENCIL_FAIL
GLenum
GL.KEEP
stencil test fail action
GL.STENCIL_PASS_DEPTH_FAIL
GLenum
GL.KEEP
depth test fail action
GL.STENCIL_PASS_DEPTH_PASS
GLenum
GL.KEEP
depth test pass action
GL.STENCIL_BACK_FAIL
GLenum
GL.KEEP
stencil test fail action, back
GL.STENCIL_BACK_PASS_DEPTH_FAIL
GLenum
GL.KEEP
depth test fail action, back
GL.STENCIL_BACK_PASS_DEPTH_PASS
GLenum
GL.KEEP
depth test pass action, back
Stencil Test Functions
Values for GL.STENCIL_TEST
Value
Description
GL.NEVER
Never pass
GL.LESS
Pass if (ref & mask) < (stencil & mask)
GL.EQUAL
Pass if (ref & mask) = (stencil & mask)
GL.LEQUAL
Pass if (ref & mask) <= (stencil & mask)
GL.GREATER
Pass if (ref & mask) > (stencil & mask)
GL.NOTEQUAL
Pass if (ref & mask) != (stencil & mask)
GL.GEQUAL
Pass if (ref & mask) >= (stencil & mask)
GL.ALWAYS
Always pass
Stencil Operations
Value
Description
GL.KEEP
Keeps the current value
GL.ZERO
Sets the stencil buffer value to 0
GL.REPLACE
Sets the stencil buffer value to the reference value as specified by stencilFunc
GL.INCR
Increments the current stencil buffer value. Clamps to the maximum representable unsigned value
GL.INCR_WRAP
Increments the current stencil buffer value. Wraps to zero when incrementing the maximum representable unsigned value
GL.DECR
Decrements current stencil buffer value. Clamps to 0
GL.DECR_WRAP
Decrements current stencil buffer value, wraps to maximum unsigned value when decrementing 0
GL.INVERT
Inverts the current stencil buffer value bitwise
Action when the stencil test fails, front and back.
stencil test fail action,
depth test fail action,
pass action
Viewport
Specifies the transformation from normalized device coordinates to
window/framebuffer coordinates. The maximum supported value, is defined by the
GL.MAX_VIEWPORT_DIMS limit.
// Set viewport to maximum supported sizeconst maxViewport =getLimits(gl)[GL.MAX_VIEWPORT_DIMS];setState(gl,{viewport:[0,0, maxViewport[0], maxViewport[1]]});
Pixel Pack/Unpack Modes
Specifies how bitmaps are written to and read from memory
Parameter
Type
Default
Description
GL.PACK_ALIGNMENT
GLint
4
Byte alignment of pixel row data in memory (1,2,4,8 bytes) when storing data
GL.UNPACK_ALIGNMENT
GLint
4
Byte alignment of pixel row data in memory (1,2,4,8 bytes) when reading data
GL.UNPACK_FLIP_Y_WEBGL
GLboolean
false
Flip source data along its vertical axis
GL.UNPACK_PREMULTIPLY_ALPHA_WEBGL
GLboolean
false
Multiplies the alpha channel into the other color channels
GL.UNPACK_COLORSPACE_CONVERSION_WEBGL
GLenum
GL.BROWSER_DEFAULT_WEBGL
Use default or no color space conversion.
Pixel Pack/Unpack Modes WebGL 2
Specifies how bitmaps are written to and read from memory
Parameter
Type
Default
Description
GL.PACK_ROW_LENGTH
GLint
0
Number of pixels in a row
GL.PACK_SKIP_PIXELS
GLint
0
Number of pixels skipped before the first pixel is written into memory
GL.PACK_SKIP_ROWS
GLint
0
Number of rows of pixels skipped before first pixel is written to memory
GL.UNPACK_ROW_LENGTH
GLint
0
Number of pixels in a row.
GL.UNPACK_IMAGE_HEIGHT
GLint
0
Image height used for reading pixel data from memory
GL.UNPACK_SKIP_PIXELS
GLint
0
Number of pixel images skipped before first pixel is read from memory
GL.UNPACK_SKIP_ROWS
GLint
0
Number of rows of pixels skipped before first pixel is read from memory
GL.UNPACK_SKIP_IMAGES
GLint
0
Number of pixel images skipped before first pixel is read from memory
Remarks
WebGL State Management can be quite complicated.
A large part of the WebGL API is devoted to parameters. When reading, querying individual values using GL constants is the norm, and when writing, special purpose functions are provided for most parameters. luma.gl supports both forms for both reading and writing parameters.
Reading values from WebGL can be very slow if it requires a GPU roundtrip. To get around this, luma.gl reads values once, caches them and tracks them as they are changed through luma functions. The cached values can get out of sync if the context is shared outside of luma.gl.
luma.gl's state management enables "conflict-free" programming, so that even when setting global state, one part of the code does not need to worry about whether other parts are changing the global state.
Note that to fully support the conflict-free model and detect changes done e.g. in other WebGL libraries, luma.gl needs to hook into the WebGL context to track state changes.