2

I am trying to figure out how to implement batching in my OpenGL/DirectX based applications. I know that when depth test / depth write is enabled, I can put a bunch of objects that render with a common material in the same coordinate space and draw using a single draw call. But in my particular application, I have several objects that test against the depth buffer, but do not write to the depth buffer, and rely on draw order get the correct appearance. These are typically 2D geometry (e.g. a panel with dynamic information) in a 3D world.

Is there a general solution I can use to be able to batch items that normally need a specific draw order? Or do I need to tweak my scene and enable depth writing on everything to take advantage of batching?

default
  • 2,637
  • 21
  • 44
  • *e.g. a panel with dynamic information*. You mean to say that objects already in the scene are allowed to occlude these if their depth is sufficient (e.g. closer and the depth function is `LESS`)? I have to wonder why you do not want them to write to the depth buffer? If you allow them to write to the depth buffer and draw them first you can improve performance in areas where your GUI completely occludes world geometry that has potentially expensive pixel/fragment shaders to evaluate. – Andon M. Coleman Nov 27 '13 at 04:49
  • 1
    Basically there are several 2d objects written to the same location to compose a panel. One of the objects will depth write, The rest will only depth test since a write would cause z-fighting with subsequent draws to that location. – default Nov 27 '13 at 14:34

2 Answers2

4

GPUs will render in geometry order, even within a single draw call. In other words, the first triangle will draw before the second triangle, and so forth --- even though the shading calculations may occur in parallel.

So you shouldn't need to do anything special here. As long as your instances are arranged in draw order within the batch, the results will be the same as if you used multiple draw calls.

0

You could partition your normalized Z space in N (number of 2D objects) then trace each object using a different normalized Z value corresponding to your draw order and let the depth test do the "sorting" for you.

  • Keep in mind that you might have to use a non-linear partition algorithm, for the depth range is not linear. Let me know if you need more info on that. – Jean-Simon Brochu Nov 26 '13 at 17:52
  • I'm am familiar with why depth buffers are non-linear, but not any "non-linear partition" algorithms. If you have extra info, i'll take it! – default Nov 26 '13 at 18:11
  • Well it's a term I just invented. Imagine a linear depth range from 0 to 500. If you have 500 2D items to draw, just assign a Z value to each items in the "order" you want to trace them ( just like you would do without depth writing ). But with depth writing enabled, OpenGL will do the trick for you. I'm not 100% sure you need to do this. Have you tried just NOT writing to the depth buffer? – Jean-Simon Brochu Nov 27 '13 at 14:39