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.
18 #include "scene-graph-material.h"
21 #include <dali/public-api/shader-effects/material.h>
22 #include <dali/public-api/shader-effects/shader-effect.h>
23 #include <dali/internal/common/internal-constants.h>
24 #include <dali/internal/update/effects/scene-graph-sampler.h>
25 #include <dali/internal/render/data-providers/sampler-data-provider.h>
26 #include <dali/internal/render/shaders/scene-graph-shader.h>
27 #include <dali/public-api/actors/blending.h>
38 const unsigned int DEFAULT_BLENDING_OPTIONS( BlendingOptions().GetBitmask() );
42 : mColor( Color::WHITE ),
43 mBlendColor( Color::TRANSPARENT ),
44 mFaceCullingMode(Dali::Material::NONE),
45 mBlendingMode(Dali::BlendingMode::AUTO),
46 mBlendingOptions( DEFAULT_BLENDING_OPTIONS ),
50 // Observe own property-owner's uniform map
51 AddUniformMapObserver( *this );
58 void Material::SetShader( Shader* shader )
61 mShader->AddUniformMapObserver( *this );
63 // Inform NewRenderer about this shader: (Will force a re-load of the
64 // shader from the data providers)
65 mConnectionObservers.ConnectionsChanged(*this);
68 Shader* Material::GetShader() const
70 // @todo - Fix this - move shader setup to the Renderer connect to stage...
74 void Material::AddSampler( Sampler* sampler )
76 mSamplers.PushBack( sampler );
78 sampler->AddConnectionObserver( *this );
79 sampler->AddUniformMapObserver( *this );
81 mConnectionObservers.ConnectionsChanged(*this);
84 void Material::RemoveSampler( Sampler* sampler )
86 Vector<Sampler*>::Iterator match = std::find( mSamplers.Begin(), mSamplers.End(), sampler );
88 DALI_ASSERT_DEBUG( mSamplers.End() != match );
89 if( mSamplers.End() != match )
91 sampler->RemoveConnectionObserver( *this );
92 sampler->RemoveUniformMapObserver( *this );
93 mSamplers.Erase( match );
94 mConnectionObservers.ConnectionsChanged(*this);
98 DALI_ASSERT_DEBUG( 0 && "Sampler not found" );
102 void Material::PrepareRender( BufferIndex bufferIndex )
104 mBlendPolicy = OPAQUE;
106 // @todo MESH_REWORK Add dirty flags to reduce processing.
108 switch(mBlendingMode[bufferIndex])
110 case BlendingMode::OFF:
112 mBlendPolicy = OPAQUE;
115 case BlendingMode::ON:
117 mBlendPolicy = TRANSPARENT;
120 case BlendingMode::AUTO:
124 // @todo: MESH_REWORK - Change hints for new SceneGraphShader:
125 // If shader hint OUTPUT_IS_OPAQUE is enabled, set policy to ALWAYS_OPAQUE
126 // If shader hint OUTPUT_IS_TRANSPARENT is enabled, set policy to ALWAYS_TRANSPARENT
127 // else test remainder, and set policy to either ALWAYS_TRANSPARENT or USE_ACTOR_COLOR
129 if( mShader->GeometryHintEnabled( Dali::ShaderEffect::HINT_BLENDING ) )
136 // Check the material color:
137 opaque = ( mColor[ bufferIndex ].a >= FULLY_OPAQUE );
142 // Require that all affecting samplers are opaque
143 unsigned int opaqueCount=0;
144 unsigned int affectingCount=0;
146 for( Vector<Sampler*>::ConstIterator iter = mSamplers.Begin();
147 iter != mSamplers.End(); ++iter )
149 const Sampler* sampler = *iter;
150 if( sampler != NULL )
152 if( sampler->AffectsTransparency( bufferIndex ) )
155 if( sampler->IsFullyOpaque( bufferIndex ) )
162 opaque = (opaqueCount == affectingCount);
165 mBlendPolicy = opaque ? Material::USE_ACTOR_COLOR : Material::TRANSPARENT;
170 Vector<Sampler*>& Material::GetSamplers()
175 Material::BlendPolicy Material::GetBlendPolicy() const
180 void Material::SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options )
182 mBlendingOptions.Set( updateBufferIndex, options );
185 const Vector4& Material::GetBlendColor(BufferIndex bufferIndex) const
187 return mBlendColor[bufferIndex];
190 BlendingFactor::Type Material::GetBlendSrcFactorRgb( BufferIndex bufferIndex ) const
192 BlendingOptions blendingOptions;
193 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
194 return blendingOptions.GetBlendSrcFactorRgb();
197 BlendingFactor::Type Material::GetBlendSrcFactorAlpha( BufferIndex bufferIndex ) const
199 BlendingOptions blendingOptions;
200 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
201 return blendingOptions.GetBlendSrcFactorAlpha();
204 BlendingFactor::Type Material::GetBlendDestFactorRgb( BufferIndex bufferIndex ) const
206 BlendingOptions blendingOptions;
207 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
208 return blendingOptions.GetBlendDestFactorRgb();
211 BlendingFactor::Type Material::GetBlendDestFactorAlpha( BufferIndex bufferIndex ) const
213 BlendingOptions blendingOptions;
214 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
215 return blendingOptions.GetBlendDestFactorAlpha();
218 BlendingEquation::Type Material::GetBlendEquationRgb( BufferIndex bufferIndex ) const
220 BlendingOptions blendingOptions;
221 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
222 return blendingOptions.GetBlendEquationRgb();
225 BlendingEquation::Type Material::GetBlendEquationAlpha( BufferIndex bufferIndex ) const
227 BlendingOptions blendingOptions;
228 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
229 return blendingOptions.GetBlendEquationAlpha();
232 void Material::ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
236 void Material::DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
240 void Material::AddConnectionObserver( ConnectionChangePropagator::Observer& observer )
242 mConnectionObservers.Add(observer);
245 void Material::RemoveConnectionObserver( ConnectionChangePropagator::Observer& observer )
247 mConnectionObservers.Remove(observer);
250 void Material::UniformMappingsChanged( const UniformMap& mappings )
252 // Our uniform map, or that of one of the watched children has changed.
253 // Inform connected observers.
254 mConnectionObservers.ConnectedUniformMapChanged();
257 void Material::ConnectionsChanged( PropertyOwner& owner )
259 mConnectionObservers.ConnectionsChanged(*this);
262 void Material::ConnectedUniformMapChanged( )
264 mConnectionObservers.ConnectedUniformMapChanged();
267 void Material::ResetDefaultProperties( BufferIndex updateBufferIndex )
269 mColor.ResetToBaseValue( updateBufferIndex );
270 mBlendColor.ResetToBaseValue( updateBufferIndex );
271 mFaceCullingMode.CopyPrevious( updateBufferIndex );
273 mBlendingMode.CopyPrevious( updateBufferIndex );
274 mBlendingOptions.CopyPrevious( updateBufferIndex );
277 } // namespace SceneGraph
278 } // namespace Internal