Hash :
b23bf47c
Author :
Date :
2023-01-25T18:50:40
Reland "Metal: rewrite default uniforms and uniform blocks" Instead of rewriting uniforms in shaders to match std140 layout, re-pack incoming uniform blocks' std140 packed variables to match Metal's layout. This change intorduces a new BlockLayoutEncoder for Metal types The block encoder handles packing typically larger GL types (bools) into smaller types, and adding support for more compressed matrix types. Since we no longer need to do shader-time packing and unpacking of data from std140 padded structs, complicated shader transformations have been removed. This patch greatly reduces register pressure, especially when working with shaders with arrays of previously expanded types. (Vec3's) Reland: Fix an issue where the default uniform block's final size was not aligned to the default uniform block's alignment requirements, causing crashes with the debug layer enabled. Bug: angleproject:7137 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3733524 Commit-Queue: Kyle Piddington <kpiddington@apple.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Kenneth Russell <kbr@chromium.org> Change-Id: I89d3b817675486fde73b91b0be0f4c25986d4ba5 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4209867
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
//
// 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 <cstring>
#include <unordered_map>
#include <unordered_set>
#include "compiler/translator/TranslatorMetalDirect.h"
#include "compiler/translator/TranslatorMetalDirect/AstHelpers.h"
#include "compiler/translator/TranslatorMetalDirect/DiscoverDependentFunctions.h"
#include "compiler/translator/TranslatorMetalDirect/IdGen.h"
#include "compiler/translator/TranslatorMetalDirect/MapSymbols.h"
#include "compiler/translator/TranslatorMetalDirect/Pipeline.h"
#include "compiler/translator/TranslatorMetalDirect/RewritePipelines.h"
#include "compiler/translator/TranslatorMetalDirect/SymbolEnv.h"
#include "compiler/translator/tree_ops/PruneNoOps.h"
#include "compiler/translator/tree_util/DriverUniform.h"
#include "compiler/translator/tree_util/FindMain.h"
#include "compiler/translator/tree_util/IntermRebuild.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
using namespace sh;
////////////////////////////////////////////////////////////////////////////////
namespace
{
bool IsVariableInvariant(const std::vector<sh::ShaderVariable> &mVars, const ImmutableString &name)
{
for (const auto &var : mVars)
{
if (name == var.name)
{
return var.isInvariant;
}
}
// TODO(kpidington): this should be UNREACHABLE() but isn't because the translator generates
// declarations to unused built-in variables.
return false;
}
using VariableSet = std::unordered_set<const TVariable *>;
using VariableList = std::vector<const TVariable *>;
////////////////////////////////////////////////////////////////////////////////
struct PipelineStructInfo
{
VariableSet pipelineVariables;
PipelineScoped<TStructure> pipelineStruct;
const TFunction *funcOriginalToModified = nullptr;
const TFunction *funcModifiedToOriginal = nullptr;
bool isEmpty() const
{
if (pipelineStruct.isTotallyEmpty())
{
ASSERT(pipelineVariables.empty());
return true;
}
else
{
ASSERT(pipelineStruct.isTotallyFull());
ASSERT(!pipelineVariables.empty());
return false;
}
}
};
class GeneratePipelineStruct : private TIntermRebuild
{
private:
const Pipeline &mPipeline;
SymbolEnv &mSymbolEnv;
const std::vector<sh::ShaderVariable> *mVariableInfos;
VariableList mPipelineVariableList;
IdGen &mIdGen;
PipelineStructInfo mInfo;
public:
static bool Exec(PipelineStructInfo &out,
TCompiler &compiler,
TIntermBlock &root,
IdGen &idGen,
const Pipeline &pipeline,
SymbolEnv &symbolEnv,
const std::vector<sh::ShaderVariable> *variableInfos)
{
GeneratePipelineStruct self(compiler, idGen, pipeline, symbolEnv, variableInfos);
if (!self.exec(root))
{
return false;
}
out = self.mInfo;
return true;
}
private:
GeneratePipelineStruct(TCompiler &compiler,
IdGen &idGen,
const Pipeline &pipeline,
SymbolEnv &symbolEnv,
const std::vector<sh::ShaderVariable> *variableInfos)
: TIntermRebuild(compiler, true, true),
mPipeline(pipeline),
mSymbolEnv(symbolEnv),
mVariableInfos(variableInfos),
mIdGen(idGen)
{}
bool exec(TIntermBlock &root)
{
if (!rebuildRoot(root))
{
return false;
}
if (mInfo.pipelineVariables.empty())
{
return true;
}
TIntermSequence seq;
const TStructure &pipelineStruct = [&]() -> const TStructure & {
if (mPipeline.globalInstanceVar)
{
return *mPipeline.globalInstanceVar->getType().getStruct();
}
else
{
return createInternalPipelineStruct(root, seq);
}
}();
ModifiedStructMachineries modifiedMachineries;
const bool isUBO = mPipeline.type == Pipeline::Type::UniformBuffer;
const bool isUniform = mPipeline.type == Pipeline::Type::UniformBuffer ||
mPipeline.type == Pipeline::Type::UserUniforms;
const bool modified = TryCreateModifiedStruct(
mCompiler, mSymbolEnv, mIdGen, mPipeline.externalStructModifyConfig(), pipelineStruct,
mPipeline.getStructTypeName(Pipeline::Variant::Modified), modifiedMachineries, isUBO,
!isUniform);
if (modified)
{
ASSERT(mPipeline.type != Pipeline::Type::Texture);
ASSERT(mPipeline.type == Pipeline::Type::AngleUniforms ||
!mPipeline.globalInstanceVar); // This shouldn't happen by construction.
auto getFunction = [](sh::TIntermFunctionDefinition *funcDecl) {
return funcDecl ? funcDecl->getFunction() : nullptr;
};
const size_t size = modifiedMachineries.size();
ASSERT(size > 0);
for (size_t i = 0; i < size; ++i)
{
const ModifiedStructMachinery &machinery = modifiedMachineries.at(i);
ASSERT(machinery.modifiedStruct);
seq.push_back(new TIntermDeclaration{
&CreateStructTypeVariable(mSymbolTable, *machinery.modifiedStruct)});
if (mPipeline.isPipelineOut())
{
ASSERT(machinery.funcOriginalToModified);
ASSERT(!machinery.funcModifiedToOriginal);
seq.push_back(machinery.funcOriginalToModified);
}
else
{
ASSERT(machinery.funcModifiedToOriginal);
ASSERT(!machinery.funcOriginalToModified);
seq.push_back(machinery.funcModifiedToOriginal);
}
if (i == size - 1)
{
mInfo.funcOriginalToModified = getFunction(machinery.funcOriginalToModified);
mInfo.funcModifiedToOriginal = getFunction(machinery.funcModifiedToOriginal);
mInfo.pipelineStruct.internal = &pipelineStruct;
mInfo.pipelineStruct.external =
modified ? machinery.modifiedStruct : &pipelineStruct;
}
}
}
else
{
mInfo.pipelineStruct.internal = &pipelineStruct;
mInfo.pipelineStruct.external = &pipelineStruct;
}
root.insertChildNodes(FindMainIndex(&root), seq);
return true;
}
private:
PreResult visitFunctionDefinitionPre(TIntermFunctionDefinition &node) override
{
return {node, VisitBits::Neither};
}
PostResult visitDeclarationPost(TIntermDeclaration &declNode) override
{
Declaration decl = ViewDeclaration(declNode);
const TVariable &var = decl.symbol.variable();
if (mPipeline.uses(var))
{
ASSERT(mInfo.pipelineVariables.find(&var) == mInfo.pipelineVariables.end());
mInfo.pipelineVariables.insert(&var);
mPipelineVariableList.push_back(&var);
return nullptr;
}
return declNode;
}
const TStructure &createInternalPipelineStruct(TIntermBlock &root, TIntermSequence &outDeclSeq)
{
auto &fields = *new TFieldList();
switch (mPipeline.type)
{
case Pipeline::Type::Texture:
{
for (const TVariable *var : mPipelineVariableList)
{
const TType &varType = var->getType();
const TBasicType samplerType = varType.getBasicType();
const TStructure &textureEnv = mSymbolEnv.getTextureEnv(samplerType);
auto *textureEnvType = new TType(&textureEnv, false);
if (varType.isArray())
{
textureEnvType->makeArrays(varType.getArraySizes());
}
fields.push_back(
new TField(textureEnvType, var->name(), kNoSourceLoc, var->symbolType()));
}
}
break;
case Pipeline::Type::Image:
{
for (const TVariable *var : mPipelineVariableList)
{
auto &type = CloneType(var->getType());
auto *field = new TField(&type, var->name(), kNoSourceLoc, var->symbolType());
fields.push_back(field);
}
}
break;
case Pipeline::Type::UniformBuffer:
{
for (const TVariable *var : mPipelineVariableList)
{
auto &type = CloneType(var->getType());
auto *field = new TField(&type, var->name(), kNoSourceLoc, var->symbolType());
mSymbolEnv.markAsPointer(*field, AddressSpace::Constant);
mSymbolEnv.markAsUBO(*field);
mSymbolEnv.markAsPointer(*var, AddressSpace::Constant);
fields.push_back(field);
}
}
break;
default:
{
for (const TVariable *var : mPipelineVariableList)
{
auto &type = CloneType(var->getType());
if (mVariableInfos && IsVariableInvariant(*mVariableInfos, var->name()))
{
type.setInvariant(true);
}
auto *field = new TField(&type, var->name(), kNoSourceLoc, var->symbolType());
fields.push_back(field);
}
}
break;
}
Name pipelineStructName = mPipeline.getStructTypeName(Pipeline::Variant::Original);
auto &s = *new TStructure(&mSymbolTable, pipelineStructName.rawName(), &fields,
pipelineStructName.symbolType());
outDeclSeq.push_back(new TIntermDeclaration{&CreateStructTypeVariable(mSymbolTable, s)});
return s;
}
};
////////////////////////////////////////////////////////////////////////////////
PipelineScoped<TVariable> CreatePipelineMainLocalVar(TSymbolTable &symbolTable,
const Pipeline &pipeline,
PipelineScoped<TStructure> pipelineStruct)
{
ASSERT(pipelineStruct.isTotallyFull());
PipelineScoped<TVariable> pipelineMainLocalVar;
auto populateExternalMainLocalVar = [&]() {
ASSERT(!pipelineMainLocalVar.external);
pipelineMainLocalVar.external = &CreateInstanceVariable(
symbolTable, *pipelineStruct.external,
pipeline.getStructInstanceName(pipelineStruct.isUniform()
? Pipeline::Variant::Original
: Pipeline::Variant::Modified));
};
auto populateDistinctInternalMainLocalVar = [&]() {
ASSERT(!pipelineMainLocalVar.internal);
pipelineMainLocalVar.internal =
&CreateInstanceVariable(symbolTable, *pipelineStruct.internal,
pipeline.getStructInstanceName(Pipeline::Variant::Original));
};
if (pipeline.type == Pipeline::Type::InstanceId)
{
populateDistinctInternalMainLocalVar();
}
else if (pipeline.alwaysRequiresLocalVariableDeclarationInMain())
{
populateExternalMainLocalVar();
if (pipelineStruct.isUniform())
{
pipelineMainLocalVar.internal = pipelineMainLocalVar.external;
}
else
{
populateDistinctInternalMainLocalVar();
}
}
else if (!pipelineStruct.isUniform())
{
populateDistinctInternalMainLocalVar();
}
return pipelineMainLocalVar;
}
class PipelineFunctionEnv
{
private:
TCompiler &mCompiler;
SymbolEnv &mSymbolEnv;
TSymbolTable &mSymbolTable;
IdGen &mIdGen;
const Pipeline &mPipeline;
const std::unordered_set<const TFunction *> &mPipelineFunctions;
const PipelineScoped<TStructure> mPipelineStruct;
PipelineScoped<TVariable> &mPipelineMainLocalVar;
size_t mFirstParamIdxInMainFn = 0;
std::unordered_map<const TFunction *, const TFunction *> mFuncMap;
// Optional expression with which to initialize mPipelineMainLocalVar.
TIntermTyped *mPipelineInitExpr = nullptr;
public:
PipelineFunctionEnv(TCompiler &compiler,
SymbolEnv &symbolEnv,
IdGen &idGen,
const Pipeline &pipeline,
const std::unordered_set<const TFunction *> &pipelineFunctions,
PipelineScoped<TStructure> pipelineStruct,
PipelineScoped<TVariable> &pipelineMainLocalVar)
: mCompiler(compiler),
mSymbolEnv(symbolEnv),
mSymbolTable(symbolEnv.symbolTable()),
mIdGen(idGen),
mPipeline(pipeline),
mPipelineFunctions(pipelineFunctions),
mPipelineStruct(pipelineStruct),
mPipelineMainLocalVar(pipelineMainLocalVar)
{}
bool isOriginalPipelineFunction(const TFunction &func) const
{
return mPipelineFunctions.find(&func) != mPipelineFunctions.end();
}
bool isUpdatedPipelineFunction(const TFunction &func) const
{
auto it = mFuncMap.find(&func);
if (it == mFuncMap.end())
{
return false;
}
return &func == it->second;
}
const TFunction &getUpdatedFunction(const TFunction &func)
{
ASSERT(isOriginalPipelineFunction(func) || isUpdatedPipelineFunction(func));
const TFunction *newFunc;
auto it = mFuncMap.find(&func);
if (it == mFuncMap.end())
{
const bool isMain = func.isMain();
if (isMain)
{
mFirstParamIdxInMainFn = func.getParamCount();
}
if (isMain && mPipeline.isPipelineOut())
{
ASSERT(func.getReturnType().getBasicType() == TBasicType::EbtVoid);
newFunc = &CloneFunctionAndChangeReturnType(mSymbolTable, nullptr, func,
*mPipelineStruct.external);
if (mPipeline.type == Pipeline::Type::FragmentOut &&
mCompiler.hasPixelLocalStorageUniforms())
{
// Add an input argument to main() that contains the current framebuffer
// attachment values, for loading pixel local storage.
TType *type = new TType(mPipelineStruct.external, true);
TVariable *lastFragmentOut =
new TVariable(&mSymbolTable, ImmutableString("lastFragmentOut"), type,
SymbolType::AngleInternal);
newFunc = &CloneFunctionAndPrependParam(mSymbolTable, nullptr, *newFunc,
*lastFragmentOut);
// Initialize the main local variable with the current framebuffer contents.
mPipelineInitExpr = new TIntermSymbol(lastFragmentOut);
}
}
else if (isMain && (mPipeline.type == Pipeline::Type::InvocationVertexGlobals ||
mPipeline.type == Pipeline::Type::InvocationFragmentGlobals))
{
std::vector<const TVariable *> variables;
for (const TField *field : mPipelineStruct.external->fields())
{
variables.push_back(new TVariable(&mSymbolTable, field->name(), field->type(),
field->symbolType()));
}
newFunc = &CloneFunctionAndAppendParams(mSymbolTable, nullptr, func, variables);
}
else if (isMain && mPipeline.type == Pipeline::Type::Texture)
{
std::vector<const TVariable *> variables;
TranslatorMetalReflection *reflection =
mtl::getTranslatorMetalReflection(&mCompiler);
for (const TField *field : mPipelineStruct.external->fields())
{
const TStructure *textureEnv = field->type()->getStruct();
ASSERT(textureEnv && textureEnv->fields().size() == 2);
for (const TField *subfield : textureEnv->fields())
{
const Name name = mIdGen.createNewName({field->name(), subfield->name()});
TType &type = *new TType(*subfield->type());
ASSERT(!type.isArray());
type.makeArrays(field->type()->getArraySizes());
auto *var =
new TVariable(&mSymbolTable, name.rawName(), &type, name.symbolType());
variables.push_back(var);
reflection->addOriginalName(var->uniqueId().get(), field->name().data());
}
}
newFunc = &CloneFunctionAndAppendParams(mSymbolTable, nullptr, func, variables);
}
else if (isMain && mPipeline.type == Pipeline::Type::InstanceId)
{
Name instanceIdName = mPipeline.getStructInstanceName(Pipeline::Variant::Modified);
auto *instanceIdVar =
new TVariable(&mSymbolTable, instanceIdName.rawName(),
new TType(TBasicType::EbtUInt), instanceIdName.symbolType());
auto *baseInstanceVar =
new TVariable(&mSymbolTable, kBaseInstanceName.rawName(),
new TType(TBasicType::EbtUInt), kBaseInstanceName.symbolType());
newFunc = &CloneFunctionAndPrependTwoParams(mSymbolTable, nullptr, func,
*instanceIdVar, *baseInstanceVar);
mPipelineMainLocalVar.external = instanceIdVar;
mPipelineMainLocalVar.externalExtra = baseInstanceVar;
}
else if (isMain && mPipeline.alwaysRequiresLocalVariableDeclarationInMain())
{
ASSERT(mPipelineMainLocalVar.isTotallyFull());
newFunc = &func;
}
else
{
const TVariable *var;
AddressSpace addressSpace;
if (isMain && !mPipelineMainLocalVar.isUniform())
{
var = &CreateInstanceVariable(
mSymbolTable, *mPipelineStruct.external,
mPipeline.getStructInstanceName(Pipeline::Variant::Modified));
addressSpace = mPipeline.externalAddressSpace();
}
else
{
var = &CreateInstanceVariable(
mSymbolTable, *mPipelineStruct.internal,
mPipeline.getStructInstanceName(Pipeline::Variant::Original));
addressSpace = mPipelineMainLocalVar.isUniform()
? mPipeline.externalAddressSpace()
: AddressSpace::Thread;
}
bool markAsReference = true;
if (isMain)
{
switch (mPipeline.type)
{
case Pipeline::Type::VertexIn:
case Pipeline::Type::FragmentIn:
case Pipeline::Type::Image:
markAsReference = false;
break;
default:
break;
}
}
if (markAsReference)
{
mSymbolEnv.markAsReference(*var, addressSpace);
}
newFunc = &CloneFunctionAndPrependParam(mSymbolTable, nullptr, func, *var);
}
mFuncMap[&func] = newFunc;
mFuncMap[newFunc] = newFunc;
}
else
{
newFunc = it->second;
}
return *newFunc;
}
TIntermFunctionPrototype *createUpdatedFunctionPrototype(
TIntermFunctionPrototype &funcProtoNode)
{
const TFunction &func = *funcProtoNode.getFunction();
if (!isOriginalPipelineFunction(func) && !isUpdatedPipelineFunction(func))
{
return nullptr;
}
const TFunction &newFunc = getUpdatedFunction(func);
return new TIntermFunctionPrototype(&newFunc);
}
// If not null, this is the value we need to initialize the pipeline main local variable with.
TIntermTyped *getOptionalPipelineInitExpr() { return mPipelineInitExpr; }
size_t getFirstParamIdxInMainFn() const { return mFirstParamIdxInMainFn; }
};
class UpdatePipelineFunctions : private TIntermRebuild
{
private:
const Pipeline &mPipeline;
const PipelineScoped<TStructure> mPipelineStruct;
PipelineScoped<TVariable> &mPipelineMainLocalVar;
SymbolEnv &mSymbolEnv;
PipelineFunctionEnv mEnv;
const TFunction *mFuncOriginalToModified;
const TFunction *mFuncModifiedToOriginal;
public:
static bool ThreadPipeline(TCompiler &compiler,
TIntermBlock &root,
const Pipeline &pipeline,
const std::unordered_set<const TFunction *> &pipelineFunctions,
PipelineScoped<TStructure> pipelineStruct,
PipelineScoped<TVariable> &pipelineMainLocalVar,
IdGen &idGen,
SymbolEnv &symbolEnv,
const TFunction *funcOriginalToModified,
const TFunction *funcModifiedToOriginal)
{
UpdatePipelineFunctions self(compiler, pipeline, pipelineFunctions, pipelineStruct,
pipelineMainLocalVar, idGen, symbolEnv, funcOriginalToModified,
funcModifiedToOriginal);
if (!self.rebuildRoot(root))
{
return false;
}
return true;
}
private:
UpdatePipelineFunctions(TCompiler &compiler,
const Pipeline &pipeline,
const std::unordered_set<const TFunction *> &pipelineFunctions,
PipelineScoped<TStructure> pipelineStruct,
PipelineScoped<TVariable> &pipelineMainLocalVar,
IdGen &idGen,
SymbolEnv &symbolEnv,
const TFunction *funcOriginalToModified,
const TFunction *funcModifiedToOriginal)
: TIntermRebuild(compiler, false, true),
mPipeline(pipeline),
mPipelineStruct(pipelineStruct),
mPipelineMainLocalVar(pipelineMainLocalVar),
mSymbolEnv(symbolEnv),
mEnv(compiler,
symbolEnv,
idGen,
pipeline,
pipelineFunctions,
pipelineStruct,
mPipelineMainLocalVar),
mFuncOriginalToModified(funcOriginalToModified),
mFuncModifiedToOriginal(funcModifiedToOriginal)
{
ASSERT(mPipelineStruct.isTotallyFull());
}
const TVariable &getInternalPipelineVariable(const TFunction &pipelineFunc)
{
if (pipelineFunc.isMain() && (mPipeline.alwaysRequiresLocalVariableDeclarationInMain() ||
!mPipelineMainLocalVar.isUniform()))
{
ASSERT(mPipelineMainLocalVar.internal);
return *mPipelineMainLocalVar.internal;
}
else
{
ASSERT(pipelineFunc.getParamCount() > 0);
return *pipelineFunc.getParam(0);
}
}
const TVariable &getExternalPipelineVariable(const TFunction &mainFunc)
{
ASSERT(mainFunc.isMain());
if (mPipelineMainLocalVar.external)
{
return *mPipelineMainLocalVar.external;
}
else
{
ASSERT(mainFunc.getParamCount() > 0);
return *mainFunc.getParam(0);
}
}
const TVariable &getExternalExtraPipelineVariable(const TFunction &mainFunc)
{
ASSERT(mainFunc.isMain());
if (mPipelineMainLocalVar.externalExtra)
{
return *mPipelineMainLocalVar.externalExtra;
}
else
{
ASSERT(mainFunc.getParamCount() > 1);
return *mainFunc.getParam(1);
}
}
PostResult visitAggregatePost(TIntermAggregate &callNode) override
{
if (callNode.isConstructor())
{
return callNode;
}
else
{
const TFunction &oldCalledFunc = *callNode.getFunction();
if (!mEnv.isOriginalPipelineFunction(oldCalledFunc))
{
return callNode;
}
const TFunction &newCalledFunc = mEnv.getUpdatedFunction(oldCalledFunc);
const TFunction *oldOwnerFunc = getParentFunction();
ASSERT(oldOwnerFunc);
const TFunction &newOwnerFunc = mEnv.getUpdatedFunction(*oldOwnerFunc);
return *TIntermAggregate::CreateFunctionCall(
newCalledFunc, &CloneSequenceAndPrepend(
*callNode.getSequence(),
*new TIntermSymbol(&getInternalPipelineVariable(newOwnerFunc))));
}
}
PostResult visitFunctionPrototypePost(TIntermFunctionPrototype &funcProtoNode) override
{
TIntermFunctionPrototype *newFuncProtoNode =
mEnv.createUpdatedFunctionPrototype(funcProtoNode);
if (newFuncProtoNode == nullptr)
{
return funcProtoNode;
}
return *newFuncProtoNode;
}
PostResult visitFunctionDefinitionPost(TIntermFunctionDefinition &funcDefNode) override
{
if (funcDefNode.getFunction()->isMain())
{
return visitMain(funcDefNode);
}
else
{
return visitNonMain(funcDefNode);
}
}
TIntermNode &visitNonMain(TIntermFunctionDefinition &funcDefNode)
{
TIntermFunctionPrototype &funcProtoNode = *funcDefNode.getFunctionPrototype();
ASSERT(!funcProtoNode.getFunction()->isMain());
TIntermFunctionPrototype *newFuncProtoNode =
mEnv.createUpdatedFunctionPrototype(funcProtoNode);
if (newFuncProtoNode == nullptr)
{
return funcDefNode;
}
const TFunction &func = *newFuncProtoNode->getFunction();
ASSERT(!func.isMain());
TIntermBlock *body = funcDefNode.getBody();
return *new TIntermFunctionDefinition(newFuncProtoNode, body);
}
TIntermNode &visitMain(TIntermFunctionDefinition &funcDefNode)
{
TIntermFunctionPrototype &funcProtoNode = *funcDefNode.getFunctionPrototype();
ASSERT(funcProtoNode.getFunction()->isMain());
TIntermFunctionPrototype *newFuncProtoNode =
mEnv.createUpdatedFunctionPrototype(funcProtoNode);
if (newFuncProtoNode == nullptr)
{
return funcDefNode;
}
const TFunction &func = *newFuncProtoNode->getFunction();
ASSERT(func.isMain());
auto callModifiedToOriginal = [&](TIntermBlock &body) {
ASSERT(mPipelineMainLocalVar.internal);
if (!mPipeline.isPipelineOut())
{
ASSERT(mFuncModifiedToOriginal);
auto *m = new TIntermSymbol(&getExternalPipelineVariable(func));
auto *o = new TIntermSymbol(mPipelineMainLocalVar.internal);
body.appendStatement(TIntermAggregate::CreateFunctionCall(
*mFuncModifiedToOriginal, new TIntermSequence{m, o}));
}
};
auto callOriginalToModified = [&](TIntermBlock &body) {
ASSERT(mPipelineMainLocalVar.internal);
if (mPipeline.isPipelineOut())
{
ASSERT(mFuncOriginalToModified);
auto *o = new TIntermSymbol(mPipelineMainLocalVar.internal);
auto *m = new TIntermSymbol(&getExternalPipelineVariable(func));
body.appendStatement(TIntermAggregate::CreateFunctionCall(
*mFuncOriginalToModified, new TIntermSequence{o, m}));
}
};
TIntermBlock *body = funcDefNode.getBody();
if (mPipeline.alwaysRequiresLocalVariableDeclarationInMain())
{
ASSERT(mPipelineMainLocalVar.isTotallyFull());
auto *newBody = new TIntermBlock();
newBody->appendStatement(new TIntermDeclaration(mPipelineMainLocalVar.internal,
mEnv.getOptionalPipelineInitExpr()));
if (mPipeline.type == Pipeline::Type::InvocationVertexGlobals ||
mPipeline.type == Pipeline::Type::InvocationFragmentGlobals)
{
// Populate struct instance with references to global pipeline variables.
for (const TField *field : mPipelineStruct.external->fields())
{
auto *var = new TVariable(&mSymbolTable, field->name(), field->type(),
field->symbolType());
auto *symbol = new TIntermSymbol(var);
auto &accessNode = AccessField(*mPipelineMainLocalVar.internal, var->name());
auto *assignNode = new TIntermBinary(TOperator::EOpAssign, &accessNode, symbol);
newBody->appendStatement(assignNode);
}
}
else if (mPipeline.type == Pipeline::Type::Texture)
{
const TFieldList &fields = mPipelineStruct.external->fields();
ASSERT(func.getParamCount() >= mEnv.getFirstParamIdxInMainFn() + 2 * fields.size());
size_t paramIndex = mEnv.getFirstParamIdxInMainFn();
for (const TField *field : fields)
{
const TVariable &textureParam = *func.getParam(paramIndex++);
const TVariable &samplerParam = *func.getParam(paramIndex++);
auto go = [&](TIntermTyped &env, const int *index) {
TIntermTyped &textureField = AccessField(
AccessIndex(*env.deepCopy(), index), ImmutableString("texture"));
TIntermTyped &samplerField = AccessField(
AccessIndex(*env.deepCopy(), index), ImmutableString("sampler"));
auto mkAssign = [&](TIntermTyped &field, const TVariable ¶m) {
return new TIntermBinary(TOperator::EOpAssign, &field,
&mSymbolEnv.callFunctionOverload(
Name("addressof"), field.getType(),
*new TIntermSequence{&AccessIndex(
*new TIntermSymbol(¶m), index)}));
};
newBody->appendStatement(mkAssign(textureField, textureParam));
newBody->appendStatement(mkAssign(samplerField, samplerParam));
};
TIntermTyped &env = AccessField(*mPipelineMainLocalVar.internal, field->name());
const TType &envType = env.getType();
if (envType.isArray())
{
ASSERT(!envType.isArrayOfArrays());
const auto n = static_cast<int>(envType.getArraySizeProduct());
for (int i = 0; i < n; ++i)
{
go(env, &i);
}
}
else
{
go(env, nullptr);
}
}
}
else if (mPipeline.type == Pipeline::Type::InstanceId)
{
auto varInstanceId = new TIntermSymbol(&getExternalPipelineVariable(func));
auto varBaseInstance = new TIntermSymbol(&getExternalExtraPipelineVariable(func));
newBody->appendStatement(new TIntermBinary(
TOperator::EOpAssign,
&AccessFieldByIndex(*new TIntermSymbol(&getInternalPipelineVariable(func)), 0),
&AsType(
mSymbolEnv, *new TType(TBasicType::EbtInt),
*new TIntermBinary(TOperator::EOpSub, varInstanceId, varBaseInstance))));
}
else if (!mPipelineMainLocalVar.isUniform())
{
newBody->appendStatement(new TIntermDeclaration{mPipelineMainLocalVar.external});
callModifiedToOriginal(*newBody);
}
newBody->appendStatement(body);
if (!mPipelineMainLocalVar.isUniform())
{
callOriginalToModified(*newBody);
}
if (mPipeline.isPipelineOut())
{
newBody->appendStatement(new TIntermBranch(
TOperator::EOpReturn, new TIntermSymbol(mPipelineMainLocalVar.external)));
}
body = newBody;
}
else if (!mPipelineMainLocalVar.isUniform())
{
ASSERT(!mPipelineMainLocalVar.external);
ASSERT(mPipelineMainLocalVar.internal);
auto *newBody = new TIntermBlock();
newBody->appendStatement(new TIntermDeclaration{mPipelineMainLocalVar.internal});
callModifiedToOriginal(*newBody);
newBody->appendStatement(body);
callOriginalToModified(*newBody);
body = newBody;
}
return *new TIntermFunctionDefinition(newFuncProtoNode, body);
}
};
////////////////////////////////////////////////////////////////////////////////
bool UpdatePipelineSymbols(Pipeline::Type pipelineType,
TCompiler &compiler,
TIntermBlock &root,
SymbolEnv &symbolEnv,
const VariableSet &pipelineVariables,
PipelineScoped<TVariable> pipelineMainLocalVar)
{
auto map = [&](const TFunction *owner, TIntermSymbol &symbol) -> TIntermNode & {
if (!owner)
return symbol;
const TVariable &var = symbol.variable();
if (pipelineVariables.find(&var) == pipelineVariables.end())
{
return symbol;
}
const TVariable *structInstanceVar;
if (owner->isMain() && pipelineType != Pipeline::Type::FragmentIn)
{
ASSERT(pipelineMainLocalVar.internal);
structInstanceVar = pipelineMainLocalVar.internal;
}
else
{
ASSERT(owner->getParamCount() > 0);
structInstanceVar = owner->getParam(0);
}
ASSERT(structInstanceVar);
return AccessField(*structInstanceVar, var.name());
};
return MapSymbols(compiler, root, map);
}
////////////////////////////////////////////////////////////////////////////////
bool RewritePipeline(TCompiler &compiler,
TIntermBlock &root,
IdGen &idGen,
const Pipeline &pipeline,
SymbolEnv &symbolEnv,
const std::vector<sh::ShaderVariable> *variableInfo,
PipelineScoped<TStructure> &outStruct)
{
ASSERT(outStruct.isTotallyEmpty());
TSymbolTable &symbolTable = compiler.getSymbolTable();
PipelineStructInfo psi;
if (!GeneratePipelineStruct::Exec(psi, compiler, root, idGen, pipeline, symbolEnv,
variableInfo))
{
return false;
}
if (psi.isEmpty())
{
return true;
}
const auto pipelineFunctions = DiscoverDependentFunctions(root, [&](const TVariable &var) {
return psi.pipelineVariables.find(&var) != psi.pipelineVariables.end();
});
auto pipelineMainLocalVar =
CreatePipelineMainLocalVar(symbolTable, pipeline, psi.pipelineStruct);
if (!UpdatePipelineFunctions::ThreadPipeline(
compiler, root, pipeline, pipelineFunctions, psi.pipelineStruct, pipelineMainLocalVar,
idGen, symbolEnv, psi.funcOriginalToModified, psi.funcModifiedToOriginal))
{
return false;
}
if (!pipeline.globalInstanceVar)
{
if (!UpdatePipelineSymbols(pipeline.type, compiler, root, symbolEnv, psi.pipelineVariables,
pipelineMainLocalVar))
{
return false;
}
}
if (!PruneNoOps(&compiler, &root, &compiler.getSymbolTable()))
{
return false;
}
outStruct = psi.pipelineStruct;
return true;
}
} // anonymous namespace
bool sh::RewritePipelines(TCompiler &compiler,
TIntermBlock &root,
const std::vector<sh::ShaderVariable> &inputVaryings,
const std::vector<sh::ShaderVariable> &outputVaryings,
IdGen &idGen,
DriverUniform &angleUniformsGlobalInstanceVar,
SymbolEnv &symbolEnv,
PipelineStructs &outStructs)
{
struct Info
{
Pipeline::Type pipelineType;
PipelineScoped<TStructure> &outStruct;
const TVariable *globalInstanceVar;
const std::vector<sh::ShaderVariable> *variableInfo;
};
Info infos[] = {
{Pipeline::Type::InstanceId, outStructs.instanceId, nullptr, nullptr},
{Pipeline::Type::Texture, outStructs.image, nullptr, nullptr},
{Pipeline::Type::Image, outStructs.texture, nullptr, nullptr},
{Pipeline::Type::NonConstantGlobals, outStructs.nonConstantGlobals, nullptr, nullptr},
{Pipeline::Type::AngleUniforms, outStructs.angleUniforms,
angleUniformsGlobalInstanceVar.getDriverUniformsVariable(), nullptr},
{Pipeline::Type::UserUniforms, outStructs.userUniforms, nullptr, nullptr},
{Pipeline::Type::VertexIn, outStructs.vertexIn, nullptr, &inputVaryings},
{Pipeline::Type::VertexOut, outStructs.vertexOut, nullptr, &outputVaryings},
{Pipeline::Type::FragmentIn, outStructs.fragmentIn, nullptr, &inputVaryings},
{Pipeline::Type::FragmentOut, outStructs.fragmentOut, nullptr, &outputVaryings},
{Pipeline::Type::InvocationVertexGlobals, outStructs.invocationVertexGlobals, nullptr,
nullptr},
{Pipeline::Type::InvocationFragmentGlobals, outStructs.invocationFragmentGlobals, nullptr,
&inputVaryings},
{Pipeline::Type::UniformBuffer, outStructs.uniformBuffers, nullptr, nullptr},
};
for (Info &info : infos)
{
if ((compiler.getShaderType() != GL_VERTEX_SHADER &&
(info.pipelineType == Pipeline::Type::VertexIn ||
info.pipelineType == Pipeline::Type::VertexOut ||
info.pipelineType == Pipeline::Type::InvocationVertexGlobals)) ||
(compiler.getShaderType() != GL_FRAGMENT_SHADER &&
(info.pipelineType == Pipeline::Type::FragmentIn ||
info.pipelineType == Pipeline::Type::FragmentOut ||
info.pipelineType == Pipeline::Type::InvocationFragmentGlobals)))
continue;
Pipeline pipeline{info.pipelineType, info.globalInstanceVar};
if (!RewritePipeline(compiler, root, idGen, pipeline, symbolEnv, info.variableInfo,
info.outStruct))
{
return false;
}
}
return true;
}