Factorize all first pass materials.

This commit is contained in:
vlj 2014-07-12 00:21:30 +02:00
parent 5acc53685c
commit e44f68afa2
7 changed files with 132 additions and 141 deletions

View File

@ -53,6 +53,32 @@
#define MAX2(a, b) ((a) > (b) ? (a) : (b))
#define MIN2(a, b) ((a) > (b) ? (b) : (a))
template<unsigned N>
struct unroll_args
{
template<typename Shader, typename ...TupleTypes, typename ...Args>
static void exec(const std::tuple<TupleTypes...> &t, Args... args)
{
unroll_args<N - 1>::template exec<Shader>(t, std::get<N - 1>(t), args...);
}
};
template<>
struct unroll_args<0>
{
template<typename Shader, typename ...TupleTypes, typename ...Args>
static void exec(const std::tuple<TupleTypes...> &t, Args... args)
{
draw<Shader>(args...);
}
};
template<typename Shader, typename... TupleType>
void apply(const std::tuple<TupleType...> &arg)
{
unroll_args<std::tuple_size<std::tuple<TupleType...> >::value >::template exec<Shader>(arg);
}
void IrrDriver::renderGLSL(float dt)
{
World *world = World::getWorld(); // Never NULL.
@ -504,6 +530,32 @@ void IrrDriver::computeSunVisibility()
}
}
template<typename Shader, enum E_VERTEX_TYPE VertexType, typename... TupleType>
void renderMeshes1stPass(const std::vector<GLuint> &TexUnits, std::vector<std::tuple<TupleType...> > &meshes)
{
glUseProgram(Shader::Program);
glBindVertexArray(getVAO(VertexType));
for (unsigned i = 0; i < meshes.size(); i++)
{
GLMesh &mesh = *(std::get<0>(meshes[i]));
for (unsigned j = 0; j < TexUnits.size(); j++)
{
if (!mesh.textures[j])
mesh.textures[j] = getUnicolorTexture(video::SColor(255, 255, 255, 255));
compressTexture(mesh.textures[j], true);
setTexture(TexUnits[j], getTextureGLuint(mesh.textures[j]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true);
}
if (mesh.VAOType != VertexType)
{
#ifdef DEBUG
Log::error("Materials", "Wrong vertex Type associed to pass 1 (hint texture : %s)", mesh.textures[0]->getName().getPath().c_str());
#endif
continue;
}
apply<Shader>(meshes[i]);
}
}
void IrrDriver::renderSolidFirstPass()
{
m_rtts->getFBO(FBO_NORMAL_AND_DEPTHS).Bind();
@ -517,10 +569,10 @@ void IrrDriver::renderSolidFirstPass()
glDisable(GL_BLEND);
glEnable(GL_CULL_FACE);
irr_driver->setPhase(SOLID_NORMAL_AND_DEPTH_PASS);
GroupedFPSM<FPSM_DEFAULT_STANDARD>::reset();
GroupedFPSM<FPSM_DEFAULT_2TCOORD>::reset();
GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::reset();
GroupedFPSM<FPSM_NORMAL_MAP>::reset();
ListDefaultStandardG::Arguments.clear();
ListDefault2TCoordG::Arguments.clear();
ListAlphaRefG::Arguments.clear();
ListNormalG::Arguments.clear();
m_scene_manager->drawAll(scene::ESNRP_SOLID);
if (!UserConfigParams::m_dynamic_lights)
@ -528,87 +580,11 @@ void IrrDriver::renderSolidFirstPass()
{
ScopedGPUTimer Timer(getGPUTimer(Q_SOLID_PASS1));
glUseProgram(MeshShader::ObjectPass1Shader::Program);
glBindVertexArray(getVAO(video::EVT_STANDARD));
for (unsigned i = 0; i < GroupedFPSM<FPSM_DEFAULT_STANDARD>::MeshSet.size(); ++i)
{
GLMesh &mesh = *GroupedFPSM<FPSM_DEFAULT_STANDARD>::MeshSet[i];
if (!mesh.textures[0])
mesh.textures[0] = getUnicolorTexture(video::SColor(255, 255, 255, 255));
compressTexture(mesh.textures[0], true);
setTexture(0, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true);
draw<MeshShader::ObjectPass1Shader>(&mesh, GroupedFPSM<FPSM_DEFAULT_STANDARD>::MVPSet[i], GroupedFPSM<FPSM_DEFAULT_STANDARD>::TIMVSet[i], 0);
renderMeshes1stPass<MeshShader::ObjectPass1Shader, video::EVT_STANDARD>({ MeshShader::ObjectPass1Shader::TU_tex }, ListDefaultStandardG::Arguments);
renderMeshes1stPass<MeshShader::ObjectPass1Shader, video::EVT_2TCOORDS>({ MeshShader::ObjectPass1Shader::TU_tex }, ListDefault2TCoordG::Arguments);
renderMeshes1stPass<MeshShader::ObjectRefPass1Shader, video::EVT_STANDARD>({ MeshShader::ObjectRefPass1Shader::TU_tex }, ListAlphaRefG::Arguments);
renderMeshes1stPass<MeshShader::NormalMapShader, video::EVT_TANGENTS>({ MeshShader::NormalMapShader::TU_glossy, MeshShader::NormalMapShader::TU_normalmap }, ListNormalG::Arguments);
}
glBindVertexArray(getVAO(video::EVT_2TCOORDS));
for (unsigned i = 0; i < GroupedFPSM<FPSM_DEFAULT_2TCOORD>::MeshSet.size(); ++i)
{
GLMesh &mesh = *GroupedFPSM<FPSM_DEFAULT_2TCOORD>::MeshSet[i];
if (!mesh.textures[0])
mesh.textures[0] = getUnicolorTexture(video::SColor(255, 255, 255, 255));
compressTexture(mesh.textures[0], true);
setTexture(0, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true);
draw<MeshShader::ObjectPass1Shader>(&mesh, GroupedFPSM<FPSM_DEFAULT_2TCOORD>::MVPSet[i], GroupedFPSM<FPSM_DEFAULT_2TCOORD>::TIMVSet[i], 0);
}
glUseProgram(MeshShader::ObjectRefPass1Shader::Program);
glBindVertexArray(getVAO(EVT_STANDARD));
for (unsigned i = 0; i < GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::MeshSet.size(); ++i)
{
GLMesh &mesh = *GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::MeshSet[i];
if (mesh.VAOType != video::EVT_STANDARD)
{
#ifdef DEBUG
Log::error("Materials", "Wrong vertex Type associed to alpha ref pass 1 (hint texture : %s)", mesh.textures[0]->getName().getPath().c_str());
#endif
continue;
}
compressTexture(mesh.textures[0], true);
setTexture(0, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true);
draw<MeshShader::ObjectRefPass1Shader>(&mesh, GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::MVPSet[i], GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::TIMVSet[i], GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::MeshSet[i]->TextureMatrix, 0);
}
glUseProgram(MeshShader::NormalMapShader::Program);
glBindVertexArray(getVAO(EVT_TANGENTS));
for (unsigned i = 0; i < GroupedFPSM<FPSM_NORMAL_MAP>::MeshSet.size(); ++i)
{
GLMesh &mesh = *GroupedFPSM<FPSM_NORMAL_MAP>::MeshSet[i];
assert(mesh.VAOType == video::EVT_TANGENTS);
assert(mesh.textures[1]);
compressTexture(mesh.textures[1], false);
setTexture(0, getTextureGLuint(mesh.textures[1]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true);
compressTexture(mesh.textures[0], true);
setTexture(1, getTextureGLuint(mesh.textures[0]), GL_LINEAR, GL_LINEAR_MIPMAP_LINEAR, true);
draw<MeshShader::NormalMapShader>(&mesh, GroupedFPSM<FPSM_NORMAL_MAP>::MVPSet[i], GroupedFPSM<FPSM_NORMAL_MAP>::TIMVSet[i], 0, 1);
}
}
}
template<unsigned N>
struct unroll_args
{
template<typename Shader, typename ...TupleTypes, typename ...Args>
static void exec(const std::tuple<TupleTypes...> &t, Args... args)
{
unroll_args<N - 1>::template exec<Shader>(t, std::get<N - 1>(t), args...);
}
};
template<>
struct unroll_args<0>
{
template<typename Shader, typename ...TupleTypes, typename ...Args>
static void exec(const std::tuple<TupleTypes...> &t, Args... args)
{
draw<Shader>(args...);
}
};
template<typename Shader, typename... TupleType>
void apply(const std::tuple<TupleType...> &arg)
{
unroll_args<std::tuple_size<std::tuple<TupleType...> >::value >::template exec<Shader>(arg);
}
template<typename Shader, enum E_VERTEX_TYPE VertexType, typename... TupleType>

View File

@ -451,7 +451,7 @@ namespace MeshShader
GLuint ObjectPass1Shader::Program;
GLuint ObjectPass1Shader::uniform_MM;
GLuint ObjectPass1Shader::uniform_IMM;
GLuint ObjectPass1Shader::uniform_tex;
GLuint ObjectPass1Shader::TU_tex;
void ObjectPass1Shader::init()
{
@ -461,28 +461,31 @@ namespace MeshShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/object_pass1.frag").c_str());
uniform_MM = glGetUniformLocation(Program, "ModelMatrix");
uniform_IMM = glGetUniformLocation(Program, "InverseModelMatrix");
uniform_tex = glGetUniformLocation(Program, "tex");
GLuint uniform_tex = glGetUniformLocation(Program, "tex");
if (!UserConfigParams::m_ubo_disabled)
{
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
}
TU_tex = 0;
glUseProgram(Program);
glUniform1i(uniform_tex, TU_tex);
glUseProgram(0);
}
void ObjectPass1Shader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix, unsigned TU_tex)
void ObjectPass1Shader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix)
{
if (UserConfigParams::m_ubo_disabled)
bypassUBO(Program);
glUniformMatrix4fv(uniform_MM, 1, GL_FALSE, ModelMatrix.pointer());
glUniformMatrix4fv(uniform_IMM, 1, GL_FALSE, InverseModelMatrix.pointer());
glUniform1i(uniform_tex, TU_tex);
}
GLuint ObjectRefPass1Shader::Program;
GLuint ObjectRefPass1Shader::uniform_MM;
GLuint ObjectRefPass1Shader::uniform_IMM;
GLuint ObjectRefPass1Shader::uniform_TM;
GLuint ObjectRefPass1Shader::uniform_tex;
GLuint ObjectRefPass1Shader::TU_tex;
void ObjectRefPass1Shader::init()
{
@ -493,22 +496,25 @@ namespace MeshShader
uniform_MM = glGetUniformLocation(Program, "ModelMatrix");
uniform_IMM = glGetUniformLocation(Program, "InverseModelMatrix");
uniform_TM = glGetUniformLocation(Program, "TextureMatrix");
uniform_tex = glGetUniformLocation(Program, "tex");
GLuint uniform_tex = glGetUniformLocation(Program, "tex");
if (!UserConfigParams::m_ubo_disabled)
{
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
}
TU_tex = 0;
glUseProgram(Program);
glUniform1i(uniform_tex, TU_tex);
glUseProgram(0);
}
void ObjectRefPass1Shader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix, const core::matrix4 &TextureMatrix, unsigned TU_tex)
void ObjectRefPass1Shader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix, 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());
glUniformMatrix4fv(uniform_IMM, 1, GL_FALSE, InverseModelMatrix.pointer());
glUniform1i(uniform_tex, TU_tex);
}
GLuint GrassPass1Shader::Program;
@ -540,8 +546,8 @@ namespace MeshShader
GLuint NormalMapShader::Program;
GLuint NormalMapShader::uniform_MM;
GLuint NormalMapShader::uniform_IMM;
GLuint NormalMapShader::uniform_normalMap;
GLuint NormalMapShader::uniform_DiffuseForAlpha;
GLuint NormalMapShader::TU_normalmap;
GLuint NormalMapShader::TU_glossy;
void NormalMapShader::init()
{
@ -551,23 +557,27 @@ namespace MeshShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/normalmap.frag").c_str());
uniform_MM = glGetUniformLocation(Program, "ModelMatrix");
uniform_IMM = glGetUniformLocation(Program, "InverseModelMatrix");
uniform_normalMap = glGetUniformLocation(Program, "normalMap");
uniform_DiffuseForAlpha = glGetUniformLocation(Program, "DiffuseForAlpha");
GLuint uniform_normalMap = glGetUniformLocation(Program, "normalMap");
GLuint uniform_DiffuseForAlpha = glGetUniformLocation(Program, "DiffuseForAlpha");
if (!UserConfigParams::m_ubo_disabled)
{
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
}
TU_normalmap = 1;
TU_glossy = 0;
glUseProgram(Program);
glUniform1i(uniform_normalMap, TU_normalmap);
glUniform1i(uniform_DiffuseForAlpha, TU_glossy);
glUseProgram(0);
}
void NormalMapShader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix, unsigned TU_normalMap, unsigned TU_uniform_DiffuseForAlpha)
void NormalMapShader::setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix)
{
if (UserConfigParams::m_ubo_disabled)
bypassUBO(Program);
glUniformMatrix4fv(uniform_MM, 1, GL_FALSE, ModelMatrix.pointer());
glUniformMatrix4fv(uniform_IMM, 1, GL_FALSE, InverseModelMatrix.pointer());
glUniform1i(uniform_normalMap, TU_normalMap);
glUniform1i(uniform_DiffuseForAlpha, TU_uniform_DiffuseForAlpha);
}
GLuint InstancedObjectPass1Shader::Program;

