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.h>
31 #include <dali/internal/render/gl-resources/texture-cache.h>
32 #include <dali/public-api/actors/blending.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( SceneGraph::TextureCache& textureCache, Program& program )
359 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
360 for( size_t i(0); result && i<textures.size(); ++i )
362 ResourceId textureId = textures[i].GetTextureId();
363 Internal::Texture* texture = textureCache.GetTexture( textureId );
366 result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
370 GLint uniformLocation;
372 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
373 if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
375 program.SetUniform1i( uniformLocation, textureUnit );
377 unsigned int samplerBitfield(0);
378 Render::Texture& textureMapping = textures[i];
379 const Render::Sampler* sampler( textureMapping.GetSampler() );
382 samplerBitfield = ImageSampler::PackBitfield(
383 static_cast< FilterMode::Type >(sampler->GetMinifyFilterMode()),
384 static_cast< FilterMode::Type >(sampler->GetMagnifyFilterMode()),
385 static_cast< WrapMode::Type >(sampler->GetUWrapMode()),
386 static_cast< WrapMode::Type >(sampler->GetVWrapMode())
391 samplerBitfield = ImageSampler::DEFAULT_BITFIELD;
394 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
404 void Renderer::SetFaceCullingMode( FaceCullingMode::Type mode )
406 mFaceCullingMode = mode;
409 void Renderer::SetBlendingBitMask( unsigned int bitmask )
411 mBlendingOptions.SetBitmask( bitmask );
414 void Renderer::SetBlendColor( const Vector4* color )
416 mBlendingOptions.SetBlendColor( *color );
419 void Renderer::SetIndexedDrawFirstElement( size_t firstElement )
421 mIndexedDrawFirstElement = firstElement;
424 void Renderer::SetIndexedDrawElementsCount( size_t elementsCount )
426 mIndexedDrawElementsCount = elementsCount;
429 void Renderer::EnablePreMultipliedAlpha( bool enable )
431 mPremultipledAlphaEnabled = enable;
434 void Renderer::SetDepthWriteMode( DepthWriteMode::Type depthWriteMode )
436 mDepthWriteMode = depthWriteMode;
439 DepthWriteMode::Type Renderer::GetDepthWriteMode() const
441 return mDepthWriteMode;
444 void Renderer::SetDepthFunction( DepthFunction::Type depthFunction )
446 mDepthFunction = depthFunction;
449 DepthFunction::Type Renderer::GetDepthFunction() const
451 return mDepthFunction;
454 void Renderer::Render( Context& context,
455 SceneGraph::TextureCache& textureCache,
456 BufferIndex bufferIndex,
457 const SceneGraph::NodeDataProvider& node,
458 SceneGraph::Shader& defaultShader,
459 const Matrix& modelMatrix,
460 const Matrix& modelViewMatrix,
461 const Matrix& viewMatrix,
462 const Matrix& projectionMatrix,
466 // Get the program to use:
467 Program* program = mRenderDataProvider->GetShader().GetProgram();
470 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
471 program = defaultShader.GetProgram();
472 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
475 DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*)&mRenderDataProvider->GetShader() );
481 context.CullFace( mFaceCullingMode );
484 SetBlending( context, blend );
486 // Take the program into use so we can send uniforms to it
489 if( DALI_LIKELY( BindTextures( textureCache, *program ) ) )
491 // Only set up and draw if we have textures and they are all valid
493 // set projection and view matrix if program has not yet received them yet this frame
494 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
497 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
498 if( Program::UNIFORM_UNKNOWN != loc )
500 const Vector4& color = node.GetRenderColor( bufferIndex );
501 if( mPremultipledAlphaEnabled )
503 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
507 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
511 SetUniforms( bufferIndex, node, size, *program );
513 if( mUpdateAttributesLocation || mGeometry->AttributesChanged() )
515 mGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
516 mUpdateAttributesLocation = false;
519 mGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation, mIndexedDrawFirstElement, mIndexedDrawElementsCount );
523 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
525 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
526 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
527 if( !textures.empty() )
529 sortAttributes.textureResourceId = textures[0].GetTextureId();
533 sortAttributes.textureResourceId = Integration::InvalidResourceId;
536 sortAttributes.geometry = mGeometry;
539 } // namespace SceneGraph
541 } // namespace Internal