Merge pull request #22668 from hrydgard/pause-menu-load

Savestate loads: keep JIT blocks and textures, pause menu waits for loads, review fixes
This commit is contained in:
Henrik Rydgård authored and GitHub committed 2026-10-20 00:15:56 +02:00
commit 8fdadf74bc
36 files changed
+578 -108

No files matched your search

+5
View File
@@ -601,6 +601,11 @@ CChunkFileReader::Error CChunkFileReader::SaveFile(const Path &filename, const s
INFO_LOG(Log::SaveState, "Savestate: Compressed %i bytes into %i", (int)sz, (int)writeLen);
}
free(buffer);
// Buffered writes can still fail here (a full disk.)
if (!pFile.Close() && success) {
ERROR_LOG(Log::SaveState, "ChunkReader: Failed closing file");
success = false;
}
if (!success) {
return ERROR_BAD_FILE;
}
+17
View File
@@ -44,6 +44,8 @@ int CPU_HZ = 222000000;
namespace CoreTiming {
static std::vector<EventType> event_types;
// Far more than ever registered. Counts and ids from a savestate are checked against it.
static constexpr int MAX_EVENT_TYPES = 4096;
// Only used during restore.
static std::set<int> usedEventTypes;
static std::set<int> restoredEventTypes;
@@ -189,6 +191,9 @@ void RestoreRegisterEvent(int &event_type, const char *name, TimedCallback callb
// Some old states have a duplicate restore, do our best to fix...
if (restoredEventTypes.count(event_type) != 0)
event_type = -1;
// Straight from the file, so don't let a bogus id size the table.
if (event_type < -1 || event_type >= MAX_EVENT_TYPES)
event_type = -1;
if (event_type == -1)
event_type = nextEventTypeRestoreId++;
if (event_type >= (int)event_types.size()) {
@@ -542,6 +547,11 @@ void DoState(PointerWrap &p) {
int n = (int)event_types.size();
int current = n;
Do(p, n);
if (n < 0 || n > MAX_EVENT_TYPES) {
ERROR_LOG(Log::SaveState, "Savestate failure: implausible event type count %d", n);
p.SetError(p.ERROR_FAILURE);
return;
}
if (n > current) {
if (p.mode != PointerWrap::MODE_READ) {
WARN_LOG(Log::SaveState, "Savestate failure: more events than current");
@@ -579,6 +589,13 @@ void DoState(PointerWrap &p) {
}
Do(p, CPU_HZ);
if (CPU_HZ <= 0) {
// Everything divides by it.
ERROR_LOG(Log::SaveState, "Savestate failure: bad CPU frequency %d", (int)CPU_HZ);
p.SetError(p.ERROR_FAILURE);
CPU_HZ = 222000000;
return;
}
Do(p, slicelength);
Do(p, globalTimer);
Do(p, idledCycles);
+2 -1
View File
@@ -497,7 +497,8 @@ void HLEDoState(PointerWrap &p) {
delete action;
}
mipsCallActions.clear();
if (actions < 0) {
// Each action takes at least its type id.
if (actions < 0 || (size_t)actions > p.Remaining() / sizeof(int)) {
p.SetError(p.ERROR_FAILURE);
return;
}
+15
View File
@@ -237,6 +237,17 @@ struct GPUStatistics_v0 {
int moreInts[15];
};
// A savestate's display framebuffer, straight from the file. The stride can be anything (the PSP
// takes negative ones, and GE dump playback sets others): presenting checks that.
static void SanitizeLoadedFramebuf(FrameBufferState &fb) {
const bool validAddr = fb.topaddr == 0 || Memory::IsRAMAddress(fb.topaddr) || Memory::IsVRAMAddress(fb.topaddr);
if (!validAddr || fb.fmt < GE_FORMAT_565 || fb.fmt > GE_FORMAT_8888) {
ERROR_LOG(Log::SaveState, "Savestate has an invalid display framebuffer (%08x, format %d), turning the display off", fb.topaddr, (int)fb.fmt);
fb.topaddr = 0;
fb.fmt = GE_FORMAT_8888;
}
}
void __DisplayDoState(PointerWrap &p) {
auto s = p.Section("sceDisplay", 1, 8);
if (!s)
@@ -244,6 +255,10 @@ void __DisplayDoState(PointerWrap &p) {
Do(p, framebuf);
Do(p, latchedFramebuf);
if (p.mode == p.MODE_READ) {
SanitizeLoadedFramebuf(framebuf);
SanitizeLoadedFramebuf(latchedFramebuf);
}
Do(p, framebufIsLatched);
DisplayHWDoState(p, s <= 2);
Do(p, hasSetMode);
+9
View File
@@ -158,8 +158,17 @@ public:
}
void handleResult(PendingInterrupt& pend) override {
// Only from a broken savestate: the interrupt was running, but nothing is pending.
if (ge_pending_cb.empty()) {
ERROR_LOG(Log::sceGe, "GE interrupt finished with nothing pending");
return;
}
GeInterruptData intrdata = ge_pending_cb.front();
ge_pending_cb.pop_front();
if (!gpu->IsValidListId(intrdata.listid)) {
ERROR_LOG(Log::sceGe, "GE interrupt finished for invalid list %d", intrdata.listid);
return;
}
DisplayList* dl = gpu->getList(intrdata.listid);
if (!dl->interruptsEnabled) {
+23 -1
View File
@@ -534,6 +534,26 @@ static void __KernelModuleInit() {
eventModuleLoadDone = CoreTiming::RegisterEvent("LoadModuleDone", &__KernelLoadModuleDone);
}
// Set for the next load by the savestate code: code the load left as it was.
static std::vector<std::pair<u32, u32>> g_unchangedCodeOnLoad;
void __KernelModuleSetUnchangedCodeOnLoad(std::vector<std::pair<u32, u32>> unchanged) {
g_unchangedCodeOnLoad = std::move(unchanged);
}
std::vector<std::pair<u32, u32>> __KernelModuleCodeRanges() {
std::vector<std::pair<u32, u32>> ranges;
u32 error;
for (SceUID moduleId : loadedModules) {
const PSPModule *module = kernelObjects.Get<PSPModule>(moduleId, error);
// textEnd is the last instruction.
if (module && !module->isFake && module->textStart != 0 && module->textEnd >= module->textStart) {
ranges.emplace_back(module->textStart, module->textEnd + 4 - module->textStart);
}
}
return ranges;
}
void __KernelModuleDoState(PointerWrap &p) {
auto s = p.Section("sceKernelModule", 1, 5);
if (!s)
@@ -602,7 +622,9 @@ void __KernelModuleDoState(PointerWrap &p) {
}
// The functions were found in memory from before the load, where other code may have been
// (an overlay module, say.) Hash them from what's there now, so a hook only goes where it matches.
MIPSAnalyst::RehashFunctions();
// Only what the load may have changed: hashing it all again takes milliseconds.
MIPSAnalyst::RehashFunctions(g_unchangedCodeOnLoad);
g_unchangedCodeOnLoad.clear();
if (g_Config.bFuncReplacements) {
MIPSAnalyst::ReplaceFunctions();
}
+5
View File
@@ -229,6 +229,11 @@ public:
KernelObject *__KernelModuleObject();
void __KernelModuleDoState(PointerWrap &p);
// The code of the loaded modules, as (address, size).
std::vector<std::pair<u32, u32>> __KernelModuleCodeRanges();
// For the next savestate load: code it leaves as it was, as (address, size). Its functions aren't
// hashed again.
void __KernelModuleSetUnchangedCodeOnLoad(std::vector<std::pair<u32, u32>> unchanged);
void __KernelModuleShutdown();
class MIPSState;
+22 -2
View File
@@ -44,11 +44,14 @@ IRFrontend::IRFrontend(bool startDefaultPrefix) {
g_breakpoints.ClearSkipFirst();
}
void IRFrontend::DoState(PointerWrap &p) {
bool IRFrontend::DoState(PointerWrap &p) {
auto s = p.Section("Jit", 1, 2);
if (!s)
return;
return false;
const bool oldStartDefaultPrefix = js.startDefaultPrefix;
const bool oldHasSetRounding = js.hasSetRounding;
const u8 oldLastSetRounding = js.lastSetRounding;
Do(p, js.startDefaultPrefix);
if (s >= 2) {
Do(p, js.hasSetRounding);
@@ -62,6 +65,23 @@ void IRFrontend::DoState(PointerWrap &p) {
// The debugger sets this so that "go" on a breakpoint will actually... go.
// But if they reset, we can end up hitting it by mistake, since it's based on PC and ticks.
g_breakpoints.ClearSkipFirst();
if (p.mode != PointerWrap::MODE_READ) {
return false;
}
// Blocks only switch rounding modes if hasSetRounding was set when they were compiled, and
// assume default prefixes at their start if startDefaultPrefix was.
if ((js.hasSetRounding && !oldHasSetRounding) || (!js.startDefaultPrefix && oldStartDefaultPrefix)) {
return true;
}
// The other way round, the blocks already compiled just do more than they need to. Keep the
// flags that way, or a loaded hasSetRounding of false would have UpdateFCR31() throw them all
// away for a rounding mode they handle fine. lastSetRounding says they were compiled with the
// rounding checks, so the next compile doesn't throw them away to add them either.
js.hasSetRounding = js.hasSetRounding || oldHasSetRounding;
js.startDefaultPrefix = js.startDefaultPrefix && oldStartDefaultPrefix;
js.lastSetRounding = oldLastSetRounding;
return false;
}
bool IRFrontend::UpdateFCR31(u32 fcr31) {
+2 -1
View File
@@ -85,7 +85,8 @@ public:
void Comp_Vbfy(MIPSOpcode op) override;
int Replace_fabsf() override;
void DoState(PointerWrap &p);
// Returns true if a loaded state means blocks compiled before it can't run any more.
bool DoState(PointerWrap &p);
bool UpdateFCR31(u32 fcr31); // returns true if the block cache needs clearing
bool CheckRounding(u32 blockAddress); // returns true if we need a do-over
+48 -9
View File
@@ -99,7 +99,9 @@ IRJit::~IRJit() {
}
void IRJit::DoState(PointerWrap &p) {
frontend_.DoState(p);
if (frontend_.DoState(p)) {
ClearCache();
}
}
void IRJit::UpdateFCR31() {
@@ -173,18 +175,55 @@ void IRJit::InvalidateCacheAt(u32 em_address, int length) {
invalidatedBlocks_ += (int)numbers.size();
for (int block_num : numbers) {
auto block = blocks_.GetBlock(block_num);
// TODO: We are invalidating a lot of blocks that are already invalid (yu gi oh).
// INFO_LOG(Log::JIT, "Block at %08x invalidated: valid: %d", block->GetOriginalStart(), block->IsValid());
// If we're a native JIT (IR->JIT, not just IR interpreter), we write native offsets into the blocks.
int cookie = compileToNative_ ? block->GetNativeOffset() : block->GetIRArenaOffset();
// Before Destroy, which forgets the block's address. Blocks linked to it jump straight in.
InvalidateNativeBlock(&blocks_, block_num);
blocks_.RemoveBlockFromPageLookup(block_num);
block->Destroy(cookie);
InvalidateBlock(block_num);
}
}
void IRJit::InvalidateBlock(int block_num) {
IRBlock *block = blocks_.GetBlock(block_num);
// If we're a native JIT (IR->JIT, not just IR interpreter), we write native offsets into the blocks.
int cookie = compileToNative_ ? block->GetNativeOffset() : block->GetIRArenaOffset();
// Before Destroy, which forgets the block's address. Blocks linked to it jump straight in.
InvalidateNativeBlock(&blocks_, block_num);
blocks_.RemoveBlockFromPageLookup(block_num);
block->Destroy(cookie);
}
void IRJit::InvalidateCacheExcept(const std::vector<std::pair<u32, u32>> &keep) {
if (keep.empty()) {
ClearCache();
return;
}
int kept = 0;
int invalidated = 0;
for (int block_num = 0; block_num < blocks_.GetNumBlocks(); ++block_num) {
const IRBlock *block = blocks_.GetBlock(block_num);
if (!block->IsValid()) {
continue;
}
u32 start, size;
block->GetRange(&start, &size);
start &= 0x3FFFFFFF;
bool inside = false;
for (const auto &[keepStart, keepSize] : keep) {
const u32 maskedStart = keepStart & 0x3FFFFFFF;
if (start >= maskedStart && start + size <= maskedStart + keepSize) {
inside = true;
break;
}
}
if (inside) {
kept++;
} else {
InvalidateBlock(block_num);
invalidated++;
}
}
INFO_LOG(Log::JIT, "IRJit: Kept %d blocks, invalidated %d", kept, invalidated);
}
void IRJit::Compile(u32 em_address) {
_dbg_assert_(compilerEnabled_);
+2
View File
@@ -209,6 +209,7 @@ public:
void ClearCache() override;
void InvalidateCacheAt(u32 em_address, int length = 4) override;
void InvalidateCacheExcept(const std::vector<std::pair<u32, u32>> &keep) override;
void UpdateFCR31() override;
void GetStats(JitStats *stats) const override;
@@ -238,6 +239,7 @@ protected:
virtual bool CompileNativeBlock(IRBlockCache *irBlockCache, int block_num) { return true; }
virtual void FinalizeNativeBlock(IRBlockCache *irBlockCache, int block_num) {}
virtual void InvalidateNativeBlock(IRBlockCache *irBlockCache, int block_num) {}
void InvalidateBlock(int block_num);
bool compileToNative_;
+2
View File
@@ -148,6 +148,8 @@ namespace MIPSComp {
virtual const u8 *GetCrashHandler() const = 0;
virtual JitBlockCacheDebugInterface *GetBlockCacheDebugInterface() = 0;
virtual void InvalidateCacheAt(u32 em_address, int length = 4) = 0;
// Invalidates every block not entirely inside one of the (address, size) ranges.
virtual void InvalidateCacheExcept(const std::vector<std::pair<u32, u32>> &keep) = 0;
virtual void DoState(PointerWrap &p) = 0;
virtual void RunLoopUntil(u64 globalticks) = 0;
virtual void Compile(u32 em_address) = 0;
+23 -12
View File
@@ -326,6 +326,18 @@ void MIPSState::Reset() {
}
void MIPSState::Init() {
InitRegisters();
if (PSP_CoreParameter().cpuCore == CPUCore::JIT || PSP_CoreParameter().cpuCore == CPUCore::JIT_IR) {
MIPSComp::jit = MIPSComp::CreateNativeJit(this);
} else if (PSP_CoreParameter().cpuCore == CPUCore::IR_INTERPRETER) {
MIPSComp::jit = new MIPSComp::IRJit(this, false);
} else {
MIPSComp::jit = nullptr;
}
UpdateBlockShadowHook();
}
void MIPSState::InitRegisters() {
memset(r, 0, sizeof(r));
memset(f, 0, sizeof(f));
memset(v, 0, sizeof(v));
@@ -359,15 +371,6 @@ void MIPSState::Init() {
downcount = 0;
memset(vcmpResult, 0, sizeof(vcmpResult));
if (PSP_CoreParameter().cpuCore == CPUCore::JIT || PSP_CoreParameter().cpuCore == CPUCore::JIT_IR) {
MIPSComp::jit = MIPSComp::CreateNativeJit(this);
} else if (PSP_CoreParameter().cpuCore == CPUCore::IR_INTERPRETER) {
MIPSComp::jit = new MIPSComp::IRJit(this, false);
} else {
MIPSComp::jit = nullptr;
}
UpdateBlockShadowHook();
}
const float vfpuPrefixConstants[8] = { 0.f, 1.f, 2.f, 0.5f, 3.f, 1.f / 3.f, 0.25f, 1.f / 6.f };
@@ -427,9 +430,17 @@ void MIPSState::DoState(PointerWrap &p) {
if (!s)
return;
// Reset the jit if we're loading.
if (p.mode == p.MODE_READ)
Reset();
if (p.mode == p.MODE_READ) {
if (MIPSComp::jit && !keepJitBlocks_.empty()) {
// Blocks in code the load didn't change can stay. Queued invalidations stay too.
InitRegisters();
MIPSComp::jit->InvalidateCacheExcept(keepJitBlocks_);
} else {
// Start the JIT over.
Reset();
}
keepJitBlocks_.clear();
}
// Assume we're not saving state during a CPU core reset, so no lock.
if (MIPSComp::jit)
MIPSComp::jit->DoState(p);
+9
View File
@@ -22,6 +22,7 @@
#include <cstddef>
#include <cstring>
#include <type_traits>
#include <utility>
#include <vector>
#include "Common/CommonTypes.h"
@@ -180,11 +181,18 @@ public:
~MIPSState();
void Init();
// Init without the JIT.
void InitRegisters();
void Shutdown();
void Reset();
void UpdateCore(CPUCore desired);
void DoState(PointerWrap &p);
// For the next DoState load: keep the JIT blocks entirely inside these (address, size) ranges,
// code the load leaves as it is, instead of starting the JIT over.
void KeepJitBlocksOnNextLoad(std::vector<std::pair<u32, u32>> ranges) {
keepJitBlocks_ = std::move(ranges);
}
// MUST start with r and be followed by f, v, and t!
u32 r[32];
@@ -295,6 +303,7 @@ public:
// Doesn't need save stating.
std::vector<PendingCacheOperation> pendingInvalidates_;
bool invalidateAll_ = false;
std::vector<std::pair<u32, u32>> keepJitBlocks_;
};
// Access control ('private') break standard layout. Let's not allow it, to avoid excessive
+24 -3
View File
@@ -909,7 +909,22 @@ namespace MIPSAnalyst {
return DetermineRegisterUsage(reg, addr, instrs) == USAGE_CLOBBERED;
}
void HashFunctions() {
// Ranges are (address, size), sorted by address and not overlapping.
static bool InsideRanges(const std::vector<std::pair<u32, u32>> &ranges, u32 start, u32 end) {
start &= 0x3FFFFFFF;
end &= 0x3FFFFFFF;
auto iter = std::upper_bound(ranges.begin(), ranges.end(), start, [](u32 address, const std::pair<u32, u32> &range) {
return address < (range.first & 0x3FFFFFFF);
});
if (iter == ranges.begin()) {
return false;
}
--iter;
const u32 rangeStart = iter->first & 0x3FFFFFFF;
return start >= rangeStart && end <= rangeStart + iter->second;
}
static void HashFunctions(const std::vector<std::pair<u32, u32>> &unchanged = {}) {
std::lock_guard<std::recursive_mutex> guard(functions_lock);
std::vector<u32> buffer;
@@ -918,6 +933,10 @@ namespace MIPSAnalyst {
if (!Memory::IsValidRange(f.start, f.end - f.start + 4)) {
continue;
}
// end is the last instruction.
if (f.hasHash && InsideRanges(unchanged, f.start, f.end + 4)) {
continue;
}
// Immediates are masked out, so relocations don't change the hash.
buffer.resize((f.end - f.start + 4) / 4);
@@ -1311,9 +1330,11 @@ namespace MIPSAnalyst {
return false;
}
void RehashFunctions() {
void RehashFunctions(const std::vector<std::pair<u32, u32>> &unchanged) {
std::vector<std::pair<u32, u32>> sorted = unchanged;
std::sort(sorted.begin(), sorted.end());
std::lock_guard<std::recursive_mutex> guard(functions_lock);
HashFunctions();
HashFunctions(sorted);
UpdateHashToFunctionMap();
}
+4 -1
View File
@@ -18,6 +18,7 @@
#pragma once
#include <string>
#include <utility>
#include <vector>
#include "Common/CommonTypes.h"
@@ -118,7 +119,9 @@ namespace MIPSAnalyst {
const char *LookupHash(u64 hash, u32 funcSize);
// Hashes the known functions again, from what memory holds now (after a savestate load.)
void RehashFunctions();
// Functions that already have a hash and lie entirely in one of the (address, size) ranges in
// unchanged keep it: their code is what it was hashed from.
void RehashFunctions(const std::vector<std::pair<u32, u32>> &unchanged = {});
void ReplaceFunctions();
void UpdateHashMap();
+68 -4
View File
@@ -449,7 +449,60 @@ bool Reinit() {
return success;
}
static void DoMemoryVoid(PointerWrap &p, uint32_t start, uint32_t size) {
// Loads size bytes at start, comparing the parts of it in compare->ranges with what's there as it
// goes. A chunk that already matches doesn't need copying, and the matching parts are reported in
// compare->unchanged. The rest is a plain copy.
static void CopyComparing(uint8_t *dest, const uint8_t *src, u32 start, u32 size, RamCompare *compare) {
// In the size of the JIT's block lookup pages. One changed byte costs its chunk's blocks.
constexpr u32 CHUNK_SIZE = 1024;
// As offsets into the copy, in order, merged.
std::vector<std::pair<u32, u32>> ranges;
for (const auto &[address, length] : compare->ranges) {
const u32 rangeStart = std::max(address & 0x3FFFFFFF, start);
const u32 rangeEnd = std::min((address & 0x3FFFFFFF) + length, start + size);
if (rangeStart < rangeEnd) {
ranges.emplace_back(rangeStart - start, rangeEnd - start);
}
}
std::sort(ranges.begin(), ranges.end());
u32 pos = 0;
for (const auto &[rangeStart, rangeEnd] : ranges) {
const u32 from = std::max(rangeStart, pos);
if (from >= rangeEnd) {
continue;
}
if (from > pos) {
ParallelMemcpy(&g_threadManager, dest + pos, src + pos, from - pos);
}
u32 runStart = 0;
bool inRun = false;
for (u32 chunk = from; chunk < rangeEnd; chunk += CHUNK_SIZE) {
const u32 chunkSize = std::min(CHUNK_SIZE, rangeEnd - chunk);
const bool same = memcmp(dest + chunk, src + chunk, chunkSize) == 0;
if (same && !inRun) {
runStart = chunk;
inRun = true;
} else if (!same) {
memcpy(dest + chunk, src + chunk, chunkSize);
if (inRun) {
compare->unchanged.emplace_back(start + runStart, chunk - runStart);
inRun = false;
}
}
}
if (inRun) {
compare->unchanged.emplace_back(start + runStart, rangeEnd - runStart);
}
pos = rangeEnd;
}
if (pos < size) {
ParallelMemcpy(&g_threadManager, dest + pos, src + pos, size - pos);
}
}
static void DoMemoryVoid(PointerWrap &p, uint32_t start, uint32_t size, RamCompare *compare = nullptr) {
uint8_t *d = GetPointerWriteOrException(start);
uint8_t *&storage = *p.ptr;
@@ -463,7 +516,11 @@ static void DoMemoryVoid(PointerWrap &p, uint32_t start, uint32_t size) {
switch (p.mode) {
case PointerWrap::MODE_READ:
ParallelMemcpy(&g_threadManager, d, storage, size);
if (compare && !compare->ranges.empty()) {
CopyComparing(d, storage, start, size, compare);
} else {
ParallelMemcpy(&g_threadManager, d, storage, size);
}
break;
case PointerWrap::MODE_WRITE:
ParallelMemcpy(&g_threadManager, storage, d, size);
@@ -483,11 +540,13 @@ static void DoMemoryVoid(PointerWrap &p, uint32_t start, uint32_t size) {
storage += size;
}
void DoState(PointerWrap &p) {
void DoState(PointerWrap &p, RamCompare *compare) {
auto s = p.Section("Memory", 1, 3);
if (!s)
return;
const u32 memorySizeBefore = g_MemorySize;
if (s < 2) {
if (!g_RemasterMode)
g_MemorySize = RAM_NORMAL_SIZE;
@@ -529,7 +588,12 @@ void DoState(PointerWrap &p) {
}
}
DoMemoryVoid(p, PSP_GetKernelMemoryBase(), g_MemorySize);
// A new size means Reinit(): RAM is newly mapped, nothing in it to compare with, and the JIT has
// to start over (its dispatcher points at the old mapping.)
if (g_MemorySize != memorySizeBefore) {
compare = nullptr;
}
DoMemoryVoid(p, PSP_GetKernelMemoryBase(), g_MemorySize, compare);
p.DoMarker("RAM");
DoMemoryVoid(p, PSP_GetVidMemBase(), VRAM_SIZE);
+12 -1
View File
@@ -21,6 +21,8 @@
#include <cstring>
#include <cstdint>
#include <utility>
#include <vector>
#ifndef offsetof
#include <stddef.h>
#endif
@@ -169,10 +171,19 @@ enum class MemMapSetupFlags {
};
ENUM_CLASS_BITOPS(MemMapSetupFlags);
// For a savestate load: parts of RAM to compare with what's loaded over them, as they're loaded.
// Addresses can be in any mirror. The JIT keeps its blocks in code the load doesn't change.
struct RamCompare {
// In: (address, size) ranges.
std::vector<std::pair<u32, u32>> ranges;
// Out: the parts of them the load left as they were.
std::vector<std::pair<u32, u32>> unchanged;
};
// Init and Shutdown
bool Init(MemMapSetupFlags flags);
void Shutdown();
void DoState(PointerWrap &p);
void DoState(PointerWrap &p, RamCompare *compare = nullptr);
// False when shutdown has already been called.
bool IsActive();
+118 -39
View File
@@ -53,6 +53,7 @@
#include "Core/HLE/ReplaceTables.h"
#include "Core/HLE/sceDisplay.h"
#include "Core/HLE/sceKernel.h"
#include "Core/HLE/sceKernelModule.h"
#include "Core/HLE/sceUtility.h"
#include "Core/HLE/sceSas.h"
#include "Core/HLE/sceIo.h"
@@ -60,6 +61,7 @@
#include "GPU/GPU.h"
#include "GPU/GPUCommon.h"
#include "Core/MIPS/JitCommon/JitBlockCache.h"
#include "Core/MIPS/JitCommon/JitCommon.h"
#include "Core/RetroAchievements.h"
#include "HW/MemoryStick.h"
@@ -106,12 +108,6 @@ static std::map<std::string, uint64_t, std::less<>> g_files; // the value is th
// Set when a slot save finishes. That's on the emu thread, so the rescan is left to the next reader.
static std::atomic<bool> g_filesStale;
struct SaveStart {
void DoState(PointerWrap &p);
// Rewind snapshots and verification don't count towards IsStale().
bool countsAsSave = true;
};
enum class OperationType {
Save,
Load,
@@ -131,6 +127,15 @@ struct PendingRead {
std::string savedBy;
};
// A save from when it's asked for until its file is written.
struct SaveRecord {
Path path;
// How many loads had been applied when its snapshot was taken, or -1 before that.
int loadCount = -1;
// Callbacks of repeats of it (a save key pressed twice). They hear its result, with no message.
std::vector<Callback> repeats;
};
struct Operation {
// The slot number is for visual purposes only. Set to -1 for operations where we don't display a message for example.
Operation(OperationType t, const Path &f, int slot_, Callback cb)
@@ -147,15 +152,18 @@ struct Operation {
// worker part is done.
bool started = false;
std::shared_ptr<PendingRead> read;
// For a save. Its fields are under mutex.
std::shared_ptr<SaveRecord> save;
};
// Front to back, each operation's emu thread part runs in turn. A load or rewind that's still
// waiting for the worker holds up the operations behind it.
static std::deque<Operation> g_pendingOperations;
// Saves asked for and not yet written, by path, with the callbacks of any repeats of them (a save
// key pressed again before the first save finished). Under mutex.
static std::map<std::string, std::vector<Callback>> g_pendingSaves;
// Saves asked for and not yet written, oldest first. Under mutex.
static std::vector<std::shared_ptr<SaveRecord>> g_pendingSaves;
// Loads (and rewinds) applied, so a save can tell whether one came after its snapshot.
static std::atomic<int> g_loadCount;
// Save snapshots in memory, waiting to be compressed and written. Each is a whole state.
static std::atomic<int> g_saveSnapshots;
constexpr int MAX_SAVE_SNAPSHOTS = 2;
@@ -233,13 +241,20 @@ int g_screenshotFailures;
// The state may hold different code. KernelModuleDoState hooks the functions again.
std::vector<std::pair<u32, u32>> hookedOps;
// The loaded modules' code is compared with the state's as it's loaded. Where it's the same,
// the JIT keeps its blocks, so they don't all have to be compiled again.
Memory::RamCompare codeCompare;
if (p.mode == p.MODE_READ) {
g_loadCount++;
hookedOps = Replacement_GetHookedOps();
RestoreReplacedInstructions(0, 0xFFFFFFFF);
codeCompare.ranges = __KernelModuleCodeRanges();
}
Memory::DoState(p);
Memory::DoState(p, &codeCompare);
if (p.mode == p.MODE_READ) {
Replacement_RepairStaleHookOps(hookedOps);
__KernelModuleSetUnchangedCodeOnLoad(codeCompare.unchanged);
currentMIPS->KeepJitBlocksOnNextLoad(std::move(codeCompare.unchanged));
}
if (s >= 3) {
@@ -253,6 +268,12 @@ int g_screenshotFailures;
Achievements::DoState(p);
// Kernel object destructors might close open files, so do the filesystem last.
pspFileSystem.DoState(p);
if (p.mode == p.MODE_READ && p.error == PointerWrap::ERROR_FAILURE) {
// RAM may be loaded and the JIT not told, or the other way round. Keep nothing.
currentMIPS->KeepJitBlocksOnNextLoad({});
currentMIPS->ClearJitCacheDeferred();
}
}
static void WorkerLoop() {
@@ -340,39 +361,52 @@ int g_screenshotFailures;
static bool IsSavePending(const Path &path) {
std::lock_guard<std::mutex> guard(mutex);
return g_pendingSaves.find(path.ToString()) != g_pendingSaves.end();
for (const auto &save : g_pendingSaves) {
if (save->path == path) {
return true;
}
}
return false;
}
// If a save to path is already pending, this is a repeat of it (a key pressed twice): the callback
// hears that save's result, with no message so it's only shown once. Returns true if so.
// If a save to path hasn't finished yet, this is a repeat of it (a key pressed twice): the
// callback hears that save's result, with no message so it's only shown once. Returns true if so.
// Unless a load was applied after that save's snapshot: then this saves something else.
static bool AttachToPendingSave(const Path &path, const Callback &callback) {
std::lock_guard<std::mutex> guard(mutex);
auto iter = g_pendingSaves.find(path.ToString());
if (iter == g_pendingSaves.end()) {
return false;
// One queued and not yet applied comes before this save too.
for (const Operation &op : g_pendingOperations) {
if (op.type == OperationType::Load || op.type == OperationType::Rewind) {
return false;
}
}
if (callback) {
iter->second.push_back(callback);
for (auto iter = g_pendingSaves.rbegin(); iter != g_pendingSaves.rend(); ++iter) {
SaveRecord &save = **iter;
if (!(save.path == path)) {
continue;
}
if (save.loadCount != -1 && save.loadCount != g_loadCount) {
return false;
}
if (callback) {
save.repeats.push_back(callback);
}
return true;
}
return true;
return false;
}
// Takes a save out of g_pendingSaves, returning the callbacks of its repeats.
static std::vector<Callback> TakePendingSave(const Path &path) {
// A save is done (written, failed or refused). Returns the callbacks of its repeats.
static std::vector<Callback> FinishPendingSave(const std::shared_ptr<SaveRecord> &save) {
std::lock_guard<std::mutex> guard(mutex);
std::vector<Callback> repeats;
auto iter = g_pendingSaves.find(path.ToString());
if (iter != g_pendingSaves.end()) {
repeats = std::move(iter->second);
g_pendingSaves.erase(iter);
}
return repeats;
g_pendingSaves.erase(std::remove(g_pendingSaves.begin(), g_pendingSaves.end(), save), g_pendingSaves.end());
return std::move(save->repeats);
}
static void RefuseOperation(const Operation &op) {
CallbackRefused(op.callback);
if (op.type == OperationType::Save) {
for (const Callback &repeat : TakePendingSave(op.path)) {
if (op.type == OperationType::Save && op.save) {
for (const Callback &repeat : FinishPendingSave(op.save)) {
CallbackRefused(repeat);
}
}
@@ -394,10 +428,13 @@ int g_screenshotFailures;
}
std::lock_guard<std::mutex> guard(mutex);
if (op.type == OperationType::Save) {
g_pendingSaves.emplace(op.path.ToString(), std::vector<Callback>());
}
g_pendingOperations.push_back(op);
if (op.type == OperationType::Save) {
Operation &queued = g_pendingOperations.back();
queued.save = std::make_shared<SaveRecord>();
queued.save->path = op.path;
g_pendingSaves.push_back(queued.save);
}
// Don't actually run it until next frame.
needsProcess = true;
@@ -406,6 +443,7 @@ int g_screenshotFailures;
static bool QueueLoad(const Operation &op) {
if (NetworkWarnUserIfOnlineAndCantSavestate()) {
CallbackRefused(op.callback);
return false;
}
@@ -417,6 +455,7 @@ int g_screenshotFailures;
static bool QueueSave(const Operation &op) {
if (NetworkWarnUserIfOnlineAndCantSavestate()) {
CallbackRefused(op.callback);
return false;
}
if (AttachToPendingSave(op.path, op.callback)) {
@@ -638,6 +677,7 @@ int g_screenshotFailures;
bool UndoLoad(std::string_view gamePrefix, Callback callback) {
if (NetworkWarnUserIfOnlineAndCantSavestate()) {
CallbackRefused(callback);
return false;
}
@@ -1054,6 +1094,22 @@ int g_screenshotFailures;
#endif
}
// Freeze-frame reloads its snapshot every frame, which would undo any other load right away. So a
// load while frozen becomes the new frozen state (freezeNext snapshots it), and is a full load:
// the GPU only skips its resets for freeze-frame's own reloads, which it tells by frozen.
struct FrozenLoadScope {
const bool frozen = PSP_CoreParameter().frozen;
FrozenLoadScope() {
PSP_CoreParameter().frozen = false;
}
~FrozenLoadScope() {
if (frozen) {
PSP_CoreParameter().frozen = true;
PSP_CoreParameter().freezeNext = true;
}
}
};
// What a save's worker job needs, and its completion after.
struct SaveJob {
Path path;
@@ -1065,6 +1121,7 @@ int g_screenshotFailures;
bool keepUndo = false;
Path undoPath;
Callback callback;
std::shared_ptr<SaveRecord> save;
};
// Emu thread, once the file is written (or failed to be).
@@ -1092,7 +1149,7 @@ int g_screenshotFailures;
status = Status::FAILURE;
}
std::vector<Callback> repeats = TakePendingSave(job.path);
std::vector<Callback> repeats = FinishPendingSave(job.save);
if (!callCallbacks) {
return;
}
@@ -1117,6 +1174,7 @@ int g_screenshotFailures;
job.slotGamePrefix = op.slotGamePrefix;
job.slot = op.slot;
job.callback = op.callback;
job.save = op.save;
if (!op.slotGamePrefix.empty()) {
job.keepUndo = g_Config.bEnableStateUndo;
job.undoPath = GenerateSaveSlotPath(op.slotGamePrefix, op.slot, UNDO_STATE_EXTENSION);
@@ -1151,7 +1209,10 @@ int g_screenshotFailures;
} else {
DeleteIfExists(job.path);
}
File::Rename(job.writePath, job.path);
if (!File::Rename(job.writePath, job.path)) {
ERROR_LOG(Log::SaveState, "Failed to move '%s' into place", job.writePath.c_str());
result = CChunkFileReader::ERROR_BAD_FILE;
}
}
PostCompletion([job, result](bool callCallbacks) {
FinishSave(job, result, callCallbacks);
@@ -1185,7 +1246,10 @@ int g_screenshotFailures;
g_saveSnapshots--;
if (result == CChunkFileReader::ERROR_NONE) {
DeleteIfExists(backup);
File::Rename(temp, backup);
if (!File::Rename(temp, backup)) {
// The undo would load whatever was there before, maybe another game's state.
result = CChunkFileReader::ERROR_BAD_FILE;
}
}
// Not a callback, so it runs at shutdown too.
PostCompletion([result, gamePrefix](bool) {
@@ -1246,7 +1310,11 @@ int g_screenshotFailures;
saveStateInitialGitVersion = read.gitVersion;
SaveStart state;
std::string errorString;
CChunkFileReader::Error result = CChunkFileReader::LoadPtr(read.buffer.get(), read.size, state, &errorString);
CChunkFileReader::Error result;
{
FrozenLoadScope frozenScope;
result = CChunkFileReader::LoadPtr(read.buffer.get(), read.size, state, &errorString);
}
read.buffer.reset();
if (result == CChunkFileReader::ERROR_NONE) {
@@ -1300,6 +1368,8 @@ int g_screenshotFailures;
std::string message;
std::string metadata;
std::string errorString;
// Covers HandleLoadFailure()'s retries below too.
FrozenLoadScope frozenScope;
CChunkFileReader::Error result = rewindStates.FinishRestore(&errorString, &metadata);
if (result == CChunkFileReader::ERROR_NONE) {
message = sc->T("Loaded State");
@@ -1376,8 +1446,10 @@ int g_screenshotFailures;
}
// NOTE: This can cause ending of the current renderpass, due to the readback needed for the screenshot.
void Process() {
rewindStates.Process();
void Process(bool takeRewindSnapshots) {
if (takeRewindSnapshots) {
rewindStates.Process();
}
RunCompletions(true);
if (!needsProcess)
@@ -1396,6 +1468,9 @@ int g_screenshotFailures;
needsProcess = false;
lock.unlock();
for (const Operation &op : operations) {
if (op.type == OperationType::Rewind && op.started) {
rewindStates.CancelRestore();
}
RefuseOperation(op);
}
return;
@@ -1406,6 +1481,10 @@ int g_screenshotFailures;
// Enqueue() only appends, which leaves this reference alone. Unlocked, since callbacks can
// queue more.
Operation &op = g_pendingOperations.front();
if (op.save) {
// Its snapshot is next: nothing can be loaded in between, it's first in the queue.
op.save->loadCount = g_loadCount;
}
lock.unlock();
const bool finished = RunOperation(op);
lock.lock();
+12 -3
View File
@@ -36,6 +36,13 @@ namespace SaveState {
};
typedef std::function<void(Status status, std::string_view message, std::string_view metadata)> Callback;
// The whole state, for CChunkFileReader. libretro uses it directly.
struct SaveStart {
void DoState(PointerWrap &p);
// Rewind snapshots and verification don't count towards IsStale().
bool countsAsSave = true;
};
static const char * const SCREENSHOT_EXTENSION = "jpg";
void Init();
@@ -102,11 +109,11 @@ namespace SaveState {
CChunkFileReader::Error LoadFromRam(std::vector<u8> &state, std::string *errorString);
// For testing / automated tests. Runs a save state verification pass (async.)
// Warning: callback will be called on a different thread.
// The callback runs from Process().
bool Verify(Callback callback = Callback());
// To go back to a previous snapshot (only if enabled.)
// Warning: callback will be called on a different thread.
// The callback runs from Process(), once the snapshot has been applied.
bool Rewind(Callback callback = Callback());
// Returns true if there are rewind snapshots available.
@@ -126,7 +133,9 @@ namespace SaveState {
bool IsOldVersion();
// Check if there's any save stating needing to be done. Normally called once per frame.
void Process();
// While the game is paused (under the pause menu, say), pass false: there's nothing new to take
// rewind snapshots of.
void Process(bool takeRewindSnapshots = true);
// Notify save state code that new save data has been written.
void NotifySaveData();
+10
View File
@@ -161,6 +161,16 @@ CChunkFileReader::Error StateRingbuffer::Restore(std::string *errorString, std::
return FinishRestore(errorString, metadata);
}
void StateRingbuffer::CancelRestore() {
if (!restoreActive_) {
return;
}
// The decode may still be running into restoreBuffer_.
WaitForWorker();
restoreActive_ = false;
std::vector<u8>().swap(restoreBuffer_);
}
// Only moves the ring back: the state's buffer may still be in use by a worker job, and is reused
// by the next snapshot anyway.
void StateRingbuffer::DropNewest() {
+2
View File
@@ -31,6 +31,8 @@ public:
CChunkFileReader::Error FinishRestore(std::string *errorString, std::string *metadata);
// Both halves, waiting for the worker in between.
CChunkFileReader::Error Restore(std::string *errorString, std::string *metadata);
// For a rewind that was started and then dropped.
void CancelRestore();
void Clear();
bool Empty() const {
+15 -2
View File
@@ -1479,6 +1479,16 @@ void FramebufferManagerCommon::CopyFramebufferForColorTexture(VirtualFramebuffer
}
}
// Whether the 480x272 the display shows from addr is all in memory. The PSP takes strides that
// aren't: negative ones (rows going up, which this doesn't handle), and larger than VRAM has room for.
static bool IsValidDisplayRange(u32 addr, int stride, GEBufferFormat format) {
if (stride <= 0) {
return false;
}
const u32 bpp = BufferFormatBytesPerPixel(format);
return Memory::IsValidRange(addr, ((272 - 1) * (u32)stride + 480) * bpp);
}
Draw::Texture *FramebufferManagerCommon::MakePixelTexture(const u8 *srcPixels, GEBufferFormat srcPixelFormat, int srcStride, int width, int height) {
Draw::DataFormat depthFormat = Draw::DataFormat::UNDEFINED;
@@ -1787,7 +1797,7 @@ void FramebufferManagerCommon::PrepareCopyDisplayToOutput(const DisplayLayoutCon
VirtualFramebuffer *vfb = FindDisplayVFB(fbaddr, &offsetX, &offsetY);
if (!vfb) {
if (Memory::IsValidAddress(fbaddr)) {
if (IsValidDisplayRange(fbaddr, displayStride_, displayFormat_)) {
// The game is displaying something directly from RAM. In GTA, it's decoded video.
// If successful, this effectively calls presentation_->NotifyPresent();
if (!DrawFramebufferToOutput(config, Memory::GetPointerUnchecked(fbaddr), displayStride_, displayFormat_)) {
@@ -3018,7 +3028,7 @@ void FramebufferManagerCommon::NotifyBlockTransferAfter(u32 dstBasePtr, int dstS
if (!useBufferedRendering_ && dstStride >= 480 && width >= 480 && height == 272) {
bool isPrevDisplayBuffer = PrevDisplayFramebufAddr() == dstBasePtr;
bool isDisplayBuffer = CurrentDisplayFramebufAddr() == dstBasePtr;
if (isPrevDisplayBuffer || isDisplayBuffer) {
if ((isPrevDisplayBuffer || isDisplayBuffer) && IsValidDisplayRange(dstBasePtr, dstStride, displayFormat_)) {
FlushBeforeCopy();
// HACK
DrawFramebufferToOutput(displayLayoutConfigCopy_, Memory::GetPointerUnchecked(dstBasePtr), dstStride, displayFormat_);
@@ -3122,6 +3132,9 @@ void FramebufferManagerCommon::NotifyConfigChanged() {
void FramebufferManagerCommon::DestroyAllFBOs() {
DiscardFramebufferCopy();
currentRenderVfb_ = nullptr;
// Or a new framebuffer at the same address would skip its depth shift.
depthShiftedFor_ = 0;
lastRenderAddress_ = 0;
displayFramebuf_ = nullptr;
prevDisplayFramebuf_ = nullptr;
prevPrevDisplayFramebuf_ = nullptr;
+14 -1
View File
@@ -2773,6 +2773,10 @@ void TextureCacheCommon::Clear(bool delete_them) {
secondCache_.clear();
}
ReleaseDynamicClut();
}
void TextureCacheCommon::ReleaseDynamicClut() {
if (dynamicClutFbo_) {
dynamicClutFbo_->Release();
dynamicClutFbo_ = nullptr;
@@ -2781,10 +2785,19 @@ void TextureCacheCommon::Clear(bool delete_them) {
dynamicClutTemp_->Release();
dynamicClutTemp_ = nullptr;
}
// The dynamic CLUT lived in the framebuffers released above. Fall back to the RAM copy until the next LoadClut.
// The dynamic CLUT lived in framebuffers that are going away. Fall back to the RAM copy until the next LoadClut.
clutRenderAddress_ = 0xFFFFFFFF;
}
void TextureCacheCommon::RecheckAllAfterLoad() {
// Decoding them all again stutters, while hashing finds the ones that are still the same. The
// secondary cache is keyed by hash, so it's fine as it is.
for (auto &iter : cache_) {
iter.second->status |= TexStatus::HASH_RECHECK;
}
ReleaseDynamicClut();
}
// One type of texture update can happen without the involvement of the CPU: Block transfers.
// This is done for example for the text rendering in Gran Turismo, where it only uses a single letter
// worth of texture memory, and for each letter drawn, it just replaces it and then draws.
+4
View File
@@ -367,6 +367,9 @@ public:
virtual void ForgetLastTexture() = 0;
virtual void Clear(bool delete_them);
// After a savestate load, which may have changed any texture's memory (and destroyed the
// framebuffers). Textures are kept, but hashed again before they're used.
void RecheckAllAfterLoad();
virtual void NotifyConfigChanged();
// FramebufferManager keeps TextureCache updated about what regions of memory are being rendered to,
@@ -502,6 +505,7 @@ protected:
CLUTProperties clutProperties_;
// Facilities for GPU depal of static textures.
void ReleaseDynamicClut();
Draw::Framebuffer *dynamicClutTemp_ = nullptr;
Draw::Framebuffer *dynamicClutFbo_ = nullptr;
+16
View File
@@ -695,6 +695,22 @@ bool Recorder::RecordNextFrame(const std::function<void(const Path &)> callback)
return true;
}
void Recorder::RestartAfterLoad() {
if (!active) {
return;
}
NOTICE_LOG(Log::System, "Savestate loaded, restarting the recording");
commands.clear();
pushbuf.clear();
lastVRAM.clear();
lastEdramTrans = 0x400;
flipLastAction = gpuStats.totals.numFlips;
flipFinishAt = -1;
std::lock_guard<std::mutex> guard(callbackLock_);
active = false;
nextFrame = true;
}
void Recorder::FinishRecording() {
// We're done - this was just to write the result out.
if (!active) {
+3
View File
@@ -50,6 +50,9 @@ public:
return nextFrame || active;
}
bool RecordNextFrame(const std::function<void(const Path &)> callback);
// A savestate load replaced everything a recording in progress started from. It starts over
// from the loaded state, at the next frame.
void RestartAfterLoad();
void ClearCallback() {
std::lock_guard<std::mutex> guard(callbackLock_);
writeCallback = nullptr;
+18 -1
View File
@@ -1953,7 +1953,7 @@ struct DisplayList_v2 {
};
void GPUCommon::DoState(PointerWrap &p) {
auto s = p.Section("GPUCommon", 1, 7);
auto s = p.Section("GPUCommon", 1, 8);
if (!s)
return;
@@ -2056,9 +2056,26 @@ void GPUCommon::DoState(PointerWrap &p) {
if (s >= 5) {
Do(p, matrixVisible.all);
} else if (p.mode == PointerWrap::MODE_READ) {
ResetMatrices();
}
if (s >= 6) {
Do(p, edramTranslation_);
} else if (p.mode == PointerWrap::MODE_READ) {
edramTranslation_ = 0x400;
}
// Which slot (and so which id) the next list gets. Older states start over at 0, rather than
// carry on from the session before the load.
if (s >= 8) {
Do(p, nextListID);
} else {
nextListID = 0;
}
if (p.mode == PointerWrap::MODE_READ) {
if (nextListID < 0 || nextListID > DisplayListMaxCount) {
nextListID = 0;
}
recorder_.RestartAfterLoad();
}
}
+1 -2
View File
@@ -524,10 +524,9 @@ bool GPUCommonHW::PresentedThisFrame() const {
void GPUCommonHW::DoState(PointerWrap &p) {
GPUCommon::DoState(p);
// TODO: Some of these things may not be necessary.
// None of these are necessary when saving.
if (p.mode == p.MODE_READ && !PSP_CoreParameter().frozen) {
textureCache_->Clear(true);
textureCache_->RecheckAllAfterLoad();
gstate_c.Dirty(DIRTY_TEXTURE_IMAGE);
framebufferManager_->DestroyAllFBOs();
+5
View File
@@ -313,7 +313,12 @@ void GPUStateCache::DoState(PointerWrap &p) {
bool framebufChanged = false; // legacy
Do(p, framebufChanged);
// Minimized is about the host window now, whatever it was when the state was saved.
const u32 hostSkipDraw = skipDrawReason & SKIPDRAW_WINDOW_MINIMIZED;
Do(p, skipDrawReason);
if (p.mode == p.MODE_READ) {
skipDrawReason = (skipDrawReason & ~SKIPDRAW_WINDOW_MINIMIZED) | hostSkipDraw;
}
// Legacy, remove in the next bump.
UVScale uv{};
+3 -1
View File
@@ -647,7 +647,9 @@ void SoftGPU::CopyToCurrentFboFromDisplayRam(const DisplayLayoutConfig &config,
outputFlags |= OutputFlags::PILLARBOX;
}
}
} else if (!Memory::IsValidAddress(displayFramebuf_) || srcwidth == 0 || srcheight == 0) {
} else if (srcwidth == 0 || srcheight == 0 || displayStride_ < 0 || !Memory::IsValidRange(displayFramebuf_, (displayStride_ == 0 ? srcwidth : displayStride_) * srcheight * (displayFormat_ == GE_FORMAT_8888 ? 4 : 2))) {
// Nothing, or not all of it in memory: the PSP takes strides that run past the end of VRAM, or
// negative ones (rows going up.)
hasImage = false;
u1 = 1.0f;
} else if (displayFormat_ == GE_FORMAT_8888) {
+10
View File
@@ -1622,6 +1622,12 @@ ScreenRenderFlags EmuScreen::PreRender(ScreenRenderMode mode) {
// If a boot is in progress, update it.
ProcessGameBoot(gamePath_);
if (PSP_IsInited() && !ShouldRunEmulation(mode) && !Achievements::IsBlockingExecution()) {
// Paused under a screen on top, like the pause menu. A state loaded from there should be in
// place before the game resumes, not a few frames into it, so savestate operations still run.
SaveState::Process(false);
}
using namespace Draw;
skipBufferEffects_ = g_Config.bSkipBufferEffects;
if (!skipBufferEffects_ && PSP_IsInited()) {
@@ -1777,6 +1783,10 @@ ScreenRenderFlags EmuScreen::RunEmulation(bool skipBufferEffects) {
if (Achievements::HardcoreModeActive()) {
PSP_CoreParameter().freezeNext = false;
PSP_CoreParameter().frozen = false;
} else if (coreState == CORE_STEPPING_GE || coreState == CORE_RUNNING_GE) {
// A display list stopped in the GE debugger still belongs to the sceGe call that started
// it. Loading over that, or saving it mid-list, leaves them out of step. SaveState::Process()
// waits this out too.
} else if (PSP_CoreParameter().freezeNext) {
PSP_CoreParameter().frozen = true;
PSP_CoreParameter().freezeNext = false;
+38 -9
View File
@@ -96,6 +96,10 @@ public:
}
const char *tag() const override { return "ScreenshotView"; }
// Closed with DR_OK to load the slot (the pause screen does it), rather than after saving it.
bool LoadRequested() const {
return loadRequested_;
}
protected:
bool FillVertical() const override { return false; }
@@ -154,6 +158,7 @@ private:
std::string title_;
std::string customNameTemp_;
int slot_;
bool loadRequested_ = false;
};
void ScreenshotViewScreen::OnSaveState(UI::EventParams &e) {
@@ -170,12 +175,12 @@ void ScreenshotViewScreen::OnLoadState(UI::EventParams &e) {
if (g_Config.bConfirmLoadState) {
screenManager()->push(new LoadStateConfirmScreen(saveStatePrefix_, slot_, [this](bool result) {
if (result) {
SaveState::LoadSlot(saveStatePrefix_, slot_, &ShowMessageAfterSaveStateAction);
loadRequested_ = true;
TriggerFinish(DR_OK);
}
}));
} else {
SaveState::LoadSlot(saveStatePrefix_, slot_, &ShowMessageAfterSaveStateAction);
loadRequested_ = true;
TriggerFinish(DR_OK);
}
}
@@ -364,6 +369,11 @@ void GamePauseScreen::update() {
UIBaseDialogScreen::update();
if (stateLoadDone_ && *stateLoadDone_) {
stateLoadDone_.reset();
TriggerFinish(DR_CANCEL);
}
{
std::lock_guard<std::mutex> lock(finishNextFrameMutex_);
if (!firstFrame_ && g_controlMapper.PollPauseTrigger()) {
@@ -440,8 +450,9 @@ void GamePauseScreen::CreateSavestateControls(UI::LinearLayout *leftColumnItems,
SaveSlotView *v = static_cast<SaveSlotView *>(e.v);
int slotNum = v->GetSlot();
auto doLoad = [this, slotNum]() {
SaveState::LoadSlot(saveStatePrefix_, slotNum, &ShowMessageAfterSaveStateAction);
FinishNextFrame(DR_CANCEL);
CloseAfterStateLoad([this, slotNum](SaveState::Callback callback) {
SaveState::LoadSlot(saveStatePrefix_, slotNum, callback);
});
};
if (g_Config.bConfirmLoadState) {
screenManager()->push(new LoadStateConfirmScreen(saveStatePrefix_, slotNum, [doLoad](bool result) {
@@ -483,8 +494,9 @@ void GamePauseScreen::CreateSavestateControls(UI::LinearLayout *leftColumnItems,
UI::Choice *loadUndoButton = buttonRow->Add(new Choice(pa->T("Undo last load"), ImageID("I_NAVIGATE_BACK"), new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT)));
loadUndoButton->SetEnabled(SaveState::HasUndoLoad(saveStatePrefix_));
loadUndoButton->OnClick.Add([this](UI::EventParams &e) {
SaveState::UndoLoad(saveStatePrefix_, &ShowMessageAfterSaveStateAction);
TriggerFinish(DR_CANCEL);
CloseAfterStateLoad([this](SaveState::Callback callback) {
SaveState::UndoLoad(saveStatePrefix_, callback);
});
});
}
@@ -492,8 +504,9 @@ void GamePauseScreen::CreateSavestateControls(UI::LinearLayout *leftColumnItems,
UI::Choice *rewindButton = buttonRow->Add(new Choice(pa->T("Rewind"), ImageID("I_REWIND"), new LinearLayoutParams(WRAP_CONTENT, WRAP_CONTENT)));
rewindButton->SetEnabled(SaveState::CanRewind());
rewindButton->OnClick.Add([this](UI::EventParams &e) {
SaveState::Rewind(&ShowMessageAfterSaveStateAction);
TriggerFinish(DR_CANCEL);
CloseAfterStateLoad([](SaveState::Callback callback) {
SaveState::Rewind(callback);
});
});
}
@@ -861,10 +874,26 @@ void GamePauseScreen::OnState(UI::EventParams &e) {
TriggerFinish(DR_CANCEL);
}
void GamePauseScreen::CloseAfterStateLoad(const std::function<void(SaveState::Callback)> &load) {
// Shared, so a callback arriving after the menu is gone (closed some other way) is harmless.
auto done = std::make_shared<std::atomic<bool>>(false);
stateLoadDone_ = done;
load([done](SaveState::Status status, std::string_view message, std::string_view metadata) {
ShowMessageAfterSaveStateAction(status, message, metadata);
*done = true;
});
}
void GamePauseScreen::dialogFinished(const Screen *dialog, DialogResult dr) {
std::string tag = dialog->tag();
if (tag == "ScreenshotView") {
if (dr == DR_OK) {
const ScreenshotViewScreen *view = static_cast<const ScreenshotViewScreen *>(dialog);
if (dr == DR_OK && view->LoadRequested()) {
const int slot = view->GetSlot();
CloseAfterStateLoad([this, slot](SaveState::Callback callback) {
SaveState::LoadSlot(saveStatePrefix_, slot, callback);
});
} else if (dr == DR_OK) {
FinishNextFrame(DR_BACK);
} else if (dr != DR_CANCEL && dr != DR_BACK) {
// Just go back to the pause menu, but refresh the savestate thumbnails in case something changed.
+6
View File
@@ -17,6 +17,7 @@
#pragma once
#include <atomic>
#include <functional>
#include <memory>
@@ -67,6 +68,11 @@ private:
void AddExtraOptions(UI::ViewGroup *parent);
void FinishNextFrame(DialogResult finishNextFrameResult);
// Starts a savestate load (or rewind) with the callback it's given, and closes the menu once the
// state is in place. Closing right away would let the game run a few frames first.
void CloseAfterStateLoad(const std::function<void(SaveState::Callback)> &load);
// Set by the load's callback.
std::shared_ptr<std::atomic<bool>> stateLoadDone_;
// hack
std::mutex finishNextFrameMutex_;
+10 -7
View File
@@ -50,9 +50,11 @@ doesn't (worker).
The thumbnail screenshot is already asynchronous and doesn't change.
**A save to a path that already has a save queued or in flight is a duplicate keypress.** It isn't
queued again. Its callback is attached to the save in flight and called with that save's status and
an empty message (so it shows nothing, and the in-flight save's own callback shows the result once).
**A save to a path that already has a save queued, and not yet snapshotted, is a duplicate
keypress.** It isn't queued again. Its callback is attached to the queued save and called with that
save's status and an empty message (so it shows nothing, and the queued save's own callback shows
the result once). Once the snapshot is taken, another save to the path is a new save: the game may
have moved on, or a load been applied, since.
A save to a different path is queued, and takes its snapshot right away like any other. **At most two
save snapshots exist at once**: a third save, while two are still being written, waits for the
@@ -84,8 +86,7 @@ Operations still take effect in the order they were asked for:
and snapshots the loaded state, as it does today when both run in the same `Process()`. A load
waiting for its file holds up only the emu phases queued behind it.
- **Worker jobs run in queue order**, so a save to a slot followed by a load of it writes before it
reads. As a shortcut, a load whose path has a write pending takes that save's uncompressed
snapshot directly and skips the file round trip.
reads.
- **UI-thread file operations on slots wait for the worker** (`WaitForWorker()`, for all jobs, since
it's rare and short): `UndoSaveSlot` and `DeleteSlot`. `UndoLoad` is a load, so it's queued behind
the load undo write like anything else. `LoadSlot`'s existence check counts a slot with a save
@@ -100,9 +101,11 @@ need the emu thread, so it can't deadlock.
they are. So does the freeze-frame feature (`SaveToRam`/`LoadFromRam` every frame).
- **Verify** stays synchronous.
- **Shutdown and game switch** (`SaveState::Shutdown()`, from the kernel's shutdown, which app exit
goes through too): wait for the worker, so a save that was asked for is never lost. Then run the
goes through too): wait for the worker, so a save whose snapshot was taken is written. Then run the
completions' bookkeeping (the undo slot in the config) without their callbacks, which can refer to
an EmuScreen that's going away, and drop whatever is still queued, as before.
an EmuScreen that's going away, and drop whatever is still queued, as before. A save still queued
at that point (asked for in the pause menu, which doesn't run `Process()`, then quitting) is lost
like it always was: by then most of the kernel has shut down, too late to take a snapshot.
- Loads at boot (auto-load, `--state`, opening a `.ppst`) and the debugger's `game.state.*` with
`wait` already work through callbacks, and need no special handling. The game already ran at least
a frame before a boot-time load applied.
+1 -8
View File
@@ -34,6 +34,7 @@
#include "Core/HLE/__sceAudio.h"
#include "Core/HW/MemoryStick.h"
#include "Core/MemMap.h"
#include "Core/SaveState.h"
#include "Core/System.h"
#include "Core/CoreTiming.h"
#include "Core/HW/Display.h"
@@ -1808,14 +1809,6 @@ unsigned retro_get_region(void) { return RETRO_REGION_NTSC; }
bool retro_load_game_special(unsigned type, const struct retro_game_info *info, size_t num) { return false; }
namespace SaveState
{
struct SaveStart
{
void DoState(PointerWrap &p);
};
} // namespace SaveState
size_t retro_serialize_size(void)
{
if (!gpu) // The HW renderer isn't ready on first pass.