Skip to content

Commit 65a73dc

Browse files
committed
SQUASHME: fetch nv driver vsync override.
1 parent 5df8b63 commit 65a73dc

4 files changed

Lines changed: 75 additions & 8 deletions

File tree

thirdparty/nvapi/nvapi.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void nvapi_finalize(void)
8989
}
9090
}
9191

92-
bool nvapi_setup_profile(NvApiProfileOpts opts)
92+
bool nvapi_setup_profile(NvApiProfileOpts opts, NvApiProfileState *active_state)
9393
{
9494
if (g_hnvapi == NULL) {
9595
return false;
@@ -138,6 +138,27 @@ bool nvapi_setup_profile(NvApiProfileOpts opts)
138138
LOG("Added application to profile");
139139
}
140140

141+
NVDRS_SETTING vsync_setting = {
142+
.version = NVDRS_SETTING_VER,
143+
};
144+
if (NvAPI_DRS_GetSetting(session, profile, VSYNCMODE_ID, &vsync_setting)) {
145+
LOG("NvAPI_DRS_GetSetting for settingId %x failed", VSYNCMODE_ID);
146+
active_state->vsync_mode = VSYNC_MODE_APP_CONTROLLED;
147+
} else {
148+
switch (vsync_setting.u32CurrentValue) {
149+
case VSYNCMODE_PASSIVE:
150+
active_state->vsync_mode = VSYNC_MODE_APP_CONTROLLED;
151+
break;
152+
case VSYNCMODE_FORCEOFF:
153+
case VSYNCMODE_VIRTUAL:
154+
active_state->vsync_mode = VSYNC_MODE_FORCE_OFF;
155+
break;
156+
default:
157+
active_state->vsync_mode = VSYNC_MODE_FORCE_ON;
158+
break;
159+
}
160+
}
161+
141162
NVDRS_SETTING setting = {
142163
.version = NVDRS_SETTING_VER,
143164
.settingId = OGL_THREAD_CONTROL_ID,

thirdparty/nvapi/nvapi.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,19 @@ typedef struct NvApiProfileOpts {
3333
bool threaded_optimization;
3434
} NvApiProfileOpts;
3535

36+
typedef enum NvApiVsyncMode {
37+
VSYNC_MODE_APP_CONTROLLED,
38+
VSYNC_MODE_FORCE_OFF,
39+
VSYNC_MODE_FORCE_ON,
40+
} NvApiVsyncMode;
41+
42+
typedef struct NvApiProfileState {
43+
NvApiVsyncMode vsync_mode;
44+
} NvApiProfileState;
45+
3646
bool nvapi_init(void);
37-
bool nvapi_setup_profile(NvApiProfileOpts opts);
47+
bool nvapi_setup_profile(NvApiProfileOpts opts,
48+
NvApiProfileState *active_state);
3849
void nvapi_finalize(void);
3950

4051
#endif

thirdparty/nvapi/nvapi_defs.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
enum ESetting {
3434
OGL_THREAD_CONTROL_ID = 0x20C1221E,
35+
VSYNCMODE_ID = 0x00A879CF,
3536
};
3637

3738
enum EValues_OGL_THREAD_CONTROL {
@@ -41,6 +42,18 @@ enum EValues_OGL_THREAD_CONTROL {
4142
OGL_THREAD_CONTROL_DEFAULT = 0U
4243
};
4344

45+
enum EValues_VSYNCMODE {
46+
VSYNCMODE_PASSIVE = 0x60925292,
47+
VSYNCMODE_FORCEOFF = 0x08416747,
48+
VSYNCMODE_FORCEON = 0x47814940,
49+
VSYNCMODE_FLIPINTERVAL2 = 0x32610244,
50+
VSYNCMODE_FLIPINTERVAL3 = 0x71271021,
51+
VSYNCMODE_FLIPINTERVAL4 = 0x13245256,
52+
VSYNCMODE_VIRTUAL = 0x18888888,
53+
VSYNCMODE_NUM_VALUES = 7,
54+
VSYNCMODE_DEFAULT = VSYNCMODE_PASSIVE
55+
};
56+
4457
typedef uint32_t NvU32;
4558
typedef uint16_t NvU16;
4659
typedef uint8_t NvU8;
@@ -223,6 +236,7 @@ typedef NVDRS_PROFILE_V1 NVDRS_PROFILE;
223236
FUNC(NvAPI_DRS_LoadSettings, 0x375DBD6B) \
224237
FUNC(NvAPI_DRS_SaveSettings, 0xFCBC7E14) \
225238
FUNC(NvAPI_DRS_SetSetting, 0x577DD202) \
239+
FUNC(NvAPI_DRS_GetSetting, 0x73BF8338) \
226240
FUNC(NvAPI_Initialize, 0x0150E828) \
227241
FUNC(NvAPI_Unload, 0xD22BDD7E)
228242

@@ -235,6 +249,7 @@ typedef int(__cdecl *NvAPI_DRS_GetApplicationInfo_t)(NvDRSSessionHandle, NvDRSPr
235249
typedef int(__cdecl *NvAPI_DRS_LoadSettings_t)(NvDRSSessionHandle);
236250
typedef int(__cdecl *NvAPI_DRS_SaveSettings_t)(NvDRSSessionHandle);
237251
typedef int(__cdecl *NvAPI_DRS_SetSetting_t)(NvDRSSessionHandle, NvDRSProfileHandle, NVDRS_SETTING *);
252+
typedef int(__cdecl *NvAPI_DRS_GetSetting_t)(NvDRSSessionHandle, NvDRSProfileHandle, NvU32, NVDRS_SETTING *);
238253
typedef int(__cdecl *NvAPI_Initialize_t)(void);
239254
typedef int(__cdecl *NvAPI_Unload_t)(void);
240255
typedef void *(__cdecl *NvAPI_QueryInterface_t)(unsigned int interface_id);

ui/xemu.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,13 @@ static void report_stats(void)
802802
#define VSYNC_SWAP_GRACE_PERIOD_NS 300000LL
803803
static int64_t display_vsync_interval = 1000000000LL / 60;
804804

805+
enum VsyncOverrideBehavior {
806+
VOB_FORCE_OFF,
807+
VOB_FORCE_ON,
808+
VOB_NO_OVERRIDE,
809+
};
810+
static enum VsyncOverrideBehavior vsync_override = VOB_NO_OVERRIDE;
811+
805812
/**
806813
* Renders the main interface. Usually called from the main thread,
807814
* but may sometimes be called from another thread.
@@ -868,7 +875,9 @@ static void gl_render_frame(struct xemu_console *scon)
868875
nv2a_release_framebuffer_surface();
869876

870877
static int64_t last_ui_frame_time_ns = 0;
871-
if (g_config.display.window.vsync && last_ui_frame_time_ns) {
878+
if (last_ui_frame_time_ns &&
879+
(vsync_override == VOB_FORCE_ON || (vsync_override == VOB_NO_OVERRIDE &&
880+
g_config.display.window.vsync))) {
872881
int64_t next_frame = last_ui_frame_time_ns + display_vsync_interval -
873882
VSYNC_SWAP_GRACE_PERIOD_NS;
874883

@@ -1292,12 +1301,23 @@ static void setup_nvidia_profile(void)
12921301
}
12931302

12941303
if (nvapi_init()) {
1295-
nvapi_setup_profile((NvApiProfileOpts){
1296-
.profile_name = L"xemu",
1297-
.executable_name = exe_name,
1298-
.threaded_optimization = false,
1299-
});
1304+
NvApiProfileState current_state;
1305+
nvapi_setup_profile(
1306+
(NvApiProfileOpts){
1307+
.profile_name = L"xemu",
1308+
.executable_name = exe_name,
1309+
.threaded_optimization = false,
1310+
},
1311+
&current_state);
13001312
nvapi_finalize();
1313+
1314+
if (current_state.vsync_mode == VSYNC_MODE_FORCE_OFF) {
1315+
vsync_override = VOB_FORCE_OFF;
1316+
} else if (current_state.vsync_mode == VSYNC_MODE_FORCE_ON) {
1317+
vsync_override = VOB_FORCE_ON;
1318+
} else {
1319+
vsync_override = VOB_NO_OVERRIDE;
1320+
}
13011321
}
13021322
}
13031323
#endif

0 commit comments

Comments
 (0)