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 // ClampPointSize.cpp: Limit the value that is written to gl_PointSize.
7 //
8 
9 #include "compiler/translator/tree_ops/ClampPointSize.h"
10 
11 #include "compiler/translator/SymbolTable.h"
12 #include "compiler/translator/tree_util/BuiltIn.h"
13 #include "compiler/translator/tree_util/FindSymbolNode.h"
14 #include "compiler/translator/tree_util/IntermNode_util.h"
15 #include "compiler/translator/tree_util/RunAtTheEndOfShader.h"
16 
17 namespace sh
18 {
19 
ClampPointSize(TCompiler * compiler,TIntermBlock * root,float maxPointSize,TSymbolTable * symbolTable)20 bool ClampPointSize(TCompiler *compiler,
21                     TIntermBlock *root,
22                     float maxPointSize,
23                     TSymbolTable *symbolTable)
24 {
25     // Only clamp gl_PointSize if it's used in the shader.
26     if (!FindSymbolNode(root, ImmutableString("gl_PointSize")))
27     {
28         return true;
29     }
30 
31     TIntermSymbol *pointSizeNode = new TIntermSymbol(BuiltInVariable::gl_PointSize());
32 
33     TConstantUnion *maxPointSizeConstant = new TConstantUnion();
34     maxPointSizeConstant->setFConst(maxPointSize);
35     TIntermConstantUnion *maxPointSizeNode =
36         new TIntermConstantUnion(maxPointSizeConstant, TType(EbtFloat, EbpHigh, EvqConst));
37 
38     // min(gl_PointSize, maxPointSize)
39     TIntermSequence *minArguments = new TIntermSequence();
40     minArguments->push_back(pointSizeNode->deepCopy());
41     minArguments->push_back(maxPointSizeNode);
42     TIntermTyped *clampedPointSize =
43         CreateBuiltInFunctionCallNode("min", minArguments, *symbolTable, 100);
44 
45     // gl_PointSize = min(gl_PointSize, maxPointSize)
46     TIntermBinary *assignPointSize = new TIntermBinary(EOpAssign, pointSizeNode, clampedPointSize);
47 
48     return RunAtTheEndOfShader(compiler, root, assignPointSize, symbolTable);
49 }
50 
51 }  // namespace sh
52