3
votes

So I'm trying to send an array of values to my fragment shader- The shader reads values from a texture and depending on the value currently being read by the texture, I want to retrieve a value from the array-

I am able to cast the value (u.r) to an int using int(u.r), but when I actually put that into the array index to find the value, it says that the integer isn't a constant, so I can't use it...

  • ERROR: 0:75: '[]' : Index expression must be constant -

Is there a better way of sending arrays of values to the shader?

Here is some of the code- as you can see, the array "tab" is what I'm looking at mostly

    <script id="shader-fs" type="x-shader/x-fragment">


#ifdef GL_ES

precision highp float;

#endif

  uniform sampler2D uTexSamp;

uniform sampler2D uTabSamp;
  uniform float dt;
  uniform float dte;
  uniform float dth2;
  uniform float a;
  uniform float nb;
  uniform float m;
  uniform float eps;
    uniform float weee;

 uniform float tab[100];


    //uniform float temp;

  uniform int fframes;
  uniform vec2 vStimCoord;
  varying vec2 vTexCoord;

  const float d = 0.001953125; // 1./512.

void main(void) {

   vec4 t = texture2D(uTexSamp, vTexCoord);
   float u = t.r,  v = t.g,  u2 = t.b,  v2 = t.a;


    //const mediump int arrindex = floor(u*10 + u2);
    //float sigvaluetab = tab[arrindex];

    u += u2/255.;   v += v2/255.;

   //u += u2 * 0.003921568627451;
   v += v2 * 0.003921568627451;

   //Scaling factors
   v = v*1.2;
   u = u*4.;


   float temp =  (1.0 / (exp(2.0 * (u-3.0)) + 1.0)); // (1-tanh(u-3)) * 0.5



    //const mediump int utoint;
    //utoint = int(u);
    //for(int index = 0; index< 50; index++)

    int u2toint;
    u2toint = int(u2);

  //  int arrindex = utoint*10 + u2toint;
    float sigmoid = tab[u2toint];//(tab[5] + 1.);
    //float sigmoid= temp;//tab[arrindex];

   float hfunc   = sigmoid * u * u;
   float ffunc   = -u +(a - pow(v*nb,m))*hfunc ;

   float gfunc = -v;
   if (u  > 1.0) {   //u-1.0 > 0.0
       gfunc += 1.4990;
   } 

... MORE STUFF UNDER, BUT THIS IS THE IDEA

1
I feel obligated to point out that highp is not required by the OpenGL ES 2.0 specification to work in a fragment shader. For compliance, you need to test the pre-processor define GL_FRAGMENT_PRECISION_HIGH before using highp in a fragment shader; it will be 1 if the implementation supports it, undefined otherwise.Andon M. Coleman

1 Answers

2
votes

Fragment shaders are tricky, unlike vertex shaders where you can index a uniform using any integer expression in a fragment shader the expression must qualify as const-index. This can go as far as to rule out indexing uniforms in a loop in fragment shaders :-\

        GLSL ES Specification (version 100) - Appendix A: Limitations for ES 2.0 - pp. 110

                       Specification Excerpt

Many implementations exceed these requirements, but understand that fragment shaders are more restrictive than vertex shaders. If you could edit your question to include the full fragment shader, I might be able to offer you an alternate solution.

One solution might be to use a 1D texture lookup instead of array. Technically, texture lookups that use non-const coordinates are dependent lookups, which can be significantly slower. However, texture lookups do overcome limitations of array indexing in GLSL ES.