Compare commits

..

2 Commits

Author SHA1 Message Date
Till Wübbers
32d89d8f77 logging improvements 2025-04-28 08:23:16 +02:00
Till Wübbers
4cdd80977c wip 2025-04-27 12:00:22 +02:00
11 changed files with 181 additions and 42 deletions

View File

@@ -96,16 +96,18 @@ struct SharedDevData
char ShaderLog[2048]{0}; char ShaderLog[2048]{0};
}; };
struct MemoryArena
{
uint8_t* Ptr = nullptr;
uint64_t Size = 0;
uint64_t LastAllocSize = 0;
};
struct GameData struct GameData
{ {
void* PermanentStorage = nullptr; MemoryArena PermanentArena;
uint64_t PermanentStorageSize = 0; MemoryArena EntityArena;
MemoryArena TransientArena;
void* EntityStorage = nullptr;
uint64_t EntityStorageSize = 0;
void* TransientStorage = nullptr;
uint64_t TransientStorageSize = 0;
}; };
struct SharedData struct SharedData

View File

@@ -23,6 +23,10 @@ constexpr const char* DLLPath = "libPuzGame.dll";
constexpr const wchar_t* DLLWatch = L"libPuzGame2.dll"; constexpr const wchar_t* DLLWatch = L"libPuzGame2.dll";
#endif #endif
constexpr uint64_t KB = 1024LLU;
constexpr uint64_t MB = 1024LLU * 1024LLU;
constexpr uint64_t GB = 1024LLU * 1024LLU * 1024LLU;
namespace namespace
{ {
bx::AllocatorI* defaultAllocator = new bx::DefaultAllocator{}; bx::AllocatorI* defaultAllocator = new bx::DefaultAllocator{};
@@ -260,6 +264,14 @@ bool ReloadDLL()
return true; return true;
} }
void InitMemoryArena(MemoryArena& arena, uint64_t size)
{
arena.Size = size;
arena.Ptr = reinterpret_cast<uint8_t*>(
VirtualAllocEx(GetCurrentProcess(), NULL, size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
arena.LastAllocSize = 0;
}
int main() int main()
{ {
char PathBuf[512]{0}; char PathBuf[512]{0};
@@ -301,15 +313,9 @@ int main()
HANDLE compiledShaderThread = HANDLE compiledShaderThread =
CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.CompiledShaderWatcher, 0, &fileWatcherThreadId); CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.CompiledShaderWatcher, 0, &fileWatcherThreadId);
Shared.Game.PermanentStorageSize = 1024 * 1024; InitMemoryArena(Shared.Game.PermanentArena, MB);
Shared.Game.PermanentStorage = VirtualAllocEx( InitMemoryArena(Shared.Game.EntityArena, MB);
GetCurrentProcess(), NULL, Shared.Game.PermanentStorageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE); InitMemoryArena(Shared.Game.TransientArena, 2 * GB);
Shared.Game.EntityStorageSize = 1024 * 1024;
Shared.Game.EntityStorage = VirtualAllocEx(
GetCurrentProcess(), NULL, Shared.Game.EntityStorageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
Shared.Game.TransientStorageSize = 1024 * 1024 * 1024;
Shared.Game.TransientStorage = VirtualAllocEx(
GetCurrentProcess(), NULL, Shared.Game.TransientStorageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
StartupFunc(Shared); StartupFunc(Shared);
bool isRunning = true; bool isRunning = true;

View File

@@ -37,16 +37,30 @@ namespace Game
GameInst = &instance; GameInst = &instance;
} }
void* AllocateScratch(size_t byteCount, size_t align) uint8_t* AllocateScratch(size_t byteCount, size_t align)
{ {
size_t offset = GetInstance().UsedScratchAmount; size_t offset = GetInstance().UsedScratchAmount;
uint8_t* base = static_cast<uint8_t*>(GetShared().Game.TransientStorage); uint8_t* base = GetShared().Game.TransientArena.Ptr;
uint8_t* current = base + offset; uint8_t* current = base + offset;
size_t offsetAligned = ((offset + align - 1) / align) * align; size_t offsetAligned = ((offset + align - 1) / align) * align;
uint8_t* ptrAligned = base + offsetAligned; uint8_t* ptrAligned = base + offsetAligned;
size_t newOffset = offsetAligned + byteCount; size_t newOffset = offsetAligned + byteCount;
if (newOffset > GetShared().Game.TransientStorageSize) return nullptr; if (newOffset > GetShared().Game.TransientArena.Size) return nullptr;
GetInstance().UsedScratchAmount = newOffset; GetInstance().UsedScratchAmount = newOffset;
return reinterpret_cast<void*>(ptrAligned); GetShared().Game.TransientArena.LastAllocSize = byteCount;
return ptrAligned;
}
bool ResizeLastScratchAlloc(size_t newByteCount)
{
auto& arena = GetShared().Game.TransientArena;
if (newByteCount > arena.LastAllocSize)
{
LOG_ERROR("Can't resize to more than previous size!");
return false;
}
arena.Ptr -= arena.LastAllocSize;
arena.Ptr += newByteCount;
return true;
} }
} // namespace Game } // namespace Game

View File

@@ -33,5 +33,6 @@ namespace Game
void SetShared(SharedData& instance); void SetShared(SharedData& instance);
GameInstance& GetInstance(); GameInstance& GetInstance();
void SetInstance(GameInstance& instance); void SetInstance(GameInstance& instance);
void* AllocateScratch(size_t byteCount, size_t align = 16); uint8_t* AllocateScratch(size_t byteCount, size_t align = 16);
bool ResizeLastScratchAlloc(size_t newByteCount);
} // namespace Game } // namespace Game

