memory setup

This commit is contained in:
Asuro
2025-02-09 03:01:43 +01:00
parent fbed568fae
commit df752065af
12 changed files with 227 additions and 80 deletions

View File

@@ -35,6 +35,6 @@ SET(BGFX_BUILD_TOOLS ON)
SET(BGFX_BUILD_EXAMPLES OFF) SET(BGFX_BUILD_EXAMPLES OFF)
add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake") add_subdirectory("${CMAKE_SOURCE_DIR}/dependency/bgfx.cmake")
target_link_libraries(PuzGame bx bimg bgfx SDL3::SDL3) target_link_libraries(PuzGame bx bimg bgfx)
target_link_libraries(PuzGameEngine bx) target_link_libraries(PuzGameEngine bx SDL3::SDL3)
set_target_properties(PuzGame PROPERTIES OUTPUT_NAME "PuzGame2") set_target_properties(PuzGame PROPERTIES OUTPUT_NAME "PuzGame2")

26
src/engine/Shared.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
#include <cstdint>
struct SharedWindowData
{
void* Handle = nullptr;
int32_t WindowWidth = 1920;
int32_t WindowHeight = 1080;
};
struct GameData
{
void* PermanentStorage = nullptr;
uint64_t PermanentStorageSize = 0;
};
struct SharedData
{
SharedWindowData Window;
GameData Game;
};
typedef void (*Startup)(SharedData& shared);
typedef void (*Update)();
typedef void (*Shutdown)();

53
src/engine/Window.cpp Normal file
View File

