xref: /aosp_15_r20/external/angle/src/compiler/translator/tree_ops/ClampFragDepth.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2017 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // ClampFragDepth.cpp: Limit the value that is written to gl_FragDepth to the range [0.0, 1.0].
7 // The clamping is run at the very end of shader execution, and is only performed if the shader
8 // statically accesses gl_FragDepth.
9 //
10 
11 #include "compiler/translator/tree_ops/ClampFragDepth.h"
12 
13 #include "compiler/translator/ImmutableString.h"
14 #include "compiler/translator/SymbolTable.h"
15 #include "compiler/translator/tree_util/BuiltIn.h"
16 #include "compiler/translator/tree_util/FindSymbolNode.h"
17 #include "compiler/translator/tree_util/IntermNode_util.h"
18 #include "compiler/translator/tree_util/RunAtTheEndOfShader.h"
19 
20 namespace sh
21 {
22 
ClampFragDepth(TCompiler * compiler,TIntermBlock * root,TSymbolTable * symbolTable)23 bool ClampFragDepth(TCompiler *compiler, TIntermBlock *root, TSymbolTable *symbolTable)
24 {
25     // Only clamp gl_FragDepth if it's used in the shader.
26     const TIntermSymbol *fragDepthSymbol = FindSymbolNode(root, ImmutableString("gl_FragDepth"));
27     if (!fragDepthSymbol)
28     {
29         return true;
30     }
31 
32     TIntermSymbol *fragDepthNode = new TIntermSymbol(&fragDepthSymbol->variable());
33 
34     TIntermTyped *minFragDepthNode = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
35 
36     TConstantUnion *maxFragDepthConstant = new TConstantUnion();
37     maxFragDepthConstant->setFConst(1.0);
38     TIntermConstantUnion *maxFragDepthNode =
39         new TIntermConstantUnion(maxFragDepthConstant, TType(EbtFloat, EbpHigh, EvqConst));
40 
41     // clamp(gl_FragDepth, 0.0, 1.0)
42     TIntermSequence clampArguments;
43     clampArguments.push_back(fragDepthNode->deepCopy());
44     clampArguments.push_back(minFragDepthNode);
45     clampArguments.push_back(maxFragDepthNode);
46     TIntermTyped *clampedFragDepth =
47         CreateBuiltInFunctionCallNode("clamp", &clampArguments, *symbolTable, 100);
48 
49     // gl_FragDepth = clamp(gl_FragDepth, 0.0, 1.0)
50     TIntermBinary *assignFragDepth = new TIntermBinary(EOpAssign, fragDepthNode, clampedFragDepth);
51 
52     return RunAtTheEndOfShader(compiler, root, assignFragDepth, symbolTable);
53 }
54 
55 }  // namespace sh
56