View File

@@ -102,7 +102,7 @@ namespace Game
void Level::Setup(GameData& data) void Level::Setup(GameData& data)
{ {
LOG("Level setup"); LOG("Level setup");
void* storagePtr = data.EntityStorage; uint8_t* storagePtr = data.EntityArena.Ptr;
bool needReset = false; bool needReset = false;
needReset |= Cubes.Setup(storagePtr, needReset); needReset |= Cubes.Setup(storagePtr, needReset);
needReset |= Tests.Setup(storagePtr, needReset); needReset |= Tests.Setup(storagePtr, needReset);
@@ -419,6 +419,7 @@ namespace Game
UpdateMatrix(camTransform); UpdateMatrix(camTransform);
Vec3 cameraPos = camTransform.Position; Vec3 cameraPos = camTransform.Position;
// TODO: disable warning & check if parentheses make sense like this
Vec2 uiOffset = Vec2{static_cast<float>(Data.WidthTiles / Puzzle::Config::CardSize - 1), Vec2 uiOffset = Vec2{static_cast<float>(Data.WidthTiles / Puzzle::Config::CardSize - 1),
static_cast<float>(Data.HeightTiles / Puzzle::Config::CardSize) - 1}; static_cast<float>(Data.HeightTiles / Puzzle::Config::CardSize) - 1};
uiOffset *= -UICardOffset * 0.5f; uiOffset *= -UICardOffset * 0.5f;

View File

@@ -72,7 +72,7 @@ namespace Game
class IEntityManager class IEntityManager
{ {
public: public:
virtual bool Setup(void*& ptr, bool forceReset) = 0; virtual bool Setup(uint8_t*& ptr, bool forceReset) = 0;
}; };
template <typename T, typename HandleT, uint32_t C> class EntityManager : public IEntityManager template <typename T, typename HandleT, uint32_t C> class EntityManager : public IEntityManager
@@ -86,7 +86,7 @@ namespace Game
public: public:
// Returns true if size changed // Returns true if size changed
bool Setup(void*& ptr, bool forceReset) bool Setup(uint8_t*& ptr, bool forceReset)
{ {
bool changed = false; bool changed = false;
if (EntitySize != sizeof(T) || forceReset) if (EntitySize != sizeof(T) || forceReset)
@@ -96,7 +96,7 @@ namespace Game
} }
EntitySize = sizeof(T); EntitySize = sizeof(T);
Data = reinterpret_cast<T*>(ptr); Data = reinterpret_cast<T*>(ptr);
ptr = (uint8_t*)ptr + (C * EntitySize); ptr += C * EntitySize;
return changed; return changed;
} }

View File

