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