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/data-providers/uniform-name-cache.h>
29 #include <dali/internal/render/gl-resources/texture-cache.h>
30 #include <dali/public-api/actors/blending.h>
31 #include <dali/internal/render/gl-resources/gl-texture.h>
42 static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it in object to reduce storage overhead
43 static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it in object to reduce storage overhead
46 * Helper to set view and projection matrices once per program
47 * @param program to set the matrices to
48 * @param modelMatrix to set
49 * @param viewMatrix to set
50 * @param projectionMatrix to set
51 * @param modelViewMatrix to set
52 * @param modelViewProjectionMatrix to set
54 inline void SetMatrices( Program& program,
55 const Matrix& modelMatrix,
56 const Matrix& viewMatrix,
57 const Matrix& projectionMatrix,
58 const Matrix& modelViewMatrix )
60 GLint loc = program.GetUniformLocation( Program::UNIFORM_MODEL_MATRIX );
61 if( Program::UNIFORM_UNKNOWN != loc )
63 program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
65 loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
66 if( Program::UNIFORM_UNKNOWN != loc )
68 if( program.GetViewMatrix() != &viewMatrix )
70 program.SetViewMatrix( &viewMatrix );
71 program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
74 // set projection matrix if program has not yet received it this frame or if it is dirty
75 loc = program.GetUniformLocation( Program::UNIFORM_PROJECTION_MATRIX );
76 if( Program::UNIFORM_UNKNOWN != loc )
78 if( program.GetProjectionMatrix() != &projectionMatrix )
80 program.SetProjectionMatrix( &projectionMatrix );
81 program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
84 loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
85 if( Program::UNIFORM_UNKNOWN != loc )
87 program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
90 loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
91 if( Program::UNIFORM_UNKNOWN != loc )
93 Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
94 program.SetUniformMatrix4fv( loc, 1, gModelViewProjectionMatrix.AsFloat() );
97 loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
98 if( Program::UNIFORM_UNKNOWN != loc )
100 gNormalMatrix = modelViewMatrix;
101 gNormalMatrix.Invert();
102 gNormalMatrix.Transpose();
103 program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
112 Renderer* Renderer::New( SceneGraph::RenderDataProvider* dataProvider,
113 Render::Geometry* geometry,
114 unsigned int blendingBitmask,
115 const Vector4* blendColor,
116 FaceCullingMode::Type faceCullingMode,
117 bool preMultipliedAlphaEnabled,
118 DepthWriteMode::Type depthWriteMode,
119 DepthTestMode::Type depthTestMode,
120 DepthFunction::Type depthFunction,
121 StencilParameters& stencilParameters,
122 bool writeToColorBuffer )
124 return new Renderer( dataProvider, geometry, blendingBitmask, blendColor,
125 faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode,
126 depthFunction, stencilParameters, writeToColorBuffer );
129 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
130 Render::Geometry* geometry,
131 unsigned int blendingBitmask,
132 const Vector4* blendColor,
133 FaceCullingMode::Type faceCullingMode,
134 bool preMultipliedAlphaEnabled,
135 DepthWriteMode::Type depthWriteMode,
136 DepthTestMode::Type depthTestMode,
137 DepthFunction::Type depthFunction,
138 StencilParameters& stencilParameters,
139 bool writeToColorBuffer )
140 : mRenderDataProvider( dataProvider ),
142 mTextureCache( NULL ),
143 mUniformNameCache( NULL ),
144 mGeometry( geometry ),
146 mAttributesLocation(),
147 mStencilParameters( stencilParameters ),
149 mIndexedDrawFirstElement( 0 ),
150 mIndexedDrawElementsCount( 0 ),
151 mDepthFunction( depthFunction ),
152 mFaceCullingMode( faceCullingMode ),
153 mDepthWriteMode( depthWriteMode ),
154 mDepthTestMode( depthTestMode ),
155 mWriteToColorBuffer( writeToColorBuffer ),
156 mUpdateAttributesLocation( true ),
157 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
159 if( blendingBitmask != 0u )
161 mBlendingOptions.SetBitmask( blendingBitmask );
166 mBlendingOptions.SetBlendColor( *blendColor );
170 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache )
173 mTextureCache = &textureCache;
174 mUniformNameCache = &uniformNameCache;
177 Renderer::~Renderer()
181 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
183 mRenderDataProvider = dataProvider;
184 mUpdateAttributesLocation = true;
187 void Renderer::SetGeometry( Render::Geometry* geometry )
189 mGeometry = geometry;
190 mUpdateAttributesLocation = true;
193 void Renderer::SetBlending( Context& context, bool blend )
195 context.SetBlend( blend );
198 // Blend color is optional and rarely used
199 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
202 context.SetCustomBlendColor( *blendColor );
206 context.SetDefaultBlendColor();
209 // Set blend source & destination factors
210 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
211 mBlendingOptions.GetBlendDestFactorRgb(),
212 mBlendingOptions.GetBlendSrcFactorAlpha(),
213 mBlendingOptions.GetBlendDestFactorAlpha() );
215 // Set blend equations
216 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
217 mBlendingOptions.GetBlendEquationAlpha() );
221 void Renderer::GlContextDestroyed()
223 mGeometry->GlContextDestroyed();
226 void Renderer::GlCleanup()
230 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
232 // Check if the map has changed
233 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
235 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
237 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
238 node.GetUniformMapChanged(bufferIndex))
240 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
241 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
243 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
244 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
245 mUniformIndexMap.Resize( maxMaps );
247 unsigned int mapIndex(0);
248 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
250 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
251 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
254 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
256 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
258 for( unsigned int i(0); i<uniformMap.Count(); ++i )
260 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
262 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
270 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
271 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
276 mUniformIndexMap.Resize( mapIndex );
279 // Set uniforms in local map
280 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
281 end = mUniformIndexMap.End() ;
285 SetUniformFromProperty( bufferIndex, program, *iter );
288 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
291 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
295 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
297 GLint location = program.GetUniformLocation(map.uniformIndex);
298 if( Program::UNIFORM_UNKNOWN != location )
300 // switch based on property type to use correct GL uniform setter
301 switch ( map.propertyValue->GetType() )
303 case Property::INTEGER:
305 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
308 case Property::FLOAT:
310 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
313 case Property::VECTOR2:
315 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
316 program.SetUniform2f( location, value.x, value.y );
320 case Property::VECTOR3:
322 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
323 program.SetUniform3f( location, value.x, value.y, value.z );
327 case Property::VECTOR4:
329 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
330 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
334 case Property::ROTATION:
336 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
337 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
341 case Property::MATRIX:
343 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
344 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
348 case Property::MATRIX3:
350 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
351 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
357 // Other property types are ignored
364 bool Renderer::BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program )
366 unsigned int textureUnit = 0;
369 std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
371 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
372 GLint uniformLocation(-1);
373 for( size_t i(0); result && i<textures.size(); ++i )
375 ResourceId textureId = textures[i].GetTextureId();
376 Internal::Texture* texture = textureCache.GetTexture( textureId );
379 result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
383 GLint uniformLocation;
385 //TODO : This is a bug, result variable is being shadowed. Fix it!
386 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
387 if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
389 program.SetUniform1i( uniformLocation, textureUnit );
391 unsigned int samplerBitfield(ImageSampler::DEFAULT_BITFIELD);
392 const Render::Sampler* sampler( samplers[i] );
395 samplerBitfield = sampler->mBitfield;
398 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
406 std::vector<Render::NewTexture*>& newTextures( mRenderDataProvider->GetNewTextures() );
407 for( size_t i(0); result && i<newTextures.size(); ++i )
411 result = program.GetSamplerUniformLocation( i, uniformLocation ) &&
412 newTextures[i]->Bind(context, textureUnit, samplers[i] );
416 program.SetUniform1i( uniformLocation, textureUnit );
425 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
427 mFaceCullingMode = mode;
430 void Renderer::SetBlendingBitMask( unsigned int bitmask )
432 mBlendingOptions.SetBitmask( bitmask );
435 void Renderer::SetBlendColor( const Vector4* color )
437 mBlendingOptions.SetBlendColor( *color );
440 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
442 mIndexedDrawFirstElement = firstElement;
445 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
447 mIndexedDrawElementsCount = elementsCount;
450 void Renderer::EnablePreMultipliedAlpha( bool enable )
452 mPremultipledAlphaEnabled = enable;
455 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
457 mDepthWriteMode = depthWriteMode;
460 void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
462 mDepthTestMode = depthTestMode;
465 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
467 return mDepthWriteMode;
470 DepthTestMode::Type Renderer::GetDepthTestMode() const
472 return mDepthTestMode;
475 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
477 mDepthFunction = depthFunction;
480 DepthFunction::Type Renderer::GetDepthFunction() const
482 return mDepthFunction;
485 void Renderer::SetStencilMode( StencilMode::Type stencilMode )
487 mStencilParameters.stencilMode = stencilMode;
490 StencilMode::Type Renderer::GetStencilMode() const
492 return mStencilParameters.stencilMode;
495 void Renderer::SetStencilFunction( StencilFunction::Type stencilFunction )
497 mStencilParameters.stencilFunction = stencilFunction;
500 StencilFunction::Type Renderer::GetStencilFunction() const
502 return mStencilParameters.stencilFunction;
505 void Renderer::SetStencilFunctionMask( int stencilFunctionMask )
507 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
510 int Renderer::GetStencilFunctionMask() const
512 return mStencilParameters.stencilFunctionMask;
515 void Renderer::SetStencilFunctionReference( int stencilFunctionReference )
517 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
520 int Renderer::GetStencilFunctionReference() const
522 return mStencilParameters.stencilFunctionReference;
525 void Renderer::SetStencilMask( int stencilMask )
527 mStencilParameters.stencilMask = stencilMask;
530 int Renderer::GetStencilMask() const
532 return mStencilParameters.stencilMask;
535 void Renderer::SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail )
537 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
540 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
542 return mStencilParameters.stencilOperationOnFail;
545 void Renderer::SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail )
547 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
550 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
552 return mStencilParameters.stencilOperationOnZFail;
555 void Renderer::SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass )
557 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
560 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
562 return mStencilParameters.stencilOperationOnZPass;
565 void Renderer::SetWriteToColorBuffer( bool writeToColorBuffer )
567 mWriteToColorBuffer = writeToColorBuffer;
570 bool Renderer::GetWriteToColorBuffer() const
572 return mWriteToColorBuffer;
575 void Renderer::Render( Context& context,
576 SceneGraph::TextureCache& textureCache,
577 BufferIndex bufferIndex,
578 const SceneGraph::NodeDataProvider& node,
579 SceneGraph::Shader& defaultShader,
580 const Matrix& modelMatrix,
581 const Matrix& modelViewMatrix,
582 const Matrix& viewMatrix,
583 const Matrix& projectionMatrix,
587 // Get the program to use:
588 Program* program = mRenderDataProvider->GetShader().GetProgram();
591 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
592 program = defaultShader.GetProgram();
593 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
596 DALI_LOG_ERROR( "Failed to get program for shader at address %p.\n", (void*)&mRenderDataProvider->GetShader() );
602 context.CullFace( mFaceCullingMode );
605 SetBlending( context, blend );
607 // Take the program into use so we can send uniforms to it
610 if( DALI_LIKELY( BindTextures( context, textureCache, *program ) ) )
612 // Only set up and draw if we have textures and they are all valid
614 // set projection and view matrix if program has not yet received them yet this frame
615 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
618 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
619 if( Program::UNIFORM_UNKNOWN != loc )
621 const Vector4& color = node.GetRenderColor( bufferIndex );
622 if( mPremultipledAlphaEnabled )
624 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
628 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
632 SetUniforms( bufferIndex, node, size, *program );
634 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
636 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
637 mUpdateAttributesLocation = false;
640 mGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation, mIndexedDrawFirstElement, mIndexedDrawElementsCount );
644 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
646 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
647 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
648 if( !textures.empty() )
650 sortAttributes.textureResourceId = textures[0].GetTextureId();
654 sortAttributes.textureResourceId = Integration::InvalidResourceId;
657 sortAttributes.geometry = mGeometry;
660 } // namespace SceneGraph
662 } // namespace Internal