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 SceneGraph::RenderGeometry* renderGeometry,
115 unsigned int blendingBitmask,
116 const Vector4* blendColor,
117 Dali::Renderer::FaceCullingMode faceCullingMode,
118 bool preMultipliedAlphaEnabled )
120 return new Renderer( dataProvider, renderGeometry, blendingBitmask, blendColor, faceCullingMode, preMultipliedAlphaEnabled );
123 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
124 SceneGraph::RenderGeometry* renderGeometry,
125 unsigned int blendingBitmask,
126 const Vector4* blendColor,
127 Dali::Renderer::FaceCullingMode faceCullingMode,
128 bool preMultipliedAlphaEnabled)
129 : mRenderDataProvider( dataProvider ),
131 mTextureCache( NULL ),
132 mUniformNameCache( NULL ),
133 mRenderGeometry( renderGeometry ),
135 mAttributesLocation(),
137 mFaceCullingMode( faceCullingMode ),
138 mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
139 mUpdateAttributesLocation( true ),
140 mPremultipledAlphaEnabled( preMultipliedAlphaEnabled )
142 if( blendingBitmask != 0u )
144 mBlendingOptions.SetBitmask( blendingBitmask );
149 mBlendingOptions.SetBlendColor( *blendColor );
153 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache )
156 mTextureCache = &textureCache;
157 mUniformNameCache = &uniformNameCache;
160 Renderer::~Renderer()
164 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
166 mRenderDataProvider = dataProvider;
167 mUpdateAttributesLocation = true;
170 void Renderer::SetGeometry( SceneGraph::RenderGeometry* renderGeometry )
172 mRenderGeometry = renderGeometry;
173 mUpdateAttributesLocation = true;
176 // Note - this is currently called from UpdateThread, PrepareRenderInstructions,
177 // as an optimisation.
178 // @todo MESH_REWORK Should use Update thread objects only in PrepareRenderInstructions.
179 bool Renderer::RequiresDepthTest() const
181 return mRenderGeometry->RequiresDepthTest();
184 void Renderer::SetBlending( Context& context, bool blend )
186 context.SetBlend( blend );
189 // Blend color is optional and rarely used
190 const Vector4* blendColor = mBlendingOptions.GetBlendColor();
193 context.SetCustomBlendColor( *blendColor );
197 context.SetDefaultBlendColor();
200 // Set blend source & destination factors
201 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
202 mBlendingOptions.GetBlendDestFactorRgb(),
203 mBlendingOptions.GetBlendSrcFactorAlpha(),
204 mBlendingOptions.GetBlendDestFactorAlpha() );
206 // Set blend equations
207 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
208 mBlendingOptions.GetBlendEquationAlpha() );
212 void Renderer::GlContextDestroyed()
214 mRenderGeometry->GlContextDestroyed();
217 void Renderer::GlCleanup()
221 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, const Vector3& size, Program& program )
223 // Check if the map has changed
224 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
226 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
228 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
229 node.GetUniformMapChanged(bufferIndex))
231 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
232 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
234 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
235 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
236 mUniformIndexMap.Resize( maxMaps );
238 unsigned int mapIndex(0);
239 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
241 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
242 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
245 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
247 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
249 for( unsigned int i(0); i<uniformMap.Count(); ++i )
251 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
253 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
261 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
262 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
267 mUniformIndexMap.Resize( mapIndex );
270 // Set uniforms in local map
271 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
272 end = mUniformIndexMap.End() ;
276 SetUniformFromProperty( bufferIndex, program, *iter );
279 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
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 GLint uniformLocation;
373 bool result = program.GetSamplerUniformLocation( i, uniformLocation );
374 if( result && Program::UNIFORM_UNKNOWN != uniformLocation )
376 program.SetUniform1i( uniformLocation, textureUnit );
378 unsigned int samplerBitfield(0);
379 Render::Texture& textureMapping = textures[i];
380 const Render::Sampler* sampler( textureMapping.GetSampler() );
383 samplerBitfield = ImageSampler::PackBitfield(
384 static_cast< FilterMode::Type >(sampler->GetMinifyFilterMode()),
385 static_cast< FilterMode::Type >(sampler->GetMagnifyFilterMode()),
386 static_cast< WrapMode::Type >(sampler->GetUWrapMode()),
387 static_cast< WrapMode::Type >(sampler->GetVWrapMode())
392 samplerBitfield = ImageSampler::DEFAULT_BITFIELD;
395 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
405 void Renderer::SetFaceCullingMode( Dali::Renderer::FaceCullingMode mode )
407 mFaceCullingMode = mode;
410 void Renderer::SetBlendingBitMask( unsigned int bitmask )
412 mBlendingOptions.SetBitmask( bitmask );
415 void Renderer::SetBlendColor( const Vector4* color )
417 mBlendingOptions.SetBlendColor( *color );
420 void Renderer::EnablePreMultipliedAlpha( bool enable )
422 mPremultipledAlphaEnabled = enable;
425 void Renderer::SetSampler( unsigned int samplerBitfield )
427 mSamplerBitfield = samplerBitfield;
430 void Renderer::Render( Context& context,
431 SceneGraph::TextureCache& textureCache,
432 BufferIndex bufferIndex,
433 const SceneGraph::NodeDataProvider& node,
434 SceneGraph::Shader& defaultShader,
435 const Matrix& modelViewMatrix,
436 const Matrix& viewMatrix,
437 const Matrix& projectionMatrix,
441 // Get the program to use:
442 Program* program = mRenderDataProvider->GetShader().GetProgram();
445 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
446 program = defaultShader.GetProgram();
447 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
450 DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*)&mRenderDataProvider->GetShader() );
456 context.CullFace( mFaceCullingMode );
459 SetBlending( context, blend );
461 // Take the program into use so we can send uniforms to it
464 if( DALI_LIKELY( BindTextures( textureCache, *program ) ) )
466 // Only set up and draw if we have textures and they are all valid
468 // set projection and view matrix if program has not yet received them yet this frame
469 SetMatrices( *program, node.GetModelMatrix( bufferIndex ), viewMatrix, projectionMatrix, modelViewMatrix );
472 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
473 if( Program::UNIFORM_UNKNOWN != loc )
475 const Vector4& color = node.GetRenderColor( bufferIndex );
476 if( mPremultipledAlphaEnabled )
478 program->SetUniform4f( loc, color.r*color.a, color.g*color.a, color.b*color.a, color.a );
482 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
486 SetUniforms( bufferIndex, node, size, *program );
488 if( mUpdateAttributesLocation || mRenderGeometry->AttributesChanged() )
490 mRenderGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
491 mUpdateAttributesLocation = false;
494 mRenderGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation );
498 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
500 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
501 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
502 if( !textures.empty() )
504 sortAttributes.textureResourceId = textures[0].GetTextureId();
508 sortAttributes.textureResourceId = Integration::InvalidResourceId;
511 sortAttributes.geometry = mRenderGeometry;
514 } // namespace SceneGraph
516 } // namespace Internal