Skip to content

Commit

Permalink
gl swap
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Odetti <[email protected]>
  • Loading branch information
audetto committed Jul 25, 2024
1 parent 61e0311 commit a2a90fe
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
2 changes: 1 addition & 1 deletion source/frontends/common2/programoptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ namespace common2
bool imgui = true; // use imgui renderer
std::optional<Geometry> geometry; // must be initialised with defaults
bool aspectRatio = false; // preserve aspect ratio
int glSwapInterval = 1; // SDL_GL_SetSwapInterval
int glSwapInterval = -1; // SDL_GL_SetSwapInterval
std::optional<int> gameControllerIndex;
std::string gameControllerMappingFile;
std::string audioDeviceName;
Expand Down
2 changes: 1 addition & 1 deletion source/frontends/sdl/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ void run_sdl(int argc, const char * argv [])
frame = std::make_shared<sa2::SDLRendererFrame>(options);
}

std::cerr << "Default GL swap interval: " << sa2::compat::getGLSwapInterval() << std::endl;
std::cerr << "GL swap interval: " << sa2::compat::getGLSwapInterval() << std::endl;

const common2::CommonInitialisation init(frame, paddle, options);

Expand Down
12 changes: 11 additions & 1 deletion source/frontends/sdl/sdlcompat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ namespace sa2
// I am not sure whether we should worry about SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER
const char * name = index >= 0 ? SDL_GetRenderDriver(index) : nullptr;

return SDL_CreateRenderer(window, name);
SDL_PropertiesID props = SDL_CreateProperties();
SDL_SetProperty(props, SDL_PROP_RENDERER_CREATE_WINDOW_POINTER, window);
SDL_SetBooleanProperty(props, SDL_PROP_RENDERER_CREATE_PRESENT_VSYNC_NUMBER, SDL_TRUE);
if (index >= 0)
{
SDL_SetStringProperty(props, SDL_PROP_RENDERER_CREATE_NAME_STRING, SDL_GetRenderDriver(index));
}

SDL_Renderer *renderer = SDL_CreateRendererWithProperties(props);
SDL_DestroyProperties(props);
return renderer;
}

#else
Expand Down
41 changes: 26 additions & 15 deletions source/frontends/sdl/sdlframe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,23 +145,27 @@ namespace sa2

void SDLFrame::SetGLSynchronisation(const common2::EmulatorOptions & options)
{
const int defaultGLSwap = compat::getGLSwapInterval();
if (defaultGLSwap == 0)
{
// sane default
mySynchroniseWithTimer = true;
myTargetGLSwap = 0;
}
else if (options.syncWithTimer)
std::vector<int> intervalsToTry;

if (!options.syncWithTimer)
{
mySynchroniseWithTimer = true;
myTargetGLSwap = 0;
intervalsToTry.push_back(options.glSwapInterval);
intervalsToTry.push_back(std::abs(options.glSwapInterval));
intervalsToTry.push_back(-1);
intervalsToTry.push_back(1);
}
else
// fallback
intervalsToTry.push_back(0);

const auto it = std::find_if(intervalsToTry.begin(), intervalsToTry.end(), setGLSwapInterval);

if (it == intervalsToTry.end())
{
mySynchroniseWithTimer = false;
myTargetGLSwap = options.glSwapInterval;
throw std::runtime_error(decorateSDLError("SDL_GL_SetSwapInterval"));
}

myTargetGLSwap = *it;
mySynchroniseWithTimer = (myTargetGLSwap == 0) && options.syncWithTimer;
}

void SDLFrame::End()
Expand All @@ -176,12 +180,19 @@ namespace sa2
}
}

void SDLFrame::setGLSwapInterval(const int interval)
bool SDLFrame::setGLSwapInterval(const int interval)
{
const int res = SDL_GL_SetSwapInterval(interval);
return res == 0;
}


void SDLFrame::changeGLSwapInterval(const int interval)
{
const int current = compat::getGLSwapInterval();
// in QEMU with GL_RENDERER: llvmpipe (LLVM 12.0.0, 256 bits)
// SDL_GL_SetSwapInterval() always fails
if (interval != current && SDL_GL_SetSwapInterval(interval))
if (interval != current && !setGLSwapInterval(interval))
{
throw std::runtime_error(decorateSDLError("SDL_GL_SetSwapInterval"));
}
Expand Down
3 changes: 2 additions & 1 deletion source/frontends/sdl/sdlframe.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ namespace sa2

void SaveSnapshot();

static void setGLSwapInterval(const int interval);
static bool setGLSwapInterval(const int interval);
static void changeGLSwapInterval(const int interval);

protected:
void SetApplicationIcon();
Expand Down

0 comments on commit a2a90fe

Please sign in to comment.