View File

@ -53,20 +53,22 @@ class ObjectPass1Shader
{
public:
static GLuint Program;
static GLuint uniform_MM, uniform_IMM, uniform_tex;
static GLuint uniform_MM, uniform_IMM;
static GLuint TU_tex;
static void init();
static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix, unsigned TU_tex);
static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix);
};
class ObjectRefPass1Shader
{
public:
static GLuint Program;
static GLuint uniform_MM, uniform_TM, uniform_IMM, uniform_tex;
static GLuint uniform_MM, uniform_TM, uniform_IMM;
static GLuint TU_tex;
static void init();
static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix, const core::matrix4 &TextureMatrix, unsigned TU_texture);
static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix, const core::matrix4 &TextureMatrix);
};
class GrassPass1Shader
@ -83,10 +85,11 @@ class NormalMapShader
{
public:
static GLuint Program;
static GLuint uniform_MM, uniform_IMM, uniform_normalMap, uniform_DiffuseForAlpha;
static GLuint uniform_MM, uniform_IMM;
static GLuint TU_normalmap, TU_glossy;
static void init();
static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix, unsigned TU_normalMap, unsigned TU_uniform_DiffuseForAlpha);
static void setUniforms(const core::matrix4 &ModelMatrix, const core::matrix4 &InverseModelMatrix);
};
class InstancedObjectPass1Shader

