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>
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<AFileModificationListener*>::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);
}
}

View File

@ -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;

View File

@ -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);
}

View File

@ -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");

View File

@ -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
//

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 "FileModificationObserver.h"
#include <vector>
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,