// copy image and process using CUDA void processImage() { // tell cuda we are going to get into these buffers pboUnregister(pbo_source); // read data into pbo // activate destination buffer glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo_source); // read glReadPixels( 0, 0, image_width, image_height, GL_RGBA, GL_UNSIGNED_BYTE, NULL); // Done : re-register for Cuda pboRegister(pbo_source); // run the Cuda kernel process( pbo_source, pbo_dest, image_width, image_height, blur_radius); // blit convolved texture onto the screen // detach from CUDA pboUnregister(pbo_dest); // download texture from PBO glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, pbo_dest); glBindTexture( GL_TEXTURE_2D, tex_screen); glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, image_width, image_height, GL_RGBA, GL_UNSIGNED_BYTE, NULL); CUT_CHECK_ERROR_GL(); }
// copy image and process using OpenCL //***************************************************************************** void processImage() { // activate destination buffer glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo_source); //// read data into pbo. note: use BGRA format for optimal performance glReadPixels(0, 0, image_width, image_height, GL_BGRA, GL_UNSIGNED_BYTE, NULL); if (bPostprocess) { if (iProcFlag == 0) { pboRegister(); executeKernel(blur_radius); pboUnregister(); } else { // map the PBOs glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pbo_source); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo_dest); unsigned int* source_ptr = (unsigned int*)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB); unsigned int* dest_ptr = (unsigned int*)glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB); // Postprocessing on the CPU postprocessHost(source_ptr, dest_ptr, image_width, image_height, 0, blur_radius, 0.8f, 4.0f); // umap the PBOs glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); } // download texture from PBO glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo_dest); glBindTexture(GL_TEXTURE_2D, tex_screen); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width, image_height, GL_BGRA, GL_UNSIGNED_BYTE, NULL); } else { // download texture from PBO glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo_source); glBindTexture(GL_TEXTURE_2D, tex_screen); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, image_width, image_height, GL_BGRA, GL_UNSIGNED_BYTE, NULL); } }
void displayImage() { // render a screen sized quad glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glMatrixMode( GL_PROJECTION); glPushMatrix(); glLoadIdentity(); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0); glMatrixMode( GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, window_width, window_height); glBegin( GL_QUADS); glTexCoord2f( 0.0, 0.0); glVertex3f( -1.0, -1.0, 0.5); glTexCoord2f( 1.0, 0.0); glVertex3f( 1.0, -1.0, 0.5); glTexCoord2f( 1.0, 1.0); glVertex3f( 1.0, 1.0, 0.5); glTexCoord2f( 0.0, 1.0); glVertex3f( -1.0, 1.0, 0.5); glEnd(); glMatrixMode( GL_PROJECTION); glPopMatrix(); glDisable( GL_TEXTURE_2D); glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0); glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0); // re-attach to CUDA pboRegister(pbo_dest); CUT_CHECK_ERROR_GL(); }
//////////////////////////////////////////////////////////////////////////////// //! Create PBO //////////////////////////////////////////////////////////////////////////////// void GL_Main::createPBO( GLuint* pbo,int image_width , int image_height,int bpp) { // set up vertex data parameter void *data = malloc(image_width * image_height * (bpp/8)); // create buffer object glGenBuffers( 1, pbo); glBindBuffer( GL_ARRAY_BUFFER, *pbo); // buffer data glBufferData( GL_ARRAY_BUFFER, image_width * image_height * (bpp/8), data, GL_DYNAMIC_COPY); free(data); glBindBuffer( GL_ARRAY_BUFFER, 0); // attach this Buffer Object to CUDA pboRegister(*pbo); get_error(); }
//////////////////////////////////////////////////////////////////////////////// //! Create PBO //////////////////////////////////////////////////////////////////////////////// void createPBO( GLuint* pbo) { // set up vertex data parameter num_texels = image_width * image_height; num_values = num_texels * 4; size_tex_data = sizeof(GLubyte) * num_values; void *data = malloc(size_tex_data); // create buffer object glGenBuffers( 1, pbo); glBindBuffer( GL_ARRAY_BUFFER, *pbo); // buffer data glBufferData( GL_ARRAY_BUFFER, size_tex_data, data, GL_DYNAMIC_DRAW); free(data); glBindBuffer( GL_ARRAY_BUFFER, 0); // attach this Buffer Object to CUDA pboRegister(*pbo); CUT_CHECK_ERROR_GL(); }