View File

@ -146,25 +146,13 @@ void STKAnimatedMesh::render()
GLMesh* mesh;
for_in(mesh, GeometricMesh[FPSM_DEFAULT_STANDARD])
{
GroupedFPSM<FPSM_DEFAULT_STANDARD>::MeshSet.push_back(mesh);
GroupedFPSM<FPSM_DEFAULT_STANDARD>::MVPSet.push_back(AbsoluteTransformation);
GroupedFPSM<FPSM_DEFAULT_STANDARD>::TIMVSet.push_back(invmodel);
}
ListDefaultStandardG::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, invmodel));
for_in(mesh, GeometricMesh[FPSM_DEFAULT_2TCOORD])
{
GroupedFPSM<FPSM_DEFAULT_2TCOORD>::MeshSet.push_back(mesh);
GroupedFPSM<FPSM_DEFAULT_2TCOORD>::MVPSet.push_back(AbsoluteTransformation);
GroupedFPSM<FPSM_DEFAULT_2TCOORD>::TIMVSet.push_back(invmodel);
}
ListDefault2TCoordG::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, invmodel));
for_in(mesh, GeometricMesh[FPSM_ALPHA_REF_TEXTURE])
{
GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::MeshSet.push_back(mesh);
GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::MVPSet.push_back(AbsoluteTransformation);
GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::TIMVSet.push_back(invmodel);
}
ListAlphaRefG::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, invmodel, mesh->TextureMatrix));
return;
}

