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); |
| 96 | } |
| 97 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 98 | void TearDown() override |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 99 | { |
| 100 | glDeleteProgram(mProgram); |
| 101 | glDeleteTextures(1, &mTexture); |
| 102 | |
| 103 | ANGLETest::TearDown(); |
| 104 | } |
| 105 | |
| 106 | template <typename T> |
| 107 | void init2DTexture(GLenum internalFormat, GLenum dataFormat, GLenum dataType, const T* data) |
| 108 | { |
| 109 | glGenTextures(1, &mTexture); |
| 110 | glBindTexture(GL_TEXTURE_2D, mTexture); |
| 111 | glTexStorage2D(GL_TEXTURE_2D, 1, internalFormat, 1, 1); |
| 112 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, dataFormat, dataType, data); |
| 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 | { |
| 144 | glUseProgram(mProgram); |
| 145 | glBindTexture(GL_TEXTURE_2D, mTexture); |
| 146 | glUniform1i(mTextureUniformLocation, 0); |
| 147 | |
| 148 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED); |
| 149 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN); |
| 150 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE); |
| 151 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA); |
| 152 | |
| 153 | glClear(GL_COLOR_BUFFER_BIT); |
| 154 | drawQuad(mProgram, "position", 0.5f); |
| 155 | |
| 156 | GLubyte unswizzled[4]; |
| 157 | glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &unswizzled); |
| 158 | |
| 159 | for (size_t i = 0; i < mPermutations.size(); i++) |
| 160 | { |
| 161 | const swizzlePermutation& permutation = mPermutations[i]; |
| 162 | |
| 163 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, permutation.swizzleRed); |
| 164 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, permutation.swizzleGreen); |
| 165 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, permutation.swizzleBlue); |
| 166 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, permutation.swizzleAlpha); |
| 167 | |
| 168 | glClear(GL_COLOR_BUFFER_BIT); |
| 169 | drawQuad(mProgram, "position", 0.5f); |
| 170 | |
| 171 | EXPECT_PIXEL_EQ(0, 0, |
| 172 | getExpectedValue(permutation.swizzleRed, unswizzled), |
| 173 | getExpectedValue(permutation.swizzleGreen, unswizzled), |
| 174 | getExpectedValue(permutation.swizzleBlue, unswizzled), |
| 175 | getExpectedValue(permutation.swizzleAlpha, unswizzled)); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | GLuint mProgram; |
| 180 | GLint mTextureUniformLocation; |
| 181 | |
| 182 | GLuint mTexture; |
| 183 | |
| 184 | struct swizzlePermutation |
| 185 | { |
| 186 | GLenum swizzleRed; |
| 187 | GLenum swizzleGreen; |
| 188 | GLenum swizzleBlue; |
| 189 | GLenum swizzleAlpha; |
| 190 | }; |
| 191 | std::vector<swizzlePermutation> mPermutations; |
| 192 | }; |
| 193 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 194 | TEST_P(SwizzleTest, RGBA8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 195 | { |
| 196 | GLubyte data[] = { 1, 64, 128, 200 }; |
| 197 | init2DTexture(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 198 | runTest2D(); |
| 199 | } |
| 200 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 201 | TEST_P(SwizzleTest, RGB8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 202 | { |
| 203 | GLubyte data[] = { 77, 66, 55 }; |
| 204 | init2DTexture(GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, data); |
| 205 | runTest2D(); |
| 206 | } |
| 207 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 208 | TEST_P(SwizzleTest, RG8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 209 | { |
| 210 | GLubyte data[] = { 11, 99 }; |
| 211 | init2DTexture(GL_RG8, GL_RG, GL_UNSIGNED_BYTE, data); |
| 212 | runTest2D(); |
| 213 | } |
| 214 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 215 | TEST_P(SwizzleTest, R8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 216 | { |
| 217 | GLubyte data[] = { 2 }; |
Nico Weber | ce8bb2f | 2014-12-30 13:32:25 -0800 | [diff] [blame] | 218 | init2DTexture(GL_R8, GL_RED, GL_UNSIGNED_BYTE, data); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 219 | runTest2D(); |
| 220 | } |
| 221 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 222 | TEST_P(SwizzleTest, RGBA32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 223 | { |
| 224 | GLfloat data[] = { 0.25f, 0.5f, 0.75f, 0.8f }; |
| 225 | init2DTexture(GL_RGBA32F, GL_RGBA, GL_FLOAT, data); |
| 226 | runTest2D(); |
| 227 | } |
| 228 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 229 | TEST_P(SwizzleTest, RGB32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 230 | { |
| 231 | GLfloat data[] = { 0.1f, 0.2f, 0.3f }; |
| 232 | init2DTexture(GL_RGB32F, GL_RGB, GL_FLOAT, data); |
| 233 | runTest2D(); |
| 234 | } |
| 235 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 236 | TEST_P(SwizzleTest, RG32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 237 | { |
| 238 | GLfloat data[] = { 0.9f, 0.1f }; |
| 239 | init2DTexture(GL_RG32F, GL_RG, GL_FLOAT, data); |
| 240 | runTest2D(); |
| 241 | } |
| 242 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 243 | TEST_P(SwizzleTest, R32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 244 | { |
| 245 | GLfloat data[] = { 0.5f }; |
| 246 | init2DTexture(GL_R32F, GL_RED, GL_FLOAT, data); |
| 247 | runTest2D(); |
| 248 | } |
| 249 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 250 | TEST_P(SwizzleTest, D32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 251 | { |
| 252 | GLfloat data[] = { 0.5f }; |
| 253 | init2DTexture(GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, data); |
| 254 | runTest2D(); |
| 255 | } |
| 256 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 257 | TEST_P(SwizzleTest, D16_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 258 | { |
| 259 | GLushort data[] = { 0xFF }; |
| 260 | init2DTexture(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, data); |
| 261 | runTest2D(); |
| 262 | } |
| 263 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 264 | TEST_P(SwizzleTest, D24_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 265 | { |
| 266 | GLuint data[] = { 0xFFFF }; |
| 267 | init2DTexture(GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, data); |
| 268 | runTest2D(); |
| 269 | } |
| 270 | |
| 271 | #include "media/pixel.inl" |
| 272 | |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 273 | TEST_P(SwizzleTest, CompressedDXT_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 274 | { |
Geoff Lang | 167dceb | 2015-03-31 12:49:57 -0400 | [diff] [blame] | 275 | if (!extensionEnabled("GL_EXT_texture_compression_dxt1")) |
| 276 | { |
| 277 | return; |
| 278 | } |
| 279 | |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 280 | init2DCompressedTexture(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, pixel_0_width, pixel_0_height, pixel_0_size, pixel_0_data); |
| 281 | runTest2D(); |
| 282 | } |
Jamie Madill | fa05f60 | 2015-05-07 13:47:11 -0400 | [diff] [blame] | 283 | |
| 284 | // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against. |
| 285 | ANGLE_INSTANTIATE_TEST(SwizzleTest, ES3_D3D11()); |
| 286 | |
| 287 | } // namespace |