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