Accessing OpenGL state in CUDA I just need the projection matrix

Hi,

This might be a completely stupid question, but I’ve been wondering if it’s possible to access information about the current OpenGL state through CUDA. Specifically, I would like to be able to read the current projection matrix inside a kernel. I know that it would probably be fast enough to just pass it as a kernel parameter (or to keep it in shared/constant memory), but I think it would be wonderful if such level of access is permitted. Since CUDA and OpenGL run exclusive of each other, it is possible that no graphics state exists during kernel execution anyway, my question won’t be valid then. Maybe someone from NVIDIA can comment?

Thanks for the help,
Anjul

Not a stupid question!

There’s no direct access to OpenGL state from CUDA kernels. I agree this would be convenient sometimes, but in general we try to keep CUDA as simple as possible.

You can achieve the same effect like this:
GLfloat hProjMatrix;
glGetFloatv(GL_PROJECTION_MATRIX, &hProjMatrix);

constant float dProjMatrix[16];
cudaMemcpyToSymbol(dProjMatrix, hProjMatrix, sizeof(GLfloat)*16);

The matrix state is usually maintained by the OpenGL driver anyway, so this is basically what the GL driver is doing anyway, and is no less efficient.

Thank you! As a follow up, do you think it would be faster if 1000 blocks of 256 threads each access this matrix from constant memory or would it be faster if each block loaded it into shared memory first?

(1000x256 cmem fetch cycles) VS (1000 global memory fetch cycles + 1000x256x2 cycles(smem)) ?

My guess is that cmem should better in the long run as the number of blocks increases (it at least gives a bounded estimate on the time)

[repeat post sorry]

Constant memory should be better in this case.