Factorise SH coefficient in an UBO

This commit is contained in:
Vincent Lejeune 2014-12-15 00:18:03 +01:00
parent 3f8fdc7976
commit 03694d9c7c
7 changed files with 86 additions and 14 deletions

View File

@ -5,6 +5,10 @@ uniform mat4 InverseViewMatrix;
uniform mat4 InverseProjectionMatrix;
uniform mat4 ProjectionViewMatrix;
uniform vec2 screen;
uniform float blueLmn[9];
uniform float greenLmn[9];
uniform float redLmn[9];
#else
layout (std140) uniform MatrixesData
{
@ -16,4 +20,38 @@ layout (std140) uniform MatrixesData
mat4 ShadowViewProjMatrixes[4];
vec2 screen;
};
// Expand because of catalyst (14.12) not correctly associating array in UBO
layout (std140) uniform LightingData
{
float bL00;
float bL1m1;
float bL10;
float bL11;
float bL2m2;
float bL2m1;
float bL20;
float bL21;
float bL22;
float gL00;
float gL1m1;
float gL10;
float gL11;
float gL2m2;
float gL2m1;
float gL20;
float gL21;
float gL22;
float rL00;
float rL1m1;
float rL10;
float rL11;
float rL2m2;
float rL2m1;
float rL20;
float rL21;
float rL22;
};
#endif

View File

