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