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>
28 #include <dali/internal/render/gl-resources/texture-cache.h>
29 #include <dali/public-api/actors/blending.h>
30 #include <dali/internal/render/gl-resources/gl-texture.h>
41 static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it in object to reduce storage overhead
42 static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it in object to reduce storage overhead
45 * Helper to set view and projection matrices once per program
46 * @param program to set the matrices to
47 * @param modelMatrix to set
48 * @param viewMatrix to set
49 * @param projectionMatrix to set
50 * @param modelViewMatrix to set
51 * @param modelViewProjectionMatrix to set
53 inline void SetMatrices( Program& program,
54 const Matrix& modelMatrix,
55 const Matrix& viewMatrix,
56 const Matrix& projectionMatrix,
57 const Matrix& modelViewMatrix )
59 GLint loc = program.GetUniformLocation( Program::UNIFORM_MODEL_MATRIX );
60 if( Program::UNIFORM_UNKNOWN != loc )
62 program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
64 loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
65 if( Program::UNIFORM_UNKNOWN != loc )
67 if( program.GetViewMatrix() != &viewMatrix )
69 program.SetViewMatrix( &viewMatrix );
70 program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
73 // set projection matrix if program has not yet received it this frame or if it is dirty
74 loc = program.GetUniformLocation( Program::UNIFORM_PROJECTION_MATRIX );
75 if( Program::UNIFORM_UNKNOWN != loc )
77 if( program.GetProjectionMatrix() != &projectionMatrix )
79 program.SetProjectionMatrix( &projectionMatrix );
80 program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
83 loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
84 if( Program::UNIFORM_UNKNOWN != loc )
86 program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
89 loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
90 if( Program::UNIFORM_UNKNOWN != loc )
92 Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
93 program.SetUniformMatrix4fv( loc, 1, gModelViewProjectionMatrix.AsFloat() );
96 loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
97 if( Program::UNIFORM_UNKNOWN != loc )
99 gNormalMatrix = modelViewMatrix;
100 gNormalMatrix.Invert();
101 gNormalMatrix.Transpose();
102 program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
111 Renderer* Renderer::New( SceneGraph::RenderDataProvider* dataProvider,
112 Render::Geometry* geometry,
113 unsigned int blendingBitmask,
114 const Vector4* blendColor,
115 FaceCullingMode::Type faceCullingMode,
116 bool preMultipliedAlphaEnabled,
117 DepthWriteMode::Type depthWriteMode,
118 DepthTestMode::Type depthTestMode,
119 DepthFunction::Type depthFunction,
120 StencilParameters& stencilParameters )
122 return new Renderer( dataProvider, geometry, blendingBitmask, blendColor,
123 faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode,
124 depthFunction, stencilParameters );
127 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
128 Render::Geometry* geometry,
129 unsigned int blendingBitmask,
130 const Vector4* blendColor,
131 FaceCullingMode::Type faceCullingMode,
132 bool preMultipliedAlphaEnabled,
133 DepthWriteMode::Type depthWriteMode,
134 DepthTestMode::Type depthTestMode,
135 DepthFunction::Type depthFunction,
136 StencilParameters& stencilParameters )
137 : mRenderDataProvider( dataProvider ),
139 mTextureCache( NULL ),
140 mGeometry( geometry ),
142 mAttributesLocation(),
143 mStencilParameters( stencilParameters ),
145 mIndexedDrawFirstElement( 0 ),
146 mIndexedDrawElementsCount( 0 ),
147 mDepthFunction( depthFunction ),
148 mFaceCullingMode( faceCullingMode ),
149 mDepthWriteMode( depthWriteMode ),
150 mDepthTestMode( depthTestMode ),
151 mUpdateAttributesLocation( true ),
152 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
154 if( blendingBitmask != 0u )
156 mBlendingOptions.SetBitmask( blendingBitmask );
161 mBlendingOptions.SetBlendColor( *blendColor );
165 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache )
168 mTextureCache = &textureCache;
171 Renderer::~Renderer()
175 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
177 mRenderDataProvider = dataProvider;
178 mUpdateAttributesLocation = true;
181 void Renderer::SetGeometry( Render::Geometry* geometry )
183 mGeometry = geometry;
184 mUpdateAttributesLocation = true;
187 void Renderer::SetBlending( Context& context, bool blend )
189 context.SetBlend( blend );
192 // Blend color is optional and rarely used
193 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
196 context.SetCustomBlendColor( *blendColor );
200 context.SetDefaultBlendColor();
203 // Set blend source & destination factors
204 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
205 mBlendingOptions.GetBlendDestFactorRgb(),
206 mBlendingOptions.GetBlendSrcFactorAlpha(),
207 mBlendingOptions.GetBlendDestFactorAlpha() );
209 // Set blend equations
210 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
211 mBlendingOptions.GetBlendEquationAlpha() );
215 void Renderer::GlContextDestroyed()
217 mGeometry->GlContextDestroyed();
220 void Renderer::GlCleanup()
224 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
226 // Check if the map has changed
227 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
229 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
231 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
232 node.GetUniformMapChanged(bufferIndex))
234 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
235 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
237 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
238 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
239 mUniformIndexMap.Resize( maxMaps );
241 unsigned int mapIndex(0);
242 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
244 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
245 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
248 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
250 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
252 for( unsigned int i(0); i<uniformMap.Count(); ++i )
254 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
256 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
264 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
265 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
270 mUniformIndexMap.Resize( mapIndex );
273 // Set uniforms in local map
274 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
275 end = mUniformIndexMap.End() ;
279 SetUniformFromProperty( bufferIndex, program, *iter );
282 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
285 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
289 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
291 GLint location = program.GetUniformLocation(map.uniformIndex);
292 if( Program::UNIFORM_UNKNOWN != location )
294 // switch based on property type to use correct GL uniform setter
295 switch ( map.propertyValue->GetType() )
297 case Property::INTEGER:
299 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
302 case Property::FLOAT:
304 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
307 case Property::VECTOR2:
309 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
310 program.SetUniform2f( location, value.x, value.y );
314 case Property::VECTOR3:
316 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
317 program.SetUniform3f( location, value.x, value.y, value.z );
321 case Property::VECTOR4:
323 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
324 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
328 case Property::ROTATION:
330 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
331 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
335 case Property::MATRIX:
337 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
338 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
342 case Property::MATRIX3:
344 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
345 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
351 // Other property types are ignored
358 bool Renderer::BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program )
360 unsigned int textureUnit = 0;
363 std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
365 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
366 GLint uniformLocation(-1);
367 for( size_t i(0); result && i<textures.size(); ++i )
369 ResourceId textureId = textures[i].GetTextureId();
370 Internal::Texture* texture = textureCache.GetTexture( textureId );
373 result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
377 GLint uniformLocation;
379 //TODO : This is a bug, result variable is being shadowed. Fix it!
380 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
381 if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
383 program.SetUniform1i( uniformLocation, textureUnit );
385 unsigned int samplerBitfield(ImageSampler::DEFAULT_BITFIELD);
386 const Render::Sampler* sampler( samplers[i] );
389 samplerBitfield = sampler->mBitfield;
392 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
400 std::vector<Render::NewTexture*>& newTextures( mRenderDataProvider->GetNewTextures() );
401 for( size_t i(0); result && i<newTextures.size(); ++i )
405 result = program.GetSamplerUniformLocation( i, uniformLocation ) &&
406 newTextures[i]->Bind(context, textureUnit, samplers[i] );
410 program.SetUniform1i( uniformLocation, textureUnit );
419 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
421 mFaceCullingMode = mode;
424 void Renderer::SetBlendingBitMask( unsigned int bitmask )
426 mBlendingOptions.SetBitmask( bitmask );
429 void Renderer::SetBlendColor( const Vector4* color )
431 mBlendingOptions.SetBlendColor( *color );
434 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
436 mIndexedDrawFirstElement = firstElement;
439 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
441 mIndexedDrawElementsCount = elementsCount;
444 void Renderer::EnablePreMultipliedAlpha( bool enable )
446 mPremultipledAlphaEnabled = enable;
449 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
451 mDepthWriteMode = depthWriteMode;
454 void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
456 mDepthTestMode = depthTestMode;
459 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
461 return mDepthWriteMode;
464 DepthTestMode::Type Renderer::GetDepthTestMode() const
466 return mDepthTestMode;
469 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
471 mDepthFunction = depthFunction;
474 DepthFunction::Type Renderer::GetDepthFunction() const
476 return mDepthFunction;
479 void Renderer::SetRenderMode( RenderMode::Type renderMode )
481 mStencilParameters.renderMode = renderMode;
484 RenderMode::Type Renderer::GetRenderMode() const
486 return mStencilParameters.renderMode;
489 void Renderer::SetStencilFunction( StencilFunction::Type stencilFunction )
491 mStencilParameters.stencilFunction = stencilFunction;
494 StencilFunction::Type Renderer::GetStencilFunction() const
496 return mStencilParameters.stencilFunction;
499 void Renderer::SetStencilFunctionMask( int stencilFunctionMask )
501 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
504 int Renderer::GetStencilFunctionMask() const
506 return mStencilParameters.stencilFunctionMask;
509 void Renderer::SetStencilFunctionReference( int stencilFunctionReference )
511 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
514 int Renderer::GetStencilFunctionReference() const
516 return mStencilParameters.stencilFunctionReference;
519 void Renderer::SetStencilMask( int stencilMask )
521 mStencilParameters.stencilMask = stencilMask;
524 int Renderer::GetStencilMask() const
526 return mStencilParameters.stencilMask;
529 void Renderer::SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail )
531 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
534 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
536 return mStencilParameters.stencilOperationOnFail;
539 void Renderer::SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail )
541 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
544 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
546 return mStencilParameters.stencilOperationOnZFail;
549 void Renderer::SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass )
551 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
554 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
556 return mStencilParameters.stencilOperationOnZPass;
559 void Renderer::Render( Context& context,
560 SceneGraph::TextureCache& textureCache,
561 BufferIndex bufferIndex,
562 const SceneGraph::NodeDataProvider& node,
563 SceneGraph::Shader& defaultShader,
564 const Matrix& modelMatrix,
565 const Matrix& modelViewMatrix,
566 const Matrix& viewMatrix,
567 const Matrix& projectionMatrix,
571 // Get the program to use:
572 Program* program = mRenderDataProvider->GetShader().GetProgram();
575 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
576 program = defaultShader.GetProgram();
577 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
580 DALI_LOG_ERROR( "Failed to get program for shader at address %p.\n", (void*)&mRenderDataProvider->GetShader() );
586 context.CullFace( mFaceCullingMode );
589 SetBlending( context, blend );
591 // Take the program into use so we can send uniforms to it
594 if( DALI_LIKELY( BindTextures( context, textureCache, *program ) ) )
596 // Only set up and draw if we have textures and they are all valid
598 // set projection and view matrix if program has not yet received them yet this frame
599 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
602 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
603 if( Program::UNIFORM_UNKNOWN != loc )
605 const Vector4& color = node.GetRenderColor( bufferIndex );
606 if( mPremultipledAlphaEnabled )
608 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
612 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
616 SetUniforms( bufferIndex, node, size, *program );
618 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
620 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
621 mUpdateAttributesLocation = false;
624 mGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation, mIndexedDrawFirstElement, mIndexedDrawElementsCount );
628 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
630 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
631 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
632 if( !textures.empty() )
634 sortAttributes.textureResourceId = textures[0].GetTextureId();
638 sortAttributes.textureResourceId = Integration::InvalidResourceId;
641 sortAttributes.geometry = mGeometry;
644 } // namespace SceneGraph
646 } // namespace Internal