2 * Copyright (c) 2016 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>
22 #include <dali/internal/common/image-sampler.h>
23 #include <dali/internal/render/gl-resources/context.h>
24 #include <dali/internal/render/renderers/render-sampler.h>
25 #include <dali/internal/render/shaders/scene-graph-shader.h>
26 #include <dali/internal/render/shaders/program.h>
27 #include <dali/internal/render/data-providers/node-data-provider.h>
38 static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it in object to reduce storage overhead
39 static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it in object to reduce storage overhead
42 * Helper to set view and projection matrices once per program
43 * @param program to set the matrices to
44 * @param modelMatrix to set
45 * @param viewMatrix to set
46 * @param projectionMatrix to set
47 * @param modelViewMatrix to set
48 * @param modelViewProjectionMatrix to set
50 inline void SetMatrices( Program& program,
51 const Matrix& modelMatrix,
52 const Matrix& viewMatrix,
53 const Matrix& projectionMatrix,
54 const Matrix& modelViewMatrix )
56 GLint loc = program.GetUniformLocation( Program::UNIFORM_MODEL_MATRIX );
57 if( Program::UNIFORM_UNKNOWN != loc )
59 program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
61 loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
62 if( Program::UNIFORM_UNKNOWN != loc )
64 if( program.GetViewMatrix() != &viewMatrix )
66 program.SetViewMatrix( &viewMatrix );
67 program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
70 // set projection matrix if program has not yet received it this frame or if it is dirty
71 loc = program.GetUniformLocation( Program::UNIFORM_PROJECTION_MATRIX );
72 if( Program::UNIFORM_UNKNOWN != loc )
74 if( program.GetProjectionMatrix() != &projectionMatrix )
76 program.SetProjectionMatrix( &projectionMatrix );
77 program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
80 loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
81 if( Program::UNIFORM_UNKNOWN != loc )
83 program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
86 loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
87 if( Program::UNIFORM_UNKNOWN != loc )
89 Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
90 program.SetUniformMatrix4fv( loc, 1, gModelViewProjectionMatrix.AsFloat() );
93 loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
94 if( Program::UNIFORM_UNKNOWN != loc )
96 gNormalMatrix = modelViewMatrix;
97 gNormalMatrix.Invert();
98 gNormalMatrix.Transpose();
99 program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
108 Renderer* Renderer::New( SceneGraph::RenderDataProvider* dataProvider,
109 Render::Geometry* geometry,
110 unsigned int blendingBitmask,
111 const Vector4* blendColor,
112 FaceCullingMode::Type faceCullingMode,
113 bool preMultipliedAlphaEnabled,
114 DepthWriteMode::Type depthWriteMode,
115 DepthTestMode::Type depthTestMode,
116 DepthFunction::Type depthFunction,
117 StencilParameters& stencilParameters )
119 return new Renderer( dataProvider, geometry, blendingBitmask, blendColor,
120 faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode,
121 depthFunction, stencilParameters );
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 DepthTestMode::Type depthTestMode,
132 DepthFunction::Type depthFunction,
133 StencilParameters& stencilParameters )
134 : mRenderDataProvider( dataProvider ),
136 mGeometry( geometry ),
138 mAttributesLocation(),
139 mStencilParameters( stencilParameters ),
141 mIndexedDrawFirstElement( 0 ),
142 mIndexedDrawElementsCount( 0 ),
143 mDepthFunction( depthFunction ),
144 mFaceCullingMode( faceCullingMode ),
145 mDepthWriteMode( depthWriteMode ),
146 mDepthTestMode( depthTestMode ),
147 mUpdateAttributesLocation( true ),
148 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
150 if( blendingBitmask != 0u )
152 mBlendingOptions.SetBitmask( blendingBitmask );
157 mBlendingOptions.SetBlendColor( *blendColor );
161 void Renderer::Initialize( Context& context )
166 Renderer::~Renderer()
170 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
172 mRenderDataProvider = dataProvider;
173 mUpdateAttributesLocation = true;
175 //Check that the number of textures match the number of samplers in the shader
176 size_t textureCount = dataProvider->GetTextures().size();
177 Program* program = dataProvider->GetShader().GetProgram();
178 if( program && program->GetActiveSamplerCount() != textureCount )
180 DALI_LOG_WARNING("The number of active samplers in the shader(%lu) does not match the number of textures in the TextureSet(%lu)\n",
181 program->GetActiveSamplerCount(),
186 void Renderer::SetGeometry( Render::Geometry* geometry )
188 mGeometry = geometry;
189 mUpdateAttributesLocation = true;
192 void Renderer::SetBlending( Context& context, bool blend )
194 context.SetBlend( blend );
197 // Blend color is optional and rarely used
198 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
201 context.SetCustomBlendColor( *blendColor );
205 context.SetDefaultBlendColor();
208 // Set blend source & destination factors
209 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
210 mBlendingOptions.GetBlendDestFactorRgb(),
211 mBlendingOptions.GetBlendSrcFactorAlpha(),
212 mBlendingOptions.GetBlendDestFactorAlpha() );
214 // Set blend equations
215 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
216 mBlendingOptions.GetBlendEquationAlpha() );
220 void Renderer::GlContextDestroyed()
222 mGeometry->GlContextDestroyed();
225 void Renderer::GlCleanup()
229 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
231 // Check if the map has changed
232 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
234 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
236 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
237 node.GetUniformMapChanged(bufferIndex))
239 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
240 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
242 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
243 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
244 mUniformIndexMap.Resize( maxMaps );
246 unsigned int mapIndex(0);
247 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
249 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
250 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
253 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
255 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
257 for( unsigned int i(0); i<uniformMap.Count(); ++i )
259 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
261 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
269 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
270 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
275 mUniformIndexMap.Resize( mapIndex );
278 // Set uniforms in local map
279 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
280 end = mUniformIndexMap.End() ;
284 SetUniformFromProperty( bufferIndex, program, *iter );
287 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
290 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
294 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
296 GLint location = program.GetUniformLocation(map.uniformIndex);
297 if( Program::UNIFORM_UNKNOWN != location )
299 // switch based on property type to use correct GL uniform setter
300 switch ( map.propertyValue->GetType() )
302 case Property::INTEGER:
304 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
307 case Property::FLOAT:
309 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
312 case Property::VECTOR2:
314 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
315 program.SetUniform2f( location, value.x, value.y );
319 case Property::VECTOR3:
321 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
322 program.SetUniform3f( location, value.x, value.y, value.z );
326 case Property::VECTOR4:
328 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
329 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
333 case Property::ROTATION:
335 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
336 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
340 case Property::MATRIX:
342 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
343 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
347 case Property::MATRIX3:
349 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
350 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
356 // Other property types are ignored
363 bool Renderer::BindTextures( Context& context, Program& program )
365 unsigned int textureUnit = 0;
368 GLint uniformLocation(-1);
369 std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
370 std::vector<Render::Texture*>& textures( mRenderDataProvider->GetTextures() );
371 for( size_t i(0); i<textures.size() && result; ++i )
375 result = textures[i]->Bind(context, textureUnit, samplers[i] );
376 if( result && program.GetSamplerUniformLocation( i, uniformLocation ) )
378 program.SetUniform1i( uniformLocation, textureUnit );
387 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
389 mFaceCullingMode = mode;
392 void Renderer::SetBlendingBitMask( unsigned int bitmask )
394 mBlendingOptions.SetBitmask( bitmask );
397 void Renderer::SetBlendColor( const Vector4* color )
399 mBlendingOptions.SetBlendColor( *color );
402 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
404 mIndexedDrawFirstElement = firstElement;
407 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
409 mIndexedDrawElementsCount = elementsCount;
412 void Renderer::EnablePreMultipliedAlpha( bool enable )
414 mPremultipledAlphaEnabled = enable;
417 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
419 mDepthWriteMode = depthWriteMode;
422 void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
424 mDepthTestMode = depthTestMode;
427 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
429 return mDepthWriteMode;
432 DepthTestMode::Type Renderer::GetDepthTestMode() const
434 return mDepthTestMode;
437 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
439 mDepthFunction = depthFunction;
442 DepthFunction::Type Renderer::GetDepthFunction() const
444 return mDepthFunction;
447 void Renderer::SetRenderMode( RenderMode::Type renderMode )
449 mStencilParameters.renderMode = renderMode;
452 RenderMode::Type Renderer::GetRenderMode() const
454 return mStencilParameters.renderMode;
457 void Renderer::SetStencilFunction( StencilFunction::Type stencilFunction )
459 mStencilParameters.stencilFunction = stencilFunction;
462 StencilFunction::Type Renderer::GetStencilFunction() const
464 return mStencilParameters.stencilFunction;
467 void Renderer::SetStencilFunctionMask( int stencilFunctionMask )
469 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
472 int Renderer::GetStencilFunctionMask() const
474 return mStencilParameters.stencilFunctionMask;
477 void Renderer::SetStencilFunctionReference( int stencilFunctionReference )
479 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
482 int Renderer::GetStencilFunctionReference() const
484 return mStencilParameters.stencilFunctionReference;
487 void Renderer::SetStencilMask( int stencilMask )
489 mStencilParameters.stencilMask = stencilMask;
492 int Renderer::GetStencilMask() const
494 return mStencilParameters.stencilMask;
497 void Renderer::SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail )
499 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
502 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
504 return mStencilParameters.stencilOperationOnFail;
507 void Renderer::SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail )
509 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
512 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
514 return mStencilParameters.stencilOperationOnZFail;
517 void Renderer::SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass )
519 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
522 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
524 return mStencilParameters.stencilOperationOnZPass;
527 void Renderer::Render( Context& context,
528 BufferIndex bufferIndex,
529 const SceneGraph::NodeDataProvider& node,
530 SceneGraph::Shader& defaultShader,
531 const Matrix& modelMatrix,
532 const Matrix& modelViewMatrix,
533 const Matrix& viewMatrix,
534 const Matrix& projectionMatrix,
538 // Get the program to use:
539 Program* program = mRenderDataProvider->GetShader().GetProgram();
542 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
543 program = defaultShader.GetProgram();
544 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
547 DALI_LOG_ERROR( "Failed to get program for shader at address %p.\n", (void*)&mRenderDataProvider->GetShader() );
553 context.CullFace( mFaceCullingMode );
556 SetBlending( context, blend );
558 // Take the program into use so we can send uniforms to it
561 if( DALI_LIKELY( BindTextures( context, *program ) ) )
563 // Only set up and draw if we have textures and they are all valid
565 // set projection and view matrix if program has not yet received them yet this frame
566 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
569 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
570 if( Program::UNIFORM_UNKNOWN != loc )
572 const Vector4& color = node.GetRenderColor( bufferIndex );
573 if( mPremultipledAlphaEnabled )
575 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
579 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
583 SetUniforms( bufferIndex, node, size, *program );
585 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
587 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
588 mUpdateAttributesLocation = false;
591 mGeometry->UploadAndDraw( context,
594 mIndexedDrawFirstElement,
595 mIndexedDrawElementsCount );
599 void Renderer::SetSortAttributes( BufferIndex bufferIndex,
600 SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const
602 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
603 sortAttributes.geometry = mGeometry;
606 } // namespace SceneGraph
608 } // namespace Internal