Skip to content

Commit

Permalink
macOS Scaling Fix (#811)
Browse files Browse the repository at this point in the history
  • Loading branch information
larsy1995 authored Feb 14, 2025
1 parent 603b6c7 commit 5d8154e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/graphic/Fast3D/gfx_pc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4076,11 +4076,7 @@ void gfx_init(struct GfxWindowManagerAPI* wapi, struct GfxRenderingAPI* rapi, co
gfx_wapi->init(game_name, rapi->get_name(), start_in_fullscreen, width, height, posX, posY);
gfx_rapi->init();
gfx_rapi->update_framebuffer_parameters(0, width, height, 1, false, true, true, true);
#ifdef __APPLE__
gfx_current_dimensions.internal_mul = 1;
#else
gfx_current_dimensions.internal_mul = CVarGetFloat(CVAR_INTERNAL_RESOLUTION, 1);
#endif
gfx_msaa_level = CVarGetInteger(CVAR_MSAA_VALUE, 1);

gfx_current_dimensions.width = width;
Expand Down Expand Up @@ -4129,6 +4125,20 @@ bool gfx_is_frame_ready() {
return gfx_wapi->is_frame_ready();
}

bool viewport_matches_render_resolution() {
#ifdef __APPLE__
// Always treat the viewport as not matching the render resolution on mac
// to avoid issues with retina scaling.
return false;
#else
if (gfx_current_dimensions.width == gfx_current_game_window_viewport.width &&
gfx_current_dimensions.height == gfx_current_game_window_viewport.height) {
return true;
}
return false;
#endif
}

void gfx_start_frame() {
gfx_wapi->get_dimensions(&gfx_current_window_dimensions.width, &gfx_current_window_dimensions.height,
&gfx_current_window_position_x, &gfx_current_window_position_y);
Expand Down Expand Up @@ -4159,12 +4169,9 @@ void gfx_start_frame() {

gfx_prev_dimensions = gfx_current_dimensions;
gfx_prev_native_dimensions = gfx_native_dimensions;

bool different_size = gfx_current_dimensions.width != gfx_current_game_window_viewport.width ||
gfx_current_dimensions.height != gfx_current_game_window_viewport.height;
if (different_size || gfx_msaa_level > 1) {
if (!viewport_matches_render_resolution() || gfx_msaa_level > 1) {
game_renders_to_framebuffer = true;
if (different_size) {
if (!viewport_matches_render_resolution()) {
gfx_rapi->update_framebuffer_parameters(game_framebuffer, gfx_current_dimensions.width,
gfx_current_dimensions.height, gfx_msaa_level, true, true, true,
true);
Expand All @@ -4175,7 +4182,7 @@ void gfx_start_frame() {
gfx_current_window_dimensions.height, gfx_msaa_level, false, true,
true, true);
}
if (gfx_msaa_level > 1 && different_size) {
if (gfx_msaa_level > 1 && !viewport_matches_render_resolution()) {
gfx_rapi->update_framebuffer_parameters(game_framebuffer_msaa_resolved, gfx_current_dimensions.width,
gfx_current_dimensions.height, 1, false, false, false, false);
}
Expand Down Expand Up @@ -4238,10 +4245,7 @@ void gfx_run(Gfx* commands, const std::unordered_map<Mtx*, MtxF>& mtx_replacemen
gfx_rapi->clear_framebuffer(true, true);

if (gfx_msaa_level > 1) {
bool different_size = gfx_current_dimensions.width != gfx_current_game_window_viewport.width ||
gfx_current_dimensions.height != gfx_current_game_window_viewport.height;

if (different_size) {
if (!viewport_matches_render_resolution()) {
gfx_rapi->resolve_msaa_color_buffer(game_framebuffer_msaa_resolved, game_framebuffer);
gfxFramebuffer = (uintptr_t)gfx_rapi->get_framebuffer_texture_id(game_framebuffer_msaa_resolved);
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/graphic/Fast3D/gfx_sdl2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,11 @@ static void gfx_sdl_set_mouse_callbacks(bool (*on_btn_down)(int btn), bool (*on_
}

static void gfx_sdl_get_dimensions(uint32_t* width, uint32_t* height, int32_t* posX, int32_t* posY) {
#ifdef __APPLE__
SDL_GetWindowSize(wnd, static_cast<int*>((void*)width), static_cast<int*>((void*)height));
#else
SDL_GL_GetDrawableSize(wnd, static_cast<int*>((void*)width), static_cast<int*>((void*)height));
#endif
SDL_GetWindowPosition(wnd, static_cast<int*>(posX), static_cast<int*>(posY));
}

Expand Down Expand Up @@ -567,7 +571,11 @@ static void gfx_sdl_handle_single_event(SDL_Event& event) {
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_SIZE_CHANGED:
#ifdef __APPLE__
SDL_GetWindowSize(wnd, &window_width, &window_height);
#else
SDL_GL_GetDrawableSize(wnd, &window_width, &window_height);
#endif
break;
case SDL_WINDOWEVENT_CLOSE:
if (event.window.windowID == SDL_GetWindowID(wnd)) {
Expand Down

0 comments on commit 5d8154e

Please sign in to comment.