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