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