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