blob: 910cfdc0eb40e8d20000157f0fcd825b4420d0c7 [file] [log] [blame]
Corentin Wallezf4eab3b2015-03-18 12:55:45 -07001//
2// Copyright (c) 2002-2015 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
7// Analysis of the AST needed for HLSL generation
8
9#include "compiler/translator/ASTMetadataHLSL.h"
10
11#include "compiler/translator/CallDAG.h"
12#include "compiler/translator/SymbolTable.h"
13
14namespace
15{
16
17// Class used to traverse the AST of a function definition, checking if the
18// function uses a gradient, and writing the set of control flow using gradients.
19// It assumes that the analysis has already been made for the function's
20// callees.
21class PullGradient : public TIntermTraverser
22{
23 public:
24 PullGradient(MetadataList *metadataList, size_t index, const CallDAG &dag)
25 : TIntermTraverser(true, false, true),
26 mMetadataList(metadataList),
27 mMetadata(&(*metadataList)[index]),
28 mIndex(index),
29 mDag(dag)
30 {
31 ASSERT(index < metadataList->size());
32 }
33
34 void traverse(TIntermAggregate *node)
35 {
36 node->traverse(this);
37 ASSERT(mParents.empty());
38 }
39
40 // Called when a gradient operation or a call to a function using a gradient is found.
41 void onGradient()
42 {
43 mMetadata->mUsesGradient = true;
44 // Mark the latest control flow as using a gradient.
45 if (!mParents.empty())
46 {
47 mMetadata->mControlFlowsContainingGradient.insert(mParents.back());
48 }
49 }
50
51 void visitControlFlow(Visit visit, TIntermNode *node)
52 {
53 if (visit == PreVisit)
54 {
55 mParents.push_back(node);
56 }
57 else if (visit == PostVisit)
58 {
59 ASSERT(mParents.back() == node);
60 mParents.pop_back();
61 // A control flow's using a gradient means its parents are too.
62 if (mMetadata->mControlFlowsContainingGradient.count(node)> 0 && !mParents.empty())
63 {
64 mMetadata->mControlFlowsContainingGradient.insert(mParents.back());
65 }
66 }
67 }
68
69 bool visitLoop(Visit visit, TIntermLoop *loop)
70 {
71 visitControlFlow(visit, loop);
72 return true;
73 }
74
75 bool visitSelection(Visit visit, TIntermSelection *selection)
76 {
77 visitControlFlow(visit, selection);
78 return true;
79 }
80
81 bool visitUnary(Visit visit, TIntermUnary *node) override
82 {
83 if (visit == PreVisit)
84 {
85 switch (node->getOp())
86 {
87 case EOpDFdx:
88 case EOpDFdy:
89 onGradient();
90 default:
91 break;
92 }
93 }
94
95 return true;
96 }
97
98 bool visitAggregate(Visit visit, TIntermAggregate *node) override
99 {
100 if (visit == PreVisit)
101 {
102 if (node->getOp() == EOpFunctionCall)
103 {
104 if (node->isUserDefined())
105 {
106 size_t calleeIndex = mDag.findIndex(node);
107 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
108
109 if ((*mMetadataList)[calleeIndex].mUsesGradient) {
110 onGradient();
111 }
112 }
113 else
114 {
115 TString name = TFunction::unmangleName(node->getName());
116
117 if (name == "texture2D" ||
118 name == "texture2DProj" ||
119 name == "textureCube")
120 {
121 onGradient();
122 }
123 }
124 }
125 }
126
127 return true;
128 }
129
130 private:
131 MetadataList *mMetadataList;
132 ASTMetadataHLSL *mMetadata;
133 size_t mIndex;
134 const CallDAG &mDag;
135
136 // Contains a stack of the control flow nodes that are parents of the node being
137 // currently visited. It is used to mark control flows using a gradient.
138 std::vector<TIntermNode*> mParents;
139};
140
Corentin Wallez50931452015-03-19 10:39:13 -0700141// Traverses the AST of a function definition, assuming it has already been used to
142// traverse the callees of that function; computes the discontinuous loops and the if
143// statements that contain a discontinuous loop in their call graph.
144class PullComputeDiscontinuousLoops : public TIntermTraverser
145{
146 public:
147 PullComputeDiscontinuousLoops(MetadataList *metadataList, size_t index, const CallDAG &dag)
148 : TIntermTraverser(true, false, true),
149 mMetadataList(metadataList),
150 mMetadata(&(*metadataList)[index]),
151 mIndex(index),
152 mDag(dag)
153 {
154 }
155
156 void traverse(TIntermAggregate *node)
157 {
158 node->traverse(this);
159 ASSERT(mLoops.empty());
160 ASSERT(mIfs.empty());
161 }
162
163 // Called when a discontinuous loop or a call to a function with a discontinuous loop
164 // in its call graph is found.
165 void onDiscontinuousLoop()
166 {
167 mMetadata->mHasDiscontinuousLoopInCallGraph = true;
168 // Mark the latest if as using a discontinuous loop.
169 if (!mIfs.empty())
170 {
171 mMetadata->mIfsContainingDiscontinuousLoop.insert(mIfs.back());
172 }
173 }
174
175 bool visitLoop(Visit visit, TIntermLoop *loop)
176 {
177 if (visit == PreVisit)
178 {
179 mLoops.push_back(loop);
180 }
181 else if (visit == PostVisit)
182 {
183 ASSERT(mLoops.back() == loop);
184 mLoops.pop_back();
185 }
186
187 return true;
188 }
189
190 bool visitSelection(Visit visit, TIntermSelection *node)
191 {
192 if (visit == PreVisit)
193 {
194 mIfs.push_back(node);
195 }
196 else if (visit == PostVisit)
197 {
198 ASSERT(mIfs.back() == node);
199 mIfs.pop_back();
200 // An if using a discontinuous loop means its parents ifs are also discontinuous.
201 if (mMetadata->mIfsContainingDiscontinuousLoop.count(node) > 0 && !mIfs.empty())
202 {
203 mMetadata->mIfsContainingDiscontinuousLoop.insert(mIfs.back());
204 }
205 }
206
207 return true;
208 }
209
210 bool visitBranch(Visit visit, TIntermBranch *node)
211 {
212 if (visit == PreVisit)
213 {
214 switch (node->getFlowOp())
215 {
216 case EOpKill:
217 break;
218 case EOpBreak:
219 case EOpContinue:
220 ASSERT(!mLoops.empty());
221 mMetadata->mDiscontinuousLoops.insert(mLoops.back());
222 onDiscontinuousLoop();
223 break;
224 case EOpReturn:
225 // A return jumps out of all the enclosing loops
226 if (!mLoops.empty())
227 {
228 for (TIntermLoop* loop : mLoops)
229 {
230 mMetadata->mDiscontinuousLoops.insert(loop);
231 }
232 onDiscontinuousLoop();
233 }
234 break;
235 default:
236 UNREACHABLE();
237 }
238 }
239
240 return true;
241 }
242
243 bool visitAggregate(Visit visit, TIntermAggregate *node) override
244 {
245 if (visit == PreVisit && node->getOp() == EOpFunctionCall)
246 {
247 if (node->isUserDefined())
248 {
249 size_t calleeIndex = mDag.findIndex(node);
250 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
251
252 if ((*mMetadataList)[calleeIndex].mHasDiscontinuousLoopInCallGraph)
253 {
254 onDiscontinuousLoop();
255 }
256 }
257 }
258
259 return true;
260 }
261
262 private:
263 MetadataList *mMetadataList;
264 ASTMetadataHLSL *mMetadata;
265 size_t mIndex;
266 const CallDAG &mDag;
267
268 std::vector<TIntermLoop*> mLoops;
269 std::vector<TIntermSelection*> mIfs;
270};
271
272// Tags all the functions called in a discontinuous loop
273class PushDiscontinuousLoops : public TIntermTraverser
274{
275 public:
276 PushDiscontinuousLoops(MetadataList *metadataList, size_t index, const CallDAG &dag)
277 : TIntermTraverser(true, true, true),
278 mMetadataList(metadataList),
279 mMetadata(&(*metadataList)[index]),
280 mIndex(index),
281 mDag(dag),
282 mNestedDiscont(mMetadata->mCalledInDiscontinuousLoop ? 1 : 0)
283 {
284 }
285
286 void traverse(TIntermAggregate *node)
287 {
288 node->traverse(this);
289 ASSERT(mNestedDiscont == (mMetadata->mCalledInDiscontinuousLoop ? 1 : 0));
290 }
291
292 bool visitLoop(Visit visit, TIntermLoop *loop)
293 {
294 bool isDiscontinuous = mMetadata->mDiscontinuousLoops.count(loop) > 0;
295
296 if (visit == PreVisit && isDiscontinuous)
297 {
298 mNestedDiscont++;
299 }
300 else if (visit == PostVisit && isDiscontinuous)
301 {
302 mNestedDiscont--;
303 }
304
305 return true;
306 }
307
308 bool visitAggregate(Visit visit, TIntermAggregate *node) override
309 {
310 switch (node->getOp())
311 {
312 case EOpFunctionCall:
313 if (visit == PreVisit && node->isUserDefined() && mNestedDiscont > 0)
314 {
315 size_t calleeIndex = mDag.findIndex(node);
316 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
317
318 (*mMetadataList)[calleeIndex].mCalledInDiscontinuousLoop = true;
319 }
320 break;
321 default:
322 break;
323 }
324 return true;
325 }
326
327 private:
328 MetadataList *mMetadataList;
329 ASTMetadataHLSL *mMetadata;
330 size_t mIndex;
331 const CallDAG &mDag;
332
333 int mNestedDiscont;
334};
335
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700336}
337
338bool ASTMetadataHLSL::hasGradientInCallGraph(TIntermSelection *node)
339{
340 return mControlFlowsContainingGradient.count(node) > 0;
341}
342
343bool ASTMetadataHLSL::hasGradientInCallGraph(TIntermLoop *node)
344{
345 return mControlFlowsContainingGradient.count(node) > 0;
346}
347
Corentin Wallez50931452015-03-19 10:39:13 -0700348bool ASTMetadataHLSL::hasDiscontinuousLoop(TIntermSelection *node)
349{
350 return mIfsContainingDiscontinuousLoop.count(node) > 0;
351}
352
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700353MetadataList CreateASTMetadataHLSL(TIntermNode *root, const CallDAG &callDag)
354{
355 MetadataList metadataList(callDag.size());
356
357 // Compute all the information related to when gradient operations are used.
358 // We want to know for each function and control flow operation if they have
359 // a gradient operation in their call graph (shortened to "using a gradient"
360 // in the rest of the file).
361 //
362 // This computation is logically split in three steps:
363 // 1 - For each function compute if it uses a gradient in its body, ignoring
364 // calls to other user-defined functions.
365 // 2 - For each function determine if it uses a gradient in its call graph,
366 // using the result of step 1 and the CallDAG to know its callees.
367 // 3 - For each control flow statement of each function, check if it uses a
368 // gradient in the function's body, or if it calls a user-defined function that
369 // uses a gradient.
370 //
371 // We take advantage of the call graph being a DAG and instead compute 1, 2 and 3
372 // for leaves first, then going down the tree. This is correct because 1 doesn't
373 // depend on other functions, and 2 and 3 depend only on callees.
374 for (size_t i = 0; i < callDag.size(); i++)
375 {
376 PullGradient pull(&metadataList, i, callDag);
377 pull.traverse(callDag.getRecordFromIndex(i).node);
378 }
379
Corentin Wallez50931452015-03-19 10:39:13 -0700380 // Compute which loops are discontinuous and which function are called in
381 // these loops. The same way computing gradient usage is a "pull" process,
382 // computing "bing used in a discont. loop" is a push process. However we also
383 // need to know what ifs have a discontinuous loop inside so we do the same type
384 // of callgraph analysis as for the gradient.
385
386 // First compute which loops are discontinuous (no specific order) and pull
387 // the ifs and functions using a discontinuous loop.
388 for (size_t i = 0; i < callDag.size(); i++)
389 {
390 PullComputeDiscontinuousLoops pull(&metadataList, i, callDag);
391 pull.traverse(callDag.getRecordFromIndex(i).node);
392 }
393
394 // Then push the information to callees, either from the a local discontinuous
395 // loop or from the caller being called in a discontinuous loop already
396 for (size_t i = callDag.size(); i-- > 0;)
397 {
398 PushDiscontinuousLoops push(&metadataList, i, callDag);
399 push.traverse(callDag.getRecordFromIndex(i).node);
400 }
401
402 // We create "Lod0" version of functions with the gradient operations replaced
403 // by non-gradient operations so that the D3D compiler is happier with discont
404 // loops.
405 for (auto &metadata : metadataList)
406 {
407 metadata.mNeedsLod0 = metadata.mCalledInDiscontinuousLoop && metadata.mUsesGradient;
408 }
409
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700410 return metadataList;
411}