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, ShaderDefaultProperties )
45 Dali::Scripting::StringEnum ShaderHintsTable[] =
46 { { "NONE", Dali::Shader::Hint::NONE},
47 { "OUTPUT_IS_TRANSPARENT", Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT},
48 { "MODIFIES_GEOMETRY", Dali::Shader::Hint::MODIFIES_GEOMETRY}
51 const uint32_t ShaderHintsTableSize = static_cast<uint32_t>( sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] ) );
55 return Dali::BaseHandle();
58 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create, ShaderDefaultProperties );
60 #define TOKEN_STRING(x) (#x)
62 void AppendString(std::string& to, const std::string& append)
71 Property::Value HintString(const Dali::Shader::Hint::Value& hints)
75 if(hints == Dali::Shader::Hint::NONE)
80 if(hints & Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT)
82 AppendString(s, "OUTPUT_IS_TRANSPARENT");
85 if(hints & Dali::Shader::Hint::MODIFIES_GEOMETRY)
87 AppendString(s, "MODIFIES_GEOMETRY");
90 return Property::Value(s);
94 } // unnamed namespace
96 ShaderPtr Shader::New( const std::string& vertexShader,
97 const std::string& fragmentShader,
98 Dali::Shader::Hint::Value hints )
100 ShaderPtr shader( new Shader() );
101 shader->Initialize( vertexShader, fragmentShader, hints );
105 const SceneGraph::Shader* Shader::GetShaderSceneObject() const
110 SceneGraph::Shader* Shader::GetShaderSceneObject()
115 void Shader::SetDefaultProperty( Property::Index index,
116 const Property::Value& propertyValue )
120 case Dali::Shader::Property::PROGRAM:
122 if( propertyValue.GetType() == Property::MAP )
124 Dali::Property::Map* map = propertyValue.GetMap();
128 std::string fragment;
129 Dali::Shader::Hint::Value hints(Dali::Shader::Hint::NONE);
131 if( Property::Value* value = map->Find("vertex") )
133 vertex = value->Get<std::string>();
136 if( Property::Value* value = map->Find("fragment") )
138 fragment = value->Get<std::string>();
141 if( Property::Value* value = map->Find("hints") )
143 static_cast<void>( // ignore return
144 Scripting::GetEnumeration< Dali::Shader::Hint::Value >(value->Get<std::string>().c_str(),
145 ShaderHintsTable, ShaderHintsTableSize, hints)
149 Initialize(vertex, fragment, hints );
154 DALI_LOG_WARNING( "Shader program property should be a map\n" );
161 void Shader::SetSceneGraphProperty( Property::Index index,
162 const PropertyMetadata& entry,
163 const Property::Value& value )
165 ObjectImplHelper::SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
166 OnPropertySet(index, value);
169 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
171 Property::Value value;
175 case Dali::Shader::Property::PROGRAM:
177 Dali::Property::Map map;
180 map["vertex"] = Property::Value(mShaderData->GetVertexShader());
181 map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
182 map["hints"] = HintString(mShaderData->GetHints());
192 Property::Value Shader::GetDefaultPropertyCurrentValue( Property::Index index ) const
194 return GetDefaultProperty( index ); // Event-side only properties
197 const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
202 const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
207 const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
209 DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
210 const SceneGraph::PropertyBase* property = NULL;
212 property = ObjectImplHelper::GetRegisteredSceneGraphProperty( this,
213 &Shader::FindAnimatableProperty,
214 &Shader::FindCustomProperty,
217 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
219 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
225 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
227 PropertyMetadata* property = NULL;
229 if( ( index >= CHILD_PROPERTY_REGISTRATION_START_INDEX ) && // Child properties are also stored as custom properties
230 ( index <= PROPERTY_CUSTOM_MAX_INDEX ) )
232 property = FindCustomProperty( index );
234 else if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
236 property = FindAnimatableProperty( index );
239 DALI_ASSERT_ALWAYS( property && "property index is invalid" );
240 return property->GetSceneGraphProperty();
243 int Shader::GetPropertyComponentIndex( Property::Index index ) const
245 return Property::INVALID_COMPONENT_INDEX;
249 : mSceneObject( NULL ),
254 void Shader::Initialize(
255 const std::string& vertexSource,
256 const std::string& fragmentSource,
257 Dali::Shader::Hint::Value hints )
259 EventThreadServices& eventThreadServices = GetEventThreadServices();
260 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
261 mSceneObject = new SceneGraph::Shader( hints );
263 OwnerPointer< SceneGraph::Shader > transferOwnership( mSceneObject );
264 AddShaderMessage( updateManager, transferOwnership );
266 // Try to load a precompiled shader binary for the source pair:
267 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
268 ShaderFactory& shaderFactory = tls.GetShaderFactory();
270 mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
272 // Add shader program to scene-object using a message to the UpdateManager
273 SetShaderProgramMessage( updateManager, *mSceneObject, mShaderData, (hints & Dali::Shader::Hint::MODIFIES_GEOMETRY) != 0x0 );
274 eventThreadServices.RegisterObject( this );
279 if( EventThreadServices::IsCoreRunning() )
281 EventThreadServices& eventThreadServices = GetEventThreadServices();
282 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
283 RemoveShaderMessage( updateManager, *mSceneObject);
285 eventThreadServices.UnregisterObject( this );
290 } // namespace Internal