Hash :
35cd7332
Author :
Date :
2018-12-02T12:03:33
Refactor test shader style. This change enforces a lot more consistency. We pass const char * to the Compile functions instead of std::string. Also fixes the indentation of C++11 block comments to be more consistent. Bug: angleproject:2995 Change-Id: Id6e5ea94055d8cbd420df4ea2e81b2d96cb5ce78 Reviewed-on: https://chromium-review.googlesource.com/c/1357103 Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
//
// Copyright 2015 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#include "test_utils/ANGLETest.h"
#include <vector>
using namespace angle;
namespace
{
class SwizzleTest : public ANGLETest
{
protected:
SwizzleTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
constexpr GLenum swizzles[] = {
GL_RED, GL_GREEN, GL_BLUE, GL_ALPHA, GL_ZERO, GL_ONE,
};
// Only use every 13th swizzle permutation, use a prime number to make sure the permuations
// are somewhat evenly distributed. Reduces the permuations from 1296 to 100.
constexpr size_t swizzleReductionFactor = 13;
size_t swizzleCount = 0;
for (GLenum r : swizzles)
{
for (GLenum g : swizzles)
{
for (GLenum b : swizzles)
{
for (GLenum a : swizzles)
{
swizzleCount++;
if (swizzleCount % swizzleReductionFactor != 0)
{
continue;
}
swizzlePermutation permutation;
permutation.swizzleRed = r;
permutation.swizzleGreen = g;
permutation.swizzleBlue = b;
permutation.swizzleAlpha = a;
mPermutations.push_back(permutation);
}
}
}
}
}
void SetUp() override
{
ANGLETest::SetUp();
constexpr char kVS[] = R"(precision highp float;
attribute vec4 position;
varying vec2 texcoord;
void main()
{
gl_Position = position;
texcoord = (position.xy * 0.5) + 0.5;
})";
constexpr char kFS[] = R"(precision highp float;
uniform sampler2D tex;
varying vec2 texcoord;
void main()
{
gl_FragColor = texture2D(tex, texcoord);
})";
mProgram = CompileProgram(kVS, kFS);
ASSERT_NE(0u, mProgram);
mTextureUniformLocation = glGetUniformLocation(mProgram, "tex");
ASSERT_NE(-1, mTextureUniformLocation);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
ASSERT_GL_NO_ERROR();
}
void TearDown() override
{
glDeleteProgram(mProgram);
glDeleteTextures(1, &mTexture);
ANGLETest::TearDown();
}
template <typename T>
void init2DTexture(GLenum internalFormat, GLenum dataFormat, GLenum dataType, const T *data)
{
glGenTextures(1, &mTexture);
glBindTexture(GL_TEXTURE_2D, mTexture);
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, 1, 1, 0, dataFormat, dataType, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
void init2DCompressedTexture(GLenum internalFormat,
GLsizei width,
GLsizei height,
GLsizei dataSize,
const GLubyte *data)
{
glGenTextures(1, &mTexture);
glBindTexture(GL_TEXTURE_2D, mTexture);
glCompressedTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, width, height, 0,
dataSize, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
GLubyte getExpectedValue(GLenum swizzle, GLubyte unswizzled[4])
{
switch (swizzle)
{
case GL_RED:
return unswizzled[0];
case GL_GREEN:
return unswizzled[1];
case GL_BLUE:
return unswizzled[2];
case GL_ALPHA:
return unswizzled[3];
case GL_ZERO:
return 0;
case GL_ONE:
return 255;
default:
return 0;
}
}
void runTest2D()
{
glUseProgram(mProgram);
glBindTexture(GL_TEXTURE_2D, mTexture);
glUniform1i(mTextureUniformLocation, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_GREEN);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_BLUE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_ALPHA);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(mProgram, "position", 0.5f);
GLubyte unswizzled[4];
glReadPixels(0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &unswizzled);
ASSERT_GL_NO_ERROR();
for (const auto &permutation : mPermutations)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, permutation.swizzleRed);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, permutation.swizzleGreen);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, permutation.swizzleBlue);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, permutation.swizzleAlpha);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(mProgram, "position", 0.5f);
EXPECT_PIXEL_EQ(0, 0, getExpectedValue(permutation.swizzleRed, unswizzled),
getExpectedValue(permutation.swizzleGreen, unswizzled),
getExpectedValue(permutation.swizzleBlue, unswizzled),
getExpectedValue(permutation.swizzleAlpha, unswizzled));
ASSERT_GL_NO_ERROR();
}
}
GLuint mProgram;
GLint mTextureUniformLocation;
GLuint mTexture;
struct swizzlePermutation
{
GLenum swizzleRed;
GLenum swizzleGreen;
GLenum swizzleBlue;
GLenum swizzleAlpha;
};
std::vector<swizzlePermutation> mPermutations;
};
class SwizzleIntegerTest : public SwizzleTest
{
protected:
void SetUp() override
{
ANGLETest::SetUp();
constexpr char kVS[] =
"#version 300 es\n"
"precision highp float;\n"
"in vec4 position;\n"
"out vec2 texcoord;\n"
"\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
" texcoord = (position.xy * 0.5) + 0.5;\n"
"}\n";
constexpr char kFS[] =
"#version 300 es\n"
"precision highp float;\n"
"precision highp usampler2D;\n"
"uniform usampler2D tex;\n"
"in vec2 texcoord;\n"
"out vec4 my_FragColor;\n"
"\n"
"void main()\n"
"{\n"
" uvec4 s = texture(tex, texcoord);\n"
" if (s[0] == 1u) s[0] = 255u;\n"
" if (s[1] == 1u) s[1] = 255u;\n"
" if (s[2] == 1u) s[2] = 255u;\n"
" if (s[3] == 1u) s[3] = 255u;\n"
" my_FragColor = vec4(s) / 255.0;\n"
"}\n";
mProgram = CompileProgram(kVS, kFS);
ASSERT_NE(0u, mProgram);
mTextureUniformLocation = glGetUniformLocation(mProgram, "tex");
ASSERT_NE(-1, mTextureUniformLocation);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
ASSERT_GL_NO_ERROR();
}
};
TEST_P(SwizzleTest, RGBA8_2D)
{
GLubyte data[] = {1, 64, 128, 200};
init2DTexture(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, data);
runTest2D();
}
TEST_P(SwizzleTest, RGB8_2D)
{
GLubyte data[] = {77, 66, 55};
init2DTexture(GL_RGB8, GL_RGB, GL_UNSIGNED_BYTE, data);
runTest2D();
}
TEST_P(SwizzleTest, RG8_2D)
{
GLubyte data[] = {11, 99};
init2DTexture(GL_RG8, GL_RG, GL_UNSIGNED_BYTE, data);
runTest2D();
}
TEST_P(SwizzleTest, R8_2D)
{
GLubyte data[] = {2};
init2DTexture(GL_R8, GL_RED, GL_UNSIGNED_BYTE, data);
runTest2D();
}
TEST_P(SwizzleTest, RGB10_A2_2D)
{
GLuint data[] = {20u | (40u << 10) | (60u << 20) | (2u << 30)};
init2DTexture(GL_RGB10_A2, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, data);
runTest2D();
}
TEST_P(SwizzleTest, RGBA32F_2D)
{
GLfloat data[] = {0.25f, 0.5f, 0.75f, 0.8f};
init2DTexture(GL_RGBA32F, GL_RGBA, GL_FLOAT, data);
runTest2D();
}
TEST_P(SwizzleTest, RGB32F_2D)
{
GLfloat data[] = {0.1f, 0.2f, 0.3f};
init2DTexture(GL_RGB32F, GL_RGB, GL_FLOAT, data);
runTest2D();
}
TEST_P(SwizzleTest, RG32F_2D)
{
GLfloat data[] = {0.9f, 0.1f};
init2DTexture(GL_RG32F, GL_RG, GL_FLOAT, data);
runTest2D();
}
TEST_P(SwizzleTest, R32F_2D)
{
GLfloat data[] = {0.5f};
init2DTexture(GL_R32F, GL_RED, GL_FLOAT, data);
runTest2D();
}
TEST_P(SwizzleTest, D32F_2D)
{
GLfloat data[] = {0.5f};
init2DTexture(GL_DEPTH_COMPONENT32F, GL_DEPTH_COMPONENT, GL_FLOAT, data);
runTest2D();
}
TEST_P(SwizzleTest, D16_2D)
{
GLushort data[] = {0xFF};
init2DTexture(GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT, GL_UNSIGNED_SHORT, data);
runTest2D();
}
TEST_P(SwizzleTest, D24_2D)
{
GLuint data[] = {0xFFFF};
init2DTexture(GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, data);
runTest2D();
}
TEST_P(SwizzleTest, L8_2D)
{
GLubyte data[] = {0x77};
init2DTexture(GL_LUMINANCE, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
runTest2D();
}
TEST_P(SwizzleTest, A8_2D)
{
GLubyte data[] = {0x55};
init2DTexture(GL_ALPHA, GL_ALPHA, GL_UNSIGNED_BYTE, data);
runTest2D();
}
TEST_P(SwizzleTest, LA8_2D)
{
GLubyte data[] = {0x77, 0x66};
init2DTexture(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, data);
runTest2D();
}
TEST_P(SwizzleTest, L32F_2D)
{
ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_OES_texture_float"));
GLfloat data[] = {0.7f};
init2DTexture(GL_LUMINANCE, GL_LUMINANCE, GL_FLOAT, data);
runTest2D();
}
TEST_P(SwizzleTest, A32F_2D)
{
ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_OES_texture_float"));
GLfloat data[] = {
0.4f,
};
init2DTexture(GL_ALPHA, GL_ALPHA, GL_FLOAT, data);
runTest2D();
}
TEST_P(SwizzleTest, LA32F_2D)
{
ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_OES_texture_float"));
GLfloat data[] = {
0.5f,
0.6f,
};
init2DTexture(GL_LUMINANCE_ALPHA, GL_LUMINANCE_ALPHA, GL_FLOAT, data);
runTest2D();
}
#include "media/pixel.inl"
TEST_P(SwizzleTest, CompressedDXT_2D)
{
ANGLE_SKIP_TEST_IF(!extensionEnabled("GL_EXT_texture_compression_dxt1"));
init2DCompressedTexture(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT, pixel_0_width, pixel_0_height,
pixel_0_size, pixel_0_data);
runTest2D();
}
TEST_P(SwizzleIntegerTest, RGB8UI_2D)
{
GLubyte data[] = {77, 66, 55};
init2DTexture(GL_RGB8UI, GL_RGB_INTEGER, GL_UNSIGNED_BYTE, data);
runTest2D();
}
// Test that updating the texture data still generates the correct swizzles
TEST_P(SwizzleTest, SubUpdate)
{
GLColor data(1, 64, 128, 200);
init2DTexture(GL_RGBA8, GL_RGBA, GL_UNSIGNED_BYTE, &data);
glUseProgram(mProgram);
glBindTexture(GL_TEXTURE_2D, mTexture);
glUniform1i(mTextureUniformLocation, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_RED);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_RED);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_RED);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(mProgram, "position", 0.5f);
GLColor expectedData(data.R, data.R, data.R, data.R);
EXPECT_PIXEL_COLOR_EQ(0, 0, expectedData);
GLColor updateData(32, 234, 28, 232);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &updateData);
glClear(GL_COLOR_BUFFER_BIT);
drawQuad(mProgram, "position", 0.5f);
GLColor expectedUpdateData(updateData.R, updateData.R, updateData.R, updateData.R);
EXPECT_PIXEL_COLOR_EQ(0, 0, expectedUpdateData);
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these
// tests should be run against.
ANGLE_INSTANTIATE_TEST(SwizzleTest, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGL(3, 3), ES3_OPENGLES());
ANGLE_INSTANTIATE_TEST(SwizzleIntegerTest,
ES3_D3D11(),
ES3_OPENGL(),
ES3_OPENGL(3, 3),
ES3_OPENGLES());
} // namespace