System: Rewrite save state I/O

No more ByteStream or virtual calls for every piece of data.
This commit is contained in:
Stenzek
2024-07-29 16:23:39 +10:00
parent dd8bf2c9d9
commit a6518ff9dc
14 changed files with 724 additions and 515 deletions

View File

@@ -136,8 +136,8 @@ set(NEWREC_SOURCES
target_precompile_headers(core PRIVATE "pch.h")
target_include_directories(core PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/..")
target_include_directories(core PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/..")
target_link_libraries(core PUBLIC Threads::Threads common util ZLIB::ZLIB)
target_link_libraries(core PRIVATE xxhash imgui rapidyaml rcheevos cpuinfo::cpuinfo)
target_link_libraries(core PUBLIC Threads::Threads common util)
target_link_libraries(core PRIVATE xxhash imgui rapidyaml rcheevos cpuinfo::cpuinfo ZLIB::ZLIB Zstd::Zstd)
if(CPU_ARCH_X64)
target_compile_definitions(core PUBLIC "ENABLE_RECOMPILER=1" "ENABLE_NEWREC=1" "ENABLE_MMAP_FASTMEM=1")

View File

@@ -5559,11 +5559,11 @@ bool FullscreenUI::InitializeSaveStateListEntryFromPath(SaveStateListEntry* li,
void FullscreenUI::PopulateSaveStateScreenshot(SaveStateListEntry* li, const ExtendedSaveStateInfo* ssi)
{
li->preview_texture.reset();
if (ssi && !ssi->screenshot_data.empty())
if (ssi && ssi->screenshot.IsValid())
{
li->preview_texture = g_gpu_device->FetchTexture(ssi->screenshot_width, ssi->screenshot_height, 1, 1, 1,
li->preview_texture = g_gpu_device->FetchTexture(ssi->screenshot.GetWidth(), ssi->screenshot.GetHeight(), 1, 1, 1,
GPUTexture::Type::Texture, GPUTexture::Format::RGBA8,
ssi->screenshot_data.data(), sizeof(u32) * ssi->screenshot_width);
ssi->screenshot.GetPixels(), ssi->screenshot.GetPitch());
}
else
{
@@ -6112,7 +6112,7 @@ void FullscreenUI::DoLoadState(std::string path)
else
{
Error error;
if (!System::LoadState(path.c_str(), &error))
if (!System::LoadState(path.c_str(), &error, true))
{
ShowToast(std::string(),
fmt::format(TRANSLATE_FS("System", "Failed to load state: {}"), error.GetDescription()));

View File

@@ -84,7 +84,7 @@ static void HotkeyLoadStateSlot(bool global, s32 slot)
}
Error error;
if (!System::LoadState(path.c_str(), &error))
if (!System::LoadState(path.c_str(), &error, true))
{
Host::AddKeyedOSDMessage(
"LoadState",

View File

@@ -891,11 +891,11 @@ void SaveStateSelectorUI::InitializeListEntry(ListEntry* li, ExtendedSaveStateIn
{
g_gpu_device->RecycleTexture(std::move(li->preview_texture));
if (ssi && !ssi->screenshot_data.empty())
if (ssi->screenshot.IsValid())
{
li->preview_texture = g_gpu_device->FetchTexture(
ssi->screenshot_width, ssi->screenshot_height, 1, 1, 1, GPUTexture::Type::Texture, GPUTexture::Format::RGBA8,
ssi->screenshot_data.data(), sizeof(u32) * ssi->screenshot_width);
li->preview_texture = g_gpu_device->FetchTexture(ssi->screenshot.GetWidth(), ssi->screenshot.GetHeight(), 1, 1, 1,
GPUTexture::Type::Texture, GPUTexture::Format::RGBA8,
ssi->screenshot.GetPixels(), ssi->screenshot.GetPitch());
if (!li->preview_texture) [[unlikely]]
ERROR_LOG("Failed to upload save state image to GPU");
}
@@ -1084,7 +1084,7 @@ void SaveStateSelectorUI::LoadCurrentSlot()
if (FileSystem::FileExists(path.c_str()))
{
Error error;
if (!System::LoadState(path.c_str(), &error))
if (!System::LoadState(path.c_str(), &error, true))
{
Host::AddKeyedOSDMessage("LoadState",
fmt::format(TRANSLATE_FS("OSDMessage", "Failed to load state from slot {0}:\n{1}"),

View File

@@ -133,7 +133,7 @@ static bool s_receive_buffer_full = false;
static bool s_transmit_buffer_full = false;
static u32 s_last_memory_card_transfer_frame = 0;
static std::unique_ptr<GrowableMemoryByteStream> s_memory_card_backup;
static DynamicHeapArray<u8> s_memory_card_backup;
static std::unique_ptr<MemoryCard> s_dummy_card;
} // namespace Pad
@@ -145,7 +145,7 @@ void Pad::Initialize()
void Pad::Shutdown()
{
s_memory_card_backup.reset();
s_memory_card_backup.deallocate();
s_transfer_event.Deactivate();
@@ -197,7 +197,7 @@ bool Pad::DoStateController(StateWrapper& sw, u32 i)
if (controller_type != state_controller_type)
{
const Controller::ControllerInfo* state_cinfo = Controller::GetControllerInfo(state_controller_type);
Assert(sw.GetMode() == StateWrapper::Mode::Read);
Assert(sw.IsReading());
// UI notification portion is separated from emulation portion (intentional condition check redundancy)
if (g_settings.load_devices_from_save_states)
@@ -363,16 +363,10 @@ void Pad::BackupMemoryCardState()
{
DEV_LOG("Backing up memory card state.");
if (!s_memory_card_backup)
{
s_memory_card_backup =
std::make_unique<GrowableMemoryByteStream>(nullptr, MemoryCard::STATE_SIZE * NUM_CONTROLLER_AND_CARD_PORTS);
}
s_memory_card_backup->SeekAbsolute(0);
StateWrapper sw(s_memory_card_backup.get(), StateWrapper::Mode::Write, SAVE_STATE_VERSION);
if (s_memory_card_backup.empty())
s_memory_card_backup.resize(MemoryCard::STATE_SIZE * NUM_CONTROLLER_AND_CARD_PORTS);
StateWrapper sw(s_memory_card_backup.span(), StateWrapper::Mode::Write, SAVE_STATE_VERSION);
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
{
if (s_memory_cards[i])
@@ -382,13 +376,11 @@ void Pad::BackupMemoryCardState()
void Pad::RestoreMemoryCardState()
{
DebugAssert(s_memory_card_backup);
DebugAssert(!s_memory_card_backup.empty());
VERBOSE_LOG("Restoring backed up memory card state.");
s_memory_card_backup->SeekAbsolute(0);
StateWrapper sw(s_memory_card_backup.get(), StateWrapper::Mode::Read, SAVE_STATE_VERSION);
StateWrapper sw(s_memory_card_backup.cspan(), StateWrapper::Mode::Read, SAVE_STATE_VERSION);
for (u32 i = 0; i < NUM_CONTROLLER_AND_CARD_PORTS; i++)
{
if (s_memory_cards[i])

View File

@@ -493,7 +493,7 @@ bool PINEServer::PINESocket::HandleCommand(IPCCommand command, BinarySpanReader
Host::RunOnCPUThread([state_filename = std::move(state_filename)] {
Error error;
if (!System::LoadState(state_filename.c_str(), &error))
if (!System::LoadState(state_filename.c_str(), &error, true))
ERROR_LOG("PINE: Load state failed: {}", error.GetDescription());
});

View File

@@ -5,11 +5,18 @@
#include "types.h"
static constexpr u32 SAVE_STATE_MAGIC = 0x43435544;
static constexpr u32 SAVE_STATE_VERSION = 68;
static constexpr u32 SAVE_STATE_VERSION = 69;
static constexpr u32 SAVE_STATE_MINIMUM_VERSION = 42;
static_assert(SAVE_STATE_VERSION >= SAVE_STATE_MINIMUM_VERSION);
enum class SaveStateCompression : u32
{
None = 0,
ZLib = 1,
ZStd = 2,
};
#pragma pack(push, 4)
struct SAVE_STATE_HEADER
{
@@ -17,10 +24,6 @@ struct SAVE_STATE_HEADER
{
MAX_TITLE_LENGTH = 128,
MAX_SERIAL_LENGTH = 32,
COMPRESSION_TYPE_NONE = 0,
COMPRESSION_TYPE_ZLIB = 1,
COMPRESSION_TYPE_ZSTD = 2,
};
u32 magic;
@@ -28,14 +31,16 @@ struct SAVE_STATE_HEADER
char title[MAX_TITLE_LENGTH];
char serial[MAX_SERIAL_LENGTH];
u32 media_filename_length;
u32 offset_to_media_filename;
u32 media_path_length;
u32 offset_to_media_path;
u32 media_subimage_index;
u32 unused_offset_to_playlist_filename; // Unused as of version 51.
// Screenshot compression added in version 69.
// Uncompressed size not stored, it can be inferred from width/height.
u32 screenshot_compression_type;
u32 screenshot_width;
u32 screenshot_height;
u32 screenshot_size;
u32 screenshot_compressed_size;
u32 offset_to_screenshot;
u32 data_compression_type;

File diff suppressed because it is too large Load Diff

View File

@@ -7,6 +7,8 @@
#include "timing_event.h"
#include "types.h"
#include "util/image.h"
#include "common/timer.h"
#include <memory>
@@ -76,9 +78,7 @@ struct ExtendedSaveStateInfo
std::string media_path;
std::time_t timestamp;
u32 screenshot_width;
u32 screenshot_height;
std::vector<u32> screenshot_data;
RGBA8Image screenshot;
};
namespace System {
@@ -263,23 +263,11 @@ bool BootSystem(SystemBootParameters parameters, Error* error);
void PauseSystem(bool paused);
void ResetSystem();
/// Loads state from the specified filename.
bool LoadState(const char* filename, Error* error);
bool SaveState(const char* filename, Error* error, bool backup_existing_save);
/// Loads state from the specified path.
bool LoadState(const char* path, Error* error, bool save_undo_state);
bool SaveState(const char* path, Error* error, bool backup_existing_save);
bool SaveResumeState(Error* error);
/// Memory save states - only for internal use.
struct MemorySaveState
{
std::unique_ptr<GPUTexture> vram_texture;
std::unique_ptr<GrowableMemoryByteStream> state_stream;
};
bool SaveMemoryState(MemorySaveState* mss);
bool LoadMemoryState(const MemorySaveState& mss);
bool LoadStateFromStream(ByteStream* stream, Error* error, bool update_display, bool ignore_media = false);
bool SaveStateToStream(ByteStream* state, Error* error, u32 screenshot_size = 256, u32 compression_method = 0,
bool ignore_media = false);
/// Runs the VM until the CPU execution is canceled.
void Execute();