diff --git a/scwx-qt/source/scwx/qt/gl/gl_context.cpp b/scwx-qt/source/scwx/qt/gl/gl_context.cpp index e09ed177..2b996c54 100644 --- a/scwx-qt/source/scwx/qt/gl/gl_context.cpp +++ b/scwx-qt/source/scwx/qt/gl/gl_context.cpp @@ -1,8 +1,9 @@ #include #include -#include #include +#include + namespace scwx { namespace qt @@ -25,11 +26,12 @@ public: } ~Impl() {} + static std::size_t + GetShaderKey(std::initializer_list> shaders); + gl::OpenGLFunctions gl_; - std::unordered_map, - std::shared_ptr, - scwx::util::hash>> + std::unordered_map> shaderProgramMap_; std::mutex shaderProgramMutex_; @@ -54,8 +56,15 @@ std::shared_ptr GlContext::GetShaderProgram(const std::string& vertexPath, const std::string& fragmentPath) { - const std::pair key {vertexPath, fragmentPath}; - std::shared_ptr shaderProgram; + return GetShaderProgram( + {{GL_VERTEX_SHADER, vertexPath}, {GL_FRAGMENT_SHADER, fragmentPath}}); +} + +std::shared_ptr GlContext::GetShaderProgram( + std::initializer_list> shaders) +{ + const auto key = Impl::GetShaderKey(shaders); + std::shared_ptr shaderProgram; std::unique_lock lock(p->shaderProgramMutex_); @@ -64,7 +73,7 @@ GlContext::GetShaderProgram(const std::string& vertexPath, if (it == p->shaderProgramMap_.end()) { shaderProgram = std::make_shared(p->gl_); - shaderProgram->Load(vertexPath, fragmentPath); + shaderProgram->Load(shaders); p->shaderProgramMap_[key] = shaderProgram; } else @@ -91,6 +100,18 @@ GLuint GlContext::GetTextureAtlas() return p->textureAtlas_; } +std::size_t GlContext::Impl::GetShaderKey( + std::initializer_list> shaders) +{ + std::size_t seed = 0; + for (auto& shader : shaders) + { + boost::hash_combine(seed, shader.first); + boost::hash_combine(seed, shader.second); + } + return seed; +} + } // namespace gl } // namespace qt } // namespace scwx diff --git a/scwx-qt/source/scwx/qt/gl/gl_context.hpp b/scwx-qt/source/scwx/qt/gl/gl_context.hpp index 623b5855..5c53de59 100644 --- a/scwx-qt/source/scwx/qt/gl/gl_context.hpp +++ b/scwx-qt/source/scwx/qt/gl/gl_context.hpp @@ -27,6 +27,8 @@ public: std::shared_ptr GetShaderProgram(const std::string& vertexPath, const std::string& fragmentPath); + std::shared_ptr GetShaderProgram( + std::initializer_list> shaders); GLuint GetTextureAtlas(); diff --git a/scwx-qt/source/scwx/qt/gl/shader_program.cpp b/scwx-qt/source/scwx/qt/gl/shader_program.cpp index f27c4a1b..54db55fc 100644 --- a/scwx-qt/source/scwx/qt/gl/shader_program.cpp +++ b/scwx-qt/source/scwx/qt/gl/shader_program.cpp @@ -109,7 +109,7 @@ bool ShaderProgram::Load( std::string shaderSource = shaderStream.readAll().toStdString(); const char* shaderSourceC = shaderSource.c_str(); - // Create a vertex shader + // Create a shader GLuint shaderId = gl.glCreateShader(shader.first); shaderIds.push_back(shaderId);