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 ),
153 mBatchingEnabled( false )
155 if( blendingBitmask != 0u )
157 mBlendingOptions.SetBitmask( blendingBitmask );
162 mBlendingOptions.SetBlendColor( *blendColor );
166 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache )
169 mTextureCache = &textureCache;
172 Renderer::~Renderer()
176 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
178 mRenderDataProvider = dataProvider;
179 mUpdateAttributesLocation = true;
182 void Renderer::SetGeometry( Render::Geometry* geometry )
184 mGeometry = geometry;
185 mUpdateAttributesLocation = true;
188 void Renderer::SetBlending( Context& context, bool blend )
190 context.SetBlend( blend );
193 // Blend color is optional and rarely used
194 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
197 context.SetCustomBlendColor( *blendColor );
201 context.SetDefaultBlendColor();
204 // Set blend source & destination factors
205 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
206 mBlendingOptions.GetBlendDestFactorRgb(),
207 mBlendingOptions.GetBlendSrcFactorAlpha(),
208 mBlendingOptions.GetBlendDestFactorAlpha() );
210 // Set blend equations
211 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
212 mBlendingOptions.GetBlendEquationAlpha() );
216 void Renderer::GlContextDestroyed()
218 mGeometry->GlContextDestroyed();
221 void Renderer::GlCleanup()
225 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
227 // Check if the map has changed
228 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
230 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
232 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
233 node.GetUniformMapChanged(bufferIndex))
235 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
236 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
238 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
239 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
240 mUniformIndexMap.Resize( maxMaps );
242 unsigned int mapIndex(0);
243 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
245 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
246 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
249 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
251 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
253 for( unsigned int i(0); i<uniformMap.Count(); ++i )
255 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
257 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
265 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
266 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
271 mUniformIndexMap.Resize( mapIndex );
274 // Set uniforms in local map
275 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
276 end = mUniformIndexMap.End() ;
280 SetUniformFromProperty( bufferIndex, program, *iter );
283 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
286 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
290 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
292 GLint location = program.GetUniformLocation(map.uniformIndex);
293 if( Program::UNIFORM_UNKNOWN != location )
295 // switch based on property type to use correct GL uniform setter
296 switch ( map.propertyValue->GetType() )
298 case Property::INTEGER:
300 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
303 case Property::FLOAT:
305 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
308 case Property::VECTOR2:
310 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
311 program.SetUniform2f( location, value.x, value.y );
315 case Property::VECTOR3:
317 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
318 program.SetUniform3f( location, value.x, value.y, value.z );
322 case Property::VECTOR4:
324 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
325 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
329 case Property::ROTATION:
331 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
332 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
336 case Property::MATRIX:
338 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
339 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
343 case Property::MATRIX3:
345 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
346 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
352 // Other property types are ignored
359 bool Renderer::BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program )
361 unsigned int textureUnit = 0;
364 std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
366 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
367 GLint uniformLocation(-1);
368 for( size_t i(0); result && i<textures.size(); ++i )
370 ResourceId textureId = textures[i].GetTextureId();
371 Internal::Texture* texture = textureCache.GetTexture( textureId );
374 result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
378 GLint uniformLocation;
380 //TODO : This is a bug, result variable is being shadowed. Fix it!
381 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
382 if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
384 program.SetUniform1i( uniformLocation, textureUnit );
386 unsigned int samplerBitfield(ImageSampler::DEFAULT_BITFIELD);
387 const Render::Sampler* sampler( samplers[i] );
390 samplerBitfield = sampler->mBitfield;
393 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
401 std::vector<Render::NewTexture*>& newTextures( mRenderDataProvider->GetNewTextures() );
402 for( size_t i(0); result && i<newTextures.size(); ++i )
406 result = program.GetSamplerUniformLocation( i, uniformLocation ) &&
407 newTextures[i]->Bind(context, textureUnit, samplers[i] );
411 program.SetUniform1i( uniformLocation, textureUnit );
420 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
422 mFaceCullingMode = mode;
425 void Renderer::SetBlendingBitMask( unsigned int bitmask )
427 mBlendingOptions.SetBitmask( bitmask );
430 void Renderer::SetBlendColor( const Vector4* color )
432 mBlendingOptions.SetBlendColor( *color );
435 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
437 mIndexedDrawFirstElement = firstElement;
440 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
442 mIndexedDrawElementsCount = elementsCount;
445 void Renderer::EnablePreMultipliedAlpha( bool enable )
447 mPremultipledAlphaEnabled = enable;
450 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
452 mDepthWriteMode = depthWriteMode;
455 void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
457 mDepthTestMode = depthTestMode;
460 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
462 return mDepthWriteMode;
465 DepthTestMode::Type Renderer::GetDepthTestMode() const
467 return mDepthTestMode;
470 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
472 mDepthFunction = depthFunction;
475 DepthFunction::Type Renderer::GetDepthFunction() const
477 return mDepthFunction;
480 void Renderer::SetRenderMode( RenderMode::Type renderMode )
482 mStencilParameters.renderMode = renderMode;
485 RenderMode::Type Renderer::GetRenderMode() const
487 return mStencilParameters.renderMode;
490 void Renderer::SetStencilFunction( StencilFunction::Type stencilFunction )
492 mStencilParameters.stencilFunction = stencilFunction;
495 StencilFunction::Type Renderer::GetStencilFunction() const
497 return mStencilParameters.stencilFunction;
500 void Renderer::SetStencilFunctionMask( int stencilFunctionMask )
502 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
505 int Renderer::GetStencilFunctionMask() const
507 return mStencilParameters.stencilFunctionMask;
510 void Renderer::SetStencilFunctionReference( int stencilFunctionReference )
512 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
515 int Renderer::GetStencilFunctionReference() const
517 return mStencilParameters.stencilFunctionReference;
520 void Renderer::SetStencilMask( int stencilMask )
522 mStencilParameters.stencilMask = stencilMask;
525 int Renderer::GetStencilMask() const
527 return mStencilParameters.stencilMask;
530 void Renderer::SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail )
532 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
535 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
537 return mStencilParameters.stencilOperationOnFail;
540 void Renderer::SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail )
542 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
545 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
547 return mStencilParameters.stencilOperationOnZFail;
550 void Renderer::SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass )
552 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
555 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
557 return mStencilParameters.stencilOperationOnZPass;
560 void Renderer::SetBatchingEnabled( bool batchingEnabled )
562 mBatchingEnabled = batchingEnabled;
565 void Renderer::Render( Context& context,
566 SceneGraph::TextureCache& textureCache,
567 BufferIndex bufferIndex,
568 const SceneGraph::NodeDataProvider& node,
569 SceneGraph::Shader& defaultShader,
570 const Matrix& modelMatrix,
571 const Matrix& modelViewMatrix,
572 const Matrix& viewMatrix,
573 const Matrix& projectionMatrix,
575 Render::Geometry* externalGeometry,
578 // Get the program to use:
579 Program* program = mRenderDataProvider->GetShader().GetProgram();
582 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
583 program = defaultShader.GetProgram();
584 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
587 DALI_LOG_ERROR( "Failed to get program for shader at address %p.\n", (void*)&mRenderDataProvider->GetShader() );
593 context.CullFace( mFaceCullingMode );
596 SetBlending( context, blend );
598 // Take the program into use so we can send uniforms to it
601 if( DALI_LIKELY( BindTextures( context, textureCache, *program ) ) )
603 // Only set up and draw if we have textures and they are all valid
605 // set projection and view matrix if program has not yet received them yet this frame
606 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
609 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
610 if( Program::UNIFORM_UNKNOWN != loc )
612 const Vector4& color = node.GetRenderColor( bufferIndex );
613 if( mPremultipledAlphaEnabled )
615 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
619 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
623 SetUniforms( bufferIndex, node, size, *program );
624 Render::Geometry* geometry = externalGeometry ? externalGeometry : mGeometry;
626 if( mUpdateAttributesLocation || geometry->AttributesChanged() )
628 geometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
629 mUpdateAttributesLocation = false;
632 geometry->UploadAndDraw( context, bufferIndex, mAttributesLocation, mIndexedDrawFirstElement, mIndexedDrawElementsCount );
636 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RenderInstructionProcessor::SortAttributes& sortAttributes ) const
638 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
639 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
640 if( !textures.empty() )
642 sortAttributes.textureResourceId = textures[0].GetTextureId();
646 sortAttributes.textureResourceId = Integration::InvalidResourceId;
649 sortAttributes.geometry = mGeometry;
652 } // namespace SceneGraph
654 } // namespace Internal