From c71a2c131fcdfa83286e603c75e95d06404fedce Mon Sep 17 00:00:00 2001 From: vlj Date: Sun, 13 Jul 2014 11:25:31 +0200 Subject: [PATCH] Factorize transparent shaders. --- src/graphics/render.cpp | 8 ++--- src/graphics/shaders.cpp | 59 +++++++------------------------ src/graphics/shaders.hpp | 31 +++++++++------- src/graphics/stkanimatedmesh.cpp | 8 ++--- src/graphics/stkmesh.cpp | 4 +-- src/graphics/stkmesh.hpp | 4 +-- src/graphics/stkmeshscenenode.cpp | 20 +++++------ 7 files changed, 52 insertions(+), 82 deletions(-) diff --git a/src/graphics/render.cpp b/src/graphics/render.cpp index 24d385fc1..277638af2 100644 --- a/src/graphics/render.cpp +++ b/src/graphics/render.cpp @@ -717,16 +717,16 @@ void IrrDriver::renderTransparent() if (World::getWorld() && World::getWorld()->isFogEnabled()) { glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); -// renderMeshes2ndPass({ MeshShader::TransparentFogShader::TU_tex }, ListBlendTransparentFog::Arguments); + renderMeshes2ndPass(MeshShader::TransparentFogShaderInstance, { MeshShader::TransparentFogShaderInstance->TU_tex }, ListBlendTransparentFog::Arguments); glBlendFunc(GL_ONE, GL_ONE); -// renderMeshes2ndPass({ MeshShader::TransparentFogShader::TU_tex }, ListAdditiveTransparentFog::Arguments); + renderMeshes2ndPass(MeshShader::TransparentFogShaderInstance, { MeshShader::TransparentFogShaderInstance->TU_tex }, ListAdditiveTransparentFog::Arguments); } else { glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); -// renderMeshes2ndPass({ MeshShader::TransparentShader::TU_tex }, ListBlendTransparent::Arguments); + renderMeshes2ndPass(MeshShader::TransparentShaderInstance, { MeshShader::TransparentShaderInstance->TU_tex }, ListBlendTransparent::Arguments); glBlendFunc(GL_ONE, GL_ONE); -// renderMeshes2ndPass({ MeshShader::TransparentShader::TU_tex }, ListAdditiveTransparent::Arguments); + renderMeshes2ndPass(MeshShader::TransparentShaderInstance, { MeshShader::TransparentShaderInstance->TU_tex }, ListAdditiveTransparent::Arguments); } } diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 772e49107..b8fd9a9a3 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -343,8 +343,8 @@ void Shaders::loadShaders() MeshShader::GrassPass1Shader::init(); MeshShader::GrassPass2Shader::init(); MeshShader::BubbleShader::init(); - MeshShader::TransparentShader::init(); - MeshShader::TransparentFogShader::init(); + MeshShader::TransparentShaderInstance = new MeshShader::TransparentShader(); + MeshShader::TransparentFogShaderInstance = new MeshShader::TransparentFogShader(); MeshShader::BillboardShader::init(); LightShader::PointLightShader::init(); MeshShader::DisplaceShader::init(); @@ -474,6 +474,11 @@ void glUniform3fWraper(GLuint a, float b, float c, float d) glUniform3f(a, b, c, d); } +void glUniform1fWrapper(GLuint a, float b) +{ + glUniform1f(a, b); +} + namespace MeshShader { @@ -931,18 +936,12 @@ namespace MeshShader glUniform1f(uniform_transparency, transparency); } - GLuint TransparentShader::Program; - GLuint TransparentShader::uniform_MVP; - GLuint TransparentShader::uniform_TM; - GLuint TransparentShader::TU_tex; - - void TransparentShader::init() + TransparentShader::TransparentShader() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/object_pass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/transparent.frag").c_str()); - uniform_MVP = glGetUniformLocation(Program, "ModelMatrix"); - uniform_TM = glGetUniformLocation(Program, "TextureMatrix"); + AssignUniforms(Program, uniforms, {"ModelMatrix", "TextureMatrix" }); if (!UserConfigParams::m_ubo_disabled) { GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData"); @@ -953,36 +952,14 @@ namespace MeshShader AssignTextureUnit(Program, { { TU_tex, "tex" } }); } - void TransparentShader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix) - { - glUniformMatrix4fv(uniform_MVP, 1, GL_FALSE, ModelMatrix.pointer()); - glUniformMatrix4fv(uniform_TM, 1, GL_FALSE, TextureMatrix.pointer()); - } + TransparentShader *TransparentShaderInstance; - GLuint TransparentFogShader::Program; - GLuint TransparentFogShader::uniform_MVP; - GLuint TransparentFogShader::uniform_TM; - GLuint TransparentFogShader::TU_tex; - GLuint TransparentFogShader::uniform_fogmax; - GLuint TransparentFogShader::uniform_startH; - GLuint TransparentFogShader::uniform_endH; - GLuint TransparentFogShader::uniform_start; - GLuint TransparentFogShader::uniform_end; - GLuint TransparentFogShader::uniform_col; - - void TransparentFogShader::init() + TransparentFogShader::TransparentFogShader() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/object_pass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/transparentfog.frag").c_str()); - uniform_MVP = glGetUniformLocation(Program, "ModelMatrix"); - uniform_TM = glGetUniformLocation(Program, "TextureMatrix"); - uniform_fogmax = glGetUniformLocation(Program, "fogmax"); - uniform_startH = glGetUniformLocation(Program, "startH"); - uniform_endH = glGetUniformLocation(Program, "endH"); - uniform_start = glGetUniformLocation(Program, "start"); - uniform_end = glGetUniformLocation(Program, "end"); - uniform_col = glGetUniformLocation(Program, "col"); + AssignUniforms(Program, uniforms, { "ModelMatrix", "TextureMatrix", "fogmax", "startH", "endH", "start", "end", "col" }); if (!UserConfigParams::m_ubo_disabled) { GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData"); @@ -993,17 +970,7 @@ namespace MeshShader AssignTextureUnit(Program, { { TU_tex, "tex" } }); } - void TransparentFogShader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix, float fogmax, float startH, float endH, float start, float end, const core::vector3df &col, const core::vector3df &campos) - { - glUniformMatrix4fv(uniform_MVP, 1, GL_FALSE, ModelMatrix.pointer()); - glUniformMatrix4fv(uniform_TM, 1, GL_FALSE, TextureMatrix.pointer()); - glUniform1f(uniform_fogmax, fogmax); - glUniform1f(uniform_startH, startH); - glUniform1f(uniform_endH, endH); - glUniform1f(uniform_start, start); - glUniform1f(uniform_end, end); - glUniform3f(uniform_col, col.X, col.Y, col.Z); - } + TransparentFogShader *TransparentFogShaderInstance; GLuint BillboardShader::Program; GLuint BillboardShader::attrib_corner; diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index 4b8e41bd7..3b91412f9 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -74,6 +74,15 @@ void setUniformsHelper(const std::vector &uniforms, const video::SColorf setUniformsHelper(uniforms, arg...); } +void glUniform1fWrapper(GLuint, float); + +template +void setUniformsHelper(const std::vector &uniforms, float f, Args... arg) +{ + glUniform1fWrapper(uniforms[N], f); + setUniformsHelper(uniforms, arg...); +} + void bypassUBO(GLuint Program); template @@ -277,28 +286,26 @@ public: static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, unsigned TU_tex, float time, float transparency); }; -class TransparentShader +class TransparentShader : public ShaderHelper { public: - static GLuint Program; - static GLuint uniform_MVP, uniform_TM; - static GLuint TU_tex; + GLuint TU_tex; - static void init(); - static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix); + TransparentShader(); }; -class TransparentFogShader +extern TransparentShader *TransparentShaderInstance; + +class TransparentFogShader : public ShaderHelper { public: - static GLuint Program; - static GLuint uniform_MVP, uniform_TM, uniform_fogmax, uniform_startH, uniform_endH, uniform_start, uniform_end, uniform_col; - static GLuint TU_tex; + GLuint TU_tex; - static void init(); - static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix, float fogmax, float startH, float endH, float start, float end, const core::vector3df &col, const core::vector3df &campos); + TransparentFogShader(); }; +extern TransparentFogShader *TransparentFogShaderInstance; + class BillboardShader { public: diff --git a/src/graphics/stkanimatedmesh.cpp b/src/graphics/stkanimatedmesh.cpp index a71f2ffc8..337a86724 100644 --- a/src/graphics/stkanimatedmesh.cpp +++ b/src/graphics/stkanimatedmesh.cpp @@ -201,20 +201,18 @@ void STKAnimatedMesh::render() const float end = track->getFogEnd(); const video::SColor tmpcol = track->getFogColor(); - core::vector3df col(tmpcol.getRed() / 255.0f, + video::SColorf col(tmpcol.getRed() / 255.0f, tmpcol.getGreen() / 255.0f, tmpcol.getBlue() / 255.0f); for_in(mesh, TransparentMesh[TM_DEFAULT]) ListBlendTransparentFog::Arguments.push_back( std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, - fogmax, startH, endH, start, end, col, - Camera::getCamera(0)->getCameraSceneNode()->getAbsolutePosition())); + fogmax, startH, endH, start, end, col)); for_in(mesh, TransparentMesh[TM_ADDITIVE]) ListAdditiveTransparentFog::Arguments.push_back( std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, - fogmax, startH, endH, start, end, col, - Camera::getCamera(0)->getCameraSceneNode()->getAbsolutePosition())); + fogmax, startH, endH, start, end, col)); } else { diff --git a/src/graphics/stkmesh.cpp b/src/graphics/stkmesh.cpp index 7d86a4bbc..662c428d2 100644 --- a/src/graphics/stkmesh.cpp +++ b/src/graphics/stkmesh.cpp @@ -378,5 +378,5 @@ std::vector > ListUnlitSM::Arguments; std::vector > ListDetailSM::Arguments; std::vector > ListBlendTransparent::Arguments; std::vector > ListAdditiveTransparent::Arguments; -std::vector > ListBlendTransparentFog::Arguments; -std::vector > ListAdditiveTransparentFog::Arguments; +std::vector > ListBlendTransparentFog::Arguments; +std::vector > ListAdditiveTransparentFog::Arguments; diff --git a/src/graphics/stkmesh.hpp b/src/graphics/stkmesh.hpp index f0542c353..7b643f848 100644 --- a/src/graphics/stkmesh.hpp +++ b/src/graphics/stkmesh.hpp @@ -203,13 +203,13 @@ public: class ListBlendTransparentFog { public: - static std::vector > Arguments; + static std::vector > Arguments; }; class ListAdditiveTransparentFog { public: - static std::vector > Arguments; + static std::vector > Arguments; }; template diff --git a/src/graphics/stkmeshscenenode.cpp b/src/graphics/stkmeshscenenode.cpp index 022ca5371..b1c109295 100644 --- a/src/graphics/stkmeshscenenode.cpp +++ b/src/graphics/stkmeshscenenode.cpp @@ -391,7 +391,7 @@ void STKMeshSceneNode::render() if (World::getWorld() && World::getWorld()->isFogEnabled()) { - glUseProgram(MeshShader::TransparentFogShader::Program); + glUseProgram(MeshShader::TransparentFogShaderInstance->Program); for (unsigned i = 0; i < GLmeshes.size(); i++) { GLMesh &mesh = GLmeshes[i]; @@ -410,13 +410,13 @@ void STKMeshSceneNode::render() const float end = track->getFogEnd(); const video::SColor tmpcol = track->getFogColor(); - core::vector3df col(tmpcol.getRed() / 255.0f, + video::SColorf col(tmpcol.getRed() / 255.0f, tmpcol.getGreen() / 255.0f, tmpcol.getBlue() / 255.0f); compressTexture(mesh.textures[0], true); setTexture(0, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); - MeshShader::TransparentFogShader::setUniforms(AbsoluteTransformation, mesh.TextureMatrix, fogmax, startH, endH, start, end, col, Camera::getCamera(0)->getCameraSceneNode()->getAbsolutePosition()); + MeshShader::TransparentFogShaderInstance->setUniforms(AbsoluteTransformation, mesh.TextureMatrix, fogmax, startH, endH, start, end, col); assert(mesh.vao); glBindVertexArray(mesh.vao); @@ -426,7 +426,7 @@ void STKMeshSceneNode::render() } else { - glUseProgram(MeshShader::TransparentShader::Program); + glUseProgram(MeshShader::TransparentShaderInstance->Program); for (unsigned i = 0; i < GLmeshes.size(); i++) { irr_driver->IncreaseObjectCount(); @@ -436,9 +436,9 @@ void STKMeshSceneNode::render() size_t count = mesh.IndexCount; compressTexture(mesh.textures[0], true); - setTexture(MeshShader::TransparentShader::TU_tex, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); + setTexture(MeshShader::TransparentShaderInstance->TU_tex, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); - MeshShader::TransparentShader::setUniforms(AbsoluteTransformation, mesh.TextureMatrix); + MeshShader::TransparentShaderInstance->setUniforms(AbsoluteTransformation, mesh.TextureMatrix); assert(mesh.vao); glBindVertexArray(mesh.vao); glDrawElements(ptype, count, itype, 0); @@ -462,20 +462,18 @@ void STKMeshSceneNode::render() const float end = track->getFogEnd(); const video::SColor tmpcol = track->getFogColor(); - core::vector3df col(tmpcol.getRed() / 255.0f, + video::SColorf col(tmpcol.getRed() / 255.0f, tmpcol.getGreen() / 255.0f, tmpcol.getBlue() / 255.0f); for_in(mesh, TransparentMesh[TM_DEFAULT]) ListBlendTransparentFog::Arguments.push_back( std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, - fogmax, startH, endH, start, end, col, - Camera::getCamera(0)->getCameraSceneNode()->getAbsolutePosition())); + fogmax, startH, endH, start, end, col)); for_in(mesh, TransparentMesh[TM_ADDITIVE]) ListAdditiveTransparentFog::Arguments.push_back( std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, - fogmax, startH, endH, start, end, col, - Camera::getCamera(0)->getCameraSceneNode()->getAbsolutePosition())); + fogmax, startH, endH, start, end, col)); } else {