Home

Awesome

pex-context

npm version stability-stable npm minzipped size dependencies types Conventional Commits styled with prettier linted with eslint license

Modern WebGL state wrapper for PEX: allocate GPU resources (textures, buffers), setup state pipelines and passes, and combine them into commands.

Installation

npm install pex-context

Usage

import createContext from "pex-context";

import { mat4 } from "pex-math";
import { cube } from "primitive-geometry";

const W = 640;
const H = 480;
const ctx = createContext({ width: W, height: H });

const geom = cube();

const clearCmd = {
  pass: ctx.pass({
    clearColor: [0.2, 0.2, 0.2, 1],
    clearDepth: 1,
  }),
};

const drawCmd = {
  pipeline: ctx.pipeline({
    depthTest: true,
    vert: /* glsl */ `
attribute vec3 aPosition;
attribute vec3 aNormal;

uniform mat4 uProjectionMatrix;
uniform mat4 uViewMatrix;

varying vec4 vColor;

void main () {
  vColor = vec4(aNormal * 0.5 + 0.5, 1.0);

  gl_Position = uProjectionMatrix * uViewMatrix * vec4(aPosition, 1.0);
}
`,
    frag: /* glsl */ `
precision highp float;

varying vec4 vColor;

void main() {
  gl_FragColor = vColor;
}
`,
  }),
  attributes: {
    aPosition: ctx.vertexBuffer(geom.positions),
    aNormal: ctx.vertexBuffer(geom.normals),
  },
  indices: ctx.indexBuffer(geom.cells),
  uniforms: {
    uProjectionMatrix: mat4.perspective(
      mat4.create(),
      Math.PI / 4,
      W / H,
      0.1,
      100,
    ),
    uViewMatrix: mat4.lookAt(mat4.create(), [2, 2, 5], [0, 0, 0], [0, 1, 0]),
  },
};

ctx.frame(() => {
  ctx.submit(clearCmd);
  ctx.submit(drawCmd);
});

API

<!-- api-start -->

Objects

<dl> <dt><a href="#ctx">ctx</a> : <code>object</code></dt> <dd></dd> </dl>

Functions

<dl> <dt><a href="#createContext">createContext([options])</a> ⇒ <code><a href="#ctx">ctx</a></code></dt> <dd><p>Create a context object</p> </dd> </dl>

Typedefs

