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