OpenGL contexts are tied to the process that created them. You can't share CUDA mapped OpenGL objects between processes, because the OpenGL context sort of is the "owner" of the data and when you map it into CUDA you're just lending it. OpenGL does a lot of bookkeeping and higher level management behind the scenes.
There are certain internal structures in OpenGL and constraints that must be adhered to when using OpenGL CUDA interop. For example you must not actively use an OpenGL object currently mapped to CUDA as data source or target. For example a call to glTexImage2D
or glBufferData
may reuse the front facing ID, but use a different buffer, associated with a different internal ID. glTexSubImage2D
and glBufferSubData
may need to create in-situ copies to satisfy synchronization point requirements and so on. If some process to which the OpenGL state tracker has no access does stuff it its memory, things will break.
The usual OpenGL CUDA interop sequence is
- Do something in OpenGL
- Unbind objects from active use in OpenGL
- Map objects to CUDA
- Do something in CUDA
- Unmap objects from CUDA
- back to 1
Now what you must do is, instead of inter processes mapping of OpenGL objects, create an area of proxy memory shared between the processes. After doing something with OpenGL you map the OpenGL object to CUDA, use cudaMemcpy to copy the data into the proxy memory and unbind the OpenGL object.