Merge "BufferImage Fix for multiple area updates." into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-renderer.cpp
1 /*
2  * Copyright (c) 2015 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/render-renderer.h>
20
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/data-providers/node-data-provider.h>
27 #include <dali/public-api/actors/blending.h>
28 #include <dali/internal/common/image-sampler.h>
29 #include <dali/internal/render/renderers/render-new-renderer.h>
30
31 namespace Dali
32 {
33
34 namespace Internal
35 {
36
37 namespace
38 {
39
40 static Matrix gModelViewProjectionMatrix( false ); ///< a shared matrix to calculate the MVP matrix, dont want to store it in object to reduce storage overhead
41 static Matrix3 gNormalMatrix; ///< a shared matrix to calculate normal matrix, dont want to store it in object to reduce storage overhead
42
43 /**
44  * Helper to set view and projection matrices once per program
45  * @param program to set the matrices to
46  * @param modelMatrix to set
47  * @param viewMatrix to set
48  * @param projectionMatrix to set
49  * @param modelViewMatrix to set
50  * @param modelViewProjectionMatrix to set
51  */
52 inline void SetMatrices( Program& program,
53                          const Matrix& modelMatrix,
54                          const Matrix& viewMatrix,
55                          const Matrix& projectionMatrix,
56                          const Matrix& modelViewMatrix,
57                          const Matrix& modelViewProjectionMatrix )
58 {
59   GLint loc = program.GetUniformLocation(Program::UNIFORM_MODEL_MATRIX);
60   if( Program::UNIFORM_UNKNOWN != loc )
61   {
62     program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
63   }
64   loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
65   if( Program::UNIFORM_UNKNOWN != loc )
66   {
67     if( program.GetViewMatrix() != &viewMatrix )
68     {
69       program.SetViewMatrix( &viewMatrix );
70       program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
71     }
72   }
73   // set projection matrix if program has not yet received it this frame or if it is dirty
74   loc = program.GetUniformLocation( Program::UNIFORM_PROJECTION_MATRIX );
75   if( Program::UNIFORM_UNKNOWN != loc )
76   {
77     if( program.GetProjectionMatrix() != &projectionMatrix )
78     {
79       program.SetProjectionMatrix( &projectionMatrix );
80       program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
81     }
82   }
83   loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
84   if( Program::UNIFORM_UNKNOWN != loc )
85   {
86     program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
87   }
88
89   loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
90   if( Program::UNIFORM_UNKNOWN != loc )
91   {
92     program.SetUniformMatrix4fv( loc, 1, modelViewProjectionMatrix.AsFloat() );
93   }
94
95   loc = program.GetUniformLocation( Program::UNIFORM_NORMAL_MATRIX );
96   if( Program::UNIFORM_UNKNOWN != loc )
97   {
98     gNormalMatrix = modelViewMatrix;
99     gNormalMatrix.Invert();
100     gNormalMatrix.Transpose();
101     program.SetUniformMatrix3fv( loc, 1, gNormalMatrix.AsFloat() );
102   }
103 }
104
105 }
106
107 namespace Render
108 {
109
110 void Renderer::Initialize( Context& context, SceneGraph::TextureCache& textureCache )
111 {
112   mContext = &context;
113   mTextureCache = &textureCache;
114 }
115
116 Renderer::~Renderer()
117 {
118 }
119
120 void Renderer::SetShader( SceneGraph::Shader* shader )
121 {
122   mShader = shader;
123 }
124
125 void Renderer::SetCullFace( CullFaceMode mode )
126 {
127   DALI_ASSERT_DEBUG(mode >= CullNone && mode <= CullFrontAndBack);
128   mCullFaceMode = mode;
129 }
130
131 void Renderer::SetSampler( unsigned int samplerBitfield )
132 {
133   mSamplerBitfield = samplerBitfield;
134 }
135
136 void Renderer::Render( Context& context,
137                        SceneGraph::TextureCache& textureCache,
138                        BufferIndex bufferIndex,
139                        const SceneGraph::NodeDataProvider& node,
140                        SceneGraph::Shader& defaultShader,
141                        const Matrix& modelViewMatrix,
142                        const Matrix& viewMatrix,
143                        const Matrix& projectionMatrix,
144                        bool cull,
145                        bool blend )
146 {
147   NewRenderer* renderer = dynamic_cast<NewRenderer*>(this);
148
149   if( renderer )
150   {
151     // Get the shader from the material:
152     mShader = &renderer->mRenderDataProvider->GetShader();
153   }
154
155   // if mShader is NULL it means we're set to default
156   if( !mShader )
157   {
158     mShader = &defaultShader;
159   }
160
161   if( !CheckResources() )
162   {
163     // CheckResources() is overriden in derived classes.
164     // Prevents modify the GL state if resources are not ready and nothing is to be rendered.
165     return;
166   }
167
168   // Calculate the MVP matrix first so we can do the culling test
169   const Matrix& modelMatrix = node.GetModelMatrix( bufferIndex );
170   Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
171
172   // Get the program to use:
173   Program* program = mShader->GetProgram();
174   if( !program )
175   {
176     // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
177     program = defaultShader.GetProgram();
178     DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
179     if( !program )
180     {
181       DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*) &*mShader );
182       return;
183     }
184
185   }
186
187   // Check culling (does not need the program to be in use)
188   if( cull && ! program->ModifiesGeometry() )
189   {
190     if( IsOutsideClipSpace( context, gModelViewProjectionMatrix ) )
191     {
192       // don't do any further gl state changes as this renderer is not visible
193       return;
194     }
195   }
196
197   // Take the program into use so we can send uniforms to it
198   program->Use();
199
200   DoSetCullFaceMode( context, bufferIndex );
201
202   DoSetBlending( context, bufferIndex, blend );
203
204   // Ignore missing uniforms - custom shaders and flat color shaders don't have SAMPLER
205   // set projection and view matrix if program has not yet received them yet this frame
206   SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix, gModelViewProjectionMatrix );
207
208   // set color uniform
209   GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
210   if( Program::UNIFORM_UNKNOWN != loc )
211   {
212     const Vector4& color = node.GetRenderColor( bufferIndex );
213     program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
214   }
215
216   //@todo MESH_REWORK Remove after removing ImageRenderer
217   DoSetUniforms(context, bufferIndex, mShader, program );
218
219   // subclass rendering and actual draw call
220   DoRender( context, textureCache, node, bufferIndex, *program, modelViewMatrix, viewMatrix );
221 }
222
223 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
224 {
225   sortAttributes.shader = mShader;
226   sortAttributes.textureResourceId = Integration::InvalidResourceId;
227   sortAttributes.geometry = NULL;
228 }
229
230 // can be overridden by deriving class
231 void Renderer::DoSetUniforms(Context& context, BufferIndex bufferIndex, SceneGraph::Shader* shader, Program* program )
232 {
233   shader->SetUniforms( context, *program, bufferIndex );
234 }
235
236 // can be overridden by deriving class
237 void Renderer::DoSetCullFaceMode(Context& context, BufferIndex bufferIndex )
238 {
239   // Set face culling mode
240   context.CullFace( mCullFaceMode );
241 }
242
243 Renderer::Renderer()
244 : mContext(NULL),
245   mTextureCache( NULL ),
246   mShader( NULL ),
247   mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
248   mCullFaceMode( CullNone )
249 {
250 }
251
252 } // namespace SceneGraph
253
254 } // namespace Internal
255
256 } // namespace Dali