Factorize some pass2 shaders init code

This commit is contained in:
vlj 2014-07-13 02:18:20 +02:00
parent a249ee8835
commit 12af5eee9a
7 changed files with 57 additions and 85 deletions

View File

@ -612,10 +612,10 @@ void IrrDriver::renderSolidFirstPass()
}
}
template<typename Shader, enum E_VERTEX_TYPE VertexType, typename... TupleType>
void renderMeshes2ndPass(const std::vector<GLuint> &TexUnits, std::vector<std::tuple<TupleType...> > &meshes)
template<typename T, enum E_VERTEX_TYPE VertexType, typename... TupleType>
void renderMeshes2ndPass(const T *Shader, const std::vector<GLuint> &TexUnits, std::vector<std::tuple<TupleType...> > &meshes)
{
glUseProgram(Shader::Program);
glUseProgram(Shader->Program);
glBindVertexArray(getVAO(VertexType));
for (unsigned i = 0; i < meshes.size(); i++)
{
@ -645,7 +645,7 @@ void renderMeshes2ndPass(const std::vector<GLuint> &TexUnits, std::vector<std::t
#endif
continue;
}
apply<Shader>(meshes[i]);
apply_instance<T>(Shader, meshes[i]);
}
}
@ -687,13 +687,13 @@ void IrrDriver::renderSolidSecondPass()
m_scene_manager->drawAll(scene::ESNRP_SOLID);
renderMeshes2ndPass<MeshShader::ObjectPass2Shader, video::EVT_STANDARD>({ MeshShader::ObjectPass2Shader::TU_Albedo }, ListDefaultStandardSM::Arguments);
renderMeshes2ndPass<MeshShader::ObjectPass2Shader, video::EVT_TANGENTS>({ MeshShader::ObjectPass2Shader::TU_Albedo }, ListDefaultTangentSM::Arguments);
renderMeshes2ndPass<MeshShader::ObjectRefPass2Shader, video::EVT_STANDARD>({ MeshShader::ObjectRefPass2Shader::TU_Albedo }, ListAlphaRefSM::Arguments);
renderMeshes2ndPass<MeshShader::SphereMapShader, video::EVT_STANDARD>({ MeshShader::SphereMapShader::TU_tex }, ListSphereMapSM::Arguments);
renderMeshes2ndPass<MeshShader::ObjectUnlitShader, video::EVT_STANDARD>({ MeshShader::ObjectUnlitShader::TU_tex }, ListUnlitSM::Arguments);
renderMeshes2ndPass<MeshShader::DetailledObjectPass2Shader, video::EVT_2TCOORDS>({ MeshShader::DetailledObjectPass2Shader::TU_Albedo, MeshShader::DetailledObjectPass2Shader::TU_detail }, ListDetailSM::Arguments);
renderMeshes2ndPass<MeshShader::SplattingShader, video::EVT_2TCOORDS>({ 8, MeshShader::SplattingShader::TU_tex_layout, MeshShader::SplattingShader::TU_tex_detail0, MeshShader::SplattingShader::TU_tex_detail1, MeshShader::SplattingShader::TU_tex_detail2, MeshShader::SplattingShader::TU_tex_detail3 }, ListSplattingSM::Arguments);
renderMeshes2ndPass<MeshShader::ObjectPass2Shader, video::EVT_STANDARD>(MeshShader::ObjectPass2ShaderInstance, { MeshShader::ObjectPass2ShaderInstance->TU_Albedo }, ListDefaultStandardSM::Arguments);
renderMeshes2ndPass<MeshShader::ObjectPass2Shader, video::EVT_TANGENTS>(MeshShader::ObjectPass2ShaderInstance, { MeshShader::ObjectPass2ShaderInstance->TU_Albedo }, ListDefaultTangentSM::Arguments);
renderMeshes2ndPass<MeshShader::ObjectRefPass2Shader, video::EVT_STANDARD>(MeshShader::ObjectRefPass2ShaderInstance, { MeshShader::ObjectRefPass2ShaderInstance->TU_Albedo }, ListAlphaRefSM::Arguments);
// renderMeshes2ndPass<MeshShader::SphereMapShader, video::EVT_STANDARD>({ MeshShader::SphereMapShader::TU_tex }, ListSphereMapSM::Arguments);
// renderMeshes2ndPass<MeshShader::ObjectUnlitShader, video::EVT_STANDARD>({ MeshShader::ObjectUnlitShader::TU_tex }, ListUnlitSM::Arguments);
// renderMeshes2ndPass<MeshShader::DetailledObjectPass2Shader, video::EVT_2TCOORDS>({ MeshShader::DetailledObjectPass2Shader::TU_Albedo, MeshShader::DetailledObjectPass2Shader::TU_detail }, ListDetailSM::Arguments);
// renderMeshes2ndPass<MeshShader::SplattingShader, video::EVT_2TCOORDS>({ 8, MeshShader::SplattingShader::TU_tex_layout, MeshShader::SplattingShader::TU_tex_detail0, MeshShader::SplattingShader::TU_tex_detail1, MeshShader::SplattingShader::TU_tex_detail2, MeshShader::SplattingShader::TU_tex_detail3 }, ListSplattingSM::Arguments);
}
}
@ -717,16 +717,16 @@ void IrrDriver::renderTransparent()
if (World::getWorld() && World::getWorld()->isFogEnabled())
{
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
renderMeshes2ndPass<MeshShader::TransparentFogShader, video::EVT_STANDARD>({ MeshShader::TransparentFogShader::TU_tex }, ListBlendTransparentFog::Arguments);
// renderMeshes2ndPass<MeshShader::TransparentFogShader, video::EVT_STANDARD>({ MeshShader::TransparentFogShader::TU_tex }, ListBlendTransparentFog::Arguments);
glBlendFunc(GL_ONE, GL_ONE);
renderMeshes2ndPass<MeshShader::TransparentFogShader, video::EVT_STANDARD>({ MeshShader::TransparentFogShader::TU_tex }, ListAdditiveTransparentFog::Arguments);
// renderMeshes2ndPass<MeshShader::TransparentFogShader, video::EVT_STANDARD>({ MeshShader::TransparentFogShader::TU_tex }, ListAdditiveTransparentFog::Arguments);
}
else
{
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
renderMeshes2ndPass<MeshShader::TransparentShader, video::EVT_STANDARD>({ MeshShader::TransparentShader::TU_tex }, ListBlendTransparent::Arguments);
// renderMeshes2ndPass<MeshShader::TransparentShader, video::EVT_STANDARD>({ MeshShader::TransparentShader::TU_tex }, ListBlendTransparent::Arguments);
glBlendFunc(GL_ONE, GL_ONE);
renderMeshes2ndPass<MeshShader::TransparentShader, video::EVT_STANDARD>({ MeshShader::TransparentShader::TU_tex }, ListAdditiveTransparent::Arguments);
// renderMeshes2ndPass<MeshShader::TransparentShader, video::EVT_STANDARD>({ MeshShader::TransparentShader::TU_tex }, ListAdditiveTransparent::Arguments);
}
}

