Prepared reloading of shaders

simple_math_single_header
Martin Felis 2018-03-10 08:48:41 +01:00
parent e9c7a66d42
commit c1a82427c0
6 changed files with 45 additions and 11 deletions

View File

@ -4,7 +4,7 @@
#include<vector> #include<vector>
struct AFileModificationListener { struct AFileModificationListener {
virtual bool OnTrigger() = 0; virtual bool OnFileChanged(const std::string& filename) = 0;
}; };
struct FileModificationObserver { struct FileModificationObserver {
@ -26,6 +26,7 @@ struct FileModificationObserver {
if (iter == mObserverInfos.end()) { if (iter == mObserverInfos.end()) {
FileObserverInfo observer_info; FileObserverInfo observer_info;
observer_info.mListeners.push_back(listener); observer_info.mListeners.push_back(listener);
UpdateFileInfo(filename, observer_info);
mObserverInfos[filename] = observer_info; mObserverInfos[filename] = observer_info;
} else { } else {
iter->second.mListeners.push_back(listener); iter->second.mListeners.push_back(listener);
@ -40,14 +41,14 @@ struct FileModificationObserver {
std::vector<AFileModificationListener*>::iterator listener_iter = iter->second.mListeners.begin(); std::vector<AFileModificationListener*>::iterator listener_iter = iter->second.mListeners.begin();
while (listener_iter != iter->second.mListeners.end()) { while (listener_iter != iter->second.mListeners.end()) {
if ((*listener_iter)->OnTrigger()) if (!(*listener_iter)->OnFileChanged(filename))
break; break;
listener_iter++; listener_iter++;
} }
} }
void CheckFileModification(const std::string& filename, FileObserverInfo& observer_info) { bool UpdateFileInfo(const std::string& filename, FileObserverInfo& observer_info) {
struct stat attr; struct stat attr;
bool stat_result = stat(filename.c_str(), &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.mFileMTimeNSec = attr.st_mtim.tv_nsec;
observer_info.mFileSize = attr.st_size; observer_info.mFileSize = attr.st_size;
gLog ("Detected file change of %s: new size %d", return true;
filename.c_str(), attr.st_size); }
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); Trigger(filename);
} }
} }

View File

@ -3,9 +3,6 @@
#include "math_types.h" #include "math_types.h"
#include "Utils.h" #include "Utils.h"
#define USE_DOCKS 1
struct Timer; struct Timer;
extern Timer* gTimer; extern Timer* gTimer;
@ -26,3 +23,6 @@ extern ReadSerializer* gReadSerializer;
struct GuiInputState; struct GuiInputState;
extern GuiInputState* gGuiInputState; extern GuiInputState* gGuiInputState;
struct FileModificationObserver;
extern FileModificationObserver* gFileModificationObserver;

View File

@ -18,6 +18,7 @@
#include "imgui_dock.h" #include "imgui_dock.h"
#include "imgui_impl_glfw_gl3.h" #include "imgui_impl_glfw_gl3.h"
#include "FileModificationObserver.h"
#include "Serializer.h" #include "Serializer.h"
Timer* gTimer = nullptr; Timer* gTimer = nullptr;
@ -27,6 +28,7 @@ RuntimeModuleManager* gModuleManager = nullptr;
WriteSerializer* gWriteSerializer = nullptr; WriteSerializer* gWriteSerializer = nullptr;
ReadSerializer* gReadSerializer = nullptr; ReadSerializer* gReadSerializer = nullptr;
GuiInputState* gGuiInputState = nullptr; GuiInputState* gGuiInputState = nullptr;
FileModificationObserver* gFileModificationObserver = nullptr;
double gTimeAtStart = 0; double gTimeAtStart = 0;
double mouse_scroll_x = 0.; double mouse_scroll_x = 0.;
@ -143,6 +145,10 @@ int main(void)
ImGui_ImplGlfwGL3_Init(gWindow, true); ImGui_ImplGlfwGL3_Init(gWindow, true);
ImGui::LoadDock(); ImGui::LoadDock();
// FileModificationObserver
FileModificationObserver file_modification_observer;
gFileModificationObserver = &file_modification_observer;
// Timer // Timer
Timer timer; Timer timer;
gTimer = &timer; gTimer = &timer;
@ -227,6 +233,10 @@ int main(void)
usleep(16000); usleep(16000);
if (glfwGetKey(gWindow, GLFW_KEY_F5) == GLFW_PRESS) {
gFileModificationObserver->Update();
}
glfwSwapBuffers(gWindow); glfwSwapBuffers(gWindow);
} }

