Skip to content

gl: skip state that's already set - #716

Open
benface wants to merge 1 commit into
not-fl3:masterfrom
benface:gl-skip-state-already-set
Open

benface wants to merge 1 commit into
not-fl3:masterfrom
benface:gl-skip-state-already-set

Conversation

@benface

@benface benface commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Why

Each draw call set GL state that hadn't changed. With macroquad, where each batched draw call has vertex and index buffers of its own, a web build of a game (CHOMP) drawing about 200 draw calls a frame made 32.5 WebGL calls a draw call:

  • apply_bindings set each vertex attribute's pointer when its buffer changed, which it must, but also enabled the attribute and set its divisor again: 3.9 enableVertexAttribArray and 3.9 vertexAttribDivisor a draw call.
  • apply_pipeline called glUseProgram, set the depth test and set the front face every time, even for the pipeline already applied.
  • end_render_pass bound no buffers in place of the pass's, and the next pass bound them again: 4 bindBuffer a draw call where 2 do.
  • new_buffer and buffer_update bound the buffer to upload into, then bound back the one before.

Change

  • An attribute in the cache is enabled with its divisor set, so apply_bindings only enables an attribute that wasn't, and only sets a divisor that changed.
  • GlCache keeps the program in use, the depth test and the front face, as it already does blending, stencil, cull face and color mask, and apply_pipeline only sets what changed. Loading a shader leaves its program in use, so the cache takes it; deleting the program in use clears it.
  • end_render_pass leaves the buffers bound.
  • Uploads don't bind back what was bound. Draws don't read the vertex buffer binding, so it stays as the upload left it. The index buffer binding is the bindings', so an upload that binds another index buffer marks it, and draw binds back the bindings' index buffer before drawing. draw takes &self, so that mark is a Cell. A draw after an upload, without bindings applied again in between, still draws with the bindings' index buffer, as before.

Verification

  • In that web build, throttled 4× in Chrome, a draw call makes 19.8 WebGL calls rather than 32.5: bindBuffer falls from 4 to 2, enableVertexAttribArray and vertexAttribDivisor from 3.9 to 0.01, useProgram from 1 to 0.04 and frontFace from 1 to none. With gl: keep resources in a Vec by id #715, apply_bindings takes 0.27–0.37 ms of the frame rather than 0.67–0.82, and apply_pipeline 0.07–0.08 ms rather than 0.22–0.32.
  • The game's frames through OpenGL on macOS are byte-identical to before, over 235 frames of eight captures.

cargo build has no new warnings, natively or for wasm32-unknown-unknown.

🤖 Generated with Claude Code

Each draw call set state that hadn't changed:

- `apply_bindings` enabled every vertex attribute and set its divisor
  again whenever its buffer changed, which is every draw call when each
  has buffers of its own, as macroquad's batches do. An attribute in the
  cache is enabled with its divisor set, so now only its pointer is set
  again.
- `apply_pipeline` called `glUseProgram`, set the depth test and the
  front face every time. They're cached now, as blending, stencil, cull
  face and color mask already were.
- `end_render_pass` bound no buffers in place of the pass's, so the next
  pass bound them again. They stay bound now.
- `new_buffer` and `buffer_update` bound the buffer they upload into and
  then bound back the one before. Draws don't read the vertex buffer
  binding, so it stays as the upload left it, and the next draw binds
  back the index buffer the bindings applied if an upload bound another
  since.

In the web build, with 200 draw calls a frame, a draw call now makes
19.8 WebGL calls rather than 32.5: `bindBuffer` falls from 4 a draw call
to 2, `enableVertexAttribArray` and `vertexAttribDivisor` from 3.9 to
0.01, `useProgram` from 1 to 0.04, and `frontFace` from 1 to none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: multica-agent <github@multica.ai>
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.

1 participant