Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_bindingsset each vertex attribute's pointer when its buffer changed, which it must, but also enabled the attribute and set its divisor again: 3.9enableVertexAttribArrayand 3.9vertexAttribDivisora draw call.apply_pipelinecalledglUseProgram, set the depth test and set the front face every time, even for the pipeline already applied.end_render_passbound no buffers in place of the pass's, and the next pass bound them again: 4bindBuffera draw call where 2 do.new_bufferandbuffer_updatebound the buffer to upload into, then bound back the one before.Change
apply_bindingsonly enables an attribute that wasn't, and only sets a divisor that changed.GlCachekeeps the program in use, the depth test and the front face, as it already does blending, stencil, cull face and color mask, andapply_pipelineonly 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_passleaves the buffers bound.drawbinds back the bindings' index buffer before drawing.drawtakes&self, so that mark is aCell. A draw after an upload, without bindings applied again in between, still draws with the bindings' index buffer, as before.Verification
bindBufferfalls from 4 to 2,enableVertexAttribArrayandvertexAttribDivisorfrom 3.9 to 0.01,useProgramfrom 1 to 0.04 andfrontFacefrom 1 to none. With gl: keep resources in a Vec by id #715,apply_bindingstakes 0.27–0.37 ms of the frame rather than 0.67–0.82, andapply_pipeline0.07–0.08 ms rather than 0.22–0.32.cargo buildhas no new warnings, natively or forwasm32-unknown-unknown.🤖 Generated with Claude Code