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