From 118233fb249ba17c584408b4ab0f706f505848f0 Mon Sep 17 00:00:00 2001 From: hiker Date: Fri, 24 Apr 2015 14:57:36 +1000 Subject: [PATCH] Removed now duplicated code. --- src/graphics/shaders.cpp | 60 ----------- src/graphics/shaders_util.hpp | 187 ---------------------------------- 2 files changed, 247 deletions(-) diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 35e5e044e..f847d4a5c 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -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; diff --git a/src/graphics/shaders_util.hpp b/src/graphics/shaders_util.hpp index 1a5638b9c..cbba59bdd 100644 --- a/src/graphics/shaders_util.hpp +++ b/src/graphics/shaders_util.hpp @@ -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 -void loadAndAttach(GLint ProgramID) -{ - return; -} - -template -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 -void oldPrintFileList() -{ - return; -} - -template -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 -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 - static void setUniformsHelper(const std::vector &uniforms) - { - } - - template - static void setUniformsHelper(const std::vector &uniforms, const irr::core::matrix4 &mat, Args... arg) - { - glUniformMatrix4fv(uniforms[N], 1, GL_FALSE, mat.pointer()); - setUniformsHelper(uniforms, arg...); - } - - template - static void setUniformsHelper(const std::vector &uniforms, const irr::video::SColorf &col, Args... arg) - { - glUniform3f(uniforms[N], col.r, col.g, col.b); - setUniformsHelper(uniforms, arg...); - } - - template - static void setUniformsHelper(const std::vector &uniforms, const irr::video::SColor &col, Args... arg) - { - glUniform4i(uniforms[N], col.getRed(), col.getGreen(), col.getBlue(), col.getAlpha()); - setUniformsHelper(uniforms, arg...); - } - - template - static void setUniformsHelper(const std::vector &uniforms, const irr::core::vector3df &v, Args... arg) - { - glUniform3f(uniforms[N], v.X, v.Y, v.Z); - setUniformsHelper(uniforms, arg...); - } - - - template - static void setUniformsHelper(const std::vector &uniforms, const irr::core::vector2df &v, Args... arg) - { - glUniform2f(uniforms[N], v.X, v.Y); - setUniformsHelper(uniforms, arg...); - } - - template - static void setUniformsHelper(const std::vector &uniforms, const irr::core::dimension2df &v, Args... arg) - { - glUniform2f(uniforms[N], v.Width, v.Height); - setUniformsHelper(uniforms, arg...); - } - - template - static void setUniformsHelper(const std::vector &uniforms, float f, Args... arg) - { - glUniform1f(uniforms[N], f); - setUniformsHelper(uniforms, arg...); - } - - template - static void setUniformsHelper(const std::vector &uniforms, int f, Args... arg) - { - glUniform1i(uniforms[N], f); - setUniformsHelper(uniforms, arg...); - } - - template - static void setUniformsHelper(const std::vector &uniforms, const std::vector &v, Args... arg) - { - glUniform1fv(uniforms[N], (int)v.size(), v.data()); - setUniformsHelper(uniforms, arg...); - } - -}; - void bypassUBO(GLuint Program); -extern std::vector CleanTable; - -template -class ShaderHelperSingleton : public Singleton -{ -protected: - std::vector 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 - void AssignUniforms_impl(const char* name, U... rest) - { - uniforms.push_back(glGetUniformLocation(Program, name)); - AssignUniforms_impl(rest...); - } - - template - 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,