Is there some way to process stencil values stored inside the kernel? I mean, I’m rendering some GL_LINE_STRIP and I store the stencil values for the render. I want to process these stencil values in a kernel in order to compute an algorithm of mine. Does anybody knows if CUDA allows to get the values stored in the stencil buffer.
I know that the solution could be glReadPixels and then process them inside the kernel but I would like to avoid CPU->GPU->CPU->GPU.
No, there is currently no way to read directly from the frame buffer in CUDA (color or stencil). However, you can do a glReadPixels() from the frame buffer to a pixel buffer object, which can be mapped and read from CUDA. The postProcessGL sample in the SDK shows how to do this. The copy happens in GPU memory, so there is no transfer back to the CPU.
I check the example in the SDK postProcessGL to use inside a CUDA kernel a PBO from openGL. I think I understood the code but with my code (which I made some changes) I get an execution error of segmentation fault. I think all is clear but I can’t find the error.
void init()
{
void *data=malloc(SCREEN_RES_X*SCREEN_RES_Y*sizeof(GLfloat));
//create the PBO of 1 float per pixel
glGenBuffers(1,&pboSource);
glBindBuffer(GL_ARRAY_BUFFER,pboSource);
glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*SCREEN_RES_X*SCREEN_RES_Y,data,GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER,0);
//register the PBO in CUDA
cutilSafeCall(cudaGLRegisterBufferObject(pboSource));
//alloc mem to final values
cutilSafeCall(cudaMalloc((void**)&finalStencilDevice, sizeof(int)*SCREEN_RES_X*SCREEN_RES_Y));
}
void run()
{
init();
//render my scene
render();
callCUDAFunction();
}
void callCUDAFunction()
{
// tell cuda we are going to get into these buffers
cutilSafeCall(cudaGLUnregisterBufferObject(pboSource));
// activate destination buffer
glBindBuffer(GL_ARRAY_BUFFER, pboSource);
//read
glReadPixels(0,0,SCREEN_RES_X,SCREEN_RES_Y,GL_STENCIL_INDEX,GL_FLOAT,
NULL);
// Done : re-register for Cuda
cutilSafeCall(cudaGLRegisterBufferObject(pboSource));
float *stencil;
//map values from PBO
cutilSafeCall(cudaGLMapBufferObject((void**)&stencil, pboSource));
dim3 dimBlock(BLOCK_DIM_X,BLOCK_DIM_Y);
dim3 dimGrid(SCREEN_RES_X/dimBlock.x,SCREEN_RES_Y/dimBlock.y);
//call kernel
stencilJoin<<<dimGrid,dimBlock>>>(finalStencilDevice,stencil);
cutilSafeCall(cudaGLUnmapBufferObject(pboSource));
// detach from CUDA
cutilSafeCall(cudaGLUnregisterBufferObject(pboSource));
}
The error is produced in this line glBindBuffer(GL_ARRAY_BUFFER, pboSource).