Stencil Buffer Replacement

Im working on a GL implementation that does not have shaders as well as stencil buffer… might sounds like a stupid question but is there any other relatively “simple” way to implement a stencil buffer in software? Can someone point me a direction here …

Tks in advance,


There’s enough information in the spec to write a complete software renderer, with stencil buffer support :wink:

You may try with a pure addidive blend, adding a small color value, then you may copy this to texture and do thing according to the accumulated color value.
But it will hard to reproduce everything a stencil buffer can provide, and it will be even harder to make it fast.

Forget about the stencil : what precisely do you want to do ? Shadows ? Masks ? … ?
And what is the target implementation : hardware / software ? embedded ? …

there is mesa 3D, an OpenGL software implementation.
easy to download, make MSVS project and compile. hope that helps.

i would be interested as well what hardware you are targeting.

Zbuffer: Shadows, carmack zfail… hard to do without a stencil buffer :wink: since the hardware is limited, im planning to link like a really low version of the rendered one just for shadow purpose… and I can’t use planar shadow

_NK47: iPhone & iPod Touch

As Zbuffer said, I have heard of some games (PS2?) using the color buffer to do “stencil” shadow volumes.

It’s entirely possibly to implement both zpass/zfail shadows without a stencilbuffer.

For zfail set depthfunct to GL_GREATER and render all backfacing polygons with additive blending with the smallest available color value. Then render the frontfacing polygons with subtractive blending with the same small value. This will give you a shadow mask in the color buffer. Don’t forget to turn of depth write.

I used this technique on the original xbox (not because it didn’t have stencil buffers, it had), but because using the color buffer made it possible to render the shadows in one pass (the original xbox didn’t have two sided stencil test). This worked because you could set a positive color on back facing polygons and negative color on front facing.

You also had to clear the color buffer with something like 127 before because the ordinary blending didn’t wrap around. This isn’t necessary when doing the blending in two passes though, in this case clearing with 0 is fine.


brinck: Ok I got the big picture but… Im don’t understand what to do after I get the shadow mask in the color buffer, cuz I can only have access to 1 color buffer there’s no color attachment available from the implementation… Any example, code snippet or reference that can help me on this matter?

Tks in advance,


iPhone & iPT have render-to-texture via OES_framebuffer_object if that’s what you’re wanting?

Yeah I know that. Im just waiting for a pseudo-code of some kind that can help me to resolve this issue :wink:

Cuz I understand the big picture (like I mention) but I have no clue how to implement it… I don’t understand how can I use the color buffer for both drawing and stencil… Since I have only 1 color buffer available… this is why a “pseudo” code would help me…

you should use the “stencil color buffer” as a teexture; hence the reference to render-to-texture. you can use that additional texture with a full screen quad…

Yes, this should do the trick. Set up a frame buffer object and render the “stencil mask” to the alpha channel. Afterwards you should be able to use this frame buffer in conjuncture with alpha test (GL_NOTEQUAL and value set to 127, if this is what you’ve cleared the frame buffer object to begin with).

I guess you’ll also have to set up a texture matrix to automatically generate the correct texture coordinates for the mask in the frame buffer object.