PDA

View Full Version : About Stencil Buffer and 0s and 1s analogy



DragonautX
04-12-2017, 12:59 AM
In the stencil testing (https://learnopengl.com/#!Advanced-OpenGL/Stencil-testing) tutorial on Learn OpenGL, a stencil buffer is described like this:


A stencil buffer (usually) contains 8 bits per stencil value that amounts to a total of 256 different stencil values per pixel/fragment. We can then set these stencil values to values of our liking and then we can discard or keep fragments whenever a particular fragment has a certain stencil value.

A picture of the buffer is shown as 0s and 1s. Then the process is described like this:


The stencil buffer is first cleared with zeros and then an open rectangle of 1s is set in the stencil buffer. The fragments of the scene are then only rendered (the others are discarded) wherever the stencil value of that fragment contains a 1.

I'm a little confused about what a pixel/fragment in a stencil buffer can be changed to. With 256 different stencil values, can a zero be changed to something between 0 and 1 (like 0.5), or like can it be changed to 255?

So far, as I understand it, a stencil buffer starts where each pixel/fragment is represented as a 0:

0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Can a 0 be changed to something other then "1", since there's 256 different stencil values? Like this?

0 0 0 0
0 0 0 0
0 0 0.5 0.5

or this?

0 0 0 0
0 0 0 0
0 0 256 256

Dark Photon
04-12-2017, 12:19 PM
I'm a little confused about what a pixel/fragment in a stencil buffer can be changed to. With 256 different stencil values, can a zero be changed to something between 0 and 1 (like 0.5), or like can it be changed to 255?

Typically a stencil buffer (when present) is 8-bits. Think of it as an unsigned 8-bit integer. Therefore you can have values stored in each sample's stencil buffer of 0..255, inclusive.

DragonautX
04-12-2017, 03:16 PM
Okay, that makes sense. So a stencil buffer can have multiple integer values besides 0 and 1. Thanks for the help.

mhagain
04-13-2017, 03:19 AM
Only values between 0 and 255 are valid for an 8-bit stencil buffer, because that's the range of values that will fit in an 8-bit integer.

So to call back to your first post, no, you can't put 256 in it.

DragonautX
04-13-2017, 10:22 AM
Oh ya, thanks. I think I did a typo there, should've been 255.