Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / angle / tests / angle_tests / BufferDataTest.cpp
1 #include "ANGLETest.h"
2
3 #include <cstdint>
4
5 class BufferDataTest : public ANGLETest
6 {
7   protected:
8     BufferDataTest()
9         : mBuffer(0),
10           mProgram(0),
11           mAttribLocation(-1)
12     {
13         setWindowWidth(16);
14         setWindowHeight(16);
15         setConfigRedBits(8);
16         setConfigGreenBits(8);
17         setConfigBlueBits(8);
18         setConfigAlphaBits(8);
19         setConfigDepthBits(24);
20     }
21
22     virtual void SetUp()
23     {
24         ANGLETest::SetUp();
25
26         const char * vsSource = SHADER_SOURCE
27         (
28             attribute vec4 position;
29             attribute float in_attrib;
30             varying float v_attrib;
31             void main()
32             {
33                 v_attrib = in_attrib;
34                 gl_Position = position;
35             }
36         );
37
38         const char * fsSource = SHADER_SOURCE
39         (
40             precision mediump float;
41             varying float v_attrib;
42             void main()
43             {
44                 gl_FragColor = vec4(v_attrib, 0, 0, 1);
45             }
46         );
47
48         glGenBuffers(1, &mBuffer);
49         ASSERT_NE(mBuffer, 0U);
50
51         mProgram = compileProgram(vsSource, fsSource);
52         ASSERT_NE(mProgram, 0U);
53
54         mAttribLocation = glGetAttribLocation(mProgram, "in_attrib");
55         ASSERT_NE(mAttribLocation, -1);
56
57         glClearColor(0, 0, 0, 0);
58         glClearDepthf(0.0);
59         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
60
61         glDisable(GL_DEPTH_TEST);
62
63         ASSERT_GL_NO_ERROR();
64     }
65
66     virtual void TearDown()
67     {
68         glDeleteBuffers(1, &mBuffer);
69         glDeleteProgram(mProgram);
70
71         ANGLETest::TearDown();
72     }
73
74     GLuint mBuffer;
75     GLuint mProgram;
76     GLint mAttribLocation;
77 };
78
79 TEST_F(BufferDataTest, NULLData)
80 {
81     glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
82     EXPECT_GL_NO_ERROR();
83
84     const int numIterations = 128;
85     for (int i = 0; i < numIterations; ++i)
86     {
87         GLsizei bufferSize = sizeof(GLfloat) * (i + 1);
88         glBufferData(GL_ARRAY_BUFFER, bufferSize, NULL, GL_STATIC_DRAW);
89         EXPECT_GL_NO_ERROR();
90
91         for (int j = 0; j < bufferSize; j++)
92         {
93             for (int k = 0; k < bufferSize - j; k++)
94             {
95                 glBufferSubData(GL_ARRAY_BUFFER, k, j, NULL);
96                 EXPECT_GL_NO_ERROR();
97             }
98         }
99     }
100 }
101
102 TEST_F(BufferDataTest, ZeroNonNULLData)
103 {
104     glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
105     EXPECT_GL_NO_ERROR();
106
107     char *zeroData = new char[0];
108     glBufferData(GL_ARRAY_BUFFER, 0, zeroData, GL_STATIC_DRAW);
109     EXPECT_GL_NO_ERROR();
110
111     glBufferSubData(GL_ARRAY_BUFFER, 0, 0, zeroData);
112     EXPECT_GL_NO_ERROR();
113
114     delete [] zeroData;
115 }
116
117 TEST_F(BufferDataTest, HugeSetDataShouldNotCrash)
118 {
119     glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
120     EXPECT_GL_NO_ERROR();
121
122     GLsizei allocSize = std::numeric_limits<GLsizei>::max() >> 2;
123
124     uint8_t *data = NULL;
125     while (data == NULL && allocSize >= 4)
126     {
127         data = new (std::nothrow) uint8_t[allocSize];
128
129         if (data == NULL)
130         {
131             allocSize <<= 1;
132         }
133     }
134
135     ASSERT_NE(static_cast<uint8_t*>(NULL), data);
136     memset(data, 0, allocSize);
137
138     float * fValue = reinterpret_cast<float*>(data);
139     for (unsigned int f = 0; f < 6; f++)
140     {
141         fValue[f] = 1.0f;
142     }
143
144     glBufferData(GL_ARRAY_BUFFER, allocSize, data, GL_STATIC_DRAW);
145
146     GLenum error = glGetError();
147     if (error == GL_NO_ERROR)
148     {
149         // If we didn't fail because of an out of memory error, try drawing a quad
150         // using the large buffer
151
152         // DISABLED because it takes a long time, but left for posterity
153
154         //glUseProgram(mProgram);
155         //glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 4, NULL);
156         //glEnableVertexAttribArray(mAttribLocation);
157         //glBindBuffer(GL_ARRAY_BUFFER, 0);
158         //drawQuad(mProgram, "position", 0.5f);
159         //swapBuffers();
160
161         //// Draw operations can also generate out-of-memory, which is in-spec
162         //error = glGetError();
163         //if (error == GL_NO_ERROR)
164         //{
165         //    GLint viewportSize[4];
166         //    glGetIntegerv(GL_VIEWPORT, viewportSize);
167
168         //    GLint midPixelX = (viewportSize[0] + viewportSize[2]) / 2;
169         //    GLint midPixelY = (viewportSize[1] + viewportSize[3]) / 2;
170
171         //    EXPECT_PIXEL_EQ(midPixelX, midPixelY, 255, 0, 0, 255);
172         //}
173         //else
174         //{
175         //    EXPECT_EQ(GL_OUT_OF_MEMORY, error);
176         //}
177     }
178     else
179     {
180         EXPECT_EQ(GL_OUT_OF_MEMORY, error);
181     }
182
183     delete[] data;
184 }
185
186 class IndexedBufferCopyTest : public ANGLETest
187 {
188   protected:
189     IndexedBufferCopyTest()
190     {
191         setWindowWidth(16);
192         setWindowHeight(16);
193         setConfigRedBits(8);
194         setConfigGreenBits(8);
195         setConfigBlueBits(8);
196         setConfigAlphaBits(8);
197         setConfigDepthBits(24);
198         setClientVersion(3);
199     }
200
201     virtual void SetUp()
202     {
203         ANGLETest::SetUp();
204
205         const char * vsSource = SHADER_SOURCE
206         (
207             attribute vec3 in_attrib;
208             varying vec3 v_attrib;
209             void main()
210             {
211                 v_attrib = in_attrib;
212                 gl_Position = vec4(0.0, 0.0, 0.5, 1.0);
213                 gl_PointSize = 100.0;
214             }
215         );
216
217         const char * fsSource = SHADER_SOURCE
218         (
219             precision mediump float;
220             varying vec3 v_attrib;
221             void main()
222             {
223                 gl_FragColor = vec4(v_attrib, 1);
224             }
225         );
226
227         glGenBuffers(2, mBuffers);
228         ASSERT_NE(mBuffers[0], 0U);
229         ASSERT_NE(mBuffers[1], 0U);
230
231         glGenBuffers(1, &mElementBuffer);
232         ASSERT_NE(mElementBuffer, 0U);
233
234         mProgram = compileProgram(vsSource, fsSource);
235         ASSERT_NE(mProgram, 0U);
236
237         mAttribLocation = glGetAttribLocation(mProgram, "in_attrib");
238         ASSERT_NE(mAttribLocation, -1);
239
240         glClearColor(0, 0, 0, 0);
241         glDisable(GL_DEPTH_TEST);
242         glClear(GL_COLOR_BUFFER_BIT);
243
244         ASSERT_GL_NO_ERROR();
245     }
246
247     virtual void TearDown()
248     {
249         glDeleteBuffers(2, mBuffers);
250         glDeleteBuffers(1, &mElementBuffer);
251         glDeleteProgram(mProgram);
252
253         ANGLETest::TearDown();
254     }
255
256     GLuint mBuffers[2];
257     GLuint mElementBuffer;
258     GLuint mProgram;
259     GLint mAttribLocation;
260 };
261
262 // The following test covers an ANGLE bug where our index ranges
263 // weren't updated from CopyBufferSubData calls
264 // https://code.google.com/p/angleproject/issues/detail?id=709
265 TEST_F(IndexedBufferCopyTest, IndexRangeBug)
266 {
267     unsigned char vertexData[] = { 255, 0, 0, 0, 0, 0 };
268     unsigned int indexData[] = { 0, 1 };
269
270     glBindBuffer(GL_ARRAY_BUFFER, mBuffers[0]);
271     glBufferData(GL_ARRAY_BUFFER, sizeof(char) * 6, vertexData, GL_STATIC_DRAW);
272
273     glUseProgram(mProgram);
274     glVertexAttribPointer(mAttribLocation, 3, GL_UNSIGNED_BYTE, GL_TRUE, 3, NULL);
275     glEnableVertexAttribArray(mAttribLocation);
276
277     ASSERT_GL_NO_ERROR();
278
279     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBuffer);
280     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 1, indexData, GL_STATIC_DRAW);
281
282     glUseProgram(mProgram);
283
284     ASSERT_GL_NO_ERROR();
285
286     glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, NULL);
287
288     EXPECT_GL_NO_ERROR();
289     EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
290
291     glBindBuffer(GL_COPY_READ_BUFFER, mBuffers[1]);
292     glBufferData(GL_COPY_READ_BUFFER, 4, &indexData[1], GL_STATIC_DRAW);
293
294     glBindBuffer(GL_COPY_WRITE_BUFFER, mElementBuffer);
295
296     glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, sizeof(int));
297
298     ASSERT_GL_NO_ERROR();
299
300     glClear(GL_COLOR_BUFFER_BIT);
301     EXPECT_PIXEL_EQ(0, 0, 0, 0, 0, 0);
302
303     unsigned char newData[] = { 0, 255, 0 };
304     glBufferSubData(GL_ARRAY_BUFFER, 3, 3, newData);
305
306     glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, NULL);
307
308     EXPECT_GL_NO_ERROR();
309     EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255);
310 }