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