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