From cf964d62de9e273435f72dd667e6222019841de0 Mon Sep 17 00:00:00 2001 From: songchengjiang Date: Tue, 26 Aug 2014 14:18:22 +0800 Subject: [PATCH] Light Shader optimization --- cocos/renderer/CCGLProgram.cpp | 12 +++++++----- cocos/renderer/ccShader_3D_ColorNormal.frag | 14 ++++++++------ cocos/renderer/ccShader_3D_ColorNormalTex.frag | 17 +++++++++-------- .../renderer/ccShader_3D_PositionNormalTex.vert | 10 ++++++++-- 4 files changed, 32 insertions(+), 21 deletions(-) diff --git a/cocos/renderer/CCGLProgram.cpp b/cocos/renderer/CCGLProgram.cpp index c048cc54c8..d8ce016cec 100644 --- a/cocos/renderer/CCGLProgram.cpp +++ b/cocos/renderer/CCGLProgram.cpp @@ -133,7 +133,7 @@ GLProgram* GLProgram::createWithFilenames(const std::string& vShaderFilename, co ret->updateUniforms(); ret->autorelease(); return ret; - } + } CC_SAFE_DELETE(ret); return nullptr; @@ -464,7 +464,7 @@ bool GLProgram::compileShader(GLuint * shader, GLenum type, const GLchar* source const GLchar *sources[] = { #if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32 && CC_TARGET_PLATFORM != CC_PLATFORM_LINUX && CC_TARGET_PLATFORM != CC_PLATFORM_MAC) - (type == GL_VERTEX_SHADER ? "precision highp float;\n" : "precision mediump float;\n"), + (type == GL_VERTEX_SHADER ? "precision highp float;\n precision highp int;\n" : "precision mediump float;\n precision highp int;\n"), #endif def, lightStruct, @@ -986,7 +986,8 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV) { CCASSERT(enabledDirLightNum < CC_MAX_DIRECTIONAL_LIGHT_NUM, ""); const Color3B &col = light->getDisplayedColor(); - const Vec3 &dir = light->getWorldDirection(); + Vec3 dir = light->getWorldDirection(); + dir.normalize(); sprintf(str, "CC_DirLightSource[%d].%s", enabledDirLightNum, "color"); setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); sprintf(str, "CC_DirLightSource[%d].%s", enabledDirLightNum, "direction"); @@ -1009,7 +1010,8 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV) { CCASSERT(enabledSpotLightNum < CC_MAX_SPOT_LIGHT_NUM, ""); const Color3B &col = light->getDisplayedColor(); - const Vec3 &dir = light->getWorldDirection(); + Vec3 dir = light->getWorldDirection(); + dir.normalize(); Mat4 mat= light->getNodeToWorldTransform(); sprintf(str, "CC_SpotLightSource[%d].%s", enabledSpotLightNum, "color"); setUniformLocationWith3f(glGetUniformLocation(_program, str), col.r / 255.0f, col.g / 255.0f, col.b / 255.0f); @@ -1018,7 +1020,7 @@ void GLProgram::setUniformsForBuiltins(const Mat4 &matrixMV) sprintf(str, "CC_SpotLightSource[%d].%s", enabledSpotLightNum, "direction"); setUniformLocationWith3f(glGetUniformLocation(_program, str), dir.x, dir.y, dir.z); sprintf(str, "CC_SpotLightSource[%d].%s", enabledSpotLightNum, "params"); - setUniformLocationWith3f(glGetUniformLocation(_program, str), light->getInnerAngle(), light->getOuterAngle(), light->getRange()); + setUniformLocationWith3f(glGetUniformLocation(_program, str), cosf(light->getInnerAngle()), cosf(light->getOuterAngle()), light->getRange()); ++enabledSpotLightNum; } } diff --git a/cocos/renderer/ccShader_3D_ColorNormal.frag b/cocos/renderer/ccShader_3D_ColorNormal.frag index 4c1f72a224..397360bcfb 100644 --- a/cocos/renderer/ccShader_3D_ColorNormal.frag +++ b/cocos/renderer/ccShader_3D_ColorNormal.frag @@ -28,7 +28,7 @@ void PointLight(int n, vec4 ePosition, vec3 eNormal, inout vec4 intensity) \n#if CC_MAX_DIRECTIONAL_LIGHT_NUM\n void DirectionalLight(int n, vec3 eNormal, inout vec4 intensity) { - intensity.xyz += CC_DirLightSource[n].color * max(0.0, dot(normalize(-CC_DirLightSource[n].direction), eNormal)); + intensity.xyz += CC_DirLightSource[n].color * max(0.0, dot(-CC_DirLightSource[n].direction, eNormal)); intensity.w = 1.0; } \n#endif\n @@ -40,11 +40,11 @@ void SpotLight(int n, vec4 ePosition, vec3 eNormal, inout vec4 intensity) { vec3 lightDir = CC_SpotLightSource[n].position - ePosition.xyz; lightDir = normalize(lightDir); - float spotDot = dot(lightDir, normalize(-CC_SpotLightSource[n].direction)); - float innerCos = cos(CC_SpotLightSource[n].params.x); - float outerCos = cos(CC_SpotLightSource[n].params.y); + float spotDot = dot(normalize(lightDir), -CC_SpotLightSource[n].direction); + float innerCos = CC_SpotLightSource[n].params.x; + float outerCos = CC_SpotLightSource[n].params.y; float factor = smoothstep(outerCos, innerCos, spotDot); - intensity.xyz += CC_SpotLightSource[n].color * max(0.0, dot(lightDir, eNormal)) * factor; + intensity.xyz += CC_SpotLightSource[n].color * max(0.0, dot(lightDir, eNormal)) * factor; } intensity.w = 1.0; } @@ -52,8 +52,10 @@ void SpotLight(int n, vec4 ePosition, vec3 eNormal, inout vec4 intensity) void main(void) { - vec3 normal = normalize(eNormal); vec4 intensity = vec4(0.0); +\n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM || CC_MAX_POINT_LIGHT_NUM || CC_MAX_SPOT_LIGHT_NUM)\n + vec3 normal = normalize(eNormal); +\n#endif\n \n#if CC_MAX_DIRECTIONAL_LIGHT_NUM\n diff --git a/cocos/renderer/ccShader_3D_ColorNormalTex.frag b/cocos/renderer/ccShader_3D_ColorNormalTex.frag index 4f8c68c7b1..cdd9ebe393 100644 --- a/cocos/renderer/ccShader_3D_ColorNormalTex.frag +++ b/cocos/renderer/ccShader_3D_ColorNormalTex.frag @@ -28,7 +28,7 @@ void PointLight(int n, vec4 ePosition, vec3 eNormal, inout vec4 intensity) \n#if CC_MAX_DIRECTIONAL_LIGHT_NUM\n void DirectionalLight(int n, vec3 eNormal, inout vec4 intensity) { - intensity.xyz += CC_DirLightSource[n].color * max(0.0, dot(normalize(-CC_DirLightSource[n].direction), eNormal)); + intensity.xyz += CC_DirLightSource[n].color * max(0.0, dot(-CC_DirLightSource[n].direction, eNormal)); intensity.w = 1.0; } \n#endif\n @@ -40,21 +40,22 @@ void SpotLight(int n, vec4 ePosition, vec3 eNormal, inout vec4 intensity) { vec3 lightDir = CC_SpotLightSource[n].position - ePosition.xyz; lightDir = normalize(lightDir); - float spotDot = dot(normalize(lightDir), normalize(-CC_SpotLightSource[n].direction)); - float innerCos = cos(CC_SpotLightSource[n].params.x); - float outerCos = cos(CC_SpotLightSource[n].params.y); + float spotDot = dot(normalize(lightDir), -CC_SpotLightSource[n].direction); + float innerCos = CC_SpotLightSource[n].params.x; + float outerCos = CC_SpotLightSource[n].params.y; float factor = smoothstep(outerCos, innerCos, spotDot); - intensity.xyz += CC_SpotLightSource[n].color * max(0.0, dot(lightDir, eNormal)) * factor; + intensity.xyz += CC_SpotLightSource[n].color * max(0.0, dot(lightDir, eNormal)) * factor; } intensity.w = 1.0; } \n#endif\n - void main(void) -{ - vec3 normal = normalize(eNormal); +{ vec4 intensity = vec4(0.0); +\n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM || CC_MAX_POINT_LIGHT_NUM || CC_MAX_SPOT_LIGHT_NUM)\n + vec3 normal = normalize(eNormal); +\n#endif\n \n#if CC_MAX_DIRECTIONAL_LIGHT_NUM\n diff --git a/cocos/renderer/ccShader_3D_PositionNormalTex.vert b/cocos/renderer/ccShader_3D_PositionNormalTex.vert index 8b68518fdd..550b64e56e 100644 --- a/cocos/renderer/ccShader_3D_PositionNormalTex.vert +++ b/cocos/renderer/ccShader_3D_PositionNormalTex.vert @@ -10,8 +10,10 @@ varying vec3 eNormal; void main(void) { - ePosition = CC_MVMatrix * a_position; +\n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM || CC_MAX_POINT_LIGHT_NUM || CC_MAX_SPOT_LIGHT_NUM)\n eNormal = CC_NormalMatrix * a_normal; +\n#endif\n + ePosition = CC_MVMatrix * a_position; TextureCoordOut = a_texCoord; TextureCoordOut.y = 1.0 - TextureCoordOut.y; gl_Position = CC_PMatrix * ePosition; @@ -83,10 +85,12 @@ void getPositionAndNormal(out vec4 position, out vec3 normal) position.z = dot(p, matrixPalette3); position.w = p.w; +\n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM || CC_MAX_POINT_LIGHT_NUM || CC_MAX_SPOT_LIGHT_NUM)\n vec4 n = vec4(a_normal, 0.0); normal.x = dot(n, matrixPalette1); normal.y = dot(n, matrixPalette2); normal.z = dot(n, matrixPalette3); +\n#endif\n } void main() @@ -95,8 +99,10 @@ void main() vec3 normal; getPositionAndNormal(position, normal); - ePosition = CC_MVMatrix * position; +\n#if (CC_MAX_DIRECTIONAL_LIGHT_NUM || CC_MAX_POINT_LIGHT_NUM || CC_MAX_SPOT_LIGHT_NUM)\n eNormal = CC_NormalMatrix * normal; +\n#endif\n + ePosition = CC_MVMatrix * position; TextureCoordOut = a_texCoord; TextureCoordOut.y = 1.0 - TextureCoordOut.y; gl_Position = CC_PMatrix * ePosition;