Port sunlight* shaders to singleton.

This commit is contained in:
Vincent Lejeune 2014-08-07 20:07:03 +02:00
parent 160d061e84
commit e932638aef
4 changed files with 30 additions and 188 deletions

View File

@ -1,58 +0,0 @@
uniform sampler2D ntex;
uniform sampler2D dtex;
uniform sampler2DArrayShadow shadowtex;
uniform vec3 direction;
uniform vec3 col;
in vec2 uv;
out vec4 Diff;
out vec4 Spec;
vec3 DecodeNormal(vec2 n);
vec3 getSpecular(vec3 normal, vec3 eyedir, vec3 lightdir, vec3 color, float roughness);
vec3 getShadowFactor(vec3 pos)
{
vec3 cascadeColor[] = vec3[](
vec3(1., 0., 0.),
vec3(0., 1., 0.),
vec3(0., 0., 1.),
vec3(1., 1., 1.)
);
for (int i = 0; i < 4; i++)
{
vec4 shadowcoord = (ShadowViewProjMatrixes[i] * vec4(pos, 1.0));
shadowcoord /= shadowcoord.w;
vec2 shadowtexcoord = shadowcoord.xy * 0.5 + 0.5;
if (shadowtexcoord.x < 0. || shadowtexcoord.x > 1. || shadowtexcoord.y < 0. || shadowtexcoord.y > 1.)
continue;
return cascadeColor[i] * texture(shadowtex, vec4(shadowtexcoord, float(i), 0.5 * shadowcoord.z + 0.5));
}
return vec3(1.);
}
void main() {
float z = texture(dtex, uv).x;
vec4 xpos = 2.0 * vec4(uv, z, 1.0) - 1.0;
xpos = InverseViewMatrix * InverseProjectionMatrix * xpos;
xpos.xyz /= xpos.w;
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
float roughness =texture(ntex, uv).z;
vec3 eyedir = -normalize(xpos.xyz);
// Normalized on the cpu
vec3 L = direction;
float NdotL = max(0., dot(norm, L));
vec3 Specular = getSpecular(norm, eyedir, L, col, roughness) * NdotL;
vec3 outcol = NdotL * col;
// Shadows
vec3 factor = getShadowFactor(xpos.xyz);
Diff = vec4(factor * NdotL * col, 1.);
Spec = vec4(factor * Specular, 1.);
return;
}

View File

