perf(webgl2): allocate immutable texture storage once, upload with texSubImage2D - #996
Open
Endymi0n74 wants to merge 1 commit into
Open
perf(webgl2): allocate immutable texture storage once, upload with texSubImage2D#996Endymi0n74 wants to merge 1 commit into
Endymi0n74 wants to merge 1 commit into
Conversation
…xSubImage2D texImage2D() reallocates the texture's GPU storage on every frame. Allocate the storage once with texStorage2D (sized RGB8 — gl.RGB is unsized and invalid for texStorage2D: INVALID_ENUM, storage never allocated, per-frame uploads fail and the renderer goes black) and upload per frame with texSubImage2D on the immutable storage. The texture stays bound across frames (no per-frame bindTexture), and is recreated when the video resolution changes (immutable storage can't be resized). Measured on the built bundle (seed protocol, real GPU): video upload drops from ~42-78 µs to ~8-12 µs per upload (×5.5), updateFrame total wall from ~0.043-0.074 ms to ~0.011-0.020 ms (×2.8); draw unchanged (same shader).
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.
What
The WebGL2 renderer uploaded the video frame with
texImage2D()on every frame, which reallocates the texture's GPU storage each time. This PR:texStorage2D(gl.TEXTURE_2D, 1, gl.RGB8, w, h)and uploads per frame withtexSubImage2D()on that immutable storage,bindTexture— the binding now happens insideallocateStorage, once per allocation),texStorage2Dcall, but the earlier fork iteration usedgl.RGB— an unsized format that is invalid fortexStorage2D(INVALID_ENUM→ storage never allocated → per-frametexSubImage2Duploads fail → black renderer). Sizedgl.RGB8is required;texSubImage2Dkeepsgl.RGB.Why
texImage2Don a per-frame basis reallocates GPU storage every frame (~×2.1 the cost of atexSubImage2Dinto an immutable storage on the measured driver). The upload path dominates the frame cost.Safety
updateFrameallocates on the first frame wherevideoWidth/Height > 0if no storage exists yet.resourcesand deleted before re-allocating (no leak, no stale binding).destroy()nullstextureafter the resource loop.Measurement
Real GPU harness (640×360 VP9 test video, WebGL2 via ANGLE/D3D11, instrumented GL counters, seed protocol):
updateFrametotal wall (ms/frame)texImage2D+drawArraystexSubImage2D+drawArrays(0texImage2D, 0bindTexture)