2 * Copyright (c) 2015 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>
23 #include <dali/internal/common/image-sampler.h>
24 #include <dali/internal/render/gl-resources/context.h>
25 #include <dali/internal/render/renderers/render-sampler.h>
26 #include <dali/internal/render/shaders/scene-graph-shader.h>
27 #include <dali/internal/render/shaders/program.h>
28 #include <dali/internal/render/data-providers/node-data-provider.h>
29 #include <dali/internal/render/data-providers/uniform-name-cache.h>
30 #include <dali/internal/render/gl-resources/texture-cache.h>
31 #include <dali/public-api/actors/blending.h>
32 #include <dali/internal/render/gl-resources/gl-texture.h>
43 static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it in object to reduce storage overhead
44 static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it in object to reduce storage overhead
47 * Helper to set view and projection matrices once per program
48 * @param program to set the matrices to
49 * @param modelMatrix to set
50 * @param viewMatrix to set
51 * @param projectionMatrix to set
52 * @param modelViewMatrix to set
53 * @param modelViewProjectionMatrix to set
55 inline void SetMatrices( Program& program,
56 const Matrix& modelMatrix,
57 const Matrix& viewMatrix,
58 const Matrix& projectionMatrix,
59 const Matrix& modelViewMatrix )
61 GLint loc = program.GetUniformLocation( Program::UNIFORM_MODEL_MATRIX );
62 if( Program::UNIFORM_UNKNOWN != loc )
64 program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
66 loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
67 if( Program::UNIFORM_UNKNOWN != loc )
69 if( program.GetViewMatrix() != &viewMatrix )
71 program.SetViewMatrix( &viewMatrix );
72 program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
75 // set projection matrix if program has not yet received it this frame or if it is dirty
76 loc = program.GetUniformLocation( Program::UNIFORM_PROJECTION_MATRIX );
77 if( Program::UNIFORM_UNKNOWN != loc )
79 if( program.GetProjectionMatrix() != &projectionMatrix )
81 program.SetProjectionMatrix( &projectionMatrix );
82 program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
85 loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
86 if( Program::UNIFORM_UNKNOWN != loc )
88 program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
91 loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
92 if( Program::UNIFORM_UNKNOWN != loc )
94 Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
95 program.SetUniformMatrix4fv( loc, 1, gModelViewProjectionMatrix.AsFloat() );
98 loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
99 if( Program::UNIFORM_UNKNOWN != loc )
101 gNormalMatrix = modelViewMatrix;
102 gNormalMatrix.Invert();
103 gNormalMatrix.Transpose();
104 program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
113 Renderer* Renderer::New( SceneGraph::RenderDataProvider* dataProvider,
114 Render::Geometry* geometry,
115 unsigned int blendingBitmask,
116 const Vector4* blendColor,
117 FaceCullingMode::Type faceCullingMode,
118 bool preMultipliedAlphaEnabled,
119 DepthWriteMode::Type depthWriteMode,
120 DepthTestMode::Type depthTestMode,
121 DepthFunction::Type depthFunction )
123 return new Renderer( dataProvider, geometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthTestMode, depthFunction );
126 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
127 Render::Geometry* geometry,
128 unsigned int blendingBitmask,
129 const Vector4* blendColor,
130 FaceCullingMode::Type faceCullingMode,
131 bool preMultipliedAlphaEnabled,
132 DepthWriteMode::Type depthWriteMode,
133 DepthTestMode::Type depthTestMode,
134 DepthFunction::Type depthFunction )
135 : mRenderDataProvider( dataProvider ),
137 mTextureCache( NULL ),
138 mUniformNameCache( NULL ),
139 mGeometry( geometry ),
141 mAttributesLocation(),
143 mFaceCullingMode( faceCullingMode ),
144 mDepthFunction( depthFunction ),
145 mIndexedDrawFirstElement( 0 ),
146 mIndexedDrawElementsCount( 0 ),
147 mDepthWriteMode( depthWriteMode ),
148 mDepthTestMode( depthTestMode ),
149 mUpdateAttributesLocation( true ),
150 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
152 if( blendingBitmask != 0u )
154 mBlendingOptions.SetBitmask( blendingBitmask );
159 mBlendingOptions.SetBlendColor( *blendColor );
163 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache )
166 mTextureCache = &textureCache;
167 mUniformNameCache = &uniformNameCache;
170 Renderer::~Renderer()
174 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
176 mRenderDataProvider = dataProvider;
177 mUpdateAttributesLocation = true;
180 void Renderer::SetGeometry( Render::Geometry* geometry )
182 mGeometry = geometry;
183 mUpdateAttributesLocation = true;
186 void Renderer::SetBlending( Context& context, bool blend )
188 context.SetBlend( blend );
191 // Blend color is optional and rarely used
192 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
195 context.SetCustomBlendColor( *blendColor );
199 context.SetDefaultBlendColor();
202 // Set blend source & destination factors
203 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
204 mBlendingOptions.GetBlendDestFactorRgb(),
205 mBlendingOptions.GetBlendSrcFactorAlpha(),
206 mBlendingOptions.GetBlendDestFactorAlpha() );
208 // Set blend equations
209 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
210 mBlendingOptions.GetBlendEquationAlpha() );
214 void Renderer::GlContextDestroyed()
216 mGeometry->GlContextDestroyed();
219 void Renderer::GlCleanup()
223 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
225 // Check if the map has changed
226 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
228 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
230 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
231 node.GetUniformMapChanged(bufferIndex))
233 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
234 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
236 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
237 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
238 mUniformIndexMap.Resize( maxMaps );
240 unsigned int mapIndex(0);
241 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
243 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
244 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
247 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
249 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
251 for( unsigned int i(0); i<uniformMap.Count(); ++i )
253 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
255 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
263 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
264 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
269 mUniformIndexMap.Resize( mapIndex );
272 // Set uniforms in local map
273 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
274 end = mUniformIndexMap.End() ;
278 SetUniformFromProperty( bufferIndex, program, *iter );
281 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
284 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
288 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
290 GLint location = program.GetUniformLocation(map.uniformIndex);
291 if( Program::UNIFORM_UNKNOWN != location )
293 // switch based on property type to use correct GL uniform setter
294 switch ( map.propertyValue->GetType() )
296 case Property::INTEGER:
298 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
301 case Property::FLOAT:
303 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
306 case Property::VECTOR2:
308 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
309 program.SetUniform2f( location, value.x, value.y );
313 case Property::VECTOR3:
315 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
316 program.SetUniform3f( location, value.x, value.y, value.z );
320 case Property::VECTOR4:
322 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
323 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
327 case Property::ROTATION:
329 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
330 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
334 case Property::MATRIX:
336 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
337 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
341 case Property::MATRIX3:
343 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
344 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
350 // Other property types are ignored
357 bool Renderer::BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program )
359 unsigned int textureUnit = 0;
362 std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
364 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
365 GLint uniformLocation(-1);
366 for( size_t i(0); result && i<textures.size(); ++i )
368 ResourceId textureId = textures[i].GetTextureId();
369 Internal::Texture* texture = textureCache.GetTexture( textureId );
372 result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
376 GLint uniformLocation;
378 //TODO : This is a bug, result variable is being shadowed. Fix it!
379 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
380 if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
382 program.SetUniform1i( uniformLocation, textureUnit );
384 unsigned int samplerBitfield(ImageSampler::DEFAULT_BITFIELD);
385 const Render::Sampler* sampler( samplers[i] );
388 samplerBitfield = sampler->mBitfield;
391 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
399 std::vector<Render::NewTexture*>& newTextures( mRenderDataProvider->GetNewTextures() );
400 for( size_t i(0); result && i<newTextures.size(); ++i )
404 result = program.GetSamplerUniformLocation( i, uniformLocation ) &&
405 newTextures[i]->Bind(context, textureUnit, samplers[i] );
409 program.SetUniform1i( uniformLocation, textureUnit );
418 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
420 mFaceCullingMode = mode;
423 void Renderer::SetBlendingBitMask( unsigned int bitmask )
425 mBlendingOptions.SetBitmask( bitmask );
428 void Renderer::SetBlendColor( const Vector4* color )
430 mBlendingOptions.SetBlendColor( *color );
433 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
435 mIndexedDrawFirstElement = firstElement;
438 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
440 mIndexedDrawElementsCount = elementsCount;
443 void Renderer::EnablePreMultipliedAlpha( bool enable )
445 mPremultipledAlphaEnabled = enable;
448 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
450 mDepthWriteMode = depthWriteMode;
453 void Renderer::SetDepthTestMode( DepthTestMode::Type depthTestMode )
455 mDepthTestMode = depthTestMode;
458 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
460 return mDepthWriteMode;
463 DepthTestMode::Type Renderer::GetDepthTestMode() const
465 return mDepthTestMode;
468 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
470 mDepthFunction = depthFunction;
473 DepthFunction::Type Renderer::GetDepthFunction() const
475 return mDepthFunction;
478 void Renderer::Render( Context& context,
479 SceneGraph::TextureCache& textureCache,
480 BufferIndex bufferIndex,
481 const SceneGraph::NodeDataProvider& node,
482 SceneGraph::Shader& defaultShader,
483 const Matrix& modelMatrix,
484 const Matrix& modelViewMatrix,
485 const Matrix& viewMatrix,
486 const Matrix& projectionMatrix,
490 // Get the program to use:
491 Program* program = mRenderDataProvider->GetShader().GetProgram();
494 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
495 program = defaultShader.GetProgram();
496 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
499 DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*)&mRenderDataProvider->GetShader() );
505 context.CullFace( mFaceCullingMode );
508 SetBlending( context, blend );
510 // Take the program into use so we can send uniforms to it
513 if( DALI_LIKELY( BindTextures( context, textureCache, *program ) ) )
515 // Only set up and draw if we have textures and they are all valid
517 // set projection and view matrix if program has not yet received them yet this frame
518 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
521 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
522 if( Program::UNIFORM_UNKNOWN != loc )
524 const Vector4& color = node.GetRenderColor( bufferIndex );
525 if( mPremultipledAlphaEnabled )
527 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
531 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
535 SetUniforms( bufferIndex, node, size, *program );
537 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
539 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
540 mUpdateAttributesLocation = false;
543 mGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation, mIndexedDrawFirstElement, mIndexedDrawElementsCount );
547 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
549 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
550 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
551 if( !textures.empty() )
553 sortAttributes.textureResourceId = textures[0].GetTextureId();
557 sortAttributes.textureResourceId = Integration::InvalidResourceId;
560 sortAttributes.geometry = mGeometry;
563 } // namespace SceneGraph
565 } // namespace Internal