Remove render thread culling as its not used for new mesh
[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 {
58   GLint loc = program.GetUniformLocation( Program::UNIFORM_MODEL_MATRIX );
59   if( Program::UNIFORM_UNKNOWN != loc )
60   {
61     program.SetUniformMatrix4fv( loc, 1, modelMatrix.AsFloat() );
62   }
63   loc = program.GetUniformLocation( Program::UNIFORM_VIEW_MATRIX );
64   if( Program::UNIFORM_UNKNOWN != loc )
65   {
66     if( program.GetViewMatrix() != &viewMatrix )
67     {
68       program.SetViewMatrix( &viewMatrix );
69       program.SetUniformMatrix4fv( loc, 1, viewMatrix.AsFloat() );
70     }
71   }
72   // set projection matrix if program has not yet received it this frame or if it is dirty
73   loc = program.GetUniformLocation( Program::UNIFORM_PROJECTION_MATRIX );
74   if( Program::UNIFORM_UNKNOWN != loc )
75   {
76     if( program.GetProjectionMatrix() != &projectionMatrix )
77     {
78       program.SetProjectionMatrix( &projectionMatrix );
79       program.SetUniformMatrix4fv( loc, 1, projectionMatrix.AsFloat() );
80     }
81   }
82   loc = program.GetUniformLocation(Program::UNIFORM_MODELVIEW_MATRIX);
83   if( Program::UNIFORM_UNKNOWN != loc )
84   {
85     program.SetUniformMatrix4fv( loc, 1, modelViewMatrix.AsFloat() );
86   }
87
88   loc = program.GetUniformLocation( Program::UNIFORM_MVP_MATRIX );
89   if( Program::UNIFORM_UNKNOWN != loc )
90   {
91     Matrix::Multiply( gModelViewProjectionMatrix, modelViewMatrix, projectionMatrix );
92     program.SetUniformMatrix4fv( loc, 1, gModelViewProjectionMatrix.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, Render::UniformNameCache& uniformNameCache )
111 {
112   mContext = &context;
113   mTextureCache = &textureCache;
114   mUniformNameCache = &uniformNameCache;
115 }
116
117 Renderer::~Renderer()
118 {
119 }
120
121 void Renderer::SetShader( SceneGraph::Shader* shader )
122 {
123   mShader = shader;
124 }
125
126 void Renderer::SetCullFace( CullFaceMode mode )
127 {
128   DALI_ASSERT_DEBUG(mode >= CullNone && mode <= CullFrontAndBack);
129   mCullFaceMode = mode;
130 }
131
132 void Renderer::SetSampler( unsigned int samplerBitfield )
133 {
134   mSamplerBitfield = samplerBitfield;
135 }
136
137 void Renderer::Render( Context& context,
138                        SceneGraph::TextureCache& textureCache,
139                        BufferIndex bufferIndex,
140                        const SceneGraph::NodeDataProvider& node,
141                        SceneGraph::Shader& defaultShader,
142                        const Matrix& modelViewMatrix,
143                        const Matrix& viewMatrix,
144                        const Matrix& projectionMatrix,
145                        bool cull,
146                        bool blend )
147 {
148   NewRenderer* renderer = GetNewRenderer(); // avoid a dynamic cast per item per frame
149
150   if( renderer )
151   {
152     // Get the shader from the material:
153     mShader = &renderer->mRenderDataProvider->GetShader();
154   }
155
156   // if mShader is NULL it means we're set to default
157   if( !mShader )
158   {
159     mShader = &defaultShader;
160   }
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   // Get the program to use:
170   Program* program = mShader->GetProgram();
171   if( !program )
172   {
173     // if program is NULL it means this is a custom shader with non matching geometry type so we need to use default shaders program
174     program = defaultShader.GetProgram();
175     DALI_ASSERT_DEBUG( program && "Default shader should always have a program available." );
176     if( !program )
177     {
178       DALI_LOG_ERROR( "Failed to get program for shader at address %p.", (void*) &*mShader );
179       return;
180     }
181   }
182
183   // Take the program into use so we can send uniforms to it
184   program->Use();
185
186   DoSetCullFaceMode( context, bufferIndex );
187
188   DoSetBlending( context, bufferIndex, blend );
189
190   // Ignore missing uniforms - custom shaders and flat color shaders don't have SAMPLER
191   // set projection and view matrix if program has not yet received them yet this frame
192   const Matrix& modelMatrix = node.GetModelMatrix( bufferIndex );
193   SetMatrices( *program, modelMatrix, viewMatrix, projectionMatrix, modelViewMatrix );
194
195   // set color uniform
196   GLint loc = program->GetUniformLocation( Program::UNIFORM_COLOR );
197   if( Program::UNIFORM_UNKNOWN != loc )
198   {
199     const Vector4& color = node.GetRenderColor( bufferIndex );
200     program->SetUniform4f( loc, color.r, color.g, color.b, color.a );
201   }
202
203   //@todo MESH_REWORK Remove after removing ImageRenderer
204   DoSetUniforms(context, bufferIndex, mShader, program );
205
206   // subclass rendering and actual draw call
207   DoRender( context, textureCache, node, bufferIndex, *program, modelViewMatrix, viewMatrix );
208 }
209
210 void Renderer::SetSortAttributes( BufferIndex bufferIndex, SceneGraph::RendererWithSortAttributes& sortAttributes ) const
211 {
212   sortAttributes.shader = mShader;
213   sortAttributes.textureResourceId = Integration::InvalidResourceId;
214   sortAttributes.geometry = NULL;
215 }
216
217 // can be overridden by deriving class
218 void Renderer::DoSetUniforms(Context& context, BufferIndex bufferIndex, SceneGraph::Shader* shader, Program* program )
219 {
220   shader->SetUniforms( context, *program, bufferIndex );
221 }
222
223 // can be overridden by deriving class
224 void Renderer::DoSetCullFaceMode(Context& context, BufferIndex bufferIndex )
225 {
226   // Set face culling mode
227   context.CullFace( mCullFaceMode );
228 }
229
230 Renderer::Renderer()
231 : mContext(NULL),
232   mTextureCache( NULL ),
233   mUniformNameCache( NULL ),
234   mShader( NULL ),
235   mSamplerBitfield( ImageSampler::PackBitfield( FilterMode::DEFAULT, FilterMode::DEFAULT ) ),
236   mCullFaceMode( CullNone )
237 {
238 }
239
240 } // namespace SceneGraph
241
242 } // namespace Internal
243
244 } // namespace Dali