Merge branch 'devel/new_mesh' into 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
26 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
27 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
28 #include <dali/internal/event/common/thread-local-storage.h>
29 #include <dali/internal/event/effects/shader-factory.h>
30 #include <dali/internal/event/resources/resource-ticket.h>
31 #include <dali/internal/update/manager/update-manager.h>
32
33 namespace Dali
34 {
35 namespace Internal
36 {
37
38 namespace
39 {
40
41 /**
42  *            |name            |type             |writable|animatable|constraint-input|enum for index-checking|
43  */
44 DALI_PROPERTY_TABLE_BEGIN
45 DALI_PROPERTY( "program",       MAP,              true, false,  false,  Dali::Shader::Property::PROGRAM )
46 DALI_PROPERTY( "shader-hints",  UNSIGNED_INTEGER, true, false,  true,   Dali::Shader::Property::SHADER_HINTS )
47 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
48
49 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS };
50
51 BaseHandle Create()
52 {
53   return Dali::BaseHandle();
54 }
55
56 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
57
58 } // unnamed namespace
59
60 ShaderPtr Shader::New( const std::string& vertexShader,
61                        const std::string& fragmentShader,
62                        Dali::Shader::ShaderHints hints )
63 {
64   //TODO: MESH_REWORK
65   ShaderPtr shader( new Shader() );
66   shader->Initialize( vertexShader, fragmentShader, hints );
67   return shader;
68 }
69
70 const SceneGraph::Shader* Shader::GetShaderSceneObject() const
71 {
72   return mSceneObject;
73 }
74
75 unsigned int Shader::GetDefaultPropertyCount() const
76 {
77   return SHADER_IMPL.GetDefaultPropertyCount();
78 }
79
80 void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
81 {
82   SHADER_IMPL.GetDefaultPropertyIndices( indices );
83 }
84
85 const char* Shader::GetDefaultPropertyName(Property::Index index) const
86 {
87   return SHADER_IMPL.GetDefaultPropertyName( index );
88 }
89
90 Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
91 {
92   return SHADER_IMPL.GetDefaultPropertyIndex( name );
93 }
94
95 bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
96 {
97   return SHADER_IMPL.IsDefaultPropertyWritable( index );
98 }
99
100 bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
101 {
102   return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
103 }
104
105 bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
106 {
107   return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
108 }
109
110 Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
111 {
112   return SHADER_IMPL.GetDefaultPropertyType( index );
113 }
114
115 void Shader::SetDefaultProperty( Property::Index index,
116                                  const Property::Value& propertyValue )
117 {
118   switch(index)
119   {
120     case Dali::Shader::Property::PROGRAM:
121     {
122       // @todo MESH_REWORK Set program again?
123       DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
124       break;
125     }
126     case Dali::Shader::Property::SHADER_HINTS:
127     {
128       DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
129       break;
130     }
131   }
132 }
133
134 void Shader::SetSceneGraphProperty( Property::Index index,
135                                     const PropertyMetadata& entry,
136                                     const Property::Value& value )
137 {
138   SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
139   OnPropertySet(index, value);
140 }
141
142 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
143 {
144   Property::Value value;
145
146   switch(index)
147   {
148     case Dali::Shader::Property::PROGRAM:
149     {
150       DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
151       break;
152     }
153     case Dali::Shader::Property::SHADER_HINTS:
154     {
155       DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
156       break;
157     }
158   }
159
160   return value;
161 }
162
163 const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
164 {
165   return mSceneObject;
166 }
167
168 const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
169 {
170   return mSceneObject;
171 }
172
173 const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
174 {
175   DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
176   const SceneGraph::PropertyBase* property = NULL;
177
178   if( OnStage() )
179   {
180     property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
181                                                             &Shader::FindAnimatableProperty,
182                                                             &Shader::FindCustomProperty,
183                                                             index );
184
185     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
186     {
187       DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
188     }
189   }
190
191   return property;
192 }
193
194 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
195 {
196   const PropertyInputImpl* property = NULL;
197
198   if( OnStage() )
199   {
200     const SceneGraph::PropertyBase* baseProperty =
201       SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
202                                                    &Shader::FindAnimatableProperty,
203                                                    &Shader::FindCustomProperty,
204                                                    index );
205     property = static_cast<const PropertyInputImpl*>( baseProperty );
206
207     if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
208     {
209       if( index == Dali::Shader::Property::SHADER_HINTS )
210       {
211         // @todo MESH_REWORK - return the property
212       }
213       else
214       {
215         DALI_ASSERT_ALWAYS( 0 && "Property is not a valid constraint input" );
216       }
217     }
218   }
219
220   return property;
221 }
222
223 int Shader::GetPropertyComponentIndex( Property::Index index ) const
224 {
225   return Property::INVALID_COMPONENT_INDEX;
226 }
227
228 bool Shader::OnStage() const
229 {
230   return mOnStage;
231 }
232
233 void Shader::Connect()
234 {
235   mOnStage = true;
236 }
237
238 void Shader::Disconnect()
239 {
240   mOnStage = false;
241 }
242
243 Shader::Shader()
244 : mSceneObject( NULL ),
245   mOnStage( false )
246 {
247 }
248
249 void Shader::Initialize(
250   const std::string& vertexSource,
251   const std::string& fragmentSource,
252   Dali::Shader::ShaderHints hints )
253 {
254   EventThreadServices& eventThreadServices = GetEventThreadServices();
255   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
256
257   // @todo MESH_REWORK - Pass hints directly to a new scene graph shader
258   int effectHint = Dali::ShaderEffect::HINT_NONE;
259   if( hints & Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT )
260   {
261     effectHint |= Dali::ShaderEffect::HINT_BLENDING;
262   }
263
264   if( hints & Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST )
265   {
266     effectHint |= Dali::ShaderEffect::HINT_DEPTH_BUFFER;
267   }
268
269   if( (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) == 0x0 )
270   {
271     effectHint |= Dali::ShaderEffect::HINT_DOESNT_MODIFY_GEOMETRY;
272   }
273   Dali::ShaderEffect::GeometryHints shaderEffectHint = static_cast<Dali::ShaderEffect::GeometryHints>( effectHint );
274
275   mSceneObject = new SceneGraph::Shader(shaderEffectHint);
276
277   // Add to update manager
278   AddShaderMessage( updateManager, *mSceneObject );
279
280   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
281   ShaderFactory& shaderFactory = tls.GetShaderFactory();
282   size_t shaderHash;
283
284   mTicket = ResourceTicketPtr( shaderFactory.Load(vertexSource, fragmentSource, shaderHash) );
285
286   // Add shader program to scene-object using a message to the UpdateManager
287   SetShaderProgramMessage( updateManager, *mSceneObject, mTicket->GetId(), shaderHash, false );
288
289   eventThreadServices.RegisterObject( this );
290 }
291
292 Shader::~Shader()
293 {
294   if( EventThreadServices::IsCoreRunning() )
295   {
296     EventThreadServices& eventThreadServices = GetEventThreadServices();
297     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
298     RemoveShaderMessage( updateManager, *mSceneObject);
299
300     eventThreadServices.UnregisterObject( this );
301   }
302 }
303
304
305 } // namespace Internal
306 } // namespace Dali