threaded texture loading

Started by
11 comments, last by tseval 15 years, 11 months ago
I would have expected that doing an asynchronous Read() call in your main thread versus doing a synchronous Read() call in a background thread would have about the same effect, namely, not stalling the main thread.

Any recent (< 10 years old) HDD controller uses DMA, so it can push data read directly into RAM on its own without anything but marginal CPU control. An asynchronous Read() call can thus be more resource-friendly.


Some things to check:

Just to make sure, when your thread isn't doing anything, you're waiting using WaitForMultipleObjects() (or the equivalent pthreads call), not burning the CPU with a while(queue.empty()) {}, right? Otherwise, maybe it might not be the loading that leads to glitches, but the times inbetween ;)

Another place to check would be resource generation. It might take some time to create graphics device resources, so you might want to use a time budget for creating the resources if you aren't already doing so.

Maybe it would even be sensible to use a two-layered approach, run a high-priority thread for loading files (this thread idles most of the times, waiting for new files to enter the queue or files being loaded to finish loading) and a low-priority thread that creates the graphics device resources.
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
Advertisement
Pthreads is implemented on Linux by using processes. Each thread is actually a system process with shared memory. Blocking I/O should definitely block only the thread using it. Are you sure that the I/O is the issue? Maybe the reader thread is using a lot of CPU time for some reason? Have you tried monitoring CPU usage with tops or some other program? Also, you could be memory bandwidth limited and the HD access is cutting into that. Maybe you should use sched_yield() more sparsely?
I have made a small test example that loads a huge file in a separate thread while doing some processing in the main thread, and verified that this works just as intended. The loader runs nicely in the background without blocking the processing thread. So I guess that means that the fopen/fread blocking theory wasn't right after all.

The data reader thread uses semaphores to wait for incoming data, and I have verified that it uses almost no CPU at all.

It might be that the texture upload is the bottleneck if a lot of textures finishes loading in the same frame, but I have also tried reducing the number of texture uploads per frame by putting all the finished textures in an output queue and processing only a few of them per frame. I will try to investigate this part a bit more though.

Are there any tools (on linux or windows) that shows when (and why) a thread is blocking?

This topic is closed to new replies.

Advertisement