From f62228592eafda87417d60ebe21baf5108950748 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Mon, 31 Mar 2014 14:39:09 +0200 Subject: [PATCH] GL3: Cleanup samplers --HG-- branch : opengl3 --- .../opengl3/driver_glsl_shader_generator.cpp | 16 +++---- .../src/3d/driver/opengl3/driver_opengl.cpp | 27 ++---------- .../nel/src/3d/driver/opengl3/driver_opengl.h | 28 ++++++++++--- .../3d/driver/opengl3/driver_opengl_light.cpp | 1 + .../driver/opengl3/driver_opengl_material.cpp | 42 ++++--------------- .../driver/opengl3/driver_opengl_program.cpp | 41 ++++++++++++------ .../opengl3/driver_opengl_vertex_program.cpp | 2 - 7 files changed, 72 insertions(+), 85 deletions(-) diff --git a/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.cpp b/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.cpp index 7d0f7fe0a..07cb878cd 100644 --- a/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_glsl_shader_generator.cpp @@ -81,8 +81,8 @@ namespace NL3D } ss << std::endl; -#if 0 // LIGHTING DEBUG - if (desc->lightingEnabled()) +#if 1 // LIGHTING DEBUG + if (desc->lightingEnabled() || material->getShader() != CMaterial::Normal) { generateInvalidPS(); } @@ -523,7 +523,7 @@ namespace NL3D if (desc->lightingEnabled()) addLightsFS(); - //ss << "fragColor = fragColor + vec4(0.0, 0.25, 0.0, 0.0);" << std::endl; + ss << "fragColor = fragColor + vec4(0.25, 0.0, 0.0, 0.0);" << std::endl; /*if (desc->fogEnabled()) addFog(); @@ -619,6 +619,8 @@ namespace NL3D addAlphaTest(); + ss << "fragColor = fragColor + vec4(0.0, 0.25, 0.0, 0.0);" << std::endl; + ss << "}" << std::endl; } @@ -1109,11 +1111,11 @@ namespace NL3D ss << "smooth in vec3 cubeTexCoords2;" << std::endl; ss << std::endl; - ss << "uniform samplerCube cubeSampler0;" << std::endl; + ss << "uniform samplerCube sampler0;" << std::endl; ss << "uniform sampler2D sampler1;" << std::endl; if (material->getShader() == CMaterial::PerPixelLighting) - ss << "uniform samplerCube cubeSampler2;" << std::endl; + ss << "uniform samplerCube sampler2;" << std::endl; addDiffuse(); @@ -1126,11 +1128,11 @@ namespace NL3D ss << "void main(void)" << std::endl; ss << "{" << std::endl; - ss << "vec4 texel0 = texture(cubeSampler0, cubeTexCoords0);" << std::endl; + ss << "vec4 texel0 = texture(sampler0, cubeTexCoords0);" << std::endl; ss << "vec4 texel1 = texture(sampler1, texCoord1.st);" << std::endl; if (material->getShader() == CMaterial::PerPixelLighting) - ss << "vec4 texel2 = texture(cubeSampler2, cubeTexCoords2);" << std::endl; + ss << "vec4 texel2 = texture(sampler2, cubeTexCoords2);" << std::endl; ss << "vec4 texel;" << std::endl; diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl.cpp b/code/nel/src/3d/driver/opengl3/driver_opengl.cpp index 0eab0a799..90c753cd9 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl.cpp @@ -1884,38 +1884,18 @@ void CDriverGL3::reloadUserShaders() loader.loadShaders("./shaders"); } -CVertexProgramDrvInfosGL3::CVertexProgramDrvInfosGL3(CDriverGL3 *drv, ItGPUPrgDrvInfoPtrList it) : +CProgramDrvInfosGL3::CProgramDrvInfosGL3(CDriverGL3 *drv, ItGPUPrgDrvInfoPtrList it) : IProgramDrvInfos(drv, it) { programId = 0; } -CVertexProgramDrvInfosGL3::~CVertexProgramDrvInfosGL3() +CProgramDrvInfosGL3::~CProgramDrvInfosGL3() { programId = 0; } -uint CVertexProgramDrvInfosGL3::getUniformIndex(const char *name) const -{ - int idx = nglGetUniformLocation(programId, name); - if (idx == -1) - return ~0; - else - return idx; -} - -CPixelProgramDrvInfosGL3::CPixelProgramDrvInfosGL3(CDriverGL3 *drv, ItGPUPrgDrvInfoPtrList it) : -IProgramDrvInfos(drv, it) -{ - programId = 0; -} - -CPixelProgramDrvInfosGL3::~CPixelProgramDrvInfosGL3() -{ - programId = 0; -} - -uint CPixelProgramDrvInfosGL3::getUniformIndex(const char *name) const +uint CProgramDrvInfosGL3::getUniformIndex(const char *name) const { int idx = nglGetUniformLocation(programId, name); if (idx == -1) @@ -1924,7 +1904,6 @@ uint CPixelProgramDrvInfosGL3::getUniformIndex(const char *name) const return idx; } - // *************************************************************************** void displayGLError(GLenum error) { diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl.h b/code/nel/src/3d/driver/opengl3/driver_opengl.h index 39d44f943..e063edea6 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl.h +++ b/code/nel/src/3d/driver/opengl3/driver_opengl.h @@ -384,6 +384,7 @@ public: bool setupDynMatProgram(CMaterial& mat, uint pass); bool setupUniforms(); void setupUniforms(TProgram program); + void setupInitialUniforms(IProgram *program); void generateBuiltinVertexProgram(); void enableFogVP(bool enable); @@ -1414,11 +1415,25 @@ public: }; -class CVertexProgramDrvInfosGL3 : public IProgramDrvInfos +class CProgramDrvInfosGL3 : public IProgramDrvInfos { public: - CVertexProgramDrvInfosGL3(CDriverGL3 *drv, ItGPUPrgDrvInfoPtrList it); - ~CVertexProgramDrvInfosGL3(); + CProgramDrvInfosGL3(CDriverGL3 *drv, ItGPUPrgDrvInfoPtrList it); + ~CProgramDrvInfosGL3(); + uint getUniformIndex(const char *name) const; + GLuint getProgramId() const{ return programId; } + void setProgramId(GLuint id) { programId = id; } + +private: + GLuint programId; +}; + +/* +class CProgramDrvInfosGL3 : public IProgramDrvInfos +{ +public: + CProgramDrvInfosGL3(CDriverGL3 *drv, ItGPUPrgDrvInfoPtrList it); + ~CProgramDrvInfosGL3(); uint getUniformIndex(const char *name) const; uint getProgramId() const{ return programId; } void setProgramId(uint id) { programId = id; } @@ -1427,11 +1442,11 @@ private: uint programId; }; -class CPixelProgramDrvInfosGL3 : public IProgramDrvInfos +class CProgramDrvInfosGL3 : public IProgramDrvInfos { public: - CPixelProgramDrvInfosGL3(CDriverGL3 *drv, ItGPUPrgDrvInfoPtrList it); - ~CPixelProgramDrvInfosGL3(); + CProgramDrvInfosGL3(CDriverGL3 *drv, ItGPUPrgDrvInfoPtrList it); + ~CProgramDrvInfosGL3(); uint getUniformIndex(const char *name) const; uint getProgramId() const{ return programId; } void setProgramId(uint id) { programId = id; } @@ -1440,6 +1455,7 @@ private: uint programId; }; +*/ #ifdef NL_STATIC } // NLDRIVERGL3 diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_light.cpp b/code/nel/src/3d/driver/opengl3/driver_opengl_light.cpp index 2e7f37d22..38dfad884 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_light.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_light.cpp @@ -165,6 +165,7 @@ void CDriverGL3::setLightMapDynamicLight (bool enable, const CLight& light) void CDriverGL3::setupLightMapDynamicLighting(bool enable) { H_AUTO_OGL(CDriverGL3_setupLightMapDynamicLighting) + // start lightmap dynamic lighting if (enable) { diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_material.cpp b/code/nel/src/3d/driver/opengl3/driver_opengl_material.cpp index 0aed0a99b..a53ae1822 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_material.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_material.cpp @@ -738,17 +738,6 @@ void CDriverGL3::setupNormalPass() setUniform4f(IDriver::PixelProgram, cl, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ]); } } - - // Set texture - ITexture *t = mat.getTexture(i); - if (t == NULL) - continue; - - int index = m_DriverPixelProgram->getUniformIndex(CProgramIndex::TName(CProgramIndex::Sampler0 + i)); - if (index == -1) - continue; - - setUniform1i(IDriver::PixelProgram, index, i); } } @@ -936,12 +925,6 @@ void CDriverGL3::setupLightMapPass(uint pass) setUniform4f(IDriver::PixelProgram, cl, glCol[ 0 ], glCol[ 1 ], glCol[ 2 ], glCol[ 3 ]); } - int tl = m_DriverPixelProgram->getUniformIndex(CProgramIndex::TName(CProgramIndex::Sampler0 + stage)); - if (tl != -1) - { - setUniform1i(IDriver::PixelProgram, tl, stage); - } - activateTexEnvColor(stage, stdEnv); // Setup env for texture stage. @@ -976,13 +959,6 @@ void CDriverGL3::setupLightMapPass(uint pass) // Setup gen tex off _DriverGLStates.activeTexture(stage); _DriverGLStates.setTexGenMode(stage, 0); - - int tl = m_DriverPixelProgram->getUniformIndex(CProgramIndex::TName(CProgramIndex::Sampler0 + stage)); - if (tl != -1) - { - setUniform1i(IDriver::PixelProgram, tl, stage); - } - } } else @@ -1174,22 +1150,22 @@ void CDriverGL3::setupSpecularPass(uint pass) return; } - int sl0 = m_DriverPixelProgram->getUniformIndex(CProgramIndex::Sampler0); - if (sl0 != -1) + int tm0 = m_DriverVertexProgram->getUniformIndex(CProgramIndex::TexMatrix0); // FIXME GL3 VERTEX PROGRAM + if (tm0 != -1) { - setUniform1i(IDriver::PixelProgram, sl0, 0); + setUniform4x4f(IDriver::VertexProgram, tm0, _UserTexMat[ 0 ]); // OR is it just 1 (and not 0 2 3)? } - int sl1 = m_DriverPixelProgram->getUniformIndex(CProgramIndex::Sampler1); - if (sl1 != -1) + int tm2 = m_DriverVertexProgram->getUniformIndex(CProgramIndex::TexMatrix2); // FIXME GL3 VERTEX PROGRAM + if (tm2 != -1) { - setUniform1i(IDriver::PixelProgram, sl1, 1); + setUniform4x4f(IDriver::VertexProgram, tm2, _UserTexMat[ 2 ]); } - int tml = m_DriverVertexProgram->getUniformIndex(CProgramIndex::TexMatrix0); // FIXME GL3 VERTEX PROGRAM - if (tml != -1) + int tm3 = m_DriverVertexProgram->getUniformIndex(CProgramIndex::TexMatrix3); // FIXME GL3 VERTEX PROGRAM + if (tm3 != -1) { - setUniform4x4f(IDriver::VertexProgram, tml, _UserTexMat[ 1 ]); + setUniform4x4f(IDriver::VertexProgram, tm3, _UserTexMat[ 3 ]); } { diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp b/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp index f097054db..451392edd 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_program.cpp @@ -87,10 +87,7 @@ const char *g_ConstantNames[4] = bool CDriverGL3::supportVertexProgram(CVertexProgram::TProfile profile) const { - if (profile == IProgram::glsl330v) - return true; - else - return false; + return (profile == IProgram::glsl330v); } bool CDriverGL3::compileVertexProgram(CVertexProgram *program) @@ -142,21 +139,20 @@ bool CDriverGL3::compileVertexProgram(CVertexProgram *program) } } - GLenum error = glGetError(); if (error != GL_NO_ERROR) return false; ItGPUPrgDrvInfoPtrList it = _GPUPrgDrvInfos.insert(_GPUPrgDrvInfos.end(),(NL3D::IProgramDrvInfos*)NULL); - CVertexProgramDrvInfosGL3 *drvInfo = new CVertexProgramDrvInfosGL3(this, it); + CProgramDrvInfosGL3 *drvInfo = new CProgramDrvInfosGL3(this, it); *it = drvInfo; - program->m_DrvInfo = drvInfo; - drvInfo->setProgramId(id); program->buildInfo(src); + setupInitialUniforms(program); + return true; } @@ -181,7 +177,7 @@ bool CDriverGL3::activeVertexProgram(CVertexProgram *program, bool driver) } IProgramDrvInfos *di = program->m_DrvInfo; - CVertexProgramDrvInfosGL3 *drvInfo = dynamic_cast< CVertexProgramDrvInfosGL3* >(di); + CProgramDrvInfosGL3 *drvInfo = dynamic_cast< CProgramDrvInfosGL3* >(di); if (drvInfo == NULL) { m_UserVertexProgram = NULL; @@ -258,13 +254,15 @@ bool CDriverGL3::compilePixelProgram(CPixelProgram *program) return false; ItGPUPrgDrvInfoPtrList it = _GPUPrgDrvInfos.insert(_GPUPrgDrvInfos.end(), (NL3D::IProgramDrvInfos*)NULL); - CPixelProgramDrvInfosGL3 *drvInfo = new CPixelProgramDrvInfosGL3(this, it); + CProgramDrvInfosGL3 *drvInfo = new CProgramDrvInfosGL3(this, it); *it = drvInfo; drvInfo->setProgramId(id); program->m_DrvInfo = drvInfo; program->buildInfo(src); + setupInitialUniforms(program); + return true; } @@ -296,7 +294,7 @@ bool CDriverGL3::activePixelProgram(CPixelProgram *program, bool driver) } IProgramDrvInfos *di = program->m_DrvInfo; - CPixelProgramDrvInfosGL3 *drvInfo = dynamic_cast< CPixelProgramDrvInfosGL3* >(di); + CProgramDrvInfosGL3 *drvInfo = dynamic_cast< CProgramDrvInfosGL3* >(di); if (drvInfo == NULL) { m_UserPixelProgram = NULL; @@ -330,7 +328,7 @@ uint32 CDriverGL3::getProgramId(TProgram program) const if (m_DriverVertexProgram) { IProgramDrvInfos *di = m_DriverVertexProgram->m_DrvInfo; - CVertexProgramDrvInfosGL3 *drvInfo = dynamic_cast< CVertexProgramDrvInfosGL3* >(di); + CProgramDrvInfosGL3 *drvInfo = dynamic_cast< CProgramDrvInfosGL3* >(di); if (drvInfo != NULL) id = drvInfo->getProgramId(); } @@ -340,7 +338,7 @@ uint32 CDriverGL3::getProgramId(TProgram program) const if (m_DriverPixelProgram) { IProgramDrvInfos *di = m_DriverPixelProgram->m_DrvInfo; - CPixelProgramDrvInfosGL3 *drvInfo = dynamic_cast< CPixelProgramDrvInfosGL3* >(di); + CProgramDrvInfosGL3 *drvInfo = dynamic_cast< CProgramDrvInfosGL3* >(di); if (drvInfo != NULL) id = drvInfo->getProgramId(); } @@ -1106,6 +1104,23 @@ void CDriverGL3::setupUniforms(TProgram program) } } +void CDriverGL3::setupInitialUniforms(IProgram *program) +{ + IProgramDrvInfos *di = program->m_DrvInfo; + CProgramDrvInfosGL3 *drvInfo = dynamic_cast(di); + if (drvInfo != NULL) + { + GLuint id = drvInfo->getProgramId(); + + for (uint i = 0; i < IDRV_MAT_MAXTEXTURES; ++i) + { + uint samplerIdx = program->getUniformIndex((CProgramIndex::TName)(CProgramIndex::Sampler0 + i)); + if (samplerIdx >= 0) + nglProgramUniform1i(id, samplerIdx, i); + } + } +} + bool CDriverGL3::initProgramPipeline() { ppoId = 0; diff --git a/code/nel/src/3d/driver/opengl3/driver_opengl_vertex_program.cpp b/code/nel/src/3d/driver/opengl3/driver_opengl_vertex_program.cpp index b151a05e5..15b885b82 100644 --- a/code/nel/src/3d/driver/opengl3/driver_opengl_vertex_program.cpp +++ b/code/nel/src/3d/driver/opengl3/driver_opengl_vertex_program.cpp @@ -205,8 +205,6 @@ namespace // Ambient color of all lights is precalculated and added with self illumination, and multiplied with the material ambient. if (desc.Lighting) ss << "uniform vec4 selfIllumination;" << std::endl; - // ss << "uniform vec4 diffuseColor;" << std::endl; - // ss << "uniform vec4 specularColor;" << std::endl; // } if (desc.Fog || desc.Lighting) ss << "uniform mat4 modelView;" << std::endl;