Upgrade the glsl shader class a bit. Add a const char* comparitor so we can use a map with a simple "string" key. Fine to use when performance is not critical.

This commit is contained in:
Ian Curtis 2022-10-16 21:03:41 +01:00
parent 6fbf938335
commit f0872cc998
2 changed files with 99 additions and 32 deletions

View file

@ -1,28 +1,41 @@
#include "GLSLShader.h" #include "GLSLShader.h"
#include <cstdio> #include <cstdio>
GLSLShader::GLSLShader() { GLSLShader::GLSLShader()
{
m_vShader = 0; Reset();
m_fShader = 0;
m_program = 0;
for (auto &i : uniformLoc) {
i = 0;
}
for (auto &i : attribLoc) {
i = 0;
}
} }
GLSLShader::~GLSLShader() { GLSLShader::~GLSLShader()
{
UnloadShaders(); UnloadShaders();
} }
bool GLSLShader::LoadShaders(const char *vertexShader, const char *fragmentShader) { GLSLShader::GLSLShader(GLSLShader&& other) noexcept
{
*this = std::move(other);
}
GLSLShader& GLSLShader::operator=(GLSLShader&& other) noexcept
{
if (this != &other) {
m_program = other.m_program;
m_vShader = other.m_vShader;
m_fShader = other.m_fShader;
uniformLocMap = other.uniformLocMap;
attribLocMap = other.attribLocMap;
std::copy(std::begin(other.uniformLoc), std::end(other.uniformLoc), std::begin(uniformLoc));
std::copy(std::begin(other.attribLoc), std::end(other.attribLoc), std::begin(attribLoc));
other.Reset();
}
return *this;
}
bool GLSLShader::LoadShaders(const char* vertexShader, const char* fragmentShader)
{
m_program = glCreateProgram(); m_program = glCreateProgram();
m_vShader = glCreateShader(GL_VERTEX_SHADER); m_vShader = glCreateShader(GL_VERTEX_SHADER);
m_fShader = glCreateShader(GL_FRAGMENT_SHADER); m_fShader = glCreateShader(GL_FRAGMENT_SHADER);
@ -58,52 +71,80 @@ void GLSLShader::UnloadShaders()
m_program = 0; m_program = 0;
} }
void GLSLShader::EnableShader() { void GLSLShader::EnableShader()
{
glUseProgram(m_program); glUseProgram(m_program);
} }
void GLSLShader::DisableShader() { void GLSLShader::DisableShader()
{
glUseProgram(0); glUseProgram(0);
} }
void GLSLShader::PrintShaderInfoLog(GLuint obj) { void GLSLShader::Reset()
{
m_vShader = 0;
m_fShader = 0;
m_program = 0;
for (auto& i : uniformLoc) {
i = -1;
}
for (auto& i : attribLoc) {
i = -1;
}
uniformLocMap.clear();
attribLocMap.clear();
}
void GLSLShader::PrintShaderInfoLog(GLuint obj)
{
int infologLength = 0; int infologLength = 0;
int charsWritten = 0; int charsWritten = 0;
glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &infologLength); glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &infologLength);
if (infologLength > 0) { if (infologLength > 0) {
char *infoLog = new char[infologLength]; char* infoLog = new char[infologLength];
glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog); glGetShaderInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n", infoLog); printf("%s\n", infoLog);
delete[] infoLog; delete[] infoLog;
} }
} }
void GLSLShader::PrintProgramInfoLog(GLuint obj) { void GLSLShader::PrintProgramInfoLog(GLuint obj)
{
int infologLength = 0; int infologLength = 0;
int charsWritten = 0; int charsWritten = 0;
glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &infologLength); glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &infologLength);
if (infologLength > 0) { if (infologLength > 0) {
char *infoLog = new char[infologLength]; char* infoLog = new char[infologLength];
glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog); glGetProgramInfoLog(obj, infologLength, &charsWritten, infoLog);
printf("%s\n", infoLog); printf("%s\n", infoLog);
delete[] infoLog; delete[] infoLog;
} }
} }
int GLSLShader::GetUniformLocation(const char *str) int GLSLShader::GetUniformLocation(const char* str)
{ {
return glGetUniformLocation(m_program, str); return glGetUniformLocation(m_program, str);
} }
int GLSLShader::GetAttributeLocation(const char *str) void GLSLShader::GetUniformLocationMap(const char* str)
{
uniformLocMap[str] = GetUniformLocation(str);
}
int GLSLShader::GetAttributeLocation(const char* str)
{ {
return glGetAttribLocation(m_program, str); return glGetAttribLocation(m_program, str);
} }
void GLSLShader::GetAttributeLocationMap(const char* str)
{
attribLocMap[str] = GetAttributeLocation(str);
}

View file

@ -2,27 +2,52 @@
#define _GLSLSHADER_H_ #define _GLSLSHADER_H_
#include <GL/glew.h> #include <GL/glew.h>
#include <map>
class GLSLShader { class GLSLShader {
public: public:
GLSLShader(); GLSLShader();
~GLSLShader(); ~GLSLShader();
bool LoadShaders(const char *vertexShader, const char *fragmentShader); GLSLShader(GLSLShader&& other) noexcept;
GLSLShader(const GLSLShader&) = delete;
GLSLShader& operator=(const GLSLShader&) = delete;
GLSLShader& operator=(GLSLShader&& other) noexcept;
bool LoadShaders(const char* vertexShader, const char* fragmentShader);
void UnloadShaders(); void UnloadShaders();
void EnableShader(); void EnableShader();
void DisableShader(); void DisableShader();
int GetUniformLocation(const char *str); int GetUniformLocation(const char* str);
int GetAttributeLocation(const char *str); void GetUniformLocationMap(const char* str);
int GetAttributeLocation(const char* str);
void GetAttributeLocationMap(const char* str);
int uniformLoc[64]; int uniformLoc[64];
int attribLoc[16]; int attribLoc[16];
private: private:
struct cmp_str
{
bool operator()(char const* a, char const* b) const
{
return std::strcmp(a, b) < 0;
}
};
public:
std::map<const char*, int, cmp_str> uniformLocMap;
std::map<const char*, int, cmp_str> attribLocMap;
private:
void Reset();
void PrintShaderInfoLog(GLuint obj); void PrintShaderInfoLog(GLuint obj);
void PrintProgramInfoLog(GLuint obj); void PrintProgramInfoLog(GLuint obj);
@ -31,4 +56,5 @@ private:
GLuint m_fShader; GLuint m_fShader;
}; };
#endif #endif