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