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/render/renderers/render-renderer.h>
23 #include <dali/internal/common/image-sampler.h>
24 #include <dali/internal/render/gl-resources/context.h>
25 #include <dali/internal/render/renderers/render-sampler.h>
26 #include <dali/internal/render/shaders/scene-graph-shader.h>
27 #include <dali/internal/render/shaders/program.h>
28 #include <dali/internal/render/data-providers/node-data-provider.h>
29 #include <dali/internal/render/data-providers/uniform-name-cache.h>
30 #include <dali/internal/render/gl-resources/texture.h>
31 #include <dali/internal/render/gl-resources/texture-cache.h>
32 #include <dali/public-api/actors/blending.h>
43 static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it in object to reduce storage overhead
44 static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it in object to reduce storage overhead
47 * Helper to set view and projection matrices once per program
48 * @param program to set the matrices to
49 * @param modelMatrix to set
50 * @param viewMatrix to set
51 * @param projectionMatrix to set
52 * @param modelViewMatrix to set
53 * @param modelViewProjectionMatrix to set
55 inline void SetMatrices( Program& program,
56 const Matrix& modelMatrix,
57 const Matrix& viewMatrix,
58 const Matrix& projectionMatrix,
59 const Matrix& modelViewMatrix )
61 GLint loc = program.GetUniformLocation( Program::UNIFORM_MODEL_MATRIX );
62 if( Program::UNIFORM_UNKNOWN != loc )
64 program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
66 loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
67 if( Program::UNIFORM_UNKNOWN != loc )
69 if( program.GetViewMatrix() != &viewMatrix )
71 program.SetViewMatrix( &viewMatrix );
72 program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
75 // set projection matrix if program has not yet received it this frame or if it is dirty
76 loc = program.GetUniformLocation( Program::UNIFORM_PROJECTION_MATRIX );
77 if( Program::UNIFORM_UNKNOWN != loc )
79 if( program.GetProjectionMatrix() != &projectionMatrix )
81 program.SetProjectionMatrix( &projectionMatrix );
82 program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
85 loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
86 if( Program::UNIFORM_UNKNOWN != loc )
88 program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
91 loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
92 if( Program::UNIFORM_UNKNOWN != loc )
94 Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
95 program.SetUniformMatrix4fv( loc, 1, gModelViewProjectionMatrix.AsFloat() );
98 loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
99 if( Program::UNIFORM_UNKNOWN != loc )
101 gNormalMatrix = modelViewMatrix;
102 gNormalMatrix.Invert();
103 gNormalMatrix.Transpose();
104 program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
113 Renderer* Renderer::New( SceneGraph::RenderDataProvider* dataProvider,
114 Render::Geometry* geometry,
115 unsigned int blendingBitmask,
116 const Vector4* blendColor,
117 FaceCullingMode::Type faceCullingMode,
118 bool preMultipliedAlphaEnabled,
119 DepthWriteMode::Type depthWriteMode )
121 return new Renderer( dataProvider, geometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode );
124 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
125 Render::Geometry* geometry,
126 unsigned int blendingBitmask,
127 const Vector4* blendColor,
128 FaceCullingMode::Type faceCullingMode,
129 bool preMultipliedAlphaEnabled,
130 DepthWriteMode::Type depthWriteMode )
131 : mRenderDataProvider( dataProvider ),
133 mTextureCache( NULL ),
134 mUniformNameCache( NULL ),
135 mGeometry( geometry ),
137 mAttributesLocation(),
139 mFaceCullingMode( faceCullingMode ),
140 mDepthWriteMode( depthWriteMode ),
141 mIndexedDrawFirstElement( 0 ),
142 mIndexedDrawElementsCount( 0 ),
143 mUpdateAttributesLocation( true ),
144 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
146 if( blendingBitmask != 0u )
148 mBlendingOptions.SetBitmask( blendingBitmask );
153 mBlendingOptions.SetBlendColor( *blendColor );
157 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache )
160 mTextureCache = &textureCache;
161 mUniformNameCache = &uniformNameCache;
164 Renderer::~Renderer()
168 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
170 mRenderDataProvider = dataProvider;
171 mUpdateAttributesLocation = true;
174 void Renderer::SetGeometry( Render::Geometry* geometry )
176 mGeometry = geometry;
177 mUpdateAttributesLocation = true;
180 void Renderer::SetBlending( Context& context, bool blend )
182 context.SetBlend( blend );
185 // Blend color is optional and rarely used
186 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
189 context.SetCustomBlendColor( *blendColor );
193 context.SetDefaultBlendColor();
196 // Set blend source & destination factors
197 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
198 mBlendingOptions.GetBlendDestFactorRgb(),
199 mBlendingOptions.GetBlendSrcFactorAlpha(),
200 mBlendingOptions.GetBlendDestFactorAlpha() );
202 // Set blend equations
203 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
204 mBlendingOptions.GetBlendEquationAlpha() );
208 void Renderer::GlContextDestroyed()
210 mGeometry->GlContextDestroyed();
213 void Renderer::GlCleanup()
217 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
219 // Check if the map has changed
220 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
222 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
224 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
225 node.GetUniformMapChanged(bufferIndex))
227 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
228 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
230 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
231 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
232 mUniformIndexMap.Resize( maxMaps );
234 unsigned int mapIndex(0);
235 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
237 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
238 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
241 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
243 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
245 for( unsigned int i(0); i<uniformMap.Count(); ++i )
247 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
249 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
257 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
258 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
263 mUniformIndexMap.Resize( mapIndex );
266 // Set uniforms in local map
267 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
268 end = mUniformIndexMap.End() ;
272 SetUniformFromProperty( bufferIndex, program, *iter );
275 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
278 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
282 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
284 GLint location = program.GetUniformLocation(map.uniformIndex);
285 if( Program::UNIFORM_UNKNOWN != location )
287 // switch based on property type to use correct GL uniform setter
288 switch ( map.propertyValue->GetType() )
290 case Property::INTEGER:
292 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
295 case Property::FLOAT:
297 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
300 case Property::VECTOR2:
302 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
303 program.SetUniform2f( location, value.x, value.y );
307 case Property::VECTOR3:
309 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
310 program.SetUniform3f( location, value.x, value.y, value.z );
314 case Property::VECTOR4:
316 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
317 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
321 case Property::ROTATION:
323 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
324 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
328 case Property::MATRIX:
330 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
331 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
335 case Property::MATRIX3:
337 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
338 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
344 // Other property types are ignored
351 bool Renderer::BindTextures( SceneGraph::TextureCache& textureCache, Program& program )
356 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
357 for( size_t i(0); result && i<textures.size(); ++i )
359 ResourceId textureId = textures[i].GetTextureId();
360 Internal::Texture* texture = textureCache.GetTexture( textureId );
363 result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
367 GLint uniformLocation;
369 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
370 if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
372 program.SetUniform1i( uniformLocation, textureUnit );
374 unsigned int samplerBitfield(0);
375 Render::Texture& textureMapping = textures[i];
376 const Render::Sampler* sampler( textureMapping.GetSampler() );
379 samplerBitfield = ImageSampler::PackBitfield(
380 static_cast< FilterMode::Type >(sampler->GetMinifyFilterMode()),
381 static_cast< FilterMode::Type >(sampler->GetMagnifyFilterMode()),
382 static_cast< WrapMode::Type >(sampler->GetUWrapMode()),
383 static_cast< WrapMode::Type >(sampler->GetVWrapMode())
388 samplerBitfield = ImageSampler::DEFAULT_BITFIELD;
391 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
401 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
403 mFaceCullingMode = mode;
406 void Renderer::SetBlendingBitMask( unsigned int bitmask )
408 mBlendingOptions.SetBitmask( bitmask );
411 void Renderer::SetBlendColor( const Vector4* color )
413 mBlendingOptions.SetBlendColor( *color );
416 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
418 mIndexedDrawFirstElement = firstElement;
421 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
423 mIndexedDrawElementsCount = elementsCount;
426 void Renderer::EnablePreMultipliedAlpha( bool enable )
428 mPremultipledAlphaEnabled = enable;
431 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
433 mDepthWriteMode = depthWriteMode;
436 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
438 return mDepthWriteMode;
441 void Renderer::Render( Context& context,
442 SceneGraph::TextureCache& textureCache,
443 BufferIndex bufferIndex,
444 const SceneGraph::NodeDataProvider& node,
445 SceneGraph::Shader& defaultShader,
446 const Matrix& modelMatrix,
447 const Matrix& modelViewMatrix,
448 const Matrix& viewMatrix,
449 const Matrix& projectionMatrix,
453 // Get the program to use:
454 Program* program = mRenderDataProvider->GetShader().GetProgram();
457 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
458 program = defaultShader.GetProgram();
459 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
462 DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*)&mRenderDataProvider->GetShader() );
468 context.CullFace( mFaceCullingMode );
471 SetBlending( context, blend );
473 // Take the program into use so we can send uniforms to it
476 if( DALI_LIKELY( BindTextures( textureCache, *program ) ) )
478 // Only set up and draw if we have textures and they are all valid
480 // set projection and view matrix if program has not yet received them yet this frame
481 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
484 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
485 if( Program::UNIFORM_UNKNOWN != loc )
487 const Vector4& color = node.GetRenderColor( bufferIndex );
488 if( mPremultipledAlphaEnabled )
490 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
494 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
498 SetUniforms( bufferIndex, node, size, *program );
500 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
502 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
503 mUpdateAttributesLocation = false;
506 mGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation, mIndexedDrawFirstElement, mIndexedDrawElementsCount );
510 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
512 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
513 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
514 if( !textures.empty() )
516 sortAttributes.textureResourceId = textures[0].GetTextureId();
520 sortAttributes.textureResourceId = Integration::InvalidResourceId;
523 sortAttributes.geometry = mGeometry;
526 } // namespace SceneGraph
528 } // namespace Internal