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 DepthFunction::Type depthFunction )
122 return new Renderer( dataProvider, geometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled, depthWriteMode, depthFunction );
125 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
126 Render::Geometry* geometry,
127 unsigned int blendingBitmask,
128 const Vector4* blendColor,
129 FaceCullingMode::Type faceCullingMode,
130 bool preMultipliedAlphaEnabled,
131 DepthWriteMode::Type depthWriteMode,
132 DepthFunction::Type depthFunction )
133 : mRenderDataProvider( dataProvider ),
135 mTextureCache( NULL ),
136 mUniformNameCache( NULL ),
137 mGeometry( geometry ),
139 mAttributesLocation(),
141 mFaceCullingMode( faceCullingMode ),
142 mDepthWriteMode( depthWriteMode ),
143 mDepthFunction( depthFunction ),
144 mIndexedDrawFirstElement( 0 ),
145 mIndexedDrawElementsCount( 0 ),
146 mUpdateAttributesLocation( true ),
147 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
149 if( blendingBitmask != 0u )
151 mBlendingOptions.SetBitmask( blendingBitmask );
156 mBlendingOptions.SetBlendColor( *blendColor );
160 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache )
163 mTextureCache = &textureCache;
164 mUniformNameCache = &uniformNameCache;
167 Renderer::~Renderer()
171 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
173 mRenderDataProvider = dataProvider;
174 mUpdateAttributesLocation = true;
177 void Renderer::SetGeometry( Render::Geometry* geometry )
179 mGeometry = geometry;
180 mUpdateAttributesLocation = true;
183 void Renderer::SetBlending( Context& context, bool blend )
185 context.SetBlend( blend );
188 // Blend color is optional and rarely used
189 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
192 context.SetCustomBlendColor( *blendColor );
196 context.SetDefaultBlendColor();
199 // Set blend source & destination factors
200 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
201 mBlendingOptions.GetBlendDestFactorRgb(),
202 mBlendingOptions.GetBlendSrcFactorAlpha(),
203 mBlendingOptions.GetBlendDestFactorAlpha() );
205 // Set blend equations
206 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
207 mBlendingOptions.GetBlendEquationAlpha() );
211 void Renderer::GlContextDestroyed()
213 mGeometry->GlContextDestroyed();
216 void Renderer::GlCleanup()
220 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
222 // Check if the map has changed
223 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
225 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
227 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
228 node.GetUniformMapChanged(bufferIndex))
230 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
231 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
233 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
234 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
235 mUniformIndexMap.Resize( maxMaps );
237 unsigned int mapIndex(0);
238 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
240 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
241 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
244 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
246 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
248 for( unsigned int i(0); i<uniformMap.Count(); ++i )
250 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
252 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
260 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
261 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
266 mUniformIndexMap.Resize( mapIndex );
269 // Set uniforms in local map
270 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
271 end = mUniformIndexMap.End() ;
275 SetUniformFromProperty( bufferIndex, program, *iter );
278 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
281 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
285 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
287 GLint location = program.GetUniformLocation(map.uniformIndex);
288 if( Program::UNIFORM_UNKNOWN != location )
290 // switch based on property type to use correct GL uniform setter
291 switch ( map.propertyValue->GetType() )
293 case Property::INTEGER:
295 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
298 case Property::FLOAT:
300 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
303 case Property::VECTOR2:
305 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
306 program.SetUniform2f( location, value.x, value.y );
310 case Property::VECTOR3:
312 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
313 program.SetUniform3f( location, value.x, value.y, value.z );
317 case Property::VECTOR4:
319 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
320 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
324 case Property::ROTATION:
326 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
327 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
331 case Property::MATRIX:
333 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
334 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
338 case Property::MATRIX3:
340 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
341 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
347 // Other property types are ignored
354 bool Renderer::BindTextures( Context& context, SceneGraph::TextureCache& textureCache, Program& program )
356 unsigned int textureUnit = 0;
359 std::vector<Render::Sampler*>& samplers( mRenderDataProvider->GetSamplers() );
361 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
362 for( size_t i(0); result && i<textures.size(); ++i )
364 ResourceId textureId = textures[i].GetTextureId();
365 Internal::Texture* texture = textureCache.GetTexture( textureId );
368 result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
372 GLint uniformLocation;
374 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
375 if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
377 program.SetUniform1i( uniformLocation, textureUnit );
379 unsigned int samplerBitfield(ImageSampler::DEFAULT_BITFIELD);
380 const Render::Sampler* sampler( samplers[i] );
383 samplerBitfield = sampler->mBitfield;
386 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
394 std::vector<Render::NewTexture*>& newTextures( mRenderDataProvider->GetNewTextures() );
395 GLint uniformLocation(0);
396 for( size_t i(0); result && i<newTextures.size(); ++i )
400 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
403 newTextures[i]->Bind(context, textureUnit, samplers[i] );
404 program.SetUniform1i( uniformLocation, textureUnit );
413 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
415 mFaceCullingMode = mode;
418 void Renderer::SetBlendingBitMask( unsigned int bitmask )
420 mBlendingOptions.SetBitmask( bitmask );
423 void Renderer::SetBlendColor( const Vector4* color )
425 mBlendingOptions.SetBlendColor( *color );
428 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
430 mIndexedDrawFirstElement = firstElement;
433 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
435 mIndexedDrawElementsCount = elementsCount;
438 void Renderer::EnablePreMultipliedAlpha( bool enable )
440 mPremultipledAlphaEnabled = enable;
443 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
445 mDepthWriteMode = depthWriteMode;
448 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
450 return mDepthWriteMode;
453 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
455 mDepthFunction = depthFunction;
458 DepthFunction::Type Renderer::GetDepthFunction() const
460 return mDepthFunction;
463 void Renderer::Render( Context& context,
464 SceneGraph::TextureCache& textureCache,
465 BufferIndex bufferIndex,
466 const SceneGraph::NodeDataProvider& node,
467 SceneGraph::Shader& defaultShader,
468 const Matrix& modelMatrix,
469 const Matrix& modelViewMatrix,
470 const Matrix& viewMatrix,
471 const Matrix& projectionMatrix,
475 // Get the program to use:
476 Program* program = mRenderDataProvider->GetShader().GetProgram();
479 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
480 program = defaultShader.GetProgram();
481 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
484 DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*)&mRenderDataProvider->GetShader() );
490 context.CullFace( mFaceCullingMode );
493 SetBlending( context, blend );
495 // Take the program into use so we can send uniforms to it
498 if( DALI_LIKELY( BindTextures( context, textureCache, *program ) ) )
500 // Only set up and draw if we have textures and they are all valid
502 // set projection and view matrix if program has not yet received them yet this frame
503 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
506 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
507 if( Program::UNIFORM_UNKNOWN != loc )
509 const Vector4& color = node.GetRenderColor( bufferIndex );
510 if( mPremultipledAlphaEnabled )
512 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
516 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
520 SetUniforms( bufferIndex, node, size, *program );
522 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
524 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
525 mUpdateAttributesLocation = false;
528 mGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation, mIndexedDrawFirstElement, mIndexedDrawElementsCount );
532 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
534 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
535 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
536 if( !textures.empty() )
538 sortAttributes.textureResourceId = textures[0].GetTextureId();
542 sortAttributes.textureResourceId = Integration::InvalidResourceId;
545 sortAttributes.geometry = mGeometry;
548 } // namespace SceneGraph
550 } // namespace Internal