View File

@ -331,12 +331,12 @@ void Shaders::loadShaders()
MeshShader::InstancedObjectPass1Shader::init();
MeshShader::InstancedObjectRefPass1Shader::init();
MeshShader::InstancedGrassPass1Shader::init();
MeshShader::ObjectPass2Shader::init();
MeshShader::ObjectPass2ShaderInstance = new MeshShader::ObjectPass2Shader();
MeshShader::InstancedObjectPass2Shader::init();
MeshShader::InstancedObjectRefPass2Shader::init();
MeshShader::InstancedGrassPass2Shader::init();
MeshShader::DetailledObjectPass2Shader::init();
MeshShader::ObjectRefPass2Shader::init();
MeshShader::ObjectRefPass2ShaderInstance = new MeshShader::ObjectRefPass2Shader();
MeshShader::ObjectUnlitShader::init();
MeshShader::SphereMapShader::init();
MeshShader::SplattingShader::init();
@ -630,22 +630,13 @@ namespace MeshShader
}
// Solid Lit pass shaders
GLuint ObjectPass2Shader::Program;
GLuint ObjectPass2Shader::uniform_MM;
GLuint ObjectPass2Shader::uniform_TM;
GLuint ObjectPass2Shader::uniform_ambient;
GLuint ObjectPass2Shader::TU_Albedo;
void ObjectPass2Shader::init()
ObjectPass2Shader::ObjectPass2Shader()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/object_pass.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass2.frag").c_str());
uniform_MM = glGetUniformLocation(Program, "ModelMatrix");
uniform_TM = glGetUniformLocation(Program, "TextureMatrix");
uniform_ambient = glGetUniformLocation(Program, "ambient");
AssignUniforms(Program, uniforms, { "ModelMatrix", "TextureMatrix", "ambient" });
if (!UserConfigParams::m_ubo_disabled)
{
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
@ -656,15 +647,7 @@ namespace MeshShader
AssignTextureUnit(Program, { { 0, "DiffuseMap" }, { 1, "SpecularMap" }, { 2, "SSAO" }, { TU_Albedo, "Albedo" } });
}
void ObjectPass2Shader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix)
{
if (UserConfigParams::m_ubo_disabled)
bypassUBO(Program);
glUniformMatrix4fv(uniform_MM, 1, GL_FALSE, ModelMatrix.pointer());
glUniformMatrix4fv(uniform_TM, 1, GL_FALSE, TextureMatrix.pointer());
const video::SColorf s = irr_driver->getSceneManager()->getAmbientLight();
glUniform3f(uniform_ambient, s.r, s.g, s.b);
}
ObjectPass2Shader *ObjectPass2ShaderInstance;
GLuint InstancedObjectPass2Shader::Program;
GLuint InstancedObjectPass2Shader::uniform_VP;
@ -798,22 +781,13 @@ namespace MeshShader
glUniformMatrix4fv(uniform_MM, 1, GL_FALSE, ModelMatrix.pointer());
}
GLuint ObjectRefPass2Shader::Program;
GLuint ObjectRefPass2Shader::uniform_MM;
GLuint ObjectRefPass2Shader::uniform_TM;
GLuint ObjectRefPass2Shader::uniform_ambient;
GLuint ObjectRefPass2Shader::TU_Albedo;
void ObjectRefPass2Shader::init()
ObjectRefPass2Shader::ObjectRefPass2Shader()
{
initGL();
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/object_pass.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getLightFactor.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/objectref_pass2.frag").c_str());
uniform_MM = glGetUniformLocation(Program, "ModelMatrix");
uniform_TM = glGetUniformLocation(Program, "TextureMatrix");
uniform_ambient = glGetUniformLocation(Program, "ambient");
AssignUniforms(Program, uniforms, { "ModelMatrix", "TextureMatrix", "ambient" });
if (!UserConfigParams::m_ubo_disabled)
{
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
@ -824,16 +798,7 @@ namespace MeshShader
AssignTextureUnit(Program, { { 0, "DiffuseMap" }, { 1, "SpecularMap" }, { 2, "SSAO" }, { TU_Albedo, "Albedo" } });
}
void ObjectRefPass2Shader::setUniforms(const core::matrix4 &ModelMatrix,
const core::matrix4 &TextureMatrix)
{
if (UserConfigParams::m_ubo_disabled)
bypassUBO(Program);
glUniformMatrix4fv(uniform_MM, 1, GL_FALSE, ModelMatrix.pointer());
glUniformMatrix4fv(uniform_TM, 1, GL_FALSE, TextureMatrix.pointer());
const video::SColorf s = irr_driver->getSceneManager()->getAmbientLight();
glUniform3f(uniform_ambient, s.r, s.g, s.b);
}
ObjectRefPass2Shader *ObjectRefPass2ShaderInstance;
GLuint GrassPass2Shader::Program;
GLuint GrassPass2Shader::uniform_MVP;

View File

@ -65,6 +65,15 @@ void setUniformsHelper(const std::vector<GLuint> &uniforms, const core::matrix4
setUniformsHelper<N + 1>(uniforms, arg...);
}
void glUniform3fWraper(GLuint, size_t, unsigned, const float *mat);
template<unsigned N = 0, typename... Args>
void setUniformsHelper(const std::vector<GLuint> &uniforms, const video::SColorf &col, Args... arg)
{
glUniform3f(uniforms[N], col.r, col.g, col.b);
setUniformsHelper<N + 1>(uniforms, arg...);
}
void bypassUBO(GLuint Program);
template<typename... Args>
@ -153,17 +162,16 @@ public:
static void setUniforms(const core::vector3df &windDir, unsigned TU_tex);
};
class ObjectPass2Shader
class ObjectPass2Shader : public ShaderHelper<core::matrix4, core::matrix4, video::SColorf>
{
public:
static GLuint Program;
static GLuint uniform_MM, uniform_TM, uniform_ambient;
static GLuint TU_Albedo;
GLuint TU_Albedo;
static void init();
static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix);
ObjectPass2Shader();
};
extern ObjectPass2Shader *ObjectPass2ShaderInstance;
class InstancedObjectPass2Shader
{
public:
@ -208,17 +216,16 @@ public:
static void setUniforms(const core::matrix4 &ModelMatrix);
};
class ObjectRefPass2Shader
class ObjectRefPass2Shader : public ShaderHelper<core::matrix4, core::matrix4, video::SColorf>
{
public:
static GLuint Program;
static GLuint uniform_MM, uniform_TM, uniform_ambient;
static GLuint TU_Albedo;
GLuint TU_Albedo;
static void init();
static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &TextureMatrix);
ObjectRefPass2Shader();
};
extern ObjectRefPass2Shader *ObjectRefPass2ShaderInstance;
class GrassPass2Shader
{
public:

View File

@ -164,13 +164,13 @@ void STKAnimatedMesh::render()
GLMesh* mesh;
for_in(mesh, ShadedMesh[SM_DEFAULT_STANDARD])
ListDefaultStandardSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix));
ListDefaultStandardSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight()));
for_in(mesh, ShadedMesh[SM_DEFAULT_TANGENT])
ListDefaultTangentSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix));
ListDefaultTangentSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight()));
for_in(mesh, ShadedMesh[SM_ALPHA_REF_TEXTURE])
ListAlphaRefSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix));
ListAlphaRefSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight()));
for_in (mesh, ShadedMesh[SM_UNLIT])
ListUnlitSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation));

