5 class BufferDataTest : public ANGLETest
16 setConfigGreenBits(8);
18 setConfigAlphaBits(8);
19 setConfigDepthBits(24);
26 const char * vsSource = SHADER_SOURCE
28 attribute vec4 position;
29 attribute float in_attrib;
30 varying float v_attrib;
34 gl_Position = position;
38 const char * fsSource = SHADER_SOURCE
40 precision mediump float;
41 varying float v_attrib;
44 gl_FragColor = vec4(v_attrib, 0, 0, 1);
48 glGenBuffers(1, &mBuffer);
49 ASSERT_NE(mBuffer, 0U);
51 mProgram = CompileProgram(vsSource, fsSource);
52 ASSERT_NE(mProgram, 0U);
54 mAttribLocation = glGetAttribLocation(mProgram, "in_attrib");
55 ASSERT_NE(mAttribLocation, -1);
57 glClearColor(0, 0, 0, 0);
59 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
61 glDisable(GL_DEPTH_TEST);
66 virtual void TearDown()
68 glDeleteBuffers(1, &mBuffer);
69 glDeleteProgram(mProgram);
71 ANGLETest::TearDown();
76 GLint mAttribLocation;
79 TEST_F(BufferDataTest, NULLData)
81 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
84 const int numIterations = 128;
85 for (int i = 0; i < numIterations; ++i)
87 GLsizei bufferSize = sizeof(GLfloat) * (i + 1);
88 glBufferData(GL_ARRAY_BUFFER, bufferSize, NULL, GL_STATIC_DRAW);
91 for (int j = 0; j < bufferSize; j++)
93 for (int k = 0; k < bufferSize - j; k++)
95 glBufferSubData(GL_ARRAY_BUFFER, k, j, NULL);
102 TEST_F(BufferDataTest, ZeroNonNULLData)
104 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
105 EXPECT_GL_NO_ERROR();
107 char *zeroData = new char[0];
108 glBufferData(GL_ARRAY_BUFFER, 0, zeroData, GL_STATIC_DRAW);
109 EXPECT_GL_NO_ERROR();
111 glBufferSubData(GL_ARRAY_BUFFER, 0, 0, zeroData);
112 EXPECT_GL_NO_ERROR();
117 TEST_F(BufferDataTest, NULLResolvedData)
119 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
120 glBufferData(GL_ARRAY_BUFFER, 128, NULL, GL_DYNAMIC_DRAW);
122 glUseProgram(mProgram);
123 glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 4, NULL);
124 glEnableVertexAttribArray(mAttribLocation);
125 glBindBuffer(GL_ARRAY_BUFFER, 0);
127 drawQuad(mProgram, "position", 0.5f);
130 TEST_F(BufferDataTest, HugeSetDataShouldNotCrash)
132 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
133 EXPECT_GL_NO_ERROR();
135 GLsizei allocSize = std::numeric_limits<GLsizei>::max() >> 2;
137 uint8_t *data = NULL;
138 while (data == NULL && allocSize >= 4)
140 data = new (std::nothrow) uint8_t[allocSize];
148 ASSERT_NE(static_cast<uint8_t*>(NULL), data);
149 memset(data, 0, allocSize);
151 float * fValue = reinterpret_cast<float*>(data);
152 for (unsigned int f = 0; f < 6; f++)
157 glBufferData(GL_ARRAY_BUFFER, allocSize, data, GL_STATIC_DRAW);
159 GLenum error = glGetError();
160 if (error == GL_NO_ERROR)
162 // If we didn't fail because of an out of memory error, try drawing a quad
163 // using the large buffer
165 // DISABLED because it takes a long time, but left for posterity
167 //glUseProgram(mProgram);
168 //glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 4, NULL);
169 //glEnableVertexAttribArray(mAttribLocation);
170 //glBindBuffer(GL_ARRAY_BUFFER, 0);
171 //drawQuad(mProgram, "position", 0.5f);
174 //// Draw operations can also generate out-of-memory, which is in-spec
175 //error = glGetError();
176 //if (error == GL_NO_ERROR)
178 // GLint viewportSize[4];
179 // glGetIntegerv(GL_VIEWPORT, viewportSize);
181 // GLint midPixelX = (viewportSize[0] + viewportSize[2]) / 2;
182 // GLint midPixelY = (viewportSize[1] + viewportSize[3]) / 2;
184 // EXPECT_PIXEL_EQ(midPixelX, midPixelY, 255, 0, 0, 255);
188 // EXPECT_EQ(GL_OUT_OF_MEMORY, error);
193 EXPECT_EQ(GL_OUT_OF_MEMORY, error);
199 class IndexedBufferCopyTest : public ANGLETest
202 IndexedBufferCopyTest()
207 setConfigGreenBits(8);
208 setConfigBlueBits(8);
209 setConfigAlphaBits(8);
210 setConfigDepthBits(24);
218 const char * vsSource = SHADER_SOURCE
220 attribute vec3 in_attrib;
221 varying vec3 v_attrib;
224 v_attrib = in_attrib;
225 gl_Position = vec4(0.0, 0.0, 0.5, 1.0);
226 gl_PointSize = 100.0;
230 const char * fsSource = SHADER_SOURCE
232 precision mediump float;
233 varying vec3 v_attrib;
236 gl_FragColor = vec4(v_attrib, 1);
240 glGenBuffers(2, mBuffers);
241 ASSERT_NE(mBuffers[0], 0U);
242 ASSERT_NE(mBuffers[1], 0U);
244 glGenBuffers(1, &mElementBuffer);
245 ASSERT_NE(mElementBuffer, 0U);
247 mProgram = CompileProgram(vsSource, fsSource);
248 ASSERT_NE(mProgram, 0U);
250 mAttribLocation = glGetAttribLocation(mProgram, "in_attrib");
251 ASSERT_NE(mAttribLocation, -1);
253 glClearColor(0, 0, 0, 0);
254 glDisable(GL_DEPTH_TEST);
255 glClear(GL_COLOR_BUFFER_BIT);
257 ASSERT_GL_NO_ERROR();
260 virtual void TearDown()
262 glDeleteBuffers(2, mBuffers);
263 glDeleteBuffers(1, &mElementBuffer);
264 glDeleteProgram(mProgram);
266 ANGLETest::TearDown();
270 GLuint mElementBuffer;
272 GLint mAttribLocation;
275 // The following test covers an ANGLE bug where our index ranges
276 // weren't updated from CopyBufferSubData calls
277 // https://code.google.com/p/angleproject/issues/detail?id=709
278 TEST_F(IndexedBufferCopyTest, IndexRangeBug)
280 unsigned char vertexData[] = { 255, 0, 0, 0, 0, 0 };
281 unsigned int indexData[] = { 0, 1 };
283 glBindBuffer(GL_ARRAY_BUFFER, mBuffers[0]);
284 glBufferData(GL_ARRAY_BUFFER, sizeof(char) * 6, vertexData, GL_STATIC_DRAW);
286 glUseProgram(mProgram);
287 glVertexAttribPointer(mAttribLocation, 3, GL_UNSIGNED_BYTE, GL_TRUE, 3, NULL);
288 glEnableVertexAttribArray(mAttribLocation);
290 ASSERT_GL_NO_ERROR();
292 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBuffer);
293 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 1, indexData, GL_STATIC_DRAW);
295 glUseProgram(mProgram);
297 ASSERT_GL_NO_ERROR();
299 glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, NULL);
301 EXPECT_GL_NO_ERROR();
302 EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
304 glBindBuffer(GL_COPY_READ_BUFFER, mBuffers[1]);
305 glBufferData(GL_COPY_READ_BUFFER, 4, &indexData[1], GL_STATIC_DRAW);
307 glBindBuffer(GL_COPY_WRITE_BUFFER, mElementBuffer);
309 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, sizeof(int));
311 ASSERT_GL_NO_ERROR();
313 glClear(GL_COLOR_BUFFER_BIT);
314 EXPECT_PIXEL_EQ(0, 0, 0, 0, 0, 0);
316 unsigned char newData[] = { 0, 255, 0 };
317 glBufferSubData(GL_ARRAY_BUFFER, 3, 3, newData);
319 glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, NULL);
321 EXPECT_GL_NO_ERROR();
322 EXPECT_PIXEL_EQ(0, 0, 0, 255, 0, 255);