Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 1 | #include "ANGLETest.h" |
| 2 | |
| 3 | #include <vector> |
| 4 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 5 | // Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against. |
Geoff Lang | 0d3683c | 2014-10-23 11:08:16 -0400 | [diff] [blame] | 6 | ANGLE_TYPED_TEST_CASE(SwizzleTest, ES3_D3D11); |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 7 | |
| 8 | template<typename T> |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 9 | class SwizzleTest : public ANGLETest |
| 10 | { |
| 11 | protected: |
Geoff Lang | 0d3683c | 2014-10-23 11:08:16 -0400 | [diff] [blame] | 12 | SwizzleTest() : ANGLETest(T::GetGlesMajorVersion(), T::GetPlatform()) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 13 | { |
| 14 | setWindowWidth(128); |
| 15 | setWindowHeight(128); |
| 16 | setConfigRedBits(8); |
| 17 | setConfigGreenBits(8); |
| 18 | setConfigBlueBits(8); |
| 19 | setConfigAlphaBits(8); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 20 | |
| 21 | GLenum swizzles[] = |
| 22 | { |
| 23 | GL_RED, |
| 24 | GL_GREEN, |
| 25 | GL_BLUE, |
| 26 | GL_ALPHA, |
| 27 | GL_ZERO, |
| 28 | GL_ONE, |
| 29 | }; |
| 30 | |
| 31 | for (int r = 0; r < 6; r++) |
| 32 | { |
| 33 | for (int g = 0; g < 6; g++) |
| 34 | { |
| 35 | for (int b = 0; b < 6; b++) |
| 36 | { |
| 37 | for (int a = 0; a < 6; a++) |
| 38 | { |
| 39 | swizzlePermutation permutation; |
| 40 | permutation.swizzleRed = swizzles[r]; |
| 41 | permutation.swizzleGreen = swizzles[g]; |
| 42 | permutation.swizzleBlue = swizzles[b]; |
| 43 | permutation.swizzleAlpha = swizzles[a]; |
| 44 | mPermutations.push_back(permutation); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | virtual void SetUp() |
| 52 | { |
| 53 | ANGLETest::SetUp(); |
| 54 | |
| 55 | const std::string vertexShaderSource = SHADER_SOURCE |
| 56 | ( |
| 57 | precision highp float; |
| 58 | attribute vec4 position; |
| 59 | varying vec2 texcoord; |
| 60 | |
| 61 | void main() |
| 62 | { |
| 63 | gl_Position = position; |
| 64 | texcoord = (position.xy * 0.5) + 0.5; |
| 65 | } |
| 66 | ); |
| 67 | |
| 68 | const std::string fragmentShaderSource = SHADER_SOURCE |
| 69 | ( |
| 70 | precision highp float; |
| 71 | uniform sampler2D tex; |
| 72 | varying vec2 texcoord; |
| 73 | |
| 74 | void main() |
| 75 | { |
| 76 | gl_FragColor = texture2D(tex, texcoord); |
| 77 | } |
| 78 | ); |
| 79 | |
Jamie Madill | 5599c8f | 2014-08-26 13:16:39 -0400 | [diff] [blame] | 80 | mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 81 | if (mProgram == 0) |
| 82 | { |
| 83 | FAIL() << "shader compilation failed."; |
| 84 | } |
| 85 | |
| 86 | mTextureUniformLocation = glGetUniformLocation(mProgram, "tex"); |
| 87 | |
| 88 | glClearColor(0.0f, 0.0f, 0.0f, 0.0f); |
| 89 | } |
| 90 | |
| 91 | virtual void TearDown() |
| 92 | { |
| 93 | glDeleteProgram(mProgram); |
| 94 | glDeleteTextures(1, &mTexture); |
| 95 | |
| 96 | ANGLETest::TearDown(); |
| 97 | } |
| 98 | |
| 99 | template <typename T> |
| 100 | void init2DTexture(GLenum internalFormat, GLenum dataFormat, GLenum dataType, const T* data) |
| 101 | { |
| 102 | glGenTextures(1, &mTexture); |
| 103 | glBindTexture(GL_TEXTURE_2D, mTexture); |
| 104 | glTexStorage2D(GL_TEXTURE_2D, 1, internalFormat, 1, 1); |
| 105 | glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, dataFormat, dataType, data); |
| 106 | |
| 107 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 108 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 109 | } |
| 110 | |
| 111 | void init2DCompressedTexture(GLenum internalFormat, GLsizei width, GLsizei height, GLsizei dataSize, const GLubyte* data) |
| 112 | { |
| 113 | glGenTextures(1, &mTexture); |
| 114 | glBindTexture(GL_TEXTURE_2D, mTexture); |
| 115 | glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0, dataSize, data); |
| 116 | |
| 117 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 118 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 119 | } |
| 120 | |
| 121 | GLubyte getExpectedValue(GLenum swizzle, GLubyte unswizzled[4]) |
| 122 | { |
| 123 | switch (swizzle) |
| 124 | { |
| 125 | case GL_RED: return unswizzled[0]; |
| 126 | case GL_GREEN: return unswizzled[1]; |
| 127 | case GL_BLUE: return unswizzled[2]; |
| 128 | case GL_ALPHA: return unswizzled[3]; |
| 129 | case GL_ZERO: return 0; |
| 130 | case GL_ONE: return 255; |
| 131 | default: return 0; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void runTest2D() |
| 136 | { |
| 137 | glUseProgram(mProgram); |
| 138 | glBindTexture(GL_TEXTURE_2D, mTexture); |
| 139 | glUniform1i(mTextureUniformLocation, 0); |
| 140 | |
| 141 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED); |
| 142 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN); |
| 143 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE); |
| 144 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA); |
| 145 | |
| 146 | glClear(GL_COLOR_BUFFER_BIT); |
| 147 | drawQuad(mProgram, "position", 0.5f); |
| 148 | |
| 149 | GLubyte unswizzled[4]; |
| 150 | glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &unswizzled); |
| 151 | |
| 152 | for (size_t i = 0; i < mPermutations.size(); i++) |
| 153 | { |
| 154 | const swizzlePermutation& permutation = mPermutations[i]; |
| 155 | |
| 156 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, permutation.swizzleRed); |
| 157 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, permutation.swizzleGreen); |
| 158 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, permutation.swizzleBlue); |
| 159 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, permutation.swizzleAlpha); |
| 160 | |
| 161 | glClear(GL_COLOR_BUFFER_BIT); |
| 162 | drawQuad(mProgram, "position", 0.5f); |
| 163 | |
| 164 | EXPECT_PIXEL_EQ(0, 0, |
| 165 | getExpectedValue(permutation.swizzleRed, unswizzled), |
| 166 | getExpectedValue(permutation.swizzleGreen, unswizzled), |
| 167 | getExpectedValue(permutation.swizzleBlue, unswizzled), |
| 168 | getExpectedValue(permutation.swizzleAlpha, unswizzled)); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | GLuint mProgram; |
| 173 | GLint mTextureUniformLocation; |
| 174 | |
| 175 | GLuint mTexture; |
| 176 | |
| 177 | struct swizzlePermutation |
| 178 | { |
| 179 | GLenum swizzleRed; |
| 180 | GLenum swizzleGreen; |
| 181 | GLenum swizzleBlue; |
| 182 | GLenum swizzleAlpha; |
| 183 | }; |
| 184 | std::vector<swizzlePermutation> mPermutations; |
| 185 | }; |
| 186 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 187 | TYPED_TEST(SwizzleTest, RGBA8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 188 | { |
| 189 | GLubyte data[] = { 1, 64, 128, 200 }; |
| 190 | init2DTexture(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, data); |
| 191 | runTest2D(); |
| 192 | } |
| 193 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 194 | TYPED_TEST(SwizzleTest, RGB8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 195 | { |
| 196 | GLubyte data[] = { 77, 66, 55 }; |
| 197 | init2DTexture(GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, data); |
| 198 | runTest2D(); |
| 199 | } |
| 200 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 201 | TYPED_TEST(SwizzleTest, RG8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 202 | { |
| 203 | GLubyte data[] = { 11, 99 }; |
| 204 | init2DTexture(GL_RG8, GL_RG, GL_UNSIGNED_BYTE, data); |
| 205 | runTest2D(); |
| 206 | } |
| 207 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 208 | TYPED_TEST(SwizzleTest, R8_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 209 | { |
| 210 | GLubyte data[] = { 2 }; |
Nico Weber | ce8bb2f | 2014-12-30 13:32:25 -0800 | [diff] [blame] | 211 | init2DTexture(GL_R8, GL_RED, GL_UNSIGNED_BYTE, data); |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 212 | runTest2D(); |
| 213 | } |
| 214 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 215 | TYPED_TEST(SwizzleTest, RGBA32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 216 | { |
| 217 | GLfloat data[] = { 0.25f, 0.5f, 0.75f, 0.8f }; |
| 218 | init2DTexture(GL_RGBA32F, GL_RGBA, GL_FLOAT, data); |
| 219 | runTest2D(); |
| 220 | } |
| 221 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 222 | TYPED_TEST(SwizzleTest, RGB32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 223 | { |
| 224 | GLfloat data[] = { 0.1f, 0.2f, 0.3f }; |
| 225 | init2DTexture(GL_RGB32F, GL_RGB, GL_FLOAT, data); |
| 226 | runTest2D(); |
| 227 | } |
| 228 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 229 | TYPED_TEST(SwizzleTest, RG32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 230 | { |
| 231 | GLfloat data[] = { 0.9f, 0.1f }; |
| 232 | init2DTexture(GL_RG32F, GL_RG, GL_FLOAT, data); |
| 233 | runTest2D(); |
| 234 | } |
| 235 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 236 | TYPED_TEST(SwizzleTest, R32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 237 | { |
| 238 | GLfloat data[] = { 0.5f }; |
| 239 | init2DTexture(GL_R32F, GL_RED, GL_FLOAT, data); |
| 240 | runTest2D(); |
| 241 | } |
| 242 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 243 | TYPED_TEST(SwizzleTest, D32F_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 244 | { |
| 245 | GLfloat data[] = { 0.5f }; |
| 246 | init2DTexture(GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, data); |
| 247 | runTest2D(); |
| 248 | } |
| 249 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 250 | TYPED_TEST(SwizzleTest, D16_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 251 | { |
| 252 | GLushort data[] = { 0xFF }; |
| 253 | init2DTexture(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, data); |
| 254 | runTest2D(); |
| 255 | } |
| 256 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 257 | TYPED_TEST(SwizzleTest, D24_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 258 | { |
| 259 | GLuint data[] = { 0xFFFF }; |
| 260 | init2DTexture(GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, data); |
| 261 | runTest2D(); |
| 262 | } |
| 263 | |
| 264 | #include "media/pixel.inl" |
| 265 | |
Austin Kinross | 18b931d | 2014-09-29 12:58:31 -0700 | [diff] [blame] | 266 | TYPED_TEST(SwizzleTest, CompressedDXT_2D) |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 267 | { |
Geoff Lang | 167dceb | 2015-03-31 12:49:57 -0400 | [diff] [blame^] | 268 | if (!extensionEnabled("GL_EXT_texture_compression_dxt1")) |
| 269 | { |
| 270 | return; |
| 271 | } |
| 272 | |
Geoff Lang | 2c254d8 | 2014-01-15 14:51:23 -0500 | [diff] [blame] | 273 | init2DCompressedTexture(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, pixel_0_width, pixel_0_height, pixel_0_size, pixel_0_data); |
| 274 | runTest2D(); |
| 275 | } |