Remove Geometry scene object
[platform/core/uifw/dali-core.git] / automated-tests / src / dali-internal / utc-Dali-Internal-FrustumCulling.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 #include <iostream>
19 #include <algorithm>
20 #include <stdlib.h>
21 #include <dali/public-api/dali-core.h>
22 #include <dali-test-suite-utils.h>
23 #include <dali/devel-api/object/property-buffer.h>
24 #include <dali/devel-api/rendering/geometry.h>
25 #include <dali/devel-api/rendering/texture-set.h>
26 #include <dali/devel-api/rendering/renderer.h>
27 #include <dali/devel-api/rendering/sampler.h>
28 #include <dali/devel-api/rendering/shader.h>
29
30 using namespace Dali;
31
32 #define MAKE_SHADER(A)#A
33
34 const char* VERTEX_SHADER = MAKE_SHADER(
35 attribute mediump vec2    aPosition;
36 attribute mediump vec2    aTexCoord;
37 uniform   mediump mat4    uMvpMatrix;
38 uniform   mediump vec3    uSize;
39 varying   mediump vec2    vTexCoord;
40
41 void main()
42 {
43   mediump vec4 vertexPosition = vec4(aPosition, 0.0, 1.0);
44   vertexPosition.xyz *= uSize;
45   vertexPosition = uMvpMatrix * vertexPosition;
46   vTexCoord = aTexCoord;
47   gl_Position = vertexPosition;
48 }
49 );
50
51 const char* FRAGMENT_SHADER = MAKE_SHADER(
52 uniform Sampler2D sTexture;
53 varying mediump vec2 vTexCoord;
54 void main()
55 {
56   gl_FragColor = texture2D( sTexture, vTexCoord );
57 }
58 );
59
60 Geometry CreateGeometry()
61 {
62   const float halfQuadSize = .5f;
63   struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
64   TexturedQuadVertex texturedQuadVertexData[4] = {
65     { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
66     { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
67     { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
68     { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
69
70   Property::Map texturedQuadVertexFormat;
71   texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
72   texturedQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
73   PropertyBuffer texturedQuadVertices = PropertyBuffer::New( texturedQuadVertexFormat );
74   texturedQuadVertices.SetData( texturedQuadVertexData, 4 );
75
76   // Create indices
77   unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
78
79   // Create the geometry object
80   Geometry texturedQuadGeometry = Geometry::New();
81   texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
82   texturedQuadGeometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
83
84   return texturedQuadGeometry;
85 }
86
87 Actor CreateMeshActorToStage( TestApplication& application, Vector3 parentOrigin = ParentOrigin::CENTER, Vector3 anchorPoint = AnchorPoint::CENTER, Shader::ShaderHints shaderHints = Shader::HINT_NONE )
88 {
89   PixelBuffer* pixelBuffer = new PixelBuffer[ 4 ];
90   BufferImage image = BufferImage::New( pixelBuffer, 1, 1 );
91
92   Geometry geometry = CreateGeometry();
93   Shader shader = Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, shaderHints );
94   TextureSet textureSet = TextureSet::New();
95   textureSet.SetImage( 0u, image );
96   Renderer renderer = Renderer::New( geometry, shader );
97   renderer.SetTextures( textureSet );
98
99   Actor meshActor = Actor::New();
100   meshActor.AddRenderer( renderer );
101   meshActor.SetSize( Vector3( 400.0f, 400.0f, 0.1f ) );
102   meshActor.SetParentOrigin( parentOrigin );
103   meshActor.SetAnchorPoint( anchorPoint );
104   Stage::GetCurrent().Add( meshActor );
105
106   application.SendNotification();
107   application.Render( 16 );
108
109   return meshActor;
110 }
111
112 bool GetCameraDepths( TestApplication& application, float& nearPlane, float& farPlane, float& cameraDepth )
113 {
114   RenderTaskList renderTasks = Stage::GetCurrent().GetRenderTaskList();
115   CameraActor cameraActor;
116   for( unsigned int i = 0; i < renderTasks.GetTaskCount(); ++i )
117   {
118     RenderTask task = renderTasks.GetTask( i );
119     cameraActor = task.GetCameraActor();
120     if( cameraActor )
121     {
122       break;
123     }
124   }
125   if( cameraActor )
126   {
127     application.SendNotification();
128     application.Render( 16 );
129
130     nearPlane = cameraActor.GetNearClippingPlane();
131     farPlane = cameraActor.GetFarClippingPlane();
132     cameraDepth = cameraActor.GetCurrentPosition().z;
133   }
134
135   return !!cameraActor;
136 }
137
138 int UtcFrustumCullN(void)
139 {
140   TestApplication application;
141   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
142   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
143   drawTrace.Enable( true );
144
145   CreateMeshActorToStage( application );
146
147   drawTrace.Reset();
148   application.SendNotification();
149   application.Render( 16 );
150
151   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
152
153   END_TEST;
154 }
155
156 int UtcFrustumLeftCullP(void)
157 {
158   TestApplication application;
159   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
160   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
161   drawTrace.Enable( true );
162
163   float offset = -0.01f;
164   Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_RIGHT );
165
166   float radius = meshActor.GetTargetSize().Length() * 0.5f;
167   Vector2 stageSize = Stage::GetCurrent().GetSize();
168   meshActor.SetParentOrigin( Vector3( -radius / stageSize.width + offset, 0.5f, 0.5f ) );
169   meshActor.SetAnchorPoint( AnchorPoint::CENTER );
170
171   drawTrace.Reset();
172   application.SendNotification();
173   application.Render( 16 );
174
175   // This will be sphere culled
176   DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
177
178   END_TEST;
179 }
180
181 int UtcFrustumLeftCullN(void)
182 {
183   TestApplication application;
184   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
185   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
186   drawTrace.Enable( true );
187
188   float offset = 0.01f;
189   Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_RIGHT );
190
191   drawTrace.Reset();
192   application.SendNotification();
193   application.Render( 16 );
194
195   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
196
197   END_TEST;
198 }
199
200 int UtcFrustumRightCullP(void)
201 {
202   TestApplication application;
203   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
204   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
205   drawTrace.Enable( true );
206
207   float offset = 1.01f;
208   Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_LEFT );
209
210   float radius = meshActor.GetTargetSize().Length() * 0.5f;
211   Vector2 stageSize = Stage::GetCurrent().GetSize();
212
213   meshActor.SetParentOrigin( Vector3( radius / stageSize.width + offset, 0.5f, 0.5f ) );
214   meshActor.SetAnchorPoint( AnchorPoint::CENTER );
215
216   drawTrace.Reset();
217   application.SendNotification();
218   application.Render( 16 );
219
220   // This will be sphere culled
221   DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
222
223   END_TEST;
224 }
225
226 int UtcFrustumRightCullN(void)
227 {
228   TestApplication application;
229   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
230   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
231   drawTrace.Enable( true );
232
233   float offset = 0.99f;
234   Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_LEFT );
235
236   drawTrace.Reset();
237   application.SendNotification();
238   application.Render( 16 );
239
240   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
241
242   END_TEST;
243 }
244
245 int UtcFrustumTopCullP(void)
246 {
247   TestApplication application;
248   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
249   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
250   drawTrace.Enable( true );
251
252   float offset = -0.01f;
253   Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::BOTTOM_CENTER );
254
255   float radius = meshActor.GetTargetSize().Length() * 0.5f;
256   Vector2 stageSize = Stage::GetCurrent().GetSize();
257
258   meshActor.SetParentOrigin( Vector3( 0.5f, -radius / stageSize.width + offset, 0.5f ) );
259   meshActor.SetAnchorPoint( AnchorPoint::CENTER );
260
261   drawTrace.Reset();
262   application.SendNotification();
263   application.Render( 16 );
264
265   // This will be sphere culled
266   DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
267
268   END_TEST;
269 }
270
271 int UtcFrustumTopCullN(void)
272 {
273   TestApplication application;
274   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
275   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
276   drawTrace.Enable( true );
277
278   float offset = 0.01f;
279   Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::BOTTOM_CENTER );
280
281   drawTrace.Reset();
282   application.SendNotification();
283   application.Render( 16 );
284
285   // This will be box culled
286   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
287
288   END_TEST;
289 }
290
291 int UtcFrustumBottomCullP(void)
292 {
293   TestApplication application;
294   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
295   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
296   drawTrace.Enable( true );
297
298   float offset = 1.01f;
299   Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::TOP_CENTER );
300
301   float radius = meshActor.GetTargetSize().Length() * 0.5f;
302   Vector2 stageSize = Stage::GetCurrent().GetSize();
303
304   meshActor.SetParentOrigin( Vector3( 0.5f, radius / stageSize.width + offset, 0.5f ) );
305   meshActor.SetAnchorPoint( AnchorPoint::CENTER );
306
307   drawTrace.Reset();
308   application.SendNotification();
309   application.Render( 16 );
310
311   // This will be sphere culled
312   DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
313
314   END_TEST;
315 }
316
317 int UtcFrustumBottomCullN(void)
318 {
319   TestApplication application;
320   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
321   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
322   drawTrace.Enable( true );
323
324   float offset = 0.99f;
325   Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::TOP_CENTER );
326
327   drawTrace.Reset();
328   application.SendNotification();
329   application.Render( 16 );
330
331   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
332
333   END_TEST;
334 }
335
336 int UtcFrustumNearCullP(void)
337 {
338   TestApplication application;
339   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
340   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
341   drawTrace.Enable( true );
342
343   float nearPlane, farPlane, cameraDepth;
344   DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
345
346   Actor meshActor = CreateMeshActorToStage( application );
347   Vector3 meshPosition = meshActor.GetCurrentPosition();
348
349   float radius = meshActor.GetTargetSize().Length() * 0.5f;
350   float offset = radius + 0.1f;
351   meshPosition.z = cameraDepth - nearPlane + offset;
352   meshActor.SetPosition( meshPosition );
353
354   drawTrace.Reset();
355   application.SendNotification();
356   application.Render( 16 );
357
358   // This will be sphere culled
359   DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
360
361   END_TEST;
362 }
363
364 int UtcFrustumNearCullN(void)
365 {
366   TestApplication application;
367   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
368   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
369   drawTrace.Enable( true );
370
371   float nearPlane, farPlane, cameraDepth;
372   DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
373
374   Actor meshActor = CreateMeshActorToStage( application );
375   Vector3 meshPosition = meshActor.GetCurrentPosition();
376
377   float offset = meshActor.GetTargetSize().z - 0.1f;
378   meshPosition.z = cameraDepth - nearPlane + offset;
379   meshActor.SetPosition( meshPosition );
380
381   drawTrace.Reset();
382   application.SendNotification();
383   application.Render( 16 );
384
385   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
386
387   END_TEST;
388 }
389
390 int UtcFrustumFarCullP(void)
391 {
392   TestApplication application;
393   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
394   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
395   drawTrace.Enable( true );
396
397   float nearPlane, farPlane, cameraDepth;
398   DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
399
400   Actor meshActor = CreateMeshActorToStage( application );
401   Vector3 meshPosition = meshActor.GetCurrentPosition();
402
403   float radius = meshActor.GetTargetSize().Length() * 0.5f;
404   float offset = radius + 0.1f;
405   meshPosition.z = cameraDepth - farPlane - offset;
406   meshActor.SetPosition( meshPosition );
407
408   drawTrace.Reset();
409   application.SendNotification();
410   application.Render( 16 );
411
412   // This will be sphere culled
413   DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
414
415   END_TEST;
416 }
417
418 int UtcFrustumFarCullN(void)
419 {
420   TestApplication application;
421   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
422   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
423   drawTrace.Enable( true );
424
425   float nearPlane, farPlane, cameraDepth;
426   DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
427
428   Actor meshActor = CreateMeshActorToStage( application );
429   Vector3 meshPosition = meshActor.GetCurrentPosition();
430
431   float offset = meshActor.GetTargetSize().z - 0.1f;
432   meshPosition.z = cameraDepth - farPlane - offset;
433   meshActor.SetPosition( meshPosition );
434
435   drawTrace.Reset();
436   application.SendNotification();
437   application.Render( 16 );
438
439   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
440
441   END_TEST;
442 }
443
444 int UtcFrustumCullDisabledP(void)
445 {
446   TestApplication application;
447   TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
448   TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
449   drawTrace.Enable( true );
450
451   CreateMeshActorToStage( application, Vector3( 7.0f, 0.5f, 0.5f ), AnchorPoint::CENTER, Shader::HINT_MODIFIES_GEOMETRY );
452
453   drawTrace.Reset();
454   application.SendNotification();
455   application.Render( 16 );
456
457   // This should not be culled
458   DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
459
460   END_TEST;
461 }