2 * Copyright (c) 2014 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/scene-graph-renderer.h>
20 #include <dali/internal/render/renderers/scene-graph-renderer-declarations.h>
23 #include <dali/internal/render/gl-resources/context.h>
24 #include <dali/internal/render/shaders/scene-graph-shader.h>
25 #include <dali/internal/render/shaders/program.h>
26 #include <dali/internal/render/renderers/scene-graph-renderer-debug.h>
27 #include <dali/internal/render/data-providers/node-data-provider.h>
28 #include <dali/public-api/actors/blending.h>
29 #include <dali/internal/common/image-sampler.h>
30 #include <dali/internal/render/renderers/render-renderer.h>
41 static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it in object to reduce storage overhead
42 static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it in object to reduce storage overhead
45 * Helper to set view and projection matrices once per program
46 * @param program to set the matrices to
47 * @param modelMatrix to set
48 * @param viewMatrix to set
49 * @param projectionMatrix to set
50 * @param modelViewMatrix to set
51 * @param modelViewProjectionMatrix to set
53 inline void SetMatrices( Program& program,
54 const Matrix& modelMatrix,
55 const Matrix& viewMatrix,
56 const Matrix& projectionMatrix,
57 const Matrix& modelViewMatrix,
58 const Matrix& modelViewProjectionMatrix )
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 program.SetUniformMatrix4fv( loc, 1, modelViewProjectionMatrix.AsFloat() );
96 loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
97 if( Program::UNIFORM_UNKNOWN != loc )
99 gNormalMatrix = modelViewMatrix;
100 gNormalMatrix.Invert();
101 gNormalMatrix.Transpose();
102 program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
111 void Renderer::Initialize( Context& context, TextureCache& textureCache )
113 mContextDELETEME = &context;
114 mTextureCacheDELETEME = &textureCache;
117 Renderer::~Renderer()
121 void Renderer::SetShader( Shader* shader )
126 void Renderer::SetUseBlend( bool useBlend )
128 mUseBlend = useBlend;
131 void Renderer::SetBlendingOptions( unsigned int options )
133 mBlendingOptions.SetBitmask( options );
136 void Renderer::SetBlendColor( const Vector4& color )
138 mBlendingOptions.SetBlendColor( color );
141 void Renderer::SetCullFace( CullFaceMode mode )
143 DALI_ASSERT_DEBUG(mode >= CullNone && mode <= CullFrontAndBack);
144 mCullFaceMode = mode;
147 void Renderer::SetSampler( unsigned int samplerBitfield )
149 mSamplerBitfield = samplerBitfield;
152 void Renderer::Render( Context& context,
153 TextureCache& textureCache,
154 BufferIndex bufferIndex,
155 Shader& defaultShader,
156 const Matrix& modelViewMatrix,
157 const Matrix& viewMatrix,
158 const Matrix& projectionMatrix,
162 // @todo MESH_REWORK Fix when merging! :D
163 NewRenderer* renderer = dynamic_cast<NewRenderer*>(this);
166 // Get the shader from the material:
167 mShader = &renderer->mRenderDataProvider->GetShader();
170 // if mShader is NULL it means we're set to default
173 mShader = &defaultShader;
176 if( !CheckResources() )
178 // CheckResources() is overriden in derived classes.
179 // Prevents modify the GL state if resources are not ready and nothing is to be rendered.
183 // Calculate the MVP matrix first so we can do the culling test
184 const Matrix& modelMatrix = mDataProvider.GetModelMatrix( bufferIndex );
185 Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
187 // Get the program to use:
188 Program* program = mShader->GetProgram();
191 // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
192 program = defaultShader.GetProgram();
193 DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
196 DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*) &*mShader );
202 // Check culling (does not need the program to be in use)
203 if( cull && ! program->ModifiesGeometry() )
205 if( IsOutsideClipSpace( context, modelMatrix, gModelViewProjectionMatrix ) )
207 // don't do any further gl state changes as this renderer is not visible
212 // Take the program into use so we can send uniforms to it
215 DoSetCullFaceMode( context, bufferIndex );
217 DoSetBlending( context, bufferIndex );
219 // Ignore missing uniforms - custom shaders and flat color shaders don't have SAMPLER
220 // set projection and view matrix if program has not yet received them yet this frame
221 SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix, gModelViewProjectionMatrix );
224 GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
225 if( Program::UNIFORM_UNKNOWN != loc )
227 const Vector4& color = mDataProvider.GetRenderColor( bufferIndex );
228 program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
230 loc = program->GetUniformLocation(Program::UNIFORM_TIME_DELTA);
231 if( Program::UNIFORM_UNKNOWN != loc )
233 program->SetUniform1f( loc, frametime );
236 //@todo MESH_REWORK Remove after removing ImageRenderer
237 DoSetUniforms(context, bufferIndex, mShader, program );
239 // subclass rendering and actual draw call
240 DoRender( context, textureCache, bufferIndex, *program, modelViewMatrix, viewMatrix );
243 // can be overridden by deriving class
244 void Renderer::DoSetUniforms(Context& context, BufferIndex bufferIndex, Shader* shader, Program* program )
246 shader->SetUniforms( context, *program, bufferIndex );
249 // can be overridden by deriving class
250 void Renderer::DoSetCullFaceMode(Context& context, BufferIndex bufferIndex )
252 // Set face culling mode
253 context.CullFace( mCullFaceMode );
256 // can be overridden by deriving class
257 void Renderer::DoSetBlending(Context& context, BufferIndex bufferIndex )
259 // Enables/disables blending mode.
260 context.SetBlend( mUseBlend );
262 // Set the blend color
263 const Vector4* const customColor = mBlendingOptions.GetBlendColor();
266 context.SetCustomBlendColor( *customColor );
270 context.SetDefaultBlendColor();
273 // Set blend source & destination factors
274 context.BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
275 mBlendingOptions.GetBlendDestFactorRgb(),
276 mBlendingOptions.GetBlendSrcFactorAlpha(),
277 mBlendingOptions.GetBlendDestFactorAlpha() );
279 // Set blend equations
280 context.BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
281 mBlendingOptions.GetBlendEquationAlpha() );
285 Renderer::Renderer( NodeDataProvider& dataprovider )
286 : mDataProvider( dataprovider ),
287 mContextDELETEME(NULL),
288 mTextureCacheDELETEME( NULL ),
290 mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
292 mCullFaceMode( CullNone )
296 } // namespace SceneGraph
298 } // namespace Internal