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