From c1a82427c02c98a0245e9e567c2a83cd04b24564 Mon Sep 17 00:00:00 2001 From: Martin Felis Date: Sat, 10 Mar 2018 08:48:41 +0100 Subject: [PATCH] Prepared reloading of shaders --- src/FileModificationObserver.h | 17 ++++++++++++----- src/Globals.h | 6 +++--- src/main.cc | 10 ++++++++++ src/modules/RenderModule.cc | 3 +++ src/modules/RenderUtils.cc | 11 +++++++++++ src/modules/RenderUtils.h | 9 ++++++--- 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/FileModificationObserver.h b/src/FileModificationObserver.h index 4e3ad53..463d0a1 100644 --- a/src/FileModificationObserver.h +++ b/src/FileModificationObserver.h @@ -4,7 +4,7 @@ #include struct AFileModificationListener { - virtual bool OnTrigger() = 0; + virtual bool OnFileChanged(const std::string& filename) = 0; }; struct FileModificationObserver { @@ -26,6 +26,7 @@ struct FileModificationObserver { if (iter == mObserverInfos.end()) { FileObserverInfo observer_info; observer_info.mListeners.push_back(listener); + UpdateFileInfo(filename, observer_info); mObserverInfos[filename] = observer_info; } else { iter->second.mListeners.push_back(listener); @@ -40,14 +41,14 @@ struct FileModificationObserver { std::vector::iterator listener_iter = iter->second.mListeners.begin(); while (listener_iter != iter->second.mListeners.end()) { - if ((*listener_iter)->OnTrigger()) + if (!(*listener_iter)->OnFileChanged(filename)) break; listener_iter++; } } - void CheckFileModification(const std::string& filename, FileObserverInfo& observer_info) { + bool UpdateFileInfo(const std::string& filename, FileObserverInfo& observer_info) { struct stat attr; bool stat_result = stat(filename.c_str(), &attr); @@ -67,9 +68,15 @@ struct FileModificationObserver { observer_info.mFileMTimeNSec = attr.st_mtim.tv_nsec; observer_info.mFileSize = attr.st_size; - gLog ("Detected file change of %s: new size %d", - filename.c_str(), attr.st_size); + return true; + } + return false; + } + void CheckFileModification(const std::string& filename, FileObserverInfo& observer_info) { + if (UpdateFileInfo(filename, observer_info)) { + gLog ("Detected file change of %s: new size %d", + filename.c_str(), observer_info.mFileSize); Trigger(filename); } } diff --git a/src/Globals.h b/src/Globals.h index d11d734..3e61165 100644 --- a/src/Globals.h +++ b/src/Globals.h @@ -3,9 +3,6 @@ #include "math_types.h" #include "Utils.h" -#define USE_DOCKS 1 - - struct Timer; extern Timer* gTimer; @@ -26,3 +23,6 @@ extern ReadSerializer* gReadSerializer; struct GuiInputState; extern GuiInputState* gGuiInputState; + +struct FileModificationObserver; +extern FileModificationObserver* gFileModificationObserver; diff --git a/src/main.cc b/src/main.cc index ecf1f0c..c240e58 100644 --- a/src/main.cc +++ b/src/main.cc @@ -18,6 +18,7 @@ #include "imgui_dock.h" #include "imgui_impl_glfw_gl3.h" +#include "FileModificationObserver.h" #include "Serializer.h" Timer* gTimer = nullptr; @@ -27,6 +28,7 @@ RuntimeModuleManager* gModuleManager = nullptr; WriteSerializer* gWriteSerializer = nullptr; ReadSerializer* gReadSerializer = nullptr; GuiInputState* gGuiInputState = nullptr; +FileModificationObserver* gFileModificationObserver = nullptr; double gTimeAtStart = 0; double mouse_scroll_x = 0.; @@ -143,6 +145,10 @@ int main(void) ImGui_ImplGlfwGL3_Init(gWindow, true); ImGui::LoadDock(); + // FileModificationObserver + FileModificationObserver file_modification_observer; + gFileModificationObserver = &file_modification_observer; + // Timer Timer timer; gTimer = &timer; @@ -227,6 +233,10 @@ int main(void) usleep(16000); + if (glfwGetKey(gWindow, GLFW_KEY_F5) == GLFW_PRESS) { + gFileModificationObserver->Update(); + } + glfwSwapBuffers(gWindow); } diff --git a/src/modules/RenderModule.cc b/src/modules/RenderModule.cc index ce490be..d7a6bb2 100644 --- a/src/modules/RenderModule.cc +++ b/src/modules/RenderModule.cc @@ -312,6 +312,7 @@ void Renderer::Initialize(int width, int height) { // Simple Shader mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); bool load_result = mDefaultProgram.Load(); + mDefaultProgram.RegisterFileModification(); assert(load_result); muDefaultModelViewProjection = mDefaultProgram.GetUniformLocation("uModelViewProj"); muDefaultColor = mDefaultProgram.GetUniformLocation("uColor"); @@ -333,6 +334,7 @@ void Renderer::Initialize(int width, int height) { // Program for color texture rendering mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl"); load_result = mRenderQuadProgramColor.Load(); + mRenderQuadProgramColor.RegisterFileModification(); assert(load_result); muRenderQuadModelViewProj = mRenderQuadProgramColor.GetUniformLocation("uModelViewProj"); @@ -342,6 +344,7 @@ void Renderer::Initialize(int width, int height) { // Program for depth texture rendering mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl"); load_result = mRenderQuadProgramDepth.Load(); + mRenderQuadProgramDepth.RegisterFileModification(); assert(load_result); muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj"); muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear"); diff --git a/src/modules/RenderUtils.cc b/src/modules/RenderUtils.cc index 8df695b..e5d997f 100644 --- a/src/modules/RenderUtils.cc +++ b/src/modules/RenderUtils.cc @@ -7,6 +7,7 @@ #include "RenderModule.h" #include "RenderUtils.h" #include "Globals.h" +#include "FileModificationObserver.h" #define STB_IMAGE_IMPLEMENTATION @@ -140,6 +141,16 @@ GLuint RenderProgram::GetUniformLocation(const std::string& name) { return result; } +void RenderProgram::RegisterFileModification() { + gFileModificationObserver->AddListener(mVertexShaderFilename, this); + gFileModificationObserver->AddListener(mFragmentShaderFilename, this); +} + +bool RenderProgram::OnFileChanged(const std::string& filename) { + gLog("Renderprogram reload as file %s changed", filename.c_str()); + return true; +} + // // RenderTarget // diff --git a/src/modules/RenderUtils.h b/src/modules/RenderUtils.h index d60449b..b139578 100644 --- a/src/modules/RenderUtils.h +++ b/src/modules/RenderUtils.h @@ -5,6 +5,8 @@ #include // This example is using gl3w to access OpenGL functions (because it is small). You may use glew/glad/glLoadGen/etc. whatever already works for you. +#include "FileModificationObserver.h" + #include struct Transform { @@ -127,7 +129,7 @@ struct Transform { } }; -struct RenderProgram { +struct RenderProgram : AFileModificationListener { std::string mVertexShaderFilename; std::string mFragmentShaderFilename; @@ -146,6 +148,9 @@ struct RenderProgram { ~RenderProgram(); bool Load(); + + void RegisterFileModification(); + virtual bool OnFileChanged(const std::string& filename); }; struct RenderSettings; @@ -244,8 +249,6 @@ struct VertexArray { b(255), a(255) {} - - VertexData( float x, float y,