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 UnpackRowLengthTest : public ANGLETest
15 {
16   protected:
UnpackRowLengthTest()17     UnpackRowLengthTest()
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 
testRowLength(int texSize,int rowLength)69     void testRowLength(int texSize, int rowLength)
70     {
71         glPixelStorei(GL_UNPACK_ROW_LENGTH, rowLength);
72 
73         if ((getClientMajorVersion() == 3) || extensionEnabled("GL_EXT_unpack_subimage"))
74         {
75             // Only texSize * texSize region is filled as WHITE, other parts are BLACK.
76             // If the UNPACK_ROW_LENGTH is implemented correctly, all texels inside this texture are WHITE.
77             std::vector<GLubyte> buf(rowLength * texSize * 4);
78             for (int y = 0; y < texSize; y++)
79             {
80                 std::vector<GLubyte>::iterator rowIter = buf.begin() + y * rowLength * 4;
81                 std::fill(rowIter, rowIter + texSize * 4, static_cast<GLubyte>(255u));
82                 std::fill(rowIter + texSize * 4, rowIter + rowLength * 4, static_cast<GLubyte>(0u));
83             }
84 
85             GLuint tex;
86             glGenTextures(1, &tex);
87             glBindTexture(GL_TEXTURE_2D, tex);
88 
89             glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSize, texSize, 0, GL_RGBA, GL_UNSIGNED_BYTE, &buf[0]);
90             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
92             glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
93 
94             drawQuad(mProgram, "position", 0.5f);
95 
96             EXPECT_PIXEL_EQ(0, 0, 255, 255, 255, 255);
97             EXPECT_PIXEL_EQ(1, 0, 255, 255, 255, 255);
98 
99             glDeleteTextures(1, &tex);
100         }
101         else
102         {
103             EXPECT_GL_ERROR(GL_INVALID_ENUM);
104         }
105     }
106 
107     GLuint mProgram;
108 };
109 
TEST_P(UnpackRowLengthTest,RowLength128)110 TEST_P(UnpackRowLengthTest, RowLength128)
111 {
112     testRowLength(128, 128);
113 }
114 
TEST_P(UnpackRowLengthTest,RowLength1024)115 TEST_P(UnpackRowLengthTest, RowLength1024)
116 {
117     testRowLength(128, 1024);
118 }
119 
120 // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
121 ANGLE_INSTANTIATE_TEST(UnpackRowLengthTest,
122                        ES3_D3D11(),
123                        ES2_D3D11(),
124                        ES2_D3D9(),
125                        ES2_OPENGL(),
126                        ES3_OPENGL(),
127                        ES2_OPENGLES(),
128                        ES3_OPENGLES());
129 
130 } // namespace
131