I wondering how I am supposed to loop through an array from a shader. I've read a lot of things but I'm still quite confused.
Here is the JavaScript that creates the array:
var particles = []; // I'm creating an array of x and y coordinates
for (var i = 0; i < 100; i++) {
particles.push([
Math.floor(Math.random()*1000) + .5,
Math.floor(Math.random()*1000) + .5
]);
}
gl.uniform2fv(gl.getUniformLocation(glProgram, 'particles'), particles);
And here is a piece of my fragment shader:
uniform vec2 particles;
void main( void ) {
bool found = false;
for (int i = 0; i < 100; i ++) {
if (particles[i].x == 1.0) {
found = true;
}
}
}
The error I'm getting is "field selection requires structure or vector on left hand side". What am I missing?
particlesin your shader is incorrect, it should probably beuniform vec2 particles[100];.uniform vec2 particles[129]where will be many GPUs that can't run the shader because they support a maximum array size of 128. (currently 1 of 4 will fail) Typically if you need random access to data you put that data in a texture..xof 1.0 before the shader steps. You could create separate buffers. That way you could avoid the linear search.