@ -278,11 +278,11 @@ void PostProcessing::renderSunlight()
glBlendFunc(GL_ONE, GL_ONE);
glBlendEquation(GL_FUNC_ADD);
glUseProgram(FullScreenShader::SunLightShader::Program);
glBindVertexArray(FullScreenShader::SunLightShader::vao);
setTexture(0, irr_driver->getRenderTargetTexture(RTT_NORMAL_AND_DEPTH), GL_NEAREST, GL_NEAREST);
setTexture(1, irr_driver->getDepthStencilTexture(), GL_NEAREST, GL_NEAREST);
FullScreenShader::SunLightShader::setUniforms(cb->getPosition(), cb->getRed(), cb->getGreen(), cb->getBlue(), 0, 1);
glUseProgram(FullScreenShader::SunLightShader::getInstance()->Program);
glBindVertexArray(FullScreenShader::SunLightShader::getInstance()->vao);
setTexture(FullScreenShader::SunLightShader::getInstance()->TU_ntex, irr_driver->getRenderTargetTexture(RTT_NORMAL_AND_DEPTH), GL_NEAREST, GL_NEAREST);
setTexture(FullScreenShader::SunLightShader::getInstance()->TU_dtex, irr_driver->getDepthStencilTexture(), GL_NEAREST, GL_NEAREST);
FullScreenShader::SunLightShader::getInstance()->setUniforms(cb->getPosition(), video::SColorf(cb->getRed(), cb->getGreen(), cb->getBlue()));
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}
@ -296,9 +296,9 @@ void PostProcessing::renderShadowedSunlight(const std::vector<core::matrix4> &su
glBlendFunc(GL_ONE, GL_ONE);
glBlendEquation(GL_FUNC_ADD);
setTexture(0, irr_driver->getRenderTargetTexture(RTT_NORMAL_AND_DEPTH), GL_NEAREST, GL_NEAREST);
setTexture(1, irr_driver->getDepthStencilTexture(), GL_NEAREST, GL_NEAREST);
glActiveTexture(GL_TEXTURE2);
setTexture(FullScreenShader::ShadowedSunLightShader::getInstance()->TU_ntex, irr_driver->getRenderTargetTexture(RTT_NORMAL_AND_DEPTH), GL_NEAREST, GL_NEAREST);
setTexture(FullScreenShader::ShadowedSunLightShader::getInstance()->TU_dtex, irr_driver->getDepthStencilTexture(), GL_NEAREST, GL_NEAREST);
glActiveTexture(GL_TEXTURE0 + FullScreenShader::ShadowedSunLightShader::getInstance()->TU_shadowtex);
glBindTexture(GL_TEXTURE_2D_ARRAY, depthtex);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@ -307,19 +307,10 @@ void PostProcessing::renderShadowedSunlight(const std::vector<core::matrix4> &su
glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
if (irr_driver->getShadowViz())
{
glUseProgram(FullScreenShader::ShadowedSunLightDebugShader::Program);
glBindVertexArray(FullScreenShader::ShadowedSunLightDebugShader::vao);
FullScreenShader::ShadowedSunLightDebugShader::setUniforms(cb->getPosition(), cb->getRed(), cb->getGreen(), cb->getBlue(), 0, 1, 2);
glUseProgram(FullScreenShader::ShadowedSunLightShader::getInstance()->Program);
glBindVertexArray(FullScreenShader::ShadowedSunLightShader::getInstance()->vao);
FullScreenShader::ShadowedSunLightShader::getInstance()->setUniforms(cb->getPosition(), video::SColorf(cb->getRed(), cb->getGreen(), cb->getBlue()));
}
else
{
glUseProgram(FullScreenShader::ShadowedSunLightShader::Program);
glBindVertexArray(FullScreenShader::ShadowedSunLightShader::vao);
FullScreenShader::ShadowedSunLightShader::setUniforms(cb->getPosition(), cb->getRed(), cb->getGreen(), cb->getBlue(), 0, 1, 2);
}
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}

View File

@ -390,10 +390,7 @@ void Shaders::loadShaders()
FullScreenShader::LayerPassThroughShader::init();
FullScreenShader::LinearizeDepthShader::init();
FullScreenShader::SSAOShader::init();
FullScreenShader::SunLightShader::init();
FullScreenShader::DiffuseEnvMapShader::init();
FullScreenShader::ShadowedSunLightShader::init();
FullScreenShader::ShadowedSunLightDebugShader::init();
FullScreenShader::RadianceHintsConstructionShader::init();
FullScreenShader::RHDebug::init();
FullScreenShader::GlobalIlluminationReconstructionShader::init();
@ -1623,14 +1620,7 @@ namespace FullScreenShader
glUniform1i(uniform_depth, TU_dtex);
}
GLuint SunLightShader::Program;
GLuint SunLightShader::uniform_ntex;
GLuint SunLightShader::uniform_dtex;
GLuint SunLightShader::uniform_direction;
GLuint SunLightShader::uniform_col;
GLuint SunLightShader::vao;
void SunLightShader::init()
SunLightShader::SunLightShader()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(),
@ -1638,26 +1628,16 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getSpecular.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getPosFromUVDepth.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/sunlight.frag").c_str());
uniform_ntex = glGetUniformLocation(Program, "ntex");
uniform_dtex = glGetUniformLocation(Program, "dtex");
uniform_direction = glGetUniformLocation(Program, "direction");
uniform_col = glGetUniformLocation(Program, "col");
TU_ntex = 0;
TU_dtex = 1;
AssignTextureUnit(Program, TexUnit(TU_ntex, "ntex"), TexUnit(TU_dtex, "dtex"));
AssignUniforms("direction", "col");
vao = createFullScreenVAO(Program);
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
}
void SunLightShader::setUniforms(const core::vector3df &direction, float r, float g, float b, unsigned TU_ntex, unsigned TU_dtex)
{
if (irr_driver->needUBOWorkaround())
bypassUBO(Program);
glUniform3f(uniform_direction, direction.X, direction.Y, direction.Z);
glUniform3f(uniform_col, r, g, b);
glUniform1i(uniform_ntex, TU_ntex);
glUniform1i(uniform_dtex, TU_dtex);
}
GLuint DiffuseEnvMapShader::Program;
GLuint DiffuseEnvMapShader::uniform_ntex;
GLuint DiffuseEnvMapShader::uniform_blueLmn;
@ -1689,15 +1669,7 @@ namespace FullScreenShader
glUniform1fv(uniform_redLmn, 9, redSHCoeff);
}
GLuint ShadowedSunLightShader::Program;
GLuint ShadowedSunLightShader::uniform_ntex;
GLuint ShadowedSunLightShader::uniform_dtex;
GLuint ShadowedSunLightShader::uniform_shadowtex;
GLuint ShadowedSunLightShader::uniform_direction;
GLuint ShadowedSunLightShader::uniform_col;
GLuint ShadowedSunLightShader::vao;
void ShadowedSunLightShader::init()
ShadowedSunLightShader::ShadowedSunLightShader()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(),
@ -1705,65 +1677,17 @@ namespace FullScreenShader
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getSpecular.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getPosFromUVDepth.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/sunlightshadow.frag").c_str());
uniform_ntex = glGetUniformLocation(Program, "ntex");
uniform_dtex = glGetUniformLocation(Program, "dtex");
uniform_shadowtex = glGetUniformLocation(Program, "shadowtex");
uniform_direction = glGetUniformLocation(Program, "direction");
uniform_col = glGetUniformLocation(Program, "col");
TU_ntex = 0;
TU_dtex = 1;
TU_shadowtex = 2;
AssignTextureUnit(Program, TexUnit(TU_ntex, "ntex"), TexUnit(TU_dtex, "dtex"), TexUnit(TU_shadowtex, "shadowtex"));
AssignUniforms("direction", "col");
vao = createFullScreenVAO(Program);
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
}
void ShadowedSunLightShader::setUniforms(const core::vector3df &direction, float r, float g, float b, unsigned TU_ntex, unsigned TU_dtex, unsigned TU_shadowtex)
{
if (irr_driver->needUBOWorkaround())
bypassUBO(Program);
glUniform3f(uniform_direction, direction.X, direction.Y, direction.Z);
glUniform3f(uniform_col, r, g, b);
glUniform1i(uniform_ntex, TU_ntex);
glUniform1i(uniform_dtex, TU_dtex);
glUniform1i(uniform_shadowtex, TU_shadowtex);
}
GLuint ShadowedSunLightDebugShader::Program;
GLuint ShadowedSunLightDebugShader::uniform_ntex;
GLuint ShadowedSunLightDebugShader::uniform_dtex;
GLuint ShadowedSunLightDebugShader::uniform_shadowtex;
GLuint ShadowedSunLightDebugShader::uniform_direction;
GLuint ShadowedSunLightDebugShader::uniform_col;
GLuint ShadowedSunLightDebugShader::vao;
void ShadowedSunLightDebugShader::init()
{
Program = LoadProgram(
GL_VERTEX_SHADER, file_manager->getAsset("shaders/screenquad.vert").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/decodeNormal.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/utils/getSpecular.frag").c_str(),
GL_FRAGMENT_SHADER, file_manager->getAsset("shaders/sunlightshadowdebug.frag").c_str());
uniform_ntex = glGetUniformLocation(Program, "ntex");
uniform_dtex = glGetUniformLocation(Program, "dtex");
uniform_shadowtex = glGetUniformLocation(Program, "shadowtex");
uniform_direction = glGetUniformLocation(Program, "direction");
uniform_col = glGetUniformLocation(Program, "col");
vao = createVAO(Program);
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
}
void ShadowedSunLightDebugShader::setUniforms(const core::vector3df &direction, float r, float g, float b, unsigned TU_ntex, unsigned TU_dtex, unsigned TU_shadowtex)
{
if (irr_driver->needUBOWorkaround())
bypassUBO(Program);
glUniform3f(uniform_direction, direction.X, direction.Y, direction.Z);
glUniform3f(uniform_col, r, g, b);
glUniform1i(uniform_ntex, TU_ntex);
glUniform1i(uniform_dtex, TU_dtex);
glUniform1i(uniform_shadowtex, TU_shadowtex);
}
GLuint RadianceHintsConstructionShader::Program;
GLuint RadianceHintsConstructionShader::uniform_ctex;
GLuint RadianceHintsConstructionShader::uniform_ntex;

