Skip to content

feat(webgl): cache vertex attribute state per VAO and reuse per-program VAOs - #986

Merged
wouterlucas merged 1 commit into
lightning-js:mainfrom
wouterlucas:feat/phase1-webgl-attrib-vao-caching
Nov 26, 2026
Merged

wouterlucas merged 1 commit into
lightning-js:mainfrom
wouterlucas:feat/phase1-webgl-attrib-vao-caching

Conversation

@wouterlucas

Copy link
Copy Markdown
Contributor

Apply fast paths: behavior-preserving reduction of redundant WebGL attribute setup.

  • WebGlContextWrapper now keeps per-VAO shadow state (enable flags, full vertexAttribPointer parameter sets, element-buffer binding) and skips native calls when the effective state is unchanged. The ARRAY_BUFFER bind still happens before a skipped pointer call. deleteBuffer/deleteVertexArray invalidate the shadow tables; the constructor seeds them from the live context; context restoration recreates the wrapper, so no stale GL assumptions survive.
  • WebGlShaderProgram owns a lazily created VAO on WebGL2 and configures it once per BufferCollection layout (identity + layout version), so rebinding the same layout issues zero enable/pointer calls in steady state. WebGL1 keeps the per-attribute path, backed by the wrapper shadow state. detach() unbinds the VAO instead of disabling attributes; destroy() deletes it.
  • BufferCollection gains a layout version (invalidateLayout) so caches keyed on (collection, version) reconfigure on real layout changes.
  • createIndexBuffer returns the shared index buffer, stored on the renderer so each program VAO can bind it (ELEMENT_ARRAY_BUFFER is captured per VAO in WebGL2).

…am VAOs

Phase 1 of the L2-style fast paths: behavior-preserving reduction of
redundant WebGL attribute setup.

- WebGlContextWrapper now keeps per-VAO shadow state (enable flags,
  full vertexAttribPointer parameter sets, element-buffer binding) and
  skips native calls when the effective state is unchanged. The
  ARRAY_BUFFER bind still happens before a skipped pointer call.
  deleteBuffer/deleteVertexArray invalidate the shadow tables; the
  constructor seeds them from the live context; context restoration
  recreates the wrapper, so no stale GL assumptions survive.
- WebGlShaderProgram owns a lazily created VAO on WebGL2 and configures
  it once per BufferCollection layout (identity + layout version), so
  rebinding the same layout issues zero enable/pointer calls in steady
  state. WebGL1 keeps the per-attribute path, backed by the wrapper
  shadow state. detach() unbinds the VAO instead of disabling
  attributes; destroy() deletes it.
- BufferCollection gains a layout version (invalidateLayout) so caches
  keyed on (collection, version) reconfigure on real layout changes.
- createIndexBuffer returns the shared index buffer, stored on the
  renderer so each program VAO can bind it (ELEMENT_ARRAY_BUFFER is
  captured per VAO in WebGL2).

Tests: WebGlContextWrapper.attribState.test.ts (8) and
WebGlShaderProgram.vao.test.ts (4). Full suite 561 passed, build clean.
@wouterlucas
wouterlucas merged commit d4887bd into lightning-js:main Nov 26, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants