Pass array into shader


what are the good ways to pass a large array (obtained by cuda code) to fragment/vertex shader?


If I understand correctly, you want to pass the data generated to a buffer using cuda code to the fragment/vertex shader.

I would recommend using uniform buffers or texture buffers. The former provides usually faster access, the later provides much larger storage.

so uniform data are allocated to shared memory from cuda point of view?

I have tried set a large uniform array and copy to it, but it does not work.

So binding texture is the only way? I prefer something like “cudaMemcpy” to pass the data to the shader…

so uniform data are allocated to shared memory from cuda point of view?

No, he said uniform buffers.

I prefer something like “cudaMemcpy” to pass the data to the shader…

But using the buffer as a buffer texture or a uniform buffer doesn’t involve a memory copy. You just use CUDA to write directly to the buffer, then use the buffer as a uniform buffer or a buffer texture and read from your shader.

One thing worth thinking about: since you are using CUDA, it means you are using NVIDIA hardware… so I strongly suggest you look into GL_NV_shader_buffer_load. The basic setup you would have is the following:

Create buffer object so that CUDA and GL can share it.
Let CUDA do it’s thing
API calls to let GL use the buffer object
Use GL_NV_shader_buffer_load to access buffer object contents from shader…

Looking at it looks like one does this:

  /*-- create GL buffer object -- */

  /** --register to CUDA --*/  

   /* -- map  buffer object to CUDA */

   /* CUDA call to compute */

   /* unmap buffer object from CUDA*/

   //use buffer object freely in GL... the best one if by far to 
   //fetch the Buffer objects GPU address and use that value as a uniform,
   //see GL_NV_shader_buffer_load.


naturally, likely you will want to have 2 buffer objects flying around, one where you compute to, and the other that you use to render from and swapping between the two.

Since you are using CUDA, I strongly recommend that you also use GL_NV_shader_buffer_load, as it allows much more flexible and natural access to the buffer object data… binding the buffer object to a uniform (known as uniform buffer objects and bindable uniforms) has size limitations and texture buffer objects (i.e. a buffer object becomes the data source for a large array) forces one to have that the data is just an array of simple data types: AvecN where A=float, int8, int16, int32, uint8, uint16 or uint32 and N=1,2 or 4 (GeForce 4xx cards support N=3 though).

hey, guys, got plenty stuff here! many thanks for these information!

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.