Change ShaderProgram class
authorHeeyong Song <heeyong.song@samsung.com>
Wed, 16 Oct 2013 08:00:44 +0000 (17:00 +0900)
committerHeeyong Song <heeyong.song@samsung.com>
Wed, 16 Oct 2013 08:07:40 +0000 (17:07 +0900)
Change-Id: I819cad6011975a36021ce99e8d23db90ef199e08

inc/FUiAnimShaderProgram.h
src/ui/animations/FUiAnimShaderProgram.cpp
src/ui/animations/FUiAnim_GeometryInfo.cpp
src/ui/animations/FUiAnim_GlNode.cpp
src/ui/animations/FUiAnim_ShaderProgramImpl.cpp
src/ui/animations/FUiAnim_ShaderProgramImpl.h

index 14aa1e7..fb8bb52 100644 (file)
@@ -53,9 +53,7 @@
 #define _FUI_ANIM_SHADER_PROGRAM_H_
 
 #include <FBaseObject.h>
-
 #include <FUiVariant.h>
-
 #include <FUiAnimShader.h>
 
 namespace Tizen { namespace Ui { namespace Animations
@@ -107,22 +105,6 @@ public:
        result Construct(Shader& vertexShader, Shader& fragmentShader);
 
        /**
-        * Constructs the ShaderProgram using vertexShader and fragmentShader.
-        *
-        * @since 3.0
-        *
-        * @return              An error code
-        *
-        * @param[in]   vertexShader     source code of vertex shader
-        * @param[in]   fragmentShader   source code of fragment shader
-        *
-        * @exception   E_SUCCESS
-        * @exception   E_OUT_OF_MEMORY
-        * @exception   E_SYSTEM if shader was not constructed or compiled with an error
-        */
-       result Construct(const Tizen::Base::String& vertexShader, const Tizen::Base::String& fragmentShader);
-
-       /**
         * Gets the value of uniform by name of the current instance of %ShaderProgram
         *
         * @since 3.0
@@ -152,6 +134,8 @@ public:
         */
        result SetUniform(const Tizen::Base::String& name, const Tizen::Ui::Variant& value);
 
+       bool Equals(const ShaderProgram& obj) const;
+
 private:
 
        ShaderProgram& operator =(const ShaderProgram& rhs);
index d38c743..59cd08b 100644 (file)
@@ -21,8 +21,6 @@
  *
  * This file contains implementation ShaderProgram class.
  */
-
-
 #include <FBaseSysLog.h>
 #include <FUiVariant.h>
 #include <FUiAnimShader.h>
 
 #include "FUiAnim_ShaderProgramImpl.h"
 
-
 namespace Tizen { namespace Ui { namespace Animations
 {
 
-
-
 ShaderProgram::ShaderProgram(void)
        : __pShaderProgramImpl(null)
 {
 }
+
 ShaderProgram::ShaderProgram(const ShaderProgram& shaderProgram)
        : __pShaderProgramImpl(null)
 {
@@ -47,11 +43,12 @@ ShaderProgram::ShaderProgram(const ShaderProgram& shaderProgram)
 
        __pShaderProgramImpl = shaderProgram.__pShaderProgramImpl;
 
-    if(__pShaderProgramImpl != null)
+       if (__pShaderProgramImpl != null)
     {
        __pShaderProgramImpl->AddRef();
     }
 }
+
 ShaderProgram::~ShaderProgram(void)
 {
        if (__pShaderProgramImpl)
@@ -73,21 +70,10 @@ ShaderProgram::Construct(Shader& vertexShader, Shader& fragmentShader)
 
        result r = __pShaderProgramImpl->Construct(vertexShader, fragmentShader);
        SysTryReturnResult(NID_UI_ANIM, r == E_SUCCESS, r, "Propagating.");
-       return E_SUCCESS;
-}
 
-result
-ShaderProgram::Construct(const Tizen::Base::String& vertexShader, const Tizen::Base::String& fragmentShader)
-{
-       ClearLastResult();
-       SysAssertf(__pShaderProgramImpl == null,
-                                       "Already constructed. Calling Construct() twice or more on a same instance is not allowed for this class.");
-       __pShaderProgramImpl = new (std::nothrow) _ShaderProgramImpl();
-       SysTryReturnResult(NID_UI_ANIM, __pShaderProgramImpl != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
-       result r = __pShaderProgramImpl->Construct(vertexShader, fragmentShader);
-       SysTryReturnResult(NID_UI_ANIM, r == E_SUCCESS, r, "Propagating.");
        return E_SUCCESS;
 }
+
 Variant
 ShaderProgram::GetUniform(const Tizen::Base::String& name) const
 {
@@ -107,5 +93,16 @@ ShaderProgram::SetUniform(const Tizen::Base::String& name, const Tizen::Ui::Vari
 
        return __pShaderProgramImpl->SetUniform(name, value);
 }
+
+bool
+ShaderProgram::Equals(const ShaderProgram& obj) const
+{
+       ClearLastResult();
+       SysAssertf(__pShaderProgramImpl != null,
+                                               "Not yet constructed. Construct() should be called before use.");
+
+       return __pShaderProgramImpl->Equals(obj);
+}
+
 }}}            // Tizen::Ui::Animations
 
index f3656bc..8b9b733 100644 (file)
@@ -203,13 +203,12 @@ _GeometryInfo::SetShaderProgram(ShaderProgram* pShaderProgram)
 {
        unsigned int program = 0;
 
-       if (pShaderProgram)
+       if (pShaderProgram != null)
        {
                program = _ShaderProgramImpl::GetInstance(*pShaderProgram)->__program;
 
                if (program <= 0)
                {
-                       //__pShaderProgram = null;
                        if (__pShaderProgram != null)
                        {
                                delete __pShaderProgram;
@@ -218,20 +217,19 @@ _GeometryInfo::SetShaderProgram(ShaderProgram* pShaderProgram)
                        SysLog(NID_UI_ANIM, "[E_INVALID_ARG] Invalid argument is used. programId = %d", program);
                        return E_INVALID_ARG;
                }
-       }
-       if (pShaderProgram != null)
-       {
 
-                if (__pShaderProgram)
-                {
-                        delete __pShaderProgram;
-                        __pShaderProgram = new (std::nothrow) ShaderProgram(*pShaderProgram);
-                }
-                else
-                {
-                        __pShaderProgram = new (std::nothrow) ShaderProgram(*pShaderProgram);
-                }
-        }
+               if (!__pShaderProgram || !pShaderProgram->Equals(*__pShaderProgram))
+               {
+                       if (__pShaderProgram)
+                       {
+                               delete __pShaderProgram;
+                               __pShaderProgram = null;
+                       }
+
+                       __pShaderProgram = new (std::nothrow) ShaderProgram(*pShaderProgram);
+                       SysAssertf(__pShaderProgram, "failed to create shader program");
+               }
+       }
        else
        {
                if (__pShaderProgram != null)
@@ -241,7 +239,6 @@ _GeometryInfo::SetShaderProgram(ShaderProgram* pShaderProgram)
                }
        }
 
-       //__pShaderProgram = pShaderProgram;
        __isShaderProgramUpdated = true;
 
        return E_SUCCESS;
index 2b7e2f8..2740dc0 100644 (file)
@@ -528,19 +528,29 @@ _GlNode::SyncStatus(VisualElementSurface* pSurface, _VisualElementImpl& element)
                }
        }
 
-       //temp for shader
-       ShaderProgram* pProgram = element.GetSharedData().geometry.GetShaderProgram();
+       ShaderProgram* pShaderProgram = element.GetSharedData().geometry.GetShaderProgram();
 
-       if (__pShaderProgram && pProgram)
+       if (unlikely(pShaderProgram))
        {
-               if (_ShaderProgramImpl::GetInstance(*__pShaderProgram)->__program != _ShaderProgramImpl::GetInstance(*pProgram)->__program)
+               if (!__pShaderProgram || !pShaderProgram->Equals(*__pShaderProgram))
                {
-                       __pShaderProgram = pProgram;
+                       if (__pShaderProgram)
+                       {
+                               delete __pShaderProgram;
+                               __pShaderProgram = null;
+                       }
+
+                       __pShaderProgram = new (std::nothrow) ShaderProgram(*pShaderProgram);
+                       SysAssertf(__pShaderProgram, "failed to create shader program");
                }
        }
        else
        {
-               __pShaderProgram = pProgram;
+               if (__pShaderProgram)
+               {
+                       delete __pShaderProgram;
+                       __pShaderProgram = null;
+               }
        }
 
        __syncNeeded = false;
@@ -548,7 +558,6 @@ _GlNode::SyncStatus(VisualElementSurface* pSurface, _VisualElementImpl& element)
        return E_SUCCESS;
 }
 
-
 VisualElementSurface*
 _GlNode::GetSurface(void) const
 {
index 3afea03..dfebf72 100644 (file)
@@ -111,25 +111,6 @@ _ShaderProgramImpl::Construct(Shader& vertexShader, Shader& fragmentShader)
        return E_SUCCESS;
 }
 
-result
-_ShaderProgramImpl::Construct(const Tizen::Base::String&vertexShader, const Tizen::Base::String& fragmentShader)
-{
-       std::wstring wStr(vertexShader.GetPointer());
-       std::string vertexShaderStr(wStr.begin(), wStr.end());
-
-       std::wstring wStr1(fragmentShader.GetPointer());
-       std::string fragmentShaderStr(wStr1.begin(), wStr1.end());
-       Shader vert;
-       vert.Construct(Shader::SHADER_VERTEX,vertexShaderStr.c_str());
-       Shader frag;
-       frag.Construct(Shader::SHADER_FRAGMENT,fragmentShaderStr.c_str());
-       __vertexShader =  _ShaderImpl::GetInstance(vert)->__shaderId;
-       __fragmentShader =  _ShaderImpl::GetInstance(frag)->__shaderId;
-       _GlRenderManager::GetInstance()->BuildShaderProgram(this);
-
-       return E_SUCCESS;
-}
-
 Variant
 _ShaderProgramImpl::GetUniform(const Tizen::Base::String& name) const
 {
@@ -161,6 +142,20 @@ _ShaderProgramImpl::SetUniform(const Tizen::Base::String& name, const Tizen::Ui:
        return E_INVALID_ARG;
 }
 
+bool
+_ShaderProgramImpl::Equals(const ShaderProgram& program) const
+{
+       const ShaderProgram* pProgram = dynamic_cast< const ShaderProgram* >(&program);
+       if (pProgram == null)
+       {
+               return false;
+       }
+
+       const _ShaderProgramImpl* pProgramImpl = _ShaderProgramImpl::GetInstance(program);
+
+       return (pProgramImpl == this);
+}
+
 _ShaderProgramImpl*
 _ShaderProgramImpl::GetInstance(ShaderProgram& program)
 {
index aaa205b..cadde78 100644 (file)
@@ -106,10 +106,12 @@ public:
        virtual ~_ShaderProgramImpl(void);
 
        result Construct(Shader& vertexShader, Shader& fragmentShader);
-       result Construct(const Tizen::Base::String& vertexShader, const Tizen::Base::String& fragmentShader);   // TODO: remove
 
        Variant GetUniform(const Tizen::Base::String& name) const;
        result SetUniform(const Tizen::Base::String& name, const Tizen::Ui::Variant& value);
+
+       bool Equals(const ShaderProgram& program) const;
+
        static _ShaderProgramImpl* GetInstance(ShaderProgram& program);
        static const _ShaderProgramImpl* GetInstance(const ShaderProgram& program);
 
@@ -120,7 +122,7 @@ private:
        void MoveUniformItem(int index, const wchar_t* pUniformName);
 
 private:
-       _ShaderProgramImpl(const _ShaderProgramImpl& shader);
+       _ShaderProgramImpl(const _ShaderProgramImpl& program);
        _ShaderProgramImpl& operator =(const _ShaderProgramImpl& rhs);
 
 private:
@@ -141,8 +143,7 @@ private:
 
        friend class _GlContext;
        friend class _GlRenderManager;
-       friend class _GlNode;           // TODO: remove. add GetProgramId()
-       friend class _GeometryInfo;     // TODO: remove. add GetProgramId()
+       friend class _GeometryInfo;
 };
 
 }}} //Tizen::Ui::Animations