View File

@ -312,6 +312,7 @@ void Renderer::Initialize(int width, int height) {
// Simple Shader // Simple Shader
mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl"); mDefaultProgram = RenderProgram("data/shaders/vs_simple.glsl", "data/shaders/fs_simple.glsl");
bool load_result = mDefaultProgram.Load(); bool load_result = mDefaultProgram.Load();
mDefaultProgram.RegisterFileModification();
assert(load_result); assert(load_result);
muDefaultModelViewProjection = mDefaultProgram.GetUniformLocation("uModelViewProj"); muDefaultModelViewProjection = mDefaultProgram.GetUniformLocation("uModelViewProj");
muDefaultColor = mDefaultProgram.GetUniformLocation("uColor"); muDefaultColor = mDefaultProgram.GetUniformLocation("uColor");
@ -333,6 +334,7 @@ void Renderer::Initialize(int width, int height) {
// Program for color texture rendering // Program for color texture rendering
mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl"); mRenderQuadProgramColor = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_simpletexture.glsl");
load_result = mRenderQuadProgramColor.Load(); load_result = mRenderQuadProgramColor.Load();
mRenderQuadProgramColor.RegisterFileModification();
assert(load_result); assert(load_result);
muRenderQuadModelViewProj = mRenderQuadProgramColor.GetUniformLocation("uModelViewProj"); muRenderQuadModelViewProj = mRenderQuadProgramColor.GetUniformLocation("uModelViewProj");
@ -342,6 +344,7 @@ void Renderer::Initialize(int width, int height) {
// Program for depth texture rendering // Program for depth texture rendering
mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl"); mRenderQuadProgramDepth = RenderProgram("data/shaders/vs_passthrough.glsl", "data/shaders/fs_depthbuffer.glsl");
load_result = mRenderQuadProgramDepth.Load(); load_result = mRenderQuadProgramDepth.Load();
mRenderQuadProgramDepth.RegisterFileModification();
assert(load_result); assert(load_result);
muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj"); muRenderQuadDepthModelViewProj = mRenderQuadProgramDepth.GetUniformLocation( "uModelViewProj");
muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear"); muRenderQuadDepthNear = mRenderQuadProgramDepth.GetUniformLocation("uNear");

View File

@ -7,6 +7,7 @@
#include "RenderModule.h" #include "RenderModule.h"
#include "RenderUtils.h" #include "RenderUtils.h"
#include "Globals.h" #include "Globals.h"
#include "FileModificationObserver.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
@ -140,6 +141,16 @@ GLuint RenderProgram::GetUniformLocation(const std::string& name) {
return result; 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 // RenderTarget
// //

View File

@ -5,6 +5,8 @@
#include <GL/gl3w.h> // 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 <GL/gl3w.h> // 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 <vector> #include <vector>
struct Transform { struct Transform {
@ -127,7 +129,7 @@ struct Transform {
} }
}; };
struct RenderProgram { struct RenderProgram : AFileModificationListener {
std::string mVertexShaderFilename; std::string mVertexShaderFilename;
std::string mFragmentShaderFilename; std::string mFragmentShaderFilename;
@ -146,6 +148,9 @@ struct RenderProgram {
~RenderProgram(); ~RenderProgram();
bool Load(); bool Load();
void RegisterFileModification();
virtual bool OnFileChanged(const std::string& filename);
}; };
struct RenderSettings; struct RenderSettings;
@ -244,8 +249,6 @@ struct VertexArray {
b(255), b(255),
a(255) {} a(255) {}
VertexData( VertexData(
float x, float x,
float y, float y,