Merge "Stop setting crazy Z value when SetSize(width,height) is used" into devel...
[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 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   property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
179                                                           &Shader::FindAnimatableProperty,
180                                                           &Shader::FindCustomProperty,
181                                                           index );
182
183   if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
184   {
185     DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
186   }
187
188   return property;
189 }
190
191 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
192 {
193   const PropertyInputImpl* property = NULL;
194
195   const SceneGraph::PropertyBase* baseProperty =
196     SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
197                                                  &Shader::FindAnimatableProperty,
198                                                  &Shader::FindCustomProperty,
199                                                  index );
200   property = static_cast<const PropertyInputImpl*>( baseProperty );
201
202   if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
203   {
204     if( index == Dali::Shader::Property::SHADER_HINTS )
205     {
206       // @todo MESH_REWORK - return the property
207     }
208     else
209     {
210       DALI_ASSERT_ALWAYS( 0 && "Property is not a valid constraint input" );
211     }
212   }
213
214   return property;
215 }
216
217 int Shader::GetPropertyComponentIndex( Property::Index index ) const
218 {
219   return Property::INVALID_COMPONENT_INDEX;
220 }
221
222 Shader::Shader()
223 : mSceneObject( NULL )
224 {
225 }
226
227 void Shader::Initialize(
228   const std::string& vertexSource,
229   const std::string& fragmentSource,
230   Dali::Shader::ShaderHints hints )
231 {
232   EventThreadServices& eventThreadServices = GetEventThreadServices();
233   SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
234
235   // @todo MESH_REWORK - Pass hints directly to a new scene graph shader
236   int effectHint = Dali::ShaderEffect::HINT_NONE;
237   if( hints & Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT )
238   {
239     effectHint |= Dali::ShaderEffect::HINT_BLENDING;
240   }
241
242   if( hints & Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST )
243   {
244     effectHint |= Dali::ShaderEffect::HINT_DEPTH_BUFFER;
245   }
246
247   if( (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) == 0x0 )
248   {
249     effectHint |= Dali::ShaderEffect::HINT_DOESNT_MODIFY_GEOMETRY;
250   }
251   Dali::ShaderEffect::GeometryHints shaderEffectHint = static_cast<Dali::ShaderEffect::GeometryHints>( effectHint );
252
253   mSceneObject = new SceneGraph::Shader( shaderEffectHint );
254
255   // Add to update manager
256   AddShaderMessage( updateManager, *mSceneObject );
257
258   // Try to load a precompiled shader binary for the source pair:
259   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
260   ShaderFactory& shaderFactory = tls.GetShaderFactory();
261   size_t shaderHash;
262   Internal::ShaderDataPtr shaderData = shaderFactory.Load( vertexSource, fragmentSource, shaderHash );
263
264   // Add shader program to scene-object using a message to the UpdateManager
265   SetShaderProgramMessage( updateManager, *mSceneObject, shaderData, (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) != 0x0 );
266   eventThreadServices.RegisterObject( this );
267 }
268
269 Shader::~Shader()
270 {
271   if( EventThreadServices::IsCoreRunning() )
272   {
273     EventThreadServices& eventThreadServices = GetEventThreadServices();
274     SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
275     RemoveShaderMessage( updateManager, *mSceneObject);
276
277     eventThreadServices.UnregisterObject( this );
278   }
279 }
280
281
282 } // namespace Internal
283 } // namespace Dali