Additional support for loading geometry shaders (and other shaders)

This commit is contained in:
Dan Paulat 2023-08-16 00:13:09 -05:00
parent 913151e063
commit 2113cb9ba8
3 changed files with 31 additions and 8 deletions

View file

@ -1,8 +1,9 @@
#include <scwx/qt/gl/gl_context.hpp> #include <scwx/qt/gl/gl_context.hpp>
#include <scwx/qt/util/texture_atlas.hpp> #include <scwx/qt/util/texture_atlas.hpp>
#include <scwx/util/hash.hpp>
#include <scwx/util/logger.hpp> #include <scwx/util/logger.hpp>
#include <boost/container_hash/hash.hpp>
namespace scwx namespace scwx
{ {
namespace qt namespace qt
@ -25,11 +26,12 @@ public:
} }
~Impl() {} ~Impl() {}
static std::size_t
GetShaderKey(std::initializer_list<std::pair<GLenum, std::string>> shaders);
gl::OpenGLFunctions gl_; gl::OpenGLFunctions gl_;
std::unordered_map<std::pair<std::string, std::string>, std::unordered_map<std::size_t, std::shared_ptr<gl::ShaderProgram>>
std::shared_ptr<gl::ShaderProgram>,
scwx::util::hash<std::pair<std::string, std::string>>>
shaderProgramMap_; shaderProgramMap_;
std::mutex shaderProgramMutex_; std::mutex shaderProgramMutex_;
@ -54,8 +56,15 @@ std::shared_ptr<gl::ShaderProgram>
GlContext::GetShaderProgram(const std::string& vertexPath, GlContext::GetShaderProgram(const std::string& vertexPath,
const std::string& fragmentPath) const std::string& fragmentPath)
{ {
const std::pair<std::string, std::string> key {vertexPath, fragmentPath}; return GetShaderProgram(
std::shared_ptr<gl::ShaderProgram> shaderProgram; {{GL_VERTEX_SHADER, vertexPath}, {GL_FRAGMENT_SHADER, fragmentPath}});
}
std::shared_ptr<gl::ShaderProgram> GlContext::GetShaderProgram(
std::initializer_list<std::pair<GLenum, std::string>> shaders)
{
const auto key = Impl::GetShaderKey(shaders);
std::shared_ptr<gl::ShaderProgram> shaderProgram;
std::unique_lock lock(p->shaderProgramMutex_); std::unique_lock lock(p->shaderProgramMutex_);
@ -64,7 +73,7 @@ GlContext::GetShaderProgram(const std::string& vertexPath,
if (it == p->shaderProgramMap_.end()) if (it == p->shaderProgramMap_.end())
{ {
shaderProgram = std::make_shared<gl::ShaderProgram>(p->gl_); shaderProgram = std::make_shared<gl::ShaderProgram>(p->gl_);
shaderProgram->Load(vertexPath, fragmentPath); shaderProgram->Load(shaders);
p->shaderProgramMap_[key] = shaderProgram; p->shaderProgramMap_[key] = shaderProgram;
} }
else else
@ -91,6 +100,18 @@ GLuint GlContext::GetTextureAtlas()
return p->textureAtlas_; return p->textureAtlas_;
} }
std::size_t GlContext::Impl::GetShaderKey(
std::initializer_list<std::pair<GLenum, std::string>> 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 gl
} // namespace qt } // namespace qt
} // namespace scwx } // namespace scwx

View file

@ -27,6 +27,8 @@ public:
std::shared_ptr<gl::ShaderProgram> std::shared_ptr<gl::ShaderProgram>
GetShaderProgram(const std::string& vertexPath, GetShaderProgram(const std::string& vertexPath,
const std::string& fragmentPath); const std::string& fragmentPath);
std::shared_ptr<gl::ShaderProgram> GetShaderProgram(
std::initializer_list<std::pair<GLenum, std::string>> shaders);
GLuint GetTextureAtlas(); GLuint GetTextureAtlas();

View file

@ -109,7 +109,7 @@ bool ShaderProgram::Load(
std::string shaderSource = shaderStream.readAll().toStdString(); std::string shaderSource = shaderStream.readAll().toStdString();
const char* shaderSourceC = shaderSource.c_str(); const char* shaderSourceC = shaderSource.c_str();
// Create a vertex shader // Create a shader
GLuint shaderId = gl.glCreateShader(shader.first); GLuint shaderId = gl.glCreateShader(shader.first);
shaderIds.push_back(shaderId); shaderIds.push_back(shaderId);