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