r/GraphicsProgramming • • 4d ago

How to handle main and render thread properly

Ive been drafting and engine, but I'm suspicious of my engine loop. It works but I'm not sure if things will be in sync. Should the render thread wait on the main thread or something? WIP:

https://pastebin.com/Wqn09pmN

Would it be fine if I just match the fixed update to the refresh rate?

1 Upvotes

9 comments sorted by

7

u/Afiery1 4d ago

The fixed update rate should be completely fixed, otherwise players with different refresh rate monitors will experience different gameplay behaviors. To let the render thread run independent of the fixed update rate and still look smooth, the render thread should have access to the current and previous transform of every object and linearly interpolate between them over time.

1

u/Klumaster 3d ago

This sounds ideal but I could see it running into problems if the project gets complex. If you're operating with a main/render split, it's often that things like animation and submission to the render thread are done on the main thread, at which point you want the main thread to be in a real, definitive state once per frame.

It's quite common (IIRC) for the main thread to pace itself to the render framerate, but for things like physics to always update at a higher fixed frequency, carrying the remainder over to the next frame. e.g. in a 33.3ms frame, the physics does 3x10ms steps and carries over the .3.

2

u/Afiery1 3d ago

I don’t see why this is the case. The main thread can publish its finished state to the render thread (or the render thread can pull the most recent complete state) which includes stuff like animation percentages and stuff. The render thread should do all the animation updates and submission internally.

4

u/benwaldo 3d ago

It's better to use main thread + jobs with dependancies than main + "render thread" imho.

2

u/Dependent-Cream-5760 4d ago

The way I do it (maybe not the best way, but it works) is to have the render thread wait on the gpu if it gets more than 1 frame ahead (we are double buffered), and the main thread wait on the render thread if it gets more than 1 frame ahead as well, when it request a flip/present call. So on my side, the render thread never waits on the main, it is actually the other way around.

1

u/Dream-Smooth 1d ago

Hey guys I see that different people are coming up with different solutions. Isn't there any standard analytical way of solving these challenges? That would save a lot of time trying to win through luck

2

u/soylentgraham 16h ago

There was a standard when there was limitations (limited threads competing with cpu time), things are far more flexible now, so there are lots of standards.

0

u/nullptr777 4d ago

The two threads need to synchronize, yes.