From 7383b1a20a38952a13d8d616941a8e29ad0044ab Mon Sep 17 00:00:00 2001 From: Heeyong Song Date: Fri, 25 Oct 2013 10:46:45 +0900 Subject: [PATCH] Shader bug fix when using multiple contexts Change-Id: I9a2676579ab15b1335992974ca10c9dcffd15655 --- src/ui/animations/FUiAnim_GlContext.h | 12 +- src/ui/animations/FUiAnim_GlRenderManager.cpp | 303 +---------------------- src/ui/animations/FUiAnim_GlRenderManager.h | 9 - src/ui/animations/platform/FUiAnim_Egl.cpp | 2 + src/ui/animations/platform/FUiAnim_GlContext.cpp | 287 ++++++++++++++++++++- 5 files changed, 307 insertions(+), 306 deletions(-) diff --git a/src/ui/animations/FUiAnim_GlContext.h b/src/ui/animations/FUiAnim_GlContext.h index 5c53637..80887d6 100644 --- a/src/ui/animations/FUiAnim_GlContext.h +++ b/src/ui/animations/FUiAnim_GlContext.h @@ -88,6 +88,7 @@ protected: bool IsSupportedBGRA() const { return __supportBGRA8888; } void InitGL(void); + void TerminateGL(void); private: virtual Handle CreateWindowSurface(EGLNativeWindowType window) { return null; } @@ -98,6 +99,8 @@ private: void LoadTexture(TextureInfo& textureInfo, bool create); + void PrepareDefaultShaders(void); + protected: Handle __nativeDisplay; //TODO remove @@ -107,9 +110,16 @@ protected: bool __useStencil; bool __useDepthTest; - ShaderProgram* __pCurrentShader; unsigned int __textureId; + ShaderProgram* __pCurrentShader; + + ShaderProgram* __pColorShader; + ShaderProgram* __pUniformColorShader; + ShaderProgram* __pTextureShader; + ShaderProgram* __pTextureOpacityShader; + ShaderProgram* __pLightShader; + private: #ifndef VE_USE_GL_MULTI_CONTEXT static _GlContext* __pInstance; diff --git a/src/ui/animations/FUiAnim_GlRenderManager.cpp b/src/ui/animations/FUiAnim_GlRenderManager.cpp index a3b4fbd..223196d 100644 --- a/src/ui/animations/FUiAnim_GlRenderManager.cpp +++ b/src/ui/animations/FUiAnim_GlRenderManager.cpp @@ -251,19 +251,19 @@ _GlRenderManager::_RenderObject::SetObject(_GlNode* pNode, const _Matrix3Df& mvp if (_FloatCompare(pNode->__opacity, 1.0f)) { - __pProgram = _GlRenderManager::GetInstance()->__pTextureShader; + __pProgram = _GlRenderManager::GetInstance()->__pGlContext->__pTextureShader; } else { - __pProgram = _GlRenderManager::GetInstance()->__pTextureOpacityShader; + __pProgram = _GlRenderManager::GetInstance()->__pGlContext->__pTextureOpacityShader; } } else { __textureId = 0; - __pProgram = _GlRenderManager::GetInstance()->__pUniformColorShader; + __pProgram = _GlRenderManager::GetInstance()->__pGlContext->__pUniformColorShader; } } else @@ -317,25 +317,25 @@ _GlRenderManager::_RenderObject::SetObject(_GlNode* pNode, const _Matrix3Df& mvp if (_FloatCompare(pNode->__opacity, 1.0f)) { - __pProgram = _GlRenderManager::GetInstance()->__pTextureShader; + __pProgram = _GlRenderManager::GetInstance()->__pGlContext->__pTextureShader; } else { - __pProgram = _GlRenderManager::GetInstance()->__pTextureOpacityShader; + __pProgram = _GlRenderManager::GetInstance()->__pGlContext->__pTextureOpacityShader; } } else { __textureId = 0; - __pProgram = _GlRenderManager::GetInstance()->__pUniformColorShader; + __pProgram = _GlRenderManager::GetInstance()->__pGlContext->__pUniformColorShader; } if (pMeshImpl->IsColorEnabled()) { __pColors = pMeshImpl->__pColors; - __pProgram = _GlRenderManager::GetInstance()->__pColorShader; + __pProgram = _GlRenderManager::GetInstance()->__pGlContext->__pColorShader; } if (pMeshImpl->IsNormalEnabled()) @@ -355,7 +355,7 @@ _GlRenderManager::_RenderObject::SetObject(_GlNode* pNode, const _Matrix3Df& mvp { __pLight = pNode->__pLight; - __pProgram = _GlRenderManager::GetInstance()->__pLightShader; + __pProgram = _GlRenderManager::GetInstance()->__pGlContext->__pLightShader; } if (pNode->__pMaterial) @@ -478,11 +478,6 @@ _GlRenderManager::_GlRenderManager(void) , __queueCount(0) , __needClear(false) , __pAnimationManager(0) - , __pColorShader(null) - , __pUniformColorShader(null) - , __pTextureShader(null) - , __pTextureOpacityShader(null) - , __pLightShader(null) { memset(&__threadInfo, 0x00, sizeof(pthread_t)); if (pthread_create(&__threadInfo, NULL, _GlRenderManager::RenderProc, this) != 0) @@ -493,12 +488,6 @@ _GlRenderManager::_GlRenderManager(void) ClearLastResult(); __pRenderQueue = new (std::nothrow) _RenderObject[VE_RENDER_QUEUE_COUNT_MAX]; - - __pColorShader = new (std::nothrow) ShaderProgram; - __pUniformColorShader = new (std::nothrow) ShaderProgram; - __pTextureShader = new (std::nothrow) ShaderProgram; - __pTextureOpacityShader = new (std::nothrow) ShaderProgram; - __pLightShader = new (std::nothrow) ShaderProgram; } _GlRenderManager::~_GlRenderManager(void) @@ -1119,8 +1108,6 @@ _GlRenderManager::CommandHandlerInitializeGLLayer(_GlLayer* pGlLayer) __pGlContext = pGlLayer->__pContext; - PrepareShaders(); - PRINT(">> CommandHandlerInitializeGLLayer() pContext %p \n", pGlLayer->__pContext); #else CHECK_GL_CONTEXT(); @@ -1145,11 +1132,6 @@ _GlRenderManager::CommandHandlerFinalizeGLLayer(_GlLayer* pGlLayer) #ifdef VE_USE_GL_MULTI_CONTEXT PRINT(">> CommandHandlerFinalizeGLLayer() pContext %p \n", pGlLayer->__pContext); - delete __pColorShader; - delete __pUniformColorShader; - delete __pTextureShader; - delete __pTextureOpacityShader; - delete pGlLayer->__pContext; pGlLayer->__pContext = null; @@ -1341,268 +1323,6 @@ _GlRenderManager::DeleteShaderProgram(_ShaderProgramImpl* pShaderProgram) return (__commandResult == E_SUCCESS); } -void -_GlRenderManager::PrepareShaders(void) -{ - static const char strVertexShader_uniform_color[] = - "uniform mat4 u_mvp;\n" - "attribute vec4 a_position;\n" - "void main()\n" - "{\n" - " gl_Position = u_mvp * a_position;\n" - "}\n"; - static const char strFragmentShader_uniform_color[] = - "precision mediump float;\n" - "uniform vec4 u_color;\n" - "void main()\n" - "{\n" - " gl_FragColor = u_color;\n" - "}\n"; - - - static const char strVertexShader_color[] = - "uniform mat4 u_mvp;\n" - "attribute vec4 a_position;\n" - "attribute vec4 a_color;\n" - "varying vec4 v_color;\n" - "void main()\n" - "{\n" - " v_color = a_color;\n" - " gl_Position = u_mvp * a_position;\n" - "}\n"; - static const char strFragmentShader_color[] = - "precision mediump float;\n" - "varying vec4 v_color;\n" - "void main()\n" - "{\n" - " gl_FragColor = v_color;\n" - "}\n"; - - static const char strVertexShader_texture[] = - "uniform mat4 u_mvp;\n" - "attribute vec4 a_position;\n" - "attribute vec4 a_texcoord;\n" - "varying vec2 v_texcoord;\n" - "void main()\n" - "{\n" - " v_texcoord = a_texcoord.xy;\n" - " gl_Position = u_mvp * a_position;\n" - "}\n"; - - static const char strFragmentShader_texture[] = - "precision highp float;\n" - "uniform sampler2D u_tex2d;\n" - "uniform float u_opacity;\n" - "varying vec2 v_texcoord;\n" - "void main()\n" - "{\n" - " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra;\n" - "}\n"; - - static const char strFragmentShader_texture_bgra[] = - "precision highp float;\n" - "uniform sampler2D u_tex2d;\n" - "uniform float u_opacity;\n" - "varying vec2 v_texcoord;\n" - "void main()\n" - "{\n" - " gl_FragColor = texture2D(u_tex2d, v_texcoord);\n" - "}\n"; - - static const char strFragmentShader_texture_opacity[] = - "precision highp float;\n" - "uniform sampler2D u_tex2d;\n" - "uniform float u_opacity;\n" - "varying vec2 v_texcoord;\n" - "void main()\n" - "{\n" - " gl_FragColor.rgba = (texture2D(u_tex2d, v_texcoord) * u_opacity).bgra;\n" - "}\n"; - - static const char strFragmentShader_texture_opacity_bgra[] = - "precision highp float;\n" - "uniform sampler2D u_tex2d;\n" - "uniform float u_opacity;\n" - "varying vec2 v_texcoord;\n" - "void main()\n" - "{\n" - " gl_FragColor = texture2D(u_tex2d, v_texcoord) * u_opacity;\n" - "}\n"; - - static const char strVertexShader_light[] = - "struct light\n" - "{\n" - " int type;\n" - " vec4 ambient;\n" - " vec4 diffuse;\n" - " vec4 specular;\n" - " vec4 position;\n" - " vec3 direction;\n" - " float exponent;\n" - " float cutoff;\n" - " vec3 attenuation;\n" - "};\n" - "struct material\n" - "{\n" - " vec4 ambient;\n" - " vec4 diffuse;\n" - " vec4 specular;\n" - " vec4 emissive;\n" - " float shininess;\n" - "};\n" - "\n" - "const float c_zero = 0.0;\n" - "const float c_one = 1.0;\n" - "const int indx_zero = 0;\n" - "const int indx_one = 1;\n" - "\n" - "uniform mat4 u_mvp;\n" - "uniform mat4 u_modelview;\n" - "//uniform mat3 u_inv_modelview;\n" - "uniform mat4 u_inv_modelview;\n" - "\n" - "uniform material u_material;\n" - "uniform light u_light;\n" - "\n" - "attribute vec4 a_position;\n" - "attribute vec4 a_texcoord;\n" - "attribute vec4 a_color;\n" - "attribute vec3 a_normal;\n" - "\n" - "varying vec2 v_texcoord;\n" - "varying vec4 v_color;\n" - "\n" - "vec4\n" - "do_lighting()\n" - "{\n" - " vec3 lightdir, n, h;\n" - " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n" - " float ndotl, ndoth, attenuation;\n" - "\n" - " n = normalize((u_inv_modelview * vec4(a_normal, 1.0)).xyz);\n" - "\n" - " if (u_light.type < 2)\n" - " {\n" - " vec3 distance;\n" - " float spot_factor;\n" - "\n" - " // point or spot case\n" - " lightdir = u_light.position.xyz - (u_modelview * a_position).xyz;\n" - "\n" - " // compute distance attenuation\n" - " distance.x = c_one;\n" - " distance.z = dot(lightdir, lightdir);\n" - " distance.y = sqrt(distance.z);\n" - "\n" - " attenuation = c_one / dot(distance, u_light.attenuation);" - "\n" - " lightdir = normalize(lightdir);\n" - "\n" - " if (u_light.cutoff < 180.0)\n" - " {\n" - " // compute spot factor\n" - " spot_factor = dot(-lightdir, normalize(u_light.direction));\n" - "\n" - " if (spot_factor >= cos(radians(u_light.cutoff)))\n" - " spot_factor = pow(spot_factor, u_light.exponent);\n" - " else\n" - " spot_factor = c_zero;\n" - "\n" - " attenuation *= spot_factor;\n" - " }\n" - " }\n" - " else\n" - " {\n" - " // directional light\n" - " lightdir = normalize(u_light.position.xyz);\n" - " attenuation = c_one;\n" - " }\n" - "\n" - " if (attenuation > c_zero)\n" - " {\n" - " color = u_light.ambient * u_material.ambient;\n" - "\n" - " ndotl = max(c_zero, dot(n, lightdir));\n" - " color += ndotl * u_light.diffuse * u_material.diffuse;\n" - "\n" - " h = normalize(lightdir + vec3(c_zero, c_zero, c_one));\n" - " ndoth = dot(n, h);\n" - "\n" - " if (ndoth > c_zero)\n" - " {\n" - " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light.specular;\n" - " }\n" - "\n" - " color *= attenuation;\n" - " }\n" - "\n" - " color += u_material.emissive;\n" - "// color += u_material.emissive + u_material.ambient * u_scene_ambient; //todo\n" - "\n" - " color.a = u_material.diffuse.a;\n" - "\n" - " return color;\n" - "}\n" - "\n" - "void\n" - "main()\n" - "{\n" - " v_color = do_lighting();\n" - " v_texcoord = a_texcoord.xy;\n" - " gl_Position = u_mvp * a_position;\n" - "}\n"; - - static const char strFragmentShader_light[] = - "precision highp float;\n" - "uniform sampler2D u_tex2d;\n" - "varying vec2 v_texcoord;\n" - "varying vec4 v_color;\n" - "void main()\n" - "{\n" - " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n" - "}\n"; - - Shader vertexColor, fragmentColor; - Shader vertexUniformColor, fragmentUniformColor; - Shader vertexTexture, fragmentTexture, fragmentOpacityTexture; - Shader vertexLight, fragmentLight; - - // for color - vertexColor.Construct(Shader::SHADER_VERTEX, strVertexShader_color); - fragmentColor.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_color); - - __pColorShader->Construct(vertexColor, fragmentColor); - - vertexUniformColor.Construct(Shader::SHADER_VERTEX, strVertexShader_uniform_color); - fragmentUniformColor.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_uniform_color); - - __pUniformColorShader->Construct(vertexUniformColor, fragmentUniformColor); - - // for texture - if (__pGlContext->IsSupportedBGRA()) - { - vertexTexture.Construct(Shader::SHADER_VERTEX, strVertexShader_texture); - fragmentTexture.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_texture_bgra); - - fragmentOpacityTexture.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_texture_opacity_bgra); - } - else - { - vertexTexture.Construct(Shader::SHADER_VERTEX, strVertexShader_texture); - fragmentTexture.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_texture); - - fragmentOpacityTexture.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_texture_opacity); - } - - __pTextureShader->Construct(vertexTexture, fragmentTexture); - __pTextureOpacityShader->Construct(vertexTexture, fragmentOpacityTexture); - - vertexLight.Construct(Shader::SHADER_VERTEX, strVertexShader_light); - fragmentLight.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_light); - - __pLightShader->Construct(vertexLight, fragmentLight); -} - void* _GlRenderManager::RenderProc(void* pData) { @@ -1613,8 +1333,6 @@ _GlRenderManager::RenderProc(void* pData) #ifndef VE_USE_GL_MULTI_CONTEXT _GlContext::CreateInstance(_NativeWindow::GetDisplay()); pThis->__pGlContext = _GlContext::GetInstance(); - - pThis->PrepareShaders(); #endif pThis->__pAnimationManager = _AnimationManager::GetInstance(); @@ -1738,11 +1456,6 @@ exit_renderer: #endif #ifndef VE_USE_GL_MULTI_CONTEXT - delete pThis->__pColorShader; - delete pThis->__pUniformColorShader; - delete pThis->__pTextureShader; - delete pThis->__pTextureOpacityShader; - _GlContext::ReleaseInstance(); pThis->__pGlContext = null; #endif diff --git a/src/ui/animations/FUiAnim_GlRenderManager.h b/src/ui/animations/FUiAnim_GlRenderManager.h index 9d85edb..92f6afa 100644 --- a/src/ui/animations/FUiAnim_GlRenderManager.h +++ b/src/ui/animations/FUiAnim_GlRenderManager.h @@ -115,8 +115,6 @@ private: void CommandHandlerBuildShaderProgram(_ShaderProgramImpl* pShaderProgram); void CommandHandlerDeleteShaderProgram(_ShaderProgramImpl* pShaderProgram); - void PrepareShaders(void); - void CompositeLayer(_GlLayer* pLayer); void CompositeNode(VisualElement* pRenderObject, const _Matrix3Df& viewport, bool useStencil, unsigned int depth); @@ -228,13 +226,6 @@ private: _AnimationManager* __pAnimationManager; - //temp shader - ShaderProgram* __pColorShader; - ShaderProgram* __pUniformColorShader; - ShaderProgram* __pTextureShader; - ShaderProgram* __pTextureOpacityShader; - ShaderProgram* __pLightShader; - friend class _RenderObject; }; // _GlRenderManager diff --git a/src/ui/animations/platform/FUiAnim_Egl.cpp b/src/ui/animations/platform/FUiAnim_Egl.cpp index e70ea4c..00931fd 100644 --- a/src/ui/animations/platform/FUiAnim_Egl.cpp +++ b/src/ui/animations/platform/FUiAnim_Egl.cpp @@ -117,6 +117,8 @@ _Egl::_Egl(Handle nativeDisplay, EGLNativeWindowType window) _Egl::~_Egl(void) { + TerminateGL(); + #ifndef BUILD_UI_CORE #ifndef VE_USE_GL_MULTI_CONTEXT if (__nativeDummyPixmap) diff --git a/src/ui/animations/platform/FUiAnim_GlContext.cpp b/src/ui/animations/platform/FUiAnim_GlContext.cpp index 0336569..166f2dc 100644 --- a/src/ui/animations/platform/FUiAnim_GlContext.cpp +++ b/src/ui/animations/platform/FUiAnim_GlContext.cpp @@ -77,8 +77,13 @@ _GlContext::_GlContext(Handle nativeDisplay, EGLNativeWindowType window) , __useBlend(false) , __useStencil(false) , __useDepthTest(false) - , __pCurrentShader(null) , __textureId(0) + , __pCurrentShader(null) + , __pColorShader(null) + , __pUniformColorShader(null) + , __pTextureShader(null) + , __pTextureOpacityShader(null) + , __pLightShader(null) { PRINT(">> _GlContext:_GlContext() \n"); } @@ -322,6 +327,8 @@ _GlContext::CheckBGRASupport(void) void _GlContext::InitGL(void) { + PrepareDefaultShaders(); + glDisable(GL_BLEND); glDisable(GL_STENCIL_TEST); glDisable(GL_DEPTH_TEST); @@ -335,6 +342,16 @@ _GlContext::InitGL(void) } void +_GlContext::TerminateGL(void) +{ + delete __pColorShader; + delete __pUniformColorShader; + delete __pTextureShader; + delete __pTextureOpacityShader; + delete __pLightShader; +} + +void _GlContext::BindTexture(unsigned int textureId) { if (likely(__textureId != textureId)) @@ -672,4 +689,272 @@ _GlContext::DisableVertexAttribArray(_ShaderProgramImpl* pShaderProgram) } } +void +_GlContext::PrepareDefaultShaders(void) +{ + static const char strVertexShader_uniform_color[] = + "uniform mat4 u_mvp;\n" + "attribute vec4 a_position;\n" + "void main()\n" + "{\n" + " gl_Position = u_mvp * a_position;\n" + "}\n"; + static const char strFragmentShader_uniform_color[] = + "precision mediump float;\n" + "uniform vec4 u_color;\n" + "void main()\n" + "{\n" + " gl_FragColor = u_color;\n" + "}\n"; + + + static const char strVertexShader_color[] = + "uniform mat4 u_mvp;\n" + "attribute vec4 a_position;\n" + "attribute vec4 a_color;\n" + "varying vec4 v_color;\n" + "void main()\n" + "{\n" + " v_color = a_color;\n" + " gl_Position = u_mvp * a_position;\n" + "}\n"; + static const char strFragmentShader_color[] = + "precision mediump float;\n" + "varying vec4 v_color;\n" + "void main()\n" + "{\n" + " gl_FragColor = v_color;\n" + "}\n"; + + static const char strVertexShader_texture[] = + "uniform mat4 u_mvp;\n" + "attribute vec4 a_position;\n" + "attribute vec4 a_texcoord;\n" + "varying vec2 v_texcoord;\n" + "void main()\n" + "{\n" + " v_texcoord = a_texcoord.xy;\n" + " gl_Position = u_mvp * a_position;\n" + "}\n"; + + static const char strFragmentShader_texture[] = + "precision highp float;\n" + "uniform sampler2D u_tex2d;\n" + "uniform float u_opacity;\n" + "varying vec2 v_texcoord;\n" + "void main()\n" + "{\n" + " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra;\n" + "}\n"; + + static const char strFragmentShader_texture_bgra[] = + "precision highp float;\n" + "uniform sampler2D u_tex2d;\n" + "uniform float u_opacity;\n" + "varying vec2 v_texcoord;\n" + "void main()\n" + "{\n" + " gl_FragColor = texture2D(u_tex2d, v_texcoord);\n" + "}\n"; + + static const char strFragmentShader_texture_opacity[] = + "precision highp float;\n" + "uniform sampler2D u_tex2d;\n" + "uniform float u_opacity;\n" + "varying vec2 v_texcoord;\n" + "void main()\n" + "{\n" + " gl_FragColor.rgba = (texture2D(u_tex2d, v_texcoord) * u_opacity).bgra;\n" + "}\n"; + + static const char strFragmentShader_texture_opacity_bgra[] = + "precision highp float;\n" + "uniform sampler2D u_tex2d;\n" + "uniform float u_opacity;\n" + "varying vec2 v_texcoord;\n" + "void main()\n" + "{\n" + " gl_FragColor = texture2D(u_tex2d, v_texcoord) * u_opacity;\n" + "}\n"; + + static const char strVertexShader_light[] = + "struct light\n" + "{\n" + " int type;\n" + " vec4 ambient;\n" + " vec4 diffuse;\n" + " vec4 specular;\n" + " vec4 position;\n" + " vec3 direction;\n" + " float exponent;\n" + " float cutoff;\n" + " vec3 attenuation;\n" + "};\n" + "struct material\n" + "{\n" + " vec4 ambient;\n" + " vec4 diffuse;\n" + " vec4 specular;\n" + " vec4 emissive;\n" + " float shininess;\n" + "};\n" + "\n" + "const float c_zero = 0.0;\n" + "const float c_one = 1.0;\n" + "const int indx_zero = 0;\n" + "const int indx_one = 1;\n" + "\n" + "uniform mat4 u_mvp;\n" + "uniform mat4 u_modelview;\n" + "//uniform mat3 u_inv_modelview;\n" + "uniform mat4 u_inv_modelview;\n" + "\n" + "uniform material u_material;\n" + "uniform light u_light;\n" + "\n" + "attribute vec4 a_position;\n" + "attribute vec4 a_texcoord;\n" + "attribute vec4 a_color;\n" + "attribute vec3 a_normal;\n" + "\n" + "varying vec2 v_texcoord;\n" + "varying vec4 v_color;\n" + "\n" + "vec4\n" + "do_lighting()\n" + "{\n" + " vec3 lightdir, n, h;\n" + " vec4 color = vec4(c_zero, c_zero, c_zero, c_zero);\n" + " float ndotl, ndoth, attenuation;\n" + "\n" + " n = normalize((u_inv_modelview * vec4(a_normal, 1.0)).xyz);\n" + "\n" + " if (u_light.type < 2)\n" + " {\n" + " vec3 distance;\n" + " float spot_factor;\n" + "\n" + " // point or spot case\n" + " lightdir = u_light.position.xyz - (u_modelview * a_position).xyz;\n" + "\n" + " // compute distance attenuation\n" + " distance.x = c_one;\n" + " distance.z = dot(lightdir, lightdir);\n" + " distance.y = sqrt(distance.z);\n" + "\n" + " attenuation = c_one / dot(distance, u_light.attenuation);" + "\n" + " lightdir = normalize(lightdir);\n" + "\n" + " if (u_light.cutoff < 180.0)\n" + " {\n" + " // compute spot factor\n" + " spot_factor = dot(-lightdir, normalize(u_light.direction));\n" + "\n" + " if (spot_factor >= cos(radians(u_light.cutoff)))\n" + " spot_factor = pow(spot_factor, u_light.exponent);\n" + " else\n" + " spot_factor = c_zero;\n" + "\n" + " attenuation *= spot_factor;\n" + " }\n" + " }\n" + " else\n" + " {\n" + " // directional light\n" + " lightdir = normalize(u_light.position.xyz);\n" + " attenuation = c_one;\n" + " }\n" + "\n" + " if (attenuation > c_zero)\n" + " {\n" + " color = u_light.ambient * u_material.ambient;\n" + "\n" + " ndotl = max(c_zero, dot(n, lightdir));\n" + " color += ndotl * u_light.diffuse * u_material.diffuse;\n" + "\n" + " h = normalize(lightdir + vec3(c_zero, c_zero, c_one));\n" + " ndoth = dot(n, h);\n" + "\n" + " if (ndoth > c_zero)\n" + " {\n" + " color += pow(ndoth, u_material.shininess) * u_material.specular * u_light.specular;\n" + " }\n" + "\n" + " color *= attenuation;\n" + " }\n" + "\n" + " color += u_material.emissive;\n" + "// color += u_material.emissive + u_material.ambient * u_scene_ambient; //todo\n" + "\n" + " color.a = u_material.diffuse.a;\n" + "\n" + " return color;\n" + "}\n" + "\n" + "void\n" + "main()\n" + "{\n" + " v_color = do_lighting();\n" + " v_texcoord = a_texcoord.xy;\n" + " gl_Position = u_mvp * a_position;\n" + "}\n"; + + static const char strFragmentShader_light[] = + "precision highp float;\n" + "uniform sampler2D u_tex2d;\n" + "varying vec2 v_texcoord;\n" + "varying vec4 v_color;\n" + "void main()\n" + "{\n" + " gl_FragColor.rgba = texture2D(u_tex2d, v_texcoord).bgra * v_color;\n" + "}\n"; + + Shader vertexColor, fragmentColor; + Shader vertexUniformColor, fragmentUniformColor; + Shader vertexTexture, fragmentTexture, fragmentOpacityTexture; + Shader vertexLight, fragmentLight; + + __pColorShader = new (std::nothrow) ShaderProgram; + __pUniformColorShader = new (std::nothrow) ShaderProgram; + __pTextureShader = new (std::nothrow) ShaderProgram; + __pTextureOpacityShader = new (std::nothrow) ShaderProgram; + __pLightShader = new (std::nothrow) ShaderProgram; + + // for color + vertexColor.Construct(Shader::SHADER_VERTEX, strVertexShader_color); + fragmentColor.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_color); + + __pColorShader->Construct(vertexColor, fragmentColor); + + vertexUniformColor.Construct(Shader::SHADER_VERTEX, strVertexShader_uniform_color); + fragmentUniformColor.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_uniform_color); + + __pUniformColorShader->Construct(vertexUniformColor, fragmentUniformColor); + + // for texture + if (IsSupportedBGRA()) + { + vertexTexture.Construct(Shader::SHADER_VERTEX, strVertexShader_texture); + fragmentTexture.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_texture_bgra); + + fragmentOpacityTexture.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_texture_opacity_bgra); + } + else + { + vertexTexture.Construct(Shader::SHADER_VERTEX, strVertexShader_texture); + fragmentTexture.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_texture); + + fragmentOpacityTexture.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_texture_opacity); + } + + __pTextureShader->Construct(vertexTexture, fragmentTexture); + __pTextureOpacityShader->Construct(vertexTexture, fragmentOpacityTexture); + + vertexLight.Construct(Shader::SHADER_VERTEX, strVertexShader_light); + fragmentLight.Construct(Shader::SHADER_FRAGMENT, strFragmentShader_light); + + __pLightShader->Construct(vertexLight, fragmentLight); +} + }}} // Tizen::Ui::Animations -- 2.7.4