ebeckers's picture

Fast texture loading


I'm looking for the fastest way to load an image into a texture
At the moment I use 2 threads.
Thread 1 uses the C# Image.FromFile class to load the image into memory like this:

Bitmap _bitmap;
BitmapData _bmpData;
int  _texture;
void LoadImage()
   _bitmap = new Bitmap(fileName);
   _bmpData = _bitmap.LockBits(new Rectangle(0, 0, _bitmap.Width, _bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

Thread 2 is the main thread which contains the renderloop.
It uses the GL.TexImage2D() to copy the image from memory into the texture.

void Render()
     if (_bitmap != null)
        GL.GenTextures(1, out _texture);
        GL.BindTexture(TextureTarget.Texture2D, _texture);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, _bmpData.Width, _bmpData.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, _bmpData.Scan0);
        _bitmap = null;
    // render the texture...

However sometimes i need to load a couple of new textures and then the calls to GL.TexImage2D() are stalling the renderloop and so the FPS drops. Is there a better/faster way to do this? I read something about PBO's, but i think this is only helpfull if you need to update a texture with new pixel data and does not really improve the first-time loading.

Another option would be multi-threading so textures can be created and loaded into thread 1 while rendering takes place in thread 2. Is there any simple example based on GameWindow which shows how to do multi threading with openTK ?




Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ebeckers's picture

New issue created http://www.opentk.com/node/1354
So .. no more excuses :-))))