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/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>
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 );
74 texturedQuadVertices.SetData( texturedQuadVertexData, 4 );
77 unsigned short indexData[6] = { 0, 3, 1, 0, 2, 3 };
79 // Create the geometry object
80 Geometry texturedQuadGeometry = Geometry::New();
81 texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
82 texturedQuadGeometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0]) );
84 return texturedQuadGeometry;
87 Actor CreateMeshActorToStage( TestApplication& application, Vector3 parentOrigin = ParentOrigin::CENTER, Vector3 anchorPoint = AnchorPoint::CENTER, Shader::ShaderHints shaderHints = Shader::HINT_NONE )
89 PixelBuffer* pixelBuffer = new PixelBuffer[ 4 ];
90 BufferImage image = BufferImage::New( pixelBuffer, 1, 1 );
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 );
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 );
106 application.SendNotification();
107 application.Render( 16 );
112 bool GetCameraDepths( TestApplication& application, float& nearPlane, float& farPlane, float& cameraDepth )
114 RenderTaskList renderTasks = Stage::GetCurrent().GetRenderTaskList();
115 CameraActor cameraActor;
116 for( unsigned int i = 0; i < renderTasks.GetTaskCount(); ++i )
118 RenderTask task = renderTasks.GetTask( i );
119 cameraActor = task.GetCameraActor();
127 application.SendNotification();
128 application.Render( 16 );
130 nearPlane = cameraActor.GetNearClippingPlane();
131 farPlane = cameraActor.GetFarClippingPlane();
132 cameraDepth = cameraActor.GetCurrentPosition().z;
135 return !!cameraActor;
138 int UtcFrustumCullN(void)
140 TestApplication application;
141 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
142 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
143 drawTrace.Enable( true );
145 CreateMeshActorToStage( application );
148 application.SendNotification();
149 application.Render( 16 );
151 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
156 int UtcFrustumLeftCullP(void)
158 TestApplication application;
159 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
160 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
161 drawTrace.Enable( true );
163 float offset = -0.01f;
164 Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_RIGHT );
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 );
172 application.SendNotification();
173 application.Render( 16 );
175 // This will be sphere culled
176 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
181 int UtcFrustumLeftCullN(void)
183 TestApplication application;
184 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
185 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
186 drawTrace.Enable( true );
188 float offset = 0.01f;
189 Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_RIGHT );
192 application.SendNotification();
193 application.Render( 16 );
195 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
200 int UtcFrustumRightCullP(void)
202 TestApplication application;
203 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
204 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
205 drawTrace.Enable( true );
207 float offset = 1.01f;
208 Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_LEFT );
210 float radius = meshActor.GetTargetSize().Length() * 0.5f;
211 Vector2 stageSize = Stage::GetCurrent().GetSize();
213 meshActor.SetParentOrigin( Vector3( radius / stageSize.width + offset, 0.5f, 0.5f ) );
214 meshActor.SetAnchorPoint( AnchorPoint::CENTER );
217 application.SendNotification();
218 application.Render( 16 );
220 // This will be sphere culled
221 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
226 int UtcFrustumRightCullN(void)
228 TestApplication application;
229 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
230 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
231 drawTrace.Enable( true );
233 float offset = 0.99f;
234 Actor meshActor = CreateMeshActorToStage( application, Vector3( offset, 0.5f, 0.5f ), AnchorPoint::CENTER_LEFT );
237 application.SendNotification();
238 application.Render( 16 );
240 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
245 int UtcFrustumTopCullP(void)
247 TestApplication application;
248 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
249 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
250 drawTrace.Enable( true );
252 float offset = -0.01f;
253 Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::BOTTOM_CENTER );
255 float radius = meshActor.GetTargetSize().Length() * 0.5f;
256 Vector2 stageSize = Stage::GetCurrent().GetSize();
258 meshActor.SetParentOrigin( Vector3( 0.5f, -radius / stageSize.width + offset, 0.5f ) );
259 meshActor.SetAnchorPoint( AnchorPoint::CENTER );
262 application.SendNotification();
263 application.Render( 16 );
265 // This will be sphere culled
266 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
271 int UtcFrustumTopCullN(void)
273 TestApplication application;
274 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
275 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
276 drawTrace.Enable( true );
278 float offset = 0.01f;
279 Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::BOTTOM_CENTER );
282 application.SendNotification();
283 application.Render( 16 );
285 // This will be box culled
286 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
291 int UtcFrustumBottomCullP(void)
293 TestApplication application;
294 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
295 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
296 drawTrace.Enable( true );
298 float offset = 1.01f;
299 Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::TOP_CENTER );
301 float radius = meshActor.GetTargetSize().Length() * 0.5f;
302 Vector2 stageSize = Stage::GetCurrent().GetSize();
304 meshActor.SetParentOrigin( Vector3( 0.5f, radius / stageSize.width + offset, 0.5f ) );
305 meshActor.SetAnchorPoint( AnchorPoint::CENTER );
308 application.SendNotification();
309 application.Render( 16 );
311 // This will be sphere culled
312 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
317 int UtcFrustumBottomCullN(void)
319 TestApplication application;
320 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
321 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
322 drawTrace.Enable( true );
324 float offset = 0.99f;
325 Actor meshActor = CreateMeshActorToStage( application, Vector3( 0.5f, offset, 0.5f ), AnchorPoint::TOP_CENTER );
328 application.SendNotification();
329 application.Render( 16 );
331 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
336 int UtcFrustumNearCullP(void)
338 TestApplication application;
339 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
340 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
341 drawTrace.Enable( true );
343 float nearPlane, farPlane, cameraDepth;
344 DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
346 Actor meshActor = CreateMeshActorToStage( application );
347 Vector3 meshPosition = meshActor.GetCurrentPosition();
349 float radius = meshActor.GetTargetSize().Length() * 0.5f;
350 float offset = radius + 0.1f;
351 meshPosition.z = cameraDepth - nearPlane + offset;
352 meshActor.SetPosition( meshPosition );
355 application.SendNotification();
356 application.Render( 16 );
358 // This will be sphere culled
359 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
364 int UtcFrustumNearCullN(void)
366 TestApplication application;
367 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
368 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
369 drawTrace.Enable( true );
371 float nearPlane, farPlane, cameraDepth;
372 DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
374 Actor meshActor = CreateMeshActorToStage( application );
375 Vector3 meshPosition = meshActor.GetCurrentPosition();
377 float offset = meshActor.GetTargetSize().z - 0.1f;
378 meshPosition.z = cameraDepth - nearPlane + offset;
379 meshActor.SetPosition( meshPosition );
382 application.SendNotification();
383 application.Render( 16 );
385 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
390 int UtcFrustumFarCullP(void)
392 TestApplication application;
393 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
394 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
395 drawTrace.Enable( true );
397 float nearPlane, farPlane, cameraDepth;
398 DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
400 Actor meshActor = CreateMeshActorToStage( application );
401 Vector3 meshPosition = meshActor.GetCurrentPosition();
403 float radius = meshActor.GetTargetSize().Length() * 0.5f;
404 float offset = radius + 0.1f;
405 meshPosition.z = cameraDepth - farPlane - offset;
406 meshActor.SetPosition( meshPosition );
409 application.SendNotification();
410 application.Render( 16 );
412 // This will be sphere culled
413 DALI_TEST_CHECK( !drawTrace.FindMethod( "DrawElements" ) );
418 int UtcFrustumFarCullN(void)
420 TestApplication application;
421 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
422 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
423 drawTrace.Enable( true );
425 float nearPlane, farPlane, cameraDepth;
426 DALI_TEST_CHECK( GetCameraDepths( application, nearPlane, farPlane, cameraDepth ) );
428 Actor meshActor = CreateMeshActorToStage( application );
429 Vector3 meshPosition = meshActor.GetCurrentPosition();
431 float offset = meshActor.GetTargetSize().z - 0.1f;
432 meshPosition.z = cameraDepth - farPlane - offset;
433 meshActor.SetPosition( meshPosition );
436 application.SendNotification();
437 application.Render( 16 );
439 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );
444 int UtcFrustumCullDisabledP(void)
446 TestApplication application;
447 TestGlAbstraction& glAbstraction = application.GetGlAbstraction();
448 TraceCallStack& drawTrace = glAbstraction.GetDrawTrace();
449 drawTrace.Enable( true );
451 CreateMeshActorToStage( application, Vector3( 7.0f, 0.5f, 0.5f ), AnchorPoint::CENTER, Shader::HINT_MODIFIES_GEOMETRY );
454 application.SendNotification();
455 application.Render( 16 );
457 // This should not be culled
458 DALI_TEST_CHECK( drawTrace.FindMethod( "DrawElements" ) );