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.0
or 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!
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With