Wrapped ShaderHints enum inside a struct
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / shader-impl.cpp
index 1a075c2..2eb7cf6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,8 +21,7 @@
 // INTERNAL INCLUDES
 #include <dali/public-api/object/type-registry.h>
 #include <dali/public-api/shader-effects/shader-effect.h> // Dali::ShaderEffect::GeometryHints // TODO: MESH_REWORK REMOVE
-#include <dali/devel-api/rendering/shader.h> // Dali::Shader
-
+#include <dali/devel-api/scripting/scripting.h>
 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
 #include <dali/internal/event/common/thread-local-storage.h>
@@ -43,11 +42,18 @@ namespace
  */
 DALI_PROPERTY_TABLE_BEGIN
 DALI_PROPERTY( "program",       MAP,     true,     false,     false,  Dali::Shader::Property::PROGRAM )
-DALI_PROPERTY( "shader-hints",  INTEGER, true,     false,     true,   Dali::Shader::Property::SHADER_HINTS )
 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
 
 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS };
 
+Dali::Scripting::StringEnum ShaderHintsTable[] =
+  { { "NONE",                     Dali::Shader::Hint::NONE},
+    { "OUTPUT_IS_TRANSPARENT",    Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT},
+    { "MODIFIES_GEOMETRY",        Dali::Shader::Hint::MODIFIES_GEOMETRY}
+  };
+
+const unsigned int ShaderHintsTableSize = sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] );
+
 BaseHandle Create()
 {
   return Dali::BaseHandle();
@@ -55,13 +61,46 @@ BaseHandle Create()
 
 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
 
+#define TOKEN_STRING(x) (#x)
+
+void AppendString(std::string& to, const std::string& append)
+{
+  if(to.size())
+  {
+    to += ",";
+  }
+  to += append;
+}
+
+Property::Value HintString(const Dali::Shader::Hint::Value& hints)
+{
+  std::string s;
+
+  if(hints == Dali::Shader::Hint::NONE)
+  {
+    s = "NONE";
+  }
+
+  if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
+  {
+    AppendString(s, "OUTPUT_IS_TRANSPARENT");
+  }
+
+  if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
+  {
+    AppendString(s, "MODIFIES_GEOMETRY");
+  }
+
+  return Property::Value(s);
+}
+
+
 } // unnamed namespace
 
 ShaderPtr Shader::New( const std::string& vertexShader,
                        const std::string& fragmentShader,
-                       Dali::Shader::ShaderHints hints )
+                       Dali::Shader::Hint::Value hints )
 {
-  //TODO: MESH_REWORK
   ShaderPtr shader( new Shader() );
   shader->Initialize( vertexShader, fragmentShader, hints );
   return shader;
@@ -124,13 +163,40 @@ void Shader::SetDefaultProperty( Property::Index index,
   {
     case Dali::Shader::Property::PROGRAM:
     {
-      // @todo MESH_REWORK Set program again?
-      DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
-      break;
-    }
-    case Dali::Shader::Property::SHADER_HINTS:
-    {
-      DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
+      if( propertyValue.GetType() == Property::MAP )
+      {
+        Dali::Property::Map* map = propertyValue.GetMap();
+        if( map )
+        {
+          std::string vertex;
+          std::string fragment;
+          Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
+
+          if( Property::Value* value = map->Find("vertex") )
+          {
+            vertex = value->Get<std::string>();
+          }
+
+          if( Property::Value* value = map->Find("fragment") )
+          {
+            fragment = value->Get<std::string>();
+          }
+
+          if( Property::Value* value = map->Find("hints") )
+          {
+            static_cast<void>( // ignore return
+              Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
+                                                                     ShaderHintsTable, ShaderHintsTableSize, hints)
+              );
+          }
+
+          Initialize(vertex, fragment, hints );
+        }
+      }
+      else
+      {
+        DALI_LOG_WARNING( "Shader program property should be a map\n" );
+      }
       break;
     }
   }
@@ -152,12 +218,14 @@ Property::Value Shader::GetDefaultProperty( Property::Index index ) const
   {
     case Dali::Shader::Property::PROGRAM:
     {
-      DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
-      break;
-    }
-    case Dali::Shader::Property::SHADER_HINTS:
-    {
-      DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
+      Dali::Property::Map map;
+      if( mShaderData )
+      {
+        map["vertex"] = Property::Value(mShaderData->GetVertexShader());
+        map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
+        map["hints"] = HintString(mShaderData->GetHints());
+      }
+      value = map;
       break;
     }
   }
@@ -195,28 +263,19 @@ const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Proper
 
 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
 {
-  const PropertyInputImpl* property = NULL;
-
-  const SceneGraph::PropertyBase* baseProperty =
-    SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
-                                                 &Shader::FindAnimatableProperty,
-                                                 &Shader::FindCustomProperty,
-                                                 index );
-  property = static_cast<const PropertyInputImpl*>( baseProperty );
+  PropertyMetadata* property = NULL;
 
-  if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
+  if(index >= PROPERTY_CUSTOM_START_INDEX )
   {
-    if( index == Dali::Shader::Property::SHADER_HINTS )
-    {
-      // @todo MESH_REWORK - return the property
-    }
-    else
-    {
-      DALI_ASSERT_ALWAYS( 0 && "Property is not a valid constraint input" );
-    }
+    property = FindCustomProperty( index );
+  }
+  else
+  {
+    property = FindAnimatableProperty( index );
   }
 
-  return property;
+  DALI_ASSERT_ALWAYS( property && "property index is invalid" );
+  return property->GetSceneGraphProperty();
 }
 
 int Shader::GetPropertyComponentIndex( Property::Index index ) const
