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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
//
// 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.
//
// SixteenBppTextureTest:
// Basic tests using 16bpp texture formats (e.g. GL_RGB565).
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include "image_util/imageformats.h"
using namespace angle;
namespace
{
GLColor Convert565(const R5G6B5 &rgb565)
{
gl::ColorF colorf;
R5G6B5::readColor(&colorf, &rgb565);
Vector4 vecColor(colorf.red, colorf.green, colorf.blue, colorf.alpha);
return GLColor(vecColor);
}
R5G6B5 Convert565(const GLColor &glColor)
{
const Vector4 &vecColor = glColor.toNormalizedVector();
gl::ColorF colorf(vecColor.x(), vecColor.y(), vecColor.z(), vecColor.w());
R5G6B5 rgb565;
R5G6B5::writeColor(&rgb565, &colorf);
return rgb565;
}
class SixteenBppTextureTest : public ANGLETest
{
protected:
SixteenBppTextureTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
void SetUp() override
{
ANGLETest::SetUp();
constexpr char kVS[] = R"(precision highp float;
attribute vec4 position;
varying vec2 texcoord;
void main()
{
gl_Position = vec4(position.xy, 0.0, 1.0);
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);
})";
m2DProgram = CompileProgram(kVS, kFS);
mTexture2DUniformLocation = glGetUniformLocation(m2DProgram, "tex");
}
void TearDown() override
{
glDeleteProgram(m2DProgram);
ANGLETest::TearDown();
}
void simpleValidationBase(GLuint tex)
{
// Draw a quad using the texture
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(m2DProgram);
glUniform1i(mTexture2DUniformLocation, 0);
drawQuad(m2DProgram, "position", 0.5f);
ASSERT_GL_NO_ERROR();
int w = getWindowWidth() - 1;
int h = getWindowHeight() - 1;
// Check that it drew as expected
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(w, 0, GLColor::green);
EXPECT_PIXEL_COLOR_EQ(0, h, GLColor::blue);
EXPECT_PIXEL_COLOR_EQ(w, h, GLColor::yellow);
// Generate mipmaps
glGenerateMipmap(GL_TEXTURE_2D);
// Draw a quad using the texture
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(m2DProgram);
glUniform1i(mTexture2DUniformLocation, 0);
drawQuad(m2DProgram, "position", 0.5f);
ASSERT_GL_NO_ERROR();
// Check that it drew as expected
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(w, 0, GLColor::green);
EXPECT_PIXEL_COLOR_EQ(0, h, GLColor::blue);
EXPECT_PIXEL_COLOR_EQ(w, h, GLColor::yellow);
// Bind the texture as a framebuffer, render to it, then check the results
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_UNSUPPORTED)
{
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(0, 0, 255, 0, 0, 255);
EXPECT_PIXEL_EQ(1, 0, 255, 0, 0, 255);
EXPECT_PIXEL_EQ(1, 1, 255, 0, 0, 255);
EXPECT_PIXEL_EQ(0, 1, 255, 0, 0, 255);
}
else
{
std::cout << "Skipping rendering to an unsupported framebuffer format" << std::endl;
}
}
GLuint m2DProgram;
GLint mTexture2DUniformLocation;
};
class SixteenBppTextureTestES3 : public SixteenBppTextureTest
{};
// Simple validation test for GL_RGB565 textures.
// Samples from the texture, renders to it, generates mipmaps etc.
TEST_P(SixteenBppTextureTest, RGB565Validation)
{
GLuint test;
memcpy(&test, &GLColor::black, 4);
R5G6B5 pixels[4] = {Convert565(GLColor::red), Convert565(GLColor::green),
Convert565(GLColor::blue), Convert565(GLColor::yellow)};
glClearColor(0, 0, 0, 0);
// Create a simple RGB565 texture
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
EXPECT_GL_NO_ERROR();
// Supply the data to it
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, pixels);
EXPECT_GL_NO_ERROR();
simpleValidationBase(tex.get());
}
// Simple validation test for GL_RGBA5551 textures.
// Samples from the texture, renders to it, generates mipmaps etc.
TEST_P(SixteenBppTextureTest, RGBA5551Validation)
{
GLushort pixels[4] = {
0xF801, // Red
0x07C1, // Green
0x003F, // Blue
0xFFC1 // Red + Green
};
// Create a simple 5551 texture
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
EXPECT_GL_NO_ERROR();
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, pixels);
EXPECT_GL_NO_ERROR();
simpleValidationBase(tex.get());
}
// Test to ensure calling Clear() on an RGBA5551 texture does something reasonable
// Based on WebGL test conformance/textures/texture-attachment-formats.html
TEST_P(SixteenBppTextureTest, RGBA5551ClearAlpha)
{
GLTexture tex;
GLFramebuffer fbo;
// Create a simple 5551 texture
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
EXPECT_GL_NO_ERROR();
// Bind the texture as a framebuffer, clear it, then check the results
glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
glBindTexture(GL_TEXTURE_2D, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.get(), 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_UNSUPPORTED)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(0, 0, 0, 0, 0, 0);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(0, 0, 0, 0, 0, 255);
}
else
{
std::cout << "Skipping rendering to an unsupported framebuffer format" << std::endl;
}
}
// Simple validation test for GL_RGBA4444 textures.
// Samples from the texture, renders to it, generates mipmaps etc.
TEST_P(SixteenBppTextureTest, RGBA4444Validation)
{
GLushort pixels[4] = {
0xF00F, // Red
0x0F0F, // Green
0x00FF, // Blue
0xFF0F // Red + Green
};
glClearColor(0, 0, 0, 0);
// Generate a RGBA4444 texture, no mipmaps
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
EXPECT_GL_NO_ERROR();
// Provide some data for the texture
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 2, 2, GL_RGBA, GL_UNSIGNED_SHORT_4_4_4_4, pixels);
EXPECT_GL_NO_ERROR();
simpleValidationBase(tex.get());
}
// Test uploading RGBA8 data to RGBA4 textures.
TEST_P(SixteenBppTextureTestES3, RGBA4UploadRGBA8)
{
const std::array<GLColor, 4> kFourColors = {
{GLColor::red, GLColor::green, GLColor::blue, GLColor::yellow}};
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
kFourColors.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
ASSERT_GL_NO_ERROR();
simpleValidationBase(tex.get());
}
// Test uploading RGB8 data to RGB565 textures.
TEST_P(SixteenBppTextureTestES3, RGB565UploadRGB8)
{
std::vector<GLColorRGB> fourColors;
fourColors.push_back(GLColorRGB::red);
fourColors.push_back(GLColorRGB::green);
fourColors.push_back(GLColorRGB::blue);
fourColors.push_back(GLColorRGB::yellow);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB565, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, fourColors.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
ASSERT_GL_NO_ERROR();
simpleValidationBase(tex.get());
}
// Test uploading RGBA8 data to RGB5A41 textures.
TEST_P(SixteenBppTextureTestES3, RGB5A1UploadRGBA8)
{
std::vector<GLColor> fourColors;
fourColors.push_back(GLColor::red);
fourColors.push_back(GLColor::green);
fourColors.push_back(GLColor::blue);
fourColors.push_back(GLColor::yellow);
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
fourColors.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
ASSERT_GL_NO_ERROR();
simpleValidationBase(tex.get());
}
// Test uploading RGB10A2 data to RGB5A1 textures.
TEST_P(SixteenBppTextureTestES3, RGB5A1UploadRGB10A2)
{
struct RGB10A2
{
RGB10A2(uint32_t r, uint32_t g, uint32_t b, uint32_t a) : R(r), G(g), B(b), A(a) {}
uint32_t R : 10;
uint32_t G : 10;
uint32_t B : 10;
uint32_t A : 2;
};
uint32_t one10 = (1u << 10u) - 1u;
RGB10A2 red(one10, 0u, 0u, 0x3u);
RGB10A2 green(0u, one10, 0u, 0x3u);
RGB10A2 blue(0u, 0u, one10, 0x3u);
RGB10A2 yellow(one10, one10, 0u, 0x3u);
std::vector<RGB10A2> fourColors;
fourColors.push_back(red);
fourColors.push_back(green);
fourColors.push_back(blue);
fourColors.push_back(yellow);
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB5_A1, 2, 2, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV,
fourColors.data());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
ASSERT_GL_NO_ERROR();
simpleValidationBase(tex.get());
}
// Test reading from RGBA4 textures attached to FBO.
TEST_P(SixteenBppTextureTestES3, RGBA4FramebufferReadback)
{
// TODO(jmadill): Fix bug with GLES
ANGLE_SKIP_TEST_IF(IsOpenGLES());
Vector4 rawColor(0.5f, 0.7f, 1.0f, 0.0f);
GLColor expectedColor(rawColor);
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA4, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.get(), 0);
glClearColor(rawColor.x(), rawColor.y(), rawColor.z(), rawColor.w());
glClear(GL_COLOR_BUFFER_BIT);
ASSERT_GL_NO_ERROR();
GLenum colorReadFormat = GL_NONE;
GLenum colorReadType = GL_NONE;
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, reinterpret_cast<GLint *>(&colorReadFormat));
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, reinterpret_cast<GLint *>(&colorReadType));
if (colorReadFormat == GL_RGBA && colorReadType == GL_UNSIGNED_BYTE)
{
GLColor actualColor = GLColor::black;
glReadPixels(0, 0, 1, 1, colorReadFormat, colorReadType, &actualColor);
EXPECT_COLOR_NEAR(expectedColor, actualColor, 20);
}
else
{
ASSERT_GLENUM_EQ(GL_RGBA, colorReadFormat);
ASSERT_TRUE(colorReadType == GL_UNSIGNED_SHORT_4_4_4_4_REV_EXT ||
colorReadType == GL_UNSIGNED_SHORT_4_4_4_4);
uint16_t rgba = 0;
glReadPixels(0, 0, 1, 1, colorReadFormat, colorReadType, &rgba);
GLubyte r8 = static_cast<GLubyte>((rgba & 0xF000) >> 12);
GLubyte g8 = static_cast<GLubyte>((rgba & 0x0F00) >> 8);
GLubyte b8 = static_cast<GLubyte>((rgba & 0x00F0) >> 4);
GLubyte a8 = static_cast<GLubyte>((rgba & 0x000F));
GLColor actualColor(r8 << 4, g8 << 4, b8 << 4, a8 << 4);
EXPECT_COLOR_NEAR(expectedColor, actualColor, 20);
}
ASSERT_GL_NO_ERROR();
}
// Test reading from RGB565 textures attached to FBO.
TEST_P(SixteenBppTextureTestES3, RGB565FramebufferReadback)
{
// TODO(jmadill): Fix bug with GLES
ANGLE_SKIP_TEST_IF(IsOpenGLES());
GLFramebuffer fbo;
glBindFramebuffer(GL_FRAMEBUFFER, fbo.get());
std::vector<GLColor> fourColors;
fourColors.push_back(GLColor::red);
fourColors.push_back(GLColor::green);
fourColors.push_back(GLColor::blue);
fourColors.push_back(GLColor::white);
constexpr char kVS[] =
"#version 300 es\n"
"in vec4 color;\n"
"in vec2 position;\n"
"out vec4 fcolor;\n"
"void main() {\n"
" fcolor = color;\n"
" gl_Position = vec4(position, 0.5, 1.0);\n"
"}";
constexpr char kFS[] =
"#version 300 es\n"
"in mediump vec4 fcolor;\n"
"out mediump vec4 color;\n"
"void main() {\n"
" color = fcolor;\n"
"}";
GLuint program = CompileProgram(kVS, kFS);
glUseProgram(program);
GLint colorLocation = glGetAttribLocation(program, "color");
ASSERT_NE(-1, colorLocation);
glEnableVertexAttribArray(colorLocation);
glVertexAttribPointer(colorLocation, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, fourColors.data());
int w = getWindowWidth();
int h = getWindowHeight();
glViewport(0, 0, w, h);
GLTexture tex;
glBindTexture(GL_TEXTURE_2D, tex.get());
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB565, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, tex.get(), 0);
drawIndexedQuad(program, "position", 0.5f);
ASSERT_GL_NO_ERROR();
int t = 12;
EXPECT_PIXEL_COLOR_NEAR(0, h - 1, GLColor::red, t);
EXPECT_PIXEL_COLOR_NEAR(0, 0, GLColor::green, t);
EXPECT_PIXEL_COLOR_NEAR(w - 1, 0, GLColor::blue, t);
EXPECT_PIXEL_COLOR_NEAR(w - 1, h - 1, GLColor::white, t);
GLenum colorReadFormat = GL_NONE;
GLenum colorReadType = GL_NONE;
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_FORMAT, reinterpret_cast<GLint *>(&colorReadFormat));
glGetIntegerv(GL_IMPLEMENTATION_COLOR_READ_TYPE, reinterpret_cast<GLint *>(&colorReadType));
if (colorReadFormat == GL_RGB && colorReadType == GL_UNSIGNED_SHORT_5_6_5)
{
std::vector<R5G6B5> readColors(w * h);
glReadPixels(0, 0, w, h, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, readColors.data());
int hoffset = (h - 1) * w;
EXPECT_COLOR_NEAR(GLColor::red, Convert565(readColors[hoffset]), t);
EXPECT_COLOR_NEAR(GLColor::green, Convert565(readColors[0]), t);
EXPECT_COLOR_NEAR(GLColor::blue, Convert565(readColors[w - 1]), t);
EXPECT_COLOR_NEAR(GLColor::white, Convert565(readColors[w - 1 + hoffset]), t);
}
glDeleteProgram(program);
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these
// tests should be run against.
ANGLE_INSTANTIATE_TEST(SixteenBppTextureTest,
ES2_D3D9(),
ES2_D3D11(),
ES2_D3D11_FL9_3(),
ES2_OPENGL(),
ES2_OPENGLES(),
ES2_VULKAN());
ANGLE_INSTANTIATE_TEST(SixteenBppTextureTestES3, ES3_D3D11(), ES3_OPENGL(), ES3_OPENGLES());
} // namespace