<dl> <dt><a href="#BufferOptions">BufferOptions</a> : <code><a href="#PexResource">PexResource</a></code></dt> <dd></dd> <dt><a href="#Attachment">Attachment</a> : <code>object</code></dt> <dd></dd> <dt><a href="#PassOptions">PassOptions</a> : <code><a href="#PexResource">PexResource</a></code></dt> <dd></dd> <dt><a href="#PipelineOptions">PipelineOptions</a> : <code><a href="#PexResource">PexResource</a></code></dt> <dd></dd> <dt><a href="#QueryOptions">QueryOptions</a> : <code><a href="#PexResource">PexResource</a></code></dt> <dd></dd> <dt><a href="#PexQuery">PexQuery</a> : <code><a href="#QueryOptions">QueryOptions</a></code></dt> <dd></dd> <dt><a href="#RenderbufferOptions">RenderbufferOptions</a> : <code><a href="#PexResource">PexResource</a></code></dt> <dd></dd> <dt><a href="#TextureOptionsData">TextureOptionsData</a> : <code>HTMLImageElement</code> | <code>HTMLVideoElement</code> | <code>HTMLCanvasElement</code></dt> <dd></dd> <dt><a href="#TextureTarget">TextureTarget</a> : <code>WebGLRenderingContext.TEXTURE_2D</code> | <code>WebGLRenderingContext.TEXTURE_CUBE_MAP</code></dt> <dd></dd> <dt><a href="#TextureOptions">TextureOptions</a> : <code><a href="#PexResource">PexResource</a></code></dt> <dd></dd> <dt><a href="#TextureCubeOptions">TextureCubeOptions</a> : <code><a href="#PexResource">PexResource</a></code></dt> <dd></dd> <dt><a href="#VertexArrayOptions">VertexArrayOptions</a> : <code><a href="#PexResource">PexResource</a></code></dt> <dd></dd> <dt><a href="#PexContextOptions">PexContextOptions</a> : <code>object</code></dt> <dd></dd> <dt><a href="#PexResource">PexResource</a> : <code>object</code></dt> <dd><p>All resources are plain js object and once constructed their properties can be accessed directly. Please note those props are read only. To set new values or upload new data to GPU see <a href="#ctx.update">updating resources</a>.</p> </dd> <dt><a href="#PexTexture2D">PexTexture2D</a> : <code>object</code></dt> <dd></dd> <dt><a href="#PexAttribute">PexAttribute</a> : <code>object</code></dt> <dd></dd> <dt><a href="#PexCommand">PexCommand</a> : <code>object</code></dt> <dd></dd> <dt><a href="#MultiDrawOptions">MultiDrawOptions</a> : <code>object</code></dt> <dd></dd> <dt><a href="#PexContextSetOptions">PexContextSetOptions</a> : <code>object</code></dt> <dd></dd> <dt><a href="#Viewport">Viewport</a> : <code>Array.&lt;number&gt;</code></dt> <dd><p>[x, y, w, h]</p> </dd> <dt><a href="#Color">Color</a> : <code>Array.&lt;number&gt;</code></dt> <dd><p>[r, g, b, a]</p> </dd> <dt><a href="#TypedArray">TypedArray</a> : <code>Int8Array</code> | <code>Uint8Array</code> | <code>Uint8ClampedArray</code> | <code>Int16Array</code> | <code>Uint16Array</code> | <code>Int32Array</code> | <code>Uint32Array</code> | <code>Float32Array</code> | <code>Float64Array</code> | <code>BigInt64Array</code> | <code>BigUint64Array</code></dt> <dd></dd> </dl>

<a name="ctx"></a>

ctx : <code>object</code>

Kind: global namespace

<a name="ctx.gl"></a>

ctx.gl

The RenderingContext returned by pex-gl

Kind: static property of <code>ctx</code> <a name="ctx.capabilities"></a>

ctx.capabilities

Max capabilities and extensions availability. See Capabilities Table.

Kind: static property of <code>ctx</code> <a name="ctx.width"></a>

ctx.width ⇒ <code>number</code>

Getter for gl.drawingBufferWidth

Kind: static property of <code>ctx</code> <a name="ctx.height"></a>

ctx.height ⇒ <code>number</code>

Getter for gl.drawingBufferHeight

Kind: static property of <code>ctx</code> <a name="ctx.BlendFactor"></a>

ctx.BlendFactor

Kind: static enum of <code>ctx</code> Properties

NameDefault
One<code>gl.ONE</code>
Zero<code>gl.ZERO</code>
SrcAlpha<code>gl.SRC_ALPHA</code>
OneMinusSrcAlpha<code>gl.ONE_MINUS_SRC_ALPHA</code>
DstAlpha<code>gl.DST_ALPHA</code>
OneMinusDstAlpha<code>gl.ONE_MINUS_DST_ALPHA</code>
SrcColor<code>gl.SRC_COLOR</code>
OneMinusSrcColor<code>gl.ONE_MINUS_SRC_COLOR</code>
DstColor<code>gl.DST_COLOR</code>
OneMinusDstColor<code>gl.ONE_MINUS_DST_COLOR</code>

<a name="ctx.CubemapFace"></a>

ctx.CubemapFace

Kind: static enum of <code>ctx</code> Properties

NameDefault
PositiveX<code>gl.TEXTURE_CUBE_MAP_POSITIVE_X</code>
NegativeX<code>gl.TEXTURE_CUBE_MAP_NEGATIVE_X</code>
PositiveY<code>gl.TEXTURE_CUBE_MAP_POSITIVE_Y</code>
NegativeY<code>gl.TEXTURE_CUBE_MAP_NEGATIVE_Y</code>
PositiveZ<code>gl.TEXTURE_CUBE_MAP_POSITIVE_Z</code>
NegativeZ<code>gl.TEXTURE_CUBE_MAP_NEGATIVE_Z</code>