@@ -17,6 +17,7 @@ namespace
{ {
char LineBuffer[LogInternal::MaxLineSize]{0}; char LineBuffer[LogInternal::MaxLineSize]{0};
char OutBuffer[LogInternal::MaxLineSize]{0}; char OutBuffer[LogInternal::MaxLineSize]{0};
char OutBufferUI[LogInternal::MaxLineSize]{0};
bx::HandleHashMapT<1024> OnceMap; bx::HandleHashMapT<1024> OnceMap;
LogHistory History; LogHistory History;
@@ -36,11 +37,13 @@ void Log(ELogType logType, const char* file, uint32_t line, const char* format,
bx::snprintf(LineBuffer, sizeof(LineBuffer), LineFormat, format); bx::snprintf(LineBuffer, sizeof(LineBuffer), LineFormat, format);
bx::vprintf(LineBuffer, args); bx::vprintf(LineBuffer, args);
bx::vsnprintf(OutBuffer, sizeof(OutBuffer), LineBuffer, args); bx::vsnprintf(OutBuffer, sizeof(OutBuffer), LineBuffer, args);
bx::vsnprintf(OutBufferUI, sizeof(OutBufferUI), format, args);
va_end(args); va_end(args);
OutputDebugStringA(OutBuffer); OutputDebugStringA(OutBuffer);
bx::strCopy(&History.LogBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, OutBuffer); bx::strCopy(&History.LogBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, OutBufferUI);
History.WriteTime[History.WriteIdx] = bx::getHPCounter(); History.WriteTime[History.WriteIdx] = bx::getHPCounter();
History.WriteType[History.WriteIdx] = logType;
bx::strCopy(&History.FileBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, file); bx::strCopy(&History.FileBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, file);
History.LineBuffer[History.WriteIdx] = line; History.LineBuffer[History.WriteIdx] = line;

View File

@@ -40,6 +40,7 @@ struct LogHistory
uint32_t LineBuffer[LogInternal::LogHistorySize]{0}; uint32_t LineBuffer[LogInternal::LogHistorySize]{0};
int32_t WriteIdx = 0; int32_t WriteIdx = 0;
int64_t WriteTime[LogInternal::LogHistorySize]{0}; int64_t WriteTime[LogInternal::LogHistorySize]{0};
ELogType WriteType[LogInternal::LogHistorySize]{ELogType::Log};
}; };
void Log(ELogType logType, const char* file, uint32_t line, const char* format, ...); void Log(ELogType logType, const char* file, uint32_t line, const char* format, ...);

View File

@@ -33,22 +33,22 @@ namespace Game
tracy::StartupProfiler(); tracy::StartupProfiler();
#endif #endif
if (shared.Game.PermanentStorage == nullptr) if (shared.Game.PermanentArena.Ptr == nullptr)
{ {
LOG_ERROR("Game memory not initialized!!"); LOG_ERROR("Game memory not initialized!!");
return; return;
} }
if (shared.Game.EntityStorage == nullptr) if (shared.Game.EntityArena.Ptr == nullptr)
{ {
LOG_ERROR("Entity memory not initialized!"); LOG_ERROR("Entity memory not initialized!");
return; return;
} }
if (shared.Game.PermanentStorageSize < sizeof(GameInstance)) if (shared.Game.PermanentArena.Size < sizeof(GameInstance))
{ {
LOG_ERROR("Game memory too small! %u < %u", shared.Game.PermanentStorageSize, sizeof(GameInstance)); LOG_ERROR("Game memory too small! %u < %u", shared.Game.PermanentArena.Size, sizeof(GameInstance));
return; return;
} }
GameInstance& instance = *reinterpret_cast<GameInstance*>(shared.Game.PermanentStorage); GameInstance& instance = *reinterpret_cast<GameInstance*>(shared.Game.PermanentArena.Ptr);
if (sizeof(GameInstance) != instance.Size) if (sizeof(GameInstance) != instance.Size)
{ {
LOG_WARN("Game instance size changed, resetting!"); LOG_WARN("Game instance size changed, resetting!");

View File

@@ -2,6 +2,7 @@
#include "Gen.h" #include "Gen.h"
#include "Global.h" #include "Global.h"
#include "Instance.h" #include "Instance.h"
#include "Log.h"
#include "Mesh.h" #include "Mesh.h"
#include "Puzzle.h" #include "Puzzle.h"
#include "Tools.h" #include "Tools.h"
@@ -153,27 +154,45 @@ namespace Tools
if (ImGui::Begin("Log")) if (ImGui::Begin("Log"))
{ {
ImGui::Checkbox("Shorten File Names", &debug.ShortenLogFileNames); ImGui::Checkbox("Shorten File Names", &debug.ShortenLogFileNames);
ImGui::BeginTable( ImGui::BeginTable("tbl",
"tbl", 4, ImGuiTableFlags_Resizable | ImGuiTableFlags_Hideable | ImGuiTableFlags_SizingFixedFit); 4,
ImGuiTableFlags_Resizable | ImGuiTableFlags_Hideable |
ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg);
ImGui::TableSetupColumn("Time", ImGuiTableColumnFlags_NoResize); ImGui::TableSetupColumn("Time", ImGuiTableColumnFlags_NoResize);
ImGui::TableSetupColumn("Log"); ImGui::TableSetupColumn("Log");
ImGui::TableSetupColumn("Line", ImGuiTableColumnFlags_NoResize); ImGui::TableSetupColumn("Line", ImGuiTableColumnFlags_NoResize);
ImGui::TableSetupColumn("File", ImGuiTableColumnFlags_NoResize); ImGui::TableSetupColumn("File", ImGuiTableColumnFlags_NoResize);
ImGui::TableHeadersRow(); ImGui::TableHeadersRow();
auto& logs = GetLogHistory();
for (int32_t i = 0; i < bx::min(100, LogInternal::LogHistorySize); ++i) for (int32_t i = 0; i < bx::min(100, LogInternal::LogHistorySize); ++i)
{ {
int32_t idx = GetLogHistory().WriteIdx - i - 1; int32_t idx = logs.WriteIdx - i - 1;
if (idx < 0) idx += LogInternal::LogHistorySize; if (idx < 0) idx += LogInternal::LogHistorySize;
const char* line = &GetLogHistory().LogBuffer[idx * LogInternal::MaxLineSize]; const char* line = &logs.LogBuffer[idx * LogInternal::MaxLineSize];
if (line[0] != 0) if (line[0] != 0)
{ {
int64_t timeOffset = GetLogHistory().WriteTime[idx] - time.StartTime; int64_t timeOffset = logs.WriteTime[idx] - time.StartTime;
double writeTime = (double)timeOffset / bx::getHPFrequency(); double writeTime = (double)timeOffset / bx::getHPFrequency();
uint32_t fileLine = GetLogHistory().LineBuffer[idx]; uint32_t fileLine = logs.LineBuffer[idx];
const char* filePath = &GetLogHistory().FileBuffer[idx * LogInternal::MaxLineSize]; const char* filePath = &logs.FileBuffer[idx * LogInternal::MaxLineSize];
const char* filePathRes = const char* filePathRes =
debug.ShortenLogFileNames ? bx::FilePath{filePath}.getFileName().getPtr() : filePath; debug.ShortenLogFileNames ? bx::FilePath{filePath}.getFileName().getPtr() : filePath;
// ImVec4 texCol = {1.0f, 1.0f, 1.0f, 1.0f};
// if (logs.WriteType[idx] == ELogType::Warn)
// texCol = {1.0f, 1.0f, 0.8f, 1.0f};
// else if (logs.WriteType[idx] == ELogType::Error)
// texCol = {1.0f, 0.8f, 0.8f, 1.0f};
// ImGui::PushStyleColor(ImGuiCol_Text, texCol);
ImVec4 bgCol = {0.0f, 0.0f, 0.0f, 1.0f};
if (logs.WriteType[idx] == ELogType::Warn)
bgCol = {0.2f, 0.2f, 0.0f, 1.0f};
else if (logs.WriteType[idx] == ELogType::Error)
bgCol = {0.2f, 0.0f, 0.0f, 1.0f};
ImGui::PushStyleColor(ImGuiCol_TableRowBg, bgCol);
ImGui::PushStyleColor(ImGuiCol_TableRowBgAlt, bgCol);
ImGui::TableNextRow(); ImGui::TableNextRow();
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%.01f", writeTime); ImGui::Text("%.01f", writeTime);
@@ -187,6 +206,8 @@ namespace Tools
ImGui::TableNextColumn(); ImGui::TableNextColumn();
ImGui::Text("%s", filePathRes); ImGui::Text("%s", filePathRes);
ImGui::PopStyleColor(2);
} }
} }
ImGui::EndTable(); ImGui::EndTable();

View File

@@ -32,15 +32,104 @@ namespace Game
{ {
namespace namespace
{ {
constexpr size_t ChunkSize = 1024;
constexpr size_t MaxFileSize = ChunkSize * 1024 * 1024;
constexpr size_t MaxChunkCount = MaxFileSize / ChunkSize;
bool BufferedFileRead(FILE* file, uint8_t* writePtr, size_t& totalReadCount)
{
for (int32_t i = 0; i < MaxChunkCount; ++i)
{
size_t readCount = std::fread(writePtr, 1, ChunkSize, file);
writePtr += readCount;
totalReadCount += readCount;
if (readCount != ChunkSize)
{
if (std::feof(file))
{
return true;
}
int err = std::ferror(file);
if (err != 0)
{
LOG_ERROR("Error reading file: %i", err);
return false;
}
LOG_ERROR("This should never happen!");
return false;
}
}
if (!std::feof(file))
{
LOG_ERROR("File too big to be read!");
return false;
}
return true;
}
const bgfx::Memory* LoadBinaryFile(const char* path, int32_t retryCount = 1)
{
FILE* file = nullptr;
for (int32_t i = 0; i < retryCount; ++i)
{
file = std::fopen(path, "rb");
if (file == nullptr)
{
if (i < retryCount - 1)
{
std::this_thread::sleep_for(100ms);
LOG_WARN("Failed to open file, retrying...");
break;
}
else
{
LOG_ERROR("Failed to open file!");
return nullptr;
}
}
}
uint8_t* dataPtr = AllocateScratch(MaxFileSize);
if (dataPtr == nullptr)
{
LOG_ERROR("Failed to load file, exceeded scratch memory! %s", path);
return nullptr;
}
uint64_t totalReadCount = 0;
bool success = BufferedFileRead(file, dataPtr, totalReadCount);
std::fclose(file);
if (!success)
{
LOG_ERROR("Failed to read file %s", path);
ResizeLastScratchAlloc(0);
return nullptr;
}
if (!ResizeLastScratchAlloc(totalReadCount))
{
LOG_ERROR("This should never happen!");
return nullptr;
}
return bgfx::makeRef(dataPtr, totalReadCount);
}
const bgfx::Memory* loadFile(const char* path, bool appendZero = false, int32_t retryCount = 1) const bgfx::Memory* loadFile(const char* path, bool appendZero = false, int32_t retryCount = 1)
{ {
FILE* file; FILE* file;
for (int32_t i = 0; i < retryCount; ++i) for (int32_t i = 0; i < retryCount; ++i)
{ {
file = fopen(path, "rb"); file = std::fopen(path, "rb");
if (file == nullptr && i < retryCount - 1) if (file == nullptr && i < retryCount - 1)
{ {
std::this_thread::sleep_for(100ms); std::this_thread::sleep_for(100ms);
LOG_WARN("Failed to open file, retrying...");
break; break;
} }
@@ -48,11 +137,12 @@ namespace Game
long fileSize = ftell(file); long fileSize = ftell(file);
fseek(file, 0, SEEK_SET); fseek(file, 0, SEEK_SET);
long fileSizeX = appendZero ? fileSize + 1 : fileSize; long fileSizeX = appendZero ? (fileSize + 1) : fileSize;
void* rawMem = AllocateScratch(fileSizeX); void* rawMem = AllocateScratch(fileSizeX);
if (rawMem == nullptr) if (rawMem == nullptr)
{ {
LOG_ERROR("Failed to load file, exceeded scratch memory! %s", path); LOG_ERROR("Failed to load file, exceeded scratch memory! %s", path);
return nullptr;
} }
const bgfx::Memory* mem = bgfx::makeRef(rawMem, fileSizeX); const bgfx::Memory* mem = bgfx::makeRef(rawMem, fileSizeX);
fread(mem->data, 1, fileSize, file); fread(mem->data, 1, fileSize, file);
@@ -127,7 +217,7 @@ namespace Game
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE; bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
bx::Error err; bx::Error err;
const bgfx::Memory* data = loadFile(_filePath.getCPtr()); const bgfx::Memory* data = LoadBinaryFile(_filePath.getCPtr());
if (data == nullptr || data->data == nullptr || data->size == 0) if (data == nullptr || data->data == nullptr || data->size == 0)
{ {
LOG_WARN("Failed to find image %s", _filePath.getCPtr()); LOG_WARN("Failed to find image %s", _filePath.getCPtr());