352d57866d3925e19f2eae53737dad2aaa7bf542
[platform/core/uifw/dali-core.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/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/renderers/render-data-provider.h>
28 #include <dali/public-api/actors/blending.h>
29 #include <dali/internal/common/image-sampler.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40
41 static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it locally to reduce storage overhead
42 static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it locally 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   mContext = &context;
114   mTextureCache = &textureCache;
115 }
116
117 Renderer::~Renderer()
118 {
119 }
120
121 void Renderer::SetShader( Shader* shader )
122 {
123   mShader = shader;
124 }
125
126 void Renderer::SetUseBlend( bool useBlend )
127 {
128   mUseBlend = useBlend;
129 }
130
131 void Renderer::SetBlendingOptions( unsigned int options )
132 {
133   mBlendingOptions.SetBitmask( options );
134 }
135
136 void Renderer::SetBlendColor( const Vector4& color )
137 {
138   mBlendingOptions.SetBlendColor( color );
139 }
140
141 void Renderer::SetCullFace( CullFaceMode mode )
142 {
143   DALI_ASSERT_DEBUG(mode >= CullNone && mode <= CullFrontAndBack);
144   mCullFaceMode = mode;
145 }
146
147 void Renderer::SetSampler( unsigned int samplerBitfield )
148 {
149   mSamplerBitfield = samplerBitfield;
150 }
151
152 void Renderer::Render( BufferIndex bufferIndex,
153                        const Matrix& modelViewMatrix,
154                        const Matrix& viewMatrix,
155                        const Matrix& projectionMatrix,
156                        float frametime,
157                        bool cull )
158 {
159   DALI_ASSERT_DEBUG( mContext && "Renderer::Render. Renderer not initialised!! (mContext == NULL)." );
160   DALI_ASSERT_DEBUG( mShader && "Renderer::Render. Shader not set!!" );
161
162   if( !CheckResources() )
163   {
164     // CheckResources() is overriden in derived classes.
165     // Prevents modify the GL state if resources are not ready and nothing is to be rendered.
166     return;
167   }
168
169   // Calculate the MVP matrix first so we can do the culling test
170   const Matrix& modelMatrix = mDataProvider.GetModelMatrix( bufferIndex );
171   Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
172
173   // Get the program to use
174   GeometryType geometryType=GEOMETRY_TYPE_IMAGE;
175   ShaderSubTypes subType=SHADER_DEFAULT;
176   ResolveGeometryTypes( bufferIndex, geometryType, subType );
177   unsigned int programIndex = 0;
178   Program& program = mShader->GetProgram( *mContext, geometryType, subType, programIndex );
179
180   // Check culling (does not need the program to be in use)
181   bool areVerticesFixed = program.AreVerticesFixed();
182   if( cull && areVerticesFixed )
183   {
184     if( IsOutsideClipSpace( modelMatrix, gModelViewProjectionMatrix ) )
185     {
186       // don't do any further gl state changes as this renderer is not visible
187       return;
188     }
189   }
190
191   // Take the program into use so we can send uniforms to it
192   program.Use();
193
194   // Enables/disables blending mode.
195   mContext->SetBlend( mUseBlend );
196
197   // Set face culling mode
198   mContext->CullFace( mCullFaceMode );
199
200   // Set the blend color
201   const Vector4* const customColor = mBlendingOptions.GetBlendColor();
202   if( customColor )
203   {
204     mContext->SetCustomBlendColor( *customColor );
205   }
206   else
207   {
208     mContext->SetDefaultBlendColor();
209   }
210
211   // Set blend source & destination factors
212   mContext->BlendFuncSeparate( mBlendingOptions.GetBlendSrcFactorRgb(),
213                                mBlendingOptions.GetBlendDestFactorRgb(),
214                                mBlendingOptions.GetBlendSrcFactorAlpha(),
215                                mBlendingOptions.GetBlendDestFactorAlpha() );
216
217   // Set blend equations
218   mContext->BlendEquationSeparate( mBlendingOptions.GetBlendEquationRgb(),
219                                    mBlendingOptions.GetBlendEquationAlpha() );
220
221   // Ignore missing uniforms - custom shaders and flat color shaders don't have SAMPLER
222   // set projection and view matrix if program has not yet received them yet this frame
223   SetMatrices( program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix, gModelViewProjectionMatrix );
224
225   // set color uniform
226   GLint loc = program.GetUniformLocation( Program::UNIFORM_COLOR );
227   if( Program::UNIFORM_UNKNOWN != loc )
228   {
229     const Vector4& color = mDataProvider.GetRenderColor( bufferIndex );
230     program.SetUniform4f( loc, color.r, color.g, color.b, color.a );
231   }
232   loc = program.GetUniformLocation(Program::UNIFORM_TIME_DELTA);
233   if( Program::UNIFORM_UNKNOWN != loc )
234   {
235     program.SetUniform1f( loc, frametime );
236   }
237
238   // set custom uniforms
239   mShader->SetUniforms( *mContext, program, bufferIndex, programIndex, subType );
240
241   // subclass rendering and actual draw call
242   DoRender( bufferIndex, program, modelViewMatrix, viewMatrix );
243 }
244
245 Renderer::Renderer( RenderDataProvider& dataprovider )
246 : mDataProvider( dataprovider ),
247   mContext( NULL ),
248
249   mTextureCache( NULL ),
250   mShader( NULL ),
251   mSamplerBitfield( ImageSampler::DefaultOptions() ),
252   mUseBlend( false ),
253   mCullFaceMode( CullNone )
254 {
255 }
256
257 } // namespace SceneGraph
258
259 } // namespace Internal
260
261 } // namespace Dali