daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 7 | #include "compiler/translator/OutputHLSL.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 8 | |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 9 | #include <algorithm> |
shannon.woods@transgaming.com | fff89b3 | 2013-02-28 23:20:15 +0000 | [diff] [blame] | 10 | #include <cfloat> |
| 11 | #include <stdio.h> |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 12 | |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 13 | #include "common/angleutils.h" |
| 14 | #include "common/utilities.h" |
| 15 | #include "compiler/translator/BuiltInFunctionEmulatorHLSL.h" |
| 16 | #include "compiler/translator/DetectDiscontinuity.h" |
| 17 | #include "compiler/translator/FlagStd140Structs.h" |
| 18 | #include "compiler/translator/InfoSink.h" |
| 19 | #include "compiler/translator/NodeSearch.h" |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 20 | #include "compiler/translator/RemoveSwitchFallThrough.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 21 | #include "compiler/translator/RewriteElseBlocks.h" |
| 22 | #include "compiler/translator/SearchSymbol.h" |
| 23 | #include "compiler/translator/StructureHLSL.h" |
| 24 | #include "compiler/translator/TranslatorHLSL.h" |
| 25 | #include "compiler/translator/UnfoldShortCircuit.h" |
| 26 | #include "compiler/translator/UniformHLSL.h" |
| 27 | #include "compiler/translator/UtilsHLSL.h" |
| 28 | #include "compiler/translator/blocklayout.h" |
| 29 | #include "compiler/translator/compilerdebug.h" |
| 30 | #include "compiler/translator/util.h" |
| 31 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 32 | namespace sh |
| 33 | { |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 34 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 35 | TString OutputHLSL::TextureFunction::name() const |
| 36 | { |
| 37 | TString name = "gl_texture"; |
| 38 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 39 | if (IsSampler2D(sampler)) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 40 | { |
| 41 | name += "2D"; |
| 42 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 43 | else if (IsSampler3D(sampler)) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 44 | { |
| 45 | name += "3D"; |
| 46 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 47 | else if (IsSamplerCube(sampler)) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 48 | { |
| 49 | name += "Cube"; |
| 50 | } |
| 51 | else UNREACHABLE(); |
| 52 | |
| 53 | if (proj) |
| 54 | { |
| 55 | name += "Proj"; |
| 56 | } |
| 57 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 58 | if (offset) |
| 59 | { |
| 60 | name += "Offset"; |
| 61 | } |
| 62 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 63 | switch(method) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 64 | { |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 65 | case IMPLICIT: break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 66 | case BIAS: break; // Extra parameter makes the signature unique |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 67 | case LOD: name += "Lod"; break; |
| 68 | case LOD0: name += "Lod0"; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 69 | case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 70 | case SIZE: name += "Size"; break; |
| 71 | case FETCH: name += "Fetch"; break; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 72 | case GRAD: name += "Grad"; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 73 | default: UNREACHABLE(); |
| 74 | } |
| 75 | |
| 76 | return name + "("; |
| 77 | } |
| 78 | |
| 79 | bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const |
| 80 | { |
| 81 | if (sampler < rhs.sampler) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 82 | if (sampler > rhs.sampler) return false; |
| 83 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 84 | if (coords < rhs.coords) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 85 | if (coords > rhs.coords) return false; |
| 86 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 87 | if (!proj && rhs.proj) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 88 | if (proj && !rhs.proj) return false; |
| 89 | |
| 90 | if (!offset && rhs.offset) return true; |
| 91 | if (offset && !rhs.offset) return false; |
| 92 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 93 | if (method < rhs.method) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 94 | if (method > rhs.method) return false; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 95 | |
| 96 | return false; |
| 97 | } |
| 98 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 99 | OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion, |
| 100 | const TExtensionBehavior &extensionBehavior, |
| 101 | const char *sourcePath, ShShaderOutput outputType, |
| 102 | int numRenderTargets, const std::vector<Uniform> &uniforms, |
| 103 | int compileOptions) |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 104 | : TIntermTraverser(true, true, true), |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 105 | mShaderType(shaderType), |
| 106 | mShaderVersion(shaderVersion), |
| 107 | mExtensionBehavior(extensionBehavior), |
| 108 | mSourcePath(sourcePath), |
| 109 | mOutputType(outputType), |
| 110 | mNumRenderTargets(numRenderTargets), |
| 111 | mCompileOptions(compileOptions) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 112 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 113 | mUnfoldShortCircuit = new UnfoldShortCircuit(this); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 114 | mInsideFunction = false; |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 115 | |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 116 | mUsesFragColor = false; |
| 117 | mUsesFragData = false; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 118 | mUsesDepthRange = false; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 119 | mUsesFragCoord = false; |
| 120 | mUsesPointCoord = false; |
| 121 | mUsesFrontFacing = false; |
| 122 | mUsesPointSize = false; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 123 | mUsesInstanceID = false; |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 124 | mUsesFragDepth = false; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 125 | mUsesXor = false; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 126 | mUsesDiscardRewriting = false; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 127 | mUsesNestedBreak = false; |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 128 | |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 129 | mUniqueIndex = 0; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 130 | |
| 131 | mContainsLoopDiscontinuity = false; |
Corentin Wallez | 80bacde | 2014-11-10 12:07:37 -0800 | [diff] [blame] | 132 | mContainsAnyLoop = false; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 133 | mOutputLod0Function = false; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 134 | mInsideDiscontinuousLoop = false; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 135 | mNestedLoopDepth = 0; |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 136 | |
| 137 | mExcessiveLoopIndex = NULL; |
daniel@transgaming.com | 652468c | 2012-12-20 21:11:57 +0000 | [diff] [blame] | 138 | |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 139 | mStructureHLSL = new StructureHLSL; |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 140 | mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms); |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 141 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 142 | if (mOutputType == SH_HLSL9_OUTPUT) |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 143 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 144 | if (mShaderType == GL_FRAGMENT_SHADER) |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 145 | { |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 146 | // Reserve registers for dx_DepthRange, dx_ViewCoords and dx_DepthFront |
| 147 | mUniformHLSL->reserveUniformRegisters(3); |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 148 | } |
| 149 | else |
| 150 | { |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 151 | // Reserve registers for dx_DepthRange, dx_ViewAdjust and dx_ViewCoords |
| 152 | mUniformHLSL->reserveUniformRegisters(3); |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 153 | } |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 154 | } |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 155 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 156 | // Reserve registers for the default uniform block and driver constants |
| 157 | mUniformHLSL->reserveInterfaceBlockRegisters(2); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 158 | } |
| 159 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 160 | OutputHLSL::~OutputHLSL() |
| 161 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 162 | SafeDelete(mUnfoldShortCircuit); |
| 163 | SafeDelete(mStructureHLSL); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 164 | SafeDelete(mUniformHLSL); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 167 | void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink) |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 168 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 169 | mContainsLoopDiscontinuity = mShaderType == GL_FRAGMENT_SHADER && containsLoopDiscontinuity(treeRoot); |
| 170 | mContainsAnyLoop = containsAnyLoop(treeRoot); |
| 171 | const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 172 | makeFlaggedStructMaps(flaggedStructs); |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 173 | |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 174 | // Work around D3D9 bug that would manifest in vertex shaders with selection blocks which |
| 175 | // use a vertex attribute as a condition, and some related computation in the else block. |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 176 | if (mOutputType == SH_HLSL9_OUTPUT && mShaderType == GL_VERTEX_SHADER) |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 177 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 178 | RewriteElseBlocks(treeRoot); |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 179 | } |
| 180 | |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 181 | BuiltInFunctionEmulatorHLSL builtInFunctionEmulator; |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 182 | builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 183 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 184 | // Output the body and footer first to determine what has to go in the header |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 185 | mInfoSinkStack.push(&mBody); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 186 | treeRoot->traverse(this); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 187 | mInfoSinkStack.pop(); |
| 188 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 189 | mInfoSinkStack.push(&mFooter); |
| 190 | if (!mDeferredGlobalInitializers.empty()) |
| 191 | { |
| 192 | writeDeferredGlobalInitializers(mFooter); |
| 193 | } |
| 194 | mInfoSinkStack.pop(); |
| 195 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 196 | mInfoSinkStack.push(&mHeader); |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 197 | header(&builtInFunctionEmulator); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 198 | mInfoSinkStack.pop(); |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 199 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 200 | objSink << mHeader.c_str(); |
| 201 | objSink << mBody.c_str(); |
| 202 | objSink << mFooter.c_str(); |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 203 | |
| 204 | builtInFunctionEmulator.Cleanup(); |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 207 | void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs) |
| 208 | { |
| 209 | for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++) |
| 210 | { |
| 211 | TIntermTyped *flaggedNode = flaggedStructs[structIndex]; |
| 212 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 213 | TInfoSinkBase structInfoSink; |
| 214 | mInfoSinkStack.push(&structInfoSink); |
| 215 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 216 | // This will mark the necessary block elements as referenced |
| 217 | flaggedNode->traverse(this); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 218 | |
| 219 | TString structName(structInfoSink.c_str()); |
| 220 | mInfoSinkStack.pop(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 221 | |
| 222 | mFlaggedStructOriginalNames[flaggedNode] = structName; |
| 223 | |
| 224 | for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.')) |
| 225 | { |
| 226 | structName.erase(pos, 1); |
| 227 | } |
| 228 | |
| 229 | mFlaggedStructMappedNames[flaggedNode] = "map" + structName; |
| 230 | } |
| 231 | } |
| 232 | |
Jamie Madill | 4e1fd41 | 2014-07-10 17:50:10 -0400 | [diff] [blame] | 233 | const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const |
| 234 | { |
| 235 | return mUniformHLSL->getInterfaceBlockRegisterMap(); |
| 236 | } |
| 237 | |
Jamie Madill | 9fe25e9 | 2014-07-18 10:33:08 -0400 | [diff] [blame] | 238 | const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const |
| 239 | { |
| 240 | return mUniformHLSL->getUniformRegisterMap(); |
| 241 | } |
| 242 | |
daniel@transgaming.com | 0b6b834 | 2010-04-26 15:33:45 +0000 | [diff] [blame] | 243 | int OutputHLSL::vectorSize(const TType &type) const |
| 244 | { |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 245 | int elementSize = type.isMatrix() ? type.getCols() : 1; |
daniel@transgaming.com | 0b6b834 | 2010-04-26 15:33:45 +0000 | [diff] [blame] | 246 | int arraySize = type.isArray() ? type.getArraySize() : 1; |
| 247 | |
| 248 | return elementSize * arraySize; |
| 249 | } |
| 250 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 251 | TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 252 | { |
| 253 | TString init; |
| 254 | |
| 255 | TString preIndentString; |
| 256 | TString fullIndentString; |
| 257 | |
| 258 | for (int spaces = 0; spaces < (indent * 4); spaces++) |
| 259 | { |
| 260 | preIndentString += ' '; |
| 261 | } |
| 262 | |
| 263 | for (int spaces = 0; spaces < ((indent+1) * 4); spaces++) |
| 264 | { |
| 265 | fullIndentString += ' '; |
| 266 | } |
| 267 | |
| 268 | init += preIndentString + "{\n"; |
| 269 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 270 | const TFieldList &fields = structure.fields(); |
| 271 | for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 272 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 273 | const TField &field = *fields[fieldIndex]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 274 | const TString &fieldName = rhsStructName + "." + Decorate(field.name()); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 275 | const TType &fieldType = *field.type(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 276 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 277 | if (fieldType.getStruct()) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 278 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 279 | init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 280 | } |
| 281 | else |
| 282 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 283 | init += fullIndentString + fieldName + ",\n"; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | |
| 287 | init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n"; |
| 288 | |
| 289 | return init; |
| 290 | } |
| 291 | |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 292 | void OutputHLSL::header(const BuiltInFunctionEmulatorHLSL *builtInFunctionEmulator) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 293 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 294 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 295 | |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 296 | TString varyings; |
| 297 | TString attributes; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 298 | TString flaggedStructs; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 299 | |
Jamie Madill | 829f59e | 2013-11-13 19:40:54 -0500 | [diff] [blame] | 300 | for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 301 | { |
| 302 | TIntermTyped *structNode = flaggedStructIt->first; |
| 303 | const TString &mappedName = flaggedStructIt->second; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 304 | const TStructure &structure = *structNode->getType().getStruct(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 305 | const TString &originalName = mFlaggedStructOriginalNames[structNode]; |
| 306 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 307 | flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n"; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 308 | flaggedStructs += structInitializerString(0, structure, originalName); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 309 | flaggedStructs += "\n"; |
| 310 | } |
| 311 | |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 312 | for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++) |
| 313 | { |
| 314 | const TType &type = varying->second->getType(); |
| 315 | const TString &name = varying->second->getSymbol(); |
| 316 | |
| 317 | // Program linking depends on this exact format |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 318 | varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " + |
| 319 | Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n"; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++) |
| 323 | { |
| 324 | const TType &type = attribute->second->getType(); |
| 325 | const TString &name = attribute->second->getSymbol(); |
| 326 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 327 | attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n"; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 330 | out << mStructureHLSL->structsHeader(); |
Jamie Madill | 529077d | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 331 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 332 | out << mUniformHLSL->uniformsHeader(mOutputType, mReferencedUniforms); |
| 333 | out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks); |
| 334 | |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 335 | if (!mStructEqualityFunctions.empty()) |
| 336 | { |
| 337 | out << "\n// Structure equality functions\n\n"; |
| 338 | for (const auto &eqFunction : mStructEqualityFunctions) |
| 339 | { |
| 340 | out << eqFunction.functionDefinition << "\n"; |
| 341 | } |
| 342 | } |
| 343 | |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 344 | if (mUsesDiscardRewriting) |
| 345 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 346 | out << "#define ANGLE_USES_DISCARD_REWRITING\n"; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 347 | } |
| 348 | |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 349 | if (mUsesNestedBreak) |
| 350 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 351 | out << "#define ANGLE_USES_NESTED_BREAK\n"; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 352 | } |
| 353 | |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 354 | out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n" |
| 355 | "#define LOOP [loop]\n" |
| 356 | "#define FLATTEN [flatten]\n" |
| 357 | "#else\n" |
| 358 | "#define LOOP\n" |
| 359 | "#define FLATTEN\n" |
| 360 | "#endif\n"; |
| 361 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 362 | if (mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 363 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 364 | TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers"); |
| 365 | const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire)); |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 366 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 367 | out << "// Varyings\n"; |
| 368 | out << varyings; |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 369 | out << "\n"; |
| 370 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 371 | if (mShaderVersion >= 300) |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 372 | { |
Jamie Madill | 829f59e | 2013-11-13 19:40:54 -0500 | [diff] [blame] | 373 | for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++) |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 374 | { |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 375 | const TString &variableName = outputVariableIt->first; |
| 376 | const TType &variableType = outputVariableIt->second->getType(); |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 377 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 378 | out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) + |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 379 | " = " + initializer(variableType) + ";\n"; |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 380 | } |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 381 | } |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 382 | else |
| 383 | { |
| 384 | const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1; |
| 385 | |
| 386 | out << "static float4 gl_Color[" << numColorValues << "] =\n" |
| 387 | "{\n"; |
| 388 | for (unsigned int i = 0; i < numColorValues; i++) |
| 389 | { |
| 390 | out << " float4(0, 0, 0, 0)"; |
| 391 | if (i + 1 != numColorValues) |
| 392 | { |
| 393 | out << ","; |
| 394 | } |
| 395 | out << "\n"; |
| 396 | } |
| 397 | |
| 398 | out << "};\n"; |
| 399 | } |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 400 | |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 401 | if (mUsesFragDepth) |
| 402 | { |
| 403 | out << "static float gl_Depth = 0.0;\n"; |
| 404 | } |
| 405 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 406 | if (mUsesFragCoord) |
| 407 | { |
| 408 | out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n"; |
| 409 | } |
| 410 | |
| 411 | if (mUsesPointCoord) |
| 412 | { |
| 413 | out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n"; |
| 414 | } |
| 415 | |
| 416 | if (mUsesFrontFacing) |
| 417 | { |
| 418 | out << "static bool gl_FrontFacing = false;\n"; |
| 419 | } |
| 420 | |
| 421 | out << "\n"; |
| 422 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 423 | if (mUsesDepthRange) |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 424 | { |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 425 | out << "struct gl_DepthRangeParameters\n" |
| 426 | "{\n" |
| 427 | " float near;\n" |
| 428 | " float far;\n" |
| 429 | " float diff;\n" |
| 430 | "};\n" |
| 431 | "\n"; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 432 | } |
| 433 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 434 | if (mOutputType == SH_HLSL11_OUTPUT) |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 435 | { |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 436 | out << "cbuffer DriverConstants : register(b1)\n" |
| 437 | "{\n"; |
| 438 | |
| 439 | if (mUsesDepthRange) |
| 440 | { |
| 441 | out << " float3 dx_DepthRange : packoffset(c0);\n"; |
| 442 | } |
| 443 | |
| 444 | if (mUsesFragCoord) |
| 445 | { |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 446 | out << " float4 dx_ViewCoords : packoffset(c1);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (mUsesFragCoord || mUsesFrontFacing) |
| 450 | { |
| 451 | out << " float3 dx_DepthFront : packoffset(c2);\n"; |
| 452 | } |
| 453 | |
| 454 | out << "};\n"; |
| 455 | } |
| 456 | else |
| 457 | { |
| 458 | if (mUsesDepthRange) |
| 459 | { |
| 460 | out << "uniform float3 dx_DepthRange : register(c0);"; |
| 461 | } |
| 462 | |
| 463 | if (mUsesFragCoord) |
| 464 | { |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 465 | out << "uniform float4 dx_ViewCoords : register(c1);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | if (mUsesFragCoord || mUsesFrontFacing) |
| 469 | { |
| 470 | out << "uniform float3 dx_DepthFront : register(c2);\n"; |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | out << "\n"; |
| 475 | |
| 476 | if (mUsesDepthRange) |
| 477 | { |
| 478 | out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n" |
| 479 | "\n"; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 480 | } |
daniel@transgaming.com | 5024cc4 | 2010-04-20 18:52:04 +0000 | [diff] [blame] | 481 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 482 | if (!flaggedStructs.empty()) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 483 | { |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 484 | out << "// Std140 Structures accessed by value\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 485 | out << "\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 486 | out << flaggedStructs; |
| 487 | out << "\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 488 | } |
| 489 | |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 490 | if (usingMRTExtension && mNumRenderTargets > 1) |
| 491 | { |
| 492 | out << "#define GL_USES_MRT\n"; |
| 493 | } |
| 494 | |
| 495 | if (mUsesFragColor) |
| 496 | { |
| 497 | out << "#define GL_USES_FRAG_COLOR\n"; |
| 498 | } |
| 499 | |
| 500 | if (mUsesFragData) |
| 501 | { |
| 502 | out << "#define GL_USES_FRAG_DATA\n"; |
| 503 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 504 | } |
daniel@transgaming.com | d25ab25 | 2010-03-30 03:36:26 +0000 | [diff] [blame] | 505 | else // Vertex shader |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 506 | { |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 507 | out << "// Attributes\n"; |
| 508 | out << attributes; |
| 509 | out << "\n" |
| 510 | "static float4 gl_Position = float4(0, 0, 0, 0);\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 511 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 512 | if (mUsesPointSize) |
| 513 | { |
| 514 | out << "static float gl_PointSize = float(1);\n"; |
| 515 | } |
| 516 | |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 517 | if (mUsesInstanceID) |
| 518 | { |
| 519 | out << "static int gl_InstanceID;"; |
| 520 | } |
| 521 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 522 | out << "\n" |
| 523 | "// Varyings\n"; |
| 524 | out << varyings; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 525 | out << "\n"; |
| 526 | |
| 527 | if (mUsesDepthRange) |
| 528 | { |
| 529 | out << "struct gl_DepthRangeParameters\n" |
| 530 | "{\n" |
| 531 | " float near;\n" |
| 532 | " float far;\n" |
| 533 | " float diff;\n" |
| 534 | "};\n" |
| 535 | "\n"; |
| 536 | } |
| 537 | |
| 538 | if (mOutputType == SH_HLSL11_OUTPUT) |
| 539 | { |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 540 | out << "cbuffer DriverConstants : register(b1)\n" |
| 541 | "{\n"; |
| 542 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 543 | if (mUsesDepthRange) |
| 544 | { |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 545 | out << " float3 dx_DepthRange : packoffset(c0);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 546 | } |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 547 | |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 548 | // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders. |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 549 | // However, we declare it for all shaders (including Feature Level 10+). |
| 550 | // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused. |
| 551 | out << " float4 dx_ViewAdjust : packoffset(c1);\n"; |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 552 | out << " float2 dx_ViewCoords : packoffset(c2);\n"; |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 553 | |
| 554 | out << "};\n" |
| 555 | "\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 556 | } |
| 557 | else |
| 558 | { |
| 559 | if (mUsesDepthRange) |
| 560 | { |
| 561 | out << "uniform float3 dx_DepthRange : register(c0);\n"; |
| 562 | } |
| 563 | |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 564 | out << "uniform float4 dx_ViewAdjust : register(c1);\n"; |
| 565 | out << "uniform float2 dx_ViewCoords : register(c2);\n" |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 566 | "\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 567 | } |
| 568 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 569 | if (mUsesDepthRange) |
| 570 | { |
| 571 | out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n" |
| 572 | "\n"; |
| 573 | } |
| 574 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 575 | if (!flaggedStructs.empty()) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 576 | { |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 577 | out << "// Std140 Structures accessed by value\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 578 | out << "\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 579 | out << flaggedStructs; |
| 580 | out << "\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 581 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 582 | } |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 583 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 584 | for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++) |
| 585 | { |
| 586 | // Return type |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 587 | if (textureFunction->method == TextureFunction::SIZE) |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 588 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 589 | switch(textureFunction->sampler) |
| 590 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 591 | case EbtSampler2D: out << "int2 "; break; |
| 592 | case EbtSampler3D: out << "int3 "; break; |
| 593 | case EbtSamplerCube: out << "int2 "; break; |
| 594 | case EbtSampler2DArray: out << "int3 "; break; |
| 595 | case EbtISampler2D: out << "int2 "; break; |
| 596 | case EbtISampler3D: out << "int3 "; break; |
| 597 | case EbtISamplerCube: out << "int2 "; break; |
| 598 | case EbtISampler2DArray: out << "int3 "; break; |
| 599 | case EbtUSampler2D: out << "int2 "; break; |
| 600 | case EbtUSampler3D: out << "int3 "; break; |
| 601 | case EbtUSamplerCube: out << "int2 "; break; |
| 602 | case EbtUSampler2DArray: out << "int3 "; break; |
| 603 | case EbtSampler2DShadow: out << "int2 "; break; |
| 604 | case EbtSamplerCubeShadow: out << "int2 "; break; |
| 605 | case EbtSampler2DArrayShadow: out << "int3 "; break; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 606 | default: UNREACHABLE(); |
| 607 | } |
| 608 | } |
| 609 | else // Sampling function |
| 610 | { |
| 611 | switch(textureFunction->sampler) |
| 612 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 613 | case EbtSampler2D: out << "float4 "; break; |
| 614 | case EbtSampler3D: out << "float4 "; break; |
| 615 | case EbtSamplerCube: out << "float4 "; break; |
| 616 | case EbtSampler2DArray: out << "float4 "; break; |
| 617 | case EbtISampler2D: out << "int4 "; break; |
| 618 | case EbtISampler3D: out << "int4 "; break; |
| 619 | case EbtISamplerCube: out << "int4 "; break; |
| 620 | case EbtISampler2DArray: out << "int4 "; break; |
| 621 | case EbtUSampler2D: out << "uint4 "; break; |
| 622 | case EbtUSampler3D: out << "uint4 "; break; |
| 623 | case EbtUSamplerCube: out << "uint4 "; break; |
| 624 | case EbtUSampler2DArray: out << "uint4 "; break; |
| 625 | case EbtSampler2DShadow: out << "float "; break; |
| 626 | case EbtSamplerCubeShadow: out << "float "; break; |
| 627 | case EbtSampler2DArrayShadow: out << "float "; break; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 628 | default: UNREACHABLE(); |
| 629 | } |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 632 | // Function name |
| 633 | out << textureFunction->name(); |
| 634 | |
| 635 | // Argument list |
| 636 | int hlslCoords = 4; |
| 637 | |
| 638 | if (mOutputType == SH_HLSL9_OUTPUT) |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 639 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 640 | switch(textureFunction->sampler) |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 641 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 642 | case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break; |
| 643 | case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break; |
| 644 | default: UNREACHABLE(); |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 645 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 646 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 647 | switch(textureFunction->method) |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 648 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 649 | case TextureFunction::IMPLICIT: break; |
| 650 | case TextureFunction::BIAS: hlslCoords = 4; break; |
| 651 | case TextureFunction::LOD: hlslCoords = 4; break; |
| 652 | case TextureFunction::LOD0: hlslCoords = 4; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 653 | case TextureFunction::LOD0BIAS: hlslCoords = 4; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 654 | default: UNREACHABLE(); |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 655 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 656 | } |
| 657 | else if (mOutputType == SH_HLSL11_OUTPUT) |
| 658 | { |
| 659 | switch(textureFunction->sampler) |
| 660 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 661 | case EbtSampler2D: out << "Texture2D x, SamplerState s"; hlslCoords = 2; break; |
| 662 | case EbtSampler3D: out << "Texture3D x, SamplerState s"; hlslCoords = 3; break; |
| 663 | case EbtSamplerCube: out << "TextureCube x, SamplerState s"; hlslCoords = 3; break; |
| 664 | case EbtSampler2DArray: out << "Texture2DArray x, SamplerState s"; hlslCoords = 3; break; |
| 665 | case EbtISampler2D: out << "Texture2D<int4> x, SamplerState s"; hlslCoords = 2; break; |
| 666 | case EbtISampler3D: out << "Texture3D<int4> x, SamplerState s"; hlslCoords = 3; break; |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 667 | case EbtISamplerCube: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break; |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 668 | case EbtISampler2DArray: out << "Texture2DArray<int4> x, SamplerState s"; hlslCoords = 3; break; |
| 669 | case EbtUSampler2D: out << "Texture2D<uint4> x, SamplerState s"; hlslCoords = 2; break; |
| 670 | case EbtUSampler3D: out << "Texture3D<uint4> x, SamplerState s"; hlslCoords = 3; break; |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 671 | case EbtUSamplerCube: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break; |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 672 | case EbtUSampler2DArray: out << "Texture2DArray<uint4> x, SamplerState s"; hlslCoords = 3; break; |
| 673 | case EbtSampler2DShadow: out << "Texture2D x, SamplerComparisonState s"; hlslCoords = 2; break; |
| 674 | case EbtSamplerCubeShadow: out << "TextureCube x, SamplerComparisonState s"; hlslCoords = 3; break; |
| 675 | case EbtSampler2DArrayShadow: out << "Texture2DArray x, SamplerComparisonState s"; hlslCoords = 3; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 676 | default: UNREACHABLE(); |
| 677 | } |
| 678 | } |
| 679 | else UNREACHABLE(); |
| 680 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 681 | if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 682 | { |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 683 | switch(textureFunction->coords) |
| 684 | { |
| 685 | case 2: out << ", int2 t"; break; |
| 686 | case 3: out << ", int3 t"; break; |
| 687 | default: UNREACHABLE(); |
| 688 | } |
| 689 | } |
| 690 | else // Floating-point coordinates (except textureSize) |
| 691 | { |
| 692 | switch(textureFunction->coords) |
| 693 | { |
| 694 | case 1: out << ", int lod"; break; // textureSize() |
| 695 | case 2: out << ", float2 t"; break; |
| 696 | case 3: out << ", float3 t"; break; |
| 697 | case 4: out << ", float4 t"; break; |
| 698 | default: UNREACHABLE(); |
| 699 | } |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 702 | if (textureFunction->method == TextureFunction::GRAD) |
| 703 | { |
| 704 | switch(textureFunction->sampler) |
| 705 | { |
| 706 | case EbtSampler2D: |
| 707 | case EbtISampler2D: |
| 708 | case EbtUSampler2D: |
| 709 | case EbtSampler2DArray: |
| 710 | case EbtISampler2DArray: |
| 711 | case EbtUSampler2DArray: |
| 712 | case EbtSampler2DShadow: |
| 713 | case EbtSampler2DArrayShadow: |
| 714 | out << ", float2 ddx, float2 ddy"; |
| 715 | break; |
| 716 | case EbtSampler3D: |
| 717 | case EbtISampler3D: |
| 718 | case EbtUSampler3D: |
| 719 | case EbtSamplerCube: |
| 720 | case EbtISamplerCube: |
| 721 | case EbtUSamplerCube: |
| 722 | case EbtSamplerCubeShadow: |
| 723 | out << ", float3 ddx, float3 ddy"; |
| 724 | break; |
| 725 | default: UNREACHABLE(); |
| 726 | } |
| 727 | } |
| 728 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 729 | switch(textureFunction->method) |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 730 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 731 | case TextureFunction::IMPLICIT: break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 732 | case TextureFunction::BIAS: break; // Comes after the offset parameter |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 733 | case TextureFunction::LOD: out << ", float lod"; break; |
| 734 | case TextureFunction::LOD0: break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 735 | case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 736 | case TextureFunction::SIZE: break; |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 737 | case TextureFunction::FETCH: out << ", int mip"; break; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 738 | case TextureFunction::GRAD: break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 739 | default: UNREACHABLE(); |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 742 | if (textureFunction->offset) |
| 743 | { |
| 744 | switch(textureFunction->sampler) |
| 745 | { |
| 746 | case EbtSampler2D: out << ", int2 offset"; break; |
| 747 | case EbtSampler3D: out << ", int3 offset"; break; |
| 748 | case EbtSampler2DArray: out << ", int2 offset"; break; |
| 749 | case EbtISampler2D: out << ", int2 offset"; break; |
| 750 | case EbtISampler3D: out << ", int3 offset"; break; |
| 751 | case EbtISampler2DArray: out << ", int2 offset"; break; |
| 752 | case EbtUSampler2D: out << ", int2 offset"; break; |
| 753 | case EbtUSampler3D: out << ", int3 offset"; break; |
| 754 | case EbtUSampler2DArray: out << ", int2 offset"; break; |
| 755 | case EbtSampler2DShadow: out << ", int2 offset"; break; |
Nicolas Capens | bf7db10 | 2014-02-19 17:20:28 -0500 | [diff] [blame] | 756 | case EbtSampler2DArrayShadow: out << ", int2 offset"; break; |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 757 | default: UNREACHABLE(); |
| 758 | } |
| 759 | } |
| 760 | |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 761 | if (textureFunction->method == TextureFunction::BIAS || |
| 762 | textureFunction->method == TextureFunction::LOD0BIAS) |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 763 | { |
| 764 | out << ", float bias"; |
| 765 | } |
| 766 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 767 | out << ")\n" |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 768 | "{\n"; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 769 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 770 | if (textureFunction->method == TextureFunction::SIZE) |
| 771 | { |
| 772 | if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler)) |
| 773 | { |
| 774 | if (IsSamplerArray(textureFunction->sampler)) |
| 775 | { |
| 776 | out << " uint width; uint height; uint layers; uint numberOfLevels;\n" |
| 777 | " x.GetDimensions(lod, width, height, layers, numberOfLevels);\n"; |
| 778 | } |
| 779 | else |
| 780 | { |
| 781 | out << " uint width; uint height; uint numberOfLevels;\n" |
| 782 | " x.GetDimensions(lod, width, height, numberOfLevels);\n"; |
| 783 | } |
| 784 | } |
| 785 | else if (IsSampler3D(textureFunction->sampler)) |
| 786 | { |
| 787 | out << " uint width; uint height; uint depth; uint numberOfLevels;\n" |
| 788 | " x.GetDimensions(lod, width, height, depth, numberOfLevels);\n"; |
| 789 | } |
| 790 | else UNREACHABLE(); |
| 791 | |
| 792 | switch(textureFunction->sampler) |
| 793 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 794 | case EbtSampler2D: out << " return int2(width, height);"; break; |
| 795 | case EbtSampler3D: out << " return int3(width, height, depth);"; break; |
| 796 | case EbtSamplerCube: out << " return int2(width, height);"; break; |
| 797 | case EbtSampler2DArray: out << " return int3(width, height, layers);"; break; |
| 798 | case EbtISampler2D: out << " return int2(width, height);"; break; |
| 799 | case EbtISampler3D: out << " return int3(width, height, depth);"; break; |
| 800 | case EbtISamplerCube: out << " return int2(width, height);"; break; |
| 801 | case EbtISampler2DArray: out << " return int3(width, height, layers);"; break; |
| 802 | case EbtUSampler2D: out << " return int2(width, height);"; break; |
| 803 | case EbtUSampler3D: out << " return int3(width, height, depth);"; break; |
| 804 | case EbtUSamplerCube: out << " return int2(width, height);"; break; |
| 805 | case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break; |
| 806 | case EbtSampler2DShadow: out << " return int2(width, height);"; break; |
| 807 | case EbtSamplerCubeShadow: out << " return int2(width, height);"; break; |
| 808 | case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 809 | default: UNREACHABLE(); |
| 810 | } |
| 811 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 812 | else |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 813 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 814 | if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler)) |
| 815 | { |
| 816 | out << " float width; float height; float layers; float levels;\n"; |
| 817 | |
| 818 | out << " uint mip = 0;\n"; |
| 819 | |
| 820 | out << " x.GetDimensions(mip, width, height, layers, levels);\n"; |
| 821 | |
| 822 | out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n"; |
| 823 | out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n"; |
| 824 | out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n"; |
| 825 | out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n"; |
| 826 | |
| 827 | // FACE_POSITIVE_X = 000b |
| 828 | // FACE_NEGATIVE_X = 001b |
| 829 | // FACE_POSITIVE_Y = 010b |
| 830 | // FACE_NEGATIVE_Y = 011b |
| 831 | // FACE_POSITIVE_Z = 100b |
| 832 | // FACE_NEGATIVE_Z = 101b |
| 833 | out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n"; |
| 834 | |
| 835 | out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n"; |
| 836 | out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n"; |
| 837 | out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n"; |
| 838 | |
| 839 | out << " t.x = (u * 0.5f / m) + 0.5f;\n"; |
| 840 | out << " t.y = (v * 0.5f / m) + 0.5f;\n"; |
| 841 | } |
| 842 | else if (IsIntegerSampler(textureFunction->sampler) && |
| 843 | textureFunction->method != TextureFunction::FETCH) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 844 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 845 | if (IsSampler2D(textureFunction->sampler)) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 846 | { |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 847 | if (IsSamplerArray(textureFunction->sampler)) |
| 848 | { |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 849 | out << " float width; float height; float layers; float levels;\n"; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 850 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 851 | if (textureFunction->method == TextureFunction::LOD0) |
| 852 | { |
| 853 | out << " uint mip = 0;\n"; |
| 854 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 855 | else if (textureFunction->method == TextureFunction::LOD0BIAS) |
| 856 | { |
| 857 | out << " uint mip = bias;\n"; |
| 858 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 859 | else |
| 860 | { |
| 861 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 862 | textureFunction->method == TextureFunction::BIAS) |
| 863 | { |
| 864 | out << " x.GetDimensions(0, width, height, layers, levels);\n" |
| 865 | " float2 tSized = float2(t.x * width, t.y * height);\n" |
| 866 | " float dx = length(ddx(tSized));\n" |
| 867 | " float dy = length(ddy(tSized));\n" |
Jamie Madill | 03847b6 | 2013-11-13 19:42:39 -0500 | [diff] [blame] | 868 | " float lod = log2(max(dx, dy));\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 869 | |
| 870 | if (textureFunction->method == TextureFunction::BIAS) |
| 871 | { |
| 872 | out << " lod += bias;\n"; |
| 873 | } |
| 874 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 875 | else if (textureFunction->method == TextureFunction::GRAD) |
| 876 | { |
| 877 | out << " x.GetDimensions(0, width, height, layers, levels);\n" |
| 878 | " float lod = log2(max(length(ddx), length(ddy)));\n"; |
| 879 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 880 | |
| 881 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 882 | } |
| 883 | |
| 884 | out << " x.GetDimensions(mip, width, height, layers, levels);\n"; |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 885 | } |
| 886 | else |
| 887 | { |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 888 | out << " float width; float height; float levels;\n"; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 889 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 890 | if (textureFunction->method == TextureFunction::LOD0) |
| 891 | { |
| 892 | out << " uint mip = 0;\n"; |
| 893 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 894 | else if (textureFunction->method == TextureFunction::LOD0BIAS) |
| 895 | { |
| 896 | out << " uint mip = bias;\n"; |
| 897 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 898 | else |
| 899 | { |
| 900 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 901 | textureFunction->method == TextureFunction::BIAS) |
| 902 | { |
| 903 | out << " x.GetDimensions(0, width, height, levels);\n" |
| 904 | " float2 tSized = float2(t.x * width, t.y * height);\n" |
| 905 | " float dx = length(ddx(tSized));\n" |
| 906 | " float dy = length(ddy(tSized));\n" |
Jamie Madill | 03847b6 | 2013-11-13 19:42:39 -0500 | [diff] [blame] | 907 | " float lod = log2(max(dx, dy));\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 908 | |
| 909 | if (textureFunction->method == TextureFunction::BIAS) |
| 910 | { |
| 911 | out << " lod += bias;\n"; |
| 912 | } |
| 913 | } |
Nicolas Capens | 2adc256 | 2014-02-14 23:50:59 -0500 | [diff] [blame] | 914 | else if (textureFunction->method == TextureFunction::LOD) |
| 915 | { |
| 916 | out << " x.GetDimensions(0, width, height, levels);\n"; |
| 917 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 918 | else if (textureFunction->method == TextureFunction::GRAD) |
| 919 | { |
| 920 | out << " x.GetDimensions(0, width, height, levels);\n" |
| 921 | " float lod = log2(max(length(ddx), length(ddy)));\n"; |
| 922 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 923 | |
| 924 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 925 | } |
| 926 | |
| 927 | out << " x.GetDimensions(mip, width, height, levels);\n"; |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 928 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 929 | } |
| 930 | else if (IsSampler3D(textureFunction->sampler)) |
| 931 | { |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 932 | out << " float width; float height; float depth; float levels;\n"; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 933 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 934 | if (textureFunction->method == TextureFunction::LOD0) |
| 935 | { |
| 936 | out << " uint mip = 0;\n"; |
| 937 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 938 | else if (textureFunction->method == TextureFunction::LOD0BIAS) |
| 939 | { |
| 940 | out << " uint mip = bias;\n"; |
| 941 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 942 | else |
| 943 | { |
| 944 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 945 | textureFunction->method == TextureFunction::BIAS) |
| 946 | { |
| 947 | out << " x.GetDimensions(0, width, height, depth, levels);\n" |
| 948 | " float3 tSized = float3(t.x * width, t.y * height, t.z * depth);\n" |
| 949 | " float dx = length(ddx(tSized));\n" |
| 950 | " float dy = length(ddy(tSized));\n" |
Jamie Madill | 03847b6 | 2013-11-13 19:42:39 -0500 | [diff] [blame] | 951 | " float lod = log2(max(dx, dy));\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 952 | |
| 953 | if (textureFunction->method == TextureFunction::BIAS) |
| 954 | { |
| 955 | out << " lod += bias;\n"; |
| 956 | } |
| 957 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 958 | else if (textureFunction->method == TextureFunction::GRAD) |
| 959 | { |
| 960 | out << " x.GetDimensions(0, width, height, depth, levels);\n" |
| 961 | " float lod = log2(max(length(ddx), length(ddy)));\n"; |
| 962 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 963 | |
| 964 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 965 | } |
| 966 | |
| 967 | out << " x.GetDimensions(mip, width, height, depth, levels);\n"; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 968 | } |
| 969 | else UNREACHABLE(); |
| 970 | } |
| 971 | |
| 972 | out << " return "; |
| 973 | |
| 974 | // HLSL intrinsic |
| 975 | if (mOutputType == SH_HLSL9_OUTPUT) |
| 976 | { |
| 977 | switch(textureFunction->sampler) |
| 978 | { |
| 979 | case EbtSampler2D: out << "tex2D"; break; |
| 980 | case EbtSamplerCube: out << "texCUBE"; break; |
| 981 | default: UNREACHABLE(); |
| 982 | } |
| 983 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 984 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 985 | { |
| 986 | case TextureFunction::IMPLICIT: out << "(s, "; break; |
| 987 | case TextureFunction::BIAS: out << "bias(s, "; break; |
| 988 | case TextureFunction::LOD: out << "lod(s, "; break; |
| 989 | case TextureFunction::LOD0: out << "lod(s, "; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 990 | case TextureFunction::LOD0BIAS: out << "lod(s, "; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 991 | default: UNREACHABLE(); |
| 992 | } |
| 993 | } |
| 994 | else if (mOutputType == SH_HLSL11_OUTPUT) |
| 995 | { |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 996 | if (textureFunction->method == TextureFunction::GRAD) |
| 997 | { |
| 998 | if (IsIntegerSampler(textureFunction->sampler)) |
| 999 | { |
| 1000 | out << "x.Load("; |
| 1001 | } |
| 1002 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1003 | { |
| 1004 | out << "x.SampleCmpLevelZero(s, "; |
| 1005 | } |
| 1006 | else |
| 1007 | { |
| 1008 | out << "x.SampleGrad(s, "; |
| 1009 | } |
| 1010 | } |
| 1011 | else if (IsIntegerSampler(textureFunction->sampler) || |
| 1012 | textureFunction->method == TextureFunction::FETCH) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1013 | { |
| 1014 | out << "x.Load("; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1015 | } |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1016 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1017 | { |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1018 | switch(textureFunction->method) |
| 1019 | { |
| 1020 | case TextureFunction::IMPLICIT: out << "x.SampleCmp(s, "; break; |
| 1021 | case TextureFunction::BIAS: out << "x.SampleCmp(s, "; break; |
| 1022 | case TextureFunction::LOD: out << "x.SampleCmp(s, "; break; |
| 1023 | case TextureFunction::LOD0: out << "x.SampleCmpLevelZero(s, "; break; |
| 1024 | case TextureFunction::LOD0BIAS: out << "x.SampleCmpLevelZero(s, "; break; |
| 1025 | default: UNREACHABLE(); |
| 1026 | } |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1027 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1028 | else |
| 1029 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1030 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1031 | { |
| 1032 | case TextureFunction::IMPLICIT: out << "x.Sample(s, "; break; |
| 1033 | case TextureFunction::BIAS: out << "x.SampleBias(s, "; break; |
| 1034 | case TextureFunction::LOD: out << "x.SampleLevel(s, "; break; |
| 1035 | case TextureFunction::LOD0: out << "x.SampleLevel(s, "; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1036 | case TextureFunction::LOD0BIAS: out << "x.SampleLevel(s, "; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1037 | default: UNREACHABLE(); |
| 1038 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1039 | } |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1040 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1041 | else UNREACHABLE(); |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1042 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1043 | // Integer sampling requires integer addresses |
| 1044 | TString addressx = ""; |
| 1045 | TString addressy = ""; |
| 1046 | TString addressz = ""; |
| 1047 | TString close = ""; |
| 1048 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1049 | if (IsIntegerSampler(textureFunction->sampler) || |
| 1050 | textureFunction->method == TextureFunction::FETCH) |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1051 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1052 | switch(hlslCoords) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1053 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1054 | case 2: out << "int3("; break; |
| 1055 | case 3: out << "int4("; break; |
| 1056 | default: UNREACHABLE(); |
| 1057 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1058 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1059 | // Convert from normalized floating-point to integer |
| 1060 | if (textureFunction->method != TextureFunction::FETCH) |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 1061 | { |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1062 | addressx = "int(floor(width * frac(("; |
| 1063 | addressy = "int(floor(height * frac(("; |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 1064 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1065 | if (IsSamplerArray(textureFunction->sampler)) |
| 1066 | { |
| 1067 | addressz = "int(max(0, min(layers - 1, floor(0.5 + "; |
| 1068 | } |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1069 | else if (IsSamplerCube(textureFunction->sampler)) |
| 1070 | { |
| 1071 | addressz = "(((("; |
| 1072 | } |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1073 | else |
| 1074 | { |
| 1075 | addressz = "int(floor(depth * frac(("; |
| 1076 | } |
| 1077 | |
| 1078 | close = "))))"; |
| 1079 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1080 | } |
| 1081 | else |
| 1082 | { |
| 1083 | switch(hlslCoords) |
| 1084 | { |
| 1085 | case 2: out << "float2("; break; |
| 1086 | case 3: out << "float3("; break; |
| 1087 | case 4: out << "float4("; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1088 | default: UNREACHABLE(); |
| 1089 | } |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1090 | } |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1091 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1092 | TString proj = ""; // Only used for projected textures |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1093 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1094 | if (textureFunction->proj) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1095 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1096 | switch(textureFunction->coords) |
| 1097 | { |
| 1098 | case 3: proj = " / t.z"; break; |
| 1099 | case 4: proj = " / t.w"; break; |
| 1100 | default: UNREACHABLE(); |
| 1101 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1102 | } |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 1103 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1104 | out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1105 | |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1106 | if (mOutputType == SH_HLSL9_OUTPUT) |
| 1107 | { |
| 1108 | if (hlslCoords >= 3) |
| 1109 | { |
| 1110 | if (textureFunction->coords < 3) |
| 1111 | { |
| 1112 | out << ", 0"; |
| 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | out << ", t.z" + proj; |
| 1117 | } |
| 1118 | } |
| 1119 | |
| 1120 | if (hlslCoords == 4) |
| 1121 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1122 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1123 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1124 | case TextureFunction::BIAS: out << ", bias"; break; |
| 1125 | case TextureFunction::LOD: out << ", lod"; break; |
| 1126 | case TextureFunction::LOD0: out << ", 0"; break; |
| 1127 | case TextureFunction::LOD0BIAS: out << ", bias"; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1128 | default: UNREACHABLE(); |
| 1129 | } |
| 1130 | } |
| 1131 | |
| 1132 | out << "));\n"; |
| 1133 | } |
| 1134 | else if (mOutputType == SH_HLSL11_OUTPUT) |
| 1135 | { |
| 1136 | if (hlslCoords >= 3) |
| 1137 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1138 | if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler)) |
| 1139 | { |
| 1140 | out << ", face"; |
| 1141 | } |
| 1142 | else |
| 1143 | { |
| 1144 | out << ", " + addressz + ("t.z" + proj) + close; |
| 1145 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1146 | } |
| 1147 | |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1148 | if (textureFunction->method == TextureFunction::GRAD) |
| 1149 | { |
| 1150 | if (IsIntegerSampler(textureFunction->sampler)) |
| 1151 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1152 | out << ", mip)"; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1153 | } |
| 1154 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1155 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1156 | // Compare value |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1157 | if (textureFunction->proj) |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1158 | { |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1159 | // According to ESSL 3.00.4 sec 8.8 p95 on textureProj: |
| 1160 | // The resulting third component of P' in the shadow forms is used as Dref |
| 1161 | out << "), t.z" << proj; |
| 1162 | } |
| 1163 | else |
| 1164 | { |
| 1165 | switch(textureFunction->coords) |
| 1166 | { |
| 1167 | case 3: out << "), t.z"; break; |
| 1168 | case 4: out << "), t.w"; break; |
| 1169 | default: UNREACHABLE(); |
| 1170 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1171 | } |
| 1172 | } |
| 1173 | else |
| 1174 | { |
| 1175 | out << "), ddx, ddy"; |
| 1176 | } |
| 1177 | } |
| 1178 | else if (IsIntegerSampler(textureFunction->sampler) || |
| 1179 | textureFunction->method == TextureFunction::FETCH) |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1180 | { |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1181 | out << ", mip)"; |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1182 | } |
| 1183 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1184 | { |
| 1185 | // Compare value |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1186 | if (textureFunction->proj) |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1187 | { |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1188 | // According to ESSL 3.00.4 sec 8.8 p95 on textureProj: |
| 1189 | // The resulting third component of P' in the shadow forms is used as Dref |
| 1190 | out << "), t.z" << proj; |
| 1191 | } |
| 1192 | else |
| 1193 | { |
| 1194 | switch(textureFunction->coords) |
| 1195 | { |
| 1196 | case 3: out << "), t.z"; break; |
| 1197 | case 4: out << "), t.w"; break; |
| 1198 | default: UNREACHABLE(); |
| 1199 | } |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1200 | } |
| 1201 | } |
| 1202 | else |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1203 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1204 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1205 | { |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1206 | case TextureFunction::IMPLICIT: out << ")"; break; |
| 1207 | case TextureFunction::BIAS: out << "), bias"; break; |
| 1208 | case TextureFunction::LOD: out << "), lod"; break; |
| 1209 | case TextureFunction::LOD0: out << "), 0"; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1210 | case TextureFunction::LOD0BIAS: out << "), bias"; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1211 | default: UNREACHABLE(); |
| 1212 | } |
| 1213 | } |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1214 | |
| 1215 | if (textureFunction->offset) |
| 1216 | { |
| 1217 | out << ", offset"; |
| 1218 | } |
| 1219 | |
| 1220 | out << ");"; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1221 | } |
| 1222 | else UNREACHABLE(); |
| 1223 | } |
| 1224 | |
| 1225 | out << "\n" |
| 1226 | "}\n" |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1227 | "\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 1230 | if (mUsesFragCoord) |
| 1231 | { |
| 1232 | out << "#define GL_USES_FRAG_COORD\n"; |
| 1233 | } |
| 1234 | |
| 1235 | if (mUsesPointCoord) |
| 1236 | { |
| 1237 | out << "#define GL_USES_POINT_COORD\n"; |
| 1238 | } |
| 1239 | |
| 1240 | if (mUsesFrontFacing) |
| 1241 | { |
| 1242 | out << "#define GL_USES_FRONT_FACING\n"; |
| 1243 | } |
| 1244 | |
| 1245 | if (mUsesPointSize) |
| 1246 | { |
| 1247 | out << "#define GL_USES_POINT_SIZE\n"; |
| 1248 | } |
| 1249 | |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 1250 | if (mUsesFragDepth) |
| 1251 | { |
| 1252 | out << "#define GL_USES_FRAG_DEPTH\n"; |
| 1253 | } |
| 1254 | |
shannonwoods@chromium.org | 0329988 | 2013-05-30 00:05:26 +0000 | [diff] [blame] | 1255 | if (mUsesDepthRange) |
| 1256 | { |
| 1257 | out << "#define GL_USES_DEPTH_RANGE\n"; |
| 1258 | } |
| 1259 | |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 1260 | if (mUsesXor) |
| 1261 | { |
| 1262 | out << "bool xor(bool p, bool q)\n" |
| 1263 | "{\n" |
| 1264 | " return (p || q) && !(p && q);\n" |
| 1265 | "}\n" |
| 1266 | "\n"; |
| 1267 | } |
| 1268 | |
Olli Etuaho | 80a5a6c | 2015-01-12 15:35:27 +0200 | [diff] [blame] | 1269 | builtInFunctionEmulator->OutputEmulatedFunctionDefinition(out); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
| 1272 | void OutputHLSL::visitSymbol(TIntermSymbol *node) |
| 1273 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1274 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1275 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 1276 | // Handle accessing std140 structs by value |
| 1277 | if (mFlaggedStructMappedNames.count(node) > 0) |
| 1278 | { |
| 1279 | out << mFlaggedStructMappedNames[node]; |
| 1280 | return; |
| 1281 | } |
| 1282 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1283 | TString name = node->getSymbol(); |
| 1284 | |
shannon.woods%transgaming.com@gtempaccount.com | e7d4a24 | 2013-04-13 03:38:33 +0000 | [diff] [blame] | 1285 | if (name == "gl_DepthRange") |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 1286 | { |
| 1287 | mUsesDepthRange = true; |
| 1288 | out << name; |
| 1289 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1290 | else |
| 1291 | { |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1292 | TQualifier qualifier = node->getQualifier(); |
| 1293 | |
| 1294 | if (qualifier == EvqUniform) |
| 1295 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1296 | const TType& nodeType = node->getType(); |
| 1297 | const TInterfaceBlock* interfaceBlock = nodeType.getInterfaceBlock(); |
| 1298 | |
| 1299 | if (interfaceBlock) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1300 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1301 | mReferencedInterfaceBlocks[interfaceBlock->name()] = node; |
shannonwoods@chromium.org | 4430b0d | 2013-05-30 00:12:34 +0000 | [diff] [blame] | 1302 | } |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1303 | else |
| 1304 | { |
| 1305 | mReferencedUniforms[name] = node; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1306 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1307 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1308 | out << DecorateUniform(name, nodeType); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1309 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1310 | else if (qualifier == EvqAttribute || qualifier == EvqVertexIn) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1311 | { |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 1312 | mReferencedAttributes[name] = node; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1313 | out << Decorate(name); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1314 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1315 | else if (IsVarying(qualifier)) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1316 | { |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 1317 | mReferencedVaryings[name] = node; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1318 | out << Decorate(name); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1319 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1320 | else if (qualifier == EvqFragmentOut) |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 1321 | { |
| 1322 | mReferencedOutputVariables[name] = node; |
| 1323 | out << "out_" << name; |
| 1324 | } |
| 1325 | else if (qualifier == EvqFragColor) |
shannon.woods%transgaming.com@gtempaccount.com | e7d4a24 | 2013-04-13 03:38:33 +0000 | [diff] [blame] | 1326 | { |
| 1327 | out << "gl_Color[0]"; |
| 1328 | mUsesFragColor = true; |
| 1329 | } |
| 1330 | else if (qualifier == EvqFragData) |
| 1331 | { |
| 1332 | out << "gl_Color"; |
| 1333 | mUsesFragData = true; |
| 1334 | } |
| 1335 | else if (qualifier == EvqFragCoord) |
| 1336 | { |
| 1337 | mUsesFragCoord = true; |
| 1338 | out << name; |
| 1339 | } |
| 1340 | else if (qualifier == EvqPointCoord) |
| 1341 | { |
| 1342 | mUsesPointCoord = true; |
| 1343 | out << name; |
| 1344 | } |
| 1345 | else if (qualifier == EvqFrontFacing) |
| 1346 | { |
| 1347 | mUsesFrontFacing = true; |
| 1348 | out << name; |
| 1349 | } |
| 1350 | else if (qualifier == EvqPointSize) |
| 1351 | { |
| 1352 | mUsesPointSize = true; |
| 1353 | out << name; |
| 1354 | } |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 1355 | else if (qualifier == EvqInstanceID) |
| 1356 | { |
| 1357 | mUsesInstanceID = true; |
| 1358 | out << name; |
| 1359 | } |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 1360 | else if (name == "gl_FragDepthEXT") |
| 1361 | { |
| 1362 | mUsesFragDepth = true; |
| 1363 | out << "gl_Depth"; |
| 1364 | } |
Jamie Madill | e53c98b | 2014-02-03 11:57:13 -0500 | [diff] [blame] | 1365 | else if (qualifier == EvqInternal) |
| 1366 | { |
| 1367 | out << name; |
| 1368 | } |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1369 | else |
| 1370 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1371 | out << Decorate(name); |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1372 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 1376 | void OutputHLSL::visitRaw(TIntermRaw *node) |
| 1377 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1378 | getInfoSink() << node->getRawText(); |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 1379 | } |
| 1380 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1381 | bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node) |
| 1382 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1383 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1384 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 1385 | // Handle accessing std140 structs by value |
| 1386 | if (mFlaggedStructMappedNames.count(node) > 0) |
| 1387 | { |
| 1388 | out << mFlaggedStructMappedNames[node]; |
| 1389 | return false; |
| 1390 | } |
| 1391 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1392 | switch (node->getOp()) |
| 1393 | { |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1394 | case EOpAssign: outputTriplet(visit, "(", " = ", ")"); break; |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1395 | case EOpInitialize: |
| 1396 | if (visit == PreVisit) |
| 1397 | { |
| 1398 | // GLSL allows to write things like "float x = x;" where a new variable x is defined |
| 1399 | // and the value of an existing variable x is assigned. HLSL uses C semantics (the |
| 1400 | // new variable is created before the assignment is evaluated), so we need to convert |
| 1401 | // this to "float t = x, x = t;". |
| 1402 | |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1403 | TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode(); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1404 | ASSERT(symbolNode); |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1405 | TIntermTyped *expression = node->getRight(); |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1406 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1407 | // TODO (jmadill): do a 'deep' scan to know if an expression is statically const |
| 1408 | if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst) |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1409 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1410 | // For variables which are not constant, defer their real initialization until |
| 1411 | // after we initialize other globals: uniforms, attributes and varyings. |
| 1412 | mDeferredGlobalInitializers.push_back(std::make_pair(symbolNode, expression)); |
| 1413 | const TString &initString = initializer(node->getType()); |
| 1414 | node->setRight(new TIntermRaw(node->getType(), initString)); |
| 1415 | } |
| 1416 | else if (writeSameSymbolInitializer(out, symbolNode, expression)) |
| 1417 | { |
| 1418 | // Skip initializing the rest of the expression |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1419 | return false; |
| 1420 | } |
| 1421 | } |
| 1422 | else if (visit == InVisit) |
| 1423 | { |
| 1424 | out << " = "; |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1425 | } |
| 1426 | break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1427 | case EOpAddAssign: outputTriplet(visit, "(", " += ", ")"); break; |
| 1428 | case EOpSubAssign: outputTriplet(visit, "(", " -= ", ")"); break; |
| 1429 | case EOpMulAssign: outputTriplet(visit, "(", " *= ", ")"); break; |
| 1430 | case EOpVectorTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break; |
| 1431 | case EOpMatrixTimesScalarAssign: outputTriplet(visit, "(", " *= ", ")"); break; |
| 1432 | case EOpVectorTimesMatrixAssign: |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1433 | if (visit == PreVisit) |
| 1434 | { |
| 1435 | out << "("; |
| 1436 | } |
| 1437 | else if (visit == InVisit) |
| 1438 | { |
| 1439 | out << " = mul("; |
| 1440 | node->getLeft()->traverse(this); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1441 | out << ", transpose("; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1442 | } |
| 1443 | else |
| 1444 | { |
daniel@transgaming.com | 3aa7420 | 2010-04-29 03:39:04 +0000 | [diff] [blame] | 1445 | out << ")))"; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1446 | } |
| 1447 | break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1448 | case EOpMatrixTimesMatrixAssign: |
| 1449 | if (visit == PreVisit) |
| 1450 | { |
| 1451 | out << "("; |
| 1452 | } |
| 1453 | else if (visit == InVisit) |
| 1454 | { |
| 1455 | out << " = mul("; |
| 1456 | node->getLeft()->traverse(this); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1457 | out << ", "; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1458 | } |
| 1459 | else |
| 1460 | { |
daniel@transgaming.com | 3aa7420 | 2010-04-29 03:39:04 +0000 | [diff] [blame] | 1461 | out << "))"; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1462 | } |
| 1463 | break; |
| 1464 | case EOpDivAssign: outputTriplet(visit, "(", " /= ", ")"); break; |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame] | 1465 | case EOpIModAssign: outputTriplet(visit, "(", " %= ", ")"); break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1466 | case EOpBitShiftLeftAssign: outputTriplet(visit, "(", " <<= ", ")"); break; |
| 1467 | case EOpBitShiftRightAssign: outputTriplet(visit, "(", " >>= ", ")"); break; |
| 1468 | case EOpBitwiseAndAssign: outputTriplet(visit, "(", " &= ", ")"); break; |
| 1469 | case EOpBitwiseXorAssign: outputTriplet(visit, "(", " ^= ", ")"); break; |
| 1470 | case EOpBitwiseOrAssign: outputTriplet(visit, "(", " |= ", ")"); break; |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1471 | case EOpIndexDirect: |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1472 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1473 | const TType& leftType = node->getLeft()->getType(); |
| 1474 | if (leftType.isInterfaceBlock()) |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1475 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1476 | if (visit == PreVisit) |
| 1477 | { |
| 1478 | TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock(); |
| 1479 | const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1480 | mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1481 | out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1482 | return false; |
| 1483 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1484 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1485 | else |
| 1486 | { |
| 1487 | outputTriplet(visit, "", "[", "]"); |
| 1488 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1489 | } |
| 1490 | break; |
| 1491 | case EOpIndexIndirect: |
| 1492 | // We do not currently support indirect references to interface blocks |
| 1493 | ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock); |
| 1494 | outputTriplet(visit, "", "[", "]"); |
| 1495 | break; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1496 | case EOpIndexDirectStruct: |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1497 | if (visit == InVisit) |
| 1498 | { |
| 1499 | const TStructure* structure = node->getLeft()->getType().getStruct(); |
| 1500 | const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); |
| 1501 | const TField* field = structure->fields()[index->getIConst(0)]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1502 | out << "." + DecorateField(field->name(), *structure); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1503 | |
| 1504 | return false; |
| 1505 | } |
| 1506 | break; |
shannonwoods@chromium.org | 4430b0d | 2013-05-30 00:12:34 +0000 | [diff] [blame] | 1507 | case EOpIndexDirectInterfaceBlock: |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1508 | if (visit == InVisit) |
| 1509 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1510 | const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock(); |
| 1511 | const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); |
| 1512 | const TField* field = interfaceBlock->fields()[index->getIConst(0)]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1513 | out << "." + Decorate(field->name()); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1514 | |
| 1515 | return false; |
| 1516 | } |
| 1517 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1518 | case EOpVectorSwizzle: |
| 1519 | if (visit == InVisit) |
| 1520 | { |
| 1521 | out << "."; |
| 1522 | |
| 1523 | TIntermAggregate *swizzle = node->getRight()->getAsAggregate(); |
| 1524 | |
| 1525 | if (swizzle) |
| 1526 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1527 | TIntermSequence *sequence = swizzle->getSequence(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1528 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1529 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1530 | { |
| 1531 | TIntermConstantUnion *element = (*sit)->getAsConstantUnion(); |
| 1532 | |
| 1533 | if (element) |
| 1534 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 1535 | int i = element->getIConst(0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1536 | |
| 1537 | switch (i) |
| 1538 | { |
| 1539 | case 0: out << "x"; break; |
| 1540 | case 1: out << "y"; break; |
| 1541 | case 2: out << "z"; break; |
| 1542 | case 3: out << "w"; break; |
| 1543 | default: UNREACHABLE(); |
| 1544 | } |
| 1545 | } |
| 1546 | else UNREACHABLE(); |
| 1547 | } |
| 1548 | } |
| 1549 | else UNREACHABLE(); |
| 1550 | |
| 1551 | return false; // Fully processed |
| 1552 | } |
| 1553 | break; |
| 1554 | case EOpAdd: outputTriplet(visit, "(", " + ", ")"); break; |
| 1555 | case EOpSub: outputTriplet(visit, "(", " - ", ")"); break; |
| 1556 | case EOpMul: outputTriplet(visit, "(", " * ", ")"); break; |
| 1557 | case EOpDiv: outputTriplet(visit, "(", " / ", ")"); break; |
Olli Etuaho | ff805cc | 2015-02-13 10:59:34 +0200 | [diff] [blame] | 1558 | case EOpIMod: outputTriplet(visit, "(", " % ", ")"); break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1559 | case EOpBitShiftLeft: outputTriplet(visit, "(", " << ", ")"); break; |
| 1560 | case EOpBitShiftRight: outputTriplet(visit, "(", " >> ", ")"); break; |
| 1561 | case EOpBitwiseAnd: outputTriplet(visit, "(", " & ", ")"); break; |
| 1562 | case EOpBitwiseXor: outputTriplet(visit, "(", " ^ ", ")"); break; |
| 1563 | case EOpBitwiseOr: outputTriplet(visit, "(", " | ", ")"); break; |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1564 | case EOpEqual: |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1565 | case EOpNotEqual: |
daniel@transgaming.com | d91cfe7 | 2010-04-13 03:26:17 +0000 | [diff] [blame] | 1566 | if (node->getLeft()->isScalar()) |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1567 | { |
daniel@transgaming.com | d91cfe7 | 2010-04-13 03:26:17 +0000 | [diff] [blame] | 1568 | if (node->getOp() == EOpEqual) |
| 1569 | { |
| 1570 | outputTriplet(visit, "(", " == ", ")"); |
| 1571 | } |
| 1572 | else |
| 1573 | { |
| 1574 | outputTriplet(visit, "(", " != ", ")"); |
| 1575 | } |
| 1576 | } |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1577 | else |
| 1578 | { |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 1579 | if (visit == PreVisit && node->getOp() == EOpNotEqual) |
daniel@transgaming.com | d91cfe7 | 2010-04-13 03:26:17 +0000 | [diff] [blame] | 1580 | { |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 1581 | out << "!"; |
| 1582 | } |
| 1583 | |
| 1584 | if (node->getLeft()->getBasicType() == EbtStruct) |
| 1585 | { |
| 1586 | const TStructure &structure = *node->getLeft()->getType().getStruct(); |
| 1587 | const TString &functionName = addStructEqualityFunction(structure); |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 1588 | outputTriplet(visit, (functionName + "(").c_str(), ", ", ")"); |
daniel@transgaming.com | d91cfe7 | 2010-04-13 03:26:17 +0000 | [diff] [blame] | 1589 | } |
| 1590 | else |
| 1591 | { |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 1592 | ASSERT(node->getLeft()->isMatrix() || node->getLeft()->isVector()); |
| 1593 | outputTriplet(visit, "all(", " == ", ")"); |
daniel@transgaming.com | d91cfe7 | 2010-04-13 03:26:17 +0000 | [diff] [blame] | 1594 | } |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 1595 | } |
| 1596 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1597 | case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break; |
| 1598 | case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break; |
| 1599 | case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break; |
| 1600 | case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break; |
| 1601 | case EOpVectorTimesScalar: outputTriplet(visit, "(", " * ", ")"); break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1602 | case EOpMatrixTimesScalar: outputTriplet(visit, "(", " * ", ")"); break; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1603 | case EOpVectorTimesMatrix: outputTriplet(visit, "mul(", ", transpose(", "))"); break; |
| 1604 | case EOpMatrixTimesVector: outputTriplet(visit, "mul(transpose(", "), ", ")"); break; |
daniel@transgaming.com | 69f084b | 2010-04-23 18:34:46 +0000 | [diff] [blame] | 1605 | case EOpMatrixTimesMatrix: outputTriplet(visit, "transpose(mul(transpose(", "), transpose(", ")))"); break; |
daniel@transgaming.com | 8915eba | 2012-04-28 00:34:44 +0000 | [diff] [blame] | 1606 | case EOpLogicalOr: |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 1607 | if (node->getRight()->hasSideEffects()) |
| 1608 | { |
| 1609 | out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); |
| 1610 | return false; |
| 1611 | } |
| 1612 | else |
| 1613 | { |
| 1614 | outputTriplet(visit, "(", " || ", ")"); |
| 1615 | return true; |
| 1616 | } |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 1617 | case EOpLogicalXor: |
| 1618 | mUsesXor = true; |
| 1619 | outputTriplet(visit, "xor(", ", ", ")"); |
| 1620 | break; |
daniel@transgaming.com | 8915eba | 2012-04-28 00:34:44 +0000 | [diff] [blame] | 1621 | case EOpLogicalAnd: |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 1622 | if (node->getRight()->hasSideEffects()) |
| 1623 | { |
| 1624 | out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); |
| 1625 | return false; |
| 1626 | } |
| 1627 | else |
| 1628 | { |
| 1629 | outputTriplet(visit, "(", " && ", ")"); |
| 1630 | return true; |
| 1631 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1632 | default: UNREACHABLE(); |
| 1633 | } |
| 1634 | |
| 1635 | return true; |
| 1636 | } |
| 1637 | |
| 1638 | bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node) |
| 1639 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1640 | switch (node->getOp()) |
| 1641 | { |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 1642 | case EOpNegative: outputTriplet(visit, "(-", "", ")"); break; |
Zhenyao Mo | de1e00e | 2014-10-09 16:55:32 -0700 | [diff] [blame] | 1643 | case EOpPositive: outputTriplet(visit, "(+", "", ")"); break; |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 1644 | case EOpVectorLogicalNot: outputTriplet(visit, "(!", "", ")"); break; |
| 1645 | case EOpLogicalNot: outputTriplet(visit, "(!", "", ")"); break; |
Olli Etuaho | 31b5fc6 | 2015-01-16 12:13:36 +0200 | [diff] [blame] | 1646 | case EOpBitwiseNot: outputTriplet(visit, "(~", "", ")"); break; |
Nicolas Capens | 16004fc | 2014-06-11 11:29:11 -0400 | [diff] [blame] | 1647 | case EOpPostIncrement: outputTriplet(visit, "(", "", "++)"); break; |
| 1648 | case EOpPostDecrement: outputTriplet(visit, "(", "", "--)"); break; |
| 1649 | case EOpPreIncrement: outputTriplet(visit, "(++", "", ")"); break; |
| 1650 | case EOpPreDecrement: outputTriplet(visit, "(--", "", ")"); break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1651 | case EOpRadians: outputTriplet(visit, "radians(", "", ")"); break; |
| 1652 | case EOpDegrees: outputTriplet(visit, "degrees(", "", ")"); break; |
| 1653 | case EOpSin: outputTriplet(visit, "sin(", "", ")"); break; |
| 1654 | case EOpCos: outputTriplet(visit, "cos(", "", ")"); break; |
| 1655 | case EOpTan: outputTriplet(visit, "tan(", "", ")"); break; |
| 1656 | case EOpAsin: outputTriplet(visit, "asin(", "", ")"); break; |
| 1657 | case EOpAcos: outputTriplet(visit, "acos(", "", ")"); break; |
| 1658 | case EOpAtan: outputTriplet(visit, "atan(", "", ")"); break; |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 1659 | case EOpSinh: outputTriplet(visit, "sinh(", "", ")"); break; |
| 1660 | case EOpCosh: outputTriplet(visit, "cosh(", "", ")"); break; |
| 1661 | case EOpTanh: outputTriplet(visit, "tanh(", "", ")"); break; |
| 1662 | case EOpAsinh: |
| 1663 | ASSERT(node->getUseEmulatedFunction()); |
| 1664 | writeEmulatedFunctionTriplet(visit, "asinh("); |
| 1665 | break; |
| 1666 | case EOpAcosh: |
| 1667 | ASSERT(node->getUseEmulatedFunction()); |
| 1668 | writeEmulatedFunctionTriplet(visit, "acosh("); |
| 1669 | break; |
| 1670 | case EOpAtanh: |
| 1671 | ASSERT(node->getUseEmulatedFunction()); |
| 1672 | writeEmulatedFunctionTriplet(visit, "atanh("); |
| 1673 | break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1674 | case EOpExp: outputTriplet(visit, "exp(", "", ")"); break; |
| 1675 | case EOpLog: outputTriplet(visit, "log(", "", ")"); break; |
| 1676 | case EOpExp2: outputTriplet(visit, "exp2(", "", ")"); break; |
| 1677 | case EOpLog2: outputTriplet(visit, "log2(", "", ")"); break; |
| 1678 | case EOpSqrt: outputTriplet(visit, "sqrt(", "", ")"); break; |
| 1679 | case EOpInverseSqrt: outputTriplet(visit, "rsqrt(", "", ")"); break; |
| 1680 | case EOpAbs: outputTriplet(visit, "abs(", "", ")"); break; |
| 1681 | case EOpSign: outputTriplet(visit, "sign(", "", ")"); break; |
| 1682 | case EOpFloor: outputTriplet(visit, "floor(", "", ")"); break; |
Qingqing Deng | 5dbece5 | 2015-02-27 20:35:38 -0800 | [diff] [blame^] | 1683 | case EOpTrunc: outputTriplet(visit, "trunc(", "", ")"); break; |
| 1684 | case EOpRound: outputTriplet(visit, "round(", "", ")"); break; |
| 1685 | case EOpRoundEven: |
| 1686 | ASSERT(node->getUseEmulatedFunction()); |
| 1687 | writeEmulatedFunctionTriplet(visit, "roundEven("); |
| 1688 | break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1689 | case EOpCeil: outputTriplet(visit, "ceil(", "", ")"); break; |
| 1690 | case EOpFract: outputTriplet(visit, "frac(", "", ")"); break; |
Arun Patole | 0c1726e | 2015-02-18 14:35:02 +0530 | [diff] [blame] | 1691 | case EOpIsNan: outputTriplet(visit, "isnan(", "", ")"); break; |
| 1692 | case EOpIsInf: outputTriplet(visit, "isinf(", "", ")"); break; |
Olli Etuaho | e8d2c07 | 2015-01-08 16:33:54 +0200 | [diff] [blame] | 1693 | case EOpFloatBitsToInt: outputTriplet(visit, "asint(", "", ")"); break; |
| 1694 | case EOpFloatBitsToUint: outputTriplet(visit, "asuint(", "", ")"); break; |
| 1695 | case EOpIntBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break; |
| 1696 | case EOpUintBitsToFloat: outputTriplet(visit, "asfloat(", "", ")"); break; |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 1697 | case EOpPackSnorm2x16: |
| 1698 | ASSERT(node->getUseEmulatedFunction()); |
| 1699 | writeEmulatedFunctionTriplet(visit, "packSnorm2x16("); |
| 1700 | break; |
| 1701 | case EOpPackUnorm2x16: |
| 1702 | ASSERT(node->getUseEmulatedFunction()); |
| 1703 | writeEmulatedFunctionTriplet(visit, "packUnorm2x16("); |
| 1704 | break; |
| 1705 | case EOpPackHalf2x16: |
| 1706 | ASSERT(node->getUseEmulatedFunction()); |
| 1707 | writeEmulatedFunctionTriplet(visit, "packHalf2x16("); |
| 1708 | break; |
| 1709 | case EOpUnpackSnorm2x16: |
| 1710 | ASSERT(node->getUseEmulatedFunction()); |
| 1711 | writeEmulatedFunctionTriplet(visit, "unpackSnorm2x16("); |
| 1712 | break; |
| 1713 | case EOpUnpackUnorm2x16: |
| 1714 | ASSERT(node->getUseEmulatedFunction()); |
| 1715 | writeEmulatedFunctionTriplet(visit, "unpackUnorm2x16("); |
| 1716 | break; |
| 1717 | case EOpUnpackHalf2x16: |
| 1718 | ASSERT(node->getUseEmulatedFunction()); |
| 1719 | writeEmulatedFunctionTriplet(visit, "unpackHalf2x16("); |
| 1720 | break; |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1721 | case EOpLength: outputTriplet(visit, "length(", "", ")"); break; |
| 1722 | case EOpNormalize: outputTriplet(visit, "normalize(", "", ")"); break; |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 1723 | case EOpDFdx: |
| 1724 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1725 | { |
| 1726 | outputTriplet(visit, "(", "", ", 0.0)"); |
| 1727 | } |
| 1728 | else |
| 1729 | { |
| 1730 | outputTriplet(visit, "ddx(", "", ")"); |
| 1731 | } |
| 1732 | break; |
| 1733 | case EOpDFdy: |
| 1734 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1735 | { |
| 1736 | outputTriplet(visit, "(", "", ", 0.0)"); |
| 1737 | } |
| 1738 | else |
| 1739 | { |
apatrick@chromium.org | 9616e58 | 2012-06-22 18:27:01 +0000 | [diff] [blame] | 1740 | outputTriplet(visit, "ddy(", "", ")"); |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 1741 | } |
| 1742 | break; |
| 1743 | case EOpFwidth: |
| 1744 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1745 | { |
| 1746 | outputTriplet(visit, "(", "", ", 0.0)"); |
| 1747 | } |
| 1748 | else |
| 1749 | { |
| 1750 | outputTriplet(visit, "fwidth(", "", ")"); |
| 1751 | } |
| 1752 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 1753 | case EOpTranspose: outputTriplet(visit, "transpose(", "", ")"); break; |
| 1754 | case EOpDeterminant: outputTriplet(visit, "determinant(transpose(", "", "))"); break; |
Olli Etuaho | abf6dad | 2015-01-14 14:45:16 +0200 | [diff] [blame] | 1755 | case EOpInverse: |
| 1756 | ASSERT(node->getUseEmulatedFunction()); |
| 1757 | writeEmulatedFunctionTriplet(visit, "inverse("); |
| 1758 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 1759 | |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 1760 | case EOpAny: outputTriplet(visit, "any(", "", ")"); break; |
| 1761 | case EOpAll: outputTriplet(visit, "all(", "", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1762 | default: UNREACHABLE(); |
| 1763 | } |
| 1764 | |
| 1765 | return true; |
| 1766 | } |
| 1767 | |
| 1768 | bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node) |
| 1769 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1770 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1771 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1772 | switch (node->getOp()) |
| 1773 | { |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1774 | case EOpSequence: |
| 1775 | { |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1776 | if (mInsideFunction) |
| 1777 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 1778 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1779 | out << "{\n"; |
| 1780 | } |
| 1781 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1782 | for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++) |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1783 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 1784 | outputLineDirective((*sit)->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 1785 | |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 1786 | traverseStatements(*sit); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1787 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 1788 | // Don't output ; after case labels, they're terminated by : |
| 1789 | // This is needed especially since outputting a ; after a case statement would turn empty |
| 1790 | // case statements into non-empty case statements, disallowing fall-through from them. |
| 1791 | if ((*sit)->getAsCaseNode() == nullptr) |
| 1792 | out << ";\n"; |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1793 | } |
| 1794 | |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1795 | if (mInsideFunction) |
| 1796 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 1797 | outputLineDirective(node->getLine().last_line); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1798 | out << "}\n"; |
| 1799 | } |
| 1800 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 1801 | return false; |
| 1802 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1803 | case EOpDeclaration: |
| 1804 | if (visit == PreVisit) |
| 1805 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1806 | TIntermSequence *sequence = node->getSequence(); |
| 1807 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1808 | |
daniel@transgaming.com | d25ab25 | 2010-03-30 03:36:26 +0000 | [diff] [blame] | 1809 | if (variable && (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1810 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 1811 | TStructure *structure = variable->getType().getStruct(); |
| 1812 | |
| 1813 | if (structure) |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 1814 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 1815 | mStructureHLSL->addConstructor(variable->getType(), StructNameString(*structure), NULL); |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1818 | if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1819 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1820 | for (const auto &seqElement : *sequence) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1821 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1822 | if (isSingleStatement(seqElement)) |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 1823 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1824 | mUnfoldShortCircuit->traverse(seqElement); |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 1825 | } |
| 1826 | |
Nicolas Capens | d974db4 | 2014-10-07 10:50:19 -0400 | [diff] [blame] | 1827 | if (!mInsideFunction) |
| 1828 | { |
| 1829 | out << "static "; |
| 1830 | } |
| 1831 | |
| 1832 | out << TypeString(variable->getType()) + " "; |
| 1833 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1834 | TIntermSymbol *symbol = seqElement->getAsSymbolNode(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1835 | |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1836 | if (symbol) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1837 | { |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1838 | symbol->traverse(this); |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1839 | out << ArrayString(symbol->getType()); |
Jamie Madill | 79bb0d9 | 2013-12-09 16:20:28 -0500 | [diff] [blame] | 1840 | out << " = " + initializer(symbol->getType()); |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1841 | } |
| 1842 | else |
| 1843 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1844 | seqElement->traverse(this); |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1847 | if (seqElement != sequence->back()) |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1848 | { |
Nicolas Capens | d974db4 | 2014-10-07 10:50:19 -0400 | [diff] [blame] | 1849 | out << ";\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1850 | } |
| 1851 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1852 | } |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1853 | else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration |
| 1854 | { |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 1855 | // Already added to constructor map |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 1856 | } |
| 1857 | else UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1858 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1859 | else if (variable && IsVaryingOut(variable->getQualifier())) |
shannon.woods@transgaming.com | cb332ab | 2013-02-28 23:12:18 +0000 | [diff] [blame] | 1860 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1861 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++) |
shannon.woods@transgaming.com | cb332ab | 2013-02-28 23:12:18 +0000 | [diff] [blame] | 1862 | { |
| 1863 | TIntermSymbol *symbol = (*sit)->getAsSymbolNode(); |
| 1864 | |
| 1865 | if (symbol) |
| 1866 | { |
| 1867 | // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking |
| 1868 | mReferencedVaryings[symbol->getSymbol()] = symbol; |
| 1869 | } |
| 1870 | else |
| 1871 | { |
| 1872 | (*sit)->traverse(this); |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1877 | return false; |
| 1878 | } |
| 1879 | else if (visit == InVisit) |
| 1880 | { |
| 1881 | out << ", "; |
| 1882 | } |
| 1883 | break; |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 1884 | case EOpInvariantDeclaration: |
| 1885 | // Do not do any translation |
| 1886 | return false; |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1887 | case EOpPrototype: |
| 1888 | if (visit == PreVisit) |
| 1889 | { |
Olli Etuaho | 76acee8 | 2014-11-04 13:44:03 +0200 | [diff] [blame] | 1890 | out << TypeString(node->getType()) << " " << Decorate(TFunction::unmangleName(node->getName())) << (mOutputLod0Function ? "Lod0(" : "("); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1891 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1892 | TIntermSequence *arguments = node->getSequence(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1893 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1894 | for (unsigned int i = 0; i < arguments->size(); i++) |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1895 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1896 | TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1897 | |
| 1898 | if (symbol) |
| 1899 | { |
| 1900 | out << argumentString(symbol); |
| 1901 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1902 | if (i < arguments->size() - 1) |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1903 | { |
| 1904 | out << ", "; |
| 1905 | } |
| 1906 | } |
| 1907 | else UNREACHABLE(); |
| 1908 | } |
| 1909 | |
| 1910 | out << ");\n"; |
| 1911 | |
daniel@transgaming.com | 0e5bb40 | 2012-10-17 18:24:53 +0000 | [diff] [blame] | 1912 | // Also prototype the Lod0 variant if needed |
| 1913 | if (mContainsLoopDiscontinuity && !mOutputLod0Function) |
| 1914 | { |
| 1915 | mOutputLod0Function = true; |
| 1916 | node->traverse(this); |
| 1917 | mOutputLod0Function = false; |
| 1918 | } |
| 1919 | |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 1920 | return false; |
| 1921 | } |
| 1922 | break; |
daniel@transgaming.com | ed2180d | 2012-03-26 17:08:54 +0000 | [diff] [blame] | 1923 | case EOpComma: outputTriplet(visit, "(", ", ", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1924 | case EOpFunction: |
| 1925 | { |
alokp@chromium.org | 4388487 | 2010-03-30 00:08:52 +0000 | [diff] [blame] | 1926 | TString name = TFunction::unmangleName(node->getName()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1927 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1928 | out << TypeString(node->getType()) << " "; |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1929 | |
| 1930 | if (name == "main") |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1931 | { |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1932 | out << "gl_main("; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1933 | } |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1934 | else |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1935 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1936 | out << Decorate(name) << (mOutputLod0Function ? "Lod0(" : "("); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1937 | } |
daniel@transgaming.com | 6369186 | 2010-04-29 03:32:42 +0000 | [diff] [blame] | 1938 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1939 | TIntermSequence *sequence = node->getSequence(); |
| 1940 | TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence(); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1941 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1942 | for (unsigned int i = 0; i < arguments->size(); i++) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1943 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1944 | TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode(); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1945 | |
| 1946 | if (symbol) |
| 1947 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 1948 | TStructure *structure = symbol->getType().getStruct(); |
| 1949 | |
| 1950 | if (structure) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1951 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 1952 | mStructureHLSL->addConstructor(symbol->getType(), StructNameString(*structure), NULL); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
| 1955 | out << argumentString(symbol); |
| 1956 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1957 | if (i < arguments->size() - 1) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1958 | { |
| 1959 | out << ", "; |
| 1960 | } |
| 1961 | } |
| 1962 | else UNREACHABLE(); |
| 1963 | } |
| 1964 | |
| 1965 | out << ")\n" |
| 1966 | "{\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1967 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1968 | if (sequence->size() > 1) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1969 | { |
| 1970 | mInsideFunction = true; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1971 | (*sequence)[1]->traverse(this); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 1972 | mInsideFunction = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1973 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1974 | |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1975 | out << "}\n"; |
| 1976 | |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 1977 | if (mContainsLoopDiscontinuity && !mOutputLod0Function) |
| 1978 | { |
daniel@transgaming.com | ecdf44a | 2012-06-01 01:45:15 +0000 | [diff] [blame] | 1979 | if (name != "main") |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 1980 | { |
| 1981 | mOutputLod0Function = true; |
| 1982 | node->traverse(this); |
| 1983 | mOutputLod0Function = false; |
| 1984 | } |
| 1985 | } |
| 1986 | |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 1987 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1988 | } |
| 1989 | break; |
| 1990 | case EOpFunctionCall: |
| 1991 | { |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 1992 | TString name = TFunction::unmangleName(node->getName()); |
| 1993 | bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1994 | TIntermSequence *arguments = node->getSequence(); |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 1995 | |
| 1996 | if (node->isUserDefined()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1997 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1998 | out << Decorate(name) << (lod0 ? "Lod0(" : "("); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1999 | } |
| 2000 | else |
| 2001 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2002 | TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType(); |
shannonwoods@chromium.org | c6ac65f | 2013-05-30 00:02:50 +0000 | [diff] [blame] | 2003 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2004 | TextureFunction textureFunction; |
| 2005 | textureFunction.sampler = samplerType; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2006 | textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize(); |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2007 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2008 | textureFunction.proj = false; |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2009 | textureFunction.offset = false; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2010 | |
| 2011 | if (name == "texture2D" || name == "textureCube" || name == "texture") |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2012 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2013 | textureFunction.method = TextureFunction::IMPLICIT; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2014 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2015 | else if (name == "texture2DProj" || name == "textureProj") |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2016 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2017 | textureFunction.method = TextureFunction::IMPLICIT; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2018 | textureFunction.proj = true; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2019 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2020 | else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" || |
| 2021 | name == "texture2DLodEXT" || name == "textureCubeLodEXT") |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2022 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2023 | textureFunction.method = TextureFunction::LOD; |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2024 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2025 | else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT") |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2026 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2027 | textureFunction.method = TextureFunction::LOD; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2028 | textureFunction.proj = true; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2029 | } |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2030 | else if (name == "textureSize") |
| 2031 | { |
| 2032 | textureFunction.method = TextureFunction::SIZE; |
| 2033 | } |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2034 | else if (name == "textureOffset") |
| 2035 | { |
| 2036 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2037 | textureFunction.offset = true; |
| 2038 | } |
Nicolas Capens | df86c6b | 2014-02-14 20:09:17 -0500 | [diff] [blame] | 2039 | else if (name == "textureProjOffset") |
| 2040 | { |
| 2041 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2042 | textureFunction.offset = true; |
| 2043 | textureFunction.proj = true; |
| 2044 | } |
| 2045 | else if (name == "textureLodOffset") |
| 2046 | { |
| 2047 | textureFunction.method = TextureFunction::LOD; |
| 2048 | textureFunction.offset = true; |
| 2049 | } |
Nicolas Capens | 2adc256 | 2014-02-14 23:50:59 -0500 | [diff] [blame] | 2050 | else if (name == "textureProjLodOffset") |
| 2051 | { |
| 2052 | textureFunction.method = TextureFunction::LOD; |
| 2053 | textureFunction.proj = true; |
| 2054 | textureFunction.offset = true; |
| 2055 | } |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 2056 | else if (name == "texelFetch") |
| 2057 | { |
| 2058 | textureFunction.method = TextureFunction::FETCH; |
| 2059 | } |
| 2060 | else if (name == "texelFetchOffset") |
| 2061 | { |
| 2062 | textureFunction.method = TextureFunction::FETCH; |
| 2063 | textureFunction.offset = true; |
| 2064 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2065 | else if (name == "textureGrad" || name == "texture2DGradEXT") |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 2066 | { |
| 2067 | textureFunction.method = TextureFunction::GRAD; |
| 2068 | } |
Nicolas Capens | bf7db10 | 2014-02-19 17:20:28 -0500 | [diff] [blame] | 2069 | else if (name == "textureGradOffset") |
| 2070 | { |
| 2071 | textureFunction.method = TextureFunction::GRAD; |
| 2072 | textureFunction.offset = true; |
| 2073 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2074 | else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT") |
Nicolas Capens | f7378e3 | 2014-02-19 17:29:32 -0500 | [diff] [blame] | 2075 | { |
| 2076 | textureFunction.method = TextureFunction::GRAD; |
| 2077 | textureFunction.proj = true; |
| 2078 | } |
| 2079 | else if (name == "textureProjGradOffset") |
| 2080 | { |
| 2081 | textureFunction.method = TextureFunction::GRAD; |
| 2082 | textureFunction.proj = true; |
| 2083 | textureFunction.offset = true; |
| 2084 | } |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2085 | else UNREACHABLE(); |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2086 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2087 | if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2088 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2089 | unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments |
| 2090 | |
| 2091 | if (textureFunction.offset) |
| 2092 | { |
| 2093 | mandatoryArgumentCount++; |
| 2094 | } |
| 2095 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2096 | bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2097 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2098 | if (lod0 || mShaderType == GL_VERTEX_SHADER) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2099 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2100 | if (bias) |
| 2101 | { |
| 2102 | textureFunction.method = TextureFunction::LOD0BIAS; |
| 2103 | } |
| 2104 | else |
| 2105 | { |
| 2106 | textureFunction.method = TextureFunction::LOD0; |
| 2107 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2108 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2109 | else if (bias) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2110 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2111 | textureFunction.method = TextureFunction::BIAS; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | mUsesTexture.insert(textureFunction); |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2116 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2117 | out << textureFunction.name(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2118 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2119 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2120 | for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2121 | { |
| 2122 | if (mOutputType == SH_HLSL11_OUTPUT && IsSampler((*arg)->getAsTyped()->getBasicType())) |
| 2123 | { |
| 2124 | out << "texture_"; |
| 2125 | (*arg)->traverse(this); |
| 2126 | out << ", sampler_"; |
| 2127 | } |
| 2128 | |
| 2129 | (*arg)->traverse(this); |
| 2130 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2131 | if (arg < arguments->end() - 1) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2132 | { |
| 2133 | out << ", "; |
| 2134 | } |
| 2135 | } |
| 2136 | |
| 2137 | out << ")"; |
| 2138 | |
| 2139 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2140 | } |
| 2141 | break; |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2142 | case EOpParameters: outputTriplet(visit, "(", ", ", ")\n{\n"); break; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2143 | case EOpConstructFloat: outputConstructor(visit, node->getType(), "vec1", node->getSequence()); break; |
| 2144 | case EOpConstructVec2: outputConstructor(visit, node->getType(), "vec2", node->getSequence()); break; |
| 2145 | case EOpConstructVec3: outputConstructor(visit, node->getType(), "vec3", node->getSequence()); break; |
| 2146 | case EOpConstructVec4: outputConstructor(visit, node->getType(), "vec4", node->getSequence()); break; |
| 2147 | case EOpConstructBool: outputConstructor(visit, node->getType(), "bvec1", node->getSequence()); break; |
| 2148 | case EOpConstructBVec2: outputConstructor(visit, node->getType(), "bvec2", node->getSequence()); break; |
| 2149 | case EOpConstructBVec3: outputConstructor(visit, node->getType(), "bvec3", node->getSequence()); break; |
| 2150 | case EOpConstructBVec4: outputConstructor(visit, node->getType(), "bvec4", node->getSequence()); break; |
| 2151 | case EOpConstructInt: outputConstructor(visit, node->getType(), "ivec1", node->getSequence()); break; |
| 2152 | case EOpConstructIVec2: outputConstructor(visit, node->getType(), "ivec2", node->getSequence()); break; |
| 2153 | case EOpConstructIVec3: outputConstructor(visit, node->getType(), "ivec3", node->getSequence()); break; |
| 2154 | case EOpConstructIVec4: outputConstructor(visit, node->getType(), "ivec4", node->getSequence()); break; |
| 2155 | case EOpConstructUInt: outputConstructor(visit, node->getType(), "uvec1", node->getSequence()); break; |
| 2156 | case EOpConstructUVec2: outputConstructor(visit, node->getType(), "uvec2", node->getSequence()); break; |
| 2157 | case EOpConstructUVec3: outputConstructor(visit, node->getType(), "uvec3", node->getSequence()); break; |
| 2158 | case EOpConstructUVec4: outputConstructor(visit, node->getType(), "uvec4", node->getSequence()); break; |
| 2159 | case EOpConstructMat2: outputConstructor(visit, node->getType(), "mat2", node->getSequence()); break; |
| 2160 | case EOpConstructMat3: outputConstructor(visit, node->getType(), "mat3", node->getSequence()); break; |
| 2161 | case EOpConstructMat4: outputConstructor(visit, node->getType(), "mat4", node->getSequence()); break; |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 2162 | case EOpConstructStruct: |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 2163 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2164 | const TString &structName = StructNameString(*node->getType().getStruct()); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2165 | mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence()); |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2166 | outputTriplet(visit, (structName + "_ctor(").c_str(), ", ", ")"); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 2167 | } |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 2168 | break; |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 2169 | case EOpLessThan: outputTriplet(visit, "(", " < ", ")"); break; |
| 2170 | case EOpGreaterThan: outputTriplet(visit, "(", " > ", ")"); break; |
| 2171 | case EOpLessThanEqual: outputTriplet(visit, "(", " <= ", ")"); break; |
| 2172 | case EOpGreaterThanEqual: outputTriplet(visit, "(", " >= ", ")"); break; |
| 2173 | case EOpVectorEqual: outputTriplet(visit, "(", " == ", ")"); break; |
| 2174 | case EOpVectorNotEqual: outputTriplet(visit, "(", " != ", ")"); break; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2175 | case EOpMod: |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2176 | ASSERT(node->getUseEmulatedFunction()); |
| 2177 | writeEmulatedFunctionTriplet(visit, "mod("); |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2178 | break; |
Olli Etuaho | b6e07a6 | 2015-02-16 12:22:10 +0200 | [diff] [blame] | 2179 | case EOpModf: outputTriplet(visit, "modf(", ", ", ")"); break; |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 2180 | case EOpPow: outputTriplet(visit, "pow(", ", ", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2181 | case EOpAtan: |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2182 | ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2183 | ASSERT(node->getUseEmulatedFunction()); |
| 2184 | writeEmulatedFunctionTriplet(visit, "atan("); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2185 | break; |
| 2186 | case EOpMin: outputTriplet(visit, "min(", ", ", ")"); break; |
| 2187 | case EOpMax: outputTriplet(visit, "max(", ", ", ")"); break; |
| 2188 | case EOpClamp: outputTriplet(visit, "clamp(", ", ", ")"); break; |
| 2189 | case EOpMix: outputTriplet(visit, "lerp(", ", ", ")"); break; |
| 2190 | case EOpStep: outputTriplet(visit, "step(", ", ", ")"); break; |
| 2191 | case EOpSmoothStep: outputTriplet(visit, "smoothstep(", ", ", ")"); break; |
| 2192 | case EOpDistance: outputTriplet(visit, "distance(", ", ", ")"); break; |
| 2193 | case EOpDot: outputTriplet(visit, "dot(", ", ", ")"); break; |
| 2194 | case EOpCross: outputTriplet(visit, "cross(", ", ", ")"); break; |
daniel@transgaming.com | 0bbb031 | 2010-04-26 15:33:39 +0000 | [diff] [blame] | 2195 | case EOpFaceForward: |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2196 | ASSERT(node->getUseEmulatedFunction()); |
| 2197 | writeEmulatedFunctionTriplet(visit, "faceforward("); |
daniel@transgaming.com | 0bbb031 | 2010-04-26 15:33:39 +0000 | [diff] [blame] | 2198 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2199 | case EOpReflect: outputTriplet(visit, "reflect(", ", ", ")"); break; |
| 2200 | case EOpRefract: outputTriplet(visit, "refract(", ", ", ")"); break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 2201 | case EOpOuterProduct: |
| 2202 | ASSERT(node->getUseEmulatedFunction()); |
| 2203 | writeEmulatedFunctionTriplet(visit, "outerProduct("); |
| 2204 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2205 | case EOpMul: outputTriplet(visit, "(", " * ", ")"); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2206 | default: UNREACHABLE(); |
| 2207 | } |
| 2208 | |
| 2209 | return true; |
| 2210 | } |
| 2211 | |
| 2212 | bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node) |
| 2213 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2214 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2215 | |
alokp@chromium.org | 60fe407 | 2010-03-29 20:58:29 +0000 | [diff] [blame] | 2216 | if (node->usesTernaryOperator()) |
| 2217 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 2218 | out << "s" << mUnfoldShortCircuit->getNextTemporaryIndex(); |
alokp@chromium.org | 60fe407 | 2010-03-29 20:58:29 +0000 | [diff] [blame] | 2219 | } |
| 2220 | else // if/else statement |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2221 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 2222 | mUnfoldShortCircuit->traverse(node->getCondition()); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2223 | |
Corentin Wallez | 80bacde | 2014-11-10 12:07:37 -0800 | [diff] [blame] | 2224 | // D3D errors when there is a gradient operation in a loop in an unflattened if |
| 2225 | // however flattening all the ifs in branch heavy shaders made D3D error too. |
| 2226 | // As a temporary workaround we flatten the ifs only if there is at least a loop |
| 2227 | // present somewhere in the shader. |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2228 | if (mShaderType == GL_FRAGMENT_SHADER && mContainsAnyLoop) |
Corentin Wallez | 80bacde | 2014-11-10 12:07:37 -0800 | [diff] [blame] | 2229 | { |
| 2230 | out << "FLATTEN "; |
| 2231 | } |
| 2232 | |
| 2233 | out << "if ("; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2234 | |
| 2235 | node->getCondition()->traverse(this); |
| 2236 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2237 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2238 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2239 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2240 | out << "{\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2241 | |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2242 | bool discard = false; |
| 2243 | |
daniel@transgaming.com | bb88532 | 2010-04-15 20:45:24 +0000 | [diff] [blame] | 2244 | if (node->getTrueBlock()) |
| 2245 | { |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2246 | traverseStatements(node->getTrueBlock()); |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2247 | |
| 2248 | // Detect true discard |
| 2249 | discard = (discard || FindDiscard::search(node->getTrueBlock())); |
daniel@transgaming.com | bb88532 | 2010-04-15 20:45:24 +0000 | [diff] [blame] | 2250 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2251 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2252 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2253 | out << ";\n}\n"; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2254 | |
| 2255 | if (node->getFalseBlock()) |
| 2256 | { |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2257 | out << "else\n"; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2258 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2259 | outputLineDirective(node->getFalseBlock()->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2260 | out << "{\n"; |
| 2261 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2262 | outputLineDirective(node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2263 | traverseStatements(node->getFalseBlock()); |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2264 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2265 | outputLineDirective(node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2266 | out << ";\n}\n"; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2267 | |
| 2268 | // Detect false discard |
| 2269 | discard = (discard || FindDiscard::search(node->getFalseBlock())); |
| 2270 | } |
| 2271 | |
| 2272 | // ANGLE issue 486: Detect problematic conditional discard |
| 2273 | if (discard && FindSideEffectRewriting::search(node)) |
| 2274 | { |
| 2275 | mUsesDiscardRewriting = true; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2276 | } |
| 2277 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2278 | |
| 2279 | return false; |
| 2280 | } |
| 2281 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2282 | bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2283 | { |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2284 | if (node->getStatementList()) |
| 2285 | { |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 2286 | node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList())); |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2287 | outputTriplet(visit, "switch (", ") ", ""); |
| 2288 | // The curly braces get written when visiting the statementList aggregate |
| 2289 | } |
| 2290 | else |
| 2291 | { |
| 2292 | // No statementList, so it won't output curly braces |
| 2293 | outputTriplet(visit, "switch (", ") {", "}\n"); |
| 2294 | } |
| 2295 | return true; |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2296 | } |
| 2297 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2298 | bool OutputHLSL::visitCase(Visit visit, TIntermCase *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2299 | { |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2300 | if (node->hasCondition()) |
| 2301 | { |
| 2302 | outputTriplet(visit, "case (", "", "):\n"); |
| 2303 | return true; |
| 2304 | } |
| 2305 | else |
| 2306 | { |
| 2307 | TInfoSinkBase &out = getInfoSink(); |
| 2308 | out << "default:\n"; |
| 2309 | return false; |
| 2310 | } |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2311 | } |
| 2312 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2313 | void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node) |
| 2314 | { |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2315 | writeConstantUnion(node->getType(), node->getUnionArrayPointer()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node) |
| 2319 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2320 | mNestedLoopDepth++; |
| 2321 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2322 | bool wasDiscontinuous = mInsideDiscontinuousLoop; |
| 2323 | |
shannon.woods@transgaming.com | e91615c | 2013-01-25 21:56:03 +0000 | [diff] [blame] | 2324 | if (mContainsLoopDiscontinuity && !mInsideDiscontinuousLoop) |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2325 | { |
| 2326 | mInsideDiscontinuousLoop = containsLoopDiscontinuity(node); |
| 2327 | } |
| 2328 | |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2329 | if (mOutputType == SH_HLSL9_OUTPUT) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2330 | { |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2331 | if (handleExcessiveLoop(node)) |
| 2332 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2333 | mInsideDiscontinuousLoop = wasDiscontinuous; |
| 2334 | mNestedLoopDepth--; |
| 2335 | |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2336 | return false; |
| 2337 | } |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2338 | } |
| 2339 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2340 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2341 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2342 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2343 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 2344 | out << "{LOOP do\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2345 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2346 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2347 | out << "{\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2348 | } |
| 2349 | else |
| 2350 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 2351 | out << "{LOOP for("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2352 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2353 | if (node->getInit()) |
| 2354 | { |
| 2355 | node->getInit()->traverse(this); |
| 2356 | } |
| 2357 | |
| 2358 | out << "; "; |
| 2359 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2360 | if (node->getCondition()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2361 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2362 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2363 | } |
| 2364 | |
| 2365 | out << "; "; |
| 2366 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2367 | if (node->getExpression()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2368 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2369 | node->getExpression()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2372 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2373 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2374 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2375 | out << "{\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | if (node->getBody()) |
| 2379 | { |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2380 | traverseStatements(node->getBody()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2381 | } |
| 2382 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2383 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | 7fb81e8 | 2011-09-23 18:20:46 +0000 | [diff] [blame] | 2384 | out << ";}\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2385 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2386 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2387 | { |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2388 | outputLineDirective(node->getCondition()->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2389 | out << "while(\n"; |
| 2390 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2391 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2392 | |
daniel@transgaming.com | 7353698 | 2012-03-21 20:45:49 +0000 | [diff] [blame] | 2393 | out << ");"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2394 | } |
| 2395 | |
daniel@transgaming.com | 7353698 | 2012-03-21 20:45:49 +0000 | [diff] [blame] | 2396 | out << "}\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2397 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2398 | mInsideDiscontinuousLoop = wasDiscontinuous; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2399 | mNestedLoopDepth--; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2400 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2401 | return false; |
| 2402 | } |
| 2403 | |
| 2404 | bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node) |
| 2405 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2406 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2407 | |
| 2408 | switch (node->getFlowOp()) |
| 2409 | { |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2410 | case EOpKill: |
| 2411 | outputTriplet(visit, "discard;\n", "", ""); |
| 2412 | break; |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2413 | case EOpBreak: |
| 2414 | if (visit == PreVisit) |
| 2415 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2416 | if (mNestedLoopDepth > 1) |
| 2417 | { |
| 2418 | mUsesNestedBreak = true; |
| 2419 | } |
| 2420 | |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2421 | if (mExcessiveLoopIndex) |
| 2422 | { |
| 2423 | out << "{Break"; |
| 2424 | mExcessiveLoopIndex->traverse(this); |
| 2425 | out << " = true; break;}\n"; |
| 2426 | } |
| 2427 | else |
| 2428 | { |
| 2429 | out << "break;\n"; |
| 2430 | } |
| 2431 | } |
| 2432 | break; |
apatrick@chromium.org | 05a5d8e | 2011-02-16 19:07:20 +0000 | [diff] [blame] | 2433 | case EOpContinue: outputTriplet(visit, "continue;\n", "", ""); break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2434 | case EOpReturn: |
| 2435 | if (visit == PreVisit) |
| 2436 | { |
| 2437 | if (node->getExpression()) |
| 2438 | { |
| 2439 | out << "return "; |
| 2440 | } |
| 2441 | else |
| 2442 | { |
| 2443 | out << "return;\n"; |
| 2444 | } |
| 2445 | } |
| 2446 | else if (visit == PostVisit) |
| 2447 | { |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2448 | if (node->getExpression()) |
| 2449 | { |
| 2450 | out << ";\n"; |
| 2451 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2452 | } |
| 2453 | break; |
| 2454 | default: UNREACHABLE(); |
| 2455 | } |
| 2456 | |
| 2457 | return true; |
| 2458 | } |
| 2459 | |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2460 | void OutputHLSL::traverseStatements(TIntermNode *node) |
| 2461 | { |
| 2462 | if (isSingleStatement(node)) |
| 2463 | { |
daniel@transgaming.com | f8f8f36 | 2012-04-28 00:35:00 +0000 | [diff] [blame] | 2464 | mUnfoldShortCircuit->traverse(node); |
daniel@transgaming.com | 44fffee | 2012-04-28 00:34:20 +0000 | [diff] [blame] | 2465 | } |
| 2466 | |
| 2467 | node->traverse(this); |
| 2468 | } |
| 2469 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2470 | bool OutputHLSL::isSingleStatement(TIntermNode *node) |
| 2471 | { |
| 2472 | TIntermAggregate *aggregate = node->getAsAggregate(); |
| 2473 | |
| 2474 | if (aggregate) |
| 2475 | { |
| 2476 | if (aggregate->getOp() == EOpSequence) |
| 2477 | { |
| 2478 | return false; |
| 2479 | } |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 2480 | else if (aggregate->getOp() == EOpDeclaration) |
| 2481 | { |
| 2482 | // Declaring multiple comma-separated variables must be considered multiple statements |
| 2483 | // because each individual declaration has side effects which are visible in the next. |
| 2484 | return false; |
| 2485 | } |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2486 | else |
| 2487 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2488 | for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++) |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2489 | { |
| 2490 | if (!isSingleStatement(*sit)) |
| 2491 | { |
| 2492 | return false; |
| 2493 | } |
| 2494 | } |
| 2495 | |
| 2496 | return true; |
| 2497 | } |
| 2498 | } |
| 2499 | |
| 2500 | return true; |
| 2501 | } |
| 2502 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2503 | // Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them |
| 2504 | // (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254). |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2505 | bool OutputHLSL::handleExcessiveLoop(TIntermLoop *node) |
| 2506 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2507 | const int MAX_LOOP_ITERATIONS = 254; |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2508 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2509 | |
| 2510 | // Parse loops of the form: |
| 2511 | // for(int index = initial; index [comparator] limit; index += increment) |
| 2512 | TIntermSymbol *index = NULL; |
| 2513 | TOperator comparator = EOpNull; |
| 2514 | int initial = 0; |
| 2515 | int limit = 0; |
| 2516 | int increment = 0; |
| 2517 | |
| 2518 | // Parse index name and intial value |
| 2519 | if (node->getInit()) |
| 2520 | { |
| 2521 | TIntermAggregate *init = node->getInit()->getAsAggregate(); |
| 2522 | |
| 2523 | if (init) |
| 2524 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2525 | TIntermSequence *sequence = init->getSequence(); |
| 2526 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2527 | |
| 2528 | if (variable && variable->getQualifier() == EvqTemporary) |
| 2529 | { |
| 2530 | TIntermBinary *assign = variable->getAsBinaryNode(); |
| 2531 | |
| 2532 | if (assign->getOp() == EOpInitialize) |
| 2533 | { |
| 2534 | TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode(); |
| 2535 | TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion(); |
| 2536 | |
| 2537 | if (symbol && constant) |
| 2538 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2539 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2540 | { |
| 2541 | index = symbol; |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2542 | initial = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2543 | } |
| 2544 | } |
| 2545 | } |
| 2546 | } |
| 2547 | } |
| 2548 | } |
| 2549 | |
| 2550 | // Parse comparator and limit value |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2551 | if (index != NULL && node->getCondition()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2552 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2553 | TIntermBinary *test = node->getCondition()->getAsBinaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2554 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2555 | if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId()) |
| 2556 | { |
| 2557 | TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion(); |
| 2558 | |
| 2559 | if (constant) |
| 2560 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2561 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2562 | { |
| 2563 | comparator = test->getOp(); |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2564 | limit = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2565 | } |
| 2566 | } |
| 2567 | } |
| 2568 | } |
| 2569 | |
| 2570 | // Parse increment |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2571 | if (index != NULL && comparator != EOpNull && node->getExpression()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2572 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2573 | TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode(); |
| 2574 | TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2575 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2576 | if (binaryTerminal) |
| 2577 | { |
| 2578 | TOperator op = binaryTerminal->getOp(); |
| 2579 | TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion(); |
| 2580 | |
| 2581 | if (constant) |
| 2582 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2583 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2584 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2585 | int value = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2586 | |
| 2587 | switch (op) |
| 2588 | { |
| 2589 | case EOpAddAssign: increment = value; break; |
| 2590 | case EOpSubAssign: increment = -value; break; |
| 2591 | default: UNIMPLEMENTED(); |
| 2592 | } |
| 2593 | } |
| 2594 | } |
| 2595 | } |
| 2596 | else if (unaryTerminal) |
| 2597 | { |
| 2598 | TOperator op = unaryTerminal->getOp(); |
| 2599 | |
| 2600 | switch (op) |
| 2601 | { |
| 2602 | case EOpPostIncrement: increment = 1; break; |
| 2603 | case EOpPostDecrement: increment = -1; break; |
| 2604 | case EOpPreIncrement: increment = 1; break; |
| 2605 | case EOpPreDecrement: increment = -1; break; |
| 2606 | default: UNIMPLEMENTED(); |
| 2607 | } |
| 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | if (index != NULL && comparator != EOpNull && increment != 0) |
| 2612 | { |
| 2613 | if (comparator == EOpLessThanEqual) |
| 2614 | { |
| 2615 | comparator = EOpLessThan; |
| 2616 | limit += 1; |
| 2617 | } |
| 2618 | |
| 2619 | if (comparator == EOpLessThan) |
| 2620 | { |
daniel@transgaming.com | f1f538e | 2011-02-09 16:30:01 +0000 | [diff] [blame] | 2621 | int iterations = (limit - initial) / increment; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2622 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2623 | if (iterations <= MAX_LOOP_ITERATIONS) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2624 | { |
| 2625 | return false; // Not an excessive loop |
| 2626 | } |
| 2627 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 2628 | TIntermSymbol *restoreIndex = mExcessiveLoopIndex; |
| 2629 | mExcessiveLoopIndex = index; |
| 2630 | |
daniel@transgaming.com | 0933b0c | 2012-07-11 20:37:28 +0000 | [diff] [blame] | 2631 | out << "{int "; |
| 2632 | index->traverse(this); |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2633 | out << ";\n" |
| 2634 | "bool Break"; |
| 2635 | index->traverse(this); |
| 2636 | out << " = false;\n"; |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 2637 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2638 | bool firstLoopFragment = true; |
| 2639 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2640 | while (iterations > 0) |
| 2641 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2642 | int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2643 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2644 | if (!firstLoopFragment) |
| 2645 | { |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2646 | out << "if (!Break"; |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2647 | index->traverse(this); |
| 2648 | out << ") {\n"; |
| 2649 | } |
daniel@transgaming.com | 2fe20a8 | 2012-07-11 20:37:41 +0000 | [diff] [blame] | 2650 | |
| 2651 | if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment |
| 2652 | { |
| 2653 | mExcessiveLoopIndex = NULL; // Stops setting the Break flag |
| 2654 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2655 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2656 | // for(int index = initial; index < clampedLimit; index += increment) |
| 2657 | |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 2658 | out << "LOOP for("; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2659 | index->traverse(this); |
| 2660 | out << " = "; |
| 2661 | out << initial; |
| 2662 | |
| 2663 | out << "; "; |
| 2664 | index->traverse(this); |
| 2665 | out << " < "; |
| 2666 | out << clampedLimit; |
| 2667 | |
| 2668 | out << "; "; |
| 2669 | index->traverse(this); |
| 2670 | out << " += "; |
| 2671 | out << increment; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2672 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2673 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2674 | outputLineDirective(node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2675 | out << "{\n"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2676 | |
| 2677 | if (node->getBody()) |
| 2678 | { |
| 2679 | node->getBody()->traverse(this); |
| 2680 | } |
| 2681 | |
Jamie Madill | 075edd8 | 2013-07-08 13:30:19 -0400 | [diff] [blame] | 2682 | outputLineDirective(node->getLine().first_line); |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2683 | out << ";}\n"; |
| 2684 | |
| 2685 | if (!firstLoopFragment) |
| 2686 | { |
| 2687 | out << "}\n"; |
| 2688 | } |
| 2689 | |
| 2690 | firstLoopFragment = false; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2691 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2692 | initial += MAX_LOOP_ITERATIONS * increment; |
| 2693 | iterations -= MAX_LOOP_ITERATIONS; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2694 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2695 | |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 2696 | out << "}"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2697 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 2698 | mExcessiveLoopIndex = restoreIndex; |
| 2699 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2700 | return true; |
| 2701 | } |
| 2702 | else UNIMPLEMENTED(); |
| 2703 | } |
| 2704 | |
| 2705 | return false; // Not handled as an excessive loop |
| 2706 | } |
| 2707 | |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2708 | void OutputHLSL::outputTriplet(Visit visit, const char *preString, const char *inString, const char *postString) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2709 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2710 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2711 | |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2712 | if (visit == PreVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2713 | { |
| 2714 | out << preString; |
| 2715 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2716 | else if (visit == InVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2717 | { |
| 2718 | out << inString; |
| 2719 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2720 | else if (visit == PostVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2721 | { |
| 2722 | out << postString; |
| 2723 | } |
| 2724 | } |
| 2725 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2726 | void OutputHLSL::outputLineDirective(int line) |
| 2727 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2728 | if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2729 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2730 | TInfoSinkBase &out = getInfoSink(); |
| 2731 | |
| 2732 | out << "\n"; |
| 2733 | out << "#line " << line; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2734 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2735 | if (mSourcePath) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2736 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2737 | out << " \"" << mSourcePath << "\""; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2738 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2739 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2740 | out << "\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2741 | } |
| 2742 | } |
| 2743 | |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2744 | TString OutputHLSL::argumentString(const TIntermSymbol *symbol) |
| 2745 | { |
| 2746 | TQualifier qualifier = symbol->getQualifier(); |
| 2747 | const TType &type = symbol->getType(); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2748 | TString name = symbol->getSymbol(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2749 | |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2750 | if (name.empty()) // HLSL demands named arguments, also for prototypes |
| 2751 | { |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 2752 | name = "x" + str(mUniqueIndex++); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2753 | } |
| 2754 | else |
| 2755 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2756 | name = Decorate(name); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2759 | if (mOutputType == SH_HLSL11_OUTPUT && IsSampler(type.getBasicType())) |
| 2760 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2761 | return QualifierString(qualifier) + " " + TextureString(type) + " texture_" + name + ArrayString(type) + ", " + |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2762 | QualifierString(qualifier) + " " + SamplerString(type) + " sampler_" + name + ArrayString(type); |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2765 | return QualifierString(qualifier) + " " + TypeString(type) + " " + name + ArrayString(type); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | TString OutputHLSL::initializer(const TType &type) |
| 2769 | { |
| 2770 | TString string; |
| 2771 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2772 | size_t size = type.getObjectSize(); |
| 2773 | for (size_t component = 0; component < size; component++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2774 | { |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 2775 | string += "0"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2776 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2777 | if (component + 1 < size) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2778 | { |
| 2779 | string += ", "; |
| 2780 | } |
| 2781 | } |
| 2782 | |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 2783 | return "{" + string + "}"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2784 | } |
daniel@transgaming.com | 72d0b52 | 2010-04-13 19:53:44 +0000 | [diff] [blame] | 2785 | |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2786 | void OutputHLSL::outputConstructor(Visit visit, const TType &type, const char *name, const TIntermSequence *parameters) |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2787 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2788 | TInfoSinkBase &out = getInfoSink(); |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2789 | |
| 2790 | if (visit == PreVisit) |
| 2791 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 2792 | mStructureHLSL->addConstructor(type, name, parameters); |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2793 | |
Daniel Bratell | 2919008 | 2015-02-20 16:42:54 +0100 | [diff] [blame] | 2794 | out << name << "("; |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 2795 | } |
| 2796 | else if (visit == InVisit) |
| 2797 | { |
| 2798 | out << ", "; |
| 2799 | } |
| 2800 | else if (visit == PostVisit) |
| 2801 | { |
| 2802 | out << ")"; |
| 2803 | } |
| 2804 | } |
| 2805 | |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2806 | const ConstantUnion *OutputHLSL::writeConstantUnion(const TType &type, const ConstantUnion *constUnion) |
| 2807 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2808 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2809 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2810 | const TStructure* structure = type.getStruct(); |
| 2811 | if (structure) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2812 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2813 | out << StructNameString(*structure) + "_ctor("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2814 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2815 | const TFieldList& fields = structure->fields(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2816 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2817 | for (size_t i = 0; i < fields.size(); i++) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2818 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2819 | const TType *fieldType = fields[i]->type(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2820 | constUnion = writeConstantUnion(*fieldType, constUnion); |
| 2821 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 2822 | if (i != fields.size() - 1) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2823 | { |
| 2824 | out << ", "; |
| 2825 | } |
| 2826 | } |
| 2827 | |
| 2828 | out << ")"; |
| 2829 | } |
| 2830 | else |
| 2831 | { |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2832 | size_t size = type.getObjectSize(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2833 | bool writeType = size > 1; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2834 | |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2835 | if (writeType) |
| 2836 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2837 | out << TypeString(type) << "("; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2838 | } |
| 2839 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 2840 | for (size_t i = 0; i < size; i++, constUnion++) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2841 | { |
| 2842 | switch (constUnion->getType()) |
| 2843 | { |
daniel@transgaming.com | 6c1203f | 2013-01-11 04:12:43 +0000 | [diff] [blame] | 2844 | case EbtFloat: out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); break; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2845 | case EbtInt: out << constUnion->getIConst(); break; |
Nicolas Capens | c0f7c61 | 2013-06-05 11:46:09 -0400 | [diff] [blame] | 2846 | case EbtUInt: out << constUnion->getUConst(); break; |
alokp@chromium.org | 4e4facd | 2010-06-02 15:21:22 +0000 | [diff] [blame] | 2847 | case EbtBool: out << constUnion->getBConst(); break; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 2848 | default: UNREACHABLE(); |
| 2849 | } |
| 2850 | |
| 2851 | if (i != size - 1) |
| 2852 | { |
| 2853 | out << ", "; |
| 2854 | } |
| 2855 | } |
| 2856 | |
| 2857 | if (writeType) |
| 2858 | { |
| 2859 | out << ")"; |
| 2860 | } |
| 2861 | } |
| 2862 | |
| 2863 | return constUnion; |
| 2864 | } |
| 2865 | |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 2866 | void OutputHLSL::writeEmulatedFunctionTriplet(Visit visit, const char *preStr) |
| 2867 | { |
| 2868 | TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr); |
| 2869 | outputTriplet(visit, preString.c_str(), ", ", ")"); |
| 2870 | } |
| 2871 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 2872 | bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression) |
| 2873 | { |
| 2874 | sh::SearchSymbol searchSymbol(symbolNode->getSymbol()); |
| 2875 | expression->traverse(&searchSymbol); |
| 2876 | |
| 2877 | if (searchSymbol.foundMatch()) |
| 2878 | { |
| 2879 | // Type already printed |
| 2880 | out << "t" + str(mUniqueIndex) + " = "; |
| 2881 | expression->traverse(this); |
| 2882 | out << ", "; |
| 2883 | symbolNode->traverse(this); |
| 2884 | out << " = t" + str(mUniqueIndex); |
| 2885 | |
| 2886 | mUniqueIndex++; |
| 2887 | return true; |
| 2888 | } |
| 2889 | |
| 2890 | return false; |
| 2891 | } |
| 2892 | |
| 2893 | void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out) |
| 2894 | { |
| 2895 | out << "#define ANGLE_USES_DEFERRED_INIT\n" |
| 2896 | << "\n" |
| 2897 | << "void initializeDeferredGlobals()\n" |
| 2898 | << "{\n"; |
| 2899 | |
| 2900 | for (const auto &deferredGlobal : mDeferredGlobalInitializers) |
| 2901 | { |
| 2902 | TIntermSymbol *symbol = deferredGlobal.first; |
| 2903 | TIntermTyped *expression = deferredGlobal.second; |
| 2904 | ASSERT(symbol); |
| 2905 | ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst); |
| 2906 | |
| 2907 | out << " " << Decorate(symbol->getSymbol()) << " = "; |
| 2908 | |
| 2909 | if (!writeSameSymbolInitializer(out, symbol, expression)) |
| 2910 | { |
| 2911 | ASSERT(mInfoSinkStack.top() == &out); |
| 2912 | expression->traverse(this); |
| 2913 | } |
| 2914 | |
| 2915 | out << ";\n"; |
| 2916 | } |
| 2917 | |
| 2918 | out << "}\n" |
| 2919 | << "\n"; |
| 2920 | } |
| 2921 | |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 2922 | TString OutputHLSL::addStructEqualityFunction(const TStructure &structure) |
| 2923 | { |
| 2924 | const TFieldList &fields = structure.fields(); |
| 2925 | |
| 2926 | for (const auto &eqFunction : mStructEqualityFunctions) |
| 2927 | { |
| 2928 | if (eqFunction.structure == &structure) |
| 2929 | { |
| 2930 | return eqFunction.functionName; |
| 2931 | } |
| 2932 | } |
| 2933 | |
| 2934 | const TString &structNameString = StructNameString(structure); |
| 2935 | |
| 2936 | StructEqualityFunction function; |
| 2937 | function.structure = &structure; |
| 2938 | function.functionName = "angle_eq_" + structNameString; |
| 2939 | |
| 2940 | TString &func = function.functionDefinition; |
| 2941 | |
| 2942 | func = "bool " + function.functionName + "(" + structNameString + " a, " + structNameString + " b)\n" + |
| 2943 | "{\n" |
| 2944 | " return "; |
| 2945 | |
| 2946 | for (size_t i = 0; i < fields.size(); i++) |
| 2947 | { |
| 2948 | const TField *field = fields[i]; |
| 2949 | const TType *fieldType = field->type(); |
| 2950 | |
| 2951 | const TString &fieldNameA = "a." + Decorate(field->name()); |
| 2952 | const TString &fieldNameB = "b." + Decorate(field->name()); |
| 2953 | |
| 2954 | if (i > 0) |
| 2955 | { |
| 2956 | func += " && "; |
| 2957 | } |
| 2958 | |
| 2959 | if (fieldType->getBasicType() == EbtStruct) |
| 2960 | { |
| 2961 | const TStructure &fieldStruct = *fieldType->getStruct(); |
| 2962 | const TString &functionName = addStructEqualityFunction(fieldStruct); |
| 2963 | func += functionName + "(" + fieldNameA + ", " + fieldNameB + ")"; |
| 2964 | } |
| 2965 | else if (fieldType->isScalar()) |
| 2966 | { |
| 2967 | func += "(" + fieldNameA + " == " + fieldNameB + ")"; |
| 2968 | } |
| 2969 | else |
| 2970 | { |
| 2971 | ASSERT(fieldType->isMatrix() || fieldType->isVector()); |
| 2972 | func += "all(" + fieldNameA + " == " + fieldNameB + ")"; |
| 2973 | } |
| 2974 | } |
| 2975 | |
| 2976 | func = func + ";\n" + "}\n"; |
| 2977 | |
| 2978 | mStructEqualityFunctions.push_back(function); |
| 2979 | |
| 2980 | return function.functionName; |
| 2981 | } |
| 2982 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2983 | } |