<a name="ctx.DepthFunc"></a>

ctx.DepthFunc

Kind: static enum of <code>ctx</code> Properties

NameDefault
Never<code>gl.NEVER</code>
Less<code>gl.LESS</code>
Equal<code>gl.EQUAL</code>
LessEqual<code>gl.LEQUAL</code>
Greater<code>gl.GREATER</code>
NotEqual<code>gl.NOTEQUAL</code>
GreaterEqual<code>gl.GEQUAL</code>
Always<code>gl.ALWAYS</code>

<a name="ctx.Face"></a>

ctx.Face

Kind: static enum of <code>ctx</code> Properties

NameDefault
Front<code>gl.FRONT</code>
Back<code>gl.BACK</code>
FrontAndBack<code>gl.FRONT_AND_BACK</code>

<a name="ctx.Filter"></a>

ctx.Filter

Kind: static enum of <code>ctx</code> Properties

NameDefault
Nearest<code>gl.NEAREST</code>
Linear<code>gl.LINEAR</code>
NearestMipmapNearest<code>gl.NEAREST_MIPMAP_NEAREST</code>
NearestMipmapLinear<code>gl.NEAREST_MIPMAP_LINEAR</code>
LinearMipmapNearest<code>gl.LINEAR_MIPMAP_NEAREST</code>
LinearMipmapLinear<code>gl.LINEAR_MIPMAP_LINEAR</code>

<a name="ctx.PixelFormat"></a>

ctx.PixelFormat

Mapping of ctx.TextureFormat keys to their string values and legacy depth formats

One of:

Kind: static enum of <code>ctx</code> <a name="ctx.RenderbufferFloatFormat"></a>

ctx.RenderbufferFloatFormat

Kind: static enum of <code>ctx</code> Properties

NameDefault
RGBA32F<code></code>
RGBA16F<code></code>
R16F<code></code>
RG16F<code></code>
R32F<code></code>
RG32F<code></code>
R11F_G11F_B10F<code></code>

<a name="ctx.Encoding"></a>

ctx.Encoding

Kind: static enum of <code>ctx</code> Properties

NameDefault
Linear<code>1</code>
Gamma<code>2</code>
SRGB<code>3</code>
RGBM<code>4</code>

<a name="ctx.Primitive"></a>

ctx.Primitive

Kind: static enum of <code>ctx</code> Properties

NameDefault
Points<code>gl.POINTS</code>
Lines<code>gl.LINES</code>
LineStrip<code>gl.LINE_STRIP</code>
Triangles<code>gl.TRIANGLES</code>
TriangleStrip<code>gl.TRIANGLE_STRIP</code>

<a name="ctx.Usage"></a>

ctx.Usage

Kind: static enum of <code>ctx</code> Properties

NameDefault
StaticDraw<code>gl.STATIC_DRAW</code>
DynamicDraw<code>gl.DYNAMIC_DRAW</code>
StreamDraw<code>gl.STREAM_DRAW</code>

<a name="ctx.Wrap"></a>

ctx.Wrap

Kind: static enum of <code>ctx</code> Properties

NameDefault
ClampToEdge<code>gl.CLAMP_TO_EDGE</code>
Repeat<code>gl.REPEAT</code>
MirroredRepeat<code>gl.MIRRORED_REPEAT</code>

<a name="ctx.QueryTarget"></a>

ctx.QueryTarget

Kind: static enum of <code>ctx</code> Properties

NameDefault
TimeElapsed<code>gl.TIME_ELAPSED</code>
AnySamplesPassed<code>gl.ANY_SAMPLES_PASSED</code>
AnySamplesPassedConservative<code>gl.ANY_SAMPLES_PASSED_CONSERVATIVE</code>
TransformFeedbackPrimitivesWritten<code>gl.TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN</code>

<a name="ctx.QueryState"></a>

ctx.QueryState

Kind: static enum of <code>ctx</code> Properties

NameDefault
Ready<code>ready</code>
Active<code>active</code>
Pending<code>pending</code>

