2 * Copyright (c) 2015 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/rendering/shader.h> // Dali::Shader
25 #include <dali/devel-api/scripting/scripting.h>
27 #include <dali/internal/event/common/object-impl-helper.h> // Dali::Internal::ObjectHelper
28 #include <dali/internal/event/common/property-helper.h> // DALI_PROPERTY_TABLE_BEGIN, DALI_PROPERTY, DALI_PROPERTY_TABLE_END
29 #include <dali/internal/event/common/thread-local-storage.h>
30 #include <dali/internal/event/effects/shader-factory.h>
31 #include <dali/internal/event/resources/resource-ticket.h>
32 #include <dali/internal/update/manager/update-manager.h>
43 * |name |type |writable|animatable|constraint-input|enum for index-checking|
45 DALI_PROPERTY_TABLE_BEGIN
46 DALI_PROPERTY( "program", MAP, true, false, false, Dali::Shader::Property::PROGRAM )
47 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
49 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> SHADER_IMPL = { DEFAULT_PROPERTY_DETAILS };
51 Dali::Scripting::StringEnum ShaderHintsTable[] =
52 { { "HINT_NONE", Dali::Shader::HINT_NONE},
53 { "HINT_OUTPUT_IS_TRANSPARENT", Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT},
54 { "HINT_MODIFIES_GEOMETRY", Dali::Shader::HINT_MODIFIES_GEOMETRY}
57 const unsigned int ShaderHintsTableSize = sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] );
61 return Dali::BaseHandle();
64 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
66 #define TOKEN_STRING(x) (#x)
68 void AppendString(std::string& to, const std::string& append)
77 Property::Value HintString(const Dali::Shader::ShaderHints& hints)
81 if(hints == Dali::Shader::HINT_NONE)
86 if(hints & Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT)
88 AppendString(s, "HINT_OUTPUT_IS_TRANSPARENT");
91 if(hints & Dali::Shader::HINT_MODIFIES_GEOMETRY)
93 AppendString(s, "HINT_MODIFIES_GEOMETRY");
96 return Property::Value(s);
100 } // unnamed namespace
102 ShaderPtr Shader::New( const std::string& vertexShader,
103 const std::string& fragmentShader,
104 Dali::Shader::ShaderHints hints )
106 ShaderPtr shader( new Shader() );
107 shader->Initialize( vertexShader, fragmentShader, hints );
111 const SceneGraph::Shader* Shader::GetShaderSceneObject() const
116 SceneGraph::Shader* Shader::GetShaderSceneObject()
121 unsigned int Shader::GetDefaultPropertyCount() const
123 return SHADER_IMPL.GetDefaultPropertyCount();
126 void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
128 SHADER_IMPL.GetDefaultPropertyIndices( indices );
131 const char* Shader::GetDefaultPropertyName(Property::Index index) const
133 return SHADER_IMPL.GetDefaultPropertyName( index );
136 Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
138 return SHADER_IMPL.GetDefaultPropertyIndex( name );
141 bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
143 return SHADER_IMPL.IsDefaultPropertyWritable( index );
146 bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
148 return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
151 bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
153 return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
156 Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
158 return SHADER_IMPL.GetDefaultPropertyType( index );
161 void Shader::SetDefaultProperty( Property::Index index,
162 const Property::Value& propertyValue )
166 case Dali::Shader::Property::PROGRAM:
168 if( propertyValue.GetType() == Property::MAP )
170 Dali::Property::Map* map = propertyValue.GetMap();
172 std::string fragment;
173 Dali::Shader::ShaderHints 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::ShaderHints >(value->Get<std::string>().c_str(),
189 ShaderHintsTable, ShaderHintsTableSize, hints)
193 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 >= PROPERTY_CUSTOM_START_INDEX )
269 property = FindCustomProperty( index );
273 property = FindAnimatableProperty( index );
276 DALI_ASSERT_ALWAYS( property && "property index is invalid" );
277 return property->GetSceneGraphProperty();
280 int Shader::GetPropertyComponentIndex( Property::Index index ) const
282 return Property::INVALID_COMPONENT_INDEX;
286 : mSceneObject( NULL ),
291 void Shader::Initialize(
292 const std::string& vertexSource,
293 const std::string& fragmentSource,
294 Dali::Shader::ShaderHints hints )
296 EventThreadServices& eventThreadServices = GetEventThreadServices();
297 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
298 mSceneObject = new SceneGraph::Shader( hints );
300 // Add to update manager
301 AddShaderMessage( updateManager, *mSceneObject );
303 // Try to load a precompiled shader binary for the source pair:
304 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
305 ShaderFactory& shaderFactory = tls.GetShaderFactory();
307 mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
309 // Add shader program to scene-object using a message to the UpdateManager
310 SetShaderProgramMessage( updateManager, *mSceneObject, mShaderData, (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) != 0x0 );
311 eventThreadServices.RegisterObject( this );
316 if( EventThreadServices::IsCoreRunning() )
318 EventThreadServices& eventThreadServices = GetEventThreadServices();
319 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
320 RemoveShaderMessage( updateManager, *mSceneObject);
322 eventThreadServices.UnregisterObject( this );
327 } // namespace Internal