Sign in to follow this  
Hnefi

Dynamic memory allocation on GPU?

Recommended Posts

Since I can't get real-time vertex displacement mapping to work on the GPU, I decided to try displacing my vertices by passing object locations to the vertex shader and calculating displacement for each vertex depending on the distance to these objects. Works great, and it seems pretty fast too. Problem is, I don't know how many objects I need to pass to the shader until runtime. Had I done this in software, it would not have been a problem - using a list or queue or whatever would let me deal with any amount of objects. However, right now I have to pass each object explicitly, putting a cap on the max amount of objects I can pass to the shader. So, I'm wondering, is there a way to pass a list or array or similar object of arbitrary size to a vertex shader and then iterate through said list/array on the GPU?

Share this post


Link to post
Share on other sites
Do you mean as uniforms or as vertex attributes ? In the former case, just supply an array and an object counter to the shader. Of course you'll still have a fixed max amount you can pass, but the GPU is not a CPU after all.

Share this post


Link to post
Share on other sites
Neat, didn't know that was possible. So I can make an array of, say, 100 2D vectors and pass them as an array to GLSL?

Could you tell me which command I should use to pass arrays to the shader like this (glUniform2fv()?) and how to declare the uniform in the shader?

Thanks for your help.

Share this post


Link to post
Share on other sites
In the shader, you can declare it like this:

uniform vec2 Objects[20];


In your CPU side code, you can write to this array using glUniform2fv(ARB):

glUniform2fv(location, count, Your_Data_Array);


Just make sure to not exceed the native uniform limit of your hardware, and that your hardware supports looping in vertex shaders.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this