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/actors/blending.h>
22 #include <dali/public-api/shader-effects/shader-effect.h>
23 #include <dali/devel-api/rendering/material.h>
24 #include <dali/internal/common/internal-constants.h>
25 #include <dali/internal/update/rendering/scene-graph-sampler.h>
26 #include <dali/internal/render/data-providers/sampler-data-provider.h>
27 #include <dali/internal/render/shaders/scene-graph-shader.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 );
56 mConnectionObservers.Destroy( *this );
59 void Material::SetShader( Shader* shader )
62 mShader->AddUniformMapObserver( *this );
64 // Inform NewRenderer about this shader: (Will force a re-load of the
65 // shader from the data providers)
66 mConnectionObservers.ConnectionsChanged(*this);
69 Shader* Material::GetShader() const
71 // @todo - Fix this - move shader setup to the Renderer connect to stage...
75 void Material::AddSampler( Sampler* sampler )
77 mSamplers.PushBack( sampler );
79 sampler->AddConnectionObserver( *this );
80 sampler->AddUniformMapObserver( *this );
82 mConnectionObservers.ConnectionsChanged(*this);
85 void Material::RemoveSampler( Sampler* sampler )
87 Vector<Sampler*>::Iterator match = std::find( mSamplers.Begin(), mSamplers.End(), sampler );
89 DALI_ASSERT_DEBUG( mSamplers.End() != match );
90 if( mSamplers.End() != match )
92 sampler->RemoveConnectionObserver( *this );
93 sampler->RemoveUniformMapObserver( *this );
94 mSamplers.Erase( match );
95 mConnectionObservers.ConnectionsChanged(*this);
99 DALI_ASSERT_DEBUG( 0 && "Sampler not found" );
103 void Material::PrepareRender( BufferIndex bufferIndex )
105 mBlendPolicy = OPAQUE;
107 // @todo MESH_REWORK Add dirty flags to reduce processing.
109 switch(mBlendingMode[bufferIndex])
111 case BlendingMode::OFF:
113 mBlendPolicy = OPAQUE;
116 case BlendingMode::ON:
118 mBlendPolicy = TRANSPARENT;
121 case BlendingMode::AUTO:
125 // @todo: MESH_REWORK - Change hints for new SceneGraphShader:
126 // If shader hint OUTPUT_IS_OPAQUE is enabled, set policy to ALWAYS_OPAQUE
127 // If shader hint OUTPUT_IS_TRANSPARENT is enabled, set policy to ALWAYS_TRANSPARENT
128 // else test remainder, and set policy to either ALWAYS_TRANSPARENT or USE_ACTOR_COLOR
130 if( mShader->GeometryHintEnabled( Dali::ShaderEffect::HINT_BLENDING ) )
137 // Check the material color:
138 opaque = ( mColor[ bufferIndex ].a >= FULLY_OPAQUE );
143 // Require that all affecting samplers are opaque
144 unsigned int opaqueCount=0;
145 unsigned int affectingCount=0;
147 for( Vector<Sampler*>::ConstIterator iter = mSamplers.Begin();
148 iter != mSamplers.End(); ++iter )
150 const Sampler* sampler = *iter;
151 if( sampler != NULL )
153 if( sampler->AffectsTransparency( bufferIndex ) )
156 if( sampler->IsFullyOpaque( bufferIndex ) )
163 opaque = (opaqueCount == affectingCount);
166 mBlendPolicy = opaque ? Material::USE_ACTOR_COLOR : Material::TRANSPARENT;
171 Vector<Sampler*>& Material::GetSamplers()
176 Material::BlendPolicy Material::GetBlendPolicy() const
181 void Material::SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options )
183 mBlendingOptions.Set( updateBufferIndex, options );
186 const Vector4& Material::GetBlendColor(BufferIndex bufferIndex) const
188 return mBlendColor[bufferIndex];
191 BlendingFactor::Type Material::GetBlendSrcFactorRgb( BufferIndex bufferIndex ) const
193 BlendingOptions blendingOptions;
194 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
195 return blendingOptions.GetBlendSrcFactorRgb();
198 BlendingFactor::Type Material::GetBlendSrcFactorAlpha( BufferIndex bufferIndex ) const
200 BlendingOptions blendingOptions;
201 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
202 return blendingOptions.GetBlendSrcFactorAlpha();
205 BlendingFactor::Type Material::GetBlendDestFactorRgb( BufferIndex bufferIndex ) const
207 BlendingOptions blendingOptions;
208 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
209 return blendingOptions.GetBlendDestFactorRgb();
212 BlendingFactor::Type Material::GetBlendDestFactorAlpha( BufferIndex bufferIndex ) const
214 BlendingOptions blendingOptions;
215 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
216 return blendingOptions.GetBlendDestFactorAlpha();
219 BlendingEquation::Type Material::GetBlendEquationRgb( BufferIndex bufferIndex ) const
221 BlendingOptions blendingOptions;
222 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
223 return blendingOptions.GetBlendEquationRgb();
226 BlendingEquation::Type Material::GetBlendEquationAlpha( BufferIndex bufferIndex ) const
228 BlendingOptions blendingOptions;
229 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
230 return blendingOptions.GetBlendEquationAlpha();
233 void Material::ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
237 void Material::DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
241 void Material::AddConnectionObserver( ConnectionChangePropagator::Observer& observer )
243 mConnectionObservers.Add(observer);
246 void Material::RemoveConnectionObserver( ConnectionChangePropagator::Observer& observer )
248 mConnectionObservers.Remove(observer);
251 void Material::UniformMappingsChanged( const UniformMap& mappings )
253 // Our uniform map, or that of one of the watched children has changed.
254 // Inform connected observers.
255 mConnectionObservers.ConnectedUniformMapChanged();
258 void Material::ConnectionsChanged( PropertyOwner& owner )
260 mConnectionObservers.ConnectionsChanged(*this);
263 void Material::ConnectedUniformMapChanged( )
265 mConnectionObservers.ConnectedUniformMapChanged();
268 void Material::ResetDefaultProperties( BufferIndex updateBufferIndex )
270 mColor.ResetToBaseValue( updateBufferIndex );
271 mBlendColor.ResetToBaseValue( updateBufferIndex );
272 mFaceCullingMode.CopyPrevious( updateBufferIndex );
274 mBlendingMode.CopyPrevious( updateBufferIndex );
275 mBlendingOptions.CopyPrevious( updateBufferIndex );
278 } // namespace SceneGraph
279 } // namespace Internal