2 * Copyright (c) 2019 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>
28 #include <dali/internal/render/common/render-instruction.h>
41 * Helper to set view and projection matrices once per program
42 * @param program to set the matrices to
43 * @param modelMatrix to set
44 * @param viewMatrix to set
45 * @param projectionMatrix to set
46 * @param modelViewMatrix to set
47 * @param modelViewProjectionMatrix to set
49 inline void SetMatrices( Program& program,
50 const Matrix& modelMatrix,
51 const Matrix& viewMatrix,
52 const Matrix& projectionMatrix,
53 const Matrix& modelViewMatrix )
55 GLint loc = program.GetUniformLocation( Program::UNIFORM_MODEL_MATRIX );
56 if( Program::UNIFORM_UNKNOWN != loc )
58 program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
60 loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
61 if( Program::UNIFORM_UNKNOWN != loc )
63 if( program.GetViewMatrix() != &viewMatrix )
65 program.SetViewMatrix( &viewMatrix );
66 program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
69 // set projection matrix if program has not yet received it this frame or if it is dirty
70 loc = program.GetUniformLocation( Program::UNIFORM_PROJECTION_MATRIX );
71 if( Program::UNIFORM_UNKNOWN != loc )
73 if( program.GetProjectionMatrix() != &projectionMatrix )
75 program.SetProjectionMatrix( &projectionMatrix );
76 program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
79 loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
80 if( Program::UNIFORM_UNKNOWN != loc )
82 program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
85 loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
86 if( Program::UNIFORM_UNKNOWN != loc )
88 Matrix modelViewProjectionMatrix(false);
89 Matrix::Multiply( modelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
90 program.SetUniformMatrix4fv( loc, 1, modelViewProjectionMatrix.AsFloat() );
93 loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
94 if( Program::UNIFORM_UNKNOWN != loc )
97 normalMatrix = modelViewMatrix;
98 normalMatrix.Invert();
99 normalMatrix.Transpose();
100 program.SetUniformMatrix3fv( loc, 1, normalMatrix.AsFloat() );
109 Renderer* Renderer::New( SceneGraph::RenderDataProvider* dataProvider,
110 Render::Geometry* geometry,
111 uint32_t blendingBitmask,
112 const Vector4& blendColor,
113 FaceCullingMode::Type faceCullingMode,
114 bool preMultipliedAlphaEnabled,
115 DepthWriteMode::Type depthWriteMode,
116 DepthTestMode::Type depthTestMode,
117 DepthFunction::Type depthFunction,
118 StencilParameters& stencilParameters )
120 return new Renderer( dataProvider, geometry, blendingBitmask, blendColor,
121 faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode,
122 depthFunction, stencilParameters );
125 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
126 Render::Geometry* geometry,
127 uint32_t blendingBitmask,
128 const Vector4& blendColor,
129 FaceCullingMode::Type faceCullingMode,
130 bool preMultipliedAlphaEnabled,
131 DepthWriteMode::Type depthWriteMode,
132 DepthTestMode::Type depthTestMode,
133 DepthFunction::Type depthFunction,
134 StencilParameters& stencilParameters )
135 : mRenderDataProvider( dataProvider ),
137 mGeometry( geometry ),
139 mAttributesLocation(),
140 mStencilParameters( stencilParameters ),
142 mIndexedDrawFirstElement( 0 ),
143 mIndexedDrawElementsCount( 0 ),
144 mDepthFunction( depthFunction ),
145 mFaceCullingMode( faceCullingMode ),
146 mDepthWriteMode( depthWriteMode ),
147 mDepthTestMode( depthTestMode ),
148 mUpdateAttributesLocation( true ),
149 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled ),
150 mShaderChanged( false )
152 if( blendingBitmask != 0u )
154 mBlendingOptions.SetBitmask( blendingBitmask );
157 mBlendingOptions.SetBlendColor( blendColor );
160 void Renderer::Initialize( Context& context )
165 Renderer::~Renderer()
169 void Renderer::SetGeometry( Render::Geometry* geometry )
171 mGeometry = geometry;
172 mUpdateAttributesLocation = true;
175 void Renderer::SetBlending( Context& context, bool blend )
177 context.SetBlend( blend );
180 // Blend color is optional and rarely used
181 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
184 context.SetCustomBlendColor( *blendColor );
188 context.SetDefaultBlendColor();
191 // Set blend source & destination factors
192 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
193 mBlendingOptions.GetBlendDestFactorRgb(),
194 mBlendingOptions.GetBlendSrcFactorAlpha(),
195 mBlendingOptions.GetBlendDestFactorAlpha() );
197 // Set blend equations
198 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
199 mBlendingOptions.GetBlendEquationAlpha() );
203 void Renderer::GlContextDestroyed()
205 mGeometry->GlContextDestroyed();
208 void Renderer::GlCleanup()
212 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
214 // Check if the map has changed
215 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
217 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
219 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
220 node.GetUniformMapChanged(bufferIndex) ||
221 mUniformIndexMap.Count() == 0 ||
224 // Reset shader pointer
225 mShaderChanged = false;
227 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
228 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
230 uint32_t maxMaps = static_cast<uint32_t>( uniformMap.Count() + uniformMapNode.Count() ); // 4,294,967,295 maps should be enough
231 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
232 mUniformIndexMap.Resize( maxMaps );
234 uint32_t 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( uint32_t nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
243 uint32_t uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
245 for( uint32_t 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( Context& context, Program& program, Vector<GLuint>& boundTextures )
353 uint32_t textureUnit = 0;
356 GLint uniformLocation(-1);
357 std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
358 std::vector<Render::Texture*>& textures( mRenderDataProvider->GetTextures() );
359 for( uint32_t i = 0; i < static_cast<uint32_t>( textures.size() ) && result; ++i ) // not expecting more than uint32_t of textures
363 result = textures[i]->Bind(context, textureUnit, samplers[i] );
364 boundTextures.PushBack( textures[i]->GetId() );
365 if( result && program.GetSamplerUniformLocation( i, uniformLocation ) )
367 program.SetUniform1i( uniformLocation, textureUnit );
376 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
378 mFaceCullingMode = mode;
381 void Renderer::SetBlendingBitMask( uint32_t bitmask )
383 mBlendingOptions.SetBitmask( bitmask );
386 void Renderer::SetBlendColor( const Vector4& color )
388 mBlendingOptions.SetBlendColor( color );
391 void Renderer::SetIndexedDrawFirstElement( uint32_t firstElement )
393 mIndexedDrawFirstElement = firstElement;
396 void Renderer::SetIndexedDrawElementsCount( uint32_t elementsCount )
398 mIndexedDrawElementsCount = elementsCount;
401 void Renderer::EnablePreMultipliedAlpha( bool enable )
403 mPremultipledAlphaEnabled = enable;
406 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
408 mDepthWriteMode = depthWriteMode;
411 void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
413 mDepthTestMode = depthTestMode;
416 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
418 return mDepthWriteMode;
421 DepthTestMode::Type Renderer::GetDepthTestMode() const
423 return mDepthTestMode;
426 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
428 mDepthFunction = depthFunction;
431 DepthFunction::Type Renderer::GetDepthFunction() const
433 return mDepthFunction;
436 void Renderer::SetRenderMode( RenderMode::Type renderMode )
438 mStencilParameters.renderMode = renderMode;
441 RenderMode::Type Renderer::GetRenderMode() const
443 return mStencilParameters.renderMode;
446 void Renderer::SetStencilFunction( StencilFunction::Type stencilFunction )
448 mStencilParameters.stencilFunction = stencilFunction;
451 StencilFunction::Type Renderer::GetStencilFunction() const
453 return mStencilParameters.stencilFunction;
456 void Renderer::SetStencilFunctionMask( int stencilFunctionMask )
458 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
461 int Renderer::GetStencilFunctionMask() const
463 return mStencilParameters.stencilFunctionMask;
466 void Renderer::SetStencilFunctionReference( int stencilFunctionReference )
468 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
471 int Renderer::GetStencilFunctionReference() const
473 return mStencilParameters.stencilFunctionReference;
476 void Renderer::SetStencilMask( int stencilMask )
478 mStencilParameters.stencilMask = stencilMask;
481 int Renderer::GetStencilMask() const
483 return mStencilParameters.stencilMask;
486 void Renderer::SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail )
488 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
491 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
493 return mStencilParameters.stencilOperationOnFail;
496 void Renderer::SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail )
498 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
501 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
503 return mStencilParameters.stencilOperationOnZFail;
506 void Renderer::SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass )
508 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
511 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
513 return mStencilParameters.stencilOperationOnZPass;
516 void Renderer::Upload( Context& context )
518 mGeometry->Upload( context );
521 void Renderer::Render( Context& context,
522 BufferIndex bufferIndex,
523 const SceneGraph::NodeDataProvider& node,
524 const Matrix& modelMatrix,
525 const Matrix& modelViewMatrix,
526 const Matrix& viewMatrix,
527 const Matrix& projectionMatrix,
530 Vector<GLuint>& boundTextures,
531 const Dali::Internal::SceneGraph::RenderInstruction& instruction )
533 // Get the program to use:
534 Program* program = mRenderDataProvider->GetShader().GetProgram();
537 DALI_LOG_ERROR( "Failed to get program for shader at address %p.\n", reinterpret_cast< void* >( &mRenderDataProvider->GetShader() ) );
542 const Dali::Internal::SceneGraph::Camera* cam = instruction.GetCamera();
543 if (cam->GetReflectionUsed())
545 auto adjFaceCullingMode = mFaceCullingMode;
546 switch( mFaceCullingMode )
548 case FaceCullingMode::Type::FRONT:
550 adjFaceCullingMode = FaceCullingMode::Type::BACK;
553 case FaceCullingMode::Type::BACK:
555 adjFaceCullingMode = FaceCullingMode::Type::FRONT;
560 // nothing to do, leave culling as it is
563 context.CullFace( adjFaceCullingMode );
567 context.CullFace( mFaceCullingMode );
571 SetBlending( context, blend );
573 // Take the program into use so we can send uniforms to it
576 if( DALI_LIKELY( BindTextures( context, *program, boundTextures ) ) )
578 // Only set up and draw if we have textures and they are all valid
580 // set projection and view matrix if program has not yet received them yet this frame
581 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
584 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
585 if( Program::UNIFORM_UNKNOWN != loc )
587 const Vector4& color = node.GetRenderColor( bufferIndex );
588 if( mPremultipledAlphaEnabled )
590 float alpha = color.a * mRenderDataProvider->GetOpacity( bufferIndex );
591 program->SetUniform4f( loc, color.r * alpha, color.g * alpha, color.b * alpha, alpha );
595 program->SetUniform4f( loc, color.r, color.g, color.b, color.a * mRenderDataProvider->GetOpacity( bufferIndex ) );
599 SetUniforms( bufferIndex, node, size, *program );
601 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
603 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
604 mUpdateAttributesLocation = false;
607 mGeometry->Draw( context,
610 mIndexedDrawFirstElement,
611 mIndexedDrawElementsCount );
615 void Renderer::SetSortAttributes( BufferIndex bufferIndex,
616 SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const
618 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
619 sortAttributes.geometry = mGeometry;
622 void Renderer::SetShaderChanged( bool value )
624 mShaderChanged = value;
627 } // namespace SceneGraph
629 } // namespace Internal