1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 // MatrixBuiltinsTest.cpp: Tests basic usage of builtin matrix operations.
8 
9 #include "test_utils/ANGLETest.h"
10 #include "test_utils/gl_raii.h"
11 
12 #include "common/matrix_utils.h"
13 #include "util/random_utils.h"
14 
15 #include <stdint.h>
16 
17 using namespace angle;
18 
19 class MatrixBuiltinsTest : public ANGLETest
20 {
21   protected:
MatrixBuiltinsTest()22     MatrixBuiltinsTest()
23     {
24         setWindowWidth(32);
25         setWindowHeight(32);
26         setConfigRedBits(8);
27         setConfigGreenBits(8);
28         setConfigBlueBits(8);
29         setConfigAlphaBits(8);
30         setConfigDepthBits(24);
31     }
32 };
33 
34 // Test rotation and check the matrix for closeness to a rotation matrix.
TEST_P(MatrixBuiltinsTest,Rotate)35 TEST_P(MatrixBuiltinsTest, Rotate)
36 {
37     constexpr float angle = 90.0f;
38     constexpr float x     = 1.0f;
39     constexpr float y     = 1.0f;
40     constexpr float z     = 1.0f;
41 
42     angle::Mat4 testMatrix = angle::Mat4::Rotate(angle, angle::Vector3(x, y, z));
43 
44     glRotatef(angle, x, y, z);
45     EXPECT_GL_NO_ERROR();
46 
47     angle::Mat4 outputMatrix;
48     glGetFloatv(GL_MODELVIEW_MATRIX, outputMatrix.data());
49     EXPECT_GL_NO_ERROR();
50 
51     EXPECT_TRUE(testMatrix.nearlyEqual(0.00001f, outputMatrix));
52 }
53 
54 // Test translation and check the matrix for closeness to a translation matrix.
TEST_P(MatrixBuiltinsTest,Translate)55 TEST_P(MatrixBuiltinsTest, Translate)
56 {
57     constexpr float x = 1.0f;
58     constexpr float y = 1.0f;
59     constexpr float z = 1.0f;
60 
61     angle::Mat4 testMatrix = angle::Mat4::Translate(angle::Vector3(x, y, z));
62 
63     glTranslatef(1.0f, 0.0f, 0.0f);
64     glTranslatef(0.0f, 1.0f, 0.0f);
65     glTranslatef(0.0f, 0.0f, 1.0f);
66     EXPECT_GL_NO_ERROR();
67 
68     angle::Mat4 outputMatrix;
69     glGetFloatv(GL_MODELVIEW_MATRIX, outputMatrix.data());
70     EXPECT_GL_NO_ERROR();
71 
72     EXPECT_TRUE(testMatrix.nearlyEqual(0.00001f, outputMatrix));
73 }
74 
75 // Test scale and check the matrix for closeness to a scale matrix.
TEST_P(MatrixBuiltinsTest,Scale)76 TEST_P(MatrixBuiltinsTest, Scale)
77 {
78     constexpr float x = 3.0f;
79     constexpr float y = 9.0f;
80     constexpr float z = 27.0f;
81 
82     angle::Mat4 testMatrix = angle::Mat4::Scale(angle::Vector3(x, y, z));
83 
84     glScalef(3.0f, 3.0f, 3.0f);
85     glScalef(1.0f, 3.0f, 3.0f);
86     glScalef(1.0f, 1.0f, 3.0f);
87     EXPECT_GL_NO_ERROR();
88 
89     angle::Mat4 outputMatrix;
90     glGetFloatv(GL_MODELVIEW_MATRIX, outputMatrix.data());
91     EXPECT_GL_NO_ERROR();
92 
93     EXPECT_TRUE(testMatrix.nearlyEqual(0.00001f, outputMatrix));
94 }
95 
96 // Test frustum projection and check the matrix values.
TEST_P(MatrixBuiltinsTest,Frustum)97 TEST_P(MatrixBuiltinsTest, Frustum)
98 {
99 
100     constexpr float l = -1.0f;
101     constexpr float r = 1.0f;
102     constexpr float b = -1.0f;
103     constexpr float t = 1.0f;
104     constexpr float n = 0.1f;
105     constexpr float f = 1.0f;
106 
107     angle::Mat4 testMatrix = angle::Mat4::Frustum(l, r, b, t, n, f);
108 
109     glFrustumf(l, r, b, t, n, f);
110     EXPECT_GL_NO_ERROR();
111 
112     angle::Mat4 outputMatrix;
113     glGetFloatv(GL_MODELVIEW_MATRIX, outputMatrix.data());
114     EXPECT_GL_NO_ERROR();
115 
116     EXPECT_TRUE(testMatrix.nearlyEqual(0.00001f, outputMatrix));
117 }
118 
119 // Test orthographic projection and check the matrix values.
TEST_P(MatrixBuiltinsTest,Ortho)120 TEST_P(MatrixBuiltinsTest, Ortho)
121 {
122     constexpr float l = -1.0f;
123     constexpr float r = 1.0f;
124     constexpr float b = -1.0f;
125     constexpr float t = 1.0f;
126     constexpr float n = 0.1f;
127     constexpr float f = 1.0f;
128 
129     angle::Mat4 testMatrix = angle::Mat4::Ortho(l, r, b, t, n, f);
130 
131     glOrthof(l, r, b, t, n, f);
132     EXPECT_GL_NO_ERROR();
133 
134     angle::Mat4 outputMatrix;
135     glGetFloatv(GL_MODELVIEW_MATRIX, outputMatrix.data());
136     EXPECT_GL_NO_ERROR();
137 
138     EXPECT_TRUE(testMatrix.nearlyEqual(0.00001f, outputMatrix));
139 }
140 
141 // Test that GL_INVALID_VALUE is issued if potential divide by zero situations happen for
142 // glFrustumf.
TEST_P(MatrixBuiltinsTest,FrustumNegative)143 TEST_P(MatrixBuiltinsTest, FrustumNegative)
144 {
145     glFrustumf(1.0f, 1.0f, 0.0f, 1.0f, 0.1f, 1.0f);
146     EXPECT_GL_ERROR(GL_INVALID_VALUE);
147     glFrustumf(0.0f, 1.0f, 1.0f, 1.0f, 0.1f, 1.0f);
148     EXPECT_GL_ERROR(GL_INVALID_VALUE);
149     glFrustumf(0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f);
150     EXPECT_GL_ERROR(GL_INVALID_VALUE);
151     glFrustumf(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f);
152     EXPECT_GL_ERROR(GL_INVALID_VALUE);
153     glFrustumf(0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f);
154     EXPECT_GL_ERROR(GL_INVALID_VALUE);
155     glFrustumf(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
156     EXPECT_GL_ERROR(GL_INVALID_VALUE);
157 }
158 
159 // Test that GL_INVALID_VALUE is issued if potential divide by zero situations happen for glOrthof.
TEST_P(MatrixBuiltinsTest,OrthoNegative)160 TEST_P(MatrixBuiltinsTest, OrthoNegative)
161 {
162     glOrthof(1.0f, 1.0f, 0.0f, 1.0f, 0.1f, 1.0f);
163     EXPECT_GL_ERROR(GL_INVALID_VALUE);
164     glOrthof(0.0f, 1.0f, 1.0f, 1.0f, 0.1f, 1.0f);
165     EXPECT_GL_ERROR(GL_INVALID_VALUE);
166     glOrthof(0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f);
167     EXPECT_GL_ERROR(GL_INVALID_VALUE);
168 }
169 
170 // Test that glOrtho{fx} don't issue error result if near or far is negative.
TEST_P(MatrixBuiltinsTest,OrthoNegativeNearFar)171 TEST_P(MatrixBuiltinsTest, OrthoNegativeNearFar)
172 {
173     glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 0.0f);
174     EXPECT_GL_NO_ERROR();
175     glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, 0.0f, -1.0f);
176     EXPECT_GL_NO_ERROR();
177     glOrthox(-1, 1, -1, 1, -1, 0);
178     EXPECT_GL_NO_ERROR();
179     glOrthox(-1, 1, -1, 1, 0, -1);
180     EXPECT_GL_NO_ERROR();
181 }
182 
183 ANGLE_INSTANTIATE_TEST_ES1(MatrixBuiltinsTest);
184