2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/rendering/shader-impl.h> // Dali::Internal::Shader
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/devel-api/scripting/scripting.h>
24 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
25 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/event/effects/shader-factory.h>
28 #include <dali/internal/event/resources/resource-ticket.h>
29 #include <dali/internal/update/manager/update-manager.h>
40 * |name |type |writable|animatable|constraint-input|enum for index-checking|
42 DALI_PROPERTY_TABLE_BEGIN
43 DALI_PROPERTY( "program", MAP, true, false, false, Dali::Shader::Property::PROGRAM )
44 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
46 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS };
48 Dali::Scripting::StringEnum ShaderHintsTable[] =
49 { { "NONE", Dali::Shader::Hint::NONE},
50 { "OUTPUT_IS_TRANSPARENT", Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT},
51 { "MODIFIES_GEOMETRY", Dali::Shader::Hint::MODIFIES_GEOMETRY}
54 const unsigned int ShaderHintsTableSize = sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] );
58 return Dali::BaseHandle();
61 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
63 #define TOKEN_STRING(x) (#x)
65 void AppendString(std::string& to, const std::string& append)
74 Property::Value HintString(const Dali::Shader::Hint::Value& hints)
78 if(hints == Dali::Shader::Hint::NONE)
83 if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
85 AppendString(s, "OUTPUT_IS_TRANSPARENT");
88 if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
90 AppendString(s, "MODIFIES_GEOMETRY");
93 return Property::Value(s);
97 } // unnamed namespace
99 ShaderPtr Shader::New( const std::string& vertexShader,
100 const std::string& fragmentShader,
101 Dali::Shader::Hint::Value hints )
103 ShaderPtr shader( new Shader() );
104 shader->Initialize( vertexShader, fragmentShader, hints );
108 const SceneGraph::Shader* Shader::GetShaderSceneObject() const
113 SceneGraph::Shader* Shader::GetShaderSceneObject()
118 unsigned int Shader::GetDefaultPropertyCount() const
120 return SHADER_IMPL.GetDefaultPropertyCount();
123 void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
125 SHADER_IMPL.GetDefaultPropertyIndices( indices );
128 const char* Shader::GetDefaultPropertyName(Property::Index index) const
130 return SHADER_IMPL.GetDefaultPropertyName( index );
133 Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
135 return SHADER_IMPL.GetDefaultPropertyIndex( name );
138 bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
140 return SHADER_IMPL.IsDefaultPropertyWritable( index );
143 bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
145 return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
148 bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
150 return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
153 Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
155 return SHADER_IMPL.GetDefaultPropertyType( index );
158 void Shader::SetDefaultProperty( Property::Index index,
159 const Property::Value& propertyValue )
163 case Dali::Shader::Property::PROGRAM:
165 if( propertyValue.GetType() == Property::MAP )
167 Dali::Property::Map* map = propertyValue.GetMap();
171 std::string fragment;
172 Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
174 if( Property::Value* value = map->Find("vertex") )
176 vertex = value->Get<std::string>();
179 if( Property::Value* value = map->Find("fragment") )
181 fragment = value->Get<std::string>();
184 if( Property::Value* value = map->Find("hints") )
186 static_cast<void>( // ignore return
187 Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
188 ShaderHintsTable, ShaderHintsTableSize, hints)
192 Initialize(vertex, fragment, hints );
197 DALI_LOG_WARNING( "Shader program property should be a map\n" );
204 void Shader::SetSceneGraphProperty( Property::Index index,
205 const PropertyMetadata& entry,
206 const Property::Value& value )
208 SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
209 OnPropertySet(index, value);
212 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
214 Property::Value value;
218 case Dali::Shader::Property::PROGRAM:
220 Dali::Property::Map map;
223 map["vertex"] = Property::Value(mShaderData->GetVertexShader());
224 map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
225 map["hints"] = HintString(mShaderData->GetHints());
235 const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
240 const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
245 const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
247 DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
248 const SceneGraph::PropertyBase* property = NULL;
250 property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
251 &Shader::FindAnimatableProperty,
252 &Shader::FindCustomProperty,
255 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
257 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
263 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
265 PropertyMetadata* property = NULL;
267 if( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
268 ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
270 property = FindCustomProperty( index );
272 else if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
274 property = FindAnimatableProperty( index );
277 DALI_ASSERT_ALWAYS( property && "property index is invalid" );
278 return property->GetSceneGraphProperty();
281 int Shader::GetPropertyComponentIndex( Property::Index index ) const
283 return Property::INVALID_COMPONENT_INDEX;
287 : mSceneObject( NULL ),
292 void Shader::Initialize(
293 const std::string& vertexSource,
294 const std::string& fragmentSource,
295 Dali::Shader::Hint::Value hints )
297 EventThreadServices& eventThreadServices = GetEventThreadServices();
298 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
299 mSceneObject = new SceneGraph::Shader( hints );
301 // Add to update manager
302 AddShaderMessage( updateManager, *mSceneObject );
304 // Try to load a precompiled shader binary for the source pair:
305 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
306 ShaderFactory& shaderFactory = tls.GetShaderFactory();
308 mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
310 // Add shader program to scene-object using a message to the UpdateManager
311 SetShaderProgramMessage( updateManager, *mSceneObject, mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
312 eventThreadServices.RegisterObject( this );
317 if( EventThreadServices::IsCoreRunning() )
319 EventThreadServices& eventThreadServices = GetEventThreadServices();
320 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
321 RemoveShaderMessage( updateManager, *mSceneObject);
323 eventThreadServices.UnregisterObject( this );
328 } // namespace Internal