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