Could you cap the frame rate without v-sync or a delay function ?

So when you draw a frame the program wait until the said frame is drawn before continuing the execution.
And if you don’t use V-sync you need use a delay function to reduce the frame rate.

I think it would be better to declare that frames must stay displayed a said amound of time before the next is drawn.
But is it possible? And how ?

I haven’t found yet… (ç-ç|

Edit: I misunderstood the question. Not what OP was asking about

Combining SDL_GetTicks and SDL_Delay, you can calculate how much time you need to delay for the current frame to be displayed for the desired time. Something like this:

const int desired_frame_duration = 16;//desired frame time in ms
while(!quit) {
    int ticks_before = SDL_GetTicks();
    process_input();//input related operations
    draw();//all drawing operations
    int ticks_after = SDL_GetTicks();
    int ticks_passed = ticks_after - ticks_before;
    int amount_to_wait = desired_frame_duration - ticks_passed;

    if(amount_to_wait > 0) {
1 Like

@Andrei_Rafael You didn’t read the title… :neutral_face:

What i want to do is to give a metronom to the GPU and tell it to render frames only the said metronom tick.
Not to tell CPU to wait X(ish) milliseconds.
Not to tell the GPU to sync with the monitor.

Well it’s kinda like v-sync, but if i could choose the monitor’s refresh rate and if the said monitor was inside the GPU.

In sumary i want to cap my fps inside the GPU , instead of inside the CPU using programming voodoo.

1 Like

I’m definitely no expert but it seems like what you are asking for would introduce serious problems.
If your CPU had no delay function (ie, was not locked in sync with the frame rate), it would be difficult to ensure that your CPU would draw everything that needs drawing exactly once per frame.
Also, the CPU delay is not only about maintaining a particular frame rate, but it is also about saving on power consumption which is important on many devices.

GPUs do not have any timer. So you can’t do this and need some CPU solution(Delay + GetTick above or SDL_AddTimer - SDL Wiki etc).

(Some GPUs have timestamp counter for performance metric purposes but you cannot tell GPUs to “wait” for it.)

1 Like