<a name="ctx.set"></a>

ctx.set(options)

Set the context size and pixelRatio The new size and resolution will not be applied immediately but before drawing the next frame to avoid flickering. Context's canvas doesn't resize automatically, even if you don't pass width/height on init and the canvas is assigned the dimensions of the window. To handle resizing use the following code:

window.addEventListener("resize", () => {
  ctx.set({ width: window.innerWidth, height: window.innerHeight });
});

Kind: static method of <code>ctx</code>

ParamType
options<code>PexContextSetOptions</code>

<a name="ctx.debug"></a>

ctx.debug([enabled])

Enable debug mode

Kind: static method of <code>ctx</code>

ParamType
[enabled]<code>boolean</code>

<a name="ctx.frame"></a>

ctx.frame(cb)

Render Loop

Kind: static method of <code>ctx</code>

ParamTypeDescription
cb<code>function</code>Request Animation Frame callback

<a name="ctx.submit"></a>

ctx.submit(cmd, [batches], [subCommand])

Submit a command to the GPU. Commands are plain js objects with GPU resources needed to complete a draw call.

const cmd = {
  pass: Pass
  pipeline: Pipeline,
  attributes: { name:  VertexBuffer | { buffer: VertexBuffer, offset: number, stride: number } },
  indices: IndexBuffer | { buffer: IndexBuffer, offset: number, count: number },
  // or
  count: number,
  instances: number,
  uniforms: { name: number, name: Array, name: Texture2D },
  viewport: [0, 0, 1920, 1080],
  scissor: [0, 0, 1920, 1080]
}

Note: Either indices or count need to be specified when drawing geometry. Note: Scissor region is by default set to null and scissor test disabled.

Kind: static method of <code>ctx</code>

ParamType
cmd<code>PexCommand</code>
[batches]<code>PexCommand</code> | <code>Array.<PexCommand></code>
[subCommand]<code>PexCommand</code>

Example

