2
votes

I'm developing application with open GL / ES for desktop/mobile (android/iOS) platform.
And I'm using const arrays in my vertex shader code. I test it on my nv8800GT with and without const arrays and saw no difference. But I read, that const arrays could be MUCH slower than uniforms.

const float offset_s[4] = float[4](0.625, 0.625, 0.75, 0.85);

The question is - should I avoid const array in uniform favor? Or everytrhing ok now? Drivers fixed and so on...

1
Const arrays often are the same thing as uniforms. They take up the same storage limits, and in some newer drivers (e.g. NVIDIA) if you query the list of uniforms after compiling a shader with a const array in it, you will see that it generates a specially named array of uniforms for them.Andon M. Coleman
For instance: const vec4 pos [4] = vec4 [] ( ... ) becomes a uniform named vec4 _main_pos_0[0], with size=4. Because it is used in the function: main (...).Andon M. Coleman
So this means - I should count them when work with GL_MAX_VERTEX_UNIFORM_VECTORS ? I mean - if I already waste all uniform space - than I can't add a single const float?tower120
It is not fleshed out this way in the official specification, but of the implementations I have worked with this turns out to be the case. NV's drivers make the behavior very noticeable by actually generating an automatic uniform variable for const qualified variables at global scope. It probably is better to use uniforms instead of global const arrays to ensure consistent behavior across all implementations. Also, that limitation you mentioned only applies to the vertex shader stage - you can do this sort of thing in any stage. Each one will have its own different limitation.Andon M. Coleman
I have only two stages :)) vertex and fragment. Well, thanks, I'll keep that in mind.tower120

1 Answers

1
votes

I am experiencing weird behaviour with const and non-const arrays in GLSL. Changing const arrays to non-const arrays provided a small speedup in some shaders. On the other hand, removing the "const" keyword in other shaders resulted in a massive performance drop (from ~2ms down to ~20ms for the render pass). The behaviour seems to be very inconsisent and my guess is that it's a driver bug.
My GPU is an Nvidia GTX 460 and I'm using the driver version 358.50 (similar behaviour observed with an older driver).