View File

@ -543,15 +543,13 @@ public:
static void setUniforms(unsigned TU_tex, unsigned TU_depth);
};
class SunLightShader
class SunLightShader : public ShaderHelperSingleton<SunLightShader, core::vector3df, video::SColorf>
{
public:
static GLuint Program;
static GLuint uniform_ntex, uniform_dtex, uniform_direction, uniform_col;
static GLuint vao;
GLuint TU_ntex, TU_dtex;
GLuint vao;
static void init();
static void setUniforms(const core::vector3df &direction, float r, float g, float b, unsigned TU_ntex, unsigned TU_dtex);
SunLightShader();
};
class DiffuseEnvMapShader
@ -565,26 +563,13 @@ public:
static void setUniforms(const core::matrix4 &TransposeViewMatrix, const float *blueSHCoeff, const float *greenSHCoeff, const float *redSHCoeff, unsigned TU_ntex);
};
class ShadowedSunLightShader
class ShadowedSunLightShader : public ShaderHelperSingleton<ShadowedSunLightShader, core::vector3df, video::SColorf>
{
public:
static GLuint Program;
static GLuint uniform_ntex, uniform_dtex, uniform_shadowtex, uniform_direction, uniform_col;
static GLuint vao;
GLuint TU_ntex, TU_dtex, TU_shadowtex;
GLuint vao;
static void init();
static void setUniforms(const core::vector3df &direction, float r, float g, float b, unsigned TU_ntex, unsigned TU_dtex, unsigned TU_shadowtex);
};
class ShadowedSunLightDebugShader
{
public:
static GLuint Program;
static GLuint uniform_ntex, uniform_dtex, uniform_shadowtex, uniform_direction, uniform_col;
static GLuint vao;
static void init();
static void setUniforms(const core::vector3df &direction, float r, float g, float b, unsigned TU_ntex, unsigned TU_dtex, unsigned TU_shadowtex);
ShadowedSunLightShader();
};
class RadianceHintsConstructionShader