Tizen 2.4.0 rev3 SDK Public Release
[framework/graphics/dali.git] / dali / internal / render / renderers / scene-graph-renderer.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/render/renderers/scene-graph-renderer.h>
20 #include <dali/internal/render/renderers/scene-graph-renderer-declarations.h>
21
22 // INTERNAL INCLUDES
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>
31
32 namespace Dali
33 {
34
35 namespace Internal
36 {
37
38 namespace
39 {
40
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
43
44 /**
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
52  */
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 )
59 {
60   GLint loc = program.GetUniformLocation(Program::UNIFORM_MODEL_MATRIX);
61   if( Program::UNIFORM_UNKNOWN != loc )
62   {
63     program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
64   }
65   loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
66   if( Program::UNIFORM_UNKNOWN != loc )
67   {
68     if( program.GetViewMatrix() != &viewMatrix )
69     {
70       program.SetViewMatrix( &viewMatrix );
71       program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
72     }
73   }
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 )
77   {
78     if( program.GetProjectionMatrix() != &projectionMatrix )
79     {
80       program.SetProjectionMatrix( &projectionMatrix );
81       program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
82     }
83   }
84   loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
85   if( Program::UNIFORM_UNKNOWN != loc )
86   {
87     program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
88   }
89
90   loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
91   if( Program::UNIFORM_UNKNOWN != loc )
92   {
93     program.SetUniformMatrix4fv( loc, 1, modelViewProjectionMatrix.AsFloat() );
94   }
95
96   loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
97   if( Program::UNIFORM_UNKNOWN != loc )
98   {
99     gNormalMatrix = modelViewMatrix;
100     gNormalMatrix.Invert();
101     gNormalMatrix.Transpose();
102     program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
103   }
104 }
105
106 }
107
108 namespace SceneGraph
109 {
110
111 void Renderer::Initialize( Context& context, TextureCache& textureCache )
112 {
113   mContextDELETEME = &context;
114   mTextureCacheDELETEME = &textureCache;
115 }
116
117 Renderer::~Renderer()
118 {
119 }
120
121 void Renderer::SetDataProvider( NodeDataProvider* dataProvider )
122 {
123   mDataProvider = dataProvider;
124 }
125
126 void Renderer::SetShader( Shader* shader )
127 {
128   mShader = shader;
129 }
130
131 void Renderer::SetCullFace( CullFaceMode mode )
132 {
133   DALI_ASSERT_DEBUG(mode >= CullNone && mode <= CullFrontAndBack);
134   mCullFaceMode = mode;
135 }
136
137 void Renderer::SetSampler( unsigned int samplerBitfield )
138 {
139   mSamplerBitfield = samplerBitfield;
140 }
141
142 void Renderer::Render( Context& context,
143                        TextureCache& textureCache,
144                        BufferIndex bufferIndex,
145                        Shader& defaultShader,
146                        const Matrix& modelViewMatrix,
147                        const Matrix& viewMatrix,
148                        const Matrix& projectionMatrix,
149                        float frametime,
150                        bool cull )
151 {
152   // @todo MESH_REWORK Fix when merging! :D
153   NewRenderer* renderer = dynamic_cast<NewRenderer*>(this);
154   if( renderer )
155   {
156     // Get the shader from the material:
157     mShader = &renderer->mRenderDataProvider->GetShader();
158   }
159
160   // if mShader is NULL it means we're set to default
161   if( !mShader )
162   {
163     mShader = &defaultShader;
164   }
165
166   if( !CheckResources() )
167   {
168     // CheckResources() is overriden in derived classes.
169     // Prevents modify the GL state if resources are not ready and nothing is to be rendered.
170     return;
171   }
172
173   // Calculate the MVP matrix first so we can do the culling test
174   const Matrix& modelMatrix = mDataProvider->GetModelMatrix( bufferIndex );
175   Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
176
177   // Get the program to use:
178   Program* program = mShader->GetProgram();
179   if( !program )
180   {
181     // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
182     program = defaultShader.GetProgram();
183     DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
184     if( !program )
185     {
186       DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*) &*mShader );
187       return;
188     }
189
190   }
191
192   // Check culling (does not need the program to be in use)
193   if( cull && ! program->ModifiesGeometry() )
194   {
195     if( IsOutsideClipSpace( context, modelMatrix, gModelViewProjectionMatrix ) )
196     {
197       // don't do any further gl state changes as this renderer is not visible
198       return;
199     }
200   }
201
202   // Take the program into use so we can send uniforms to it
203   program->Use();
204
205   DoSetCullFaceMode( context, bufferIndex );
206
207   DoSetBlending( context, bufferIndex );
208
209   // Ignore missing uniforms - custom shaders and flat color shaders don't have SAMPLER
210   // set projection and view matrix if program has not yet received them yet this frame
211   SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix, gModelViewProjectionMatrix );
212
213   // set color uniform
214   GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
215   if( Program::UNIFORM_UNKNOWN != loc )
216   {
217     const Vector4& color = mDataProvider->GetRenderColor( bufferIndex );
218     program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
219   }
220   loc = program->GetUniformLocation(Program::UNIFORM_TIME_DELTA);
221   if( Program::UNIFORM_UNKNOWN != loc )
222   {
223     program->SetUniform1f( loc, frametime );
224   }
225
226   //@todo MESH_REWORK Remove after removing ImageRenderer
227   DoSetUniforms(context, bufferIndex, mShader, program );
228
229   // subclass rendering and actual draw call
230   DoRender( context, textureCache, bufferIndex, *program, modelViewMatrix, viewMatrix );
231 }
232
233 // can be overridden by deriving class
234 void Renderer::DoSetUniforms(Context& context, BufferIndex bufferIndex, Shader* shader, Program* program )
235 {
236   shader->SetUniforms( context, *program, bufferIndex );
237 }
238
239 // can be overridden by deriving class
240 void Renderer::DoSetCullFaceMode(Context& context, BufferIndex bufferIndex )
241 {
242   // Set face culling mode
243   context.CullFace( mCullFaceMode );
244 }
245
246 Renderer::Renderer( NodeDataProvider& dataprovider )
247 : mDataProvider( &dataprovider ),
248   mContextDELETEME(NULL),
249   mTextureCacheDELETEME( NULL ),
250   mShader( NULL ),
251   mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
252   mCullFaceMode( CullNone )
253 {
254 }
255
256 } // namespace SceneGraph
257
258 } // namespace Internal
259
260 } // namespace Dali