Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 1 | // |
| 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 | |
| 14 | namespace |
| 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. |
| 21 | class 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 Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 34 | void traverse(TIntermFunctionDefinition *node) |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 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 | |
Austin Kinross | 0998fe9 | 2015-12-11 11:31:38 -0800 | [diff] [blame] | 69 | bool visitLoop(Visit visit, TIntermLoop *loop) override |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 70 | { |
| 71 | visitControlFlow(visit, loop); |
| 72 | return true; |
| 73 | } |
| 74 | |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 75 | bool visitIfElse(Visit visit, TIntermIfElse *ifElse) override |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 76 | { |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 77 | visitControlFlow(visit, ifElse); |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 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 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 106 | size_t calleeIndex = mDag.findIndex(node->getFunctionSymbolInfo()); |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 107 | ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex); |
| 108 | |
| 109 | if ((*mMetadataList)[calleeIndex].mUsesGradient) { |
| 110 | onGradient(); |
| 111 | } |
| 112 | } |
| 113 | else |
| 114 | { |
Olli Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 115 | TString name = |
| 116 | TFunction::unmangleName(node->getFunctionSymbolInfo()->getName()); |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 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 Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 142 | // 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. |
| 146 | class PullComputeDiscontinuousAndGradientLoops : public TIntermTraverser |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 147 | { |
| 148 | public: |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 149 | PullComputeDiscontinuousAndGradientLoops(MetadataList *metadataList, |
| 150 | size_t index, |
| 151 | const CallDAG &dag) |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 152 | : TIntermTraverser(true, false, true), |
| 153 | mMetadataList(metadataList), |
| 154 | mMetadata(&(*metadataList)[index]), |
| 155 | mIndex(index), |
| 156 | mDag(dag) |
| 157 | { |
| 158 | } |
| 159 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 160 | void traverse(TIntermFunctionDefinition *node) |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 161 | { |
| 162 | node->traverse(this); |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 163 | ASSERT(mLoopsAndSwitches.empty()); |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 164 | ASSERT(mIfs.empty()); |
| 165 | } |
| 166 | |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 167 | // 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 Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 170 | { |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 171 | mMetadata->mHasGradientLoopInCallGraph = true; |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 172 | // Mark the latest if as using a discontinuous loop. |
| 173 | if (!mIfs.empty()) |
| 174 | { |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 175 | mMetadata->mIfsContainingGradientLoop.insert(mIfs.back()); |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 179 | bool visitLoop(Visit visit, TIntermLoop *loop) override |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 180 | { |
| 181 | if (visit == PreVisit) |
| 182 | { |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 183 | mLoopsAndSwitches.push_back(loop); |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 184 | |
| 185 | if (mMetadata->hasGradientInCallGraph(loop)) |
| 186 | { |
| 187 | onGradientLoop(); |
| 188 | } |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 189 | } |
| 190 | else if (visit == PostVisit) |
| 191 | { |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 192 | ASSERT(mLoopsAndSwitches.back() == loop); |
| 193 | mLoopsAndSwitches.pop_back(); |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return true; |
| 197 | } |
| 198 | |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 199 | bool visitIfElse(Visit visit, TIntermIfElse *node) override |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 200 | { |
| 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 Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 210 | if (mMetadata->mIfsContainingGradientLoop.count(node) > 0 && !mIfs.empty()) |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 211 | { |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 212 | mMetadata->mIfsContainingGradientLoop.insert(mIfs.back()); |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
| 216 | return true; |
| 217 | } |
| 218 | |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 219 | bool visitBranch(Visit visit, TIntermBranch *node) override |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 220 | { |
| 221 | if (visit == PreVisit) |
| 222 | { |
| 223 | switch (node->getFlowOp()) |
| 224 | { |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 225 | case EOpBreak: |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 226 | { |
| 227 | ASSERT(!mLoopsAndSwitches.empty()); |
| 228 | TIntermLoop *loop = mLoopsAndSwitches.back()->getAsLoopNode(); |
| 229 | if (loop != nullptr) |
| 230 | { |
| 231 | mMetadata->mDiscontinuousLoops.insert(loop); |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | break; |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 235 | case EOpContinue: |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 236 | { |
| 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 Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 247 | } |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 248 | break; |
Corentin Wallez | a1884f2 | 2015-04-29 10:15:16 -0700 | [diff] [blame] | 249 | case EOpKill: |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 250 | case EOpReturn: |
Corentin Wallez | a1884f2 | 2015-04-29 10:15:16 -0700 | [diff] [blame] | 251 | // A return or discard jumps out of all the enclosing loops |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 252 | if (!mLoopsAndSwitches.empty()) |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 253 | { |
Cooper Partin | 4f48849 | 2015-08-07 16:05:25 -0700 | [diff] [blame] | 254 | for (TIntermNode *intermNode : mLoopsAndSwitches) |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 255 | { |
Cooper Partin | 4f48849 | 2015-08-07 16:05:25 -0700 | [diff] [blame] | 256 | TIntermLoop *loop = intermNode->getAsLoopNode(); |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 257 | if (loop) |
| 258 | { |
| 259 | mMetadata->mDiscontinuousLoops.insert(loop); |
| 260 | } |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 261 | } |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 262 | } |
| 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 Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 278 | size_t calleeIndex = mDag.findIndex(node->getFunctionSymbolInfo()); |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 279 | ASSERT(calleeIndex != CallDAG::InvalidIndex && calleeIndex < mIndex); |
| 280 | |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 281 | if ((*mMetadataList)[calleeIndex].mHasGradientLoopInCallGraph) |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 282 | { |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 283 | onGradientLoop(); |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | return true; |
| 289 | } |
| 290 | |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 291 | 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 Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 305 | private: |
| 306 | MetadataList *mMetadataList; |
| 307 | ASTMetadataHLSL *mMetadata; |
| 308 | size_t mIndex; |
| 309 | const CallDAG &mDag; |
| 310 | |
Olli Etuaho | 3873cd0 | 2015-04-10 15:00:55 +0300 | [diff] [blame] | 311 | std::vector<TIntermNode*> mLoopsAndSwitches; |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 312 | std::vector<TIntermIfElse *> mIfs; |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 313 | }; |
| 314 | |
| 315 | // Tags all the functions called in a discontinuous loop |
| 316 | class 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 Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 329 | void traverse(TIntermFunctionDefinition *node) |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 330 | { |
| 331 | node->traverse(this); |
| 332 | ASSERT(mNestedDiscont == (mMetadata->mCalledInDiscontinuousLoop ? 1 : 0)); |
| 333 | } |
| 334 | |
Austin Kinross | 0998fe9 | 2015-12-11 11:31:38 -0800 | [diff] [blame] | 335 | bool visitLoop(Visit visit, TIntermLoop *loop) override |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 336 | { |
| 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 Etuaho | bd67455 | 2016-10-06 13:28:42 +0100 | [diff] [blame] | 358 | size_t calleeIndex = mDag.findIndex(node->getFunctionSymbolInfo()); |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 359 | 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 Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 379 | } |
| 380 | |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 381 | bool ASTMetadataHLSL::hasGradientInCallGraph(TIntermLoop *node) |
| 382 | { |
| 383 | return mControlFlowsContainingGradient.count(node) > 0; |
| 384 | } |
| 385 | |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 386 | bool ASTMetadataHLSL::hasGradientLoop(TIntermIfElse *node) |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 387 | { |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 388 | return mIfsContainingGradientLoop.count(node) > 0; |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 391 | MetadataList 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 Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 418 | // 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 Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 425 | // the ifs and functions using a gradient loop. |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 426 | for (size_t i = 0; i < callDag.size(); i++) |
| 427 | { |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 428 | PullComputeDiscontinuousAndGradientLoops pull(&metadataList, i, callDag); |
Corentin Wallez | 5093145 | 2015-03-19 10:39:13 -0700 | [diff] [blame] | 429 | 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 Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 448 | return metadataList; |
| 449 | } |