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/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/data-providers/uniform-name-cache.h>
29 #include <dali/internal/render/gl-resources/texture.h>
30 #include <dali/internal/render/gl-resources/texture-cache.h>
31 #include <dali/public-api/actors/blending.h>
42 static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it in object to reduce storage overhead
43 static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it in object to reduce storage overhead
46 * Helper to set view and projection matrices once per program
47 * @param program to set the matrices to
48 * @param modelMatrix to set
49 * @param viewMatrix to set
50 * @param projectionMatrix to set
51 * @param modelViewMatrix to set
52 * @param modelViewProjectionMatrix to set
54 inline void SetMatrices( Program& program,
55 const Matrix& modelMatrix,
56 const Matrix& viewMatrix,
57 const Matrix& projectionMatrix,
58 const Matrix& modelViewMatrix )
60 GLint loc = program.GetUniformLocation( Program::UNIFORM_MODEL_MATRIX );
61 if( Program::UNIFORM_UNKNOWN != loc )
63 program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
65 loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
66 if( Program::UNIFORM_UNKNOWN != loc )
68 if( program.GetViewMatrix() != &viewMatrix )
70 program.SetViewMatrix( &viewMatrix );
71 program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
74 // set projection matrix if program has not yet received it this frame or if it is dirty
75 loc = program.GetUniformLocation( Program::UNIFORM_PROJECTION_MATRIX );
76 if( Program::UNIFORM_UNKNOWN != loc )
78 if( program.GetProjectionMatrix() != &projectionMatrix )
80 program.SetProjectionMatrix( &projectionMatrix );
81 program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
84 loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
85 if( Program::UNIFORM_UNKNOWN != loc )
87 program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
90 loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
91 if( Program::UNIFORM_UNKNOWN != loc )
93 Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
94 program.SetUniformMatrix4fv( loc, 1, gModelViewProjectionMatrix.AsFloat() );
97 loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
98 if( Program::UNIFORM_UNKNOWN != loc )
100 gNormalMatrix = modelViewMatrix;
101 gNormalMatrix.Invert();
102 gNormalMatrix.Transpose();
103 program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
112 Renderer* Renderer::New( SceneGraph::RenderDataProvider* dataProvider,
113 SceneGraph::RenderGeometry* renderGeometry,
114 unsigned int blendingBitmask,
115 const Vector4* blendColor,
116 Dali::Renderer::FaceCullingMode faceCullingMode,
117 bool preMultipliedAlphaEnabled )
119 return new Renderer( dataProvider, renderGeometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled );
122 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
123 SceneGraph::RenderGeometry* renderGeometry,
124 unsigned int blendingBitmask,
125 const Vector4* blendColor,
126 Dali::Renderer::FaceCullingMode faceCullingMode,
127 bool preMultipliedAlphaEnabled)
128 : mRenderDataProvider( dataProvider ),
130 mTextureCache( NULL ),
131 mUniformNameCache( NULL ),
132 mRenderGeometry( renderGeometry ),
134 mAttributesLocation(),
136 mFaceCullingMode( faceCullingMode ),
137 mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
138 mUpdateAttributesLocation( true ),
139 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
141 if( blendingBitmask != 0u )
143 mBlendingOptions.SetBitmask( blendingBitmask );
148 mBlendingOptions.SetBlendColor( *blendColor );
152 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache )
155 mTextureCache = &textureCache;
156 mUniformNameCache = &uniformNameCache;
159 Renderer::~Renderer()
163 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
165 mRenderDataProvider = dataProvider;
166 mUpdateAttributesLocation = true;
169 void Renderer::SetGeometry( SceneGraph::RenderGeometry* renderGeometry )
171 mRenderGeometry = renderGeometry;
172 mUpdateAttributesLocation = true;
175 // Note - this is currently called from UpdateThread, PrepareRenderInstructions,
176 // as an optimisation.
177 // @todo MESH_REWORK Should use Update thread objects only in PrepareRenderInstructions.
178 bool Renderer::RequiresDepthTest() const
180 return mRenderGeometry->RequiresDepthTest();
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 mRenderGeometry->GlContextDestroyed();
216 void Renderer::GlCleanup()
220 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, 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 Vector3 size = node.GetRenderSize( bufferIndex );
282 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
286 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
288 GLint location = program.GetUniformLocation(map.uniformIndex);
289 if( Program::UNIFORM_UNKNOWN != location )
291 // switch based on property type to use correct GL uniform setter
292 switch ( map.propertyValue->GetType() )
294 case Property::INTEGER:
296 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
299 case Property::FLOAT:
301 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
304 case Property::VECTOR2:
306 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
307 program.SetUniform2f( location, value.x, value.y );
311 case Property::VECTOR3:
313 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
314 program.SetUniform3f( location, value.x, value.y, value.z );
318 case Property::VECTOR4:
320 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
321 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
325 case Property::ROTATION:
327 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
328 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
332 case Property::MATRIX:
334 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
335 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
339 case Property::MATRIX3:
341 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
342 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
348 // Other property types are ignored
355 bool Renderer::BindTextures( SceneGraph::TextureCache& textureCache, Program& program )
360 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
361 for( size_t i(0); result && i<textures.size(); ++i )
363 ResourceId textureId = textures[i].GetTextureId();
364 Internal::Texture* texture = textureCache.GetTexture( textureId );
367 result = textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
371 Render::Texture& textureMapping = textures[i];
372 // Set sampler uniform location for the texture
373 int32_t uniqueIndex = textureMapping.GetUniformUniqueIndex();
374 if( Render::Texture::NOT_INITIALIZED == uniqueIndex )
376 uniqueIndex = mUniformNameCache->GetSamplerUniformUniqueIndex( textureMapping.GetUniformName() );
377 textureMapping.SetUniformUniqueIndex( uniqueIndex );
379 GLint uniformLocation = program.GetSamplerUniformLocation( uniqueIndex, textureMapping.GetUniformName() );
380 if( Program::UNIFORM_UNKNOWN != uniformLocation )
382 program.SetUniform1i( uniformLocation, textureUnit );
385 unsigned int samplerBitfield(0);
386 const Render::Sampler* sampler( textureMapping.GetSampler() );
389 samplerBitfield = ImageSampler::PackBitfield(
390 static_cast< FilterMode::Type >(sampler->GetMinifyFilterMode()),
391 static_cast< FilterMode::Type >(sampler->GetMagnifyFilterMode()),
392 static_cast< WrapMode::Type >(sampler->GetUWrapMode()),
393 static_cast< WrapMode::Type >(sampler->GetVWrapMode())
398 samplerBitfield = ImageSampler::DEFAULT_BITFIELD;
401 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
410 void Renderer::SetFaceCullingMode( Dali::Renderer::FaceCullingMode mode )
412 mFaceCullingMode = mode;
415 void Renderer::SetBlendingBitMask( unsigned int bitmask )
417 mBlendingOptions.SetBitmask( bitmask );
420 void Renderer::SetBlendColor( const Vector4* color )
422 mBlendingOptions.SetBlendColor( *color );
425 void Renderer::EnablePreMultipliedAlpha( bool enable )
427 mPremultipledAlphaEnabled = enable;
430 void Renderer::SetSampler( unsigned int samplerBitfield )
432 mSamplerBitfield = samplerBitfield;
435 void Renderer::Render( Context& context,
436 SceneGraph::TextureCache& textureCache,
437 BufferIndex bufferIndex,
438 const SceneGraph::NodeDataProvider& node,
439 SceneGraph::Shader& defaultShader,
440 const Matrix& modelViewMatrix,
441 const Matrix& viewMatrix,
442 const Matrix& projectionMatrix,
445 // Get the program to use:
446 Program* program = mRenderDataProvider->GetShader().GetProgram();
449 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
450 program = defaultShader.GetProgram();
451 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
454 DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*)&mRenderDataProvider->GetShader() );
460 context.CullFace( mFaceCullingMode );
463 SetBlending( context, blend );
465 // Take the program into use so we can send uniforms to it
468 if( DALI_LIKELY( BindTextures( textureCache, *program ) ) )
470 // Only set up and draw if we have textures and they are all valid
472 // set projection and view matrix if program has not yet received them yet this frame
473 SetMatrices( *program, node.GetModelMatrix( bufferIndex ), viewMatrix, projectionMatrix, modelViewMatrix );
476 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
477 if( Program::UNIFORM_UNKNOWN != loc )
479 const Vector4& color = node.GetRenderColor( bufferIndex );
480 if( mPremultipledAlphaEnabled )
482 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
486 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
490 SetUniforms( bufferIndex, node, *program );
492 if( mUpdateAttributesLocation || mRenderGeometry->AttributesChanged() )
494 mRenderGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
495 mUpdateAttributesLocation = false;
498 mRenderGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation );
502 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
504 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
505 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
506 if( !textures.empty() )
508 sortAttributes.textureResourceId = textures[0].GetTextureId();
512 sortAttributes.textureResourceId = Integration::InvalidResourceId;
515 sortAttributes.geometry = mRenderGeometry;
518 } // namespace SceneGraph
520 } // namespace Internal