Hash :
45237a04
Author :
Date :
2022-01-11T00:14:01
Metal: Fix undefined behavior of depth write Writing to an unbound depth attachment is undefined behavior in Metal. Fix this by emitting a function constant to guard depth buffer use in fragment shaders. Bug: angleproject:6865 Change-Id: Id7c10d0aeb349aacfe09c397bc292a71199ab50a Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3380304 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Kenneth Russell <kbr@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
//
// Copyright 2022 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.
//
// GuardFragDepthWrite: Guards use of frag depth behind the function constant
// ANGLEDepthWriteEnabled to ensure it is only used when a valid depth buffer
// is bound.
#include "compiler/translator/TranslatorMetalDirect/GuardFragDepthWrite.h"
#include "compiler/translator/TranslatorMetalDirect/AstHelpers.h"
#include "compiler/translator/tree_util/BuiltIn.h"
#include "compiler/translator/tree_util/IntermRebuild.h"
using namespace sh;
////////////////////////////////////////////////////////////////////////////////
namespace
{
class Rewriter : public TIntermRebuild
{
public:
Rewriter(TCompiler &compiler) : TIntermRebuild(compiler, false, true) {}
PostResult visitBinaryPost(TIntermBinary &node) override
{
if (TIntermSymbol *leftSymbolNode = node.getLeft()->getAsSymbolNode())
{
if (leftSymbolNode->getType().getQualifier() == TQualifier::EvqFragDepth)
{
// This transformation leaves the tree in an inconsistent state by using a variable
// that's defined in text, outside of the knowledge of the AST.
// FIXME(jcunningham): remove once function constants (specconst) are implemented
// with the metal translator.
mCompiler.disableValidateVariableReferences();
TSymbolTable *symbolTable = &mCompiler.getSymbolTable();
// Create kDepthWriteEnabled variable reference.
TType *boolType = new TType(EbtBool);
boolType->setQualifier(EvqConst);
TVariable *depthWriteEnabledVar = new TVariable(
symbolTable, sh::ImmutableString(sh::mtl::kDepthWriteEnabledConstName),
boolType, SymbolType::AngleInternal);
TIntermBlock *innerif = new TIntermBlock;
innerif->appendStatement(&node);
TIntermSymbol *depthWriteEnabled = new TIntermSymbol(depthWriteEnabledVar);
TIntermIfElse *ifCall = new TIntermIfElse(depthWriteEnabled, innerif, nullptr);
return ifCall;
}
}
return node;
}
};
} // anonymous namespace
////////////////////////////////////////////////////////////////////////////////
bool sh::GuardFragDepthWrite(TCompiler &compiler, TIntermBlock &root)
{
Rewriter rewriter(compiler);
if (!rewriter.rebuildRoot(root))
{
return false;
}
return true;
}