blob: 055408daa68d6d272c83f0cfbf242680240d6809 [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"
Olli Etuahocccf2b02017-07-05 14:50:54 +030012#include "compiler/translator/IntermTraverse.h"
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070013#include "compiler/translator/SymbolTable.h"
14
Jamie Madill45bcc782016-11-07 13:58:48 -050015namespace sh
16{
17
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070018namespace
19{
20
21// Class used to traverse the AST of a function definition, checking if the
22// function uses a gradient, and writing the set of control flow using gradients.
23// It assumes that the analysis has already been made for the function's
24// callees.
25class PullGradient : public TIntermTraverser
26{
27 public:
28 PullGradient(MetadataList *metadataList, size_t index, const CallDAG &dag)
29 : TIntermTraverser(true, false, true),
30 mMetadataList(metadataList),
31 mMetadata(&(*metadataList)[index]),
32 mIndex(index),
33 mDag(dag)
34 {
35 ASSERT(index < metadataList->size());
Corentin Wallezc16678a2017-02-22 15:24:55 -080036
37 // ESSL 100 builtin gradient functions
Olli Etuahofbb1c792018-01-19 16:26:59 +020038 mGradientBuiltinFunctions.insert(ImmutableString("texture2D"));
39 mGradientBuiltinFunctions.insert(ImmutableString("texture2DProj"));
40 mGradientBuiltinFunctions.insert(ImmutableString("textureCube"));
Corentin Wallezc16678a2017-02-22 15:24:55 -080041
42 // ESSL 300 builtin gradient functions
Olli Etuahofbb1c792018-01-19 16:26:59 +020043 mGradientBuiltinFunctions.insert(ImmutableString("texture"));
44 mGradientBuiltinFunctions.insert(ImmutableString("textureProj"));
45 mGradientBuiltinFunctions.insert(ImmutableString("textureOffset"));
46 mGradientBuiltinFunctions.insert(ImmutableString("textureProjOffset"));
Corentin Wallezc16678a2017-02-22 15:24:55 -080047
48 // ESSL 310 doesn't add builtin gradient functions
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070049 }
50
Olli Etuaho336b1472016-10-05 16:37:55 +010051 void traverse(TIntermFunctionDefinition *node)
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070052 {
53 node->traverse(this);
54 ASSERT(mParents.empty());
55 }
56
57 // Called when a gradient operation or a call to a function using a gradient is found.
58 void onGradient()
59 {
60 mMetadata->mUsesGradient = true;
61 // Mark the latest control flow as using a gradient.
62 if (!mParents.empty())
63 {
64 mMetadata->mControlFlowsContainingGradient.insert(mParents.back());
65 }
66 }
67
68 void visitControlFlow(Visit visit, TIntermNode *node)
69 {
70 if (visit == PreVisit)
71 {
72 mParents.push_back(node);
73 }
74 else if (visit == PostVisit)
75 {
76 ASSERT(mParents.back() == node);
77 mParents.pop_back();
78 // A control flow's using a gradient means its parents are too.
Jamie Madilld7b1ab52016-12-12 14:42:19 -050079 if (mMetadata->mControlFlowsContainingGradient.count(node) > 0 && !mParents.empty())
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070080 {
81 mMetadata->mControlFlowsContainingGradient.insert(mParents.back());
82 }
83 }
84 }
85
Austin Kinross0998fe92015-12-11 11:31:38 -080086 bool visitLoop(Visit visit, TIntermLoop *loop) override
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070087 {
88 visitControlFlow(visit, loop);
89 return true;
90 }
91
Olli Etuaho57961272016-09-14 13:57:46 +030092 bool visitIfElse(Visit visit, TIntermIfElse *ifElse) override
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070093 {
Olli Etuaho57961272016-09-14 13:57:46 +030094 visitControlFlow(visit, ifElse);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -070095 return true;
96 }
97
98 bool visitUnary(Visit visit, TIntermUnary *node) override
99 {
100 if (visit == PreVisit)
101 {
102 switch (node->getOp())
103 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500104 case EOpDFdx:
105 case EOpDFdy:
Corentin Wallezc16678a2017-02-22 15:24:55 -0800106 case EOpFwidth:
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500107 onGradient();
108 default:
109 break;
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700110 }
111 }
112
113 return true;
114 }
115
116 bool visitAggregate(Visit visit, TIntermAggregate *node) override
117 {
118 if (visit == PreVisit)
119 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800120 if (node->getOp() == EOpCallFunctionInAST)
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700121 {
Olli Etuaho1bb85282017-12-14 13:39:53 +0200122 size_t calleeIndex = mDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800123 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700124
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800125 if ((*mMetadataList)[calleeIndex].mUsesGradient)
126 {
127 onGradient();
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700128 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800129 }
130 else if (node->getOp() == EOpCallBuiltInFunction)
131 {
Olli Etuahobed35d72017-12-20 16:36:26 +0200132 if (mGradientBuiltinFunctions.find(node->getFunction()->name()) !=
Olli Etuahoec9232b2017-03-27 17:01:37 +0300133 mGradientBuiltinFunctions.end())
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800134 {
135 onGradient();
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700136 }
137 }
138 }
139
140 return true;
141 }
142
143 private:
144 MetadataList *mMetadataList;
145 ASTMetadataHLSL *mMetadata;
146 size_t mIndex;
147 const CallDAG &mDag;
148
149 // Contains a stack of the control flow nodes that are parents of the node being
150 // currently visited. It is used to mark control flows using a gradient.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500151 std::vector<TIntermNode *> mParents;
Corentin Wallezc16678a2017-02-22 15:24:55 -0800152
153 // A list of builtin functions that use gradients
Olli Etuahofbb1c792018-01-19 16:26:59 +0200154 std::set<ImmutableString> mGradientBuiltinFunctions;
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700155};
156
Corentin Wallez477b2432015-08-31 10:41:16 -0700157// Traverses the AST of a function definition to compute the the discontinuous loops
158// and the if statements containing gradient loops. It assumes that the gradient loops
159// (loops that contain a gradient) have already been computed and that it has already
160// traversed the current function's callees.
161class PullComputeDiscontinuousAndGradientLoops : public TIntermTraverser
Corentin Wallez50931452015-03-19 10:39:13 -0700162{
163 public:
Corentin Wallez477b2432015-08-31 10:41:16 -0700164 PullComputeDiscontinuousAndGradientLoops(MetadataList *metadataList,
165 size_t index,
166 const CallDAG &dag)
Corentin Wallez50931452015-03-19 10:39:13 -0700167 : TIntermTraverser(true, false, true),
168 mMetadataList(metadataList),
169 mMetadata(&(*metadataList)[index]),
170 mIndex(index),
171 mDag(dag)
172 {
173 }
174
Olli Etuaho336b1472016-10-05 16:37:55 +0100175 void traverse(TIntermFunctionDefinition *node)
Corentin Wallez50931452015-03-19 10:39:13 -0700176 {
177 node->traverse(this);
Olli Etuaho3873cd02015-04-10 15:00:55 +0300178 ASSERT(mLoopsAndSwitches.empty());
Corentin Wallez50931452015-03-19 10:39:13 -0700179 ASSERT(mIfs.empty());
180 }
181
Corentin Wallez477b2432015-08-31 10:41:16 -0700182 // Called when traversing a gradient loop or a call to a function with a
183 // gradient loop in its call graph.
184 void onGradientLoop()
Corentin Wallez50931452015-03-19 10:39:13 -0700185 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700186 mMetadata->mHasGradientLoopInCallGraph = true;
Corentin Wallez50931452015-03-19 10:39:13 -0700187 // Mark the latest if as using a discontinuous loop.
188 if (!mIfs.empty())
189 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700190 mMetadata->mIfsContainingGradientLoop.insert(mIfs.back());
Corentin Wallez50931452015-03-19 10:39:13 -0700191 }
192 }
193
Olli Etuaho3873cd02015-04-10 15:00:55 +0300194 bool visitLoop(Visit visit, TIntermLoop *loop) override
Corentin Wallez50931452015-03-19 10:39:13 -0700195 {
196 if (visit == PreVisit)
197 {
Olli Etuaho3873cd02015-04-10 15:00:55 +0300198 mLoopsAndSwitches.push_back(loop);
Corentin Wallez477b2432015-08-31 10:41:16 -0700199
200 if (mMetadata->hasGradientInCallGraph(loop))
201 {
202 onGradientLoop();
203 }
Corentin Wallez50931452015-03-19 10:39:13 -0700204 }
205 else if (visit == PostVisit)
206 {
Olli Etuaho3873cd02015-04-10 15:00:55 +0300207 ASSERT(mLoopsAndSwitches.back() == loop);
208 mLoopsAndSwitches.pop_back();
Corentin Wallez50931452015-03-19 10:39:13 -0700209 }
210
211 return true;
212 }
213
Olli Etuaho57961272016-09-14 13:57:46 +0300214 bool visitIfElse(Visit visit, TIntermIfElse *node) override
Corentin Wallez50931452015-03-19 10:39:13 -0700215 {
216 if (visit == PreVisit)
217 {
218 mIfs.push_back(node);
219 }
220 else if (visit == PostVisit)
221 {
222 ASSERT(mIfs.back() == node);
223 mIfs.pop_back();
224 // An if using a discontinuous loop means its parents ifs are also discontinuous.
Corentin Wallez477b2432015-08-31 10:41:16 -0700225 if (mMetadata->mIfsContainingGradientLoop.count(node) > 0 && !mIfs.empty())
Corentin Wallez50931452015-03-19 10:39:13 -0700226 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700227 mMetadata->mIfsContainingGradientLoop.insert(mIfs.back());
Corentin Wallez50931452015-03-19 10:39:13 -0700228 }
229 }
230
231 return true;
232 }
233
Olli Etuaho3873cd02015-04-10 15:00:55 +0300234 bool visitBranch(Visit visit, TIntermBranch *node) override
Corentin Wallez50931452015-03-19 10:39:13 -0700235 {
236 if (visit == PreVisit)
237 {
238 switch (node->getFlowOp())
239 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500240 case EOpBreak:
Olli Etuaho3873cd02015-04-10 15:00:55 +0300241 {
242 ASSERT(!mLoopsAndSwitches.empty());
243 TIntermLoop *loop = mLoopsAndSwitches.back()->getAsLoopNode();
244 if (loop != nullptr)
245 {
246 mMetadata->mDiscontinuousLoops.insert(loop);
Olli Etuaho3873cd02015-04-10 15:00:55 +0300247 }
248 }
249 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500250 case EOpContinue:
Olli Etuaho3873cd02015-04-10 15:00:55 +0300251 {
252 ASSERT(!mLoopsAndSwitches.empty());
253 TIntermLoop *loop = nullptr;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500254 size_t i = mLoopsAndSwitches.size();
Olli Etuaho3873cd02015-04-10 15:00:55 +0300255 while (loop == nullptr && i > 0)
256 {
257 --i;
258 loop = mLoopsAndSwitches.at(i)->getAsLoopNode();
259 }
260 ASSERT(loop != nullptr);
261 mMetadata->mDiscontinuousLoops.insert(loop);
Olli Etuaho3873cd02015-04-10 15:00:55 +0300262 }
Corentin Wallez50931452015-03-19 10:39:13 -0700263 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500264 case EOpKill:
265 case EOpReturn:
266 // A return or discard jumps out of all the enclosing loops
267 if (!mLoopsAndSwitches.empty())
Corentin Wallez50931452015-03-19 10:39:13 -0700268 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500269 for (TIntermNode *intermNode : mLoopsAndSwitches)
Olli Etuaho3873cd02015-04-10 15:00:55 +0300270 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500271 TIntermLoop *loop = intermNode->getAsLoopNode();
272 if (loop)
273 {
274 mMetadata->mDiscontinuousLoops.insert(loop);
275 }
Olli Etuaho3873cd02015-04-10 15:00:55 +0300276 }
Corentin Wallez50931452015-03-19 10:39:13 -0700277 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500278 break;
279 default:
280 UNREACHABLE();
Corentin Wallez50931452015-03-19 10:39:13 -0700281 }
282 }
283
284 return true;
285 }
286
287 bool visitAggregate(Visit visit, TIntermAggregate *node) override
288 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800289 if (visit == PreVisit && node->getOp() == EOpCallFunctionInAST)
Corentin Wallez50931452015-03-19 10:39:13 -0700290 {
Olli Etuaho1bb85282017-12-14 13:39:53 +0200291 size_t calleeIndex = mDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800292 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
Corentin Wallez50931452015-03-19 10:39:13 -0700293
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800294 if ((*mMetadataList)[calleeIndex].mHasGradientLoopInCallGraph)
295 {
296 onGradientLoop();
Corentin Wallez50931452015-03-19 10:39:13 -0700297 }
298 }
299
300 return true;
301 }
302
Olli Etuaho3873cd02015-04-10 15:00:55 +0300303 bool visitSwitch(Visit visit, TIntermSwitch *node) override
304 {
305 if (visit == PreVisit)
306 {
307 mLoopsAndSwitches.push_back(node);
308 }
309 else if (visit == PostVisit)
310 {
311 ASSERT(mLoopsAndSwitches.back() == node);
312 mLoopsAndSwitches.pop_back();
313 }
314 return true;
315 }
316
Corentin Wallez50931452015-03-19 10:39:13 -0700317 private:
318 MetadataList *mMetadataList;
319 ASTMetadataHLSL *mMetadata;
320 size_t mIndex;
321 const CallDAG &mDag;
322
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500323 std::vector<TIntermNode *> mLoopsAndSwitches;
Olli Etuaho57961272016-09-14 13:57:46 +0300324 std::vector<TIntermIfElse *> mIfs;
Corentin Wallez50931452015-03-19 10:39:13 -0700325};
326
327// Tags all the functions called in a discontinuous loop
328class PushDiscontinuousLoops : public TIntermTraverser
329{
330 public:
331 PushDiscontinuousLoops(MetadataList *metadataList, size_t index, const CallDAG &dag)
332 : TIntermTraverser(true, true, true),
333 mMetadataList(metadataList),
334 mMetadata(&(*metadataList)[index]),
335 mIndex(index),
336 mDag(dag),
337 mNestedDiscont(mMetadata->mCalledInDiscontinuousLoop ? 1 : 0)
338 {
339 }
340
Olli Etuaho336b1472016-10-05 16:37:55 +0100341 void traverse(TIntermFunctionDefinition *node)
Corentin Wallez50931452015-03-19 10:39:13 -0700342 {
343 node->traverse(this);
344 ASSERT(mNestedDiscont == (mMetadata->mCalledInDiscontinuousLoop ? 1 : 0));
345 }
346
Austin Kinross0998fe92015-12-11 11:31:38 -0800347 bool visitLoop(Visit visit, TIntermLoop *loop) override
Corentin Wallez50931452015-03-19 10:39:13 -0700348 {
349 bool isDiscontinuous = mMetadata->mDiscontinuousLoops.count(loop) > 0;
350
351 if (visit == PreVisit && isDiscontinuous)
352 {
353 mNestedDiscont++;
354 }
355 else if (visit == PostVisit && isDiscontinuous)
356 {
357 mNestedDiscont--;
358 }
359
360 return true;
361 }
362
363 bool visitAggregate(Visit visit, TIntermAggregate *node) override
364 {
365 switch (node->getOp())
366 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -0800367 case EOpCallFunctionInAST:
368 if (visit == PreVisit && mNestedDiscont > 0)
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500369 {
Olli Etuaho1bb85282017-12-14 13:39:53 +0200370 size_t calleeIndex = mDag.findIndex(node->getFunction()->uniqueId());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500371 ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex);
Corentin Wallez50931452015-03-19 10:39:13 -0700372
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500373 (*mMetadataList)[calleeIndex].mCalledInDiscontinuousLoop = true;
374 }
375 break;
376 default:
377 break;
Corentin Wallez50931452015-03-19 10:39:13 -0700378 }
379 return true;
380 }
381
382 private:
383 MetadataList *mMetadataList;
384 ASTMetadataHLSL *mMetadata;
385 size_t mIndex;
386 const CallDAG &mDag;
387
388 int mNestedDiscont;
389};
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700390}
391
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700392bool ASTMetadataHLSL::hasGradientInCallGraph(TIntermLoop *node)
393{
394 return mControlFlowsContainingGradient.count(node) > 0;
395}
396
Olli Etuaho57961272016-09-14 13:57:46 +0300397bool ASTMetadataHLSL::hasGradientLoop(TIntermIfElse *node)
Corentin Wallez50931452015-03-19 10:39:13 -0700398{
Corentin Wallez477b2432015-08-31 10:41:16 -0700399 return mIfsContainingGradientLoop.count(node) > 0;
Corentin Wallez50931452015-03-19 10:39:13 -0700400}
401
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700402MetadataList CreateASTMetadataHLSL(TIntermNode *root, const CallDAG &callDag)
403{
404 MetadataList metadataList(callDag.size());
405
406 // Compute all the information related to when gradient operations are used.
407 // We want to know for each function and control flow operation if they have
408 // a gradient operation in their call graph (shortened to "using a gradient"
409 // in the rest of the file).
410 //
411 // This computation is logically split in three steps:
412 // 1 - For each function compute if it uses a gradient in its body, ignoring
413 // calls to other user-defined functions.
414 // 2 - For each function determine if it uses a gradient in its call graph,
415 // using the result of step 1 and the CallDAG to know its callees.
416 // 3 - For each control flow statement of each function, check if it uses a
417 // gradient in the function's body, or if it calls a user-defined function that
418 // uses a gradient.
419 //
420 // We take advantage of the call graph being a DAG and instead compute 1, 2 and 3
421 // for leaves first, then going down the tree. This is correct because 1 doesn't
422 // depend on other functions, and 2 and 3 depend only on callees.
423 for (size_t i = 0; i < callDag.size(); i++)
424 {
425 PullGradient pull(&metadataList, i, callDag);
426 pull.traverse(callDag.getRecordFromIndex(i).node);
427 }
428
Corentin Wallez50931452015-03-19 10:39:13 -0700429 // Compute which loops are discontinuous and which function are called in
430 // these loops. The same way computing gradient usage is a "pull" process,
431 // computing "bing used in a discont. loop" is a push process. However we also
432 // need to know what ifs have a discontinuous loop inside so we do the same type
433 // of callgraph analysis as for the gradient.
434
435 // First compute which loops are discontinuous (no specific order) and pull
Corentin Wallez477b2432015-08-31 10:41:16 -0700436 // the ifs and functions using a gradient loop.
Corentin Wallez50931452015-03-19 10:39:13 -0700437 for (size_t i = 0; i < callDag.size(); i++)
438 {
Corentin Wallez477b2432015-08-31 10:41:16 -0700439 PullComputeDiscontinuousAndGradientLoops pull(&metadataList, i, callDag);
Corentin Wallez50931452015-03-19 10:39:13 -0700440 pull.traverse(callDag.getRecordFromIndex(i).node);
441 }
442
443 // Then push the information to callees, either from the a local discontinuous
444 // loop or from the caller being called in a discontinuous loop already
445 for (size_t i = callDag.size(); i-- > 0;)
446 {
447 PushDiscontinuousLoops push(&metadataList, i, callDag);
448 push.traverse(callDag.getRecordFromIndex(i).node);
449 }
450
451 // We create "Lod0" version of functions with the gradient operations replaced
452 // by non-gradient operations so that the D3D compiler is happier with discont
453 // loops.
454 for (auto &metadata : metadataList)
455 {
456 metadata.mNeedsLod0 = metadata.mCalledInDiscontinuousLoop && metadata.mUsesGradient;
457 }
458
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700459 return metadataList;
460}
Jamie Madill45bcc782016-11-07 13:58:48 -0500461
462} // namespace sh