0

I have to optimize a stencil-based layer rendering system because it produces too many drawcalls (hundreds). The scenario is as this:

  • Several dozens geometry batches are drawn. Each batch has its own drawcall.
  • Each batch has multiple layers (about 10). These layers are to be sorted globally. Currently, this is done via glStencilFunc by assigning different ref values for every layer.
  • Since one drawcall cannot use multiple glStencilFunc configurations as far as I know, this yields numberOfBatches * numberOfLayers drawcalls.
  • There is other geometry in the scene, so I cannot disable depth test/depth read but depth write is disables while the batches are rendered.

Now my question is: Do you know an approach for layered sorting without glStencilFunc so I only have to issue one render call per batch?

My first idea would be a two-pass approach with an FBO. In the first pass, only the batches are rendered and the depth value is used for layer sorting because I can write it from the vertex/fragment shader. In the second pass, said FBO is sampled in the fragment shader and only the fragments with the surviving depth value are preserved. This would replace one pass with hundreds of drawcalls by two passes with dozens of drawcalls. The question is, whether the saving in drawcalls will make up for the performance loss due to render target switching.

Or do you have a better idea?

The approach should work with OpenGL ES 3.0 (maybe also 3.1).

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Desperado17
  • 835
  • 6
  • 12
  • Without knowing more about your application it's going to be hard to give any sensible answer. Optimizing batching typically requires exploiting knowledge about the scene layout, the geometry, what's transparent or not, and constraints about what can overlap, what ordering can be assumed, etc. – solidpixel Jul 23 '19 at 07:25
  • 1
    Questions: Is any of this geometry transparent and using blending? Does geometry inside each batch intersect other geometry in the batch? Can batches insect each other? Is sorting local to a single batch, or global across batches? Is stencil only used for providing layer information, or does it also e.g. apply some clip mask shape? – solidpixel Jul 23 '19 at 07:28
  • It doesn't do blending and does not implement clip masks. The stencil is used to perform a depth sorting within layers within the batches in addition to the regular, global depth sorting that happens with the other objects. Batches can overlap each other, the ordering of layers of a certain type still has to be preserved (layer A of batch 1 should be in front of layer B of batch 2 etc. ). This is why a global method like stencil is used. – Desperado17 Jul 23 '19 at 09:37
  • If nothing is using blending and it's all opaque can't you just use depth testing? Then nothing needs sorting at all and you can just throw geometry at the GPU ... – solidpixel Jul 23 '19 at 12:19
  • Is this 2D or 3D geometry - i.e. can you abuse the Z value? – solidpixel Jul 23 '19 at 12:21

0 Answers0