views:

24

answers:

3

Is it possible to use shader for calculating some values and then return them back for further use?

For example I send mesh down to GPU, with some parameters about how it should be modified(change position of vertices), and take back resulting mesh? I see that rather impossible because I haven't seen any variable for comunication from shaders to CPU. I'm using GLSL so there are just uniform, atributes and varying. Should I use atribute or uniform, would they be still valid after rendering? Can I change values of those variables and read them back in CPU? There are methods for mapping data in GPU but would those be changed and valid?

This is the way I'm thinking about this, though there could be other way, which is unknow to me. I would be glad if someone could explain me this, as I've just read some books about GLSL and now I would like to program more complex shaders, and I wouldn't like to relieve on methods that are impossible at this time.

Thanks

A: 

My best guess would be to send you to BehaveRT which is a library created to harness GPUs for behavorial models. I think that if you can formulate your modifications in the library, you could benefit from its abstraction

About the data passing back and forth between your cpu and gpu, i'll let you browse the documentation, i'm not sure about it

samy
+1  A: 

This is easily possible on modern graphics cards using either Open CL, Microsoft Direct Compute (part of DirectX 11) or CUDA. The normal shader languages are utilized (GLSL, HLSL for example). The first two work on both Nvidia and ATI graphics cards, cuda is nvidia exclusive.

These are special libaries for computing stuff on the graphics card. I wouldn't use a normal 3D API for this, althought it is possible with some workarounds.

testalino
+1  A: 

Great question! Welcome to the brave new world of General-purpose computing on graphics processing units (GPGPU). What you want is possible with pixel shaders. You load texture (load data) apply shader (do computations) and then render to texture and load it from GPU to main memory (this is missed link in your question). But this technology developed and there were tool sets created for this, most notable are: OpenCL and CUDA.

They greatly aid GPGPU so that this sort of programming looks almost as CPU programming. They do not require any 3D graphics experience (but still preferred :) ). You don't need to do tricks with textures, you just load arrays into GPU memory. Processing algorithms are written in C or it's slightly modified version. Latest version of CUDA supports C++.

So I recommend to start with CUDA, since it is most mature one: http://www.nvidia.com/object/cuda_home_new.html

Andrey
As testalino wrote, CUDA is nVidia specific. As I am running Radeon, wouldn't I suffer from slow-down, or is it even possible to run CUDA on Radeon? Is OpenCL less effective or why you don't recomend it to use as first?
Raven
@Raven Yes, CUDA is completely nVidia specific. If I was to choose i would prefer CUDA+nVidia. Since you have ATI you have to pick OpenCL. As far as i know CUDA is more mature, tools are better, etc. In terms of performance i don't think there is a significant difference.
Andrey
@Raven, I would choose Direct Compute if I was using Direct X already, Open CL if I was using Open GL. I would never use CUDA for the reason stated. You can't make sure that everyone that uses your application has an NVIDIA card.
testalino
thanks for opinion. For the reason you stated as I hate those physx games I can't fully play + I was learning openGL not CG nor DirectX, I think that openCL will suite my needs best.
Raven
@Raven if you have ATI then this is the only option.
Andrey