@@ -225,37 +284,19 @@ int Shader::GetPropertyComponentIndex( Property::Index index ) const
 }
 
 Shader::Shader()
-: mSceneObject( NULL )
+  : mSceneObject( NULL ),
+    mShaderData( NULL )
 {
 }
 
 void Shader::Initialize(
   const std::string& vertexSource,
   const std::string& fragmentSource,
-  Dali::Shader::ShaderHints hints )
+  Dali::Shader::Hint::Value hints )
 {
   EventThreadServices& eventThreadServices = GetEventThreadServices();
   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
-
-  // @todo MESH_REWORK - Pass hints directly to a new scene graph shader
-  int effectHint = Dali::ShaderEffect::HINT_NONE;
-  if( hints & Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT )
-  {
-    effectHint |= Dali::ShaderEffect::HINT_BLENDING;
-  }
-
-  if( hints & Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST )
-  {
-    effectHint |= Dali::ShaderEffect::HINT_DEPTH_BUFFER;
-  }
-
-  if( (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) == 0x0 )
-  {
-    effectHint |= Dali::ShaderEffect::HINT_DOESNT_MODIFY_GEOMETRY;
-  }
-  Dali::ShaderEffect::GeometryHints shaderEffectHint = static_cast<Dali::ShaderEffect::GeometryHints>( effectHint );
-
-  mSceneObject = new SceneGraph::Shader( shaderEffectHint );
+  mSceneObject = new SceneGraph::Shader( hints );
 
   // Add to update manager
   AddShaderMessage( updateManager, *mSceneObject );
@@ -264,10 +305,10 @@ void Shader::Initialize(
   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
   ShaderFactory& shaderFactory = tls.GetShaderFactory();
   size_t shaderHash;
-  Internal::ShaderDataPtr shaderData = shaderFactory.Load( vertexSource, fragmentSource, shaderHash );
+  mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
 
   // Add shader program to scene-object using a message to the UpdateManager
-  SetShaderProgramMessage( updateManager, *mSceneObject, shaderData, (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) != 0x0 );
+  SetShaderProgramMessage( updateManager, *mSceneObject, mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
   eventThreadServices.RegisterObject( this );
 }