hot reload setup
This commit is contained in:
3
src/.clang-format
Normal file
3
src/.clang-format
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
BasedOnStyle: Microsoft
|
||||||
|
IndentWidth: 4
|
||||||
|
NamespaceIndentation: NI_All
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <bx/spscqueue.h>
|
|
||||||
|
|
||||||
struct SharedWindowData
|
struct SharedWindowData
|
||||||
{
|
{
|
||||||
@@ -16,7 +15,8 @@ struct FileChangeNotification
|
|||||||
|
|
||||||
struct SharedDevData
|
struct SharedDevData
|
||||||
{
|
{
|
||||||
bx::SpScUnboundedQueueT<FileChangeNotification>* ShaderChangeQueue;
|
uint32_t ChangedShaderCount = 0;
|
||||||
|
FileChangeNotification ChangedShaders[16];
|
||||||
};
|
};
|
||||||
|
|
||||||
struct GameData
|
struct GameData
|
||||||
|
|||||||
@@ -51,3 +51,8 @@ void EngineWindow::Update(SharedWindowData& Shared)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EngineWindow::Shutdown()
|
||||||
|
{
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cwchar>
|
#include <cwchar>
|
||||||
#include <fileapi.h>
|
#include <fileapi.h>
|
||||||
|
#include <synchapi.h>
|
||||||
#define WIN32_LEAN_AND_MEAN
|
#define WIN32_LEAN_AND_MEAN
|
||||||
#include <Windows.h>
|
#include <Windows.h>
|
||||||
#undef min
|
#undef min
|
||||||
@@ -8,7 +9,7 @@
|
|||||||
|
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <bx/string.h>
|
#include <bx/string.h>
|
||||||
#include <bx/spscqueue.h>
|
#include <bx/mpscqueue.h>
|
||||||
#include <bx/allocator.h>
|
#include <bx/allocator.h>
|
||||||
|
|
||||||
#include "Shared.h"
|
#include "Shared.h"
|
||||||
@@ -23,7 +24,7 @@ constexpr const char* DLLPath = "PuzGame.dll";
|
|||||||
constexpr const wchar_t* DLLWatch = L"PuzGame2.dll";
|
constexpr const wchar_t* DLLWatch = L"PuzGame2.dll";
|
||||||
#else
|
#else
|
||||||
constexpr const char* DLLPath = "libPuzGame.dll";
|
constexpr const char* DLLPath = "libPuzGame.dll";
|
||||||
constexpr const wchar_t* DLLWatch = L"cmake-build\\libPuzGame2.dll";
|
constexpr const wchar_t* DLLWatch = L"libPuzGame2.dll";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace
|
namespace
|
||||||
@@ -38,19 +39,26 @@ enum class FileChangeType
|
|||||||
CompiledShader,
|
CompiledShader,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct FileWatcherStartup
|
||||||
|
{
|
||||||
|
char DirPath[512]{0};
|
||||||
|
wchar_t CompName[512]{0};
|
||||||
|
FileChangeType ChangeType = FileChangeType::Shader;
|
||||||
|
bool Shutdown = false;
|
||||||
|
};
|
||||||
|
|
||||||
struct FileWatcherData
|
struct FileWatcherData
|
||||||
{
|
{
|
||||||
bx::SpScUnboundedQueueT<FileChangeNotification> ShaderQueue{defaultAllocator};
|
FileWatcherStartup DLLWatcher;
|
||||||
bx::SpScUnboundedQueueT<FileChangeNotification> DLLQueue{defaultAllocator};
|
FileWatcherStartup ShaderWatcher;
|
||||||
|
FileWatcherStartup CompiledShaderWatcher;
|
||||||
|
bx::MpScUnboundedQueueT<FileChangeNotification> ShaderQueue{defaultAllocator};
|
||||||
|
bx::MpScUnboundedQueueT<FileChangeNotification> DLLQueue{defaultAllocator};
|
||||||
};
|
};
|
||||||
|
|
||||||
struct DevelopmentData
|
struct DevelopmentData
|
||||||
{
|
{
|
||||||
uint8_t FileChangeBuffer[1024]{ 0 };
|
|
||||||
HMODULE GameLib = NULL;
|
HMODULE GameLib = NULL;
|
||||||
HANDLE DevDir = NULL;
|
|
||||||
HANDLE ShaderDir = NULL;
|
|
||||||
HANDLE CompiledShaderDir = NULL;
|
|
||||||
FileWatcherData FileWatcher;
|
FileWatcherData FileWatcher;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -71,18 +79,20 @@ namespace
|
|||||||
|
|
||||||
void FileChangeCheck(HANDLE dirHandle, FileChangeType changeType, const wchar_t* compName = nullptr)
|
void FileChangeCheck(HANDLE dirHandle, FileChangeType changeType, const wchar_t* compName = nullptr)
|
||||||
{
|
{
|
||||||
|
if (dirHandle == NULL) return;
|
||||||
|
|
||||||
|
uint8_t fileChangeBuffer[1024]{ 0 };
|
||||||
|
bx::memSet(fileChangeBuffer, 0, sizeof(fileChangeBuffer));
|
||||||
DWORD bytesReturned = 0;
|
DWORD bytesReturned = 0;
|
||||||
bx::memSet(DevData.FileChangeBuffer, 0, sizeof(DevData.FileChangeBuffer));
|
if (ReadDirectoryChangesW(dirHandle, fileChangeBuffer, sizeof(fileChangeBuffer), true, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE, &bytesReturned, NULL, NULL))
|
||||||
if (ReadDirectoryChangesW(dirHandle, DevData.FileChangeBuffer, sizeof(DevData.FileChangeBuffer), true, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_SIZE, &bytesReturned, NULL, NULL))
|
|
||||||
{
|
{
|
||||||
uint32_t offset = 0;
|
uint32_t offset = 0;
|
||||||
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*>(&fileChangeBuffer[offset]);
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
if (compName == nullptr || wcscmp(notifyData->FileName, compName) == 0)
|
if (compName == nullptr || compName[0] == 0 || wcscmp(notifyData->FileName, compName) == 0)
|
||||||
{
|
{
|
||||||
FileChangeNotification notif;
|
FileChangeNotification notif;
|
||||||
wcscpy(notif.FileName, notifyData->FileName);
|
wcscpy(notif.FileName, notifyData->FileName);
|
||||||
@@ -92,14 +102,13 @@ void FileChangeCheck(HANDLE dirHandle, FileChangeType changeType, const wchar_t*
|
|||||||
}
|
}
|
||||||
else if (changeType == FileChangeType::Shader)
|
else if (changeType == FileChangeType::Shader)
|
||||||
{
|
{
|
||||||
std::system("../shadercompile.bat");
|
std::system("shadercompile.bat");
|
||||||
}
|
}
|
||||||
else if (changeType == FileChangeType::CompiledShader)
|
else if (changeType == FileChangeType::CompiledShader)
|
||||||
{
|
{
|
||||||
DevData.FileWatcher.ShaderQueue.push(¬if);
|
DevData.FileWatcher.ShaderQueue.push(¬if);
|
||||||
}
|
}
|
||||||
printf("detected file change of type %u!\n", changeType);
|
printf("detected file change of type %u!\n", changeType);
|
||||||
wprintf(L"%s\n", notifyData->FileName);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,14 +118,42 @@ void FileChangeCheck(HANDLE dirHandle, FileChangeType changeType, const wchar_t*
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HANDLE LoadDirHandle(const char* name)
|
||||||
|
{
|
||||||
|
HANDLE h = CreateFileA(name,
|
||||||
|
FILE_LIST_DIRECTORY,
|
||||||
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||||
|
NULL,
|
||||||
|
OPEN_EXISTING,
|
||||||
|
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
|
||||||
|
NULL);
|
||||||
|
|
||||||
|
if (h == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to load dir handle for %s", name);
|
||||||
|
}
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
|
||||||
unsigned long FileWatcherThread(void* data)
|
unsigned long FileWatcherThread(void* data)
|
||||||
{
|
{
|
||||||
bool isRunning = DevData.DevDir;
|
FileWatcherStartup* startupData = reinterpret_cast<FileWatcherStartup*>(data);
|
||||||
while (isRunning)
|
printf("Starting file watcher of type %u\n", startupData->ChangeType);
|
||||||
|
if (startupData->DirPath[0] == 0)
|
||||||
{
|
{
|
||||||
FileChangeCheck(DevData.DevDir, FileChangeType::DLL, DLLWatch);
|
printf("Invalid file watcher path!\n");
|
||||||
FileChangeCheck(DevData.ShaderDir, FileChangeType::Shader);
|
return 1;
|
||||||
FileChangeCheck(DevData.CompiledShaderDir, FileChangeType::CompiledShader);
|
}
|
||||||
|
HANDLE dirHandle = LoadDirHandle(startupData->DirPath);
|
||||||
|
if (dirHandle == NULL)
|
||||||
|
{
|
||||||
|
printf("Failed to load dir %s for file watcher!\n", startupData->DirPath);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (!startupData->Shutdown)
|
||||||
|
{
|
||||||
|
FileChangeCheck(dirHandle, startupData->ChangeType, startupData->CompName);
|
||||||
}
|
}
|
||||||
printf("File watcher thread ended!\n");
|
printf("File watcher thread ended!\n");
|
||||||
return 0;
|
return 0;
|
||||||
@@ -191,17 +228,6 @@ bool ReloadDLL()
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
HANDLE LoadDirHandle(const char* name)
|
|
||||||
{
|
|
||||||
return CreateFileA(name,
|
|
||||||
FILE_LIST_DIRECTORY,
|
|
||||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
|
||||||
NULL,
|
|
||||||
OPEN_EXISTING,
|
|
||||||
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
|
|
||||||
NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
char PathBuf[512]{ 0 };
|
char PathBuf[512]{ 0 };
|
||||||
@@ -217,13 +243,20 @@ int main()
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Shared.Dev.ShaderChangeQueue = &DevData.FileWatcher.ShaderQueue;
|
DevData.FileWatcher.DLLWatcher.ChangeType = FileChangeType::DLL;
|
||||||
DevData.DevDir = LoadDirHandle(".");
|
DevData.FileWatcher.ShaderWatcher.ChangeType = FileChangeType::Shader;
|
||||||
DevData.ShaderDir = LoadDirHandle("../game/shaders");
|
DevData.FileWatcher.CompiledShaderWatcher.ChangeType = FileChangeType::CompiledShader;
|
||||||
DevData.CompiledShaderDir = LoadDirHandle("../game/compiled-shaders");
|
|
||||||
|
bx::strCopy(DevData.FileWatcher.DLLWatcher.DirPath, sizeof(DevData.FileWatcher.DLLWatcher.DirPath), "cmake-build");
|
||||||
|
bx::strCopy(DevData.FileWatcher.ShaderWatcher.DirPath, sizeof(DevData.FileWatcher.ShaderWatcher.DirPath), "game/shaders");
|
||||||
|
bx::strCopy(DevData.FileWatcher.CompiledShaderWatcher.DirPath, sizeof(DevData.FileWatcher.CompiledShaderWatcher.DirPath), "game/compiled-shaders");
|
||||||
|
|
||||||
|
wcscpy(DevData.FileWatcher.DLLWatcher.CompName, DLLWatch);
|
||||||
|
|
||||||
DWORD fileWatcherThreadId = 0;
|
DWORD fileWatcherThreadId = 0;
|
||||||
CreateThread(NULL, 0, FileWatcherThread, NULL, 0, &fileWatcherThreadId);
|
HANDLE dllThread = CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.DLLWatcher, 0, &fileWatcherThreadId);
|
||||||
|
HANDLE shaderThread = CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.ShaderWatcher, 0, &fileWatcherThreadId);
|
||||||
|
HANDLE compiledShaderThread = CreateThread(NULL, 0, FileWatcherThread, &DevData.FileWatcher.CompiledShaderWatcher, 0, &fileWatcherThreadId);
|
||||||
|
|
||||||
StartupFunc(Shared);
|
StartupFunc(Shared);
|
||||||
|
|
||||||
@@ -245,7 +278,32 @@ int main()
|
|||||||
StartupFunc(Shared);
|
StartupFunc(Shared);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FileChangeNotification* CompiledShaderChange = nullptr;
|
||||||
|
while ((CompiledShaderChange = DevData.FileWatcher.ShaderQueue.pop()))
|
||||||
|
{
|
||||||
|
if (Shared.Dev.ChangedShaderCount < BX_COUNTOF(Shared.Dev.ChangedShaders))
|
||||||
|
{
|
||||||
|
wcscpy(Shared.Dev.ChangedShaders[Shared.Dev.ChangedShaderCount].FileName, CompiledShaderChange->FileName);
|
||||||
|
++Shared.Dev.ChangedShaderCount;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
printf("Ran out of shader change buffer!\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
UpdateFunc();
|
UpdateFunc();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ShutdownFunc();
|
||||||
|
Engine.Window.Shutdown();
|
||||||
|
|
||||||
|
DevData.FileWatcher.DLLWatcher.Shutdown = true;
|
||||||
|
DevData.FileWatcher.ShaderWatcher.Shutdown = true;
|
||||||
|
DevData.FileWatcher.CompiledShaderWatcher.Shutdown = true;
|
||||||
|
WaitForSingleObject(dllThread, 100);
|
||||||
|
WaitForSingleObject(shaderThread, 100);
|
||||||
|
WaitForSingleObject(compiledShaderThread, 100);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -154,11 +154,11 @@ namespace Game
|
|||||||
SharedData& shared = GetShared();
|
SharedData& shared = GetShared();
|
||||||
|
|
||||||
FileChangeNotification* shaderChange = nullptr;
|
FileChangeNotification* shaderChange = nullptr;
|
||||||
while ((shaderChange = shared.Dev.ShaderChangeQueue->pop()))
|
if (shared.Dev.ChangedShaderCount > 0)
|
||||||
{
|
{
|
||||||
|
shared.Dev.ChangedShaderCount = 0;
|
||||||
|
|
||||||
bgfx::destroy(Shader);
|
bgfx::destroy(Shader);
|
||||||
VertexBuffer = bgfx::createVertexBuffer(bgfx::makeRef(cubeVertices, sizeof(cubeVertices)), VertLayout);
|
|
||||||
IndexBuffer = bgfx::createIndexBuffer(bgfx::makeRef(cubeTriList, sizeof(cubeTriList)));
|
|
||||||
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);
|
||||||
@@ -223,7 +223,7 @@ namespace Game
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.2f", time);
|
bgfx::dbgTextPrintf(1, 1, 0x0F, "Time: %.1f", time);
|
||||||
bgfx::dbgTextPrintf(1, 2, 0x0f, "Frame: %u", GetInstance().FrameCounter);
|
bgfx::dbgTextPrintf(1, 2, 0x0f, "Frame: %u", GetInstance().FrameCounter);
|
||||||
|
|
||||||
bgfx::frame();
|
bgfx::frame();
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\vert.sc -o .\game\compiled-shaders\dx11\vert.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type v --platform windows --profile s_5_0
|
.\cmake-build\shaderc.exe -f .\game\shaders\vert.sc -o .\game\compiled-shaders\dx11\vert.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type v --platform windows --profile s_5_0
|
||||||
.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\frag.sc -o .\game\compiled-shaders\dx11\frag.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type f --platform windows --profile s_5_0
|
.\cmake-build\shaderc.exe -f .\game\shaders\frag.sc -o .\game\compiled-shaders\dx11\frag.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type f --platform windows --profile s_5_0
|
||||||
.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\vert.sc -o .\game\compiled-shaders\glsl\vert.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type v --platform windows --profile 430
|
.\cmake-build\shaderc.exe -f .\game\shaders\vert.sc -o .\game\compiled-shaders\glsl\vert.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type v --platform windows --profile 430
|
||||||
.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\frag.sc -o .\game\compiled-shaders\glsl\frag.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type f --platform windows --profile 430
|
.\cmake-build\shaderc.exe -f .\game\shaders\frag.sc -o .\game\compiled-shaders\glsl\frag.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type f --platform windows --profile 430
|
||||||
.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\vert.sc -o .\game\compiled-shaders\spirv\vert.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type v --platform windows --profile spirv16-13
|
.\cmake-build\shaderc.exe -f .\game\shaders\vert.sc -o .\game\compiled-shaders\spirv\vert.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type v --platform windows --profile spirv16-13
|
||||||
.\cmake-build\dependency\bgfx.cmake\cmake\bgfx\shaderc.exe -f .\game\shaders\frag.sc -o .\game\compiled-shaders\spirv\frag.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type f --platform windows --profile spirv16-13
|
.\cmake-build\shaderc.exe -f .\game\shaders\frag.sc -o .\game\compiled-shaders\spirv\frag.bin -i .\dependency\bgfx.cmake\bgfx\src\ --type f --platform windows --profile spirv16-13
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user