Main Menu

Efficiency

Started by dosse, September 23, 2009, 15:43:52

Previous topic - Next topic

dosse

Hi! i'm trying to make some optimizations to my game: is there any way i can create a new thread and use it to pre-load some textures without causing a crash?

broumbroum

Sure, I currently use a schema that is similar to what MediaTracker does with bufferedImage images.
The goal is to provide a loading process that operates outside the main thread, isn't it ? It uses one java class, mainly, java.util.Timer and its associated member TimerTask.

in a few words, the Timer is needed to perform sequential tasks in a separate Thread, whose tasks provide a simple state report when invoked. Thereafter the loading is completed, the opengl Thread can get access to the data and complete the loading with texture declarations and bindings.

Hence, a loader class is created and all loading stuff are forwarded to an underlying statical Timer that receives tasks as  TimerTask, scheduled and processed as needed. The class is set to LOADING and when the Timertask is completed, set to LOADED.
The openGL Thread must be continuously monitoring the loader class state. When a loader class becomes LOADED it calls a method of the loader class that will be invoked in the opengl thread and creates textures.
C=class; T=Thread
C : LoaderClass -> loadData() -> LOADING ->Timer.scheduleTask(TimerTask.run() { readFromFile, LOADED}
T : timerThread (handled by Timer) : wait for new timerTasks - Timertask scheduled ? -> TimerTask.run()
T : openglThread (handled by LWJGL) : Renderer -> monitor the LoaderClass.getState() - LOADED ? -> LoaderClass.loadGLdata()
                                                             |_ LOADING ? -> render next frame

- something that is NOT possible is loading GL textures from an external thread while you render.
- we could figure out a model that'd invoke arbitrary Threads and acquire locks on the GL context without using Timer, but this is not permitted by opengl that only allows ONE Thread owning the lock.
- Timer is efficient because it asserts that only ONE Task at a time is processed and on the SAME THREAD.
- if the Timer thread would acquire the GLContext, then it will be admitted that the loading occurs on another thread, but you still wouldn't be able to render simultanously, so the screen may appear black or frozen while the LoaderClass loads the data.

dosse

thanks for answering... i want to load textures that will be used later by the game in a separated thread... from what i understood it's not possible, but if you have some code for faster image loadings i'd love to try it

broumbroum

You may try http://lwjgl.org/forum/index.php/topic,2886.0.html yet i'm working on a tile cache system for bigger pictures, which I'll post later along with a way to make load stuff easy-to-access and faster (than ever...). :D

dosse

Quote from: broumbroum on September 23, 2009, 21:38:23
You may try http://lwjgl.org/forum/index.php/topic,2886.0.html yet i'm working on a tile cache system for bigger pictures, which I'll post later along with a way to make load stuff easy-to-access and faster (than ever...). :D

thanks! i'll try that!