Converting cudaDecodeGL to work with GLFW

cudaDecodeGL in the CUDA_Samples directory is designed to work with the freeglut library. I’m trying to convert it to use GLFW. I changed the beginning initializations, but much of the code from the example stays the same. For example, the render step performs the commands:

glClear(GL_COLOR_BUFFER_BIT);

// load texture from pbo
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, gl_pbo_[field_num]);
glBindTexture(GL_TEXTURE_TYPE, gl_texid_[field_num]);
glTexSubImage2D(GL_TEXTURE_TYPE, 0, 0, 0, nTexWidth_, nTexHeight_, GL_BGRA, GL_UNSIGNED_BYTE, 0);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);

// fragment program is required to display floating point texture
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader_);
glEnable(GL_FRAGMENT_PROGRAM_ARB);
glDisable(GL_DEPTH_TEST);

float fTexWidth = (float)nWidth_ / (float)nTexWidth_;
float fTexHeight = (float)nHeight_ / (float)nTexHeight_;

glBegin(GL_QUADS);

glTexCoord2f(0, (GLfloat)nTexHeight_);
glVertex2f(0, 0);
glTexCoord2f((GLfloat)nTexWidth_, (GLfloat)nTexHeight_);
glVertex2f(1, 0);
glTexCoord2f((GLfloat)nTexWidth_, 0);
glVertex2f(1, 1);
glTexCoord2f(0, 0);
glVertex2f(0, 1);

glEnd();
glBindTexture(GL_TEXTURE_TYPE, 0);
glDisable(GL_FRAGMENT_PROGRAM_ARB);

After calling this render command, the original freeglut code calls the

glutSwapBuffers();

command and a video frame is rendered to the output window. So in my GLFW-version, I retain the above render steps (as well as the base frame decoding logic in the sample code). I have confirmed that a frame is getting read in and decoded and even mapped to the interop buffer. But when I call what I think are the equivalent GLFW buffer swap commands:

glfwSwapBuffers(window);
glfwPollEvents();

no video frame shows up.

Has anyone successfully implemented a GLFW version of the cudaDecodeGL example code? I’m uncertain what I’m doing wrong and am desperately seeking an example showing how to get this working.

Hi efpkopin,

Can you list how you are executing your new program, with the same flags with a working version of the example? The rendering code looks fine, so I’m unsure that is the issue. And it sounds like the image is getting pumped through the decode system. Try running with both versions, and don’t forget the -displayvideo flag.

@galapaegos, thanks for your response. I apologize for the delay in my response.

I ultimately was able to get the GLFW version working. As for the issue identified above, it turns out that I was rendering correctly, I just was culling my back surface and/or not setting my view volume correctly. The updated version of the code above is as follows:

glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, gl_pbo_[field_num]); 
glBindTexture(GL_TEXTURE_TYPE, gl_texid_[field_num]);
glTexSubImage2D(GL_TEXTURE_TYPE, 0, 0, 0, nTexWidth_, nTexHeight_, GL_BGRA, GL_UNSIGNED_BYTE, 0); 		
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, gl_shader_);
glEnable(GL_FRAGMENT_PROGRAM_ARB);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
	
float fTexWidth = (float)nWidth_ / (float)nTexWidth_;
float fTexHeight= (float)nHeight_/ (float)nTexHeight_;

glBegin(GL_QUADS);
	glTexCoord2f(                   0,   (GLfloat)nTexHeight_); glVertex3f(-1.0f, -1.0f, 0.0f);
	glTexCoord2f( (GLfloat)nTexWidth_,   (GLfloat)nTexHeight_); glVertex3f( 1.0f, -1.0f, 0.0f);
	glTexCoord2f( (GLfloat)nTexWidth_,                      0); glVertex3f( 1.0f,  1.0f, 0.0f);
	glTexCoord2f(                   0,                      0); glVertex3f(-1.0f,  1.0f, 0.0f);
glEnd();
glBindTexture(GL_TEXTURE_TYPE, 0);
glDisable(GL_FRAGMENT_PROGRAM_ARB);

This along with a more careful set-up of my OpenGL initialization steps led to more success.

efpkopin, thanks for the followup post! That will help anyone in the future doing anything similar.

You might consider posting the full code just for an example of getting CUDA+GLFW working together. As you say, the use of freeglut in the samples is often limiting, and GLFW is a much more practical and modern framework.