[3.0] Remove/move experimental features
[platform/core/uifw/dali-core.git] / dali / internal / event / rendering / shader-impl.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/rendering/shader-impl.h> // Dali::Internal::Shader
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/devel-api/scripting/scripting.h>
24 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
25 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/effects/shader-factory.h>
28 #include <dali/internal/event/resources/resource-ticket.h>
29 #include <dali/internal/update/manager/update-manager.h>
30
31 namespace Dali
32 {
33 namespace Internal
34 {
35
36 namespace
37 {
38
39 /**
40  *            |name             |type    |writable|animatable|constraint-input|enum for index-checking|
41  */
42 DALI_PROPERTY_TABLE_BEGIN
43 DALI_PROPERTY( "program",       MAP,     true,     false,     false,  Dali::Shader::Property::PROGRAM )
44 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
45
46 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS };
47
48 Dali::Scripting::StringEnum ShaderHintsTable[] =
49   { { "NONE",                     Dali::Shader::Hint::NONE},
50     { "OUTPUT_IS_TRANSPARENT",    Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT},
51     { "MODIFIES_GEOMETRY",        Dali::Shader::Hint::MODIFIES_GEOMETRY}
52   };
53
54 const unsigned int ShaderHintsTableSize = sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] );
55
56 BaseHandle Create()
57 {
58   return Dali::BaseHandle();
59 }
60
61 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
62
63 #define TOKEN_STRING(x) (#x)
64
65 void AppendString(std::string& to, const std::string& append)
66 {
67   if(to.size())
68   {
69     to += ",";
70   }
71   to += append;
72 }
73
74 Property::Value HintString(const Dali::Shader::Hint::Value& hints)
75 {
76   std::string s;
77
78   if(hints == Dali::Shader::Hint::NONE)
79   {
80     s = "NONE";
81   }
82
83   if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
84   {
85     AppendString(s, "OUTPUT_IS_TRANSPARENT");
86   }
87
88   if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
89   {
90     AppendString(s, "MODIFIES_GEOMETRY");
91   }
92
93   return Property::Value(s);
94 }
95
96
97 } // unnamed namespace
98
99 ShaderPtr Shader::New( const std::string& vertexShader,
100                        const std::string& fragmentShader,
101                        Dali::Shader::Hint::Value hints )
102 {
103   ShaderPtr shader( new Shader() );
104   shader->Initialize( vertexShader, fragmentShader, hints );
105   return shader;
106 }
107
108 const SceneGraph::Shader* Shader::GetShaderSceneObject() const
109 {
110   return mSceneObject;
111 }
112
113 SceneGraph::Shader* Shader::GetShaderSceneObject()
114 {
115   return mSceneObject;
116 }
117
118 unsigned int Shader::GetDefaultPropertyCount() const
119 {
120   return SHADER_IMPL.GetDefaultPropertyCount();
121 }
122
123 void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
124 {
125   SHADER_IMPL.GetDefaultPropertyIndices( indices );
126 }
127
128 const char* Shader::GetDefaultPropertyName(Property::Index index) const
129 {
130   return SHADER_IMPL.GetDefaultPropertyName( index );
131 }
132
133 Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
134 {
135   return SHADER_IMPL.GetDefaultPropertyIndex( name );
136 }
137
138 bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
139 {
140   return SHADER_IMPL.IsDefaultPropertyWritable( index );
141 }
142
143 bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
144 {
145   return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
146 }
147
148 bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
149 {
150   return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
151 }
152
153 Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
154 {
155   return SHADER_IMPL.GetDefaultPropertyType( index );
156 }
157
158 void Shader::SetDefaultProperty( Property::Index index,
159                                  const Property::Value& propertyValue )
160 {
161   switch(index)
162   {
163     case Dali::Shader::Property::PROGRAM:
164     {
165       if( propertyValue.GetType() == Property::MAP )
166       {
167         Dali::Property::Map* map = propertyValue.GetMap();
168         if( map )
169         {
170           std::string vertex;
171           std::string fragment;
172           Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
173
174           if( Property::Value* value = map->Find("vertex") )
175           {
176             vertex = value->Get<std::string>();
177           }
178
179           if( Property::Value* value = map->Find("fragment") )
180           {
181             fragment = value->Get<std::string>();
182           }
183
184           if( Property::Value* value = map->Find("hints") )
185           {
186             static_cast<void>( // ignore return
187               Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
188                                                                      ShaderHintsTable, ShaderHintsTableSize, hints)
189               );
190           }
191
192           Initialize(vertex, fragment, hints );
193         }
194       }
195       else
196       {
197         DALI_LOG_WARNING( "Shader program property should be a map\n" );
198       }
199       break;
200     }
201   }
202 }
203
204 void Shader::SetSceneGraphProperty( Property::Index index,
205                                     const PropertyMetadata& entry,
206                                     const Property::Value& value )
207 {
208   SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
209   OnPropertySet(index, value);
210 }
211
212 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
213 {
214   Property::Value value;
215
216   switch(index)
217   {
218     case Dali::Shader::Property::PROGRAM:
219     {
220       Dali::Property::Map map;
221       if( mShaderData )
222       {
223         map["vertex"] = Property::Value(mShaderData->GetVertexShader());
224         map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
225         map["hints"] = HintString(mShaderData->GetHints());
226       }
227       value = map;
228       break;
229     }
230   }
231
232   return value;
233 }
234
235 const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
236 {
237   return mSceneObject;
238 }
239
240 const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
241 {
242   return mSceneObject;
243 }
244
245 const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
246 {
247   DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
248   const SceneGraph::PropertyBase* property = NULL;
249
250   property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
251                                                           &Shader::FindAnimatableProperty,
252                                                           &Shader::FindCustomProperty,
253                                                           index );
254
255   if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
256   {
257     DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
258   }
259
260   return property;
261 }
262
263 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
264 {
265   PropertyMetadata* property = NULL;
266
267   if( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
268       ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
269   {
270     property = FindCustomProperty( index );
271   }
272   else if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
273   {
274     property = FindAnimatableProperty( index );
275   }
276
277   DALI_ASSERT_ALWAYS( property && "property index is invalid" );
278   return property->GetSceneGraphProperty();
279 }
280
281 int Shader::GetPropertyComponentIndex( Property::Index index ) const
282 {
283   return Property::INVALID_COMPONENT_INDEX;
284 }
285
286 Shader::Shader()
287   : mSceneObject( NULL ),
288     mShaderData( NULL )
289 {
290 }
291
292 void Shader::Initialize(
293   const std::string& vertexSource,
294   const std::string& fragmentSource,
295   Dali::Shader::Hint::Value hints )
296 {
297   EventThreadServices& eventThreadServices = GetEventThreadServices();
298   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
299   mSceneObject = new SceneGraph::Shader( hints );
300
301   // Add to update manager
302   AddShaderMessage( updateManager, *mSceneObject );
303
304   // Try to load a precompiled shader binary for the source pair:
305   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
306   ShaderFactory& shaderFactory = tls.GetShaderFactory();
307   size_t shaderHash;
308   mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
309
310   // Add shader program to scene-object using a message to the UpdateManager
311   SetShaderProgramMessage( updateManager, *mSceneObject, mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
312   eventThreadServices.RegisterObject( this );
313 }
314
315 Shader::~Shader()
316 {
317   if( EventThreadServices::IsCoreRunning() )
318   {
319     EventThreadServices& eventThreadServices = GetEventThreadServices();
320     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
321     RemoveShaderMessage( updateManager, *mSceneObject);
322
323     eventThreadServices.UnregisterObject( this );
324   }
325 }
326
327
328 } // namespace Internal
329 } // namespace Dali