Hash :
1bfe5c57
Author :
Date :
2024-07-30T15:58:47
Metal: partially implement EXT_multisampled_render_to_texture. Implement by implicitly attaching a multisampled texture to the render pass. The content will be preserved across render passes by loading/storing to the implicit multisampled texture. However this won't work if the single sampled texture is used in multiple render passes with different glFramebufferTexture2DMultisampleEXT's sample counts. For that to work we need to implement unresolve step to load the resolve texture's texels into the implicit multisampled texture. That will be implemented in a separate CL. Bug: angleproject:42261786 Change-Id: I12be75af17ce5b98266946846417d0a43fcba455 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5746180 Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Quyen Le <lehoangquyen@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 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 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
//
// Copyright 2019 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.
//
// MultisampleTest: Tests of multisampled default framebuffer
#include "test_utils/ANGLETest.h"
#include "test_utils/gl_raii.h"
#include "util/OSWindow.h"
#include "util/shader_utils.h"
#include "util/test_utils.h"
using namespace angle;
namespace
{
class MultisampleTest : public ANGLETest<>
{
protected:
MultisampleTest()
{
setWindowWidth(kWindowWidth);
setWindowHeight(kWindowHeight);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
setConfigDepthBits(24);
setConfigStencilBits(8);
setSamples(4);
setMultisampleEnabled(true);
}
void prepareVertexBuffer(GLBuffer &vertexBuffer,
const Vector3 *vertices,
size_t vertexCount,
GLint positionLocation)
{
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(*vertices) * vertexCount, vertices, GL_STATIC_DRAW);
glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnableVertexAttribArray(positionLocation);
}
protected:
static constexpr int kWindowWidth = 16;
static constexpr int kWindowHeight = 8;
};
class MultisampleTestES3 : public MultisampleTest
{};
class MultisampleTestES32 : public MultisampleTest
{};
// Test point rendering on a multisampled surface. GLES2 section 3.3.1.
TEST_P(MultisampleTest, Point)
{
// http://anglebug.com/42262135
ANGLE_SKIP_TEST_IF(IsAndroid() && IsNVIDIAShield() && IsOpenGLES());
// http://anglebug.com/42264264
ANGLE_SKIP_TEST_IF(IsOzone());
constexpr char kPointsVS[] = R"(precision highp float;
attribute vec4 a_position;
void main()
{
gl_PointSize = 3.0;
gl_Position = a_position;
})";
ANGLE_GL_PROGRAM(program, kPointsVS, essl1_shaders::fs::Red());
glUseProgram(program);
const GLint positionLocation = glGetAttribLocation(program, "a_position");
GLBuffer vertexBuffer;
const Vector3 vertices[1] = {{0.0f, 0.0f, 0.0f}};
prepareVertexBuffer(vertexBuffer, vertices, 1, positionLocation);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_POINTS, 0, 1);
ASSERT_GL_NO_ERROR();
// The center pixels should be all red.
EXPECT_PIXEL_COLOR_EQ(kWindowWidth / 2, kWindowHeight / 2, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(kWindowWidth / 2 - 1, kWindowHeight / 2, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(kWindowWidth / 2, kWindowHeight / 2 - 1, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(kWindowWidth / 2 - 1, kWindowHeight / 2 - 1, GLColor::red);
// Border pixels should be between red and black, and not exactly either; corners are darker and
// sides are brighter.
const GLColor kSideColor = {128, 0, 0, 128};
const GLColor kCornerColor = {64, 0, 0, 64};
constexpr int kErrorMargin = 16;
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 - 2, kWindowHeight / 2 - 2, kCornerColor,
kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 - 2, kWindowHeight / 2 + 1, kCornerColor,
kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 + 1, kWindowHeight / 2 - 2, kCornerColor,
kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 + 1, kWindowHeight / 2 + 1, kCornerColor,
kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 - 2, kWindowHeight / 2 - 1, kSideColor, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 - 2, kWindowHeight / 2, kSideColor, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 - 1, kWindowHeight / 2 - 2, kSideColor, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 - 1, kWindowHeight / 2 + 1, kSideColor, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2, kWindowHeight / 2 - 2, kSideColor, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2, kWindowHeight / 2 + 1, kSideColor, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 + 1, kWindowHeight / 2 - 1, kSideColor, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2 + 1, kWindowHeight / 2, kSideColor, kErrorMargin);
}
// Test line rendering on a multisampled surface. GLES2 section 3.4.4.
TEST_P(MultisampleTest, Line)
{
ANGLE_SKIP_TEST_IF(IsARM64() && IsWindows() && IsD3D());
// http://anglebug.com/42264264
ANGLE_SKIP_TEST_IF(IsOzone());
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
glUseProgram(program);
const GLint positionLocation = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
GLBuffer vertexBuffer;
const Vector3 vertices[2] = {{-1.0f, -0.3f, 0.0f}, {1.0f, 0.3f, 0.0f}};
prepareVertexBuffer(vertexBuffer, vertices, 2, positionLocation);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_LINES, 0, 2);
ASSERT_GL_NO_ERROR();
// The line goes from left to right at about -17 degrees slope. It renders as such (captured
// with renderdoc):
//
// D D = Dark Red (0.25) or (0.5)
// BRA R = Red (1.0)
// ARB M = Middle Red (0.75)
// D B = Bright Red (1.0 or 0.75)
// A = Any red (0.5, 0.75 or 1.0)
//
// Verify that rendering is done as above.
const GLColor kDarkRed = {128, 0, 0, 128};
const GLColor kMidRed = {192, 0, 0, 192};
constexpr int kErrorMargin = 16;
constexpr int kLargeMargin = 80;
static_assert(kWindowWidth == 16, "Verification code written for 16x8 window");
EXPECT_PIXEL_COLOR_NEAR(0, 2, kDarkRed, kLargeMargin);
EXPECT_PIXEL_COLOR_NEAR(3, 3, GLColor::red, kLargeMargin);
EXPECT_PIXEL_COLOR_NEAR(4, 3, GLColor::red, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(6, 3, kMidRed, kLargeMargin);
EXPECT_PIXEL_COLOR_NEAR(8, 4, kMidRed, kLargeMargin);
EXPECT_PIXEL_COLOR_NEAR(11, 4, GLColor::red, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(12, 4, GLColor::red, kLargeMargin);
EXPECT_PIXEL_COLOR_NEAR(15, 5, kDarkRed, kLargeMargin);
}
// Test polygon rendering on a multisampled surface. GLES2 section 3.5.3.
TEST_P(MultisampleTest, Triangle)
{
// http://anglebug.com/42262135
ANGLE_SKIP_TEST_IF(IsAndroid() && IsNVIDIAShield() && IsOpenGLES());
// http://anglebug.com/42264264
ANGLE_SKIP_TEST_IF(IsOzone());
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
glUseProgram(program);
const GLint positionLocation = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
GLBuffer vertexBuffer;
const Vector3 vertices[3] = {{-1.0f, -1.0f, 0.0f}, {-1.0f, 1.0f, 0.0f}, {1.0f, -1.0f, 0.0f}};
prepareVertexBuffer(vertexBuffer, vertices, 3, positionLocation);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
ASSERT_GL_NO_ERROR();
// Top-left pixels should be all red.
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(kWindowWidth / 4, kWindowHeight / 4, GLColor::red);
// Diagonal pixels from bottom-left to top-right are between red and black. Pixels above the
// diagonal are red and pixels below it are black.
const GLColor kMidRed = {128, 0, 0, 128};
// D3D11 is off by 63 for red (191 instead of 128), where other back-ends get 128
constexpr int kErrorMargin = 64;
for (int i = 2; i + 2 < kWindowWidth; i += 2)
{
int j = kWindowHeight - 1 - (i / 2);
EXPECT_PIXEL_COLOR_NEAR(i, j, kMidRed, kErrorMargin);
EXPECT_PIXEL_COLOR_EQ(i, j - 1, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(i, j + 1, GLColor::transparentBlack);
}
}
// Test polygon rendering on a multisampled surface. And rendering is interrupted by a compute pass
// that converts the index buffer. Make sure the rendering's multisample result is preserved after
// interruption.
TEST_P(MultisampleTest, ContentPresevedAfterInterruption)
{
ANGLE_SKIP_TEST_IF(!IsGLExtensionEnabled("GL_OES_rgb8_rgba8"));
// http://anglebug.com/42262135
ANGLE_SKIP_TEST_IF(IsAndroid() && IsNVIDIAShield() && IsOpenGLES());
// http://anglebug.com/42263216
ANGLE_SKIP_TEST_IF(IsD3D11());
// http://anglebug.com/42264264
ANGLE_SKIP_TEST_IF(IsOzone());
ANGLE_GL_PROGRAM(program, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
glUseProgram(program);
const GLint positionLocation = glGetAttribLocation(program, essl1_shaders::PositionAttrib());
if (IsGLExtensionEnabled("GL_EXT_discard_framebuffer"))
{
GLenum attachments[] = {GL_COLOR_EXT, GL_DEPTH_EXT, GL_STENCIL_EXT};
glDiscardFramebufferEXT(GL_FRAMEBUFFER, 3, attachments);
}
// Draw triangle
GLBuffer vertexBuffer;
const Vector3 vertices[3] = {{-1.0f, -1.0f, 0.0f}, {-1.0f, 1.0f, 0.0f}, {1.0f, -1.0f, 0.0f}};
prepareVertexBuffer(vertexBuffer, vertices, 3, positionLocation);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
ASSERT_GL_NO_ERROR();
// Draw a line
GLBuffer vertexBuffer2;
GLBuffer indexBuffer2;
const Vector3 vertices2[2] = {{-1.0f, -0.3f, 0.0f}, {1.0f, 0.3f, 0.0f}};
const GLubyte indices2[] = {0, 1};
prepareVertexBuffer(vertexBuffer2, vertices2, 2, positionLocation);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer2);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices2), indices2, GL_STATIC_DRAW);
glDrawElements(GL_LINES, 2, GL_UNSIGNED_BYTE, 0);
ASSERT_GL_NO_ERROR();
// Top-left pixels should be all red.
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(kWindowWidth / 4, kWindowHeight / 4, GLColor::red);
// Triangle edge:
// Diagonal pixels from bottom-left to top-right are between red and black. Pixels above the
// diagonal are red and pixels below it are black.
{
const GLColor kMidRed = {128, 0, 0, 128};
constexpr int kErrorMargin = 16;
for (int i = 2; i + 2 < kWindowWidth; i += 2)
{
// Exclude the middle pixel where the triangle and line cross each other.
if (abs(kWindowHeight / 2 - (i / 2)) <= 1)
{
continue;
}
int j = kWindowHeight - 1 - (i / 2);
EXPECT_PIXEL_COLOR_NEAR(i, j, kMidRed, kErrorMargin);
EXPECT_PIXEL_COLOR_EQ(i, j - 1, GLColor::red);
EXPECT_PIXEL_COLOR_EQ(i, j + 1, GLColor::transparentBlack);
}
}
// Line edge:
{
const GLColor kDarkRed = {128, 0, 0, 128};
constexpr int kErrorMargin = 16;
constexpr int kLargeMargin = 80;
static_assert(kWindowWidth == 16, "Verification code written for 16x8 window");
// Exclude the triangle region.
EXPECT_PIXEL_COLOR_NEAR(11, 4, GLColor::red, kErrorMargin);
EXPECT_PIXEL_COLOR_NEAR(12, 4, GLColor::red, kLargeMargin);
EXPECT_PIXEL_COLOR_NEAR(15, 5, kDarkRed, kLargeMargin);
}
}
// Test that alpha to coverage is enabled works properly along with early fragment test.
TEST_P(MultisampleTest, AlphaToSampleCoverage)
{
// http://anglebug.com/42264264
ANGLE_SKIP_TEST_IF(IsOzone());
constexpr char kFS[] =
"precision highp float;\n"
"void main()\n"
"{\n"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 0.0);\n"
"}\n";
ANGLE_GL_PROGRAM(transparentRedProgram, essl1_shaders::vs::Simple(), kFS);
glUseProgram(transparentRedProgram);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearDepthf(1.0f);
glClearColor(0.0f, 1.0f, 0.0f, 1.0f); // clear to green
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// This should pass depth test, but because of the alpha to coverage enabled, and alpha is 0,
// the fragment should be discarded. If early fragment test is disabled, no depth will be
// written. depth buffer should be 1.0.
glEnable(GL_SAMPLE_ALPHA_TO_COVERAGE);
// There was a bug in ANGLE that we are checking sampler coverage enabled or not instead of
// alpha to sample coverage enabled or not. This is specically try to trick ANGLE so that it
// will enable early fragment test. When early fragment test is accidentally enabled, then the
// depth test will occur before fragment shader, and depth buffer maybe written with value
// (0.0+1.0)/2.0=0.5.
glEnable(GL_SAMPLE_COVERAGE);
drawQuad(transparentRedProgram, essl1_shaders::PositionAttrib(), 0.0f);
// Now draw with blue color but to test against 0.0f. This should fail depth test
glDisable(GL_SAMPLE_ALPHA_TO_COVERAGE);
glDisable(GL_SAMPLE_COVERAGE);
glDepthFunc(GL_GREATER);
ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
// Zd = 0.5f means (0.5+1.0)/2.0=0.75. Depends on early fragment on or off this will pass or
// fail depth test.
drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
EXPECT_PIXEL_COLOR_EQ(1, 1, GLColor::green);
ASSERT_GL_NO_ERROR();
}
// Test that resolve from multisample default framebuffer works.
TEST_P(MultisampleTestES3, ResolveToFBO)
{
GLTexture resolveTexture;
glBindTexture(GL_TEXTURE_2D, resolveTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kWindowWidth, kWindowHeight, 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLFramebuffer resolveFBO;
glBindFramebuffer(GL_FRAMEBUFFER, resolveFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, resolveTexture, 0);
// Clear the default framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClearColor(0.25, 0.5, 0.75, 0.25);
glClear(GL_COLOR_BUFFER_BIT);
// Resolve into FBO
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolveFBO);
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBlitFramebuffer(0, 0, kWindowWidth, kWindowHeight, 0, 0, kWindowWidth, kWindowHeight,
GL_COLOR_BUFFER_BIT, GL_NEAREST);
ASSERT_GL_NO_ERROR();
const GLColor kResult = GLColor(63, 127, 191, 63);
glBindFramebuffer(GL_READ_FRAMEBUFFER, resolveFBO);
EXPECT_PIXEL_COLOR_NEAR(0, 0, kResult, 1);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth - 1, 0, kResult, 1);
EXPECT_PIXEL_COLOR_NEAR(0, kWindowHeight - 1, kResult, 1);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth - 1, kWindowHeight - 1, kResult, 1);
EXPECT_PIXEL_COLOR_NEAR(kWindowWidth / 2, kWindowHeight / 2, kResult, 1);
}
// Test that resolve from multisample default framebuffer after an open render pass works when the
// framebuffer is also immediately implicitly resolved due to swap afterwards.
TEST_P(MultisampleTestES3, RenderPassResolveToFBOThenSwap)
{
GLTexture resolveTexture;
glBindTexture(GL_TEXTURE_2D, resolveTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kWindowWidth, kWindowHeight, 0, GL_RGBA,
GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLFramebuffer resolveFBO;
glBindFramebuffer(GL_FRAMEBUFFER, resolveFBO);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, resolveTexture, 0);
auto runTest = [&](bool flipY) {
// Open a render pass by drawing to the default framebuffer
glBindFramebuffer(GL_FRAMEBUFFER, 0);
ANGLE_GL_PROGRAM(red, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
drawQuad(red, essl1_shaders::PositionAttrib(), 0.5f);
// Resolve into FBO
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolveFBO);
if (flipY)
{
glFramebufferParameteriMESA(GL_DRAW_FRAMEBUFFER, GL_FRAMEBUFFER_FLIP_Y_MESA, 1);
}
glBlitFramebuffer(0, 0, kWindowWidth, kWindowHeight, 0, 0, kWindowWidth, kWindowHeight,
GL_COLOR_BUFFER_BIT, GL_NEAREST);
ASSERT_GL_NO_ERROR();
// Immediately swap so that an implicit resolve to the backbuffer happens right away.
swapBuffers();
glBindFramebuffer(GL_READ_FRAMEBUFFER, resolveFBO);
EXPECT_PIXEL_RECT_EQ(0, 0, kWindowWidth, kWindowHeight, GLColor::red);
};
runTest(false);
if (IsGLExtensionEnabled("GL_MESA_framebuffer_flip_y"))
{
// With multiple backends, the default framebuffer is flipped w.r.t GL's coordinates. As a
// result, the glBlitFramebuffer may need to take a different path from a direct multisample
// resolve. This test ensures a direct resolve is also tested where possible.
runTest(true);
}
}
class MultisampleResolveTest : public ANGLETest<>
{
protected:
static const GLColor kEXPECTED_R8;
static const GLColor kEXPECTED_RG8;
static const GLColor kEXPECTED_RGB8;
static const GLColor kEXPECTED_RGBA8;
static const GLColor32F kEXPECTED_RF;
static const GLColor32F kEXPECTED_RGF;
static const GLColor32F kEXPECTED_RGBF;
static const GLColor32F kEXPECTED_RGBAF;
static constexpr GLint kWidth = 13;
static constexpr GLint kHeight = 11;
MultisampleResolveTest() {}
struct GLResources
{
GLFramebuffer fb;
GLRenderbuffer rb;
};
void resolveToFBO(GLenum format,
GLint samples,
GLint width,
GLint height,
GLResources &resources)
{
constexpr char kVS[] = R"(#version 300 es
layout(location = 0) in vec4 position;
void main() {
gl_Position = position;
}
)";
constexpr char kFS[] = R"(#version 300 es
precision highp float;
out vec4 color;
void main() {
color = vec4(0.5, 0.6, 0.7, 0.8);
}
)";
ANGLE_GL_PROGRAM(program, kVS, kFS);
// Make samples = 4 multi-sample framebuffer.
GLFramebuffer fb0;
glBindFramebuffer(GL_FRAMEBUFFER, fb0);
GLRenderbuffer rb0;
glBindRenderbuffer(GL_RENDERBUFFER, rb0);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, format, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb0);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
// Make samples = 0 multi-sample framebuffer.
glBindFramebuffer(GL_FRAMEBUFFER, resources.fb);
glBindRenderbuffer(GL_RENDERBUFFER, resources.rb);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 0, format, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
resources.rb);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
// Draw quad to fb0.
glBindFramebuffer(GL_FRAMEBUFFER, fb0);
glViewport(0, 0, width, height);
glUseProgram(program);
GLBuffer buf;
glBindBuffer(GL_ARRAY_BUFFER, buf);
constexpr float vertices[] = {
-1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_TRIANGLES, 0, 6);
// Blit fb0 to fb1.
glBindFramebuffer(GL_READ_FRAMEBUFFER, fb0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resources.fb);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT,
GL_NEAREST);
ASSERT_GL_NO_ERROR();
// Prep for read pixels.
glBindFramebuffer(GL_READ_FRAMEBUFFER, resources.fb);
}
void testResolveToUNormFBO(GLenum format,
const GLColor &expected_color,
GLint samples,
GLint width,
GLint height)
{
GLResources resources;
resolveToFBO(format, samples, width, height, resources);
// Check the results
for (GLint y = 0; y < kHeight; ++y)
{
for (GLint x = 0; x < kWidth; ++x)
{
EXPECT_PIXEL_COLOR_NEAR(x, y, expected_color, 2);
}
}
ASSERT_GL_NO_ERROR();
}
void testResolveToHalfFBO(GLenum format,
const GLColor32F &expected_color,
GLint samples,
GLint width,
GLint height)
{
if (!IsGLExtensionEnabled("GL_EXT_color_buffer_half_float"))
{
return;
}
GLResources resources;
resolveToFBO(format, samples, width, height, resources);
// Check the results
for (GLint y = 0; y < kHeight; ++y)
{
for (GLint x = 0; x < kWidth; ++x)
{
EXPECT_PIXEL_COLOR32F_NEAR(x, y, expected_color, 2.0f / 255.0f);
}
}
ASSERT_GL_NO_ERROR();
}
void testResolveToFloatFBO(GLenum format,
const GLColor32F &expected_color,
GLint samples,
GLint width,
GLint height)
{
if (!IsGLExtensionEnabled("GL_CHROMIUM_color_buffer_float_rgba"))
{
return;
}
GLResources resources;
resolveToFBO(format, samples, width, height, resources);
// Check the results
for (GLint y = 0; y < kHeight; ++y)
{
for (GLint x = 0; x < kWidth; ++x)
{
EXPECT_PIXEL_COLOR32F_NEAR(x, y, expected_color, 2.0f / 255.0f);
}
}
ASSERT_GL_NO_ERROR();
}
void testResolveToRGBFloatFBO(GLenum format,
const GLColor32F &expected_color,
GLint samples,
GLint width,
GLint height)
{
if (!IsGLExtensionEnabled("GL_CHROMIUM_color_buffer_float_rgb"))
{
return;
}
GLResources resources;
resolveToFBO(format, samples, width, height, resources);
// Check the results
for (GLint y = 0; y < kHeight; ++y)
{
for (GLint x = 0; x < kWidth; ++x)
{
EXPECT_PIXEL_COLOR32F_NEAR(x, y, expected_color, 2.0f / 255.0f);
}
}
ASSERT_GL_NO_ERROR();
}
void peelDepth(GLint colorLoc)
{
// Draw full quads from front to back and increasing depths
// with depth test = less.
glDepthMask(GL_FALSE);
constexpr int steps = 64;
for (int i = 0; i < steps; ++i)
{
float l = float(i) / float(steps);
float c = l;
float z = c * 2.0f - 1.0f;
glVertexAttrib4f(1, 0, 0, z, 0);
glUniform4f(colorLoc, c, c, c, c);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
glDepthMask(GL_TRUE);
}
void testResolveDepthToFBO(GLenum format,
GLenum attachment,
GLint samples,
GLint width,
GLint height)
{
constexpr char kVS[] = R"(#version 300 es
layout(location = 0) in vec4 position;
void main() {
gl_Position = position;
}
)";
constexpr char kFS[] = R"(#version 300 es
precision highp float;
out vec4 color;
void main() {
color = vec4(0.5, 0.6, 0.7, 0.8);
}
)";
constexpr char kDepthVS[] = R"(#version 300 es
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 offset;
void main() {
gl_Position = position + offset;
}
)";
constexpr char kDepthFS[] = R"(#version 300 es
precision highp float;
uniform vec4 color;
out vec4 outColor;
void main() {
outColor = color;
}
)";
ANGLE_GL_PROGRAM(program, kVS, kFS);
ANGLE_GL_PROGRAM(depthProgram, kDepthVS, kDepthFS);
// Make samples = 4 multi-sample framebuffer.
GLFramebuffer fb0;
glBindFramebuffer(GL_FRAMEBUFFER, fb0);
GLRenderbuffer rb0;
glBindRenderbuffer(GL_RENDERBUFFER, rb0);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, GL_RGBA8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb0);
GLRenderbuffer db0;
glBindRenderbuffer(GL_RENDERBUFFER, db0);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, format, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, db0);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
// Make samples = 0 multi-sample framebuffer.
GLFramebuffer fb1;
glBindFramebuffer(GL_FRAMEBUFFER, fb1);
GLRenderbuffer rb1;
glBindRenderbuffer(GL_RENDERBUFFER, rb1);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, rb1);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
GLRenderbuffer db1;
glBindRenderbuffer(GL_RENDERBUFFER, db1);
glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, attachment, GL_RENDERBUFFER, db1);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
// Draw quad to fb0.
glBindFramebuffer(GL_FRAMEBUFFER, fb0);
glViewport(0, 0, width, height);
glClearColor(1, 1, 1, 1);
glUseProgram(program);
GLVertexArray va0;
glBindVertexArray(va0);
GLBuffer buf0;
glBindBuffer(GL_ARRAY_BUFFER, buf0);
// clang-format off
constexpr float vertices[] = {
-1.0f, -1.0f, -1.0,
1.0f, -1.0f, 0.0,
-1.0f, 1.0f, 0.0,
-1.0f, 1.0f, 0.0,
1.0f, -1.0f, 0.0,
1.0f, 1.0f, 1.0,
};
// clang-format on
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_ALWAYS);
glDrawArrays(GL_TRIANGLES, 0, 6);
// Blit fb0 to fb1.
glBindFramebuffer(GL_READ_FRAMEBUFFER, fb0);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb1);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_DEPTH_BUFFER_BIT,
GL_NEAREST);
ASSERT_GL_NO_ERROR();
GLVertexArray va1;
glBindVertexArray(va1);
// clang-format off
constexpr float depthVertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
-1.0f, 1.0f,
1.0f, -1.0f,
1.0f, 1.0f,
};
// clang-format on
GLBuffer buf1;
glBindBuffer(GL_ARRAY_BUFFER, buf1);
glBufferData(GL_ARRAY_BUFFER, sizeof(depthVertices), depthVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glUseProgram(depthProgram);
GLint colorLoc = glGetUniformLocation(depthProgram, "color");
// Extract the depth results.
glBindFramebuffer(GL_FRAMEBUFFER, fb1);
glClear(GL_COLOR_BUFFER_BIT);
glDepthFunc(GL_LESS);
peelDepth(colorLoc);
std::vector<GLColor> actual(width * height);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, actual.data());
// Render what should be a similar result to the non-multi-sampled fb
glBindVertexArray(va0);
glDepthFunc(GL_ALWAYS);
glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 6);
// Extract the expected depth results.
glBindVertexArray(va1);
glUseProgram(depthProgram);
glClear(GL_COLOR_BUFFER_BIT);
glDepthFunc(GL_LESS);
peelDepth(colorLoc);
std::vector<GLColor> expected(width * height);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, expected.data());
for (size_t i = 0; i < expected.size(); ++i)
{
EXPECT_NEAR(expected[i].R, actual[i].R, 8)
<< "at " << (i % width) << "," << (i / width);
}
// Verify we read the depth buffer.
const GLint minDimension = std::min(width, height);
for (GLint i = 1; i < minDimension; ++i)
{
const GLColor &c1 = expected[i - 1];
const GLColor &c2 = expected[i * width + i];
EXPECT_LT(c1.R, c2.R);
}
ASSERT_GL_NO_ERROR();
}
};
// Test the multisampled optimized resolve subpass
TEST_P(MultisampleResolveTest, DISABLED_ResolveSubpassMSImage)
{
ANGLE_GL_PROGRAM(greenProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Green());
// Draw green.
drawQuad(greenProgram, essl1_shaders::PositionAttrib(), 0.5f);
swapBuffers();
// Wait for visual verification.
angle::Sleep(2000);
}
// This is a test that must be verified visually.
//
// Tests that clear of the default framebuffer with multisample applies to the window.
TEST_P(MultisampleTestES3, DISABLED_ClearMSAAReachesWindow)
{
ANGLE_GL_PROGRAM(blueProgram, essl1_shaders::vs::Simple(), essl1_shaders::fs::Blue());
// Draw blue.
drawQuad(blueProgram, essl1_shaders::PositionAttrib(), 0.5f);
swapBuffers();
// Use glClear to clear to red. Regression test for the Vulkan backend where this clear
// remained "deferred" and didn't make it to the window on swap.
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
swapBuffers();
// Wait for visual verification.
angle::Sleep(2000);
}
// According to the spec, the minimum value for the multisample line width range limits is one.
TEST_P(MultisampleTestES32, MultisampleLineWidthRangeCheck)
{
GLfloat range[2] = {0, 0};
glGetFloatv(GL_MULTISAMPLE_LINE_WIDTH_RANGE, range);
EXPECT_GL_NO_ERROR();
EXPECT_GE(range[0], 1.0f);
EXPECT_GE(range[1], 1.0f);
}
// The multisample line width granularity should not be negative.
TEST_P(MultisampleTestES32, MultisampleLineWidthGranularityCheck)
{
GLfloat granularity = -1.0f;
glGetFloatv(GL_MULTISAMPLE_LINE_WIDTH_GRANULARITY, &granularity);
EXPECT_GL_NO_ERROR();
EXPECT_GE(granularity, 0.0f);
}
// These colors match the shader in resolveToFBO which returns (0.5, 0.6, 0.7, 0.8).
const GLColor MultisampleResolveTest::kEXPECTED_R8(128, 0, 0, 255);
const GLColor MultisampleResolveTest::kEXPECTED_RG8(128, 153, 0, 255);
const GLColor MultisampleResolveTest::kEXPECTED_RGB8(128, 153, 178, 255);
const GLColor MultisampleResolveTest::kEXPECTED_RGBA8(128, 153, 178, 204);
const GLColor32F MultisampleResolveTest::kEXPECTED_RF(0.5f, 0.0f, 0.0f, 1.0f);
const GLColor32F MultisampleResolveTest::kEXPECTED_RGF(0.5f, 0.6f, 0.0f, 1.0f);
const GLColor32F MultisampleResolveTest::kEXPECTED_RGBF(0.5f, 0.6f, 0.7f, 1.0f);
const GLColor32F MultisampleResolveTest::kEXPECTED_RGBAF(0.5f, 0.6f, 0.7f, 0.8f);
// Test we can render to and resolve an RGBA8 renderbuffer
TEST_P(MultisampleResolveTest, ResolveRGBA8ToFBO4Samples)
{
testResolveToUNormFBO(GL_RGBA8, kEXPECTED_RGBA8, 4, kWidth, kHeight);
}
// Test we can render to and resolve an RGB8 renderbuffer
TEST_P(MultisampleResolveTest, ResolveRGB8ToFBO4Samples)
{
testResolveToUNormFBO(GL_RGB8, kEXPECTED_RGB8, 4, kWidth, kHeight);
}
// Test we can render to and resolve an RG8 renderbuffer
TEST_P(MultisampleResolveTest, ResolveRG8ToFBO4Samples)
{
testResolveToUNormFBO(GL_RG8, kEXPECTED_RG8, 4, kWidth, kHeight);
}
// Test we can render to and resolve an R8 renderbuffer
TEST_P(MultisampleResolveTest, ResolveR8ToFBO4Samples)
{
testResolveToUNormFBO(GL_R8, kEXPECTED_R8, 4, kWidth, kHeight);
}
// Test we can render to and resolve an R16F renderbuffer
TEST_P(MultisampleResolveTest, ResolveR16FToFBO4Samples)
{
testResolveToHalfFBO(GL_R16F, kEXPECTED_RF, 4, kWidth, kHeight);
}
// Test we can render to and resolve an RG16F renderbuffer
TEST_P(MultisampleResolveTest, ResolveRG16FToFBO4Samples)
{
testResolveToHalfFBO(GL_RG16F, kEXPECTED_RGF, 4, kWidth, kHeight);
}
// Test we can render to and resolve an RGB16F renderbuffer
TEST_P(MultisampleResolveTest, ResolveRGB16FToFBO4Samples)
{
testResolveToHalfFBO(GL_RGB16F, kEXPECTED_RGBF, 4, kWidth, kHeight);
}
// Test we can render to and resolve an RGBA16F renderbuffer
TEST_P(MultisampleResolveTest, ResolveRGBA16FToFBO4Samples)
{
testResolveToHalfFBO(GL_RGBA16F, kEXPECTED_RGBAF, 4, kWidth, kHeight);
}
// Test we can render to and resolve an R32F renderbuffer
TEST_P(MultisampleResolveTest, ResolveR32FToFBO4Samples)
{
testResolveToFloatFBO(GL_R32F, kEXPECTED_RF, 4, kWidth, kHeight);
}
// Test we can render to and resolve an RG32F renderbuffer
TEST_P(MultisampleResolveTest, ResolveRG32FToFBO4Samples)
{
testResolveToFloatFBO(GL_RG32F, kEXPECTED_RGF, 4, kWidth, kHeight);
}
// Test we can render to and resolve an RGB32F renderbuffer
TEST_P(MultisampleResolveTest, ResolveRGB32FToFBO4Samples)
{
testResolveToRGBFloatFBO(GL_RGB32F, kEXPECTED_RGBF, 4, kWidth, kHeight);
}
// Test we can render to and resolve an RGBA32F renderbuffer
TEST_P(MultisampleResolveTest, ResolveRGBA32FToFBO4Samples)
{
testResolveToFloatFBO(GL_RGBA32F, kEXPECTED_RGBAF, 4, kWidth, kHeight);
}
TEST_P(MultisampleResolveTest, ResolveD32FS8F4Samples)
{
testResolveDepthToFBO(GL_DEPTH32F_STENCIL8, GL_DEPTH_STENCIL_ATTACHMENT, 4, kWidth, kHeight);
}
TEST_P(MultisampleResolveTest, ResolveD24S8Samples)
{
testResolveDepthToFBO(GL_DEPTH24_STENCIL8, GL_DEPTH_STENCIL_ATTACHMENT, 4, kWidth, kHeight);
}
TEST_P(MultisampleResolveTest, ResolveD32FSamples)
{
testResolveDepthToFBO(GL_DEPTH_COMPONENT32F, GL_DEPTH_ATTACHMENT, 4, kWidth, kHeight);
}
TEST_P(MultisampleResolveTest, ResolveD24Samples)
{
testResolveDepthToFBO(GL_DEPTH_COMPONENT24, GL_DEPTH_ATTACHMENT, 4, kWidth, kHeight);
}
TEST_P(MultisampleResolveTest, ResolveD16Samples)
{
testResolveDepthToFBO(GL_DEPTH_COMPONENT16, GL_DEPTH_ATTACHMENT, 4, kWidth, kHeight);
}
void drawRectAndBlit(GLuint msFramebuffer,
GLuint resolveFramebuffer,
GLint width,
GLint height,
GLint matLoc,
GLint colorLoc,
float x,
float y,
float w,
float h,
const GLColor &color)
{
glBindFramebuffer(GL_FRAMEBUFFER, msFramebuffer);
float matrix[16] = {
w, 0, 0, 0, 0, h, 0, 0, 0, 0, 1, 0, x, y, 0, 1,
};
glUniformMatrix4fv(matLoc, 1, false, matrix);
angle::Vector4 c(color.toNormalizedVector());
glUniform4f(colorLoc, c[0], c[1], c[2], c[3]);
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolveFramebuffer);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
// Tests if we resolve(blit) a multisample renderbuffer that it
// does not lose its contents.
TEST_P(MultisampleResolveTest, DrawAndResolveMultipleTimes)
{
constexpr GLint samples = 4;
constexpr GLenum format = GL_RGBA8;
constexpr GLint width = 16;
constexpr GLint height = 16;
constexpr char kVS[] = R"(#version 300 es
layout(location = 0) in vec4 position;
uniform mat4 mat;
void main() {
gl_Position = mat * position;
}
)";
constexpr char kFS[] = R"(#version 300 es
precision highp float;
uniform vec4 color;
out vec4 outColor;
void main() {
outColor = color;
}
)";
glViewport(0, 0, width, height);
ANGLE_GL_PROGRAM(program, kVS, kFS);
GLint matLoc = glGetUniformLocation(program, "mat");
GLint colorLoc = glGetUniformLocation(program, "color");
glUseProgram(program);
GLBuffer buf;
glBindBuffer(GL_ARRAY_BUFFER, buf);
constexpr float vertices[] = {
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
};
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
// Make samples = 4 multi-sample framebuffer.
GLFramebuffer msFB;
glBindFramebuffer(GL_FRAMEBUFFER, msFB);
GLRenderbuffer msRB;
glBindRenderbuffer(GL_RENDERBUFFER, msRB);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, format, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msRB);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
// Make non-multi-sample framebuffer.
GLFramebuffer drawFB;
glBindFramebuffer(GL_FRAMEBUFFER, drawFB);
GLRenderbuffer drawRB;
glBindRenderbuffer(GL_RENDERBUFFER, drawRB);
glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, drawRB);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
drawRectAndBlit(msFB, drawFB, width, height, matLoc, colorLoc, -1, -1, 2, 2, GLColor::red);
drawRectAndBlit(msFB, drawFB, width, height, matLoc, colorLoc, 0, -1, 1, 1, GLColor::green);
drawRectAndBlit(msFB, drawFB, width, height, matLoc, colorLoc, -1, 0, 1, 1, GLColor::blue);
drawRectAndBlit(msFB, drawFB, width, height, matLoc, colorLoc, 0, 0, 1, 1, GLColor::yellow);
glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);
drawRectAndBlit(msFB, drawFB, width, height, matLoc, colorLoc, -0.5, -0.5, 1, 1,
GLColor(0x80, 0x80, 0x80, 0x80));
glDisable(GL_BLEND);
ASSERT_GL_NO_ERROR();
/*
expected
+-------------+--------------+
| blue | yellow |
| +---------+----------+ |
| |.5,.5,1,1| 1,1,.5,1 | |
+---+---------+----------+---+
| |1,.5,.5,1| .5,1,.5,1| |
| +---------+----------+ |
| red | green |
+-------------+--------------+
0,0
*/
glBindFramebuffer(GL_FRAMEBUFFER, drawFB);
EXPECT_PIXEL_RECT_EQ(0, 0, width / 2, height / 4, GLColor::red);
EXPECT_PIXEL_RECT_EQ(width / 2, 0, width / 2, height / 4, GLColor::green);
EXPECT_PIXEL_RECT_EQ(0, height * 3 / 4, width / 2, height / 4, GLColor::blue);
EXPECT_PIXEL_RECT_EQ(width / 2, height * 3 / 4, width / 2, height / 4, GLColor::yellow);
EXPECT_PIXEL_RECT_EQ(width / 4, height / 4, width / 4, height / 4, GLColor(255, 128, 128, 255));
EXPECT_PIXEL_RECT_EQ(width / 2, height / 4, width / 4, height / 4, GLColor(128, 255, 128, 255));
EXPECT_PIXEL_RECT_EQ(width / 4, height / 2, width / 4, height / 4, GLColor(128, 128, 255, 255));
EXPECT_PIXEL_RECT_EQ(width / 2, height / 2, width / 4, height / 4, GLColor(255, 255, 128, 255));
}
// Tests resolve after the read framebuffer's attachment has been swapped out.
TEST_P(MultisampleResolveTest, SwitchAttachmentsBeforeResolve)
{
constexpr GLuint kWidth = 16;
constexpr GLuint kHeight = 24;
GLRenderbuffer msaaColor0, msaaColor1;
glBindRenderbuffer(GL_RENDERBUFFER, msaaColor0);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA8, kWidth, kHeight);
glBindRenderbuffer(GL_RENDERBUFFER, msaaColor1);
glRenderbufferStorageMultisample(GL_RENDERBUFFER, 4, GL_RGBA8, kWidth, kHeight);
{
// First, make one of the MSAA color buffers green.
GLFramebuffer clearFbo;
glBindFramebuffer(GL_FRAMEBUFFER, clearFbo);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
msaaColor1);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
glClearColor(0, 1, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
// Make sure clear is performed.
GLTexture resolveTexture;
glBindTexture(GL_TEXTURE_2D, resolveTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, kWidth, kHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
nullptr);
GLFramebuffer verifyFBO;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, verifyFBO);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
resolveTexture, 0);
glBlitFramebuffer(0, 0, kWidth, kHeight, 0, 0, kWidth, kHeight, GL_COLOR_BUFFER_BIT,
GL_NEAREST);
glBindFramebuffer(GL_READ_FRAMEBUFFER, verifyFBO);
EXPECT_PIXEL_RECT_EQ(0, 0, kWidth, kHeight, GLColor::green);
}
// Set up the msaa framebuffer with the other MSAA color buffer.
GLFramebuffer msaaFBO;
glBindFramebuffer(GL_FRAMEBUFFER, msaaFBO);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, msaaColor0);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_FRAMEBUFFER);
// Draw into it to start a render pass
ANGLE_GL_PROGRAM(red, essl1_shaders::vs::Simple(), essl1_shaders::fs::Red());
drawQuad(red, essl1_shaders::PositionAttrib(), 0.5f);
// Set up the resolve framebuffer
GLRenderbuffer resolveColor;
glBindRenderbuffer(GL_RENDERBUFFER, resolveColor);
glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, kWidth, kHeight);
GLFramebuffer resolveFBO;
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, resolveFBO);
glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
resolveColor);
EXPECT_GL_FRAMEBUFFER_COMPLETE(GL_DRAW_FRAMEBUFFER);
// Before resolve the MSAA framebuffer, switch its attachment. Regression test for a bug where
// the resolve used the previous attachment.
glFramebufferRenderbuffer(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER,
msaaColor1);
glBlitFramebuffer(0, 0, kWidth, kHeight, 0, 0, kWidth, kHeight, GL_COLOR_BUFFER_BIT,
GL_NEAREST);
// Verify that the resolve happened on the pre-cleared attachment, not the rendered one.
glBindFramebuffer(GL_READ_FRAMEBUFFER, resolveFBO);
EXPECT_PIXEL_RECT_EQ(0, 0, kWidth, kHeight, GLColor::green);
}
ANGLE_INSTANTIATE_TEST_ES2_AND_ES3_AND_ES31_AND(
MultisampleTest,
ES3_VULKAN().enable(Feature::EmulatedPrerotation90),
ES3_VULKAN().enable(Feature::EmulatedPrerotation180),
ES3_VULKAN().enable(Feature::EmulatedPrerotation270),
// Simulate missing msaa auto resolve feature in Metal.
ES2_METAL().disable(Feature::AllowMultisampleStoreAndResolve));
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MultisampleTestES3);
ANGLE_INSTANTIATE_TEST_ES3_AND_ES31_AND(MultisampleTestES3,
ES3_VULKAN().enable(Feature::EmulatedPrerotation90),
ES3_VULKAN().enable(Feature::EmulatedPrerotation180),
ES3_VULKAN().enable(Feature::EmulatedPrerotation270));
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MultisampleTestES32);
ANGLE_INSTANTIATE_TEST_ES32_AND(MultisampleTestES32,
ES32_VULKAN().enable(Feature::EmulatedPrerotation90),
ES32_VULKAN().enable(Feature::EmulatedPrerotation180),
ES32_VULKAN().enable(Feature::EmulatedPrerotation270));
ANGLE_INSTANTIATE_TEST_ES3_AND(
MultisampleResolveTest,
ES3_VULKAN().enable(Feature::EmulatedPrerotation90),
ES3_VULKAN().enable(Feature::EmulatedPrerotation180),
ES3_VULKAN().enable(Feature::EmulatedPrerotation270),
ES3_VULKAN().enable(Feature::PreferDrawClearOverVkCmdClearAttachments));
} // anonymous namespace