xref: /aosp_15_r20/external/angle/src/tests/gl_tests/BufferDataTest.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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/42264622
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/42261546: 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> &paramsInfo)
210 {
211     const BufferSubDataTestParams &params = 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/42260302)
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);
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);
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);
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/42264515
1291     ANGLE_SKIP_TEST_IF(IsWindows() && IsOpenGL() && IsIntel());
1292 
1293     // http://anglebug.com/42264516
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 produces no error
1528     glBufferStorageEXT(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), data.data(), 0);
1529     ASSERT_GL_NO_ERROR();
1530 }
1531 
1532 // Verify that consecutive BufferStorage calls don't clobber data
1533 // This is a regression test for an AllocateNonZeroMemory bug, where the offset
1534 // of the suballocation wasn't being used properly
TEST_P(BufferStorageTestES3,BufferStorageClobber)1535 TEST_P(BufferStorageTestES3, BufferStorageClobber)
1536 {
1537     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1538                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1539 
1540     constexpr size_t largeSizes[] = {101, 103, 107, 109, 113, 127, 131, 137, 139};
1541     constexpr size_t smallSizes[] = {7, 11, 13, 17, 19, 23, 29, 31, 37, 41};
1542     constexpr size_t readBackSize = 16;
1543 
1544     for (size_t largeSize : largeSizes)
1545     {
1546         std::vector<GLubyte> data0(largeSize * 1024, 0x1E);
1547 
1548         // Check for a test author error, we can't read back more than the size of data0.
1549         ASSERT(readBackSize <= data0.size());
1550 
1551         // Do a large write first, ensure this is a device-local buffer only (no storage flags)
1552         GLBuffer buffer0;
1553         glBindBuffer(GL_ARRAY_BUFFER, buffer0);
1554         glBufferStorageEXT(GL_ARRAY_BUFFER, sizeof(GLubyte) * data0.size(), data0.data(), 0);
1555         ASSERT_GL_NO_ERROR();
1556 
1557         // Do a bunch of smaller writes next, creating/deleting buffers as
1558         // we go (we just want to try to fuzz it so we might write to the
1559         // same suballocation as the above)
1560         for (size_t smallSize : smallSizes)
1561         {
1562             std::vector<GLubyte> data1(smallSize, 0x4A);
1563             GLBuffer buffer1;
1564             glBindBuffer(GL_ARRAY_BUFFER, buffer1);
1565             glBufferStorageEXT(GL_ARRAY_BUFFER, sizeof(GLubyte) * data1.size(), data1.data(), 0);
1566             ASSERT_GL_NO_ERROR();
1567 
1568             // Force the buffer write (and other buffer creation setup work) to
1569             // flush
1570             glFinish();
1571         }
1572 
1573         // Create a staging area to read back the buffer
1574         GLBuffer mappable;
1575         glBindBuffer(GL_ARRAY_BUFFER, mappable);
1576         glBufferStorageEXT(GL_ARRAY_BUFFER, sizeof(GLubyte) * readBackSize, nullptr,
1577                            GL_MAP_READ_BIT);
1578         ASSERT_GL_NO_ERROR();
1579 
1580         glBindBuffer(GL_COPY_READ_BUFFER, buffer0);
1581         glBindBuffer(GL_COPY_WRITE_BUFFER, mappable);
1582         glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0,
1583                             sizeof(GLubyte) * readBackSize);
1584         ASSERT_GL_NO_ERROR();
1585         glBindBuffer(GL_COPY_READ_BUFFER, 0);
1586         glBindBuffer(GL_COPY_WRITE_BUFFER, 0);
1587 
1588         GLubyte *mapped = reinterpret_cast<GLubyte *>(
1589             glMapBufferRange(GL_ARRAY_BUFFER, 0, sizeof(GLubyte) * readBackSize, GL_MAP_READ_BIT));
1590         ASSERT_NE(mapped, nullptr);
1591         ASSERT_GL_NO_ERROR();
1592         for (size_t i = 0; i < readBackSize; i++)
1593         {
1594             EXPECT_EQ(mapped[i], data0[i])
1595                 << "Expected " << static_cast<int>(data0[i]) << " at index " << i << ", got "
1596                 << static_cast<int>(mapped[i]);
1597         }
1598     }
1599 }
1600 
1601 // Verify that we can perform subdata updates to a buffer marked with GL_DYNAMIC_STORAGE_BIT_EXT
1602 // usage flag
TEST_P(BufferStorageTestES3,StorageBufferSubData)1603 TEST_P(BufferStorageTestES3, StorageBufferSubData)
1604 {
1605     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1606                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1607 
1608     std::vector<GLfloat> data(6, 0.0f);
1609 
1610     glUseProgram(mProgram);
1611     glBindBuffer(GL_ARRAY_BUFFER, mBuffer);
1612     glBufferStorageEXT(GL_ARRAY_BUFFER, sizeof(GLfloat) * data.size(), nullptr,
1613                        GL_DYNAMIC_STORAGE_BIT_EXT);
1614     glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * data.size(), data.data());
1615     glVertexAttribPointer(mAttribLocation, 1, GL_FLOAT, GL_FALSE, 0, nullptr);
1616     glEnableVertexAttribArray(mAttribLocation);
1617 
1618     drawQuad(mProgram, "position", 0.5f);
1619     EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::black);
1620     EXPECT_GL_NO_ERROR();
1621 
1622     std::vector<GLfloat> data2(6, 1.0f);
1623     glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLfloat) * data2.size(), data2.data());
1624 
1625     drawQuad(mProgram, "position", 0.5f);
1626     EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::red);
1627     EXPECT_GL_NO_ERROR();
1628 }
1629 
1630 // Test interaction between GL_OES_mapbuffer and GL_EXT_buffer_storage extensions.
TEST_P(BufferStorageTestES3,StorageBufferMapBufferOES)1631 TEST_P(BufferStorageTestES3, StorageBufferMapBufferOES)
1632 {
1633     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1634                        !IsGLExtensionEnabled("GL_EXT_buffer_storage") ||
1635                        !IsGLExtensionEnabled("GL_EXT_map_buffer_range"));
1636 
1637     std::vector<uint8_t> data(1024);
1638     FillVectorWithRandomUBytes(&data);
1639 
1640     GLBuffer buffer;
1641     glBindBuffer(GL_ARRAY_BUFFER, buffer);
1642     glBufferStorageEXT(GL_ARRAY_BUFFER, data.size(), nullptr, GL_MAP_READ_BIT | GL_MAP_WRITE_BIT);
1643 
1644     // Validate that other map flags don't work.
1645     void *badMapPtr = glMapBufferOES(GL_ARRAY_BUFFER, GL_MAP_READ_BIT);
1646     EXPECT_EQ(nullptr, badMapPtr);
1647     EXPECT_GL_ERROR(GL_INVALID_ENUM);
1648 
1649     // Map and write.
1650     void *mapPtr = glMapBufferOES(GL_ARRAY_BUFFER, GL_WRITE_ONLY_OES);
1651     ASSERT_NE(nullptr, mapPtr);
1652     ASSERT_GL_NO_ERROR();
1653     memcpy(mapPtr, data.data(), data.size());
1654     glUnmapBufferOES(GL_ARRAY_BUFFER);
1655 
1656     // Validate data with EXT_map_buffer_range
1657     void *readMapPtr = glMapBufferRangeEXT(GL_ARRAY_BUFFER, 0, data.size(), GL_MAP_READ_BIT_EXT);
1658     ASSERT_NE(nullptr, readMapPtr);
1659     ASSERT_GL_NO_ERROR();
1660     std::vector<uint8_t> actualData(data.size());
1661     memcpy(actualData.data(), readMapPtr, data.size());
1662     glUnmapBufferOES(GL_ARRAY_BUFFER);
1663 
1664     EXPECT_EQ(data, actualData);
1665 }
1666 
1667 // Verify persistently mapped buffers can use glCopyBufferSubData
1668 // Tests a pattern used by Fortnite's GLES backend
TEST_P(BufferStorageTestES3,StorageCopyBufferSubDataMapped)1669 TEST_P(BufferStorageTestES3, StorageCopyBufferSubDataMapped)
1670 {
1671     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1672                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1673 
1674     const std::array<GLColor, 4> kInitialData = {GLColor::red, GLColor::green, GLColor::blue,
1675                                                  GLColor::yellow};
1676 
1677     // Set up the read buffer
1678     GLBuffer readBuffer;
1679     glBindBuffer(GL_ARRAY_BUFFER, readBuffer);
1680     glBufferData(GL_ARRAY_BUFFER, sizeof(kInitialData), kInitialData.data(), GL_DYNAMIC_DRAW);
1681 
1682     // Set up the write buffer to be persistently mapped
1683     GLBuffer writeBuffer;
1684     glBindBuffer(GL_COPY_WRITE_BUFFER, writeBuffer);
1685     glBufferStorageEXT(GL_COPY_WRITE_BUFFER, 16, nullptr,
1686                        GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1687     void *readMapPtr =
1688         glMapBufferRange(GL_COPY_WRITE_BUFFER, 0, 16,
1689                          GL_MAP_READ_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1690     ASSERT_NE(nullptr, readMapPtr);
1691     ASSERT_GL_NO_ERROR();
1692 
1693     // Verify we can copy into the write buffer
1694     glBindBuffer(GL_COPY_READ_BUFFER, readBuffer);
1695     glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, 0, 16);
1696     ASSERT_GL_NO_ERROR();
1697 
1698     // Flush the buffer.
1699     glFinish();
1700 
1701     // Check the contents
1702     std::array<GLColor, 4> resultingData;
1703     memcpy(resultingData.data(), readMapPtr, resultingData.size() * sizeof(GLColor));
1704     glUnmapBuffer(GL_COPY_WRITE_BUFFER);
1705     EXPECT_EQ(kInitialData, resultingData);
1706     ASSERT_GL_NO_ERROR();
1707 }
1708 
1709 // Verify persistently mapped element array buffers can use glDrawElements
TEST_P(BufferStorageTestES3,DrawElementsElementArrayBufferMapped)1710 TEST_P(BufferStorageTestES3, DrawElementsElementArrayBufferMapped)
1711 {
1712     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1713                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1714 
1715     GLfloat kVertexBuffer[] = {-1.0f, -1.0f, 1.0f,  // (x, y, R)
1716                                -1.0f, 1.0f,  1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f};
1717     // Set up array buffer
1718     GLBuffer readBuffer;
1719     glBindBuffer(GL_ARRAY_BUFFER, readBuffer);
1720     glBufferData(GL_ARRAY_BUFFER, sizeof(kVertexBuffer), kVertexBuffer, GL_DYNAMIC_DRAW);
1721     GLint vLoc = glGetAttribLocation(mProgram, "position");
1722     GLint cLoc = mAttribLocation;
1723     glVertexAttribPointer(vLoc, 2, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
1724     glEnableVertexAttribArray(vLoc);
1725     glVertexAttribPointer(cLoc, 1, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (const GLvoid *)8);
1726     glEnableVertexAttribArray(cLoc);
1727 
1728     // Set up the element array buffer to be persistently mapped
1729     GLshort kElementArrayBuffer[] = {0, 0, 0, 0, 0, 0};
1730 
1731     GLBuffer indexBuffer;
1732     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
1733     glBufferStorageEXT(GL_ELEMENT_ARRAY_BUFFER, sizeof(kElementArrayBuffer), kElementArrayBuffer,
1734                        GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1735                            GL_MAP_COHERENT_BIT_EXT);
1736 
1737     glUseProgram(mProgram);
1738 
1739     glClearColor(0, 0, 0, 0);
1740     glClear(GL_COLOR_BUFFER_BIT);
1741 
1742     glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
1743     ASSERT_GL_NO_ERROR();
1744     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::transparentBlack);
1745 
1746     GLshort *mappedPtr = (GLshort *)glMapBufferRange(
1747         GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(kElementArrayBuffer),
1748         GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1749     ASSERT_NE(nullptr, mappedPtr);
1750     ASSERT_GL_NO_ERROR();
1751 
1752     mappedPtr[0] = 0;
1753     mappedPtr[1] = 1;
1754     mappedPtr[2] = 2;
1755     mappedPtr[3] = 2;
1756     mappedPtr[4] = 1;
1757     mappedPtr[5] = 3;
1758     glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_SHORT, 0);
1759 
1760     glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
1761 
1762     ASSERT_GL_NO_ERROR();
1763 
1764     EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
1765 }
1766 
1767 // Test that maps a coherent buffer storage and does not call glUnmapBuffer.
TEST_P(BufferStorageTestES3,NoUnmap)1768 TEST_P(BufferStorageTestES3, NoUnmap)
1769 {
1770     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1771                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1772 
1773     GLsizei size = sizeof(GLfloat) * 128;
1774 
1775     GLBuffer buffer;
1776     glBindBuffer(GL_ARRAY_BUFFER, buffer);
1777     glBufferStorageEXT(GL_ARRAY_BUFFER, size, nullptr,
1778                        GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1779                            GL_MAP_COHERENT_BIT_EXT);
1780 
1781     GLshort *mappedPtr = (GLshort *)glMapBufferRange(
1782         GL_ARRAY_BUFFER, 0, size,
1783         GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1784     ASSERT_NE(nullptr, mappedPtr);
1785 
1786     ASSERT_GL_NO_ERROR();
1787 }
1788 
1789 // Test that we are able to perform glTex*D calls while a pixel unpack buffer is bound
1790 // and persistently mapped.
TEST_P(BufferStorageTestES3,TexImage2DPixelUnpackBufferMappedPersistently)1791 TEST_P(BufferStorageTestES3, TexImage2DPixelUnpackBufferMappedPersistently)
1792 {
1793     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1794                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1795 
1796     std::vector<uint8_t> data(64);
1797     FillVectorWithRandomUBytes(&data);
1798 
1799     GLBuffer buffer;
1800     glBindBuffer(GL_PIXEL_UNPACK_BUFFER, buffer);
1801     glBufferStorageEXT(GL_PIXEL_UNPACK_BUFFER, data.size(), data.data(),
1802                        GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT);
1803 
1804     // Map the buffer.
1805     void *mapPtr = glMapBufferRange(GL_PIXEL_UNPACK_BUFFER, 0, data.size(),
1806                                     GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT);
1807     ASSERT_NE(nullptr, mapPtr);
1808     ASSERT_GL_NO_ERROR();
1809 
1810     // Create a 2D texture and fill it using the persistenly mapped unpack buffer
1811     GLTexture tex;
1812     glBindTexture(GL_TEXTURE_2D, tex);
1813 
1814     constexpr GLsizei kTextureWidth  = 4;
1815     constexpr GLsizei kTextureHeight = 4;
1816     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, kTextureWidth, kTextureHeight, 0, GL_RGBA,
1817                  GL_UNSIGNED_BYTE, 0);
1818     ASSERT_GL_NO_ERROR();
1819 
1820     glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER);
1821     ASSERT_GL_NO_ERROR();
1822 }
1823 
1824 // Verify persistently mapped buffers can use glBufferSubData
TEST_P(BufferStorageTestES3,StorageBufferSubDataMapped)1825 TEST_P(BufferStorageTestES3, StorageBufferSubDataMapped)
1826 {
1827     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1828                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1829 
1830     const std::array<GLColor, 4> kUpdateData1 = {GLColor::red, GLColor::green, GLColor::blue,
1831                                                  GLColor::yellow};
1832 
1833     // Set up the buffer to be persistently mapped and dynamic
1834     GLBuffer buffer;
1835     glBindBuffer(GL_ARRAY_BUFFER, buffer);
1836     glBufferStorageEXT(GL_ARRAY_BUFFER, 16, nullptr,
1837                        GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1838                            GL_MAP_COHERENT_BIT_EXT | GL_DYNAMIC_STORAGE_BIT_EXT);
1839     void *readMapPtr = glMapBufferRange(
1840         GL_ARRAY_BUFFER, 0, 16,
1841         GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1842     ASSERT_NE(nullptr, readMapPtr);
1843     ASSERT_GL_NO_ERROR();
1844 
1845     // Verify we can push new data into the buffer
1846     glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLColor) * kUpdateData1.size(), kUpdateData1.data());
1847     ASSERT_GL_NO_ERROR();
1848 
1849     // Flush the buffer.
1850     glFinish();
1851 
1852     // Check the contents
1853     std::array<GLColor, 4> persistentData1;
1854     memcpy(persistentData1.data(), readMapPtr, persistentData1.size() * sizeof(GLColor));
1855     EXPECT_EQ(kUpdateData1, persistentData1);
1856     glUnmapBuffer(GL_ARRAY_BUFFER);
1857     ASSERT_GL_NO_ERROR();
1858 }
1859 
1860 // Verify that persistently mapped coherent buffers can be used as uniform buffers,
1861 // and written to by using the pointer from glMapBufferRange.
TEST_P(BufferStorageTestES3,UniformBufferMapped)1862 TEST_P(BufferStorageTestES3, UniformBufferMapped)
1863 {
1864     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1865                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1866 
1867     const char *mkFS = R"(#version 300 es
1868 precision highp float;
1869 uniform uni { vec4 color; };
1870 out vec4 fragColor;
1871 void main()
1872 {
1873     fragColor = color;
1874 })";
1875 
1876     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), mkFS);
1877     ASSERT_NE(program, 0u);
1878 
1879     GLint uniformBufferIndex = glGetUniformBlockIndex(program, "uni");
1880     ASSERT_NE(uniformBufferIndex, -1);
1881 
1882     GLBuffer uniformBuffer;
1883 
1884     ASSERT_GL_NO_ERROR();
1885 
1886     glViewport(0, 0, getWindowWidth(), getWindowHeight());
1887     glClear(GL_COLOR_BUFFER_BIT);
1888 
1889     glBindBuffer(GL_UNIFORM_BUFFER, uniformBuffer);
1890 
1891     glBufferStorageEXT(GL_UNIFORM_BUFFER, sizeof(float) * 4, nullptr,
1892                        GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1893 
1894     float *mapPtr = static_cast<float *>(
1895         glMapBufferRange(GL_UNIFORM_BUFFER, 0, sizeof(float) * 4,
1896                          GL_MAP_WRITE_BIT | GL_MAP_UNSYNCHRONIZED_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1897                              GL_MAP_COHERENT_BIT_EXT));
1898 
1899     ASSERT_NE(mapPtr, nullptr);
1900 
1901     glBindBufferBase(GL_UNIFORM_BUFFER, 0, uniformBuffer);
1902 
1903     glUniformBlockBinding(program, uniformBufferIndex, 0);
1904 
1905     mapPtr[0] = 0.5f;
1906     mapPtr[1] = 0.75f;
1907     mapPtr[2] = 0.25f;
1908     mapPtr[3] = 1.0f;
1909 
1910     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5f);
1911 
1912     EXPECT_PIXEL_NEAR(0, 0, 128, 191, 64, 255, 1);
1913 
1914     glUnmapBuffer(GL_UNIFORM_BUFFER);
1915 
1916     glDeleteProgram(program);
1917 
1918     ASSERT_GL_NO_ERROR();
1919 }
1920 
1921 // Verify that persistently mapped coherent buffers can be used as vertex array buffers,
1922 // and written to by using the pointer from glMapBufferRange.
TEST_P(BufferStorageTestES3,VertexBufferMapped)1923 TEST_P(BufferStorageTestES3, VertexBufferMapped)
1924 {
1925     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
1926                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
1927 
1928     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), essl3_shaders::fs::Red());
1929     ASSERT_NE(program, 0u);
1930 
1931     glUseProgram(program);
1932 
1933     auto quadVertices = GetQuadVertices();
1934 
1935     size_t bufferSize = sizeof(GLfloat) * quadVertices.size() * 3;
1936 
1937     GLBuffer positionBuffer;
1938     glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
1939 
1940     glBufferStorageEXT(GL_ARRAY_BUFFER, bufferSize, nullptr,
1941                        GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
1942                            GL_MAP_COHERENT_BIT_EXT);
1943 
1944     GLint positionLocation = glGetAttribLocation(program, essl3_shaders::PositionAttrib());
1945     ASSERT_NE(-1, positionLocation);
1946     glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
1947     glEnableVertexAttribArray(positionLocation);
1948 
1949     void *mappedPtr =
1950         glMapBufferRange(GL_ARRAY_BUFFER, 0, bufferSize,
1951                          GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1952     ASSERT_NE(nullptr, mappedPtr);
1953 
1954     memcpy(mappedPtr, reinterpret_cast<void *>(quadVertices.data()), bufferSize);
1955 
1956     glDrawArrays(GL_TRIANGLES, 0, quadVertices.size());
1957     EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::red);
1958 
1959     glUnmapBuffer(GL_ARRAY_BUFFER);
1960     glDeleteProgram(program);
1961 
1962     EXPECT_GL_NO_ERROR();
1963 }
1964 
TestPageSharingBuffers(std::function<void (void)> swapCallback,size_t bufferSize,const std::array<Vector3,6> & quadVertices,GLint positionLocation)1965 void TestPageSharingBuffers(std::function<void(void)> swapCallback,
1966                             size_t bufferSize,
1967                             const std::array<Vector3, 6> &quadVertices,
1968                             GLint positionLocation)
1969 {
1970     size_t dataSize = sizeof(GLfloat) * quadVertices.size() * 3;
1971 
1972     if (bufferSize == 0)
1973     {
1974         bufferSize = dataSize;
1975     }
1976 
1977     constexpr size_t bufferCount = 10;
1978 
1979     std::vector<GLBuffer> buffers(bufferCount);
1980     std::vector<void *> mapPointers(bufferCount);
1981 
1982     // Init and map
1983     for (uint32_t i = 0; i < bufferCount; i++)
1984     {
1985         glBindBuffer(GL_ARRAY_BUFFER, buffers[i]);
1986 
1987         glBufferStorageEXT(GL_ARRAY_BUFFER, bufferSize, nullptr,
1988                            GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT |
1989                                GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1990 
1991         glEnableVertexAttribArray(positionLocation);
1992 
1993         mapPointers[i] = glMapBufferRange(
1994             GL_ARRAY_BUFFER, 0, bufferSize,
1995             GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
1996         ASSERT_NE(nullptr, mapPointers[i]);
1997     }
1998 
1999     // Write, draw and unmap
2000     for (uint32_t i = 0; i < bufferCount; i++)
2001     {
2002         memcpy(mapPointers[i], reinterpret_cast<const void *>(quadVertices.data()), dataSize);
2003 
2004         // Write something to last float
2005         if (bufferSize > dataSize + sizeof(GLfloat))
2006         {
2007             size_t lastPosition = bufferSize / sizeof(GLfloat) - 1;
2008             reinterpret_cast<float *>(mapPointers[i])[lastPosition] = 1.0f;
2009         }
2010 
2011         glBindBuffer(GL_ARRAY_BUFFER, buffers[i]);
2012         glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
2013         glDrawArrays(GL_TRIANGLES, 0, quadVertices.size());
2014         EXPECT_PIXEL_COLOR_EQ(8, 8, GLColor::red);
2015         swapCallback();
2016         glUnmapBuffer(GL_ARRAY_BUFFER);
2017     }
2018 }
2019 
2020 // Create multiple persistently mapped coherent buffers of different sizes that will likely share a
2021 // page. Map all buffers together and unmap each buffer after writing to it and using it for a draw.
2022 // This tests the behaviour of the coherent buffer tracker in frame capture when buffers that share
2023 // a page are written to after the other one is removed.
TEST_P(BufferStorageTestES3,PageSharingBuffers)2024 TEST_P(BufferStorageTestES3, PageSharingBuffers)
2025 {
2026     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
2027                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
2028 
2029     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), essl3_shaders::fs::Red());
2030     ASSERT_NE(program, 0u);
2031 
2032     glUseProgram(program);
2033 
2034     auto quadVertices = GetQuadVertices();
2035 
2036     std::function<void(void)> swapCallback = [this]() { swapBuffers(); };
2037 
2038     GLint positionLocation = glGetAttribLocation(program, essl3_shaders::PositionAttrib());
2039     ASSERT_NE(-1, positionLocation);
2040 
2041     TestPageSharingBuffers(swapCallback, 0, quadVertices, positionLocation);
2042     TestPageSharingBuffers(swapCallback, 1000, quadVertices, positionLocation);
2043     TestPageSharingBuffers(swapCallback, 4096, quadVertices, positionLocation);
2044     TestPageSharingBuffers(swapCallback, 6144, quadVertices, positionLocation);
2045     TestPageSharingBuffers(swapCallback, 40960, quadVertices, positionLocation);
2046 
2047     glDeleteProgram(program);
2048 
2049     EXPECT_GL_NO_ERROR();
2050 }
2051 
2052 class BufferStorageTestES3Threaded : public ANGLETest<>
2053 {
2054   protected:
BufferStorageTestES3Threaded()2055     BufferStorageTestES3Threaded()
2056     {
2057         setWindowWidth(16);
2058         setWindowHeight(16);
2059         setConfigRedBits(8);
2060         setConfigGreenBits(8);
2061         setConfigBlueBits(8);
2062         setConfigAlphaBits(8);
2063         setConfigDepthBits(24);
2064 
2065         mProgram = 0;
2066     }
2067 
testSetUp()2068     void testSetUp() override
2069     {
2070         constexpr char kVS[] = R"(#version 300 es
2071 
2072 in vec4 position;
2073 in vec4 color;
2074 out vec4 out_color;
2075 
2076 void main()
2077 {
2078     out_color = color;
2079     gl_Position = position;
2080 })";
2081 
2082         constexpr char kFS[] = R"(#version 300 es
2083 precision highp float;
2084 
2085 in vec4 out_color;
2086 out vec4 fragColor;
2087 
2088 void main()
2089 {
2090     fragColor = vec4(out_color);
2091 })";
2092 
2093         mProgram = CompileProgram(kVS, kFS);
2094         ASSERT_NE(mProgram, 0U);
2095 
2096         glClearColor(0, 0, 0, 0);
2097         glClearDepthf(0.0);
2098         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
2099 
2100         glDisable(GL_DEPTH_TEST);
2101 
2102         ASSERT_GL_NO_ERROR();
2103     }
2104 
testTearDown()2105     void testTearDown() override { glDeleteProgram(mProgram); }
2106 
updateColors(int i,int offset,const GLColor & color)2107     void updateColors(int i, int offset, const GLColor &color)
2108     {
2109         Vector4 colorVec               = color.toNormalizedVector();
2110         mMappedPtr[offset + i * 4 + 0] = colorVec.x();
2111         mMappedPtr[offset + i * 4 + 1] = colorVec.y();
2112         mMappedPtr[offset + i * 4 + 2] = colorVec.z();
2113         mMappedPtr[offset + i * 4 + 3] = colorVec.w();
2114     }
2115 
updateThreadedAndDraw(int offset,const GLColor & color)2116     void updateThreadedAndDraw(int offset, const GLColor &color)
2117     {
2118         std::mutex mutex;
2119         std::vector<std::thread> threads(4);
2120         for (size_t i = 0; i < 4; i++)
2121         {
2122             threads[i] = std::thread([&, i]() {
2123                 std::lock_guard<decltype(mutex)> lock(mutex);
2124                 updateColors(i, offset, color);
2125             });
2126         }
2127 
2128         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
2129 
2130         for (std::thread &thread : threads)
2131         {
2132             thread.join();
2133         }
2134     }
2135 
2136     GLuint mProgram;
2137     GLfloat *mMappedPtr = nullptr;
2138 };
2139 
2140 // Test using a large buffer storage for a color vertex array buffer, which is
2141 // off set every iteration step via glVertexAttribPointer.
2142 // Write to the buffer storage map pointer from multiple threads for the next iteration,
2143 // while drawing the current one.
TEST_P(BufferStorageTestES3Threaded,VertexBuffer)2144 TEST_P(BufferStorageTestES3Threaded, VertexBuffer)
2145 {
2146     ANGLE_SKIP_TEST_IF(getClientMajorVersion() < 3 ||
2147                        !IsGLExtensionEnabled("GL_EXT_buffer_storage"));
2148 
2149     auto vertices = GetIndexedQuadVertices();
2150 
2151     // Set up position buffer
2152     GLBuffer positionBuffer;
2153     glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
2154     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(),
2155                  GL_STATIC_DRAW);
2156 
2157     GLint positionLoc = glGetAttribLocation(mProgram, "position");
2158     glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), nullptr);
2159     glEnableVertexAttribArray(positionLoc);
2160 
2161     // Set up color buffer
2162     GLBuffer colorBuffer;
2163     glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
2164 
2165     // Let's create a big buffer which fills 10 pages at pagesize 4096
2166     GLint bufferSize   = sizeof(GLfloat) * 1024 * 10;
2167     GLint offsetFloats = 0;
2168 
2169     glBufferStorageEXT(GL_ARRAY_BUFFER, bufferSize, nullptr,
2170                        GL_DYNAMIC_STORAGE_BIT_EXT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT |
2171                            GL_MAP_COHERENT_BIT_EXT);
2172     GLint colorLoc = glGetAttribLocation(mProgram, "color");
2173     glEnableVertexAttribArray(colorLoc);
2174 
2175     auto indices = GetQuadIndices();
2176 
2177     GLBuffer indexBuffer;
2178     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
2179     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * indices.size(), indices.data(),
2180                  GL_STATIC_DRAW);
2181 
2182     glUseProgram(mProgram);
2183 
2184     glClearColor(0, 0, 0, 0);
2185     glClear(GL_COLOR_BUFFER_BIT);
2186 
2187     ASSERT_GL_NO_ERROR();
2188 
2189     mMappedPtr = (GLfloat *)glMapBufferRange(
2190         GL_ARRAY_BUFFER, 0, bufferSize,
2191         GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT | GL_MAP_COHERENT_BIT_EXT);
2192     ASSERT_NE(nullptr, mMappedPtr);
2193     ASSERT_GL_NO_ERROR();
2194 
2195     // Initial color
2196     for (int i = 0; i < 4; i++)
2197     {
2198         updateColors(i, offsetFloats, GLColor::black);
2199     }
2200 
2201     std::vector<GLColor> colors = {GLColor::red, GLColor::green, GLColor::blue};
2202 
2203     // 4 vertices, 4 floats
2204     GLint contentSize = 4 * 4;
2205 
2206     // Update and draw last
2207     int i = 0;
2208     while (bufferSize > (int)((offsetFloats + contentSize) * sizeof(GLfloat)))
2209     {
2210         glVertexAttribPointer(colorLoc, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
2211                               reinterpret_cast<const GLvoid *>(offsetFloats * sizeof(GLfloat)));
2212 
2213         offsetFloats += contentSize;
2214         GLColor color = colors[i % colors.size()];
2215         updateThreadedAndDraw(offsetFloats, color);
2216 
2217         if (i > 0)
2218         {
2219             GLColor lastColor = colors[(i - 1) % colors.size()];
2220             EXPECT_PIXEL_COLOR_EQ(0, 0, lastColor);
2221         }
2222         ASSERT_GL_NO_ERROR();
2223         i++;
2224     }
2225 
2226     // Last draw
2227     glVertexAttribPointer(colorLoc, 3, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat),
2228                           reinterpret_cast<const GLvoid *>(offsetFloats * sizeof(GLfloat)));
2229     glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
2230 
2231     glUnmapBuffer(GL_ARRAY_BUFFER);
2232 
2233     ASSERT_GL_NO_ERROR();
2234 }
2235 
2236 // Test that buffer self-copy works when buffer is used as UBO
TEST_P(BufferDataTestES3,CopyBufferSubDataSelfDependency)2237 TEST_P(BufferDataTestES3, CopyBufferSubDataSelfDependency)
2238 {
2239     constexpr char kFS[] = R"(#version 300 es
2240 precision mediump float;
2241 out vec4 color;
2242 
2243 uniform UBO
2244 {
2245     vec4 data[128];
2246 };
2247 
2248 void main()
2249 {
2250     color = data[12];
2251 })";
2252 
2253     ANGLE_GL_PROGRAM(program, essl3_shaders::vs::Simple(), kFS);
2254     glUseProgram(program);
2255 
2256     constexpr uint32_t kVec4Size   = 4 * sizeof(float);
2257     constexpr uint32_t kUBOSize    = 128 * kVec4Size;
2258     constexpr uint32_t kDataOffset = 12 * kVec4Size;
2259 
2260     // Init data is 4 times the size of UBO as the buffer is created larger than the UBO throughout
2261     // the test.
2262     const std::vector<float> kInitData(kUBOSize, 123.45);
2263 
2264     // Set up a throw-away buffer just to make buffer suballocations not use offset 0.
2265     GLBuffer throwaway;
2266     glBindBuffer(GL_UNIFORM_BUFFER, throwaway);
2267     glBufferData(GL_UNIFORM_BUFFER, 1024, nullptr, GL_DYNAMIC_DRAW);
2268 
2269     // Set up the buffer
2270     GLBuffer buffer;
2271     glBindBuffer(GL_UNIFORM_BUFFER, buffer);
2272     glBufferData(GL_UNIFORM_BUFFER, kUBOSize * 2, kInitData.data(), GL_DYNAMIC_DRAW);
2273 
2274     const std::vector<float> kColorData = {
2275         0.75,
2276         0.5,
2277         0.25,
2278         1.0,
2279     };
2280     glBufferSubData(GL_UNIFORM_BUFFER, kDataOffset, kVec4Size, kColorData.data());
2281 
2282     glClearColor(0, 0, 0, 1);
2283     glClear(GL_COLOR_BUFFER_BIT);
2284     const int w = getWindowWidth();
2285     const int h = getWindowHeight();
2286 
2287     // Use the buffer, then do a big self-copy
2288     glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, 0, kUBOSize);
2289     glScissor(0, 0, w / 2, h / 2);
2290     glEnable(GL_SCISSOR_TEST);
2291     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2292 
2293     // Duplicate the buffer in the second half
2294     glCopyBufferSubData(GL_UNIFORM_BUFFER, GL_UNIFORM_BUFFER, 0, kUBOSize, kUBOSize);
2295 
2296     // Draw again, making sure the copy succeeded.
2297     glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, kUBOSize, kUBOSize);
2298     glScissor(w / 2, 0, w / 2, h / 2);
2299     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2300 
2301     // Do a small self-copy
2302     constexpr uint32_t kCopySrcOffset = 4 * kVec4Size;
2303     constexpr uint32_t kCopyDstOffset = (64 + 4) * kVec4Size;
2304     glCopyBufferSubData(GL_UNIFORM_BUFFER, GL_UNIFORM_BUFFER, kCopySrcOffset, kCopyDstOffset,
2305                         kDataOffset);
2306 
2307     // color data was previously at [12], and is now available at [68 + 12 - 4]
2308     glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, kCopyDstOffset - kCopySrcOffset, kUBOSize);
2309     glScissor(0, h / 2, w / 2, h / 2);
2310     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2311 
2312     // Validate results
2313     EXPECT_PIXEL_NEAR(0, 0, 191, 127, 63, 255, 1);
2314     EXPECT_PIXEL_NEAR(w / 2 + 1, 0, 191, 127, 63, 255, 1);
2315     EXPECT_PIXEL_NEAR(0, h / 2 + 1, 191, 127, 63, 255, 1);
2316     EXPECT_PIXEL_COLOR_EQ(w / 2 + 1, h / 2 + 1, GLColor::black);
2317 
2318     // Do a big copy again, but this time the buffer is unused by the GPU
2319     glCopyBufferSubData(GL_UNIFORM_BUFFER, GL_UNIFORM_BUFFER, kUBOSize, 0, kUBOSize);
2320     glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, 0, kUBOSize);
2321     glScissor(w / 2, h / 2, w / 2, h / 2);
2322     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2323     EXPECT_PIXEL_NEAR(w / 2 + 1, h / 2 + 1, 191, 127, 63, 255, 1);
2324 
2325     // Do a small copy again, but this time the buffer is unused by the GPU
2326     glCopyBufferSubData(GL_UNIFORM_BUFFER, GL_UNIFORM_BUFFER, kUBOSize + kCopySrcOffset,
2327                         kCopyDstOffset, kDataOffset);
2328     glBindBufferRange(GL_UNIFORM_BUFFER, 0, buffer, kCopyDstOffset - kCopySrcOffset, kUBOSize);
2329     glDisable(GL_SCISSOR_TEST);
2330     glClear(GL_COLOR_BUFFER_BIT);
2331     drawQuad(program, essl3_shaders::PositionAttrib(), 0.5);
2332     EXPECT_PIXEL_NEAR(0, 0, 191, 127, 63, 255, 1);
2333 
2334     ASSERT_GL_NO_ERROR();
2335 }
2336 
2337 ANGLE_INSTANTIATE_TEST_ES2(BufferDataTest);
2338 
2339 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BufferSubDataTest);
2340 ANGLE_INSTANTIATE_TEST_COMBINE_1(BufferSubDataTest,
2341                                  BufferSubDataTestPrint,
2342                                  testing::Bool(),
2343                                  ANGLE_ALL_TEST_PLATFORMS_ES3,
2344                                  ES3_VULKAN().enable(Feature::PreferCPUForBufferSubData));
2345 
2346 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BufferDataTestES3);
2347 ANGLE_INSTANTIATE_TEST_ES3_AND(BufferDataTestES3,
2348                                ES3_VULKAN().enable(Feature::PreferCPUForBufferSubData),
2349                                ES3_METAL().enable(Feature::ForceBufferGPUStorage));
2350 
2351 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BufferStorageTestES3);
2352 ANGLE_INSTANTIATE_TEST_ES3_AND(BufferStorageTestES3,
2353                                ES3_VULKAN().enable(Feature::AllocateNonZeroMemory));
2354 
2355 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(IndexedBufferCopyTest);
2356 ANGLE_INSTANTIATE_TEST_ES3(IndexedBufferCopyTest);
2357 
2358 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BufferStorageTestES3Threaded);
2359 ANGLE_INSTANTIATE_TEST_ES3(BufferStorageTestES3Threaded);
2360 
2361 #ifdef _WIN64
2362 
2363 // Test a bug where an integer overflow bug could trigger a crash in D3D.
2364 // The test uses 8 buffers with a size just under 0x2000000 to overflow max uint
2365 // (with the internal D3D rounding to 16-byte values) and trigger the bug.
2366 // Only handle this bug on 64-bit Windows for now. Harder to repro on 32-bit.
2367 class BufferDataOverflowTest : public ANGLETest<>
2368 {
2369   protected:
BufferDataOverflowTest()2370     BufferDataOverflowTest() {}
2371 };
2372 
2373 // See description above.
TEST_P(BufferDataOverflowTest,VertexBufferIntegerOverflow)2374 TEST_P(BufferDataOverflowTest, VertexBufferIntegerOverflow)
2375 {
2376     // These values are special, to trigger the rounding bug.
2377     unsigned int numItems       = 0x7FFFFFE;
2378     constexpr GLsizei bufferCnt = 8;
2379 
2380     std::vector<GLBuffer> buffers(bufferCnt);
2381 
2382     std::stringstream vertexShaderStr;
2383 
2384     for (GLsizei bufferIndex = 0; bufferIndex < bufferCnt; ++bufferIndex)
2385     {
2386         vertexShaderStr << "attribute float attrib" << bufferIndex << ";\n";
2387     }
2388 
2389     vertexShaderStr << "attribute vec2 position;\n"
2390                        "varying float v_attrib;\n"
2391                        "void main() {\n"
2392                        "  gl_Position = vec4(position, 0, 1);\n"
2393                        "  v_attrib = 0.0;\n";
2394 
2395     for (GLsizei bufferIndex = 0; bufferIndex < bufferCnt; ++bufferIndex)
2396     {
2397         vertexShaderStr << "v_attrib += attrib" << bufferIndex << ";\n";
2398     }
2399 
2400     vertexShaderStr << "}";
2401 
2402     constexpr char kFS[] =
2403         "varying highp float v_attrib;\n"
2404         "void main() {\n"
2405         "  gl_FragColor = vec4(v_attrib, 0, 0, 1);\n"
2406         "}";
2407 
2408     ANGLE_GL_PROGRAM(program, vertexShaderStr.str().c_str(), kFS);
2409     glUseProgram(program);
2410 
2411     std::vector<GLfloat> data(numItems, 1.0f);
2412 
2413     for (GLsizei bufferIndex = 0; bufferIndex < bufferCnt; ++bufferIndex)
2414     {
2415         glBindBuffer(GL_ARRAY_BUFFER, buffers[bufferIndex]);
2416         glBufferData(GL_ARRAY_BUFFER, numItems * sizeof(float), &data[0], GL_DYNAMIC_DRAW);
2417 
2418         std::stringstream attribNameStr;
2419         attribNameStr << "attrib" << bufferIndex;
2420 
2421         GLint attribLocation = glGetAttribLocation(program, attribNameStr.str().c_str());
2422         ASSERT_NE(-1, attribLocation);
2423 
2424         glVertexAttribPointer(attribLocation, 1, GL_FLOAT, GL_FALSE, 4, nullptr);
2425         glEnableVertexAttribArray(attribLocation);
2426     }
2427 
2428     GLint positionLocation = glGetAttribLocation(program, "position");
2429     ASSERT_NE(-1, positionLocation);
2430     glDisableVertexAttribArray(positionLocation);
2431     glVertexAttrib2f(positionLocation, 1.0f, 1.0f);
2432 
2433     EXPECT_GL_NO_ERROR();
2434     glDrawArrays(GL_TRIANGLES, 0, numItems);
2435     EXPECT_GL_ERROR(GL_OUT_OF_MEMORY);
2436 
2437     // Test that a small draw still works.
2438     for (GLsizei bufferIndex = 0; bufferIndex < bufferCnt; ++bufferIndex)
2439     {
2440         std::stringstream attribNameStr;
2441         attribNameStr << "attrib" << bufferIndex;
2442         GLint attribLocation = glGetAttribLocation(program, attribNameStr.str().c_str());
2443         ASSERT_NE(-1, attribLocation);
2444         glDisableVertexAttribArray(attribLocation);
2445     }
2446 
2447     glDrawArrays(GL_TRIANGLES, 0, 3);
2448     EXPECT_GL_ERROR(GL_NO_ERROR);
2449 }
2450 
2451 // Tests a security bug in our CopyBufferSubData validation (integer overflow).
TEST_P(BufferDataOverflowTest,CopySubDataValidation)2452 TEST_P(BufferDataOverflowTest, CopySubDataValidation)
2453 {
2454     GLBuffer readBuffer, writeBuffer;
2455 
2456     glBindBuffer(GL_COPY_READ_BUFFER, readBuffer);
2457     glBindBuffer(GL_COPY_WRITE_BUFFER, writeBuffer);
2458 
2459     constexpr int bufSize = 100;
2460 
2461     glBufferData(GL_COPY_READ_BUFFER, bufSize, nullptr, GL_STATIC_DRAW);
2462     glBufferData(GL_COPY_WRITE_BUFFER, bufSize, nullptr, GL_STATIC_DRAW);
2463 
2464     GLintptr big = std::numeric_limits<GLintptr>::max() - bufSize + 90;
2465 
2466     glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, big, 0, 50);
2467     EXPECT_GL_ERROR(GL_INVALID_VALUE);
2468 
2469     glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, 0, big, 50);
2470     EXPECT_GL_ERROR(GL_INVALID_VALUE);
2471 }
2472 
2473 ANGLE_INSTANTIATE_TEST_ES3(BufferDataOverflowTest);
2474 
2475 #endif  // _WIN64
2476