@ -1,31 +1,34 @@
// From "An Efficient Representation for Irradiance Environment Maps" article
// See http://graphics.stanford.edu/papers/envmap/
// Coefficients are calculated in IBL.cpp
uniform float blueLmn[9];
uniform float greenLmn[9];
uniform float redLmn[9];
mat4 getMatrix(float L[9])
mat4 getMatrix(float L00, float L1m1, float L10, float L11, float L2m2, float L2m1, float L20, float L21, float L22)
{
float c1 = 0.429043, c2 = 0.511664, c3 = 0.743125, c4 = 0.886227, c5 = 0.247708;
return mat4(
c1 * L[8] /*L22*/, c1 * L[4] /*L2-2*/, c1 * L[7] /*L21*/, c2 * L[3] /*L11*/,
c1 * L[4], - c1 * L[8], c1 * L[5] /*L2-1*/, c2 * L[1] /*L1-1*/,
c1 * L[7], c1 * L[5], c3 * L[6] /*L20*/, c2 * L[2] /*L10*/,
c2 * L[3], c2 * L[1], c2 * L[2], c4 * L[0] /*L00*/ - c5 * L[6]
c1 * L22, c1 * L2m2, c1 * L21, c2 * L11,
c1 * L2m2, - c1 * L22, c1 * L2m1, c2 * L1m1,
c1 * L21, c1 * L2m1, c3 * L20, c2 * L10,
c2 * L11, c2 * L1m1, c2 * L10, c4 * L00 - c5 * L20
);
}
vec3 DiffuseIBL(vec3 normal)
{
// Convert normal in world space (where SH coordinates were computed)
// Convert normal in wobLd space (where SH coordinates were computed)
vec4 extendednormal = transpose(ViewMatrix) * vec4(normal, 0.);
extendednormal.w = 1.;
mat4 rmat = getMatrix(redLmn);
mat4 gmat = getMatrix(greenLmn);
mat4 bmat = getMatrix(blueLmn);
#ifdef UBO_DISABLED
mat4 rmat = getMatrix(redLmn[0], redLmn[1], redLmn[2], redLmn[3], redLmn[4], redLmn[5], redLmn[6], redLmn[7], redLmn[8]);
mat4 gmat = getMatrix(greenLmn[0], greenLmn[1], greenLmn[2], greenLmn[3], greenLmn[4], greenLmn[5], greenLmn[6], greenLmn[7], greenLmn[8]);
mat4 bmat = getMatrix(blueLmn[0], blueLmn[1], blueLmn[2], blueLmn[3], blueLmn[4], blueLmn[5], blueLmn[6], blueLmn[7], blueLmn[8]);
#else
mat4 rmat = getMatrix(rL00, rL1m1, rL10, rL11, rL2m2, rL2m1, rL20, rL21, rL22);
mat4 gmat = getMatrix(gL00, gL1m1, gL10, gL11, gL2m2, gL2m1, gL20, gL21, gL22);
mat4 bmat = getMatrix(bL00, bL1m1, bL10, bL11, bL2m2, bL2m1, bL20, bL21, bL22);
#endif
float r = dot(extendednormal, rmat * extendednormal);
float g = dot(extendednormal, gmat * extendednormal);

View File

@ -238,9 +238,11 @@ private:
std::vector<video::ITexture *> SphericalHarmonicsTextures;
bool m_skybox_ready;
public:
float blueSHCoeff[9];
float greenSHCoeff[9];
float redSHCoeff[9];
private:
/** Keep a trace of the origin file name of a texture. */
std::map<video::ITexture*, std::string> m_texturesFileName;

View File

@ -342,6 +342,7 @@ void IrrDriver::renderGLSL(float dt)
void IrrDriver::renderScene(scene::ICameraSceneNode * const camnode, unsigned pointlightcount, std::vector<GlowData>& glows, float dt, bool hasShadow, bool forceRTT)
{
glBindBufferBase(GL_UNIFORM_BUFFER, 0, SharedObject::ViewProjectionMatrixesUBO);
glBindBufferBase(GL_UNIFORM_BUFFER, 1, SharedObject::LightingDataUBO);
m_scene_manager->setActiveCamera(camnode);
PROFILER_PUSH_CPU_MARKER("- Draw Call Generation", 0xFF, 0xFF, 0xFF);
@ -961,6 +962,14 @@ void IrrDriver::computeCameraMatrix(scene::ICameraSceneNode * const camnode, siz
tmp[145] = float(height);
glBindBuffer(GL_UNIFORM_BUFFER, SharedObject::ViewProjectionMatrixesUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, (16 * 9 + 2) * sizeof(float), tmp);
float Lighting[27];
memcpy(Lighting, blueSHCoeff, 9 * sizeof(float));
memcpy(&Lighting[9], greenSHCoeff, 9 * sizeof(float));
memcpy(&Lighting[18], redSHCoeff, 9 * sizeof(float));
glBindBuffer(GL_UNIFORM_BUFFER, SharedObject::LightingDataUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, 27 * sizeof(float), Lighting);
}

View File

@ -427,6 +427,16 @@ static void initShadowVPMUBO()
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
GLuint SharedObject::LightingDataUBO;
static void initLightingDataUBO()
{
glGenBuffers(1, &SharedObject::LightingDataUBO);
glBindBuffer(GL_UNIFORM_BUFFER, SharedObject::LightingDataUBO);
glBufferData(GL_UNIFORM_BUFFER, 27 * sizeof(float), 0, GL_STREAM_DRAW);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
}
GLuint SharedObject::ParticleQuadVBO = 0;
static void initParticleQuadVBO()
@ -527,6 +537,7 @@ void Shaders::loadShaders()
initCubeVBO();
initFrustrumVBO();
initShadowVPMUBO();
initLightingDataUBO();
initParticleQuadVBO();
}
@ -574,6 +585,12 @@ void bypassUBO(GLuint Program)
glUniformMatrix4fv(IPM, 1, GL_FALSE, irr_driver->getInvProjMatrix().pointer());
GLint Screen = glGetUniformLocation(Program, "screen");
glUniform2f(Screen, irr_driver->getCurrentScreenSize().X, irr_driver->getCurrentScreenSize().Y);
GLint bLmn = glGetUniformLocation(Program, "blueLmn[0]");
glUniform1fv(bLmn, 9, irr_driver->blueSHCoeff);
GLint gLmn = glGetUniformLocation(Program, "greenLmn[0]");
glUniform1fv(gLmn, 9, irr_driver->greenSHCoeff);
GLint rLmn = glGetUniformLocation(Program, "redLmn[0]");
glUniform1fv(rLmn, 9, irr_driver->redSHCoeff);
}
namespace UtilShader

View File

@ -31,7 +31,7 @@ class SharedObject
public:
static GLuint billboardvbo;
static GLuint cubevbo, cubeindexes, frustrumvbo, frustrumindexes, ParticleQuadVBO;
static GLuint ViewProjectionMatrixesUBO;
static GLuint ViewProjectionMatrixesUBO, LightingDataUBO;
static GLuint FullScreenQuadVAO;
static GLuint UIVAO;
};

View File

@ -171,6 +171,9 @@ protected:
GLuint uniform_ViewProjectionMatrixesUBO = glGetUniformBlockIndex(Program, "MatrixesData");
if (uniform_ViewProjectionMatrixesUBO != GL_INVALID_INDEX)
glUniformBlockBinding(Program, uniform_ViewProjectionMatrixesUBO, 0);
GLuint uniform_LightingUBO = glGetUniformBlockIndex(Program, "LightingData");
if (uniform_LightingUBO != GL_INVALID_INDEX)
glUniformBlockBinding(Program, uniform_LightingUBO, 1);
}
template<typename... U>