Skip to content

Commit

Permalink
Runahead system
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwedit committed Mar 28, 2018
1 parent 2143540 commit f5e0346
Show file tree
Hide file tree
Showing 28 changed files with 1,841 additions and 5 deletions.
6 changes: 6 additions & 0 deletions config.def.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,12 @@ static const float slowmotion_ratio = 3.0;
/* Maximum fast forward ratio. */
static const float fastforward_ratio = 0.0;

/* Run core logic one or more frames ahead then load the state back to reduce perceived input lag. */
static const unsigned run_ahead_frames = 1;

/* When using the Run Ahead feature, use a secondary instance of the core. */
static const bool run_ahead_secondary_instance = true;

/* Enable stdin/network command interface. */
static const bool network_cmd_enable = false;
static const uint16_t network_cmd_port = 55355;
Expand Down
4 changes: 4 additions & 0 deletions configuration.c
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,8 @@ static struct config_bool_setting *populate_settings_bool(settings_t *settings,
SETTING_BOOL("ui_menubar_enable", &settings->bools.ui_menubar_enable, true, true, false);
SETTING_BOOL("suspend_screensaver_enable", &settings->bools.ui_suspend_screensaver_enable, true, true, false);
SETTING_BOOL("rewind_enable", &settings->bools.rewind_enable, true, rewind_enable, false);
SETTING_BOOL("run_ahead_enabled", &settings->bools.run_ahead_enabled, true, false, false);
SETTING_BOOL("run_ahead_secondary_instance", &settings->bools.run_ahead_secondary_instance, true, false, false);
SETTING_BOOL("audio_sync", &settings->bools.audio_sync, true, audio_sync, false);
SETTING_BOOL("video_shader_enable", &settings->bools.video_shader_enable, true, shader_enable, false);
SETTING_BOOL("video_shader_watch_files", &settings->bools.video_shader_watch_files, true, video_shader_watch_files, false);
Expand Down Expand Up @@ -1513,6 +1515,8 @@ static struct config_uint_setting *populate_settings_uint(settings_t *settings,
SETTING_UINT("video_msg_bgcolor_green", &settings->uints.video_msg_bgcolor_green, true, message_bgcolor_green, false);
SETTING_UINT("video_msg_bgcolor_blue", &settings->uints.video_msg_bgcolor_blue, true, message_bgcolor_blue, false);

SETTING_UINT("run_ahead_frames", &settings->uints.run_ahead_frames, true, 1, false);

*size = count;

return tmp;
Expand Down
4 changes: 4 additions & 0 deletions configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ typedef struct settings
bool playlist_entry_remove;
bool playlist_entry_rename;
bool rewind_enable;
bool run_ahead_enabled;
bool run_ahead_secondary_instance;
bool pause_nonactive;
bool block_sram_overwrite;
bool savestate_auto_index;
Expand Down Expand Up @@ -380,6 +382,8 @@ typedef struct settings
unsigned input_remap_ids[MAX_USERS][RARCH_CUSTOM_BIND_LIST_END];

unsigned led_map[MAX_LEDS];

unsigned run_ahead_frames;
} uints;

struct
Expand Down
3 changes: 3 additions & 0 deletions core.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ bool core_set_poll_type(unsigned *type);
/* Runs the core for one frame. */
bool core_run(void);

/* Runs the core for one frame, but does not trigger any input polling */
bool core_run_no_input_polling(void);

bool core_init(void);

bool core_deinit(void *data);
Expand Down
14 changes: 14 additions & 0 deletions core_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
#include "gfx/video_driver.h"
#include "audio/audio_driver.h"

#include "runahead/copy_load_info.h"
#include "runahead/secondary_core.h"

struct retro_callbacks retro_ctx;
struct retro_core_t current_core;

Expand Down Expand Up @@ -262,6 +265,9 @@ bool core_set_controller_port_device(retro_ctx_controller_info_t *pad)
{
if (!pad)
return false;

remember_controller_port_device(pad->port, pad->device);

current_core.retro_set_controller_port_device(pad->port, pad->device);
return true;
}
Expand All @@ -277,6 +283,8 @@ bool core_get_memory(retro_ctx_memory_info_t *info)

bool core_load_game(retro_ctx_load_content_info_t *load_info)
{
set_load_content_info(load_info);

bool contentless = false;
bool is_inited = false;

Expand Down Expand Up @@ -424,6 +432,12 @@ bool core_run(void)
return true;
}

bool core_run_no_input_polling(void)
{
current_core.retro_run();
return true;
}

bool core_load(unsigned poll_type_behavior)
{
current_core.poll_type = poll_type_behavior;
Expand Down
35 changes: 31 additions & 4 deletions dynamic.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@
#include "msg_hash.h"
#include "verbosity.h"

#include "runahead/secondary_core.h"

#ifdef HAVE_DYNAMIC
#define SYMBOL(x) do { \
function_t func = dylib_proc(lib_handle, #x); \
function_t func = dylib_proc(lib_handle_local, #x); \
memcpy(&current_core->x, &func, sizeof(func)); \
if (current_core->x == NULL) { RARCH_ERR("Failed to load symbol: \"%s\"\n", #x); retroarch_fail(1, "init_libretro_sym()"); } \
} while (0)
Expand Down Expand Up @@ -383,14 +385,32 @@ bool libretro_get_system_info(const char *path,
* Setup libretro callback symbols. Returns true on success,
* or false if symbols could not be loaded.
**/
static bool load_symbols(enum rarch_core_type type, struct retro_core_t *current_core)
bool init_libretro_sym_custom(enum rarch_core_type type, struct retro_core_t *current_core, const char *lib_path, dylib_t *lib_handle_p)
{
/* the library handle for use with the SYMBOL macro */
dylib_t lib_handle_local;
switch (type)
{
case CORE_TYPE_PLAIN:
#ifdef HAVE_DYNAMIC
if (!load_dynamic_core())
return false;

if (lib_path == NULL || lib_handle_p == NULL)
{
if (!load_dynamic_core())
return false;
lib_handle_local = lib_handle;
}
else
{
/* for a secondary core, we already have a primary library loaded, so we can skip some checks and just load the library */
retro_assert(lib_path != NULL && lib_handle_p != NULL);
lib_handle_local = dylib_load(lib_path);
if (lib_handle_local == NULL)
{
return false;
}
*lib_handle_p = lib_handle_local;
}
#endif

SYMBOL(retro_init);
Expand Down Expand Up @@ -615,6 +635,11 @@ static bool load_symbols(enum rarch_core_type type, struct retro_core_t *current
return true;
}

static bool load_symbols(enum rarch_core_type type, struct retro_core_t *current_core)
{
return init_libretro_sym_custom(type, current_core, NULL, NULL);
}

/**
* init_libretro_sym:
* @type : Type of core to be loaded.
Expand All @@ -634,6 +659,8 @@ bool init_libretro_sym(enum rarch_core_type type, struct retro_core_t *current_c
if (!load_symbols(type, current_core))
return false;

/* remember last core type created, so creating a secondary core will know what core type to use */
set_last_core_type(type);
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions dynamic.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <boolean.h>
#include <retro_common_api.h>
#include <libretro.h>
#include <dynamic/dylib.h>

#include "core_type.h"

Expand Down Expand Up @@ -132,6 +133,8 @@ bool libretro_get_shared_context(void);
bool init_libretro_sym(enum rarch_core_type type,
struct retro_core_t *core);

bool init_libretro_sym_custom(enum rarch_core_type type, struct retro_core_t *current_core, const char *lib_path, dylib_t *lib_handle_p);

/**
* uninit_libretro_sym:
*
Expand Down
10 changes: 10 additions & 0 deletions griffin/griffin.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ COMPATIBILITY

#include "../libretro-common/compat/compat_fnmatch.c"
#include "../libretro-common/compat/fopen_utf8.c"
#if defined(HAVE_DYNAMIC) && HAVE_DYNAMIC
#include "../libretro-common/compat/unlink_utf8.c"
#endif
#include "../libretro-common/memmap/memalign.c"

/*============================================================
Expand Down Expand Up @@ -1252,6 +1255,13 @@ MENU
#include "../libretro-common/net/net_http_parse.c"
#endif

#include "../runahead/mem_util.c"
#include "../runahead/secondary_core.c"
#include "../runahead/run_ahead.c"
#include "../runahead/copy_load_info.c"
#include "../runahead/dirty_input.c"
#include "../runahead/mylist.c"

/*============================================================
DEPENDENCIES
============================================================ */
Expand Down
6 changes: 6 additions & 0 deletions intl/msg_hash_lbl.h
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,12 @@ MSG_HASH(MENU_ENUM_LABEL_SHUTDOWN,
"shutdown")
MSG_HASH(MENU_ENUM_LABEL_SLOWMOTION_RATIO,
"slowmotion_ratio")
MSG_HASH(MENU_ENUM_LABEL_RUN_AHEAD_ENABLED,
"run_ahead_enabled")
MSG_HASH(MENU_ENUM_LABEL_RUN_AHEAD_SECONDARY_INSTANCE,
"run_ahead_secondary_instance")
MSG_HASH(MENU_ENUM_LABEL_RUN_AHEAD_FRAMES,
"run_ahead_frames")
MSG_HASH(MENU_ENUM_LABEL_SORT_SAVEFILES_ENABLE,
"sort_savefiles_enable")
MSG_HASH(MENU_ENUM_LABEL_SORT_SAVESTATES_ENABLE,
Expand Down
18 changes: 18 additions & 0 deletions intl/msg_hash_us.h
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,12 @@ MSG_HASH(MENU_ENUM_LABEL_VALUE_SHUTDOWN,
"Shutdown")
MSG_HASH(MENU_ENUM_LABEL_VALUE_SLOWMOTION_RATIO,
"Slow-Motion Ratio")
MSG_HASH(MENU_ENUM_LABEL_VALUE_RUN_AHEAD_ENABLED,
"Run-Ahead to Reduce Latency")
MSG_HASH(MENU_ENUM_LABEL_VALUE_RUN_AHEAD_FRAMES,
"Number of Frames to Run Ahead")
MSG_HASH(MENU_ENUM_LABEL_VALUE_RUN_AHEAD_SECONDARY_INSTANCE,
"Runahead Use Second Instance")
MSG_HASH(MENU_ENUM_LABEL_VALUE_SORT_SAVEFILES_ENABLE,
"Sort Saves In Folders")
MSG_HASH(MENU_ENUM_LABEL_VALUE_SORT_SAVESTATES_ENABLE,
Expand Down Expand Up @@ -2720,6 +2726,18 @@ MSG_HASH(
MENU_ENUM_SUBLABEL_SLOWMOTION_RATIO,
"When in slow motion, content will slow down by the factor specified/set."
)
MSG_HASH(
MENU_ENUM_SUBLABEL_RUN_AHEAD_ENABLED,
"Run core logic one or more frames ahead then load the state back to reduce perceived input lag."
)
MSG_HASH(
MENU_ENUM_SUBLABEL_RUN_AHEAD_FRAMES,
"The number of frames to run ahead. Causes gameplay issues such as jitter if you exceed the number of lag frames internal to the game."
)
MSG_HASH(
MENU_ENUM_SUBLABEL_RUN_AHEAD_SECONDARY_INSTANCE,
"Use a second instance of the RetroArch core to run ahead. Prevents audio problems due to loading state."
)
MSG_HASH(
MENU_ENUM_SUBLABEL_REWIND_ENABLE,
"Enable rewinding. This will take a performance hit when playing."
Expand Down
30 changes: 30 additions & 0 deletions libretro-common/compat/unlink_utf8.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <compat/unlink_utf8.h>
#include <encodings/utf.h>
#include "boolean.h"
#include <malloc.h>

#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0500 || defined(_XBOX)
#ifndef LEGACY_WIN32
#define LEGACY_WIN32
#endif
#endif

#ifdef _WIN32

#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>

bool unlink_utf8(const char * filename)
{
#if defined(LEGACY_WIN32)
bool result = DeleteFileA(filename_w);
#else
wchar_t * filename_w = utf8_to_utf16_string_alloc(filename);
bool result = DeleteFileW(filename_w);
free(filename_w);
#endif
return result;
}

#endif
23 changes: 23 additions & 0 deletions libretro-common/include/compat/unlink_utf8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __UNLINK_UTF8_H
#define __UNLINK_UTF8_H

#include "boolean.h"

#ifdef _WIN32

#if __cplusplus
extern "C"
{
#endif

bool unlink_utf8(const char * filename);

#if __cplusplus
}
#endif

#else
#include <unistd.h>
#define unlink_utf8 unlink
#endif
#endif
12 changes: 12 additions & 0 deletions menu/cbs/menu_cbs_sublabel.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ default_sublabel_macro(action_bind_sublabel_savestate_auto_index, MENU_
default_sublabel_macro(action_bind_sublabel_block_sram_overwrite, MENU_ENUM_SUBLABEL_BLOCK_SRAM_OVERWRITE)
default_sublabel_macro(action_bind_sublabel_fastforward_ratio, MENU_ENUM_SUBLABEL_FASTFORWARD_RATIO)
default_sublabel_macro(action_bind_sublabel_slowmotion_ratio, MENU_ENUM_SUBLABEL_SLOWMOTION_RATIO)
default_sublabel_macro(action_bind_sublabel_run_ahead_enabled, MENU_ENUM_SUBLABEL_RUN_AHEAD_ENABLED)
default_sublabel_macro(action_bind_sublabel_run_ahead_secondary_instance, MENU_ENUM_SUBLABEL_RUN_AHEAD_SECONDARY_INSTANCE)
default_sublabel_macro(action_bind_sublabel_run_ahead_frames, MENU_ENUM_SUBLABEL_RUN_AHEAD_FRAMES)
default_sublabel_macro(action_bind_sublabel_rewind, MENU_ENUM_SUBLABEL_REWIND_ENABLE)
default_sublabel_macro(action_bind_sublabel_rewind_granularity, MENU_ENUM_SUBLABEL_REWIND_GRANULARITY)
default_sublabel_macro(action_bind_sublabel_libretro_log_level, MENU_ENUM_SUBLABEL_LIBRETRO_LOG_LEVEL)
Expand Down Expand Up @@ -1109,6 +1112,15 @@ int menu_cbs_init_bind_sublabel(menu_file_list_cbs_t *cbs,
case MENU_ENUM_LABEL_SLOWMOTION_RATIO:
BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_slowmotion_ratio);
break;
case MENU_ENUM_LABEL_RUN_AHEAD_ENABLED:
BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_run_ahead_enabled);
break;
case MENU_ENUM_LABEL_RUN_AHEAD_SECONDARY_INSTANCE:
BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_run_ahead_secondary_instance);
break;
case MENU_ENUM_LABEL_RUN_AHEAD_FRAMES:
BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_run_ahead_frames);
break;
case MENU_ENUM_LABEL_FASTFORWARD_RATIO:
BIND_ACTION_SUBLABEL(cbs, action_bind_sublabel_fastforward_ratio);
break;
Expand Down
9 changes: 9 additions & 0 deletions menu/menu_displaylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -4963,6 +4963,15 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type, void *data)
menu_displaylist_parse_settings_enum(menu, info,
MENU_ENUM_LABEL_SLOWMOTION_RATIO,
PARSE_ONLY_FLOAT, false);
menu_displaylist_parse_settings_enum(menu, info,
MENU_ENUM_LABEL_RUN_AHEAD_ENABLED,
PARSE_ONLY_BOOL, false);
menu_displaylist_parse_settings_enum(menu, info,
MENU_ENUM_LABEL_RUN_AHEAD_FRAMES,
PARSE_ONLY_UINT, false);
menu_displaylist_parse_settings_enum(menu, info,
MENU_ENUM_LABEL_RUN_AHEAD_SECONDARY_INSTANCE,
PARSE_ONLY_BOOL, false);
if (settings->bools.menu_show_advanced_settings)
menu_displaylist_parse_settings_enum(menu, info,
MENU_ENUM_LABEL_MENU_THROTTLE_FRAMERATE,
Expand Down
Loading

0 comments on commit f5e0346

Please sign in to comment.