Hash :
18091004
Author :
Date :
2025-06-19T16:50:58
GLES1: Skip updating vertex attrib array when binding zero texture Previously, when usecases bind the zero texture, the vertex attrib array was disabled. This made the following bound textures lose the vertex attrib data. In this change, updating vertex attrib array in prepareForDraw() is skipped when the application binds zero texture but does not activate the corresponding texture unit. Also GLES1 tests using zero texture are added to make sure vertex attrib array is enabled correctly. Test: angle_end2end_tests --gtest_filter=DrawTextureTest.* Bug: b/426740680 Change-Id: Ic5aad0a60c9d987edcb1d05cdb6b68dd93eac309 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6659335 Reviewed-by: Cody Northrop <cnorthrop@google.com> Reviewed-by: Amirali Abdolrashidi <abdolrashidi@google.com> Commit-Queue: Amirali Abdolrashidi <abdolrashidi@google.com>
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 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
//
// Copyright 2018 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.
//
// DrawTextureTest.cpp: Tests basic usage of glDrawTex*.
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include <memory>
#include <vector>
using namespace angle;
class DrawTextureTest : public ANGLETest<>
{
protected:
DrawTextureTest()
{
setWindowWidth(kWindowWidth);
setWindowHeight(kWindowHeight);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
setConfigDepthBits(24);
}
void testSetUp() override
{
mTexture.reset(new GLTexture());
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, mTexture->get());
}
void testTearDown() override { mTexture.reset(); }
std::unique_ptr<GLTexture> mTexture;
static constexpr int kWindowWidth = 32;
static constexpr int kWindowHeight = 32;
};
// Negative test for invalid width/height values.
TEST_P(DrawTextureTest, NegativeValue)
{
glDrawTexiOES(0, 0, 0, 0, 0);
EXPECT_GL_ERROR(GL_INVALID_VALUE);
glDrawTexiOES(0, 0, 0, -1, 0);
EXPECT_GL_ERROR(GL_INVALID_VALUE);
glDrawTexiOES(0, 0, 0, 0, -1);
EXPECT_GL_ERROR(GL_INVALID_VALUE);
glDrawTexiOES(0, 0, 0, -1, -1);
EXPECT_GL_ERROR(GL_INVALID_VALUE);
}
// Basic draw.
TEST_P(DrawTextureTest, Basic)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GLColor::green);
glDrawTexiOES(0, 0, 0, 1, 1);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Tests that odd viewport dimensions are handled correctly.
// If the viewport dimension is even, then the incorrect way
// of getting the center screen coordinate by dividing by 2 and
// converting to integer will work in that case, but not if
// the viewport dimension is odd.
TEST_P(DrawTextureTest, CorrectNdcForOddViewportDimensions)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// clang-format off
std::array<GLColor, 2> textureData = {
GLColor::green, GLColor::green
};
// clang-format on
glViewport(0, 0, 3, 3);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData.data());
GLint cropRect[] = {0, 0, 2, 1};
glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, cropRect);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
EXPECT_GL_NO_ERROR();
GLint x = 1;
GLint y = 1;
glDrawTexiOES(x, y, 0, 2, 1);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(x, y, GLColor::green);
EXPECT_PIXEL_COLOR_EQ(x + 1, y, GLColor::green);
EXPECT_PIXEL_COLOR_EQ(x, y + 1, GLColor::black);
EXPECT_PIXEL_COLOR_EQ(x + 1, y + 1, GLColor::black);
EXPECT_PIXEL_COLOR_EQ(x + 2, y, GLColor::black);
EXPECT_PIXEL_COLOR_EQ(x + 3, y, GLColor::black);
}
// Tests that vertex attributes enabled with fewer than 6 verts do not cause a crash.
TEST_P(DrawTextureTest, VertexAttributesNoCrash)
{
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, 0, &GLColor::white);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GLColor::green);
EXPECT_GL_NO_ERROR();
glDrawTexiOES(0, 0, 0, 1, 1);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Tests that the color array, if enabled, is not used as the vertex color.
TEST_P(DrawTextureTest, ColorArrayNotUsed)
{
glEnableClientState(GL_COLOR_ARRAY);
// This color is set to black on purpose to ensure that the color in the upcoming vertex array
// is not used in the texture draw. If it is used, then the texture we want to read will be
// modulated with the color in the vertex array instead of GL_CURRENT_COLOR (which at the moment
// is white (1.0, 1.0, 1.0, 1.0).
glColorPointer(4, GL_FLOAT, 0, &GLColor::black);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GLColor::green);
EXPECT_GL_NO_ERROR();
glDrawTexiOES(0, 0, 0, 1, 1);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Tests that values of differenty types are properly normalized with glColorPointer
TEST_P(DrawTextureTest, ColorArrayDifferentTypes)
{
constexpr GLubyte kTextureColorData[] = {0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF,
0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF, 0x00, 0xFF};
constexpr GLfloat kVertexPtrData[] = {-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f};
constexpr GLfloat kTexCoordPtrData[] = {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f};
constexpr GLubyte kGLubyteData[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
constexpr GLfloat kGLfloatData[] = {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0};
constexpr GLfixed kGLfixedData[] = {0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000,
0x10000, 0x10000, 0x10000, 0x10000, 0x10000, 0x10000,
0x10000, 0x10000, 0x10000, 0x10000};
// We check a pixel coordinate at the border of where linear interpolation starts as
// we fail to get correct interpolated values when we do not normalize the GLbyte values.
constexpr GLint kCheckedPixelX = 16;
constexpr GLint kCheckedPixelY = 8;
constexpr unsigned int kPixelTolerance = 10u;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, kTextureColorData);
glVertexPointer(2, GL_FLOAT, 0, kVertexPtrData);
glTexCoordPointer(2, GL_FLOAT, 0, kTexCoordPtrData);
// Ensure the results do not change unexpectedly regardless of the color data format
// Test GLubyte
glColorPointer(4, GL_UNSIGNED_BYTE, 0, kGLubyteData);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_NEAR(kCheckedPixelX, kCheckedPixelY, GLColor::red, kPixelTolerance);
// Test GLfloat
glColorPointer(4, GL_FLOAT, 0, kGLfloatData);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_NEAR(kCheckedPixelX, kCheckedPixelY, GLColor::red, kPixelTolerance);
// Test GLfixed
glColorPointer(4, GL_FIXED, 0, kGLfixedData);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_NEAR(kCheckedPixelX, kCheckedPixelY, GLColor::red, kPixelTolerance);
}
// Tests that drawing a primitive works with enabled tex coord pointer, but with texture disabled.
TEST_P(DrawTextureTest, DrawWithTexCoordPtrDataAndDisabledTexture2D)
{
std::vector<GLfloat> vertexPtrData = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f};
std::vector<GLfloat> texCoordPtrData = {0.0f};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Draw a triangle fan that covers the entire window. GL_TEXTURE_2D is disabled even though
// texture coord pointer is set.
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData.data());
glTexCoordPointer(2, GL_FLOAT, 0, texCoordPtrData.data());
glDisable(GL_TEXTURE_2D);
glColor4ub(0, 0xFF, 0xFF, 0xFF);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth, kWindowHeight, GLColor::cyan);
}
// Tests that drawing a primitive works with enabled tex coord pointer and texture environment set
// so the used texture replaces the current color.
TEST_P(DrawTextureTest, DrawWithTexCoordPtrDataAndEnvModeReplace)
{
std::vector<GLfloat> vertexPtrData = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f};
std::vector<GLfloat> texCoordPtrData = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// Set up the texture. By default, the texture is multiplied by the current color set through
// glColor calls. Here the environment is set so it would replace the color instead.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_RGBA, GL_UNSIGNED_BYTE, &GLColor::red);
glTexEnvx(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
// Draw a triangle fan that covers the entire window. The texture should replace the color
// regardless of the value of the current color.
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData.data());
glTexCoordPointer(2, GL_FLOAT, 0, texCoordPtrData.data());
glColor4f(0.0f, 0.0f, 0.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth, kWindowHeight, GLColor::red);
}
// Tests that tex coord pointer is only used when texture is enabled, and that it is possible to
// disable the texture and draw another primitive by setting a color without using the texture data.
TEST_P(DrawTextureTest, DrawWithTexCoordPtrThenDisableTexture2DAndDrawAnother)
{
// There will be two vertex arrays for triangle fan draws. Both cover the entire screen, but the
// one with no texture uses more vertices.
std::vector<GLfloat> vertexPtrDataWithTexture = {-1.0f, -1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f, -1.0f};
std::vector<GLfloat> vertexPtrDataNoTexture = {-1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f};
std::vector<GLfloat> texCoordPtrData = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f};
// Enable vertex and texture coordinate pointers. Also set up texture data with four colors; one
// color per corner.
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
constexpr GLColor kColorCornerBL = GLColor(0x11, 0x22, 0x33, 0xFF);
constexpr GLColor kColorCornerBR = GLColor(0x44, 0x55, 0x66, 0xFF);
constexpr GLColor kColorCornerTL = GLColor(0x77, 0x88, 0x99, 0xFF);
constexpr GLColor kColorCornerTR = GLColor(0xAA, 0xBB, 0xCC, 0xFF);
std::vector<GLColor> textureData;
textureData.push_back(kColorCornerBL);
textureData.push_back(kColorCornerBR);
textureData.push_back(kColorCornerTL);
textureData.push_back(kColorCornerTR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData.data());
// Draw the first triangle fan using texture coord pointer.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glVertexPointer(2, GL_FLOAT, 0, vertexPtrDataWithTexture.data());
glTexCoordPointer(2, GL_FLOAT, 0, texCoordPtrData.data());
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, kColorCornerBL);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, kColorCornerBR);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2, kColorCornerTL);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
kColorCornerTR);
// Disable texture and draw the second triangle fan. This time, the draw uses more vertex
// coordinates and a preset color. Note that the texture coord pointer must no longer be used.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);
glVertexPointer(2, GL_FLOAT, 0, vertexPtrDataNoTexture.data());
glColor4ub(0, 0, 0xFF, 0xFF);
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth, kWindowHeight, GLColor::blue);
// Re-enable texture and draw using the same vertex pointer. This is to make sure that enabling
// GL_TEXTURE_2D is enough to use the texture data.
// There is no need to set the texture coord pointer again. However, since GL_TEXTURE_ENV_MODE
// is set to the default GL_MODULATE, the effect of glColor4ub() from before should be reversed
// by resetting the color to the default (1, 1, 1, 1).
// In addition, since the tex coord pointer is not defined for the current vertex pointer, the
// texture colors will shift position to be mapped to the new primitive, which will only cover
// the top-left half of the window.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_COLOR_EQ(0, kWindowHeight / 4, kColorCornerBL);
EXPECT_PIXEL_COLOR_EQ(kWindowWidth * 3 / 4, kWindowHeight - 1, kColorCornerBR);
EXPECT_PIXEL_COLOR_EQ(0, kWindowHeight * 3 / 4, kColorCornerTL);
EXPECT_PIXEL_COLOR_EQ(kWindowWidth / 2, kWindowHeight - 1, kColorCornerTR);
// Update the vertex pointer to the original and draw a final triangle fan.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glVertexPointer(2, GL_FLOAT, 0, vertexPtrDataWithTexture.data());
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, kColorCornerBL);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, kColorCornerBR);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2, kColorCornerTL);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
kColorCornerTR);
}
// Tests that vertex and tex coord pointers are used correctly.
// Draw same non-zero texture twice with same vertex and tex coord pointers, but do not disable
// texture.
TEST_P(DrawTextureTest, DrawNonZeroSameTexWithSameVertexPtrWithoutDisabledTexture2D)
{
// Set up texture with four colors; one color per corner to verify if the texture will be
// uploaded with appropriate vertex attributes.
std::vector<GLColor> textureColors = {GLColor::red, GLColor::yellow, GLColor::green,
GLColor::blue};
GLTexture tex2D;
glBindTexture(GL_TEXTURE_2D, tex2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
textureColors.data());
// Set up vertex and texture coordinate pointers.
std::vector<GLfloat> vertexPtrData = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f};
std::vector<GLfloat> texCoordPtrData = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f};
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData.data());
glTexCoordPointer(2, GL_FLOAT, 0, texCoordPtrData.data());
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Bind tex2D and draw.
// -----------------------
// | Green | Blue |
// | Red | Yellow |
// -----------------------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex2D);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::red);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::yellow);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2, GLColor::green);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::blue);
// Bind tex2D and draw again. Expect the vertex attributes remain.
// -----------------------
// | Green | Blue |
// | Red | Yellow |
// -----------------------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex2D);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::red);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::yellow);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2, GLColor::green);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::blue);
}
// Tests that vertex and tex coord pointers are used correctly.
// Draw same non-zero texture twice with different vertex pointer, but do not disable texture.
TEST_P(DrawTextureTest, DrawNonZeroSameTexWithDifferentVertexPtrWithoutDisabledTexture2D)
{
// Set up texture with four colors; one color per corner to verify if the texture will be
// uploaded with appropriate vertex attributes.
std::vector<GLColor> textureColors = {GLColor::red, GLColor::yellow, GLColor::green,
GLColor::blue};
GLTexture tex2D;
glBindTexture(GL_TEXTURE_2D, tex2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
textureColors.data());
// Set up vertex and texture coordinate pointers.
std::vector<GLfloat> vertexPtrData_1 = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f};
std::vector<GLfloat> texCoordPtrData = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f};
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData_1.data());
glTexCoordPointer(2, GL_FLOAT, 0, texCoordPtrData.data());
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Bind tex2D and draw.
// -----------------------
// | Green | Blue |
// | Red | Yellow |
// -----------------------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex2D);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::red);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::yellow);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2, GLColor::green);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::blue);
// Change vertex data.
std::vector<GLfloat> vertexPtrData_2 = {-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f};
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData_2.data());
// Bind tex2D and draw.
// -----------------------
// | Yellow | Blue |
// | Red | Green |
// -----------------------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex2D);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::red);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::green);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::yellow);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::blue);
}
// Test that vertex and tex coord pointers are used correctly.
// Draw two different non-zero textures with different vertex pointer, but do not disable texture.
TEST_P(DrawTextureTest, DrawNonZeroTwoTexWithDifferentVertexPtrWithoutDisableTexture2D)
{
// Set up texture with four colors; one color per corner to verify if the texture will be
// uploaded with appropriate vertex attributes.
std::vector<GLColor> textureColors_1 = {GLColor::red, GLColor::yellow, GLColor::green,
GLColor::blue};
GLTexture tex2D_1;
glBindTexture(GL_TEXTURE_2D, tex2D_1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
textureColors_1.data());
// Set up vertex and texture coordinate pointers.
std::vector<GLfloat> vertexPtrData_1 = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f};
std::vector<GLfloat> texCoordPtrData = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f};
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData_1.data());
glTexCoordPointer(2, GL_FLOAT, 0, texCoordPtrData.data());
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Bind tex2D and draw.
// -----------------------
// | Green | Blue |
// | Red | Yellow |
// -----------------------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::red);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::yellow);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2, GLColor::green);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::blue);
// Change texture to bind.
std::vector<GLColor> textureColors_2 = {GLColor::magenta, GLColor::transparentBlack,
GLColor::white, GLColor::cyan};
GLTexture tex2D_2;
glBindTexture(GL_TEXTURE_2D, tex2D_2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
textureColors_2.data());
// Change vertex data.
std::vector<GLfloat> vertexPtrData_2 = {-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f};
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData_2.data());
// Bind tex2D and draw.
// -----------------------------------
// | TransparentBlack | Cyan |
// | Magenta | White |
// -----------------------------------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::magenta);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::white);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::transparentBlack);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::cyan);
}
// Tests that vertex and tex coord pointers are used correctly.
// Draw zero texture first then draw tex2D with same vertex and tex coord pointers, but do not
// disable texture.
TEST_P(DrawTextureTest, DrawZeroTexWithSameVertexPtrWithoutDisableTexture2D)
{
// Set up texture with four colors; one color per corner to verify if the texture will be
// uploaded with appropriate vertex attributes.
std::vector<GLColor> textureColors = {GLColor::red, GLColor::yellow, GLColor::green,
GLColor::blue};
GLTexture tex2D;
glBindTexture(GL_TEXTURE_2D, tex2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
textureColors.data());
// Set up vertex and texture coordinate pointers.
std::vector<GLfloat> vertexPtrData = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f};
std::vector<GLfloat> texCoordPtrData = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f};
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData.data());
glTexCoordPointer(2, GL_FLOAT, 0, texCoordPtrData.data());
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Bind zero texture and draw.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, 0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
// Bind tex2D and draw. Expect the vertex attributes remain.
// -----------------------
// | Green | Blue |
// | Red | Yellow |
// -----------------------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex2D);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::red);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::yellow);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2, GLColor::green);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::blue);
}
// Tests that vertex and tex coord pointers are used correctly.
// Draw zero texture first then draw tex2D with different vertex pointers, but do not disable
// texture.
TEST_P(DrawTextureTest, DrawZeroTexWithDifferentVertexPtrWithoutDisableTexture2D)
{
// Set up texture with four colors; one color per corner to verify if the texture will be
// uploaded with appropriate vertex attributes.
std::vector<GLColor> textureColors = {GLColor::red, GLColor::yellow, GLColor::green,
GLColor::blue};
GLTexture tex2D;
glBindTexture(GL_TEXTURE_2D, tex2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE,
textureColors.data());
// Set up vertex and texture coordinate pointers.
std::vector<GLfloat> vertexPtrData_1 = {-1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f};
std::vector<GLfloat> texCoordPtrData = {0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f};
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData_1.data());
glTexCoordPointer(2, GL_FLOAT, 0, texCoordPtrData.data());
glEnable(GL_TEXTURE_2D);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Bind zero texture and draw.
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, 0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
// Change vertex data.
std::vector<GLfloat> vertexPtrData_2 = {-1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f};
glVertexPointer(2, GL_FLOAT, 0, vertexPtrData_2.data());
// Bind tex2D and draw.
// -----------------------
// | Yellow | Blue |
// | Red | Green |
// -----------------------
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex2D);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::red);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, 0, kWindowWidth / 2, kWindowHeight / 2, GLColor::green);
EXPECT_PIXEL_RECT_EQ(0, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::yellow);
EXPECT_PIXEL_RECT_EQ(kWindowWidth / 2, kWindowHeight / 2, kWindowWidth / 2, kWindowHeight / 2,
GLColor::blue);
}
ANGLE_INSTANTIATE_TEST_ES1(DrawTextureTest);