Compare commits
2 Commits
d75e5627f9
...
32d89d8f77
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
32d89d8f77 | ||
|
|
4cdd80977c |
@@ -96,16 +96,18 @@ struct SharedDevData
|
||||
char ShaderLog[2048]{0};
|
||||
};
|
||||
|
||||
struct MemoryArena
|
||||
{
|
||||
uint8_t* Ptr = nullptr;
|
||||
uint64_t Size = 0;
|
||||
uint64_t LastAllocSize = 0;
|
||||
};
|
||||
|
||||
struct GameData
|
||||
{
|
||||
void* PermanentStorage = nullptr;
|
||||
uint64_t PermanentStorageSize = 0;
|
||||
|
||||
void* EntityStorage = nullptr;
|
||||
uint64_t EntityStorageSize = 0;
|
||||
|
||||
void* TransientStorage = nullptr;
|
||||
uint64_t TransientStorageSize = 0;
|
||||
MemoryArena PermanentArena;
|
||||
MemoryArena EntityArena;
|
||||
MemoryArena TransientArena;
|
||||
};
|
||||
|
||||
struct SharedData
|
||||
|
||||
@@ -23,6 +23,10 @@ constexpr const char* DLLPath = "libPuzGame.dll";
|
||||
constexpr const wchar_t* DLLWatch = L"libPuzGame2.dll";
|
||||
#endif
|
||||
|
||||
constexpr uint64_t KB = 1024LLU;
|
||||
constexpr uint64_t MB = 1024LLU * 1024LLU;
|
||||
constexpr uint64_t GB = 1024LLU * 1024LLU * 1024LLU;
|
||||
|
||||
namespace
|
||||
{
|
||||
bx::AllocatorI* defaultAllocator = new bx::DefaultAllocator{};
|
||||
@@ -260,6 +264,14 @@ bool ReloadDLL()
|
||||
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()
|
||||
{
|
||||
char PathBuf[512]{0};
|
||||
@@ -301,15 +313,9 @@ int main()
|
||||
HANDLE compiledShaderThread =
|
||||
CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.CompiledShaderWatcher, 0, &fileWatcherThreadId);
|
||||
|
||||
Shared.Game.PermanentStorageSize = 1024 * 1024;
|
||||
Shared.Game.PermanentStorage = VirtualAllocEx(
|
||||
GetCurrentProcess(), NULL, Shared.Game.PermanentStorageSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
||||
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);
|
||||
InitMemoryArena(Shared.Game.PermanentArena, MB);
|
||||
InitMemoryArena(Shared.Game.EntityArena, MB);
|
||||
InitMemoryArena(Shared.Game.TransientArena, 2 * GB);
|
||||
StartupFunc(Shared);
|
||||
|
||||
bool isRunning = true;
|
||||
|
||||
@@ -37,16 +37,30 @@ namespace Game
|
||||
GameInst = &instance;
|
||||
}
|
||||
|
||||
void* AllocateScratch(size_t byteCount, size_t align)
|
||||
uint8_t* AllocateScratch(size_t byteCount, size_t align)
|
||||
{
|
||||
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;
|
||||
size_t offsetAligned = ((offset + align - 1) / align) * align;
|
||||
uint8_t* ptrAligned = base + offsetAligned;
|
||||
size_t newOffset = offsetAligned + byteCount;
|
||||
if (newOffset > GetShared().Game.TransientStorageSize) return nullptr;
|
||||
if (newOffset > GetShared().Game.TransientArena.Size) return nullptr;
|
||||
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
|
||||
|
||||
@@ -33,5 +33,6 @@ namespace Game
|
||||
void SetShared(SharedData& instance);
|
||||
GameInstance& GetInstance();
|
||||
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
|
||||
|
||||
@@ -102,7 +102,7 @@ namespace Game
|
||||
void Level::Setup(GameData& data)
|
||||
{
|
||||
LOG("Level setup");
|
||||
void* storagePtr = data.EntityStorage;
|
||||
uint8_t* storagePtr = data.EntityArena.Ptr;
|
||||
bool needReset = false;
|
||||
needReset |= Cubes.Setup(storagePtr, needReset);
|
||||
needReset |= Tests.Setup(storagePtr, needReset);
|
||||
@@ -419,6 +419,7 @@ namespace Game
|
||||
UpdateMatrix(camTransform);
|
||||
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),
|
||||
static_cast<float>(Data.HeightTiles / Puzzle::Config::CardSize) - 1};
|
||||
uiOffset *= -UICardOffset * 0.5f;
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace Game
|
||||
class IEntityManager
|
||||
{
|
||||
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
|
||||
@@ -86,7 +86,7 @@ namespace Game
|
||||
|
||||
public:
|
||||
// Returns true if size changed
|
||||
bool Setup(void*& ptr, bool forceReset)
|
||||
bool Setup(uint8_t*& ptr, bool forceReset)
|
||||
{
|
||||
bool changed = false;
|
||||
if (EntitySize != sizeof(T) || forceReset)
|
||||
@@ -96,7 +96,7 @@ namespace Game
|
||||
}
|
||||
EntitySize = sizeof(T);
|
||||
Data = reinterpret_cast<T*>(ptr);
|
||||
ptr = (uint8_t*)ptr + (C * EntitySize);
|
||||
ptr += C * EntitySize;
|
||||
return changed;
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ namespace
|
||||
{
|
||||
char LineBuffer[LogInternal::MaxLineSize]{0};
|
||||
char OutBuffer[LogInternal::MaxLineSize]{0};
|
||||
char OutBufferUI[LogInternal::MaxLineSize]{0};
|
||||
bx::HandleHashMapT<1024> OnceMap;
|
||||
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::vprintf(LineBuffer, args);
|
||||
bx::vsnprintf(OutBuffer, sizeof(OutBuffer), LineBuffer, args);
|
||||
bx::vsnprintf(OutBufferUI, sizeof(OutBufferUI), format, args);
|
||||
va_end(args);
|
||||
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.WriteType[History.WriteIdx] = logType;
|
||||
bx::strCopy(&History.FileBuffer[History.WriteIdx * LogInternal::MaxLineSize], LogInternal::MaxLineSize, file);
|
||||
History.LineBuffer[History.WriteIdx] = line;
|
||||
|
||||
|
||||
@@ -40,6 +40,7 @@ struct LogHistory
|
||||
uint32_t LineBuffer[LogInternal::LogHistorySize]{0};
|
||||
int32_t WriteIdx = 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, ...);
|
||||
|
||||
@@ -33,22 +33,22 @@ namespace Game
|
||||
tracy::StartupProfiler();
|
||||
#endif
|
||||
|
||||
if (shared.Game.PermanentStorage == nullptr)
|
||||
if (shared.Game.PermanentArena.Ptr == nullptr)
|
||||
{
|
||||
LOG_ERROR("Game memory not initialized!!");
|
||||
return;
|
||||
}
|
||||
if (shared.Game.EntityStorage == nullptr)
|
||||
if (shared.Game.EntityArena.Ptr == nullptr)
|
||||
{
|
||||
LOG_ERROR("Entity memory not initialized!");
|
||||
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;
|
||||
}
|
||||
GameInstance& instance = *reinterpret_cast<GameInstance*>(shared.Game.PermanentStorage);
|
||||
GameInstance& instance = *reinterpret_cast<GameInstance*>(shared.Game.PermanentArena.Ptr);
|
||||
if (sizeof(GameInstance) != instance.Size)
|
||||
{
|
||||
LOG_WARN("Game instance size changed, resetting!");
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "Gen.h"
|
||||
#include "Global.h"
|
||||
#include "Instance.h"
|
||||
#include "Log.h"
|
||||
#include "Mesh.h"
|
||||
#include "Puzzle.h"
|
||||
#include "Tools.h"
|
||||
@@ -153,27 +154,45 @@ namespace Tools
|
||||
if (ImGui::Begin("Log"))
|
||||
{
|
||||
ImGui::Checkbox("Shorten File Names", &debug.ShortenLogFileNames);
|
||||
ImGui::BeginTable(
|
||||
"tbl", 4, ImGuiTableFlags_Resizable | ImGuiTableFlags_Hideable | ImGuiTableFlags_SizingFixedFit);
|
||||
ImGui::BeginTable("tbl",
|
||||
4,
|
||||
ImGuiTableFlags_Resizable | ImGuiTableFlags_Hideable |
|
||||
ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_RowBg);
|
||||
ImGui::TableSetupColumn("Time", ImGuiTableColumnFlags_NoResize);
|
||||
ImGui::TableSetupColumn("Log");
|
||||
ImGui::TableSetupColumn("Line", ImGuiTableColumnFlags_NoResize);
|
||||
ImGui::TableSetupColumn("File", ImGuiTableColumnFlags_NoResize);
|
||||
ImGui::TableHeadersRow();
|
||||
|
||||
auto& logs = GetLogHistory();
|
||||
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;
|
||||
const char* line = &GetLogHistory().LogBuffer[idx * LogInternal::MaxLineSize];
|
||||
const char* line = &logs.LogBuffer[idx * LogInternal::MaxLineSize];
|
||||
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();
|
||||
uint32_t fileLine = GetLogHistory().LineBuffer[idx];
|
||||
const char* filePath = &GetLogHistory().FileBuffer[idx * LogInternal::MaxLineSize];
|
||||
uint32_t fileLine = logs.LineBuffer[idx];
|
||||
const char* filePath = &logs.FileBuffer[idx * LogInternal::MaxLineSize];
|
||||
const char* filePathRes =
|
||||
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::TableNextColumn();
|
||||
ImGui::Text("%.01f", writeTime);
|
||||
@@ -187,6 +206,8 @@ namespace Tools
|
||||
|
||||
ImGui::TableNextColumn();
|
||||
ImGui::Text("%s", filePathRes);
|
||||
|
||||
ImGui::PopStyleColor(2);
|
||||
}
|
||||
}
|
||||
ImGui::EndTable();
|
||||
|
||||
@@ -32,15 +32,104 @@ namespace Game
|
||||
{
|
||||
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)
|
||||
{
|
||||
FILE* file;
|
||||
for (int32_t i = 0; i < retryCount; ++i)
|
||||
{
|
||||
file = fopen(path, "rb");
|
||||
file = std::fopen(path, "rb");
|
||||
if (file == nullptr && i < retryCount - 1)
|
||||
{
|
||||
std::this_thread::sleep_for(100ms);
|
||||
LOG_WARN("Failed to open file, retrying...");
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -48,11 +137,12 @@ namespace Game
|
||||
long fileSize = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
|
||||
long fileSizeX = appendZero ? fileSize + 1 : fileSize;
|
||||
long fileSizeX = appendZero ? (fileSize + 1) : fileSize;
|
||||
void* rawMem = AllocateScratch(fileSizeX);
|
||||
if (rawMem == nullptr)
|
||||
{
|
||||
LOG_ERROR("Failed to load file, exceeded scratch memory! %s", path);
|
||||
return nullptr;
|
||||
}
|
||||
const bgfx::Memory* mem = bgfx::makeRef(rawMem, fileSizeX);
|
||||
fread(mem->data, 1, fileSize, file);
|
||||
@@ -127,7 +217,7 @@ namespace Game
|
||||
bgfx::TextureHandle handle = BGFX_INVALID_HANDLE;
|
||||
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)
|
||||
{
|
||||
LOG_WARN("Failed to find image %s", _filePath.getCPtr());
|
||||
|
||||
Reference in New Issue
Block a user