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/material-impl.h> // Dali::Internal::Material
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/devel-api/rendering/material.h> // Dali::Internal::Material
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/update/manager/update-manager.h>
27 #include <dali/internal/update/rendering/scene-graph-material.h>
28 #include <dali/internal/update/rendering/scene-graph-sampler.h>
39 * |name |type |writable|animatable|constraint-input|enum for index-checking|
41 DALI_PROPERTY_TABLE_BEGIN
42 DALI_PROPERTY( "color", VECTOR4, true, true, true, Dali::Material::Property::COLOR )
43 DALI_PROPERTY( "face-culling-mode", STRING, true, false, false, Dali::Material::Property::FACE_CULLING_MODE )
44 DALI_PROPERTY( "blending-mode", STRING, true, false, false, Dali::Material::Property::BLENDING_MODE )
45 DALI_PROPERTY( "blend-equation-rgb", STRING, true, false, false, Dali::Material::Property::BLEND_EQUATION_RGB )
46 DALI_PROPERTY( "blend-equation-alpha", STRING, true, false, false, Dali::Material::Property::BLEND_EQUATION_ALPHA )
47 DALI_PROPERTY( "source-blend-factor-rgb", STRING, true, false, false, Dali::Material::Property::BLENDING_SRC_FACTOR_RGB )
48 DALI_PROPERTY( "destination-blend-factor-rgb", STRING, true, false, false, Dali::Material::Property::BLENDING_DEST_FACTOR_RGB )
49 DALI_PROPERTY( "source-blend-factor-alpha", STRING, true, false, false, Dali::Material::Property::BLENDING_SRC_FACTOR_ALPHA )
50 DALI_PROPERTY( "destination-blend-factor-alpha", STRING, true, false, false, Dali::Material::Property::BLENDING_DEST_FACTOR_ALPHA )
51 DALI_PROPERTY( "blend-color", VECTOR4, true, true, true, Dali::Material::Property::BLEND_COLOR )
52 DALI_PROPERTY_TABLE_END( DEFAULT_ACTOR_PROPERTY_START_INDEX )
54 const ObjectImplHelper<DEFAULT_PROPERTY_COUNT> MATERIAL_IMPL = { DEFAULT_PROPERTY_DETAILS };
58 return Dali::BaseHandle();
61 TypeRegistration mType( typeid( Dali::Material ), typeid( Dali::Handle ), Create );
63 } // unnamed namespace
65 MaterialPtr Material::New()
67 MaterialPtr material( new Material() );
68 material->Initialize();
72 void Material::SetShader( Shader& shader )
74 DALI_ASSERT_DEBUG( mSceneObject )
75 mShaderConnector.Set( shader, OnStage() );
77 const SceneGraph::Shader& sceneGraphShader = dynamic_cast<const SceneGraph::Shader&>( *shader.GetSceneObject() );
78 SceneGraph::SetShaderMessage( GetEventThreadServices(), *mSceneObject, sceneGraphShader );
81 Shader* Material::GetShader() const
83 return mShaderConnector.Get().Get();
86 void Material::AddSampler( Sampler& sampler )
88 SamplerConnector connector;
89 connector.Set( sampler, OnStage() );
90 mSamplerConnectors.push_back( connector );
92 const SceneGraph::Sampler& sceneGraphSampler = dynamic_cast<const SceneGraph::Sampler&>( *sampler.GetSceneObject() );
93 SceneGraph::AddSamplerMessage( GetEventThreadServices(), *mSceneObject, sceneGraphSampler );
96 std::size_t Material::GetNumberOfSamplers() const
98 return mSamplerConnectors.size();
101 void Material::RemoveSampler( std::size_t index )
103 mSamplerConnectors.erase( mSamplerConnectors.begin() + index );
106 Sampler* Material::GetSamplerAt( unsigned int index ) const
108 return mSamplerConnectors[index].Get().Get();
111 void Material::SetFaceCullingMode( Dali::Material::FaceCullingMode cullingMode )
113 if( NULL != mSceneObject )
115 SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mFaceCullingMode, &SceneGraph::DoubleBufferedProperty<int>::Set, static_cast<int>(cullingMode) );
119 void Material::SetBlendMode( BlendingMode::Type mode )
121 mBlendingMode = mode;
123 if( NULL != mSceneObject )
125 SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mBlendingMode, &SceneGraph::DoubleBufferedProperty<int>::Set, static_cast<int>(mode) );
129 BlendingMode::Type Material::GetBlendMode() const
131 return mBlendingMode;
134 void Material::SetBlendFunc( BlendingFactor::Type srcFactorRgba, BlendingFactor::Type destFactorRgba )
136 mBlendingOptions.SetBlendFunc( srcFactorRgba, destFactorRgba, srcFactorRgba, destFactorRgba );
137 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
140 void Material::SetBlendFunc( BlendingFactor::Type srcFactorRgb,
141 BlendingFactor::Type destFactorRgb,
142 BlendingFactor::Type srcFactorAlpha,
143 BlendingFactor::Type destFactorAlpha )
145 mBlendingOptions.SetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
146 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
149 void Material::GetBlendFunc( BlendingFactor::Type& srcFactorRgb,
150 BlendingFactor::Type& destFactorRgb,
151 BlendingFactor::Type& srcFactorAlpha,
152 BlendingFactor::Type& destFactorAlpha ) const
154 srcFactorRgb = mBlendingOptions.GetBlendSrcFactorRgb();
155 destFactorRgb = mBlendingOptions.GetBlendDestFactorRgb();
156 srcFactorAlpha = mBlendingOptions.GetBlendSrcFactorAlpha();
157 destFactorAlpha = mBlendingOptions.GetBlendDestFactorAlpha();
160 void Material::SetBlendEquation( BlendingEquation::Type equationRgba )
162 mBlendingOptions.SetBlendEquation( equationRgba, equationRgba );
163 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
166 void Material::SetBlendEquation( BlendingEquation::Type equationRgb,
167 BlendingEquation::Type equationAlpha )
169 mBlendingOptions.SetBlendEquation( equationRgb, equationAlpha );
170 SetBlendingOptionsMessage( GetEventThreadServices(), *mSceneObject, mBlendingOptions.GetBitmask() );
173 void Material::GetBlendEquation( BlendingEquation::Type& equationRgb,
174 BlendingEquation::Type& equationAlpha ) const
176 // These are not animatable, the cached values are up-to-date.
177 equationRgb = mBlendingOptions.GetBlendEquationRgb();
178 equationAlpha = mBlendingOptions.GetBlendEquationAlpha();
181 void Material::SetBlendColor( const Vector4& color )
185 SceneGraph::AnimatablePropertyMessage<Vector4>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mBlendColor, &SceneGraph::AnimatableProperty<Vector4>::Bake, color );
189 const Vector4& Material::GetBlendColor() const
191 return mSceneObject->mBlendColor[ GetEventThreadServices().GetEventBufferIndex() ];
194 const SceneGraph::Material* Material::GetMaterialSceneObject() const
199 unsigned int Material::GetDefaultPropertyCount() const
201 return MATERIAL_IMPL.GetDefaultPropertyCount();
204 void Material::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
206 MATERIAL_IMPL.GetDefaultPropertyIndices( indices );
209 const char* Material::GetDefaultPropertyName(Property::Index index) const
211 return MATERIAL_IMPL.GetDefaultPropertyName( index );
214 Property::Index Material::GetDefaultPropertyIndex( const std::string& name ) const
216 return MATERIAL_IMPL.GetDefaultPropertyIndex( name );
219 bool Material::IsDefaultPropertyWritable( Property::Index index ) const
221 return MATERIAL_IMPL.IsDefaultPropertyWritable( index );
224 bool Material::IsDefaultPropertyAnimatable( Property::Index index ) const
226 return MATERIAL_IMPL.IsDefaultPropertyAnimatable( index );
229 bool Material::IsDefaultPropertyAConstraintInput( Property::Index index ) const
231 return MATERIAL_IMPL.IsDefaultPropertyAConstraintInput( index );
234 Property::Type Material::GetDefaultPropertyType( Property::Index index ) const
236 return MATERIAL_IMPL.GetDefaultPropertyType( index );
239 void Material::SetDefaultProperty( Property::Index index,
240 const Property::Value& propertyValue )
244 case Dali::Material::Property::COLOR:
246 SceneGraph::AnimatablePropertyMessage<Vector4>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mColor, &SceneGraph::AnimatableProperty<Vector4>::Bake, propertyValue.Get<Vector4>() );
249 case Dali::Material::Property::FACE_CULLING_MODE:
251 SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mFaceCullingMode, &SceneGraph::DoubleBufferedProperty<int>::Set, propertyValue.Get<int>() );
254 case Dali::Material::Property::BLENDING_MODE:
258 SceneGraph::DoubleBufferedPropertyMessage<int>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mBlendingMode, &SceneGraph::DoubleBufferedProperty<int>::Set, propertyValue.Get<int>() );
262 case Dali::Material::Property::BLEND_EQUATION_RGB:
264 BlendingEquation::Type alphaEquation = mBlendingOptions.GetBlendEquationAlpha();
265 mBlendingOptions.SetBlendEquation( static_cast<BlendingEquation::Type>(propertyValue.Get<int>()), alphaEquation );
268 case Dali::Material::Property::BLEND_EQUATION_ALPHA:
270 BlendingEquation::Type rgbEquation = mBlendingOptions.GetBlendEquationRgb();
271 mBlendingOptions.SetBlendEquation( rgbEquation, static_cast<BlendingEquation::Type>(propertyValue.Get<int>()) );
274 case Dali::Material::Property::BLENDING_SRC_FACTOR_RGB:
276 BlendingFactor::Type srcFactorRgb;
277 BlendingFactor::Type destFactorRgb;
278 BlendingFactor::Type srcFactorAlpha;
279 BlendingFactor::Type destFactorAlpha;
280 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
281 SetBlendFunc( static_cast<BlendingFactor::Type>(propertyValue.Get<int>()),
287 case Dali::Material::Property::BLENDING_DEST_FACTOR_RGB:
289 BlendingFactor::Type srcFactorRgb;
290 BlendingFactor::Type destFactorRgb;
291 BlendingFactor::Type srcFactorAlpha;
292 BlendingFactor::Type destFactorAlpha;
293 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
294 SetBlendFunc( srcFactorRgb,
295 static_cast<BlendingFactor::Type>(propertyValue.Get<int>()),
300 case Dali::Material::Property::BLENDING_SRC_FACTOR_ALPHA:
302 BlendingFactor::Type srcFactorRgb;
303 BlendingFactor::Type destFactorRgb;
304 BlendingFactor::Type srcFactorAlpha;
305 BlendingFactor::Type destFactorAlpha;
306 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
307 SetBlendFunc( srcFactorRgb,
309 static_cast<BlendingFactor::Type>(propertyValue.Get<int>()),
313 case Dali::Material::Property::BLENDING_DEST_FACTOR_ALPHA:
315 BlendingFactor::Type srcFactorRgb;
316 BlendingFactor::Type destFactorRgb;
317 BlendingFactor::Type srcFactorAlpha;
318 BlendingFactor::Type destFactorAlpha;
319 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
320 SetBlendFunc( srcFactorRgb,
323 static_cast<BlendingFactor::Type>(propertyValue.Get<int>()) );
326 case Dali::Material::Property::BLEND_COLOR:
328 SceneGraph::AnimatablePropertyMessage<Vector4>::Send( GetEventThreadServices(), mSceneObject, &mSceneObject->mBlendColor, &SceneGraph::AnimatableProperty<Vector4>::Bake, propertyValue.Get<Vector4>() );
334 void Material::SetSceneGraphProperty( Property::Index index,
335 const PropertyMetadata& entry,
336 const Property::Value& value )
338 MATERIAL_IMPL.SetSceneGraphProperty( GetEventThreadServices(), this, index, entry, value );
339 OnPropertySet(index, value);
342 Property::Value Material::GetDefaultProperty( Property::Index index ) const
344 BufferIndex bufferIndex = GetEventThreadServices().GetEventBufferIndex();
345 Property::Value value;
349 case Dali::Material::Property::COLOR:
353 value = mSceneObject->mColor[bufferIndex];
357 case Dali::Material::Property::FACE_CULLING_MODE:
361 value = mSceneObject->mFaceCullingMode[bufferIndex];
365 case Dali::Material::Property::BLENDING_MODE:
369 value = mSceneObject->mBlendingMode[bufferIndex];
373 case Dali::Material::Property::BLEND_EQUATION_RGB:
375 value = static_cast<int>( mBlendingOptions.GetBlendEquationRgb() );
378 case Dali::Material::Property::BLEND_EQUATION_ALPHA:
380 value = static_cast<int>( mBlendingOptions.GetBlendEquationAlpha() );
383 case Dali::Material::Property::BLENDING_SRC_FACTOR_RGB:
385 BlendingFactor::Type srcFactorRgb;
386 BlendingFactor::Type destFactorRgb;
387 BlendingFactor::Type srcFactorAlpha;
388 BlendingFactor::Type destFactorAlpha;
389 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
390 value = static_cast<int>( srcFactorRgb );
393 case Dali::Material::Property::BLENDING_DEST_FACTOR_RGB:
395 BlendingFactor::Type srcFactorRgb;
396 BlendingFactor::Type destFactorRgb;
397 BlendingFactor::Type srcFactorAlpha;
398 BlendingFactor::Type destFactorAlpha;
399 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
400 value = static_cast<int>( destFactorRgb );
403 case Dali::Material::Property::BLENDING_SRC_FACTOR_ALPHA:
405 BlendingFactor::Type srcFactorRgb;
406 BlendingFactor::Type destFactorRgb;
407 BlendingFactor::Type srcFactorAlpha;
408 BlendingFactor::Type destFactorAlpha;
409 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
410 value = static_cast<int>( srcFactorAlpha );
413 case Dali::Material::Property::BLENDING_DEST_FACTOR_ALPHA:
415 BlendingFactor::Type srcFactorRgb;
416 BlendingFactor::Type destFactorRgb;
417 BlendingFactor::Type srcFactorAlpha;
418 BlendingFactor::Type destFactorAlpha;
419 GetBlendFunc( srcFactorRgb, destFactorRgb, srcFactorAlpha, destFactorAlpha );
420 value = static_cast<int>( destFactorAlpha );
423 case Dali::Material::Property::BLEND_COLOR:
427 value = mSceneObject->mBlendColor[bufferIndex];
436 const SceneGraph::PropertyOwner* Material::GetPropertyOwner() const
441 const SceneGraph::PropertyOwner* Material::GetSceneObject() const
446 const SceneGraph::PropertyBase* Material::GetSceneObjectAnimatableProperty( Property::Index index ) const
448 DALI_ASSERT_ALWAYS( IsPropertyAnimatable( index ) && "Property is not animatable" );
450 const SceneGraph::PropertyBase* property = NULL;
454 property = MATERIAL_IMPL.GetRegisteredSceneGraphProperty( this,
455 &Material::FindAnimatableProperty,
456 &Material::FindCustomProperty,
459 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
463 case Dali::Material::Property::COLOR:
465 property = &mSceneObject->mColor;
468 case Dali::Material::Property::BLEND_COLOR:
470 property = &mSceneObject->mBlendColor;
475 DALI_ASSERT_ALWAYS( 0 && "Property is not animatable");
485 const PropertyInputImpl* Material::GetSceneObjectInputProperty( Property::Index index ) const
487 const PropertyInputImpl* property = NULL;
491 const SceneGraph::PropertyBase* baseProperty =
492 MATERIAL_IMPL.GetRegisteredSceneGraphProperty( this,
493 &Material::FindAnimatableProperty,
494 &Material::FindCustomProperty,
496 property = static_cast<const PropertyInputImpl*>( baseProperty );
498 if( property == NULL && index < DEFAULT_PROPERTY_MAX_COUNT )
502 case Dali::Material::Property::COLOR:
504 property = &mSceneObject->mColor;
507 case Dali::Material::Property::FACE_CULLING_MODE:
509 property = &mSceneObject->mFaceCullingMode;
512 case Dali::Material::Property::BLEND_COLOR:
514 property = &mSceneObject->mBlendColor;
519 DALI_ASSERT_ALWAYS( 0 && "Property cannot be a constraint input");
529 int Material::GetPropertyComponentIndex( Property::Index index ) const
531 // @todo MESH_REWORK - Change this if component properties are added for color/blend-color
532 return Property::INVALID_COMPONENT_INDEX;
535 bool Material::OnStage() const
540 void Material::Connect()
544 SamplerConnectorContainer::const_iterator end = mSamplerConnectors.end();
545 for( SamplerConnectorContainer::iterator it = mSamplerConnectors.begin();
549 it->OnStageConnect();
551 mShaderConnector.OnStageConnect();
554 void Material::Disconnect()
558 SamplerConnectorContainer::const_iterator end = mSamplerConnectors.end();
559 for( SamplerConnectorContainer::iterator it = mSamplerConnectors.begin();
563 it->OnStageDisconnect();
565 mShaderConnector.OnStageDisconnect();
569 : mSceneObject( NULL ),
574 void Material::Initialize()
576 EventThreadServices& eventThreadServices = GetEventThreadServices();
577 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
579 mSceneObject = new SceneGraph::Material();
580 AddMessage( updateManager, updateManager.GetMaterialOwner(), *mSceneObject );
582 eventThreadServices.RegisterObject( this );
585 Material::~Material()
587 if( EventThreadServices::IsCoreRunning() )
589 EventThreadServices& eventThreadServices = GetEventThreadServices();
590 SceneGraph::UpdateManager& updateManager = eventThreadServices.GetUpdateManager();
591 RemoveMessage( updateManager, updateManager.GetMaterialOwner(), *mSceneObject );
593 eventThreadServices.UnregisterObject( this );
597 } // namespace Internal