Ovilia
Ovilia

Reputation: 7256

const float array in WebGL shader

I'm using THREE.js with WebGL shader. I want to declare an array of float numbers in fragment shader. The GLSL constant float array is defined like:

#define KERNEL_LENGTH 9
const float kernel[KERNEL_LENGTH] = {
    1.0/16.0, 2.0/16.0, 1.0/16.0,
    2.0/16.0, 4.0/16.0, 2.0/16.0,
    1.0/16.0, 2.0/16.0, 1.0/16.0
};

I've also tried with:

#define KERNEL_LENGTH 9
const float kernel[KERNEL_LENGTH] = float[KERNEL_LENGTH](
    1.0/16.0, 2.0/16.0, 1.0/16.0,
    2.0/16.0, 4.0/16.0, 2.0/16.0,
    1.0/16.0, 2.0/16.0, 1.0/16.0
);

But with WebGL, neither of them works. Error information:

ERROR: 0:44: 'kernel' : arrays may not be declared constant since they cannot be initialized ERROR: 0:44: '=' : syntax error

So how should I define an array of const float?

Upvotes: 3

Views: 8295

Answers (2)

Ovilia
Ovilia

Reputation: 7256

Just a walk-around:

float kernel[KERNEL_LENGTH];
kernel[0] = kernel[4] = kernel[20] = kernel[24] = 1.0/273.0;
kernel[1] = kernel[3] = kernel[5] = kernel[9] = kernel[15] = kernel[19] 
    = kernel[21] = kernel[23] = 4.0/273.0;
kernel[2] = kernel[10] = kernel[14] = kernel[22] = 7.0/273.0;
kernel[6] = kernel[8] = kernel[16] = kernel[18] = 16.0/273.0;
kernel[7] = kernel[11] = kernel[13] = kernel[17] = 26.0/273.0;
kernel[12] = 41.0/273.0;

Upvotes: 2

bjorke
bjorke

Reputation: 3305

pass the values as uniforms, rather than trying to do what it tells you you cannot: initialize an array as constants.

Upvotes: 1

Related Questions