2 * Copyright (c) 2017 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 );
155 mBlendingOptions.SetBlendColor( blendColor );
158 void Renderer::Initialize( Context& context )
163 Renderer::~Renderer()
167 void Renderer::SetRenderDataProvider( OwnerPointer<SceneGraph::RenderDataProvider>& dataProvider )
169 mRenderDataProvider = dataProvider;
170 mUpdateAttributesLocation = true;
172 // Check that the number of textures match the number of samplers in the shader
173 size_t textureCount = mRenderDataProvider->GetTextures().size();
174 Program* program = mRenderDataProvider->GetShader().GetProgram();
175 if( program && program->GetActiveSamplerCount() != textureCount )
177 DALI_LOG_WARNING("The number of active samplers in the shader(%lu) does not match the number of textures in the TextureSet(%lu)\n",
178 program->GetActiveSamplerCount(),
183 void Renderer::SetGeometry( Render::Geometry* geometry )
185 mGeometry = geometry;
186 mUpdateAttributesLocation = true;
189 void Renderer::SetBlending( Context& context, bool blend )
191 context.SetBlend( blend );
194 // Blend color is optional and rarely used
195 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
198 context.SetCustomBlendColor( *blendColor );
202 context.SetDefaultBlendColor();
205 // Set blend source & destination factors
206 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
207 mBlendingOptions.GetBlendDestFactorRgb(),
208 mBlendingOptions.GetBlendSrcFactorAlpha(),
209 mBlendingOptions.GetBlendDestFactorAlpha() );
211 // Set blend equations
212 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
213 mBlendingOptions.GetBlendEquationAlpha() );
217 void Renderer::GlContextDestroyed()
219 mGeometry->GlContextDestroyed();
222 void Renderer::GlCleanup()
226 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
228 // Check if the map has changed
229 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
231 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
233 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
234 node.GetUniformMapChanged(bufferIndex))
236 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
237 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
239 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
240 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
241 mUniformIndexMap.Resize( maxMaps );
243 unsigned int mapIndex(0);
244 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
246 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
247 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
250 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
252 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
254 for( unsigned int i(0); i<uniformMap.Count(); ++i )
256 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
258 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
266 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
267 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
272 mUniformIndexMap.Resize( mapIndex );
275 // Set uniforms in local map
276 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
277 end = mUniformIndexMap.End() ;
281 SetUniformFromProperty( bufferIndex, program, *iter );
284 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
287 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
291 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
293 GLint location = program.GetUniformLocation(map.uniformIndex);
294 if( Program::UNIFORM_UNKNOWN != location )
296 // switch based on property type to use correct GL uniform setter
297 switch ( map.propertyValue->GetType() )
299 case Property::INTEGER:
301 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
304 case Property::FLOAT:
306 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
309 case Property::VECTOR2:
311 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
312 program.SetUniform2f( location, value.x, value.y );
316 case Property::VECTOR3:
318 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
319 program.SetUniform3f( location, value.x, value.y, value.z );
323 case Property::VECTOR4:
325 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
326 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
330 case Property::ROTATION:
332 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
333 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
337 case Property::MATRIX:
339 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
340 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
344 case Property::MATRIX3:
346 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
347 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
353 // Other property types are ignored
360 bool Renderer::BindTextures( Context& context, Program& program )
362 unsigned int textureUnit = 0;
365 GLint uniformLocation(-1);
366 std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
367 std::vector<Render::Texture*>& textures( mRenderDataProvider->GetTextures() );
368 for( size_t i(0); i<textures.size() && result; ++i )
372 result = textures[i]->Bind(context, textureUnit, samplers[i] );
373 if( result && program.GetSamplerUniformLocation( i, uniformLocation ) )
375 program.SetUniform1i( uniformLocation, textureUnit );
384 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
386 mFaceCullingMode = mode;
389 void Renderer::SetBlendingBitMask( unsigned int bitmask )
391 mBlendingOptions.SetBitmask( bitmask );
394 void Renderer::SetBlendColor( const Vector4& color )
396 mBlendingOptions.SetBlendColor( color );
399 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
401 mIndexedDrawFirstElement = firstElement;
404 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
406 mIndexedDrawElementsCount = elementsCount;
409 void Renderer::EnablePreMultipliedAlpha( bool enable )
411 mPremultipledAlphaEnabled = enable;
414 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
416 mDepthWriteMode = depthWriteMode;
419 void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
421 mDepthTestMode = depthTestMode;
424 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
426 return mDepthWriteMode;
429 DepthTestMode::Type Renderer::GetDepthTestMode() const
431 return mDepthTestMode;
434 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
436 mDepthFunction = depthFunction;
439 DepthFunction::Type Renderer::GetDepthFunction() const
441 return mDepthFunction;
444 void Renderer::SetRenderMode( RenderMode::Type renderMode )
446 mStencilParameters.renderMode = renderMode;
449 RenderMode::Type Renderer::GetRenderMode() const
451 return mStencilParameters.renderMode;
454 void Renderer::SetStencilFunction( StencilFunction::Type stencilFunction )
456 mStencilParameters.stencilFunction = stencilFunction;
459 StencilFunction::Type Renderer::GetStencilFunction() const
461 return mStencilParameters.stencilFunction;
464 void Renderer::SetStencilFunctionMask( int stencilFunctionMask )
466 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
469 int Renderer::GetStencilFunctionMask() const
471 return mStencilParameters.stencilFunctionMask;
474 void Renderer::SetStencilFunctionReference( int stencilFunctionReference )
476 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
479 int Renderer::GetStencilFunctionReference() const
481 return mStencilParameters.stencilFunctionReference;
484 void Renderer::SetStencilMask( int stencilMask )
486 mStencilParameters.stencilMask = stencilMask;
489 int Renderer::GetStencilMask() const
491 return mStencilParameters.stencilMask;
494 void Renderer::SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail )
496 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
499 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
501 return mStencilParameters.stencilOperationOnFail;
504 void Renderer::SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail )
506 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
509 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
511 return mStencilParameters.stencilOperationOnZFail;
514 void Renderer::SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass )
516 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
519 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
521 return mStencilParameters.stencilOperationOnZPass;
524 void Renderer::Render( Context& context,
525 BufferIndex bufferIndex,
526 const SceneGraph::NodeDataProvider& node,
527 const Matrix& modelMatrix,
528 const Matrix& modelViewMatrix,
529 const Matrix& viewMatrix,
530 const Matrix& projectionMatrix,
534 // Get the program to use:
535 Program* program = mRenderDataProvider->GetShader().GetProgram();
538 DALI_LOG_ERROR( "Failed to get program for shader at address %p.\n", reinterpret_cast< void* >( &mRenderDataProvider->GetShader() ) );
543 context.CullFace( mFaceCullingMode );
546 SetBlending( context, blend );
548 // Take the program into use so we can send uniforms to it
551 if( DALI_LIKELY( BindTextures( context, *program ) ) )
553 // Only set up and draw if we have textures and they are all valid
555 // set projection and view matrix if program has not yet received them yet this frame
556 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
559 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
560 if( Program::UNIFORM_UNKNOWN != loc )
562 const Vector4& color = node.GetRenderColor( bufferIndex );
563 if( mPremultipledAlphaEnabled )
565 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
569 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
573 SetUniforms( bufferIndex, node, size, *program );
575 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
577 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
578 mUpdateAttributesLocation = false;
581 mGeometry->UploadAndDraw( context,
584 mIndexedDrawFirstElement,
585 mIndexedDrawElementsCount );
589 void Renderer::SetSortAttributes( BufferIndex bufferIndex,
590 SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const
592 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
593 sortAttributes.geometry = mGeometry;
596 } // namespace SceneGraph
598 } // namespace Internal