#How to make deltatime using vulkan and glfw
34 messages · Page 1 of 1 (latest)
If you only want to get time spans for the vulkan side of things you could use time stamps
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
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
I see what you mean but not necessarily. The rate is the same except cases like Unity which has a separate FixedUpdate loop. The phase is different however, but that will eventually catch up to the logic loop if the program is gpu bound
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
not yet, there's a WIP extension for that
I guess you could then just use the timestamp at the bottom of the pipe of your last render command buffer, but that totally ignores presentation time, which is crucial for vsync
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
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
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
You either need to explain why you need this deltatime and how you'll use it or do it how I explained it to you
Like this
It's a good easy first solution but a better solution would require knowing why you need it
So if users will have different pc characteristic and one will be rendering and processing faster, he wouldn't be different from one that has slower processing
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
Then you can do it how I explained
But i want to know should i use glfwgettime()?
I wanted to place one before the frame drawn and after and compare them
currentTime = getTime()
deltaTime = currentTime - lastTime
lastTime = currentTime
To get the time it took to draw and operate a frsme
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
wait, getTime() is same with glfwGetTime() ?
I'm just simplifying the code, I have never used glfwGetTime() but it's probably like all other time functions, so yes it's the same