Robust main loop; best practices/ideas...

Started by
3 comments, last by ATC 11 years, 6 months ago
Anyone can cobble together a decent main loop... but doing one "perfectly" is a bit of a different story. So before I revisit my main loop implementation for my engine, I decided to open this discussion to get better ideas. I must offer the following features (NOTE: project is in C#):

  • Optional variable/fixed time-step (precise enough for realistic physics)
  • Optional multi-threaded updates
  • Optional multi-threaded rendering (for D3D11 & OGL)
  • Thread safety for memory and forms/controls/GUI stuff

I already have implemented an extremely hi-resolution timer/clock for the engine. So that is taken care of. But I want to re-implement the entire main loop from start to finish, and create a robust implementation that can satisfy virtually any game or hard-core simulation.

Here are some of the things that concern me...

  1. Proper separation of render and update logic?
  2. Proper way to implement option of fixed vs variable time-step?
  3. Implementation of a Winforms-like "InvokeRequired" property for GUI thread safety?


I'd also like to know what might happen if I try to render something from another thread in, say, D3D10 (which doesn't offer multi-threaded rendering like 11)? Does the device interface just perform the rendering on the thread it was created on, or would it crash my engine? What about running my update loop on a totally separate thread from rendering? Is this acceptable or a "bad practice"? Should I instead offer the option to set "game components" to update themselves on separate threads (e.g., physics engine, content loading/streaming, etc as components which run their updates on new threads)? What is the best way to synchronize a very complex multi-threaded environment like this? What problems might I encounter, and what is the best way to work around them? Any further information is greatly appreciated...
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
Advertisement
No idea if you've read these yet, but both of these articles may give you some ideas: deWiTTERS Game Loop and Fix Your Timestep!. I know a lot of people use variations of what are discussed here.

Regarding all of your desired options, I don't think it's practical to make a single game loop configurable to that degree. If I were going to do such a thing, for, say, a game framework, I would prefer to make separate implementations targeting the different conditions to take advantage of any idiosyncrasies in that environment. Piling everything in one cover-all-the-bases loop is going to be inefficient, but the bigger concern in my book is maintainability. IMO, the perfect game loop (if there is such a thing) isn't one that is highly configurable, but one that matches the needs of the game.
In my current project I felt a definite need to invest some of my critical thinking and programming skills into this issue, and the end result was basically a kernel that divided everything up into a grid of states/systems. The X/Y dimensions of this grid are the Systems and States of the engine/kernel.. Thus, systems like Render/Network/Physics, etc. with various 'states' that can be invoked across threads, and done so repeatedly according to a desired time interval.. Eg: Render_Setup, Render_Cycle, Render_Shutdown...

Each system I allow a min-interval and a max-interval. All systems attempt to reach a 'balance' so that their actual execution intervals are within their defined ranges. If one system is running plenty quick for a particlar state (eg: STATE_SETUP, STATE_STANDBY, STATE_INGAME, STATE_SHUTDOWN) and other systems aren't, it will simply increase the wait period until the next execution..

Along with all this is a neat ability to create logic-chains, where one state - depending on the result of each system executing for that state - determines what state to progress to next. So if the STATE_SETUP fails on one of the systems, it will go to STATE_SHUTDOWN.. If the setup for all systems succeeds then it can, otherwise, progress the state to STATE_STANDBY to display a menu, or whatever.

I just create a new thread for each system, unless the system specifies that it should run in the main thread (some things like hardware rendering don't much appreciate occurring outside the main thread) and each thread runs its own main-loop that determines if/when its corresponding system should execute. Integrating multithreading into the system is probably the trickiest part of the whole deal, but the key is to not over-think it..

As far as overhead is concerned, my primary goal when devising this setup was having the ability to control the use of resources by each system so as to provide the end-user an optimal experience as far as responsiveness is concerned, by enabling the core, or 'main loop', to skip the refresh of certain systems which can afford it..

If one system magically takes a bunch of time and causes other systems to fall behind, forcing them to execute well beyond their max-interval, what I do is take the total amount of elapsed time since the last refresh (for each system, individually), divide it up evenly, and execute the system however many times in an attempt to emulate proper execution intervals so as to allow the system to 'catch up'.

I'm not sure how well I explained this, but it works great and I'm happy with it.
@ radioteeth:

Yes, you did explain things very well; about as good as one can without "going all out" and supplying diagrams, code, etc... You've given me some very good ideas which I'm going to work into the design and test; and take it from there, tweaking it out for performance and improving the logic itself. So +1 for you and Aldacron, whom I must also thank for his time.

Any others have some insight that might be beneficial for me to hear? My ears will be open! :-)

Thanks for all thus far,

--ATC--
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________
Ok, I put a little thought into a fixed time-step implementation and I've come up with something like this:

[source lang="csharp"]
void _MSG_LOOP() {

if ( abort )
game.ShutDown();

clock.Step();
float delta = clock.Elapsed.Milliseconds;

if (IsFixedTimeStep)
{
accumMS += delta;
float stepRate = (1 / TargetUPS);

int nSteps = (int)Math.Floor(accumMS / stepRate);
int count = Math.Min(nSteps, MAX_STEPS);

while (count-- > 0)
{
_update(stepRate);
accumMS -= stepRate;
}

_render(stepRate);
}

else
{
_update(delta);
_render(delta);
}
}
[/source]

So what I'm wondering is: Is this correct? the way I should be doing it? Or am I doing something wrong? If so, please explain... I just want to "perfect" this little algorithm before I code out of the real system for the engine...

I'm also considering the idea of having my algorithm compute the "remainder" any time left over after executing nSteps... basically, if the remainder fell within the acceptable delta value range for the physics engine it would then dispatch one more update to clear the ms accumulator and pass that remainder as the delta value... For example:

if( remainder > min_stepRate && remainder < max_stepRate)
_update( remainder );

Is this advisable or not?
_______________________________________________________________________________
CEO & Lead Developer at ATCWARE™
"Project X-1"; a 100% managed, platform-agnostic game & simulation engine

Please visit our new forums and help us test them and break the ice!
___________________________________________________________________________________

This topic is closed to new replies.

Advertisement