View File

@ -467,6 +467,11 @@ void initvaostate(GLMesh &mesh, TransparentMaterial TranspMat)
mesh.vao = createVAO(mesh.vertex_buffer, mesh.index_buffer, getVTXTYPEFromStride(mesh.Stride));
}
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > ListDefaultStandardG::Arguments;
std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > ListDefault2TCoordG::Arguments;
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;

View File

@ -71,6 +71,31 @@ bool isObject(video::E_MATERIAL_TYPE type);
core::vector3df getWind();
// Pass 1 shader (ie shaders that outputs normals and depth)
class ListDefaultStandardG
{
public:
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > Arguments;
};
class ListDefault2TCoordG
{
public:
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > Arguments;
};
class ListAlphaRefG
{
public:
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4, core::matrix4> > Arguments;
};
class ListNormalG
{
public:
static std::vector<std::tuple<GLMesh *, core::matrix4, core::matrix4> > Arguments;
};
template<enum GeometricMaterial T>
class GroupedFPSM
{

View File

@ -268,7 +268,7 @@ void STKMeshSceneNode::render()
GLenum itype = mesh.IndexType;
size_t count = mesh.IndexCount;
MeshShader::ObjectPass1Shader::setUniforms(AbsoluteTransformation, invmodel, 0);
MeshShader::ObjectPass1Shader::setUniforms(AbsoluteTransformation, invmodel);
assert(mesh.vao);
glBindVertexArray(mesh.vao);
glDrawElements(ptype, count, itype, 0);
@ -281,32 +281,16 @@ void STKMeshSceneNode::render()
GLMesh* mesh;
for_in(mesh, GeometricMesh[FPSM_DEFAULT_STANDARD])
{
GroupedFPSM<FPSM_DEFAULT_STANDARD>::MeshSet.push_back(mesh);
GroupedFPSM<FPSM_DEFAULT_STANDARD>::MVPSet.push_back(AbsoluteTransformation);
GroupedFPSM<FPSM_DEFAULT_STANDARD>::TIMVSet.push_back(invmodel);
}
ListDefaultStandardG::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, invmodel));
for_in(mesh, GeometricMesh[FPSM_DEFAULT_2TCOORD])
{
GroupedFPSM<FPSM_DEFAULT_2TCOORD>::MeshSet.push_back(mesh);
GroupedFPSM<FPSM_DEFAULT_2TCOORD>::MVPSet.push_back(AbsoluteTransformation);
GroupedFPSM<FPSM_DEFAULT_2TCOORD>::TIMVSet.push_back(invmodel);
}
ListDefault2TCoordG::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, invmodel));
for_in(mesh, GeometricMesh[FPSM_ALPHA_REF_TEXTURE])
{
GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::MeshSet.push_back(mesh);
GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::MVPSet.push_back(AbsoluteTransformation);
GroupedFPSM<FPSM_ALPHA_REF_TEXTURE>::TIMVSet.push_back(invmodel);
}
ListAlphaRefG::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, invmodel, mesh->TextureMatrix));
for_in(mesh, GeometricMesh[FPSM_NORMAL_MAP])
{
GroupedFPSM<FPSM_NORMAL_MAP>::MeshSet.push_back(mesh);
GroupedFPSM<FPSM_NORMAL_MAP>::MVPSet.push_back(AbsoluteTransformation);
GroupedFPSM<FPSM_NORMAL_MAP>::TIMVSet.push_back(invmodel);
}
ListNormalG::Arguments.push_back(std::make_tuple(mesh, AbsoluteTransformation, invmodel));
if (!GeometricMesh[FPSM_GRASS].empty())
glUseProgram(MeshShader::GrassPass1Shader::Program);