1 //
2 // Copyright 2015 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 #include "test_utils/ANGLETest.h"
8 #include "test_utils/gl_raii.h"
9
10 #include "util/random_utils.h"
11
12 #include <stdint.h>
13 #include <thread>
14
15 using namespace angle;
16
17 class BufferDataTest : public ANGLETest<>
18 {
19 protected:
BufferDataTest()20 BufferDataTest()
21 {
22 setWindowWidth(16);
23 setWindowHeight(16);
24 setConfigRedBits(8);
25 setConfigGreenBits(8);
26 setConfigBlueBits(8);
27 setConfigAlphaBits(8);
28 setConfigDepthBits(24);
29
30 mBuffer = 0;
31 mProgram = 0;
32 mAttribLocation = -1;
33 }
34
testSetUp()35 void testSetUp() override
36 {
37 constexpr char kVS[] = R"(attribute vec4 position;
38 attribute float in_attrib;
39 varying float v_attrib;
40 void main()
41 {
42 v_attrib = in_attrib;
43 gl_Position = position;
44 })";
45
46 constexpr char kFS[] = R"(precision mediump float;
47 varying float v_attrib;
48 void main()
49 {
50 gl_FragColor = vec4(v_attrib, 0, 0, 1);
51 })";
52
53 glGenBuffers(1, &mBuffer);
54 ASSERT_NE(mBuffer, 0U);
55
56 mProgram = CompileProgram(kVS, kFS);
57 ASSERT_NE(mProgram, 0U);
58
59 mAttribLocation = glGetAttribLocation(mProgram, "in_attrib");
60 ASSERT_NE(mAttribLocation, -1);
61
62 glClearColor(0, 0, 0, 0);
63 glClearDepthf(0.0);
64 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
65
66 glDisable(GL_DEPTH_TEST);
67
68 ASSERT_GL_NO_ERROR();
69 }
70
testTearDown()71 void testTearDown() override
72 {
73 glDeleteBuffers(1, &mBuffer);
74 glDeleteProgram(mProgram);
75 }
76
77 GLuint mBuffer;
78 GLuint mProgram;
79 GLint mAttribLocation;
80 };
81
82 // If glBufferData was not called yet the capturing must not try to
83 // read the data. http://anglebug.com/6093
TEST_P(BufferDataTest,Uninitialized)84 TEST_P(BufferDataTest, Uninitialized)
85 {
86 // Trigger frame capture to try capturing the
87 // generated but uninitialized buffer
88 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
89 swapBuffers();
90 }
91
TEST_P(BufferDataTest,ZeroNonNULLData)92 TEST_P(BufferDataTest, ZeroNonNULLData)
93 {
94 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
95 EXPECT_GL_NO_ERROR();
96
97 char *zeroData = new char[0];
98 glBufferData(GL_ARRAY_BUFFER, 0, zeroData, GL_STATIC_DRAW);
99 EXPECT_GL_NO_ERROR();
100
101 glBufferSubData(GL_ARRAY_BUFFER, 0, 0, zeroData);
102 EXPECT_GL_NO_ERROR();
103
104 delete[] zeroData;
105 }
106
TEST_P(BufferDataTest,NULLResolvedData)107 TEST_P(BufferDataTest, NULLResolvedData)
108 {
109 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
110 glBufferData(GL_ARRAY_BUFFER, 128, nullptr, GL_DYNAMIC_DRAW);
111
112 glUseProgram(mProgram);
113 glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 4, nullptr);
114 glEnableVertexAttribArray(mAttribLocation);
115 glBindBuffer(GL_ARRAY_BUFFER, 0);
116
117 drawQuad(mProgram, "position", 0.5f);
118 }
119
120 // Internally in D3D, we promote dynamic data to static after many draw loops. This code tests
121 // path.
TEST_P(BufferDataTest,RepeatedDrawWithDynamic)122 TEST_P(BufferDataTest, RepeatedDrawWithDynamic)
123 {
124 std::vector<GLfloat> data;
125 for (int i = 0; i < 16; ++i)
126 {
127 data.push_back(static_cast<GLfloat>(i));
128 }
129
130 glUseProgram(mProgram);
131 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
132 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), data.data(), GL_DYNAMIC_DRAW);
133 glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
134 glBindBuffer(GL_ARRAY_BUFFER, 0);
135 glEnableVertexAttribArray(mAttribLocation);
136
137 for (int drawCount = 0; drawCount < 40; ++drawCount)
138 {
139 drawQuad(mProgram, "position", 0.5f);
140 }
141
142 EXPECT_GL_NO_ERROR();
143 }
144
145 // Tests for a bug where vertex attribute translation was not being invalidated when switching to
146 // DYNAMIC
TEST_P(BufferDataTest,RepeatedDrawDynamicBug)147 TEST_P(BufferDataTest, RepeatedDrawDynamicBug)
148 {
149 // http://anglebug.com/2843: Seems to be an Intel driver bug.
150 ANGLE_SKIP_TEST_IF(IsVulkan() && IsIntel() && IsWindows());
151
152 glUseProgram(mProgram);
153
154 GLint positionLocation = glGetAttribLocation(mProgram, "position");
155 ASSERT_NE(-1, positionLocation);
156
157 auto quadVertices = GetQuadVertices();
158 for (angle::Vector3 &vertex : quadVertices)
159 {
160 vertex.x() *= 1.0f;
161 vertex.y() *= 1.0f;
162 vertex.z() = 0.0f;
163 }
164
165 // Set up quad vertices with DYNAMIC data
166 GLBuffer positionBuffer;
167 glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
168 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * quadVertices.size() * 3, quadVertices.data(),
169 GL_DYNAMIC_DRAW);
170 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
171 glEnableVertexAttribArray(positionLocation);
172 glBindBuffer(GL_ARRAY_BUFFER, 0);
173 EXPECT_GL_NO_ERROR();
174
175 // Set up color data so red is drawn
176 std::vector<GLfloat> data(6, 1.0f);
177
178 // Set data to DYNAMIC
179 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
180 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), data.data(), GL_DYNAMIC_DRAW);
181 glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
182 glEnableVertexAttribArray(mAttribLocation);
183 EXPECT_GL_NO_ERROR();
184
185 // Draw enough times to promote data to DIRECT mode
186 for (int i = 0; i < 20; i++)
187 {
188 glDrawArrays(GL_TRIANGLES, 0, 6);
189 }
190
191 // Verify red was drawn
192 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
193
194 // Set up color value so black is drawn
195 std::fill(data.begin(), data.end(), 0.0f);
196
197 // Update the data, changing back to DYNAMIC mode.
198 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), data.data(), GL_DYNAMIC_DRAW);
199
200 // This draw should produce a black quad
201 glDrawArrays(GL_TRIANGLES, 0, 6);
202 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::black);
203 EXPECT_GL_NO_ERROR();
204 }
205
206 using BufferSubDataTestParams = std::tuple<angle::PlatformParameters, bool>;
207
BufferSubDataTestPrint(const::testing::TestParamInfo<BufferSubDataTestParams> & paramsInfo)208 std::string BufferSubDataTestPrint(
209 const ::testing::TestParamInfo<BufferSubDataTestParams> ¶msInfo)
210 {
211 const BufferSubDataTestParams ¶ms = paramsInfo.param;
212 std::ostringstream out;
213
214 out << std::get<0>(params) << "__";
215
216 const bool useCopySubData = std::get<1>(params);
217 if (useCopySubData)
218 {
219 out << "CopyBufferSubData";
220 }
221 else
222 {
223 out << "BufferSubData";
224 }
225
226 return out.str();
227 }
228
229 class BufferSubDataTest : public ANGLETest<BufferSubDataTestParams>
230 {
231 protected:
BufferSubDataTest()232 BufferSubDataTest()
233 {
234 setWindowWidth(16);
235 setWindowHeight(16);
236 setConfigRedBits(8);
237 setConfigGreenBits(8);
238 setConfigBlueBits(8);
239 setConfigAlphaBits(8);
240 setConfigDepthBits(24);
241
242 mBuffer = 0;
243 }
244
testSetUp()245 void testSetUp() override
246 {
247 glGenBuffers(1, &mBuffer);
248 ASSERT_NE(mBuffer, 0U);
249
250 glClearColor(0, 0, 0, 0);
251 glClearDepthf(0.0);
252 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
253
254 glDisable(GL_DEPTH_TEST);
255
256 ASSERT_GL_NO_ERROR();
257 }
258
updateBuffer(GLenum target,GLintptr offset,GLsizeiptr size,const void * data)259 void updateBuffer(GLenum target, GLintptr offset, GLsizeiptr size, const void *data)
260 {
261 const bool useCopySubData = std::get<1>(GetParam());
262 if (!useCopySubData)
263 {
264 // If using glBufferSubData, directly upload data on the specified target (where the
265 // buffer is already bound)
266 glBufferSubData(target, offset, size, data);
267 }
268 else
269 {
270 // Otherwise copy through a temp buffer. Use a non-zero offset for more coverage.
271 constexpr GLintptr kStagingOffset = 935;
272 GLBuffer staging;
273 glBindBuffer(GL_COPY_READ_BUFFER, staging);
274 glBufferData(GL_COPY_READ_BUFFER, offset + size + kStagingOffset * 3 / 2, nullptr,
275 GL_STATIC_DRAW);
276 glBufferSubData(GL_COPY_READ_BUFFER, kStagingOffset, size, data);
277 glCopyBufferSubData(GL_COPY_READ_BUFFER, target, kStagingOffset, offset, size);
278 }
279 }
280
testTearDown()281 void testTearDown() override { glDeleteBuffers(1, &mBuffer); }
282 GLuint mBuffer;
283 };
284
285 // Test that updating a small index buffer after drawing with it works.
286 // In the Vulkan backend, the CPU may be used to perform this copy.
TEST_P(BufferSubDataTest,SmallIndexBufferUpdateAfterDraw)287 TEST_P(BufferSubDataTest, SmallIndexBufferUpdateAfterDraw)
288 {
289 constexpr std::array<GLfloat, 4> kRed = {1.0f, 0.0f, 0.0f, 1.0f};
290 constexpr std::array<GLfloat, 4> kGreen = {0.0f, 1.0f, 0.0f, 1.0f};
291 // Index buffer data
292 GLuint indexData[] = {0, 1, 2, 0};
293 // Vertex buffer data fully cover the screen
294 float vertexData[] = {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f};
295
296 GLBuffer indexBuffer;
297 ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
298 GLint vPos = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
299 ASSERT_NE(vPos, -1);
300 glUseProgram(program);
301 GLint colorUniformLocation =
302 glGetUniformLocation(program, angle::essl1_shaders::ColorUniform());
303 ASSERT_NE(colorUniformLocation, -1);
304
305 // Bind vertex buffer
306 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
307 glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
308 glVertexAttribPointer(vPos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
309 glEnableVertexAttribArray(vPos);
310
311 // Bind index buffer
312 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
313 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_DYNAMIC_DRAW);
314
315 glUniform4fv(colorUniformLocation, 1, kRed.data());
316 // Draw left red triangle
317 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
318 // Update the index buffer data.
319 indexData[1] = 1;
320 indexData[2] = 2;
321 indexData[3] = 3;
322 // Partial copy to trigger the buffer pool allocation
323 updateBuffer(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint), 3 * sizeof(GLuint), &indexData[1]);
324 // Draw triangle with index (1, 2, 3).
325 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (const void *)sizeof(GLuint));
326 // Update the index buffer again
327 indexData[0] = 0;
328 indexData[1] = 0;
329 indexData[2] = 2;
330 glUniform4fv(colorUniformLocation, 1, kGreen.data());
331 updateBuffer(GL_ELEMENT_ARRAY_BUFFER, 0, 3 * sizeof(GLuint), &indexData[0]);
332 // Draw triangle with index (0, 2, 3), hope angle copy the last index 3 back.
333 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (const void *)sizeof(GLuint));
334
335 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() - 1, 0, GLColor::red);
336 // Verify pixel top left corner is green
337 EXPECT_PIXEL_COLOR_EQ(0, getWindowHeight() - 1, GLColor::green);
338 }
339
340 // Test that updating a small index buffer after drawing with it works.
341 // In the Vulkan backend, the CPU may be used to perform this copy.
TEST_P(BufferSubDataTest,SmallVertexDataUpdateAfterDraw)342 TEST_P(BufferSubDataTest, SmallVertexDataUpdateAfterDraw)
343 {
344 constexpr std::array<GLfloat, 4> kGreen = {0.0f, 1.0f, 0.0f, 1.0f};
345 // Index buffer data
346 GLuint indexData[] = {0, 1, 2, 0};
347 // Vertex buffer data lower left triangle
348 // 2
349 //
350 // o 1
351 float vertexData1[] = {
352 -1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f,
353 };
354 // Vertex buffer data upper right triangle
355 // 2 1
356 //
357 // 0
358 float vertexData2[] = {
359 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f,
360 };
361 GLBuffer indexBuffer;
362 ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::UniformColor());
363 GLint vPos = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
364 ASSERT_NE(vPos, -1);
365 glUseProgram(program);
366 GLint colorUniformLocation =
367 glGetUniformLocation(program, angle::essl1_shaders::ColorUniform());
368 ASSERT_NE(colorUniformLocation, -1);
369
370 // Bind vertex buffer
371 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
372 glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData1), vertexData1, GL_DYNAMIC_DRAW);
373 glVertexAttribPointer(vPos, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
374 glEnableVertexAttribArray(vPos);
375
376 // Bind index buffer
377 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
378 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_DYNAMIC_DRAW);
379
380 glUniform4fv(colorUniformLocation, 1, kGreen.data());
381 // Draw left red triangle
382 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, nullptr);
383 // Update the vertex buffer data.
384 // Partial copy to trigger the buffer pool allocation
385 updateBuffer(GL_ARRAY_BUFFER, 0, sizeof(vertexData2), vertexData2);
386 // Draw triangle with index (0,1,2).
387 glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, (const void *)sizeof(GLuint));
388 // Verify pixel corners are green
389 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
390 EXPECT_PIXEL_COLOR_EQ(0, getWindowHeight() - 1, GLColor::green);
391 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() - 1, 0, GLColor::green);
392 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() - 1, getWindowHeight() - 1, GLColor::green);
393 }
394 class IndexedBufferCopyTest : public ANGLETest<>
395 {
396 protected:
IndexedBufferCopyTest()397 IndexedBufferCopyTest()
398 {
399 setWindowWidth(16);
400 setWindowHeight(16);
401 setConfigRedBits(8);
402 setConfigGreenBits(8);
403 setConfigBlueBits(8);
404 setConfigAlphaBits(8);
405 setConfigDepthBits(24);
406 }
407
testSetUp()408 void testSetUp() override
409 {
410 constexpr char kVS[] = R"(attribute vec3 in_attrib;
411 varying vec3 v_attrib;
412 void main()
413 {
414 v_attrib = in_attrib;
415 gl_Position = vec4(0.0, 0.0, 0.5, 1.0);
416 gl_PointSize = 100.0;
417 })";
418
419 constexpr char kFS[] = R"(precision mediump float;
420 varying vec3 v_attrib;
421 void main()
422 {
423 gl_FragColor = vec4(v_attrib, 1);
424 })";
425
426 glGenBuffers(2, mBuffers);
427 ASSERT_NE(mBuffers[0], 0U);
428 ASSERT_NE(mBuffers[1], 0U);
429
430 glGenBuffers(1, &mElementBuffer);
431 ASSERT_NE(mElementBuffer, 0U);
432
433 mProgram = CompileProgram(kVS, kFS);
434 ASSERT_NE(mProgram, 0U);
435
436 mAttribLocation = glGetAttribLocation(mProgram, "in_attrib");
437 ASSERT_NE(mAttribLocation, -1);
438
439 glClearColor(0, 0, 0, 0);
440 glDisable(GL_DEPTH_TEST);
441 glClear(GL_COLOR_BUFFER_BIT);
442
443 ASSERT_GL_NO_ERROR();
444 }
445
testTearDown()446 void testTearDown() override
447 {
448 glDeleteBuffers(2, mBuffers);
449 glDeleteBuffers(1, &mElementBuffer);
450 glDeleteProgram(mProgram);
451 }
452
453 GLuint mBuffers[2];
454 GLuint mElementBuffer;
455 GLuint mProgram;
456 GLint mAttribLocation;
457 };
458
459 // The following test covers an ANGLE bug where our index ranges
460 // weren't updated from CopyBufferSubData calls
461 // https://code.google.com/p/angleproject/issues/detail?id=709
TEST_P(IndexedBufferCopyTest,IndexRangeBug)462 TEST_P(IndexedBufferCopyTest, IndexRangeBug)
463 {
464 // TODO(geofflang): Figure out why this fails on AMD OpenGL (http://anglebug.com/1291)
465 ANGLE_SKIP_TEST_IF(IsAMD() && IsOpenGL());
466
467 unsigned char vertexData[] = {255, 0, 0, 0, 0, 0};
468 unsigned int indexData[] = {0, 1};
469
470 glBindBuffer(GL_ARRAY_BUFFER, mBuffers[0]);
471 glBufferData(GL_ARRAY_BUFFER, sizeof(char) * 6, vertexData, GL_STATIC_DRAW);
472
473 glUseProgram(mProgram);
474 glVertexAttribPointer(mAttribLocation, 3, GL_UNSIGNED_BYTE, GL_TRUE, 3, nullptr);
475 glEnableVertexAttribArray(mAttribLocation);
476
477 ASSERT_GL_NO_ERROR();
478
479 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBuffer);
480 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int) * 1, indexData, GL_STATIC_DRAW);
481
482 glUseProgram(mProgram);
483
484 ASSERT_GL_NO_ERROR();
485
486 glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, nullptr);
487
488 EXPECT_GL_NO_ERROR();
489 EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
490
491 glBindBuffer(GL_COPY_READ_BUFFER, mBuffers[1]);
492 glBufferData(GL_COPY_READ_BUFFER, 4, &indexData[1], GL_STATIC_DRAW);
493
494 glBindBuffer(GL_COPY_WRITE_BUFFER, mElementBuffer);
495
496 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, sizeof(int));
497
498 ASSERT_GL_NO_ERROR();
499
500 glClear(GL_COLOR_BUFFER_BIT);
501 EXPECT_PIXEL_EQ(0, 0, 0, 0, 0, 0);
502
503 unsigned char newData[] = {0, 255, 0};
504 glBufferSubData(GL_ARRAY_BUFFER, 3, 3, newData);
505
506 glDrawElements(GL_POINTS, 1, GL_UNSIGNED_INT, nullptr);
507
508 EXPECT_GL_NO_ERROR();
509 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
510 }
511
512 class BufferDataTestES3 : public BufferDataTest
513 {};
514
515 // The following test covers an ANGLE bug where the buffer storage
516 // is not resized by Buffer11::getLatestBufferStorage when needed.
517 // https://code.google.com/p/angleproject/issues/detail?id=897
TEST_P(BufferDataTestES3,BufferResizing)518 TEST_P(BufferDataTestES3, BufferResizing)
519 {
520 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
521 ASSERT_GL_NO_ERROR();
522
523 // Allocate a buffer with one byte
524 uint8_t singleByte[] = {0xaa};
525 glBufferData(GL_ARRAY_BUFFER, 1, singleByte, GL_STATIC_DRAW);
526
527 // Resize the buffer
528 // To trigger the bug, the buffer need to be big enough because some hardware copy buffers
529 // by chunks of pages instead of the minimum number of bytes needed.
530 const size_t numBytes = 4096 * 4;
531 glBufferData(GL_ARRAY_BUFFER, numBytes, nullptr, GL_STATIC_DRAW);
532
533 // Copy the original data to the buffer
534 uint8_t srcBytes[numBytes];
535 for (size_t i = 0; i < numBytes; ++i)
536 {
537 srcBytes[i] = static_cast<uint8_t>(i);
538 }
539
540 void *dest = glMapBufferRange(GL_ARRAY_BUFFER, 0, numBytes,
541 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
542
543 ASSERT_GL_NO_ERROR();
544
545 memcpy(dest, srcBytes, numBytes);
546 glUnmapBuffer(GL_ARRAY_BUFFER);
547
548 EXPECT_GL_NO_ERROR();
549
550 // Create a new buffer and copy the data to it
551 GLuint readBuffer;
552 glGenBuffers(1, &readBuffer);
553 glBindBuffer(GL_COPY_WRITE_BUFFER, readBuffer);
554 uint8_t zeros[numBytes];
555 for (size_t i = 0; i < numBytes; ++i)
556 {
557 zeros[i] = 0;
558 }
559 glBufferData(GL_COPY_WRITE_BUFFER, numBytes, zeros, GL_STATIC_DRAW);
560 glCopyBufferSubData(GL_ARRAY_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, numBytes);
561
562 ASSERT_GL_NO_ERROR();
563
564 // Read back the data and compare it to the original
565 uint8_t *data = reinterpret_cast<uint8_t *>(
566 glMapBufferRange(GL_COPY_WRITE_BUFFER, 0, numBytes, GL_MAP_READ_BIT));
567
568 ASSERT_GL_NO_ERROR();
569
570 for (size_t i = 0; i < numBytes; ++i)
571 {
572 EXPECT_EQ(srcBytes[i], data[i]);
573 }
574 glUnmapBuffer(GL_COPY_WRITE_BUFFER);
575
576 glDeleteBuffers(1, &readBuffer);
577
578 EXPECT_GL_NO_ERROR();
579 }
580
581 // Test to verify mapping a buffer after copying to it contains flushed/updated data
TEST_P(BufferDataTestES3,CopyBufferSubDataMapReadTest)582 TEST_P(BufferDataTestES3, CopyBufferSubDataMapReadTest)
583 {
584 const char simpleVertex[] = R"(attribute vec2 position;
585 attribute vec4 color;
586 varying vec4 vColor;
587 void main()
588 {
589 gl_Position = vec4(position, 0, 1);
590 vColor = color;
591 }
592 )";
593 const char simpleFragment[] = R"(precision mediump float;
594 varying vec4 vColor;
595 void main()
596 {
597 gl_FragColor = vColor;
598 }
599 )";
600
601 const uint32_t numComponents = 3;
602 const uint32_t width = 4;
603 const uint32_t height = 4;
604 const size_t numElements = width * height * numComponents;
605 std::vector<uint8_t> srcData(numElements);
606 std::vector<uint8_t> dstData(numElements);
607
608 for (uint8_t i = 0; i < srcData.size(); i++)
609 {
610 srcData[i] = 128;
611 }
612 for (uint8_t i = 0; i < dstData.size(); i++)
613 {
614 dstData[i] = 0;
615 }
616
617 GLBuffer srcBuffer;
618 GLBuffer dstBuffer;
619
620 glBindBuffer(GL_ARRAY_BUFFER, srcBuffer);
621 glBufferData(GL_ARRAY_BUFFER, srcData.size(), srcData.data(), GL_STATIC_DRAW);
622 ASSERT_GL_NO_ERROR();
623
624 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dstBuffer);
625 glBufferData(GL_PIXEL_UNPACK_BUFFER, dstData.size(), dstData.data(), GL_STATIC_READ);
626 ASSERT_GL_NO_ERROR();
627
628 ANGLE_GL_PROGRAM(program, simpleVertex, simpleFragment);
629 glUseProgram(program);
630
631 GLint colorLoc = glGetAttribLocation(program, "color");
632 ASSERT_NE(-1, colorLoc);
633
634 glBindBuffer(GL_ARRAY_BUFFER, srcBuffer);
635 glVertexAttribPointer(colorLoc, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, nullptr);
636 glEnableVertexAttribArray(colorLoc);
637
638 drawQuad(program, "position", 0.5f, 1.0f, true);
639 ASSERT_GL_NO_ERROR();
640
641 glCopyBufferSubData(GL_ARRAY_BUFFER, GL_PIXEL_UNPACK_BUFFER, 0, 0, numElements);
642
643 // With GL_MAP_READ_BIT, we expect the data to be flushed and updated to match srcData
644 uint8_t *data = reinterpret_cast<uint8_t *>(
645 glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, numElements, GL_MAP_READ_BIT));
646 EXPECT_GL_NO_ERROR();
647 for (size_t i = 0; i < numElements; ++i)
648 {
649 EXPECT_EQ(srcData[i], data[i]);
650 }
651 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
652 EXPECT_GL_NO_ERROR();
653 }
654
655 // Test to verify mapping a buffer after copying to it contains expected data
656 // with GL_MAP_UNSYNCHRONIZED_BIT
TEST_P(BufferDataTestES3,MapBufferUnsynchronizedReadTest)657 TEST_P(BufferDataTestES3, MapBufferUnsynchronizedReadTest)
658 {
659 const char simpleVertex[] = R"(attribute vec2 position;
660 attribute vec4 color;
661 varying vec4 vColor;
662 void main()
663 {
664 gl_Position = vec4(position, 0, 1);
665 vColor = color;
666 }
667 )";
668 const char simpleFragment[] = R"(precision mediump float;
669 varying vec4 vColor;
670 void main()
671 {
672 gl_FragColor = vColor;
673 }
674 )";
675
676 const uint32_t numComponents = 3;
677 const uint32_t width = 4;
678 const uint32_t height = 4;
679 const size_t numElements = width * height * numComponents;
680 std::vector<uint8_t> srcData(numElements);
681 std::vector<uint8_t> dstData(numElements);
682
683 for (uint8_t i = 0; i < srcData.size(); i++)
684 {
685 srcData[i] = 128;
686 }
687 for (uint8_t i = 0; i < dstData.size(); i++)
688 {
689 dstData[i] = 0;
690 }
691
692 GLBuffer srcBuffer;
693 GLBuffer dstBuffer;
694
695 glBindBuffer(GL_ARRAY_BUFFER, srcBuffer);
696 glBufferData(GL_ARRAY_BUFFER, srcData.size(), srcData.data(), GL_STATIC_DRAW);
697 ASSERT_GL_NO_ERROR();
698
699 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, dstBuffer);
700 glBufferData(GL_PIXEL_UNPACK_BUFFER, dstData.size(), dstData.data(), GL_STATIC_READ);
701 ASSERT_GL_NO_ERROR();
702
703 ANGLE_GL_PROGRAM(program, simpleVertex, simpleFragment);
704 glUseProgram(program);
705
706 GLint colorLoc = glGetAttribLocation(program, "color");
707 ASSERT_NE(-1, colorLoc);
708
709 glBindBuffer(GL_ARRAY_BUFFER, srcBuffer);
710 glVertexAttribPointer(colorLoc, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, nullptr);
711 glEnableVertexAttribArray(colorLoc);
712
713 drawQuad(program, "position", 0.5f, 1.0f, true);
714 ASSERT_GL_NO_ERROR();
715
716 glCopyBufferSubData(GL_ARRAY_BUFFER, GL_PIXEL_UNPACK_BUFFER, 0, 0, numElements);
717
718 // Synchronize.
719 glFinish();
720
721 // Map with GL_MAP_UNSYNCHRONIZED_BIT and overwrite buffers data with srcData
722 uint8_t *data = reinterpret_cast<uint8_t *>(glMapBufferRange(
723 GL_PIXEL_UNPACK_BUFFER, 0, numElements, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
724 EXPECT_GL_NO_ERROR();
725 memcpy(data, srcData.data(), srcData.size());
726 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
727 EXPECT_GL_NO_ERROR();
728
729 // Map without GL_MAP_UNSYNCHRONIZED_BIT and read data. We expect it to be srcData
730 data = reinterpret_cast<uint8_t *>(
731 glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, numElements, GL_MAP_READ_BIT));
732 EXPECT_GL_NO_ERROR();
733 for (size_t i = 0; i < numElements; ++i)
734 {
735 EXPECT_EQ(srcData[i], data[i]);
736 }
737 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
738 EXPECT_GL_NO_ERROR();
739 }
740
741 // Verify the functionality of glMapBufferRange()'s GL_MAP_UNSYNCHRONIZED_BIT
742 // NOTE: On Vulkan, if we ever use memory that's not `VK_MEMORY_PROPERTY_HOST_COHERENT_BIT`, then
743 // this could incorrectly pass.
TEST_P(BufferDataTestES3,MapBufferRangeUnsynchronizedBit)744 TEST_P(BufferDataTestES3, MapBufferRangeUnsynchronizedBit)
745 {
746 // We can currently only control the behavior of the Vulkan backend's synchronizing operation's
747 ANGLE_SKIP_TEST_IF(!IsVulkan());
748
749 const size_t numElements = 10;
750 std::vector<uint8_t> srcData(numElements);
751 std::vector<uint8_t> dstData(numElements);
752
753 for (uint8_t i = 0; i < srcData.size(); i++)
754 {
755 srcData[i] = i;
756 }
757 for (uint8_t i = 0; i < dstData.size(); i++)
758 {
759 dstData[i] = static_cast<uint8_t>(i + dstData.size());
760 }
761
762 GLBuffer srcBuffer;
763 GLBuffer dstBuffer;
764
765 glBindBuffer(GL_COPY_READ_BUFFER, srcBuffer);
766 ASSERT_GL_NO_ERROR();
767 glBindBuffer(GL_COPY_WRITE_BUFFER, dstBuffer);
768 ASSERT_GL_NO_ERROR();
769
770 glBufferData(GL_COPY_READ_BUFFER, srcData.size(), srcData.data(), GL_STATIC_DRAW);
771 ASSERT_GL_NO_ERROR();
772 glBufferData(GL_COPY_WRITE_BUFFER, dstData.size(), dstData.data(), GL_STATIC_READ);
773 ASSERT_GL_NO_ERROR();
774
775 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, numElements);
776
777 // With GL_MAP_UNSYNCHRONIZED_BIT, we expect the data to be stale and match dstData
778 // NOTE: We are specifying GL_MAP_WRITE_BIT so we can use GL_MAP_UNSYNCHRONIZED_BIT. This is
779 // venturing into undefined behavior, since we are actually planning on reading from this
780 // pointer.
781 auto *data = reinterpret_cast<uint8_t *>(glMapBufferRange(
782 GL_COPY_WRITE_BUFFER, 0, numElements, GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
783 EXPECT_GL_NO_ERROR();
784 for (size_t i = 0; i < numElements; ++i)
785 {
786 // Allow for the possibility that data matches either "dstData" or "srcData"
787 if (dstData[i] != data[i])
788 {
789 EXPECT_EQ(srcData[i], data[i]);
790 }
791 }
792 glUnmapBuffer(GL_COPY_WRITE_BUFFER);
793 EXPECT_GL_NO_ERROR();
794
795 // Without GL_MAP_UNSYNCHRONIZED_BIT, we expect the data to be copied and match srcData
796 data = reinterpret_cast<uint8_t *>(
797 glMapBufferRange(GL_COPY_WRITE_BUFFER, 0, numElements, GL_MAP_READ_BIT));
798 EXPECT_GL_NO_ERROR();
799 for (size_t i = 0; i < numElements; ++i)
800 {
801 EXPECT_EQ(srcData[i], data[i]);
802 }
803 glUnmapBuffer(GL_COPY_WRITE_BUFFER);
804 EXPECT_GL_NO_ERROR();
805 }
806
807 // Verify OES_mapbuffer is present if EXT_map_buffer_range is.
TEST_P(BufferDataTest,ExtensionDependency)808 TEST_P(BufferDataTest, ExtensionDependency)
809 {
810 if (IsGLExtensionEnabled("GL_EXT_map_buffer_range"))
811 {
812 ASSERT_TRUE(IsGLExtensionEnabled("GL_OES_mapbuffer"));
813 }
814 }
815
816 // Test mapping with the OES extension.
TEST_P(BufferDataTest,MapBufferOES)817 TEST_P(BufferDataTest, MapBufferOES)
818 {
819 if (!IsGLExtensionEnabled("GL_EXT_map_buffer_range"))
820 {
821 // Needed for test validation.
822 return;
823 }
824
825 std::vector<uint8_t> data(1024);
826 FillVectorWithRandomUBytes(&data);
827
828 GLBuffer buffer;
829 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
830 glBufferData(GL_ARRAY_BUFFER, data.size(), nullptr, GL_STATIC_DRAW);
831
832 // Validate that other map flags don't work.
833 void *badMapPtr = glMapBufferOES(GL_ARRAY_BUFFER, GL_MAP_READ_BIT);
834 EXPECT_EQ(nullptr, badMapPtr);
835 EXPECT_GL_ERROR(GL_INVALID_ENUM);
836
837 // Map and write.
838 void *mapPtr = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
839 ASSERT_NE(nullptr, mapPtr);
840 ASSERT_GL_NO_ERROR();
841 memcpy(mapPtr, data.data(), data.size());
842 glUnmapBufferOES(GL_ARRAY_BUFFER);
843
844 // Validate data with EXT_map_buffer_range
845 void *readMapPtr = glMapBufferRangeEXT(GL_ARRAY_BUFFER, 0, data.size(), GL_MAP_READ_BIT_EXT);
846 ASSERT_NE(nullptr, readMapPtr);
847 ASSERT_GL_NO_ERROR();
848 std::vector<uint8_t> actualData(data.size());
849 memcpy(actualData.data(), readMapPtr, data.size());
850 glUnmapBufferOES(GL_ARRAY_BUFFER);
851
852 EXPECT_EQ(data, actualData);
853 }
854
855 // Test to verify mapping a dynamic buffer with GL_MAP_UNSYNCHRONIZED_BIT to modify a portion
856 // won't affect draw calls using other portions.
TEST_P(BufferDataTest,MapDynamicBufferUnsynchronizedEXTTest)857 TEST_P(BufferDataTest, MapDynamicBufferUnsynchronizedEXTTest)
858 {
859 ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_map_buffer_range"));
860
861 const char simpleVertex[] = R"(attribute vec2 position;
862 attribute vec4 color;
863 varying vec4 vColor;
864 void main()
865 {
866 gl_Position = vec4(position, 0, 1);
867 vColor = color;
868 }
869 )";
870 const char simpleFragment[] = R"(precision mediump float;
871 varying vec4 vColor;
872 void main()
873 {
874 gl_FragColor = vColor;
875 }
876 )";
877
878 constexpr int kNumVertices = 6;
879
880 std::vector<GLubyte> color(8 * kNumVertices);
881 for (int i = 0; i < kNumVertices; ++i)
882 {
883 color[4 * i] = 255;
884 color[4 * i + 3] = 255;
885 }
886 GLBuffer buffer;
887 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
888 glBufferData(GL_ARRAY_BUFFER, color.size(), color.data(), GL_DYNAMIC_DRAW);
889
890 ANGLE_GL_PROGRAM(program, simpleVertex, simpleFragment);
891 glUseProgram(program);
892
893 GLint colorLoc = glGetAttribLocation(program, "color");
894 ASSERT_NE(-1, colorLoc);
895
896 glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, nullptr);
897 glEnableVertexAttribArray(colorLoc);
898
899 glViewport(0, 0, 2, 2);
900 drawQuad(program, "position", 0.5f, 1.0f, true);
901 ASSERT_GL_NO_ERROR();
902
903 // Map with GL_MAP_UNSYNCHRONIZED_BIT and overwrite buffers data at offset 24
904 uint8_t *data = reinterpret_cast<uint8_t *>(
905 glMapBufferRangeEXT(GL_ARRAY_BUFFER, 4 * kNumVertices, 4 * kNumVertices,
906 GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
907 EXPECT_GL_NO_ERROR();
908 for (int i = 0; i < kNumVertices; ++i)
909 {
910 data[4 * i] = 0;
911 data[4 * i + 1] = 255;
912 data[4 * i + 2] = 0;
913 data[4 * i + 3] = 255;
914 }
915 glUnmapBufferOES(GL_ARRAY_BUFFER);
916 EXPECT_GL_NO_ERROR();
917
918 // Re-draw using offset = 0 but to different viewport
919 glViewport(0, 2, 2, 2);
920 drawQuad(program, "position", 0.5f, 1.0f, true);
921 ASSERT_GL_NO_ERROR();
922
923 // Change vertex attribute to use buffer starting from offset 24
924 glVertexAttribPointer(colorLoc, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0,
925 reinterpret_cast<void *>(4 * kNumVertices));
926
927 glViewport(2, 2, 2, 2);
928 drawQuad(program, "position", 0.5f, 1.0f, true);
929 ASSERT_GL_NO_ERROR();
930
931 EXPECT_PIXEL_COLOR_EQ(1, 1, GLColor::red);
932 EXPECT_PIXEL_COLOR_EQ(1, 3, GLColor::red);
933
934 // The result below is undefined. The glBufferData at the top puts
935 // [red, red, red, ..., zero, zero, zero, ...]
936 // in the buffer and the glMap,glUnmap tries to overwrite the zeros with green
937 // but because UNSYNCHRONIZED was passed in there's no guarantee those
938 // zeros have been written yet. If they haven't they'll overwrite the
939 // greens.
940 // EXPECT_PIXEL_COLOR_EQ(3, 3, GLColor::green);
941 }
942
943 // Verify that we can map and write the buffer between draws and the second draw sees the new buffer
944 // data, using drawQuad().
TEST_P(BufferDataTest,MapWriteArrayBufferDataDrawQuad)945 TEST_P(BufferDataTest, MapWriteArrayBufferDataDrawQuad)
946 {
947 ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_map_buffer_range"));
948
949 std::vector<GLfloat> data(6, 0.0f);
950
951 glUseProgram(mProgram);
952 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
953 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), nullptr, GL_STATIC_DRAW);
954 glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
955 glEnableVertexAttribArray(mAttribLocation);
956
957 // Don't read back to verify black, so we don't break the render pass.
958 drawQuad(mProgram, "position", 0.5f);
959 EXPECT_GL_NO_ERROR();
960
961 // Map and write.
962 std::vector<GLfloat> data2(6, 1.0f);
963 void *mapPtr = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
964 ASSERT_NE(nullptr, mapPtr);
965 ASSERT_GL_NO_ERROR();
966 memcpy(mapPtr, data2.data(), sizeof(GLfloat) * data2.size());
967 glUnmapBufferOES(GL_ARRAY_BUFFER);
968
969 drawQuad(mProgram, "position", 0.5f);
970 EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::red);
971 EXPECT_GL_NO_ERROR();
972 }
973
974 // Verify that we can map and write the buffer between draws and the second draw sees the new buffer
975 // data, calling glDrawArrays() directly.
TEST_P(BufferDataTest,MapWriteArrayBufferDataDrawArrays)976 TEST_P(BufferDataTest, MapWriteArrayBufferDataDrawArrays)
977 {
978 ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_map_buffer_range"));
979
980 std::vector<GLfloat> data(6, 0.0f);
981
982 glUseProgram(mProgram);
983
984 GLint positionLocation = glGetAttribLocation(mProgram, "position");
985 ASSERT_NE(-1, positionLocation);
986
987 // Set up position attribute, don't use drawQuad.
988 auto quadVertices = GetQuadVertices();
989
990 GLBuffer positionBuffer;
991 glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
992 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * quadVertices.size() * 3, quadVertices.data(),
993 GL_DYNAMIC_DRAW);
994 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
995 glEnableVertexAttribArray(positionLocation);
996 EXPECT_GL_NO_ERROR();
997
998 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
999 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), nullptr, GL_STATIC_DRAW);
1000 glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
1001 glEnableVertexAttribArray(mAttribLocation);
1002 EXPECT_GL_NO_ERROR();
1003
1004 // Don't read back to verify black, so we don't break the render pass.
1005 glDrawArrays(GL_TRIANGLES, 0, 6);
1006 EXPECT_GL_NO_ERROR();
1007
1008 // Map and write.
1009 std::vector<GLfloat> data2(6, 1.0f);
1010 void *mapPtr = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
1011 ASSERT_NE(nullptr, mapPtr);
1012 ASSERT_GL_NO_ERROR();
1013 memcpy(mapPtr, data2.data(), sizeof(GLfloat) * data2.size());
1014 glUnmapBufferOES(GL_ARRAY_BUFFER);
1015
1016 glDrawArrays(GL_TRIANGLES, 0, 6);
1017 EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::red);
1018 EXPECT_GL_NO_ERROR();
1019 }
1020
1021 // Verify that buffer sub data uploads are properly validated within the buffer size range on 32-bit
1022 // systems.
TEST_P(BufferDataTest,BufferSizeValidation32Bit)1023 TEST_P(BufferDataTest, BufferSizeValidation32Bit)
1024 {
1025 GLBuffer buffer;
1026 glBindBuffer(GL_ARRAY_BUFFER, buffer);
1027 glBufferData(GL_ARRAY_BUFFER, 100, nullptr, GL_STATIC_DRAW);
1028
1029 GLubyte data = 0;
1030 glBufferSubData(GL_ARRAY_BUFFER, std::numeric_limits<uint32_t>::max(), 1, &data);
1031 EXPECT_GL_ERROR(GL_INVALID_VALUE);
1032 }
1033
1034 // Some drivers generate errors when array buffer bindings are left mapped during draw calls.
1035 // crbug.com/1345777
TEST_P(BufferDataTestES3,GLDriverErrorWhenMappingArrayBuffersDuringDraw)1036 TEST_P(BufferDataTestES3, GLDriverErrorWhenMappingArrayBuffersDuringDraw)
1037 {
1038 ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
1039 ASSERT_NE(program, 0u);
1040
1041 glUseProgram(program);
1042
1043 auto quadVertices = GetQuadVertices();
1044
1045 GLBuffer vb;
1046 glBindBuffer(GL_ARRAY_BUFFER, vb.get());
1047 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * quadVertices.size(), quadVertices.data(),
1048 GL_STATIC_DRAW);
1049
1050 GLint positionLocation = glGetAttribLocation(program, essl3_shaders::PositionAttrib());
1051 ASSERT_NE(-1, positionLocation);
1052 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
1053 glEnableVertexAttribArray(positionLocation);
1054
1055 glDrawArrays(GL_TRIANGLES, 0, 6);
1056 EXPECT_GL_NO_ERROR();
1057
1058 GLBuffer pb;
1059 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pb);
1060 glBufferData(GL_PIXEL_UNPACK_BUFFER, 1024, nullptr, GL_STREAM_DRAW);
1061 glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, 1024, GL_MAP_WRITE_BIT);
1062 EXPECT_GL_NO_ERROR();
1063
1064 glDrawArrays(GL_TRIANGLES, 0, 6);
1065 EXPECT_GL_NO_ERROR();
1066 }
1067
1068 // Tests a null crash bug caused by copying from null back-end buffer pointer
1069 // when calling bufferData again after drawing without calling bufferData in D3D11.
TEST_P(BufferDataTestES3,DrawWithNotCallingBufferData)1070 TEST_P(BufferDataTestES3, DrawWithNotCallingBufferData)
1071 {
1072 ANGLE_GL_PROGRAM(drawRed, essl3_shaders::vs::Simple(), essl3_shaders::fs::Red());
1073 glUseProgram(drawRed);
1074
1075 GLint mem = 0;
1076 GLBuffer buffer;
1077 glBindBuffer(GL_ARRAY_BUFFER, buffer);
1078 glEnableVertexAttribArray(0);
1079 glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
1080 glDrawArrays(GL_TRIANGLES, 0, 3);
1081 glBindBuffer(GL_COPY_WRITE_BUFFER, buffer);
1082 glBufferData(GL_COPY_WRITE_BUFFER, 1, &mem, GL_STREAM_DRAW);
1083 ASSERT_GL_NO_ERROR();
1084 }
1085
1086 // Tests a bug where copying buffer data immediately after creation hit a nullptr in D3D11.
TEST_P(BufferDataTestES3,NoBufferInitDataCopyBug)1087 TEST_P(BufferDataTestES3, NoBufferInitDataCopyBug)
1088 {
1089 constexpr GLsizei size = 64;
1090
1091 GLBuffer sourceBuffer;
1092 glBindBuffer(GL_COPY_READ_BUFFER, sourceBuffer);
1093 glBufferData(GL_COPY_READ_BUFFER, size, nullptr, GL_STATIC_DRAW);
1094
1095 GLBuffer destBuffer;
1096 glBindBuffer(GL_ARRAY_BUFFER, destBuffer);
1097 glBufferData(GL_ARRAY_BUFFER, size, nullptr, GL_STATIC_DRAW);
1098
1099 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_ARRAY_BUFFER, 0, 0, size);
1100 ASSERT_GL_NO_ERROR();
1101 }
1102
1103 // This a shortened version of dEQP functional.buffer.copy.basic.array_copy_read. It provoked
1104 // a bug in copyBufferSubData. The bug appeared to be that conversion buffers were not marked
1105 // as dirty and therefore after copyBufferSubData the next draw call using the buffer that
1106 // just had data copied to it was not re-converted. It's not clear to me how this ever worked
1107 // or why changes to bufferSubData from
1108 // https://chromium-review.googlesource.com/c/angle/angle/+/3842641 made this issue appear and
1109 // why it wasn't already broken.
TEST_P(BufferDataTestES3,CopyBufferSubDataDraw)1110 TEST_P(BufferDataTestES3, CopyBufferSubDataDraw)
1111 {
1112 const char simpleVertex[] = R"(attribute vec2 position;
1113 attribute vec4 color;
1114 varying vec4 vColor;
1115 void main()
1116 {
1117 gl_Position = vec4(position, 0, 1);
1118 vColor = color;
1119 }
1120 )";
1121 const char simpleFragment[] = R"(precision mediump float;
1122 varying vec4 vColor;
1123 void main()
1124 {
1125 gl_FragColor = vColor;
1126 }
1127 )";
1128
1129 ANGLE_GL_PROGRAM(program, simpleVertex, simpleFragment);
1130 glUseProgram(program);
1131
1132 GLint colorLoc = glGetAttribLocation(program, "color");
1133 ASSERT_NE(-1, colorLoc);
1134 GLint posLoc = glGetAttribLocation(program, "position");
1135 ASSERT_NE(-1, posLoc);
1136
1137 glClearColor(0, 0, 0, 0);
1138
1139 GLBuffer srcBuffer; // green
1140 GLBuffer dstBuffer; // red
1141
1142 constexpr size_t numElements = 399;
1143 std::vector<GLColorRGB> reds(numElements, GLColorRGB::red);
1144 std::vector<GLColorRGB> greens(numElements, GLColorRGB::green);
1145 constexpr size_t sizeOfElem = sizeof(decltype(greens)::value_type);
1146 constexpr size_t sizeInBytes = numElements * sizeOfElem;
1147
1148 glBindBuffer(GL_ARRAY_BUFFER, srcBuffer);
1149 glBufferData(GL_ARRAY_BUFFER, sizeInBytes, greens.data(), GL_STREAM_DRAW);
1150
1151 glBindBuffer(GL_COPY_READ_BUFFER, dstBuffer);
1152 glBufferData(GL_COPY_READ_BUFFER, sizeInBytes, reds.data(), GL_STREAM_DRAW);
1153 ASSERT_GL_NO_ERROR();
1154
1155 constexpr size_t numQuads = numElements / 4;
1156
1157 // Generate quads that fill clip space to use all the vertex colors
1158 std::vector<float> positions(numQuads * 4 * 2);
1159 for (size_t quad = 0; quad < numQuads; ++quad)
1160 {
1161 size_t offset = quad * 4 * 2;
1162 float x0 = float(quad + 0) / numQuads * 2.0f - 1.0f;
1163 float x1 = float(quad + 1) / numQuads * 2.0f - 1.0f;
1164
1165 /*
1166 2--3
1167 | |
1168 0--1
1169 */
1170 positions[offset + 0] = x0;
1171 positions[offset + 1] = -1;
1172 positions[offset + 2] = x1;
1173 positions[offset + 3] = -1;
1174 positions[offset + 4] = x0;
1175 positions[offset + 5] = 1;
1176 positions[offset + 6] = x1;
1177 positions[offset + 7] = 1;
1178 }
1179 glBindBuffer(GL_ARRAY_BUFFER, 0);
1180 glEnableVertexAttribArray(posLoc);
1181 glVertexAttribPointer(posLoc, 2, GL_FLOAT, GL_FALSE, 0, positions.data());
1182 ASSERT_GL_NO_ERROR();
1183
1184 glBindBuffer(GL_ARRAY_BUFFER, srcBuffer);
1185 glEnableVertexAttribArray(colorLoc);
1186 glVertexAttribPointer(colorLoc, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, nullptr);
1187 ASSERT_GL_NO_ERROR();
1188
1189 glClear(GL_COLOR_BUFFER_BIT);
1190
1191 std::vector<GLushort> indices(numQuads * 6);
1192 for (size_t quad = 0; quad < numQuads; ++quad)
1193 {
1194 size_t ndx = quad * 4;
1195 size_t offset = quad * 6;
1196 indices[offset + 0] = ndx;
1197 indices[offset + 1] = ndx + 1;
1198 indices[offset + 2] = ndx + 2;
1199 indices[offset + 3] = ndx + 2;
1200 indices[offset + 4] = ndx + 1;
1201 indices[offset + 5] = ndx + 3;
1202 }
1203 GLBuffer indexBuffer;
1204 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
1205 glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(decltype(indices)::value_type),
1206 indices.data(), GL_STATIC_DRAW);
1207
1208 // Draw with srcBuffer (green)
1209 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, 0);
1210 EXPECT_PIXEL_RECT_EQ(0, 0, 16, 16, GLColor::green);
1211 ASSERT_GL_NO_ERROR();
1212
1213 // Draw with dstBuffer (red)
1214 glBindBuffer(GL_ARRAY_BUFFER, dstBuffer);
1215 glEnableVertexAttribArray(colorLoc);
1216 glVertexAttribPointer(colorLoc, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, nullptr);
1217 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, 0);
1218 EXPECT_PIXEL_RECT_EQ(0, 0, 16, 16, GLColor::red);
1219 ASSERT_GL_NO_ERROR();
1220
1221 // Copy src to dst. Yes, we're using GL_COPY_READ_BUFFER as dest because that's what the dEQP
1222 // test was testing.
1223 glBindBuffer(GL_ARRAY_BUFFER, srcBuffer);
1224 glBindBuffer(GL_COPY_READ_BUFFER, dstBuffer);
1225 glCopyBufferSubData(GL_ARRAY_BUFFER, GL_COPY_READ_BUFFER, 0, 0, sizeInBytes);
1226 ASSERT_GL_NO_ERROR();
1227
1228 // Draw with srcBuffer. It should still be green.
1229 glBindBuffer(GL_ARRAY_BUFFER, srcBuffer);
1230 glEnableVertexAttribArray(colorLoc);
1231 glVertexAttribPointer(colorLoc, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, nullptr);
1232 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, 0);
1233 EXPECT_PIXEL_RECT_EQ(0, 0, 16, 16, GLColor::green);
1234 ASSERT_GL_NO_ERROR();
1235
1236 // Draw with dstBuffer. It should now be green too.
1237 glBindBuffer(GL_ARRAY_BUFFER, dstBuffer);
1238 glEnableVertexAttribArray(colorLoc);
1239 glVertexAttribPointer(colorLoc, 3, GL_UNSIGNED_BYTE, GL_TRUE, 0, nullptr);
1240 glDrawElements(GL_TRIANGLES, numQuads * 6, GL_UNSIGNED_SHORT, 0);
1241 EXPECT_PIXEL_RECT_EQ(0, 0, 16, 16, GLColor::green);
1242
1243 ASSERT_GL_NO_ERROR();
1244 }
1245
1246 // Ensures that calling glBufferData on a mapped buffer results in an unmapped buffer
TEST_P(BufferDataTestES3,BufferDataUnmap)1247 TEST_P(BufferDataTestES3, BufferDataUnmap)
1248 {
1249 // Per the OpenGL ES 3.0 spec, buffers are implicity unmapped when a call to
1250 // BufferData happens on a mapped buffer:
1251 //
1252 // If any portion of the buffer object is mapped in the current context or
1253 // any context current to another thread, it is as though UnmapBuffer
1254 // (see section 2.10.3) is executed in each such context prior to deleting
1255 // the existing data store.
1256 //
1257
1258 std::vector<uint8_t> data1(16);
1259 std::vector<uint8_t> data2(16);
1260
1261 GLBuffer dataBuffer;
1262 glBindBuffer(GL_ARRAY_BUFFER, dataBuffer);
1263 glBufferData(GL_ARRAY_BUFFER, data1.size(), data1.data(), GL_STATIC_DRAW);
1264
1265 // Map the buffer once
1266 glMapBufferRange(GL_ARRAY_BUFFER, 0, data1.size(),
1267 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_FLUSH_EXPLICIT_BIT |
1268 GL_MAP_UNSYNCHRONIZED_BIT);
1269
1270 // Then repopulate the buffer. This should cause the buffer to become unmapped.
1271 glBufferData(GL_ARRAY_BUFFER, data2.size(), data2.data(), GL_STATIC_DRAW);
1272 ASSERT_GL_NO_ERROR();
1273
1274 // Try to unmap the buffer, this should fail
1275 bool result = glUnmapBuffer(GL_ARRAY_BUFFER);
1276 ASSERT_EQ(result, false);
1277 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1278
1279 // Try to map the buffer again, which should succeed
1280 glMapBufferRange(GL_ARRAY_BUFFER, 0, data2.size(),
1281 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT | GL_MAP_FLUSH_EXPLICIT_BIT |
1282 GL_MAP_UNSYNCHRONIZED_BIT);
1283 ASSERT_GL_NO_ERROR();
1284 }
1285
1286 // Ensures that mapping buffer with GL_MAP_INVALIDATE_BUFFER_BIT followed by glBufferSubData calls
1287 // works. Regression test for the Vulkan backend where that flag caused use after free.
TEST_P(BufferSubDataTest,MapInvalidateThenBufferSubData)1288 TEST_P(BufferSubDataTest, MapInvalidateThenBufferSubData)
1289 {
1290 // http://anglebug.com/5984
1291 ANGLE_SKIP_TEST_IF(IsWindows() && IsOpenGL() && IsIntel());
1292
1293 // http://anglebug.com/5985
1294 ANGLE_SKIP_TEST_IF(IsNexus5X() && IsOpenGLES());
1295
1296 const std::array<GLColor, 4> kInitialData = {GLColor::red, GLColor::red, GLColor::red,
1297 GLColor::red};
1298 const std::array<GLColor, 4> kUpdateData1 = {GLColor::white, GLColor::white, GLColor::white,
1299 GLColor::white};
1300 const std::array<GLColor, 4> kUpdateData2 = {GLColor::blue, GLColor::blue, GLColor::blue,
1301 GLColor::blue};
1302
1303 GLBuffer buffer;
1304 glBindBuffer(GL_UNIFORM_BUFFER, buffer);
1305 glBufferData(GL_UNIFORM_BUFFER, sizeof(kInitialData), kInitialData.data(), GL_DYNAMIC_DRAW);
1306 glBindBufferBase(GL_UNIFORM_BUFFER, 0, buffer);
1307 EXPECT_GL_NO_ERROR();
1308
1309 // Draw
1310 constexpr char kVerifyUBO[] = R"(#version 300 es
1311 precision mediump float;
1312 uniform block {
1313 uvec4 data;
1314 } ubo;
1315 uniform uint expect;
1316 uniform vec4 successOutput;
1317 out vec4 colorOut;
1318 void main()
1319 {
1320 if (all(equal(ubo.data, uvec4(expect))))
1321 colorOut = successOutput;
1322 else
1323 colorOut = vec4(1.0, 0, 0, 1.0);
1324 })";
1325
1326 ANGLE_GL_PROGRAM(verifyUbo, essl3_shaders::vs::Simple(), kVerifyUBO);
1327 glUseProgram(verifyUbo);
1328
1329 GLint expectLoc = glGetUniformLocation(verifyUbo, "expect");
1330 EXPECT_NE(-1, expectLoc);
1331 GLint successLoc = glGetUniformLocation(verifyUbo, "successOutput");
1332 EXPECT_NE(-1, successLoc);
1333
1334 glUniform1ui(expectLoc, kInitialData[0].asUint());
1335 glUniform4f(successLoc, 0, 1, 0, 1);
1336
1337 drawQuad(verifyUbo, essl3_shaders::PositionAttrib(), 0.5);
1338 EXPECT_GL_NO_ERROR();
1339
1340 // Dont't verify the buffer. This is testing GL_MAP_INVALIDATE_BUFFER_BIT while the buffer is
1341 // in use by the GPU.
1342
1343 // Map the buffer and update it.
1344 void *mappedBuffer = glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(kInitialData),
1345 GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
1346
1347 memcpy(mappedBuffer, kUpdateData1.data(), sizeof(kInitialData));
1348
1349 glUnmapBuffer(GL_UNIFORM_BUFFER);
1350 EXPECT_GL_NO_ERROR();
1351
1352 // Verify that the buffer has the updated value.
1353 glUniform1ui(expectLoc, kUpdateData1[0].asUint());
1354 glUniform4f(successLoc, 0, 0, 1, 1);
1355
1356 drawQuad(verifyUbo, essl3_shaders::PositionAttrib(), 0.5);
1357 EXPECT_GL_NO_ERROR();
1358
1359 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::blue);
1360
1361 // Update the buffer with glBufferSubData or glCopyBufferSubData
1362 updateBuffer(GL_UNIFORM_BUFFER, 0, sizeof(kUpdateData2), kUpdateData2.data());
1363 EXPECT_GL_NO_ERROR();
1364
1365 // Verify that the buffer has the updated value.
1366 glUniform1ui(expectLoc, kUpdateData2[0].asUint());
1367 glUniform4f(successLoc, 0, 1, 1, 1);
1368
1369 drawQuad(verifyUbo, essl3_shaders::PositionAttrib(), 0.5);
1370 EXPECT_GL_NO_ERROR();
1371
1372 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::cyan);
1373 }
1374
1375 // Verify that previous draws are not affected when a buffer is respecified with null data
1376 // and updated by calling map.
TEST_P(BufferDataTestES3,BufferDataWithNullFollowedByMap)1377 TEST_P(BufferDataTestES3, BufferDataWithNullFollowedByMap)
1378 {
1379 // Draw without using drawQuad.
1380 glUseProgram(mProgram);
1381
1382 // Set up position attribute
1383 const auto &quadVertices = GetQuadVertices();
1384 GLint positionLocation = glGetAttribLocation(mProgram, "position");
1385 ASSERT_NE(-1, positionLocation);
1386 GLBuffer positionBuffer;
1387 glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
1388 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * quadVertices.size() * 3, quadVertices.data(),
1389 GL_DYNAMIC_DRAW);
1390 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
1391 glEnableVertexAttribArray(positionLocation);
1392 EXPECT_GL_NO_ERROR();
1393
1394 // Set up "in_attrib" attribute
1395 const std::vector<GLfloat> kData(6, 1.0f);
1396 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
1397 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * kData.size(), kData.data(), GL_STATIC_DRAW);
1398 glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
1399 glEnableVertexAttribArray(mAttribLocation);
1400 EXPECT_GL_NO_ERROR();
1401
1402 // This draw (draw_0) renders red to the entire window.
1403 glDrawArrays(GL_TRIANGLES, 0, 6);
1404 EXPECT_GL_NO_ERROR();
1405
1406 // Respecify buffer bound to "in_attrib" attribute then map it and fill it with zeroes.
1407 const std::vector<GLfloat> kZeros(6, 0.0f);
1408 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * kZeros.size(), nullptr, GL_STATIC_DRAW);
1409 uint8_t *mapPtr = reinterpret_cast<uint8_t *>(
1410 glMapBufferRange(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * kZeros.size(),
1411 GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT));
1412 ASSERT_NE(nullptr, mapPtr);
1413 ASSERT_GL_NO_ERROR();
1414 memcpy(mapPtr, kZeros.data(), sizeof(GLfloat) * kZeros.size());
1415 glUnmapBuffer(GL_ARRAY_BUFFER);
1416 ASSERT_GL_NO_ERROR();
1417
1418 // This draw (draw_1) renders black to the upper right triangle.
1419 glDrawArrays(GL_TRIANGLES, 3, 3);
1420 EXPECT_GL_NO_ERROR();
1421
1422 // Respecification and data update of mBuffer should not have affected draw_0.
1423 // Expect bottom left to be red and top right to be black.
1424 EXPECT_PIXEL_COLOR_EQ(1, 1, GLColor::red);
1425 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() - 1, getWindowHeight() - 1, GLColor::black);
1426 EXPECT_GL_NO_ERROR();
1427 }
1428
1429 // Test glFenceSync call breaks renderPass followed by glCopyBufferSubData that read access the same
1430 // buffer that renderPass reads. There was a bug that this triggers assertion angleproject.com/7903.
TEST_P(BufferDataTestES3,bufferReadFromRenderPassAndOutsideRenderPassWithFenceSyncInBetween)1431 TEST_P(BufferDataTestES3, bufferReadFromRenderPassAndOutsideRenderPassWithFenceSyncInBetween)
1432 {
1433 glUseProgram(mProgram);
1434 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
1435 std::vector<GLfloat> data(6, 1.0f);
1436 GLsizei bufferSize = sizeof(GLfloat) * data.size();
1437 glBufferData(GL_ARRAY_BUFFER, bufferSize, data.data(), GL_DYNAMIC_DRAW);
1438 glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
1439 glEnableVertexAttribArray(mAttribLocation);
1440 glScissor(0, 0, getWindowWidth() / 2, getWindowHeight());
1441 drawQuad(mProgram, "position", 0.5f);
1442 EXPECT_GL_NO_ERROR();
1443
1444 GLsync sync = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
1445 EXPECT_GL_NO_ERROR();
1446
1447 GLBuffer dstBuffer;
1448 glBindBuffer(GL_COPY_WRITE_BUFFER, dstBuffer);
1449 glBufferData(GL_COPY_WRITE_BUFFER, bufferSize, nullptr, GL_STATIC_DRAW);
1450 glCopyBufferSubData(GL_ARRAY_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, bufferSize);
1451
1452 glBindBuffer(GL_ARRAY_BUFFER, dstBuffer);
1453 glScissor(getWindowWidth() / 2, 0, getWindowWidth() / 2, getWindowHeight());
1454 drawQuad(mProgram, "position", 0.5f);
1455 EXPECT_GL_NO_ERROR();
1456 glClientWaitSync(sync, GL_SYNC_FLUSH_COMMANDS_BIT, GL_TIMEOUT_IGNORED);
1457 EXPECT_PIXEL_COLOR_EQ(1, 1, GLColor::red);
1458 EXPECT_PIXEL_COLOR_EQ(getWindowWidth() - 1, getWindowHeight() - 1, GLColor::red);
1459 }
1460
1461 class BufferStorageTestES3 : public BufferDataTest
1462 {};
1463
1464 // Tests that proper error value is returned when bad size is passed in
TEST_P(BufferStorageTestES3,BufferStorageInvalidSize)1465 TEST_P(BufferStorageTestES3, BufferStorageInvalidSize)
1466 {
1467 ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1468
1469 std::vector<GLfloat> data(6, 1.0f);
1470
1471 GLBuffer buffer;
1472 glBindBuffer(GL_ARRAY_BUFFER, buffer);
1473 glBufferStorageEXT(GL_ARRAY_BUFFER, 0, data.data(), 0);
1474 EXPECT_GL_ERROR(GL_INVALID_VALUE);
1475 }
1476
1477 // Tests that buffer storage can be allocated with the GL_MAP_PERSISTENT_BIT_EXT and
1478 // GL_MAP_COHERENT_BIT_EXT flags
TEST_P(BufferStorageTestES3,BufferStorageFlagsPersistentCoherentWrite)1479 TEST_P(BufferStorageTestES3, BufferStorageFlagsPersistentCoherentWrite)
1480 {
1481 ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1482
1483 std::vector<GLfloat> data(6, 1.0f);
1484
1485 GLBuffer buffer;
1486 glBindBuffer(GL_ARRAY_BUFFER, buffer);
1487 glBufferStorageEXT(GL_ARRAY_BUFFER, data.size(), data.data(),
1488 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1489 ASSERT_GL_NO_ERROR();
1490 }
1491
1492 // Verify that glBufferStorage makes a buffer immutable
TEST_P(BufferStorageTestES3,StorageBufferBufferData)1493 TEST_P(BufferStorageTestES3, StorageBufferBufferData)
1494 {
1495 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1496 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1497
1498 std::vector<GLfloat> data(6, 1.0f);
1499
1500 GLBuffer buffer;
1501 glBindBuffer(GL_ARRAY_BUFFER, buffer);
1502 glBufferStorageEXT(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), data.data(), 0);
1503 ASSERT_GL_NO_ERROR();
1504
1505 // Verify that calling glBufferStorageEXT again produces an error.
1506 glBufferStorageEXT(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), data.data(), 0);
1507 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1508
1509 // Verify that calling glBufferData after calling glBufferStorageEXT produces an error.
1510 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), data.data(), GL_STATIC_DRAW);
1511 EXPECT_GL_ERROR(GL_INVALID_OPERATION);
1512 }
1513
1514 // Verify that glBufferStorageEXT can be called after glBufferData
TEST_P(BufferStorageTestES3,BufferDataStorageBuffer)1515 TEST_P(BufferStorageTestES3, BufferDataStorageBuffer)
1516 {
1517 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1518 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1519
1520 std::vector<GLfloat> data(6, 1.0f);
1521
1522 GLBuffer buffer;
1523 glBindBuffer(GL_ARRAY_BUFFER, buffer);
1524 glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), data.data(), GL_STATIC_DRAW);
1525 ASSERT_GL_NO_ERROR();
1526
1527 // Verify that calling glBufferStorageEXT again produces an error.
1528 glBufferStorageEXT(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), data.data(), 0);
1529 ASSERT_GL_NO_ERROR();
1530 }
1531
1532 // Verify that we can perform subdata updates to a buffer marked with GL_DYNAMIC_STORAGE_BIT_EXT
1533 // usage flag
TEST_P(BufferStorageTestES3,StorageBufferSubData)1534 TEST_P(BufferStorageTestES3, StorageBufferSubData)
1535 {
1536 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1537 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1538
1539 std::vector<GLfloat> data(6, 0.0f);
1540
1541 glUseProgram(mProgram);
1542 glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
1543 glBufferStorageEXT(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), nullptr,
1544 GL_DYNAMIC_STORAGE_BIT_EXT);
1545 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * data.size(), data.data());
1546 glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
1547 glEnableVertexAttribArray(mAttribLocation);
1548
1549 drawQuad(mProgram, "position", 0.5f);
1550 EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::black);
1551 EXPECT_GL_NO_ERROR();
1552
1553 std::vector<GLfloat> data2(6, 1.0f);
1554 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * data2.size(), data2.data());
1555
1556 drawQuad(mProgram, "position", 0.5f);
1557 EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::red);
1558 EXPECT_GL_NO_ERROR();
1559 }
1560
1561 // Test interaction between GL_OES_mapbuffer and GL_EXT_buffer_storage extensions.
TEST_P(BufferStorageTestES3,StorageBufferMapBufferOES)1562 TEST_P(BufferStorageTestES3, StorageBufferMapBufferOES)
1563 {
1564 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1565 !IsGLExtensionEnabled("GL_EXT_buffer_storage") ||
1566 !IsGLExtensionEnabled("GL_EXT_map_buffer_range"));
1567
1568 std::vector<uint8_t> data(1024);
1569 FillVectorWithRandomUBytes(&data);
1570
1571 GLBuffer buffer;
1572 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
1573 glBufferStorageEXT(GL_ARRAY_BUFFER, data.size(), nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
1574
1575 // Validate that other map flags don't work.
1576 void *badMapPtr = glMapBufferOES(GL_ARRAY_BUFFER, GL_MAP_READ_BIT);
1577 EXPECT_EQ(nullptr, badMapPtr);
1578 EXPECT_GL_ERROR(GL_INVALID_ENUM);
1579
1580 // Map and write.
1581 void *mapPtr = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
1582 ASSERT_NE(nullptr, mapPtr);
1583 ASSERT_GL_NO_ERROR();
1584 memcpy(mapPtr, data.data(), data.size());
1585 glUnmapBufferOES(GL_ARRAY_BUFFER);
1586
1587 // Validate data with EXT_map_buffer_range
1588 void *readMapPtr = glMapBufferRangeEXT(GL_ARRAY_BUFFER, 0, data.size(), GL_MAP_READ_BIT_EXT);
1589 ASSERT_NE(nullptr, readMapPtr);
1590 ASSERT_GL_NO_ERROR();
1591 std::vector<uint8_t> actualData(data.size());
1592 memcpy(actualData.data(), readMapPtr, data.size());
1593 glUnmapBufferOES(GL_ARRAY_BUFFER);
1594
1595 EXPECT_EQ(data, actualData);
1596 }
1597
1598 // Verify persistently mapped buffers can use glCopyBufferSubData
1599 // Tests a pattern used by Fortnite's GLES backend
TEST_P(BufferStorageTestES3,StorageCopyBufferSubDataMapped)1600 TEST_P(BufferStorageTestES3, StorageCopyBufferSubDataMapped)
1601 {
1602 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1603 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1604
1605 const std::array<GLColor, 4> kInitialData = {GLColor::red, GLColor::green, GLColor::blue,
1606 GLColor::yellow};
1607
1608 // Set up the read buffer
1609 GLBuffer readBuffer;
1610 glBindBuffer(GL_ARRAY_BUFFER, readBuffer.get());
1611 glBufferData(GL_ARRAY_BUFFER, sizeof(kInitialData), kInitialData.data(), GL_DYNAMIC_DRAW);
1612
1613 // Set up the write buffer to be persistently mapped
1614 GLBuffer writeBuffer;
1615 glBindBuffer(GL_COPY_WRITE_BUFFER, writeBuffer.get());
1616 glBufferStorageEXT(GL_COPY_WRITE_BUFFER, 16, nullptr,
1617 GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1618 void *readMapPtr =
1619 glMapBufferRange(GL_COPY_WRITE_BUFFER, 0, 16,
1620 GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1621 ASSERT_NE(nullptr, readMapPtr);
1622 ASSERT_GL_NO_ERROR();
1623
1624 // Verify we can copy into the write buffer
1625 glBindBuffer(GL_COPY_READ_BUFFER, readBuffer.get());
1626 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, 16);
1627 ASSERT_GL_NO_ERROR();
1628
1629 // Flush the buffer.
1630 glFinish();
1631
1632 // Check the contents
1633 std::array<GLColor, 4> resultingData;
1634 memcpy(resultingData.data(), readMapPtr, resultingData.size() * sizeof(GLColor));
1635 glUnmapBuffer(GL_COPY_WRITE_BUFFER);
1636 EXPECT_EQ(kInitialData, resultingData);
1637 ASSERT_GL_NO_ERROR();
1638 }
1639
1640 // Verify persistently mapped element array buffers can use glDrawElements
TEST_P(BufferStorageTestES3,DrawElementsElementArrayBufferMapped)1641 TEST_P(BufferStorageTestES3, DrawElementsElementArrayBufferMapped)
1642 {
1643 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1644 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1645
1646 GLfloat kVertexBuffer[] = {-1.0f, -1.0f, 1.0f, // (x, y, R)
1647 -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
1648 // Set up array buffer
1649 GLBuffer readBuffer;
1650 glBindBuffer(GL_ARRAY_BUFFER, readBuffer.get());
1651 glBufferData(GL_ARRAY_BUFFER, sizeof(kVertexBuffer), kVertexBuffer, GL_DYNAMIC_DRAW);
1652 GLint vLoc = glGetAttribLocation(mProgram, "position");
1653 GLint cLoc = mAttribLocation;
1654 glVertexAttribPointer(vLoc, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
1655 glEnableVertexAttribArray(vLoc);
1656 glVertexAttribPointer(cLoc, 1, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (const GLvoid *)8);
1657 glEnableVertexAttribArray(cLoc);
1658
1659 // Set up the element array buffer to be persistently mapped
1660 GLshort kElementArrayBuffer[] = {0, 0, 0, 0, 0, 0};
1661
1662 GLBuffer indexBuffer;
1663 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
1664 glBufferStorageEXT(GL_ELEMENT_ARRAY_BUFFER, sizeof(kElementArrayBuffer), kElementArrayBuffer,
1665 GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1666 GL_MAP_COHERENT_BIT_EXT);
1667
1668 glUseProgram(mProgram);
1669
1670 glClearColor(0, 0, 0, 0);
1671 glClear(GL_COLOR_BUFFER_BIT);
1672
1673 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
1674 ASSERT_GL_NO_ERROR();
1675 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack);
1676
1677 GLshort *mappedPtr = (GLshort *)glMapBufferRange(
1678 GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(kElementArrayBuffer),
1679 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1680 ASSERT_NE(nullptr, mappedPtr);
1681 ASSERT_GL_NO_ERROR();
1682
1683 mappedPtr[0] = 0;
1684 mappedPtr[1] = 1;
1685 mappedPtr[2] = 2;
1686 mappedPtr[3] = 2;
1687 mappedPtr[4] = 1;
1688 mappedPtr[5] = 3;
1689 glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
1690
1691 glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
1692
1693 ASSERT_GL_NO_ERROR();
1694
1695 EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
1696 }
1697
1698 // Test that maps a coherent buffer storage and does not call glUnmapBuffer.
TEST_P(BufferStorageTestES3,NoUnmap)1699 TEST_P(BufferStorageTestES3, NoUnmap)
1700 {
1701 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1702 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1703
1704 GLsizei size = sizeof(GLfloat) * 128;
1705
1706 GLBuffer buffer;
1707 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
1708 glBufferStorageEXT(GL_ARRAY_BUFFER, size, nullptr,
1709 GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1710 GL_MAP_COHERENT_BIT_EXT);
1711
1712 GLshort *mappedPtr = (GLshort *)glMapBufferRange(
1713 GL_ARRAY_BUFFER, 0, size,
1714 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1715 ASSERT_NE(nullptr, mappedPtr);
1716
1717 ASSERT_GL_NO_ERROR();
1718 }
1719
1720 // Test that we are able to perform glTex*D calls while a pixel unpack buffer is bound
1721 // and persistently mapped.
TEST_P(BufferStorageTestES3,TexImage2DPixelUnpackBufferMappedPersistently)1722 TEST_P(BufferStorageTestES3, TexImage2DPixelUnpackBufferMappedPersistently)
1723 {
1724 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1725 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1726
1727 std::vector<uint8_t> data(64);
1728 FillVectorWithRandomUBytes(&data);
1729
1730 GLBuffer buffer;
1731 glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer.get());
1732 glBufferStorageEXT(GL_PIXEL_UNPACK_BUFFER, data.size(), data.data(),
1733 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT);
1734
1735 // Map the buffer.
1736 void *mapPtr = glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, data.size(),
1737 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT);
1738 ASSERT_NE(nullptr, mapPtr);
1739 ASSERT_GL_NO_ERROR();
1740
1741 // Create a 2D texture and fill it using the persistenly mapped unpack buffer
1742 GLTexture tex;
1743 glBindTexture(GL_TEXTURE_2D, tex);
1744
1745 constexpr GLsizei kTextureWidth = 4;
1746 constexpr GLsizei kTextureHeight = 4;
1747 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kTextureWidth, kTextureHeight, 0, GL_RGBA,
1748 GL_UNSIGNED_BYTE, 0);
1749 ASSERT_GL_NO_ERROR();
1750
1751 glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1752 ASSERT_GL_NO_ERROR();
1753 }
1754
1755 // Verify persistently mapped buffers can use glBufferSubData
TEST_P(BufferStorageTestES3,StorageBufferSubDataMapped)1756 TEST_P(BufferStorageTestES3, StorageBufferSubDataMapped)
1757 {
1758 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1759 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1760
1761 const std::array<GLColor, 4> kUpdateData1 = {GLColor::red, GLColor::green, GLColor::blue,
1762 GLColor::yellow};
1763
1764 // Set up the buffer to be persistently mapped and dynamic
1765 GLBuffer buffer;
1766 glBindBuffer(GL_ARRAY_BUFFER, buffer.get());
1767 glBufferStorageEXT(GL_ARRAY_BUFFER, 16, nullptr,
1768 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1769 GL_MAP_COHERENT_BIT_EXT | GL_DYNAMIC_STORAGE_BIT_EXT);
1770 void *readMapPtr = glMapBufferRange(
1771 GL_ARRAY_BUFFER, 0, 16,
1772 GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1773 ASSERT_NE(nullptr, readMapPtr);
1774 ASSERT_GL_NO_ERROR();
1775
1776 // Verify we can push new data into the buffer
1777 glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLColor) * kUpdateData1.size(), kUpdateData1.data());
1778 ASSERT_GL_NO_ERROR();
1779
1780 // Flush the buffer.
1781 glFinish();
1782
1783 // Check the contents
1784 std::array<GLColor, 4> persistentData1;
1785 memcpy(persistentData1.data(), readMapPtr, persistentData1.size() * sizeof(GLColor));
1786 EXPECT_EQ(kUpdateData1, persistentData1);
1787 glUnmapBuffer(GL_ARRAY_BUFFER);
1788 ASSERT_GL_NO_ERROR();
1789 }
1790
1791 // Verify that persistently mapped coherent buffers can be used as uniform buffers,
1792 // and written to by using the pointer from glMapBufferRange.
TEST_P(BufferStorageTestES3,UniformBufferMapped)1793 TEST_P(BufferStorageTestES3, UniformBufferMapped)
1794 {
1795 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1796 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1797
1798 const char *mkFS = R"(#version 300 es
1799 precision highp float;
1800 uniform uni { vec4 color; };
1801 out vec4 fragColor;
1802 void main()
1803 {
1804 fragColor = color;
1805 })";
1806
1807 ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), mkFS);
1808 ASSERT_NE(program, 0u);
1809
1810 GLint uniformBufferIndex = glGetUniformBlockIndex(program, "uni");
1811 ASSERT_NE(uniformBufferIndex, -1);
1812
1813 GLBuffer uniformBuffer;
1814
1815 ASSERT_GL_NO_ERROR();
1816
1817 glViewport(0, 0, getWindowWidth(), getWindowHeight());
1818 glClear(GL_COLOR_BUFFER_BIT);
1819
1820 glBindBuffer(GL_UNIFORM_BUFFER, uniformBuffer.get());
1821
1822 glBufferStorageEXT(GL_UNIFORM_BUFFER, sizeof(float) * 4, nullptr,
1823 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1824
1825 float *mapPtr = static_cast<float *>(
1826 glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(float) * 4,
1827 GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1828 GL_MAP_COHERENT_BIT_EXT));
1829
1830 ASSERT_NE(mapPtr, nullptr);
1831
1832 glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniformBuffer.get());
1833
1834 glUniformBlockBinding(program, uniformBufferIndex, 0);
1835
1836 mapPtr[0] = 0.5f;
1837 mapPtr[1] = 0.75f;
1838 mapPtr[2] = 0.25f;
1839 mapPtr[3] = 1.0f;
1840
1841 drawQuad(program, essl3_shaders::PositionAttrib(), 0.5f);
1842
1843 EXPECT_PIXEL_NEAR(0, 0, 128, 191, 64, 255, 1);
1844
1845 glUnmapBuffer(GL_UNIFORM_BUFFER);
1846
1847 glDeleteProgram(program);
1848
1849 ASSERT_GL_NO_ERROR();
1850 }
1851
1852 // Verify that persistently mapped coherent buffers can be used as vertex array buffers,
1853 // and written to by using the pointer from glMapBufferRange.
TEST_P(BufferStorageTestES3,VertexBufferMapped)1854 TEST_P(BufferStorageTestES3, VertexBufferMapped)
1855 {
1856 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1857 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1858
1859 ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), essl3_shaders::fs::Red());
1860 ASSERT_NE(program, 0u);
1861
1862 glUseProgram(program);
1863
1864 auto quadVertices = GetQuadVertices();
1865
1866 size_t bufferSize = sizeof(GLfloat) * quadVertices.size() * 3;
1867
1868 GLBuffer positionBuffer;
1869 glBindBuffer(GL_ARRAY_BUFFER, positionBuffer.get());
1870
1871 glBufferStorageEXT(GL_ARRAY_BUFFER, bufferSize, nullptr,
1872 GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1873 GL_MAP_COHERENT_BIT_EXT);
1874
1875 GLint positionLocation = glGetAttribLocation(program, essl3_shaders::PositionAttrib());
1876 ASSERT_NE(-1, positionLocation);
1877 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
1878 glEnableVertexAttribArray(positionLocation);
1879
1880 void *mappedPtr =
1881 glMapBufferRange(GL_ARRAY_BUFFER, 0, bufferSize,
1882 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1883 ASSERT_NE(nullptr, mappedPtr);
1884
1885 memcpy(mappedPtr, reinterpret_cast<void *>(quadVertices.data()), bufferSize);
1886
1887 glDrawArrays(GL_TRIANGLES, 0, quadVertices.size());
1888 EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::red);
1889
1890 glUnmapBuffer(GL_ARRAY_BUFFER);
1891 glDeleteProgram(program);
1892
1893 EXPECT_GL_NO_ERROR();
1894 }
1895
TestPageSharingBuffers(std::function<void (void)> swapCallback,size_t bufferSize,const std::array<Vector3,6> & quadVertices,GLint positionLocation)1896 void TestPageSharingBuffers(std::function<void(void)> swapCallback,
1897 size_t bufferSize,
1898 const std::array<Vector3, 6> &quadVertices,
1899 GLint positionLocation)
1900 {
1901 size_t dataSize = sizeof(GLfloat) * quadVertices.size() * 3;
1902
1903 if (bufferSize == 0)
1904 {
1905 bufferSize = dataSize;
1906 }
1907
1908 constexpr size_t bufferCount = 10;
1909
1910 std::vector<GLBuffer> buffers(bufferCount);
1911 std::vector<void *> mapPointers(bufferCount);
1912
1913 // Init and map
1914 for (uint32_t i = 0; i < bufferCount; i++)
1915 {
1916 glBindBuffer(GL_ARRAY_BUFFER, buffers[i].get());
1917
1918 glBufferStorageEXT(GL_ARRAY_BUFFER, bufferSize, nullptr,
1919 GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT |
1920 GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1921
1922 glEnableVertexAttribArray(positionLocation);
1923
1924 mapPointers[i] = glMapBufferRange(
1925 GL_ARRAY_BUFFER, 0, bufferSize,
1926 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1927 ASSERT_NE(nullptr, mapPointers[i]);
1928 }
1929
1930 // Write, draw and unmap
1931 for (uint32_t i = 0; i < bufferCount; i++)
1932 {
1933 memcpy(mapPointers[i], reinterpret_cast<const void *>(quadVertices.data()), dataSize);
1934
1935 // Write something to last float
1936 if (bufferSize > dataSize + sizeof(GLfloat))
1937 {
1938 size_t lastPosition = bufferSize / sizeof(GLfloat) - 1;
1939 reinterpret_cast<float *>(mapPointers[i])[lastPosition] = 1.0f;
1940 }
1941
1942 glBindBuffer(GL_ARRAY_BUFFER, buffers[i].get());
1943 glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
1944 glDrawArrays(GL_TRIANGLES, 0, quadVertices.size());
1945 EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::red);
1946 swapCallback();
1947 glUnmapBuffer(GL_ARRAY_BUFFER);
1948 }
1949 }
1950
1951 // Create multiple persistently mapped coherent buffers of different sizes that will likely share a
1952 // page. Map all buffers together and unmap each buffer after writing to it and using it for a draw.
1953 // This tests the behaviour of the coherent buffer tracker in frame capture when buffers that share
1954 // a page are written to after the other one is removed.
TEST_P(BufferStorageTestES3,PageSharingBuffers)1955 TEST_P(BufferStorageTestES3, PageSharingBuffers)
1956 {
1957 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1958 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1959
1960 ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), essl3_shaders::fs::Red());
1961 ASSERT_NE(program, 0u);
1962
1963 glUseProgram(program);
1964
1965 auto quadVertices = GetQuadVertices();
1966
1967 std::function<void(void)> swapCallback = [this]() { swapBuffers(); };
1968
1969 GLint positionLocation = glGetAttribLocation(program, essl3_shaders::PositionAttrib());
1970 ASSERT_NE(-1, positionLocation);
1971
1972 TestPageSharingBuffers(swapCallback, 0, quadVertices, positionLocation);
1973 TestPageSharingBuffers(swapCallback, 1000, quadVertices, positionLocation);
1974 TestPageSharingBuffers(swapCallback, 4096, quadVertices, positionLocation);
1975 TestPageSharingBuffers(swapCallback, 6144, quadVertices, positionLocation);
1976 TestPageSharingBuffers(swapCallback, 40960, quadVertices, positionLocation);
1977
1978 glDeleteProgram(program);
1979
1980 EXPECT_GL_NO_ERROR();
1981 }
1982
1983 class BufferStorageTestES3Threaded : public ANGLETest<>
1984 {
1985 protected:
BufferStorageTestES3Threaded()1986 BufferStorageTestES3Threaded()
1987 {
1988 setWindowWidth(16);
1989 setWindowHeight(16);
1990 setConfigRedBits(8);
1991 setConfigGreenBits(8);
1992 setConfigBlueBits(8);
1993 setConfigAlphaBits(8);
1994 setConfigDepthBits(24);
1995
1996 mProgram = 0;
1997 }
1998
testSetUp()1999 void testSetUp() override
2000 {
2001 constexpr char kVS[] = R"(#version 300 es
2002
2003 in vec4 position;
2004 in vec4 color;
2005 out vec4 out_color;
2006
2007 void main()
2008 {
2009 out_color = color;
2010 gl_Position = position;
2011 })";
2012
2013 constexpr char kFS[] = R"(#version 300 es
2014 precision highp float;
2015
2016 in vec4 out_color;
2017 out vec4 fragColor;
2018
2019 void main()
2020 {
2021 fragColor = vec4(out_color);
2022 })";
2023
2024 mProgram = CompileProgram(kVS, kFS);
2025 ASSERT_NE(mProgram, 0U);
2026
2027 glClearColor(0, 0, 0, 0);
2028 glClearDepthf(0.0);
2029 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2030
2031 glDisable(GL_DEPTH_TEST);
2032
2033 ASSERT_GL_NO_ERROR();
2034 }
2035
testTearDown()2036 void testTearDown() override { glDeleteProgram(mProgram); }
2037
updateColors(int i,int offset,const GLColor & color)2038 void updateColors(int i, int offset, const GLColor &color)
2039 {
2040 Vector4 colorVec = color.toNormalizedVector();
2041 mMappedPtr[offset + i * 4 + 0] = colorVec.x();
2042 mMappedPtr[offset + i * 4 + 1] = colorVec.y();
2043 mMappedPtr[offset + i * 4 + 2] = colorVec.z();
2044 mMappedPtr[offset + i * 4 + 3] = colorVec.w();
2045 }
2046
updateThreadedAndDraw(int offset,const GLColor & color)2047 void updateThreadedAndDraw(int offset, const GLColor &color)
2048 {
2049 std::mutex mutex;
2050 std::vector<std::thread> threads(4);
2051 for (size_t i = 0; i < 4; i++)
2052 {
2053 threads[i] = std::thread([&, i]() {
2054 std::lock_guard<decltype(mutex)> lock(mutex);
2055 updateColors(i, offset, color);
2056 });
2057 }
2058
2059 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
2060
2061 for (std::thread &thread : threads)
2062 {
2063 thread.join();
2064 }
2065 }
2066
2067 GLuint mProgram;
2068 GLfloat *mMappedPtr = nullptr;
2069 };
2070
2071 // Test using a large buffer storage for a color vertex array buffer, which is
2072 // off set every iteration step via glVertexAttribPointer.
2073 // Write to the buffer storage map pointer from multiple threads for the next iteration,
2074 // while drawing the current one.
TEST_P(BufferStorageTestES3Threaded,VertexBuffer)2075 TEST_P(BufferStorageTestES3Threaded, VertexBuffer)
2076 {
2077 ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
2078 !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
2079
2080 auto vertices = GetIndexedQuadVertices();
2081
2082 // Set up position buffer
2083 GLBuffer positionBuffer;
2084 glBindBuffer(GL_ARRAY_BUFFER, positionBuffer.get());
2085 glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(),
2086 GL_STATIC_DRAW);
2087
2088 GLint positionLoc = glGetAttribLocation(mProgram, "position");
2089 glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
2090 glEnableVertexAttribArray(positionLoc);
2091
2092 // Set up color buffer
2093 GLBuffer colorBuffer;
2094 glBindBuffer(GL_ARRAY_BUFFER, colorBuffer.get());
2095
2096 // Let's create a big buffer which fills 10 pages at pagesize 4096
2097 GLint bufferSize = sizeof(GLfloat) * 1024 * 10;
2098 GLint offsetFloats = 0;
2099
2100 glBufferStorageEXT(GL_ARRAY_BUFFER, bufferSize, nullptr,
2101 GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
2102 GL_MAP_COHERENT_BIT_EXT);
2103 GLint colorLoc = glGetAttribLocation(mProgram, "color");
2104 glEnableVertexAttribArray(colorLoc);
2105
2106 auto indices = GetQuadIndices();
2107
2108 GLBuffer indexBuffer;
2109 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer.get());
2110 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * indices.size(), indices.data(),
2111 GL_STATIC_DRAW);
2112
2113 glUseProgram(mProgram);
2114
2115 glClearColor(0, 0, 0, 0);
2116 glClear(GL_COLOR_BUFFER_BIT);
2117
2118 ASSERT_GL_NO_ERROR();
2119
2120 mMappedPtr = (GLfloat *)glMapBufferRange(
2121 GL_ARRAY_BUFFER, 0, bufferSize,
2122 GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
2123 ASSERT_NE(nullptr, mMappedPtr);
2124 ASSERT_GL_NO_ERROR();
2125
2126 // Initial color
2127 for (int i = 0; i < 4; i++)
2128 {
2129 updateColors(i, offsetFloats, GLColor::black);
2130 }
2131
2132 std::vector<GLColor> colors = {GLColor::red, GLColor::green, GLColor::blue};
2133
2134 // 4 vertices, 4 floats
2135 GLint contentSize = 4 * 4;
2136
2137 // Update and draw last
2138 int i = 0;
2139 while (bufferSize > (int)((offsetFloats + contentSize) * sizeof(GLfloat)))
2140 {
2141 glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
2142 reinterpret_cast<const GLvoid *>(offsetFloats * sizeof(GLfloat)));
2143
2144 offsetFloats += contentSize;
2145 GLColor color = colors[i % colors.size()];
2146 updateThreadedAndDraw(offsetFloats, color);
2147
2148 if (i > 0)
2149 {
2150 GLColor lastColor = colors[(i - 1) % colors.size()];
2151 EXPECT_PIXEL_COLOR_EQ(0, 0, lastColor);
2152 }
2153 ASSERT_GL_NO_ERROR();
2154 i++;
2155 }
2156
2157 // Last draw
2158 glVertexAttribPointer(colorLoc, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
2159 reinterpret_cast<const GLvoid *>(offsetFloats * sizeof(GLfloat)));
2160 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
2161
2162 glUnmapBuffer(GL_ARRAY_BUFFER);
2163
2164 ASSERT_GL_NO_ERROR();
2165 }
2166
2167 // Test that buffer self-copy works when buffer is used as UBO
TEST_P(BufferDataTestES3,CopyBufferSubDataSelfDependency)2168 TEST_P(BufferDataTestES3, CopyBufferSubDataSelfDependency)
2169 {
2170 constexpr char kFS[] = R"(#version 300 es
2171 precision mediump float;
2172 out vec4 color;
2173
2174 uniform UBO
2175 {
2176 vec4 data[128];
2177 };
2178
2179 void main()
2180 {
2181 color = data[12];
2182 })";
2183
2184 ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
2185 glUseProgram(program);
2186
2187 constexpr uint32_t kVec4Size = 4 * sizeof(float);
2188 constexpr uint32_t kUBOSize = 128 * kVec4Size;
2189 constexpr uint32_t kDataOffset = 12 * kVec4Size;
2190
2191 // Init data is 4 times the size of UBO as the buffer is created larger than the UBO throughout
2192 // the test.
2193 const std::vector<float> kInitData(kUBOSize, 123.45);
2194
2195 // Set up a throw-away buffer just to make buffer suballocations not use offset 0.
2196 GLBuffer throwaway;
2197 glBindBuffer(GL_UNIFORM_BUFFER, throwaway);
2198 glBufferData(GL_UNIFORM_BUFFER, 1024, nullptr, GL_DYNAMIC_DRAW);
2199
2200 // Set up the buffer
2201 GLBuffer buffer;
2202 glBindBuffer(GL_UNIFORM_BUFFER, buffer);
2203 glBufferData(GL_UNIFORM_BUFFER, kUBOSize * 2, kInitData.data(), GL_DYNAMIC_DRAW);
2204
2205 const std::vector<float> kColorData = {
2206 0.75,
2207 0.5,
2208 0.25,
2209 1.0,
2210 };
2211 glBufferSubData(GL_UNIFORM_BUFFER, kDataOffset, kVec4Size, kColorData.data());
2212
2213 glClearColor(0, 0, 0, 1);
2214 glClear(GL_COLOR_BUFFER_BIT);
2215 const int w = getWindowWidth();
2216 const int h = getWindowHeight();
2217
2218 // Use the buffer, then do a big self-copy
2219 glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, 0, kUBOSize);
2220 glScissor(0, 0, w / 2, h / 2);
2221 glEnable(GL_SCISSOR_TEST);
2222 drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2223
2224 // Duplicate the buffer in the second half
2225 glCopyBufferSubData(GL_UNIFORM_BUFFER, GL_UNIFORM_BUFFER, 0, kUBOSize, kUBOSize);
2226
2227 // Draw again, making sure the copy succeeded.
2228 glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, kUBOSize, kUBOSize);
2229 glScissor(w / 2, 0, w / 2, h / 2);
2230 drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2231
2232 // Do a small self-copy
2233 constexpr uint32_t kCopySrcOffset = 4 * kVec4Size;
2234 constexpr uint32_t kCopyDstOffset = (64 + 4) * kVec4Size;
2235 glCopyBufferSubData(GL_UNIFORM_BUFFER, GL_UNIFORM_BUFFER, kCopySrcOffset, kCopyDstOffset,
2236 kDataOffset);
2237
2238 // color data was previously at [12], and is now available at [68 + 12 - 4]
2239 glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, kCopyDstOffset - kCopySrcOffset, kUBOSize);
2240 glScissor(0, h / 2, w / 2, h / 2);
2241 drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2242
2243 // Validate results
2244 EXPECT_PIXEL_NEAR(0, 0, 191, 127, 63, 255, 1);
2245 EXPECT_PIXEL_NEAR(w / 2 + 1, 0, 191, 127, 63, 255, 1);
2246 EXPECT_PIXEL_NEAR(0, h / 2 + 1, 191, 127, 63, 255, 1);
2247 EXPECT_PIXEL_COLOR_EQ(w / 2 + 1, h / 2 + 1, GLColor::black);
2248
2249 // Do a big copy again, but this time the buffer is unused by the GPU
2250 glCopyBufferSubData(GL_UNIFORM_BUFFER, GL_UNIFORM_BUFFER, kUBOSize, 0, kUBOSize);
2251 glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, 0, kUBOSize);
2252 glScissor(w / 2, h / 2, w / 2, h / 2);
2253 drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2254 EXPECT_PIXEL_NEAR(w / 2 + 1, h / 2 + 1, 191, 127, 63, 255, 1);
2255
2256 // Do a small copy again, but this time the buffer is unused by the GPU
2257 glCopyBufferSubData(GL_UNIFORM_BUFFER, GL_UNIFORM_BUFFER, kUBOSize + kCopySrcOffset,
2258 kCopyDstOffset, kDataOffset);
2259 glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, kCopyDstOffset - kCopySrcOffset, kUBOSize);
2260 glDisable(GL_SCISSOR_TEST);
2261 glClear(GL_COLOR_BUFFER_BIT);
2262 drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2263 EXPECT_PIXEL_NEAR(0, 0, 191, 127, 63, 255, 1);
2264
2265 ASSERT_GL_NO_ERROR();
2266 }
2267
2268 ANGLE_INSTANTIATE_TEST_ES2(BufferDataTest);
2269
2270 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BufferSubDataTest);
2271 ANGLE_INSTANTIATE_TEST_COMBINE_1(BufferSubDataTest,
2272 BufferSubDataTestPrint,
2273 testing::Bool(),
2274 ANGLE_ALL_TEST_PLATFORMS_ES3,
2275 ES3_VULKAN().enable(Feature::PreferCPUForBufferSubData));
2276
2277 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BufferDataTestES3);
2278 ANGLE_INSTANTIATE_TEST_ES3_AND(BufferDataTestES3,
2279 ES3_VULKAN().enable(Feature::PreferCPUForBufferSubData),
2280 ES3_METAL().enable(Feature::ForceBufferGPUStorage));
2281
2282 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BufferStorageTestES3);
2283 ANGLE_INSTANTIATE_TEST_ES3(BufferStorageTestES3);
2284
2285 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(IndexedBufferCopyTest);
2286 ANGLE_INSTANTIATE_TEST_ES3(IndexedBufferCopyTest);
2287
2288 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BufferStorageTestES3Threaded);
2289 ANGLE_INSTANTIATE_TEST_ES3(BufferStorageTestES3Threaded);
2290
2291 #ifdef _WIN64
2292
2293 // Test a bug where an integer overflow bug could trigger a crash in D3D.
2294 // The test uses 8 buffers with a size just under 0x2000000 to overflow max uint
2295 // (with the internal D3D rounding to 16-byte values) and trigger the bug.
2296 // Only handle this bug on 64-bit Windows for now. Harder to repro on 32-bit.
2297 class BufferDataOverflowTest : public ANGLETest<>
2298 {
2299 protected:
BufferDataOverflowTest()2300 BufferDataOverflowTest() {}
2301 };
2302
2303 // See description above.
TEST_P(BufferDataOverflowTest,VertexBufferIntegerOverflow)2304 TEST_P(BufferDataOverflowTest, VertexBufferIntegerOverflow)
2305 {
2306 // These values are special, to trigger the rounding bug.
2307 unsigned int numItems = 0x7FFFFFE;
2308 constexpr GLsizei bufferCnt = 8;
2309
2310 std::vector<GLBuffer> buffers(bufferCnt);
2311
2312 std::stringstream vertexShaderStr;
2313
2314 for (GLsizei bufferIndex = 0; bufferIndex < bufferCnt; ++bufferIndex)
2315 {
2316 vertexShaderStr << "attribute float attrib" << bufferIndex << ";\n";
2317 }
2318
2319 vertexShaderStr << "attribute vec2 position;\n"
2320 "varying float v_attrib;\n"
2321 "void main() {\n"
2322 " gl_Position = vec4(position, 0, 1);\n"
2323 " v_attrib = 0.0;\n";
2324
2325 for (GLsizei bufferIndex = 0; bufferIndex < bufferCnt; ++bufferIndex)
2326 {
2327 vertexShaderStr << "v_attrib += attrib" << bufferIndex << ";\n";
2328 }
2329
2330 vertexShaderStr << "}";
2331
2332 constexpr char kFS[] =
2333 "varying highp float v_attrib;\n"
2334 "void main() {\n"
2335 " gl_FragColor = vec4(v_attrib, 0, 0, 1);\n"
2336 "}";
2337
2338 ANGLE_GL_PROGRAM(program, vertexShaderStr.str().c_str(), kFS);
2339 glUseProgram(program.get());
2340
2341 std::vector<GLfloat> data(numItems, 1.0f);
2342
2343 for (GLsizei bufferIndex = 0; bufferIndex < bufferCnt; ++bufferIndex)
2344 {
2345 glBindBuffer(GL_ARRAY_BUFFER, buffers[bufferIndex].get());
2346 glBufferData(GL_ARRAY_BUFFER, numItems * sizeof(float), &data[0], GL_DYNAMIC_DRAW);
2347
2348 std::stringstream attribNameStr;
2349 attribNameStr << "attrib" << bufferIndex;
2350
2351 GLint attribLocation = glGetAttribLocation(program.get(), attribNameStr.str().c_str());
2352 ASSERT_NE(-1, attribLocation);
2353
2354 glVertexAttribPointer(attribLocation, 1, GL_FLOAT, GL_FALSE, 4, nullptr);
2355 glEnableVertexAttribArray(attribLocation);
2356 }
2357
2358 GLint positionLocation = glGetAttribLocation(program.get(), "position");
2359 ASSERT_NE(-1, positionLocation);
2360 glDisableVertexAttribArray(positionLocation);
2361 glVertexAttrib2f(positionLocation, 1.0f, 1.0f);
2362
2363 EXPECT_GL_NO_ERROR();
2364 glDrawArrays(GL_TRIANGLES, 0, numItems);
2365 EXPECT_GL_ERROR(GL_OUT_OF_MEMORY);
2366
2367 // Test that a small draw still works.
2368 for (GLsizei bufferIndex = 0; bufferIndex < bufferCnt; ++bufferIndex)
2369 {
2370 std::stringstream attribNameStr;
2371 attribNameStr << "attrib" << bufferIndex;
2372 GLint attribLocation = glGetAttribLocation(program.get(), attribNameStr.str().c_str());
2373 ASSERT_NE(-1, attribLocation);
2374 glDisableVertexAttribArray(attribLocation);
2375 }
2376
2377 glDrawArrays(GL_TRIANGLES, 0, 3);
2378 EXPECT_GL_ERROR(GL_NO_ERROR);
2379 }
2380
2381 // Tests a security bug in our CopyBufferSubData validation (integer overflow).
TEST_P(BufferDataOverflowTest,CopySubDataValidation)2382 TEST_P(BufferDataOverflowTest, CopySubDataValidation)
2383 {
2384 GLBuffer readBuffer, writeBuffer;
2385
2386 glBindBuffer(GL_COPY_READ_BUFFER, readBuffer.get());
2387 glBindBuffer(GL_COPY_WRITE_BUFFER, writeBuffer.get());
2388
2389 constexpr int bufSize = 100;
2390
2391 glBufferData(GL_COPY_READ_BUFFER, bufSize, nullptr, GL_STATIC_DRAW);
2392 glBufferData(GL_COPY_WRITE_BUFFER, bufSize, nullptr, GL_STATIC_DRAW);
2393
2394 GLintptr big = std::numeric_limits<GLintptr>::max() - bufSize + 90;
2395
2396 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, big, 0, 50);
2397 EXPECT_GL_ERROR(GL_INVALID_VALUE);
2398
2399 glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, big, 50);
2400 EXPECT_GL_ERROR(GL_INVALID_VALUE);
2401 }
2402
2403 ANGLE_INSTANTIATE_TEST_ES3(BufferDataOverflowTest);
2404
2405 #endif // _WIN64
2406