2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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/material.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>
32 #define MAKE_SHADER(A)#A
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;
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;
51 const char* FRAGMENT_SHADER = MAKE_SHADER(
52 uniform Sampler2D sTexture;
53 varying mediump vec2 vTexCoord;
56 gl_FragColor = texture2D( sTexture, vTexCoord );
60 Geometry CreateGeometry()
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) } };
70 Property::Map texturedQuadVertexFormat;
71 texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
72 texturedQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
73 PropertyBuffer texturedQuadVertices = PropertyBuffer::New( texturedQuadVertexFormat, 4 );
74 texturedQuadVertices.SetData(texturedQuadVertexData);
77 unsigned int indexData[6] = { 0, 3, 1, 0, 2, 3 };
78 Property::Map indexFormat;
79 indexFormat["indices"] = Property::INTEGER;
80 PropertyBuffer indices = PropertyBuffer::New( indexFormat, sizeof(indexData)/sizeof(indexData[0]) );
81 indices.SetData(indexData);
83 // Create the geometry object
84 Geometry texturedQuadGeometry = Geometry::New();
85 texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
86 texturedQuadGeometry.SetIndexBuffer( indices );
88 return texturedQuadGeometry;
91 Actor CreateMeshActorToStage( TestApplication& application, Vector3 parentOrigin = ParentOrigin::CENTER, Vector3 anchorPoint = AnchorPoint::CENTER, Shader::ShaderHints shaderHints = Shader::HINT_NONE )
93 PixelBuffer* pixelBuffer = new PixelBuffer[ 4 ];
94 BufferImage image = BufferImage::New( pixelBuffer, 1, 1 );
96 Geometry geometry = CreateGeometry();
97 Material material = Material::New( Shader::New( VERTEX_SHADER, FRAGMENT_SHADER, shaderHints ) );
98 material.AddTexture( image, "sTexture" );
99 Renderer renderer = Renderer::New( geometry, material );
101 Actor meshActor = Actor::New();
102 meshActor.AddRenderer( renderer );
103 meshActor.SetSize( Vector3( 400.0f, 400.0f, 0.1f ) );
104 meshActor.SetParentOrigin( parentOrigin );
105 meshActor.SetAnchorPoint( anchorPoint );
106 Stage::GetCurrent().Add( meshActor );
108 application.SendNotification();
109 application.Render( 16 );
114 bool GetCameraDepths( TestApplication& application, float& nearPlane, float& farPlane, float& cameraDepth )
116 RenderTaskList renderTasks = Stage::GetCurrent().GetRenderTaskList();
117 CameraActor cameraActor;
118 for( unsigned int i = 0; i < renderTasks.GetTaskCount(); ++i )
120 RenderTask task = renderTasks.GetTask( i );
121 cameraActor = task.GetCameraActor();
129 application.SendNotification();
130 application.Render( 16 );
132 nearPlane = cameraActor.GetNearClippingPlane();
133 farPlane = cameraActor.GetFarClippingPlane();
134 cameraDepth = cameraActor.GetCurrentPosition().z;
137 return !!cameraActor;
140 int UtcFrustumCullN(void)
142 TestApplication application;
143 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
144 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
145 drawTrace.Enable( true );
147 CreateMeshActorToStage( application );
150 application.SendNotification();
151 application.Render( 16 );
153 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
158 int UtcFrustumLeftCullP(void)
160 TestApplication application;
161 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
162 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
163 drawTrace.Enable( true );
165 float offset = -0.01f;
166 Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_RIGHT );
169 application.SendNotification();
170 application.Render( 16 );
172 // This will be box culled
173 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
175 float radius = meshActor.GetTargetSize().Length() * 0.5f;
176 Vector2 stageSize = Stage::GetCurrent().GetSize();
178 meshActor.SetParentOrigin( Vector3( -radius / stageSize.width + offset, 0.5f, 0.5f ) );
179 meshActor.SetAnchorPoint( AnchorPoint::CENTER );
182 application.SendNotification();
183 application.Render( 16 );
185 // This will be sphere culled
186 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
191 int UtcFrustumLeftCullN(void)
193 TestApplication application;
194 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
195 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
196 drawTrace.Enable( true );
198 float offset = 0.01f;
199 Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_RIGHT );
202 application.SendNotification();
203 application.Render( 16 );
205 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
210 int UtcFrustumRightCullP(void)
212 TestApplication application;
213 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
214 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
215 drawTrace.Enable( true );
217 float offset = 1.01f;
218 Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_LEFT );
221 application.SendNotification();
222 application.Render( 16 );
224 // This will be box culled
225 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
227 float radius = meshActor.GetTargetSize().Length() * 0.5f;
228 Vector2 stageSize = Stage::GetCurrent().GetSize();
230 meshActor.SetParentOrigin( Vector3( radius / stageSize.width + offset, 0.5f, 0.5f ) );
231 meshActor.SetAnchorPoint( AnchorPoint::CENTER );
234 application.SendNotification();
235 application.Render( 16 );
237 // This will be sphere culled
238 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
243 int UtcFrustumRightCullN(void)
245 TestApplication application;
246 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
247 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
248 drawTrace.Enable( true );
250 float offset = 0.99f;
251 Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_LEFT );
254 application.SendNotification();
255 application.Render( 16 );
257 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
262 int UtcFrustumTopCullP(void)
264 TestApplication application;
265 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
266 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
267 drawTrace.Enable( true );
269 float offset = -0.01f;
270 Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::BOTTOM_CENTER );
273 application.SendNotification();
274 application.Render( 16 );
276 // This will be box culled
277 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
279 float radius = meshActor.GetTargetSize().Length() * 0.5f;
280 Vector2 stageSize = Stage::GetCurrent().GetSize();
282 meshActor.SetParentOrigin( Vector3( 0.5f, -radius / stageSize.width + offset, 0.5f ) );
283 meshActor.SetAnchorPoint( AnchorPoint::CENTER );
286 application.SendNotification();
287 application.Render( 16 );
289 // This will be sphere culled
290 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
295 int UtcFrustumTopCullN(void)
297 TestApplication application;
298 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
299 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
300 drawTrace.Enable( true );
302 float offset = 0.01f;
303 Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::BOTTOM_CENTER );
306 application.SendNotification();
307 application.Render( 16 );
309 // This will be box culled
310 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
315 int UtcFrustumBottomCullP(void)
317 TestApplication application;
318 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
319 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
320 drawTrace.Enable( true );
322 float offset = 1.01f;
323 Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::TOP_CENTER );
326 application.SendNotification();
327 application.Render( 16 );
329 // This will be box culled
330 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
332 float radius = meshActor.GetTargetSize().Length() * 0.5f;
333 Vector2 stageSize = Stage::GetCurrent().GetSize();
335 meshActor.SetParentOrigin( Vector3( 0.5f, radius / stageSize.width + offset, 0.5f ) );
336 meshActor.SetAnchorPoint( AnchorPoint::CENTER );
339 application.SendNotification();
340 application.Render( 16 );
342 // This will be sphere culled
343 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
348 int UtcFrustumBottomCullN(void)
350 TestApplication application;
351 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
352 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
353 drawTrace.Enable( true );
355 float offset = 0.99f;
356 Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::TOP_CENTER );
359 application.SendNotification();
360 application.Render( 16 );
362 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
367 int UtcFrustumNearCullP(void)
369 TestApplication application;
370 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
371 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
372 drawTrace.Enable( true );
374 float nearPlane, farPlane, cameraDepth;
375 DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
377 Actor meshActor = CreateMeshActorToStage( application );
378 Vector3 meshPosition = meshActor.GetCurrentPosition();
380 float radius = meshActor.GetTargetSize().Length() * 0.5f;
381 float offset = ( meshActor.GetTargetSize().z + radius ) * 0.5f; //midpoint between AABB and sphere
382 meshPosition.z = cameraDepth - nearPlane + offset;
383 meshActor.SetPosition( meshPosition );
386 application.SendNotification();
387 application.Render( 16 );
389 // This will be box culled
390 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
392 offset = radius + 0.1f;
393 meshPosition.z = cameraDepth - nearPlane + offset;
394 meshActor.SetPosition( meshPosition );
397 application.SendNotification();
398 application.Render( 16 );
400 // This will be sphere culled
401 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
406 int UtcFrustumNearCullN(void)
408 TestApplication application;
409 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
410 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
411 drawTrace.Enable( true );
413 float nearPlane, farPlane, cameraDepth;
414 DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
416 Actor meshActor = CreateMeshActorToStage( application );
417 Vector3 meshPosition = meshActor.GetCurrentPosition();
419 float offset = meshActor.GetTargetSize().z - 0.1f;
420 meshPosition.z = cameraDepth - nearPlane + offset;
421 meshActor.SetPosition( meshPosition );
424 application.SendNotification();
425 application.Render( 16 );
427 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
432 int UtcFrustumFarCullP(void)
434 TestApplication application;
435 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
436 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
437 drawTrace.Enable( true );
439 float nearPlane, farPlane, cameraDepth;
440 DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
442 Actor meshActor = CreateMeshActorToStage( application );
443 Vector3 meshPosition = meshActor.GetCurrentPosition();
445 float radius = meshActor.GetTargetSize().Length() * 0.5f;
446 float offset = ( meshActor.GetTargetSize().z + radius ) * 0.5f; //midpoint between AABB and sphere
448 meshPosition.z = cameraDepth - farPlane - offset;
449 meshActor.SetPosition( meshPosition );
452 application.SendNotification();
453 application.Render( 16 );
455 // This will be box culled
456 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
458 offset = radius + 0.1f;
459 meshPosition.z = cameraDepth - farPlane - offset;
460 meshActor.SetPosition( meshPosition );
463 application.SendNotification();
464 application.Render( 16 );
466 // This will be sphere culled
467 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
472 int UtcFrustumFarCullN(void)
474 TestApplication application;
475 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
476 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
477 drawTrace.Enable( true );
479 float nearPlane, farPlane, cameraDepth;
480 DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
482 Actor meshActor = CreateMeshActorToStage( application );
483 Vector3 meshPosition = meshActor.GetCurrentPosition();
485 float offset = meshActor.GetTargetSize().z - 0.1f;
486 meshPosition.z = cameraDepth - farPlane - offset;
487 meshActor.SetPosition( meshPosition );
490 application.SendNotification();
491 application.Render( 16 );
493 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
498 int UtcFrustumCullDisabledP(void)
500 TestApplication application;
501 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
502 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
503 drawTrace.Enable( true );
505 CreateMeshActorToStage( application, Vector3( 7.0f, 0.5f, 0.5f ), AnchorPoint::CENTER, Shader::HINT_MODIFIES_GEOMETRY );
508 application.SendNotification();
509 application.Render( 16 );
511 // This should not be culled
512 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );