blob: 2b5e63a4d7cb479ee199684a86733e4aa654776e [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
Olli Etuaho336b1472016-10-05 16:37:55 +010034 void traverse(TIntermFunctionDefinition *node)
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070035 {
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
Austin Kinross0998fe92015-12-11 11:31:38 -080069 bool visitLoop(Visit visit, TIntermLoop *loop) override
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070070 {
71 visitControlFlow(visit, loop);
72 return true;
73 }
74
Olli Etuaho57961272016-09-14 13:57:46 +030075 bool visitIfElse(Visit visit, TIntermIfElse *ifElse) override
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070076 {
Olli Etuaho57961272016-09-14 13:57:46 +030077 visitControlFlow(visit, ifElse);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070078 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 {
Olli Etuahobd674552016-10-06 13:28:42 +0100106 size_t calleeIndex = mDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700107 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
108
109 if ((*mMetadataList)[calleeIndex].mUsesGradient) {
110 onGradient();
111 }
112 }
113 else
114 {
Olli Etuahobd674552016-10-06 13:28:42 +0100115 TString name =
116 TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700117
118 if (name == "texture2D" ||
119 name == "texture2DProj" ||
120 name == "textureCube")
121 {
122 onGradient();
123 }
124 }
125 }
126 }
127
128 return true;
129 }
130
131 private:
132 MetadataList *mMetadataList;
133 ASTMetadataHLSL *mMetadata;
134 size_t mIndex;
135 const CallDAG &mDag;
136
137 // Contains a stack of the control flow nodes that are parents of the node being
138 // currently visited. It is used to mark control flows using a gradient.
139 std::vector<TIntermNode*> mParents;
140};
141
Corentin Wallez477b2432015-08-31 10:41:16 -0700142// Traverses the AST of a function definition to compute the the discontinuous loops
143// and the if statements containing gradient loops. It assumes that the gradient loops
144// (loops that contain a gradient) have already been computed and that it has already
145// traversed the current function's callees.
146class PullComputeDiscontinuousAndGradientLoops : public TIntermTraverser
Corentin Wallez50931452015-03-19 10:39:13 -0700147{
148 public:
Corentin Wallez477b2432015-08-31 10:41:16 -0700149 PullComputeDiscontinuousAndGradientLoops(MetadataList *metadataList,
150 size_t index,
151 const CallDAG &dag)
Corentin Wallez50931452015-03-19 10:39:13 -0700152 : TIntermTraverser(true, false, true),
153 mMetadataList(metadataList),
154 mMetadata(&(*metadataList)[index]),
155 mIndex(index),
156 mDag(dag)
157 {
158 }
159
Olli Etuaho336b1472016-10-05 16:37:55 +0100160 void traverse(TIntermFunctionDefinition *node)
Corentin Wallez50931452015-03-19 10:39:13 -0700161 {
162 node->traverse(this);
Olli Etuaho3873cd02015-04-10 15:00:55 +0300163 ASSERT(mLoopsAndSwitches.empty());
Corentin Wallez50931452015-03-19 10:39:13 -0700164 ASSERT(mIfs.empty());
165 }
166
Corentin Wallez477b2432015-08-31 10:41:16 -0700167 // Called when traversing a gradient loop or a call to a function with a
168 // gradient loop in its call graph.
169 void onGradientLoop()
Corentin Wallez50931452015-03-19 10:39:13 -0700170 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700171 mMetadata->mHasGradientLoopInCallGraph = true;
Corentin Wallez50931452015-03-19 10:39:13 -0700172 // Mark the latest if as using a discontinuous loop.
173 if (!mIfs.empty())
174 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700175 mMetadata->mIfsContainingGradientLoop.insert(mIfs.back());
Corentin Wallez50931452015-03-19 10:39:13 -0700176 }
177 }
178
Olli Etuaho3873cd02015-04-10 15:00:55 +0300179 bool visitLoop(Visit visit, TIntermLoop *loop) override
Corentin Wallez50931452015-03-19 10:39:13 -0700180 {
181 if (visit == PreVisit)
182 {
Olli Etuaho3873cd02015-04-10 15:00:55 +0300183 mLoopsAndSwitches.push_back(loop);
Corentin Wallez477b2432015-08-31 10:41:16 -0700184
185 if (mMetadata->hasGradientInCallGraph(loop))
186 {
187 onGradientLoop();
188 }
Corentin Wallez50931452015-03-19 10:39:13 -0700189 }
190 else if (visit == PostVisit)
191 {
Olli Etuaho3873cd02015-04-10 15:00:55 +0300192 ASSERT(mLoopsAndSwitches.back() == loop);
193 mLoopsAndSwitches.pop_back();
Corentin Wallez50931452015-03-19 10:39:13 -0700194 }
195
196 return true;
197 }
198
Olli Etuaho57961272016-09-14 13:57:46 +0300199 bool visitIfElse(Visit visit, TIntermIfElse *node) override
Corentin Wallez50931452015-03-19 10:39:13 -0700200 {
201 if (visit == PreVisit)
202 {
203 mIfs.push_back(node);
204 }
205 else if (visit == PostVisit)
206 {
207 ASSERT(mIfs.back() == node);
208 mIfs.pop_back();
209 // An if using a discontinuous loop means its parents ifs are also discontinuous.
Corentin Wallez477b2432015-08-31 10:41:16 -0700210 if (mMetadata->mIfsContainingGradientLoop.count(node) > 0 && !mIfs.empty())
Corentin Wallez50931452015-03-19 10:39:13 -0700211 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700212 mMetadata->mIfsContainingGradientLoop.insert(mIfs.back());
Corentin Wallez50931452015-03-19 10:39:13 -0700213 }
214 }
215
216 return true;
217 }
218
Olli Etuaho3873cd02015-04-10 15:00:55 +0300219 bool visitBranch(Visit visit, TIntermBranch *node) override
Corentin Wallez50931452015-03-19 10:39:13 -0700220 {
221 if (visit == PreVisit)
222 {
223 switch (node->getFlowOp())
224 {
Corentin Wallez50931452015-03-19 10:39:13 -0700225 case EOpBreak:
Olli Etuaho3873cd02015-04-10 15:00:55 +0300226 {
227 ASSERT(!mLoopsAndSwitches.empty());
228 TIntermLoop *loop = mLoopsAndSwitches.back()->getAsLoopNode();
229 if (loop != nullptr)
230 {
231 mMetadata->mDiscontinuousLoops.insert(loop);
Olli Etuaho3873cd02015-04-10 15:00:55 +0300232 }
233 }
234 break;
Corentin Wallez50931452015-03-19 10:39:13 -0700235 case EOpContinue:
Olli Etuaho3873cd02015-04-10 15:00:55 +0300236 {
237 ASSERT(!mLoopsAndSwitches.empty());
238 TIntermLoop *loop = nullptr;
239 size_t i = mLoopsAndSwitches.size();
240 while (loop == nullptr && i > 0)
241 {
242 --i;
243 loop = mLoopsAndSwitches.at(i)->getAsLoopNode();
244 }
245 ASSERT(loop != nullptr);
246 mMetadata->mDiscontinuousLoops.insert(loop);
Olli Etuaho3873cd02015-04-10 15:00:55 +0300247 }
Corentin Wallez50931452015-03-19 10:39:13 -0700248 break;
Corentin Walleza1884f22015-04-29 10:15:16 -0700249 case EOpKill:
Corentin Wallez50931452015-03-19 10:39:13 -0700250 case EOpReturn:
Corentin Walleza1884f22015-04-29 10:15:16 -0700251 // A return or discard jumps out of all the enclosing loops
Olli Etuaho3873cd02015-04-10 15:00:55 +0300252 if (!mLoopsAndSwitches.empty())
Corentin Wallez50931452015-03-19 10:39:13 -0700253 {
Cooper Partin4f488492015-08-07 16:05:25 -0700254 for (TIntermNode *intermNode : mLoopsAndSwitches)
Corentin Wallez50931452015-03-19 10:39:13 -0700255 {
Cooper Partin4f488492015-08-07 16:05:25 -0700256 TIntermLoop *loop = intermNode->getAsLoopNode();
Olli Etuaho3873cd02015-04-10 15:00:55 +0300257 if (loop)
258 {
259 mMetadata->mDiscontinuousLoops.insert(loop);
260 }
Corentin Wallez50931452015-03-19 10:39:13 -0700261 }
Corentin Wallez50931452015-03-19 10:39:13 -0700262 }
263 break;
264 default:
265 UNREACHABLE();
266 }
267 }
268
269 return true;
270 }
271
272 bool visitAggregate(Visit visit, TIntermAggregate *node) override
273 {
274 if (visit == PreVisit && node->getOp() == EOpFunctionCall)
275 {
276 if (node->isUserDefined())
277 {
Olli Etuahobd674552016-10-06 13:28:42 +0100278 size_t calleeIndex = mDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez50931452015-03-19 10:39:13 -0700279 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
280
Corentin Wallez477b2432015-08-31 10:41:16 -0700281 if ((*mMetadataList)[calleeIndex].mHasGradientLoopInCallGraph)
Corentin Wallez50931452015-03-19 10:39:13 -0700282 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700283 onGradientLoop();
Corentin Wallez50931452015-03-19 10:39:13 -0700284 }
285 }
286 }
287
288 return true;
289 }
290
Olli Etuaho3873cd02015-04-10 15:00:55 +0300291 bool visitSwitch(Visit visit, TIntermSwitch *node) override
292 {
293 if (visit == PreVisit)
294 {
295 mLoopsAndSwitches.push_back(node);
296 }
297 else if (visit == PostVisit)
298 {
299 ASSERT(mLoopsAndSwitches.back() == node);
300 mLoopsAndSwitches.pop_back();
301 }
302 return true;
303 }
304
Corentin Wallez50931452015-03-19 10:39:13 -0700305 private:
306 MetadataList *mMetadataList;
307 ASTMetadataHLSL *mMetadata;
308 size_t mIndex;
309 const CallDAG &mDag;
310
Olli Etuaho3873cd02015-04-10 15:00:55 +0300311 std::vector<TIntermNode*> mLoopsAndSwitches;
Olli Etuaho57961272016-09-14 13:57:46 +0300312 std::vector<TIntermIfElse *> mIfs;
Corentin Wallez50931452015-03-19 10:39:13 -0700313};
314
315// Tags all the functions called in a discontinuous loop
316class PushDiscontinuousLoops : public TIntermTraverser
317{
318 public:
319 PushDiscontinuousLoops(MetadataList *metadataList, size_t index, const CallDAG &dag)
320 : TIntermTraverser(true, true, true),
321 mMetadataList(metadataList),
322 mMetadata(&(*metadataList)[index]),
323 mIndex(index),
324 mDag(dag),
325 mNestedDiscont(mMetadata->mCalledInDiscontinuousLoop ? 1 : 0)
326 {
327 }
328
Olli Etuaho336b1472016-10-05 16:37:55 +0100329 void traverse(TIntermFunctionDefinition *node)
Corentin Wallez50931452015-03-19 10:39:13 -0700330 {
331 node->traverse(this);
332 ASSERT(mNestedDiscont == (mMetadata->mCalledInDiscontinuousLoop ? 1 : 0));
333 }
334
Austin Kinross0998fe92015-12-11 11:31:38 -0800335 bool visitLoop(Visit visit, TIntermLoop *loop) override
Corentin Wallez50931452015-03-19 10:39:13 -0700336 {
337 bool isDiscontinuous = mMetadata->mDiscontinuousLoops.count(loop) > 0;
338
339 if (visit == PreVisit && isDiscontinuous)
340 {
341 mNestedDiscont++;
342 }
343 else if (visit == PostVisit && isDiscontinuous)
344 {
345 mNestedDiscont--;
346 }
347
348 return true;
349 }
350
351 bool visitAggregate(Visit visit, TIntermAggregate *node) override
352 {
353 switch (node->getOp())
354 {
355 case EOpFunctionCall:
356 if (visit == PreVisit && node->isUserDefined() && mNestedDiscont > 0)
357 {
Olli Etuahobd674552016-10-06 13:28:42 +0100358 size_t calleeIndex = mDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez50931452015-03-19 10:39:13 -0700359 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
360
361 (*mMetadataList)[calleeIndex].mCalledInDiscontinuousLoop = true;
362 }
363 break;
364 default:
365 break;
366 }
367 return true;
368 }
369
370 private:
371 MetadataList *mMetadataList;
372 ASTMetadataHLSL *mMetadata;
373 size_t mIndex;
374 const CallDAG &mDag;
375
376 int mNestedDiscont;
377};
378
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700379}
380
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700381bool ASTMetadataHLSL::hasGradientInCallGraph(TIntermLoop *node)
382{
383 return mControlFlowsContainingGradient.count(node) > 0;
384}
385
Olli Etuaho57961272016-09-14 13:57:46 +0300386bool ASTMetadataHLSL::hasGradientLoop(TIntermIfElse *node)
Corentin Wallez50931452015-03-19 10:39:13 -0700387{
Corentin Wallez477b2432015-08-31 10:41:16 -0700388 return mIfsContainingGradientLoop.count(node) > 0;
Corentin Wallez50931452015-03-19 10:39:13 -0700389}
390
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700391MetadataList CreateASTMetadataHLSL(TIntermNode *root, const CallDAG &callDag)
392{
393 MetadataList metadataList(callDag.size());
394
395 // Compute all the information related to when gradient operations are used.
396 // We want to know for each function and control flow operation if they have
397 // a gradient operation in their call graph (shortened to "using a gradient"
398 // in the rest of the file).
399 //
400 // This computation is logically split in three steps:
401 // 1 - For each function compute if it uses a gradient in its body, ignoring
402 // calls to other user-defined functions.
403 // 2 - For each function determine if it uses a gradient in its call graph,
404 // using the result of step 1 and the CallDAG to know its callees.
405 // 3 - For each control flow statement of each function, check if it uses a
406 // gradient in the function's body, or if it calls a user-defined function that
407 // uses a gradient.
408 //
409 // We take advantage of the call graph being a DAG and instead compute 1, 2 and 3
410 // for leaves first, then going down the tree. This is correct because 1 doesn't
411 // depend on other functions, and 2 and 3 depend only on callees.
412 for (size_t i = 0; i < callDag.size(); i++)
413 {
414 PullGradient pull(&metadataList, i, callDag);
415 pull.traverse(callDag.getRecordFromIndex(i).node);
416 }
417
Corentin Wallez50931452015-03-19 10:39:13 -0700418 // Compute which loops are discontinuous and which function are called in
419 // these loops. The same way computing gradient usage is a "pull" process,
420 // computing "bing used in a discont. loop" is a push process. However we also
421 // need to know what ifs have a discontinuous loop inside so we do the same type
422 // of callgraph analysis as for the gradient.
423
424 // First compute which loops are discontinuous (no specific order) and pull
Corentin Wallez477b2432015-08-31 10:41:16 -0700425 // the ifs and functions using a gradient loop.
Corentin Wallez50931452015-03-19 10:39:13 -0700426 for (size_t i = 0; i < callDag.size(); i++)
427 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700428 PullComputeDiscontinuousAndGradientLoops pull(&metadataList, i, callDag);
Corentin Wallez50931452015-03-19 10:39:13 -0700429 pull.traverse(callDag.getRecordFromIndex(i).node);
430 }
431
432 // Then push the information to callees, either from the a local discontinuous
433 // loop or from the caller being called in a discontinuous loop already
434 for (size_t i = callDag.size(); i-- > 0;)
435 {
436 PushDiscontinuousLoops push(&metadataList, i, callDag);
437 push.traverse(callDag.getRecordFromIndex(i).node);
438 }
439
440 // We create "Lod0" version of functions with the gradient operations replaced
441 // by non-gradient operations so that the D3D compiler is happier with discont
442 // loops.
443 for (auto &metadata : metadataList)
444 {
445 metadata.mNeedsLod0 = metadata.mCalledInDiscontinuousLoop && metadata.mUsesGradient;
446 }
447
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700448 return metadataList;
449}