Merge "Clean up the code to build successfully on macOS" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / shader-impl.cpp
index 0d25764..aa85ac7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2018 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.
 
 // 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/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
+#include <dali/devel-api/scripting/scripting.h>
 #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>
 #include <dali/internal/event/effects/shader-factory.h>
-#include <dali/internal/event/resources/resource-ticket.h>
 #include <dali/internal/update/manager/update-manager.h>
 
 namespace Dali
@@ -39,106 +35,132 @@ namespace
 {
 
 /**
- *            |name            |type             |writable|animatable|constraint-input|enum for index-checking|
+ *            |name             |type    |writable|animatable|constraint-input|enum for index-checking|
  */
 DALI_PROPERTY_TABLE_BEGIN
-DALI_PROPERTY( "program",       MAP,              true, false,  false,  Dali::Shader::Property::PROGRAM )
-DALI_PROPERTY( "shader-hints",  UNSIGNED_INTEGER, true, false,  true,   Dali::Shader::Property::SHADER_HINTS )
-DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
+DALI_PROPERTY( "program",       MAP,     true,     false,     false,  Dali::Shader::Property::PROGRAM )
+DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX, ShaderDefaultProperties )
+
+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 ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS };
+const uint32_t ShaderHintsTableSize = static_cast<uint32_t>( sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] ) );
 
 BaseHandle Create()
 {
   return Dali::BaseHandle();
 }
 
-TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
+TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create, ShaderDefaultProperties );
 
-} // unnamed namespace
+#define TOKEN_STRING(x) (#x)
 
-ShaderPtr Shader::New( const std::string& vertexShader,
-                       const std::string& fragmentShader,
-                       Dali::Shader::ShaderHints hints )
+void AppendString(std::string& to, const std::string& append)
 {
-  //TODO: MESH_REWORK
-  ShaderPtr shader( new Shader() );
-  shader->Initialize( vertexShader, fragmentShader, hints );
-  return shader;
+  if(to.size())
+  {
+    to += ",";
+  }
+  to += append;
 }
 
-const SceneGraph::Shader* Shader::GetShaderSceneObject() const
+Property::Value HintString(const Dali::Shader::Hint::Value& hints)
 {
-  return mSceneObject;
-}
+  std::string s;
 
-unsigned int Shader::GetDefaultPropertyCount() const
-{
-  return SHADER_IMPL.GetDefaultPropertyCount();
-}
+  if(hints == Dali::Shader::Hint::NONE)
+  {
+    s = "NONE";
+  }
 
-void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
-{
-  SHADER_IMPL.GetDefaultPropertyIndices( indices );
-}
+  if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
+  {
+    AppendString(s, "OUTPUT_IS_TRANSPARENT");
+  }
 
-const char* Shader::GetDefaultPropertyName(Property::Index index) const
-{
-  return SHADER_IMPL.GetDefaultPropertyName( index );
-}
+  if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
+  {
+    AppendString(s, "MODIFIES_GEOMETRY");
+  }
 
-Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
-{
-  return SHADER_IMPL.GetDefaultPropertyIndex( name );
+  return Property::Value(s);
 }
 
-bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
-{
-  return SHADER_IMPL.IsDefaultPropertyWritable( index );
-}
+} // unnamed namespace
 
-bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
+ShaderPtr Shader::New( const std::string& vertexShader,
+                       const std::string& fragmentShader,
+                       Dali::Shader::Hint::Value hints )
 {
-  return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
-}
+  // create scene object first so it's guaranteed to exist for the event side
+  auto sceneObject = new SceneGraph::Shader( hints );
+  OwnerPointer< SceneGraph::Shader > transferOwnership( sceneObject );
+  // pass the pointer to base for message passing
+  ShaderPtr shader( new Shader( sceneObject ) );
+  // transfer scene object ownership to update manager
+  auto&& services = shader->GetEventThreadServices();
+  SceneGraph::UpdateManager& updateManager = services.GetUpdateManager();
+  AddShaderMessage( updateManager, transferOwnership );
 
-bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
-{
-  return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
+  services.RegisterObject( shader.Get() );
+  shader->SetShader( vertexShader, fragmentShader, hints );
+
+  return shader;
 }
 
-Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
+const SceneGraph::Shader& Shader::GetShaderSceneObject() const
 {
-  return SHADER_IMPL.GetDefaultPropertyType( index );
+  return static_cast<const SceneGraph::Shader&>( GetSceneObject() );
 }
 
-void Shader::SetDefaultProperty( Property::Index index,
-                                 const Property::Value& propertyValue )
+void Shader::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
 {
   switch(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 )
+      {
+        const 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)
+              );
+          }
+
+          SetShader( vertex, fragment, hints );
+        }
+      }
+      else
+      {
+        DALI_LOG_WARNING( "Shader program property should be a map\n" );
+      }
       break;
     }
   }
 }
 
