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
|
//
// 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.
//
// The ValidateClipCullDistance function checks if the sum of array sizes for gl_ClipDistance and
// gl_CullDistance exceeds gl_MaxCombinedClipAndCullDistances
//
#include "ValidateClipCullDistance.h"
#include "compiler/translator/Diagnostics.h"
#include "compiler/translator/SymbolTable.h"
#include "compiler/translator/tree_util/IntermTraverse.h"
#include "compiler/translator/util.h"
namespace sh
{
namespace
{
void error(const TIntermSymbol &symbol, const char *reason, TDiagnostics *diagnostics)
{
diagnostics->error(symbol.getLine(), reason, symbol.getName().data());
}
class ValidateClipCullDistanceTraverser : public TIntermTraverser
{
public:
ValidateClipCullDistanceTraverser();
void validate(TDiagnostics *diagnostics, const unsigned int maxCombinedClipAndCullDistances);
private:
bool visitDeclaration(Visit visit, TIntermDeclaration *node) override;
bool visitBinary(Visit visit, TIntermBinary *node) override;
unsigned int mClipDistanceSize;
unsigned int mCullDistanceSize;
unsigned int mMaxClipDistanceIndex;
unsigned int mMaxCullDistanceIndex;
const TIntermSymbol *mClipDistance;
const TIntermSymbol *mCullDistance;
};
ValidateClipCullDistanceTraverser::ValidateClipCullDistanceTraverser()
: TIntermTraverser(true, false, false),
mClipDistanceSize(0),
mCullDistanceSize(0),
mMaxClipDistanceIndex(0),
mMaxCullDistanceIndex(0),
mClipDistance(nullptr),
mCullDistance(nullptr)
{}
bool ValidateClipCullDistanceTraverser::visitDeclaration(Visit visit, TIntermDeclaration *node)
{
const TIntermSequence &sequence = *(node->getSequence());
if (sequence.size() != 1)
{
return true;
}
const TIntermSymbol *symbol = sequence.front()->getAsSymbolNode();
if (symbol == nullptr)
{
return true;
}
if (symbol->getName() == "gl_ClipDistance")
{
mClipDistanceSize = symbol->getOutermostArraySize();
mClipDistance = symbol;
}
else if (symbol->getName() == "gl_CullDistance")
{
mCullDistanceSize = symbol->getOutermostArraySize();
mCullDistance = symbol;
}
return true;
}
bool ValidateClipCullDistanceTraverser::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;
}
ImmutableString varName(left->getName());
if (varName != "gl_ClipDistance" && varName != "gl_CullDistance")
{
return true;
}
const TConstantUnion *constIdx = node->getRight()->getConstantValue();
if (constIdx)
{
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;
}
if (varName == "gl_ClipDistance")
{
if (idx > mMaxClipDistanceIndex)
{
mMaxClipDistanceIndex = idx;
if (!mClipDistance)
{
mClipDistance = left;
}
}
}
else
{
ASSERT(varName == "gl_CullDistance");
if (idx > mMaxCullDistanceIndex)
{
mMaxCullDistanceIndex = idx;
if (!mCullDistance)
{
mCullDistance = left;
}
}
}
}
return true;
}
void ValidateClipCullDistanceTraverser::validate(TDiagnostics *diagnostics,
const unsigned int maxCombinedClipAndCullDistances)
{
ASSERT(diagnostics);
unsigned int enabledClipDistances =
(mClipDistanceSize > 0 ? mClipDistanceSize
: (mClipDistance ? mMaxClipDistanceIndex + 1 : 0));
unsigned int enabledCullDistances =
(mCullDistanceSize > 0 ? mCullDistanceSize
: (mCullDistance ? mMaxCullDistanceIndex + 1 : 0));
unsigned int combinedClipAndCullDistances =
(enabledClipDistances > 0 && enabledCullDistances > 0
? enabledClipDistances + enabledCullDistances
: 0);
if (combinedClipAndCullDistances > maxCombinedClipAndCullDistances)
{
const TIntermSymbol *greaterSymbol =
(enabledClipDistances >= enabledCullDistances ? mClipDistance : mCullDistance);
std::stringstream strstr = sh::InitializeStream<std::stringstream>();
strstr << "The sum of 'gl_ClipDistance' and 'gl_CullDistance' size is greater than "
"gl_MaxCombinedClipAndCullDistances ("
<< combinedClipAndCullDistances << " > " << maxCombinedClipAndCullDistances << ")";
error(*greaterSymbol, strstr.str().c_str(), diagnostics);
}
}
} // anonymous namespace
bool ValidateClipCullDistance(TIntermBlock *root,
TDiagnostics *diagnostics,
const unsigned int maxCombinedClipAndCullDistances)
{
ValidateClipCullDistanceTraverser varyingValidator;
root->traverse(&varyingValidator);
int numErrorsBefore = diagnostics->numErrors();
varyingValidator.validate(diagnostics, maxCombinedClipAndCullDistances);
return (diagnostics->numErrors() == numErrorsBefore);
}
} // namespace sh
|