Fixed Material::RemoveSampler not calling SceneGraph::RemoveSamplerMessage to match...
[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",  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 SceneGraph::Shader* Shader::GetShaderSceneObject()
76 {
77   return mSceneObject;
78 }
79
80 unsigned int Shader::GetDefaultPropertyCount() const
81 {
82   return SHADER_IMPL.GetDefaultPropertyCount();
83 }
84
85 void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
86 {
87   SHADER_IMPL.GetDefaultPropertyIndices( indices );
88 }
89
90 const char* Shader::GetDefaultPropertyName(Property::Index index) const
91 {
92   return SHADER_IMPL.GetDefaultPropertyName( index );
93 }
94
95 Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
96 {
97   return SHADER_IMPL.GetDefaultPropertyIndex( name );
98 }
99
100 bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
101 {
102   return SHADER_IMPL.IsDefaultPropertyWritable( index );
103 }
104
105 bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
106 {
107   return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
108 }
109
110 bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
111 {
112   return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
113 }
114
115 Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
116 {
117   return SHADER_IMPL.GetDefaultPropertyType( index );
118 }
119
120 void Shader::SetDefaultProperty( Property::Index index,
121                                  const Property::Value& propertyValue )
122 {
123   switch(index)
124   {
125     case Dali::Shader::Property::PROGRAM:
126     {
127       // @todo MESH_REWORK Set program again?
128       DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
129       break;
130     }
131     case Dali::Shader::Property::SHADER_HINTS:
132     {
133       DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
134       break;
135     }
136   }
137 }
138
139 void Shader::SetSceneGraphProperty( Property::Index index,
140                                     const PropertyMetadata& entry,
141                                     const Property::Value& value )
142 {
143   SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
144   OnPropertySet(index, value);
145 }
146
147 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
148 {
149   Property::Value value;
150
151   switch(index)
152   {
153     case Dali::Shader::Property::PROGRAM:
154     {
155       DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
156       break;
157     }
158     case Dali::Shader::Property::SHADER_HINTS:
159     {
160       DALI_ASSERT_ALWAYS( 0 && "MESH_REWORK" );
161       break;
162     }
163   }
164
165   return value;
166 }
167
168 const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
169 {
170   return mSceneObject;
171 }
172
173 const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
174 {
175   return mSceneObject;
176 }
177
178 const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
179 {
180   DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
181   const SceneGraph::PropertyBase* property = NULL;
182
183   property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
184                                                           &Shader::FindAnimatableProperty,
185                                                           &Shader::FindCustomProperty,
186                                                           index );
187
188   if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
189   {
190     DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
191   }
192
193   return property;
194 }
195
196 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
197 {
198   const PropertyInputImpl* property = NULL;
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   return property;
220 }
221
222 int Shader::GetPropertyComponentIndex( Property::Index index ) const
223 {
224   return Property::INVALID_COMPONENT_INDEX;
225 }
226
227 Shader::Shader()
228 : mSceneObject( NULL )
229 {
230 }
231
232 void Shader::Initialize(
233   const std::string& vertexSource,
234   const std::string& fragmentSource,
235   Dali::Shader::ShaderHints hints )
236 {
237   EventThreadServices& eventThreadServices = GetEventThreadServices();
238   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
239
240   // @todo MESH_REWORK - Pass hints directly to a new scene graph shader
241   int effectHint = Dali::ShaderEffect::HINT_NONE;
242   if( hints & Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT )
243   {
244     effectHint |= Dali::ShaderEffect::HINT_BLENDING;
245   }
246
247   if( hints & Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST )
248   {
249     effectHint |= Dali::ShaderEffect::HINT_DEPTH_BUFFER;
250   }
251
252   if( (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) == 0x0 )
253   {
254     effectHint |= Dali::ShaderEffect::HINT_DOESNT_MODIFY_GEOMETRY;
255   }
256   Dali::ShaderEffect::GeometryHints shaderEffectHint = static_cast<Dali::ShaderEffect::GeometryHints>( effectHint );
257
258   mSceneObject = new SceneGraph::Shader( shaderEffectHint );
259
260   // Add to update manager
261   AddShaderMessage( updateManager, *mSceneObject );
262
263   // Try to load a precompiled shader binary for the source pair:
264   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
265   ShaderFactory& shaderFactory = tls.GetShaderFactory();
266   size_t shaderHash;
267   Internal::ShaderDataPtr shaderData = shaderFactory.Load( vertexSource, fragmentSource, shaderHash );
268
269   // Add shader program to scene-object using a message to the UpdateManager
270   SetShaderProgramMessage( updateManager, *mSceneObject, shaderData, (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) != 0x0 );
271   eventThreadServices.RegisterObject( this );
272 }
273
274 Shader::~Shader()
275 {
276   if( EventThreadServices::IsCoreRunning() )
277   {
278     EventThreadServices& eventThreadServices = GetEventThreadServices();
279     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
280     RemoveShaderMessage( updateManager, *mSceneObject);
281
282     eventThreadServices.UnregisterObject( this );
283   }
284 }
285
286
287 } // namespace Internal
288 } // namespace Dali