memononen / nanovg

Antialiased 2D vector drawing library on top of OpenGL for UI and visualizations.
zlib License
5.13k stars 768 forks source link

What state should the stencil buffer be in each frame? #254

Open cmaughan opened 8 years ago

cmaughan commented 8 years ago

I recently debugged a difference between D3D and GL rendering. It turned out that the D3D renderer was clearing the stencil to 1, but the GL was clearing to 0. I thought nanovg didn't care about the initial state, but I guess not. Just checking - is the assumption that each frame the stencil is cleared to 0?

memononen commented 8 years ago

Yes, the stencil is assumed to be cleared to 0.

memononen commented 8 years ago

The relevant code is here: https://github.com/memononen/nanovg/blob/master/src/nanovg_gl.h#L943

In the chunk which draws stuff with color mask false, does the even/odd thing. The expected result is that the regions outside the shape are 0.

Then the AA fringe is drawn, only the pixels outside the shape get written. The shape is inset by half a pixel earlier to make this work.

And finally the fill is drawn, clearing the stencil to 0 is it goes.

cmaughan commented 8 years ago

That makes sense, thanks. I had missed that the final fill re-clears the stencil. Clever ;)