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,
121 bool writeToColorBuffer )
123 return new Renderer( dataProvider, geometry, blendingBitmask, blendColor,
124 faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode,
125 depthFunction, stencilParameters, writeToColorBuffer );
128 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
129 Render::Geometry* geometry,
130 unsigned int blendingBitmask,
131 const Vector4* blendColor,
132 FaceCullingMode::Type faceCullingMode,
133 bool preMultipliedAlphaEnabled,
134 DepthWriteMode::Type depthWriteMode,
135 DepthTestMode::Type depthTestMode,
136 DepthFunction::Type depthFunction,
137 StencilParameters& stencilParameters,
138 bool writeToColorBuffer )
139 : mRenderDataProvider( dataProvider ),
141 mTextureCache( NULL ),
142 mGeometry( geometry ),
144 mAttributesLocation(),
145 mStencilParameters( stencilParameters ),
147 mIndexedDrawFirstElement( 0 ),
148 mIndexedDrawElementsCount( 0 ),
149 mDepthFunction( depthFunction ),
150 mFaceCullingMode( faceCullingMode ),
151 mDepthWriteMode( depthWriteMode ),
152 mDepthTestMode( depthTestMode ),
153 mWriteToColorBuffer( writeToColorBuffer ),
154 mUpdateAttributesLocation( true ),
155 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
157 if( blendingBitmask != 0u )
159 mBlendingOptions.SetBitmask( blendingBitmask );
164 mBlendingOptions.SetBlendColor( *blendColor );
168 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache )
171 mTextureCache = &textureCache;
174 Renderer::~Renderer()
178 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
180 mRenderDataProvider = dataProvider;
181 mUpdateAttributesLocation = true;
184 void Renderer::SetGeometry( Render::Geometry* geometry )
186 mGeometry = geometry;
187 mUpdateAttributesLocation = true;
190 void Renderer::SetBlending( Context& context, bool blend )
192 context.SetBlend( blend );
195 // Blend color is optional and rarely used
196 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
199 context.SetCustomBlendColor( *blendColor );
203 context.SetDefaultBlendColor();
206 // Set blend source & destination factors
207 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
208 mBlendingOptions.GetBlendDestFactorRgb(),
209 mBlendingOptions.GetBlendSrcFactorAlpha(),
210 mBlendingOptions.GetBlendDestFactorAlpha() );
212 // Set blend equations
213 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
214 mBlendingOptions.GetBlendEquationAlpha() );
218 void Renderer::GlContextDestroyed()
220 mGeometry->GlContextDestroyed();
223 void Renderer::GlCleanup()
227 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
229 // Check if the map has changed
230 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
232 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
234 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
235 node.GetUniformMapChanged(bufferIndex))
237 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
238 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
240 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
241 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
242 mUniformIndexMap.Resize( maxMaps );
244 unsigned int mapIndex(0);
245 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
247 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
248 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
251 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
253 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
255 for( unsigned int i(0); i<uniformMap.Count(); ++i )
257 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
259 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
267 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
268 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
273 mUniformIndexMap.Resize( mapIndex );
276 // Set uniforms in local map
277 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
278 end = mUniformIndexMap.End() ;
282 SetUniformFromProperty( bufferIndex, program, *iter );
285 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
288 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
292 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
294 GLint location = program.GetUniformLocation(map.uniformIndex);
295 if( Program::UNIFORM_UNKNOWN != location )
297 // switch based on property type to use correct GL uniform setter
298 switch ( map.propertyValue->GetType() )
300 case Property::INTEGER:
302 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
305 case Property::FLOAT:
307 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
310 case Property::VECTOR2:
312 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
313 program.SetUniform2f( location, value.x, value.y );
317 case Property::VECTOR3:
319 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
320 program.SetUniform3f( location, value.x, value.y, value.z );
324 case Property::VECTOR4:
326 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
327 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
331 case Property::ROTATION:
333 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
334 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
338 case Property::MATRIX:
340 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
341 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
345 case Property::MATRIX3:
347 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
348 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
354 // Other property types are ignored
361 bool Renderer::BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program )
363 unsigned int textureUnit = 0;
366 std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
368 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
369 GLint uniformLocation(-1);
370 for( size_t i(0); result && i<textures.size(); ++i )
372 ResourceId textureId = textures[i].GetTextureId();
373 Internal::Texture* texture = textureCache.GetTexture( textureId );
376 result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
380 GLint uniformLocation;
382 //TODO : This is a bug, result variable is being shadowed. Fix it!
383 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
384 if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
386 program.SetUniform1i( uniformLocation, textureUnit );
388 unsigned int samplerBitfield(ImageSampler::DEFAULT_BITFIELD);
389 const Render::Sampler* sampler( samplers[i] );
392 samplerBitfield = sampler->mBitfield;
395 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
403 std::vector<Render::NewTexture*>& newTextures( mRenderDataProvider->GetNewTextures() );
404 for( size_t i(0); result && i<newTextures.size(); ++i )
408 result = program.GetSamplerUniformLocation( i, uniformLocation ) &&
409 newTextures[i]->Bind(context, textureUnit, samplers[i] );
413 program.SetUniform1i( uniformLocation, textureUnit );
422 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
424 mFaceCullingMode = mode;
427 void Renderer::SetBlendingBitMask( unsigned int bitmask )
429 mBlendingOptions.SetBitmask( bitmask );
432 void Renderer::SetBlendColor( const Vector4* color )
434 mBlendingOptions.SetBlendColor( *color );
437 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
439 mIndexedDrawFirstElement = firstElement;
442 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
444 mIndexedDrawElementsCount = elementsCount;
447 void Renderer::EnablePreMultipliedAlpha( bool enable )
449 mPremultipledAlphaEnabled = enable;
452 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
454 mDepthWriteMode = depthWriteMode;
457 void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
459 mDepthTestMode = depthTestMode;
462 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
464 return mDepthWriteMode;
467 DepthTestMode::Type Renderer::GetDepthTestMode() const
469 return mDepthTestMode;
472 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
474 mDepthFunction = depthFunction;
477 DepthFunction::Type Renderer::GetDepthFunction() const
479 return mDepthFunction;
482 void Renderer::SetStencilMode( StencilMode::Type stencilMode )
484 mStencilParameters.stencilMode = stencilMode;
487 StencilMode::Type Renderer::GetStencilMode() const
489 return mStencilParameters.stencilMode;
492 void Renderer::SetStencilFunction( StencilFunction::Type stencilFunction )
494 mStencilParameters.stencilFunction = stencilFunction;
497 StencilFunction::Type Renderer::GetStencilFunction() const
499 return mStencilParameters.stencilFunction;
502 void Renderer::SetStencilFunctionMask( int stencilFunctionMask )
504 mStencilParameters.stencilFunctionMask = stencilFunctionMask;
507 int Renderer::GetStencilFunctionMask() const
509 return mStencilParameters.stencilFunctionMask;
512 void Renderer::SetStencilFunctionReference( int stencilFunctionReference )
514 mStencilParameters.stencilFunctionReference = stencilFunctionReference;
517 int Renderer::GetStencilFunctionReference() const
519 return mStencilParameters.stencilFunctionReference;
522 void Renderer::SetStencilMask( int stencilMask )
524 mStencilParameters.stencilMask = stencilMask;
527 int Renderer::GetStencilMask() const
529 return mStencilParameters.stencilMask;
532 void Renderer::SetStencilOperationOnFail( StencilOperation::Type stencilOperationOnFail )
534 mStencilParameters.stencilOperationOnFail = stencilOperationOnFail;
537 StencilOperation::Type Renderer::GetStencilOperationOnFail() const
539 return mStencilParameters.stencilOperationOnFail;
542 void Renderer::SetStencilOperationOnZFail( StencilOperation::Type stencilOperationOnZFail )
544 mStencilParameters.stencilOperationOnZFail = stencilOperationOnZFail;
547 StencilOperation::Type Renderer::GetStencilOperationOnZFail() const
549 return mStencilParameters.stencilOperationOnZFail;
552 void Renderer::SetStencilOperationOnZPass( StencilOperation::Type stencilOperationOnZPass )
554 mStencilParameters.stencilOperationOnZPass = stencilOperationOnZPass;
557 StencilOperation::Type Renderer::GetStencilOperationOnZPass() const
559 return mStencilParameters.stencilOperationOnZPass;
562 void Renderer::SetWriteToColorBuffer( bool writeToColorBuffer )
564 mWriteToColorBuffer = writeToColorBuffer;
567 bool Renderer::GetWriteToColorBuffer() const
569 return mWriteToColorBuffer;
572 void Renderer::Render( Context& context,
573 SceneGraph::TextureCache& textureCache,
574 BufferIndex bufferIndex,
575 const SceneGraph::NodeDataProvider& node,
576 SceneGraph::Shader& defaultShader,
577 const Matrix& modelMatrix,
578 const Matrix& modelViewMatrix,
579 const Matrix& viewMatrix,
580 const Matrix& projectionMatrix,
584 // Get the program to use:
585 Program* program = mRenderDataProvider->GetShader().GetProgram();
588 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
589 program = defaultShader.GetProgram();
590 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
593 DALI_LOG_ERROR( "Failed to get program for shader at address %p.\n", (void*)&mRenderDataProvider->GetShader() );
599 context.CullFace( mFaceCullingMode );
602 SetBlending( context, blend );
604 // Take the program into use so we can send uniforms to it
607 if( DALI_LIKELY( BindTextures( context, textureCache, *program ) ) )
609 // Only set up and draw if we have textures and they are all valid
611 // set projection and view matrix if program has not yet received them yet this frame
612 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
615 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
616 if( Program::UNIFORM_UNKNOWN != loc )
618 const Vector4& color = node.GetRenderColor( bufferIndex );
619 if( mPremultipledAlphaEnabled )
621 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
625 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
629 SetUniforms( bufferIndex, node, size, *program );
631 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
633 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
634 mUpdateAttributesLocation = false;
637 mGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation, mIndexedDrawFirstElement, mIndexedDrawElementsCount );
641 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
643 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
644 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
645 if( !textures.empty() )
647 sortAttributes.textureResourceId = textures[0].GetTextureId();
651 sortAttributes.textureResourceId = Integration::InvalidResourceId;
654 sortAttributes.geometry = mGeometry;
657 } // namespace SceneGraph
659 } // namespace Internal