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