From 098eb7f595f0faf585d8f02d163e4034396778bf Mon Sep 17 00:00:00 2001 From: vlj Date: Mon, 14 Jul 2014 23:25:52 +0200 Subject: [PATCH] Port remaining pass 1 Instanced shaders to singleton --- src/graphics/shaders.cpp | 38 ++++++++------------------ src/graphics/shaders.hpp | 20 +++++++------- src/graphics/stkinstancedscenenode.cpp | 12 ++++---- 3 files changed, 27 insertions(+), 43 deletions(-) diff --git a/src/graphics/shaders.cpp b/src/graphics/shaders.cpp index 65dc6e743..5fb7f5263 100644 --- a/src/graphics/shaders.cpp +++ b/src/graphics/shaders.cpp @@ -329,8 +329,8 @@ void Shaders::loadShaders() MeshShader::ObjectPass1ShaderInstance = new MeshShader::ObjectPass1Shader(); MeshShader::ObjectRefPass1ShaderInstance = new MeshShader::ObjectRefPass1Shader(); MeshShader::InstancedObjectPass1ShaderInstance = new MeshShader::InstancedObjectPass1Shader(); - MeshShader::InstancedObjectRefPass1Shader::init(); - MeshShader::InstancedGrassPass1Shader::init(); + MeshShader::InstancedObjectRefPass1ShaderInstance = new MeshShader::InstancedObjectRefPass1Shader(); + MeshShader::InstancedGrassPass1ShaderInstance = new MeshShader::InstancedGrassPass1Shader(); MeshShader::ObjectPass2ShaderInstance = new MeshShader::ObjectPass2Shader(); MeshShader::InstancedObjectPass2Shader::init(); MeshShader::InstancedObjectRefPass2Shader::init(); @@ -571,17 +571,15 @@ namespace MeshShader InstancedObjectPass1Shader *InstancedObjectPass1ShaderInstance; - GLuint InstancedObjectRefPass1Shader::Program; - GLuint InstancedObjectRefPass1Shader::uniform_tex; - - void InstancedObjectRefPass1Shader::init() + InstancedObjectRefPass1Shader::InstancedObjectRefPass1Shader() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_object_pass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass1.frag").c_str()); - uniform_tex = glGetUniformLocation(Program, "tex"); + TU_tex = 0; + AssignTextureUnit(Program, { { TU_tex, "tex" } }); if (!UserConfigParams::m_ubo_disabled) { GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData"); @@ -589,26 +587,18 @@ namespace MeshShader } } - void InstancedObjectRefPass1Shader::setUniforms(unsigned TU_tex) - { - if (UserConfigParams::m_ubo_disabled) - bypassUBO(Program); - glUniform1i(uniform_tex, TU_tex); - } + InstancedObjectRefPass1Shader *InstancedObjectRefPass1ShaderInstance; - GLuint InstancedGrassPass1Shader::Program; - GLuint InstancedGrassPass1Shader::uniform_windDir; - GLuint InstancedGrassPass1Shader::uniform_tex; - - void InstancedGrassPass1Shader::init() + InstancedGrassPass1Shader::InstancedGrassPass1Shader() { Program = LoadProgram( GL_VERTEX_SHADER, file_manager->getAsset("shaders/utils/getworldmatrix.vert").c_str(), GL_VERTEX_SHADER, file_manager->getAsset("shaders/instanced_grass.vert").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/encode_normal.frag").c_str(), GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass1.frag").c_str()); - uniform_windDir = glGetUniformLocation(Program, "windDir"); - uniform_tex = glGetUniformLocation(Program, "tex"); + AssignUniforms(Program, uniforms, { "windDir" }); + TU_tex = 0; + AssignTextureUnit(Program, { { TU_tex, "tex" } }); if (!UserConfigParams::m_ubo_disabled) { GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData"); @@ -616,13 +606,7 @@ namespace MeshShader } } - void InstancedGrassPass1Shader::setUniforms(const core::vector3df &windDir, unsigned TU_tex) - { - if (UserConfigParams::m_ubo_disabled) - bypassUBO(Program); - glUniform3f(uniform_windDir, windDir.X, windDir.Y, windDir.Z); - glUniform1i(uniform_tex, TU_tex); - } + InstancedGrassPass1Shader *InstancedGrassPass1ShaderInstance; // Solid Lit pass shaders ObjectPass2Shader::ObjectPass2Shader() diff --git a/src/graphics/shaders.hpp b/src/graphics/shaders.hpp index b8badf48c..ba1f4c88e 100644 --- a/src/graphics/shaders.hpp +++ b/src/graphics/shaders.hpp @@ -169,26 +169,26 @@ public: extern InstancedObjectPass1Shader *InstancedObjectPass1ShaderInstance; -class InstancedObjectRefPass1Shader +class InstancedObjectRefPass1Shader : public ShaderHelper<> { public: - static GLuint Program; - static GLuint uniform_tex; + GLuint TU_tex; - static void init(); - static void setUniforms(unsigned TU_tex); + InstancedObjectRefPass1Shader(); }; -class InstancedGrassPass1Shader +extern InstancedObjectRefPass1Shader *InstancedObjectRefPass1ShaderInstance; + +class InstancedGrassPass1Shader : public ShaderHelper { public: - static GLuint Program; - static GLuint uniform_windDir, uniform_tex; + GLuint TU_tex; - static void init(); - static void setUniforms(const core::vector3df &windDir, unsigned TU_tex); + InstancedGrassPass1Shader(); }; +extern InstancedGrassPass1Shader *InstancedGrassPass1ShaderInstance; + class ObjectPass2Shader : public ShaderHelper { public: diff --git a/src/graphics/stkinstancedscenenode.cpp b/src/graphics/stkinstancedscenenode.cpp index f739852f7..745953983 100644 --- a/src/graphics/stkinstancedscenenode.cpp +++ b/src/graphics/stkinstancedscenenode.cpp @@ -172,8 +172,8 @@ static void drawFSPMAlphaRefTexture(GLMesh &mesh, size_t instance_count) size_t count = mesh.IndexCount; compressTexture(mesh.textures[0], true); - setTexture(0, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); - MeshShader::InstancedObjectRefPass1Shader::setUniforms(0); + setTexture(MeshShader::InstancedObjectRefPass1ShaderInstance->TU_tex, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); + MeshShader::InstancedObjectRefPass1ShaderInstance->setUniforms(); glBindVertexArray(mesh.vao); glDrawElementsInstanced(ptype, count, itype, 0, instance_count); @@ -202,8 +202,8 @@ static void drawFSPMGrass(GLMesh &mesh, const core::vector3df &windDir, size_t i size_t count = mesh.IndexCount; compressTexture(mesh.textures[0], true); - setTexture(0, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); - MeshShader::InstancedGrassPass1Shader::setUniforms(windDir, 0); + setTexture(MeshShader::InstancedGrassPass1ShaderInstance->TU_tex, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true); + MeshShader::InstancedGrassPass1ShaderInstance->setUniforms(windDir); glBindVertexArray(mesh.vao); glDrawElementsInstanced(ptype, count, itype, 0, instance_count); @@ -309,13 +309,13 @@ void STKInstancedSceneNode::render() drawFSPMDefault(*GeometricMesh[FPSM_DEFAULT_STANDARD][i], instance_pos.size() / 9); if (!GeometricMesh[FPSM_ALPHA_REF_TEXTURE].empty()) - glUseProgram(MeshShader::InstancedObjectRefPass1Shader::Program); + glUseProgram(MeshShader::InstancedObjectRefPass1ShaderInstance->Program); for (unsigned i = 0; i < GeometricMesh[FPSM_ALPHA_REF_TEXTURE].size(); i++) drawFSPMAlphaRefTexture(*GeometricMesh[FPSM_ALPHA_REF_TEXTURE][i], instance_pos.size() / 9); windDir = getWind(); if (!GeometricMesh[FPSM_GRASS].empty()) - glUseProgram(MeshShader::InstancedGrassPass1Shader::Program); + glUseProgram(MeshShader::InstancedGrassPass1ShaderInstance->Program); for (unsigned i = 0; i < GeometricMesh[FPSM_GRASS].size(); i++) drawFSPMGrass(*GeometricMesh[FPSM_GRASS][i], windDir, instance_pos.size() / 9); return;