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/public-api/shader-effects/shader-effect.h> // Dali::ShaderEffect::GeometryHints // TODO: MESH_REWORK REMOVE
24 #include <dali/devel-api/scripting/scripting.h>
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 #include <dali/internal/event/common/thread-local-storage.h>
28 #include <dali/internal/event/effects/shader-factory.h>
29 #include <dali/internal/event/resources/resource-ticket.h>
30 #include <dali/internal/update/manager/update-manager.h>
41 * |name |type |writable|animatable|constraint-input|enum for index-checking|
43 DALI_PROPERTY_TABLE_BEGIN
44 DALI_PROPERTY( "program", MAP, true, false, false, Dali::Shader::Property::PROGRAM )
45 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
47 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS };
49 Dali::Scripting::StringEnum ShaderHintsTable[] =
50 { { "NONE", Dali::Shader::Hint::NONE},
51 { "OUTPUT_IS_TRANSPARENT", Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT},
52 { "MODIFIES_GEOMETRY", Dali::Shader::Hint::MODIFIES_GEOMETRY}
55 const unsigned int ShaderHintsTableSize = sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] );
59 return Dali::BaseHandle();
62 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
64 #define TOKEN_STRING(x) (#x)
66 void AppendString(std::string& to, const std::string& append)
75 Property::Value HintString(const Dali::Shader::Hint::Value& hints)
79 if(hints == Dali::Shader::Hint::NONE)
84 if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
86 AppendString(s, "OUTPUT_IS_TRANSPARENT");
89 if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
91 AppendString(s, "MODIFIES_GEOMETRY");
94 return Property::Value(s);
98 } // unnamed namespace
100 ShaderPtr Shader::New( const std::string& vertexShader,
101 const std::string& fragmentShader,
102 Dali::Shader::Hint::Value hints )
104 ShaderPtr shader( new Shader() );
105 shader->Initialize( vertexShader, fragmentShader, hints );
109 const SceneGraph::Shader* Shader::GetShaderSceneObject() const
114 SceneGraph::Shader* Shader::GetShaderSceneObject()
119 unsigned int Shader::GetDefaultPropertyCount() const
121 return SHADER_IMPL.GetDefaultPropertyCount();
124 void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
126 SHADER_IMPL.GetDefaultPropertyIndices( indices );
129 const char* Shader::GetDefaultPropertyName(Property::Index index) const
131 return SHADER_IMPL.GetDefaultPropertyName( index );
134 Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
136 return SHADER_IMPL.GetDefaultPropertyIndex( name );
139 bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
141 return SHADER_IMPL.IsDefaultPropertyWritable( index );
144 bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
146 return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
149 bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
151 return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
154 Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
156 return SHADER_IMPL.GetDefaultPropertyType( index );
159 void Shader::SetDefaultProperty( Property::Index index,
160 const Property::Value& propertyValue )
164 case Dali::Shader::Property::PROGRAM:
166 if( propertyValue.GetType() == Property::MAP )
168 Dali::Property::Map* map = propertyValue.GetMap();
172 std::string fragment;
173 Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
175 if( Property::Value* value = map->Find("vertex") )
177 vertex = value->Get<std::string>();
180 if( Property::Value* value = map->Find("fragment") )
182 fragment = value->Get<std::string>();
185 if( Property::Value* value = map->Find("hints") )
187 static_cast<void>( // ignore return
188 Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
189 ShaderHintsTable, ShaderHintsTableSize, hints)
193 Initialize(vertex, fragment, hints );
198 DALI_LOG_WARNING( "Shader program property should be a map\n" );
205 void Shader::SetSceneGraphProperty( Property::Index index,
206 const PropertyMetadata& entry,
207 const Property::Value& value )
209 SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
210 OnPropertySet(index, value);
213 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
215 Property::Value value;
219 case Dali::Shader::Property::PROGRAM:
221 Dali::Property::Map map;
224 map["vertex"] = Property::Value(mShaderData->GetVertexShader());
225 map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
226 map["hints"] = HintString(mShaderData->GetHints());
236 const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
241 const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
246 const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
248 DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
249 const SceneGraph::PropertyBase* property = NULL;
251 property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
252 &Shader::FindAnimatableProperty,
253 &Shader::FindCustomProperty,
256 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
258 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
264 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
266 PropertyMetadata* property = NULL;
268 if( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
269 ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
271 property = FindCustomProperty( index );
273 else if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
275 property = FindAnimatableProperty( index );
278 DALI_ASSERT_ALWAYS( property && "property index is invalid" );
279 return property->GetSceneGraphProperty();
282 int Shader::GetPropertyComponentIndex( Property::Index index ) const
284 return Property::INVALID_COMPONENT_INDEX;
288 : mSceneObject( NULL ),
293 void Shader::Initialize(
294 const std::string& vertexSource,
295 const std::string& fragmentSource,
296 Dali::Shader::Hint::Value hints )
298 EventThreadServices& eventThreadServices = GetEventThreadServices();
299 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
300 mSceneObject = new SceneGraph::Shader( hints );
302 // Add to update manager
303 AddShaderMessage( updateManager, *mSceneObject );
305 // Try to load a precompiled shader binary for the source pair:
306 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
307 ShaderFactory& shaderFactory = tls.GetShaderFactory();
309 mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
311 // Add shader program to scene-object using a message to the UpdateManager
312 SetShaderProgramMessage( updateManager, *mSceneObject, mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
313 eventThreadServices.RegisterObject( this );
318 if( EventThreadServices::IsCoreRunning() )
320 EventThreadServices& eventThreadServices = GetEventThreadServices();
321 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
322 RemoveShaderMessage( updateManager, *mSceneObject);
324 eventThreadServices.UnregisterObject( this );
329 } // namespace Internal