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 
14 class UnpackAlignmentTest : public ANGLETest
15 {
16   protected:
UnpackAlignmentTest()17     UnpackAlignmentTest()
18     {
19         setWindowWidth(128);
20         setWindowHeight(128);
21         setConfigRedBits(8);
22         setConfigGreenBits(8);
23         setConfigBlueBits(8);
24         setConfigAlphaBits(8);
25         setConfigDepthBits(24);
26 
27         mProgram = 0;
28     }
29 
testSetUp()30     void testSetUp() override
31     {
32         constexpr char kFS[] = R"(uniform sampler2D tex;
33 void main()
34 {
35     gl_FragColor = texture2D(tex, vec2(0.0, 1.0));
36 })";
37 
38         mProgram = CompileProgram(essl1_shaders::vs::Simple(), kFS);
39         if (mProgram == 0)
40         {
41             FAIL() << "shader compilation failed.";
42         }
43     }
44 
testTearDown()45     void testTearDown() override { glDeleteProgram(mProgram); }
46 
getPixelSize(GLenum format,GLenum type,unsigned int * size)47     void getPixelSize(GLenum format, GLenum type, unsigned int *size)
48     {
49         switch (type)
50         {
51             case GL_UNSIGNED_SHORT_5_5_5_1:
52             case GL_UNSIGNED_SHORT_5_6_5:
53             case GL_UNSIGNED_SHORT_4_4_4_4:
54                 *size = sizeof(GLushort);
55                 break;
56 
57             case GL_UNSIGNED_BYTE:
58             {
59                 unsigned int compCount = 0;
60                 switch (format)
61                 {
62                     case GL_RGBA:
63                         compCount = 4;
64                         break;
65                     case GL_RGB:
66                         compCount = 3;
67                         break;
68                     case GL_LUMINANCE_ALPHA:
69                         compCount = 2;
70                         break;
71                     case GL_LUMINANCE:
72                         compCount = 1;
73                         break;
74                     case GL_ALPHA:
75                         compCount = 1;
76                         break;
77                     default:
78                         FAIL() << "unknown pixel format.";
79                 }
80                 *size = sizeof(GLubyte) * compCount;
81             }
82             break;
83             default:
84                 FAIL() << "unknown pixel type.";
85         }
86     }
87 
formatHasRGB(GLenum format)88     bool formatHasRGB(GLenum format) { return (format != GL_ALPHA); }
89 
testAlignment(int alignment,unsigned int offset,GLenum format,GLenum type)90     void testAlignment(int alignment, unsigned int offset, GLenum format, GLenum type)
91     {
92         static const unsigned int width  = 7;
93         static const unsigned int height = 2;
94 
95         glPixelStorei(GL_UNPACK_ALIGNMENT, alignment);
96 
97         GLint readbackAlignment;
98         glGetIntegerv(GL_UNPACK_ALIGNMENT, &readbackAlignment);
99         EXPECT_EQ(alignment, readbackAlignment);
100 
101         GLubyte buf[1024];
102         memset(buf, 0, sizeof(buf));
103 
104         unsigned int pixelSize;
105         getPixelSize(format, type, &pixelSize);
106         for (unsigned int i = 0; i < pixelSize; i++)
107         {
108             buf[offset + i] = 0xFF;
109         }
110 
111         GLuint tex;
112         glGenTextures(1, &tex);
113         glBindTexture(GL_TEXTURE_2D, tex);
114 
115         glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, type, &buf[0]);
116         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
117         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
118         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
119 
120         drawQuad(mProgram, essl1_shaders::PositionAttrib(), 0.5f);
121 
122         GLubyte expectedRGB = formatHasRGB(format) ? 255 : 0;
123         EXPECT_PIXEL_EQ(0, 0, expectedRGB, expectedRGB, expectedRGB, 255);
124 
125         glDeleteTextures(1, &tex);
126     }
127 
128     GLuint mProgram;
129 };
130 
TEST_P(UnpackAlignmentTest,DefaultAlignment)131 TEST_P(UnpackAlignmentTest, DefaultAlignment)
132 {
133     GLint defaultAlignment;
134     glGetIntegerv(GL_UNPACK_ALIGNMENT, &defaultAlignment);
135     EXPECT_EQ(defaultAlignment, 4);
136 }
137 
TEST_P(UnpackAlignmentTest,Alignment1RGBAUByte)138 TEST_P(UnpackAlignmentTest, Alignment1RGBAUByte)
139 {
140     testAlignment(1, 7 * 4, GL_RGBA, GL_UNSIGNED_BYTE);
141 }
142 
TEST_P(UnpackAlignmentTest,Alignment1RGBUByte)143 TEST_P(UnpackAlignmentTest, Alignment1RGBUByte)
144 {
145     testAlignment(1, 7 * 3, GL_RGB, GL_UNSIGNED_BYTE);
146 }
147 
TEST_P(UnpackAlignmentTest,Alignment1RGBAUShort4444)148 TEST_P(UnpackAlignmentTest, Alignment1RGBAUShort4444)
149 {
150     testAlignment(1, 7 * 2, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4);
151 }
152 
TEST_P(UnpackAlignmentTest,Alignment1RGBAUShort5551)153 TEST_P(UnpackAlignmentTest, Alignment1RGBAUShort5551)
154 {
155     testAlignment(1, 7 * 2, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1);
156 }
157 
TEST_P(UnpackAlignmentTest,Alignment1RGBAUShort565)158 TEST_P(UnpackAlignmentTest, Alignment1RGBAUShort565)
159 {
160     testAlignment(1, 7 * 2, GL_RGB, GL_UNSIGNED_SHORT_5_6_5);
161 }
162 
TEST_P(UnpackAlignmentTest,Alignment1LAUByte)163 TEST_P(UnpackAlignmentTest, Alignment1LAUByte)
164 {
165     testAlignment(1, 7 * 2, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE);
166 }
167 
TEST_P(UnpackAlignmentTest,Alignment1LUByte)168 TEST_P(UnpackAlignmentTest, Alignment1LUByte)
169 {
170     testAlignment(1, 7, GL_LUMINANCE, GL_UNSIGNED_BYTE);
171 }
172 
TEST_P(UnpackAlignmentTest,Alignment1AUByte)173 TEST_P(UnpackAlignmentTest, Alignment1AUByte)
174 {
175     testAlignment(1, 7, GL_ALPHA, GL_UNSIGNED_BYTE);
176 }
177 
TEST_P(UnpackAlignmentTest,Alignment2RGBAUByte)178 TEST_P(UnpackAlignmentTest, Alignment2RGBAUByte)
179 {
180     testAlignment(2, 7 * 4, GL_RGBA, GL_UNSIGNED_BYTE);
181 }
182 
TEST_P(UnpackAlignmentTest,Alignment2RGBUByte)183 TEST_P(UnpackAlignmentTest, Alignment2RGBUByte)
184 {
185     testAlignment(2, 7 * 3 + 1, GL_RGB, GL_UNSIGNED_BYTE);
186 }
187 
TEST_P(UnpackAlignmentTest,Alignment2RGBAUShort4444)188 TEST_P(UnpackAlignmentTest, Alignment2RGBAUShort4444)
189 {
190     testAlignment(2, 7 * 2, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4);
191 }
192 
TEST_P(UnpackAlignmentTest,Alignment2RGBAUShort5551)193 TEST_P(UnpackAlignmentTest, Alignment2RGBAUShort5551)
194 {
195     testAlignment(2, 7 * 2, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1);
196 }
197 
TEST_P(UnpackAlignmentTest,Alignment2RGBAUShort565)198 TEST_P(UnpackAlignmentTest, Alignment2RGBAUShort565)
199 {
200     testAlignment(2, 7 * 2, GL_RGB, GL_UNSIGNED_SHORT_5_6_5);
201 }
202 
TEST_P(UnpackAlignmentTest,Alignment2LAUByte)203 TEST_P(UnpackAlignmentTest, Alignment2LAUByte)
204 {
205     testAlignment(2, 7 * 2, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE);
206 }
207 
TEST_P(UnpackAlignmentTest,Alignment2LAByte)208 TEST_P(UnpackAlignmentTest, Alignment2LAByte)
209 {
210     testAlignment(2, 7 + 1, GL_LUMINANCE, GL_UNSIGNED_BYTE);
211 }
212 
TEST_P(UnpackAlignmentTest,Alignment2AUByte)213 TEST_P(UnpackAlignmentTest, Alignment2AUByte)
214 {
215     testAlignment(2, 7 + 1, GL_ALPHA, GL_UNSIGNED_BYTE);
216 }
217 
TEST_P(UnpackAlignmentTest,Alignment4RGBAUByte)218 TEST_P(UnpackAlignmentTest, Alignment4RGBAUByte)
219 {
220     testAlignment(4, 7 * 4, GL_RGBA, GL_UNSIGNED_BYTE);
221 }
222 
TEST_P(UnpackAlignmentTest,Alignment4RGBUByte)223 TEST_P(UnpackAlignmentTest, Alignment4RGBUByte)
224 {
225     testAlignment(4, 7 * 3 + 3, GL_RGB, GL_UNSIGNED_BYTE);
226 }
227 
TEST_P(UnpackAlignmentTest,Alignment4RGBAUShort4444)228 TEST_P(UnpackAlignmentTest, Alignment4RGBAUShort4444)
229 {
230     testAlignment(4, 7 * 2 + 2, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4);
231 }
232 
TEST_P(UnpackAlignmentTest,Alignment4RGBAUShort5551)233 TEST_P(UnpackAlignmentTest, Alignment4RGBAUShort5551)
234 {
235     testAlignment(4, 7 * 2 + 2, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1);
236 }
237 
TEST_P(UnpackAlignmentTest,Alignment4RGBAUShort565)238 TEST_P(UnpackAlignmentTest, Alignment4RGBAUShort565)
239 {
240     testAlignment(4, 7 * 2 + 2, GL_RGB, GL_UNSIGNED_SHORT_5_6_5);
241 }
242 
TEST_P(UnpackAlignmentTest,Alignment4LAUByte)243 TEST_P(UnpackAlignmentTest, Alignment4LAUByte)
244 {
245     testAlignment(4, 7 * 2 + 2, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE);
246 }
247 
TEST_P(UnpackAlignmentTest,Alignment4LUByte)248 TEST_P(UnpackAlignmentTest, Alignment4LUByte)
249 {
250     testAlignment(4, 7 + 1, GL_LUMINANCE, GL_UNSIGNED_BYTE);
251 }
252 
TEST_P(UnpackAlignmentTest,Alignment4AUByte)253 TEST_P(UnpackAlignmentTest, Alignment4AUByte)
254 {
255     testAlignment(4, 7 + 1, GL_ALPHA, GL_UNSIGNED_BYTE);
256 }
257 
TEST_P(UnpackAlignmentTest,Alignment8RGBAUByte)258 TEST_P(UnpackAlignmentTest, Alignment8RGBAUByte)
259 {
260     testAlignment(8, 7 * 4 + 4, GL_RGBA, GL_UNSIGNED_BYTE);
261 }
262 
TEST_P(UnpackAlignmentTest,Alignment8RGBUByte)263 TEST_P(UnpackAlignmentTest, Alignment8RGBUByte)
264 {
265     testAlignment(8, 7 * 3 + 3, GL_RGB, GL_UNSIGNED_BYTE);
266 }
267 
TEST_P(UnpackAlignmentTest,Alignment8RGBAUShort4444)268 TEST_P(UnpackAlignmentTest, Alignment8RGBAUShort4444)
269 {
270     testAlignment(8, 7 * 2 + 2, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4);
271 }
272 
TEST_P(UnpackAlignmentTest,Alignment8RGBAUShort5551)273 TEST_P(UnpackAlignmentTest, Alignment8RGBAUShort5551)
274 {
275     testAlignment(8, 7 * 2 + 2, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1);
276 }
277 
TEST_P(UnpackAlignmentTest,Alignment8RGBAUShort565)278 TEST_P(UnpackAlignmentTest, Alignment8RGBAUShort565)
279 {
280     testAlignment(8, 7 * 2 + 2, GL_RGB, GL_UNSIGNED_SHORT_5_6_5);
281 }
282 
TEST_P(UnpackAlignmentTest,Alignment8LAUByte)283 TEST_P(UnpackAlignmentTest, Alignment8LAUByte)
284 {
285     testAlignment(8, 7 * 2 + 2, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE);
286 }
287 
TEST_P(UnpackAlignmentTest,Alignment8LUByte)288 TEST_P(UnpackAlignmentTest, Alignment8LUByte)
289 {
290     testAlignment(8, 7 + 1, GL_LUMINANCE, GL_UNSIGNED_BYTE);
291 }
292 
TEST_P(UnpackAlignmentTest,Alignment8AUByte)293 TEST_P(UnpackAlignmentTest, Alignment8AUByte)
294 {
295     testAlignment(8, 7 + 1, GL_ALPHA, GL_UNSIGNED_BYTE);
296 }
297 
298 // Use this to select which configurations (e.g. which renderer, which GLES major version) these
299 // tests should be run against.
300 ANGLE_INSTANTIATE_TEST_ES2_AND_ES3(UnpackAlignmentTest);
301 
302 }  // namespace
303