Dark Bit Factory & Gravity
PROGRAMMING => General coding questions => Topic started by: frea on March 12, 2008
-
Hi.
is it normal that this is slow ( ~0.5-1fps )? :
float a[ 3 ];
a[ 0 ] = 0;
a[ 1 ] = 0;
a[ 2 ] = 0;
int b = 0;
if( a[ b ] == 0 ) gl_FragColor = vec4( 1, 1, 1, 1 );
If i replace the if by
if( a[ 0 ] == 0 ) (....)
I get normal fps again.
I understand that the compiler can be unable to convert first if to the second, but why i get ~100x framedrop ?
-
If the compiler thinks the hardware wont cope (if it cant spot the obvious here and leaves the array access as dynamic), it could signal the drivers to run in software. Try updating drivers maybe? One other thing to try is using correct types. You may be avoiding a compiler rule by not having floats (0.0). Its a wild stab in the dark but worth a try. Lastly check the string returned by compile and link...you may find a complete explanation there. Generally under shader model 110 arrays are not first class citizens and cause huge problems. Under shader model 120 (#version 120) the code usually compiles and links fine but then often hardware doesn't cope.
I guess it just an example so my next syggestion is no use, but if you really want an array of 4 items, couldnt you use vec4 ?
-
You were right, it was running in software.
I need bigger arrays that 4, more that 16 also :).
Seems i will have to emulate arrays somehow, or rethink the algorithms.
Thanks.
-
Frea,
dead right, arrays are not first class citizens until recently and most cards still dont support it well (if at all). This is huge problem in writing shaders at the moment. BTW its even worse. Nvidia allow illegal syntax for array declaration and then people think ati suck - but its because Nvidias GLSL compiler is very dodgy, being derived from their Cg compiler and ... well, not correctly.
Generally, in order to avoid this problem, arrays can be put in textures - if you only need them in pixel shaders. Otherwise, rethink and use vectors or matrices.
Its a real pain right now.
-
Thanks Taj for sharing your hard-won knowledge! Let's hope things get better as technology improves...
K+
Jim
-
...
This is huge problem in writing shaders at the moment. BTW its even worse. Nvidia allow illegal syntax for array declaration and then people think ati suck - but its because Nvidias GLSL compiler is very dodgy, being derived from their Cg compiler and ... well, not correctly.
....
Its a real pain right now.
I've one solution, we all need to use only Nvidia cards ;D
(Just kidding)
Quote from "OpenGL Shading Language - second edition" page 73:
3.2.6 Arrays
Arrays of any type can be created.
It's not the truth, right ???
-
previously i thought of glsl as beeing powerful, but damn! i can't do array indexing nor looping. A simple for makes it run in software mode.
At least i hope i can write long programs. i'll need to unroll all loops ;D.
-
Ill say this again : GLSL is not C! Kinda makes you admire people who make shaders work though doesnt it :-).
The standard has only one chance of succeeding : if board manufacturers implement it correctly. Otherwise nobody will use it. You see Nvidias little game here. Pissed at the fact CG wasnt chosen to be the OpenGL standard (giving them ... oh an 18 month lead on ATi) but instead a good standard proposed by 3dlabs, they have deliberately placed less resources in getting glsl right and in some cases deliberately made choices that hijack it. Its despicable. Its business.
Meanwhile ATi are much more correct but fail in lots of areas (like arrays).
Whilst its true you can create an array of any type, you can only do so with a constructor, not a definition. Therefore:
float a[2]={1.0,2.0}; is completely illegal but Nvidia allow it, screwing us all by undermining the standard and locking you into their hardware with your shader.
Meanwhile Ati allow the legal syntax:
float a[2]=float[2](1.0,2.0);
Great! Only their hardware doesnt support it!!!
So in the end they are as bad as each other when it comes to glsl.
So yes, Rbraz, you can create - through constructor syntax only - arrays of any type. They wont work though. And no array declarations in C form are legal even in next gen shaders. Don't ask me why but its so. But dont misunderstand - you should be able to treat arrays as first class citizens in version 120 - but it simply does not work...there is specs and reality. I think your GLSL book also says there is a noise function...but only 3dlabs cards ever implemented it.
Taj
-
Yeah, that shader noise function would be really great, and I was talking to myself - "I must use this!" :P
-
LOL imagine all the clouds/terrains and marble textures we would see in 4ks :-) I agree it would be damn useful :-(
-
taj did you mean that ati doesn't support arrays or that syntax? the syntax works for me.
anyway, no loops, no arrays = long code, which is also forbidden! That's cool i have super-fast hardware, but i can't use it at all :/.
Mabye i overestimated shaders, but 'everyone' says they are great and something super new, fast etc. and yet i can only do _very_ simple stuff with it.
I at least hope CUDA is better.