Hash :
776c6015
Author :
Date :
2021-02-26T00:00:57
Vulkan: Call glslang at compile time
With this change, the ANGLE translator immediately compiles the
generated GLSL into SPIR-V with glslang and discards the source. This
is in preparation for generating SPIR-V directly, by making the frontend
and backend already able to digest it.
This change also allows the expensive glslang calls to be parallelized,
improving the following perf test by about 20%:
LinkProgramBenchmark.Run/vulkan_compile_and_link_multi_thread
Previously, the test was run as such in the Vulkan backend:
Main Thread 1 Thread 2
Compile1 --->
Compile2 --------------------->
Translator Translator
<---
<---------------------
Link
glslang
for
shader1
glslang
for
shader2
Done
With this change, it is run as such:
Main Thread 1 Thread 2
Compile1 --->
Compile2 --------------------->
Translator Translator
glslang glslang
<---
<---------------------
Link
Done
glslang_wrapper_utils no longer interacts with glslang! A rename will
follow.
Bug: angleproject:4889
Change-Id: If4303e8ba0ba43b1a2f47f8c0a9133d0bee1a19a
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2721195
Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
Reviewed-by: Tim Van Patten <timvp@google.com>
Reviewed-by: Jamie Madill <jmadill@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
//
// 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.
//
// ReplaceForShaderFramebufferFetch.h: Find any references to gl_LastFragData, and replace it with
// ANGLELastFragData.
//
#include "compiler/translator/tree_ops/vulkan/ReplaceForShaderFramebufferFetch.h"
#include "common/bitset_utils.h"
#include "compiler/translator/ImmutableStringBuilder.h"
#include "compiler/translator/StaticType.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/BuiltIn.h"
#include "compiler/translator/tree_util/IntermNode_util.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/tree_util/RunAtTheBeginningOfShader.h"
#include "compiler/translator/tree_util/SpecializationConstant.h"
#include "compiler/translator/util.h"
namespace sh
{
namespace
{
using InputAttachmentIdxSet = angle::BitSet<32>;
using MapForReplacement = const std::map<const TVariable *, const TIntermTyped *>;
constexpr unsigned int kInputAttachmentZero = 0;
constexpr unsigned int kArraySizeZero = 0;
enum class InputType
{
SubpassInput = 0,
SubpassInputMS,
ISubpassInput,
ISubpassInputMS,
USubpassInput,
USubpassInputMS,
InvalidEnum,
EnumCount = InvalidEnum,
};
class InputAttachmentReferenceTraverser : public TIntermTraverser
{
public:
InputAttachmentReferenceTraverser(std::map<unsigned int, TIntermSymbol *> *declaredSymOut,
unsigned int *maxInputAttachmentIndex,
InputAttachmentIdxSet *constIndicesOut,
bool *usedNonConstIndex)
: TIntermTraverser(true, false, false),
mDeclaredSym(declaredSymOut),
mMaxInputAttachmentIndex(maxInputAttachmentIndex),
mConstInputAttachmentIndices(constIndicesOut),
mUsedNonConstIndex(usedNonConstIndex)
{
mDeclaredSym->clear();
*mMaxInputAttachmentIndex = 0;
mConstInputAttachmentIndices->reset();
*mUsedNonConstIndex = false;
}
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
bool visitBinary(Visit visit, TIntermBinary *node) override;
private:
void setInputAttachmentIndex(unsigned int index);
std::map<unsigned int, TIntermSymbol *> *mDeclaredSym;
unsigned int *mMaxInputAttachmentIndex;
InputAttachmentIdxSet *mConstInputAttachmentIndices;
bool *mUsedNonConstIndex;
};
class ReplaceVariableTraverser : public TIntermTraverser
{
public:
ReplaceVariableTraverser(
const std::map<const TVariable *, const TIntermTyped *> &replacementMap)
: TIntermTraverser(true, false, false), mReplacementMap(replacementMap)
{}
ReplaceVariableTraverser(const TVariable *toBeReplaced, const TIntermTyped *replacement)
: TIntermTraverser(true, false, false), mReplacementMap({{toBeReplaced, replacement}})
{}
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
void visitSymbol(TIntermSymbol *node) override;
private:
const std::map<const TVariable *, const TIntermTyped *> mReplacementMap;
};
void InputAttachmentReferenceTraverser::setInputAttachmentIndex(unsigned int inputAttachmentIdx)
{
ASSERT(inputAttachmentIdx < mConstInputAttachmentIndices->size());
mConstInputAttachmentIndices->set(inputAttachmentIdx);
*mMaxInputAttachmentIndex = std::max(*mMaxInputAttachmentIndex, inputAttachmentIdx);
}
bool InputAttachmentReferenceTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
{
const TIntermSequence &sequence = *(node->getSequence());
if (sequence.size() != 1)
{
return true;
}
TIntermTyped *variable = sequence.front()->getAsTyped();
TIntermSymbol *symbol = variable->getAsSymbolNode();
if (symbol == nullptr)
{
return true;
}
if (symbol->getType().getQualifier() == EvqFragmentInOut)
{
unsigned int inputAttachmentIdx = symbol->getType().getLayoutQualifier().location;
if (symbol->getType().isArray())
{
for (unsigned int index = 0; index < symbol->getType().getOutermostArraySize(); index++)
{
unsigned int realInputAttachmentIdx = inputAttachmentIdx + index;
setInputAttachmentIndex(realInputAttachmentIdx);
}
}
else
{
setInputAttachmentIndex(inputAttachmentIdx);
}
mDeclaredSym->emplace(inputAttachmentIdx, symbol);
}
return true;
}
bool InputAttachmentReferenceTraverser::visitBinary(Visit visit, TIntermBinary *node)
{
TOperator op = node->getOp();
if (op != EOpIndexDirect && op != EOpIndexIndirect)
{
return true;
}
TIntermSymbol *left = node->getLeft()->getAsSymbolNode();
if (!left)
{
return true;
}
else if (left->getName() != "gl_LastFragData")
{
return true;
}
const TConstantUnion *constIdx = node->getRight()->getConstantValue();
if (!constIdx)
{
// If the shader code uses gl_LastFragData with a non-const index, the input attachment
// variable should be created as the maximum number. So, the previous redeclared
// variable will be reset.
mDeclaredSym->clear();
*mUsedNonConstIndex = true;
mDeclaredSym->emplace(0, left);
return true;
}
else
{
unsigned int idx = 0;
switch (constIdx->getType())
{
case EbtInt:
idx = constIdx->getIConst();
break;
case EbtUInt:
idx = constIdx->getUConst();
break;
case EbtFloat:
idx = static_cast<unsigned int>(constIdx->getFConst());
break;
case EbtBool:
idx = constIdx->getBConst() ? 1 : 0;
break;
default:
UNREACHABLE();
break;
}
ASSERT(idx < mConstInputAttachmentIndices->size());
mConstInputAttachmentIndices->set(idx);
*mMaxInputAttachmentIndex = std::max(*mMaxInputAttachmentIndex, idx);
mDeclaredSym->emplace(idx, left);
}
return true;
}
bool ReplaceVariableTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
{
const TIntermSequence &sequence = *(node->getSequence());
if (sequence.size() != 1)
{
return true;
}
TIntermTyped *nodeType = sequence.front()->getAsTyped();
TIntermSymbol *symbol = nodeType->getAsSymbolNode();
if (symbol == nullptr)
{
return true;
}
const TVariable *variable = &symbol->variable();
if (mReplacementMap.find(variable) != mReplacementMap.end())
{
TIntermSequence emptyReplacement;
mMultiReplacements.emplace_back(getParentNode()->getAsBlock(), node,
std::move(emptyReplacement));
return true;
}
return true;
}
void ReplaceVariableTraverser::visitSymbol(TIntermSymbol *node)
{
const TVariable *variable = &node->variable();
if (mReplacementMap.find(variable) != mReplacementMap.end())
{
queueReplacement(mReplacementMap.at(variable)->deepCopy(), OriginalNode::IS_DROPPED);
}
}
InputType GetInputTypeOfSubpassInput(const TBasicType &basicType)
{
switch (basicType)
{
case TBasicType::EbtSubpassInput:
return InputType::SubpassInput;
case TBasicType::EbtSubpassInputMS:
return InputType::SubpassInputMS;
case TBasicType::EbtISubpassInput:
return InputType::ISubpassInput;
case TBasicType::EbtISubpassInputMS:
return InputType::ISubpassInputMS;
case TBasicType::EbtUSubpassInput:
return InputType::USubpassInput;
case TBasicType::EbtUSubpassInputMS:
return InputType::USubpassInputMS;
default:
UNREACHABLE();
return InputType::InvalidEnum;
}
}
TBasicType GetBasicTypeOfSubpassInput(const InputType &inputType)
{
switch (inputType)
{
case InputType::SubpassInput:
return EbtSubpassInput;
case InputType::SubpassInputMS:
return EbtSubpassInputMS;
case InputType::ISubpassInput:
return EbtISubpassInput;
case InputType::ISubpassInputMS:
return EbtISubpassInputMS;
case InputType::USubpassInput:
return EbtUSubpassInput;
case InputType::USubpassInputMS:
return EbtUSubpassInputMS;
default:
UNREACHABLE();
return TBasicType::EbtVoid;
}
}
TBasicType GetBasicTypeForSubpassInput(const TBasicType &inputType)
{
switch (inputType)
{
case EbtFloat:
return EbtSubpassInput;
case EbtInt:
return EbtISubpassInput;
case EbtUInt:
return EbtUSubpassInput;
default:
UNREACHABLE();
return EbtVoid;
}
}
TBasicType GetBasicTypeForSubpassInput(const TIntermSymbol *originSymbol)
{
if (originSymbol->getName().beginsWith("gl_LastFragData"))
{
return GetBasicTypeForSubpassInput(EbtFloat);
}
return GetBasicTypeForSubpassInput(originSymbol->getBasicType());
}
ImmutableString GetTypeNameOfSubpassInput(const InputType &inputType)
{
switch (inputType)
{
case InputType::SubpassInput:
return ImmutableString("subpassInput");
case InputType::SubpassInputMS:
return ImmutableString("subpassInputMS");
case InputType::ISubpassInput:
return ImmutableString("isubpassInput");
case InputType::ISubpassInputMS:
return ImmutableString("isubpassInputMS");
case InputType::USubpassInput:
return ImmutableString("usubpassInput");
case InputType::USubpassInputMS:
return ImmutableString("usubpassInputMS");
default:
UNREACHABLE();
return kEmptyImmutableString;
}
}
ImmutableString GetFunctionNameOfSubpassLoad(const InputType &inputType)
{
switch (inputType)
{
case InputType::SubpassInput:
case InputType::ISubpassInput:
case InputType::USubpassInput:
return ImmutableString("subpassLoad");
case InputType::SubpassInputMS:
case InputType::ISubpassInputMS:
case InputType::USubpassInputMS:
return ImmutableString("subpassLoadMS");
default:
UNREACHABLE();
return kEmptyImmutableString;
}
}
TIntermAggregate *CreateSubpassLoadFuncCall(TSymbolTable *symbolTable,
std::map<InputType, TFunction *> *functionMap,
const InputType &inputType,
TIntermSequence *arguments)
{
TBasicType subpassInputType = GetBasicTypeOfSubpassInput(inputType);
ASSERT(subpassInputType != TBasicType::EbtVoid);
TFunction **currentFunc = &(*functionMap)[inputType];
if (*currentFunc == nullptr)
{
TType *inputAttachmentType = new TType(subpassInputType, EbpUndefined, EvqUniform, 1);
*currentFunc = new TFunction(symbolTable, GetFunctionNameOfSubpassLoad(inputType),
SymbolType::AngleInternal,
new TType(EbtFloat, EbpUndefined, EvqGlobal, 4, 1), true);
(*currentFunc)
->addParameter(new TVariable(symbolTable, GetTypeNameOfSubpassInput(inputType),
inputAttachmentType, SymbolType::AngleInternal));
}
return TIntermAggregate::CreateFunctionCall(**currentFunc, arguments);
}
class ReplaceSubpassInputUtils
{
public:
ReplaceSubpassInputUtils(TCompiler *compiler,
TSymbolTable *symbolTable,
TIntermBlock *root,
std::vector<ShaderVariable> *uniforms,
const bool usedNonConstIndex,
const InputAttachmentIdxSet &constIndices,
const std::map<unsigned int, TIntermSymbol *> &declaredVarVec)
: mCompiler(compiler),
mSymbolTable(symbolTable),
mRoot(root),
mUniforms(uniforms),
mUsedNonConstIndex(usedNonConstIndex),
mConstIndices(constIndices),
mDeclaredVarVec(declaredVarVec)
{
mDeclareVariables.clear();
mInputAttachmentArrayIdSeq = 0;
mInputAttachmentVarList.clear();
mDataLoadVarList.clear();
mFunctionMap.clear();
}
virtual ~ReplaceSubpassInputUtils() = default;
virtual bool declareSubpassInputVariables() = 0;
void declareVariablesForFetch(const unsigned int inputAttachmentIndex,
const TVariable *dataLoadVar)
{
mDataLoadVarList[inputAttachmentIndex] = dataLoadVar;
TIntermDeclaration *dataLoadVarDecl = new TIntermDeclaration;
TIntermSymbol *dataLoadVarDeclarator =
new TIntermSymbol(mDataLoadVarList[inputAttachmentIndex]);
dataLoadVarDecl->appendDeclarator(dataLoadVarDeclarator);
mDeclareVariables.push_back(dataLoadVarDecl);
}
virtual bool loadInputAttachmentData() = 0;
void submitNewDeclaration()
{
for (unsigned int index = 0; index < mDeclareVariables.size(); index++)
{
mRoot->insertStatement(index, mDeclareVariables[index]);
}
mDeclareVariables.clear();
}
protected:
bool declareSubpassInputVariableImpl(const TIntermSymbol *declaredVarSym,
const unsigned int inputAttachmentIndex);
void addInputAttachmentUniform(const unsigned int inputAttachmentIndex);
TIntermNode *assignSubpassLoad(TIntermTyped *resultVar,
TIntermTyped *inputAttachmentSymbol,
const int targetVecSize);
TIntermNode *loadInputAttachmentDataImpl(const size_t arraySize,
const unsigned int inputAttachmentIndex,
const TVariable *loadInputAttachmentDataVar);
ImmutableString getInputAttachmentName(unsigned int index);
ImmutableString getInputAttachmentArrayName();
TCompiler *mCompiler;
TSymbolTable *mSymbolTable;
TIntermBlock *mRoot;
std::vector<ShaderVariable> *mUniforms;
const bool mUsedNonConstIndex;
const InputAttachmentIdxSet mConstIndices;
const std::map<unsigned int, TIntermSymbol *> mDeclaredVarVec;
TIntermSequence mDeclareVariables;
unsigned int mInputAttachmentArrayIdSeq;
std::map<InputType, TFunction *> mFunctionMap;
std::map<unsigned int, TVariable *> mInputAttachmentVarList;
std::map<unsigned int, const TVariable *> mDataLoadVarList;
};
ImmutableString ReplaceSubpassInputUtils::getInputAttachmentArrayName()
{
constexpr ImmutableString suffix("Array");
std::stringstream nameStream = sh::InitializeStream<std::stringstream>();
nameStream << sh::vk::kInputAttachmentName << suffix << mInputAttachmentArrayIdSeq++;
return ImmutableString(nameStream.str());
}
ImmutableString ReplaceSubpassInputUtils::getInputAttachmentName(unsigned int index)
{
std::stringstream nameStream = sh::InitializeStream<std::stringstream>();
nameStream << sh::vk::kInputAttachmentName << index;
return ImmutableString(nameStream.str());
}
bool ReplaceSubpassInputUtils::declareSubpassInputVariableImpl(
const TIntermSymbol *declaredVarSym,
const unsigned int inputAttachmentIndex)
{
TBasicType subpassInputType = GetBasicTypeForSubpassInput(declaredVarSym);
if (subpassInputType == EbtVoid)
{
return false;
}
TType *inputAttachmentType = new TType(subpassInputType, EbpUndefined, EvqUniform, 1);
TLayoutQualifier inputAttachmentQualifier = inputAttachmentType->getLayoutQualifier();
inputAttachmentQualifier.inputAttachmentIndex = inputAttachmentIndex;
inputAttachmentType->setLayoutQualifier(inputAttachmentQualifier);
mInputAttachmentVarList[inputAttachmentIndex] =
new TVariable(mSymbolTable, getInputAttachmentName(inputAttachmentIndex),
inputAttachmentType, SymbolType::AngleInternal);
TIntermSymbol *inputAttachmentDeclarator =
new TIntermSymbol(mInputAttachmentVarList[inputAttachmentIndex]);
TIntermDeclaration *inputAttachmentDecl = new TIntermDeclaration;
inputAttachmentDecl->appendDeclarator(inputAttachmentDeclarator);
mDeclareVariables.push_back(inputAttachmentDecl);
return true;
}
void ReplaceSubpassInputUtils::addInputAttachmentUniform(const unsigned int inputAttachmentIndex)
{
const TVariable *inputAttachmentVar = mInputAttachmentVarList[inputAttachmentIndex];
ShaderVariable inputAttachmentUniform;
inputAttachmentUniform.active = true;
inputAttachmentUniform.staticUse = true;
inputAttachmentUniform.name.assign(inputAttachmentVar->name().data(),
inputAttachmentVar->name().length());
inputAttachmentUniform.mappedName.assign(inputAttachmentUniform.name);
inputAttachmentUniform.isFragmentInOut = true;
inputAttachmentUniform.location =
inputAttachmentVar->getType().getLayoutQualifier().inputAttachmentIndex;
mUniforms->push_back(inputAttachmentUniform);
}
TIntermNode *ReplaceSubpassInputUtils::assignSubpassLoad(TIntermTyped *resultVar,
TIntermTyped *inputAttachmentSymbol,
const int targetVecSize)
{
TIntermSequence *subpassArguments = new TIntermSequence();
subpassArguments->push_back(inputAttachmentSymbol);
TIntermAggregate *subpassLoadFuncCall = CreateSubpassLoadFuncCall(
mSymbolTable, &mFunctionMap,
GetInputTypeOfSubpassInput(inputAttachmentSymbol->getBasicType()), subpassArguments);
TVector<int> fieldOffsets(targetVecSize);
for (int i = 0; i < targetVecSize; i++)
{
fieldOffsets[i] = i;
}
TIntermTyped *right = new TIntermSwizzle(subpassLoadFuncCall, fieldOffsets);
return new TIntermBinary(EOpAssign, resultVar, right);
}
TIntermNode *ReplaceSubpassInputUtils::loadInputAttachmentDataImpl(
const size_t arraySize,
const unsigned int inputAttachmentIndex,
const TVariable *loadInputAttachmentDataVar)
{
TIntermNode *retExpression = nullptr;
TIntermSymbol *loadInputAttachmentDataSymbol = new TIntermSymbol(loadInputAttachmentDataVar);
TIntermTyped *left = nullptr;
if (arraySize > 0)
{
TIntermBlock *blockNode = new TIntermBlock();
for (uint32_t index = 0; index < arraySize; index++)
{
uint32_t attachmentIndex = inputAttachmentIndex + index;
left = new TIntermBinary(EOpIndexDirect, loadInputAttachmentDataSymbol->deepCopy(),
CreateIndexNode(index));
blockNode->appendStatement(
assignSubpassLoad(left, new TIntermSymbol(mInputAttachmentVarList[attachmentIndex]),
left->getNominalSize()));
}
retExpression = blockNode;
}
else
{
if (loadInputAttachmentDataSymbol->isArray())
{
left = new TIntermBinary(EOpIndexDirect, loadInputAttachmentDataSymbol->deepCopy(),
CreateIndexNode(inputAttachmentIndex));
}
else
{
left = loadInputAttachmentDataSymbol->deepCopy();
}
retExpression = assignSubpassLoad(
left, new TIntermSymbol(mInputAttachmentVarList[inputAttachmentIndex]),
left->getNominalSize());
}
return retExpression;
}
class ReplaceGlLastFragDataUtils : public ReplaceSubpassInputUtils
{
public:
ReplaceGlLastFragDataUtils(TCompiler *compiler,
TSymbolTable *symbolTable,
TIntermBlock *root,
std::vector<ShaderVariable> *uniforms,
const bool usedNonConstIndex,
const InputAttachmentIdxSet &constIndices,
const std::map<unsigned int, TIntermSymbol *> &declaredVarVec)
: ReplaceSubpassInputUtils(compiler,
symbolTable,
root,
uniforms,
usedNonConstIndex,
constIndices,
declaredVarVec)
{}
bool declareSubpassInputVariables() override;
bool loadInputAttachmentData() override;
};
bool ReplaceGlLastFragDataUtils::declareSubpassInputVariables()
{
for (auto declaredVar : mDeclaredVarVec)
{
const unsigned int inputAttachmentIndex = declaredVar.first;
if (mConstIndices.test(inputAttachmentIndex))
{
if (!declareSubpassInputVariableImpl(declaredVar.second, inputAttachmentIndex))
{
return false;
}
addInputAttachmentUniform(inputAttachmentIndex);
}
}
return true;
}
bool ReplaceGlLastFragDataUtils::loadInputAttachmentData()
{
TIntermNode *loadInputAttachmentBlock = new TIntermBlock();
for (auto declaredVar : mDeclaredVarVec)
{
const unsigned int inputAttachmentIndex = declaredVar.first;
if (mConstIndices.test(inputAttachmentIndex))
{
loadInputAttachmentBlock->getAsBlock()->appendStatement(loadInputAttachmentDataImpl(
kArraySizeZero, inputAttachmentIndex, mDataLoadVarList[kArraySizeZero]));
}
}
ASSERT(loadInputAttachmentBlock->getChildCount() > 0);
if (!RunAtTheBeginningOfShader(mCompiler, mRoot, loadInputAttachmentBlock))
{
return false;
}
return true;
}
class ReplaceInOutUtils : public ReplaceSubpassInputUtils
{
public:
ReplaceInOutUtils(TCompiler *compiler,
TSymbolTable *symbolTable,
TIntermBlock *root,
std::vector<ShaderVariable> *uniforms,
const bool usedNonConstIndex,
const InputAttachmentIdxSet &constIndices,
const std::map<unsigned int, TIntermSymbol *> &declaredVarVec)
: ReplaceSubpassInputUtils(compiler,
symbolTable,
root,
uniforms,
usedNonConstIndex,
constIndices,
declaredVarVec)
{}
bool declareSubpassInputVariables() override;
bool loadInputAttachmentData() override;
};
bool ReplaceInOutUtils::declareSubpassInputVariables()
{
for (auto declaredVar : mDeclaredVarVec)
{
const unsigned int inputAttachmentIndex = declaredVar.first;
const unsigned int arraySize =
(declaredVar.second->isArray() ? declaredVar.second->getOutermostArraySize() : 1);
for (unsigned int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
{
unsigned int attachmentIndex = inputAttachmentIndex + arrayIndex;
if (!declareSubpassInputVariableImpl(declaredVar.second, attachmentIndex))
{
return false;
}
addInputAttachmentUniform(attachmentIndex);
}
}
return true;
}
bool ReplaceInOutUtils::loadInputAttachmentData()
{
TIntermBlock *loadInputAttachmentBlock = new TIntermBlock();
for (auto declaredVar : mDeclaredVarVec)
{
const unsigned int inputAttachmentIndex = declaredVar.first;
size_t arraySize =
(declaredVar.second->isArray() ? declaredVar.second->getOutermostArraySize() : 0);
loadInputAttachmentBlock->appendStatement(loadInputAttachmentDataImpl(
arraySize, inputAttachmentIndex, mDataLoadVarList[inputAttachmentIndex]));
}
ASSERT(loadInputAttachmentBlock->getChildCount() > 0);
if (!RunAtTheBeginningOfShader(mCompiler, mRoot, loadInputAttachmentBlock))
{
return false;
}
return true;
}
} // anonymous namespace
ANGLE_NO_DISCARD bool ReplaceLastFragData(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
std::vector<ShaderVariable> *uniforms)
{
// Common variables
InputAttachmentIdxSet constIndices;
std::map<unsigned int, TIntermSymbol *> glLastFragDataUsageMap;
unsigned int maxInputAttachmentIndex = 0;
bool usedNonConstIndex = false;
// Get informations for gl_LastFragData
InputAttachmentReferenceTraverser informationTraverser(
&glLastFragDataUsageMap, &maxInputAttachmentIndex, &constIndices, &usedNonConstIndex);
root->traverse(&informationTraverser);
if (constIndices.none() && !usedNonConstIndex)
{
// No references of gl_LastFragData
return true;
}
// Declare subpassInput uniform variables
ReplaceGlLastFragDataUtils replaceSubpassInputUtils(compiler, symbolTable, root, uniforms,
usedNonConstIndex, constIndices,
glLastFragDataUsageMap);
if (!replaceSubpassInputUtils.declareSubpassInputVariables())
{
return false;
}
// Declare the variables which store the result of subpassLoad function
const TVariable *glLastFragDataVar = nullptr;
if (glLastFragDataUsageMap.size() > 0)
{
glLastFragDataVar = &glLastFragDataUsageMap.begin()->second->variable();
}
else
{
glLastFragDataVar = static_cast<const TVariable *>(
symbolTable->findBuiltIn(ImmutableString("gl_LastFragData"), 100));
}
if (!glLastFragDataVar)
{
return false;
}
const TBasicType loadVarBasicType = glLastFragDataVar->getType().getBasicType();
const TPrecision loadVarPrecision = glLastFragDataVar->getType().getPrecision();
const unsigned int loadVarVecSize = glLastFragDataVar->getType().getNominalSize();
const int loadVarArraySize = glLastFragDataVar->getType().getOutermostArraySize();
ImmutableString loadVarName("ANGLELastFragData");
TType *loadVarType = new TType(loadVarBasicType, loadVarPrecision, EvqGlobal,
static_cast<unsigned char>(loadVarVecSize));
loadVarType->makeArray(loadVarArraySize);
TVariable *loadVar =
new TVariable(symbolTable, loadVarName, loadVarType, SymbolType::AngleInternal);
replaceSubpassInputUtils.declareVariablesForFetch(kInputAttachmentZero, loadVar);
replaceSubpassInputUtils.submitNewDeclaration();
// 3) Add the routine for reading InputAttachment data
if (!replaceSubpassInputUtils.loadInputAttachmentData())
{
return false;
}
// 4) Replace gl_LastFragData with ANGLELastFragData
ReplaceVariableTraverser replaceTraverser(glLastFragDataVar, new TIntermSymbol(loadVar));
root->traverse(&replaceTraverser);
if (!replaceTraverser.updateTree(compiler, root))
{
return false;
}
return true;
}
ANGLE_NO_DISCARD bool ReplaceInOutVariables(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
std::vector<ShaderVariable> *uniforms)
{
// Common variables
InputAttachmentIdxSet constIndices;
std::map<unsigned int, TIntermSymbol *> declaredInOutVarMap;
unsigned int maxInputAttachmentIndex = 0;
bool usedNonConstIndex = false;
// Get informations for gl_LastFragData
InputAttachmentReferenceTraverser informationTraverser(
&declaredInOutVarMap, &maxInputAttachmentIndex, &constIndices, &usedNonConstIndex);
root->traverse(&informationTraverser);
if (declaredInOutVarMap.size() == 0)
{
// No references of the variable decorated with a inout qualifier
return true;
}
// Declare subpassInput uniform variables
ReplaceInOutUtils replaceSubpassInputUtils(compiler, symbolTable, root, uniforms,
usedNonConstIndex, constIndices,
declaredInOutVarMap);
if (!replaceSubpassInputUtils.declareSubpassInputVariables())
{
return false;
}
std::map<unsigned int, const TVariable *> toBeReplaced;
std::map<unsigned int, const TVariable *> newOutVarArray;
for (auto originInOutVarIter : declaredInOutVarMap)
{
const unsigned int inputAttachmentIndex = originInOutVarIter.first;
const TIntermSymbol *originInOutVar = originInOutVarIter.second;
const TBasicType loadVarBasicType = originInOutVar->getType().getBasicType();
const TPrecision loadVarPrecision = originInOutVar->getType().getPrecision();
const unsigned int loadVarVecSize = originInOutVar->getType().getNominalSize();
const unsigned int loadVarArraySize =
(originInOutVar->isArray() ? originInOutVar->getOutermostArraySize() : 0);
TType *newOutVarType = new TType(loadVarBasicType, loadVarPrecision, EvqGlobal,
static_cast<unsigned char>(loadVarVecSize));
// We just want to use the original variable decorated with a inout qualifier, except
// the qualifier itself. The qualifier will be changed from inout to out.
newOutVarType->setQualifier(TQualifier::EvqFragmentOut);
if (loadVarArraySize > 0)
{
newOutVarType->makeArray(loadVarArraySize);
}
TVariable *newOutVar = new TVariable(symbolTable, originInOutVar->getName(), newOutVarType,
SymbolType::UserDefined);
newOutVarArray[inputAttachmentIndex] = newOutVar;
replaceSubpassInputUtils.declareVariablesForFetch(inputAttachmentIndex,
newOutVarArray[inputAttachmentIndex]);
toBeReplaced[inputAttachmentIndex] = &originInOutVar->variable();
}
replaceSubpassInputUtils.submitNewDeclaration();
// 3) Add the routine for reading InputAttachment data
if (!replaceSubpassInputUtils.loadInputAttachmentData())
{
return false;
}
std::map<const TVariable *, const TIntermTyped *> replacementMap;
for (auto newOutVar = newOutVarArray.begin(); newOutVar != newOutVarArray.end(); newOutVar++)
{
const unsigned int index = newOutVar->first;
replacementMap[toBeReplaced[index]] = new TIntermSymbol(newOutVar->second);
}
// 4) Replace previous 'inout' variable with newly created 'inout' variable
ReplaceVariableTraverser replaceTraverser(replacementMap);
root->traverse(&replaceTraverser);
if (!replaceTraverser.updateTree(compiler, root))
{
return false;
}
return true;
}
} // namespace sh