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