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::WHITE ),
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 // Inform NewRenderer about this shader: (Will force a re-load of the
62 // shader from the data providers)
63 mConnectionObservers.ConnectionsChanged(*this);
66 Shader* Material::GetShader() const
68 // @todo - Fix this - move shader setup to the Renderer connect to stage...
72 void Material::AddSampler( Sampler* sampler )
74 mSamplers.PushBack( sampler );
76 sampler->AddConnectionObserver( *this );
77 sampler->AddUniformMapObserver( *this );
79 mConnectionObservers.ConnectionsChanged(*this);
82 void Material::RemoveSampler( Sampler* sampler )
84 Vector<Sampler*>::Iterator match = std::find( mSamplers.Begin(), mSamplers.End(), sampler );
86 DALI_ASSERT_DEBUG( mSamplers.End() != match );
87 if( mSamplers.End() != match )
89 sampler->RemoveConnectionObserver( *this );
90 sampler->RemoveUniformMapObserver( *this );
91 mSamplers.Erase( match );
92 mConnectionObservers.ConnectionsChanged(*this);
96 DALI_ASSERT_DEBUG( 0 && "Sampler not found" );
100 void Material::PrepareRender( BufferIndex bufferIndex )
102 mBlendPolicy = OPAQUE;
104 // @todo MESH_REWORK Add dirty flags to reduce processing.
106 switch(mBlendingMode[bufferIndex])
108 case BlendingMode::OFF:
110 mBlendPolicy = OPAQUE;
113 case BlendingMode::ON:
115 mBlendPolicy = TRANSPARENT;
118 case BlendingMode::AUTO:
122 // @todo: MESH_REWORK - Change hints for new SceneGraphShader:
123 // If shader hint OUTPUT_IS_OPAQUE is enabled, set policy to ALWAYS_OPAQUE
124 // If shader hint OUTPUT_IS_TRANSPARENT is enabled, set policy to ALWAYS_TRANSPARENT
125 // else test remainder, and set policy to either ALWAYS_TRANSPARENT or USE_ACTOR_COLOR
127 if( mShader->GeometryHintEnabled( Dali::ShaderEffect::HINT_BLENDING ) )
134 // Check the material color:
135 opaque = ( mColor[ bufferIndex ].a >= FULLY_OPAQUE );
140 // Require that all affecting samplers are opaque
141 unsigned int opaqueCount=0;
142 unsigned int affectingCount=0;
144 for( Vector<Sampler*>::ConstIterator iter = mSamplers.Begin();
145 iter != mSamplers.End(); ++iter )
147 const Sampler* sampler = *iter;
148 if( sampler != NULL )
150 if( sampler->AffectsTransparency( bufferIndex ) )
153 if( sampler->IsFullyOpaque( bufferIndex ) )
160 opaque = (opaqueCount == affectingCount);
163 mBlendPolicy = opaque ? Material::USE_ACTOR_COLOR : Material::TRANSPARENT;
168 Vector<Sampler*>& Material::GetSamplers()
173 Material::BlendPolicy Material::GetBlendPolicy() const
178 void Material::SetBlendingOptions( BufferIndex updateBufferIndex, unsigned int options )
180 mBlendingOptions.Set( updateBufferIndex, options );
183 const Vector4& Material::GetBlendColor(BufferIndex bufferIndex) const
185 return mBlendColor[bufferIndex];
188 BlendingFactor::Type Material::GetBlendSrcFactorRgb( BufferIndex bufferIndex ) const
190 BlendingOptions blendingOptions;
191 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
192 return blendingOptions.GetBlendSrcFactorRgb();
195 BlendingFactor::Type Material::GetBlendSrcFactorAlpha( BufferIndex bufferIndex ) const
197 BlendingOptions blendingOptions;
198 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
199 return blendingOptions.GetBlendSrcFactorAlpha();
202 BlendingFactor::Type Material::GetBlendDestFactorRgb( BufferIndex bufferIndex ) const
204 BlendingOptions blendingOptions;
205 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
206 return blendingOptions.GetBlendDestFactorRgb();
209 BlendingFactor::Type Material::GetBlendDestFactorAlpha( BufferIndex bufferIndex ) const
211 BlendingOptions blendingOptions;
212 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
213 return blendingOptions.GetBlendDestFactorAlpha();
216 BlendingEquation::Type Material::GetBlendEquationRgb( BufferIndex bufferIndex ) const
218 BlendingOptions blendingOptions;
219 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
220 return blendingOptions.GetBlendEquationRgb();
223 BlendingEquation::Type Material::GetBlendEquationAlpha( BufferIndex bufferIndex ) const
225 BlendingOptions blendingOptions;
226 blendingOptions.SetBitmask( mBlendingOptions[ bufferIndex ] );
227 return blendingOptions.GetBlendEquationAlpha();
230 void Material::ConnectToSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
234 void Material::DisconnectFromSceneGraph( SceneController& sceneController, BufferIndex bufferIndex )
238 void Material::AddConnectionObserver( ConnectionChangePropagator::Observer& observer )
240 mConnectionObservers.Add(observer);
243 void Material::RemoveConnectionObserver( ConnectionChangePropagator::Observer& observer )
245 mConnectionObservers.Remove(observer);
248 void Material::UniformMappingsChanged( const UniformMap& mappings )
250 // Our uniform map, or that of one of the watched children has changed.
251 // Inform connected observers.
252 mConnectionObservers.ConnectedUniformMapChanged();
255 void Material::ConnectionsChanged( PropertyOwner& owner )
257 mConnectionObservers.ConnectionsChanged(*this);
260 void Material::ConnectedUniformMapChanged( )
262 mConnectionObservers.ConnectedUniformMapChanged();
265 void Material::ResetDefaultProperties( BufferIndex updateBufferIndex )
267 mColor.ResetToBaseValue( updateBufferIndex );
268 mBlendColor.ResetToBaseValue( updateBufferIndex );
269 mFaceCullingMode.CopyPrevious( updateBufferIndex );
271 mBlendingMode.CopyPrevious( updateBufferIndex );
272 mBlendingOptions.CopyPrevious( updateBufferIndex );
275 } // namespace SceneGraph
276 } // namespace Internal