@@ -0,0 +1,53 @@
#include "Window.h"
#include "SDL3/SDL_events.h"
#include "Shared.h"
#include <cstdio>
#include <SDL3/SDL.h>
void EngineWindow::Startup(SharedWindowData& shared)
{
if (!SDL_Init(SDL_INIT_VIDEO))
{
printf("Failed to init SDL!\n");
return;
}
Window = SDL_CreateWindow("SDL", shared.WindowWidth, shared.WindowHeight, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
if (Window == nullptr)
{
printf("Failed to init SDL Window!\n");
return;
}
auto props = SDL_GetWindowProperties(Window);
SDL_EnumerateProperties(props, [](void*, SDL_PropertiesID id, const char* name)
{
printf("Prop: %s\n", name);
}, nullptr);
shared.Handle = SDL_GetPointerProperty(props, "SDL.window.win32.hwnd", nullptr);
if (shared.Handle == nullptr)
{
printf("Failed to get window pointer!\n");
return;
}
}
void EngineWindow::Update(SharedWindowData& Shared)
{
SDL_Event evt;
while (SDL_PollEvent(&evt))
{
switch (evt.type)
{
case SDL_EVENT_WINDOW_RESIZED:
{
SDL_GetWindowSize(Window, &Shared.WindowWidth, &Shared.WindowHeight);
break;
}
case SDL_EVENT_WINDOW_CLOSE_REQUESTED:
{
CloseRequested = true;
break;
}
}
}
}

15
src/engine/Window.h Normal file
View File

@@ -0,0 +1,15 @@
#pragma once
#include "Shared.h"
#include <SDL3/SDL.h>
class EngineWindow
{
public:
SDL_Window* Window;
bool CloseRequested = false;
public:
void Startup(SharedWindowData& Shared);
void Update(SharedWindowData& Shared);
void Shutdown();
};

View File

@@ -6,20 +6,19 @@
#include <cstdio> #include <cstdio>
#include <bx/string.h> #include <bx/string.h>
//#define VISUAL_STUDIO #include "Shared.h"
#include "Window.h"
typedef void (*Startup)(void*); //#define VISUAL_STUDIO
typedef void (*Update)();
typedef void (*Shutdown)();
constexpr UINT WM_CUSTOM_DLL_CHANGE = WM_USER + 1; constexpr UINT WM_CUSTOM_DLL_CHANGE = WM_USER + 1;
#ifdef VISUAL_STUDIO #ifdef VISUAL_STUDIO
constexpr char* DLLPath = "PuzGame.dll"; constexpr const char* DLLPath = "PuzGame.dll";
constexpr wchar_t* DLLWatch = L"PuzGame2.dll"; constexpr const wchar_t* DLLWatch = L"PuzGame2.dll";
#else #else
constexpr char* DLLPath = "libPuzGame.dll"; constexpr const char* DLLPath = "libPuzGame.dll";
constexpr wchar_t* DLLWatch = L"libPuzGame2.dll"; constexpr const wchar_t* DLLWatch = L"cmake-build\\libPuzGame2.dll";
#endif #endif
struct FileWatcherData struct FileWatcherData
@@ -35,9 +34,16 @@ struct DevelopmentData
FileWatcherData FileWatcher; FileWatcherData FileWatcher;
}; };
struct EngineData
{
EngineWindow Window;
};
namespace namespace
{ {
DevelopmentData DevData; DevelopmentData DevData;
EngineData Engine;
SharedData Shared;
Startup StartupFunc; Startup StartupFunc;
Update UpdateFunc; Update UpdateFunc;
Shutdown ShutdownFunc; Shutdown ShutdownFunc;
@@ -56,7 +62,7 @@ unsigned long FileWatcherThread(void* data)
while (true) while (true)
{ {
FILE_NOTIFY_INFORMATION* notifyData = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(&DevData.FileChangeBuffer[offset]); FILE_NOTIFY_INFORMATION* notifyData = reinterpret_cast<FILE_NOTIFY_INFORMATION*>(&DevData.FileChangeBuffer[offset]);
wprintf(L"Change: %ls\n", notifyData->FileName); // wprintf(L"Change: %ls\n", notifyData->FileName);
if (notifyData->Action == FILE_ACTION_ADDED || notifyData->Action == FILE_ACTION_MODIFIED || notifyData->Action == FILE_ACTION_RENAMED_NEW_NAME) if (notifyData->Action == FILE_ACTION_ADDED || notifyData->Action == FILE_ACTION_MODIFIED || notifyData->Action == FILE_ACTION_RENAMED_NEW_NAME)
{ {
@@ -107,7 +113,7 @@ bool ReloadDLL()
#ifdef VISUAL_STUDIO #ifdef VISUAL_STUDIO
Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "?Setup@Game@@YAXPEAX@Z"); Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "?Setup@Game@@YAXPEAX@Z");
#else #else
Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "_ZN4Game5SetupEPv"); Startup StartupReloaded = (Startup)GetProcAddress(DevData.GameLib, "_ZN4Game5SetupER10SharedData");
#endif #endif
if (StartupReloaded == NULL) if (StartupReloaded == NULL)
{ {
@@ -153,6 +159,13 @@ int main()
if (!ReloadDLL()) return 1; if (!ReloadDLL()) return 1;
Engine.Window.Startup(Shared.Window);
if (Shared.Window.Handle == nullptr)
{
printf("Failed to set up window!\n");
return 1;
}
DevData.hDevDir = CreateFile(".", DevData.hDevDir = CreateFile(".",
FILE_LIST_DIRECTORY, FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
@@ -163,16 +176,22 @@ int main()
DWORD fileWatcherThreadId = 0; DWORD fileWatcherThreadId = 0;
CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId); CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId);
StartupFunc(nullptr); StartupFunc(Shared);
bool isRunning = true; bool isRunning = true;
while (isRunning) while (isRunning)
{ {
Engine.Window.Update(Shared.Window);
if (Engine.Window.CloseRequested)
{
isRunning = false;
}
if (DevData.FileWatcher.Change) if (DevData.FileWatcher.Change)
{ {
DevData.FileWatcher.Change = false; DevData.FileWatcher.Change = false;
ShutdownFunc(); ShutdownFunc();
ReloadDLL(); ReloadDLL();
StartupFunc(nullptr); StartupFunc(Shared);
} }
UpdateFunc(); UpdateFunc();

33
src/game/Global.cpp Normal file
View File

@@ -0,0 +1,33 @@
#include "Global.h"
#include <cassert>
namespace
{
SharedData* SharedInstance = nullptr;
Game::GameInstance* GameInst = nullptr;
}
namespace Game
{
SharedData& GetShared()
{
assert(SharedInstance != nullptr);
return *SharedInstance;
}
void SetShared(SharedData& instance)
{
SharedInstance = &instance;
}
GameInstance& GetInstance()
{
assert(GameInst != nullptr);
return *GameInst;
}
void SetInstance(Game::GameInstance& instance)
{
GameInst = &instance;
}
}

20
src/game/Global.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
struct SharedData;
namespace Game
{
struct GameInstance
{
bool IsInitialized = false;
uint64_t Size = sizeof(GameInstance);
uint32_t FrameCounter = 0;
int64_t StartTime = 0;
};
SharedData& GetShared();
void SetShared(SharedData& instance);
GameInstance& GetInstance();
void SetInstance(GameInstance& instance);
}

View File

@@ -1,9 +1,7 @@
#include "Setup.h" #include "Setup.h"
#include "Log.h" #include "Log.h"
#include "SDL3/SDL_video.h"
#include "rendering/Rendering.h" #include "rendering/Rendering.h"
#include <cstdint> #include "Global.h"
#include <SDL3/SDL.h>
namespace Game namespace Game
{ {
@@ -11,59 +9,37 @@ namespace Game
{ {
public: public:
GameRendering Rendering; GameRendering Rendering;
int32_t FrameCounter = 0;
}; };
namespace namespace
{ {
GameSetup Instance; GameSetup SetupInstance;
} }
void Setup(void* window) void Setup(SharedData& shared)
{ {
Log("Game Setup Start!"); Log("Game Setup Start!");
if (!SDL_Init(SDL_INIT_VIDEO))
GameInstance& instance = *reinterpret_cast<GameInstance*>(&shared.Game.PermanentStorage);
if (sizeof(GameInstance) != instance.Size)
{ {
Log("Failed to init SDL!"); Log("Game instance size changed, resetting!");
return; instance = {};
} }
SDL_Window* sdlWindow = SDL_CreateWindow("SDL", 1920, 1080, SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL); SetShared(shared);
if (sdlWindow == nullptr) SetInstance(instance);
{ SetupInstance.Rendering.Setup();
Log("Failed to init SDL Window!");
return;
}
auto props = SDL_GetWindowProperties(sdlWindow);
SDL_EnumerateProperties(props, [](void*, SDL_PropertiesID id, const char* name)
{
Log("Prop: %s", name);
}, nullptr);
void* hwnd = SDL_GetPointerProperty(props, "SDL.window.win32.hwnd", nullptr);
if (hwnd == nullptr)
{
Log("Failed to get window pointer!");
return;
}
Instance.Rendering.Setup(hwnd);
} }
void Update() void Update()
{ {
SDL_Event evt; ++GetInstance().FrameCounter;
while (SDL_PollEvent(&evt)) {} SetupInstance.Rendering.Update();
++Instance.FrameCounter;
if (Instance.FrameCounter % 100 == 0)
{
Log("Frame!");
}
Instance.Rendering.Update();
} }
void Shutdown() void Shutdown()
{ {
Log("Shutdown"); Log("Shutdown");
Instance.Rendering.Shutdown(); SetupInstance.Rendering.Shutdown();
} }
} }

View File

@@ -1,10 +1,12 @@
#pragma once #pragma once
#include "../engine/Shared.h"
#define DLLEXPORT __declspec(dllexport) #define DLLEXPORT __declspec(dllexport)
namespace Game namespace Game
{ {
DLLEXPORT void Setup(void* window); DLLEXPORT void Setup(SharedData& shared);
DLLEXPORT void Update(); DLLEXPORT void Update();
DLLEXPORT void Shutdown(); DLLEXPORT void Shutdown();
} }

View File

@@ -1,7 +1,8 @@
#include "Rendering.h" #include "Rendering.h"
#include "../Log.h" #include "../Log.h"
#include "../../engine/Shared.h"
#include "../Global.h"
#include "bgfx/defines.h" #include "bgfx/defines.h"
#include "bgfx/platform.h"
#include "bx/timer.h" #include "bx/timer.h"
#include <bx/file.h> #include <bx/file.h>
#include <bgfx/bgfx.h> #include <bgfx/bgfx.h>
@@ -46,7 +47,6 @@ namespace Game
namespace namespace
{ {
static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath) static const bgfx::Memory* loadMem(bx::FileReaderI* _reader, const bx::FilePath& _filePath)
{ {
if (bx::open(_reader, _filePath)) if (bx::open(_reader, _filePath))
@@ -68,6 +68,9 @@ namespace Game
const char* shaderPath = "???"; const char* shaderPath = "???";
switch (bgfx::getRendererType()) { switch (bgfx::getRendererType()) {
case bgfx::RendererType::Agc:
case bgfx::RendererType::Nvn:
case bgfx::RendererType::Count:
case bgfx::RendererType::Noop: break; case bgfx::RendererType::Noop: break;
case bgfx::RendererType::Direct3D11: case bgfx::RendererType::Direct3D11:
case bgfx::RendererType::Direct3D12: shaderPath = "game/compiled-shaders/dx11/"; break; case bgfx::RendererType::Direct3D12: shaderPath = "game/compiled-shaders/dx11/"; break;
@@ -99,20 +102,24 @@ namespace Game
} }
} }
void GameRendering::Setup(void* window) void GameRendering::Setup()
{ {
Log("Game rendering setup..."); Log("Game rendering setup...");
bgfx::renderFrame(); SharedData& shared = GetShared();
bgfx::Init init; bgfx::Init init;
init.type = bgfx::RendererType::Direct3D12; init.type = bgfx::RendererType::Direct3D12;
init.debug = true; init.debug = true;
init.callback = &Callback; init.callback = &Callback;
init.platformData.nwh = window; init.platformData.nwh = shared.Window.Handle;
init.platformData.ndt = nullptr; init.platformData.ndt = nullptr;
init.platformData.type = bgfx::NativeWindowHandleType::Default; init.platformData.type = bgfx::NativeWindowHandleType::Default;
init.resolution.width = State.WindowWidth; init.resolution.width = shared.Window.WindowWidth;
init.resolution.height = State.WindowHeight; init.resolution.height = shared.Window.WindowHeight;
init.resolution.reset = BGFX_RESET_VSYNC; init.resolution.reset = BGFX_RESET_VSYNC;
Log("%i by %i", init.resolution.width, init.resolution.height);
if (!bgfx::init(init)) if (!bgfx::init(init))
{ {
Log("BGFX setup failed!"); Log("BGFX setup failed!");
@@ -134,12 +141,19 @@ namespace Game
bgfx::ShaderHandle vertexShader = loadShader("vert"); bgfx::ShaderHandle vertexShader = loadShader("vert");
bgfx::ShaderHandle fragmentShader = loadShader("frag"); bgfx::ShaderHandle fragmentShader = loadShader("frag");
Shader = bgfx::createProgram(vertexShader, fragmentShader, true); Shader = bgfx::createProgram(vertexShader, fragmentShader, true);
State.StartTime = bx::getHPCounter();
if (!GetInstance().IsInitialized)
{
GetInstance().IsInitialized = true;
GetInstance().StartTime = bx::getHPCounter();
}
} }
void GameRendering::Update() void GameRendering::Update()
{ {
float time = (float)((bx::getHPCounter() - State.StartTime) / double(bx::getHPFrequency())); SharedData& shared = GetShared();
int64_t tickDelta = bx::getHPCounter() - GetInstance().StartTime;
double time = tickDelta / double(bx::getHPFrequency());
const bx::Vec3 at = { 0.0f, 0.0f, 0.0f }; const bx::Vec3 at = { 0.0f, 0.0f, 0.0f };
const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f }; const bx::Vec3 eye = { 0.0f, 0.0f, -35.0f };
@@ -150,11 +164,11 @@ namespace Game
bx::mtxLookAt(view, eye, at); bx::mtxLookAt(view, eye, at);
float proj[16]; float proj[16];
bx::mtxProj(proj, 60.0f, float(State.WindowWidth) / float(State.WindowHeight), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth); bx::mtxProj(proj, 60.0f, float(shared.Window.WindowWidth) / float(shared.Window.WindowHeight), 0.1f, 100.0f, bgfx::getCaps()->homogeneousDepth);
bgfx::setViewTransform(0, view, proj); bgfx::setViewTransform(0, view, proj);
// Set view 0 default viewport. // Set view 0 default viewport.
bgfx::setViewRect(0, 0, 0, State.WindowWidth, State.WindowHeight); bgfx::setViewRect(0, 0, 0, shared.Window.WindowWidth, shared.Window.WindowHeight);
} }
// This dummy draw call is here to make sure that view 0 is cleared // This dummy draw call is here to make sure that view 0 is cleared
@@ -163,15 +177,12 @@ namespace Game
bgfx::IndexBufferHandle ibh = IndexBuffer; bgfx::IndexBufferHandle ibh = IndexBuffer;
uint64_t state = 0 uint64_t state = 0
| (true ? BGFX_STATE_WRITE_R : 0) | BGFX_STATE_WRITE_RGB
| (true ? BGFX_STATE_WRITE_G : 0) | BGFX_STATE_WRITE_A
| (true ? BGFX_STATE_WRITE_B : 0)
| (true ? BGFX_STATE_WRITE_A : 0)
| BGFX_STATE_WRITE_Z | BGFX_STATE_WRITE_Z
| BGFX_STATE_DEPTH_TEST_LESS | BGFX_STATE_DEPTH_TEST_LESS
| BGFX_STATE_CULL_CW | BGFX_STATE_CULL_CW
| BGFX_STATE_MSAA | BGFX_STATE_MSAA
| 0
; ;
// Submit 11x11 cubes. // Submit 11x11 cubes.
@@ -200,10 +211,9 @@ namespace Game
} }
} }
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %f", time); bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1f", time);
bgfx::dbgTextPrintf(1, 2, 0x0f, "Frame: %u", GetInstance().FrameCounter);
// Advance to next frame. Rendering thread will be kicked to
// process submitted rendering primitives.
bgfx::frame(); bgfx::frame();
} }

View File

@@ -79,9 +79,6 @@ namespace Game
struct RenderState struct RenderState
{ {
int64_t StartTime = 0;
uint32_t WindowWidth = 1920;
uint32_t WindowHeight = 1080;
}; };
class GameRendering class GameRendering
@@ -94,7 +91,7 @@ namespace Game
BgfxCallback Callback; BgfxCallback Callback;
RenderState State; RenderState State;
public: public:
void Setup(void* window); void Setup();
void Update(); void Update();
void Shutdown(); void Shutdown();
}; };

View File

@@ -1,4 +0,0 @@
cd dependency/bgfx.cmake
cmake -G "Visual Studio 17 2022" -S . -B cmake-build-vs -DBGFX_BUILD_TOOLS=OFF -DBGFX_BUILD_EXAMPLES=OFF
cd ..\..
cmake -G "Visual Studio 17 2022" -S . -B cmake-build-vs -DCMAKE_EXPORT_COMPILE_COMMANDS=ON