f827e3a1787f3d41e34060d584385e30b7587538
[platform/framework/web/crosswalk.git] / src / third_party / angle / tests / angle_tests / LineLoopTest.cpp
1 #include "ANGLETest.h"
2
3 // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
4 typedef ::testing::Types<TFT<Gles::Two, Rend::D3D11>, TFT<Gles::Two, Rend::D3D9>> TestFixtureTypes;
5 TYPED_TEST_CASE(LineLoopTest, TestFixtureTypes);
6
7 template<typename T>
8 class LineLoopTest : public ANGLETest
9 {
10 protected:
11     LineLoopTest() : ANGLETest(T::GetGlesMajorVersion(), T::GetRequestedRenderer())
12     {
13         setWindowWidth(256);
14         setWindowHeight(256);
15         setConfigRedBits(8);
16         setConfigGreenBits(8);
17         setConfigBlueBits(8);
18         setConfigAlphaBits(8);
19     }
20
21     virtual void SetUp()
22     {
23         ANGLETest::SetUp();
24
25         const std::string vsSource = SHADER_SOURCE
26         (
27             attribute highp vec4 position;
28             attribute highp vec4 in_color;
29
30             varying highp vec4 color;
31
32             void main(void)
33             {
34                 gl_Position = position;
35                 color = in_color;
36             }
37         );
38
39         const std::string fsSource = SHADER_SOURCE
40         (
41             varying highp vec4 color;
42             void main(void)
43             {
44                 gl_FragColor = color;
45             }
46         );
47
48         mProgram = CompileProgram(vsSource, fsSource);
49         if (mProgram == 0)
50         {
51             FAIL() << "shader compilation failed.";
52         }
53
54         mPositionLocation = glGetAttribLocation(mProgram, "position");
55         mColorLocation = glGetAttribLocation(mProgram, "in_color");
56
57         glBlendFunc(GL_ONE, GL_ONE);
58         glEnable(GL_BLEND);
59
60         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
61         glClear(GL_COLOR_BUFFER_BIT);
62
63         ASSERT_GL_NO_ERROR();
64     }
65
66     virtual void TearDown()
67     {
68         glDeleteProgram(mProgram);
69
70         ANGLETest::TearDown();
71     }
72
73     void runTest(GLenum indexType, GLubyte indexBuffer, const GLvoid *indexPtr)
74     {
75         static const GLfloat loopPositions[] =
76         {
77              0.0f,  0.0f,
78              0.0f,  0.0f,
79              0.0f,  0.0f,
80              0.0f,  0.0f,
81              0.0f,  0.0f,
82              0.0f,  0.0f,
83             -0.5f, -0.5f,
84             -0.5f,  0.5f,
85              0.5f,  0.5f,
86              0.5f, -0.5f
87         };
88
89         static const GLfloat stripPositions[] =
90         {
91             -0.5f, -0.5f,
92             -0.5f,  0.5f,
93              0.5f,  0.5f,
94              0.5f, -0.5f
95         };
96         static const GLubyte stripIndices[] =
97         {
98             2, 0, 3, 1, 2
99         };
100
101         glUseProgram(mProgram);
102         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
103         glEnableVertexAttribArray(mPositionLocation);
104         glVertexAttribPointer(mPositionLocation, 2, GL_FLOAT, GL_FALSE, 0, loopPositions);
105         glUniform4f(mColorLocation, 0.0f, 0.0f, 1.0f, 1.0f);
106         glDrawElements(GL_LINE_LOOP, 4, indexType, indexPtr);
107
108         glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
109         glVertexAttribPointer(mPositionLocation, 2, GL_FLOAT, GL_FALSE, 0, stripPositions);
110         glUniform4f(mColorLocation, 0, 1, 0, 1);
111         glDrawElements(GL_LINE_STRIP, 5, GL_UNSIGNED_BYTE, stripIndices);
112
113         std::vector<GLubyte> pixels(getWindowWidth() * getWindowHeight() * 4);
114         glReadPixels(0, 0, getWindowWidth(), getWindowHeight(), GL_RGBA, GL_UNSIGNED_BYTE, &pixels[0]);
115
116         for (int y = 0; y < getWindowHeight(); y++)
117         {
118             for (int x = 0; x < getWindowWidth(); x++)
119             {
120                 const GLubyte* pixel = &pixels[0] + ((y * getWindowWidth() + x) * 4);
121
122                 EXPECT_EQ(pixel[0], 0);
123                 EXPECT_EQ(pixel[1], pixel[2]);
124                 EXPECT_EQ(pixel[3], 255);
125             }
126         }
127     }
128
129     GLuint mProgram;
130     GLint mPositionLocation;
131     GLint mColorLocation;
132 };
133
134 TYPED_TEST(LineLoopTest, LineLoopUByteIndices)
135 {
136     static const GLubyte indices[] = { 0, 7, 6, 9, 8, 0 };
137     runTest(GL_UNSIGNED_BYTE, 0, indices + 1);
138 }
139
140 TYPED_TEST(LineLoopTest, LineLoopUShortIndices)
141 {
142     static const GLushort indices[] = { 0, 7, 6, 9, 8, 0 };
143     runTest(GL_UNSIGNED_SHORT, 0, indices + 1);
144 }
145
146 TYPED_TEST(LineLoopTest, LineLoopUIntIndices)
147 {
148     if (!extensionEnabled("GL_OES_element_index_uint"))
149     {
150         return;
151     }
152
153     static const GLuint indices[] = { 0, 7, 6, 9, 8, 0 };
154     runTest(GL_UNSIGNED_INT, 0, indices + 1);
155 }
156
157 TYPED_TEST(LineLoopTest, LineLoopUByteIndexBuffer)
158 {
159     static const GLubyte indices[] = { 0, 7, 6, 9, 8, 0 };
160
161     GLuint buf;
162     glGenBuffers(1, &buf);
163     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf);
164     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
165
166     runTest(GL_UNSIGNED_BYTE, buf, reinterpret_cast<const void *>(sizeof(GLubyte)));
167
168     glDeleteBuffers(1, &buf);
169 }
170
171 TYPED_TEST(LineLoopTest, LineLoopUShortIndexBuffer)
172 {
173     static const GLushort indices[] = { 0, 7, 6, 9, 8, 0 };
174
175     GLuint buf;
176     glGenBuffers(1, &buf);
177     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf);
178     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
179
180     runTest(GL_UNSIGNED_SHORT, buf, reinterpret_cast<const void *>(sizeof(GLushort)));
181
182     glDeleteBuffers(1, &buf);
183 }
184
185 TYPED_TEST(LineLoopTest, LineLoopUIntIndexBuffer)
186 {
187     if (!extensionEnabled("GL_OES_element_index_uint"))
188     {
189         return;
190     }
191
192     static const GLuint indices[] = { 0, 7, 6, 9, 8, 0 };
193
194     GLuint buf;
195     glGenBuffers(1, &buf);
196     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buf);
197     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
198
199     runTest(GL_UNSIGNED_INT, buf, reinterpret_cast<const void *>(sizeof(GLuint)));
200
201     glDeleteBuffers(1, &buf);
202 }