What is the fastes way to load textures in opengl ?

Started by
21 comments, last by swiftcoder 12 years, 8 months ago
Instead of using an actual image format (such as TGA) which supports BGRA format, why not ship your images as plain image files already in that format. You don't need all the headers, you just need four bytes per pixel.

Lay them all out in a big block of ram, save that to disk, gzip it. When you come time to load it, you're only loading one file -- you hoof it into one giant block of ram (only 1 allocation, only 1 directory walk). Ungzipping a file is pretty easy on loading it. In addition your compression should be even better; larger files generally compress better than a set of smaller files because the compression can find more similarities to reduce.

Instead of generating the mipmaps on load every time, generate them on first run and save them out to disk. Again, do this one big block if possible. On load, pull it in, rattle down it transmitting the mipmap levels individually.


If you're on an OS which supports mapping the files, you don't need to load them or do an actual memory allocation. Map the file into your address space, provide suitable "linear access" hints to your buffer caching system so it will do read-aheads, start accessing the memory and away you go. The OS will try and load pages ahead of your accesses in the background; you don't have to wait for the "read()" call to complete. The effect of this is that you can already be generating and uploading mipmaps for texture 1 while the disk IO channels are still loading texture 20.


Instead of asking the GLU library to generate your mipmaps, you could build them yourself by using rendering of the fullsize image to pixel buffer objects and copies internal to the card to make them. That way the mipmap data doesn't have to transit the host-GPU bus.
Advertisement

[quote name='YogurtEmperor' timestamp='1313547798' post='4850117']
But I am using ::glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE );

The SGI version of this token has been deprecated for eons. For OpenGL 2 and before, use the standard GL_GENERATE_MIPMAP token in its place.

For OpenGL 3+, use the glGenerateMipmap(GLenum target) function instead.
[/quote]
I don’t actually use SGIS (I use GL_GENERATE_MIPMAP). I just added that just in case the original poster is trying to maintain heavy backwards compatibility as I once did in the past.


Also I strongly suspect that the original poster’s bottleneck is indeed mipmaps.
Yes, they can be generated offline, but I have found so little slowdown with GL_GENERATE_MIPMAP that I don’t believe it is necessary, and it can save you disk space.
I recommend that he test his speed using GL_GENERATE_MIPMAP and then decide if he wants to trade mipmap quality for larger files on disk.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


Yes, they can be generated offline, but I have found so little slowdown with GL_GENERATE_MIPMAP that I don’t believe it is necessary, and it can save you disk space.
I recommend that he test his speed using GL_GENERATE_MIPMAP and then decide if he wants to trade mipmap quality for larger files on disk.


Yes, I would agree.

And if GL_GENERATE_MIPMAP isn't high enough quality (although it usually is), you can always use an FBO + pixel shader, to manually generate the mipmap chain directly on the GPU.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement