View Full Version : Efficiently using many stencil buffers?

Yuri Meiburg
09-17-2010, 03:18 PM
Hi, first of all: Thanks for trying to help :-). The problem is as follows:

I have this reasonably large dataset consisting of 2 dimensional lines (200.000+) . This dataset is clustered in to a large number of smaller datasets. What I want to do is draw something on top of those lines, so what I do now is draw each line in a stencil buffer using a thickness of around 10 pixels and then draw my overlay using the stencil buffer to hide areas which I do not want to draw on.

What I am doing now is each frame render I calculate the stencil buffer again by drawing all lines on top of it. I do this because I do not know how many clusters I get (who knows, even more than 200 for some datasets). To me this sounds very computationally inefficient.

Is there maybe a trick to for instance render all lines to some texture, so when I define the stencil buffer I only have to draw a rendered quad? Or is there any other trick I am missing?

I hope I made myself clear :).
Thanks again!

09-17-2010, 05:44 PM
I think it would be beneficial if you describe in better detail what you're trying to do. I'm not understanding what your clusters have to do with anything. Are you saying that for each dataset, you render the dataset's lines, draw something else only over the regions the line was drawn (using the stencil buffer), and start over again with another dataset until you've rendered every dataset?

Yuri Meiburg
09-18-2010, 12:59 AM
That is correct! I mentioned the clusters to illustrate that I can't just render a stencil buffer and not clear it, because I need many different stencil buffers: One for each cluster.

This is exactly why I am asking this, if there were only a few clusters, I could just use not clear the stencil buffer and reuse them again next render pass. But I don't have 200 stencil buffers, right?

09-18-2010, 06:21 AM
I'm still not sure I completely understand your situation, but why don't you do something like...

set stencil writes to 1
draw lines of dataset 1
draw onto lines where stencil is 1

set stencil writes to 2
draw lines of dataset 2
draw onto lines where stencil is 2

May I ask what you are drawing on top of the lines? You may not even need the stencil buffer.

Yuri Meiburg
09-19-2010, 02:29 AM
what exactly does 'set stencil writes to 2' mean? Use the second bit of a stencil buffer? If so: Does that work for up to 200 clusters?

And I am drawing some form of flow simulation on top of the lines. The method unfortunately always results in a rectangle, so I need to clip it to fit to the lines.

Alfonse Reinheart
09-19-2010, 04:08 AM
what exactly does 'set stencil writes to 2' mean?

The stencil buffer is just an 8-bit unsigned integer texture, associated with the framebuffer. So each pixel in the framebuffer can store an 8-bit unsigned integer.

The stencil test allows you to conditionally draw or not draw based on the contents of the stencil buffer relative to a value you assign. This is what glStencilFunc does: define the comparison between the stencil buffer value and a value you provide to the function.

So if your line drawing writes the number 1 to the stencil buffer, you can use a glStencilFunc call to discard fragments unless the stencil buffer contains the number "1". The same goes for 2, 3, 4, etc. Up to 255.