Search code examples
c++opengloptimization2dvertex-shader

OpenGL: Setting Z depth as a uniform vs. stored redundantly within vertices


A 2D rendering engine in OpenGL will usually render 2D images using quads, which contain 4 vertices each at the same Z-depth. These vertices usually are composed of (X, Y, Z), and (U, V) for texture coordinates.

However, since all 4 vertices for any given image will always occupy the same depth, would it be better to only upload (X, Y, U, V) per vertice and set their Z depth as a separate uniform in the shader, or would the cost of updating a uniform every 4 vertices be too significant in practice?


Solution

  • You could definitely do it any way you like. It would not make a single bit of detectable difference - it is really virtually impossible for any change you make to the 4 vertices to have any effect on performance, when you will be running in the order of a few million fragment shader instances for the same 4 vertex shader executions.

    Personally, I don't use attributes at all for quads - I use vertex ID to switch between 4 fixed values, which saves me from having to use a buffer at all.