blob: d81648843d1c6d64b92aa693004a31b8f8c797f6 [file] [log] [blame]
Jamie Madillfa05f602015-05-07 13:47:11 -04001//
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 Wallezd3970de2015-05-14 11:07:48 -04007#include "test_utils/ANGLETest.h"
Geoff Lang2c254d82014-01-15 14:51:23 -05008
9#include <vector>
10
Jamie Madillfa05f602015-05-07 13:47:11 -040011using namespace angle;
Austin Kinross18b931d2014-09-29 12:58:31 -070012
Jamie Madillfa05f602015-05-07 13:47:11 -040013namespace
14{
15
Geoff Lang2c254d82014-01-15 14:51:23 -050016class SwizzleTest : public ANGLETest
17{
Jamie Madillfa05f602015-05-07 13:47:11 -040018 protected:
19 SwizzleTest()
Geoff Lang2c254d82014-01-15 14:51:23 -050020 {
21 setWindowWidth(128);
22 setWindowHeight(128);
23 setConfigRedBits(8);
24 setConfigGreenBits(8);
25 setConfigBlueBits(8);
26 setConfigAlphaBits(8);
Geoff Lang2c254d82014-01-15 14:51:23 -050027
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 Madillfa05f602015-05-07 13:47:11 -040058 void SetUp() override
Geoff Lang2c254d82014-01-15 14:51:23 -050059 {
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 Madill5599c8f2014-08-26 13:16:39 -040087 mProgram = CompileProgram(vertexShaderSource, fragmentShaderSource);
Geoff Lang2c254d82014-01-15 14:51:23 -050088 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 Madillfa05f602015-05-07 13:47:11 -040098 void TearDown() override
Geoff Lang2c254d82014-01-15 14:51:23 -050099 {
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);
Geoff Lang53b8aec2015-08-24 10:33:25 -0400111 glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, 1, 1, 0, dataFormat, dataType, data);
Geoff Lang2c254d82014-01-15 14:51:23 -0500112
113 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
115 }
116
117 void init2DCompressedTexture(GLenum internalFormat, GLsizei width, GLsizei height, GLsizei dataSize, const GLubyte* data)
118 {
119 glGenTextures(1, &mTexture);
120 glBindTexture(GL_TEXTURE_2D, mTexture);
121 glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0, dataSize, data);
122
123 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
124 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
125 }
126
127 GLubyte getExpectedValue(GLenum swizzle, GLubyte unswizzled[4])
128 {
129 switch (swizzle)
130 {
131 case GL_RED: return unswizzled[0];
132 case GL_GREEN: return unswizzled[1];
133 case GL_BLUE: return unswizzled[2];
134 case GL_ALPHA: return unswizzled[3];
135 case GL_ZERO: return 0;
136 case GL_ONE: return 255;
137 default: return 0;
138 }
139 }
140
141 void runTest2D()
142 {
143 glUseProgram(mProgram);
144 glBindTexture(GL_TEXTURE_2D, mTexture);
145 glUniform1i(mTextureUniformLocation, 0);
146
147 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
148 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
150 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
151
152 glClear(GL_COLOR_BUFFER_BIT);
153 drawQuad(mProgram, "position", 0.5f);
154
155 GLubyte unswizzled[4];
156 glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &unswizzled);
157
158 for (size_t i = 0; i < mPermutations.size(); i++)
159 {
160 const swizzlePermutation& permutation = mPermutations[i];
161
162 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, permutation.swizzleRed);
163 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, permutation.swizzleGreen);
164 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, permutation.swizzleBlue);
165 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, permutation.swizzleAlpha);
166
167 glClear(GL_COLOR_BUFFER_BIT);
168 drawQuad(mProgram, "position", 0.5f);
169
170 EXPECT_PIXEL_EQ(0, 0,
171 getExpectedValue(permutation.swizzleRed, unswizzled),
172 getExpectedValue(permutation.swizzleGreen, unswizzled),
173 getExpectedValue(permutation.swizzleBlue, unswizzled),
174 getExpectedValue(permutation.swizzleAlpha, unswizzled));
175 }
176 }
177
178 GLuint mProgram;
179 GLint mTextureUniformLocation;
180
181 GLuint mTexture;
182
183 struct swizzlePermutation
184 {
185 GLenum swizzleRed;
186 GLenum swizzleGreen;
187 GLenum swizzleBlue;
188 GLenum swizzleAlpha;
189 };
190 std::vector<swizzlePermutation> mPermutations;
191};
192
Jamie Madillfa05f602015-05-07 13:47:11 -0400193TEST_P(SwizzleTest, RGBA8_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500194{
195 GLubyte data[] = { 1, 64, 128, 200 };
196 init2DTexture(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, data);
197 runTest2D();
198}
199
Jamie Madillfa05f602015-05-07 13:47:11 -0400200TEST_P(SwizzleTest, RGB8_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500201{
202 GLubyte data[] = { 77, 66, 55 };
203 init2DTexture(GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, data);
204 runTest2D();
205}
206
Jamie Madillfa05f602015-05-07 13:47:11 -0400207TEST_P(SwizzleTest, RG8_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500208{
209 GLubyte data[] = { 11, 99 };
210 init2DTexture(GL_RG8, GL_RG, GL_UNSIGNED_BYTE, data);
211 runTest2D();
212}
213
Jamie Madillfa05f602015-05-07 13:47:11 -0400214TEST_P(SwizzleTest, R8_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500215{
216 GLubyte data[] = { 2 };
Nico Weberce8bb2f2014-12-30 13:32:25 -0800217 init2DTexture(GL_R8, GL_RED, GL_UNSIGNED_BYTE, data);
Geoff Lang2c254d82014-01-15 14:51:23 -0500218 runTest2D();
219}
220
Jamie Madillfa05f602015-05-07 13:47:11 -0400221TEST_P(SwizzleTest, RGBA32F_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500222{
223 GLfloat data[] = { 0.25f, 0.5f, 0.75f, 0.8f };
224 init2DTexture(GL_RGBA32F, GL_RGBA, GL_FLOAT, data);
225 runTest2D();
226}
227
Jamie Madillfa05f602015-05-07 13:47:11 -0400228TEST_P(SwizzleTest, RGB32F_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500229{
230 GLfloat data[] = { 0.1f, 0.2f, 0.3f };
231 init2DTexture(GL_RGB32F, GL_RGB, GL_FLOAT, data);
232 runTest2D();
233}
234
Jamie Madillfa05f602015-05-07 13:47:11 -0400235TEST_P(SwizzleTest, RG32F_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500236{
237 GLfloat data[] = { 0.9f, 0.1f };
238 init2DTexture(GL_RG32F, GL_RG, GL_FLOAT, data);
239 runTest2D();
240}
241
Jamie Madillfa05f602015-05-07 13:47:11 -0400242TEST_P(SwizzleTest, R32F_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500243{
244 GLfloat data[] = { 0.5f };
245 init2DTexture(GL_R32F, GL_RED, GL_FLOAT, data);
246 runTest2D();
247}
248
Jamie Madillfa05f602015-05-07 13:47:11 -0400249TEST_P(SwizzleTest, D32F_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500250{
251 GLfloat data[] = { 0.5f };
252 init2DTexture(GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, data);
253 runTest2D();
254}
255
Jamie Madillfa05f602015-05-07 13:47:11 -0400256TEST_P(SwizzleTest, D16_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500257{
258 GLushort data[] = { 0xFF };
259 init2DTexture(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, data);
260 runTest2D();
261}
262
Jamie Madillfa05f602015-05-07 13:47:11 -0400263TEST_P(SwizzleTest, D24_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500264{
265 GLuint data[] = { 0xFFFF };
266 init2DTexture(GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, data);
267 runTest2D();
268}
269
Geoff Lang53b8aec2015-08-24 10:33:25 -0400270TEST_P(SwizzleTest, L8_2D)
271{
272 GLubyte data[] = {0x77};
273 init2DTexture(GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
274 runTest2D();
275}
276
277TEST_P(SwizzleTest, A8_2D)
278{
279 GLubyte data[] = {0x55};
280 init2DTexture(GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, data);
281 runTest2D();
282}
283
284TEST_P(SwizzleTest, LA8_2D)
285{
286 GLubyte data[] = {0x77, 0x66};
287 init2DTexture(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data);
288 runTest2D();
289}
290
291TEST_P(SwizzleTest, L32F_2D)
292{
293 GLfloat data[] = {0.7f};
294 init2DTexture(GL_LUMINANCE, GL_LUMINANCE, GL_FLOAT, data);
295 runTest2D();
296}
297
298TEST_P(SwizzleTest, A32F_2D)
299{
300 GLfloat data[] = {
301 0.4f,
302 };
303 init2DTexture(GL_ALPHA, GL_ALPHA, GL_FLOAT, data);
304 runTest2D();
305}
306
307TEST_P(SwizzleTest, LA32F_2D)
308{
309 GLfloat data[] = {
310 0.5f, 0.6f,
311 };
312 init2DTexture(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
313 runTest2D();
314}
315
Geoff Lang2c254d82014-01-15 14:51:23 -0500316#include "media/pixel.inl"
317
Jamie Madillfa05f602015-05-07 13:47:11 -0400318TEST_P(SwizzleTest, CompressedDXT_2D)
Geoff Lang2c254d82014-01-15 14:51:23 -0500319{
Geoff Lang167dceb2015-03-31 12:49:57 -0400320 if (!extensionEnabled("GL_EXT_texture_compression_dxt1"))
321 {
Geoff Langf34d1db2015-05-20 14:10:46 -0400322 std::cout << "Test skipped due to missing GL_EXT_texture_compression_dxt1." << std::endl;
Geoff Lang167dceb2015-03-31 12:49:57 -0400323 return;
324 }
325
Geoff Lang2c254d82014-01-15 14:51:23 -0500326 init2DCompressedTexture(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, pixel_0_width, pixel_0_height, pixel_0_size, pixel_0_data);
327 runTest2D();
328}
Jamie Madillfa05f602015-05-07 13:47:11 -0400329
330// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
Geoff Lang53b8aec2015-08-24 10:33:25 -0400331ANGLE_INSTANTIATE_TEST(SwizzleTest, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGL(3, 3));
Jamie Madillfa05f602015-05-07 13:47:11 -0400332
333} // namespace