How to properly get delta time on modern hardware?

Started by
62 comments, last by Finalspace 7 years, 1 month ago

Wait a second I'm a bit confused, the code you have times the DoLotsOfWork function, not the elapsed time between the call of the function.

It times the loop. You need to stare at the flow of code until you see this. The code you have posted until now indicates that you are having a very hard time understanding how updating certain values at certain points cause certain things to be timed. Hodgman has already pointed out that anything happening at the very bottom of the loop is exactly logically the same as it happening at the top of the loop. Moving the last line of code from the bottom of the loop to the top of the loop still means that all the same statements are executed in all the same orders.

otherwise I'm just timing how long my raster operations take.

You would need GPU timers to time those. This is something you can learn later, but just as a head's up now, timing a call to a render only times how long the CPU takes to put a command into a buffer to be executed later by the GPU.

But the code you have doesn't time any of that, why?

There is a single update of the time in the loop, thus he is timing the loop. Anything not being timed is because it is not in the loop.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Advertisement

No I'm doing software rasterizing, GPU is only involved in backbone. My code had the SetLastTIme at end of one of the branches of the loop, not at the end of the loop that is why I assumed that it won't always be circular. But again why am I timing my raster function and including that into delta time? The raster operations take about 120ms, so my delta time gets bloated out of control. I always though that the point was to only time the time between the calls to raster function, not raster call function timing in itself.

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

The point of the delta time computation is to know how much real time passed since the last time you did an update. You want that time to include time spent "rendering" (whatever that means to your program). Otherwise you're not actually measuring the real time between two updates. This will not cause your delta time to "bloat out of control." It will cause your delta time to be correct.

But this will completely destroy my animations then. By the time I finish rendering the elapsed time delta will be so huge that it's just going to be like watching bad stop motion anim. I guess I'm using the wrong approach to begin with. I need to cancel out the render time delta right, so just using a fixed time step would do it?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

But this will completely destroy my animations then. By the time I finish rendering the elapsed time delta will be so huge that it's just going to be like watching bad stop motion anim.

You fix that problem by making your system faster, not by making your timers lie. If your animations are destroyed by fixing your timer, then your animations are already destroyed and you are simply aware of it now.

I need to cancel out the render time delta right, so just using a fixed time step would do it?

A fixed-step loop ensures that your logical updates produce consistent and reliable results; rendering is still done once-per-loop as you have now. If a long time between frames is causing you to have bad results, a fix may be to spread the update over fixed-size intervals (implementing a fixed time step), but it won't allow you to render faster.
The only fix is to make your renderer faster. If it takes 120 milliseconds to render, then that is the hard limit no matter what type of loop you implement.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This should not "destroy" your animations unless you are also doing those incorrectly. You should be spending much less than 1/60th of a second rendering (ideally, one frame -- both game logic update and rendering -- takes 1/60 of a second at the longest in order to get to 60FPS).

You do not need a fixed time step for this (those are for fixing different issues), and you don't need to "cancel out" the time spent rendering. That is exactly the opposite of what you want to do.

My animations are based on delta time, akin to this <scalar*dir.x*delta_time, .scalar*dir.y*delta_time> so because my render function takes more time than acceptable refresh it completely destroys the look of the animation. What I think I have to do is to untie my anims from realtime (timedelta). But you're saying fixed timestep is not the way to go? So what should I do?

Edit: but is there really no way to avoid including my raster function in time loop? I don't even understand how a high rez timer timer does capture the time to get from bottom of loop to top, you're saying that the jmp instruction is taking less that one tick? And QPC can see 1 clock tick right?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

My animations are based on delta time, akin to this <scalar*dir.x*delta_time, .scalar*dir.y*delta_time> so because my render function takes more time than acceptable refresh it completely destroys the look of the animation. What I think I have to do is to untie my anims from realtime (timedelta). But you're saying fixed timestep is not the way to go? So what should I do?

The process of applying transforms (scalar*direction*time) is logical. It is separate from rendering.

A fixed-step update applies logical updates using a fixed delta, which would stabilize your results, assuming you have properly separated logical updates from rendering.
So yes, in this case a fixed-step update may help, but only if you've done it properly.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

So in other words you're saying it may help but it's not an ideal way? So what do you suggest I do then if not this?

You didn't come into this world. You came out of it, like a wave from the ocean. You are not a stranger here. -Alan Watts

I don't even understand how a high rez timer timer does capture the time to get from bottom of loop to top, you're saying that the jmp instruction is taking less that one tick? And QPC can see 1 clock tick right?

The conceptual process looks like this


while(!done) {
  CurrentTime = GetTheCurrentTime();
  ElapsedTime = CurrentTime - PreviousTime;
  PreviousTime = CurrentTime;

  Update(ElapsedTime);
  Render(ElapsedTime);
}

You get the current time at the top of the loop, subtract it from whatever the time was at the top of the last iteration of the loop. That's your elapsed time. You're measure the time it took to go from the top of the loop, through all the instructions in the body, and get back to the top. There's no need to concern yourself with trivial details like the cycle count of a jump instruction.

so because my render function takes more time than acceptable refresh it completely destroys the look of the animation.

This sounds like a problem that you should fix. Updating animations (and game logic) via delta time is correct. But using a fixed timestep to do that is not going to solve the problem where it takes too long to render. What makes your game run like a slideshow is the fact that it takes 120ms to render stuff. That's like... 8 frames per second. If you subtract out the time spent rendering, your animations will make smaller adjustments between any two presented frames. But they will still only render at 8 FPS, and when you eventually fix the renderer or switch to a real one, all of your assumed times will be wrong.

The real solution to this is probably to stop doing software rendering. It's a good educational project, but making one that is both capable of robust visuals and runs at acceptable framerates is quite a challenge.

This topic is closed to new replies.

Advertisement