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