memononen/nanovg — pull request #165 by floooh (2014-09-04 19:24:03+00:00)
Title: Redundant GL state filter

This removes redundant calls to glBindTexture, glStencilFunc and glStencilMask (these were the only ones which has any redundant state changes in the demo scene). Especially glBindTexture(..., 0) was called very often.

One point I was not sure about: glStencilMask is never called with anything other then 255 during rendering. If this will remain so in the future, it might be better to only set the state once when rendering starts. Otherwise the implementation in the PR might be better.

I'm not sure whether the NANOVG_GL_USE_STATE_FILTER makes sense, let me know if I should remove it (might be useful for checking how effective the state filter is in different scenes).

Some numbers from the demo scene in WebGL/GLES (however, these number already have the uniform update optimization:

With an NVG_STENCIL_STROKES | NVG_ANTIALIAS context:
original: 550 draws, 2583 GL calls
redundant state filer: 550 draws, 2138 GL calls

With a no-flags context:
orig: 280 draws, 1343 GL calls
redundant state filter: 280 draws, 1119 calls

More detailed:
NVG_STENCIL_STROKES | NVG_ANTIALIAS:
original: 550 draws, 2583 GL calls
no redundant bind-texture: 550 draws, 2333 calls
no redundant stencilmask: 550 draws, 2223 calls
no redundant stencilfunc: 550 draws, 2138 calls

No-flags context:
orig: 280 draws, 1343 GL calls
no redundant bind-texture: 280 draws, 1161 calls
no redundant stencilmask: 280 draws, 1119 calls
no redundant stencilfunc: 280 draws, 1119 calls