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