PDA

View Full Version : Cg faster than GLSL for non-const index array access?



hippynerd
05-27-2005, 10:12 PM
Is it just me, or is accessing a uniform array with a non-constant variable, faster under Cg than GLSL?

I'll try to post more specific details later, but the gist is that

uniform float array[10];

void main(){
int i = 4;
float val = array[i];
}

Will drop a framerate from 400 to 40fps.

Under a simmilar example in CG, around 30,000 verticies are rendered without a significant performane decrease, when they access a uniform array in the same manner.

The reason I want to do this is I would like to have a lookup table for a certian custom mathematical operation I'm using. I could use a texture for the lookup, but then this lookup would have be be performed in the texture shader, and I'd rather not have this, as a number of mathematical operations need to be performed based on the lookup value.

Korval
05-28-2005, 12:41 AM
Talk to the person implementing your glslang. Since you spoke of vertex texturing, that's probably nVidia. Complain to them about it. Send in sample code.

That's the only way glslang implementations will get better.

Overmind
05-28-2005, 12:45 AM
I would also try to test with a really variable index. My guess is that the Cg compiler realizes that in your example the index is really a constant and optimizes it away, while the GLSL compiler does not.

hippynerd
05-28-2005, 07:21 AM
The Cg code is actaully a proper variable (calculated as the result of a few maths operations on other input uniforms). So yeah if anything the GLSL example should be optmised to be even faster.

Anyway I will be sending some code when I have a proper example to the appropriate people I love GLSL and really hope it continues to become more flexible and optimised.

Overmind
05-29-2005, 04:02 AM
Still, if the value depends only on uniforms, it could be seen as runtime constant by the compiler, so the array lookup can be replaced by a single uniform parameter that is recalculated on the cpu every time you cange a uniform.

I don't know if the Cg compiler actually does this, but it is possible. Try to look at the assembly output of the Cg compiler. Perhaps the issue is something completely different...

But you are right, it would be nice if GLSL would also produce this kind of optimised code.