Hash :
0197826b
Author :
Date :
2024-03-22T14:42:34
Revert "Add conversion operator from ImmutableString to std::string"
This reverts commit 8c0dae388bccb00f11cd94d641d719cc68325826.
Reason for revert:
Breaks Android rolls:
external/angle/src/compiler/translator/ImmutableString.h:103:15: error: constexpr function's return type 'std::string' (aka 'basic_string<char, char_traits<char>, allocator<char>>') is not a literal type
103 | constexpr operator std::string() const { return std::string(data(), length()); }
| ^
external/libcxx/include/string:4332:64: note: 'basic_string<char>' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors
4332 | _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_string<char>)
Original change's description:
> Add conversion operator from ImmutableString to std::string
>
> Also use the operator in a couple of places. Sometimes saves a strlen.
>
> Bug: angleproject:8614
> Change-Id: I429f3ac02af04b568ac7d1adf22ab65e5007fbda
> Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5372728
> Reviewed-by: Geoff Lang <geofflang@chromium.org>
> Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com>
> Commit-Queue: Geoff Lang <geofflang@chromium.org>
> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Bug: angleproject:8614
Change-Id: I46963686f9506f7029e164250b1bf81f8ab2c519
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/5388255
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
Commit-Queue: Roman Lavrov <romanl@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 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
//
// Copyright 2020 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 <algorithm>
#include <cstring>
#include <functional>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include "compiler/translator/Compiler.h"
#include "compiler/translator/msl/AstHelpers.h"
#include "compiler/translator/msl/ModifyStruct.h"
#include "compiler/translator/msl/TranslatorMSL.h"
using namespace sh;
////////////////////////////////////////////////////////////////////////////////
size_t ModifiedStructMachineries::size() const
{
return ordering.size();
}
const ModifiedStructMachinery &ModifiedStructMachineries::at(size_t index) const
{
ASSERT(index < size());
const TStructure *s = ordering[index];
const ModifiedStructMachinery *m = find(*s);
ASSERT(m);
return *m;
}
const ModifiedStructMachinery *ModifiedStructMachineries::find(const TStructure &s) const
{
auto iter = originalToMachinery.find(&s);
if (iter == originalToMachinery.end())
{
return nullptr;
}
return &iter->second;
}
void ModifiedStructMachineries::insert(const TStructure &s,
const ModifiedStructMachinery &machinery)
{
ASSERT(!find(s));
originalToMachinery[&s] = machinery;
ordering.push_back(&s);
}
////////////////////////////////////////////////////////////////////////////////
namespace
{
TIntermTyped &Flatten(SymbolEnv &symbolEnv, TIntermTyped &node)
{
auto &type = node.getType();
ASSERT(type.isArray());
auto &retType = InnermostType(type);
retType.makeArray(1);
return symbolEnv.callFunctionOverload(Name("flatten"), retType, *new TIntermSequence{&node});
}
struct FlattenArray
{};
struct PathItem
{
enum class Type
{
Field, // Struct field indexing.
Index, // Array, vector, or matrix indexing.
FlattenArray, // Array of any rank -> pointer of innermost type.
};
PathItem(const TField &field) : field(&field), type(Type::Field) {}
PathItem(int index) : index(index), type(Type::Index) {}
PathItem(unsigned index) : PathItem(static_cast<int>(index)) {}
PathItem(FlattenArray flatten) : type(Type::FlattenArray) {}
union
{
const TField *field;
int index;
};
Type type;
};
TIntermTyped &BuildPathAccess(SymbolEnv &symbolEnv,
const TVariable &var,
const std::vector<PathItem> &path)
{
TIntermTyped *curr = new TIntermSymbol(&var);
for (const PathItem &item : path)
{
switch (item.type)
{
case PathItem::Type::Field:
curr = &AccessField(*curr, Name(*item.field));
break;
case PathItem::Type::Index:
curr = &AccessIndex(*curr, item.index);
break;
case PathItem::Type::FlattenArray:
{
curr = &Flatten(symbolEnv, *curr);
}
break;
}
}
return *curr;
}
////////////////////////////////////////////////////////////////////////////////
using OriginalParam = const TVariable &;
using ModifiedParam = const TVariable &;
using OriginalAccess = TIntermTyped;
using ModifiedAccess = TIntermTyped;
struct Access
{
OriginalAccess &original;
ModifiedAccess &modified;
struct Env
{
const ConvertType type;
};
};
using ConversionFunc = std::function<Access(Access::Env &, OriginalAccess &, ModifiedAccess &)>;
class ConvertStructState : angle::NonCopyable
{
private:
struct ConversionInfo
{
ConversionFunc stdFunc;
const TFunction *astFunc;
std::vector<PathItem> pathItems;
Name modifiedFieldName;
};
public:
ConvertStructState(TCompiler &compiler,
SymbolEnv &symbolEnv,
IdGen &idGen,
const ModifyStructConfig &config,
ModifiedStructMachineries &outMachineries,
const bool isUBO,
const bool useAttributeAliasing)
: mCompiler(compiler),
config(config),
symbolEnv(symbolEnv),
modifiedFields(*new TFieldList()),
symbolTable(symbolEnv.symbolTable()),
idGen(idGen),
outMachineries(outMachineries),
isUBO(isUBO),
useAttributeAliasing(useAttributeAliasing)
{}
~ConvertStructState()
{
ASSERT(namePath.empty());
ASSERT(namePathSizes.empty());
}
void publish(const TStructure &originalStruct, const Name &modifiedStructName)
{
const bool isOriginalToModified = config.convertType == ConvertType::OriginalToModified;
auto &modifiedStruct = *new TStructure(&symbolTable, modifiedStructName.rawName(),
&modifiedFields, modifiedStructName.symbolType());
auto &func = *new TFunction(
&symbolTable,
idGen.createNewName(isOriginalToModified ? "originalToModified" : "modifiedToOriginal")
.rawName(),
SymbolType::AngleInternal, new TType(TBasicType::EbtVoid), false);
OriginalParam originalParam =
CreateInstanceVariable(symbolTable, originalStruct, Name("original"));
ModifiedParam modifiedParam =
CreateInstanceVariable(symbolTable, modifiedStruct, Name("modified"));
symbolEnv.markAsReference(originalParam, AddressSpace::Thread);
symbolEnv.markAsReference(modifiedParam, config.externalAddressSpace);
if (isOriginalToModified)
{
func.addParameter(&originalParam);
func.addParameter(&modifiedParam);
}
else
{
func.addParameter(&modifiedParam);
func.addParameter(&originalParam);
}
TIntermBlock &body = *new TIntermBlock();
Access::Env env{config.convertType};
for (ConversionInfo &info : conversionInfos)
{
auto convert = [&](OriginalAccess &original, ModifiedAccess &modified) {
if (info.astFunc)
{
ASSERT(!info.stdFunc);
TIntermTyped &src = isOriginalToModified ? modified : original;
TIntermTyped &dest = isOriginalToModified ? original : modified;
body.appendStatement(TIntermAggregate::CreateFunctionCall(
*info.astFunc, new TIntermSequence{&dest, &src}));
}
else
{
ASSERT(info.stdFunc);
Access access = info.stdFunc(env, original, modified);
TIntermTyped &src = isOriginalToModified ? access.original : access.modified;
TIntermTyped &dest = isOriginalToModified ? access.modified : access.original;
body.appendStatement(new TIntermBinary(TOperator::EOpAssign, &dest, &src));
}
};
OriginalAccess *original = &BuildPathAccess(symbolEnv, originalParam, info.pathItems);
ModifiedAccess *modified = &AccessField(modifiedParam, info.modifiedFieldName);
if (useAttributeAliasing)
{
std::ostringstream aliasedName;
aliasedName << "ANGLE_ALIASED_" << info.modifiedFieldName;
TType *placeholderType = new TType(modified->getType());
placeholderType->setQualifier(EvqSpecConst);
modified = new TIntermSymbol(
new TVariable(&symbolTable, sh::ImmutableString(aliasedName.str()),
placeholderType, SymbolType::AngleInternal));
}
const TType ot = original->getType();
const TType mt = modified->getType();
ASSERT(ot.isArray() == mt.isArray());
// Clip distance output uses float[n] type, so the field must be assigned per-element
// when filling the modified struct. Explicit path name is used because original types
// are not available here.
if (ot.isArray() &&
(ot.getLayoutQualifier().matrixPacking == EmpRowMajor || ot != mt ||
info.modifiedFieldName == Name("gl_ClipDistance", SymbolType::BuiltIn)))
{
ASSERT(ot.getArraySizes() == mt.getArraySizes());
if (ot.isArrayOfArrays())
{
original = &Flatten(symbolEnv, *original);
modified = &Flatten(symbolEnv, *modified);
}
const int volume = static_cast<int>(ot.getArraySizeProduct());
for (int i = 0; i < volume; ++i)
{
if (i != 0)
{
original = original->deepCopy();
modified = modified->deepCopy();
}
OriginalAccess &o = AccessIndex(*original, i);
OriginalAccess &m = AccessIndex(*modified, i);
convert(o, m);
}
}
else
{
convert(*original, *modified);
}
}
auto *funcProto = new TIntermFunctionPrototype(&func);
auto *funcDef = new TIntermFunctionDefinition(funcProto, &body);
ModifiedStructMachinery machinery;
machinery.modifiedStruct = &modifiedStruct;
machinery.getConverter(config.convertType) = funcDef;
outMachineries.insert(originalStruct, machinery);
}
void pushPath(PathItem const &item)
{
pathItems.push_back(item);
switch (item.type)
{
case PathItem::Type::Field:
pushNamePath(item.field->name());
break;
case PathItem::Type::Index:
pushNamePath(item.index);
break;
case PathItem::Type::FlattenArray:
namePathSizes.push_back(namePath.size());
break;
}
}
void popPath()
{
ASSERT(!namePath.empty());
ASSERT(!namePathSizes.empty());
namePath.resize(namePathSizes.back());
namePathSizes.pop_back();
ASSERT(!pathItems.empty());
pathItems.pop_back();
}
void finalize(const bool allowPadding)
{
ASSERT(!finalized);
finalized = true;
introducePacking();
ASSERT(metalLayoutTotal == Layout::Identity());
// Only pad substructs. We don't want to pad the structure that contains all the UBOs, only
// individual UBOs.
if (allowPadding)
introducePadding();
}
Name generateModifiedFieldName(const TField &field, const TType &newType)
{
SymbolType type = field.symbolType();
if (pathItems.size() > 1)
{
// This is an input field that generates multiple modified fields. We cannot generate a
// new field name into "user" namespace, as user could choose a clashing name. Trust
// that we generate new names only for 1:N expansions.
type = SymbolType::AngleInternal;
}
return Name(namePath, type);
}
void addModifiedField(const TField &field,
TType &newType,
TLayoutBlockStorage storage,
TLayoutMatrixPacking packing,
const AddressSpace *addressSpace)
{
TLayoutQualifier layoutQualifier = newType.getLayoutQualifier();
layoutQualifier.blockStorage = storage;
layoutQualifier.matrixPacking = packing;
newType.setLayoutQualifier(layoutQualifier);
Name newName = generateModifiedFieldName(field, newType);
TField *modifiedField =
new TField(&newType, newName.rawName(), field.line(), newName.symbolType());
if (addressSpace)
{
symbolEnv.markAsPointer(*modifiedField, *addressSpace);
}
if (symbolEnv.isUBO(field))
{
symbolEnv.markAsUBO(*modifiedField);
}
modifiedFields.push_back(modifiedField);
}
void addConversion(const ConversionFunc &func)
{
ASSERT(!modifiedFields.empty());
conversionInfos.push_back({func, nullptr, pathItems, Name(*modifiedFields.back())});
}
void addConversion(const TFunction &func)
{
ASSERT(!modifiedFields.empty());
conversionInfos.push_back({{}, &func, pathItems, Name(*modifiedFields.back())});
}
bool hasPacking() const { return containsPacked; }
bool hasPadding() const { return padFieldCount > 0; }
bool recurse(const TStructure &structure,
ModifiedStructMachinery &outMachinery,
const bool isUBORecurse)
{
const ModifiedStructMachinery *m = outMachineries.find(structure);
if (m == nullptr)
{
TranslatorMetalReflection *reflection = mtl::getTranslatorMetalReflection(&mCompiler);
reflection->addOriginalName(structure.uniqueId().get(), structure.name().data());
const Name name = idGen.createNewName(structure.name().data());
if (!TryCreateModifiedStruct(mCompiler, symbolEnv, idGen, config, structure, name,
outMachineries, isUBORecurse, config.allowPadding, false))
{
return false;
}
m = outMachineries.find(structure);
ASSERT(m);
}
outMachinery = *m;
return true;
}
bool getIsUBO() const { return isUBO; }
private:
void addPadding(size_t padAmount, bool updateLayout)
{
if (padAmount == 0)
{
return;
}
const size_t begin = modifiedFields.size();
// Iteratively adding in scalar or vector padding because some struct types will not
// allow matrix or array members.
while (padAmount > 0)
{
TType *padType;
if (padAmount >= 16)
{
padAmount -= 16;
padType = new TType(TBasicType::EbtFloat, 4);
}
else if (padAmount >= 8)
{
padAmount -= 8;
padType = new TType(TBasicType::EbtFloat, 2);
}
else if (padAmount >= 4)
{
padAmount -= 4;
padType = new TType(TBasicType::EbtFloat);
}
else if (padAmount >= 2)
{
padAmount -= 2;
padType = new TType(TBasicType::EbtBool, 2);
}
else
{
ASSERT(padAmount == 1);
padAmount -= 1;
padType = new TType(TBasicType::EbtBool);
}
if (padType->getBasicType() != EbtBool)
{
padType->setPrecision(EbpLow);
}
if (updateLayout)
{
metalLayoutTotal += MetalLayoutOf(*padType);
}
const Name name = idGen.createNewName("pad");
modifiedFields.push_back(
new TField(padType, name.rawName(), kNoSourceLoc, name.symbolType()));
++padFieldCount;
}
std::reverse(modifiedFields.begin() + begin, modifiedFields.end());
}
void introducePacking()
{
if (!config.allowPacking)
{
return;
}
auto setUnpackedStorage = [](TType &type) {
TLayoutBlockStorage storage = type.getLayoutQualifier().blockStorage;
switch (storage)
{
case TLayoutBlockStorage::EbsShared:
storage = TLayoutBlockStorage::EbsStd140;
break;
case TLayoutBlockStorage::EbsPacked:
storage = TLayoutBlockStorage::EbsStd430;
break;
case TLayoutBlockStorage::EbsStd140:
case TLayoutBlockStorage::EbsStd430:
case TLayoutBlockStorage::EbsUnspecified:
break;
}
SetBlockStorage(type, storage);
};
Layout glslLayoutTotal = Layout::Identity();
const size_t size = modifiedFields.size();
for (size_t i = 0; i < size; ++i)
{
TField &curr = *modifiedFields[i];
TType &currType = *curr.type();
const bool canBePacked = CanBePacked(currType);
auto dontPack = [&]() {
if (canBePacked)
{
setUnpackedStorage(currType);
}
glslLayoutTotal += GlslLayoutOf(currType);
};
if (!CanBePacked(currType))
{
dontPack();
continue;
}
const Layout packedGlslLayout = GlslLayoutOf(currType);
const TLayoutBlockStorage packedStorage = currType.getLayoutQualifier().blockStorage;
setUnpackedStorage(currType);
const Layout unpackedGlslLayout = GlslLayoutOf(currType);
SetBlockStorage(currType, packedStorage);
ASSERT(packedGlslLayout.sizeOf <= unpackedGlslLayout.sizeOf);
if (packedGlslLayout.sizeOf == unpackedGlslLayout.sizeOf)
{
dontPack();
continue;
}
const size_t j = i + 1;
if (j == size)
{
dontPack();
break;
}
const size_t pad = unpackedGlslLayout.sizeOf - packedGlslLayout.sizeOf;
const TField &next = *modifiedFields[j];
const Layout nextGlslLayout = GlslLayoutOf(*next.type());
if (pad < nextGlslLayout.sizeOf)
{
dontPack();
continue;
}
symbolEnv.markAsPacked(curr);
glslLayoutTotal += packedGlslLayout;
containsPacked = true;
}
}
void introducePadding()
{
if (!config.allowPadding)
{
return;
}
MetalLayoutOfConfig layoutConfig;
layoutConfig.disablePacking = !config.allowPacking;
layoutConfig.assumeStructsAreTailPadded = true;
TFieldList fields = std::move(modifiedFields);
ASSERT(!fields.empty()); // GLSL requires at least one member.
const TField *const first = fields.front();
for (TField *field : fields)
{
const TType &type = *field->type();
const Layout glslLayout = GlslLayoutOf(type);
const Layout metalLayout = MetalLayoutOf(type, layoutConfig);
size_t prePadAmount = 0;
if (glslLayout.alignOf > metalLayout.alignOf && field != first)
{
const size_t prePaddedSize = metalLayoutTotal.sizeOf;
metalLayoutTotal.requireAlignment(glslLayout.alignOf, true);
const size_t paddedSize = metalLayoutTotal.sizeOf;
prePadAmount = paddedSize - prePaddedSize;
metalLayoutTotal += metalLayout;
addPadding(prePadAmount, false); // Note: requireAlignment() already updated layout
}
else
{
metalLayoutTotal += metalLayout;
}
modifiedFields.push_back(field);
if (glslLayout.sizeOf > metalLayout.sizeOf && field != fields.back())
{
const bool updateLayout = true; // XXX: Correct?
const size_t padAmount = glslLayout.sizeOf - metalLayout.sizeOf;
addPadding(padAmount, updateLayout);
}
}
}
template <typename T>
void pushNamePath(T &&fieldName)
{
namePathSizes.push_back(namePath.size());
std::ostringstream str;
if (!namePath.empty())
{
str << std::move(namePath) << '_';
}
str << fieldName;
namePath = str.str();
}
public:
TCompiler &mCompiler;
const ModifyStructConfig &config;
SymbolEnv &symbolEnv;
private:
TFieldList &modifiedFields;
Layout metalLayoutTotal = Layout::Identity();
size_t padFieldCount = 0;
bool containsPacked = false;
bool finalized = false;
std::vector<PathItem> pathItems;
std::vector<size_t> namePathSizes;
std::string namePath;
std::vector<ConversionInfo> conversionInfos;
TSymbolTable &symbolTable;
IdGen &idGen;
ModifiedStructMachineries &outMachineries;
const bool isUBO;
const bool useAttributeAliasing;
};
////////////////////////////////////////////////////////////////////////////////
using ModifyFunc = bool (*)(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing);
bool ModifyRecursive(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing);
bool IdentityModify(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
const TType &type = *field.type();
state.addModifiedField(field, CloneType(type), storage, packing, nullptr);
state.addConversion([=](Access::Env &, OriginalAccess &o, ModifiedAccess &m) {
return Access{o, m};
});
return false;
}
bool InlineStruct(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
const TType &type = *field.type();
const TStructure *substructure = state.symbolEnv.remap(type.getStruct());
if (!substructure)
{
return false;
}
if (type.isArray())
{
return false;
}
if (!state.config.inlineStruct(field))
{
return false;
}
const TFieldList &subfields = substructure->fields();
for (const TField *subfield : subfields)
{
const TType &subtype = *subfield->type();
const TLayoutBlockStorage substorage = Overlay(storage, subtype);
const TLayoutMatrixPacking subpacking = Overlay(packing, subtype);
ModifyRecursive(state, *subfield, substorage, subpacking);
}
return true;
}
bool RecurseStruct(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
const TType &type = *field.type();
const TStructure *substructure = state.symbolEnv.remap(type.getStruct());
if (!substructure)
{
return false;
}
if (!state.config.recurseStruct(field))
{
return false;
}
ModifiedStructMachinery machinery;
if (!state.recurse(*substructure, machinery, state.getIsUBO()))
{
return false;
}
TType &newType = *new TType(machinery.modifiedStruct, false);
if (type.isArray())
{
newType.makeArrays(type.getArraySizes());
}
TIntermFunctionDefinition *converter = machinery.getConverter(state.config.convertType);
ASSERT(converter);
state.addModifiedField(field, newType, storage, packing, state.symbolEnv.isPointer(field));
if (state.symbolEnv.isPointer(field))
{
state.symbolEnv.removePointer(field);
}
state.addConversion(*converter->getFunction());
return true;
}
bool SplitMatrixColumns(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
const TType &type = *field.type();
if (!type.isMatrix())
{
return false;
}
if (!state.config.splitMatrixColumns(field))
{
return false;
}
const uint8_t cols = type.getCols();
TType &rowType = DropColumns(type);
for (uint8_t c = 0; c < cols; ++c)
{
state.pushPath(c);
state.addModifiedField(field, rowType, storage, packing, state.symbolEnv.isPointer(field));
if (state.symbolEnv.isPointer(field))
{
state.symbolEnv.removePointer(field);
}
state.addConversion([=](Access::Env &, OriginalAccess &o, ModifiedAccess &m) {
return Access{o, m};
});
state.popPath();
}
return true;
}
bool SaturateMatrixRows(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
const TType &type = *field.type();
if (!type.isMatrix())
{
return false;
}
const bool isRowMajor = type.getLayoutQualifier().matrixPacking == EmpRowMajor;
const uint8_t rows = type.getRows();
const uint8_t saturation = state.config.saturateMatrixRows(field);
if (saturation <= rows)
{
return false;
}
const uint8_t cols = type.getCols();
TType &satType = SetMatrixRowDim(type, saturation);
state.addModifiedField(field, satType, storage, packing, state.symbolEnv.isPointer(field));
if (state.symbolEnv.isPointer(field))
{
state.symbolEnv.removePointer(field);
}
for (uint8_t c = 0; c < cols; ++c)
{
for (uint8_t r = 0; r < rows; ++r)
{
state.addConversion([=](Access::Env &, OriginalAccess &o, ModifiedAccess &m) {
uint8_t firstModifiedIndex = isRowMajor ? r : c;
uint8_t secondModifiedIndex = isRowMajor ? c : r;
auto &o_ = AccessIndex(AccessIndex(o, c), r);
auto &m_ = AccessIndex(AccessIndex(m, firstModifiedIndex), secondModifiedIndex);
return Access{o_, m_};
});
}
}
return true;
}
bool TestBoolToUint(ConvertStructState &state, const TField &field)
{
if (field.type()->getBasicType() != TBasicType::EbtBool)
{
return false;
}
if (!state.config.promoteBoolToUint(field))
{
return false;
}
return true;
}
Access ConvertBoolToUint(ConvertType convertType, OriginalAccess &o, ModifiedAccess &m)
{
auto coerce = [](TIntermTyped &to, TIntermTyped &from) -> TIntermTyped & {
return *TIntermAggregate::CreateConstructor(to.getType(), new TIntermSequence{&from});
};
switch (convertType)
{
case ConvertType::OriginalToModified:
return Access{coerce(m, o), m};
case ConvertType::ModifiedToOriginal:
return Access{o, coerce(o, m)};
}
}
bool SaturateScalarOrVectorCommon(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing,
const bool array)
{
const TType &type = *field.type();
if (type.isArray() != array)
{
return false;
}
if (!((type.isRank0() && HasScalarBasicType(type)) || type.isVector()))
{
return false;
}
const auto saturator =
array ? state.config.saturateScalarOrVectorArrays : state.config.saturateScalarOrVector;
const uint8_t dim = type.getNominalSize();
const uint8_t saturation = saturator(field);
if (saturation <= dim)
{
return false;
}
TType &satType = SetVectorDim(type, saturation);
const bool boolToUint = TestBoolToUint(state, field);
if (boolToUint)
{
satType.setBasicType(TBasicType::EbtUInt);
}
state.addModifiedField(field, satType, storage, packing, state.symbolEnv.isPointer(field));
if (state.symbolEnv.isPointer(field))
{
state.symbolEnv.removePointer(field);
}
for (uint8_t d = 0; d < dim; ++d)
{
state.addConversion([=](Access::Env &env, OriginalAccess &o, ModifiedAccess &m) {
auto &o_ = dim > 1 ? AccessIndex(o, d) : o;
auto &m_ = AccessIndex(m, d);
if (boolToUint)
{
return ConvertBoolToUint(env.type, o_, m_);
}
else
{
return Access{o_, m_};
}
});
}
return true;
}
bool SaturateScalarOrVectorArrays(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
return SaturateScalarOrVectorCommon(state, field, storage, packing, true);
}
bool SaturateScalarOrVector(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
return SaturateScalarOrVectorCommon(state, field, storage, packing, false);
}
bool PromoteBoolToUint(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
if (!TestBoolToUint(state, field))
{
return false;
}
auto &promotedType = CloneType(*field.type());
promotedType.setBasicType(TBasicType::EbtUInt);
state.addModifiedField(field, promotedType, storage, packing, state.symbolEnv.isPointer(field));
if (state.symbolEnv.isPointer(field))
{
state.symbolEnv.removePointer(field);
}
state.addConversion([=](Access::Env &env, OriginalAccess &o, ModifiedAccess &m) {
return ConvertBoolToUint(env.type, o, m);
});
return true;
}
bool ModifyCommon(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
ModifyFunc funcs[] = {
InlineStruct, //
RecurseStruct, //
SplitMatrixColumns, //
SaturateMatrixRows, //
SaturateScalarOrVectorArrays, //
SaturateScalarOrVector, //
PromoteBoolToUint, //
};
for (ModifyFunc func : funcs)
{
if (func(state, field, storage, packing))
{
return true;
}
}
return IdentityModify(state, field, storage, packing);
}
bool InlineArray(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
const TType &type = *field.type();
if (!type.isArray())
{
return false;
}
if (!state.config.inlineArray(field))
{
return false;
}
const unsigned volume = type.getArraySizeProduct();
const bool isMultiDim = type.isArrayOfArrays();
auto &innermostType = InnermostType(type);
if (isMultiDim)
{
state.pushPath(FlattenArray());
}
for (unsigned i = 0; i < volume; ++i)
{
state.pushPath(i);
TType setType(innermostType);
if (setType.getLayoutQualifier().locationsSpecified)
{
TLayoutQualifier qualifier(innermostType.getLayoutQualifier());
qualifier.location = innermostType.getLayoutQualifier().location + i;
qualifier.locationsSpecified = 1;
setType.setLayoutQualifier(qualifier);
}
const TField innermostField(&setType, field.name(), field.line(), field.symbolType());
ModifyCommon(state, innermostField, storage, packing);
state.popPath();
}
if (isMultiDim)
{
state.popPath();
}
return true;
}
bool ModifyRecursive(ConvertStructState &state,
const TField &field,
const TLayoutBlockStorage storage,
const TLayoutMatrixPacking packing)
{
state.pushPath(field);
bool modified;
if (InlineArray(state, field, storage, packing))
{
modified = true;
}
else
{
modified = ModifyCommon(state, field, storage, packing);
}
state.popPath();
return modified;
}
} // anonymous namespace
////////////////////////////////////////////////////////////////////////////////
bool sh::TryCreateModifiedStruct(TCompiler &compiler,
SymbolEnv &symbolEnv,
IdGen &idGen,
const ModifyStructConfig &config,
const TStructure &originalStruct,
const Name &modifiedStructName,
ModifiedStructMachineries &outMachineries,
const bool isUBO,
const bool allowPadding,
const bool useAttributeAliasing)
{
ConvertStructState state(compiler, symbolEnv, idGen, config, outMachineries, isUBO,
useAttributeAliasing);
size_t identicalFieldCount = 0;
const TFieldList &originalFields = originalStruct.fields();
for (TField *originalField : originalFields)
{
const TType &originalType = *originalField->type();
const TLayoutBlockStorage storage = Overlay(config.initialBlockStorage, originalType);
const TLayoutMatrixPacking packing = Overlay(config.initialMatrixPacking, originalType);
if (!ModifyRecursive(state, *originalField, storage, packing))
{
++identicalFieldCount;
}
}
state.finalize(allowPadding);
if (identicalFieldCount == originalFields.size() && !state.hasPacking() &&
!state.hasPadding() && !useAttributeAliasing)
{
return false;
}
state.publish(originalStruct, modifiedStructName);
return true;
}