PDA

View Full Version : early fragment kill / stencil buffer & fragment shader



Bregosch
02-19-2003, 01:49 AM
Hi,

I'd like to know if it is possible to perform an "early fragment kill". By that I mean that when the stencil buffer "discards" a pixel the pixel shader MUST NOT process it anymore.

Currently, if the stencil buffer discards a pixel, the pixel shader regardlessly processes the pixel... http://www.opengl.org/discussion_boards/ubb/frown.gif

Any ideas on this topic? Perhaps there's even a flag for exactly this?

Thanks,
Thomas

pocketmoon
02-19-2003, 02:38 AM
Originally posted by Bregosch:
Hi,

I'd like to know if it is possible to perform an "early fragment kill". By that I mean that when the stencil buffer "discards" a pixel the pixel shader MUST NOT process it anymore.

Currently, if the stencil buffer discards a pixel, the pixel shader regardlessly processes the pixel... http://www.opengl.org/discussion_boards/ubb/frown.gif

Any ideas on this topic? Perhaps there's even a flag for exactly this?

Thanks,
Thomas

Not an answer but I would love to see access to the (current) stencil value in fragment shaders.

Bregosch
02-19-2003, 02:40 AM
Yes that would (almost) be sufficient! But still not the fastest way to solve this problem... http://www.opengl.org/discussion_boards/ubb/wink.gif

Anyone here with the solution? Anyone? http://www.opengl.org/discussion_boards/ubb/smile.gif

zeckensack
02-19-2003, 02:54 AM
Current graphics chips already do this. It's called early-Z rejection, or something close to that. Methinks it works for stenciling too, but not for all combinations of render state.

But note that it's still possible not to see a gain. The problem is that a discarded pixel leaves behind an empty fragment pipe which needs to be filled again - think 'branch misprediction penalty', and you're close.

Bregosch
02-19-2003, 03:16 AM
But how can I make use of this feature? I mean, is this early rejection through the stencil buffer automatically active on the NV30 (my developing environment)?

I think you're right with the low (or even none) performance gain. But speed isn't that critical for my app, I just wish to use the stencil buffer to mask out pixels completely, with no shader processing.

pbrown
02-19-2003, 07:21 PM
Apologies for being away from this board for so long.

"Early fragment kill" (which has many marketing names) works automatically, and is initated by the driver whenever possible. Some such algorithms kill with 100% accuracy; others may fail to kill fragments that will be killed at the end -- these are always backed up by a normal "late fragment kill". There is generally no guarantee that any given fragment would be killed before it's shaded, and other than by measuring performance, there's no way for you to tell. Fragment programs have no side effects....

zeckensack is right that is sometimes leave bubbles in the pipeline (when a long string of fragments are being killed one after another). But if you are running a long, complicated shader, things tend to back up anyways.

All you have to do to use this feature is to turn on GL_STENCIL_TEST and/or GL_DEPTH_TEST as appropriate and let us do the rest.

Note that a depth-replacing fragment program (one that writes result.depth on ARB_f_p or o[DEPR] on NV_f_p) limits the applicability of any early depth test operations, since you have to run most (if not all) of the fragment program to have a depth value to test.

Bregosch
02-20-2003, 02:22 AM
Thank you all very much for your answers!!

I guess I know now how to proceed. I use a texture in my shader to look up which fragment should be processed and which shouldn't. And then I kill the unwanted ones http://www.opengl.org/discussion_boards/ubb/smile.gif Works fine!