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