1

I'm using Orthographic projection to draw my objects. Each object items is being added to different buffers and being drawn in several cycles. Let's say that each object has an outline square and fill for the square (in different color). So i'm drawing first the all the fillings, and then the outlines.

I'm using depth buffer to make sure that the outlines will not be over all the fills as shown at the picture enter image description here

Now i'm facing a problem that each object contains another drawing item on it (such as text - points) which can be longer than this squares. So i'm using the stencil buffer for cutting this additional drawing over the square. Although, when doing this there is no consideration in the depth buffer. Meaning that one text item can be drawn over the other square. as showed below.enter image description here

Is there anyway\trick to make it happen ?

Raziza O
  • 1,560
  • 1
  • 17
  • 37

1 Answers1

0

You should be able to set the stencil buffer to a different value for each of the squares (provided there is <= 255 squares, as you won't be able to get a more than 8-bit stencil buffer). Configure the stencil value to KEEP for pixels that fail the depth test, causing any stencil values written by quads that are further in front but have been drawn earlier to be retained.

This will allow clipping each text individually.

Another way is to use only the depth buffer and pass the pixel extents of the current quad into the text pixel shader, where you can discard any extra pixels. This requires less state changes.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • I have much more than 255 objects, and technically they are not squares - they are some texture from texture atlas - like a font :) I wanted to simplify it for the question. There is any faster way than checking every pixel in the pixel shader if it's out of bounds ? – Raziza O Jul 13 '14 at 12:33
  • I don't think so - both stencil and scissor tests require expensive state changes. The pixel shader check is going to be relatively cheap however. – Alexander Gessler Jul 13 '14 at 13:56
  • You say that computing and 'discard'ing pixels in the shader is faster then using the stencil buffer for doing it ? Is not the purpose of this buffer ? – Raziza O Jul 13 '14 at 14:49
  • If you have to change stencil states between drawing single objects that would otherwise get batched together it will be prohibitively expensive. However, if you can manage to efficiently clip in the pixel shader you might be able to do with much less state changes. At least that is my intuition, you will have to profile it. – Alexander Gessler Jul 13 '14 at 14:56
  • I've solved it by changing the DepthTest to EQUALS. Now the texts are drawing only where the background is on the same Z value. – Raziza O Oct 23 '14 at 16:32