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 | |
Corentin Wallez | d3970de | 2015-05-14 11:07:48 -0400 | [diff] [blame] | 7 | #include "test_utils/ANGLETest.h" |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 8 | |
| 9 | #include <vector> |
| 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 | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 16 | class SwizzleTest : public ANGLETest |
| 17 | { |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 18 | protected: |
| 19 | SwizzleTest() |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 20 | { |
| 21 | setWindowWidth(128); |
| 22 | setWindowHeight(128); |
| 23 | setConfigRedBits(8); |
| 24 | setConfigGreenBits(8); |
| 25 | setConfigBlueBits(8); |
| 26 | setConfigAlphaBits(8); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 27 | |
| 28 | GLenum swizzles[] = |
| 29 | { |
| 30 | GL_RED, |
| 31 | GL_GREEN, |
| 32 | GL_BLUE, |
| 33 | GL_ALPHA, |
| 34 | GL_ZERO, |
| 35 | GL_ONE, |
| 36 | }; |
| 37 | |
| 38 | for (int r = 0; r < 6; r++) |
| 39 | { |
| 40 | for (int g = 0; g < 6; g++) |
| 41 | { |
| 42 | for (int b = 0; b < 6; b++) |
| 43 | { |
| 44 | for (int a = 0; a < 6; a++) |
| 45 | { |
| 46 | swizzlePermutation permutation; |
| 47 | permutation.swizzleRed = swizzles[r]; |
| 48 | permutation.swizzleGreen = swizzles[g]; |
| 49 | permutation.swizzleBlue = swizzles[b]; |
| 50 | permutation.swizzleAlpha = swizzles[a]; |
| 51 | mPermutations.push_back(permutation); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 58 | void SetUp() override |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 59 | { |
| 60 | ANGLETest::SetUp(); |
| 61 | |
| 62 | const std::string vertexShaderSource = SHADER_SOURCE |
| 63 | ( |
| 64 | precision highp float; |
| 65 | attribute vec4 position; |
| 66 | varying vec2 texcoord; |
| 67 | |
| 68 | void main() |
| 69 | { |
| 70 | gl_Position = position; |
| 71 | texcoord = (position.xy * 0.5) + 0.5; |
| 72 | } |
| 73 | ); |
| 74 | |
| 75 | const std::string fragmentShaderSource = SHADER_SOURCE |
| 76 | ( |
| 77 | precision highp float; |
| 78 | uniform sampler2D tex; |
| 79 | varying vec2 texcoord; |
| 80 | |
| 81 | void main() |
| 82 | { |
| 83 | gl_FragColor = texture2D(tex, texcoord); |
| 84 | } |
| 85 | ); |
| 86 | |
Jamie Madill | 5599c8f | 2014-08-26 13:16:39 -0400 | [diff] [blame] | 87 | mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 88 | if (mProgram == 0) |
| 89 | { |
| 90 | FAIL() << "shader compilation failed."; |
| 91 | } |
| 92 | |
| 93 | mTextureUniformLocation = glGetUniformLocation(mProgram, "tex"); |
| 94 | |
| 95 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
Jamie Madill | 1ea9aaa | 2015-10-07 11:13:55 -0400 | [diff] [blame] | 96 | ASSERT_GL_NO_ERROR(); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 99 | void TearDown() override |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 100 | { |
| 101 | glDeleteProgram(mProgram); |
| 102 | glDeleteTextures(1, &mTexture); |
| 103 | |
| 104 | ANGLETest::TearDown(); |
| 105 | } |
| 106 | |
| 107 | template <typename T> |
| 108 | void init2DTexture(GLenum internalFormat, GLenum dataFormat, GLenum dataType, const T* data) |
| 109 | { |
| 110 | glGenTextures(1, &mTexture); |
| 111 | glBindTexture(GL_TEXTURE_2D, mTexture); |
Geoff Lang | 53b8aec | 2015-08-24 10:33:25 -0400 | [diff] [blame] | 112 | glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, 1, 1, 0, dataFormat, dataType, data); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 113 | |
| 114 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 115 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 116 | } |
| 117 | |
| 118 | void init2DCompressedTexture(GLenum internalFormat, GLsizei width, GLsizei height, GLsizei dataSize, const GLubyte* data) |
| 119 | { |
| 120 | glGenTextures(1, &mTexture); |
| 121 | glBindTexture(GL_TEXTURE_2D, mTexture); |
| 122 | glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0, dataSize, data); |
| 123 | |
| 124 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 125 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 126 | } |
| 127 | |
| 128 | GLubyte getExpectedValue(GLenum swizzle, GLubyte unswizzled[4]) |
| 129 | { |
| 130 | switch (swizzle) |
| 131 | { |
| 132 | case GL_RED: return unswizzled[0]; |
| 133 | case GL_GREEN: return unswizzled[1]; |
| 134 | case GL_BLUE: return unswizzled[2]; |
| 135 | case GL_ALPHA: return unswizzled[3]; |
| 136 | case GL_ZERO: return 0; |
| 137 | case GL_ONE: return 255; |
| 138 | default: return 0; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | void runTest2D() |
| 143 | { |
Jamie Madill | 1ea9aaa | 2015-10-07 11:13:55 -0400 | [diff] [blame] | 144 | // TODO(jmadill): Figure out why this fails on Intel. |
Jamie Madill | f09bf66 | 2016-03-02 11:26:01 -0500 | [diff] [blame^] | 145 | if (isIntel() && GetParam().getRenderer() == EGL_PLATFORM_ANGLE_TYPE_OPENGL_ANGLE) |
Jamie Madill | 1ea9aaa | 2015-10-07 11:13:55 -0400 | [diff] [blame] | 146 | { |
| 147 | std::cout << "Test skipped on Intel." << std::endl; |
| 148 | return; |
| 149 | } |
| 150 | |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 151 | glUseProgram(mProgram); |
| 152 | glBindTexture(GL_TEXTURE_2D, mTexture); |
| 153 | glUniform1i(mTextureUniformLocation, 0); |
| 154 | |
| 155 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED); |
| 156 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN); |
| 157 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE); |
| 158 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA); |
| 159 | |
| 160 | glClear(GL_COLOR_BUFFER_BIT); |
| 161 | drawQuad(mProgram, "position", 0.5f); |
| 162 | |
| 163 | GLubyte unswizzled[4]; |
| 164 | glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &unswizzled); |
| 165 | |
Jamie Madill | 1ea9aaa | 2015-10-07 11:13:55 -0400 | [diff] [blame] | 166 | ASSERT_GL_NO_ERROR(); |
| 167 | |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 168 | for (size_t i = 0; i < mPermutations.size(); i++) |
| 169 | { |
| 170 | const swizzlePermutation& permutation = mPermutations[i]; |
| 171 | |
| 172 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, permutation.swizzleRed); |
| 173 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, permutation.swizzleGreen); |
| 174 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, permutation.swizzleBlue); |
| 175 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, permutation.swizzleAlpha); |
| 176 | |
| 177 | glClear(GL_COLOR_BUFFER_BIT); |
| 178 | drawQuad(mProgram, "position", 0.5f); |
| 179 | |
| 180 | EXPECT_PIXEL_EQ(0, 0, |
| 181 | getExpectedValue(permutation.swizzleRed, unswizzled), |
| 182 | getExpectedValue(permutation.swizzleGreen, unswizzled), |
| 183 | getExpectedValue(permutation.swizzleBlue, unswizzled), |
| 184 | getExpectedValue(permutation.swizzleAlpha, unswizzled)); |
Jamie Madill | 1ea9aaa | 2015-10-07 11:13:55 -0400 | [diff] [blame] | 185 | |
| 186 | ASSERT_GL_NO_ERROR(); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | GLuint mProgram; |
| 191 | GLint mTextureUniformLocation; |
| 192 | |
| 193 | GLuint mTexture; |
| 194 | |
| 195 | struct swizzlePermutation |
| 196 | { |
| 197 | GLenum swizzleRed; |
| 198 | GLenum swizzleGreen; |
| 199 | GLenum swizzleBlue; |
| 200 | GLenum swizzleAlpha; |
| 201 | }; |
| 202 | std::vector<swizzlePermutation> mPermutations; |
| 203 | }; |
| 204 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 205 | TEST_P(SwizzleTest, RGBA8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 206 | { |
| 207 | GLubyte data[] = { 1, 64, 128, 200 }; |
| 208 | init2DTexture(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 209 | runTest2D(); |
| 210 | } |
| 211 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 212 | TEST_P(SwizzleTest, RGB8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 213 | { |
| 214 | GLubyte data[] = { 77, 66, 55 }; |
| 215 | init2DTexture(GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, data); |
| 216 | runTest2D(); |
| 217 | } |
| 218 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 219 | TEST_P(SwizzleTest, RG8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 220 | { |
| 221 | GLubyte data[] = { 11, 99 }; |
| 222 | init2DTexture(GL_RG8, GL_RG, GL_UNSIGNED_BYTE, data); |
| 223 | runTest2D(); |
| 224 | } |
| 225 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 226 | TEST_P(SwizzleTest, R8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 227 | { |
| 228 | GLubyte data[] = { 2 }; |
Nico Weber | ce8bb2f | 2014-12-30 13:32:25 -0800 | [diff] [blame] | 229 | init2DTexture(GL_R8, GL_RED, GL_UNSIGNED_BYTE, data); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 230 | runTest2D(); |
| 231 | } |
| 232 | |
Olli Etuaho | bc49758 | 2016-02-23 14:43:19 +0200 | [diff] [blame] | 233 | TEST_P(SwizzleTest, RGB10_A2_2D) |
| 234 | { |
| 235 | GLuint data[] = {20u | (40u << 10) | (60u << 20) | (2u << 30)}; |
| 236 | init2DTexture(GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, data); |
| 237 | runTest2D(); |
| 238 | } |
| 239 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 240 | TEST_P(SwizzleTest, RGBA32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 241 | { |
| 242 | GLfloat data[] = { 0.25f, 0.5f, 0.75f, 0.8f }; |
| 243 | init2DTexture(GL_RGBA32F, GL_RGBA, GL_FLOAT, data); |
| 244 | runTest2D(); |
| 245 | } |
| 246 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 247 | TEST_P(SwizzleTest, RGB32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 248 | { |
| 249 | GLfloat data[] = { 0.1f, 0.2f, 0.3f }; |
| 250 | init2DTexture(GL_RGB32F, GL_RGB, GL_FLOAT, data); |
| 251 | runTest2D(); |
| 252 | } |
| 253 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 254 | TEST_P(SwizzleTest, RG32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 255 | { |
| 256 | GLfloat data[] = { 0.9f, 0.1f }; |
| 257 | init2DTexture(GL_RG32F, GL_RG, GL_FLOAT, data); |
| 258 | runTest2D(); |
| 259 | } |
| 260 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 261 | TEST_P(SwizzleTest, R32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 262 | { |
| 263 | GLfloat data[] = { 0.5f }; |
| 264 | init2DTexture(GL_R32F, GL_RED, GL_FLOAT, data); |
| 265 | runTest2D(); |
| 266 | } |
| 267 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 268 | TEST_P(SwizzleTest, D32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 269 | { |
| 270 | GLfloat data[] = { 0.5f }; |
| 271 | init2DTexture(GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, data); |
| 272 | runTest2D(); |
| 273 | } |
| 274 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 275 | TEST_P(SwizzleTest, D16_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 276 | { |
| 277 | GLushort data[] = { 0xFF }; |
| 278 | init2DTexture(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, data); |
| 279 | runTest2D(); |
| 280 | } |
| 281 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 282 | TEST_P(SwizzleTest, D24_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 283 | { |
| 284 | GLuint data[] = { 0xFFFF }; |
| 285 | init2DTexture(GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, data); |
| 286 | runTest2D(); |
| 287 | } |
| 288 | |
Geoff Lang | 53b8aec | 2015-08-24 10:33:25 -0400 | [diff] [blame] | 289 | TEST_P(SwizzleTest, L8_2D) |
| 290 | { |
| 291 | GLubyte data[] = {0x77}; |
| 292 | init2DTexture(GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, data); |
| 293 | runTest2D(); |
| 294 | } |
| 295 | |
| 296 | TEST_P(SwizzleTest, A8_2D) |
| 297 | { |
| 298 | GLubyte data[] = {0x55}; |
| 299 | init2DTexture(GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, data); |
| 300 | runTest2D(); |
| 301 | } |
| 302 | |
| 303 | TEST_P(SwizzleTest, LA8_2D) |
| 304 | { |
| 305 | GLubyte data[] = {0x77, 0x66}; |
| 306 | init2DTexture(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data); |
| 307 | runTest2D(); |
| 308 | } |
| 309 | |
| 310 | TEST_P(SwizzleTest, L32F_2D) |
| 311 | { |
| 312 | GLfloat data[] = {0.7f}; |
| 313 | init2DTexture(GL_LUMINANCE, GL_LUMINANCE, GL_FLOAT, data); |
| 314 | runTest2D(); |
| 315 | } |
| 316 | |
| 317 | TEST_P(SwizzleTest, A32F_2D) |
| 318 | { |
| 319 | GLfloat data[] = { |
| 320 | 0.4f, |
| 321 | }; |
| 322 | init2DTexture(GL_ALPHA, GL_ALPHA, GL_FLOAT, data); |
| 323 | runTest2D(); |
| 324 | } |
| 325 | |
| 326 | TEST_P(SwizzleTest, LA32F_2D) |
| 327 | { |
| 328 | GLfloat data[] = { |
| 329 | 0.5f, 0.6f, |
| 330 | }; |
| 331 | init2DTexture(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_FLOAT, data); |
| 332 | runTest2D(); |
| 333 | } |
| 334 | |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 335 | #include "media/pixel.inl" |
| 336 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 337 | TEST_P(SwizzleTest, CompressedDXT_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 338 | { |
Geoff Lang | 167dceb | 2015-03-31 12:49:57 -0400 | [diff] [blame] | 339 | if (!extensionEnabled("GL_EXT_texture_compression_dxt1")) |
| 340 | { |
Geoff Lang | f34d1db | 2015-05-20 14:10:46 -0400 | [diff] [blame] | 341 | std::cout << "Test skipped due to missing GL_EXT_texture_compression_dxt1." << std::endl; |
Geoff Lang | 167dceb | 2015-03-31 12:49:57 -0400 | [diff] [blame] | 342 | return; |
| 343 | } |
| 344 | |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 345 | init2DCompressedTexture(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, pixel_0_width, pixel_0_height, pixel_0_size, pixel_0_data); |
| 346 | runTest2D(); |
| 347 | } |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 348 | |
| 349 | // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against. |
Geoff Lang | e0cc2a4 | 2016-01-20 10:58:17 -0500 | [diff] [blame] | 350 | ANGLE_INSTANTIATE_TEST(SwizzleTest, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGL(3, 3), ES3_OPENGLES()); |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 351 | |
| 352 | } // namespace |