Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

How do I pass to my vertex shader an array of N elements?

From what I see I can only pass float arrays of 3|4 elements, vectors 2|3d and matrices, but I want to pass a POD array and use it like this:

void ShaderFunction(float* array,int count)
{
    for (int i = 0;i < count;i++) 
    {
        DoJob(array[i]);
    }
}

So far all of my tests fail.

share|improve this question

3 Answers 3

up vote 1 down vote accepted

You can define an array in GLSL like so:

uniform float array[size];  

Then passing in depends on what language you're using. For Java (LWJGL):

FloatBuffer floatBuffer = BufferUtils.createFloatBuffer(size);
floatBuffer.put(x);
floatBuffer.put(y);
...
int location = GL20.glGetUniformLocation(shaderProgram, "array");
GL20.glUniform1f(location, floatBuffer);

Or in C++

GLfloat floatArray[size] = {...};
int location = glGetUniformLocation(program, "array");
glUniform1fv(location, size, floatArray);

Then you can use it:

void ShaderFunction(int count)
{
    for (int i = 0;i < count;i++) 
    {
        DoJob(array[i]);
    }
}
share|improve this answer
    
Thank you.So is it still needed to send vertices as usual or could i define a buffer with vertex pointer and iterate through it? –  user1010005 Apr 10 '12 at 22:53
1  
I'd send the vertices as usual. If you need access to the vertices you can access them as well. See this handy cheat sheet (PDF) to see some common variables and functions. Including vec4 gl_Position; which is your vertex position in the vertex shader. –  Byte56 Apr 10 '12 at 22:57
2  
Please note that your array size must be defined at compile time. GLSL does not allow variable size arrays. So you must pick a size that you think will work, and have CPU side code that ensures you don't exceed that size. –  Byte56 Apr 10 '12 at 23:00
    
Thanks alot.I thought that i could drop the CPU load for the rendering part but now i see that its impossible ;) –  user1010005 Apr 10 '12 at 23:03
1  
Perhaps the way you were thinking of doing it isn't possible, but I'm sure there are numerous ways to take a load off the CPU for your game. –  Byte56 Apr 10 '12 at 23:08

Short answer: You can't.

Long answer: Need more info what you are trying to do and why.

share|improve this answer
    
I want to move my tile map rendering code to the GPU if its possible. (Sorry if its stupid i warned in the OP that i just set up my shader system but it has been edited) –  user1010005 Apr 10 '12 at 22:39
1  
Just use texture to hold all data and skip vertices all together. java-gaming.org/topics/rendering-tiles-fast-isometric-too/25292/… –  kalle_h Apr 11 '12 at 9:44

Another way you might do this is by using a floating point texture - you would create a one-channel FP texture of the required size, upload your data to it, and sample from that in your shader (remember that vertex shaders can also sample from textures).

Where this is going to cause you huge trouble - irrespective of your chosen solution - is in terms of instruction counts. GPUs are not really designed around being used for this kind of programming, and if your count value is sufficiently high and/or if it varies between calls your performance is at risk of falling off the edge of a cliff.

Since you say that you're looking to do this in a vertex shader, it may yet turn out to be more performance-efficient (and even advantageous from a code-cleanliness perspective) to leave it running on the CPU instead.

share|improve this answer
    
That sounds like an amazing idea.But im not sure if i can pull that off at the moment though (sounds tough ;D) –  user1010005 Apr 10 '12 at 23:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.