View File

@ -369,9 +369,9 @@ std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > ListDefault2TCo
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, core::matrix4> > ListAlphaRefG::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > ListNormalG::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > ListDefaultStandardSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > ListDefaultTangentSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > ListAlphaRefSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, video::SColorf> > ListDefaultStandardSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, video::SColorf> > ListDefaultTangentSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, video::SColorf> > ListAlphaRefSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4> > ListSplattingSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, video::SColorf> > ListSphereMapSM::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4> > ListUnlitSM::Arguments;

View File

@ -149,19 +149,19 @@ void drawGrassPass1(const GLMesh &mesh, const core::matrix4 & ModelViewProjectio
class ListDefaultStandardSM
{
public:
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > Arguments;
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, video::SColorf> > Arguments;
};
class ListDefaultTangentSM
{
public:
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > Arguments;
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, video::SColorf> > Arguments;
};
class ListAlphaRefSM
{
public:
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > Arguments;
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, video::SColorf> > Arguments;
};
class ListSphereMapSM

View File

@ -314,7 +314,7 @@ void STKMeshSceneNode::render()
glDisable(GL_CULL_FACE);
if (!spareWhiteTex)
spareWhiteTex = getUnicolorTexture(video::SColor(255, 255, 255, 255));
glUseProgram(MeshShader::ObjectPass2Shader::Program);
glUseProgram(MeshShader::ObjectPass2ShaderInstance->Program);
// Only untextured
for (unsigned i = 0; i < GLmeshes.size(); i++)
{
@ -324,8 +324,8 @@ void STKMeshSceneNode::render()
GLenum itype = mesh.IndexType;
size_t count = mesh.IndexCount;
setTexture(MeshShader::ObjectPass2Shader::TU_Albedo, getTextureGLuint(spareWhiteTex), GL_NEAREST, GL_NEAREST, false);
MeshShader::ObjectPass2Shader::setUniforms(AbsoluteTransformation, mesh.TextureMatrix);
setTexture(MeshShader::ObjectPass2ShaderInstance->TU_Albedo, getTextureGLuint(spareWhiteTex), GL_NEAREST, GL_NEAREST, false);
MeshShader::ObjectPass2ShaderInstance->setUniforms(AbsoluteTransformation, mesh.TextureMatrix, irr_driver->getSceneManager()->getAmbientLight());
assert(mesh.vao);
glBindVertexArray(mesh.vao);
glDrawElements(ptype, count, itype, 0);
@ -337,13 +337,13 @@ void STKMeshSceneNode::render()
GLMesh* mesh;
for_in(mesh, ShadedMesh[SM_DEFAULT_STANDARD])
ListDefaultStandardSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix));
ListDefaultStandardSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight()));
for_in(mesh, ShadedMesh[SM_DEFAULT_TANGENT])
ListDefaultTangentSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix));
ListDefaultTangentSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight()));
for_in(mesh, ShadedMesh[SM_ALPHA_REF_TEXTURE])
ListAlphaRefSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix));
ListAlphaRefSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, mesh->TextureMatrix, irr_driver->getSceneManager()->getAmbientLight()));
for_in(mesh, ShadedMesh[SM_SPHEREMAP])
ListSphereMapSM::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, invmodel, irr_driver->getSceneManager()->getAmbientLight()));