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_REQUIRES_SELF_DEPTH_TEST", Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST},
54 { "HINT_OUTPUT_IS_TRANSPARENT", Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT},
55 { "HINT_OUTPUT_IS_OPAQUE", Dali::Shader::HINT_OUTPUT_IS_OPAQUE},
56 { "HINT_MODIFIES_GEOMETRY", Dali::Shader::HINT_MODIFIES_GEOMETRY}
59 const unsigned int ShaderHintsTableSize = sizeof( ShaderHintsTable ) / sizeof( ShaderHintsTable[0] );
63 return Dali::BaseHandle();
66 TypeRegistration mType( typeid( Dali::Shader ), typeid( Dali::Handle ), Create );
68 #define TOKEN_STRING(x) (#x)
70 void AppendString(std::string& to, const std::string& append)
79 Property::Value HintString(const Dali::Shader::ShaderHints& hints)
83 if(hints == Dali::Shader::HINT_NONE)
88 if(hints & Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST)
90 AppendString(s, "HINT_REQUIRES_SELF_DEPTH_TEST");
93 if(hints & Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT)
95 AppendString(s, "HINT_OUTPUT_IS_TRANSPARENT");
98 if(hints & Dali::Shader::HINT_OUTPUT_IS_OPAQUE)
100 AppendString(s, "HINT_OUTPUT_IS_OPAQUE");
103 if(hints & Dali::Shader::HINT_MODIFIES_GEOMETRY)
105 AppendString(s, "HINT_MODIFIES_GEOMETRY");
108 return Property::Value(s);
112 } // unnamed namespace
114 ShaderPtr Shader::New( const std::string& vertexShader,
115 const std::string& fragmentShader,
116 Dali::Shader::ShaderHints hints )
119 ShaderPtr shader( new Shader() );
120 shader->Initialize( vertexShader, fragmentShader, hints );
124 const SceneGraph::Shader* Shader::GetShaderSceneObject() const
129 SceneGraph::Shader* Shader::GetShaderSceneObject()
134 unsigned int Shader::GetDefaultPropertyCount() const
136 return SHADER_IMPL.GetDefaultPropertyCount();
139 void Shader::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
141 SHADER_IMPL.GetDefaultPropertyIndices( indices );
144 const char* Shader::GetDefaultPropertyName(Property::Index index) const
146 return SHADER_IMPL.GetDefaultPropertyName( index );
149 Property::Index Shader::GetDefaultPropertyIndex( const std::string& name ) const
151 return SHADER_IMPL.GetDefaultPropertyIndex( name );
154 bool Shader::IsDefaultPropertyWritable( Property::Index index ) const
156 return SHADER_IMPL.IsDefaultPropertyWritable( index );
159 bool Shader::IsDefaultPropertyAnimatable( Property::Index index ) const
161 return SHADER_IMPL.IsDefaultPropertyAnimatable( index );
164 bool Shader::IsDefaultPropertyAConstraintInput( Property::Index index ) const
166 return SHADER_IMPL.IsDefaultPropertyAConstraintInput( index );
169 Property::Type Shader::GetDefaultPropertyType( Property::Index index ) const
171 return SHADER_IMPL.GetDefaultPropertyType( index );
174 void Shader::SetDefaultProperty( Property::Index index,
175 const Property::Value& propertyValue )
179 case Dali::Shader::Property::PROGRAM:
181 if( propertyValue.GetType() == Property::MAP )
183 Dali::Property::Map* map = propertyValue.GetMap();
185 std::string fragment;
186 Dali::Shader::ShaderHints hints(Dali::Shader::HINT_NONE);
188 if( Property::Value* value = map->Find("vertex") )
190 vertex = value->Get<std::string>();
193 if( Property::Value* value = map->Find("fragment") )
195 fragment = value->Get<std::string>();
198 if( Property::Value* value = map->Find("hints") )
200 static_cast<void>( // ignore return
201 Scripting::GetEnumeration< Dali::Shader::ShaderHints >(value->Get<std::string>().c_str(),
202 ShaderHintsTable, ShaderHintsTableSize, hints)
206 Initialize(vertex, fragment, hints );
210 DALI_LOG_WARNING( "Shader program property should be a map\n" );
217 void Shader::SetSceneGraphProperty( Property::Index index,
218 const PropertyMetadata& entry,
219 const Property::Value& value )
221 SHADER_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
222 OnPropertySet(index, value);
225 Property::Value Shader::GetDefaultProperty( Property::Index index ) const
227 Property::Value value;
231 case Dali::Shader::Property::PROGRAM:
233 Dali::Property::Map map;
236 map["vertex"] = Property::Value(mShaderData->GetVertexShader());
237 map["fragment"] = Property::Value(mShaderData->GetFragmentShader());
238 map["hints"] = HintString(mShaderData->GetHints());
248 const SceneGraph::PropertyOwner* Shader::GetPropertyOwner() const
253 const SceneGraph::PropertyOwner* Shader::GetSceneObject() const
258 const SceneGraph::PropertyBase* Shader::GetSceneObjectAnimatableProperty( Property::Index index ) const
260 DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
261 const SceneGraph::PropertyBase* property = NULL;
263 property = SHADER_IMPL.GetRegisteredSceneGraphProperty( this,
264 &Shader::FindAnimatableProperty,
265 &Shader::FindCustomProperty,
268 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
270 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable" );
276 const PropertyInputImpl* Shader::GetSceneObjectInputProperty( Property::Index index ) const
278 PropertyMetadata* property = NULL;
280 if(index >= PROPERTY_CUSTOM_START_INDEX )
282 property = FindCustomProperty( index );
286 property = FindAnimatableProperty( index );
289 DALI_ASSERT_ALWAYS( property && "property index is invalid" );
290 return property->GetSceneGraphProperty();
293 int Shader::GetPropertyComponentIndex( Property::Index index ) const
295 return Property::INVALID_COMPONENT_INDEX;
299 : mSceneObject( NULL ),
304 void Shader::Initialize(
305 const std::string& vertexSource,
306 const std::string& fragmentSource,
307 Dali::Shader::ShaderHints hints )
309 EventThreadServices& eventThreadServices = GetEventThreadServices();
310 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
312 // @todo MESH_REWORK - Pass hints directly to a new scene graph shader
313 int effectHint = Dali::ShaderEffect::HINT_NONE;
314 if( hints & Dali::Shader::HINT_OUTPUT_IS_TRANSPARENT )
316 effectHint |= Dali::ShaderEffect::HINT_BLENDING;
319 if( hints & Dali::Shader::HINT_REQUIRES_SELF_DEPTH_TEST )
321 effectHint |= Dali::ShaderEffect::HINT_DEPTH_BUFFER;
324 if( (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) == 0x0 )
326 effectHint |= Dali::ShaderEffect::HINT_DOESNT_MODIFY_GEOMETRY;
328 Dali::ShaderEffect::GeometryHints shaderEffectHint = static_cast<Dali::ShaderEffect::GeometryHints>( effectHint );
330 mSceneObject = new SceneGraph::Shader( shaderEffectHint );
332 // Add to update manager
333 AddShaderMessage( updateManager, *mSceneObject );
335 // Try to load a precompiled shader binary for the source pair:
336 ThreadLocalStorage& tls = ThreadLocalStorage::Get();
337 ShaderFactory& shaderFactory = tls.GetShaderFactory();
339 mShaderData = shaderFactory.Load( vertexSource, fragmentSource, hints, shaderHash );
341 // Add shader program to scene-object using a message to the UpdateManager
342 SetShaderProgramMessage( updateManager, *mSceneObject, mShaderData, (hints & Dali::Shader::HINT_MODIFIES_GEOMETRY) != 0x0 );
343 eventThreadServices.RegisterObject( this );
348 if( EventThreadServices::IsCoreRunning() )
350 EventThreadServices& eventThreadServices = GetEventThreadServices();
351 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
352 RemoveShaderMessage( updateManager, *mSceneObject);
354 eventThreadServices.UnregisterObject( this );
359 } // namespace Internal