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 )
115 return new Renderer( dataProvider, renderGeometry);
118 Renderer::Renderer( SceneGraph::RenderDataProvider* dataProvider,
119 SceneGraph::RenderGeometry* renderGeometry )
120 : mRenderDataProvider( dataProvider ),
122 mTextureCache( NULL ),
123 mUniformNameCache( NULL ),
124 mRenderGeometry( renderGeometry ),
126 mAttributesLocation(),
127 mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
128 mUpdateAttributesLocation( true ),
129 mCullFaceMode( Dali::Material::NONE )
133 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache, Render::UniformNameCache& uniformNameCache )
136 mTextureCache = &textureCache;
137 mUniformNameCache = &uniformNameCache;
140 Renderer::~Renderer()
144 void Renderer::SetRenderDataProvider( SceneGraph::RenderDataProvider* dataProvider )
146 mRenderDataProvider = dataProvider;
147 mUpdateAttributesLocation = true;
150 void Renderer::SetGeometry( SceneGraph::RenderGeometry* renderGeometry )
152 mRenderGeometry = renderGeometry;
153 mUpdateAttributesLocation = true;
156 // Note - this is currently called from UpdateThread, PrepareRenderInstructions,
157 // as an optimisation.
158 // @todo MESH_REWORK Should use Update thread objects only in PrepareRenderInstructions.
159 bool Renderer::RequiresDepthTest() const
161 return mRenderGeometry->RequiresDepthTest();
164 void Renderer::SetBlending( Context& context, bool blend )
166 context.SetBlend( blend );
169 const SceneGraph::MaterialDataProvider& material = mRenderDataProvider->GetMaterial();
171 // Blend color is optional and rarely used
172 Vector4* blendColor = material.GetBlendColor();
175 context.SetCustomBlendColor( *blendColor );
179 context.SetDefaultBlendColor();
182 const BlendingOptions& blending = material.GetBlendingOptions();
183 // Set blend source & destination factors
184 context.BlendFuncSeparate( blending.GetBlendSrcFactorRgb(),
185 blending.GetBlendDestFactorRgb(),
186 blending.GetBlendSrcFactorAlpha(),
187 blending.GetBlendDestFactorAlpha() );
189 // Set blend equations
190 context.BlendEquationSeparate( blending.GetBlendEquationRgb(),
191 blending.GetBlendEquationAlpha() );
195 void Renderer::GlContextDestroyed()
197 mRenderGeometry->GlContextDestroyed();
200 void Renderer::GlCleanup()
204 void Renderer::SetUniforms( BufferIndex bufferIndex, const SceneGraph::NodeDataProvider& node, Program& program )
206 // Check if the map has changed
207 DALI_ASSERT_DEBUG( mRenderDataProvider && "No Uniform map data provider available" );
209 const SceneGraph::UniformMapDataProvider& uniformMapDataProvider = mRenderDataProvider->GetUniformMap();
211 if( uniformMapDataProvider.GetUniformMapChanged( bufferIndex ) ||
212 node.GetUniformMapChanged(bufferIndex))
214 const SceneGraph::CollectedUniformMap& uniformMap = uniformMapDataProvider.GetUniformMap( bufferIndex );
215 const SceneGraph::CollectedUniformMap& uniformMapNode = node.GetUniformMap( bufferIndex );
217 unsigned int maxMaps = uniformMap.Count() + uniformMapNode.Count();
218 mUniformIndexMap.Clear(); // Clear contents, but keep memory if we don't change size
219 mUniformIndexMap.Resize( maxMaps );
221 unsigned int mapIndex(0);
222 for(; mapIndex < uniformMap.Count() ; ++mapIndex )
224 mUniformIndexMap[mapIndex].propertyValue = uniformMap[mapIndex]->propertyPtr;
225 mUniformIndexMap[mapIndex].uniformIndex = program.RegisterUniform( uniformMap[mapIndex]->uniformName );
228 for( unsigned int nodeMapIndex = 0; nodeMapIndex < uniformMapNode.Count() ; ++nodeMapIndex )
230 unsigned int uniformIndex = program.RegisterUniform( uniformMapNode[nodeMapIndex]->uniformName );
232 for( unsigned int i(0); i<uniformMap.Count(); ++i )
234 if( mUniformIndexMap[i].uniformIndex == uniformIndex )
236 mUniformIndexMap[i].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
244 mUniformIndexMap[mapIndex].propertyValue = uniformMapNode[nodeMapIndex]->propertyPtr;
245 mUniformIndexMap[mapIndex].uniformIndex = uniformIndex;
250 mUniformIndexMap.Resize( mapIndex );
253 // Set uniforms in local map
254 for( UniformIndexMappings::Iterator iter = mUniformIndexMap.Begin(),
255 end = mUniformIndexMap.End() ;
259 SetUniformFromProperty( bufferIndex, program, *iter );
262 GLint sizeLoc = program.GetUniformLocation( Program::UNIFORM_SIZE );
265 Vector3 size = node.GetRenderSize( bufferIndex );
266 program.SetSizeUniform3f( sizeLoc, size.x, size.y, size.z );
270 void Renderer::SetUniformFromProperty( BufferIndex bufferIndex, Program& program, UniformIndexMap& map )
272 GLint location = program.GetUniformLocation(map.uniformIndex);
273 if( Program::UNIFORM_UNKNOWN != location )
275 // switch based on property type to use correct GL uniform setter
276 switch ( map.propertyValue->GetType() )
278 case Property::INTEGER:
280 program.SetUniform1i( location, map.propertyValue->GetInteger( bufferIndex ) );
283 case Property::FLOAT:
285 program.SetUniform1f( location, map.propertyValue->GetFloat( bufferIndex ) );
288 case Property::VECTOR2:
290 Vector2 value( map.propertyValue->GetVector2( bufferIndex ) );
291 program.SetUniform2f( location, value.x, value.y );
295 case Property::VECTOR3:
297 Vector3 value( map.propertyValue->GetVector3( bufferIndex ) );
298 program.SetUniform3f( location, value.x, value.y, value.z );
302 case Property::VECTOR4:
304 Vector4 value( map.propertyValue->GetVector4( bufferIndex ) );
305 program.SetUniform4f( location, value.x, value.y, value.z, value.w );
309 case Property::ROTATION:
311 Quaternion value( map.propertyValue->GetQuaternion( bufferIndex ) );
312 program.SetUniform4f( location, value.mVector.x, value.mVector.y, value.mVector.z, value.mVector.w );
316 case Property::MATRIX:
318 const Matrix& value = map.propertyValue->GetMatrix(bufferIndex);
319 program.SetUniformMatrix4fv(location, 1, value.AsFloat() );
323 case Property::MATRIX3:
325 const Matrix3& value = map.propertyValue->GetMatrix3(bufferIndex);
326 program.SetUniformMatrix3fv(location, 1, value.AsFloat() );
332 // Other property types are ignored
339 void Renderer::BindTextures( SceneGraph::TextureCache& textureCache, Program& program )
343 std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
344 for( size_t i(0); i<textures.size(); ++i )
346 ResourceId textureId = textures[i].GetTextureId();
347 Internal::Texture* texture = textureCache.GetTexture( textureId );
350 textureCache.BindTexture( texture, textureId, GL_TEXTURE_2D, (TextureUnit)textureUnit );
352 Render::Texture& textureMapping = textures[i];
353 // Set sampler uniform location for the texture
354 int32_t uniqueIndex = textureMapping.GetUniformUniqueIndex();
355 if( Render::Texture::NOT_INITIALIZED == uniqueIndex )
357 uniqueIndex = mUniformNameCache->GetSamplerUniformUniqueIndex( textureMapping.GetUniformName() );
358 textureMapping.SetUniformUniqueIndex( uniqueIndex );
360 GLint uniformLocation = program.GetSamplerUniformLocation( uniqueIndex, textureMapping.GetUniformName() );
361 if( Program::UNIFORM_UNKNOWN != uniformLocation )
363 program.SetUniform1i( uniformLocation, textureUnit );
366 unsigned int samplerBitfield(0);
367 const Render::Sampler* sampler( textureMapping.GetSampler() );
370 samplerBitfield = ImageSampler::PackBitfield(
371 static_cast< FilterMode::Type >(sampler->GetMinifyFilterMode()),
372 static_cast< FilterMode::Type >(sampler->GetMagnifyFilterMode()),
373 static_cast< WrapMode::Type >(sampler->GetUWrapMode()),
374 static_cast< WrapMode::Type >(sampler->GetVWrapMode())
379 samplerBitfield = ImageSampler::DEFAULT_BITFIELD;
382 texture->ApplySampler( (TextureUnit)textureUnit, samplerBitfield );
389 void Renderer::SetCullFace( Dali::Material::FaceCullingMode mode )
391 DALI_ASSERT_DEBUG( mode >= Dali::Material::NONE && mode <= Dali::Material::CULL_BACK_AND_FRONT );
392 mCullFaceMode = mode;
395 void Renderer::SetSampler( unsigned int samplerBitfield )
397 mSamplerBitfield = samplerBitfield;
400 void Renderer::Render( Context& context,
401 SceneGraph::TextureCache& textureCache,
402 BufferIndex bufferIndex,
403 const SceneGraph::NodeDataProvider& node,
404 SceneGraph::Shader& defaultShader,
405 const Matrix& modelViewMatrix,
406 const Matrix& viewMatrix,
407 const Matrix& projectionMatrix,
410 // Get the program to use:
411 Program* program = mRenderDataProvider->GetShader().GetProgram();
414 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
415 program = defaultShader.GetProgram();
416 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
419 DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*)&mRenderDataProvider->GetShader() );
425 context.CullFace( mRenderDataProvider->GetMaterial().GetFaceCullingMode() );
428 SetBlending( context, blend );
430 // Take the program into use so we can send uniforms to it
433 // set projection and view matrix if program has not yet received them yet this frame
434 SetMatrices( *program, node.GetModelMatrix( bufferIndex ), viewMatrix, projectionMatrix, modelViewMatrix );
437 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
438 if( Program::UNIFORM_UNKNOWN != loc )
440 const Vector4& color = node.GetRenderColor( bufferIndex );
441 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
445 BindTextures( textureCache, *program );
448 SetUniforms( bufferIndex, node, *program );
450 if( mUpdateAttributesLocation || mRenderGeometry->AttributesChanged() )
452 mRenderGeometry->GetAttributeLocationFromProgram( mAttributesLocation, *program, bufferIndex );
453 mUpdateAttributesLocation = false;
456 mRenderGeometry->UploadAndDraw( context, bufferIndex, mAttributesLocation );
459 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
461 sortAttributes.shader = &( mRenderDataProvider->GetShader() );
462 const std::vector<Render::Texture>& textures( mRenderDataProvider->GetTextures() );
463 if( !textures.empty() )
465 sortAttributes.textureResourceId = textures[0].GetTextureId();
469 sortAttributes.textureResourceId = Integration::InvalidResourceId;
472 sortAttributes.geometry = mRenderGeometry;
475 } // namespace SceneGraph
477 } // namespace Internal