melonDS/src/Savestate.h

125 lines
2.9 KiB
C
Raw Normal View History

/*
2024-06-15 15:01:19 +00:00
Copyright 2016-2024 melonDS team
This file is part of melonDS.
melonDS is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation, either version 3 of the License, or (at your option)
any later version.
melonDS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with melonDS. If not, see http://www.gnu.org/licenses/.
*/
#ifndef SAVESTATE_H
#define SAVESTATE_H
#include <cstring>
#include <string>
#include <stdio.h>
#include "types.h"
Protect savestates while the threaded software renderer is running (#1864) * First crack at ensuring the render thread doesn't touch GPU state while it's being serialized * Get rid of the semaphore wait * Add some extra fields into GPU3D's serialization * Oops, TempVertexBuffer is already serialized * Move vertex serialization into its own method * Lock the GPU3D state when rendering on the render thread or serializing it * Revert "Lock the GPU3D state when rendering on the render thread or serializing it" This reverts commit 2f49a551c13934b9dc815bbda67a45098f0482a7. * Add comments that describe the synchronization within GPU3D_Soft - I need to understand it before I can solve my actual problem - Now I do * Revert "Revert "Lock the GPU3D state when rendering on the render thread or serializing it"" This reverts commit 1977566a6d8671d72bd94ba4ebf832c3bf08933a. * Let's try locking the GPU3D state throughout NDS::RunFrame - Just to see what happens * Slim down the lock's scope * Narrow the lock's scope some more * Remove the lock entirely * Try protecting the GPU3D state with just a mutex - I'll clean this up once I know it works * Remove a duplicate method definition * Add a missing `noexcept` specifier * Remove an unused function * Cut some non-hardware state from `GPU3D`'s savestate * Assume that the next frame after loading a savestate won't be identical * Actually, it _is_ worth it * Don't serialize the clip matrix - It's recalculated anyway * Serialize `RenderPolygonRAM` as an array of indexes * Clean up some comments - I liked the dialogue style, but oh well * Try restarting the render thread instead of using the lock - Let's see what happens * Put the lock back * Fix some polygon and vertex indexes being saved incorrectly - Taking the difference between two pointers results in the number of elements, not the number of bytes * Remove `SoftRenderer::StateBusy` since it turns out we don't need it - The real synchronization was the friends we made along the way
2024-01-07 22:39:43 +00:00
#define SAVESTATE_MAJOR 12
#define SAVESTATE_MINOR 1
namespace melonDS
{
class Savestate
{
public:
static constexpr u32 DEFAULT_SIZE = 32 * 1024 * 1024; // 32 MB
Savestate(void* buffer, u32 size, bool save);
explicit Savestate(u32 initial_size = DEFAULT_SIZE);
~Savestate();
bool Error;
bool Saving;
u32 CurSection;
2019-05-04 13:07:02 +00:00
void Section(const char* magic);
void Var8(u8* var)
{
VarArray(var, sizeof(*var));
}
void Var16(u16* var)
{
VarArray(var, sizeof(*var));
}
void Var32(u32* var)
{
VarArray(var, sizeof(*var));
}
void Var64(u64* var)
{
VarArray(var, sizeof(*var));
}
void Bool32(bool* var);
void VarArray(void* data, u32 len);
void Finish();
// TODO rewinds the stream
void Rewind(bool save);
bool IsAtLeastVersion(u32 major, u32 minor)
2018-11-25 21:40:08 +00:00
{
u16 major_version = MajorVersion();
if (MajorVersion() > major) return true;
if (major_version == major && MinorVersion() >= minor) return true;
2018-11-25 21:40:08 +00:00
return false;
}
void* Buffer() { return buffer; }
[[nodiscard]] const void* Buffer() const { return buffer; }
[[nodiscard]] u32 BufferLength() const { return buffer_length; }
[[nodiscard]] u32 Length() const { return buffer_offset; }
[[nodiscard]] u16 MajorVersion() const
{
// major version is stored at offset 0x04
u16 major = 0;
memcpy(&major, buffer + 0x04, sizeof(major));
return major;
}
[[nodiscard]] u16 MinorVersion() const
{
// minor version is stored at offset 0x06
u16 minor = 0;
memcpy(&minor, buffer + 0x06, sizeof(minor));
return minor;
}
private:
static constexpr u32 NO_SECTION = 0xffffffff;
void CloseCurrentSection();
bool Resize(u32 new_length);
void WriteSavestateHeader();
void WriteStateLength();
u32 FindSection(const char* magic) const;
u8* buffer;
u32 buffer_offset;
u32 buffer_length;
bool buffer_owned;
bool finished;
};
}
#endif // SAVESTATE_H