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 | |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 9 | #include <algorithm> |
shannon.woods@transgaming.com | fff89b3 | 2013-02-28 23:20:15 +0000 | [diff] [blame] | 10 | #include <cfloat> |
| 11 | #include <stdio.h> |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 12 | |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 13 | #include "common/angleutils.h" |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 14 | #include "common/debug.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 15 | #include "common/utilities.h" |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 16 | #include "compiler/translator/BuiltInFunctionEmulator.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 17 | #include "compiler/translator/BuiltInFunctionEmulatorHLSL.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 18 | #include "compiler/translator/FlagStd140Structs.h" |
| 19 | #include "compiler/translator/InfoSink.h" |
| 20 | #include "compiler/translator/NodeSearch.h" |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 21 | #include "compiler/translator/RemoveSwitchFallThrough.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 22 | #include "compiler/translator/SearchSymbol.h" |
| 23 | #include "compiler/translator/StructureHLSL.h" |
| 24 | #include "compiler/translator/TranslatorHLSL.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 25 | #include "compiler/translator/UniformHLSL.h" |
| 26 | #include "compiler/translator/UtilsHLSL.h" |
| 27 | #include "compiler/translator/blocklayout.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 28 | #include "compiler/translator/util.h" |
| 29 | |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 30 | namespace |
| 31 | { |
| 32 | |
| 33 | bool IsSequence(TIntermNode *node) |
| 34 | { |
| 35 | return node->getAsAggregate() != nullptr && node->getAsAggregate()->getOp() == EOpSequence; |
| 36 | } |
| 37 | |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 38 | void WriteSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion) |
| 39 | { |
| 40 | ASSERT(constUnion != nullptr); |
| 41 | switch (constUnion->getType()) |
| 42 | { |
| 43 | case EbtFloat: |
| 44 | out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst())); |
| 45 | break; |
| 46 | case EbtInt: |
| 47 | out << constUnion->getIConst(); |
| 48 | break; |
| 49 | case EbtUInt: |
| 50 | out << constUnion->getUConst(); |
| 51 | break; |
| 52 | case EbtBool: |
| 53 | out << constUnion->getBConst(); |
| 54 | break; |
| 55 | default: |
| 56 | UNREACHABLE(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | const TConstantUnion *WriteConstantUnionArray(TInfoSinkBase &out, |
| 61 | const TConstantUnion *const constUnion, |
| 62 | const size_t size) |
| 63 | { |
| 64 | const TConstantUnion *constUnionIterated = constUnion; |
| 65 | for (size_t i = 0; i < size; i++, constUnionIterated++) |
| 66 | { |
| 67 | WriteSingleConstant(out, constUnionIterated); |
| 68 | |
| 69 | if (i != size - 1) |
| 70 | { |
| 71 | out << ", "; |
| 72 | } |
| 73 | } |
| 74 | return constUnionIterated; |
| 75 | } |
| 76 | |
Olli Etuaho | b079c7a | 2016-04-01 12:32:52 +0300 | [diff] [blame^] | 77 | void OutputIntTexCoordWrap(TInfoSinkBase &out, |
| 78 | const char *wrapMode, |
| 79 | const char *size, |
| 80 | const TString &texCoord, |
| 81 | const TString &texCoordOffset, |
| 82 | const char *texCoordOutName) |
| 83 | { |
| 84 | // GLES 3.0.4 table 3.22 specifies how the wrap modes work. We don't use the formulas verbatim |
| 85 | // but rather use equivalent formulas that map better to HLSL. |
| 86 | out << "int " << texCoordOutName << ";\n"; |
| 87 | out << "float " << texCoordOutName << "Offset = " << texCoord << " + float(" << texCoordOffset |
| 88 | << ") / " << size << ";\n"; |
| 89 | |
| 90 | // CLAMP_TO_EDGE |
| 91 | out << "if (" << wrapMode << " == 1)\n"; |
| 92 | out << "{\n"; |
| 93 | out << " " << texCoordOutName << " = clamp(int(floor(" << size << " * " << texCoordOutName |
| 94 | << "Offset)), 0, int(" << size << ") - 1);\n"; |
| 95 | out << "}\n"; |
| 96 | |
| 97 | // MIRRORED_REPEAT |
| 98 | out << "else if (" << wrapMode << " == 3)\n"; |
| 99 | out << "{\n"; |
| 100 | out << " float coordWrapped = 1.0 - abs(frac(abs(" << texCoordOutName |
| 101 | << "Offset) * 0.5) * 2.0 - 1.0);\n"; |
| 102 | out << " " << texCoordOutName << " = int(floor(" << size << " * coordWrapped));\n"; |
| 103 | out << "}\n"; |
| 104 | |
| 105 | // REPEAT |
| 106 | out << "else\n"; |
| 107 | out << "{\n"; |
| 108 | out << " " << texCoordOutName << " = int(floor(" << size << " * frac(" << texCoordOutName |
| 109 | << "Offset)));\n"; |
| 110 | out << "}\n"; |
| 111 | } |
| 112 | |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 113 | } // namespace |
| 114 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 115 | namespace sh |
| 116 | { |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 117 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 118 | TString OutputHLSL::TextureFunction::name() const |
| 119 | { |
| 120 | TString name = "gl_texture"; |
| 121 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 122 | // We need to include full the sampler type in the function name to make the signature unique |
| 123 | // on D3D11, where samplers are passed to texture functions as indices. |
| 124 | name += TextureTypeSuffix(this->sampler); |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 125 | |
| 126 | if (proj) |
| 127 | { |
| 128 | name += "Proj"; |
| 129 | } |
| 130 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 131 | if (offset) |
| 132 | { |
| 133 | name += "Offset"; |
| 134 | } |
| 135 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 136 | switch(method) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 137 | { |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 138 | case IMPLICIT: break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 139 | case BIAS: break; // Extra parameter makes the signature unique |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 140 | case LOD: name += "Lod"; break; |
| 141 | case LOD0: name += "Lod0"; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 142 | case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 143 | case SIZE: name += "Size"; break; |
| 144 | case FETCH: name += "Fetch"; break; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 145 | case GRAD: name += "Grad"; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 146 | default: UNREACHABLE(); |
| 147 | } |
| 148 | |
| 149 | return name + "("; |
| 150 | } |
| 151 | |
| 152 | bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const |
| 153 | { |
| 154 | if (sampler < rhs.sampler) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 155 | if (sampler > rhs.sampler) return false; |
| 156 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 157 | if (coords < rhs.coords) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 158 | if (coords > rhs.coords) return false; |
| 159 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 160 | if (!proj && rhs.proj) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 161 | if (proj && !rhs.proj) return false; |
| 162 | |
| 163 | if (!offset && rhs.offset) return true; |
| 164 | if (offset && !rhs.offset) return false; |
| 165 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 166 | if (method < rhs.method) return true; |
Nicolas Capens | 04296f8 | 2014-04-14 14:24:38 -0400 | [diff] [blame] | 167 | if (method > rhs.method) return false; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 168 | |
| 169 | return false; |
| 170 | } |
| 171 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 172 | OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion, |
| 173 | const TExtensionBehavior &extensionBehavior, |
| 174 | const char *sourcePath, ShShaderOutput outputType, |
| 175 | int numRenderTargets, const std::vector<Uniform> &uniforms, |
| 176 | int compileOptions) |
Jamie Madill | 54ad4f8 | 2014-09-03 09:40:46 -0400 | [diff] [blame] | 177 | : TIntermTraverser(true, true, true), |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 178 | mShaderType(shaderType), |
| 179 | mShaderVersion(shaderVersion), |
| 180 | mExtensionBehavior(extensionBehavior), |
| 181 | mSourcePath(sourcePath), |
| 182 | mOutputType(outputType), |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 183 | mCompileOptions(compileOptions), |
Sam McNally | 5a0edc6 | 2015-06-30 12:36:07 +1000 | [diff] [blame] | 184 | mNumRenderTargets(numRenderTargets), |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 185 | mCurrentFunctionMetadata(nullptr) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 186 | { |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 187 | mInsideFunction = false; |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 188 | |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 189 | mUsesFragColor = false; |
| 190 | mUsesFragData = false; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 191 | mUsesDepthRange = false; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 192 | mUsesFragCoord = false; |
| 193 | mUsesPointCoord = false; |
| 194 | mUsesFrontFacing = false; |
| 195 | mUsesPointSize = false; |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 196 | mUsesInstanceID = false; |
Corentin Wallez | b076add | 2016-01-11 16:45:46 -0500 | [diff] [blame] | 197 | mUsesVertexID = false; |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 198 | mUsesFragDepth = false; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 199 | mUsesXor = false; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 200 | mUsesDiscardRewriting = false; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 201 | mUsesNestedBreak = false; |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 202 | mRequiresIEEEStrictCompiling = false; |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 203 | |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 204 | mUniqueIndex = 0; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 205 | |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 206 | mOutputLod0Function = false; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 207 | mInsideDiscontinuousLoop = false; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 208 | mNestedLoopDepth = 0; |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 209 | |
| 210 | mExcessiveLoopIndex = NULL; |
daniel@transgaming.com | 652468c | 2012-12-20 21:11:57 +0000 | [diff] [blame] | 211 | |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 212 | mStructureHLSL = new StructureHLSL; |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 213 | mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms); |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 214 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 215 | if (mOutputType == SH_HLSL_3_0_OUTPUT) |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 216 | { |
Arun Patole | 6341939 | 2015-03-13 11:51:07 +0530 | [diff] [blame] | 217 | // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront. |
| 218 | // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust. |
| 219 | // In both cases total 3 uniform registers need to be reserved. |
| 220 | mUniformHLSL->reserveUniformRegisters(3); |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 221 | } |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 222 | |
Geoff Lang | 00140f4 | 2016-02-03 18:47:33 +0000 | [diff] [blame] | 223 | // Reserve registers for the default uniform block and driver constants |
| 224 | mUniformHLSL->reserveInterfaceBlockRegisters(2); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 225 | } |
| 226 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 227 | OutputHLSL::~OutputHLSL() |
| 228 | { |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 229 | SafeDelete(mStructureHLSL); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 230 | SafeDelete(mUniformHLSL); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 231 | for (auto &eqFunction : mStructEqualityFunctions) |
| 232 | { |
| 233 | SafeDelete(eqFunction); |
| 234 | } |
| 235 | for (auto &eqFunction : mArrayEqualityFunctions) |
| 236 | { |
| 237 | SafeDelete(eqFunction); |
| 238 | } |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 241 | void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink) |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 242 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 243 | const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 244 | makeFlaggedStructMaps(flaggedStructs); |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 245 | |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 246 | BuiltInFunctionEmulator builtInFunctionEmulator; |
| 247 | InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 248 | builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 249 | |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 250 | // Now that we are done changing the AST, do the analyses need for HLSL generation |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 251 | CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink); |
| 252 | ASSERT(success == CallDAG::INITDAG_SUCCESS); |
Olli Etuaho | d57e0db | 2015-04-24 15:05:08 +0300 | [diff] [blame] | 253 | UNUSED_ASSERTION_VARIABLE(success); |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 254 | mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag); |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 255 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 256 | // Output the body and footer first to determine what has to go in the header |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 257 | mInfoSinkStack.push(&mBody); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 258 | treeRoot->traverse(this); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 259 | mInfoSinkStack.pop(); |
| 260 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 261 | mInfoSinkStack.push(&mFooter); |
| 262 | if (!mDeferredGlobalInitializers.empty()) |
| 263 | { |
| 264 | writeDeferredGlobalInitializers(mFooter); |
| 265 | } |
| 266 | mInfoSinkStack.pop(); |
| 267 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 268 | mInfoSinkStack.push(&mHeader); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 269 | header(mHeader, &builtInFunctionEmulator); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 270 | mInfoSinkStack.pop(); |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 271 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 272 | objSink << mHeader.c_str(); |
| 273 | objSink << mBody.c_str(); |
| 274 | objSink << mFooter.c_str(); |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 275 | |
| 276 | builtInFunctionEmulator.Cleanup(); |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 277 | } |
| 278 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 279 | void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs) |
| 280 | { |
| 281 | for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++) |
| 282 | { |
| 283 | TIntermTyped *flaggedNode = flaggedStructs[structIndex]; |
| 284 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 285 | TInfoSinkBase structInfoSink; |
| 286 | mInfoSinkStack.push(&structInfoSink); |
| 287 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 288 | // This will mark the necessary block elements as referenced |
| 289 | flaggedNode->traverse(this); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 290 | |
| 291 | TString structName(structInfoSink.c_str()); |
| 292 | mInfoSinkStack.pop(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 293 | |
| 294 | mFlaggedStructOriginalNames[flaggedNode] = structName; |
| 295 | |
| 296 | for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.')) |
| 297 | { |
| 298 | structName.erase(pos, 1); |
| 299 | } |
| 300 | |
| 301 | mFlaggedStructMappedNames[flaggedNode] = "map" + structName; |
| 302 | } |
| 303 | } |
| 304 | |
Jamie Madill | 4e1fd41 | 2014-07-10 17:50:10 -0400 | [diff] [blame] | 305 | const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const |
| 306 | { |
| 307 | return mUniformHLSL->getInterfaceBlockRegisterMap(); |
| 308 | } |
| 309 | |
Jamie Madill | 9fe25e9 | 2014-07-18 10:33:08 -0400 | [diff] [blame] | 310 | const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const |
| 311 | { |
| 312 | return mUniformHLSL->getUniformRegisterMap(); |
| 313 | } |
| 314 | |
daniel@transgaming.com | 0b6b834 | 2010-04-26 15:33:45 +0000 | [diff] [blame] | 315 | int OutputHLSL::vectorSize(const TType &type) const |
| 316 | { |
shannonwoods@chromium.org | 9bd22fa | 2013-05-30 00:18:47 +0000 | [diff] [blame] | 317 | int elementSize = type.isMatrix() ? type.getCols() : 1; |
daniel@transgaming.com | 0b6b834 | 2010-04-26 15:33:45 +0000 | [diff] [blame] | 318 | int arraySize = type.isArray() ? type.getArraySize() : 1; |
| 319 | |
| 320 | return elementSize * arraySize; |
| 321 | } |
| 322 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 323 | TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 324 | { |
| 325 | TString init; |
| 326 | |
| 327 | TString preIndentString; |
| 328 | TString fullIndentString; |
| 329 | |
| 330 | for (int spaces = 0; spaces < (indent * 4); spaces++) |
| 331 | { |
| 332 | preIndentString += ' '; |
| 333 | } |
| 334 | |
| 335 | for (int spaces = 0; spaces < ((indent+1) * 4); spaces++) |
| 336 | { |
| 337 | fullIndentString += ' '; |
| 338 | } |
| 339 | |
| 340 | init += preIndentString + "{\n"; |
| 341 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 342 | const TFieldList &fields = structure.fields(); |
| 343 | for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 344 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 345 | const TField &field = *fields[fieldIndex]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 346 | const TString &fieldName = rhsStructName + "." + Decorate(field.name()); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 347 | const TType &fieldType = *field.type(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 348 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 349 | if (fieldType.getStruct()) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 350 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 351 | init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 352 | } |
| 353 | else |
| 354 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 355 | init += fullIndentString + fieldName + ",\n"; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n"; |
| 360 | |
| 361 | return init; |
| 362 | } |
| 363 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 364 | void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 365 | { |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 366 | TString varyings; |
| 367 | TString attributes; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 368 | TString flaggedStructs; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 369 | |
Jamie Madill | 829f59e | 2013-11-13 19:40:54 -0500 | [diff] [blame] | 370 | 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] | 371 | { |
| 372 | TIntermTyped *structNode = flaggedStructIt->first; |
| 373 | const TString &mappedName = flaggedStructIt->second; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 374 | const TStructure &structure = *structNode->getType().getStruct(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 375 | const TString &originalName = mFlaggedStructOriginalNames[structNode]; |
| 376 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 377 | flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n"; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 378 | flaggedStructs += structInitializerString(0, structure, originalName); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 379 | flaggedStructs += "\n"; |
| 380 | } |
| 381 | |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 382 | for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++) |
| 383 | { |
| 384 | const TType &type = varying->second->getType(); |
| 385 | const TString &name = varying->second->getSymbol(); |
| 386 | |
| 387 | // Program linking depends on this exact format |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 388 | varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " + |
| 389 | Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n"; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++) |
| 393 | { |
| 394 | const TType &type = attribute->second->getType(); |
| 395 | const TString &name = attribute->second->getSymbol(); |
| 396 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 397 | attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n"; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 400 | out << mStructureHLSL->structsHeader(); |
Jamie Madill | 529077d | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 401 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 402 | mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 403 | out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks); |
| 404 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 405 | if (!mEqualityFunctions.empty()) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 406 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 407 | out << "\n// Equality functions\n\n"; |
| 408 | for (const auto &eqFunction : mEqualityFunctions) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 409 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 410 | out << eqFunction->functionDefinition << "\n"; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 411 | } |
| 412 | } |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 413 | if (!mArrayAssignmentFunctions.empty()) |
| 414 | { |
| 415 | out << "\n// Assignment functions\n\n"; |
| 416 | for (const auto &assignmentFunction : mArrayAssignmentFunctions) |
| 417 | { |
| 418 | out << assignmentFunction.functionDefinition << "\n"; |
| 419 | } |
| 420 | } |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 421 | if (!mArrayConstructIntoFunctions.empty()) |
| 422 | { |
| 423 | out << "\n// Array constructor functions\n\n"; |
| 424 | for (const auto &constructIntoFunction : mArrayConstructIntoFunctions) |
| 425 | { |
| 426 | out << constructIntoFunction.functionDefinition << "\n"; |
| 427 | } |
| 428 | } |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 429 | |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 430 | if (mUsesDiscardRewriting) |
| 431 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 432 | out << "#define ANGLE_USES_DISCARD_REWRITING\n"; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 433 | } |
| 434 | |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 435 | if (mUsesNestedBreak) |
| 436 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 437 | out << "#define ANGLE_USES_NESTED_BREAK\n"; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 438 | } |
| 439 | |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 440 | if (mRequiresIEEEStrictCompiling) |
| 441 | { |
| 442 | out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n"; |
| 443 | } |
| 444 | |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 445 | out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n" |
| 446 | "#define LOOP [loop]\n" |
| 447 | "#define FLATTEN [flatten]\n" |
| 448 | "#else\n" |
| 449 | "#define LOOP\n" |
| 450 | "#define FLATTEN\n" |
| 451 | "#endif\n"; |
| 452 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 453 | if (mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 454 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 455 | TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers"); |
| 456 | const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire)); |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 457 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 458 | out << "// Varyings\n"; |
| 459 | out << varyings; |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 460 | out << "\n"; |
| 461 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 462 | if (mShaderVersion >= 300) |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 463 | { |
Jamie Madill | 829f59e | 2013-11-13 19:40:54 -0500 | [diff] [blame] | 464 | 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] | 465 | { |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 466 | const TString &variableName = outputVariableIt->first; |
| 467 | const TType &variableType = outputVariableIt->second->getType(); |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 468 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 469 | out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) + |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 470 | " = " + initializer(variableType) + ";\n"; |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 471 | } |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 472 | } |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 473 | else |
| 474 | { |
| 475 | const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1; |
| 476 | |
| 477 | out << "static float4 gl_Color[" << numColorValues << "] =\n" |
| 478 | "{\n"; |
| 479 | for (unsigned int i = 0; i < numColorValues; i++) |
| 480 | { |
| 481 | out << " float4(0, 0, 0, 0)"; |
| 482 | if (i + 1 != numColorValues) |
| 483 | { |
| 484 | out << ","; |
| 485 | } |
| 486 | out << "\n"; |
| 487 | } |
| 488 | |
| 489 | out << "};\n"; |
| 490 | } |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 491 | |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 492 | if (mUsesFragDepth) |
| 493 | { |
| 494 | out << "static float gl_Depth = 0.0;\n"; |
| 495 | } |
| 496 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 497 | if (mUsesFragCoord) |
| 498 | { |
| 499 | out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n"; |
| 500 | } |
| 501 | |
| 502 | if (mUsesPointCoord) |
| 503 | { |
| 504 | out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n"; |
| 505 | } |
| 506 | |
| 507 | if (mUsesFrontFacing) |
| 508 | { |
| 509 | out << "static bool gl_FrontFacing = false;\n"; |
| 510 | } |
| 511 | |
| 512 | out << "\n"; |
| 513 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 514 | if (mUsesDepthRange) |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 515 | { |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 516 | out << "struct gl_DepthRangeParameters\n" |
| 517 | "{\n" |
| 518 | " float near;\n" |
| 519 | " float far;\n" |
| 520 | " float diff;\n" |
| 521 | "};\n" |
| 522 | "\n"; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 523 | } |
| 524 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 525 | if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 526 | { |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 527 | out << "cbuffer DriverConstants : register(b1)\n" |
| 528 | "{\n"; |
| 529 | |
| 530 | if (mUsesDepthRange) |
| 531 | { |
| 532 | out << " float3 dx_DepthRange : packoffset(c0);\n"; |
| 533 | } |
| 534 | |
| 535 | if (mUsesFragCoord) |
| 536 | { |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 537 | out << " float4 dx_ViewCoords : packoffset(c1);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | if (mUsesFragCoord || mUsesFrontFacing) |
| 541 | { |
| 542 | out << " float3 dx_DepthFront : packoffset(c2);\n"; |
| 543 | } |
| 544 | |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 545 | if (mUsesFragCoord) |
| 546 | { |
| 547 | // dx_ViewScale is only used in the fragment shader to correct |
| 548 | // the value for glFragCoord if necessary |
| 549 | out << " float2 dx_ViewScale : packoffset(c3);\n"; |
| 550 | } |
| 551 | |
Olli Etuaho | 618bebc | 2016-01-15 16:40:00 +0200 | [diff] [blame] | 552 | if (mOutputType == SH_HLSL_4_1_OUTPUT) |
| 553 | { |
| 554 | mUniformHLSL->samplerMetadataUniforms(out, "c4"); |
| 555 | } |
| 556 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 557 | out << "};\n"; |
| 558 | } |
| 559 | else |
| 560 | { |
| 561 | if (mUsesDepthRange) |
| 562 | { |
| 563 | out << "uniform float3 dx_DepthRange : register(c0);"; |
| 564 | } |
| 565 | |
| 566 | if (mUsesFragCoord) |
| 567 | { |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 568 | out << "uniform float4 dx_ViewCoords : register(c1);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | if (mUsesFragCoord || mUsesFrontFacing) |
| 572 | { |
| 573 | out << "uniform float3 dx_DepthFront : register(c2);\n"; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | out << "\n"; |
| 578 | |
| 579 | if (mUsesDepthRange) |
| 580 | { |
| 581 | out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n" |
| 582 | "\n"; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 583 | } |
daniel@transgaming.com | 5024cc4 | 2010-04-20 18:52:04 +0000 | [diff] [blame] | 584 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 585 | if (!flaggedStructs.empty()) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 586 | { |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 587 | out << "// Std140 Structures accessed by value\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 588 | out << "\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 589 | out << flaggedStructs; |
| 590 | out << "\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 591 | } |
| 592 | |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 593 | if (usingMRTExtension && mNumRenderTargets > 1) |
| 594 | { |
| 595 | out << "#define GL_USES_MRT\n"; |
| 596 | } |
| 597 | |
| 598 | if (mUsesFragColor) |
| 599 | { |
| 600 | out << "#define GL_USES_FRAG_COLOR\n"; |
| 601 | } |
| 602 | |
| 603 | if (mUsesFragData) |
| 604 | { |
| 605 | out << "#define GL_USES_FRAG_DATA\n"; |
| 606 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 607 | } |
daniel@transgaming.com | d25ab25 | 2010-03-30 03:36:26 +0000 | [diff] [blame] | 608 | else // Vertex shader |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 609 | { |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 610 | out << "// Attributes\n"; |
| 611 | out << attributes; |
| 612 | out << "\n" |
| 613 | "static float4 gl_Position = float4(0, 0, 0, 0);\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 614 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 615 | if (mUsesPointSize) |
| 616 | { |
| 617 | out << "static float gl_PointSize = float(1);\n"; |
| 618 | } |
| 619 | |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 620 | if (mUsesInstanceID) |
| 621 | { |
| 622 | out << "static int gl_InstanceID;"; |
| 623 | } |
| 624 | |
Corentin Wallez | b076add | 2016-01-11 16:45:46 -0500 | [diff] [blame] | 625 | if (mUsesVertexID) |
| 626 | { |
| 627 | out << "static int gl_VertexID;"; |
| 628 | } |
| 629 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 630 | out << "\n" |
| 631 | "// Varyings\n"; |
| 632 | out << varyings; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 633 | out << "\n"; |
| 634 | |
| 635 | if (mUsesDepthRange) |
| 636 | { |
| 637 | out << "struct gl_DepthRangeParameters\n" |
| 638 | "{\n" |
| 639 | " float near;\n" |
| 640 | " float far;\n" |
| 641 | " float diff;\n" |
| 642 | "};\n" |
| 643 | "\n"; |
| 644 | } |
| 645 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 646 | if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 647 | { |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 648 | out << "cbuffer DriverConstants : register(b1)\n" |
| 649 | "{\n"; |
| 650 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 651 | if (mUsesDepthRange) |
| 652 | { |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 653 | out << " float3 dx_DepthRange : packoffset(c0);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 654 | } |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 655 | |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 656 | // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 |
| 657 | // shaders. However, we declare it for all shaders (including Feature Level 10+). |
| 658 | // The bytecode is the same whether we declare it or not, since D3DCompiler removes it |
| 659 | // if it's unused. |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 660 | out << " float4 dx_ViewAdjust : packoffset(c1);\n"; |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 661 | out << " float2 dx_ViewCoords : packoffset(c2);\n"; |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 662 | out << " float2 dx_ViewScale : packoffset(c3);\n"; |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 663 | |
Olli Etuaho | 618bebc | 2016-01-15 16:40:00 +0200 | [diff] [blame] | 664 | if (mOutputType == SH_HLSL_4_1_OUTPUT) |
| 665 | { |
| 666 | mUniformHLSL->samplerMetadataUniforms(out, "c4"); |
| 667 | } |
| 668 | |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 669 | out << "};\n" |
| 670 | "\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 671 | } |
| 672 | else |
| 673 | { |
| 674 | if (mUsesDepthRange) |
| 675 | { |
| 676 | out << "uniform float3 dx_DepthRange : register(c0);\n"; |
| 677 | } |
| 678 | |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 679 | out << "uniform float4 dx_ViewAdjust : register(c1);\n"; |
| 680 | out << "uniform float2 dx_ViewCoords : register(c2);\n" |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 681 | "\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 682 | } |
| 683 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 684 | if (mUsesDepthRange) |
| 685 | { |
| 686 | out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n" |
| 687 | "\n"; |
| 688 | } |
| 689 | |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 690 | if (!flaggedStructs.empty()) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 691 | { |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 692 | out << "// Std140 Structures accessed by value\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 693 | out << "\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 694 | out << flaggedStructs; |
| 695 | out << "\n"; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 696 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 697 | } |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 698 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 699 | for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++) |
| 700 | { |
| 701 | // Return type |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 702 | if (textureFunction->method == TextureFunction::SIZE) |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 703 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 704 | switch(textureFunction->sampler) |
| 705 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 706 | case EbtSampler2D: out << "int2 "; break; |
| 707 | case EbtSampler3D: out << "int3 "; break; |
| 708 | case EbtSamplerCube: out << "int2 "; break; |
| 709 | case EbtSampler2DArray: out << "int3 "; break; |
| 710 | case EbtISampler2D: out << "int2 "; break; |
| 711 | case EbtISampler3D: out << "int3 "; break; |
| 712 | case EbtISamplerCube: out << "int2 "; break; |
| 713 | case EbtISampler2DArray: out << "int3 "; break; |
| 714 | case EbtUSampler2D: out << "int2 "; break; |
| 715 | case EbtUSampler3D: out << "int3 "; break; |
| 716 | case EbtUSamplerCube: out << "int2 "; break; |
| 717 | case EbtUSampler2DArray: out << "int3 "; break; |
| 718 | case EbtSampler2DShadow: out << "int2 "; break; |
| 719 | case EbtSamplerCubeShadow: out << "int2 "; break; |
| 720 | case EbtSampler2DArrayShadow: out << "int3 "; break; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 721 | default: UNREACHABLE(); |
| 722 | } |
| 723 | } |
| 724 | else // Sampling function |
| 725 | { |
| 726 | switch(textureFunction->sampler) |
| 727 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 728 | case EbtSampler2D: out << "float4 "; break; |
| 729 | case EbtSampler3D: out << "float4 "; break; |
| 730 | case EbtSamplerCube: out << "float4 "; break; |
| 731 | case EbtSampler2DArray: out << "float4 "; break; |
| 732 | case EbtISampler2D: out << "int4 "; break; |
| 733 | case EbtISampler3D: out << "int4 "; break; |
| 734 | case EbtISamplerCube: out << "int4 "; break; |
| 735 | case EbtISampler2DArray: out << "int4 "; break; |
| 736 | case EbtUSampler2D: out << "uint4 "; break; |
| 737 | case EbtUSampler3D: out << "uint4 "; break; |
| 738 | case EbtUSamplerCube: out << "uint4 "; break; |
| 739 | case EbtUSampler2DArray: out << "uint4 "; break; |
| 740 | case EbtSampler2DShadow: out << "float "; break; |
| 741 | case EbtSamplerCubeShadow: out << "float "; break; |
| 742 | case EbtSampler2DArrayShadow: out << "float "; break; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 743 | default: UNREACHABLE(); |
| 744 | } |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 747 | // Function name |
| 748 | out << textureFunction->name(); |
| 749 | |
| 750 | // Argument list |
| 751 | int hlslCoords = 4; |
| 752 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 753 | if (mOutputType == SH_HLSL_3_0_OUTPUT) |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 754 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 755 | switch(textureFunction->sampler) |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 756 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 757 | case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break; |
| 758 | case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break; |
| 759 | default: UNREACHABLE(); |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 760 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 761 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 762 | switch(textureFunction->method) |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 763 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 764 | case TextureFunction::IMPLICIT: break; |
| 765 | case TextureFunction::BIAS: hlslCoords = 4; break; |
| 766 | case TextureFunction::LOD: hlslCoords = 4; break; |
| 767 | case TextureFunction::LOD0: hlslCoords = 4; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 768 | case TextureFunction::LOD0BIAS: hlslCoords = 4; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 769 | default: UNREACHABLE(); |
shannon.woods@transgaming.com | fb256be | 2013-01-25 21:49:25 +0000 | [diff] [blame] | 770 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 771 | } |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 772 | else |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 773 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 774 | hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler); |
| 775 | if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 776 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 777 | out << TextureString(textureFunction->sampler) << " x, " |
| 778 | << SamplerString(textureFunction->sampler) << " s"; |
| 779 | } |
| 780 | else |
| 781 | { |
| 782 | ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT); |
| 783 | out << "const uint samplerIndex"; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 784 | } |
| 785 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 786 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 787 | if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 788 | { |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 789 | switch(textureFunction->coords) |
| 790 | { |
| 791 | case 2: out << ", int2 t"; break; |
| 792 | case 3: out << ", int3 t"; break; |
| 793 | default: UNREACHABLE(); |
| 794 | } |
| 795 | } |
| 796 | else // Floating-point coordinates (except textureSize) |
| 797 | { |
| 798 | switch(textureFunction->coords) |
| 799 | { |
| 800 | case 1: out << ", int lod"; break; // textureSize() |
| 801 | case 2: out << ", float2 t"; break; |
| 802 | case 3: out << ", float3 t"; break; |
| 803 | case 4: out << ", float4 t"; break; |
| 804 | default: UNREACHABLE(); |
| 805 | } |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 808 | if (textureFunction->method == TextureFunction::GRAD) |
| 809 | { |
| 810 | switch(textureFunction->sampler) |
| 811 | { |
| 812 | case EbtSampler2D: |
| 813 | case EbtISampler2D: |
| 814 | case EbtUSampler2D: |
| 815 | case EbtSampler2DArray: |
| 816 | case EbtISampler2DArray: |
| 817 | case EbtUSampler2DArray: |
| 818 | case EbtSampler2DShadow: |
| 819 | case EbtSampler2DArrayShadow: |
| 820 | out << ", float2 ddx, float2 ddy"; |
| 821 | break; |
| 822 | case EbtSampler3D: |
| 823 | case EbtISampler3D: |
| 824 | case EbtUSampler3D: |
| 825 | case EbtSamplerCube: |
| 826 | case EbtISamplerCube: |
| 827 | case EbtUSamplerCube: |
| 828 | case EbtSamplerCubeShadow: |
| 829 | out << ", float3 ddx, float3 ddy"; |
| 830 | break; |
| 831 | default: UNREACHABLE(); |
| 832 | } |
| 833 | } |
| 834 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 835 | switch(textureFunction->method) |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 836 | { |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 837 | case TextureFunction::IMPLICIT: break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 838 | case TextureFunction::BIAS: break; // Comes after the offset parameter |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 839 | case TextureFunction::LOD: out << ", float lod"; break; |
| 840 | case TextureFunction::LOD0: break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 841 | case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 842 | case TextureFunction::SIZE: break; |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 843 | case TextureFunction::FETCH: out << ", int mip"; break; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 844 | case TextureFunction::GRAD: break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 845 | default: UNREACHABLE(); |
daniel@transgaming.com | 1579519 | 2011-05-11 15:36:20 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 848 | if (textureFunction->offset) |
| 849 | { |
| 850 | switch(textureFunction->sampler) |
| 851 | { |
| 852 | case EbtSampler2D: out << ", int2 offset"; break; |
| 853 | case EbtSampler3D: out << ", int3 offset"; break; |
| 854 | case EbtSampler2DArray: out << ", int2 offset"; break; |
| 855 | case EbtISampler2D: out << ", int2 offset"; break; |
| 856 | case EbtISampler3D: out << ", int3 offset"; break; |
| 857 | case EbtISampler2DArray: out << ", int2 offset"; break; |
| 858 | case EbtUSampler2D: out << ", int2 offset"; break; |
| 859 | case EbtUSampler3D: out << ", int3 offset"; break; |
| 860 | case EbtUSampler2DArray: out << ", int2 offset"; break; |
| 861 | case EbtSampler2DShadow: out << ", int2 offset"; break; |
Nicolas Capens | bf7db10 | 2014-02-19 17:20:28 -0500 | [diff] [blame] | 862 | case EbtSampler2DArrayShadow: out << ", int2 offset"; break; |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 863 | default: UNREACHABLE(); |
| 864 | } |
| 865 | } |
| 866 | |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 867 | if (textureFunction->method == TextureFunction::BIAS || |
| 868 | textureFunction->method == TextureFunction::LOD0BIAS) |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 869 | { |
| 870 | out << ", float bias"; |
| 871 | } |
| 872 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 873 | out << ")\n" |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 874 | "{\n"; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 875 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 876 | // In some cases we use a variable to store the texture/sampler objects, but to work around |
| 877 | // a D3D11 compiler bug related to discard inside a loop that is conditional on texture |
| 878 | // sampling we need to call the function directly on a reference to the array. The bug was |
| 879 | // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests. |
| 880 | TString textureReference("x"); |
| 881 | TString samplerReference("s"); |
| 882 | if (mOutputType == SH_HLSL_4_1_OUTPUT) |
| 883 | { |
| 884 | TString suffix = TextureGroupSuffix(textureFunction->sampler); |
| 885 | if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D) |
| 886 | { |
| 887 | textureReference = TString("textures") + suffix + "[samplerIndex]"; |
| 888 | samplerReference = TString("samplers") + suffix + "[samplerIndex]"; |
| 889 | } |
| 890 | else |
| 891 | { |
| 892 | out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix |
| 893 | << ";\n"; |
| 894 | textureReference = TString("textures") + suffix + "[textureIndex]"; |
| 895 | out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset" |
| 896 | << suffix << ";\n"; |
| 897 | samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]"; |
| 898 | } |
| 899 | } |
| 900 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 901 | if (textureFunction->method == TextureFunction::SIZE) |
| 902 | { |
Olli Etuaho | b079c7a | 2016-04-01 12:32:52 +0300 | [diff] [blame^] | 903 | out << "int baseLevel = samplerMetadata[samplerIndex].baseLevel;\n"; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 904 | if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler)) |
| 905 | { |
Olli Etuaho | bce743a | 2016-01-15 17:18:28 +0200 | [diff] [blame] | 906 | if (IsSamplerArray(textureFunction->sampler) || |
| 907 | (IsIntegerSampler(textureFunction->sampler) && |
| 908 | IsSamplerCube(textureFunction->sampler))) |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 909 | { |
| 910 | out << " uint width; uint height; uint layers; uint numberOfLevels;\n" |
Olli Etuaho | bce743a | 2016-01-15 17:18:28 +0200 | [diff] [blame] | 911 | << " " << textureReference << ".GetDimensions(baseLevel + lod, width, " |
| 912 | "height, layers, numberOfLevels);\n"; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 913 | } |
| 914 | else |
| 915 | { |
| 916 | out << " uint width; uint height; uint numberOfLevels;\n" |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 917 | << " " << textureReference |
Olli Etuaho | bce743a | 2016-01-15 17:18:28 +0200 | [diff] [blame] | 918 | << ".GetDimensions(baseLevel + lod, width, height, numberOfLevels);\n"; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 919 | } |
| 920 | } |
| 921 | else if (IsSampler3D(textureFunction->sampler)) |
| 922 | { |
| 923 | out << " uint width; uint height; uint depth; uint numberOfLevels;\n" |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 924 | << " " << textureReference |
Olli Etuaho | bce743a | 2016-01-15 17:18:28 +0200 | [diff] [blame] | 925 | << ".GetDimensions(baseLevel + lod, width, height, depth, numberOfLevels);\n"; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 926 | } |
| 927 | else UNREACHABLE(); |
| 928 | |
| 929 | switch(textureFunction->sampler) |
| 930 | { |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 931 | case EbtSampler2D: out << " return int2(width, height);"; break; |
| 932 | case EbtSampler3D: out << " return int3(width, height, depth);"; break; |
| 933 | case EbtSamplerCube: out << " return int2(width, height);"; break; |
| 934 | case EbtSampler2DArray: out << " return int3(width, height, layers);"; break; |
| 935 | case EbtISampler2D: out << " return int2(width, height);"; break; |
| 936 | case EbtISampler3D: out << " return int3(width, height, depth);"; break; |
| 937 | case EbtISamplerCube: out << " return int2(width, height);"; break; |
| 938 | case EbtISampler2DArray: out << " return int3(width, height, layers);"; break; |
| 939 | case EbtUSampler2D: out << " return int2(width, height);"; break; |
| 940 | case EbtUSampler3D: out << " return int3(width, height, depth);"; break; |
| 941 | case EbtUSamplerCube: out << " return int2(width, height);"; break; |
| 942 | case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break; |
| 943 | case EbtSampler2DShadow: out << " return int2(width, height);"; break; |
| 944 | case EbtSamplerCubeShadow: out << " return int2(width, height);"; break; |
| 945 | case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break; |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 946 | default: UNREACHABLE(); |
| 947 | } |
| 948 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 949 | else |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 950 | { |
Olli Etuaho | d4102f0 | 2016-01-22 14:54:04 +0200 | [diff] [blame] | 951 | TString texCoordX("t.x"); |
| 952 | TString texCoordY("t.y"); |
| 953 | TString texCoordZ("t.z"); |
| 954 | TString proj = ""; |
| 955 | |
| 956 | if (textureFunction->proj) |
| 957 | { |
| 958 | switch (textureFunction->coords) |
| 959 | { |
| 960 | case 3: |
| 961 | proj = " / t.z"; |
| 962 | break; |
| 963 | case 4: |
| 964 | proj = " / t.w"; |
| 965 | break; |
| 966 | default: |
| 967 | UNREACHABLE(); |
| 968 | } |
| 969 | if (proj != "") |
| 970 | { |
| 971 | texCoordX = "(" + texCoordX + proj + ")"; |
| 972 | texCoordY = "(" + texCoordY + proj + ")"; |
| 973 | texCoordZ = "(" + texCoordZ + proj + ")"; |
| 974 | } |
| 975 | } |
| 976 | |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 977 | if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler)) |
| 978 | { |
| 979 | out << " float width; float height; float layers; float levels;\n"; |
| 980 | |
| 981 | out << " uint mip = 0;\n"; |
| 982 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 983 | out << " " << textureReference |
| 984 | << ".GetDimensions(mip, width, height, layers, levels);\n"; |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 985 | |
| 986 | out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n"; |
| 987 | out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n"; |
| 988 | out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n"; |
| 989 | out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n"; |
| 990 | |
| 991 | // FACE_POSITIVE_X = 000b |
| 992 | // FACE_NEGATIVE_X = 001b |
| 993 | // FACE_POSITIVE_Y = 010b |
| 994 | // FACE_NEGATIVE_Y = 011b |
| 995 | // FACE_POSITIVE_Z = 100b |
| 996 | // FACE_NEGATIVE_Z = 101b |
| 997 | out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n"; |
| 998 | |
| 999 | out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n"; |
| 1000 | out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n"; |
| 1001 | out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n"; |
| 1002 | |
| 1003 | out << " t.x = (u * 0.5f / m) + 0.5f;\n"; |
| 1004 | out << " t.y = (v * 0.5f / m) + 0.5f;\n"; |
Jamie Madill | 8d9f35f | 2015-11-24 16:10:20 -0500 | [diff] [blame] | 1005 | |
| 1006 | // Mip level computation. |
Olli Etuaho | ced8705 | 2016-04-04 16:34:27 +0300 | [diff] [blame] | 1007 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 1008 | textureFunction->method == TextureFunction::LOD || |
| 1009 | textureFunction->method == TextureFunction::GRAD) |
Jamie Madill | 8d9f35f | 2015-11-24 16:10:20 -0500 | [diff] [blame] | 1010 | { |
Olli Etuaho | ced8705 | 2016-04-04 16:34:27 +0300 | [diff] [blame] | 1011 | if (textureFunction->method == TextureFunction::IMPLICIT) |
| 1012 | { |
| 1013 | out << " float2 tSized = float2(t.x * width, t.y * height);\n" |
| 1014 | " float2 dx = ddx(tSized);\n" |
| 1015 | " float2 dy = ddy(tSized);\n" |
| 1016 | " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"; |
| 1017 | } |
| 1018 | else if (textureFunction->method == TextureFunction::GRAD) |
| 1019 | { |
| 1020 | // ESSL 3.00.6 spec section 8.8: "For the cube version, the partial |
| 1021 | // derivatives of P are assumed to be in the coordinate system used before |
| 1022 | // texture coordinates are projected onto the appropriate cube face." |
| 1023 | // ddx[0] and ddy[0] are the derivatives of t.x passed into the function |
| 1024 | // ddx[1] and ddy[1] are the derivatives of t.y passed into the function |
| 1025 | // ddx[2] and ddy[2] are the derivatives of t.z passed into the function |
| 1026 | // Determine the derivatives of u, v and m |
| 1027 | out << " float dudx = xMajor ? ddx[2] : (yMajor && t.y < 0.0f ? -ddx[0] " |
| 1028 | ": ddx[0]);\n" |
| 1029 | " float dudy = xMajor ? ddy[2] : (yMajor && t.y < 0.0f ? -ddy[0] " |
| 1030 | ": ddy[0]);\n" |
| 1031 | " float dvdx = yMajor ? ddx[2] : (negative ? ddx[1] : -ddx[1]);\n" |
| 1032 | " float dvdy = yMajor ? ddy[2] : (negative ? ddy[1] : -ddy[1]);\n" |
| 1033 | " float dmdx = xMajor ? ddx[0] : (yMajor ? ddx[1] : ddx[2]);\n" |
| 1034 | " float dmdy = xMajor ? ddy[0] : (yMajor ? ddy[1] : ddy[2]);\n"; |
| 1035 | // Now determine the derivatives of the face coordinates, using the |
| 1036 | // derivatives calculated above. |
| 1037 | // d / dx (u(x) * 0.5 / m(x) + 0.5) |
| 1038 | // = 0.5 * (m(x) * u'(x) - u(x) * m'(x)) / m(x)^2 |
| 1039 | out << " float dfacexdx = 0.5f * (m * dudx - u * dmdx) / (m * m);\n" |
| 1040 | " float dfaceydx = 0.5f * (m * dvdx - v * dmdx) / (m * m);\n" |
| 1041 | " float dfacexdy = 0.5f * (m * dudy - u * dmdy) / (m * m);\n" |
| 1042 | " float dfaceydy = 0.5f * (m * dvdy - v * dmdy) / (m * m);\n" |
| 1043 | " float2 sizeVec = float2(width, height);\n" |
| 1044 | " float2 faceddx = float2(dfacexdx, dfaceydx) * sizeVec;\n" |
| 1045 | " float2 faceddy = float2(dfacexdy, dfaceydy) * sizeVec;\n"; |
| 1046 | // Optimization: instead of: log2(max(length(faceddx), length(faceddy))) |
| 1047 | // we compute: log2(max(length(faceddx)^2, length(faceddy)^2)) / 2 |
| 1048 | out << " float lengthfaceddx2 = dot(faceddx, faceddx);\n" |
| 1049 | " float lengthfaceddy2 = dot(faceddy, faceddy);\n" |
| 1050 | " float lod = log2(max(lengthfaceddx2, lengthfaceddy2)) * " |
| 1051 | "0.5f;\n"; |
| 1052 | } |
| 1053 | out << " mip = uint(min(max(round(lod), 0), levels - 1));\n" |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1054 | << " " << textureReference |
| 1055 | << ".GetDimensions(mip, width, height, layers, levels);\n"; |
Jamie Madill | 8d9f35f | 2015-11-24 16:10:20 -0500 | [diff] [blame] | 1056 | } |
Olli Etuaho | d4102f0 | 2016-01-22 14:54:04 +0200 | [diff] [blame] | 1057 | |
| 1058 | // Convert from normalized floating-point to integer |
| 1059 | texCoordX = "int(floor(width * frac(" + texCoordX + ")))"; |
| 1060 | texCoordY = "int(floor(height * frac(" + texCoordY + ")))"; |
| 1061 | texCoordZ = "face"; |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1062 | } |
| 1063 | else if (IsIntegerSampler(textureFunction->sampler) && |
| 1064 | textureFunction->method != TextureFunction::FETCH) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1065 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1066 | if (IsSampler2D(textureFunction->sampler)) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1067 | { |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 1068 | if (IsSamplerArray(textureFunction->sampler)) |
| 1069 | { |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1070 | out << " float width; float height; float layers; float levels;\n"; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1071 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1072 | if (textureFunction->method == TextureFunction::LOD0) |
| 1073 | { |
| 1074 | out << " uint mip = 0;\n"; |
| 1075 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1076 | else if (textureFunction->method == TextureFunction::LOD0BIAS) |
| 1077 | { |
| 1078 | out << " uint mip = bias;\n"; |
| 1079 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1080 | else |
| 1081 | { |
Olli Etuaho | e8e4deb | 2015-11-18 17:15:38 +0200 | [diff] [blame] | 1082 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1083 | out << " " << textureReference |
| 1084 | << ".GetDimensions(0, width, height, layers, levels);\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1085 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 1086 | textureFunction->method == TextureFunction::BIAS) |
| 1087 | { |
Olli Etuaho | e8e4deb | 2015-11-18 17:15:38 +0200 | [diff] [blame] | 1088 | out << " float2 tSized = float2(t.x * width, t.y * height);\n" |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1089 | " float dx = length(ddx(tSized));\n" |
| 1090 | " float dy = length(ddy(tSized));\n" |
Jamie Madill | 03847b6 | 2013-11-13 19:42:39 -0500 | [diff] [blame] | 1091 | " float lod = log2(max(dx, dy));\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1092 | |
| 1093 | if (textureFunction->method == TextureFunction::BIAS) |
| 1094 | { |
| 1095 | out << " lod += bias;\n"; |
| 1096 | } |
| 1097 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1098 | else if (textureFunction->method == TextureFunction::GRAD) |
| 1099 | { |
Olli Etuaho | ced8705 | 2016-04-04 16:34:27 +0300 | [diff] [blame] | 1100 | out << " float2 sizeVec = float2(width, height);\n" |
| 1101 | " float2 sizeDdx = ddx * sizeVec;\n" |
| 1102 | " float2 sizeDdy = ddy * sizeVec;\n" |
| 1103 | " float lod = log2(max(dot(sizeDdx, sizeDdx), " |
| 1104 | "dot(sizeDdy, sizeDdy))) * 0.5f;\n"; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1105 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1106 | |
| 1107 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 1108 | } |
| 1109 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1110 | out << " " << textureReference |
| 1111 | << ".GetDimensions(mip, width, height, layers, levels);\n"; |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 1112 | } |
| 1113 | else |
| 1114 | { |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1115 | out << " float width; float height; float levels;\n"; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1116 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1117 | if (textureFunction->method == TextureFunction::LOD0) |
| 1118 | { |
| 1119 | out << " uint mip = 0;\n"; |
| 1120 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1121 | else if (textureFunction->method == TextureFunction::LOD0BIAS) |
| 1122 | { |
| 1123 | out << " uint mip = bias;\n"; |
| 1124 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1125 | else |
| 1126 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1127 | out << " " << textureReference |
| 1128 | << ".GetDimensions(0, width, height, levels);\n"; |
Olli Etuaho | e8e4deb | 2015-11-18 17:15:38 +0200 | [diff] [blame] | 1129 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1130 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 1131 | textureFunction->method == TextureFunction::BIAS) |
| 1132 | { |
Olli Etuaho | e8e4deb | 2015-11-18 17:15:38 +0200 | [diff] [blame] | 1133 | out << " float2 tSized = float2(t.x * width, t.y * height);\n" |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1134 | " float dx = length(ddx(tSized));\n" |
| 1135 | " float dy = length(ddy(tSized));\n" |
Jamie Madill | 03847b6 | 2013-11-13 19:42:39 -0500 | [diff] [blame] | 1136 | " float lod = log2(max(dx, dy));\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1137 | |
| 1138 | if (textureFunction->method == TextureFunction::BIAS) |
| 1139 | { |
| 1140 | out << " lod += bias;\n"; |
| 1141 | } |
| 1142 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1143 | else if (textureFunction->method == TextureFunction::GRAD) |
| 1144 | { |
Olli Etuaho | ced8705 | 2016-04-04 16:34:27 +0300 | [diff] [blame] | 1145 | out << " float2 sizeVec = float2(width, height);\n" |
| 1146 | " float2 sizeDdx = ddx * sizeVec;\n" |
| 1147 | " float2 sizeDdy = ddy * sizeVec;\n" |
| 1148 | " float lod = log2(max(dot(sizeDdx, sizeDdx), " |
| 1149 | "dot(sizeDdy, sizeDdy))) * 0.5f;\n"; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1150 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1151 | |
| 1152 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 1153 | } |
| 1154 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1155 | out << " " << textureReference |
| 1156 | << ".GetDimensions(mip, width, height, levels);\n"; |
Nicolas Capens | 93e50de | 2013-07-09 13:46:28 -0400 | [diff] [blame] | 1157 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1158 | } |
| 1159 | else if (IsSampler3D(textureFunction->sampler)) |
| 1160 | { |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1161 | out << " float width; float height; float depth; float levels;\n"; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1162 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1163 | if (textureFunction->method == TextureFunction::LOD0) |
| 1164 | { |
| 1165 | out << " uint mip = 0;\n"; |
| 1166 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1167 | else if (textureFunction->method == TextureFunction::LOD0BIAS) |
| 1168 | { |
| 1169 | out << " uint mip = bias;\n"; |
| 1170 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1171 | else |
| 1172 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1173 | out << " " << textureReference |
| 1174 | << ".GetDimensions(0, width, height, depth, levels);\n"; |
Olli Etuaho | e8e4deb | 2015-11-18 17:15:38 +0200 | [diff] [blame] | 1175 | |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1176 | if (textureFunction->method == TextureFunction::IMPLICIT || |
| 1177 | textureFunction->method == TextureFunction::BIAS) |
| 1178 | { |
Olli Etuaho | e8e4deb | 2015-11-18 17:15:38 +0200 | [diff] [blame] | 1179 | out << " float3 tSized = float3(t.x * width, t.y * height, t.z * " |
| 1180 | "depth);\n" |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1181 | " float dx = length(ddx(tSized));\n" |
| 1182 | " float dy = length(ddy(tSized));\n" |
Jamie Madill | 03847b6 | 2013-11-13 19:42:39 -0500 | [diff] [blame] | 1183 | " float lod = log2(max(dx, dy));\n"; |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1184 | |
| 1185 | if (textureFunction->method == TextureFunction::BIAS) |
| 1186 | { |
| 1187 | out << " lod += bias;\n"; |
| 1188 | } |
| 1189 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1190 | else if (textureFunction->method == TextureFunction::GRAD) |
| 1191 | { |
Olli Etuaho | ced8705 | 2016-04-04 16:34:27 +0300 | [diff] [blame] | 1192 | out << " float3 sizeVec = float3(width, height, depth);\n" |
| 1193 | " float3 sizeDdx = ddx * sizeVec;\n" |
| 1194 | " float3 sizeDdy = ddy * sizeVec;\n" |
| 1195 | " float lod = log2(max(dot(sizeDdx, sizeDdx), dot(sizeDdy, " |
| 1196 | "sizeDdy))) * 0.5f;\n"; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1197 | } |
Nicolas Capens | 9edebd6 | 2013-08-06 10:59:10 -0400 | [diff] [blame] | 1198 | |
| 1199 | out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n"; |
| 1200 | } |
| 1201 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1202 | out << " " << textureReference |
| 1203 | << ".GetDimensions(mip, width, height, depth, levels);\n"; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1204 | } |
| 1205 | else UNREACHABLE(); |
Olli Etuaho | d4102f0 | 2016-01-22 14:54:04 +0200 | [diff] [blame] | 1206 | |
| 1207 | // Convert from normalized floating-point to integer |
Olli Etuaho | b079c7a | 2016-04-01 12:32:52 +0300 | [diff] [blame^] | 1208 | out << "int wrapS = samplerMetadata[samplerIndex].wrapModes & 0x3;\n"; |
| 1209 | if (textureFunction->offset) |
| 1210 | { |
| 1211 | OutputIntTexCoordWrap(out, "wrapS", "width", texCoordX, "offset.x", "tix"); |
| 1212 | } |
| 1213 | else |
| 1214 | { |
| 1215 | OutputIntTexCoordWrap(out, "wrapS", "width", texCoordX, "0", "tix"); |
| 1216 | } |
| 1217 | texCoordX = "tix"; |
| 1218 | out << "int wrapT = (samplerMetadata[samplerIndex].wrapModes >> 2) & 0x3;\n"; |
| 1219 | if (textureFunction->offset) |
| 1220 | { |
| 1221 | OutputIntTexCoordWrap(out, "wrapT", "height", texCoordY, "offset.y", "tiy"); |
| 1222 | } |
| 1223 | else |
| 1224 | { |
| 1225 | OutputIntTexCoordWrap(out, "wrapT", "height", texCoordY, "0", "tiy"); |
| 1226 | } |
| 1227 | texCoordY = "tiy"; |
Olli Etuaho | d4102f0 | 2016-01-22 14:54:04 +0200 | [diff] [blame] | 1228 | |
| 1229 | if (IsSamplerArray(textureFunction->sampler)) |
| 1230 | { |
| 1231 | texCoordZ = "int(max(0, min(layers - 1, floor(0.5 + t.z))))"; |
| 1232 | } |
| 1233 | else if (!IsSamplerCube(textureFunction->sampler) && |
| 1234 | !IsSampler2D(textureFunction->sampler)) |
| 1235 | { |
Olli Etuaho | b079c7a | 2016-04-01 12:32:52 +0300 | [diff] [blame^] | 1236 | out << "int wrapR = (samplerMetadata[samplerIndex].wrapModes >> 4) & 0x3;\n"; |
| 1237 | if (textureFunction->offset) |
| 1238 | { |
| 1239 | OutputIntTexCoordWrap(out, "wrapR", "depth", texCoordZ, "offset.z", "tiz"); |
| 1240 | } |
| 1241 | else |
| 1242 | { |
| 1243 | OutputIntTexCoordWrap(out, "wrapR", "depth", texCoordZ, "0", "tiz"); |
| 1244 | } |
| 1245 | texCoordZ = "tiz"; |
Olli Etuaho | d4102f0 | 2016-01-22 14:54:04 +0200 | [diff] [blame] | 1246 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1247 | } |
| 1248 | |
| 1249 | out << " return "; |
| 1250 | |
| 1251 | // HLSL intrinsic |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1252 | if (mOutputType == SH_HLSL_3_0_OUTPUT) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1253 | { |
| 1254 | switch(textureFunction->sampler) |
| 1255 | { |
| 1256 | case EbtSampler2D: out << "tex2D"; break; |
| 1257 | case EbtSamplerCube: out << "texCUBE"; break; |
| 1258 | default: UNREACHABLE(); |
| 1259 | } |
| 1260 | |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1261 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1262 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1263 | case TextureFunction::IMPLICIT: |
| 1264 | out << "(" << samplerReference << ", "; |
| 1265 | break; |
| 1266 | case TextureFunction::BIAS: |
| 1267 | out << "bias(" << samplerReference << ", "; |
| 1268 | break; |
| 1269 | case TextureFunction::LOD: |
| 1270 | out << "lod(" << samplerReference << ", "; |
| 1271 | break; |
| 1272 | case TextureFunction::LOD0: |
| 1273 | out << "lod(" << samplerReference << ", "; |
| 1274 | break; |
| 1275 | case TextureFunction::LOD0BIAS: |
| 1276 | out << "lod(" << samplerReference << ", "; |
| 1277 | break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1278 | default: UNREACHABLE(); |
| 1279 | } |
| 1280 | } |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1281 | else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1282 | { |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1283 | if (textureFunction->method == TextureFunction::GRAD) |
| 1284 | { |
| 1285 | if (IsIntegerSampler(textureFunction->sampler)) |
| 1286 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1287 | out << "" << textureReference << ".Load("; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1288 | } |
| 1289 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1290 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1291 | out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference |
| 1292 | << ", "; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1293 | } |
| 1294 | else |
| 1295 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1296 | out << "" << textureReference << ".SampleGrad(" << samplerReference << ", "; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | else if (IsIntegerSampler(textureFunction->sampler) || |
| 1300 | textureFunction->method == TextureFunction::FETCH) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1301 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1302 | out << "" << textureReference << ".Load("; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1303 | } |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1304 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1305 | { |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1306 | switch(textureFunction->method) |
| 1307 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1308 | case TextureFunction::IMPLICIT: |
| 1309 | out << "" << textureReference << ".SampleCmp(" << samplerReference |
| 1310 | << ", "; |
| 1311 | break; |
| 1312 | case TextureFunction::BIAS: |
| 1313 | out << "" << textureReference << ".SampleCmp(" << samplerReference |
| 1314 | << ", "; |
| 1315 | break; |
| 1316 | case TextureFunction::LOD: |
| 1317 | out << "" << textureReference << ".SampleCmp(" << samplerReference |
| 1318 | << ", "; |
| 1319 | break; |
| 1320 | case TextureFunction::LOD0: |
| 1321 | out << "" << textureReference << ".SampleCmpLevelZero(" |
| 1322 | << samplerReference << ", "; |
| 1323 | break; |
| 1324 | case TextureFunction::LOD0BIAS: |
| 1325 | out << "" << textureReference << ".SampleCmpLevelZero(" |
| 1326 | << samplerReference << ", "; |
| 1327 | break; |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1328 | default: UNREACHABLE(); |
| 1329 | } |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1330 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1331 | else |
| 1332 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1333 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1334 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1335 | case TextureFunction::IMPLICIT: |
| 1336 | out << "" << textureReference << ".Sample(" << samplerReference << ", "; |
| 1337 | break; |
| 1338 | case TextureFunction::BIAS: |
| 1339 | out << "" << textureReference << ".SampleBias(" << samplerReference |
| 1340 | << ", "; |
| 1341 | break; |
| 1342 | case TextureFunction::LOD: |
| 1343 | out << "" << textureReference << ".SampleLevel(" << samplerReference |
| 1344 | << ", "; |
| 1345 | break; |
| 1346 | case TextureFunction::LOD0: |
| 1347 | out << "" << textureReference << ".SampleLevel(" << samplerReference |
| 1348 | << ", "; |
| 1349 | break; |
| 1350 | case TextureFunction::LOD0BIAS: |
| 1351 | out << "" << textureReference << ".SampleLevel(" << samplerReference |
| 1352 | << ", "; |
| 1353 | break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1354 | default: UNREACHABLE(); |
| 1355 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1356 | } |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1357 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1358 | else UNREACHABLE(); |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1359 | |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 1360 | if (IsIntegerSampler(textureFunction->sampler) || |
| 1361 | textureFunction->method == TextureFunction::FETCH) |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1362 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1363 | switch(hlslCoords) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1364 | { |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1365 | case 2: out << "int3("; break; |
| 1366 | case 3: out << "int4("; break; |
| 1367 | default: UNREACHABLE(); |
| 1368 | } |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1369 | } |
| 1370 | else |
| 1371 | { |
| 1372 | switch(hlslCoords) |
| 1373 | { |
| 1374 | case 2: out << "float2("; break; |
| 1375 | case 3: out << "float3("; break; |
| 1376 | case 4: out << "float4("; break; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1377 | default: UNREACHABLE(); |
| 1378 | } |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1379 | } |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 1380 | |
Olli Etuaho | d4102f0 | 2016-01-22 14:54:04 +0200 | [diff] [blame] | 1381 | out << texCoordX << ", " << texCoordY; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1382 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1383 | if (mOutputType == SH_HLSL_3_0_OUTPUT) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1384 | { |
| 1385 | if (hlslCoords >= 3) |
| 1386 | { |
| 1387 | if (textureFunction->coords < 3) |
| 1388 | { |
| 1389 | out << ", 0"; |
| 1390 | } |
| 1391 | else |
| 1392 | { |
Olli Etuaho | d4102f0 | 2016-01-22 14:54:04 +0200 | [diff] [blame] | 1393 | out << ", t.z" << proj; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | if (hlslCoords == 4) |
| 1398 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1399 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1400 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1401 | case TextureFunction::BIAS: out << ", bias"; break; |
| 1402 | case TextureFunction::LOD: out << ", lod"; break; |
| 1403 | case TextureFunction::LOD0: out << ", 0"; break; |
| 1404 | case TextureFunction::LOD0BIAS: out << ", bias"; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1405 | default: UNREACHABLE(); |
| 1406 | } |
| 1407 | } |
| 1408 | |
| 1409 | out << "));\n"; |
| 1410 | } |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 1411 | else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1412 | { |
| 1413 | if (hlslCoords >= 3) |
| 1414 | { |
Olli Etuaho | d4102f0 | 2016-01-22 14:54:04 +0200 | [diff] [blame] | 1415 | ASSERT(!IsIntegerSampler(textureFunction->sampler) || |
| 1416 | !IsSamplerCube(textureFunction->sampler) || texCoordZ == "face"); |
| 1417 | out << ", " << texCoordZ; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1418 | } |
| 1419 | |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1420 | if (textureFunction->method == TextureFunction::GRAD) |
| 1421 | { |
| 1422 | if (IsIntegerSampler(textureFunction->sampler)) |
| 1423 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1424 | out << ", mip)"; |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1425 | } |
| 1426 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1427 | { |
Nicolas Capens | 0027fa9 | 2014-02-20 14:26:42 -0500 | [diff] [blame] | 1428 | // Compare value |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1429 | if (textureFunction->proj) |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1430 | { |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1431 | // According to ESSL 3.00.4 sec 8.8 p95 on textureProj: |
| 1432 | // The resulting third component of P' in the shadow forms is used as Dref |
| 1433 | out << "), t.z" << proj; |
| 1434 | } |
| 1435 | else |
| 1436 | { |
| 1437 | switch(textureFunction->coords) |
| 1438 | { |
| 1439 | case 3: out << "), t.z"; break; |
| 1440 | case 4: out << "), t.w"; break; |
| 1441 | default: UNREACHABLE(); |
| 1442 | } |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | else |
| 1446 | { |
| 1447 | out << "), ddx, ddy"; |
| 1448 | } |
| 1449 | } |
| 1450 | else if (IsIntegerSampler(textureFunction->sampler) || |
| 1451 | textureFunction->method == TextureFunction::FETCH) |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1452 | { |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1453 | out << ", mip)"; |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1454 | } |
| 1455 | else if (IsShadowSampler(textureFunction->sampler)) |
| 1456 | { |
| 1457 | // Compare value |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1458 | if (textureFunction->proj) |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1459 | { |
Gregoire Payen de La Garanderie | 5cc9ac8 | 2015-02-20 11:27:56 +0000 | [diff] [blame] | 1460 | // According to ESSL 3.00.4 sec 8.8 p95 on textureProj: |
| 1461 | // The resulting third component of P' in the shadow forms is used as Dref |
| 1462 | out << "), t.z" << proj; |
| 1463 | } |
| 1464 | else |
| 1465 | { |
| 1466 | switch(textureFunction->coords) |
| 1467 | { |
| 1468 | case 3: out << "), t.z"; break; |
| 1469 | case 4: out << "), t.w"; break; |
| 1470 | default: UNREACHABLE(); |
| 1471 | } |
Nicolas Capens | cb127d3 | 2013-07-15 17:26:18 -0400 | [diff] [blame] | 1472 | } |
| 1473 | } |
| 1474 | else |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1475 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 1476 | switch(textureFunction->method) |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1477 | { |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1478 | case TextureFunction::IMPLICIT: out << ")"; break; |
| 1479 | case TextureFunction::BIAS: out << "), bias"; break; |
| 1480 | case TextureFunction::LOD: out << "), lod"; break; |
| 1481 | case TextureFunction::LOD0: out << "), 0"; break; |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 1482 | case TextureFunction::LOD0BIAS: out << "), bias"; break; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1483 | default: UNREACHABLE(); |
| 1484 | } |
| 1485 | } |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1486 | |
Olli Etuaho | b079c7a | 2016-04-01 12:32:52 +0300 | [diff] [blame^] | 1487 | if (textureFunction->offset && (!IsIntegerSampler(textureFunction->sampler) || |
| 1488 | textureFunction->method == TextureFunction::FETCH)) |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 1489 | { |
| 1490 | out << ", offset"; |
| 1491 | } |
| 1492 | |
| 1493 | out << ");"; |
Nicolas Capens | 6d232bb | 2013-07-08 15:56:38 -0400 | [diff] [blame] | 1494 | } |
| 1495 | else UNREACHABLE(); |
| 1496 | } |
| 1497 | |
| 1498 | out << "\n" |
| 1499 | "}\n" |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 1500 | "\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 1503 | if (mUsesFragCoord) |
| 1504 | { |
| 1505 | out << "#define GL_USES_FRAG_COORD\n"; |
| 1506 | } |
| 1507 | |
| 1508 | if (mUsesPointCoord) |
| 1509 | { |
| 1510 | out << "#define GL_USES_POINT_COORD\n"; |
| 1511 | } |
| 1512 | |
| 1513 | if (mUsesFrontFacing) |
| 1514 | { |
| 1515 | out << "#define GL_USES_FRONT_FACING\n"; |
| 1516 | } |
| 1517 | |
| 1518 | if (mUsesPointSize) |
| 1519 | { |
| 1520 | out << "#define GL_USES_POINT_SIZE\n"; |
| 1521 | } |
| 1522 | |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 1523 | if (mUsesFragDepth) |
| 1524 | { |
| 1525 | out << "#define GL_USES_FRAG_DEPTH\n"; |
| 1526 | } |
| 1527 | |
shannonwoods@chromium.org | 0329988 | 2013-05-30 00:05:26 +0000 | [diff] [blame] | 1528 | if (mUsesDepthRange) |
| 1529 | { |
| 1530 | out << "#define GL_USES_DEPTH_RANGE\n"; |
| 1531 | } |
| 1532 | |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 1533 | if (mUsesXor) |
| 1534 | { |
| 1535 | out << "bool xor(bool p, bool q)\n" |
| 1536 | "{\n" |
| 1537 | " return (p || q) && !(p && q);\n" |
| 1538 | "}\n" |
| 1539 | "\n"; |
| 1540 | } |
| 1541 | |
Olli Etuaho | 95cd3c6 | 2015-03-03 16:45:32 +0200 | [diff] [blame] | 1542 | builtInFunctionEmulator->OutputEmulatedFunctions(out); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | void OutputHLSL::visitSymbol(TIntermSymbol *node) |
| 1546 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1547 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1548 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 1549 | // Handle accessing std140 structs by value |
| 1550 | if (mFlaggedStructMappedNames.count(node) > 0) |
| 1551 | { |
| 1552 | out << mFlaggedStructMappedNames[node]; |
| 1553 | return; |
| 1554 | } |
| 1555 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1556 | TString name = node->getSymbol(); |
| 1557 | |
shannon.woods%transgaming.com@gtempaccount.com | e7d4a24 | 2013-04-13 03:38:33 +0000 | [diff] [blame] | 1558 | if (name == "gl_DepthRange") |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 1559 | { |
| 1560 | mUsesDepthRange = true; |
| 1561 | out << name; |
| 1562 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1563 | else |
| 1564 | { |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1565 | TQualifier qualifier = node->getQualifier(); |
| 1566 | |
| 1567 | if (qualifier == EvqUniform) |
| 1568 | { |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 1569 | const TType &nodeType = node->getType(); |
| 1570 | const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1571 | |
| 1572 | if (interfaceBlock) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1573 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1574 | mReferencedInterfaceBlocks[interfaceBlock->name()] = node; |
shannonwoods@chromium.org | 4430b0d | 2013-05-30 00:12:34 +0000 | [diff] [blame] | 1575 | } |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1576 | else |
| 1577 | { |
| 1578 | mReferencedUniforms[name] = node; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1579 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1580 | |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 1581 | ensureStructDefined(nodeType); |
| 1582 | |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1583 | const TName &nameWithMetadata = node->getName(); |
| 1584 | out << DecorateUniform(nameWithMetadata, nodeType); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1585 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1586 | else if (qualifier == EvqAttribute || qualifier == EvqVertexIn) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1587 | { |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 1588 | mReferencedAttributes[name] = node; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1589 | out << Decorate(name); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1590 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1591 | else if (IsVarying(qualifier)) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1592 | { |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 1593 | mReferencedVaryings[name] = node; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1594 | out << Decorate(name); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1595 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1596 | else if (qualifier == EvqFragmentOut) |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 1597 | { |
| 1598 | mReferencedOutputVariables[name] = node; |
| 1599 | out << "out_" << name; |
| 1600 | } |
| 1601 | else if (qualifier == EvqFragColor) |
shannon.woods%transgaming.com@gtempaccount.com | e7d4a24 | 2013-04-13 03:38:33 +0000 | [diff] [blame] | 1602 | { |
| 1603 | out << "gl_Color[0]"; |
| 1604 | mUsesFragColor = true; |
| 1605 | } |
| 1606 | else if (qualifier == EvqFragData) |
| 1607 | { |
| 1608 | out << "gl_Color"; |
| 1609 | mUsesFragData = true; |
| 1610 | } |
| 1611 | else if (qualifier == EvqFragCoord) |
| 1612 | { |
| 1613 | mUsesFragCoord = true; |
| 1614 | out << name; |
| 1615 | } |
| 1616 | else if (qualifier == EvqPointCoord) |
| 1617 | { |
| 1618 | mUsesPointCoord = true; |
| 1619 | out << name; |
| 1620 | } |
| 1621 | else if (qualifier == EvqFrontFacing) |
| 1622 | { |
| 1623 | mUsesFrontFacing = true; |
| 1624 | out << name; |
| 1625 | } |
| 1626 | else if (qualifier == EvqPointSize) |
| 1627 | { |
| 1628 | mUsesPointSize = true; |
| 1629 | out << name; |
| 1630 | } |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 1631 | else if (qualifier == EvqInstanceID) |
| 1632 | { |
| 1633 | mUsesInstanceID = true; |
| 1634 | out << name; |
| 1635 | } |
Corentin Wallez | b076add | 2016-01-11 16:45:46 -0500 | [diff] [blame] | 1636 | else if (qualifier == EvqVertexID) |
| 1637 | { |
| 1638 | mUsesVertexID = true; |
| 1639 | out << name; |
| 1640 | } |
Kimmo Kinnunen | 5f0246c | 2015-07-22 10:30:35 +0300 | [diff] [blame] | 1641 | else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth") |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 1642 | { |
| 1643 | mUsesFragDepth = true; |
| 1644 | out << "gl_Depth"; |
| 1645 | } |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1646 | else |
| 1647 | { |
Olli Etuaho | f5cfc8d | 2015-08-06 16:36:39 +0300 | [diff] [blame] | 1648 | out << DecorateIfNeeded(node->getName()); |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1649 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1650 | } |
| 1651 | } |
| 1652 | |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 1653 | void OutputHLSL::visitRaw(TIntermRaw *node) |
| 1654 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1655 | getInfoSink() << node->getRawText(); |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 1656 | } |
| 1657 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1658 | void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out) |
| 1659 | { |
| 1660 | if (type.isScalar() && !type.isArray()) |
| 1661 | { |
| 1662 | if (op == EOpEqual) |
| 1663 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1664 | outputTriplet(out, visit, "(", " == ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1665 | } |
| 1666 | else |
| 1667 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1668 | outputTriplet(out, visit, "(", " != ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1669 | } |
| 1670 | } |
| 1671 | else |
| 1672 | { |
| 1673 | if (visit == PreVisit && op == EOpNotEqual) |
| 1674 | { |
| 1675 | out << "!"; |
| 1676 | } |
| 1677 | |
| 1678 | if (type.isArray()) |
| 1679 | { |
| 1680 | const TString &functionName = addArrayEqualityFunction(type); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1681 | outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1682 | } |
| 1683 | else if (type.getBasicType() == EbtStruct) |
| 1684 | { |
| 1685 | const TStructure &structure = *type.getStruct(); |
| 1686 | const TString &functionName = addStructEqualityFunction(structure); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1687 | outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1688 | } |
| 1689 | else |
| 1690 | { |
| 1691 | ASSERT(type.isMatrix() || type.isVector()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1692 | outputTriplet(out, visit, "all(", " == ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1693 | } |
| 1694 | } |
| 1695 | } |
| 1696 | |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1697 | bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit) |
| 1698 | { |
| 1699 | // Inside InVisit the current node is already in the path. |
| 1700 | const unsigned int initialN = visit == InVisit ? 1u : 0u; |
| 1701 | for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n) |
| 1702 | { |
| 1703 | TIntermNode *ancestor = getAncestorNode(n); |
| 1704 | const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode(); |
| 1705 | if (ancestorBinary == nullptr) |
| 1706 | { |
| 1707 | return false; |
| 1708 | } |
| 1709 | switch (ancestorBinary->getOp()) |
| 1710 | { |
| 1711 | case EOpIndexDirectStruct: |
| 1712 | { |
| 1713 | const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct(); |
| 1714 | const TIntermConstantUnion *index = |
| 1715 | ancestorBinary->getRight()->getAsConstantUnion(); |
| 1716 | const TField *field = structure->fields()[index->getIConst(0)]; |
| 1717 | if (IsSampler(field->type()->getBasicType())) |
| 1718 | { |
| 1719 | return true; |
| 1720 | } |
| 1721 | break; |
| 1722 | } |
| 1723 | case EOpIndexDirect: |
| 1724 | break; |
| 1725 | default: |
| 1726 | // Returning a sampler from indirect indexing is not supported. |
| 1727 | return false; |
| 1728 | } |
| 1729 | } |
| 1730 | return false; |
| 1731 | } |
| 1732 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1733 | bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node) |
| 1734 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1735 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1736 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 1737 | // Handle accessing std140 structs by value |
| 1738 | if (mFlaggedStructMappedNames.count(node) > 0) |
| 1739 | { |
| 1740 | out << mFlaggedStructMappedNames[node]; |
| 1741 | return false; |
| 1742 | } |
| 1743 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1744 | switch (node->getOp()) |
| 1745 | { |
Olli Etuaho | e79904c | 2015-03-18 16:56:42 +0200 | [diff] [blame] | 1746 | case EOpAssign: |
| 1747 | if (node->getLeft()->isArray()) |
| 1748 | { |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 1749 | TIntermAggregate *rightAgg = node->getRight()->getAsAggregate(); |
| 1750 | if (rightAgg != nullptr && rightAgg->isConstructor()) |
| 1751 | { |
| 1752 | const TString &functionName = addArrayConstructIntoFunction(node->getType()); |
| 1753 | out << functionName << "("; |
| 1754 | node->getLeft()->traverse(this); |
| 1755 | TIntermSequence *seq = rightAgg->getSequence(); |
| 1756 | for (auto &arrayElement : *seq) |
| 1757 | { |
| 1758 | out << ", "; |
| 1759 | arrayElement->traverse(this); |
| 1760 | } |
| 1761 | out << ")"; |
| 1762 | return false; |
| 1763 | } |
Olli Etuaho | a8c414b | 2015-04-16 15:51:03 +0300 | [diff] [blame] | 1764 | // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned. |
| 1765 | ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall); |
| 1766 | |
| 1767 | const TString &functionName = addArrayAssignmentFunction(node->getType()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1768 | outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")"); |
Olli Etuaho | e79904c | 2015-03-18 16:56:42 +0200 | [diff] [blame] | 1769 | } |
| 1770 | else |
| 1771 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1772 | outputTriplet(out, visit, "(", " = ", ")"); |
Olli Etuaho | e79904c | 2015-03-18 16:56:42 +0200 | [diff] [blame] | 1773 | } |
| 1774 | break; |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1775 | case EOpInitialize: |
| 1776 | if (visit == PreVisit) |
| 1777 | { |
| 1778 | // GLSL allows to write things like "float x = x;" where a new variable x is defined |
| 1779 | // and the value of an existing variable x is assigned. HLSL uses C semantics (the |
| 1780 | // new variable is created before the assignment is evaluated), so we need to convert |
| 1781 | // this to "float t = x, x = t;". |
| 1782 | |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1783 | TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode(); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1784 | ASSERT(symbolNode); |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1785 | TIntermTyped *expression = node->getRight(); |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1786 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1787 | // TODO (jmadill): do a 'deep' scan to know if an expression is statically const |
| 1788 | if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst) |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1789 | { |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1790 | // For variables which are not constant, defer their real initialization until |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 1791 | // after we initialize uniforms. |
| 1792 | TIntermBinary *deferredInit = new TIntermBinary(EOpAssign); |
| 1793 | deferredInit->setLeft(node->getLeft()); |
| 1794 | deferredInit->setRight(node->getRight()); |
| 1795 | deferredInit->setType(node->getType()); |
| 1796 | mDeferredGlobalInitializers.push_back(deferredInit); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 1797 | const TString &initString = initializer(node->getType()); |
| 1798 | node->setRight(new TIntermRaw(node->getType(), initString)); |
| 1799 | } |
| 1800 | else if (writeSameSymbolInitializer(out, symbolNode, expression)) |
| 1801 | { |
| 1802 | // Skip initializing the rest of the expression |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1803 | return false; |
| 1804 | } |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 1805 | else if (writeConstantInitialization(out, symbolNode, expression)) |
| 1806 | { |
| 1807 | return false; |
| 1808 | } |
daniel@transgaming.com | bdfb2e5 | 2010-11-15 16:41:20 +0000 | [diff] [blame] | 1809 | } |
| 1810 | else if (visit == InVisit) |
| 1811 | { |
| 1812 | out << " = "; |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 1813 | } |
| 1814 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1815 | case EOpAddAssign: |
| 1816 | outputTriplet(out, visit, "(", " += ", ")"); |
| 1817 | break; |
| 1818 | case EOpSubAssign: |
| 1819 | outputTriplet(out, visit, "(", " -= ", ")"); |
| 1820 | break; |
| 1821 | case EOpMulAssign: |
| 1822 | outputTriplet(out, visit, "(", " *= ", ")"); |
| 1823 | break; |
| 1824 | case EOpVectorTimesScalarAssign: |
| 1825 | outputTriplet(out, visit, "(", " *= ", ")"); |
| 1826 | break; |
| 1827 | case EOpMatrixTimesScalarAssign: |
| 1828 | outputTriplet(out, visit, "(", " *= ", ")"); |
| 1829 | break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1830 | case EOpVectorTimesMatrixAssign: |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1831 | if (visit == PreVisit) |
| 1832 | { |
| 1833 | out << "("; |
| 1834 | } |
| 1835 | else if (visit == InVisit) |
| 1836 | { |
| 1837 | out << " = mul("; |
| 1838 | node->getLeft()->traverse(this); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1839 | out << ", transpose("; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1840 | } |
| 1841 | else |
| 1842 | { |
daniel@transgaming.com | 3aa7420 | 2010-04-29 03:39:04 +0000 | [diff] [blame] | 1843 | out << ")))"; |
daniel@transgaming.com | 8976c1e | 2010-04-13 19:53:27 +0000 | [diff] [blame] | 1844 | } |
| 1845 | break; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1846 | case EOpMatrixTimesMatrixAssign: |
| 1847 | if (visit == PreVisit) |
| 1848 | { |
| 1849 | out << "("; |
| 1850 | } |
| 1851 | else if (visit == InVisit) |
| 1852 | { |
Olli Etuaho | fc7fab7 | 2015-03-06 12:03:18 +0200 | [diff] [blame] | 1853 | out << " = transpose(mul(transpose("; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1854 | node->getLeft()->traverse(this); |
Olli Etuaho | fc7fab7 | 2015-03-06 12:03:18 +0200 | [diff] [blame] | 1855 | out << "), transpose("; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1856 | } |
| 1857 | else |
| 1858 | { |
Olli Etuaho | fc7fab7 | 2015-03-06 12:03:18 +0200 | [diff] [blame] | 1859 | out << "))))"; |
daniel@transgaming.com | 93a96c3 | 2010-03-28 19:36:13 +0000 | [diff] [blame] | 1860 | } |
| 1861 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1862 | case EOpDivAssign: |
| 1863 | outputTriplet(out, visit, "(", " /= ", ")"); |
| 1864 | break; |
| 1865 | case EOpIModAssign: |
| 1866 | outputTriplet(out, visit, "(", " %= ", ")"); |
| 1867 | break; |
| 1868 | case EOpBitShiftLeftAssign: |
| 1869 | outputTriplet(out, visit, "(", " <<= ", ")"); |
| 1870 | break; |
| 1871 | case EOpBitShiftRightAssign: |
| 1872 | outputTriplet(out, visit, "(", " >>= ", ")"); |
| 1873 | break; |
| 1874 | case EOpBitwiseAndAssign: |
| 1875 | outputTriplet(out, visit, "(", " &= ", ")"); |
| 1876 | break; |
| 1877 | case EOpBitwiseXorAssign: |
| 1878 | outputTriplet(out, visit, "(", " ^= ", ")"); |
| 1879 | break; |
| 1880 | case EOpBitwiseOrAssign: |
| 1881 | outputTriplet(out, visit, "(", " |= ", ")"); |
| 1882 | break; |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1883 | case EOpIndexDirect: |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1884 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1885 | const TType& leftType = node->getLeft()->getType(); |
| 1886 | if (leftType.isInterfaceBlock()) |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1887 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1888 | if (visit == PreVisit) |
| 1889 | { |
| 1890 | TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock(); |
| 1891 | const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1892 | mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 1893 | out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1894 | return false; |
| 1895 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1896 | } |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1897 | else if (ancestorEvaluatesToSamplerInStruct(visit)) |
| 1898 | { |
| 1899 | // All parts of an expression that access a sampler in a struct need to use _ as |
| 1900 | // separator to access the sampler variable that has been moved out of the struct. |
| 1901 | outputTriplet(out, visit, "", "_", ""); |
| 1902 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1903 | else |
| 1904 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1905 | outputTriplet(out, visit, "", "[", "]"); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1906 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1907 | } |
| 1908 | break; |
| 1909 | case EOpIndexIndirect: |
| 1910 | // We do not currently support indirect references to interface blocks |
| 1911 | ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1912 | outputTriplet(out, visit, "", "[", "]"); |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1913 | break; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1914 | case EOpIndexDirectStruct: |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1915 | { |
| 1916 | const TStructure* structure = node->getLeft()->getType().getStruct(); |
| 1917 | const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); |
| 1918 | const TField* field = structure->fields()[index->getIConst(0)]; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1919 | |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1920 | // In cases where indexing returns a sampler, we need to access the sampler variable |
| 1921 | // that has been moved out of the struct. |
| 1922 | bool indexingReturnsSampler = IsSampler(field->type()->getBasicType()); |
| 1923 | if (visit == PreVisit && indexingReturnsSampler) |
| 1924 | { |
| 1925 | // Samplers extracted from structs have "angle" prefix to avoid name conflicts. |
| 1926 | // This prefix is only output at the beginning of the indexing expression, which |
| 1927 | // may have multiple parts. |
| 1928 | out << "angle"; |
| 1929 | } |
| 1930 | if (!indexingReturnsSampler) |
| 1931 | { |
| 1932 | // All parts of an expression that access a sampler in a struct need to use _ as |
| 1933 | // separator to access the sampler variable that has been moved out of the struct. |
| 1934 | indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit); |
| 1935 | } |
| 1936 | if (visit == InVisit) |
| 1937 | { |
| 1938 | if (indexingReturnsSampler) |
| 1939 | { |
| 1940 | out << "_" + field->name(); |
| 1941 | } |
| 1942 | else |
| 1943 | { |
| 1944 | out << "." + DecorateField(field->name(), *structure); |
| 1945 | } |
| 1946 | |
| 1947 | return false; |
| 1948 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1949 | } |
| 1950 | break; |
shannonwoods@chromium.org | 4430b0d | 2013-05-30 00:12:34 +0000 | [diff] [blame] | 1951 | case EOpIndexDirectInterfaceBlock: |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1952 | if (visit == InVisit) |
| 1953 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1954 | const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock(); |
| 1955 | const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion(); |
| 1956 | const TField* field = interfaceBlock->fields()[index->getIConst(0)]; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1957 | out << "." + Decorate(field->name()); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1958 | |
| 1959 | return false; |
| 1960 | } |
| 1961 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1962 | case EOpVectorSwizzle: |
| 1963 | if (visit == InVisit) |
| 1964 | { |
| 1965 | out << "."; |
| 1966 | |
| 1967 | TIntermAggregate *swizzle = node->getRight()->getAsAggregate(); |
| 1968 | |
| 1969 | if (swizzle) |
| 1970 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1971 | TIntermSequence *sequence = swizzle->getSequence(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1972 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 1973 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1974 | { |
| 1975 | TIntermConstantUnion *element = (*sit)->getAsConstantUnion(); |
| 1976 | |
| 1977 | if (element) |
| 1978 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 1979 | int i = element->getIConst(0); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1980 | |
| 1981 | switch (i) |
| 1982 | { |
| 1983 | case 0: out << "x"; break; |
| 1984 | case 1: out << "y"; break; |
| 1985 | case 2: out << "z"; break; |
| 1986 | case 3: out << "w"; break; |
| 1987 | default: UNREACHABLE(); |
| 1988 | } |
| 1989 | } |
| 1990 | else UNREACHABLE(); |
| 1991 | } |
| 1992 | } |
| 1993 | else UNREACHABLE(); |
| 1994 | |
| 1995 | return false; // Fully processed |
| 1996 | } |
| 1997 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1998 | case EOpAdd: |
| 1999 | outputTriplet(out, visit, "(", " + ", ")"); |
| 2000 | break; |
| 2001 | case EOpSub: |
| 2002 | outputTriplet(out, visit, "(", " - ", ")"); |
| 2003 | break; |
| 2004 | case EOpMul: |
| 2005 | outputTriplet(out, visit, "(", " * ", ")"); |
| 2006 | break; |
| 2007 | case EOpDiv: |
| 2008 | outputTriplet(out, visit, "(", " / ", ")"); |
| 2009 | break; |
| 2010 | case EOpIMod: |
| 2011 | outputTriplet(out, visit, "(", " % ", ")"); |
| 2012 | break; |
| 2013 | case EOpBitShiftLeft: |
| 2014 | outputTriplet(out, visit, "(", " << ", ")"); |
| 2015 | break; |
| 2016 | case EOpBitShiftRight: |
| 2017 | outputTriplet(out, visit, "(", " >> ", ")"); |
| 2018 | break; |
| 2019 | case EOpBitwiseAnd: |
| 2020 | outputTriplet(out, visit, "(", " & ", ")"); |
| 2021 | break; |
| 2022 | case EOpBitwiseXor: |
| 2023 | outputTriplet(out, visit, "(", " ^ ", ")"); |
| 2024 | break; |
| 2025 | case EOpBitwiseOr: |
| 2026 | outputTriplet(out, visit, "(", " | ", ")"); |
| 2027 | break; |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 2028 | case EOpEqual: |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 2029 | case EOpNotEqual: |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 2030 | outputEqual(visit, node->getLeft()->getType(), node->getOp(), out); |
daniel@transgaming.com | 49bce7e | 2010-03-17 03:58:51 +0000 | [diff] [blame] | 2031 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2032 | case EOpLessThan: |
| 2033 | outputTriplet(out, visit, "(", " < ", ")"); |
| 2034 | break; |
| 2035 | case EOpGreaterThan: |
| 2036 | outputTriplet(out, visit, "(", " > ", ")"); |
| 2037 | break; |
| 2038 | case EOpLessThanEqual: |
| 2039 | outputTriplet(out, visit, "(", " <= ", ")"); |
| 2040 | break; |
| 2041 | case EOpGreaterThanEqual: |
| 2042 | outputTriplet(out, visit, "(", " >= ", ")"); |
| 2043 | break; |
| 2044 | case EOpVectorTimesScalar: |
| 2045 | outputTriplet(out, visit, "(", " * ", ")"); |
| 2046 | break; |
| 2047 | case EOpMatrixTimesScalar: |
| 2048 | outputTriplet(out, visit, "(", " * ", ")"); |
| 2049 | break; |
| 2050 | case EOpVectorTimesMatrix: |
| 2051 | outputTriplet(out, visit, "mul(", ", transpose(", "))"); |
| 2052 | break; |
| 2053 | case EOpMatrixTimesVector: |
| 2054 | outputTriplet(out, visit, "mul(transpose(", "), ", ")"); |
| 2055 | break; |
| 2056 | case EOpMatrixTimesMatrix: |
| 2057 | outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))"); |
| 2058 | break; |
daniel@transgaming.com | 8915eba | 2012-04-28 00:34:44 +0000 | [diff] [blame] | 2059 | case EOpLogicalOr: |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2060 | // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded. |
| 2061 | ASSERT(!node->getRight()->hasSideEffects()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2062 | outputTriplet(out, visit, "(", " || ", ")"); |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2063 | return true; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2064 | case EOpLogicalXor: |
| 2065 | mUsesXor = true; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2066 | outputTriplet(out, visit, "xor(", ", ", ")"); |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2067 | break; |
daniel@transgaming.com | 8915eba | 2012-04-28 00:34:44 +0000 | [diff] [blame] | 2068 | case EOpLogicalAnd: |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2069 | // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded. |
| 2070 | ASSERT(!node->getRight()->hasSideEffects()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2071 | outputTriplet(out, visit, "(", " && ", ")"); |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2072 | return true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2073 | default: UNREACHABLE(); |
| 2074 | } |
| 2075 | |
| 2076 | return true; |
| 2077 | } |
| 2078 | |
| 2079 | bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node) |
| 2080 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2081 | TInfoSinkBase &out = getInfoSink(); |
| 2082 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2083 | switch (node->getOp()) |
| 2084 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2085 | case EOpNegative: |
| 2086 | outputTriplet(out, visit, "(-", "", ")"); |
| 2087 | break; |
| 2088 | case EOpPositive: |
| 2089 | outputTriplet(out, visit, "(+", "", ")"); |
| 2090 | break; |
| 2091 | case EOpVectorLogicalNot: |
| 2092 | outputTriplet(out, visit, "(!", "", ")"); |
| 2093 | break; |
| 2094 | case EOpLogicalNot: |
| 2095 | outputTriplet(out, visit, "(!", "", ")"); |
| 2096 | break; |
| 2097 | case EOpBitwiseNot: |
| 2098 | outputTriplet(out, visit, "(~", "", ")"); |
| 2099 | break; |
| 2100 | case EOpPostIncrement: |
| 2101 | outputTriplet(out, visit, "(", "", "++)"); |
| 2102 | break; |
| 2103 | case EOpPostDecrement: |
| 2104 | outputTriplet(out, visit, "(", "", "--)"); |
| 2105 | break; |
| 2106 | case EOpPreIncrement: |
| 2107 | outputTriplet(out, visit, "(++", "", ")"); |
| 2108 | break; |
| 2109 | case EOpPreDecrement: |
| 2110 | outputTriplet(out, visit, "(--", "", ")"); |
| 2111 | break; |
| 2112 | case EOpRadians: |
| 2113 | outputTriplet(out, visit, "radians(", "", ")"); |
| 2114 | break; |
| 2115 | case EOpDegrees: |
| 2116 | outputTriplet(out, visit, "degrees(", "", ")"); |
| 2117 | break; |
| 2118 | case EOpSin: |
| 2119 | outputTriplet(out, visit, "sin(", "", ")"); |
| 2120 | break; |
| 2121 | case EOpCos: |
| 2122 | outputTriplet(out, visit, "cos(", "", ")"); |
| 2123 | break; |
| 2124 | case EOpTan: |
| 2125 | outputTriplet(out, visit, "tan(", "", ")"); |
| 2126 | break; |
| 2127 | case EOpAsin: |
| 2128 | outputTriplet(out, visit, "asin(", "", ")"); |
| 2129 | break; |
| 2130 | case EOpAcos: |
| 2131 | outputTriplet(out, visit, "acos(", "", ")"); |
| 2132 | break; |
| 2133 | case EOpAtan: |
| 2134 | outputTriplet(out, visit, "atan(", "", ")"); |
| 2135 | break; |
| 2136 | case EOpSinh: |
| 2137 | outputTriplet(out, visit, "sinh(", "", ")"); |
| 2138 | break; |
| 2139 | case EOpCosh: |
| 2140 | outputTriplet(out, visit, "cosh(", "", ")"); |
| 2141 | break; |
| 2142 | case EOpTanh: |
| 2143 | outputTriplet(out, visit, "tanh(", "", ")"); |
| 2144 | break; |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 2145 | case EOpAsinh: |
| 2146 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2147 | writeEmulatedFunctionTriplet(out, visit, "asinh("); |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 2148 | break; |
| 2149 | case EOpAcosh: |
| 2150 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2151 | writeEmulatedFunctionTriplet(out, visit, "acosh("); |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 2152 | break; |
| 2153 | case EOpAtanh: |
| 2154 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2155 | writeEmulatedFunctionTriplet(out, visit, "atanh("); |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 2156 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2157 | case EOpExp: |
| 2158 | outputTriplet(out, visit, "exp(", "", ")"); |
| 2159 | break; |
| 2160 | case EOpLog: |
| 2161 | outputTriplet(out, visit, "log(", "", ")"); |
| 2162 | break; |
| 2163 | case EOpExp2: |
| 2164 | outputTriplet(out, visit, "exp2(", "", ")"); |
| 2165 | break; |
| 2166 | case EOpLog2: |
| 2167 | outputTriplet(out, visit, "log2(", "", ")"); |
| 2168 | break; |
| 2169 | case EOpSqrt: |
| 2170 | outputTriplet(out, visit, "sqrt(", "", ")"); |
| 2171 | break; |
| 2172 | case EOpInverseSqrt: |
| 2173 | outputTriplet(out, visit, "rsqrt(", "", ")"); |
| 2174 | break; |
| 2175 | case EOpAbs: |
| 2176 | outputTriplet(out, visit, "abs(", "", ")"); |
| 2177 | break; |
| 2178 | case EOpSign: |
| 2179 | outputTriplet(out, visit, "sign(", "", ")"); |
| 2180 | break; |
| 2181 | case EOpFloor: |
| 2182 | outputTriplet(out, visit, "floor(", "", ")"); |
| 2183 | break; |
| 2184 | case EOpTrunc: |
| 2185 | outputTriplet(out, visit, "trunc(", "", ")"); |
| 2186 | break; |
| 2187 | case EOpRound: |
| 2188 | outputTriplet(out, visit, "round(", "", ")"); |
| 2189 | break; |
Qingqing Deng | 5dbece5 | 2015-02-27 20:35:38 -0800 | [diff] [blame] | 2190 | case EOpRoundEven: |
| 2191 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2192 | writeEmulatedFunctionTriplet(out, visit, "roundEven("); |
Qingqing Deng | 5dbece5 | 2015-02-27 20:35:38 -0800 | [diff] [blame] | 2193 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2194 | case EOpCeil: |
| 2195 | outputTriplet(out, visit, "ceil(", "", ")"); |
| 2196 | break; |
| 2197 | case EOpFract: |
| 2198 | outputTriplet(out, visit, "frac(", "", ")"); |
| 2199 | break; |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 2200 | case EOpIsNan: |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2201 | outputTriplet(out, visit, "isnan(", "", ")"); |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 2202 | mRequiresIEEEStrictCompiling = true; |
| 2203 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2204 | case EOpIsInf: |
| 2205 | outputTriplet(out, visit, "isinf(", "", ")"); |
| 2206 | break; |
| 2207 | case EOpFloatBitsToInt: |
| 2208 | outputTriplet(out, visit, "asint(", "", ")"); |
| 2209 | break; |
| 2210 | case EOpFloatBitsToUint: |
| 2211 | outputTriplet(out, visit, "asuint(", "", ")"); |
| 2212 | break; |
| 2213 | case EOpIntBitsToFloat: |
| 2214 | outputTriplet(out, visit, "asfloat(", "", ")"); |
| 2215 | break; |
| 2216 | case EOpUintBitsToFloat: |
| 2217 | outputTriplet(out, visit, "asfloat(", "", ")"); |
| 2218 | break; |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 2219 | case EOpPackSnorm2x16: |
| 2220 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2221 | writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16("); |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 2222 | break; |
| 2223 | case EOpPackUnorm2x16: |
| 2224 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2225 | writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16("); |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 2226 | break; |
| 2227 | case EOpPackHalf2x16: |
| 2228 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2229 | writeEmulatedFunctionTriplet(out, visit, "packHalf2x16("); |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 2230 | break; |
| 2231 | case EOpUnpackSnorm2x16: |
| 2232 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2233 | writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16("); |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 2234 | break; |
| 2235 | case EOpUnpackUnorm2x16: |
| 2236 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2237 | writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16("); |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | case EOpUnpackHalf2x16: |
| 2240 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2241 | writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16("); |
Olli Etuaho | 7700ff6 | 2015-01-15 12:16:29 +0200 | [diff] [blame] | 2242 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2243 | case EOpLength: |
| 2244 | outputTriplet(out, visit, "length(", "", ")"); |
| 2245 | break; |
| 2246 | case EOpNormalize: |
| 2247 | outputTriplet(out, visit, "normalize(", "", ")"); |
| 2248 | break; |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 2249 | case EOpDFdx: |
| 2250 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 2251 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2252 | outputTriplet(out, visit, "(", "", ", 0.0)"); |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 2253 | } |
| 2254 | else |
| 2255 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2256 | outputTriplet(out, visit, "ddx(", "", ")"); |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 2257 | } |
| 2258 | break; |
| 2259 | case EOpDFdy: |
| 2260 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 2261 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2262 | outputTriplet(out, visit, "(", "", ", 0.0)"); |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 2263 | } |
| 2264 | else |
| 2265 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2266 | outputTriplet(out, visit, "ddy(", "", ")"); |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 2267 | } |
| 2268 | break; |
| 2269 | case EOpFwidth: |
| 2270 | if(mInsideDiscontinuousLoop || mOutputLod0Function) |
| 2271 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2272 | outputTriplet(out, visit, "(", "", ", 0.0)"); |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 2273 | } |
| 2274 | else |
| 2275 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2276 | outputTriplet(out, visit, "fwidth(", "", ")"); |
daniel@transgaming.com | ddbb45d | 2012-05-31 01:21:41 +0000 | [diff] [blame] | 2277 | } |
| 2278 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2279 | case EOpTranspose: |
| 2280 | outputTriplet(out, visit, "transpose(", "", ")"); |
| 2281 | break; |
| 2282 | case EOpDeterminant: |
| 2283 | outputTriplet(out, visit, "determinant(transpose(", "", "))"); |
| 2284 | break; |
Olli Etuaho | abf6dad | 2015-01-14 14:45:16 +0200 | [diff] [blame] | 2285 | case EOpInverse: |
| 2286 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2287 | writeEmulatedFunctionTriplet(out, visit, "inverse("); |
Olli Etuaho | abf6dad | 2015-01-14 14:45:16 +0200 | [diff] [blame] | 2288 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 2289 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2290 | case EOpAny: |
| 2291 | outputTriplet(out, visit, "any(", "", ")"); |
| 2292 | break; |
| 2293 | case EOpAll: |
| 2294 | outputTriplet(out, visit, "all(", "", ")"); |
| 2295 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2296 | default: UNREACHABLE(); |
| 2297 | } |
| 2298 | |
| 2299 | return true; |
| 2300 | } |
| 2301 | |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2302 | TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node) |
| 2303 | { |
| 2304 | if (node->getAsSymbolNode()) |
| 2305 | { |
| 2306 | return node->getAsSymbolNode()->getSymbol(); |
| 2307 | } |
| 2308 | TIntermBinary *nodeBinary = node->getAsBinaryNode(); |
| 2309 | switch (nodeBinary->getOp()) |
| 2310 | { |
| 2311 | case EOpIndexDirect: |
| 2312 | { |
| 2313 | int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0); |
| 2314 | |
| 2315 | TInfoSinkBase prefixSink; |
| 2316 | prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index; |
| 2317 | return TString(prefixSink.c_str()); |
| 2318 | } |
| 2319 | case EOpIndexDirectStruct: |
| 2320 | { |
| 2321 | TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct(); |
| 2322 | int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0); |
| 2323 | const TField *field = s->fields()[index]; |
| 2324 | |
| 2325 | TInfoSinkBase prefixSink; |
| 2326 | prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" |
| 2327 | << field->name(); |
| 2328 | return TString(prefixSink.c_str()); |
| 2329 | } |
| 2330 | default: |
| 2331 | UNREACHABLE(); |
| 2332 | return TString(""); |
| 2333 | } |
| 2334 | } |
| 2335 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2336 | bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node) |
| 2337 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2338 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2339 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2340 | switch (node->getOp()) |
| 2341 | { |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2342 | case EOpSequence: |
| 2343 | { |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 2344 | if (mInsideFunction) |
| 2345 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2346 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 2347 | out << "{\n"; |
| 2348 | } |
| 2349 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2350 | 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] | 2351 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2352 | outputLineDirective(out, (*sit)->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2353 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2354 | (*sit)->traverse(this); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2355 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2356 | // Don't output ; after case labels, they're terminated by : |
| 2357 | // This is needed especially since outputting a ; after a case statement would turn empty |
| 2358 | // case statements into non-empty case statements, disallowing fall-through from them. |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2359 | // Also no need to output ; after selection (if) statements or sequences. This is done just |
| 2360 | // for code clarity. |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2361 | TIntermSelection *asSelection = (*sit)->getAsSelectionNode(); |
| 2362 | ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator()); |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2363 | if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit)) |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2364 | out << ";\n"; |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 2367 | if (mInsideFunction) |
| 2368 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2369 | outputLineDirective(out, node->getLine().last_line); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 2370 | out << "}\n"; |
| 2371 | } |
| 2372 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2373 | return false; |
| 2374 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2375 | case EOpDeclaration: |
| 2376 | if (visit == PreVisit) |
| 2377 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2378 | TIntermSequence *sequence = node->getSequence(); |
| 2379 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2380 | ASSERT(sequence->size() == 1); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2381 | |
Olli Etuaho | b1edc4f | 2015-11-02 17:20:03 +0200 | [diff] [blame] | 2382 | if (variable && |
| 2383 | (variable->getQualifier() == EvqTemporary || |
| 2384 | variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst)) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2385 | { |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 2386 | ensureStructDefined(variable->getType()); |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 2387 | |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 2388 | if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2389 | { |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2390 | if (!mInsideFunction) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2391 | { |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2392 | out << "static "; |
| 2393 | } |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 2394 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2395 | out << TypeString(variable->getType()) + " "; |
Nicolas Capens | d974db4 | 2014-10-07 10:50:19 -0400 | [diff] [blame] | 2396 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2397 | TIntermSymbol *symbol = variable->getAsSymbolNode(); |
Nicolas Capens | d974db4 | 2014-10-07 10:50:19 -0400 | [diff] [blame] | 2398 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2399 | if (symbol) |
| 2400 | { |
| 2401 | symbol->traverse(this); |
| 2402 | out << ArrayString(symbol->getType()); |
| 2403 | out << " = " + initializer(symbol->getType()); |
| 2404 | } |
| 2405 | else |
| 2406 | { |
| 2407 | variable->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2408 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2409 | } |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 2410 | else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration |
| 2411 | { |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 2412 | // Already added to constructor map |
daniel@transgaming.com | fe56515 | 2010-04-10 05:29:07 +0000 | [diff] [blame] | 2413 | } |
| 2414 | else UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2415 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2416 | else if (variable && IsVaryingOut(variable->getQualifier())) |
shannon.woods@transgaming.com | cb332ab | 2013-02-28 23:12:18 +0000 | [diff] [blame] | 2417 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2418 | for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++) |
shannon.woods@transgaming.com | cb332ab | 2013-02-28 23:12:18 +0000 | [diff] [blame] | 2419 | { |
| 2420 | TIntermSymbol *symbol = (*sit)->getAsSymbolNode(); |
| 2421 | |
| 2422 | if (symbol) |
| 2423 | { |
| 2424 | // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking |
| 2425 | mReferencedVaryings[symbol->getSymbol()] = symbol; |
| 2426 | } |
| 2427 | else |
| 2428 | { |
| 2429 | (*sit)->traverse(this); |
| 2430 | } |
| 2431 | } |
| 2432 | } |
| 2433 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2434 | return false; |
| 2435 | } |
| 2436 | else if (visit == InVisit) |
| 2437 | { |
| 2438 | out << ", "; |
| 2439 | } |
| 2440 | break; |
Jamie Madill | 3b5c2da | 2014-08-19 15:23:32 -0400 | [diff] [blame] | 2441 | case EOpInvariantDeclaration: |
| 2442 | // Do not do any translation |
| 2443 | return false; |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2444 | case EOpPrototype: |
| 2445 | if (visit == PreVisit) |
| 2446 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2447 | size_t index = mCallDag.findIndex(node); |
| 2448 | // Skip the prototype if it is not implemented (and thus not used) |
| 2449 | if (index == CallDAG::InvalidIndex) |
| 2450 | { |
| 2451 | return false; |
| 2452 | } |
| 2453 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2454 | TIntermSequence *arguments = node->getSequence(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2455 | |
Olli Etuaho | be59c2f | 2016-03-07 11:32:34 +0200 | [diff] [blame] | 2456 | TString name = DecorateFunctionIfNeeded(node->getNameObj()); |
| 2457 | out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments) |
| 2458 | << (mOutputLod0Function ? "Lod0(" : "("); |
| 2459 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2460 | for (unsigned int i = 0; i < arguments->size(); i++) |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2461 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2462 | TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2463 | |
| 2464 | if (symbol) |
| 2465 | { |
| 2466 | out << argumentString(symbol); |
| 2467 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2468 | if (i < arguments->size() - 1) |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2469 | { |
| 2470 | out << ", "; |
| 2471 | } |
| 2472 | } |
| 2473 | else UNREACHABLE(); |
| 2474 | } |
| 2475 | |
| 2476 | out << ");\n"; |
| 2477 | |
daniel@transgaming.com | 0e5bb40 | 2012-10-17 18:24:53 +0000 | [diff] [blame] | 2478 | // Also prototype the Lod0 variant if needed |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2479 | bool needsLod0 = mASTMetadataList[index].mNeedsLod0; |
| 2480 | if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 0e5bb40 | 2012-10-17 18:24:53 +0000 | [diff] [blame] | 2481 | { |
| 2482 | mOutputLod0Function = true; |
| 2483 | node->traverse(this); |
| 2484 | mOutputLod0Function = false; |
| 2485 | } |
| 2486 | |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2487 | return false; |
| 2488 | } |
| 2489 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2490 | case EOpComma: |
| 2491 | outputTriplet(out, visit, "(", ", ", ")"); |
| 2492 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2493 | case EOpFunction: |
| 2494 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2495 | ASSERT(mCurrentFunctionMetadata == nullptr); |
Olli Etuaho | 59f9a64 | 2015-08-06 20:38:26 +0300 | [diff] [blame] | 2496 | TString name = TFunction::unmangleName(node->getNameObj().getString()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2497 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2498 | size_t index = mCallDag.findIndex(node); |
| 2499 | ASSERT(index != CallDAG::InvalidIndex); |
| 2500 | mCurrentFunctionMetadata = &mASTMetadataList[index]; |
| 2501 | |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2502 | out << TypeString(node->getType()) << " "; |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2503 | |
Olli Etuaho | be59c2f | 2016-03-07 11:32:34 +0200 | [diff] [blame] | 2504 | TIntermSequence *sequence = node->getSequence(); |
| 2505 | TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence(); |
| 2506 | |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2507 | if (name == "main") |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2508 | { |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2509 | out << "gl_main("; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2510 | } |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2511 | else |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2512 | { |
Olli Etuaho | 59f9a64 | 2015-08-06 20:38:26 +0300 | [diff] [blame] | 2513 | out << DecorateFunctionIfNeeded(node->getNameObj()) |
Olli Etuaho | be59c2f | 2016-03-07 11:32:34 +0200 | [diff] [blame] | 2514 | << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "("); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2515 | } |
daniel@transgaming.com | 6369186 | 2010-04-29 03:32:42 +0000 | [diff] [blame] | 2516 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2517 | for (unsigned int i = 0; i < arguments->size(); i++) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2518 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2519 | TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode(); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2520 | |
| 2521 | if (symbol) |
| 2522 | { |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 2523 | ensureStructDefined(symbol->getType()); |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2524 | |
| 2525 | out << argumentString(symbol); |
| 2526 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2527 | if (i < arguments->size() - 1) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2528 | { |
| 2529 | out << ", "; |
| 2530 | } |
| 2531 | } |
| 2532 | else UNREACHABLE(); |
| 2533 | } |
| 2534 | |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2535 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2536 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2537 | if (sequence->size() > 1) |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2538 | { |
| 2539 | mInsideFunction = true; |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2540 | TIntermNode *body = (*sequence)[1]; |
| 2541 | // The function body node will output braces. |
| 2542 | ASSERT(IsSequence(body)); |
| 2543 | body->traverse(this); |
daniel@transgaming.com | f9ef107 | 2010-04-22 13:35:16 +0000 | [diff] [blame] | 2544 | mInsideFunction = false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2545 | } |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2546 | else |
| 2547 | { |
| 2548 | out << "{}\n"; |
| 2549 | } |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2550 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2551 | mCurrentFunctionMetadata = nullptr; |
| 2552 | |
| 2553 | bool needsLod0 = mASTMetadataList[index].mNeedsLod0; |
| 2554 | if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 2555 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2556 | ASSERT(name != "main"); |
| 2557 | mOutputLod0Function = true; |
| 2558 | node->traverse(this); |
| 2559 | mOutputLod0Function = false; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
daniel@transgaming.com | 0e9704b | 2012-05-31 01:20:13 +0000 | [diff] [blame] | 2562 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2563 | } |
| 2564 | break; |
| 2565 | case EOpFunctionCall: |
| 2566 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2567 | TIntermSequence *arguments = node->getSequence(); |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2568 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2569 | bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2570 | if (node->isUserDefined()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2571 | { |
Olli Etuaho | a8c414b | 2015-04-16 15:51:03 +0300 | [diff] [blame] | 2572 | if (node->isArray()) |
| 2573 | { |
| 2574 | UNIMPLEMENTED(); |
| 2575 | } |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2576 | size_t index = mCallDag.findIndex(node); |
| 2577 | ASSERT(index != CallDAG::InvalidIndex); |
| 2578 | lod0 &= mASTMetadataList[index].mNeedsLod0; |
| 2579 | |
Olli Etuaho | be59c2f | 2016-03-07 11:32:34 +0200 | [diff] [blame] | 2580 | out << DecorateFunctionIfNeeded(node->getNameObj()); |
| 2581 | out << DisambiguateFunctionName(node->getSequence()); |
| 2582 | out << (lod0 ? "Lod0(" : "("); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2583 | } |
| 2584 | else |
| 2585 | { |
Olli Etuaho | 59f9a64 | 2015-08-06 20:38:26 +0300 | [diff] [blame] | 2586 | TString name = TFunction::unmangleName(node->getNameObj().getString()); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2587 | TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType(); |
shannonwoods@chromium.org | c6ac65f | 2013-05-30 00:02:50 +0000 | [diff] [blame] | 2588 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2589 | TextureFunction textureFunction; |
| 2590 | textureFunction.sampler = samplerType; |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2591 | textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize(); |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2592 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2593 | textureFunction.proj = false; |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2594 | textureFunction.offset = false; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2595 | |
| 2596 | if (name == "texture2D" || name == "textureCube" || name == "texture") |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2597 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2598 | textureFunction.method = TextureFunction::IMPLICIT; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2599 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2600 | else if (name == "texture2DProj" || name == "textureProj") |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2601 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2602 | textureFunction.method = TextureFunction::IMPLICIT; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2603 | textureFunction.proj = true; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2604 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2605 | else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" || |
| 2606 | name == "texture2DLodEXT" || name == "textureCubeLodEXT") |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2607 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2608 | textureFunction.method = TextureFunction::LOD; |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2609 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2610 | else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT") |
Nicolas Capens | 9fe6f98 | 2013-06-24 16:05:25 -0400 | [diff] [blame] | 2611 | { |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2612 | textureFunction.method = TextureFunction::LOD; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2613 | textureFunction.proj = true; |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2614 | } |
Nicolas Capens | 75fb475 | 2013-07-10 15:14:47 -0400 | [diff] [blame] | 2615 | else if (name == "textureSize") |
| 2616 | { |
| 2617 | textureFunction.method = TextureFunction::SIZE; |
| 2618 | } |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2619 | else if (name == "textureOffset") |
| 2620 | { |
| 2621 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2622 | textureFunction.offset = true; |
| 2623 | } |
Nicolas Capens | df86c6b | 2014-02-14 20:09:17 -0500 | [diff] [blame] | 2624 | else if (name == "textureProjOffset") |
| 2625 | { |
| 2626 | textureFunction.method = TextureFunction::IMPLICIT; |
| 2627 | textureFunction.offset = true; |
| 2628 | textureFunction.proj = true; |
| 2629 | } |
| 2630 | else if (name == "textureLodOffset") |
| 2631 | { |
| 2632 | textureFunction.method = TextureFunction::LOD; |
| 2633 | textureFunction.offset = true; |
| 2634 | } |
Nicolas Capens | 2adc256 | 2014-02-14 23:50:59 -0500 | [diff] [blame] | 2635 | else if (name == "textureProjLodOffset") |
| 2636 | { |
| 2637 | textureFunction.method = TextureFunction::LOD; |
| 2638 | textureFunction.proj = true; |
| 2639 | textureFunction.offset = true; |
| 2640 | } |
Nicolas Capens | fc01454 | 2014-02-18 14:47:13 -0500 | [diff] [blame] | 2641 | else if (name == "texelFetch") |
| 2642 | { |
| 2643 | textureFunction.method = TextureFunction::FETCH; |
| 2644 | } |
| 2645 | else if (name == "texelFetchOffset") |
| 2646 | { |
| 2647 | textureFunction.method = TextureFunction::FETCH; |
| 2648 | textureFunction.offset = true; |
| 2649 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2650 | else if (name == "textureGrad" || name == "texture2DGradEXT") |
Nicolas Capens | d11d549 | 2014-02-19 17:06:10 -0500 | [diff] [blame] | 2651 | { |
| 2652 | textureFunction.method = TextureFunction::GRAD; |
| 2653 | } |
Nicolas Capens | bf7db10 | 2014-02-19 17:20:28 -0500 | [diff] [blame] | 2654 | else if (name == "textureGradOffset") |
| 2655 | { |
| 2656 | textureFunction.method = TextureFunction::GRAD; |
| 2657 | textureFunction.offset = true; |
| 2658 | } |
Nicolas Capens | 4648508 | 2014-04-15 13:12:50 -0400 | [diff] [blame] | 2659 | else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT") |
Nicolas Capens | f7378e3 | 2014-02-19 17:29:32 -0500 | [diff] [blame] | 2660 | { |
| 2661 | textureFunction.method = TextureFunction::GRAD; |
| 2662 | textureFunction.proj = true; |
| 2663 | } |
| 2664 | else if (name == "textureProjGradOffset") |
| 2665 | { |
| 2666 | textureFunction.method = TextureFunction::GRAD; |
| 2667 | textureFunction.proj = true; |
| 2668 | textureFunction.offset = true; |
| 2669 | } |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2670 | else UNREACHABLE(); |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2671 | |
Nicolas Capens | b1f45b7 | 2013-12-19 17:37:19 -0500 | [diff] [blame] | 2672 | 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] | 2673 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2674 | unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments |
| 2675 | |
| 2676 | if (textureFunction.offset) |
| 2677 | { |
| 2678 | mandatoryArgumentCount++; |
| 2679 | } |
| 2680 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2681 | bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2682 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2683 | if (lod0 || mShaderType == GL_VERTEX_SHADER) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2684 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2685 | if (bias) |
| 2686 | { |
| 2687 | textureFunction.method = TextureFunction::LOD0BIAS; |
| 2688 | } |
| 2689 | else |
| 2690 | { |
| 2691 | textureFunction.method = TextureFunction::LOD0; |
| 2692 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2693 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2694 | else if (bias) |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2695 | { |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2696 | textureFunction.method = TextureFunction::BIAS; |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2697 | } |
| 2698 | } |
| 2699 | |
| 2700 | mUsesTexture.insert(textureFunction); |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2701 | |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 2702 | out << textureFunction.name(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2703 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2704 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2705 | for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2706 | { |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2707 | TIntermTyped *typedArg = (*arg)->getAsTyped(); |
| 2708 | if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType())) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2709 | { |
| 2710 | out << "texture_"; |
| 2711 | (*arg)->traverse(this); |
| 2712 | out << ", sampler_"; |
| 2713 | } |
| 2714 | |
| 2715 | (*arg)->traverse(this); |
| 2716 | |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2717 | if (typedArg->getType().isStructureContainingSamplers()) |
| 2718 | { |
| 2719 | const TType &argType = typedArg->getType(); |
| 2720 | TVector<TIntermSymbol *> samplerSymbols; |
| 2721 | TString structName = samplerNamePrefixFromStruct(typedArg); |
| 2722 | argType.createSamplerSymbols("angle_" + structName, "", |
| 2723 | argType.isArray() ? argType.getArraySize() : 0, |
| 2724 | &samplerSymbols, nullptr); |
| 2725 | for (const TIntermSymbol *sampler : samplerSymbols) |
| 2726 | { |
| 2727 | if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
| 2728 | { |
| 2729 | out << ", texture_" << sampler->getSymbol(); |
| 2730 | out << ", sampler_" << sampler->getSymbol(); |
| 2731 | } |
| 2732 | else |
| 2733 | { |
| 2734 | // In case of HLSL 4.1+, this symbol is the sampler index, and in case |
| 2735 | // of D3D9, it's the sampler variable. |
| 2736 | out << ", " + sampler->getSymbol(); |
| 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2741 | if (arg < arguments->end() - 1) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2742 | { |
| 2743 | out << ", "; |
| 2744 | } |
| 2745 | } |
| 2746 | |
| 2747 | out << ")"; |
| 2748 | |
| 2749 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2750 | } |
| 2751 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2752 | case EOpParameters: |
| 2753 | outputTriplet(out, visit, "(", ", ", ")\n{\n"); |
| 2754 | break; |
| 2755 | case EOpConstructFloat: |
| 2756 | outputConstructor(out, visit, node->getType(), "vec1", node->getSequence()); |
| 2757 | break; |
| 2758 | case EOpConstructVec2: |
| 2759 | outputConstructor(out, visit, node->getType(), "vec2", node->getSequence()); |
| 2760 | break; |
| 2761 | case EOpConstructVec3: |
| 2762 | outputConstructor(out, visit, node->getType(), "vec3", node->getSequence()); |
| 2763 | break; |
| 2764 | case EOpConstructVec4: |
| 2765 | outputConstructor(out, visit, node->getType(), "vec4", node->getSequence()); |
| 2766 | break; |
| 2767 | case EOpConstructBool: |
| 2768 | outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence()); |
| 2769 | break; |
| 2770 | case EOpConstructBVec2: |
| 2771 | outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence()); |
| 2772 | break; |
| 2773 | case EOpConstructBVec3: |
| 2774 | outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence()); |
| 2775 | break; |
| 2776 | case EOpConstructBVec4: |
| 2777 | outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence()); |
| 2778 | break; |
| 2779 | case EOpConstructInt: |
| 2780 | outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence()); |
| 2781 | break; |
| 2782 | case EOpConstructIVec2: |
| 2783 | outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence()); |
| 2784 | break; |
| 2785 | case EOpConstructIVec3: |
| 2786 | outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence()); |
| 2787 | break; |
| 2788 | case EOpConstructIVec4: |
| 2789 | outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence()); |
| 2790 | break; |
| 2791 | case EOpConstructUInt: |
| 2792 | outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence()); |
| 2793 | break; |
| 2794 | case EOpConstructUVec2: |
| 2795 | outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence()); |
| 2796 | break; |
| 2797 | case EOpConstructUVec3: |
| 2798 | outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence()); |
| 2799 | break; |
| 2800 | case EOpConstructUVec4: |
| 2801 | outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence()); |
| 2802 | break; |
| 2803 | case EOpConstructMat2: |
| 2804 | outputConstructor(out, visit, node->getType(), "mat2", node->getSequence()); |
| 2805 | break; |
| 2806 | case EOpConstructMat2x3: |
| 2807 | outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence()); |
| 2808 | break; |
| 2809 | case EOpConstructMat2x4: |
| 2810 | outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence()); |
| 2811 | break; |
| 2812 | case EOpConstructMat3x2: |
| 2813 | outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence()); |
| 2814 | break; |
| 2815 | case EOpConstructMat3: |
| 2816 | outputConstructor(out, visit, node->getType(), "mat3", node->getSequence()); |
| 2817 | break; |
| 2818 | case EOpConstructMat3x4: |
| 2819 | outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence()); |
| 2820 | break; |
| 2821 | case EOpConstructMat4x2: |
| 2822 | outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence()); |
| 2823 | break; |
| 2824 | case EOpConstructMat4x3: |
| 2825 | outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence()); |
| 2826 | break; |
| 2827 | case EOpConstructMat4: |
| 2828 | outputConstructor(out, visit, node->getType(), "mat4", node->getSequence()); |
| 2829 | break; |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 2830 | case EOpConstructStruct: |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 2831 | { |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 2832 | if (node->getType().isArray()) |
| 2833 | { |
| 2834 | UNIMPLEMENTED(); |
| 2835 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 2836 | const TString &structName = StructNameString(*node->getType().getStruct()); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2837 | mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2838 | outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")"); |
Jamie Madill | bfa91f4 | 2014-06-05 15:45:18 -0400 | [diff] [blame] | 2839 | } |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 2840 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2841 | case EOpLessThan: |
| 2842 | outputTriplet(out, visit, "(", " < ", ")"); |
| 2843 | break; |
| 2844 | case EOpGreaterThan: |
| 2845 | outputTriplet(out, visit, "(", " > ", ")"); |
| 2846 | break; |
| 2847 | case EOpLessThanEqual: |
| 2848 | outputTriplet(out, visit, "(", " <= ", ")"); |
| 2849 | break; |
| 2850 | case EOpGreaterThanEqual: |
| 2851 | outputTriplet(out, visit, "(", " >= ", ")"); |
| 2852 | break; |
| 2853 | case EOpVectorEqual: |
| 2854 | outputTriplet(out, visit, "(", " == ", ")"); |
| 2855 | break; |
| 2856 | case EOpVectorNotEqual: |
| 2857 | outputTriplet(out, visit, "(", " != ", ")"); |
| 2858 | break; |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2859 | case EOpMod: |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2860 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2861 | writeEmulatedFunctionTriplet(out, visit, "mod("); |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 2862 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2863 | case EOpModf: |
| 2864 | outputTriplet(out, visit, "modf(", ", ", ")"); |
| 2865 | break; |
| 2866 | case EOpPow: |
| 2867 | outputTriplet(out, visit, "pow(", ", ", ")"); |
| 2868 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2869 | case EOpAtan: |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2870 | ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2871 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2872 | writeEmulatedFunctionTriplet(out, visit, "atan("); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2873 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2874 | case EOpMin: |
| 2875 | outputTriplet(out, visit, "min(", ", ", ")"); |
| 2876 | break; |
| 2877 | case EOpMax: |
| 2878 | outputTriplet(out, visit, "max(", ", ", ")"); |
| 2879 | break; |
| 2880 | case EOpClamp: |
| 2881 | outputTriplet(out, visit, "clamp(", ", ", ")"); |
| 2882 | break; |
Arun Patole | d94f664 | 2015-05-18 16:25:12 +0530 | [diff] [blame] | 2883 | case EOpMix: |
| 2884 | { |
| 2885 | TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped(); |
| 2886 | if (lastParamNode->getType().getBasicType() == EbtBool) |
| 2887 | { |
| 2888 | // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)", |
| 2889 | // so use emulated version. |
| 2890 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2891 | writeEmulatedFunctionTriplet(out, visit, "mix("); |
Arun Patole | d94f664 | 2015-05-18 16:25:12 +0530 | [diff] [blame] | 2892 | } |
| 2893 | else |
| 2894 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2895 | outputTriplet(out, visit, "lerp(", ", ", ")"); |
Arun Patole | d94f664 | 2015-05-18 16:25:12 +0530 | [diff] [blame] | 2896 | } |
| 2897 | } |
| 2898 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2899 | case EOpStep: |
| 2900 | outputTriplet(out, visit, "step(", ", ", ")"); |
| 2901 | break; |
| 2902 | case EOpSmoothStep: |
| 2903 | outputTriplet(out, visit, "smoothstep(", ", ", ")"); |
| 2904 | break; |
| 2905 | case EOpDistance: |
| 2906 | outputTriplet(out, visit, "distance(", ", ", ")"); |
| 2907 | break; |
| 2908 | case EOpDot: |
| 2909 | outputTriplet(out, visit, "dot(", ", ", ")"); |
| 2910 | break; |
| 2911 | case EOpCross: |
| 2912 | outputTriplet(out, visit, "cross(", ", ", ")"); |
| 2913 | break; |
daniel@transgaming.com | 0bbb031 | 2010-04-26 15:33:39 +0000 | [diff] [blame] | 2914 | case EOpFaceForward: |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 2915 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2916 | writeEmulatedFunctionTriplet(out, visit, "faceforward("); |
daniel@transgaming.com | 0bbb031 | 2010-04-26 15:33:39 +0000 | [diff] [blame] | 2917 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2918 | case EOpReflect: |
| 2919 | outputTriplet(out, visit, "reflect(", ", ", ")"); |
| 2920 | break; |
| 2921 | case EOpRefract: |
| 2922 | outputTriplet(out, visit, "refract(", ", ", ")"); |
| 2923 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 2924 | case EOpOuterProduct: |
| 2925 | ASSERT(node->getUseEmulatedFunction()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2926 | writeEmulatedFunctionTriplet(out, visit, "outerProduct("); |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 2927 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2928 | case EOpMul: |
| 2929 | outputTriplet(out, visit, "(", " * ", ")"); |
| 2930 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2931 | default: UNREACHABLE(); |
| 2932 | } |
| 2933 | |
| 2934 | return true; |
| 2935 | } |
| 2936 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2937 | void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2938 | { |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2939 | out << "if ("; |
| 2940 | |
| 2941 | node->getCondition()->traverse(this); |
| 2942 | |
| 2943 | out << ")\n"; |
| 2944 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2945 | outputLineDirective(out, node->getLine().first_line); |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2946 | |
| 2947 | bool discard = false; |
| 2948 | |
| 2949 | if (node->getTrueBlock()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2950 | { |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2951 | // The trueBlock child node will output braces. |
| 2952 | ASSERT(IsSequence(node->getTrueBlock())); |
| 2953 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2954 | node->getTrueBlock()->traverse(this); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2955 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2956 | // Detect true discard |
| 2957 | discard = (discard || FindDiscard::search(node->getTrueBlock())); |
| 2958 | } |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2959 | else |
| 2960 | { |
| 2961 | // TODO(oetuaho): Check if the semicolon inside is necessary. |
| 2962 | // It's there as a result of conservative refactoring of the output. |
| 2963 | out << "{;}\n"; |
| 2964 | } |
Corentin Wallez | 80bacde | 2014-11-10 12:07:37 -0800 | [diff] [blame] | 2965 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2966 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2967 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2968 | if (node->getFalseBlock()) |
| 2969 | { |
| 2970 | out << "else\n"; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2971 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2972 | outputLineDirective(out, node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2973 | |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2974 | // Either this is "else if" or the falseBlock child node will output braces. |
| 2975 | ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr); |
| 2976 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2977 | node->getFalseBlock()->traverse(this); |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2978 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2979 | outputLineDirective(out, node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2980 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2981 | // Detect false discard |
| 2982 | discard = (discard || FindDiscard::search(node->getFalseBlock())); |
| 2983 | } |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2984 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2985 | // ANGLE issue 486: Detect problematic conditional discard |
Olli Etuaho | fd3b9be | 2015-05-18 17:07:36 +0300 | [diff] [blame] | 2986 | if (discard) |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2987 | { |
| 2988 | mUsesDiscardRewriting = true; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2989 | } |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 2990 | } |
| 2991 | |
| 2992 | bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node) |
| 2993 | { |
| 2994 | TInfoSinkBase &out = getInfoSink(); |
| 2995 | |
| 2996 | ASSERT(!node->usesTernaryOperator()); |
| 2997 | |
| 2998 | if (!mInsideFunction) |
| 2999 | { |
| 3000 | // This is part of unfolded global initialization. |
| 3001 | mDeferredGlobalInitializers.push_back(node); |
| 3002 | return false; |
| 3003 | } |
| 3004 | |
| 3005 | // D3D errors when there is a gradient operation in a loop in an unflattened if. |
Corentin Wallez | 477b243 | 2015-08-31 10:41:16 -0700 | [diff] [blame] | 3006 | if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node)) |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 3007 | { |
| 3008 | out << "FLATTEN "; |
| 3009 | } |
| 3010 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3011 | writeSelection(out, node); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3012 | |
| 3013 | return false; |
| 3014 | } |
| 3015 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 3016 | bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 3017 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3018 | TInfoSinkBase &out = getInfoSink(); |
| 3019 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 3020 | if (node->getStatementList()) |
| 3021 | { |
Olli Etuaho | 2cd7a0e | 2015-02-27 13:57:32 +0200 | [diff] [blame] | 3022 | node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList())); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3023 | outputTriplet(out, visit, "switch (", ") ", ""); |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 3024 | // The curly braces get written when visiting the statementList aggregate |
| 3025 | } |
| 3026 | else |
| 3027 | { |
| 3028 | // No statementList, so it won't output curly braces |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3029 | outputTriplet(out, visit, "switch (", ") {", "}\n"); |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 3030 | } |
| 3031 | return true; |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 3032 | } |
| 3033 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 3034 | bool OutputHLSL::visitCase(Visit visit, TIntermCase *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 3035 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3036 | TInfoSinkBase &out = getInfoSink(); |
| 3037 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 3038 | if (node->hasCondition()) |
| 3039 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3040 | outputTriplet(out, visit, "case (", "", "):\n"); |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 3041 | return true; |
| 3042 | } |
| 3043 | else |
| 3044 | { |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 3045 | out << "default:\n"; |
| 3046 | return false; |
| 3047 | } |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 3048 | } |
| 3049 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3050 | void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node) |
| 3051 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3052 | TInfoSinkBase &out = getInfoSink(); |
| 3053 | writeConstantUnion(out, node->getType(), node->getUnionArrayPointer()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3054 | } |
| 3055 | |
| 3056 | bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node) |
| 3057 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 3058 | mNestedLoopDepth++; |
| 3059 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 3060 | bool wasDiscontinuous = mInsideDiscontinuousLoop; |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 3061 | mInsideDiscontinuousLoop = mInsideDiscontinuousLoop || |
Corentin Wallez | a1884f2 | 2015-04-29 10:15:16 -0700 | [diff] [blame] | 3062 | mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 3063 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3064 | TInfoSinkBase &out = getInfoSink(); |
| 3065 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 3066 | if (mOutputType == SH_HLSL_3_0_OUTPUT) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3067 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3068 | if (handleExcessiveLoop(out, node)) |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 3069 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 3070 | mInsideDiscontinuousLoop = wasDiscontinuous; |
| 3071 | mNestedLoopDepth--; |
| 3072 | |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 3073 | return false; |
| 3074 | } |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3075 | } |
| 3076 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 3077 | const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : ""; |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3078 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3079 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 3080 | out << "{" << unroll << " do\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3081 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3082 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3083 | } |
| 3084 | else |
| 3085 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 3086 | out << "{" << unroll << " for("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3087 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3088 | if (node->getInit()) |
| 3089 | { |
| 3090 | node->getInit()->traverse(this); |
| 3091 | } |
| 3092 | |
| 3093 | out << "; "; |
| 3094 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3095 | if (node->getCondition()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3096 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3097 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | out << "; "; |
| 3101 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3102 | if (node->getExpression()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3103 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3104 | node->getExpression()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3105 | } |
| 3106 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3107 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3108 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3109 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | if (node->getBody()) |
| 3113 | { |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 3114 | // The loop body node will output braces. |
| 3115 | ASSERT(IsSequence(node->getBody())); |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 3116 | node->getBody()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3117 | } |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 3118 | else |
| 3119 | { |
| 3120 | // TODO(oetuaho): Check if the semicolon inside is necessary. |
| 3121 | // It's there as a result of conservative refactoring of the output. |
| 3122 | out << "{;}\n"; |
| 3123 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3124 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3125 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3126 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3127 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3128 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3129 | outputLineDirective(out, node->getCondition()->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3130 | out << "while(\n"; |
| 3131 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3132 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3133 | |
daniel@transgaming.com | 7353698 | 2012-03-21 20:45:49 +0000 | [diff] [blame] | 3134 | out << ");"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3135 | } |
| 3136 | |
daniel@transgaming.com | 7353698 | 2012-03-21 20:45:49 +0000 | [diff] [blame] | 3137 | out << "}\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3138 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 3139 | mInsideDiscontinuousLoop = wasDiscontinuous; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 3140 | mNestedLoopDepth--; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 3141 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3142 | return false; |
| 3143 | } |
| 3144 | |
| 3145 | bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node) |
| 3146 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 3147 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3148 | |
| 3149 | switch (node->getFlowOp()) |
| 3150 | { |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 3151 | case EOpKill: |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3152 | outputTriplet(out, visit, "discard;\n", "", ""); |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 3153 | break; |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 3154 | case EOpBreak: |
| 3155 | if (visit == PreVisit) |
| 3156 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 3157 | if (mNestedLoopDepth > 1) |
| 3158 | { |
| 3159 | mUsesNestedBreak = true; |
| 3160 | } |
| 3161 | |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 3162 | if (mExcessiveLoopIndex) |
| 3163 | { |
| 3164 | out << "{Break"; |
| 3165 | mExcessiveLoopIndex->traverse(this); |
| 3166 | out << " = true; break;}\n"; |
| 3167 | } |
| 3168 | else |
| 3169 | { |
| 3170 | out << "break;\n"; |
| 3171 | } |
| 3172 | } |
| 3173 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3174 | case EOpContinue: |
| 3175 | outputTriplet(out, visit, "continue;\n", "", ""); |
| 3176 | break; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3177 | case EOpReturn: |
| 3178 | if (visit == PreVisit) |
| 3179 | { |
| 3180 | if (node->getExpression()) |
| 3181 | { |
| 3182 | out << "return "; |
| 3183 | } |
| 3184 | else |
| 3185 | { |
| 3186 | out << "return;\n"; |
| 3187 | } |
| 3188 | } |
| 3189 | else if (visit == PostVisit) |
| 3190 | { |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3191 | if (node->getExpression()) |
| 3192 | { |
| 3193 | out << ";\n"; |
| 3194 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3195 | } |
| 3196 | break; |
| 3197 | default: UNREACHABLE(); |
| 3198 | } |
| 3199 | |
| 3200 | return true; |
| 3201 | } |
| 3202 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 3203 | bool OutputHLSL::isSingleStatement(TIntermNode *node) |
| 3204 | { |
| 3205 | TIntermAggregate *aggregate = node->getAsAggregate(); |
| 3206 | |
| 3207 | if (aggregate) |
| 3208 | { |
| 3209 | if (aggregate->getOp() == EOpSequence) |
| 3210 | { |
| 3211 | return false; |
| 3212 | } |
Nicolas Capens | fa41aa0 | 2014-10-06 17:40:13 -0400 | [diff] [blame] | 3213 | else if (aggregate->getOp() == EOpDeclaration) |
| 3214 | { |
| 3215 | // Declaring multiple comma-separated variables must be considered multiple statements |
| 3216 | // because each individual declaration has side effects which are visible in the next. |
| 3217 | return false; |
| 3218 | } |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 3219 | else |
| 3220 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 3221 | 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] | 3222 | { |
| 3223 | if (!isSingleStatement(*sit)) |
| 3224 | { |
| 3225 | return false; |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | return true; |
| 3230 | } |
| 3231 | } |
| 3232 | |
| 3233 | return true; |
| 3234 | } |
| 3235 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 3236 | // Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them |
| 3237 | // (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254). |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3238 | bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3239 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 3240 | const int MAX_LOOP_ITERATIONS = 254; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3241 | |
| 3242 | // Parse loops of the form: |
| 3243 | // for(int index = initial; index [comparator] limit; index += increment) |
| 3244 | TIntermSymbol *index = NULL; |
| 3245 | TOperator comparator = EOpNull; |
| 3246 | int initial = 0; |
| 3247 | int limit = 0; |
| 3248 | int increment = 0; |
| 3249 | |
| 3250 | // Parse index name and intial value |
| 3251 | if (node->getInit()) |
| 3252 | { |
| 3253 | TIntermAggregate *init = node->getInit()->getAsAggregate(); |
| 3254 | |
| 3255 | if (init) |
| 3256 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 3257 | TIntermSequence *sequence = init->getSequence(); |
| 3258 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3259 | |
| 3260 | if (variable && variable->getQualifier() == EvqTemporary) |
| 3261 | { |
| 3262 | TIntermBinary *assign = variable->getAsBinaryNode(); |
| 3263 | |
| 3264 | if (assign->getOp() == EOpInitialize) |
| 3265 | { |
| 3266 | TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode(); |
| 3267 | TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion(); |
| 3268 | |
| 3269 | if (symbol && constant) |
| 3270 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 3271 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3272 | { |
| 3273 | index = symbol; |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 3274 | initial = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | } |
| 3279 | } |
| 3280 | } |
| 3281 | |
| 3282 | // Parse comparator and limit value |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3283 | if (index != NULL && node->getCondition()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3284 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3285 | TIntermBinary *test = node->getCondition()->getAsBinaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3286 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3287 | if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId()) |
| 3288 | { |
| 3289 | TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion(); |
| 3290 | |
| 3291 | if (constant) |
| 3292 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 3293 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3294 | { |
| 3295 | comparator = test->getOp(); |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 3296 | limit = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | } |
| 3301 | |
| 3302 | // Parse increment |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3303 | if (index != NULL && comparator != EOpNull && node->getExpression()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3304 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 3305 | TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode(); |
| 3306 | TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3307 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3308 | if (binaryTerminal) |
| 3309 | { |
| 3310 | TOperator op = binaryTerminal->getOp(); |
| 3311 | TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion(); |
| 3312 | |
| 3313 | if (constant) |
| 3314 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 3315 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3316 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 3317 | int value = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3318 | |
| 3319 | switch (op) |
| 3320 | { |
| 3321 | case EOpAddAssign: increment = value; break; |
| 3322 | case EOpSubAssign: increment = -value; break; |
| 3323 | default: UNIMPLEMENTED(); |
| 3324 | } |
| 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | else if (unaryTerminal) |
| 3329 | { |
| 3330 | TOperator op = unaryTerminal->getOp(); |
| 3331 | |
| 3332 | switch (op) |
| 3333 | { |
| 3334 | case EOpPostIncrement: increment = 1; break; |
| 3335 | case EOpPostDecrement: increment = -1; break; |
| 3336 | case EOpPreIncrement: increment = 1; break; |
| 3337 | case EOpPreDecrement: increment = -1; break; |
| 3338 | default: UNIMPLEMENTED(); |
| 3339 | } |
| 3340 | } |
| 3341 | } |
| 3342 | |
| 3343 | if (index != NULL && comparator != EOpNull && increment != 0) |
| 3344 | { |
| 3345 | if (comparator == EOpLessThanEqual) |
| 3346 | { |
| 3347 | comparator = EOpLessThan; |
| 3348 | limit += 1; |
| 3349 | } |
| 3350 | |
| 3351 | if (comparator == EOpLessThan) |
| 3352 | { |
daniel@transgaming.com | f1f538e | 2011-02-09 16:30:01 +0000 | [diff] [blame] | 3353 | int iterations = (limit - initial) / increment; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3354 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 3355 | if (iterations <= MAX_LOOP_ITERATIONS) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3356 | { |
| 3357 | return false; // Not an excessive loop |
| 3358 | } |
| 3359 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 3360 | TIntermSymbol *restoreIndex = mExcessiveLoopIndex; |
| 3361 | mExcessiveLoopIndex = index; |
| 3362 | |
daniel@transgaming.com | 0933b0c | 2012-07-11 20:37:28 +0000 | [diff] [blame] | 3363 | out << "{int "; |
| 3364 | index->traverse(this); |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 3365 | out << ";\n" |
| 3366 | "bool Break"; |
| 3367 | index->traverse(this); |
| 3368 | out << " = false;\n"; |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 3369 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 3370 | bool firstLoopFragment = true; |
| 3371 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3372 | while (iterations > 0) |
| 3373 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 3374 | int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3375 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 3376 | if (!firstLoopFragment) |
| 3377 | { |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 3378 | out << "if (!Break"; |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 3379 | index->traverse(this); |
| 3380 | out << ") {\n"; |
| 3381 | } |
daniel@transgaming.com | 2fe20a8 | 2012-07-11 20:37:41 +0000 | [diff] [blame] | 3382 | |
| 3383 | if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment |
| 3384 | { |
| 3385 | mExcessiveLoopIndex = NULL; // Stops setting the Break flag |
| 3386 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3387 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3388 | // for(int index = initial; index < clampedLimit; index += increment) |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 3389 | const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : ""; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3390 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 3391 | out << unroll << " for("; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3392 | index->traverse(this); |
| 3393 | out << " = "; |
| 3394 | out << initial; |
| 3395 | |
| 3396 | out << "; "; |
| 3397 | index->traverse(this); |
| 3398 | out << " < "; |
| 3399 | out << clampedLimit; |
| 3400 | |
| 3401 | out << "; "; |
| 3402 | index->traverse(this); |
| 3403 | out << " += "; |
| 3404 | out << increment; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3405 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3406 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3407 | outputLineDirective(out, node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3408 | out << "{\n"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3409 | |
| 3410 | if (node->getBody()) |
| 3411 | { |
| 3412 | node->getBody()->traverse(this); |
| 3413 | } |
| 3414 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3415 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 3416 | out << ";}\n"; |
| 3417 | |
| 3418 | if (!firstLoopFragment) |
| 3419 | { |
| 3420 | out << "}\n"; |
| 3421 | } |
| 3422 | |
| 3423 | firstLoopFragment = false; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3424 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 3425 | initial += MAX_LOOP_ITERATIONS * increment; |
| 3426 | iterations -= MAX_LOOP_ITERATIONS; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3427 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3428 | |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 3429 | out << "}"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3430 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 3431 | mExcessiveLoopIndex = restoreIndex; |
| 3432 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 3433 | return true; |
| 3434 | } |
| 3435 | else UNIMPLEMENTED(); |
| 3436 | } |
| 3437 | |
| 3438 | return false; // Not handled as an excessive loop |
| 3439 | } |
| 3440 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3441 | void OutputHLSL::outputTriplet(TInfoSinkBase &out, |
| 3442 | Visit visit, |
| 3443 | const char *preString, |
| 3444 | const char *inString, |
| 3445 | const char *postString) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3446 | { |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 3447 | if (visit == PreVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3448 | { |
| 3449 | out << preString; |
| 3450 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 3451 | else if (visit == InVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3452 | { |
| 3453 | out << inString; |
| 3454 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 3455 | else if (visit == PostVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3456 | { |
| 3457 | out << postString; |
| 3458 | } |
| 3459 | } |
| 3460 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3461 | void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3462 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 3463 | if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3464 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 3465 | out << "\n"; |
| 3466 | out << "#line " << line; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3467 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 3468 | if (mSourcePath) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3469 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 3470 | out << " \"" << mSourcePath << "\""; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3471 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3472 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 3473 | out << "\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 3474 | } |
| 3475 | } |
| 3476 | |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 3477 | TString OutputHLSL::argumentString(const TIntermSymbol *symbol) |
| 3478 | { |
| 3479 | TQualifier qualifier = symbol->getQualifier(); |
Olli Etuaho | f5cfc8d | 2015-08-06 16:36:39 +0300 | [diff] [blame] | 3480 | const TType &type = symbol->getType(); |
| 3481 | const TName &name = symbol->getName(); |
| 3482 | TString nameStr; |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 3483 | |
Olli Etuaho | f5cfc8d | 2015-08-06 16:36:39 +0300 | [diff] [blame] | 3484 | if (name.getString().empty()) // HLSL demands named arguments, also for prototypes |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 3485 | { |
Olli Etuaho | f5cfc8d | 2015-08-06 16:36:39 +0300 | [diff] [blame] | 3486 | nameStr = "x" + str(mUniqueIndex++); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 3487 | } |
Olli Etuaho | f5cfc8d | 2015-08-06 16:36:39 +0300 | [diff] [blame] | 3488 | else |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 3489 | { |
Olli Etuaho | f5cfc8d | 2015-08-06 16:36:39 +0300 | [diff] [blame] | 3490 | nameStr = DecorateIfNeeded(name); |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 3491 | } |
| 3492 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 3493 | if (IsSampler(type.getBasicType())) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 3494 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 3495 | if (mOutputType == SH_HLSL_4_1_OUTPUT) |
| 3496 | { |
| 3497 | // Samplers are passed as indices to the sampler array. |
| 3498 | ASSERT(qualifier != EvqOut && qualifier != EvqInOut); |
| 3499 | return "const uint " + nameStr + ArrayString(type); |
| 3500 | } |
| 3501 | if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
| 3502 | { |
| 3503 | return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) + |
| 3504 | " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) + |
| 3505 | " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr + |
| 3506 | ArrayString(type); |
| 3507 | } |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 3510 | TStringStream argString; |
| 3511 | argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr |
| 3512 | << ArrayString(type); |
| 3513 | |
| 3514 | // If the structure parameter contains samplers, they need to be passed into the function as |
| 3515 | // separate parameters. HLSL doesn't natively support samplers in structs. |
| 3516 | if (type.isStructureContainingSamplers()) |
| 3517 | { |
| 3518 | ASSERT(qualifier != EvqOut && qualifier != EvqInOut); |
| 3519 | TVector<TIntermSymbol *> samplerSymbols; |
| 3520 | type.createSamplerSymbols("angle" + nameStr, "", 0, &samplerSymbols, nullptr); |
| 3521 | for (const TIntermSymbol *sampler : samplerSymbols) |
| 3522 | { |
| 3523 | if (mOutputType == SH_HLSL_4_1_OUTPUT) |
| 3524 | { |
| 3525 | argString << ", const uint " << sampler->getSymbol() << ArrayString(type); |
| 3526 | } |
| 3527 | else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
| 3528 | { |
| 3529 | const TType &samplerType = sampler->getType(); |
| 3530 | ASSERT((!type.isArray() && !samplerType.isArray()) || |
| 3531 | type.getArraySize() == samplerType.getArraySize()); |
| 3532 | ASSERT(IsSampler(samplerType.getBasicType())); |
| 3533 | argString << ", " << QualifierString(qualifier) << " " |
| 3534 | << TextureString(samplerType.getBasicType()) << " texture_" |
| 3535 | << sampler->getSymbol() << ArrayString(type) << ", " |
| 3536 | << QualifierString(qualifier) << " " |
| 3537 | << SamplerString(samplerType.getBasicType()) << " sampler_" |
| 3538 | << sampler->getSymbol() << ArrayString(type); |
| 3539 | } |
| 3540 | else |
| 3541 | { |
| 3542 | const TType &samplerType = sampler->getType(); |
| 3543 | ASSERT((!type.isArray() && !samplerType.isArray()) || |
| 3544 | type.getArraySize() == samplerType.getArraySize()); |
| 3545 | ASSERT(IsSampler(samplerType.getBasicType())); |
| 3546 | argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType) |
| 3547 | << " " << sampler->getSymbol() << ArrayString(type); |
| 3548 | } |
| 3549 | } |
| 3550 | } |
| 3551 | |
| 3552 | return argString.str(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | TString OutputHLSL::initializer(const TType &type) |
| 3556 | { |
| 3557 | TString string; |
| 3558 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 3559 | size_t size = type.getObjectSize(); |
| 3560 | for (size_t component = 0; component < size; component++) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3561 | { |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 3562 | string += "0"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3563 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 3564 | if (component + 1 < size) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3565 | { |
| 3566 | string += ", "; |
| 3567 | } |
| 3568 | } |
| 3569 | |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 3570 | return "{" + string + "}"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3571 | } |
daniel@transgaming.com | 72d0b52 | 2010-04-13 19:53:44 +0000 | [diff] [blame] | 3572 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3573 | void OutputHLSL::outputConstructor(TInfoSinkBase &out, |
| 3574 | Visit visit, |
| 3575 | const TType &type, |
| 3576 | const char *name, |
| 3577 | const TIntermSequence *parameters) |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 3578 | { |
Olli Etuaho | f40319e | 2015-03-10 14:33:00 +0200 | [diff] [blame] | 3579 | if (type.isArray()) |
| 3580 | { |
| 3581 | UNIMPLEMENTED(); |
| 3582 | } |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 3583 | |
| 3584 | if (visit == PreVisit) |
| 3585 | { |
Olli Etuaho | be59c2f | 2016-03-07 11:32:34 +0200 | [diff] [blame] | 3586 | TString constructorName = mStructureHLSL->addConstructor(type, name, parameters); |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 3587 | |
Olli Etuaho | be59c2f | 2016-03-07 11:32:34 +0200 | [diff] [blame] | 3588 | out << constructorName << "("; |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 3589 | } |
| 3590 | else if (visit == InVisit) |
| 3591 | { |
| 3592 | out << ", "; |
| 3593 | } |
| 3594 | else if (visit == PostVisit) |
| 3595 | { |
| 3596 | out << ")"; |
| 3597 | } |
| 3598 | } |
| 3599 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3600 | const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out, |
| 3601 | const TType &type, |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3602 | const TConstantUnion *const constUnion) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3603 | { |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3604 | const TConstantUnion *constUnionIterated = constUnion; |
| 3605 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 3606 | const TStructure* structure = type.getStruct(); |
| 3607 | if (structure) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3608 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 3609 | out << StructNameString(*structure) + "_ctor("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3610 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 3611 | const TFieldList& fields = structure->fields(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3612 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 3613 | for (size_t i = 0; i < fields.size(); i++) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3614 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 3615 | const TType *fieldType = fields[i]->type(); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3616 | constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3617 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 3618 | if (i != fields.size() - 1) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3619 | { |
| 3620 | out << ", "; |
| 3621 | } |
| 3622 | } |
| 3623 | |
| 3624 | out << ")"; |
| 3625 | } |
| 3626 | else |
| 3627 | { |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 3628 | size_t size = type.getObjectSize(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3629 | bool writeType = size > 1; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3630 | |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3631 | if (writeType) |
| 3632 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 3633 | out << TypeString(type) << "("; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3634 | } |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3635 | constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3636 | if (writeType) |
| 3637 | { |
| 3638 | out << ")"; |
| 3639 | } |
| 3640 | } |
| 3641 | |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3642 | return constUnionIterated; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3643 | } |
| 3644 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3645 | void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr) |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 3646 | { |
| 3647 | TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3648 | outputTriplet(out, visit, preString.c_str(), ", ", ")"); |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 3649 | } |
| 3650 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3651 | bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression) |
| 3652 | { |
| 3653 | sh::SearchSymbol searchSymbol(symbolNode->getSymbol()); |
| 3654 | expression->traverse(&searchSymbol); |
| 3655 | |
| 3656 | if (searchSymbol.foundMatch()) |
| 3657 | { |
| 3658 | // Type already printed |
| 3659 | out << "t" + str(mUniqueIndex) + " = "; |
| 3660 | expression->traverse(this); |
| 3661 | out << ", "; |
| 3662 | symbolNode->traverse(this); |
| 3663 | out << " = t" + str(mUniqueIndex); |
| 3664 | |
| 3665 | mUniqueIndex++; |
| 3666 | return true; |
| 3667 | } |
| 3668 | |
| 3669 | return false; |
| 3670 | } |
| 3671 | |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3672 | bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression) |
| 3673 | { |
| 3674 | // We support writing constant unions and constructors that only take constant unions as |
| 3675 | // parameters as HLSL literals. |
| 3676 | if (expression->getAsConstantUnion()) |
| 3677 | { |
| 3678 | return true; |
| 3679 | } |
| 3680 | if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() || |
| 3681 | !expression->getAsAggregate()->isConstructor()) |
| 3682 | { |
| 3683 | return false; |
| 3684 | } |
| 3685 | TIntermAggregate *constructor = expression->getAsAggregate(); |
| 3686 | for (TIntermNode *&node : *constructor->getSequence()) |
| 3687 | { |
| 3688 | if (!node->getAsConstantUnion()) |
| 3689 | return false; |
| 3690 | } |
| 3691 | return true; |
| 3692 | } |
| 3693 | |
| 3694 | bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out, |
| 3695 | TIntermSymbol *symbolNode, |
| 3696 | TIntermTyped *expression) |
| 3697 | { |
| 3698 | if (canWriteAsHLSLLiteral(expression)) |
| 3699 | { |
| 3700 | symbolNode->traverse(this); |
| 3701 | if (expression->getType().isArray()) |
| 3702 | { |
| 3703 | out << "[" << expression->getType().getArraySize() << "]"; |
| 3704 | } |
| 3705 | out << " = {"; |
| 3706 | if (expression->getAsConstantUnion()) |
| 3707 | { |
| 3708 | TIntermConstantUnion *nodeConst = expression->getAsConstantUnion(); |
| 3709 | const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer(); |
| 3710 | WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize()); |
| 3711 | } |
| 3712 | else |
| 3713 | { |
| 3714 | TIntermAggregate *constructor = expression->getAsAggregate(); |
| 3715 | ASSERT(constructor != nullptr); |
| 3716 | for (TIntermNode *&node : *constructor->getSequence()) |
| 3717 | { |
| 3718 | TIntermConstantUnion *nodeConst = node->getAsConstantUnion(); |
| 3719 | ASSERT(nodeConst); |
| 3720 | const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer(); |
| 3721 | WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize()); |
| 3722 | if (node != constructor->getSequence()->back()) |
| 3723 | { |
| 3724 | out << ", "; |
| 3725 | } |
| 3726 | } |
| 3727 | } |
| 3728 | out << "}"; |
| 3729 | return true; |
| 3730 | } |
| 3731 | return false; |
| 3732 | } |
| 3733 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3734 | void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out) |
| 3735 | { |
| 3736 | out << "#define ANGLE_USES_DEFERRED_INIT\n" |
| 3737 | << "\n" |
| 3738 | << "void initializeDeferredGlobals()\n" |
| 3739 | << "{\n"; |
| 3740 | |
| 3741 | for (const auto &deferredGlobal : mDeferredGlobalInitializers) |
| 3742 | { |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 3743 | TIntermBinary *binary = deferredGlobal->getAsBinaryNode(); |
| 3744 | TIntermSelection *selection = deferredGlobal->getAsSelectionNode(); |
| 3745 | if (binary != nullptr) |
| 3746 | { |
| 3747 | TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode(); |
| 3748 | TIntermTyped *expression = binary->getRight(); |
| 3749 | ASSERT(symbol); |
| 3750 | ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3751 | |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 3752 | out << " " << Decorate(symbol->getSymbol()) << " = "; |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3753 | |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 3754 | if (!writeSameSymbolInitializer(out, symbol, expression)) |
| 3755 | { |
| 3756 | ASSERT(mInfoSinkStack.top() == &out); |
| 3757 | expression->traverse(this); |
| 3758 | } |
| 3759 | out << ";\n"; |
| 3760 | } |
| 3761 | else if (selection != nullptr) |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3762 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3763 | writeSelection(out, selection); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3764 | } |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 3765 | else |
| 3766 | { |
| 3767 | UNREACHABLE(); |
| 3768 | } |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3769 | } |
| 3770 | |
| 3771 | out << "}\n" |
| 3772 | << "\n"; |
| 3773 | } |
| 3774 | |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3775 | TString OutputHLSL::addStructEqualityFunction(const TStructure &structure) |
| 3776 | { |
| 3777 | const TFieldList &fields = structure.fields(); |
| 3778 | |
| 3779 | for (const auto &eqFunction : mStructEqualityFunctions) |
| 3780 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3781 | if (eqFunction->structure == &structure) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3782 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3783 | return eqFunction->functionName; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | const TString &structNameString = StructNameString(structure); |
| 3788 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3789 | StructEqualityFunction *function = new StructEqualityFunction(); |
| 3790 | function->structure = &structure; |
| 3791 | function->functionName = "angle_eq_" + structNameString; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3792 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3793 | TInfoSinkBase fnOut; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3794 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3795 | fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n" |
| 3796 | << "{\n" |
| 3797 | " return "; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3798 | |
| 3799 | for (size_t i = 0; i < fields.size(); i++) |
| 3800 | { |
| 3801 | const TField *field = fields[i]; |
| 3802 | const TType *fieldType = field->type(); |
| 3803 | |
| 3804 | const TString &fieldNameA = "a." + Decorate(field->name()); |
| 3805 | const TString &fieldNameB = "b." + Decorate(field->name()); |
| 3806 | |
| 3807 | if (i > 0) |
| 3808 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3809 | fnOut << " && "; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3810 | } |
| 3811 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3812 | fnOut << "("; |
| 3813 | outputEqual(PreVisit, *fieldType, EOpEqual, fnOut); |
| 3814 | fnOut << fieldNameA; |
| 3815 | outputEqual(InVisit, *fieldType, EOpEqual, fnOut); |
| 3816 | fnOut << fieldNameB; |
| 3817 | outputEqual(PostVisit, *fieldType, EOpEqual, fnOut); |
| 3818 | fnOut << ")"; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3819 | } |
| 3820 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3821 | fnOut << ";\n" << "}\n"; |
| 3822 | |
| 3823 | function->functionDefinition = fnOut.c_str(); |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3824 | |
| 3825 | mStructEqualityFunctions.push_back(function); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3826 | mEqualityFunctions.push_back(function); |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3827 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3828 | return function->functionName; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3829 | } |
| 3830 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3831 | TString OutputHLSL::addArrayEqualityFunction(const TType& type) |
| 3832 | { |
| 3833 | for (const auto &eqFunction : mArrayEqualityFunctions) |
| 3834 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3835 | if (eqFunction->type == type) |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3836 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3837 | return eqFunction->functionName; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3838 | } |
| 3839 | } |
| 3840 | |
| 3841 | const TString &typeName = TypeString(type); |
| 3842 | |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3843 | ArrayHelperFunction *function = new ArrayHelperFunction(); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3844 | function->type = type; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3845 | |
| 3846 | TInfoSinkBase fnNameOut; |
| 3847 | fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName; |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3848 | function->functionName = fnNameOut.c_str(); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3849 | |
| 3850 | TType nonArrayType = type; |
| 3851 | nonArrayType.clearArrayness(); |
| 3852 | |
| 3853 | TInfoSinkBase fnOut; |
| 3854 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3855 | fnOut << "bool " << function->functionName << "(" |
Olli Etuaho | fc7cfd1 | 2015-03-31 14:46:18 +0300 | [diff] [blame] | 3856 | << typeName << " a[" << type.getArraySize() << "], " |
| 3857 | << typeName << " b[" << type.getArraySize() << "])\n" |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3858 | << "{\n" |
| 3859 | " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n" |
| 3860 | " {\n" |
| 3861 | " if ("; |
| 3862 | |
| 3863 | outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut); |
| 3864 | fnOut << "a[i]"; |
| 3865 | outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut); |
| 3866 | fnOut << "b[i]"; |
| 3867 | outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut); |
| 3868 | |
| 3869 | fnOut << ") { return false; }\n" |
| 3870 | " }\n" |
| 3871 | " return true;\n" |
| 3872 | "}\n"; |
| 3873 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3874 | function->functionDefinition = fnOut.c_str(); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3875 | |
| 3876 | mArrayEqualityFunctions.push_back(function); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3877 | mEqualityFunctions.push_back(function); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3878 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3879 | return function->functionName; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3880 | } |
| 3881 | |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3882 | TString OutputHLSL::addArrayAssignmentFunction(const TType& type) |
| 3883 | { |
| 3884 | for (const auto &assignFunction : mArrayAssignmentFunctions) |
| 3885 | { |
| 3886 | if (assignFunction.type == type) |
| 3887 | { |
| 3888 | return assignFunction.functionName; |
| 3889 | } |
| 3890 | } |
| 3891 | |
| 3892 | const TString &typeName = TypeString(type); |
| 3893 | |
| 3894 | ArrayHelperFunction function; |
| 3895 | function.type = type; |
| 3896 | |
| 3897 | TInfoSinkBase fnNameOut; |
| 3898 | fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName; |
| 3899 | function.functionName = fnNameOut.c_str(); |
| 3900 | |
| 3901 | TInfoSinkBase fnOut; |
| 3902 | |
| 3903 | fnOut << "void " << function.functionName << "(out " |
| 3904 | << typeName << " a[" << type.getArraySize() << "], " |
| 3905 | << typeName << " b[" << type.getArraySize() << "])\n" |
| 3906 | << "{\n" |
| 3907 | " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n" |
| 3908 | " {\n" |
| 3909 | " a[i] = b[i];\n" |
| 3910 | " }\n" |
| 3911 | "}\n"; |
| 3912 | |
| 3913 | function.functionDefinition = fnOut.c_str(); |
| 3914 | |
| 3915 | mArrayAssignmentFunctions.push_back(function); |
| 3916 | |
| 3917 | return function.functionName; |
| 3918 | } |
| 3919 | |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3920 | TString OutputHLSL::addArrayConstructIntoFunction(const TType& type) |
| 3921 | { |
| 3922 | for (const auto &constructIntoFunction : mArrayConstructIntoFunctions) |
| 3923 | { |
| 3924 | if (constructIntoFunction.type == type) |
| 3925 | { |
| 3926 | return constructIntoFunction.functionName; |
| 3927 | } |
| 3928 | } |
| 3929 | |
| 3930 | const TString &typeName = TypeString(type); |
| 3931 | |
| 3932 | ArrayHelperFunction function; |
| 3933 | function.type = type; |
| 3934 | |
| 3935 | TInfoSinkBase fnNameOut; |
| 3936 | fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName; |
| 3937 | function.functionName = fnNameOut.c_str(); |
| 3938 | |
| 3939 | TInfoSinkBase fnOut; |
| 3940 | |
| 3941 | fnOut << "void " << function.functionName << "(out " |
| 3942 | << typeName << " a[" << type.getArraySize() << "]"; |
| 3943 | for (int i = 0; i < type.getArraySize(); ++i) |
| 3944 | { |
| 3945 | fnOut << ", " << typeName << " b" << i; |
| 3946 | } |
| 3947 | fnOut << ")\n" |
| 3948 | "{\n"; |
| 3949 | |
| 3950 | for (int i = 0; i < type.getArraySize(); ++i) |
| 3951 | { |
| 3952 | fnOut << " a[" << i << "] = b" << i << ";\n"; |
| 3953 | } |
| 3954 | fnOut << "}\n"; |
| 3955 | |
| 3956 | function.functionDefinition = fnOut.c_str(); |
| 3957 | |
| 3958 | mArrayConstructIntoFunctions.push_back(function); |
| 3959 | |
| 3960 | return function.functionName; |
| 3961 | } |
| 3962 | |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 3963 | void OutputHLSL::ensureStructDefined(const TType &type) |
| 3964 | { |
| 3965 | TStructure *structure = type.getStruct(); |
| 3966 | |
| 3967 | if (structure) |
| 3968 | { |
| 3969 | mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr); |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3974 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3975 | } |