Search Unity

  1. Calling all beginners! Join the FPS Beginners Mods Challenge until December 13.
    Dismiss Notice
  2. It's Cyber Week at the Asset Store!
    Dismiss Notice

How does NGUI render sprite one by one

Discussion in 'Shaders' started by Lulucifer, Jul 27, 2014.

  1. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    How does NGUI render sprite one by one,from back to front ,which ordered by its own depth(-2,-1,0,1,2,3....)
    Since all of these spirtes are drawed by one DrawCall,Because of the order of triangles send to GPU?
    I Guess that is it,but not sure, Any Guru?
     
  2. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    Not sure about NGUI internal coding but in general the order or triangles sent to the GPU would not matter .. the z position from the camera determines the drawing order. When using an orthographic camera changing the z position or GUI GameObjects does not physically change the size of the object but it does still affect the drawing order.
     
  3. Lulucifer

    Lulucifer

    Joined:
    Jul 8, 2012
    Posts:
    358
    Thanks,I forget that GPU is highly parallel optimized. so the question is little different, NGUI draw all these sprites in one draw call, how does it change their zdepth differently by triangle,is that possible?
     
  4. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    1,521
    Pretty sure that's not correct. In the past i've relied on the GPU rendering triangles in the order they are given in a mesh and its always worked. For example i've done per triangle depth position sorting in a mesh using this technique. The gpu doesn't do any sorting on z position or anything else. The z-buffer if enabled will ensure correct visual rendering and z position sorting will be done per mesh/model by Unity for meshes using transparent materials.
     
    Lulucifer likes this.
  5. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    well perhaps when every vertex in the mesh has the same z-position then it would simply be a case of painters algorithm.

    Outside of that scenario I was just saying the z-position of the vertices in a triangle are what depicts its depth.

    eg If I have a mesh that has two triangles not sharing any vertices, one triangles vertices all have a z-position of 0 , the other triangles vertices all have a z-position of -100, and the camera was at -200 .. It would not matter in which order these two triangles are inserted into the mesh triangles array.. the triangle which is closest to the camera will always be rendered above the one which is not.
     
  6. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    1,521
    True, but your initial statement said 'drawing order' which insinuated that the gpu would be doing some sorting to 'draw in a specific order' which is incorrect. While the z-buffer will ensure correct visual rendering it does not itself sort anything and of course is completely dependant upon zTest being enabled. Regardless of which the gpu will always render the triangles in the order they are provided, the z-buffer (if enabled) will ensure the correct visual results.
     
  7. shaderbytes

    shaderbytes

    Joined:
    Nov 11, 2010
    Posts:
    900
    yes , I did not word things in great accurate detail , what was meant is that presumably the z-buffer is the means of sorting. I would think in the case of a GUI system that is using a combined mesh structure to use one draw call ( not batching ) that internally it would maintain references to index start points for vertex sets. It would then be much easier to simply adjust the vertex z-positions to get per item depth manipulation than to have to rebuild and sort the entire vertex , triangle list to rely on painters algorithm.

    I did not even think about the order the triangles are sent to the GPU as a means of sorting, I would have thought its always best to use the z-buffer, but as you mentioned you have used it in this manner before so I stand corrected.
     
    Last edited: Jul 28, 2014
    Noisecrime likes this.
  8. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,398
    That is also my experience. Of course the z-buffer can still change things, but the GPU renders the triangles in the order it receives as far as I know. So for transparent GUI elements, you can send one object with the triangles ordered based on the depth with the z-buffer turned off and it should render as expected.
     
    Lulucifer likes this.
  9. metaleap

    metaleap

    Joined:
    Oct 3, 2012
    Posts:
    589
    Draw calls are always processed in the order they're submitted by the engine to the GPU.

    But the z-buffer allows the GPU to skip execution of a (potentially expensive) fragment shader when it finds after the vertex stage that the fragment is already occluded by a "closer to the camera" previously written fragment.

    Consequently, CPU-side ordering of Draw calls prior to submitting them to the GPU is highly advisable and all major engines including Unity do just that.

    Hence, for all opaque (non alpha-blended) geometry that wasn't culled on the CPU side it is beneficial for the CPU side to order Draw calls submitted to the GPU "roughly" from front to back to reduce overdraw, but this has to be weighed against other performance considerations (state changes, batching) --- exactly what an engine such as Unity does.

    This is just describing the generally preferable approach to 3D sorting though, NGUIs "everything has the same depth and should be 1 draw-call and we'll need alpha-blending more than not" may have its own hacks and approaches..
     
  10. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,398
    The question here was whether triangles are ordered within a single draw call.
     
  11. hello1111

    hello1111

    Joined:
    Apr 14, 2014
    Posts:
    13
    you can send as many triangles as you want to the GPU in a single draw call.
    however to switch textures you need multiple calls. hence texture atlases.

    the GPU can use the zbuffer to provide occlusion, but triangles are always drawn by the GPU in the order they're received.