Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring constants instead of literals in vertex shader. Standard practice, or needless rigor?

In a vertex shader, there is of course a limited amount of uniform storage allowed, and it is my understanding that different systems may implement GLSL in slightly different ways in terms of compiling code. I've heard the recommendation to use constants instead of writing out literals in the vertex shader code.

For instance, the following code could supposedly result in a reduction in available uniform storage. (I don't quite understand how.)

Example 1: With literals

vec4 myVector = vec4(1.0, 0.0, 0.0, 1.0);

To my understanding, there is the possibility that each use of 1.0or 0.0 takes up some amount of the uniform storage space. The recommendation is therefore to turn that previous code into something like the following:

Example 2: With constants instead of literals

const float zero = 0.0;
const float one = 1.0;

vec4 myVector = vec4(one, zero, zero, one);

Does anyone understand the argument behind what's going on? I'm not having any problems with code, I'm just trying to understand the stuff properly so that I don't have problems in the future.

My formal question is the following: specifically for the iOS platform using OpenGL ES 2.0, is the best practice to write out the thing with the literals (example 1), or with constants (example 2). Should I spend my time writing things out with constants each and every time, or should I write out literals and only use constants if the vertex shader fails to compile properly?

Thanks!

like image 240
Mike Avatar asked Oct 05 '22 14:10

Mike


1 Answers

Regarding Kimi's mention about not finding anything in the spec, Appendix A-7 of The OpenGL® ES Shading Language spec does include the following:

When calculating the number of uniform variables used, any literal constants present in the shader source after preprocessing are included when calculating the storage requirements. Multiple instances of identical constants should count multiple times.

This is probably the source of the recommendation in OpenGL® ES 2.0 Programming Guide that Kimi quotes.

However, the spec does not mandate this restriction, and presumably any implementation is free to improve on it, but I cannot find anything either way regarding the iOS GL drivers.

I'm curious, did anyone actually follow up on the ideas of overloading a sample shader with literals, in an attempt to reach any potential maximum uniform limit?

(Sorry...I had intended to post this answer as a comment to Kimi's answer, but don't have the required 50 Rep points yet).

like image 86
Bill Hollings Avatar answered Oct 10 '22 03:10

Bill Hollings