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