Skip to content

Broken VSYNC with Metal driver #11384

Open
@caco-underscore

Description

@caco-underscore

On M1 running macOS and ProMotion enabled (120hz), if I switch between fullscreen Xcode and a SDL2 window, SDL_RenderPresent() changes from waiting for vblank (expected behaviour) to not wait and use 100% cpu if not more (Xcode shows 150% usage for the example bellow). This only happens with the Metal driver; OpenGL driver behaves like it should. This is the test code in c++:

#include <iostream>
#include <chrono>
#include <SDL2/SDL.h>

int main() {
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window* window = SDL_CreateWindow("V-Sync Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
    SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

    SDL_Event event;
    bool running = true;

    // render loop
    while (running) {
        
        while (SDL_PollEvent(&event)) {
            if (event.type == SDL_QUIT) {
                running = false;
            }
        }

        // Clear the screen
        SDL_RenderClear(renderer);

        // Measure V-Sync wait time
        auto start = std::chrono::high_resolution_clock::now();

        SDL_RenderPresent(renderer);

        auto end = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
        std::cout << "Time spent waiting for V-Sync: " << duration.count() << " milliseconds" << std::endl;
    }

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

The lib version I'm using is 'stable 2.30.8' from Homebrew.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions