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