Skip to content

Commit

Permalink
framerate and vsync menus and fixes (#1552)
Browse files Browse the repository at this point in the history
* in-game vsync option, automatically framelimit correctly, fix minimize/alttab bugs

* fix jp text errors

* bump version number as panic

* this makes more sense i think

* clang

* oops delete debug print
  • Loading branch information
ManDude authored Jun 25, 2022
1 parent 584cbcd commit c9de15b
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 97 deletions.
3 changes: 2 additions & 1 deletion decompiler/config/all-types.gc
Original file line number Diff line number Diff line change
Expand Up @@ -1316,7 +1316,7 @@
(camera-controls-first-vert #x1004)
(camera-controls-third-horz #x1005)
(camera-controls-third-vert #x1006)
(camera-controls-original #x1007)
(restore-defaults #x1007)
(misc-options #x100f)
(accessibility-options #x1010)
(money-starburst #x1011)
Expand All @@ -1336,6 +1336,7 @@
(ps2-aspect-ratio-msg #x1038)
(aspect-ratio-ps2 #x1039)
(fit-to-screen #x103a)
(vsync #x103b)
(msaa #x1050)
(x-times-fmt #x1051)
(2-times #x1052)
Expand Down
2 changes: 2 additions & 0 deletions game/assets/jak1/text/game_text_en.gs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"ASPECT RATIO (PS2)")
(#x103a "FIT TO SCREEN"
"FIT TO SCREEN")
(#x103b "V-SYNC"
"V-SYNC")

(#x1050 "MSAA"
"MSAA")
Expand Down
6 changes: 4 additions & 2 deletions game/assets/jak1/text/game_text_ja.gs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
(#x10d2 "セイジィ")
(#x10d3 "セイジィの家")

(#x10d6 "ケイラ")

(#x10df "みしよう")

(#x10e3 "みしよう1")
Expand All @@ -59,13 +61,13 @@
(#x10fc "ラーカーのとりで")
(#x10fd "ゆきだま")
(#x10fe "マグマチューブの中かん")
(#x10ff "マグマチューブのさいしゅうかい")
(#x10ff "マグマチューブのけつまつ")
(#x1100 "黄の賢者")
(#x1101 "赤の賢者")
(#x1102 "青の賢者")
(#x1103 "城のハブ")
(#x1104 "ラスボスの中かん")
(#x1105 "ラスボスのさいしゅうかい")
(#x1105 "ラスボスのけつまつ")



5 changes: 4 additions & 1 deletion game/graphics/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ class GfxDisplay {
virtual void set_size(int w, int h) = 0;
virtual void update_fullscreen(GfxDisplayMode mode, int screen) = 0;
virtual void get_scale(float* x, float* y) = 0;
virtual void get_screen_size(int vmode_idx, s32* w, s32* h, s32* c) = 0;
virtual int get_screen_vmode_count() = 0;
virtual void get_screen_size(int vmode_idx, s32* w, s32* h) = 0;
virtual int get_screen_rate(int vmode_idx) = 0;
virtual void get_position(int* x, int* y) = 0;
virtual void get_size(int* w, int* h) = 0;
virtual GfxDisplayMode get_fullscreen() = 0;
virtual void render() = 0;
virtual void set_lock(bool lock) = 0;
virtual bool minimized() = 0;
bool is_active() const { return get_window() != nullptr; }
void set_title(const char* title);
const char* title() const { return m_title; }
Expand Down
26 changes: 24 additions & 2 deletions game/graphics/gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,32 @@ GfxDisplayMode get_fullscreen() {
}
}

void get_screen_size(s64 vmode_idx, s32* w, s32* h, s32* c) {
int get_screen_vmode_count() {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_screen_size(vmode_idx, w, h, c);
return Display::GetMainDisplay()->get_screen_vmode_count();
}
return 0;
}

int get_screen_rate(s64 vmode_idx) {
if (Display::GetMainDisplay()) {
return Display::GetMainDisplay()->get_screen_rate(vmode_idx);
}
return 0;
}

void get_screen_size(s64 vmode_idx, s32* w, s32* h) {
if (Display::GetMainDisplay()) {
Display::GetMainDisplay()->get_screen_size(vmode_idx, w, h);
}
}

void set_vsync(bool vsync) {
g_global_settings.vsync = vsync;
}

void set_frame_rate(int rate) {
g_global_settings.target_fps = rate;
}

void set_letterbox(int w, int h) {
Expand Down
17 changes: 16 additions & 1 deletion game/graphics/gfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ struct GfxGlobalSettings {
bool collision_enable = false;
bool collision_wireframe = true;

// vsync enable
bool vsync = true;
bool old_vsync = false;
// target frame rate
float target_fps = 60;
// use custom frame limiter
bool framelimiter = true;

bool experimental_accurate_lag = false;
bool sleep_in_frame_limiter = true;

// matching enum in kernel-defs.gc !!
enum CollisionRendererMode { None, Mode, Event, Material, Skip } collision_mode = Mode;
std::array<u32, (PAT_MOD_COUNT + 31) / 32> collision_mode_mask = {UINT32_MAX};
Expand Down Expand Up @@ -113,7 +124,11 @@ u64 get_window_height();
void set_window_size(u64 w, u64 h);
void get_window_scale(float* x, float* y);
GfxDisplayMode get_fullscreen();
void get_screen_size(s64 vmode_idx, s32* w, s32* h, s32* c);
int get_screen_vmode_count();
int get_screen_rate(s64 vmode_idx);
void get_screen_size(s64 vmode_idx, s32* w, s32* h);
void set_frame_rate(int rate);
void set_vsync(bool vsync);
void set_letterbox(int w, int h);
void set_fullscreen(GfxDisplayMode mode, int screen);
void set_window_lock(bool lock);
Expand Down
11 changes: 6 additions & 5 deletions game/graphics/opengl_renderer/debug_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <algorithm>

#include "game/graphics/gfx.h"
#include "game/kernel/svnrev.h"

#include "third-party/imgui/imgui.h"
Expand Down Expand Up @@ -113,16 +114,16 @@ void OpenGlDebugGui::draw(const DmaStats& dma_stats) {
}

if (ImGui::BeginMenu("Frame Rate")) {
ImGui::Checkbox("Enable V-Sync", &m_vsync);
ImGui::Checkbox("Enable V-Sync", &Gfx::g_global_settings.vsync);
ImGui::Separator();
ImGui::Checkbox("Framelimiter", &framelimiter);
ImGui::Checkbox("Framelimiter", &Gfx::g_global_settings.framelimiter);
ImGui::InputFloat("Target FPS", &target_fps_input);
if (ImGui::MenuItem("Apply")) {
target_fps = target_fps_input;
Gfx::g_global_settings.target_fps = target_fps_input;
}
ImGui::Separator();
ImGui::Checkbox("Accurate Lag Mode", &experimental_accurate_lag);
ImGui::Checkbox("Sleep in Frame Limiter", &sleep_in_frame_limiter);
ImGui::Checkbox("Accurate Lag Mode", &Gfx::g_global_settings.experimental_accurate_lag);
ImGui::Checkbox("Sleep in Frame Limiter", &Gfx::g_global_settings.sleep_in_frame_limiter);
ImGui::EndMenu();
}

Expand Down
11 changes: 1 addition & 10 deletions game/graphics/opengl_renderer/debug_gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ class OpenGlDebugGui {
const char* screenshot_name() const { return m_screenshot_save_name; }

bool should_advance_frame() { return m_frame_timer.should_advance_frame(); }
bool should_gl_finish() { return m_frame_timer.do_gl_finish; }

void init_framerate_settings();
bool should_gl_finish() const { return m_frame_timer.do_gl_finish; }

bool get_screenshot_flag() {
if (m_want_screenshot) {
Expand All @@ -60,17 +58,10 @@ class OpenGlDebugGui {
return false;
}

bool get_vsync_flag() { return m_vsync; }

bool framelimiter = false;
float target_fps = 60.f;
bool experimental_accurate_lag = false;
bool sleep_in_frame_limiter = true;
bool small_profiler = false;
bool record_events = false;
bool dump_events = false;
bool want_reboot_in_debug = false;
bool m_vsync = true;

private:
FrameTimeRecorder m_frame_timer;
Expand Down
89 changes: 53 additions & 36 deletions game/graphics/pipelines/opengl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct GraphicsData {
// vsync
std::mutex sync_mutex;
std::condition_variable sync_cv;
bool vsync_enabled = true;

// dma chain transfer
std::mutex dma_mutex;
Expand Down Expand Up @@ -185,39 +184,37 @@ static std::shared_ptr<GfxDisplay> gl_make_display(int width,
stbi_image_free(images[0].pixels);

// init framerate settings
GLFWmonitor* primary_monitor = glfwGetPrimaryMonitor();
if (primary_monitor) {
/*
if (GLFWmonitor* primary_monitor = glfwGetPrimaryMonitor()) {
auto primary_monitor_video_mode = glfwGetVideoMode(primary_monitor);
if (primary_monitor_video_mode && primary_monitor_video_mode->refreshRate > 60) {
// Use the framelimiter by default and disable vsync
g_gfx_data->debug_gui.framelimiter = true;
g_gfx_data->debug_gui.m_vsync = false;
g_gfx_data->vsync_enabled = false;
glfwSwapInterval(false);
Gfx::g_global_settings.framelimiter = true;
Gfx::g_global_settings.vsync = false;
glfwSwapInterval(0);
if (primary_monitor_video_mode->refreshRate > 100) {
BootVideoMode = VideoMode::FPS150;
g_gfx_data->debug_gui.target_fps = 150;
Gfx::g_global_settings.target_fps = 150;
} else if (primary_monitor_video_mode->refreshRate > 60) {
BootVideoMode = VideoMode::FPS100;
g_gfx_data->debug_gui.target_fps = 100;
Gfx::g_global_settings.target_fps = 100;
}
} else {
// enable vsync
g_gfx_data->debug_gui.framelimiter = false;
g_gfx_data->debug_gui.m_vsync = true;
g_gfx_data->vsync_enabled = true;
Gfx::g_global_settings.framelimiter = false;
Gfx::g_global_settings.vsync = true;
// glfwSwapInterval(1);
glfwSwapInterval(settings.vsync);
}
} else {
// enable vsync
g_gfx_data->debug_gui.framelimiter = false;
g_gfx_data->debug_gui.m_vsync = true;
g_gfx_data->vsync_enabled = true;
Gfx::g_global_settings.framelimiter = false;
Gfx::g_global_settings.vsync = true;
// glfwSwapInterval(1);
glfwSwapInterval(settings.vsync);
}
*/

SetDisplayCallbacks(window);
Pad::initialize();
Expand Down Expand Up @@ -417,24 +414,25 @@ GfxDisplayMode GLDisplay::get_fullscreen() {
}
}

void GLDisplay::get_screen_size(int vmode_idx, s32* w_out, s32* h_out, s32* count_out) {
int GLDisplay::get_screen_vmode_count() {
int count = 0;
auto vmodes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
return count;
}

void GLDisplay::get_screen_size(int vmode_idx, s32* w_out, s32* h_out) {
auto vmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
if (get_fullscreen() == GfxDisplayMode::Fullscreen) {
auto vmodes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
if (vmode_idx >= 0) {
vmode = &vmodes[vmode_idx];
} else {
for (int i = 0; i < count; ++i) {
if (!vmode || vmode->height < vmodes[i].height) {
vmode = &vmodes[i];
}
int count = 0;
auto vmodes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
if (vmode_idx >= 0) {
vmode = &vmodes[vmode_idx];
} else if (get_fullscreen() == GfxDisplayMode::Fullscreen) {
for (int i = 0; i < count; ++i) {
if (!vmode || vmode->height < vmodes[i].height) {
vmode = &vmodes[i];
}
}
}
if (count_out) {
*count_out = count;
}
if (w_out) {
*w_out = vmode->width;
}
Expand All @@ -443,6 +441,26 @@ void GLDisplay::get_screen_size(int vmode_idx, s32* w_out, s32* h_out, s32* coun
}
}

int GLDisplay::get_screen_rate(int vmode_idx) {
auto vmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
int count = 0;
auto vmodes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
if (vmode_idx >= 0) {
vmode = &vmodes[vmode_idx];
} else if (get_fullscreen() == GfxDisplayMode::Fullscreen) {
for (int i = 0; i < count; ++i) {
if (!vmode || vmode->refreshRate < vmodes[i].refreshRate) {
vmode = &vmodes[i];
}
}
}
return vmode->refreshRate;
}

bool GLDisplay::minimized() {
return glfwGetWindowAttrib(m_window, GLFW_ICONIFIED);
}

void GLDisplay::set_lock(bool lock) {
glfwSetWindowAttrib(m_window, GLFW_RESIZABLE, lock ? GLFW_TRUE : GLFW_FALSE);
}
Expand Down Expand Up @@ -521,10 +539,9 @@ void GLDisplay::render() {
}

// switch vsync modes, if requested
bool req_vsync = g_gfx_data->debug_gui.get_vsync_flag();
if (req_vsync != g_gfx_data->vsync_enabled) {
g_gfx_data->vsync_enabled = req_vsync;
glfwSwapInterval(req_vsync);
if (Gfx::g_global_settings.vsync != Gfx::g_global_settings.old_vsync) {
Gfx::g_global_settings.old_vsync = Gfx::g_global_settings.vsync;
glfwSwapInterval(Gfx::g_global_settings.vsync);
}

// actual vsync
Expand All @@ -533,17 +550,17 @@ void GLDisplay::render() {
auto p = scoped_prof("swap-buffers");
glfwSwapBuffers(m_window);
}
if (g_gfx_data->debug_gui.framelimiter) {
if (Gfx::g_global_settings.framelimiter) {
auto p = scoped_prof("frame-limiter");
g_gfx_data->frame_limiter.run(
g_gfx_data->debug_gui.target_fps, g_gfx_data->debug_gui.experimental_accurate_lag,
g_gfx_data->debug_gui.sleep_in_frame_limiter, g_gfx_data->last_engine_time);
Gfx::g_global_settings.target_fps, Gfx::g_global_settings.experimental_accurate_lag,
Gfx::g_global_settings.sleep_in_frame_limiter, g_gfx_data->last_engine_time);
}
g_gfx_data->debug_gui.start_frame();
prof().instant_event("ROOT");
update_global_profiler();

if (fullscreen_pending()) {
if (!minimized() && fullscreen_pending()) {
fullscreen_flush();
}
update_last_fullscreen_mode();
Expand Down
5 changes: 4 additions & 1 deletion game/graphics/pipelines/opengl.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ class GLDisplay : public GfxDisplay {
void get_position(int* x, int* y);
void get_size(int* w, int* h);
void get_scale(float* x, float* y);
void get_screen_size(int vmode_idx, s32* w, s32* h, s32* c);
void get_screen_size(int vmode_idx, s32* w, s32* h);
int get_screen_rate(int vmode_idx);
int get_screen_vmode_count();
GfxDisplayMode get_fullscreen();
void set_size(int w, int h);
void update_fullscreen(GfxDisplayMode mode, int screen);
void render();
bool minimized();
void set_lock(bool lock);
};

Expand Down
Loading

0 comments on commit c9de15b

Please sign in to comment.