blob: cc21a0005446d00380e70a2764f498c0bbcdc363 [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);
Nico Weber44897142015-07-10 09:50:00 -0700108 UNUSED_ASSERTION_VARIABLE(mIndex);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700109
110 if ((*mMetadataList)[calleeIndex].mUsesGradient) {
111 onGradient();
112 }
113 }
114 else
115 {
116 TString name = TFunction::unmangleName(node->getName());
117
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
160 void traverse(TIntermAggregate *node)
161 {
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 Etuaho3873cd02015-04-10 15:00:55 +0300199 bool visitSelection(Visit visit, TIntermSelection *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 {
278 size_t calleeIndex = mDag.findIndex(node);
279 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
Nico Weber44897142015-07-10 09:50:00 -0700280 UNUSED_ASSERTION_VARIABLE(mIndex);
Corentin Wallez50931452015-03-19 10:39:13 -0700281
Corentin Wallez477b2432015-08-31 10:41:16 -0700282 if ((*mMetadataList)[calleeIndex].mHasGradientLoopInCallGraph)
Corentin Wallez50931452015-03-19 10:39:13 -0700283 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700284 onGradientLoop();
Corentin Wallez50931452015-03-19 10:39:13 -0700285 }
286 }
287 }
288
289 return true;
290 }
291
Olli Etuaho3873cd02015-04-10 15:00:55 +0300292 bool visitSwitch(Visit visit, TIntermSwitch *node) override
293 {
294 if (visit == PreVisit)
295 {
296 mLoopsAndSwitches.push_back(node);
297 }
298 else if (visit == PostVisit)
299 {
300 ASSERT(mLoopsAndSwitches.back() == node);
301 mLoopsAndSwitches.pop_back();
302 }
303 return true;
304 }
305
Corentin Wallez50931452015-03-19 10:39:13 -0700306 private:
307 MetadataList *mMetadataList;
308 ASTMetadataHLSL *mMetadata;
309 size_t mIndex;
310 const CallDAG &mDag;
311
Olli Etuaho3873cd02015-04-10 15:00:55 +0300312 std::vector<TIntermNode*> mLoopsAndSwitches;
Corentin Wallez50931452015-03-19 10:39:13 -0700313 std::vector<TIntermSelection*> mIfs;
314};
315
316// Tags all the functions called in a discontinuous loop
317class PushDiscontinuousLoops : public TIntermTraverser
318{
319 public:
320 PushDiscontinuousLoops(MetadataList *metadataList, size_t index, const CallDAG &dag)
321 : TIntermTraverser(true, true, true),
322 mMetadataList(metadataList),
323 mMetadata(&(*metadataList)[index]),
324 mIndex(index),
325 mDag(dag),
326 mNestedDiscont(mMetadata->mCalledInDiscontinuousLoop ? 1 : 0)
327 {
328 }
329
330 void traverse(TIntermAggregate *node)
331 {
332 node->traverse(this);
333 ASSERT(mNestedDiscont == (mMetadata->mCalledInDiscontinuousLoop ? 1 : 0));
334 }
335
336 bool visitLoop(Visit visit, TIntermLoop *loop)
337 {
338 bool isDiscontinuous = mMetadata->mDiscontinuousLoops.count(loop) > 0;
339
340 if (visit == PreVisit && isDiscontinuous)
341 {
342 mNestedDiscont++;
343 }
344 else if (visit == PostVisit && isDiscontinuous)
345 {
346 mNestedDiscont--;
347 }
348
349 return true;
350 }
351
352 bool visitAggregate(Visit visit, TIntermAggregate *node) override
353 {
354 switch (node->getOp())
355 {
356 case EOpFunctionCall:
357 if (visit == PreVisit && node->isUserDefined() && mNestedDiscont > 0)
358 {
359 size_t calleeIndex = mDag.findIndex(node);
360 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
Nico Weber44897142015-07-10 09:50:00 -0700361 UNUSED_ASSERTION_VARIABLE(mIndex);
Corentin Wallez50931452015-03-19 10:39:13 -0700362
363 (*mMetadataList)[calleeIndex].mCalledInDiscontinuousLoop = true;
364 }
365 break;
366 default:
367 break;
368 }
369 return true;
370 }
371
372 private:
373 MetadataList *mMetadataList;
374 ASTMetadataHLSL *mMetadata;
375 size_t mIndex;
376 const CallDAG &mDag;
377
378 int mNestedDiscont;
379};
380
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700381}
382
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700383bool ASTMetadataHLSL::hasGradientInCallGraph(TIntermLoop *node)
384{
385 return mControlFlowsContainingGradient.count(node) > 0;
386}
387
Corentin Wallez477b2432015-08-31 10:41:16 -0700388bool ASTMetadataHLSL::hasGradientLoop(TIntermSelection *node)
Corentin Wallez50931452015-03-19 10:39:13 -0700389{
Corentin Wallez477b2432015-08-31 10:41:16 -0700390 return mIfsContainingGradientLoop.count(node) > 0;
Corentin Wallez50931452015-03-19 10:39:13 -0700391}
392
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700393MetadataList CreateASTMetadataHLSL(TIntermNode *root, const CallDAG &callDag)
394{
395 MetadataList metadataList(callDag.size());
396
397 // Compute all the information related to when gradient operations are used.
398 // We want to know for each function and control flow operation if they have
399 // a gradient operation in their call graph (shortened to "using a gradient"
400 // in the rest of the file).
401 //
402 // This computation is logically split in three steps:
403 // 1 - For each function compute if it uses a gradient in its body, ignoring
404 // calls to other user-defined functions.
405 // 2 - For each function determine if it uses a gradient in its call graph,
406 // using the result of step 1 and the CallDAG to know its callees.
407 // 3 - For each control flow statement of each function, check if it uses a
408 // gradient in the function's body, or if it calls a user-defined function that
409 // uses a gradient.
410 //
411 // We take advantage of the call graph being a DAG and instead compute 1, 2 and 3
412 // for leaves first, then going down the tree. This is correct because 1 doesn't
413 // depend on other functions, and 2 and 3 depend only on callees.
414 for (size_t i = 0; i < callDag.size(); i++)
415 {
416 PullGradient pull(&metadataList, i, callDag);
417 pull.traverse(callDag.getRecordFromIndex(i).node);
418 }
419
Corentin Wallez50931452015-03-19 10:39:13 -0700420 // Compute which loops are discontinuous and which function are called in
421 // these loops. The same way computing gradient usage is a "pull" process,
422 // computing "bing used in a discont. loop" is a push process. However we also
423 // need to know what ifs have a discontinuous loop inside so we do the same type
424 // of callgraph analysis as for the gradient.
425
426 // First compute which loops are discontinuous (no specific order) and pull
Corentin Wallez477b2432015-08-31 10:41:16 -0700427 // the ifs and functions using a gradient loop.
Corentin Wallez50931452015-03-19 10:39:13 -0700428 for (size_t i = 0; i < callDag.size(); i++)
429 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700430 PullComputeDiscontinuousAndGradientLoops pull(&metadataList, i, callDag);
Corentin Wallez50931452015-03-19 10:39:13 -0700431 pull.traverse(callDag.getRecordFromIndex(i).node);
432 }
433
434 // Then push the information to callees, either from the a local discontinuous
435 // loop or from the caller being called in a discontinuous loop already
436 for (size_t i = callDag.size(); i-- > 0;)
437 {
438 PushDiscontinuousLoops push(&metadataList, i, callDag);
439 push.traverse(callDag.getRecordFromIndex(i).node);
440 }
441
442 // We create "Lod0" version of functions with the gradient operations replaced
443 // by non-gradient operations so that the D3D compiler is happier with discont
444 // loops.
445 for (auto &metadata : metadataList)
446 {
447 metadata.mNeedsLod0 = metadata.mCalledInDiscontinuousLoop && metadata.mUsesGradient;
448 }
449
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700450 return metadataList;
451}