Author Topic: indexing arrays in shader  (Read 6829 times)

0 Members and 1 Guest are viewing this topic.

Offline frea

  • C= 64
  • **
  • Posts: 61
  • Karma: 2
    • View Profile
indexing arrays in shader
« on: March 12, 2008 »
Hi.
is it normal that this is slow ( ~0.5-1fps )? :
Code: [Select]
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
Code: [Select]
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 ?
Nananan.

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: indexing arrays in shader
« Reply #1 on: March 12, 2008 »
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 ?
« Last Edit: March 12, 2008 by taj »
Challenge Trophies Won:

Offline frea

  • C= 64
  • **
  • Posts: 61
  • Karma: 2
    • View Profile
Re: indexing arrays in shader
« Reply #2 on: March 12, 2008 »
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.
Nananan.

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: indexing arrays in shader
« Reply #3 on: March 13, 2008 »
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.
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: indexing arrays in shader
« Reply #4 on: March 13, 2008 »
Thanks Taj for sharing your hard-won knowledge! Let's hope things get better as technology improves...
K+
Jim
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2756
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: indexing arrays in shader
« Reply #5 on: March 13, 2008 »
...
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:
Quote
3.2.6 Arrays
Arrays of any type can be created.

It's not the truth, right ???


Challenge Trophies Won:

Offline frea

  • C= 64
  • **
  • Posts: 61
  • Karma: 2
    • View Profile
Re: indexing arrays in shader
« Reply #6 on: March 13, 2008 »
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.
Nananan.

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: indexing arrays in shader
« Reply #7 on: March 13, 2008 »
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
« Last Edit: March 13, 2008 by taj »
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2756
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: indexing arrays in shader
« Reply #8 on: March 13, 2008 »
Yeah, that shader noise function would be really great, and I was talking to myself - "I must use this!"  :P
Challenge Trophies Won:

Offline taj

  • Bytes hurt
  • DBF Aficionado
  • ******
  • Posts: 4810
  • Karma: 189
  • Scene there, done that.
    • View Profile
Re: indexing arrays in shader
« Reply #9 on: March 13, 2008 »
LOL imagine all the clouds/terrains and marble textures we would see in 4ks :-) I agree it would be damn useful :-(
Challenge Trophies Won:

Offline frea

  • C= 64
  • **
  • Posts: 61
  • Karma: 2
    • View Profile
Re: indexing arrays in shader
« Reply #10 on: March 13, 2008 »
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.
Nananan.