Files
UnleashedRecomp-hedge-dev/UnleashedRecomp/gpu/imgui/imgui_snapshot.cpp
Skyth (Asilkan) 67633917bf Linux support. (#54)
* Initial Linux attempt.

* Add clang toolchain & make tools compile.

* vcpkg as submodule.

* First implementation of IO rewrite. (#31)

* Fix directory iteration resolving symlinks.

* Refactor kernel objects to be lock-free.

* Implement guest critical sections using std::atomic.

* Make D3D12 support optional. (#33)

* Make D3D12 support optional.

* Update ShaderRecomp, fix macros.

* Replace QueryPerformanceCounter. (#35)

* Add Linux home path for GetUserPath(). (#36)

* Cross-platform Sleep. (#37)

* Add mmap implementations for virtual allocation. (#38)

* Cross-platform TLS. (#34)

* Cross-platform TLS.

* Fix front() to back(), use Mutex.

* Fix global variable namings.

---------

Co-authored-by: Skyth <19259897+blueskythlikesclouds@users.noreply.github.com>

* Unicode support. (#39)

* Replace CreateDirectoryA with Unicode version.

* Cross platform thread implementation. (#41)

* Cross-platform thread implementation.

* Put set thread name calls behind a Win32 macro.

* Cross-platform semaphore implementation. (#43)

* xam: use SDL for keyboard input

* Cross-platform atomic operations. (#44)

* Cross-platform spin lock implementation.

* Cross-platform reference counting.

* Cross-platform event implementation. (#47)

* Compiling and running on Linux. (#49)

* Current work trying to get it to compile.

* Update vcpkg.json baseline.

* vcpkg, memory mapped file.

* Bitscan forward.

* Fix localtime_s.

* FPS patches high res clock.

* Rename Window to GameWindow. Fix guest pointers.

* GetCurrentThreadID gone.

* Code cache pointers, RenderWindow type.

* Add Linux stubs.

* Refactor Config.

* Fix paths.

* Add linux-release config.

* FS fixes.

* Fix Windows compilation errors & unicode converter crash.

* Rename physical memory allocation functions to not clash with X11.

* Fix NULL character being added on RtlMultiByteToUnicodeN.

* Use std::exit.

* Add protection to memory on Linux.

* Convert majority of dependencies to submodules. (#48)

* Convert majority of dependencies to submodules.

* Don't compile header-only libraries.

* Fix a few incorrect data types.

* Fix config directory.

* Unicode fixes & sizeof asserts.

* Change the exit function to not call static destructors.

* Fix files picker.

* Add RelWithDebInfo preset for Linux.

* Implement OS Restart on Linux. (#50)

---------

Co-authored-by: Dario <dariosamo@gmail.com>

* Update PowerRecomp.

* Add Env Var detection for VCPKG_ROOT, add DLC detection.

* Use error code version on DLC directory iterator.

* Set D3D12MA::ALLOCATOR_FLAG_DONT_PREFER_SMALL_BUFFERS_COMMITTED flag.

* Linux flatpak. (#51)

* Add flatpak support.

* Add game install directory override for flatpak.

* Flatpak'ing.

* Flatpak it some more.

* We flat it, we pak it.

* Flatpak'd.

* The Marvelous Misadventures of Flatpak.

* Attempt to change logic of NFD and show error.

* Flattenpakken.

* Use game install directory instead of current path.

* Attempt to fix line endings.

* Update io.github.hedge_dev.unleashedrecomp.json

* Fix system time query implementation.

* Add Present Wait to Vulkan to improve frame pacing and reduce latency. (#53)

* Add present wait support to Vulkan.

* Default to triple buffering if presentWait is supported.

* Bracey fellas.

* Update paths.h

* SDL2 audio (again). (#52)

* Implement SDL2 audio (again).

* Call timeBeginPeriod/timeEndPeriod.

* Replace miniaudio with SDL mixer.

* Queue audio samples in a separate thread.

* Enable CMake option override policy & fix compilation error.

* Fix compilation error on Linux.

* Fix but also trim shared strings.

* Wayland support. (#55)

* Make channel index a global variable in embedded player.

* Fix SDL Audio selection for OGG on Flatpak.

* Minor installer wizard fixes.

* Fix compilation error.

* Yield in model consumer and pipeline compiler threads.

* Special case Sleep(0) to yield on Linux.

* Add App Id hint.

* Correct implementation for auto reset events. (#57)

---------

Co-authored-by: Dario <dariosamo@gmail.com>
Co-authored-by: Hyper <34012267+hyperbx@users.noreply.github.com>
2024-12-21 00:44:05 +03:00

261 lines
8.5 KiB
C++

#include "imgui_snapshot.h"
#include <locale/locale.h>
#include <res/font/im_font_atlas.bin.h>
#include <user/config.h>
#include <decompressor.h>
#include <kernel/xdbf.h>
#include <app.h>
void ImDrawDataSnapshot::Clear()
{
for (int n = 0; n < Cache.GetMapSize(); n++)
if (ImDrawDataSnapshotEntry* entry = Cache.TryGetMapData(n))
IM_DELETE(entry->OurCopy);
Cache.Clear();
DrawData.Clear();
}
void ImDrawDataSnapshot::SnapUsingSwap(ImDrawData* src, double current_time)
{
ImDrawData* dst = &DrawData;
IM_ASSERT(src != dst && src->Valid);
// Copy all fields except CmdLists[]
ImVector<ImDrawList*> backup_draw_list;
backup_draw_list.swap(src->CmdLists);
IM_ASSERT(src->CmdLists.Data == NULL);
*dst = *src;
backup_draw_list.swap(src->CmdLists);
// Swap and mark as used
for (ImDrawList* src_list : src->CmdLists)
{
ImDrawDataSnapshotEntry* entry = GetOrAddEntry(src_list);
if (entry->OurCopy == NULL)
{
entry->SrcCopy = src_list;
entry->OurCopy = IM_NEW(ImDrawList)(src_list->_Data);
}
IM_ASSERT(entry->SrcCopy == src_list);
entry->SrcCopy->CmdBuffer.swap(entry->OurCopy->CmdBuffer); // Cheap swap
entry->SrcCopy->IdxBuffer.swap(entry->OurCopy->IdxBuffer);
entry->SrcCopy->VtxBuffer.swap(entry->OurCopy->VtxBuffer);
entry->SrcCopy->CmdBuffer.reserve(entry->OurCopy->CmdBuffer.Capacity); // Preserve bigger size to avoid reallocs for two consecutive frames
entry->SrcCopy->IdxBuffer.reserve(entry->OurCopy->IdxBuffer.Capacity);
entry->SrcCopy->VtxBuffer.reserve(entry->OurCopy->VtxBuffer.Capacity);
entry->LastUsedTime = current_time;
dst->CmdLists.push_back(entry->OurCopy);
}
// Cleanup unused data
const double gc_threshold = current_time - MemoryCompactTimer;
for (int n = 0; n < Cache.GetMapSize(); n++)
if (ImDrawDataSnapshotEntry* entry = Cache.TryGetMapData(n))
{
if (entry->LastUsedTime > gc_threshold)
continue;
IM_DELETE(entry->OurCopy);
Cache.Remove(GetDrawListID(entry->SrcCopy), entry);
}
};
template<typename T1, typename T2>
void ImFontAtlasSnapshot::SnapPointer(size_t offset, const T1& value, const T2& ptr, size_t count)
{
if (ptr != nullptr && count != 0)
{
if (!objects.contains(ptr))
{
constexpr size_t ALIGN = alignof(std::remove_pointer_t<T2>);
constexpr size_t SIZE = sizeof(std::remove_pointer_t<T2>);
size_t ptrOffset = (data.size() + ALIGN - 1) & ~(ALIGN - 1);
data.resize(ptrOffset + SIZE * count);
memcpy(&data[ptrOffset], ptr, SIZE * count);
for (size_t i = 0; i < count; i++)
{
size_t curPtrOffset = ptrOffset + SIZE * i;
objects[&ptr[i]] = curPtrOffset;
Traverse(curPtrOffset, ptr[i]);
}
}
size_t fieldOffset = offset + (reinterpret_cast<ptrdiff_t>(&ptr) - reinterpret_cast<ptrdiff_t>(&value));
*reinterpret_cast<size_t*>(&data[fieldOffset]) = objects[ptr];
offsets.push_back(fieldOffset);
}
}
template<typename T>
void ImFontAtlasSnapshot::Traverse(size_t offset, const T& value)
{
if constexpr (std::is_pointer_v<T>)
{
SnapPointer(offset, value, value, 1);
}
else if constexpr (std::is_same_v<T, ImFontAtlas>)
{
SnapPointer(offset, value, value.ConfigData.Data, value.ConfigData.Size);
SnapPointer(offset, value, value.CustomRects.Data, value.CustomRects.Size);
SnapPointer(offset, value, value.Fonts.Data, value.Fonts.Size);
}
else if constexpr (std::is_same_v<T, ImFont>)
{
SnapPointer(offset, value, value.IndexAdvanceX.Data, value.IndexAdvanceX.Size);
SnapPointer(offset, value, value.IndexLookup.Data, value.IndexLookup.Size);
SnapPointer(offset, value, value.Glyphs.Data, value.Glyphs.Size);
SnapPointer(offset, value, value.FallbackGlyph, 1);
SnapPointer(offset, value, value.ContainerAtlas, 1);
SnapPointer(offset, value, value.ConfigData, value.ConfigDataCount);
}
else if constexpr (std::is_same_v<T, ImFontAtlasCustomRect>)
{
SnapPointer(offset, value, value.Font, 1);
}
else if constexpr (std::is_same_v<T, ImFontConfig>)
{
SnapPointer(offset, value, value.GlyphRanges, value.GlyphRanges != nullptr ? wcslen(reinterpret_cast<const wchar_t*>(value.GlyphRanges)) + 1 : 0);
SnapPointer(offset, value, value.DstFont, 1);
}
}
template<typename T>
size_t ImFontAtlasSnapshot::Snap(const T& value)
{
size_t offset = (data.size() + alignof(T) - 1) & ~(alignof(T) - 1);
data.resize(offset + sizeof(T));
memcpy(&data[offset], &value, sizeof(T));
objects[&value] = offset;
Traverse(offset, value);
return offset;
}
struct ImFontAtlasSnapshotHeader
{
uint32_t imguiVersion;
uint32_t dataOffset;
uint32_t offsetCount;
uint32_t offsetsOffset;
};
void ImFontAtlasSnapshot::Snap()
{
data.resize(sizeof(ImFontAtlasSnapshotHeader));
size_t dataOffset = Snap(*ImGui::GetIO().Fonts);
size_t offsetsOffset = data.size();
std::sort(offsets.begin(), offsets.end());
data.insert(data.end(),
reinterpret_cast<uint8_t*>(offsets.data()),
reinterpret_cast<uint8_t*>(offsets.data() + offsets.size()));
auto header = reinterpret_cast<ImFontAtlasSnapshotHeader*>(data.data());
header->imguiVersion = IMGUI_VERSION_NUM;
header->dataOffset = dataOffset;
header->offsetCount = offsets.size();
header->offsetsOffset = offsetsOffset;
}
static std::unique_ptr<uint8_t[]> g_imFontAtlas;
ImFontAtlas* ImFontAtlasSnapshot::Load()
{
g_imFontAtlas = decompressZstd(g_im_font_atlas, g_im_font_atlas_uncompressed_size);
auto header = reinterpret_cast<ImFontAtlasSnapshotHeader*>(g_imFontAtlas.get());
assert(header->imguiVersion == IMGUI_VERSION_NUM && "ImGui version mismatch, the font atlas needs to be regenerated!");
auto offsetTable = reinterpret_cast<uint32_t*>(g_imFontAtlas.get() + header->offsetsOffset);
for (size_t i = 0; i < header->offsetCount; i++)
{
*reinterpret_cast<size_t*>(g_imFontAtlas.get() + (*offsetTable)) += reinterpret_cast<size_t>(g_imFontAtlas.get());
++offsetTable;
}
return reinterpret_cast<ImFontAtlas*>(g_imFontAtlas.get() + header->dataOffset);
}
static void GetGlyphs(std::set<ImWchar>& glyphs, const std::string_view& value)
{
const char* cur = value.data();
while (cur < value.data() + value.size())
{
unsigned int c;
cur += ImTextCharFromUtf8(&c, cur, value.data() + value.size());
glyphs.emplace(c);
}
}
static std::vector<ImWchar> g_glyphRanges;
void ImFontAtlasSnapshot::GenerateGlyphRanges()
{
std::vector<std::string_view> localeStrings;
for (auto& config : g_configDefinitions)
config->GetLocaleStrings(localeStrings);
std::set<ImWchar> glyphs;
for (size_t i = 0x20; i <= 0xFF; i++)
glyphs.emplace(i);
for (auto& localeString : localeStrings)
GetGlyphs(glyphs, localeString);
for (auto& [name, locale] : g_locale)
{
for (auto& [language, value] : locale)
GetGlyphs(glyphs, value);
}
for (size_t i = XDBF_LANGUAGE_ENGLISH; i <= XDBF_LANGUAGE_ITALIAN; i++)
{
auto achievements = g_xdbfWrapper.GetAchievements(static_cast<EXDBFLanguage>(i));
for (auto& achievement : achievements)
{
GetGlyphs(glyphs, achievement.Name);
GetGlyphs(glyphs, achievement.UnlockedDesc);
GetGlyphs(glyphs, achievement.LockedDesc);
}
}
for (auto glyph : glyphs)
{
if (g_glyphRanges.empty() || (g_glyphRanges.back() + 1) != glyph)
{
g_glyphRanges.push_back(glyph);
g_glyphRanges.push_back(glyph);
}
else
{
g_glyphRanges.back() = glyph;
}
}
g_glyphRanges.push_back(0);
}
ImFont* ImFontAtlasSnapshot::GetFont(const char* name)
{
auto fontAtlas = ImGui::GetIO().Fonts;
for (auto& configData : fontAtlas->ConfigData)
{
if (strstr(configData.Name, name) != nullptr)
{
assert(configData.DstFont != nullptr);
return configData.DstFont;
}
}
#ifdef ENABLE_IM_FONT_ATLAS_SNAPSHOT
assert(false && "Unable to locate equivalent font in the atlas file.");
#endif
return fontAtlas->AddFontFromFileTTF(name, 24.0f, nullptr, g_glyphRanges.data());
}