Use case for cudaGraphicsGLRegisterImage with cudaGraphicsRegisterFlagsWriteDiscard?


I’m fairly new to CUDA, but I’ve managed to display something generated by a kernel on the screen using OpenGL. I’ve tried serveval approach :

  1. Using a PBO and an OpenGL texture;
  2. Using a OpenGL texture as a CUDA surface and rendering on a quad;
  3. Using a renderbuffer as a CUDA surface and rendering using glBlitFramebuffer.

All of them worked, but, while implementing #2, I erroneously set the hint as cudaGraphicsRegisterFlagsWriteDiscard. Since all of the data will be generated by CUDA, I thought this was the correct option. However, later I realized that I needed a CUDA surface to write to an OpenGL texture, and when you use a surface, you need the LoadStore flag.

So basically my question is this : Since I cannot write to a CUDA texture in a kernel, and to write to an OpenGL texture you need a CUDA surface, what is the use case of cudaGraphicsRegisterFlagsWriteDiscard in cudaGraphicsGLRegisterImage?


For those interested, I posted the same question on StackOverflow and received an answer.

Basically, when using cudaGraphicsRegisterFlagsWriteDiscard, you will be able to write to the OpenGL texture, but not from a kernel (to do that, you need a surface). However, you can use cudaMemcpyToArray to write to it. In the PostProcessGL example, a simple float array is allocated and written to by the kernel. That array is then copied to the OpenGL texture using cudaMemcpyToArray.

Performance wise, it is probably faster to use a surface since there’s no coping involved.