GLSL: Why is const int array so much slower than normal array?

I have an OpenGL shader written in glsl. I have a globally defined array at the top of the header like this:

const int permutations[256] = int[256](
    64 ,  34 ,  36 , 137 , 120 , 122 , 246 ,  46 ,  79 ,  10 ,  37 , 181,
    ...
    );

When I start the program, it is extremely slow, meaning I have only like 1 frame per second, when displaying a simple teapot using the shader.

However, as soon as I remove the "const" from the above declaration, everything works fine, and I'm back at interactive frame rates.

So, the "problem" is actually already solved... but I was wondering, why this could be the case? Very counterintuitive, as in my experience, using consts usually makes programs faster.


Update

I actually tried to use a Uniform first instead of the const int array, with exactly the same code everywhere else. However, in this case compiling the shader crashes with:

Internal error: assembly compile error for fragment shader at offset
34609:
-- error message -- line 651, column 22:  error: invalid local parameter number

Answers


That's very strange. Most probably you're having driver issues. I've ran included shader code on my Nvidia 540M under windows, and let it run in cycle. In that cycle I called shader program once and measured time (also I had checked with transform feedback if shader is working correctly). Most of cycles ended in 2-3 miliseconds, with some peaks at 25 ms. However removing const identifier had no effect on performance. These are quite small values to measure, but from your description the performance hit of const should be observable even here.

#version 150
const int permutations[256] = int[256](
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
...
246, 247, 248, 249, 250, 251, 252, 253, 254, 255
);

flat out int num1[32];

void main(){
    for(int i = 0; i < 32; i++){
        num1[i] = (permutations[2*i]+permutations[2*i+1])*(permutations[2*i+2]+permutations[2*i+3]);
    }
}

Maybe if you wouldn't mind I could try to run your program on my hardware, but it would have to be compiled for windows and please include all dynamic libraries.


Need Your Help

jQuery plugin working fine on localhost, but on web server messes with bg-image

javascript jquery html css

I have build a small lightweight plugin to display a cookie message. Testing on my localhost works fine, no web pages are affected, however, when I send the files over to the server, it is either:

Treeview Control - ContextSwitchDeadlock workarounds

c# visual-studio-2008 treeview control

I have built a treeview control that lists the directory structure of any drive or folder. However, if you select a drive, or something with a large structure of folders and sub folders the control

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.