2 * Copyright (c) 2018 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/update/manager/update-manager.h>
39 * |name |type |writable|animatable|constraint-input|enum for index-checking|
41 DALI_PROPERTY_TABLE_BEGIN
42 DALI_PROPERTY( "program", MAP, true, false, false, Dali::Shader::Property::PROGRAM )
43 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
45 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS, DEFAULT_ACTOR_PROPERTY_START_INDEX };
47 Dali::Scripting::StringEnum ShaderHintsTable[] =
48 { { "NONE", Dali::Shader::Hint::NONE},
49 { "OUTPUT_IS_TRANSPARENT", Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT},
50 { "MODIFIES_GEOMETRY", Dali::Shader::Hint::MODIFIES_GEOMETRY}
53 const uint32_t ShaderHintsTableSize = static_cast<uint32_t>( sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] ) );
57 return Dali::BaseHandle();
60 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
62 #define TOKEN_STRING(x) (#x)
64 void AppendString(std::string& to, const std::string& append)
73 Property::Value HintString(const Dali::Shader::Hint::Value& hints)
77 if(hints == Dali::Shader::Hint::NONE)
82 if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
84 AppendString(s, "OUTPUT_IS_TRANSPARENT");
87 if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
89 AppendString(s, "MODIFIES_GEOMETRY");
92 return Property::Value(s);
96 } // unnamed namespace
98 ShaderPtr Shader::New( const std::string& vertexShader,
99 const std::string& fragmentShader,
100 Dali::Shader::Hint::Value hints )
102 ShaderPtr shader( new Shader() );
103 shader->Initialize( vertexShader, fragmentShader, hints );
107 const SceneGraph::Shader* Shader::GetShaderSceneObject() const
112 SceneGraph::Shader* Shader::GetShaderSceneObject()
117 unsigned int Shader::GetDefaultPropertyCount() const
119 return SHADER_IMPL.GetDefaultPropertyCount();
122 void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
124 SHADER_IMPL.GetDefaultPropertyIndices( indices );
127 const char* Shader::GetDefaultPropertyName(Property::Index index) const
129 return SHADER_IMPL.GetDefaultPropertyName( index );
132 Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
134 return SHADER_IMPL.GetDefaultPropertyIndex( name );
137 bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
139 return SHADER_IMPL.IsDefaultPropertyWritable( index );
142 bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
144 return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
147 bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
149 return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
152 Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
154 return SHADER_IMPL.GetDefaultPropertyType( index );
157 void Shader::SetDefaultProperty( Property::Index index,
158 const Property::Value& propertyValue )
162 case Dali::Shader::Property::PROGRAM:
164 if( propertyValue.GetType() == Property::MAP )
166 Dali::Property::Map* map = propertyValue.GetMap();
170 std::string fragment;
171 Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
173 if( Property::Value* value = map->Find("vertex") )
175 vertex = value->Get<std::string>();
178 if( Property::Value* value = map->Find("fragment") )
180 fragment = value->Get<std::string>();
183 if( Property::Value* value = map->Find("hints") )
185 static_cast<void>( // ignore return
186 Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
187 ShaderHintsTable, ShaderHintsTableSize, hints)
191 Initialize(vertex, fragment, hints );
196 DALI_LOG_WARNING( "Shader program property should be a map\n" );
203 void Shader::SetSceneGraphProperty( Property::Index index,
204 const PropertyMetadata& entry,
205 const Property::Value& value )
207 SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
208 OnPropertySet(index, value);
211 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
213 Property::Value value;
217 case Dali::Shader::Property::PROGRAM:
219 Dali::Property::Map map;
222 map["vertex"] = Property::Value(mShaderData->GetVertexShader());
223 map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
224 map["hints"] = HintString(mShaderData->GetHints());
234 Property::Value Shader::GetDefaultPropertyCurrentValue( Property::Index index ) const
236 return GetDefaultProperty( index ); // Event-side only properties
239 const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
244 const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
249 const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
251 DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
252 const SceneGraph::PropertyBase* property = NULL;
254 property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
255 &Shader::FindAnimatableProperty,
256 &Shader::FindCustomProperty,
259 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
261 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
267 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
269 PropertyMetadata* property = NULL;
271 if( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
272 ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
274 property = FindCustomProperty( index );
276 else if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
278 property = FindAnimatableProperty( index );
281 DALI_ASSERT_ALWAYS( property && "property index is invalid" );
282 return property->GetSceneGraphProperty();
285 int Shader::GetPropertyComponentIndex( Property::Index index ) const
287 return Property::INVALID_COMPONENT_INDEX;
291 : mSceneObject( NULL ),
296 void Shader::Initialize(
297 const std::string& vertexSource,
298 const std::string& fragmentSource,
299 Dali::Shader::Hint::Value hints )
301 EventThreadServices& eventThreadServices = GetEventThreadServices();
302 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
303 mSceneObject = new SceneGraph::Shader( hints );
305 OwnerPointer< SceneGraph::Shader > transferOwnership( mSceneObject );
306 AddShaderMessage( updateManager, transferOwnership );
308 // Try to load a precompiled shader binary for the source pair:
309 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
310 ShaderFactory& shaderFactory = tls.GetShaderFactory();
312 mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
314 // Add shader program to scene-object using a message to the UpdateManager
315 SetShaderProgramMessage( updateManager, *mSceneObject, mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
316 eventThreadServices.RegisterObject( this );
321 if( EventThreadServices::IsCoreRunning() )
323 EventThreadServices& eventThreadServices = GetEventThreadServices();
324 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
325 RemoveShaderMessage( updateManager, *mSceneObject);
327 eventThreadServices.UnregisterObject( this );
332 } // namespace Internal