Hash :
2207213b
Author :
Date :
2014-11-20T15:15:01
Move as many files as possible from common to libANGLE. BUG=angle:733 Change-Id: If01c91cd52ac5c2102276a9fdc4b68ebc13e47f9 Reviewed-on: https://chromium-review.googlesource.com/231850 Reviewed-by: Jamie Madill <jmadill@chromium.org> Tested-by: Geoff Lang <geofflang@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 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
//
// Copyright (c) 2002-2014 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.
//
// Program.cpp: Implements the gl::Program class. Implements GL program objects
// and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28.
#include "libANGLE/BinaryStream.h"
#include "libANGLE/ProgramBinary.h"
#include "libANGLE/Framebuffer.h"
#include "libANGLE/FramebufferAttachment.h"
#include "libANGLE/Renderbuffer.h"
#include "libANGLE/renderer/ShaderExecutable.h"
#include "libANGLE/Shader.h"
#include "libANGLE/Program.h"
#include "libANGLE/renderer/ProgramImpl.h"
#include "libANGLE/renderer/d3d/ShaderD3D.h"
#include "libANGLE/Context.h"
#include "libANGLE/Buffer.h"
#include "libANGLE/features.h"
#include "common/blocklayout.h"
#include "common/debug.h"
#include "common/utilities.h"
#include "common/platform.h"
#include "common/version.h"
namespace gl
{
namespace
{
unsigned int ParseAndStripArrayIndex(std::string* name)
{
unsigned int subscript = GL_INVALID_INDEX;
// Strip any trailing array operator and retrieve the subscript
size_t open = name->find_last_of('[');
size_t close = name->find_last_of(']');
if (open != std::string::npos && close == name->length() - 1)
{
subscript = atoi(name->substr(open + 1).c_str());
name->erase(open);
}
return subscript;
}
}
VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index)
: name(name), element(element), index(index)
{
}
LinkedVarying::LinkedVarying()
{
}
LinkedVarying::LinkedVarying(const std::string &name, GLenum type, GLsizei size, const std::string &semanticName,
unsigned int semanticIndex, unsigned int semanticIndexCount)
: name(name), type(type), size(size), semanticName(semanticName), semanticIndex(semanticIndex), semanticIndexCount(semanticIndexCount)
{
}
LinkResult::LinkResult(bool linkSuccess, const Error &error)
: linkSuccess(linkSuccess),
error(error)
{
}
unsigned int ProgramBinary::mCurrentSerial = 1;
ProgramBinary::ProgramBinary(rx::ProgramImpl *impl)
: RefCountObject(0),
mProgram(impl),
mValidated(false),
mSerial(issueSerial())
{
ASSERT(impl);
for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
{
mSemanticIndex[index] = -1;
}
}
ProgramBinary::~ProgramBinary()
{
reset();
SafeDelete(mProgram);
}
unsigned int ProgramBinary::getSerial() const
{
return mSerial;
}
unsigned int ProgramBinary::issueSerial()
{
return mCurrentSerial++;
}
GLuint ProgramBinary::getAttributeLocation(const char *name)
{
if (name)
{
for (int index = 0; index < MAX_VERTEX_ATTRIBS; index++)
{
if (mLinkedAttribute[index].name == std::string(name))
{
return index;
}
}
}
return -1;
}
int ProgramBinary::getSemanticIndex(int attributeIndex)
{
ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS);
return mSemanticIndex[attributeIndex];
}
// Returns one more than the highest sampler index used.
GLint ProgramBinary::getUsedSamplerRange(SamplerType type)
{
return mProgram->getUsedSamplerRange(type);
}
bool ProgramBinary::usesPointSize() const
{
return mProgram->usesPointSize();
}
GLint ProgramBinary::getSamplerMapping(SamplerType type, unsigned int samplerIndex, const Caps &caps)
{
return mProgram->getSamplerMapping(type, samplerIndex, caps);
}
GLenum ProgramBinary::getSamplerTextureType(SamplerType type, unsigned int samplerIndex)
{
return mProgram->getSamplerTextureType(type, samplerIndex);
}
GLint ProgramBinary::getUniformLocation(std::string name)
{
return mProgram->getUniformLocation(name);
}
GLuint ProgramBinary::getUniformIndex(std::string name)
{
return mProgram->getUniformIndex(name);
}
GLuint ProgramBinary::getUniformBlockIndex(std::string name)
{
return mProgram->getUniformBlockIndex(name);
}
UniformBlock *ProgramBinary::getUniformBlockByIndex(GLuint blockIndex)
{
return mProgram->getUniformBlockByIndex(blockIndex);
}
GLint ProgramBinary::getFragDataLocation(const char *name) const
{
std::string baseName(name);
unsigned int arrayIndex;
arrayIndex = ParseAndStripArrayIndex(&baseName);
for (auto locationIt = mOutputVariables.begin(); locationIt != mOutputVariables.end(); locationIt++)
{
const VariableLocation &outputVariable = locationIt->second;
if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element))
{
return static_cast<GLint>(locationIt->first);
}
}
return -1;
}
size_t ProgramBinary::getTransformFeedbackVaryingCount() const
{
return mProgram->getTransformFeedbackLinkedVaryings().size();
}
const LinkedVarying &ProgramBinary::getTransformFeedbackVarying(size_t idx) const
{
return mProgram->getTransformFeedbackLinkedVaryings()[idx];
}
GLenum ProgramBinary::getTransformFeedbackBufferMode() const
{
return mProgram->getTransformFeedbackBufferMode();
}
void ProgramBinary::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) {
mProgram->setUniform1fv(location, count, v);
}
void ProgramBinary::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) {
mProgram->setUniform2fv(location, count, v);
}
void ProgramBinary::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) {
mProgram->setUniform3fv(location, count, v);
}
void ProgramBinary::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) {
mProgram->setUniform4fv(location, count, v);
}
void ProgramBinary::setUniform1iv(GLint location, GLsizei count, const GLint *v) {
mProgram->setUniform1iv(location, count, v);
}
void ProgramBinary::setUniform2iv(GLint location, GLsizei count, const GLint *v) {
mProgram->setUniform2iv(location, count, v);
}
void ProgramBinary::setUniform3iv(GLint location, GLsizei count, const GLint *v) {
mProgram->setUniform3iv(location, count, v);
}
void ProgramBinary::setUniform4iv(GLint location, GLsizei count, const GLint *v) {
mProgram->setUniform4iv(location, count, v);
}
void ProgramBinary::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) {
mProgram->setUniform1uiv(location, count, v);
}
void ProgramBinary::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) {
mProgram->setUniform2uiv(location, count, v);
}
void ProgramBinary::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) {
mProgram->setUniform3uiv(location, count, v);
}
void ProgramBinary::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) {
mProgram->setUniform4uiv(location, count, v);
}
void ProgramBinary::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
mProgram->setUniformMatrix2fv(location, count, transpose, v);
}
void ProgramBinary::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
mProgram->setUniformMatrix3fv(location, count, transpose, v);
}
void ProgramBinary::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
mProgram->setUniformMatrix4fv(location, count, transpose, v);
}
void ProgramBinary::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
mProgram->setUniformMatrix2x3fv(location, count, transpose, v);
}
void ProgramBinary::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
mProgram->setUniformMatrix2x4fv(location, count, transpose, v);
}
void ProgramBinary::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
mProgram->setUniformMatrix3x2fv(location, count, transpose, v);
}
void ProgramBinary::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
mProgram->setUniformMatrix3x4fv(location, count, transpose, v);
}
void ProgramBinary::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
mProgram->setUniformMatrix4x2fv(location, count, transpose, v);
}
void ProgramBinary::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) {
mProgram->setUniformMatrix4x3fv(location, count, transpose, v);
}
void ProgramBinary::getUniformfv(GLint location, GLfloat *v) {
mProgram->getUniformfv(location, v);
}
void ProgramBinary::getUniformiv(GLint location, GLint *v) {
mProgram->getUniformiv(location, v);
}
void ProgramBinary::getUniformuiv(GLint location, GLuint *v) {
mProgram->getUniformuiv(location, v);
}
void ProgramBinary::updateSamplerMapping()
{
return mProgram->updateSamplerMapping();
}
// Applies all the uniforms set for this program object to the renderer
Error ProgramBinary::applyUniforms()
{
return mProgram->applyUniforms();
}
Error ProgramBinary::applyUniformBuffers(const std::vector<gl::Buffer*> boundBuffers, const Caps &caps)
{
return mProgram->applyUniformBuffers(boundBuffers, caps);
}
bool ProgramBinary::linkVaryings(InfoLog &infoLog, Shader *fragmentShader, Shader *vertexShader)
{
std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings();
std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings();
for (size_t fragVaryingIndex = 0; fragVaryingIndex < fragmentVaryings.size(); fragVaryingIndex++)
{
PackedVarying *input = &fragmentVaryings[fragVaryingIndex];
bool matched = false;
// Built-in varyings obey special rules
if (input->isBuiltIn())
{
continue;
}
for (size_t vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++)
{
PackedVarying *output = &vertexVaryings[vertVaryingIndex];
if (output->name == input->name)
{
if (!linkValidateVaryings(infoLog, output->name, *input, *output))
{
return false;
}
output->registerIndex = input->registerIndex;
output->columnIndex = input->columnIndex;
matched = true;
break;
}
}
// We permit unmatched, unreferenced varyings
if (!matched && input->staticUse)
{
infoLog.append("Fragment varying %s does not match any vertex varying", input->name.c_str());
return false;
}
}
return true;
}
LinkResult ProgramBinary::load(InfoLog &infoLog, GLenum binaryFormat, const void *binary, GLsizei length)
{
#if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_DISABLED
return LinkResult(false, Error(GL_NO_ERROR));
#else
ASSERT(binaryFormat == mProgram->getBinaryFormat());
reset();
BinaryInputStream stream(binary, length);
GLenum format = stream.readInt<GLenum>();
if (format != mProgram->getBinaryFormat())
{
infoLog.append("Invalid program binary format.");
return LinkResult(false, Error(GL_NO_ERROR));
}
int majorVersion = stream.readInt<int>();
int minorVersion = stream.readInt<int>();
if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION)
{
infoLog.append("Invalid program binary version.");
return LinkResult(false, Error(GL_NO_ERROR));
}
unsigned char commitString[ANGLE_COMMIT_HASH_SIZE];
stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE);
if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0)
{
infoLog.append("Invalid program binary version.");
return LinkResult(false, Error(GL_NO_ERROR));
}
int compileFlags = stream.readInt<int>();
if (compileFlags != ANGLE_COMPILE_OPTIMIZATION_LEVEL)
{
infoLog.append("Mismatched compilation flags.");
return LinkResult(false, Error(GL_NO_ERROR));
}
for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
{
stream.readInt(&mLinkedAttribute[i].type);
stream.readString(&mLinkedAttribute[i].name);
stream.readInt(&mProgram->getShaderAttributes()[i].type);
stream.readString(&mProgram->getShaderAttributes()[i].name);
stream.readInt(&mSemanticIndex[i]);
}
initAttributesByLayout();
LinkResult result = mProgram->load(infoLog, &stream);
if (result.error.isError() || !result.linkSuccess)
{
return result;
}
return LinkResult(true, Error(GL_NO_ERROR));
#endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED
}
Error ProgramBinary::save(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length)
{
if (binaryFormat)
{
*binaryFormat = mProgram->getBinaryFormat();
}
BinaryOutputStream stream;
stream.writeInt(mProgram->getBinaryFormat());
stream.writeInt(ANGLE_MAJOR_VERSION);
stream.writeInt(ANGLE_MINOR_VERSION);
stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE);
stream.writeInt(ANGLE_COMPILE_OPTIMIZATION_LEVEL);
for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i)
{
stream.writeInt(mLinkedAttribute[i].type);
stream.writeString(mLinkedAttribute[i].name);
stream.writeInt(mProgram->getShaderAttributes()[i].type);
stream.writeString(mProgram->getShaderAttributes()[i].name);
stream.writeInt(mSemanticIndex[i]);
}
mProgram->save(&stream);
GLsizei streamLength = stream.length();
const void *streamData = stream.data();
if (streamLength > bufSize)
{
if (length)
{
*length = 0;
}
// TODO: This should be moved to the validation layer but computing the size of the binary before saving
// it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate
// sizes and then copy it.
return Error(GL_INVALID_OPERATION);
}
if (binary)
{
char *ptr = (char*) binary;
memcpy(ptr, streamData, streamLength);
ptr += streamLength;
ASSERT(ptr - streamLength == binary);
}
if (length)
{
*length = streamLength;
}
return Error(GL_NO_ERROR);
}
GLint ProgramBinary::getLength()
{
GLint length;
Error error = save(NULL, NULL, INT_MAX, &length);
if (error.isError())
{
return 0;
}
return length;
}
LinkResult ProgramBinary::link(const Data &data, InfoLog &infoLog, const AttributeBindings &attributeBindings,
Shader *fragmentShader, Shader *vertexShader,
const std::vector<std::string> &transformFeedbackVaryings,
GLenum transformFeedbackBufferMode)
{
if (!fragmentShader || !fragmentShader->isCompiled())
{
return LinkResult(false, Error(GL_NO_ERROR));
}
ASSERT(fragmentShader->getType() == GL_FRAGMENT_SHADER);
if (!vertexShader || !vertexShader->isCompiled())
{
return LinkResult(false, Error(GL_NO_ERROR));
}
ASSERT(vertexShader->getType() == GL_VERTEX_SHADER);
reset();
int registers;
std::vector<LinkedVarying> linkedVaryings;
LinkResult result = mProgram->link(data, infoLog, fragmentShader, vertexShader, transformFeedbackVaryings, transformFeedbackBufferMode,
®isters, &linkedVaryings, &mOutputVariables);
if (result.error.isError() || !result.linkSuccess)
{
return result;
}
if (!linkAttributes(infoLog, attributeBindings, vertexShader))
{
return LinkResult(false, Error(GL_NO_ERROR));
}
if (!mProgram->linkUniforms(infoLog, *vertexShader, *fragmentShader, *data.caps))
{
return LinkResult(false, Error(GL_NO_ERROR));
}
if (!linkUniformBlocks(infoLog, *vertexShader, *fragmentShader, *data.caps))
{
return LinkResult(false, Error(GL_NO_ERROR));
}
if (!gatherTransformFeedbackLinkedVaryings(infoLog, linkedVaryings, transformFeedbackVaryings,
transformFeedbackBufferMode, &mProgram->getTransformFeedbackLinkedVaryings(), *data.caps))
{
return LinkResult(false, Error(GL_NO_ERROR));
}
// TODO: The concept of "executables" is D3D only, and as such this belongs in ProgramD3D. It must be called,
// however, last in this function, so it can't simply be moved to ProgramD3D::link without further shuffling.
result = mProgram->compileProgramExecutables(infoLog, fragmentShader, vertexShader, registers);
if (result.error.isError() || !result.linkSuccess)
{
infoLog.append("Failed to create D3D shaders.");
reset();
return result;
}
return LinkResult(true, Error(GL_NO_ERROR));
}
bool ProgramBinary::linkUniformBlocks(gl::InfoLog &infoLog, const gl::Shader &vertexShader, const gl::Shader &fragmentShader,
const gl::Caps &caps)
{
const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks();
const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks();
// Check that interface blocks defined in the vertex and fragment shaders are identical
typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap;
UniformBlockMap linkedUniformBlocks;
for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
{
const sh::InterfaceBlock &vertexInterfaceBlock = vertexInterfaceBlocks[blockIndex];
linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock;
}
for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
{
const sh::InterfaceBlock &fragmentInterfaceBlock = fragmentInterfaceBlocks[blockIndex];
UniformBlockMap::const_iterator entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name);
if (entry != linkedUniformBlocks.end())
{
const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second;
if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock))
{
return false;
}
}
}
for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++)
{
const sh::InterfaceBlock &interfaceBlock = vertexInterfaceBlocks[blockIndex];
// Note: shared and std140 layouts are always considered active
if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
{
if (!mProgram->defineUniformBlock(infoLog, vertexShader, interfaceBlock, caps))
{
return false;
}
}
}
for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++)
{
const sh::InterfaceBlock &interfaceBlock = fragmentInterfaceBlocks[blockIndex];
// Note: shared and std140 layouts are always considered active
if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED)
{
if (!mProgram->defineUniformBlock(infoLog, fragmentShader, interfaceBlock, caps))
{
return false;
}
}
}
return true;
}
bool ProgramBinary::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock,
const sh::InterfaceBlock &fragmentInterfaceBlock)
{
const char* blockName = vertexInterfaceBlock.name.c_str();
// validate blocks for the same member types
if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size())
{
infoLog.append("Types for interface block '%s' differ between vertex and fragment shaders", blockName);
return false;
}
if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize)
{
infoLog.append("Array sizes differ for interface block '%s' between vertex and fragment shaders", blockName);
return false;
}
if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout)
{
infoLog.append("Layout qualifiers differ for interface block '%s' between vertex and fragment shaders", blockName);
return false;
}
const unsigned int numBlockMembers = vertexInterfaceBlock.fields.size();
for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++)
{
const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex];
const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex];
if (vertexMember.name != fragmentMember.name)
{
infoLog.append("Name mismatch for field %d of interface block '%s': (in vertex: '%s', in fragment: '%s')",
blockMemberIndex, blockName, vertexMember.name.c_str(), fragmentMember.name.c_str());
return false;
}
std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'";
if (!gl::ProgramBinary::linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember))
{
return false;
}
}
return true;
}
// Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices
bool ProgramBinary::linkAttributes(InfoLog &infoLog, const AttributeBindings &attributeBindings, const Shader *vertexShader)
{
const rx::ShaderD3D *vertexShaderD3D = rx::ShaderD3D::makeShaderD3D(vertexShader->getImplementation());
unsigned int usedLocations = 0;
const std::vector<sh::Attribute> &shaderAttributes = vertexShader->getActiveAttributes();
// Link attributes that have a binding location
for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
{
const sh::Attribute &attribute = shaderAttributes[attributeIndex];
ASSERT(attribute.staticUse);
const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
mProgram->getShaderAttributes()[attributeIndex] = attribute;
if (location != -1) // Set by glBindAttribLocation or by location layout qualifier
{
const int rows = VariableRegisterCount(attribute.type);
if (rows + location > MAX_VERTEX_ATTRIBS)
{
infoLog.append("Active attribute (%s) at location %d is too big to fit", attribute.name.c_str(), location);
return false;
}
for (int row = 0; row < rows; row++)
{
const int rowLocation = location + row;
sh::ShaderVariable &linkedAttribute = mLinkedAttribute[rowLocation];
// In GLSL 3.00, attribute aliasing produces a link error
// In GLSL 1.00, attribute aliasing is allowed
if (mProgram->getShaderVersion() >= 300)
{
if (!linkedAttribute.name.empty())
{
infoLog.append("Attribute '%s' aliases attribute '%s' at location %d", attribute.name.c_str(), linkedAttribute.name.c_str(), rowLocation);
return false;
}
}
linkedAttribute = attribute;
usedLocations |= 1 << rowLocation;
}
}
}
// Link attributes that don't have a binding location
for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++)
{
const sh::Attribute &attribute = shaderAttributes[attributeIndex];
ASSERT(attribute.staticUse);
const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location;
if (location == -1) // Not set by glBindAttribLocation or by location layout qualifier
{
int rows = VariableRegisterCount(attribute.type);
int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, MAX_VERTEX_ATTRIBS);
if (availableIndex == -1 || availableIndex + rows > MAX_VERTEX_ATTRIBS)
{
infoLog.append("Too many active attributes (%s)", attribute.name.c_str());
return false; // Fail to link
}
mLinkedAttribute[availableIndex] = attribute;
}
}
for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; )
{
int index = vertexShaderD3D->getSemanticIndex(mLinkedAttribute[attributeIndex].name);
int rows = VariableRegisterCount(mLinkedAttribute[attributeIndex].type);
for (int r = 0; r < rows; r++)
{
mSemanticIndex[attributeIndex++] = index++;
}
}
initAttributesByLayout();
return true;
}
bool ProgramBinary::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable,
const sh::ShaderVariable &fragmentVariable, bool validatePrecision)
{
if (vertexVariable.type != fragmentVariable.type)
{
infoLog.append("Types for %s differ between vertex and fragment shaders", variableName.c_str());
return false;
}
if (vertexVariable.arraySize != fragmentVariable.arraySize)
{
infoLog.append("Array sizes for %s differ between vertex and fragment shaders", variableName.c_str());
return false;
}
if (validatePrecision && vertexVariable.precision != fragmentVariable.precision)
{
infoLog.append("Precisions for %s differ between vertex and fragment shaders", variableName.c_str());
return false;
}
if (vertexVariable.fields.size() != fragmentVariable.fields.size())
{
infoLog.append("Structure lengths for %s differ between vertex and fragment shaders", variableName.c_str());
return false;
}
const unsigned int numMembers = vertexVariable.fields.size();
for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++)
{
const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex];
const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex];
if (vertexMember.name != fragmentMember.name)
{
infoLog.append("Name mismatch for field '%d' of %s: (in vertex: '%s', in fragment: '%s')",
memberIndex, variableName.c_str(),
vertexMember.name.c_str(), fragmentMember.name.c_str());
return false;
}
const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." +
vertexMember.name + "'";
if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision))
{
return false;
}
}
return true;
}
bool ProgramBinary::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform)
{
if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
{
return false;
}
return true;
}
bool ProgramBinary::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying)
{
if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false))
{
return false;
}
if (vertexVarying.interpolation != fragmentVarying.interpolation)
{
infoLog.append("Interpolation types for %s differ between vertex and fragment shaders", varyingName.c_str());
return false;
}
return true;
}
bool ProgramBinary::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform)
{
if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true))
{
return false;
}
if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout)
{
infoLog.append("Matrix packings for %s differ between vertex and fragment shaders", uniformName.c_str());
return false;
}
return true;
}
bool ProgramBinary::gatherTransformFeedbackLinkedVaryings(InfoLog &infoLog, const std::vector<LinkedVarying> &linkedVaryings,
const std::vector<std::string> &transformFeedbackVaryingNames,
GLenum transformFeedbackBufferMode,
std::vector<LinkedVarying> *outTransformFeedbackLinkedVaryings,
const Caps &caps) const
{
size_t totalComponents = 0;
// Gather the linked varyings that are used for transform feedback, they should all exist.
outTransformFeedbackLinkedVaryings->clear();
for (size_t i = 0; i < transformFeedbackVaryingNames.size(); i++)
{
bool found = false;
for (size_t j = 0; j < linkedVaryings.size(); j++)
{
if (transformFeedbackVaryingNames[i] == linkedVaryings[j].name)
{
for (size_t k = 0; k < outTransformFeedbackLinkedVaryings->size(); k++)
{
if (outTransformFeedbackLinkedVaryings->at(k).name == linkedVaryings[j].name)
{
infoLog.append("Two transform feedback varyings specify the same output variable (%s).", linkedVaryings[j].name.c_str());
return false;
}
}
size_t componentCount = linkedVaryings[j].semanticIndexCount * 4;
if (transformFeedbackBufferMode == GL_SEPARATE_ATTRIBS &&
componentCount > caps.maxTransformFeedbackSeparateComponents)
{
infoLog.append("Transform feedback varying's %s components (%u) exceed the maximum separate components (%u).",
linkedVaryings[j].name.c_str(), componentCount, caps.maxTransformFeedbackSeparateComponents);
return false;
}
totalComponents += componentCount;
outTransformFeedbackLinkedVaryings->push_back(linkedVaryings[j]);
found = true;
break;
}
}
// All transform feedback varyings are expected to exist since packVaryings checks for them.
ASSERT(found);
}
if (transformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS && totalComponents > caps.maxTransformFeedbackInterleavedComponents)
{
infoLog.append("Transform feedback varying total components (%u) exceed the maximum interleaved components (%u).",
totalComponents, caps.maxTransformFeedbackInterleavedComponents);
return false;
}
return true;
}
bool ProgramBinary::isValidated() const
{
return mValidated;
}
void ProgramBinary::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
{
// Skip over inactive attributes
unsigned int activeAttribute = 0;
unsigned int attribute;
for (attribute = 0; attribute < MAX_VERTEX_ATTRIBS; attribute++)
{
if (mLinkedAttribute[attribute].name.empty())
{
continue;
}
if (activeAttribute == index)
{
break;
}
activeAttribute++;
}
if (bufsize > 0)
{
const char *string = mLinkedAttribute[attribute].name.c_str();
strncpy(name, string, bufsize);
name[bufsize - 1] = '\0';
if (length)
{
*length = strlen(name);
}
}
*size = 1; // Always a single 'type' instance
*type = mLinkedAttribute[attribute].type;
}
GLint ProgramBinary::getActiveAttributeCount() const
{
int count = 0;
for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
{
if (!mLinkedAttribute[attributeIndex].name.empty())
{
count++;
}
}
return count;
}
GLint ProgramBinary::getActiveAttributeMaxLength() const
{
int maxLength = 0;
for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++)
{
if (!mLinkedAttribute[attributeIndex].name.empty())
{
maxLength = std::max((int)(mLinkedAttribute[attributeIndex].name.length() + 1), maxLength);
}
}
return maxLength;
}
void ProgramBinary::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) const
{
ASSERT(index < mProgram->getUniforms().size()); // index must be smaller than getActiveUniformCount()
if (bufsize > 0)
{
std::string string = mProgram->getUniforms()[index]->name;
if (mProgram->getUniforms()[index]->isArray())
{
string += "[0]";
}
strncpy(name, string.c_str(), bufsize);
name[bufsize - 1] = '\0';
if (length)
{
*length = strlen(name);
}
}
*size = mProgram->getUniforms()[index]->elementCount();
*type = mProgram->getUniforms()[index]->type;
}
GLint ProgramBinary::getActiveUniformCount() const
{
return mProgram->getUniforms().size();
}
GLint ProgramBinary::getActiveUniformMaxLength() const
{
int maxLength = 0;
unsigned int numUniforms = mProgram->getUniforms().size();
for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++)
{
if (!mProgram->getUniforms()[uniformIndex]->name.empty())
{
int length = (int)(mProgram->getUniforms()[uniformIndex]->name.length() + 1);
if (mProgram->getUniforms()[uniformIndex]->isArray())
{
length += 3; // Counting in "[0]".
}
maxLength = std::max(length, maxLength);
}
}
return maxLength;
}
GLint ProgramBinary::getActiveUniformi(GLuint index, GLenum pname) const
{
const gl::LinkedUniform& uniform = *mProgram->getUniforms()[index];
switch (pname)
{
case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type);
case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount());
case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0));
case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex;
case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset;
case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride;
case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride;
case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix);
default:
UNREACHABLE();
break;
}
return 0;
}
bool ProgramBinary::isValidUniformLocation(GLint location) const
{
ASSERT(rx::IsIntegerCastSafe<GLint>(mProgram->getUniformIndices().size()));
return (location >= 0 && location < static_cast<GLint>(mProgram->getUniformIndices().size()));
}
LinkedUniform *ProgramBinary::getUniformByLocation(GLint location) const
{
return mProgram->getUniformByLocation(location);
}
LinkedUniform *ProgramBinary::getUniformByName(const std::string &name) const
{
return mProgram->getUniformByName(name);
}
void ProgramBinary::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const
{
ASSERT(uniformBlockIndex < mProgram->getUniformBlocks().size()); // index must be smaller than getActiveUniformBlockCount()
const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex];
if (bufSize > 0)
{
std::string string = uniformBlock.name;
if (uniformBlock.isArrayElement())
{
string += ArrayString(uniformBlock.elementIndex);
}
strncpy(uniformBlockName, string.c_str(), bufSize);
uniformBlockName[bufSize - 1] = '\0';
if (length)
{
*length = strlen(uniformBlockName);
}
}
}
void ProgramBinary::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const
{
ASSERT(uniformBlockIndex < mProgram->getUniformBlocks().size()); // index must be smaller than getActiveUniformBlockCount()
const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex];
switch (pname)
{
case GL_UNIFORM_BLOCK_DATA_SIZE:
*params = static_cast<GLint>(uniformBlock.dataSize);
break;
case GL_UNIFORM_BLOCK_NAME_LENGTH:
*params = static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArrayElement() ? 3 : 0));
break;
case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS:
*params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size());
break;
case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES:
{
for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++)
{
params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]);
}
}
break;
case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER:
*params = static_cast<GLint>(uniformBlock.isReferencedByVertexShader());
break;
case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER:
*params = static_cast<GLint>(uniformBlock.isReferencedByFragmentShader());
break;
default: UNREACHABLE();
}
}
GLuint ProgramBinary::getActiveUniformBlockCount() const
{
return mProgram->getUniformBlocks().size();
}
GLuint ProgramBinary::getActiveUniformBlockMaxLength() const
{
unsigned int maxLength = 0;
unsigned int numUniformBlocks = mProgram->getUniformBlocks().size();
for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++)
{
const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex];
if (!uniformBlock.name.empty())
{
const unsigned int length = uniformBlock.name.length() + 1;
// Counting in "[0]".
const unsigned int arrayLength = (uniformBlock.isArrayElement() ? 3 : 0);
maxLength = std::max(length + arrayLength, maxLength);
}
}
return maxLength;
}
void ProgramBinary::validate(InfoLog &infoLog, const Caps &caps)
{
applyUniforms();
if (!validateSamplers(&infoLog, caps))
{
mValidated = false;
}
else
{
mValidated = true;
}
}
bool ProgramBinary::validateSamplers(InfoLog *infoLog, const Caps &caps)
{
return mProgram->validateSamplers(infoLog, caps);
}
struct AttributeSorter
{
AttributeSorter(const int (&semanticIndices)[MAX_VERTEX_ATTRIBS])
: originalIndices(semanticIndices)
{
}
bool operator()(int a, int b)
{
if (originalIndices[a] == -1) return false;
if (originalIndices[b] == -1) return true;
return (originalIndices[a] < originalIndices[b]);
}
const int (&originalIndices)[MAX_VERTEX_ATTRIBS];
};
void ProgramBinary::initAttributesByLayout()
{
for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
{
mAttributesByLayout[i] = i;
}
std::sort(&mAttributesByLayout[0], &mAttributesByLayout[MAX_VERTEX_ATTRIBS], AttributeSorter(mSemanticIndex));
}
void ProgramBinary::sortAttributesByLayout(rx::TranslatedAttribute attributes[MAX_VERTEX_ATTRIBS], int sortedSemanticIndices[MAX_VERTEX_ATTRIBS]) const
{
rx::TranslatedAttribute oldTranslatedAttributes[MAX_VERTEX_ATTRIBS];
for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
{
oldTranslatedAttributes[i] = attributes[i];
}
for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++)
{
int oldIndex = mAttributesByLayout[i];
sortedSemanticIndices[i] = mSemanticIndex[oldIndex];
attributes[i] = oldTranslatedAttributes[oldIndex];
}
}
void ProgramBinary::reset()
{
mOutputVariables.clear();
mProgram->reset();
mValidated = false;
}
}