Jump to content
  • Advertisement
Sign in to follow this  
Ed Welch

OpenGL which is better rgba as uniform or varying?

This topic is 1169 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I "color" my quads using 2 different ways, first is to supply the rgba color as a uniform:

		// fragment shader
                uniform sampler2D sampler2d;
		uniform lowp vec4 color1;
		varying mediump vec2 texCoordVar;
		void main (void)
		{
			gl_FragColor = texture2D(sampler2d, texCoordVar) * color1;
		}

Then, the quads are sorted according to color and then batched together, but if the color varies a lot then you end up with a lot of draw calls.

The other way is to use a vertex color attribute:

uniform highp mat4 matrixPVM;
attribute highp vec3 inVertex;
attribute lowp vec4 inColor;
attribute mediump vec2 texCoordAttr;
varying mediump vec2 texCoordVar;
varying lowp vec4 outColor;
void main(void)
{
	gl_Position = matrixPVM * vec4(inVertex, 1.0);
	texCoordVar = texCoordAttr.st;
	outColor = inColor;
}
// fragement shader
uniform sampler2D sampler2d;
varying lowp vec4 outColor;
varying mediump vec2 texCoordVar;
void main (void)
{
	gl_FragColor = texture2D(sampler2d, texCoordVar) * outColor;
}

This has much less draw call, but it's wastefull, because I only need a color attribute for each quad, not each vertex.

Maybe, there's a third way of doing this that I haven't thought of (but this is on OpenGL ES 2.0, so I can't use any fancy stuff).

Anyone have any suggestions?

Share this post


Link to post
Share on other sites
Advertisement

Depends.

 

What you call "waste" isn't necessarily so.  If you're just drawing 2D quads (which it looks like you're drawing) then an extra 4 bytes (or worst case, 4 floats) per vertex is peanuts, and the extra performance from being able to batch draw calls is more than a fair tradeoff: memory isn't everything.

Share this post


Link to post
Share on other sites

Earning batched drawcall is a win situation, while worrying about 4 extra bytes is not an issue since vertex alignment is a much larger issue (16/32/64 bytes, any other byte alignment makes a rather large performance impact). The last fact, that you have an extra varying input to fragment function, would be the only con of this solution, but of course trivial one.

Share this post


Link to post
Share on other sites

BTW, the reason vertex color is the better solution is that your goal is the best constant-framerate and best worst-case framerate *not* the best best-case framerate.

If your game can have 1-1000 colors, then your uniform solution will use 1-1000 draw calls, which means performance is dependent on number of colors. Even if the uniform solution was faster than varyings for 1 color batch (which it probably is not), the fact that it is much slower at 1000 colors is a big problem. When you have the choice, choose constant framerate even if it is slightly slower.

----

At the risk of overcomplicating the discussion, depending on the details you might consider instancing your quads. When you instance, you can set how often a varying attribute increments, so you can supply only one color per quad (or for multiples of quads). If you are only varying color per instance, you will not see a difference vs per vertex colors. However, if you vary alot more, instancing can be a win.

Whether to instancing or not has to do with what you are doing with the quads, and how they are being moved or updated.

What are the quads? How do they change?

Edited by DaveSF

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!