Hash :
a20af6d7
Author :
Date :
2017-09-18T13:32:29
Use C++11 raw string literals instead of SHADER_SOURCE macro This is better in many ways: 1. It doesn't confuse clang format 2. \n doesn't need to be included after preprocessor directives like the version directive. 3. It's using built-in functionality instead of something custom. Raw string literals should be the preferred way to include shader source in C++ files going forward. BUG=angleproject:2157 TEST=angle_end2end_tests Change-Id: I8b236a6e2d5c25d920297e5bc5b5b143eddeba1f Reviewed-on: https://chromium-review.googlesource.com/671046 Reviewed-by: Corentin Wallez <cwallez@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Olli Etuaho <oetuaho@nvidia.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 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
//
// 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 "test_utils/gl_raii.h"
using namespace angle;
namespace
{
class UniformBufferTest : public ANGLETest
{
protected:
UniformBufferTest()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
void SetUp() override
{
ANGLETest::SetUp();
mVertexShaderSource =
R"(#version 300 es
in vec4 position;
void main()
{
gl_Position = position;
})";
mFragmentShaderSource =
R"(#version 300 es
precision highp float;
uniform uni { vec4 color; };
out vec4 fragColor;
void main()
{
fragColor = color;
})";
mProgram = CompileProgram(mVertexShaderSource, mFragmentShaderSource);
ASSERT_NE(mProgram, 0u);
mUniformBufferIndex = glGetUniformBlockIndex(mProgram, "uni");
ASSERT_NE(mUniformBufferIndex, -1);
glGenBuffers(1, &mUniformBuffer);
ASSERT_GL_NO_ERROR();
}
void TearDown() override
{
glDeleteBuffers(1, &mUniformBuffer);
glDeleteProgram(mProgram);
ANGLETest::TearDown();
}
std::string mVertexShaderSource;
std::string mFragmentShaderSource;
GLuint mProgram;
GLint mUniformBufferIndex;
GLuint mUniformBuffer;
};
// Basic UBO functionality.
TEST_P(UniformBufferTest, Simple)
{
glClear(GL_COLOR_BUFFER_BIT);
float floatData[4] = {0.5f, 0.75f, 0.25f, 1.0f};
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
glBufferData(GL_UNIFORM_BUFFER, sizeof(float) * 4, floatData, GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, mUniformBuffer);
glUniformBlockBinding(mProgram, mUniformBufferIndex, 0);
drawQuad(mProgram, "position", 0.5f);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_NEAR(0, 0, 128, 191, 64, 255, 1);
}
// Test that using a UBO with a non-zero offset and size actually works.
// The first step of this test renders a color from a UBO with a zero offset.
// The second step renders a color from a UBO with a non-zero offset.
TEST_P(UniformBufferTest, UniformBufferRange)
{
int px = getWindowWidth() / 2;
int py = getWindowHeight() / 2;
// Query the uniform buffer alignment requirement
GLint alignment;
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment);
GLint64 maxUniformBlockSize;
glGetInteger64v(GL_MAX_UNIFORM_BLOCK_SIZE, &maxUniformBlockSize);
if (alignment >= maxUniformBlockSize)
{
// ANGLE doesn't implement UBO offsets for this platform.
// Ignore the test case.
return;
}
ASSERT_GL_NO_ERROR();
// Let's create a buffer which contains two vec4.
GLuint vec4Size = 4 * sizeof(float);
GLuint stride = 0;
do
{
stride += alignment;
} while (stride < vec4Size);
std::vector<char> v(2 * stride);
float *first = reinterpret_cast<float *>(v.data());
float *second = reinterpret_cast<float *>(v.data() + stride);
first[0] = 10.f / 255.f;
first[1] = 20.f / 255.f;
first[2] = 30.f / 255.f;
first[3] = 40.f / 255.f;
second[0] = 110.f / 255.f;
second[1] = 120.f / 255.f;
second[2] = 130.f / 255.f;
second[3] = 140.f / 255.f;
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
// We use on purpose a size which is not a multiple of the alignment.
glBufferData(GL_UNIFORM_BUFFER, stride + vec4Size, v.data(), GL_STATIC_DRAW);
glUniformBlockBinding(mProgram, mUniformBufferIndex, 0);
EXPECT_GL_NO_ERROR();
// Bind the first part of the uniform buffer and draw
// Use a size which is smaller than the alignment to check
// to check that this case is handle correctly in the conversion to 11.1.
glBindBufferRange(GL_UNIFORM_BUFFER, 0, mUniformBuffer, 0, vec4Size);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 10, 20, 30, 40);
// Bind the second part of the uniform buffer and draw
// Furthermore the D3D11.1 backend will internally round the vec4Size (16 bytes) to a stride
// (256 bytes) hence it will try to map the range [stride, 2 * stride] which is out-of-bound of
// the buffer bufferSize = stride + vec4Size < 2 * stride. Ensure that this behaviour works.
glBindBufferRange(GL_UNIFORM_BUFFER, 0, mUniformBuffer, stride, vec4Size);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 110, 120, 130, 140);
}
// Test uniform block bindings.
TEST_P(UniformBufferTest, UniformBufferBindings)
{
int px = getWindowWidth() / 2;
int py = getWindowHeight() / 2;
ASSERT_GL_NO_ERROR();
// Let's create a buffer which contains one vec4.
GLuint vec4Size = 4 * sizeof(float);
std::vector<char> v(vec4Size);
float *first = reinterpret_cast<float *>(v.data());
first[0] = 10.f / 255.f;
first[1] = 20.f / 255.f;
first[2] = 30.f / 255.f;
first[3] = 40.f / 255.f;
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
glBufferData(GL_UNIFORM_BUFFER, vec4Size, v.data(), GL_STATIC_DRAW);
EXPECT_GL_NO_ERROR();
// Try to bind the buffer to binding point 2
glUniformBlockBinding(mProgram, mUniformBufferIndex, 2);
glBindBufferBase(GL_UNIFORM_BUFFER, 2, mUniformBuffer);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 10, 20, 30, 40);
// Clear the framebuffer
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(px, py, 0, 0, 0, 0);
// Try to bind the buffer to another binding point
glUniformBlockBinding(mProgram, mUniformBufferIndex, 5);
glBindBufferBase(GL_UNIFORM_BUFFER, 5, mUniformBuffer);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 10, 20, 30, 40);
}
// Test that ANGLE handles used but unbound UBO.
// TODO: A test case shouldn't depend on the error code of an undefined behaviour. Move this to unit
// tests of the validation layer.
TEST_P(UniformBufferTest, UnboundUniformBuffer)
{
glUniformBlockBinding(mProgram, mUniformBufferIndex, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, 0);
EXPECT_GL_NO_ERROR();
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_ERROR(GL_INVALID_OPERATION);
}
// Update a UBO many time and verify that ANGLE uses the latest version of the data.
// https://code.google.com/p/angleproject/issues/detail?id=965
TEST_P(UniformBufferTest, UniformBufferManyUpdates)
{
// TODO(jmadill): Figure out why this fails on Intel OpenGL.
if (IsIntel() && IsOpenGL())
{
std::cout << "Test skipped on Intel OpenGL." << std::endl;
return;
}
int px = getWindowWidth() / 2;
int py = getWindowHeight() / 2;
ASSERT_GL_NO_ERROR();
float data[4];
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
glBufferData(GL_UNIFORM_BUFFER, sizeof(data), nullptr, GL_DYNAMIC_DRAW);
glUniformBlockBinding(mProgram, mUniformBufferIndex, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, mUniformBuffer);
EXPECT_GL_NO_ERROR();
// Repeteadly update the data and draw
for (size_t i = 0; i < 10; ++i)
{
data[0] = (i + 10.f) / 255.f;
data[1] = (i + 20.f) / 255.f;
data[2] = (i + 30.f) / 255.f;
data[3] = (i + 40.f) / 255.f;
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(data), data);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, i + 10, i + 20, i + 30, i + 40);
}
}
// Use a large number of buffer ranges (compared to the actual size of the UBO)
TEST_P(UniformBufferTest, ManyUniformBufferRange)
{
int px = getWindowWidth() / 2;
int py = getWindowHeight() / 2;
// Query the uniform buffer alignment requirement
GLint alignment;
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment);
GLint64 maxUniformBlockSize;
glGetInteger64v(GL_MAX_UNIFORM_BLOCK_SIZE, &maxUniformBlockSize);
if (alignment >= maxUniformBlockSize)
{
// ANGLE doesn't implement UBO offsets for this platform.
// Ignore the test case.
return;
}
ASSERT_GL_NO_ERROR();
// Let's create a buffer which contains eight vec4.
GLuint vec4Size = 4 * sizeof(float);
GLuint stride = 0;
do
{
stride += alignment;
} while (stride < vec4Size);
std::vector<char> v(8 * stride);
for (size_t i = 0; i < 8; ++i)
{
float *data = reinterpret_cast<float *>(v.data() + i * stride);
data[0] = (i + 10.f) / 255.f;
data[1] = (i + 20.f) / 255.f;
data[2] = (i + 30.f) / 255.f;
data[3] = (i + 40.f) / 255.f;
}
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
glBufferData(GL_UNIFORM_BUFFER, v.size(), v.data(), GL_STATIC_DRAW);
glUniformBlockBinding(mProgram, mUniformBufferIndex, 0);
EXPECT_GL_NO_ERROR();
// Bind each possible offset
for (size_t i = 0; i < 8; ++i)
{
glBindBufferRange(GL_UNIFORM_BUFFER, 0, mUniformBuffer, i * stride, stride);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 10 + i, 20 + i, 30 + i, 40 + i);
}
// Try to bind larger range
for (size_t i = 0; i < 7; ++i)
{
glBindBufferRange(GL_UNIFORM_BUFFER, 0, mUniformBuffer, i * stride, 2 * stride);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 10 + i, 20 + i, 30 + i, 40 + i);
}
// Try to bind even larger range
for (size_t i = 0; i < 5; ++i)
{
glBindBufferRange(GL_UNIFORM_BUFFER, 0, mUniformBuffer, i * stride, 4 * stride);
drawQuad(mProgram, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 10 + i, 20 + i, 30 + i, 40 + i);
}
}
// Tests that active uniforms have the right names.
TEST_P(UniformBufferTest, ActiveUniformNames)
{
const std::string &vertexShaderSource =
"#version 300 es\n"
"in vec2 position;\n"
"out vec2 v;\n"
"uniform blockName1 {\n"
" float f1;\n"
"} instanceName1;\n"
"uniform blockName2 {\n"
" float f2;\n"
"} instanceName2[1];\n"
"void main() {\n"
" v = vec2(instanceName1.f1, instanceName2[0].f2);\n"
" gl_Position = vec4(position, 0, 1);\n"
"}";
const std::string &fragmentShaderSource =
"#version 300 es\n"
"precision highp float;\n"
"in vec2 v;\n"
"out vec4 color;\n"
"void main() {\n"
" color = vec4(v, 0, 1);\n"
"}";
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
ASSERT_NE(0u, program);
GLint activeUniformBlocks;
glGetProgramiv(program, GL_ACTIVE_UNIFORM_BLOCKS, &activeUniformBlocks);
ASSERT_EQ(2, activeUniformBlocks);
GLuint index = glGetUniformBlockIndex(program, "blockName1");
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
index = glGetUniformBlockIndex(program, "blockName2[0]");
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
GLint activeUniforms;
glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &activeUniforms);
ASSERT_EQ(2, activeUniforms);
GLint size;
GLenum type;
GLint maxLength;
GLsizei length;
glGetProgramiv(program, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength);
std::vector<GLchar> strUniformNameBuffer(maxLength + 1, 0);
const GLchar *uniformNames[1];
uniformNames[0] = "blockName1.f1";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program, index, maxLength, &length, &size, &type, &strUniformNameBuffer[0]);
EXPECT_EQ(1, size);
EXPECT_GLENUM_EQ(GL_FLOAT, type);
EXPECT_EQ("blockName1.f1", std::string(&strUniformNameBuffer[0]));
uniformNames[0] = "blockName2.f2";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program, index, maxLength, &length, &size, &type, &strUniformNameBuffer[0]);
EXPECT_EQ(1, size);
EXPECT_GLENUM_EQ(GL_FLOAT, type);
EXPECT_EQ("blockName2.f2", std::string(&strUniformNameBuffer[0]));
}
// Tests active uniforms and blocks when the layout is std140, shared and packed.
TEST_P(UniformBufferTest, ActiveUniformNumberAndName)
{
const std::string &vertexShaderSource =
"#version 300 es\n"
"in vec2 position;\n"
"out float v;\n"
"struct S {\n"
" highp ivec3 a;\n"
" mediump ivec2 b[4];\n"
"};\n"
"layout(std140) uniform blockName0 {\n"
" S s0;\n"
" lowp vec2 v0;\n"
" S s1[2];\n"
" highp uint u0;\n"
"};\n"
"layout(std140) uniform blockName1 {\n"
" float f1;\n"
" bool b1;\n"
"} instanceName1;\n"
"layout(shared) uniform blockName2 {\n"
" float f2;\n"
"};\n"
"layout(packed) uniform blockName3 {\n"
" float f3;\n"
"};\n"
"void main() {\n"
" v = instanceName1.f1;\n"
" gl_Position = vec4(position, 0, 1);\n"
"}";
const std::string &fragmentShaderSource =
"#version 300 es\n"
"precision highp float;\n"
"in float v;\n"
"out vec4 color;\n"
"void main() {\n"
" color = vec4(v, 0, 0, 1);\n"
"}";
ANGLE_GL_PROGRAM(program, vertexShaderSource, fragmentShaderSource);
// Note that the packed |blockName3| might (or might not) be optimized out.
GLint activeUniforms;
glGetProgramiv(program.get(), GL_ACTIVE_UNIFORMS, &activeUniforms);
EXPECT_GE(activeUniforms, 11);
GLint activeUniformBlocks;
glGetProgramiv(program.get(), GL_ACTIVE_UNIFORM_BLOCKS, &activeUniformBlocks);
EXPECT_GE(activeUniformBlocks, 3);
GLint maxLength, size;
GLenum type;
GLsizei length;
GLuint index;
const GLchar *uniformNames[1];
glGetProgramiv(program.get(), GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLength);
std::vector<GLchar> strBuffer(maxLength + 1, 0);
uniformNames[0] = "s0.a";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
EXPECT_EQ(1, size);
EXPECT_EQ("s0.a", std::string(&strBuffer[0]));
uniformNames[0] = "s0.b[0]";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(4, size);
EXPECT_EQ("s0.b[0]", std::string(&strBuffer[0]));
uniformNames[0] = "v0";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(1, size);
EXPECT_EQ("v0", std::string(&strBuffer[0]));
uniformNames[0] = "s1[0].a";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(1, size);
EXPECT_EQ("s1[0].a", std::string(&strBuffer[0]));
uniformNames[0] = "s1[0].b[0]";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(4, size);
EXPECT_EQ("s1[0].b[0]", std::string(&strBuffer[0]));
uniformNames[0] = "s1[1].a";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(1, size);
EXPECT_EQ("s1[1].a", std::string(&strBuffer[0]));
uniformNames[0] = "s1[1].b[0]";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(4, size);
EXPECT_EQ("s1[1].b[0]", std::string(&strBuffer[0]));
uniformNames[0] = "u0";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(1, size);
EXPECT_EQ("u0", std::string(&strBuffer[0]));
uniformNames[0] = "blockName1.f1";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(1, size);
EXPECT_EQ("blockName1.f1", std::string(&strBuffer[0]));
uniformNames[0] = "blockName1.b1";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(1, size);
EXPECT_EQ("blockName1.b1", std::string(&strBuffer[0]));
uniformNames[0] = "f2";
glGetUniformIndices(program, 1, uniformNames, &index);
EXPECT_NE(GL_INVALID_INDEX, index);
ASSERT_GL_NO_ERROR();
glGetActiveUniform(program.get(), index, maxLength, &length, &size, &type, &strBuffer[0]);
ASSERT_GL_NO_ERROR();
EXPECT_EQ(1, size);
EXPECT_EQ("f2", std::string(&strBuffer[0]));
}
// Test that using a very large buffer to back a small uniform block works OK.
TEST_P(UniformBufferTest, VeryLarge)
{
glClear(GL_COLOR_BUFFER_BIT);
float floatData[4] = {0.5f, 0.75f, 0.25f, 1.0f};
GLsizei bigSize = 4096 * 64;
std::vector<GLubyte> zero(bigSize, 0);
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
glBufferData(GL_UNIFORM_BUFFER, bigSize, zero.data(), GL_STATIC_DRAW);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(float) * 4, floatData);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, mUniformBuffer);
glUniformBlockBinding(mProgram, mUniformBufferIndex, 0);
drawQuad(mProgram, "position", 0.5f);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_NEAR(0, 0, 128, 191, 64, 255, 1);
}
// Test that readback from a very large uniform buffer works OK.
TEST_P(UniformBufferTest, VeryLargeReadback)
{
glClear(GL_COLOR_BUFFER_BIT);
// Generate some random data.
GLsizei bigSize = 4096 * 64;
std::vector<GLubyte> expectedData(bigSize);
for (GLsizei index = 0; index < bigSize; ++index)
{
expectedData[index] = static_cast<GLubyte>(index);
}
// Initialize the GL buffer.
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
glBufferData(GL_UNIFORM_BUFFER, bigSize, expectedData.data(), GL_STATIC_DRAW);
// Do a small update.
GLsizei smallSize = sizeof(float) * 4;
std::array<float, 4> floatData = {{0.5f, 0.75f, 0.25f, 1.0f}};
memcpy(expectedData.data(), floatData.data(), smallSize);
glBufferSubData(GL_UNIFORM_BUFFER, 0, smallSize, expectedData.data());
// Draw with the buffer.
glBindBufferBase(GL_UNIFORM_BUFFER, 0, mUniformBuffer);
glUniformBlockBinding(mProgram, mUniformBufferIndex, 0);
drawQuad(mProgram, "position", 0.5f);
ASSERT_GL_NO_ERROR();
EXPECT_PIXEL_NEAR(0, 0, 128, 191, 64, 255, 1);
// Read back the large buffer data.
const void *mapPtr = glMapBufferRange(GL_UNIFORM_BUFFER, 0, bigSize, GL_MAP_READ_BIT);
ASSERT_GL_NO_ERROR();
const GLubyte *bytePtr = reinterpret_cast<const GLubyte *>(mapPtr);
std::vector<GLubyte> actualData(bytePtr, bytePtr + bigSize);
EXPECT_EQ(expectedData, actualData);
glUnmapBuffer(GL_UNIFORM_BUFFER);
}
class UniformBufferTest31 : public ANGLETest
{
protected:
UniformBufferTest31()
{
setWindowWidth(128);
setWindowHeight(128);
setConfigRedBits(8);
setConfigGreenBits(8);
setConfigBlueBits(8);
setConfigAlphaBits(8);
}
};
// Test uniform block bindings greater than GL_MAX_UNIFORM_BUFFER_BINDINGS cause compile error.
TEST_P(UniformBufferTest31, MaxUniformBufferBindingsExceeded)
{
GLint maxUniformBufferBindings;
glGetIntegerv(GL_MAX_UNIFORM_BUFFER_BINDINGS, &maxUniformBufferBindings);
std::string source =
"#version 310 es\n"
"in vec4 position;\n"
"layout(binding = ";
std::stringstream ss;
ss << maxUniformBufferBindings;
source = source + ss.str() +
") uniform uni {\n"
" vec4 color;\n"
"};\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}";
GLuint shader = CompileShader(GL_VERTEX_SHADER, source);
EXPECT_EQ(0u, shader);
}
// Test uniform block bindings specified by layout in shader work properly.
TEST_P(UniformBufferTest31, UniformBufferBindings)
{
const std::string &vertexShaderSource =
"#version 310 es\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}";
const std::string &fragmentShaderSource =
"#version 310 es\n"
"precision highp float;\n"
"layout(binding = 2) uniform uni {\n"
" vec4 color;\n"
"};\n"
"out vec4 fragColor;\n"
"void main()\n"
"{"
" fragColor = color;\n"
"}";
ANGLE_GL_PROGRAM(program, vertexShaderSource, fragmentShaderSource);
GLuint uniformBufferIndex = glGetUniformBlockIndex(program, "uni");
ASSERT_NE(GL_INVALID_INDEX, uniformBufferIndex);
GLBuffer uniformBuffer;
int px = getWindowWidth() / 2;
int py = getWindowHeight() / 2;
ASSERT_GL_NO_ERROR();
// Let's create a buffer which contains one vec4.
GLuint vec4Size = 4 * sizeof(float);
std::vector<char> v(vec4Size);
float *first = reinterpret_cast<float *>(v.data());
first[0] = 10.f / 255.f;
first[1] = 20.f / 255.f;
first[2] = 30.f / 255.f;
first[3] = 40.f / 255.f;
glBindBuffer(GL_UNIFORM_BUFFER, uniformBuffer.get());
glBufferData(GL_UNIFORM_BUFFER, vec4Size, v.data(), GL_STATIC_DRAW);
EXPECT_GL_NO_ERROR();
glBindBufferBase(GL_UNIFORM_BUFFER, 2, uniformBuffer.get());
drawQuad(program, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 10, 20, 30, 40);
// Clear the framebuffer
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
EXPECT_PIXEL_EQ(px, py, 0, 0, 0, 0);
// Try to bind the buffer to another binding point
glUniformBlockBinding(program, uniformBufferIndex, 5);
glBindBufferBase(GL_UNIFORM_BUFFER, 5, uniformBuffer.get());
drawQuad(program, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 10, 20, 30, 40);
}
// Test uniform blocks used as instanced array take next binding point for each subsequent element.
TEST_P(UniformBufferTest31, ConsecutiveBindingsForBlockArray)
{
const std::string &vertexShaderSource =
"#version 310 es\n"
"in vec4 position;\n"
"void main()\n"
"{\n"
" gl_Position = position;\n"
"}";
const std::string &fragmentShaderSource =
"#version 310 es\n"
"precision highp float;\n"
"layout(binding = 2) uniform uni {\n"
" vec4 color;\n"
"} blocks[2];\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = blocks[0].color + blocks[1].color;\n"
"}";
ANGLE_GL_PROGRAM(program, vertexShaderSource, fragmentShaderSource);
std::array<GLBuffer, 2> uniformBuffers;
int px = getWindowWidth() / 2;
int py = getWindowHeight() / 2;
ASSERT_GL_NO_ERROR();
// Let's create a buffer which contains one vec4.
GLuint vec4Size = 4 * sizeof(float);
std::vector<char> v(vec4Size);
float *first = reinterpret_cast<float *>(v.data());
first[0] = 10.f / 255.f;
first[1] = 20.f / 255.f;
first[2] = 30.f / 255.f;
first[3] = 40.f / 255.f;
glBindBuffer(GL_UNIFORM_BUFFER, uniformBuffers[0].get());
glBufferData(GL_UNIFORM_BUFFER, vec4Size, v.data(), GL_STATIC_DRAW);
EXPECT_GL_NO_ERROR();
glBindBufferBase(GL_UNIFORM_BUFFER, 2, uniformBuffers[0].get());
ASSERT_GL_NO_ERROR();
glBindBuffer(GL_UNIFORM_BUFFER, uniformBuffers[1].get());
glBufferData(GL_UNIFORM_BUFFER, vec4Size, v.data(), GL_STATIC_DRAW);
EXPECT_GL_NO_ERROR();
glBindBufferBase(GL_UNIFORM_BUFFER, 3, uniformBuffers[1].get());
drawQuad(program, "position", 0.5f);
EXPECT_GL_NO_ERROR();
EXPECT_PIXEL_EQ(px, py, 20, 40, 60, 80);
}
// Test the layout qualifier binding must be both specified(ESSL 3.10.4 section 9.2).
TEST_P(UniformBufferTest31, BindingMustBeBothSpecified)
{
const std::string &vertexShaderSource =
"#version 310 es\n"
"in vec4 position;\n"
"uniform uni\n"
"{\n"
" vec4 color;\n"
"} block;\n"
"void main()\n"
"{\n"
" gl_Position = position + block.color;\n"
"}";
const std::string &fragmentShaderSource =
"#version 310 es\n"
"precision highp float;\n"
"layout(binding = 0) uniform uni\n"
"{\n"
" vec4 color;\n"
"} block;\n"
"out vec4 fragColor;\n"
"void main()\n"
"{\n"
" fragColor = block.color;\n"
"}";
GLuint program = CompileProgram(vertexShaderSource, fragmentShaderSource);
ASSERT_EQ(0u, program);
}
// Test with a block containing an array of structs.
TEST_P(UniformBufferTest, BlockContainingArrayOfStructs)
{
const std::string &fragmentShader =
"#version 300 es\n"
"precision highp float;\n"
"out vec4 my_FragColor;\n"
"struct light_t {\n"
" vec4 intensity;\n"
"};\n"
"const int maxLights = 2;\n"
"layout(std140) uniform lightData { light_t lights[maxLights]; };\n"
"vec4 processLight(vec4 lighting, light_t light)\n"
"{\n"
" return lighting + light.intensity;\n"
"}\n"
"void main()\n"
"{\n"
" vec4 lighting = vec4(0, 0, 0, 1);\n"
" for (int n = 0; n < maxLights; n++)\n"
" {\n"
" lighting = processLight(lighting, lights[n]);\n"
" }\n"
" my_FragColor = lighting;\n"
"}\n";
ANGLE_GL_PROGRAM(program, mVertexShaderSource, fragmentShader);
GLint uniformBufferIndex = glGetUniformBlockIndex(program, "lightData");
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
const GLsizei kStructCount = 2;
const GLsizei kVectorElementCount = 4;
const GLsizei kBytesPerElement = 4;
const GLsizei kDataSize = kStructCount * kVectorElementCount * kBytesPerElement;
std::vector<GLubyte> v(kDataSize, 0);
float *vAsFloat = reinterpret_cast<float *>(v.data());
vAsFloat[1] = 0.5f;
vAsFloat[kVectorElementCount + 1] = 0.5f;
glBufferData(GL_UNIFORM_BUFFER, kDataSize, v.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, mUniformBuffer);
glUniformBlockBinding(program, uniformBufferIndex, 0);
drawQuad(program.get(), "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Test with a block containing an array of structs containing arrays.
TEST_P(UniformBufferTest, BlockContainingArrayOfStructsContainingArrays)
{
const std::string &fragmentShader =
"#version 300 es\n"
"precision highp float;\n"
"out vec4 my_FragColor;\n"
"struct light_t {\n"
" vec4 intensity[3];\n"
"};\n"
"const int maxLights = 2;\n"
"layout(std140) uniform lightData { light_t lights[maxLights]; };\n"
"vec4 processLight(vec4 lighting, light_t light)\n"
"{\n"
" return lighting + light.intensity[1];\n"
"}\n"
"void main()\n"
"{\n"
" vec4 lighting = vec4(0, 0, 0, 1);\n"
" for (int n = 0; n < maxLights; n++)\n"
" {\n"
" lighting = processLight(lighting, lights[n]);\n"
" }\n"
" my_FragColor = lighting;\n"
"}\n";
ANGLE_GL_PROGRAM(program, mVertexShaderSource, fragmentShader);
GLint uniformBufferIndex = glGetUniformBlockIndex(program, "lightData");
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
const GLsizei kStructCount = 2;
const GLsizei kVectorsPerStruct = 3;
const GLsizei kElementsPerVector = 4;
const GLsizei kBytesPerElement = 4;
const GLsizei kDataSize =
kStructCount * kVectorsPerStruct * kElementsPerVector * kBytesPerElement;
std::vector<GLubyte> v(kDataSize, 0);
float *vAsFloat = reinterpret_cast<float *>(v.data());
vAsFloat[kElementsPerVector + 1] = 0.5f;
vAsFloat[kVectorsPerStruct * kElementsPerVector + kElementsPerVector + 1] = 0.5f;
glBufferData(GL_UNIFORM_BUFFER, kDataSize, v.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, mUniformBuffer);
glUniformBlockBinding(program, uniformBufferIndex, 0);
drawQuad(program.get(), "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Test with a block containing nested structs.
TEST_P(UniformBufferTest, BlockContainingNestedStructs)
{
const std::string &fragmentShader =
"#version 300 es\n"
"precision highp float;\n"
"out vec4 my_FragColor;\n"
"struct light_t {\n"
" vec4 intensity;\n"
"};\n"
"struct lightWrapper_t {\n"
" light_t light;\n"
"};\n"
"const int maxLights = 2;\n"
"layout(std140) uniform lightData { lightWrapper_t lightWrapper; };\n"
"vec4 processLight(vec4 lighting, lightWrapper_t aLightWrapper)\n"
"{\n"
" return lighting + aLightWrapper.light.intensity;\n"
"}\n"
"void main()\n"
"{\n"
" vec4 lighting = vec4(0, 0, 0, 1);\n"
" for (int n = 0; n < maxLights; n++)\n"
" {\n"
" lighting = processLight(lighting, lightWrapper);\n"
" }\n"
" my_FragColor = lighting;\n"
"}\n";
ANGLE_GL_PROGRAM(program, mVertexShaderSource, fragmentShader);
GLint uniformBufferIndex = glGetUniformBlockIndex(program, "lightData");
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
const GLsizei kVectorsPerStruct = 3;
const GLsizei kElementsPerVector = 4;
const GLsizei kBytesPerElement = 4;
const GLsizei kDataSize = kVectorsPerStruct * kElementsPerVector * kBytesPerElement;
std::vector<GLubyte> v(kDataSize, 0);
float *vAsFloat = reinterpret_cast<float *>(v.data());
vAsFloat[1] = 1.0f;
glBufferData(GL_UNIFORM_BUFFER, kDataSize, v.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, mUniformBuffer);
glUniformBlockBinding(program, uniformBufferIndex, 0);
drawQuad(program.get(), "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Tests GetUniformBlockIndex return value on error.
TEST_P(UniformBufferTest, GetUniformBlockIndexDefaultReturn)
{
ASSERT_FALSE(glIsProgram(99));
EXPECT_EQ(GL_INVALID_INDEX, glGetUniformBlockIndex(99, "farts"));
EXPECT_GL_ERROR(GL_INVALID_VALUE);
}
// Block names can be reserved names in GLSL, as long as they're not reserved in GLSL ES.
TEST_P(UniformBufferTest, UniformBlockReservedOpenGLName)
{
const std::string &fragmentShader =
"#version 300 es\n"
"precision highp float;\n"
"out vec4 my_FragColor;\n"
"layout(std140) uniform buffer { vec4 color; };\n"
"void main()\n"
"{\n"
" my_FragColor = color;\n"
"}\n";
ANGLE_GL_PROGRAM(program, mVertexShaderSource, fragmentShader);
GLint uniformBufferIndex = glGetUniformBlockIndex(program, "buffer");
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
const GLsizei kElementsPerVector = 4;
const GLsizei kBytesPerElement = 4;
const GLsizei kDataSize = kElementsPerVector * kBytesPerElement;
std::vector<GLubyte> v(kDataSize, 0);
float *vAsFloat = reinterpret_cast<float *>(v.data());
vAsFloat[1] = 1.0f;
vAsFloat[3] = 1.0f;
glBufferData(GL_UNIFORM_BUFFER, kDataSize, v.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, mUniformBuffer);
glUniformBlockBinding(program, uniformBufferIndex, 0);
drawQuad(program.get(), "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Block instance names can be reserved names in GLSL, as long as they're not reserved in GLSL ES.
TEST_P(UniformBufferTest, UniformBlockInstanceReservedOpenGLName)
{
const std::string &fragmentShader =
"#version 300 es\n"
"precision highp float;\n"
"out vec4 my_FragColor;\n"
"layout(std140) uniform dmat2 { vec4 color; } buffer;\n"
"void main()\n"
"{\n"
" my_FragColor = buffer.color;\n"
"}\n";
ANGLE_GL_PROGRAM(program, mVertexShaderSource, fragmentShader);
GLint uniformBufferIndex = glGetUniformBlockIndex(program, "dmat2");
glBindBuffer(GL_UNIFORM_BUFFER, mUniformBuffer);
const GLsizei kElementsPerVector = 4;
const GLsizei kBytesPerElement = 4;
const GLsizei kDataSize = kElementsPerVector * kBytesPerElement;
std::vector<GLubyte> v(kDataSize, 0);
float *vAsFloat = reinterpret_cast<float *>(v.data());
vAsFloat[1] = 1.0f;
vAsFloat[3] = 1.0f;
glBufferData(GL_UNIFORM_BUFFER, kDataSize, v.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 0, mUniformBuffer);
glUniformBlockBinding(program, uniformBufferIndex, 0);
drawQuad(program.get(), "position", 0.5f);
EXPECT_PIXEL_COLOR_EQ(0, 0, GLColor::green);
}
// Use this to select which configurations (e.g. which renderer, which GLES major version) these tests should be run against.
ANGLE_INSTANTIATE_TEST(UniformBufferTest,
ES3_D3D11(),
ES3_D3D11_FL11_1(),
ES3_D3D11_FL11_1_REFERENCE(),
ES3_OPENGL(),
ES3_OPENGLES());
ANGLE_INSTANTIATE_TEST(UniformBufferTest31, ES31_D3D11(), ES31_OPENGL(), ES31_OPENGLES());
} // namespace