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