background and motivation


I wrote this with the express purpose of helping someone struggling with implementing multithreading, I will go over some very basic but specific examples so even someone with limited programming experience would be able to gain something from this. I will start with simple reading and writing using the stb image library, which is quite popular and does exactly what I need. But, the ideas should apply throughout, regardless of the library used.

This is written in a way to be read through from beginning to end, although if you skip the background will not miss anything.


Over the last year and a half I worked on writing a game engine from scratch for my portfolio. The purpose was to learn as much as I could about programming in c++ and graphics programming in openGL.

After I incorporated physically based rendering into my graphics pipeline I started to use more 4k textures since I was already using up to 16k textures for HDR irradiance maps. Loading a simple scene with one player controlled actor and about 50 collidable objects and a single irradiance map for specular and diffuse lighting, took about 60 seconds. After implementing multithreading for only texture loading, it dropped to 13 seconds.

All the objects had multiple textures up to 4k resolution. This turned out to be the main bottleneck in the loading process. Even though a lot of the objects shared the same model and texture so they were not loading the same textures multiple times this “in order” or “asynchronous” process of loading textures onto the GPU took the longest amount of time. This is where I focused my multithreading efforts to.

In my engine the models have a bunch of data associated with them. They have 5 textures for each model. Albedo, normal, roughness, height, and metalness. When go to load a model I give each texture its own thread to process the loading. Then join them to send them to the GPU.

whiteboard example of how multithreaded works in my game engine.