// Draw mesh with custom color
ctx.submit(cmd, {
  uniforms: {
    uColor: [1, 0, 0, 0],
  },
});
// Draw same mesh twice with different material and position
ctx.submit(cmd, [
  { pipeline: material1, uniforms: { uModelMatrix: position1 },
  { pipeline: material2, uniforms: { uModelMatrix: position2 }
])
// Render to texture
ctx.submit(renderToFboCmd, () => {
  ctx.submit(drawMeshCmd);
});

<a name="ctx.pass"></a>

ctx.pass(opts) ⇒ <code>PexResource</code>

Passes are responsible for setting render targets (textures) and their clearing values. FBOs are created internally and automatically.

Kind: static method of <code>ctx</code>

ParamType
opts<code>PassOptions</code>

Example

const pass = ctx.pass({
  color: [Texture2D, ...]
  color: [{ texture: Texture2D | TextureCube, target: CubemapFace }, ...]
  depth: Texture2D
  clearColor: Array,
  clearDepth: number,
})

<a name="ctx.pipeline"></a>

ctx.pipeline(opts) ⇒ <code>PexResource</code>

Pipelines represent the state of the GPU rendering pipeline (shaders, blending, depth test etc).

Kind: static method of <code>ctx</code>

ParamType
opts<code>PipelineOptions</code>

Example

const pipeline = ctx.pipeline({
  vert: string,
  frag: string,
  depthWrite: boolean,
  depthTest: boolean,
  depthFunc: DepthFunc,
  blend: boolean,
  blendSrcRGBFactor: BlendFactor,
  blendSrcAlphaFactor: BlendFactor,
  blendDstRGBFactor: BlendFactor,
  blendDstAlphaFactor: BlendFactor,
  cullFace: boolean,
  cullFaceMode: Face,
  colorMask: Array,
  primitive: Primitive,
});

<a name="ctx.vertexArray"></a>

ctx.vertexArray(opts) ⇒ <code>PexResource</code>

Create a VAO resource.

Kind: static method of <code>ctx</code>

ParamType
opts<code>VertexArrayOptions</code>

Example

const vertexLayout = {
  aPosition: { location: 0, type: "vec3" },
  aNormal: { location: 1, type: "vec3" },
};

const drawCmd = {
  pipeline: ctx.pipeline({
    vertexLayout,
    // ...
  }),
  vertexArray: ctx.vertexArray({
    vertexLayout,
    attributes: {
      aPosition: ctx.vertexBuffer(geom.positions),
      aNormal: { buffer: ctx.vertexBuffer(geom.normals) },
    },
    indices: ctx.indexBuffer(geom.cells),
  }),
  // ...
};

<a name="ctx.texture2D"></a>

ctx.texture2D(opts) ⇒ <code>PexResource</code>

Create a 2D Texture resource.

Kind: static method of <code>ctx</code>

ParamType
opts<code>HTMLImageElement</code> | <code>HTMLVideoElement</code> | <code>HTMLCanvasElement</code> | <code>TextureOptions</code>

Example

const tex = ctx.texture2D({
  data: [255, 255, 255, 255, 0, 0, 0, 255],
  width: 2,
  height: 1,
  pixelFormat: ctx.PixelFormat.RGB8,
  encoding: ctx.Encoding.Linear,
  wrap: ctx.Wrap.Repeat,
});

<a name="ctx.textureCube"></a>

ctx.textureCube(opts) ⇒ <code>PexResource</code>

Create a 2D Texture cube resource.

Kind: static method of <code>ctx</code>

ParamType
opts<code>TextureCubeOptions</code>

Example

const tex = ctx.textureCube({
  data: [posx, negx, posy, negy, posz, negz],
  width: 64,
  height: 64
])

<a name="ctx.renderbuffer"></a>

ctx.renderbuffer(opts) ⇒ <code>PexResource</code>

Renderbuffers represent pixel data store for rendering operations.

Kind: static method of <code>ctx</code>

ParamType
opts<code>RenderbufferOptions</code>

Example

const tex = ctx.renderbuffer({
  width: 1280,
  height: 720,
  pixelFormat: ctx.PixelFormat.DEPTH_COMPONENT16,
});

<a name="ctx.vertexBuffer"></a>

ctx.vertexBuffer(opts) ⇒ <code>PexResource</code>

Create an attribute buffer (ARRAY_BUFFER) resource. Stores vertex data in the GPU memory.

Kind: static method of <code>ctx</code>

ParamType
opts<code>BufferOptions</code>

Example

const vertexBuffer = ctx.vertexBuffer({
  data: Array | TypedArray | ArrayBuffer,
});

<a name="ctx.indexBuffer"></a>

ctx.indexBuffer(opts) ⇒ <code>PexResource</code>

Create an index buffer (ELEMENT_ARRAY_BUFFER) resource. Stores index data in the GPU memory.

Kind: static method of <code>ctx</code>

ParamType
opts<code>BufferOptions</code>

Example

const indexBuffer = ctx.vertexBuffer({
  data: Array | TypedArray | ArrayBuffer,
});

<a name="ctx.query"></a>

ctx.query(opts) ⇒ <code>PexResource</code>

Queries can be used for GPU timers.

Kind: static method of <code>ctx</code>

ParamType
opts<code>QueryOptions</code>

Example

const query = ctx.query({
  target: QueryTarget,
});

<a name="ctx.beginQuery"></a>

ctx.beginQuery(query)

Begin the query measurement.

Kind: static method of <code>ctx</code>

ParamTypeDescription
query<code>PexQuery</code>Note: There can be only one query running at the time.

<a name="ctx.endQuery"></a>

ctx.endQuery(query)

End the query measurement.

Kind: static method of <code>ctx</code>

ParamTypeDescription
query<code>PexQuery</code>Note: The result is not available immediately and will be null until the state changes from ctx.QueryState.Pending to ctx.QueryState.Ready.

<a name="ctx.readPixels"></a>

ctx.readPixels(viewport) ⇒ <code>Uint8Array</code>

Helper to read a block of pixels from a specified rectangle of the current color framebuffer.

Kind: static method of <code>ctx</code>

ParamType
viewport<code>Object</code>

<a name="ctx.update"></a>

ctx.update(resource, opts)

Update a resource.

Kind: static method of <code>ctx</code>

ParamType
resource<code>PexResource</code>
opts<code>object</code>

Example

ctx.update(buffer, { data: [] });

ctx.update(texture, {
  width: 1,
  height: 1,
  data: new Uint8Array([255, 0, 0, 255]),
});

<a name="ctx.dispose"></a>

ctx.dispose([resource])

Delete one or all resource(s). Disposed resources are no longer valid for use.

Kind: static method of <code>ctx</code>

ParamType
[resource]<code>PexResource</code>

Example Delete all allocated resources:

ctx.dispose();

Delete a single resource:

ctx.dispose(texture);

Note: Framebuffers are ref counted and released by Pass. Programs are also ref counted and released by Pipeline. <a name="createContext"></a>

createContext([options]) ⇒ <code>ctx</code>

Create a context object

Kind: global function

ParamType
[options]<code>PexContextOptions</code> | <code>module:pex-gl~Options</code>

<a name="BufferOptions"></a>

BufferOptions : <code>PexResource</code>

Kind: global typedef Properties

NameTypeDefault
data<code>Array</code> | <code>TypedArray</code> | <code>ArrayBuffer</code>
[type]<code>ctx.DataType</code>
[usage]<code>Usage</code><code>ctx.Usage.StaticDraw</code>
offset<code>number</code>

<a name="Attachment"></a>

Attachment : <code>object</code>

Kind: global typedef Properties

NameType
texture<code>PexResource</code>
target<code>WebGLRenderingContext.FRAMEBUFFER</code>

<a name="PassOptions"></a>

PassOptions : <code>PexResource</code>

Kind: global typedef Properties

NameTypeDescription
[color]<code>Array.<PexTexture2D></code> | <code>Array.<Attachment></code>render target
[depth]<code>PexTexture2D</code>render target
[clearColor]<code>Color</code>
[clearDepth]<code>number</code>

<a name="PipelineOptions"></a>

PipelineOptions : <code>PexResource</code>

Kind: global typedef Properties

NameTypeDefaultDescription
[vert]<code>string</code><code>null</code>Vertex shader code
[frag]<code>string</code><code>null</code>Fragment shader code
[depthWrite]<code>boolean</code><code>true</code>Depth write mask
[depthTest]<code>boolean</code><code>false</code>Depth test on/off
[depthFunc]<code>DepthFunc</code><code>ctx.DepthFunc.LessEqual</code>Depth test function
[blend]<code>boolean</code><code>false</code>Blending on/off
[blendSrcRGBFactor]<code>BlendFactor</code><code>ctx.BlendFactor.One</code>Blending source color factor
[blendSrcAlphaFactor]<code>BlendFactor</code><code>ctx.BlendFactor.One</code>Blending source alpha factor
[blendDstRGBFactor]<code>BlendFactor</code><code>ctx.BlendFactor.One</code>Blending destination color factor
[blendDstAlphaFactor]<code>BlendFactor</code><code>ctx.BlendFactor.One</code>Blending destination alpha factor
[cullFace]<code>boolean</code><code>false</code>Face culling on/off
[cullFaceMode]<code>Face</code><code>ctx.Face.Back</code>Face culling mode
[colorMask]<code>Array.<boolean></code><code>[true, true, true, true]</code>Color write mask for [r, g, b, a]
[primitive]<code>Primitive</code><code>ctx.Primitive.Triangles</code>Geometry primitive

<a name="QueryOptions"></a>

QueryOptions : <code>PexResource</code>

Kind: global typedef Properties

NameTypeDefaultDescription
[target]<code>QueryTarget</code><code>ctx.QueryTarget.TimeElapsed</code>query type

<a name="PexQuery"></a>

PexQuery : <code>QueryOptions</code>

Kind: global typedef Properties

NameTypeDefaultDescription
[state]<code>QueryState</code><code>ctx.QueryState.Ready</code>
[result]<code>number</code>result of the measurement

<a name="RenderbufferOptions"></a>

RenderbufferOptions : <code>PexResource</code>

Kind: global typedef Properties

NameTypeDefaultDescription
width<code>number</code>
height<code>number</code>
[pixelFormat]<code>PixelFormat</code><code>ctx.PixelFormat.DEPTH_COMPONENT16</code>only PixelFormat.DEPTH_COMPONENT16 is currently supported for use as render pass depth storage (e.g. ctx.pass({ depth: renderbuffer })) for platforms with no WEBGL_depth_texture support.

<a name="TextureOptionsData"></a>

TextureOptionsData : <code>HTMLImageElement</code> | <code>HTMLVideoElement</code> | <code>HTMLCanvasElement</code>

Kind: global typedef Properties

NameType
data<code>Array</code> | <code>TypedArray</code>
width<code>number</code>
height<code>number</code>

<a name="TextureTarget"></a>

TextureTarget : <code>WebGLRenderingContext.TEXTURE_2D</code> | <code>WebGLRenderingContext.TEXTURE_CUBE_MAP</code>

Kind: global typedef <a name="TextureOptions"></a>

TextureOptions : <code>PexResource</code>

Kind: global typedef Properties

NameTypeDefaultDescription
[data]<code>HTMLImageElement</code> | <code>HTMLVideoElement</code> | <code>HTMLCanvasElement</code> | <code>Array</code> | <code>TypedArray</code> | <code>TextureOptionsData</code> | <code>Array.<HTMLImageElement></code> | <code>Array.<TextureOptionsData></code>
[width]<code>number</code>
[height]<code>number</code>
[pixelFormat]<code>PixelFormat</code><code>ctx.PixelFormat.RGB8</code>
[internalFormat]<code>ctx.TextureFormat</code><code>ctx.TextureFormat.RGBA</code>
[type]<code>ctx.DataType</code><code>ctx.TextureFormat[opts.pixelFormat]</code>
[encoding]<code>Encoding</code><code>ctx.Encoding.Linear</code>
[wrapS]<code>Wrap</code><code>ctx.Wrap.ClampToEdge</code>
[wrapT]<code>Wrap</code><code>ctx.Wrap.ClampToEdge</code>
[wrap]<code>Wrap</code><code>ctx.Wrap.ClampToEdge</code>
[min]<code>Filter</code><code>ctx.Filter.Nearest</code>
[mag]<code>Filter</code><code>ctx.Filter.Nearest</code>
[aniso]<code>number</code><code>0</code>requires EXT_texture_filter_anisotropic
[mipmap]<code>boolean</code><code>true</code>requires min to be set to ctx.Filter.LinearMipmapLinear or similar
[premultiplyAlpha]<code>boolean</code><code>false</code>
[flipY]<code>boolean</code><code>false</code>
[compressed]<code>boolean</code><code>false</code>
[target]<code>TextureTarget</code>

<a name="TextureCubeOptions"></a>

TextureCubeOptions : <code>PexResource</code>

Kind: global typedef Extends: <code>TextureOptions</code> Properties

NameTypeDescription
[data]<code>Array.<HTMLImageElement></code> | <code>Array.<TypedArray></code>6 images, one for each face +X, -X, +Y, -Y, +Z, -Z

<a name="VertexArrayOptions"></a>

VertexArrayOptions : <code>PexResource</code>

Kind: global typedef Properties

NameType
vertexLayout<code>object</code>
[attributes]<code>object</code>
[indices]<code>object</code>

<a name="PexContextOptions"></a>

PexContextOptions : <code>object</code>

Kind: global typedef Properties

NameTypeDefault
[gl]<code>WebGLRenderingContext</code> | <code>WebGL2RenderingContext</code><code>WebGL2RenderingContext</code>
[width]<code>number</code><code>window.innerWidth</code>
[height]<code>number</code><code>window.innerHeight</code>
[pixelRatio]<code>number</code><code>1</code>
[type]<code>"webgl"</code> | <code>"webgl2"</code><code>"webgl2"</code>
[debug]<code>boolean</code><code>false</code>

<a name="PexResource"></a>

PexResource : <code>object</code>

All resources are plain js object and once constructed their properties can be accessed directly. Please note those props are read only. To set new values or upload new data to GPU see updating resources.

Kind: global typedef Properties

NameType
name<code>string</code>

<a name="PexTexture2D"></a>

PexTexture2D : <code>object</code>

Kind: global typedef <a name="PexAttribute"></a>

PexAttribute : <code>object</code>

Kind: global typedef Properties

NameTypeDescription
buffer<code>object</code>ctx.vertexBuffer() or ctx.indexBuffer()
[offset]<code>number</code>
[stride]<code>number</code>
[divisor]<code>number</code>
[normalized]<code>boolean</code>

<a name="PexCommand"></a>

PexCommand : <code>object</code>

Kind: global typedef Properties

NameTypeDescription
pass<code>PassOptions</code>
pipeline<code>PipelineOptions</code>
[attributes]<code>object</code>vertex attributes, map of attributeName: ctx.vertexBuffer() or attributeName: PexAttribute
[indices]<code>object</code>indices, ctx.indexBuffer() or PexAttribute
[count]<code>number</code>number of vertices to draw
[instances]<code>number</code>number instances to draw
[uniforms]<code>object</code>shader uniforms, map of name: value
[viewport]<code>Viewport</code>drawing viewport bounds
[scissor]<code>Viewport</code>scissor test bounds
[multiDraw]<code>MultiDrawOptions</code>
[baseVertex]<code>number</code>
[baseInstance]<code>number</code>

<a name="MultiDrawOptions"></a>

MultiDrawOptions : <code>object</code>

Kind: global typedef See

Properties

NameType
counts<code>Int32Array</code> | <code>Array</code>
[countsOffset]<code>number</code>
offsets<code>Int32Array</code> | <code>Array</code>
[offsetsOffset]<code>number</code>
firsts<code>Int32Array</code> | <code>Array</code>
[firstsOffset]<code>number</code>
instanceCounts<code>Int32Array</code> | <code>Array</code>
[instanceCountsOffset]<code>number</code>
baseVertices<code>Int32Array</code> | <code>Array</code>
[baseVerticesOffset]<code>number</code>
baseInstances<code>UInt32Array</code> | <code>Array</code>
[baseInstancesOffset]<code>number</code>
[drawCount]<code>number</code>

<a name="PexContextSetOptions"></a>

PexContextSetOptions : <code>object</code>

Kind: global typedef Properties

NameType
[width]<code>number</code>
[height]<code>number</code>
[pixelRatio]<code>number</code>

<a name="Viewport"></a>

Viewport : <code>Array.<number></code>

[x, y, w, h]

Kind: global typedef <a name="Color"></a>

Color : <code>Array.<number></code>

[r, g, b, a]

Kind: global typedef <a name="TypedArray"></a>

TypedArray : <code>Int8Array</code> | <code>Uint8Array</code> | <code>Uint8ClampedArray</code> | <code>Int16Array</code> | <code>Uint16Array</code> | <code>Int32Array</code> | <code>Uint32Array</code> | <code>Float32Array</code> | <code>Float64Array</code> | <code>BigInt64Array</code> | <code>BigUint64Array</code>

Kind: global typedef

<!-- api-end -->

Capabilities <a name="capabilitiesTable"></a>

Example

const maxTextureSize = ctx.capabilities.maxTextureSize;
NameType
isWebGL2number
maxColorAttachmentsnumber
maxTextureImageUnitsnumber
maxVertexTextureImageUnitsnumber
maxVertexAttribsnumber
maxTextureSizenumber
maxCubeMapTextureSizenumber
depthTextureboolean
shaderTextureLodboolean
textureFloatboolean
textureFloatLinearboolean
textureHalfFloatboolean
textureHalfFloatLinearboolean
textureFilterAnisotropicboolean
disjointTimerQueryboolean
colorBufferFloatboolean
colorBufferHalfFloatboolean
floatBlendboolean
multiDrawboolean
drawInstancedBaseboolean
multiDrawInstancedBaseboolean

License

MIT. See license file.