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 
9 using namespace angle;
10 
11 namespace
12 {
13 constexpr char kVS[] = R"(attribute vec4 aTest;
14 attribute vec2 aPosition;
15 varying vec4 vTest;
16 
17 void main()
18 {
19     vTest        = aTest;
20     gl_Position  = vec4(aPosition, 0.0, 1.0);
21     gl_PointSize = 1.0;
22 })";
23 
24 constexpr char kFS[] = R"(precision mediump float;
25 varying vec4 vTest;
26 void main()
27 {
28     gl_FragColor = vTest;
29 })";
30 }  // namespace
31 
32 class PBOExtensionTest : public ANGLETest
33 {
34   protected:
PBOExtensionTest()35     PBOExtensionTest()
36     {
37         setWindowWidth(32);
38         setWindowHeight(32);
39         setConfigRedBits(8);
40         setConfigGreenBits(8);
41         setConfigBlueBits(8);
42         setConfigAlphaBits(8);
43     }
44 
testSetUp()45     void testSetUp() override
46     {
47         if (IsGLExtensionEnabled("NV_pixel_buffer_object"))
48         {
49             glGenBuffers(1, &mPBO);
50             glBindBuffer(GL_PIXEL_PACK_BUFFER, mPBO);
51             glBufferData(GL_PIXEL_PACK_BUFFER, 4 * getWindowWidth() * getWindowHeight(), nullptr,
52                          GL_STATIC_DRAW);
53             glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
54 
55             mProgram = CompileProgram(kVS, kFS);
56 
57             glGenBuffers(1, &mPositionVBO);
58             glBindBuffer(GL_ARRAY_BUFFER, mPositionVBO);
59             glBufferData(GL_ARRAY_BUFFER, 128, nullptr, GL_DYNAMIC_DRAW);
60             glBindBuffer(GL_ARRAY_BUFFER, 0);
61         }
62         ASSERT_GL_NO_ERROR();
63     }
64 
testTearDown()65     void testTearDown() override
66     {
67         glDeleteBuffers(1, &mPBO);
68         glDeleteProgram(mProgram);
69     }
70 
71     GLuint mPBO;
72     GLuint mProgram;
73     GLuint mPositionVBO;
74 };
75 
TEST_P(PBOExtensionTest,PBOWithOtherTarget)76 TEST_P(PBOExtensionTest, PBOWithOtherTarget)
77 {
78     if (IsGLExtensionEnabled("NV_pixel_buffer_object"))
79     {
80         glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
81         glClear(GL_COLOR_BUFFER_BIT);
82         EXPECT_GL_NO_ERROR();
83 
84         glBindBuffer(GL_PIXEL_PACK_BUFFER, mPBO);
85         glReadPixels(0, 0, 16, 16, GL_RGBA, GL_UNSIGNED_BYTE, 0);
86 
87         glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
88         glBindBuffer(GL_ARRAY_BUFFER, mPBO);
89 
90         void *mappedPtr        = glMapBufferRangeEXT(GL_ARRAY_BUFFER, 0, 32, GL_MAP_READ_BIT);
91         unsigned char *dataPtr = static_cast<unsigned char *>(mappedPtr);
92         EXPECT_GL_NO_ERROR();
93 
94         EXPECT_EQ(255, dataPtr[0]);
95         EXPECT_EQ(0, dataPtr[1]);
96         EXPECT_EQ(0, dataPtr[2]);
97         EXPECT_EQ(255, dataPtr[3]);
98 
99         glUnmapBufferOES(GL_ARRAY_BUFFER);
100     }
101     EXPECT_GL_NO_ERROR();
102 }
103 
TEST_P(PBOExtensionTest,PBOWithExistingData)104 TEST_P(PBOExtensionTest, PBOWithExistingData)
105 {
106     if (IsGLExtensionEnabled("NV_pixel_buffer_object"))
107     {
108         // Clear backbuffer to red
109         glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
110         glClear(GL_COLOR_BUFFER_BIT);
111         EXPECT_GL_NO_ERROR();
112 
113         // Read 16x16 region from red backbuffer to PBO
114         glBindBuffer(GL_PIXEL_PACK_BUFFER, mPBO);
115         glReadPixels(0, 0, 16, 16, GL_RGBA, GL_UNSIGNED_BYTE, 0);
116 
117         // Clear backbuffer to green
118         glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
119         glClear(GL_COLOR_BUFFER_BIT);
120         EXPECT_GL_NO_ERROR();
121 
122         // Read 16x16 region from green backbuffer to PBO at offset 16
123         glReadPixels(0, 0, 16, 16, GL_RGBA, GL_UNSIGNED_BYTE, reinterpret_cast<void *>(16));
124         void *mappedPtr        = glMapBufferRangeEXT(GL_PIXEL_PACK_BUFFER, 0, 32, GL_MAP_READ_BIT);
125         unsigned char *dataPtr = static_cast<unsigned char *>(mappedPtr);
126         EXPECT_GL_NO_ERROR();
127 
128         // Test pixel 0 is red (existing data)
129         EXPECT_EQ(255, dataPtr[0]);
130         EXPECT_EQ(0, dataPtr[1]);
131         EXPECT_EQ(0, dataPtr[2]);
132         EXPECT_EQ(255, dataPtr[3]);
133 
134         // Test pixel 16 is green (new data)
135         EXPECT_EQ(0, dataPtr[16 * 4 + 0]);
136         EXPECT_EQ(255, dataPtr[16 * 4 + 1]);
137         EXPECT_EQ(0, dataPtr[16 * 4 + 2]);
138         EXPECT_EQ(255, dataPtr[16 * 4 + 3]);
139 
140         glUnmapBufferOES(GL_PIXEL_PACK_BUFFER);
141     }
142     EXPECT_GL_NO_ERROR();
143 }
144 
145 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
146 // tests should be run against.
147 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(PBOExtensionTest);
148