Hash :
b96d1955
Author :
Date :
2023-01-20T16:06:34
Implement GL_ARM_shader_framebuffer_fetch GL_ARM_shader_framebuffer_fetch allows fragment shaders to read gl_LastFragColorARM. The extension is similar to GL_EXT_shader_framebuffer_fetch, but is more limited in its capabilities. GL_ARM_shader_framebuffer_fetch was removed from HasFramebufferFetch() because it acts differently from GL_EXT_shader_framebuffer_fetch in significant enough ways that it should not be included in that single-use function. Tests: FramebufferFetchES31.*_ARM Bug: b/242419750 Bug: angleproject:7882 Change-Id: Id3a25c8ee50ca3e2a4c30a2261f507b0b53511ec Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4083108 Commit-Queue: Sean Risser <srisser@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Cody Northrop <cnorthrop@google.com> Reviewed-by: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
//
// 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/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>;
constexpr unsigned int kInputAttachmentZero = 0;
constexpr unsigned int kArraySizeZero = 0;
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;
void visitSymbol(TIntermSymbol *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();
ASSERT(sequence.size() == 1);
TIntermSymbol *symbol = sequence.front()->getAsSymbolNode();
if (symbol == nullptr)
{
return true;
}
if (symbol->getType().getQualifier() == EvqFragmentInOut)
{
// The input attachment index is identical to the location qualifier. If there's only one
// output, GLSL is allowed to not specify the location qualifier, in which case it would
// implicitly be at location 0.
const unsigned int inputAttachmentIdx =
std::max(0, symbol->getType().getLayoutQualifier().location);
if (symbol->getType().isArray())
{
for (unsigned int index = 0; index < symbol->getType().getOutermostArraySize(); index++)
{
const 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;
}
void InputAttachmentReferenceTraverser::visitSymbol(TIntermSymbol *symbol)
{
if (symbol->getName() != "gl_LastFragColorARM")
{
return;
}
// Record usage of the gl_LastFragColorARM symbol in the AST so we know
// how to replace it with ANGLELastFragData.
const unsigned int inputAttachmentIdx =
std::max(0, symbol->getType().getLayoutQualifier().location);
setInputAttachmentIndex(inputAttachmentIdx);
mDeclaredSym->emplace(inputAttachmentIdx, symbol);
}
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);
}
}
TBasicType GetBasicTypeForSubpassInput(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());
}
TIntermTyped *CreateSubpassLoadFuncCall(TSymbolTable *symbolTable,
TBasicType inputType,
TIntermSequence *arguments)
{
return CreateBuiltInFunctionCallNode("subpassLoad", arguments, *symbolTable,
kESSLInternalBackendBuiltIns);
}
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();
}
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 uint8_t targetVecSize);
TIntermNode *loadInputAttachmentDataImpl(const size_t arraySize,
const unsigned int inputAttachmentIndex,
const TVariable *loadInputAttachmentDataVar);
ImmutableString getInputAttachmentName(unsigned int index);
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<unsigned int, TVariable *> mInputAttachmentVarList;
std::map<unsigned int, const TVariable *> mDataLoadVarList;
};
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, declaredVarSym->getPrecision(), 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 uint8_t targetVecSize)
{
// TODO: support interaction with multisampled framebuffers. For example, the sample ID needs
// to be provided to the built-in call here. http://anglebug.com/6195
TIntermSequence subpassArguments = {inputAttachmentSymbol};
TIntermTyped *subpassLoadFuncCall = CreateSubpassLoadFuncCall(
mSymbolTable, inputAttachmentSymbol->getBasicType(), &subpassArguments);
TIntermTyped *result = subpassLoadFuncCall;
if (targetVecSize < 4)
{
TVector<int> fieldOffsets(targetVecSize);
for (uint8_t i = 0; i < targetVecSize; i++)
{
fieldOffsets[i] = i;
}
result = new TIntermSwizzle(subpassLoadFuncCall, fieldOffsets);
}
return new TIntermBinary(EOpAssign, resultVar, result);
}
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
[[nodiscard]] bool ReplaceLastFrag(TCompiler *compiler,
TIntermBlock *root,
TSymbolTable *symbolTable,
std::vector<ShaderVariable> *uniforms,
FramebufferFetchReplaceTarget target)
{
// Common variables
InputAttachmentIdxSet constIndices;
std::map<unsigned int, TIntermSymbol *> glLastFragUsageMap;
unsigned int maxInputAttachmentIndex = 0;
bool usedNonConstIndex = false;
// Get informations for gl_LastFragData/gl_LastFragColorARM
InputAttachmentReferenceTraverser informationTraverser(
&glLastFragUsageMap, &maxInputAttachmentIndex, &constIndices, &usedNonConstIndex);
root->traverse(&informationTraverser);
if (constIndices.none() && !usedNonConstIndex)
{
// No references of gl_LastFragData/ColorARM
return true;
}
ReplaceGlLastFragDataUtils replaceSubpassInputUtils(
compiler, symbolTable, root, uniforms, usedNonConstIndex, constIndices, glLastFragUsageMap);
if (!replaceSubpassInputUtils.declareSubpassInputVariables())
{
return false;
}
// Declare the variables which store the result of subpassLoad function
const TVariable *glLastFragVar = nullptr;
if (glLastFragUsageMap.size() > 0)
{
glLastFragVar = &glLastFragUsageMap.begin()->second->variable();
}
else
{
if (target == FramebufferFetchReplaceTarget::LastFragData)
{
glLastFragVar = static_cast<const TVariable *>(
symbolTable->findBuiltIn(ImmutableString("gl_LastFragData"), 100));
}
else
{
glLastFragVar = static_cast<const TVariable *>(symbolTable->findBuiltIn(
ImmutableString("gl_LastFragColorARM"), compiler->getShaderVersion()));
}
}
if (!glLastFragVar)
{
return false;
}
ImmutableString loadVarName("ANGLELastFragData");
TType *loadVarType = new TType(glLastFragVar->getType());
loadVarType->setQualifier(EvqGlobal);
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/ColorARM with ANGLELastFragData
ReplaceVariableTraverser replaceTraverser(glLastFragVar, new TIntermSymbol(loadVar));
root->traverse(&replaceTraverser);
if (!replaceTraverser.updateTree(compiler, root))
{
return false;
}
return true;
}
[[nodiscard]] 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 inout variables
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;
TType *newOutVarType = new TType(originInOutVar->getType());
// We just want to use the original variable, but without an out qualifier instead of inout.
ASSERT(originInOutVar->getName() != "gl_LastFragData");
newOutVarType->setQualifier(EvqFragmentOut);
TVariable *newOutVar = new TVariable(symbolTable, originInOutVar->getName(), newOutVarType,
originInOutVar->variable().symbolType());
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);
return replaceTraverser.updateTree(compiler, root);
}
} // namespace sh