SFML community forums

Help => Window => Topic started by: Fizix on January 23, 2014, 12:17:45 pm

Title: Vertical Sync clarification
Post by: Fizix on January 23, 2014, 12:17:45 pm
Testing the following code:

#include <iostream>
#include <vector>

#include <SFML/Graphics.hpp>

int main(int argc, char* argv[]) {
  sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Render window");
  window.setVerticalSyncEnabled(true);

  sf::RectangleShape rr(sf::Vector2f(20.f, 20.f));

  while (window.isOpen()) {
    sf::Event evt;
    while (window.pollEvent(evt)) {
      if (evt.type == sf::Event::Closed)
        window.close();
    }

    window.clear(sf::Color::Blue);

    sf::Clock clock;

    rr.setPosition(0, 0);
    window.draw(rr);

    sf::Int64 time1 = clock.restart().asMicroseconds();
    std::cout << "1: " << static_cast<double>(time1) / 1000.0 << std::endl;

    rr.setPosition(20, 20);

    window.draw(rr);

    sf::Int64 time2 = clock.restart().asMicroseconds();
    std::cout << "2: " << static_cast<double>(time2) / 1000.0 << std::endl;

    window.display();

    sf::Int64 time3 = clock.restart().asMicroseconds();
    std::cout << "3: " << static_cast<double>(time3) / 1000.0 << std::endl;

  }

  return 0;
}
 

Timing all the intervals between draws it seems that vertical sync is enforced before the first draw call is made in stead of when calling display(), which seemed more sensible in my opinion, because drawing to a back buffer doesn't nescessarily mean your going to swap it.

This can be tested by moving the .clear call to after creating the clock, then the first time is about the time of a  single vsync'd frame.

I understand that on windows at least the wglSwapIntervalEXT call sets the number of frames that is allowed to be swapped per screen refresh cycle.  Is this the way it's supposed to be enforced?  Is this the same for all other platforms?

I'm using NVIDIA drivers.
Title: Re: Vertical Sync clarification
Post by: Laurent on January 23, 2014, 01:01:04 pm
The way it is supposed to work, is that the driver waits for the next screen refresh in the SwapBuffers (Window::display) function. So something might be wrong with your test code, or the way you interpret the result.