I am currently developping a general texture loader for a 3D engine, and I was wondering what would be the intersest of using Pixel Buffer Objects for doing simple static texture loading. Here is the way I go right now :
- In a thread devoted to loading, I load the texture pixels from an image on a drive, and save it into a char* ;
- I generate the textures :
glGenTextures( 1, &mTextureID ) ; glBindTexture( GL_TEXTURE_2D, mTextureID ) ; glTexImage2D( GL_TEXTURE_2D, 0, mTextureFormat, mWidth, mHeight, 0, mPixelFormat, mDataType, mImage.GetData() ) ;
- I signal the rendering thread that the texture is ready for use.
Is it a good idea to insert a PBO operation before the glTexImage2D operation ? Something like that :
glGenTextures( 1, &mTextureID ) ; glBindTexture( GL_TEXTURE_2D, mTextureID ) ; glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, mPBO_ID ) ; glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, mImage.GetDataSize(), NULL, GL_STREAM_DRAW ) ; char* pboMemory = glMapBuffer( GL_PIXEL_UNPACK_BUFFER_ARB,GL_WRITE_ONLY ) ; memcpy( pboMemory,mImage.GetData(), mImage.GetDataSize() ) ; glTexImage2D( GL_TEXTURE_2D, 0, mTextureFormat, mWidth, mHeight, 0, mPixelFormat, mDataType, mImage.GetData() ) ;
Of course, a PBO would not be created for each texture, but only 2 or 3 (the most recently used will be queued). As I understood PBO, the pixel transfer rate make it efficient, but is the CPU glTexImage2D operation of the first piece of code really slower than the memcpy (that has to be done anyway) of the second piece of code ? Maybe the format conversion between the pixel data and the texture is slow ?
Do you think such a changing in the code could be an optimization ?