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