0
\$\begingroup\$

I have geometry that have attached some "ID" data. I have generated stencil buffer, that holds these IDs. Now, in second pass, I render different geometry, but this time I need to mask it with already existing stencil. Each part of geometry can have different "ID". Is there a way, how to control stencil buffer test value from shader?

I give an example:

  1. render geometry with ID = 1, writes 1 to stencil
  2. render geometry with ID = 2 if ID in stencil != 1, writes 2 to stencil
  3. do some other rendering with stencil turn off
  4. render bunch of particles at one go, but some of them have ID 1, some of them ID 2 -> I want to mask them, so 1 is rendered only if 1 is in stencil and 2 only if 2 is in stencil

I need this for OpenGL ES 2.0

\$\endgroup\$
1
  • \$\begingroup\$ Wouldn't you have to pass the ID through to a pixel shader, then discard the pixel depending on the ID value? You can bind a pixel shader to depth pass, it just needs to return null if I remember (This is in DirectX 11, so it obviously maybe different but the concept might be feasible).. \$\endgroup\$ Commented Feb 18, 2018 at 20:47

1 Answer 1

1
\$\begingroup\$

I don't know a way to do it with pure OpenGL ES 2.0, but it would be possible with the ARM_shader_framebuffer_fetch_depth_stencil extension.

You can not control the stencil test value from the shader, but it allows you to read the current value from the stencil using gl_LastFragStencilARM and then you can compare and discard as you wish.

There are also ways to attach stencil as a texture, so you can do something similar, but as far as I can find all need at least OpenGL ES 3.0.

If a majority of your target devices support this, you can use it and implement a fallback where the particles are split into groups by ID and rendered separately.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.