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