-void Shader::SetSceneGraphProperty( Property::Index index,
-                                    const PropertyMetadata& entry,
-                                    const Property::Value& value )
-{
-  SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
-  OnPropertySet(index, value);
-}
-
 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
 {
   Property::Value value;
@@ -147,146 +169,46 @@ 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" );
-      break;
-    }
-  }
-
-  return value;
-}
-
-const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
-{
-  return mSceneObject;
-}
-
-const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
-{
-  return mSceneObject;
-}
-
-const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
-{
-  DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
-  const SceneGraph::PropertyBase* property = NULL;
-
-  if( OnStage() )
-  {
-    property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
-                                                            &Shader::FindAnimatableProperty,
-                                                            &Shader::FindCustomProperty,
-                                                            index );
-
-    if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
-    {
-      DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
-    }
-  }
-
-  return property;
-}
-
-const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
-{
-  const PropertyInputImpl* property = NULL;
-
-  if( OnStage() )
-  {
-    const SceneGraph::PropertyBase* baseProperty =
-      SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
-                                                   &Shader::FindAnimatableProperty,
-                                                   &Shader::FindCustomProperty,
-                                                   index );
-    property = static_cast<const PropertyInputImpl*>( baseProperty );
-
-    if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
-    {
-      if( index == Dali::Shader::Property::SHADER_HINTS )
+      Dali::Property::Map map;
+      if( mShaderData )
       {
-        // @todo MESH_REWORK - return the property
-      }
-      else
-      {
-        DALI_ASSERT_ALWAYS( 0 && "Property is not a valid constraint input" );
+        map["vertex"] = Property::Value(mShaderData->GetVertexShader());
+        map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
+        map["hints"] = HintString(mShaderData->GetHints());
       }
+      value = map;
+      break;
     }
   }
 
-  return property;
-}
-
-int Shader::GetPropertyComponentIndex( Property::Index index ) const
-{
-  return Property::INVALID_COMPONENT_INDEX;
-}
-
-bool Shader::OnStage() const
-{
-  return mOnStage;
-}
-
-void Shader::Connect()
-{
-  mOnStage = true;
+  return value;
 }
 
-void Shader::Disconnect()
+Property::Value Shader::GetDefaultPropertyCurrentValue( Property::Index index ) const
 {
-  mOnStage = false;
+  return GetDefaultProperty( index ); // Event-side only properties
 }
 
-Shader::Shader()
-: mSceneObject( NULL ),
-  mOnStage( false )
+Shader::Shader( const SceneGraph::Shader* sceneObject )
+: Object( sceneObject ),
+  mShaderData( nullptr )
 {
 }
 
-void Shader::Initialize(
-  const std::string& vertexSource,
-  const std::string& fragmentSource,
-  Dali::Shader::ShaderHints hints )
+void Shader::SetShader( const std::string& vertexSource,
+                        const std::string& fragmentSource,
+                        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);
-
-  // Add to update manager
-  AddShaderMessage( updateManager, *mSceneObject );
-
+  // Try to load a pre-compiled shader binary for the source pair:
   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
   ShaderFactory& shaderFactory = tls.GetShaderFactory();
   size_t shaderHash;
-
-  mTicket = ResourceTicketPtr( 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, mTicket->GetId(), shaderHash, false );
-
-  eventThreadServices.RegisterObject( this );
+  EventThreadServices& eventThreadServices = GetEventThreadServices();
+  SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
+  SetShaderProgramMessage( updateManager, GetShaderSceneObject(), mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
 }
 
 Shader::~Shader()
@@ -295,12 +217,29 @@ Shader::~Shader()
   {
     EventThreadServices& eventThreadServices = GetEventThreadServices();
     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
-    RemoveShaderMessage( updateManager, *mSceneObject);
+    RemoveShaderMessage( updateManager, &GetShaderSceneObject() );
 
     eventThreadServices.UnregisterObject( this );
   }
 }
 
+std::string Shader::GetShaderVersionPrefix()
+{
+  Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
+  return tls.GetShaderVersionPrefix();
+}
+
+std::string Shader::GetVertexShaderPrefix()
+{
+  Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
+  return tls.GetVertexShaderPrefix();
+}
+
+std::string Shader::GetFragmentShaderPrefix()
+{
+  Dali::Internal::ThreadLocalStorage& tls = Dali::Internal::ThreadLocalStorage::Get();
+  return tls.GetFragmentShaderPrefix();
+}
 
 } // namespace Internal
 } // namespace Dali