#How to make deltatime using vulkan and glfw

34 messages · Page 1 of 1 (latest)

timid heath
#

Vulkan does not have such functionality. You either use the one provided by glfw or timing functions of the underlying operating system/programming language.

#

If you only want to get time spans for the vulkan side of things you could use time stamps

fathom moat
#

The time the whole frame render takes is different if you count the cpu or gpu or both. If you want to use this deltatime in your logic for animation for example, you would want cpu time. This is done without vulkan, you need to use a clock, timer, or anything to calculate time.
What I do is calculate the time at the beginning of every frame before the logic is called, and calculate how long it has been since the last time you have been at that same line of code : deltaTime = currentTime - lastTime.
Then you have the amount of time that has passed since you have updated your logic ; this means that if you need to move an object by a speed, you can do speed*deltatime which will move it the appropriate amount considering the time spent since the last update

#

This might not be the best for you however, because it doesn't represent the time the render took, since vulkan renders asynchronously with the cpu

merry bolt
#

If you want to use this deltatime in your logic for animation for example, you would want cpu time.
no you dont, you want how much will pass since the last image is shown to the time the next image will be shown
this is a prediction on where the animation will be by the time the next image is shown
if you only use the time between the update calls you would very easily get stuttering/jittering as the logic is running at a different rate and phase as the display

fathom moat
#

In your case you'd want to estimate how long the rendering will take which isn't trivial, you're essentially extrapolating

#

Because you don't know the time it'll take to render the next frame, you can only guess from the last frames

#

Also, there's no way to know when vulkan presents an image, so there's no way to get a perfect measure

merry bolt
#

not yet, there's a WIP extension for that

fathom moat
#

However, when you take into account vkAcquireNextImage which blocks on vsync, you get something that accounts for presentation delay and get a cpu deltatime that works with vsync and immediate

#

Though obviously it's flawed, but I don't know what isn't

merry bolt
#

yeah without present_timing there isn't much you can do for accurate numbers but there's no sign it'll be done anytime soon

granite meadow
#

so, what should i use

#

i wanted to use glfwGetTime function

#

and compare time before my function and frames done and after it

#

so i can calculate the time it took for that thing

fathom moat
fathom moat
#

It's a good easy first solution but a better solution would require knowing why you need it

granite meadow
#

Like if one frame takes for one 0.01 second to load, and other 0.4 first will be moving faster

#

But delta time can compensate that

fathom moat
#

Then you can do it how I explained

granite meadow
#

But i want to know should i use glfwgettime()?

#

I wanted to place one before the frame drawn and after and compare them

fathom moat
#

currentTime = getTime()
deltaTime = currentTime - lastTime
lastTime = currentTime

granite meadow
#

To get the time it took to draw and operate a frsme

fathom moat
#

This gets the time between two calls to that piece of code

#

So if you place it in your gameloop (if it's a game or whatever)

#

You'll get the exact time by which you should advance your simulation

granite meadow
#

wait, getTime() is same with glfwGetTime() ?

fathom moat