Removed now duplicated code.
This commit is contained in:
parent
865760ffa4
commit
118233fb24
@ -199,67 +199,7 @@ GLuint LoadShader(const char * file, unsigned type)
|
||||
return Id;
|
||||
}
|
||||
|
||||
void setAttribute(AttributeType Tp, GLuint ProgramID)
|
||||
{
|
||||
switch (Tp)
|
||||
{
|
||||
case OBJECT:
|
||||
glBindAttribLocation(ProgramID, 0, "Position");
|
||||
glBindAttribLocation(ProgramID, 1, "Normal");
|
||||
glBindAttribLocation(ProgramID, 2, "Color");
|
||||
glBindAttribLocation(ProgramID, 3, "Texcoord");
|
||||
glBindAttribLocation(ProgramID, 4, "SecondTexcoord");
|
||||
glBindAttribLocation(ProgramID, 5, "Tangent");
|
||||
glBindAttribLocation(ProgramID, 6, "Bitangent");
|
||||
glBindAttribLocation(ProgramID, 7, "Origin");
|
||||
glBindAttribLocation(ProgramID, 8, "Orientation");
|
||||
glBindAttribLocation(ProgramID, 9, "Scale");
|
||||
break;
|
||||
case PARTICLES_SIM:
|
||||
glBindAttribLocation(ProgramID, 0, "particle_position");
|
||||
glBindAttribLocation(ProgramID, 1, "lifetime");
|
||||
glBindAttribLocation(ProgramID, 2, "particle_velocity");
|
||||
glBindAttribLocation(ProgramID, 3, "size");
|
||||
glBindAttribLocation(ProgramID, 4, "particle_position_initial");
|
||||
glBindAttribLocation(ProgramID, 5, "lifetime_initial");
|
||||
glBindAttribLocation(ProgramID, 6, "particle_velocity_initial");
|
||||
glBindAttribLocation(ProgramID, 7, "size_initial");
|
||||
break;
|
||||
case PARTICLES_RENDERING:
|
||||
glBindAttribLocation(ProgramID, 1, "lifetime");
|
||||
glBindAttribLocation(ProgramID, 2, "size");
|
||||
glBindAttribLocation(ProgramID, 4, "quadcorner");
|
||||
glBindAttribLocation(ProgramID, 5, "rotationvec");
|
||||
glBindAttribLocation(ProgramID, 6, "anglespeed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
GLuint LoadTFBProgram(const char * vertex_file_path, const char **varyings, unsigned varyingscount)
|
||||
{
|
||||
GLuint Program = glCreateProgram();
|
||||
loadAndAttach(Program, GL_VERTEX_SHADER, vertex_file_path);
|
||||
if (CVS->getGLSLVersion() < 330)
|
||||
setAttribute(PARTICLES_SIM, Program);
|
||||
glTransformFeedbackVaryings(Program, varyingscount, varyings, GL_INTERLEAVED_ATTRIBS);
|
||||
glLinkProgram(Program);
|
||||
|
||||
GLint Result = GL_FALSE;
|
||||
int InfoLogLength;
|
||||
glGetProgramiv(Program, GL_LINK_STATUS, &Result);
|
||||
if (Result == GL_FALSE)
|
||||
{
|
||||
glGetProgramiv(Program, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||
char *ErrorMessage = new char[InfoLogLength];
|
||||
glGetProgramInfoLog(Program, InfoLogLength, NULL, ErrorMessage);
|
||||
Log::error("GLWrap", ErrorMessage);
|
||||
delete[] ErrorMessage;
|
||||
}
|
||||
|
||||
glGetError();
|
||||
|
||||
return Program;
|
||||
}
|
||||
|
||||
GLuint quad_vbo, tri_vbo;
|
||||
|
||||
|
@ -30,36 +30,6 @@ bool needsUBO();
|
||||
|
||||
unsigned getGLSLVersion();
|
||||
|
||||
GLuint LoadShader(const char * file, unsigned type);
|
||||
GLuint LoadTFBProgram(const char * vertex_file_path, const char **varyings, unsigned varyingscount);
|
||||
|
||||
template<typename ... Types>
|
||||
void loadAndAttach(GLint ProgramID)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
template<typename ... Types>
|
||||
void loadAndAttach(GLint ProgramID, GLint ShaderType, const char *filepath, Types ... args)
|
||||
{
|
||||
GLint ShaderID = LoadShader(filepath, ShaderType);
|
||||
glAttachShader(ProgramID, ShaderID);
|
||||
glDeleteShader(ShaderID);
|
||||
loadAndAttach(ProgramID, args...);
|
||||
}
|
||||
|
||||
template<typename ...Types>
|
||||
void oldPrintFileList()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
template<typename ...Types>
|
||||
void oldPrintFileList(GLint ShaderType, const char *filepath, Types ... args)
|
||||
{
|
||||
Log::error("GLWrapp", filepath);
|
||||
oldPrintFileList(args...);
|
||||
}
|
||||
|
||||
enum AttributeType
|
||||
{
|
||||
@ -70,165 +40,8 @@ enum AttributeType
|
||||
|
||||
void setAttribute(AttributeType Tp, GLuint ProgramID);
|
||||
|
||||
template<typename ... Types>
|
||||
GLint LoadProgram(AttributeType Tp, Types ... args)
|
||||
{
|
||||
GLint ProgramID = glCreateProgram();
|
||||
loadAndAttach(ProgramID, args...);
|
||||
if (getGLSLVersion() < 330)
|
||||
setAttribute(Tp, ProgramID);
|
||||
glLinkProgram(ProgramID);
|
||||
|
||||
GLint Result = GL_FALSE;
|
||||
int InfoLogLength;
|
||||
glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
|
||||
if (Result == GL_FALSE) {
|
||||
Log::error("GLWrapp", "Error when linking these shaders :");
|
||||
oldPrintFileList(args...);
|
||||
glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
|
||||
char *ErrorMessage = new char[InfoLogLength];
|
||||
glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, ErrorMessage);
|
||||
Log::error("GLWrapp", ErrorMessage);
|
||||
delete[] ErrorMessage;
|
||||
}
|
||||
|
||||
GLenum glErr = glGetError();
|
||||
if (glErr != GL_NO_ERROR)
|
||||
{
|
||||
Log::warn("IrrDriver", "GLWrap : OpenGL error %i\n", glErr);
|
||||
}
|
||||
|
||||
return ProgramID;
|
||||
}
|
||||
|
||||
struct UniformHelper
|
||||
{
|
||||
template<unsigned N = 0>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms)
|
||||
{
|
||||
}
|
||||
|
||||
template<unsigned N = 0, typename... Args>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms, const irr::core::matrix4 &mat, Args... arg)
|
||||
{
|
||||
glUniformMatrix4fv(uniforms[N], 1, GL_FALSE, mat.pointer());
|
||||
setUniformsHelper<N + 1>(uniforms, arg...);
|
||||
}
|
||||
|
||||
template<unsigned N = 0, typename... Args>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms, const irr::video::SColorf &col, Args... arg)
|
||||
{
|
||||
glUniform3f(uniforms[N], col.r, col.g, col.b);
|
||||
setUniformsHelper<N + 1>(uniforms, arg...);
|
||||
}
|
||||
|
||||
template<unsigned N = 0, typename... Args>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms, const irr::video::SColor &col, Args... arg)
|
||||
{
|
||||
glUniform4i(uniforms[N], col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha());
|
||||
setUniformsHelper<N + 1>(uniforms, arg...);
|
||||
}
|
||||
|
||||
template<unsigned N = 0, typename... Args>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms, const irr::core::vector3df &v, Args... arg)
|
||||
{
|
||||
glUniform3f(uniforms[N], v.X, v.Y, v.Z);
|
||||
setUniformsHelper<N + 1>(uniforms, arg...);
|
||||
}
|
||||
|
||||
|
||||
template<unsigned N = 0, typename... Args>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms, const irr::core::vector2df &v, Args... arg)
|
||||
{
|
||||
glUniform2f(uniforms[N], v.X, v.Y);
|
||||
setUniformsHelper<N + 1>(uniforms, arg...);
|
||||
}
|
||||
|
||||
template<unsigned N = 0, typename... Args>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms, const irr::core::dimension2df &v, Args... arg)
|
||||
{
|
||||
glUniform2f(uniforms[N], v.Width, v.Height);
|
||||
setUniformsHelper<N + 1>(uniforms, arg...);
|
||||
}
|
||||
|
||||
template<unsigned N = 0, typename... Args>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms, float f, Args... arg)
|
||||
{
|
||||
glUniform1f(uniforms[N], f);
|
||||
setUniformsHelper<N + 1>(uniforms, arg...);
|
||||
}
|
||||
|
||||
template<unsigned N = 0, typename... Args>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms, int f, Args... arg)
|
||||
{
|
||||
glUniform1i(uniforms[N], f);
|
||||
setUniformsHelper<N + 1>(uniforms, arg...);
|
||||
}
|
||||
|
||||
template<unsigned N = 0, typename... Args>
|
||||
static void setUniformsHelper(const std::vector<GLuint> &uniforms, const std::vector<float> &v, Args... arg)
|
||||
{
|
||||
glUniform1fv(uniforms[N], (int)v.size(), v.data());
|
||||
setUniformsHelper<N + 1>(uniforms, arg...);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void bypassUBO(GLuint Program);
|
||||
|
||||
extern std::vector<void(*)()> CleanTable;
|
||||
|
||||
template<typename T, typename... Args>
|
||||
class ShaderHelperSingleton : public Singleton<T>
|
||||
{
|
||||
protected:
|
||||
std::vector<GLuint> uniforms;
|
||||
|
||||
void AssignUniforms_impl()
|
||||
{
|
||||
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
|
||||
if (uniform_ViewProjectionMatrixesUBO != GL_INVALID_INDEX)
|
||||
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
|
||||
GLuint uniform_LightingUBO = glGetUniformBlockIndex(Program, "LightingData");
|
||||
if (uniform_LightingUBO != GL_INVALID_INDEX)
|
||||
glUniformBlockBinding(Program, uniform_LightingUBO, 1);
|
||||
}
|
||||
|
||||
template<typename... U>
|
||||
void AssignUniforms_impl(const char* name, U... rest)
|
||||
{
|
||||
uniforms.push_back(glGetUniformLocation(Program, name));
|
||||
AssignUniforms_impl(rest...);
|
||||
}
|
||||
|
||||
template<typename... U>
|
||||
void AssignUniforms(U... rest)
|
||||
{
|
||||
static_assert(sizeof...(rest) == sizeof...(Args), "Count of Uniform's name mismatch");
|
||||
AssignUniforms_impl(rest...);
|
||||
}
|
||||
|
||||
public:
|
||||
GLuint Program;
|
||||
|
||||
ShaderHelperSingleton()
|
||||
{
|
||||
CleanTable.push_back(this->kill);
|
||||
}
|
||||
|
||||
~ShaderHelperSingleton()
|
||||
{
|
||||
glDeleteProgram(Program);
|
||||
}
|
||||
|
||||
void setUniforms(const Args & ... args) const
|
||||
{
|
||||
if (!CVS->isARBUniformBufferObjectUsable())
|
||||
bypassUBO(Program);
|
||||
UniformHelper::setUniformsHelper(uniforms, args...);
|
||||
}
|
||||
};
|
||||
|
||||
enum SamplerType {
|
||||
Trilinear_Anisotropic_Filtered,
|
||||
Semi_trilinear,
|
||||
|
Loading…
Reference in New Issue
Block a user