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 | |
Jiawei Shao | a75aa3b | 2018-06-21 10:38:28 +0800 | [diff] [blame] | 9 | #include <stdio.h> |
daniel@transgaming.com | 7a7003c | 2010-04-29 03:35:33 +0000 | [diff] [blame] | 10 | #include <algorithm> |
shannon.woods@transgaming.com | fff89b3 | 2013-02-28 23:20:15 +0000 | [diff] [blame] | 11 | #include <cfloat> |
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" |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 16 | #include "compiler/translator/AtomicCounterFunctionHLSL.h" |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 17 | #include "compiler/translator/BuiltInFunctionEmulator.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 18 | #include "compiler/translator/BuiltInFunctionEmulatorHLSL.h" |
Xinghua Cao | 711b7a1 | 2017-10-09 13:38:12 +0800 | [diff] [blame] | 19 | #include "compiler/translator/ImageFunctionHLSL.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 20 | #include "compiler/translator/InfoSink.h" |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 21 | #include "compiler/translator/ResourcesHLSL.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 22 | #include "compiler/translator/StructureHLSL.h" |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 23 | #include "compiler/translator/TextureFunctionHLSL.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 24 | #include "compiler/translator/TranslatorHLSL.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 25 | #include "compiler/translator/UtilsHLSL.h" |
| 26 | #include "compiler/translator/blocklayout.h" |
Olli Etuaho | a07b421 | 2018-03-22 16:13:13 +0200 | [diff] [blame] | 27 | #include "compiler/translator/tree_ops/RemoveSwitchFallThrough.h" |
Olli Etuaho | c26214d | 2018-03-16 10:43:11 +0200 | [diff] [blame] | 28 | #include "compiler/translator/tree_util/FindSymbolNode.h" |
| 29 | #include "compiler/translator/tree_util/NodeSearch.h" |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 30 | #include "compiler/translator/util.h" |
| 31 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 32 | namespace sh |
| 33 | { |
| 34 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 35 | namespace |
| 36 | { |
| 37 | |
| 38 | TString ArrayHelperFunctionName(const char *prefix, const TType &type) |
| 39 | { |
| 40 | TStringStream fnName; |
| 41 | fnName << prefix << "_"; |
Kai Ninomiya | 57ea533 | 2017-11-22 14:04:48 -0800 | [diff] [blame] | 42 | if (type.isArray()) |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 43 | { |
Kai Ninomiya | 57ea533 | 2017-11-22 14:04:48 -0800 | [diff] [blame] | 44 | for (unsigned int arraySize : *type.getArraySizes()) |
| 45 | { |
| 46 | fnName << arraySize << "_"; |
| 47 | } |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 48 | } |
| 49 | fnName << TypeString(type); |
| 50 | return fnName.str(); |
| 51 | } |
| 52 | |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 53 | bool IsDeclarationWrittenOut(TIntermDeclaration *node) |
| 54 | { |
| 55 | TIntermSequence *sequence = node->getSequence(); |
| 56 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
| 57 | ASSERT(sequence->size() == 1); |
| 58 | ASSERT(variable); |
| 59 | return (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal || |
Jiawei Shao | a75aa3b | 2018-06-21 10:38:28 +0800 | [diff] [blame] | 60 | variable->getQualifier() == EvqConst || variable->getQualifier() == EvqShared); |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 61 | } |
| 62 | |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 63 | bool IsInStd140UniformBlock(TIntermTyped *node) |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 64 | { |
| 65 | TIntermBinary *binaryNode = node->getAsBinaryNode(); |
| 66 | |
| 67 | if (binaryNode) |
| 68 | { |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 69 | return IsInStd140UniformBlock(binaryNode->getLeft()); |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | const TType &type = node->getType(); |
| 73 | |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 74 | if (type.getQualifier() == EvqUniform) |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 75 | { |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 76 | // determine if we are in the standard layout |
| 77 | const TInterfaceBlock *interfaceBlock = type.getInterfaceBlock(); |
| 78 | if (interfaceBlock) |
| 79 | { |
| 80 | return (interfaceBlock->blockStorage() == EbsStd140); |
| 81 | } |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | return false; |
| 85 | } |
| 86 | |
Jiawei Shao | a6a7842 | 2018-06-28 08:32:54 +0800 | [diff] [blame] | 87 | const char *GetHLSLAtomicFunctionStringAndLeftParenthesis(TOperator op) |
| 88 | { |
| 89 | switch (op) |
| 90 | { |
| 91 | case EOpAtomicAdd: |
| 92 | return "InterlockedAdd("; |
| 93 | case EOpAtomicMin: |
| 94 | return "InterlockedMin("; |
| 95 | case EOpAtomicMax: |
| 96 | return "InterlockedMax("; |
| 97 | case EOpAtomicAnd: |
| 98 | return "InterlockedAnd("; |
| 99 | case EOpAtomicOr: |
| 100 | return "InterlockedOr("; |
| 101 | case EOpAtomicXor: |
| 102 | return "InterlockedXor("; |
| 103 | case EOpAtomicExchange: |
| 104 | return "InterlockedExchange("; |
| 105 | case EOpAtomicCompSwap: |
| 106 | return "InterlockedCompareExchange("; |
| 107 | default: |
| 108 | UNREACHABLE(); |
| 109 | return ""; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | bool IsAtomicFunctionDirectAssign(const TIntermBinary &node) |
| 114 | { |
| 115 | return node.getOp() == EOpAssign && node.getRight()->getAsAggregate() && |
| 116 | IsAtomicFunction(node.getRight()->getAsAggregate()->getOp()); |
| 117 | } |
| 118 | |
jchen10 | efe061b | 2018-11-13 16:44:40 +0800 | [diff] [blame^] | 119 | const char *kZeros = "_ANGLE_ZEROS_"; |
| 120 | constexpr int kZeroCount = 256; |
| 121 | std::string DefineZeroArray() |
| 122 | { |
| 123 | std::stringstream ss; |
| 124 | // For 'static', if the declaration does not include an initializer, the value is set to zero. |
| 125 | // https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/dx-graphics-hlsl-variable-syntax |
| 126 | ss << "static uint " << kZeros << "[" << kZeroCount << "];\n"; |
| 127 | return ss.str(); |
| 128 | } |
| 129 | |
| 130 | std::string GetZeroInitializer(size_t size) |
| 131 | { |
| 132 | std::stringstream ss; |
| 133 | size_t quotient = size / kZeroCount; |
| 134 | size_t reminder = size % kZeroCount; |
| 135 | |
| 136 | for (size_t i = 0; i < quotient; ++i) |
| 137 | { |
| 138 | if (i != 0) |
| 139 | { |
| 140 | ss << ", "; |
| 141 | } |
| 142 | ss << kZeros; |
| 143 | } |
| 144 | |
| 145 | for (size_t i = 0; i < reminder; ++i) |
| 146 | { |
| 147 | if (quotient != 0 || i != 0) |
| 148 | { |
| 149 | ss << ", "; |
| 150 | } |
| 151 | ss << "0"; |
| 152 | } |
| 153 | |
| 154 | return ss.str(); |
| 155 | } |
| 156 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 157 | } // anonymous namespace |
| 158 | |
Olli Etuaho | c71862a | 2017-12-21 12:58:29 +0200 | [diff] [blame] | 159 | TReferencedBlock::TReferencedBlock(const TInterfaceBlock *aBlock, |
| 160 | const TVariable *aInstanceVariable) |
| 161 | : block(aBlock), instanceVariable(aInstanceVariable) |
| 162 | { |
| 163 | } |
| 164 | |
Olli Etuaho | 56a2f95 | 2016-12-08 12:16:27 +0000 | [diff] [blame] | 165 | void OutputHLSL::writeFloat(TInfoSinkBase &out, float f) |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 166 | { |
Olli Etuaho | 56a2f95 | 2016-12-08 12:16:27 +0000 | [diff] [blame] | 167 | // This is known not to work for NaN on all drivers but make the best effort to output NaNs |
| 168 | // regardless. |
| 169 | if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300 && |
| 170 | mOutputType == SH_HLSL_4_1_OUTPUT) |
| 171 | { |
| 172 | out << "asfloat(" << gl::bitCast<uint32_t>(f) << "u)"; |
| 173 | } |
| 174 | else |
| 175 | { |
| 176 | out << std::min(FLT_MAX, std::max(-FLT_MAX, f)); |
| 177 | } |
| 178 | } |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 179 | |
Olli Etuaho | 56a2f95 | 2016-12-08 12:16:27 +0000 | [diff] [blame] | 180 | void OutputHLSL::writeSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion) |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 181 | { |
| 182 | ASSERT(constUnion != nullptr); |
| 183 | switch (constUnion->getType()) |
| 184 | { |
| 185 | case EbtFloat: |
Olli Etuaho | 56a2f95 | 2016-12-08 12:16:27 +0000 | [diff] [blame] | 186 | writeFloat(out, constUnion->getFConst()); |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 187 | break; |
| 188 | case EbtInt: |
| 189 | out << constUnion->getIConst(); |
| 190 | break; |
| 191 | case EbtUInt: |
| 192 | out << constUnion->getUConst(); |
| 193 | break; |
| 194 | case EbtBool: |
| 195 | out << constUnion->getBConst(); |
| 196 | break; |
| 197 | default: |
| 198 | UNREACHABLE(); |
| 199 | } |
| 200 | } |
| 201 | |
Olli Etuaho | 56a2f95 | 2016-12-08 12:16:27 +0000 | [diff] [blame] | 202 | const TConstantUnion *OutputHLSL::writeConstantUnionArray(TInfoSinkBase &out, |
| 203 | const TConstantUnion *const constUnion, |
| 204 | const size_t size) |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 205 | { |
| 206 | const TConstantUnion *constUnionIterated = constUnion; |
| 207 | for (size_t i = 0; i < size; i++, constUnionIterated++) |
| 208 | { |
Olli Etuaho | 56a2f95 | 2016-12-08 12:16:27 +0000 | [diff] [blame] | 209 | writeSingleConstant(out, constUnionIterated); |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 210 | |
| 211 | if (i != size - 1) |
| 212 | { |
| 213 | out << ", "; |
| 214 | } |
| 215 | } |
| 216 | return constUnionIterated; |
| 217 | } |
| 218 | |
Qiankun Miao | 7ebb97f | 2016-09-08 18:01:50 +0800 | [diff] [blame] | 219 | OutputHLSL::OutputHLSL(sh::GLenum shaderType, |
| 220 | int shaderVersion, |
| 221 | const TExtensionBehavior &extensionBehavior, |
| 222 | const char *sourcePath, |
| 223 | ShShaderOutput outputType, |
| 224 | int numRenderTargets, |
| 225 | const std::vector<Uniform> &uniforms, |
Olli Etuaho | 2d88e9b | 2017-07-21 16:52:03 +0300 | [diff] [blame] | 226 | ShCompileOptions compileOptions, |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 227 | sh::WorkGroupSize workGroupSize, |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 228 | TSymbolTable *symbolTable, |
| 229 | PerformanceDiagnostics *perfDiagnostics) |
Olli Etuaho | 2d88e9b | 2017-07-21 16:52:03 +0300 | [diff] [blame] | 230 | : TIntermTraverser(true, true, true, symbolTable), |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 231 | mShaderType(shaderType), |
| 232 | mShaderVersion(shaderVersion), |
| 233 | mExtensionBehavior(extensionBehavior), |
| 234 | mSourcePath(sourcePath), |
| 235 | mOutputType(outputType), |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 236 | mCompileOptions(compileOptions), |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 237 | mInsideFunction(false), |
| 238 | mInsideMain(false), |
Sam McNally | 5a0edc6 | 2015-06-30 12:36:07 +1000 | [diff] [blame] | 239 | mNumRenderTargets(numRenderTargets), |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 240 | mCurrentFunctionMetadata(nullptr), |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 241 | mWorkGroupSize(workGroupSize), |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 242 | mPerfDiagnostics(perfDiagnostics) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 243 | { |
Jiawei Shao | a75aa3b | 2018-06-21 10:38:28 +0800 | [diff] [blame] | 244 | mUsesFragColor = false; |
| 245 | mUsesFragData = false; |
| 246 | mUsesDepthRange = false; |
| 247 | mUsesFragCoord = false; |
| 248 | mUsesPointCoord = false; |
| 249 | mUsesFrontFacing = false; |
| 250 | mUsesPointSize = false; |
| 251 | mUsesInstanceID = false; |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 252 | mHasMultiviewExtensionEnabled = |
| 253 | IsExtensionEnabled(mExtensionBehavior, TExtension::OVR_multiview); |
Martin Radev | 41ac68e | 2017-06-06 12:16:58 +0300 | [diff] [blame] | 254 | mUsesViewID = false; |
Corentin Wallez | b076add | 2016-01-11 16:45:46 -0500 | [diff] [blame] | 255 | mUsesVertexID = false; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 256 | mUsesFragDepth = false; |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 257 | mUsesNumWorkGroups = false; |
| 258 | mUsesWorkGroupID = false; |
| 259 | mUsesLocalInvocationID = false; |
| 260 | mUsesGlobalInvocationID = false; |
| 261 | mUsesLocalInvocationIndex = false; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 262 | mUsesXor = false; |
| 263 | mUsesDiscardRewriting = false; |
| 264 | mUsesNestedBreak = false; |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 265 | mRequiresIEEEStrictCompiling = false; |
jchen10 | efe061b | 2018-11-13 16:44:40 +0800 | [diff] [blame^] | 266 | mUseZeroArray = false; |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 267 | |
daniel@transgaming.com | b6ef8f1 | 2010-11-15 16:41:14 +0000 | [diff] [blame] | 268 | mUniqueIndex = 0; |
daniel@transgaming.com | 89431aa | 2012-05-31 01:20:29 +0000 | [diff] [blame] | 269 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 270 | mOutputLod0Function = false; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 271 | mInsideDiscontinuousLoop = false; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 272 | mNestedLoopDepth = 0; |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 273 | |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 274 | mExcessiveLoopIndex = nullptr; |
daniel@transgaming.com | 652468c | 2012-12-20 21:11:57 +0000 | [diff] [blame] | 275 | |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 276 | mStructureHLSL = new StructureHLSL; |
| 277 | mTextureFunctionHLSL = new TextureFunctionHLSL; |
| 278 | mImageFunctionHLSL = new ImageFunctionHLSL; |
| 279 | mAtomicCounterFunctionHLSL = new AtomicCounterFunctionHLSL; |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 280 | |
Olli Etuaho | d8724a9 | 2017-12-29 18:40:36 +0200 | [diff] [blame] | 281 | unsigned int firstUniformRegister = |
| 282 | ((compileOptions & SH_SKIP_D3D_CONSTANT_REGISTER_ZERO) != 0) ? 1u : 0u; |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 283 | mResourcesHLSL = new ResourcesHLSL(mStructureHLSL, outputType, uniforms, firstUniformRegister); |
Olli Etuaho | d8724a9 | 2017-12-29 18:40:36 +0200 | [diff] [blame] | 284 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 285 | if (mOutputType == SH_HLSL_3_0_OUTPUT) |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 286 | { |
Arun Patole | 6341939 | 2015-03-13 11:51:07 +0530 | [diff] [blame] | 287 | // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront. |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 288 | // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and |
| 289 | // dx_ViewAdjust. |
Arun Patole | 6341939 | 2015-03-13 11:51:07 +0530 | [diff] [blame] | 290 | // In both cases total 3 uniform registers need to be reserved. |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 291 | mResourcesHLSL->reserveUniformRegisters(3); |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 292 | } |
daniel@transgaming.com | a390e1e | 2013-01-11 04:09:39 +0000 | [diff] [blame] | 293 | |
Geoff Lang | 00140f4 | 2016-02-03 18:47:33 +0000 | [diff] [blame] | 294 | // Reserve registers for the default uniform block and driver constants |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 295 | mResourcesHLSL->reserveUniformBlockRegisters(2); |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 296 | |
| 297 | mSSBOOutputHLSL = new ShaderStorageBlockOutputHLSL(this, symbolTable, mResourcesHLSL); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 298 | } |
| 299 | |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 300 | OutputHLSL::~OutputHLSL() |
| 301 | { |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 302 | SafeDelete(mSSBOOutputHLSL); |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 303 | SafeDelete(mStructureHLSL); |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 304 | SafeDelete(mResourcesHLSL); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 305 | SafeDelete(mTextureFunctionHLSL); |
Xinghua Cao | 711b7a1 | 2017-10-09 13:38:12 +0800 | [diff] [blame] | 306 | SafeDelete(mImageFunctionHLSL); |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 307 | SafeDelete(mAtomicCounterFunctionHLSL); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 308 | for (auto &eqFunction : mStructEqualityFunctions) |
| 309 | { |
| 310 | SafeDelete(eqFunction); |
| 311 | } |
| 312 | for (auto &eqFunction : mArrayEqualityFunctions) |
| 313 | { |
| 314 | SafeDelete(eqFunction); |
| 315 | } |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 318 | void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink) |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 319 | { |
Olli Etuaho | 8efc5ad | 2015-03-03 17:21:10 +0200 | [diff] [blame] | 320 | BuiltInFunctionEmulator builtInFunctionEmulator; |
| 321 | InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator); |
Shao | 6f0a0dc | 2016-09-27 13:51:29 +0800 | [diff] [blame] | 322 | if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0) |
| 323 | { |
| 324 | InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator, |
| 325 | mShaderVersion); |
| 326 | } |
| 327 | |
Olli Etuaho | dfa75e8 | 2017-01-23 09:43:06 -0800 | [diff] [blame] | 328 | builtInFunctionEmulator.markBuiltInFunctionsForEmulation(treeRoot); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 329 | |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 330 | // Now that we are done changing the AST, do the analyses need for HLSL generation |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 331 | CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr); |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 332 | ASSERT(success == CallDAG::INITDAG_SUCCESS); |
| 333 | mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag); |
Corentin Wallez | f4eab3b | 2015-03-18 12:55:45 -0700 | [diff] [blame] | 334 | |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 335 | const std::vector<MappedStruct> std140Structs = FlagStd140Structs(treeRoot); |
| 336 | // TODO(oetuaho): The std140Structs could be filtered based on which ones actually get used in |
| 337 | // the shader code. When we add shader storage blocks we might also consider an alternative |
| 338 | // solution, since the struct mapping won't work very well for shader storage blocks. |
| 339 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 340 | // 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] | 341 | mInfoSinkStack.push(&mBody); |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 342 | treeRoot->traverse(this); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 343 | mInfoSinkStack.pop(); |
| 344 | |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 345 | mInfoSinkStack.push(&mFooter); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 346 | mInfoSinkStack.pop(); |
| 347 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 348 | mInfoSinkStack.push(&mHeader); |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 349 | header(mHeader, std140Structs, &builtInFunctionEmulator); |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 350 | mInfoSinkStack.pop(); |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 351 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 352 | objSink << mHeader.c_str(); |
| 353 | objSink << mBody.c_str(); |
| 354 | objSink << mFooter.c_str(); |
Olli Etuaho | e17e319 | 2015-01-02 12:47:59 +0200 | [diff] [blame] | 355 | |
Olli Etuaho | dfa75e8 | 2017-01-23 09:43:06 -0800 | [diff] [blame] | 356 | builtInFunctionEmulator.cleanup(); |
daniel@transgaming.com | 950f993 | 2010-04-13 03:26:14 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Qin Jiajia | a602f90 | 2018-09-11 14:40:24 +0800 | [diff] [blame] | 359 | const std::map<std::string, unsigned int> &OutputHLSL::getShaderStorageBlockRegisterMap() const |
| 360 | { |
| 361 | return mResourcesHLSL->getShaderStorageBlockRegisterMap(); |
| 362 | } |
| 363 | |
Jiajia Qin | 9b11ea4 | 2017-07-11 16:50:08 +0800 | [diff] [blame] | 364 | const std::map<std::string, unsigned int> &OutputHLSL::getUniformBlockRegisterMap() const |
Jamie Madill | 4e1fd41 | 2014-07-10 17:50:10 -0400 | [diff] [blame] | 365 | { |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 366 | return mResourcesHLSL->getUniformBlockRegisterMap(); |
Jamie Madill | 4e1fd41 | 2014-07-10 17:50:10 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Jamie Madill | 9fe25e9 | 2014-07-18 10:33:08 -0400 | [diff] [blame] | 369 | const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const |
| 370 | { |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 371 | return mResourcesHLSL->getUniformRegisterMap(); |
Jamie Madill | 9fe25e9 | 2014-07-18 10:33:08 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 374 | TString OutputHLSL::structInitializerString(int indent, |
| 375 | const TType &type, |
| 376 | const TString &name) const |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 377 | { |
| 378 | TString init; |
| 379 | |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 380 | TString indentString; |
| 381 | for (int spaces = 0; spaces < indent; spaces++) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 382 | { |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 383 | indentString += " "; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 384 | } |
| 385 | |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 386 | if (type.isArray()) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 387 | { |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 388 | init += indentString + "{\n"; |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 389 | for (unsigned int arrayIndex = 0u; arrayIndex < type.getOutermostArraySize(); ++arrayIndex) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 390 | { |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 391 | TStringStream indexedString; |
| 392 | indexedString << name << "[" << arrayIndex << "]"; |
| 393 | TType elementType = type; |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 394 | elementType.toArrayElementType(); |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 395 | init += structInitializerString(indent + 1, elementType, indexedString.str()); |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 396 | if (arrayIndex < type.getOutermostArraySize() - 1) |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 397 | { |
| 398 | init += ","; |
| 399 | } |
| 400 | init += "\n"; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 401 | } |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 402 | init += indentString + "}"; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 403 | } |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 404 | else if (type.getBasicType() == EbtStruct) |
| 405 | { |
| 406 | init += indentString + "{\n"; |
| 407 | const TStructure &structure = *type.getStruct(); |
| 408 | const TFieldList &fields = structure.fields(); |
| 409 | for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) |
| 410 | { |
| 411 | const TField &field = *fields[fieldIndex]; |
| 412 | const TString &fieldName = name + "." + Decorate(field.name()); |
| 413 | const TType &fieldType = *field.type(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 414 | |
Olli Etuaho | ed049ab | 2017-06-30 17:38:33 +0300 | [diff] [blame] | 415 | init += structInitializerString(indent + 1, fieldType, fieldName); |
| 416 | if (fieldIndex < fields.size() - 1) |
| 417 | { |
| 418 | init += ","; |
| 419 | } |
| 420 | init += "\n"; |
| 421 | } |
| 422 | init += indentString + "}"; |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | init += indentString + name; |
| 427 | } |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 428 | |
| 429 | return init; |
| 430 | } |
| 431 | |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 432 | TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std140Structs) const |
| 433 | { |
| 434 | TString mappedStructs; |
| 435 | |
| 436 | for (auto &mappedStruct : std140Structs) |
| 437 | { |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 438 | const TInterfaceBlock *interfaceBlock = |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 439 | mappedStruct.blockDeclarator->getType().getInterfaceBlock(); |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 440 | TQualifier qualifier = mappedStruct.blockDeclarator->getType().getQualifier(); |
| 441 | switch (qualifier) |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 442 | { |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 443 | case EvqUniform: |
| 444 | if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0) |
| 445 | { |
| 446 | continue; |
| 447 | } |
| 448 | break; |
| 449 | case EvqBuffer: |
| 450 | continue; |
| 451 | default: |
| 452 | UNREACHABLE(); |
| 453 | return mappedStructs; |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | unsigned int instanceCount = 1u; |
| 457 | bool isInstanceArray = mappedStruct.blockDeclarator->isArray(); |
| 458 | if (isInstanceArray) |
| 459 | { |
| 460 | instanceCount = mappedStruct.blockDeclarator->getOutermostArraySize(); |
| 461 | } |
| 462 | |
| 463 | for (unsigned int instanceArrayIndex = 0; instanceArrayIndex < instanceCount; |
| 464 | ++instanceArrayIndex) |
| 465 | { |
| 466 | TString originalName; |
| 467 | TString mappedName("map"); |
| 468 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 469 | if (mappedStruct.blockDeclarator->variable().symbolType() != SymbolType::Empty) |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 470 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 471 | const ImmutableString &instanceName = |
| 472 | mappedStruct.blockDeclarator->variable().name(); |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 473 | unsigned int instanceStringArrayIndex = GL_INVALID_INDEX; |
| 474 | if (isInstanceArray) |
| 475 | instanceStringArrayIndex = instanceArrayIndex; |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 476 | TString instanceString = mResourcesHLSL->InterfaceBlockInstanceString( |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 477 | instanceName, instanceStringArrayIndex); |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 478 | originalName += instanceString; |
| 479 | mappedName += instanceString; |
| 480 | originalName += "."; |
| 481 | mappedName += "_"; |
| 482 | } |
| 483 | |
| 484 | TString fieldName = Decorate(mappedStruct.field->name()); |
| 485 | originalName += fieldName; |
| 486 | mappedName += fieldName; |
| 487 | |
| 488 | TType *structType = mappedStruct.field->type(); |
| 489 | mappedStructs += |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 490 | "static " + Decorate(structType->getStruct()->name()) + " " + mappedName; |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 491 | |
| 492 | if (structType->isArray()) |
| 493 | { |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 494 | mappedStructs += ArrayString(*mappedStruct.field->type()).data(); |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | mappedStructs += " =\n"; |
| 498 | mappedStructs += structInitializerString(0, *structType, originalName); |
| 499 | mappedStructs += ";\n"; |
| 500 | } |
| 501 | } |
| 502 | return mappedStructs; |
| 503 | } |
| 504 | |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 505 | void OutputHLSL::writeReferencedAttributes(TInfoSinkBase &out) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 506 | { |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 507 | for (const auto &attribute : mReferencedAttributes) |
| 508 | { |
| 509 | const TType &type = attribute.second->getType(); |
| 510 | const ImmutableString &name = attribute.second->name(); |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 511 | |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 512 | out << "static " << TypeString(type) << " " << Decorate(name) << ArrayString(type) << " = " |
| 513 | << zeroInitializer(type) << ";\n"; |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | void OutputHLSL::writeReferencedVaryings(TInfoSinkBase &out) const |
| 518 | { |
Olli Etuaho | b8cb939 | 2017-12-20 14:23:19 +0200 | [diff] [blame] | 519 | for (const auto &varying : mReferencedVaryings) |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 520 | { |
Jiawei Shao | 203b26f | 2018-07-25 10:30:43 +0800 | [diff] [blame] | 521 | const TType &type = varying.second->getType(); |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 522 | |
| 523 | // Program linking depends on this exact format |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 524 | out << "static " << InterpolationString(type.getQualifier()) << " " << TypeString(type) |
Olli Etuaho | da41ac6 | 2018-07-19 16:45:32 +0300 | [diff] [blame] | 525 | << " " << DecorateVariableIfNeeded(*varying.second) << ArrayString(type) << " = " |
| 526 | << zeroInitializer(type) << ";\n"; |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 527 | } |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 528 | } |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 529 | |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 530 | void OutputHLSL::header(TInfoSinkBase &out, |
| 531 | const std::vector<MappedStruct> &std140Structs, |
| 532 | const BuiltInFunctionEmulator *builtInFunctionEmulator) const |
| 533 | { |
| 534 | TString mappedStructs = generateStructMapping(std140Structs); |
daniel@transgaming.com | 8803b85 | 2012-12-20 21:11:47 +0000 | [diff] [blame] | 535 | |
Jamie Madill | 8daaba1 | 2014-06-13 10:04:33 -0400 | [diff] [blame] | 536 | out << mStructureHLSL->structsHeader(); |
Jamie Madill | 529077d | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 537 | |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 538 | mResourcesHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms, mSymbolTable); |
| 539 | out << mResourcesHLSL->uniformBlocksHeader(mReferencedUniformBlocks); |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 540 | mSSBOOutputHLSL->writeShaderStorageBlocksHeader(out); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 541 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 542 | if (!mEqualityFunctions.empty()) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 543 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 544 | out << "\n// Equality functions\n\n"; |
| 545 | for (const auto &eqFunction : mEqualityFunctions) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 546 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 547 | out << eqFunction->functionDefinition << "\n"; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 548 | } |
| 549 | } |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 550 | if (!mArrayAssignmentFunctions.empty()) |
| 551 | { |
| 552 | out << "\n// Assignment functions\n\n"; |
| 553 | for (const auto &assignmentFunction : mArrayAssignmentFunctions) |
| 554 | { |
| 555 | out << assignmentFunction.functionDefinition << "\n"; |
| 556 | } |
| 557 | } |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 558 | if (!mArrayConstructIntoFunctions.empty()) |
| 559 | { |
| 560 | out << "\n// Array constructor functions\n\n"; |
| 561 | for (const auto &constructIntoFunction : mArrayConstructIntoFunctions) |
| 562 | { |
| 563 | out << constructIntoFunction.functionDefinition << "\n"; |
| 564 | } |
| 565 | } |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 566 | |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 567 | if (mUsesDiscardRewriting) |
| 568 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 569 | out << "#define ANGLE_USES_DISCARD_REWRITING\n"; |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 570 | } |
| 571 | |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 572 | if (mUsesNestedBreak) |
| 573 | { |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 574 | out << "#define ANGLE_USES_NESTED_BREAK\n"; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 575 | } |
| 576 | |
Arun Patole | 44efa0b | 2015-03-04 17:11:05 +0530 | [diff] [blame] | 577 | if (mRequiresIEEEStrictCompiling) |
| 578 | { |
| 579 | out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n"; |
| 580 | } |
| 581 | |
Nicolas Capens | 5e0c80a | 2014-10-10 10:11:54 -0400 | [diff] [blame] | 582 | out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n" |
| 583 | "#define LOOP [loop]\n" |
| 584 | "#define FLATTEN [flatten]\n" |
| 585 | "#else\n" |
| 586 | "#define LOOP\n" |
| 587 | "#define FLATTEN\n" |
| 588 | "#endif\n"; |
| 589 | |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 590 | // array stride for atomic counter buffers is always 4 per original extension |
| 591 | // ARB_shader_atomic_counters and discussion on |
| 592 | // https://github.com/KhronosGroup/OpenGL-API/issues/5 |
| 593 | out << "\n#define ATOMIC_COUNTER_ARRAY_STRIDE 4\n\n"; |
| 594 | |
jchen10 | efe061b | 2018-11-13 16:44:40 +0800 | [diff] [blame^] | 595 | if (mUseZeroArray) |
| 596 | { |
| 597 | out << DefineZeroArray() << "\n"; |
| 598 | } |
| 599 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 600 | if (mShaderType == GL_FRAGMENT_SHADER) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 601 | { |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 602 | const bool usingMRTExtension = |
| 603 | IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers); |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 604 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 605 | out << "// Varyings\n"; |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 606 | writeReferencedVaryings(out); |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 607 | out << "\n"; |
| 608 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 609 | if (mShaderVersion >= 300) |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 610 | { |
Olli Etuaho | 93b059d | 2017-12-20 12:46:58 +0200 | [diff] [blame] | 611 | for (const auto &outputVariable : mReferencedOutputVariables) |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 612 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 613 | const ImmutableString &variableName = outputVariable.second->name(); |
Jiawei Shao | a75aa3b | 2018-06-21 10:38:28 +0800 | [diff] [blame] | 614 | const TType &variableType = outputVariable.second->getType(); |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 615 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 616 | out << "static " << TypeString(variableType) << " out_" << variableName |
| 617 | << ArrayString(variableType) << " = " << zeroInitializer(variableType) << ";\n"; |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 618 | } |
shannon.woods%transgaming.com@gtempaccount.com | a28864c | 2013-04-13 03:32:03 +0000 | [diff] [blame] | 619 | } |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 620 | else |
| 621 | { |
| 622 | const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1; |
| 623 | |
Jiawei Shao | a75aa3b | 2018-06-21 10:38:28 +0800 | [diff] [blame] | 624 | out << "static float4 gl_Color[" << numColorValues |
| 625 | << "] =\n" |
| 626 | "{\n"; |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 627 | for (unsigned int i = 0; i < numColorValues; i++) |
| 628 | { |
| 629 | out << " float4(0, 0, 0, 0)"; |
| 630 | if (i + 1 != numColorValues) |
| 631 | { |
| 632 | out << ","; |
| 633 | } |
| 634 | out << "\n"; |
| 635 | } |
| 636 | |
| 637 | out << "};\n"; |
| 638 | } |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 639 | |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 640 | if (mUsesFragDepth) |
| 641 | { |
| 642 | out << "static float gl_Depth = 0.0;\n"; |
| 643 | } |
| 644 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 645 | if (mUsesFragCoord) |
| 646 | { |
| 647 | out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n"; |
| 648 | } |
| 649 | |
| 650 | if (mUsesPointCoord) |
| 651 | { |
| 652 | out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n"; |
| 653 | } |
| 654 | |
| 655 | if (mUsesFrontFacing) |
| 656 | { |
| 657 | out << "static bool gl_FrontFacing = false;\n"; |
| 658 | } |
| 659 | |
| 660 | out << "\n"; |
| 661 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 662 | if (mUsesDepthRange) |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 663 | { |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 664 | out << "struct gl_DepthRangeParameters\n" |
| 665 | "{\n" |
| 666 | " float near;\n" |
| 667 | " float far;\n" |
| 668 | " float diff;\n" |
| 669 | "};\n" |
| 670 | "\n"; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 673 | 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] | 674 | { |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 675 | out << "cbuffer DriverConstants : register(b1)\n" |
| 676 | "{\n"; |
| 677 | |
| 678 | if (mUsesDepthRange) |
| 679 | { |
| 680 | out << " float3 dx_DepthRange : packoffset(c0);\n"; |
| 681 | } |
| 682 | |
| 683 | if (mUsesFragCoord) |
| 684 | { |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 685 | out << " float4 dx_ViewCoords : packoffset(c1);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | if (mUsesFragCoord || mUsesFrontFacing) |
| 689 | { |
| 690 | out << " float3 dx_DepthFront : packoffset(c2);\n"; |
| 691 | } |
| 692 | |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 693 | if (mUsesFragCoord) |
| 694 | { |
| 695 | // dx_ViewScale is only used in the fragment shader to correct |
| 696 | // the value for glFragCoord if necessary |
| 697 | out << " float2 dx_ViewScale : packoffset(c3);\n"; |
| 698 | } |
| 699 | |
Martin Radev | 72b4e1e | 2017-08-31 15:42:56 +0300 | [diff] [blame] | 700 | if (mHasMultiviewExtensionEnabled) |
| 701 | { |
| 702 | // We have to add a value which we can use to keep track of which multi-view code |
| 703 | // path is to be selected in the GS. |
| 704 | out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n"; |
| 705 | } |
| 706 | |
Olli Etuaho | 618bebc | 2016-01-15 16:40:00 +0200 | [diff] [blame] | 707 | if (mOutputType == SH_HLSL_4_1_OUTPUT) |
| 708 | { |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 709 | mResourcesHLSL->samplerMetadataUniforms(out, "c4"); |
Olli Etuaho | 618bebc | 2016-01-15 16:40:00 +0200 | [diff] [blame] | 710 | } |
| 711 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 712 | out << "};\n"; |
| 713 | } |
| 714 | else |
| 715 | { |
| 716 | if (mUsesDepthRange) |
| 717 | { |
| 718 | out << "uniform float3 dx_DepthRange : register(c0);"; |
| 719 | } |
| 720 | |
| 721 | if (mUsesFragCoord) |
| 722 | { |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 723 | out << "uniform float4 dx_ViewCoords : register(c1);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | if (mUsesFragCoord || mUsesFrontFacing) |
| 727 | { |
| 728 | out << "uniform float3 dx_DepthFront : register(c2);\n"; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | out << "\n"; |
| 733 | |
| 734 | if (mUsesDepthRange) |
| 735 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 736 | out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, " |
| 737 | "dx_DepthRange.y, dx_DepthRange.z};\n" |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 738 | "\n"; |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 739 | } |
daniel@transgaming.com | 5024cc4 | 2010-04-20 18:52:04 +0000 | [diff] [blame] | 740 | |
shannon.woods%transgaming.com@gtempaccount.com | aa8b5cf | 2013-04-13 03:31:55 +0000 | [diff] [blame] | 741 | if (usingMRTExtension && mNumRenderTargets > 1) |
| 742 | { |
| 743 | out << "#define GL_USES_MRT\n"; |
| 744 | } |
| 745 | |
| 746 | if (mUsesFragColor) |
| 747 | { |
| 748 | out << "#define GL_USES_FRAG_COLOR\n"; |
| 749 | } |
| 750 | |
| 751 | if (mUsesFragData) |
| 752 | { |
| 753 | out << "#define GL_USES_FRAG_DATA\n"; |
| 754 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 755 | } |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 756 | else if (mShaderType == GL_VERTEX_SHADER) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 757 | { |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 758 | out << "// Attributes\n"; |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 759 | writeReferencedAttributes(out); |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 760 | out << "\n" |
| 761 | "static float4 gl_Position = float4(0, 0, 0, 0);\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 762 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 763 | if (mUsesPointSize) |
| 764 | { |
| 765 | out << "static float gl_PointSize = float(1);\n"; |
| 766 | } |
| 767 | |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 768 | if (mUsesInstanceID) |
| 769 | { |
| 770 | out << "static int gl_InstanceID;"; |
| 771 | } |
| 772 | |
Corentin Wallez | b076add | 2016-01-11 16:45:46 -0500 | [diff] [blame] | 773 | if (mUsesVertexID) |
| 774 | { |
| 775 | out << "static int gl_VertexID;"; |
| 776 | } |
| 777 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 778 | out << "\n" |
| 779 | "// Varyings\n"; |
Olli Etuaho | d8b1c5c | 2018-06-20 12:08:46 +0300 | [diff] [blame] | 780 | writeReferencedVaryings(out); |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 781 | out << "\n"; |
| 782 | |
| 783 | if (mUsesDepthRange) |
| 784 | { |
| 785 | out << "struct gl_DepthRangeParameters\n" |
| 786 | "{\n" |
| 787 | " float near;\n" |
| 788 | " float far;\n" |
| 789 | " float diff;\n" |
| 790 | "};\n" |
| 791 | "\n"; |
| 792 | } |
| 793 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 794 | 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] | 795 | { |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 796 | out << "cbuffer DriverConstants : register(b1)\n" |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 797 | "{\n"; |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 798 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 799 | if (mUsesDepthRange) |
| 800 | { |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 801 | out << " float3 dx_DepthRange : packoffset(c0);\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 802 | } |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 803 | |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 804 | // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 |
| 805 | // shaders. However, we declare it for all shaders (including Feature Level 10+). |
| 806 | // The bytecode is the same whether we declare it or not, since D3DCompiler removes it |
| 807 | // if it's unused. |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 808 | out << " float4 dx_ViewAdjust : packoffset(c1);\n"; |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 809 | out << " float2 dx_ViewCoords : packoffset(c2);\n"; |
Austin Kinross | 2a63b3f | 2016-02-08 12:29:08 -0800 | [diff] [blame] | 810 | out << " float2 dx_ViewScale : packoffset(c3);\n"; |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 811 | |
Martin Radev | 72b4e1e | 2017-08-31 15:42:56 +0300 | [diff] [blame] | 812 | if (mHasMultiviewExtensionEnabled) |
| 813 | { |
| 814 | // We have to add a value which we can use to keep track of which multi-view code |
| 815 | // path is to be selected in the GS. |
| 816 | out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n"; |
| 817 | } |
| 818 | |
Olli Etuaho | 618bebc | 2016-01-15 16:40:00 +0200 | [diff] [blame] | 819 | if (mOutputType == SH_HLSL_4_1_OUTPUT) |
| 820 | { |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 821 | mResourcesHLSL->samplerMetadataUniforms(out, "c4"); |
Olli Etuaho | 618bebc | 2016-01-15 16:40:00 +0200 | [diff] [blame] | 822 | } |
| 823 | |
Austin Kinross | 4fd18b1 | 2014-12-22 12:32:05 -0800 | [diff] [blame] | 824 | out << "};\n" |
| 825 | "\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 826 | } |
| 827 | else |
| 828 | { |
| 829 | if (mUsesDepthRange) |
| 830 | { |
| 831 | out << "uniform float3 dx_DepthRange : register(c0);\n"; |
| 832 | } |
| 833 | |
Cooper Partin | e6664f0 | 2015-01-09 16:22:24 -0800 | [diff] [blame] | 834 | out << "uniform float4 dx_ViewAdjust : register(c1);\n"; |
| 835 | out << "uniform float2 dx_ViewCoords : register(c2);\n" |
shannon.woods@transgaming.com | a14ecf3 | 2013-02-28 23:09:42 +0000 | [diff] [blame] | 836 | "\n"; |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 837 | } |
| 838 | |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 839 | if (mUsesDepthRange) |
| 840 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 841 | out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, " |
| 842 | "dx_DepthRange.y, dx_DepthRange.z};\n" |
shannon.woods@transgaming.com | 46a5b87 | 2013-01-25 21:52:57 +0000 | [diff] [blame] | 843 | "\n"; |
| 844 | } |
Nicolas Capens | e0ba27a | 2013-06-24 16:10:52 -0400 | [diff] [blame] | 845 | } |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 846 | else // Compute shader |
| 847 | { |
| 848 | ASSERT(mShaderType == GL_COMPUTE_SHADER); |
Xinghua Cao | 73badc0 | 2017-03-29 19:14:53 +0800 | [diff] [blame] | 849 | |
| 850 | out << "cbuffer DriverConstants : register(b1)\n" |
| 851 | "{\n"; |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 852 | if (mUsesNumWorkGroups) |
| 853 | { |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 854 | out << " uint3 gl_NumWorkGroups : packoffset(c0);\n"; |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 855 | } |
Xinghua Cao | 73badc0 | 2017-03-29 19:14:53 +0800 | [diff] [blame] | 856 | ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT); |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 857 | mResourcesHLSL->samplerMetadataUniforms(out, "c1"); |
Xinghua Cao | 73badc0 | 2017-03-29 19:14:53 +0800 | [diff] [blame] | 858 | out << "};\n"; |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 859 | |
Jiawei Shao | 203b26f | 2018-07-25 10:30:43 +0800 | [diff] [blame] | 860 | std::ostringstream systemValueDeclaration; |
| 861 | std::ostringstream glBuiltinInitialization; |
| 862 | |
| 863 | systemValueDeclaration << "\nstruct CS_INPUT\n{\n"; |
| 864 | glBuiltinInitialization << "\nvoid initGLBuiltins(CS_INPUT input)\n" |
| 865 | << "{\n"; |
| 866 | |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 867 | if (mUsesWorkGroupID) |
| 868 | { |
| 869 | out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n"; |
Jiawei Shao | 203b26f | 2018-07-25 10:30:43 +0800 | [diff] [blame] | 870 | systemValueDeclaration << " uint3 dx_WorkGroupID : " |
| 871 | << "SV_GroupID;\n"; |
| 872 | glBuiltinInitialization << " gl_WorkGroupID = input.dx_WorkGroupID;\n"; |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | if (mUsesLocalInvocationID) |
| 876 | { |
| 877 | out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n"; |
Jiawei Shao | 203b26f | 2018-07-25 10:30:43 +0800 | [diff] [blame] | 878 | systemValueDeclaration << " uint3 dx_LocalInvocationID : " |
| 879 | << "SV_GroupThreadID;\n"; |
| 880 | glBuiltinInitialization << " gl_LocalInvocationID = input.dx_LocalInvocationID;\n"; |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | if (mUsesGlobalInvocationID) |
| 884 | { |
| 885 | out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n"; |
Jiawei Shao | 203b26f | 2018-07-25 10:30:43 +0800 | [diff] [blame] | 886 | systemValueDeclaration << " uint3 dx_GlobalInvocationID : " |
| 887 | << "SV_DispatchThreadID;\n"; |
| 888 | glBuiltinInitialization << " gl_GlobalInvocationID = input.dx_GlobalInvocationID;\n"; |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 889 | } |
| 890 | |
| 891 | if (mUsesLocalInvocationIndex) |
| 892 | { |
| 893 | out << "static uint gl_LocalInvocationIndex = uint(0);\n"; |
Jiawei Shao | 203b26f | 2018-07-25 10:30:43 +0800 | [diff] [blame] | 894 | systemValueDeclaration << " uint dx_LocalInvocationIndex : " |
| 895 | << "SV_GroupIndex;\n"; |
| 896 | glBuiltinInitialization |
| 897 | << " gl_LocalInvocationIndex = input.dx_LocalInvocationIndex;\n"; |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 898 | } |
Jiawei Shao | 203b26f | 2018-07-25 10:30:43 +0800 | [diff] [blame] | 899 | |
| 900 | systemValueDeclaration << "};\n\n"; |
| 901 | glBuiltinInitialization << "};\n\n"; |
| 902 | |
| 903 | out << systemValueDeclaration.str(); |
| 904 | out << glBuiltinInitialization.str(); |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 905 | } |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 906 | |
Qin Jiajia | 2a12b3d | 2018-05-23 13:42:13 +0800 | [diff] [blame] | 907 | if (!mappedStructs.empty()) |
| 908 | { |
| 909 | out << "// Structures from std140 blocks with padding removed\n"; |
| 910 | out << "\n"; |
| 911 | out << mappedStructs; |
| 912 | out << "\n"; |
| 913 | } |
| 914 | |
Geoff Lang | 1fe74c7 | 2016-08-25 13:23:01 -0400 | [diff] [blame] | 915 | bool getDimensionsIgnoresBaseLevel = |
| 916 | (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0; |
| 917 | mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel); |
Xinghua Cao | 711b7a1 | 2017-10-09 13:38:12 +0800 | [diff] [blame] | 918 | mImageFunctionHLSL->imageFunctionHeader(out); |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 919 | mAtomicCounterFunctionHLSL->atomicCounterFunctionHeader(out); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 920 | |
daniel@transgaming.com | 4af7acc | 2010-05-14 17:30:53 +0000 | [diff] [blame] | 921 | if (mUsesFragCoord) |
| 922 | { |
| 923 | out << "#define GL_USES_FRAG_COORD\n"; |
| 924 | } |
| 925 | |
| 926 | if (mUsesPointCoord) |
| 927 | { |
| 928 | out << "#define GL_USES_POINT_COORD\n"; |
| 929 | } |
| 930 | |
| 931 | if (mUsesFrontFacing) |
| 932 | { |
| 933 | out << "#define GL_USES_FRONT_FACING\n"; |
| 934 | } |
| 935 | |
| 936 | if (mUsesPointSize) |
| 937 | { |
| 938 | out << "#define GL_USES_POINT_SIZE\n"; |
| 939 | } |
| 940 | |
Martin Radev | 41ac68e | 2017-06-06 12:16:58 +0300 | [diff] [blame] | 941 | if (mHasMultiviewExtensionEnabled) |
| 942 | { |
| 943 | out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n"; |
| 944 | } |
| 945 | |
| 946 | if (mUsesViewID) |
| 947 | { |
| 948 | out << "#define GL_USES_VIEW_ID\n"; |
| 949 | } |
| 950 | |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 951 | if (mUsesFragDepth) |
| 952 | { |
| 953 | out << "#define GL_USES_FRAG_DEPTH\n"; |
| 954 | } |
| 955 | |
shannonwoods@chromium.org | 0329988 | 2013-05-30 00:05:26 +0000 | [diff] [blame] | 956 | if (mUsesDepthRange) |
| 957 | { |
| 958 | out << "#define GL_USES_DEPTH_RANGE\n"; |
| 959 | } |
| 960 | |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 961 | if (mUsesXor) |
| 962 | { |
| 963 | out << "bool xor(bool p, bool q)\n" |
| 964 | "{\n" |
| 965 | " return (p || q) && !(p && q);\n" |
| 966 | "}\n" |
| 967 | "\n"; |
| 968 | } |
| 969 | |
Olli Etuaho | dfa75e8 | 2017-01-23 09:43:06 -0800 | [diff] [blame] | 970 | builtInFunctionEmulator->outputEmulatedFunctions(out); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | void OutputHLSL::visitSymbol(TIntermSymbol *node) |
| 974 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 975 | const TVariable &variable = node->variable(); |
| 976 | |
| 977 | // Empty symbols can only appear in declarations and function arguments, and in either of those |
| 978 | // cases the symbol nodes are not visited. |
| 979 | ASSERT(variable.symbolType() != SymbolType::Empty); |
| 980 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 981 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 982 | |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 983 | // Handle accessing std140 structs by value |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 984 | if (IsInStd140UniformBlock(node) && node->getBasicType() == EbtStruct) |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 985 | { |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 986 | out << "map"; |
Jamie Madill | 570e04d | 2013-06-21 09:15:33 -0400 | [diff] [blame] | 987 | } |
| 988 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 989 | const ImmutableString &name = variable.name(); |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 990 | const TSymbolUniqueId &uniqueId = variable.uniqueId(); |
Olli Etuaho | 93b059d | 2017-12-20 12:46:58 +0200 | [diff] [blame] | 991 | |
shannon.woods%transgaming.com@gtempaccount.com | e7d4a24 | 2013-04-13 03:38:33 +0000 | [diff] [blame] | 992 | if (name == "gl_DepthRange") |
daniel@transgaming.com | d7c9810 | 2010-05-14 17:30:48 +0000 | [diff] [blame] | 993 | { |
| 994 | mUsesDepthRange = true; |
| 995 | out << name; |
| 996 | } |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 997 | else if (IsAtomicCounter(variable.getType().getBasicType())) |
| 998 | { |
| 999 | const TType &variableType = variable.getType(); |
| 1000 | if (variableType.getQualifier() == EvqUniform) |
| 1001 | { |
| 1002 | TLayoutQualifier layout = variableType.getLayoutQualifier(); |
| 1003 | mReferencedUniforms[uniqueId.get()] = &variable; |
| 1004 | out << getAtomicCounterNameForBinding(layout.binding) << ", " << layout.offset; |
| 1005 | } |
| 1006 | else |
| 1007 | { |
| 1008 | TString varName = DecorateVariableIfNeeded(variable); |
| 1009 | out << varName << ", " << varName << "_offset"; |
| 1010 | } |
| 1011 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1012 | else |
| 1013 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 1014 | const TType &variableType = variable.getType(); |
| 1015 | TQualifier qualifier = variable.getType().getQualifier(); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1016 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 1017 | ensureStructDefined(variableType); |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 1018 | |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1019 | if (qualifier == EvqUniform) |
| 1020 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 1021 | const TInterfaceBlock *interfaceBlock = variableType.getInterfaceBlock(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1022 | |
| 1023 | if (interfaceBlock) |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1024 | { |
Olli Etuaho | c71862a | 2017-12-21 12:58:29 +0200 | [diff] [blame] | 1025 | if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0) |
| 1026 | { |
| 1027 | const TVariable *instanceVariable = nullptr; |
| 1028 | if (variableType.isInterfaceBlock()) |
| 1029 | { |
| 1030 | instanceVariable = &variable; |
| 1031 | } |
| 1032 | mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] = |
| 1033 | new TReferencedBlock(interfaceBlock, instanceVariable); |
| 1034 | } |
shannonwoods@chromium.org | 4430b0d | 2013-05-30 00:12:34 +0000 | [diff] [blame] | 1035 | } |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1036 | else |
| 1037 | { |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 1038 | mReferencedUniforms[uniqueId.get()] = &variable; |
shannonwoods@chromium.org | 4a643ae | 2013-05-30 00:12:27 +0000 | [diff] [blame] | 1039 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1040 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 1041 | out << DecorateVariableIfNeeded(variable); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1042 | } |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 1043 | else if (qualifier == EvqBuffer) |
| 1044 | { |
| 1045 | UNREACHABLE(); |
| 1046 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1047 | else if (qualifier == EvqAttribute || qualifier == EvqVertexIn) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1048 | { |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 1049 | mReferencedAttributes[uniqueId.get()] = &variable; |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1050 | out << Decorate(name); |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1051 | } |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 1052 | else if (IsVarying(qualifier)) |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1053 | { |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 1054 | mReferencedVaryings[uniqueId.get()] = &variable; |
Olli Etuaho | da41ac6 | 2018-07-19 16:45:32 +0300 | [diff] [blame] | 1055 | out << DecorateVariableIfNeeded(variable); |
| 1056 | if (variable.symbolType() == SymbolType::AngleInternal && name == "ViewID_OVR") |
Martin Radev | 41ac68e | 2017-06-06 12:16:58 +0300 | [diff] [blame] | 1057 | { |
| 1058 | mUsesViewID = true; |
| 1059 | } |
daniel@transgaming.com | 86f7c9d | 2010-04-20 18:52:06 +0000 | [diff] [blame] | 1060 | } |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 1061 | else if (qualifier == EvqFragmentOut) |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 1062 | { |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 1063 | mReferencedOutputVariables[uniqueId.get()] = &variable; |
Jamie Madill | 46131a3 | 2013-06-20 11:55:50 -0400 | [diff] [blame] | 1064 | out << "out_" << name; |
| 1065 | } |
| 1066 | else if (qualifier == EvqFragColor) |
shannon.woods%transgaming.com@gtempaccount.com | e7d4a24 | 2013-04-13 03:38:33 +0000 | [diff] [blame] | 1067 | { |
| 1068 | out << "gl_Color[0]"; |
| 1069 | mUsesFragColor = true; |
| 1070 | } |
| 1071 | else if (qualifier == EvqFragData) |
| 1072 | { |
| 1073 | out << "gl_Color"; |
| 1074 | mUsesFragData = true; |
| 1075 | } |
| 1076 | else if (qualifier == EvqFragCoord) |
| 1077 | { |
| 1078 | mUsesFragCoord = true; |
| 1079 | out << name; |
| 1080 | } |
| 1081 | else if (qualifier == EvqPointCoord) |
| 1082 | { |
| 1083 | mUsesPointCoord = true; |
| 1084 | out << name; |
| 1085 | } |
| 1086 | else if (qualifier == EvqFrontFacing) |
| 1087 | { |
| 1088 | mUsesFrontFacing = true; |
| 1089 | out << name; |
| 1090 | } |
| 1091 | else if (qualifier == EvqPointSize) |
| 1092 | { |
| 1093 | mUsesPointSize = true; |
| 1094 | out << name; |
| 1095 | } |
Gregoire Payen de La Garanderie | b3dced2 | 2015-01-12 14:54:55 +0000 | [diff] [blame] | 1096 | else if (qualifier == EvqInstanceID) |
| 1097 | { |
| 1098 | mUsesInstanceID = true; |
| 1099 | out << name; |
| 1100 | } |
Corentin Wallez | b076add | 2016-01-11 16:45:46 -0500 | [diff] [blame] | 1101 | else if (qualifier == EvqVertexID) |
| 1102 | { |
| 1103 | mUsesVertexID = true; |
| 1104 | out << name; |
| 1105 | } |
Kimmo Kinnunen | 5f0246c | 2015-07-22 10:30:35 +0300 | [diff] [blame] | 1106 | else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth") |
Jamie Madill | 2aeb26a | 2013-07-08 14:02:55 -0400 | [diff] [blame] | 1107 | { |
| 1108 | mUsesFragDepth = true; |
| 1109 | out << "gl_Depth"; |
| 1110 | } |
Xinghua Cao | b123938 | 2016-12-13 15:07:05 +0800 | [diff] [blame] | 1111 | else if (qualifier == EvqNumWorkGroups) |
| 1112 | { |
| 1113 | mUsesNumWorkGroups = true; |
| 1114 | out << name; |
| 1115 | } |
| 1116 | else if (qualifier == EvqWorkGroupID) |
| 1117 | { |
| 1118 | mUsesWorkGroupID = true; |
| 1119 | out << name; |
| 1120 | } |
| 1121 | else if (qualifier == EvqLocalInvocationID) |
| 1122 | { |
| 1123 | mUsesLocalInvocationID = true; |
| 1124 | out << name; |
| 1125 | } |
| 1126 | else if (qualifier == EvqGlobalInvocationID) |
| 1127 | { |
| 1128 | mUsesGlobalInvocationID = true; |
| 1129 | out << name; |
| 1130 | } |
| 1131 | else if (qualifier == EvqLocalInvocationIndex) |
| 1132 | { |
| 1133 | mUsesLocalInvocationIndex = true; |
| 1134 | out << name; |
| 1135 | } |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1136 | else |
| 1137 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 1138 | out << DecorateVariableIfNeeded(variable); |
daniel@transgaming.com | c72c641 | 2011-09-20 16:09:17 +0000 | [diff] [blame] | 1139 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1143 | void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out) |
| 1144 | { |
| 1145 | if (type.isScalar() && !type.isArray()) |
| 1146 | { |
| 1147 | if (op == EOpEqual) |
| 1148 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1149 | outputTriplet(out, visit, "(", " == ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1150 | } |
| 1151 | else |
| 1152 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1153 | outputTriplet(out, visit, "(", " != ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1154 | } |
| 1155 | } |
| 1156 | else |
| 1157 | { |
| 1158 | if (visit == PreVisit && op == EOpNotEqual) |
| 1159 | { |
| 1160 | out << "!"; |
| 1161 | } |
| 1162 | |
| 1163 | if (type.isArray()) |
| 1164 | { |
| 1165 | const TString &functionName = addArrayEqualityFunction(type); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1166 | outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1167 | } |
| 1168 | else if (type.getBasicType() == EbtStruct) |
| 1169 | { |
| 1170 | const TStructure &structure = *type.getStruct(); |
| 1171 | const TString &functionName = addStructEqualityFunction(structure); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1172 | outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1173 | } |
| 1174 | else |
| 1175 | { |
| 1176 | ASSERT(type.isMatrix() || type.isVector()); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1177 | outputTriplet(out, visit, "all(", " == ", ")"); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 1182 | void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out) |
| 1183 | { |
| 1184 | if (type.isArray()) |
| 1185 | { |
| 1186 | const TString &functionName = addArrayAssignmentFunction(type); |
| 1187 | outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")"); |
| 1188 | } |
| 1189 | else |
| 1190 | { |
| 1191 | outputTriplet(out, visit, "(", " = ", ")"); |
| 1192 | } |
| 1193 | } |
| 1194 | |
Olli Etuaho | 1d9dcc2 | 2017-01-19 11:25:32 +0000 | [diff] [blame] | 1195 | bool OutputHLSL::ancestorEvaluatesToSamplerInStruct() |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1196 | { |
Olli Etuaho | 1d9dcc2 | 2017-01-19 11:25:32 +0000 | [diff] [blame] | 1197 | for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n) |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1198 | { |
| 1199 | TIntermNode *ancestor = getAncestorNode(n); |
| 1200 | const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode(); |
| 1201 | if (ancestorBinary == nullptr) |
| 1202 | { |
| 1203 | return false; |
| 1204 | } |
| 1205 | switch (ancestorBinary->getOp()) |
| 1206 | { |
| 1207 | case EOpIndexDirectStruct: |
| 1208 | { |
| 1209 | const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct(); |
| 1210 | const TIntermConstantUnion *index = |
| 1211 | ancestorBinary->getRight()->getAsConstantUnion(); |
| 1212 | const TField *field = structure->fields()[index->getIConst(0)]; |
| 1213 | if (IsSampler(field->type()->getBasicType())) |
| 1214 | { |
| 1215 | return true; |
| 1216 | } |
| 1217 | break; |
| 1218 | } |
| 1219 | case EOpIndexDirect: |
| 1220 | break; |
| 1221 | default: |
| 1222 | // Returning a sampler from indirect indexing is not supported. |
| 1223 | return false; |
| 1224 | } |
| 1225 | } |
| 1226 | return false; |
| 1227 | } |
| 1228 | |
Olli Etuaho | b6fa043 | 2016-09-28 16:28:05 +0100 | [diff] [blame] | 1229 | bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node) |
| 1230 | { |
| 1231 | TInfoSinkBase &out = getInfoSink(); |
| 1232 | if (visit == PostVisit) |
| 1233 | { |
| 1234 | out << "."; |
| 1235 | node->writeOffsetsAsXYZW(&out); |
| 1236 | } |
| 1237 | return true; |
| 1238 | } |
| 1239 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1240 | bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node) |
| 1241 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 1242 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1243 | |
| 1244 | switch (node->getOp()) |
| 1245 | { |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1246 | case EOpComma: |
| 1247 | outputTriplet(out, visit, "(", ", ", ")"); |
| 1248 | break; |
| 1249 | case EOpAssign: |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 1250 | if (node->isArray()) |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 1251 | { |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1252 | TIntermAggregate *rightAgg = node->getRight()->getAsAggregate(); |
| 1253 | if (rightAgg != nullptr && rightAgg->isConstructor()) |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 1254 | { |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1255 | const TString &functionName = addArrayConstructIntoFunction(node->getType()); |
| 1256 | out << functionName << "("; |
| 1257 | node->getLeft()->traverse(this); |
| 1258 | TIntermSequence *seq = rightAgg->getSequence(); |
| 1259 | for (auto &arrayElement : *seq) |
| 1260 | { |
| 1261 | out << ", "; |
| 1262 | arrayElement->traverse(this); |
| 1263 | } |
| 1264 | out << ")"; |
| 1265 | return false; |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 1266 | } |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1267 | // ArrayReturnValueToOutParameter should have eliminated expressions where a |
| 1268 | // function call is assigned. |
Olli Etuaho | 1ecd14b | 2017-01-26 13:54:15 -0800 | [diff] [blame] | 1269 | ASSERT(rightAgg == nullptr); |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 1270 | } |
Jiawei Shao | a6a7842 | 2018-06-28 08:32:54 +0800 | [diff] [blame] | 1271 | // Assignment expressions with atomic functions should be transformed into atomic |
| 1272 | // function calls in HLSL. |
| 1273 | // e.g. original_value = atomicAdd(dest, value) should be translated into |
| 1274 | // InterlockedAdd(dest, value, original_value); |
| 1275 | else if (IsAtomicFunctionDirectAssign(*node)) |
| 1276 | { |
| 1277 | TIntermAggregate *atomicFunctionNode = node->getRight()->getAsAggregate(); |
| 1278 | TOperator atomicFunctionOp = atomicFunctionNode->getOp(); |
| 1279 | out << GetHLSLAtomicFunctionStringAndLeftParenthesis(atomicFunctionOp); |
| 1280 | TIntermSequence *argumentSeq = atomicFunctionNode->getSequence(); |
| 1281 | ASSERT(argumentSeq->size() >= 2u); |
| 1282 | for (auto &argument : *argumentSeq) |
| 1283 | { |
| 1284 | argument->traverse(this); |
| 1285 | out << ", "; |
| 1286 | } |
| 1287 | node->getLeft()->traverse(this); |
| 1288 | out << ")"; |
| 1289 | return false; |
| 1290 | } |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 1291 | else if (IsInShaderStorageBlock(node->getLeft())) |
| 1292 | { |
| 1293 | mSSBOOutputHLSL->outputStoreFunctionCallPrefix(node->getLeft()); |
| 1294 | out << ", "; |
| 1295 | if (IsInShaderStorageBlock(node->getRight())) |
| 1296 | { |
| 1297 | mSSBOOutputHLSL->outputLoadFunctionCall(node->getRight()); |
| 1298 | } |
| 1299 | else |
| 1300 | { |
| 1301 | node->getRight()->traverse(this); |
| 1302 | } |
| 1303 | |
| 1304 | out << ")"; |
| 1305 | return false; |
| 1306 | } |
| 1307 | else if (IsInShaderStorageBlock(node->getRight())) |
| 1308 | { |
| 1309 | node->getLeft()->traverse(this); |
| 1310 | out << " = "; |
| 1311 | mSSBOOutputHLSL->outputLoadFunctionCall(node->getRight()); |
| 1312 | return false; |
| 1313 | } |
Jiawei Shao | a6a7842 | 2018-06-28 08:32:54 +0800 | [diff] [blame] | 1314 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 1315 | outputAssign(visit, node->getType(), out); |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1316 | break; |
| 1317 | case EOpInitialize: |
| 1318 | if (visit == PreVisit) |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 1319 | { |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1320 | TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode(); |
| 1321 | ASSERT(symbolNode); |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 1322 | TIntermTyped *initializer = node->getRight(); |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1323 | |
| 1324 | // Global initializers must be constant at this point. |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 1325 | ASSERT(symbolNode->getQualifier() != EvqGlobal || initializer->hasConstantValue()); |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1326 | |
| 1327 | // GLSL allows to write things like "float x = x;" where a new variable x is defined |
| 1328 | // and the value of an existing variable x is assigned. HLSL uses C semantics (the |
| 1329 | // new variable is created before the assignment is evaluated), so we need to |
| 1330 | // convert |
| 1331 | // this to "float t = x, x = t;". |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 1332 | if (writeSameSymbolInitializer(out, symbolNode, initializer)) |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1333 | { |
| 1334 | // Skip initializing the rest of the expression |
| 1335 | return false; |
| 1336 | } |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 1337 | else if (writeConstantInitialization(out, symbolNode, initializer)) |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1338 | { |
| 1339 | return false; |
| 1340 | } |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 1341 | } |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1342 | else if (visit == InVisit) |
| 1343 | { |
| 1344 | out << " = "; |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 1345 | if (IsInShaderStorageBlock(node->getRight())) |
| 1346 | { |
| 1347 | mSSBOOutputHLSL->outputLoadFunctionCall(node->getRight()); |
| 1348 | return false; |
| 1349 | } |
Olli Etuaho | 4db7ded | 2016-10-13 12:23:11 +0100 | [diff] [blame] | 1350 | } |
| 1351 | break; |
| 1352 | case EOpAddAssign: |
| 1353 | outputTriplet(out, visit, "(", " += ", ")"); |
| 1354 | break; |
| 1355 | case EOpSubAssign: |
| 1356 | outputTriplet(out, visit, "(", " -= ", ")"); |
| 1357 | break; |
| 1358 | case EOpMulAssign: |
| 1359 | outputTriplet(out, visit, "(", " *= ", ")"); |
| 1360 | break; |
| 1361 | case EOpVectorTimesScalarAssign: |
| 1362 | outputTriplet(out, visit, "(", " *= ", ")"); |
| 1363 | break; |
| 1364 | case EOpMatrixTimesScalarAssign: |
| 1365 | outputTriplet(out, visit, "(", " *= ", ")"); |
| 1366 | break; |
| 1367 | case EOpVectorTimesMatrixAssign: |
| 1368 | if (visit == PreVisit) |
| 1369 | { |
| 1370 | out << "("; |
| 1371 | } |
| 1372 | else if (visit == InVisit) |
| 1373 | { |
| 1374 | out << " = mul("; |
| 1375 | node->getLeft()->traverse(this); |
| 1376 | out << ", transpose("; |
| 1377 | } |
| 1378 | else |
| 1379 | { |
| 1380 | out << ")))"; |
| 1381 | } |
| 1382 | break; |
| 1383 | case EOpMatrixTimesMatrixAssign: |
| 1384 | if (visit == PreVisit) |
| 1385 | { |
| 1386 | out << "("; |
| 1387 | } |
| 1388 | else if (visit == InVisit) |
| 1389 | { |
| 1390 | out << " = transpose(mul(transpose("; |
| 1391 | node->getLeft()->traverse(this); |
| 1392 | out << "), transpose("; |
| 1393 | } |
| 1394 | else |
| 1395 | { |
| 1396 | out << "))))"; |
| 1397 | } |
| 1398 | break; |
| 1399 | case EOpDivAssign: |
| 1400 | outputTriplet(out, visit, "(", " /= ", ")"); |
| 1401 | break; |
| 1402 | case EOpIModAssign: |
| 1403 | outputTriplet(out, visit, "(", " %= ", ")"); |
| 1404 | break; |
| 1405 | case EOpBitShiftLeftAssign: |
| 1406 | outputTriplet(out, visit, "(", " <<= ", ")"); |
| 1407 | break; |
| 1408 | case EOpBitShiftRightAssign: |
| 1409 | outputTriplet(out, visit, "(", " >>= ", ")"); |
| 1410 | break; |
| 1411 | case EOpBitwiseAndAssign: |
| 1412 | outputTriplet(out, visit, "(", " &= ", ")"); |
| 1413 | break; |
| 1414 | case EOpBitwiseXorAssign: |
| 1415 | outputTriplet(out, visit, "(", " ^= ", ")"); |
| 1416 | break; |
| 1417 | case EOpBitwiseOrAssign: |
| 1418 | outputTriplet(out, visit, "(", " |= ", ")"); |
| 1419 | break; |
| 1420 | case EOpIndexDirect: |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1421 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1422 | const TType &leftType = node->getLeft()->getType(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1423 | if (leftType.isInterfaceBlock()) |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1424 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1425 | if (visit == PreVisit) |
| 1426 | { |
Jiawei Shao | a75aa3b | 2018-06-21 10:38:28 +0800 | [diff] [blame] | 1427 | TIntermSymbol *instanceArraySymbol = node->getLeft()->getAsSymbolNode(); |
Olli Etuaho | dd21ecf | 2018-01-10 12:42:09 +0200 | [diff] [blame] | 1428 | const TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock(); |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 1429 | |
| 1430 | ASSERT(leftType.getQualifier() == EvqUniform); |
Olli Etuaho | c71862a | 2017-12-21 12:58:29 +0200 | [diff] [blame] | 1431 | if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0) |
| 1432 | { |
| 1433 | mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] = |
| 1434 | new TReferencedBlock(interfaceBlock, &instanceArraySymbol->variable()); |
| 1435 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1436 | const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0); |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 1437 | out << mResourcesHLSL->InterfaceBlockInstanceString( |
Qin Jiajia | 3e217f6 | 2018-08-28 16:55:20 +0800 | [diff] [blame] | 1438 | instanceArraySymbol->getName(), arrayIndex); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1439 | return false; |
| 1440 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1441 | } |
Olli Etuaho | 1d9dcc2 | 2017-01-19 11:25:32 +0000 | [diff] [blame] | 1442 | else if (ancestorEvaluatesToSamplerInStruct()) |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1443 | { |
| 1444 | // All parts of an expression that access a sampler in a struct need to use _ as |
| 1445 | // separator to access the sampler variable that has been moved out of the struct. |
| 1446 | outputTriplet(out, visit, "", "_", ""); |
| 1447 | } |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 1448 | else if (IsAtomicCounter(leftType.getBasicType())) |
| 1449 | { |
| 1450 | outputTriplet(out, visit, "", " + (", ") * ATOMIC_COUNTER_ARRAY_STRIDE"); |
| 1451 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1452 | else |
| 1453 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1454 | outputTriplet(out, visit, "", "[", "]"); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1455 | } |
Jamie Madill | b4e664b | 2013-06-20 11:55:54 -0400 | [diff] [blame] | 1456 | } |
| 1457 | break; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1458 | case EOpIndexIndirect: |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 1459 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1460 | // We do not currently support indirect references to interface blocks |
| 1461 | ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock); |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 1462 | |
| 1463 | const TType &leftType = node->getLeft()->getType(); |
| 1464 | if (IsAtomicCounter(leftType.getBasicType())) |
| 1465 | { |
| 1466 | outputTriplet(out, visit, "", " + (", ") * ATOMIC_COUNTER_ARRAY_STRIDE"); |
| 1467 | } |
| 1468 | else |
| 1469 | { |
| 1470 | outputTriplet(out, visit, "", "[", "]"); |
| 1471 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1472 | break; |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 1473 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1474 | case EOpIndexDirectStruct: |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1475 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1476 | const TStructure *structure = node->getLeft()->getType().getStruct(); |
| 1477 | const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion(); |
| 1478 | const TField *field = structure->fields()[index->getIConst(0)]; |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1479 | |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1480 | // In cases where indexing returns a sampler, we need to access the sampler variable |
| 1481 | // that has been moved out of the struct. |
| 1482 | bool indexingReturnsSampler = IsSampler(field->type()->getBasicType()); |
| 1483 | if (visit == PreVisit && indexingReturnsSampler) |
| 1484 | { |
| 1485 | // Samplers extracted from structs have "angle" prefix to avoid name conflicts. |
| 1486 | // This prefix is only output at the beginning of the indexing expression, which |
| 1487 | // may have multiple parts. |
| 1488 | out << "angle"; |
| 1489 | } |
| 1490 | if (!indexingReturnsSampler) |
| 1491 | { |
| 1492 | // All parts of an expression that access a sampler in a struct need to use _ as |
| 1493 | // separator to access the sampler variable that has been moved out of the struct. |
Olli Etuaho | 1d9dcc2 | 2017-01-19 11:25:32 +0000 | [diff] [blame] | 1494 | indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1495 | } |
| 1496 | if (visit == InVisit) |
| 1497 | { |
| 1498 | if (indexingReturnsSampler) |
| 1499 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 1500 | out << "_" << field->name(); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1501 | } |
| 1502 | else |
| 1503 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 1504 | out << "." << DecorateField(field->name(), *structure); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1505 | } |
| 1506 | |
| 1507 | return false; |
| 1508 | } |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 1509 | } |
| 1510 | break; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1511 | case EOpIndexDirectInterfaceBlock: |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 1512 | { |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 1513 | ASSERT(!IsInShaderStorageBlock(node->getLeft())); |
| 1514 | bool structInStd140UniformBlock = |
| 1515 | node->getBasicType() == EbtStruct && IsInStd140UniformBlock(node->getLeft()); |
| 1516 | if (visit == PreVisit && structInStd140UniformBlock) |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 1517 | { |
| 1518 | out << "map"; |
| 1519 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1520 | if (visit == InVisit) |
| 1521 | { |
| 1522 | const TInterfaceBlock *interfaceBlock = |
| 1523 | node->getLeft()->getType().getInterfaceBlock(); |
| 1524 | const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion(); |
| 1525 | const TField *field = interfaceBlock->fields()[index->getIConst(0)]; |
Qin Jiajia | a735ee2 | 2018-05-18 13:29:09 +0800 | [diff] [blame] | 1526 | if (structInStd140UniformBlock) |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 1527 | { |
| 1528 | out << "_"; |
| 1529 | } |
| 1530 | else |
| 1531 | { |
| 1532 | out << "."; |
| 1533 | } |
| 1534 | out << Decorate(field->name()); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 1535 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1536 | return false; |
| 1537 | } |
| 1538 | break; |
Olli Etuaho | 2ef23e2 | 2017-11-01 16:39:11 +0200 | [diff] [blame] | 1539 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1540 | case EOpAdd: |
| 1541 | outputTriplet(out, visit, "(", " + ", ")"); |
| 1542 | break; |
| 1543 | case EOpSub: |
| 1544 | outputTriplet(out, visit, "(", " - ", ")"); |
| 1545 | break; |
| 1546 | case EOpMul: |
| 1547 | outputTriplet(out, visit, "(", " * ", ")"); |
| 1548 | break; |
| 1549 | case EOpDiv: |
| 1550 | outputTriplet(out, visit, "(", " / ", ")"); |
| 1551 | break; |
| 1552 | case EOpIMod: |
| 1553 | outputTriplet(out, visit, "(", " % ", ")"); |
| 1554 | break; |
| 1555 | case EOpBitShiftLeft: |
| 1556 | outputTriplet(out, visit, "(", " << ", ")"); |
| 1557 | break; |
| 1558 | case EOpBitShiftRight: |
| 1559 | outputTriplet(out, visit, "(", " >> ", ")"); |
| 1560 | break; |
| 1561 | case EOpBitwiseAnd: |
| 1562 | outputTriplet(out, visit, "(", " & ", ")"); |
| 1563 | break; |
| 1564 | case EOpBitwiseXor: |
| 1565 | outputTriplet(out, visit, "(", " ^ ", ")"); |
| 1566 | break; |
| 1567 | case EOpBitwiseOr: |
| 1568 | outputTriplet(out, visit, "(", " | ", ")"); |
| 1569 | break; |
| 1570 | case EOpEqual: |
| 1571 | case EOpNotEqual: |
| 1572 | outputEqual(visit, node->getLeft()->getType(), node->getOp(), out); |
| 1573 | break; |
| 1574 | case EOpLessThan: |
| 1575 | outputTriplet(out, visit, "(", " < ", ")"); |
| 1576 | break; |
| 1577 | case EOpGreaterThan: |
| 1578 | outputTriplet(out, visit, "(", " > ", ")"); |
| 1579 | break; |
| 1580 | case EOpLessThanEqual: |
| 1581 | outputTriplet(out, visit, "(", " <= ", ")"); |
| 1582 | break; |
| 1583 | case EOpGreaterThanEqual: |
| 1584 | outputTriplet(out, visit, "(", " >= ", ")"); |
| 1585 | break; |
| 1586 | case EOpVectorTimesScalar: |
| 1587 | outputTriplet(out, visit, "(", " * ", ")"); |
| 1588 | break; |
| 1589 | case EOpMatrixTimesScalar: |
| 1590 | outputTriplet(out, visit, "(", " * ", ")"); |
| 1591 | break; |
| 1592 | case EOpVectorTimesMatrix: |
| 1593 | outputTriplet(out, visit, "mul(", ", transpose(", "))"); |
| 1594 | break; |
| 1595 | case EOpMatrixTimesVector: |
| 1596 | outputTriplet(out, visit, "mul(transpose(", "), ", ")"); |
| 1597 | break; |
| 1598 | case EOpMatrixTimesMatrix: |
| 1599 | outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))"); |
| 1600 | break; |
| 1601 | case EOpLogicalOr: |
| 1602 | // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have |
| 1603 | // been unfolded. |
| 1604 | ASSERT(!node->getRight()->hasSideEffects()); |
| 1605 | outputTriplet(out, visit, "(", " || ", ")"); |
| 1606 | return true; |
| 1607 | case EOpLogicalXor: |
| 1608 | mUsesXor = true; |
| 1609 | outputTriplet(out, visit, "xor(", ", ", ")"); |
| 1610 | break; |
| 1611 | case EOpLogicalAnd: |
| 1612 | // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have |
| 1613 | // been unfolded. |
| 1614 | ASSERT(!node->getRight()->hasSideEffects()); |
| 1615 | outputTriplet(out, visit, "(", " && ", ")"); |
| 1616 | return true; |
| 1617 | default: |
| 1618 | UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
| 1624 | bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node) |
| 1625 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1626 | TInfoSinkBase &out = getInfoSink(); |
| 1627 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1628 | switch (node->getOp()) |
| 1629 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1630 | case EOpNegative: |
| 1631 | outputTriplet(out, visit, "(-", "", ")"); |
| 1632 | break; |
| 1633 | case EOpPositive: |
| 1634 | outputTriplet(out, visit, "(+", "", ")"); |
| 1635 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 1636 | case EOpLogicalNot: |
| 1637 | outputTriplet(out, visit, "(!", "", ")"); |
| 1638 | break; |
| 1639 | case EOpBitwiseNot: |
| 1640 | outputTriplet(out, visit, "(~", "", ")"); |
| 1641 | break; |
| 1642 | case EOpPostIncrement: |
| 1643 | outputTriplet(out, visit, "(", "", "++)"); |
| 1644 | break; |
| 1645 | case EOpPostDecrement: |
| 1646 | outputTriplet(out, visit, "(", "", "--)"); |
| 1647 | break; |
| 1648 | case EOpPreIncrement: |
| 1649 | outputTriplet(out, visit, "(++", "", ")"); |
| 1650 | break; |
| 1651 | case EOpPreDecrement: |
| 1652 | outputTriplet(out, visit, "(--", "", ")"); |
| 1653 | break; |
| 1654 | case EOpRadians: |
| 1655 | outputTriplet(out, visit, "radians(", "", ")"); |
| 1656 | break; |
| 1657 | case EOpDegrees: |
| 1658 | outputTriplet(out, visit, "degrees(", "", ")"); |
| 1659 | break; |
| 1660 | case EOpSin: |
| 1661 | outputTriplet(out, visit, "sin(", "", ")"); |
| 1662 | break; |
| 1663 | case EOpCos: |
| 1664 | outputTriplet(out, visit, "cos(", "", ")"); |
| 1665 | break; |
| 1666 | case EOpTan: |
| 1667 | outputTriplet(out, visit, "tan(", "", ")"); |
| 1668 | break; |
| 1669 | case EOpAsin: |
| 1670 | outputTriplet(out, visit, "asin(", "", ")"); |
| 1671 | break; |
| 1672 | case EOpAcos: |
| 1673 | outputTriplet(out, visit, "acos(", "", ")"); |
| 1674 | break; |
| 1675 | case EOpAtan: |
| 1676 | outputTriplet(out, visit, "atan(", "", ")"); |
| 1677 | break; |
| 1678 | case EOpSinh: |
| 1679 | outputTriplet(out, visit, "sinh(", "", ")"); |
| 1680 | break; |
| 1681 | case EOpCosh: |
| 1682 | outputTriplet(out, visit, "cosh(", "", ")"); |
| 1683 | break; |
| 1684 | case EOpTanh: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1685 | case EOpAsinh: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1686 | case EOpAcosh: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1687 | case EOpAtanh: |
| 1688 | ASSERT(node->getUseEmulatedFunction()); |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 1689 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1690 | break; |
| 1691 | case EOpExp: |
| 1692 | outputTriplet(out, visit, "exp(", "", ")"); |
| 1693 | break; |
| 1694 | case EOpLog: |
| 1695 | outputTriplet(out, visit, "log(", "", ")"); |
| 1696 | break; |
| 1697 | case EOpExp2: |
| 1698 | outputTriplet(out, visit, "exp2(", "", ")"); |
| 1699 | break; |
| 1700 | case EOpLog2: |
| 1701 | outputTriplet(out, visit, "log2(", "", ")"); |
| 1702 | break; |
| 1703 | case EOpSqrt: |
| 1704 | outputTriplet(out, visit, "sqrt(", "", ")"); |
| 1705 | break; |
Olli Etuaho | f7f0b8c | 2018-02-21 20:02:23 +0200 | [diff] [blame] | 1706 | case EOpInversesqrt: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1707 | outputTriplet(out, visit, "rsqrt(", "", ")"); |
| 1708 | break; |
| 1709 | case EOpAbs: |
| 1710 | outputTriplet(out, visit, "abs(", "", ")"); |
| 1711 | break; |
| 1712 | case EOpSign: |
| 1713 | outputTriplet(out, visit, "sign(", "", ")"); |
| 1714 | break; |
| 1715 | case EOpFloor: |
| 1716 | outputTriplet(out, visit, "floor(", "", ")"); |
| 1717 | break; |
| 1718 | case EOpTrunc: |
| 1719 | outputTriplet(out, visit, "trunc(", "", ")"); |
| 1720 | break; |
| 1721 | case EOpRound: |
| 1722 | outputTriplet(out, visit, "round(", "", ")"); |
| 1723 | break; |
| 1724 | case EOpRoundEven: |
| 1725 | ASSERT(node->getUseEmulatedFunction()); |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 1726 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1727 | break; |
| 1728 | case EOpCeil: |
| 1729 | outputTriplet(out, visit, "ceil(", "", ")"); |
| 1730 | break; |
| 1731 | case EOpFract: |
| 1732 | outputTriplet(out, visit, "frac(", "", ")"); |
| 1733 | break; |
Olli Etuaho | f7f0b8c | 2018-02-21 20:02:23 +0200 | [diff] [blame] | 1734 | case EOpIsnan: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1735 | if (node->getUseEmulatedFunction()) |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 1736 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1737 | else |
| 1738 | outputTriplet(out, visit, "isnan(", "", ")"); |
| 1739 | mRequiresIEEEStrictCompiling = true; |
| 1740 | break; |
Olli Etuaho | f7f0b8c | 2018-02-21 20:02:23 +0200 | [diff] [blame] | 1741 | case EOpIsinf: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1742 | outputTriplet(out, visit, "isinf(", "", ")"); |
| 1743 | break; |
| 1744 | case EOpFloatBitsToInt: |
| 1745 | outputTriplet(out, visit, "asint(", "", ")"); |
| 1746 | break; |
| 1747 | case EOpFloatBitsToUint: |
| 1748 | outputTriplet(out, visit, "asuint(", "", ")"); |
| 1749 | break; |
| 1750 | case EOpIntBitsToFloat: |
| 1751 | outputTriplet(out, visit, "asfloat(", "", ")"); |
| 1752 | break; |
| 1753 | case EOpUintBitsToFloat: |
| 1754 | outputTriplet(out, visit, "asfloat(", "", ")"); |
| 1755 | break; |
| 1756 | case EOpPackSnorm2x16: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1757 | case EOpPackUnorm2x16: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1758 | case EOpPackHalf2x16: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1759 | case EOpUnpackSnorm2x16: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1760 | case EOpUnpackUnorm2x16: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1761 | case EOpUnpackHalf2x16: |
Olli Etuaho | 25aef45 | 2017-01-29 16:15:44 -0800 | [diff] [blame] | 1762 | case EOpPackUnorm4x8: |
| 1763 | case EOpPackSnorm4x8: |
| 1764 | case EOpUnpackUnorm4x8: |
| 1765 | case EOpUnpackSnorm4x8: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1766 | ASSERT(node->getUseEmulatedFunction()); |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 1767 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1768 | break; |
| 1769 | case EOpLength: |
| 1770 | outputTriplet(out, visit, "length(", "", ")"); |
| 1771 | break; |
| 1772 | case EOpNormalize: |
| 1773 | outputTriplet(out, visit, "normalize(", "", ")"); |
| 1774 | break; |
| 1775 | case EOpDFdx: |
| 1776 | if (mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1777 | { |
| 1778 | outputTriplet(out, visit, "(", "", ", 0.0)"); |
| 1779 | } |
| 1780 | else |
| 1781 | { |
| 1782 | outputTriplet(out, visit, "ddx(", "", ")"); |
| 1783 | } |
| 1784 | break; |
| 1785 | case EOpDFdy: |
| 1786 | if (mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1787 | { |
| 1788 | outputTriplet(out, visit, "(", "", ", 0.0)"); |
| 1789 | } |
| 1790 | else |
| 1791 | { |
| 1792 | outputTriplet(out, visit, "ddy(", "", ")"); |
| 1793 | } |
| 1794 | break; |
| 1795 | case EOpFwidth: |
| 1796 | if (mInsideDiscontinuousLoop || mOutputLod0Function) |
| 1797 | { |
| 1798 | outputTriplet(out, visit, "(", "", ", 0.0)"); |
| 1799 | } |
| 1800 | else |
| 1801 | { |
| 1802 | outputTriplet(out, visit, "fwidth(", "", ")"); |
| 1803 | } |
| 1804 | break; |
| 1805 | case EOpTranspose: |
| 1806 | outputTriplet(out, visit, "transpose(", "", ")"); |
| 1807 | break; |
| 1808 | case EOpDeterminant: |
| 1809 | outputTriplet(out, visit, "determinant(transpose(", "", "))"); |
| 1810 | break; |
| 1811 | case EOpInverse: |
| 1812 | ASSERT(node->getUseEmulatedFunction()); |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 1813 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1814 | break; |
Olli Etuaho | e39706d | 2014-12-30 16:40:36 +0200 | [diff] [blame] | 1815 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1816 | case EOpAny: |
| 1817 | outputTriplet(out, visit, "any(", "", ")"); |
| 1818 | break; |
| 1819 | case EOpAll: |
| 1820 | outputTriplet(out, visit, "all(", "", ")"); |
| 1821 | break; |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 1822 | case EOpLogicalNotComponentWise: |
| 1823 | outputTriplet(out, visit, "(!", "", ")"); |
| 1824 | break; |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 1825 | case EOpBitfieldReverse: |
| 1826 | outputTriplet(out, visit, "reversebits(", "", ")"); |
| 1827 | break; |
| 1828 | case EOpBitCount: |
| 1829 | outputTriplet(out, visit, "countbits(", "", ")"); |
| 1830 | break; |
| 1831 | case EOpFindLSB: |
| 1832 | // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested |
| 1833 | // in GLSLTest and results are consistent with GL. |
| 1834 | outputTriplet(out, visit, "firstbitlow(", "", ")"); |
| 1835 | break; |
| 1836 | case EOpFindMSB: |
| 1837 | // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is |
| 1838 | // tested in GLSLTest and results are consistent with GL. |
| 1839 | outputTriplet(out, visit, "firstbithigh(", "", ")"); |
| 1840 | break; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 1841 | default: |
| 1842 | UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | return true; |
| 1846 | } |
| 1847 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 1848 | ImmutableString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node) |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1849 | { |
| 1850 | if (node->getAsSymbolNode()) |
| 1851 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 1852 | ASSERT(node->getAsSymbolNode()->variable().symbolType() != SymbolType::Empty); |
| 1853 | return node->getAsSymbolNode()->getName(); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1854 | } |
| 1855 | TIntermBinary *nodeBinary = node->getAsBinaryNode(); |
| 1856 | switch (nodeBinary->getOp()) |
| 1857 | { |
| 1858 | case EOpIndexDirect: |
| 1859 | { |
| 1860 | int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0); |
| 1861 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 1862 | std::stringstream prefixSink; |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1863 | prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index; |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 1864 | return ImmutableString(prefixSink.str()); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1865 | } |
| 1866 | case EOpIndexDirectStruct: |
| 1867 | { |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 1868 | const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct(); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1869 | int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0); |
| 1870 | const TField *field = s->fields()[index]; |
| 1871 | |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 1872 | std::stringstream prefixSink; |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1873 | prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" |
| 1874 | << field->name(); |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 1875 | return ImmutableString(prefixSink.str()); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1876 | } |
| 1877 | default: |
| 1878 | UNREACHABLE(); |
Jamie Madill | b779b12 | 2018-06-20 11:46:43 -0400 | [diff] [blame] | 1879 | return kEmptyImmutableString; |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 1880 | } |
| 1881 | } |
| 1882 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1883 | bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node) |
| 1884 | { |
| 1885 | TInfoSinkBase &out = getInfoSink(); |
| 1886 | |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 1887 | bool isMainBlock = mInsideMain && getParentNode()->getAsFunctionDefinition(); |
| 1888 | |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1889 | if (mInsideFunction) |
| 1890 | { |
| 1891 | outputLineDirective(out, node->getLine().first_line); |
| 1892 | out << "{\n"; |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 1893 | if (isMainBlock) |
| 1894 | { |
Jiawei Shao | 203b26f | 2018-07-25 10:30:43 +0800 | [diff] [blame] | 1895 | if (mShaderType == GL_COMPUTE_SHADER) |
| 1896 | { |
| 1897 | out << "initGLBuiltins(input);\n"; |
| 1898 | } |
| 1899 | else |
| 1900 | { |
| 1901 | out << "@@ MAIN PROLOGUE @@\n"; |
| 1902 | } |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 1903 | } |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1904 | } |
| 1905 | |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 1906 | for (TIntermNode *statement : *node->getSequence()) |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1907 | { |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 1908 | outputLineDirective(out, statement->getLine().first_line); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1909 | |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 1910 | statement->traverse(this); |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1911 | |
| 1912 | // Don't output ; after case labels, they're terminated by : |
| 1913 | // This is needed especially since outputting a ; after a case statement would turn empty |
| 1914 | // case statements into non-empty case statements, disallowing fall-through from them. |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 1915 | // Also the output code is clearer if we don't output ; after statements where it is not |
| 1916 | // needed: |
| 1917 | // * if statements |
| 1918 | // * switch statements |
| 1919 | // * blocks |
| 1920 | // * function definitions |
| 1921 | // * loops (do-while loops output the semicolon in VisitLoop) |
| 1922 | // * declarations that don't generate output. |
| 1923 | if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr && |
| 1924 | statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr && |
| 1925 | statement->getAsSwitchNode() == nullptr && |
| 1926 | statement->getAsFunctionDefinition() == nullptr && |
| 1927 | (statement->getAsDeclarationNode() == nullptr || |
| 1928 | IsDeclarationWrittenOut(statement->getAsDeclarationNode())) && |
| 1929 | statement->getAsInvariantDeclarationNode() == nullptr) |
| 1930 | { |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1931 | out << ";\n"; |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 1932 | } |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | if (mInsideFunction) |
| 1936 | { |
| 1937 | outputLineDirective(out, node->getLine().last_line); |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 1938 | if (isMainBlock && shaderNeedsGenerateOutput()) |
| 1939 | { |
| 1940 | // We could have an empty main, a main function without a branch at the end, or a main |
| 1941 | // function with a discard statement at the end. In these cases we need to add a return |
| 1942 | // statement. |
| 1943 | bool needReturnStatement = |
| 1944 | node->getSequence()->empty() || !node->getSequence()->back()->getAsBranchNode() || |
| 1945 | node->getSequence()->back()->getAsBranchNode()->getFlowOp() != EOpReturn; |
| 1946 | if (needReturnStatement) |
| 1947 | { |
| 1948 | out << "return " << generateOutputCall() << ";\n"; |
| 1949 | } |
| 1950 | } |
Olli Etuaho | 6d40bbd | 2016-09-30 13:49:38 +0100 | [diff] [blame] | 1951 | out << "}\n"; |
| 1952 | } |
| 1953 | |
| 1954 | return false; |
| 1955 | } |
| 1956 | |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 1957 | bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node) |
| 1958 | { |
| 1959 | TInfoSinkBase &out = getInfoSink(); |
| 1960 | |
| 1961 | ASSERT(mCurrentFunctionMetadata == nullptr); |
| 1962 | |
Olli Etuaho | beb6dc7 | 2017-12-14 16:03:03 +0200 | [diff] [blame] | 1963 | size_t index = mCallDag.findIndex(node->getFunction()->uniqueId()); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 1964 | ASSERT(index != CallDAG::InvalidIndex); |
| 1965 | mCurrentFunctionMetadata = &mASTMetadataList[index]; |
| 1966 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 1967 | const TFunction *func = node->getFunction(); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 1968 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 1969 | if (func->isMain()) |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 1970 | { |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 1971 | // The stub strings below are replaced when shader is dynamically defined by its layout: |
| 1972 | switch (mShaderType) |
| 1973 | { |
| 1974 | case GL_VERTEX_SHADER: |
| 1975 | out << "@@ VERTEX ATTRIBUTES @@\n\n" |
| 1976 | << "@@ VERTEX OUTPUT @@\n\n" |
| 1977 | << "VS_OUTPUT main(VS_INPUT input)"; |
| 1978 | break; |
| 1979 | case GL_FRAGMENT_SHADER: |
| 1980 | out << "@@ PIXEL OUTPUT @@\n\n" |
| 1981 | << "PS_OUTPUT main(@@ PIXEL MAIN PARAMETERS @@)"; |
| 1982 | break; |
| 1983 | case GL_COMPUTE_SHADER: |
| 1984 | out << "[numthreads(" << mWorkGroupSize[0] << ", " << mWorkGroupSize[1] << ", " |
| 1985 | << mWorkGroupSize[2] << ")]\n"; |
| 1986 | out << "void main(CS_INPUT input)"; |
| 1987 | break; |
| 1988 | default: |
| 1989 | UNREACHABLE(); |
| 1990 | break; |
| 1991 | } |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 1992 | } |
| 1993 | else |
| 1994 | { |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 1995 | out << TypeString(node->getFunctionPrototype()->getType()) << " "; |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 1996 | out << DecorateFunctionIfNeeded(func) << DisambiguateFunctionName(func) |
Olli Etuaho | beb6dc7 | 2017-12-14 16:03:03 +0200 | [diff] [blame] | 1997 | << (mOutputLod0Function ? "Lod0(" : "("); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 1998 | |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 1999 | size_t paramCount = func->getParamCount(); |
| 2000 | for (unsigned int i = 0; i < paramCount; i++) |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 2001 | { |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 2002 | const TVariable *param = func->getParam(i); |
| 2003 | ensureStructDefined(param->getType()); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 2004 | |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 2005 | writeParameter(param, out); |
| 2006 | |
| 2007 | if (i < paramCount - 1) |
| 2008 | { |
| 2009 | out << ", "; |
| 2010 | } |
| 2011 | } |
| 2012 | |
| 2013 | out << ")\n"; |
| 2014 | } |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 2015 | |
| 2016 | mInsideFunction = true; |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 2017 | if (func->isMain()) |
| 2018 | { |
| 2019 | mInsideMain = true; |
| 2020 | } |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 2021 | // The function body node will output braces. |
| 2022 | node->getBody()->traverse(this); |
| 2023 | mInsideFunction = false; |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 2024 | mInsideMain = false; |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 2025 | |
| 2026 | mCurrentFunctionMetadata = nullptr; |
| 2027 | |
| 2028 | bool needsLod0 = mASTMetadataList[index].mNeedsLod0; |
| 2029 | if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER) |
| 2030 | { |
Olli Etuaho | beb6dc7 | 2017-12-14 16:03:03 +0200 | [diff] [blame] | 2031 | ASSERT(!node->getFunction()->isMain()); |
Olli Etuaho | 336b147 | 2016-10-05 16:37:55 +0100 | [diff] [blame] | 2032 | mOutputLod0Function = true; |
| 2033 | node->traverse(this); |
| 2034 | mOutputLod0Function = false; |
| 2035 | } |
| 2036 | |
| 2037 | return false; |
| 2038 | } |
| 2039 | |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2040 | bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node) |
| 2041 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2042 | if (visit == PreVisit) |
| 2043 | { |
| 2044 | TIntermSequence *sequence = node->getSequence(); |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 2045 | TIntermTyped *declarator = (*sequence)[0]->getAsTyped(); |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2046 | ASSERT(sequence->size() == 1); |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 2047 | ASSERT(declarator); |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2048 | |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 2049 | if (IsDeclarationWrittenOut(node)) |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2050 | { |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 2051 | TInfoSinkBase &out = getInfoSink(); |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 2052 | ensureStructDefined(declarator->getType()); |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2053 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 2054 | if (!declarator->getAsSymbolNode() || |
| 2055 | declarator->getAsSymbolNode()->variable().symbolType() != |
| 2056 | SymbolType::Empty) // Variable declaration |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2057 | { |
Jiawei Shao | a75aa3b | 2018-06-21 10:38:28 +0800 | [diff] [blame] | 2058 | if (declarator->getQualifier() == EvqShared) |
| 2059 | { |
| 2060 | out << "groupshared "; |
| 2061 | } |
| 2062 | else if (!mInsideFunction) |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2063 | { |
| 2064 | out << "static "; |
| 2065 | } |
| 2066 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 2067 | out << TypeString(declarator->getType()) + " "; |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2068 | |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 2069 | TIntermSymbol *symbol = declarator->getAsSymbolNode(); |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2070 | |
| 2071 | if (symbol) |
| 2072 | { |
| 2073 | symbol->traverse(this); |
| 2074 | out << ArrayString(symbol->getType()); |
jchen10 | cd47a37 | 2018-11-11 11:08:16 +0800 | [diff] [blame] | 2075 | if (declarator->getQualifier() != EvqShared || |
| 2076 | mCompileOptions & SH_INIT_SHARED_VARIABLES) |
Jiawei Shao | a75aa3b | 2018-06-21 10:38:28 +0800 | [diff] [blame] | 2077 | { |
| 2078 | out << " = " + zeroInitializer(symbol->getType()); |
| 2079 | } |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2080 | } |
| 2081 | else |
| 2082 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 2083 | declarator->traverse(this); |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2084 | } |
| 2085 | } |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2086 | } |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 2087 | else if (IsVaryingOut(declarator->getQualifier())) |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2088 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 2089 | TIntermSymbol *symbol = declarator->getAsSymbolNode(); |
Olli Etuaho | 282847e | 2017-07-12 14:11:01 +0300 | [diff] [blame] | 2090 | ASSERT(symbol); // Varying declarations can't have initializers. |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2091 | |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 2092 | const TVariable &variable = symbol->variable(); |
| 2093 | |
| 2094 | if (variable.symbolType() != SymbolType::Empty) |
Olli Etuaho | 93b059d | 2017-12-20 12:46:58 +0200 | [diff] [blame] | 2095 | { |
| 2096 | // Vertex outputs which are declared but not written to should still be declared to |
| 2097 | // allow successful linking. |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 2098 | mReferencedVaryings[symbol->uniqueId().get()] = &variable; |
Olli Etuaho | 93b059d | 2017-12-20 12:46:58 +0200 | [diff] [blame] | 2099 | } |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2100 | } |
| 2101 | } |
| 2102 | return false; |
| 2103 | } |
| 2104 | |
Olli Etuaho | bf4e1b7 | 2016-12-09 11:30:15 +0000 | [diff] [blame] | 2105 | bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node) |
| 2106 | { |
| 2107 | // Do not do any translation |
| 2108 | return false; |
| 2109 | } |
| 2110 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2111 | void OutputHLSL::visitFunctionPrototype(TIntermFunctionPrototype *node) |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 2112 | { |
| 2113 | TInfoSinkBase &out = getInfoSink(); |
| 2114 | |
Olli Etuaho | beb6dc7 | 2017-12-14 16:03:03 +0200 | [diff] [blame] | 2115 | size_t index = mCallDag.findIndex(node->getFunction()->uniqueId()); |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 2116 | // Skip the prototype if it is not implemented (and thus not used) |
| 2117 | if (index == CallDAG::InvalidIndex) |
| 2118 | { |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2119 | return; |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2122 | const TFunction *func = node->getFunction(); |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 2123 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2124 | TString name = DecorateFunctionIfNeeded(func); |
| 2125 | out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(func) |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 2126 | << (mOutputLod0Function ? "Lod0(" : "("); |
| 2127 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2128 | size_t paramCount = func->getParamCount(); |
| 2129 | for (unsigned int i = 0; i < paramCount; i++) |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 2130 | { |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2131 | writeParameter(func->getParam(i), out); |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 2132 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2133 | if (i < paramCount - 1) |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 2134 | { |
| 2135 | out << ", "; |
| 2136 | } |
| 2137 | } |
| 2138 | |
| 2139 | out << ");\n"; |
| 2140 | |
| 2141 | // Also prototype the Lod0 variant if needed |
| 2142 | bool needsLod0 = mASTMetadataList[index].mNeedsLod0; |
| 2143 | if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER) |
| 2144 | { |
| 2145 | mOutputLod0Function = true; |
| 2146 | node->traverse(this); |
| 2147 | mOutputLod0Function = false; |
| 2148 | } |
Olli Etuaho | 16c745a | 2017-01-16 17:02:27 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2151 | bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node) |
| 2152 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2153 | TInfoSinkBase &out = getInfoSink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2154 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2155 | switch (node->getOp()) |
| 2156 | { |
Olli Etuaho | 1ecd14b | 2017-01-26 13:54:15 -0800 | [diff] [blame] | 2157 | case EOpCallBuiltInFunction: |
| 2158 | case EOpCallFunctionInAST: |
| 2159 | case EOpCallInternalRawFunction: |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2160 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2161 | TIntermSequence *arguments = node->getSequence(); |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2162 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2163 | bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function; |
Olli Etuaho | 1ecd14b | 2017-01-26 13:54:15 -0800 | [diff] [blame] | 2164 | if (node->getOp() == EOpCallFunctionInAST) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2165 | { |
Olli Etuaho | a8c414b | 2015-04-16 15:51:03 +0300 | [diff] [blame] | 2166 | if (node->isArray()) |
| 2167 | { |
| 2168 | UNIMPLEMENTED(); |
| 2169 | } |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 2170 | size_t index = mCallDag.findIndex(node->getFunction()->uniqueId()); |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2171 | ASSERT(index != CallDAG::InvalidIndex); |
| 2172 | lod0 &= mASTMetadataList[index].mNeedsLod0; |
| 2173 | |
Olli Etuaho | beb6dc7 | 2017-12-14 16:03:03 +0200 | [diff] [blame] | 2174 | out << DecorateFunctionIfNeeded(node->getFunction()); |
Olli Etuaho | be59c2f | 2016-03-07 11:32:34 +0200 | [diff] [blame] | 2175 | out << DisambiguateFunctionName(node->getSequence()); |
| 2176 | out << (lod0 ? "Lod0(" : "("); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2177 | } |
Olli Etuaho | 1ecd14b | 2017-01-26 13:54:15 -0800 | [diff] [blame] | 2178 | else if (node->getOp() == EOpCallInternalRawFunction) |
Olli Etuaho | b741c76 | 2016-06-29 15:49:22 +0300 | [diff] [blame] | 2179 | { |
| 2180 | // This path is used for internal functions that don't have their definitions in the |
| 2181 | // AST, such as precision emulation functions. |
Olli Etuaho | beb6dc7 | 2017-12-14 16:03:03 +0200 | [diff] [blame] | 2182 | out << DecorateFunctionIfNeeded(node->getFunction()) << "("; |
Olli Etuaho | b741c76 | 2016-06-29 15:49:22 +0300 | [diff] [blame] | 2183 | } |
Olli Etuaho | 1bb8528 | 2017-12-14 13:39:53 +0200 | [diff] [blame] | 2184 | else if (node->getFunction()->isImageFunction()) |
Xinghua Cao | 711b7a1 | 2017-10-09 13:38:12 +0800 | [diff] [blame] | 2185 | { |
Jiawei Shao | 203b26f | 2018-07-25 10:30:43 +0800 | [diff] [blame] | 2186 | const ImmutableString &name = node->getFunction()->name(); |
Olli Etuaho | 8fbd9d9 | 2018-06-21 15:27:44 +0300 | [diff] [blame] | 2187 | TType type = (*arguments)[0]->getAsTyped()->getType(); |
| 2188 | const ImmutableString &imageFunctionName = mImageFunctionHLSL->useImageFunction( |
Olli Etuaho | bed35d7 | 2017-12-20 16:36:26 +0200 | [diff] [blame] | 2189 | name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat, |
Xinghua Cao | 711b7a1 | 2017-10-09 13:38:12 +0800 | [diff] [blame] | 2190 | type.getMemoryQualifier().readonly); |
| 2191 | out << imageFunctionName << "("; |
| 2192 | } |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 2193 | else if (node->getFunction()->isAtomicCounterFunction()) |
| 2194 | { |
| 2195 | const ImmutableString &name = node->getFunction()->name(); |
| 2196 | ImmutableString atomicFunctionName = |
| 2197 | mAtomicCounterFunctionHLSL->useAtomicCounterFunction(name); |
| 2198 | out << atomicFunctionName << "("; |
| 2199 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2200 | else |
| 2201 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 2202 | const ImmutableString &name = node->getFunction()->name(); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2203 | TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType(); |
Olli Etuaho | 92db39e | 2017-02-15 12:11:04 +0000 | [diff] [blame] | 2204 | int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument. |
| 2205 | if (arguments->size() > 1) |
| 2206 | { |
| 2207 | coords = (*arguments)[1]->getAsTyped()->getNominalSize(); |
| 2208 | } |
Olli Etuaho | b4cc49f | 2018-01-25 14:37:06 +0200 | [diff] [blame] | 2209 | const ImmutableString &textureFunctionName = |
| 2210 | mTextureFunctionHLSL->useTextureFunction(name, samplerType, coords, |
| 2211 | arguments->size(), lod0, mShaderType); |
Olli Etuaho | 5858f7e | 2016-04-08 13:08:46 +0300 | [diff] [blame] | 2212 | out << textureFunctionName << "("; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2213 | } |
Nicolas Capens | 84cfa12 | 2014-04-14 13:48:45 -0400 | [diff] [blame] | 2214 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2215 | for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2216 | { |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2217 | TIntermTyped *typedArg = (*arg)->getAsTyped(); |
| 2218 | 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] | 2219 | { |
| 2220 | out << "texture_"; |
| 2221 | (*arg)->traverse(this); |
| 2222 | out << ", sampler_"; |
| 2223 | } |
| 2224 | |
| 2225 | (*arg)->traverse(this); |
| 2226 | |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2227 | if (typedArg->getType().isStructureContainingSamplers()) |
| 2228 | { |
| 2229 | const TType &argType = typedArg->getType(); |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 2230 | TVector<const TVariable *> samplerSymbols; |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 2231 | ImmutableString structName = samplerNamePrefixFromStruct(typedArg); |
| 2232 | std::string namePrefix = "angle_"; |
| 2233 | namePrefix += structName.data(); |
| 2234 | argType.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols, |
Olli Etuaho | 2d88e9b | 2017-07-21 16:52:03 +0300 | [diff] [blame] | 2235 | nullptr, mSymbolTable); |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 2236 | for (const TVariable *sampler : samplerSymbols) |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2237 | { |
| 2238 | if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
| 2239 | { |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 2240 | out << ", texture_" << sampler->name(); |
| 2241 | out << ", sampler_" << sampler->name(); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2242 | } |
| 2243 | else |
| 2244 | { |
| 2245 | // In case of HLSL 4.1+, this symbol is the sampler index, and in case |
| 2246 | // of D3D9, it's the sampler variable. |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 2247 | out << ", " << sampler->name(); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2248 | } |
| 2249 | } |
| 2250 | } |
| 2251 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2252 | if (arg < arguments->end() - 1) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2253 | { |
| 2254 | out << ", "; |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | out << ")"; |
| 2259 | |
| 2260 | return false; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2261 | } |
Olli Etuaho | 8fab320 | 2017-05-08 18:22:22 +0300 | [diff] [blame] | 2262 | case EOpConstruct: |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 2263 | outputConstructor(out, visit, node); |
Olli Etuaho | 8fab320 | 2017-05-08 18:22:22 +0300 | [diff] [blame] | 2264 | break; |
Olli Etuaho | e180559 | 2017-01-02 16:41:20 +0000 | [diff] [blame] | 2265 | case EOpEqualComponentWise: |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2266 | outputTriplet(out, visit, "(", " == ", ")"); |
| 2267 | break; |
Olli Etuaho | e180559 | 2017-01-02 16:41:20 +0000 | [diff] [blame] | 2268 | case EOpNotEqualComponentWise: |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2269 | outputTriplet(out, visit, "(", " != ", ")"); |
| 2270 | break; |
Olli Etuaho | e180559 | 2017-01-02 16:41:20 +0000 | [diff] [blame] | 2271 | case EOpLessThanComponentWise: |
| 2272 | outputTriplet(out, visit, "(", " < ", ")"); |
| 2273 | break; |
| 2274 | case EOpGreaterThanComponentWise: |
| 2275 | outputTriplet(out, visit, "(", " > ", ")"); |
| 2276 | break; |
| 2277 | case EOpLessThanEqualComponentWise: |
| 2278 | outputTriplet(out, visit, "(", " <= ", ")"); |
| 2279 | break; |
| 2280 | case EOpGreaterThanEqualComponentWise: |
| 2281 | outputTriplet(out, visit, "(", " >= ", ")"); |
| 2282 | break; |
Olli Etuaho | 5878f83 | 2016-10-07 10:14:58 +0100 | [diff] [blame] | 2283 | case EOpMod: |
| 2284 | ASSERT(node->getUseEmulatedFunction()); |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 2285 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Olli Etuaho | 5878f83 | 2016-10-07 10:14:58 +0100 | [diff] [blame] | 2286 | break; |
| 2287 | case EOpModf: |
| 2288 | outputTriplet(out, visit, "modf(", ", ", ")"); |
| 2289 | break; |
| 2290 | case EOpPow: |
| 2291 | outputTriplet(out, visit, "pow(", ", ", ")"); |
| 2292 | break; |
| 2293 | case EOpAtan: |
| 2294 | ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator |
| 2295 | ASSERT(node->getUseEmulatedFunction()); |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 2296 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Olli Etuaho | 5878f83 | 2016-10-07 10:14:58 +0100 | [diff] [blame] | 2297 | break; |
| 2298 | case EOpMin: |
| 2299 | outputTriplet(out, visit, "min(", ", ", ")"); |
| 2300 | break; |
| 2301 | case EOpMax: |
| 2302 | outputTriplet(out, visit, "max(", ", ", ")"); |
| 2303 | break; |
| 2304 | case EOpClamp: |
| 2305 | outputTriplet(out, visit, "clamp(", ", ", ")"); |
| 2306 | break; |
| 2307 | case EOpMix: |
Arun Patole | d94f664 | 2015-05-18 16:25:12 +0530 | [diff] [blame] | 2308 | { |
| 2309 | TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped(); |
| 2310 | if (lastParamNode->getType().getBasicType() == EbtBool) |
| 2311 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2312 | // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType |
| 2313 | // y, genBType a)", |
Arun Patole | d94f664 | 2015-05-18 16:25:12 +0530 | [diff] [blame] | 2314 | // so use emulated version. |
| 2315 | ASSERT(node->getUseEmulatedFunction()); |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 2316 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Arun Patole | d94f664 | 2015-05-18 16:25:12 +0530 | [diff] [blame] | 2317 | } |
| 2318 | else |
| 2319 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2320 | outputTriplet(out, visit, "lerp(", ", ", ")"); |
Arun Patole | d94f664 | 2015-05-18 16:25:12 +0530 | [diff] [blame] | 2321 | } |
Olli Etuaho | 5878f83 | 2016-10-07 10:14:58 +0100 | [diff] [blame] | 2322 | break; |
Arun Patole | d94f664 | 2015-05-18 16:25:12 +0530 | [diff] [blame] | 2323 | } |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2324 | case EOpStep: |
| 2325 | outputTriplet(out, visit, "step(", ", ", ")"); |
| 2326 | break; |
Olli Etuaho | f7f0b8c | 2018-02-21 20:02:23 +0200 | [diff] [blame] | 2327 | case EOpSmoothstep: |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2328 | outputTriplet(out, visit, "smoothstep(", ", ", ")"); |
| 2329 | break; |
Olli Etuaho | 74da73f | 2017-02-01 15:37:48 +0000 | [diff] [blame] | 2330 | case EOpFrexp: |
| 2331 | case EOpLdexp: |
| 2332 | ASSERT(node->getUseEmulatedFunction()); |
| 2333 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
| 2334 | break; |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2335 | case EOpDistance: |
| 2336 | outputTriplet(out, visit, "distance(", ", ", ")"); |
| 2337 | break; |
| 2338 | case EOpDot: |
| 2339 | outputTriplet(out, visit, "dot(", ", ", ")"); |
| 2340 | break; |
| 2341 | case EOpCross: |
| 2342 | outputTriplet(out, visit, "cross(", ", ", ")"); |
| 2343 | break; |
Jamie Madill | e72595b | 2017-06-06 15:12:26 -0400 | [diff] [blame] | 2344 | case EOpFaceforward: |
Olli Etuaho | 5878f83 | 2016-10-07 10:14:58 +0100 | [diff] [blame] | 2345 | ASSERT(node->getUseEmulatedFunction()); |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 2346 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Olli Etuaho | 5878f83 | 2016-10-07 10:14:58 +0100 | [diff] [blame] | 2347 | break; |
| 2348 | case EOpReflect: |
| 2349 | outputTriplet(out, visit, "reflect(", ", ", ")"); |
| 2350 | break; |
| 2351 | case EOpRefract: |
| 2352 | outputTriplet(out, visit, "refract(", ", ", ")"); |
| 2353 | break; |
| 2354 | case EOpOuterProduct: |
| 2355 | ASSERT(node->getUseEmulatedFunction()); |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 2356 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
Olli Etuaho | 5878f83 | 2016-10-07 10:14:58 +0100 | [diff] [blame] | 2357 | break; |
Olli Etuaho | e180559 | 2017-01-02 16:41:20 +0000 | [diff] [blame] | 2358 | case EOpMulMatrixComponentWise: |
Olli Etuaho | 5878f83 | 2016-10-07 10:14:58 +0100 | [diff] [blame] | 2359 | outputTriplet(out, visit, "(", " * ", ")"); |
| 2360 | break; |
Olli Etuaho | 9250cb2 | 2017-01-21 10:51:27 +0000 | [diff] [blame] | 2361 | case EOpBitfieldExtract: |
| 2362 | case EOpBitfieldInsert: |
| 2363 | case EOpUaddCarry: |
| 2364 | case EOpUsubBorrow: |
| 2365 | case EOpUmulExtended: |
| 2366 | case EOpImulExtended: |
| 2367 | ASSERT(node->getUseEmulatedFunction()); |
| 2368 | writeEmulatedFunctionTriplet(out, visit, node->getOp()); |
| 2369 | break; |
Xinghua Cao | 4733585 | 2018-02-12 15:41:55 +0800 | [diff] [blame] | 2370 | case EOpBarrier: |
| 2371 | // barrier() is translated to GroupMemoryBarrierWithGroupSync(), which is the |
| 2372 | // cheapest *WithGroupSync() function, without any functionality loss, but |
| 2373 | // with the potential for severe performance loss. |
| 2374 | outputTriplet(out, visit, "GroupMemoryBarrierWithGroupSync(", "", ")"); |
| 2375 | break; |
| 2376 | case EOpMemoryBarrierShared: |
| 2377 | outputTriplet(out, visit, "GroupMemoryBarrier(", "", ")"); |
| 2378 | break; |
| 2379 | case EOpMemoryBarrierAtomicCounter: |
| 2380 | case EOpMemoryBarrierBuffer: |
| 2381 | case EOpMemoryBarrierImage: |
| 2382 | outputTriplet(out, visit, "DeviceMemoryBarrier(", "", ")"); |
| 2383 | break; |
| 2384 | case EOpGroupMemoryBarrier: |
| 2385 | case EOpMemoryBarrier: |
| 2386 | outputTriplet(out, visit, "AllMemoryBarrier(", "", ")"); |
| 2387 | break; |
Jiawei Shao | a6a7842 | 2018-06-28 08:32:54 +0800 | [diff] [blame] | 2388 | |
| 2389 | // Single atomic function calls without return value. |
| 2390 | // e.g. atomicAdd(dest, value) should be translated into InterlockedAdd(dest, value). |
| 2391 | case EOpAtomicAdd: |
| 2392 | case EOpAtomicMin: |
| 2393 | case EOpAtomicMax: |
| 2394 | case EOpAtomicAnd: |
| 2395 | case EOpAtomicOr: |
| 2396 | case EOpAtomicXor: |
| 2397 | outputTriplet(out, visit, GetHLSLAtomicFunctionStringAndLeftParenthesis(node->getOp()), |
| 2398 | ",", ")"); |
| 2399 | break; |
| 2400 | |
| 2401 | // The parameter 'original_value' of InterlockedExchange(dest, value, original_value) and |
| 2402 | // InterlockedCompareExchange(dest, compare_value, value, original_value) is not optional. |
| 2403 | // https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/interlockedexchange |
| 2404 | // https://docs.microsoft.com/en-us/windows/desktop/direct3dhlsl/interlockedcompareexchange |
| 2405 | // So all the call of atomicExchange(dest, value) and atomicCompSwap(dest, compare_value, |
| 2406 | // value) should all be modified into the form of "int temp; temp = atomicExchange(dest, |
| 2407 | // value);" and "int temp; temp = atomicCompSwap(dest, compare_value, value);" in the |
| 2408 | // intermediate tree before traversing outputHLSL. |
| 2409 | case EOpAtomicExchange: |
| 2410 | case EOpAtomicCompSwap: |
Olli Etuaho | 5878f83 | 2016-10-07 10:14:58 +0100 | [diff] [blame] | 2411 | default: |
| 2412 | UNREACHABLE(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2413 | } |
| 2414 | |
| 2415 | return true; |
| 2416 | } |
| 2417 | |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 2418 | void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2419 | { |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2420 | out << "if ("; |
| 2421 | |
| 2422 | node->getCondition()->traverse(this); |
| 2423 | |
| 2424 | out << ")\n"; |
| 2425 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2426 | outputLineDirective(out, node->getLine().first_line); |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2427 | |
| 2428 | bool discard = false; |
| 2429 | |
| 2430 | if (node->getTrueBlock()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2431 | { |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2432 | // The trueBlock child node will output braces. |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2433 | node->getTrueBlock()->traverse(this); |
daniel@transgaming.com | b587598 | 2010-04-15 20:44:53 +0000 | [diff] [blame] | 2434 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2435 | // Detect true discard |
| 2436 | discard = (discard || FindDiscard::search(node->getTrueBlock())); |
| 2437 | } |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2438 | else |
| 2439 | { |
| 2440 | // TODO(oetuaho): Check if the semicolon inside is necessary. |
| 2441 | // It's there as a result of conservative refactoring of the output. |
| 2442 | out << "{;}\n"; |
| 2443 | } |
Corentin Wallez | 80bacde | 2014-11-10 12:07:37 -0800 | [diff] [blame] | 2444 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2445 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2446 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2447 | if (node->getFalseBlock()) |
| 2448 | { |
| 2449 | out << "else\n"; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2450 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2451 | outputLineDirective(out, node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2452 | |
Olli Etuaho | 32db19b | 2016-10-04 14:43:16 +0100 | [diff] [blame] | 2453 | // The falseBlock child node will output braces. |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2454 | node->getFalseBlock()->traverse(this); |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2455 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2456 | outputLineDirective(out, node->getFalseBlock()->getLine().first_line); |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2457 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2458 | // Detect false discard |
| 2459 | discard = (discard || FindDiscard::search(node->getFalseBlock())); |
| 2460 | } |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2461 | |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2462 | // ANGLE issue 486: Detect problematic conditional discard |
Olli Etuaho | fd3b9be | 2015-05-18 17:07:36 +0300 | [diff] [blame] | 2463 | if (discard) |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2464 | { |
| 2465 | mUsesDiscardRewriting = true; |
daniel@transgaming.com | 3d53fda | 2010-03-21 04:30:55 +0000 | [diff] [blame] | 2466 | } |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 2467 | } |
| 2468 | |
Olli Etuaho | d0bad2c | 2016-09-09 18:01:16 +0300 | [diff] [blame] | 2469 | bool OutputHLSL::visitTernary(Visit, TIntermTernary *) |
| 2470 | { |
| 2471 | // Ternary ops should have been already converted to something else in the AST. HLSL ternary |
| 2472 | // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator. |
| 2473 | UNREACHABLE(); |
| 2474 | return false; |
| 2475 | } |
| 2476 | |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 2477 | bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node) |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 2478 | { |
| 2479 | TInfoSinkBase &out = getInfoSink(); |
| 2480 | |
Olli Etuaho | 3d932d8 | 2016-04-12 11:10:30 +0300 | [diff] [blame] | 2481 | ASSERT(mInsideFunction); |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 2482 | |
| 2483 | // 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] | 2484 | if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node)) |
Olli Etuaho | d81ed84 | 2015-05-12 12:46:35 +0300 | [diff] [blame] | 2485 | { |
| 2486 | out << "FLATTEN "; |
| 2487 | } |
| 2488 | |
Olli Etuaho | 5796127 | 2016-09-14 13:57:46 +0300 | [diff] [blame] | 2489 | writeIfElse(out, node); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2490 | |
| 2491 | return false; |
| 2492 | } |
| 2493 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2494 | bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2495 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2496 | TInfoSinkBase &out = getInfoSink(); |
| 2497 | |
Olli Etuaho | 923ecef | 2017-10-11 12:01:38 +0300 | [diff] [blame] | 2498 | ASSERT(node->getStatementList()); |
| 2499 | if (visit == PreVisit) |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2500 | { |
Olli Etuaho | 89a69a0 | 2017-10-23 12:20:45 +0300 | [diff] [blame] | 2501 | node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics)); |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2502 | } |
Olli Etuaho | 923ecef | 2017-10-11 12:01:38 +0300 | [diff] [blame] | 2503 | outputTriplet(out, visit, "switch (", ") ", ""); |
| 2504 | // The curly braces get written when visiting the statementList block. |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2505 | return true; |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2506 | } |
| 2507 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2508 | bool OutputHLSL::visitCase(Visit visit, TIntermCase *node) |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2509 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2510 | TInfoSinkBase &out = getInfoSink(); |
| 2511 | |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2512 | if (node->hasCondition()) |
| 2513 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2514 | outputTriplet(out, visit, "case (", "", "):\n"); |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2515 | return true; |
| 2516 | } |
| 2517 | else |
| 2518 | { |
Olli Etuaho | 05ae50d | 2015-02-20 10:16:47 +0200 | [diff] [blame] | 2519 | out << "default:\n"; |
| 2520 | return false; |
| 2521 | } |
Olli Etuaho | 3c1dfb5 | 2015-02-20 11:34:03 +0200 | [diff] [blame] | 2522 | } |
| 2523 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2524 | void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node) |
| 2525 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2526 | TInfoSinkBase &out = getInfoSink(); |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 2527 | writeConstantUnion(out, node->getType(), node->getConstantValue()); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node) |
| 2531 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2532 | mNestedLoopDepth++; |
| 2533 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2534 | bool wasDiscontinuous = mInsideDiscontinuousLoop; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2535 | mInsideDiscontinuousLoop = |
| 2536 | mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2537 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2538 | TInfoSinkBase &out = getInfoSink(); |
| 2539 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 2540 | if (mOutputType == SH_HLSL_3_0_OUTPUT) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2541 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2542 | if (handleExcessiveLoop(out, node)) |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2543 | { |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2544 | mInsideDiscontinuousLoop = wasDiscontinuous; |
| 2545 | mNestedLoopDepth--; |
| 2546 | |
shannon.woods@transgaming.com | 9cbce92 | 2013-02-28 23:14:24 +0000 | [diff] [blame] | 2547 | return false; |
| 2548 | } |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2551 | const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : ""; |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2552 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2553 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2554 | out << "{" << unroll << " do\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2555 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2556 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2557 | } |
| 2558 | else |
| 2559 | { |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2560 | out << "{" << unroll << " for("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2561 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2562 | if (node->getInit()) |
| 2563 | { |
| 2564 | node->getInit()->traverse(this); |
| 2565 | } |
| 2566 | |
| 2567 | out << "; "; |
| 2568 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2569 | if (node->getCondition()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2570 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2571 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2572 | } |
| 2573 | |
| 2574 | out << "; "; |
| 2575 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2576 | if (node->getExpression()) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2577 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2578 | node->getExpression()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2581 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2582 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2583 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | if (node->getBody()) |
| 2587 | { |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2588 | // The loop body node will output braces. |
Olli Etuaho | a6f2209 | 2015-05-08 18:31:10 +0300 | [diff] [blame] | 2589 | node->getBody()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2590 | } |
Olli Etuaho | 4785fec | 2015-05-18 16:09:37 +0300 | [diff] [blame] | 2591 | else |
| 2592 | { |
| 2593 | // TODO(oetuaho): Check if the semicolon inside is necessary. |
| 2594 | // It's there as a result of conservative refactoring of the output. |
| 2595 | out << "{;}\n"; |
| 2596 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2597 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2598 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2599 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2600 | if (node->getType() == ELoopDoWhile) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2601 | { |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2602 | outputLineDirective(out, node->getCondition()->getLine().first_line); |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 2603 | out << "while ("; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2604 | |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2605 | node->getCondition()->traverse(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2606 | |
Olli Etuaho | 40dbdd6 | 2017-10-13 13:34:19 +0300 | [diff] [blame] | 2607 | out << ");\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
daniel@transgaming.com | 7353698 | 2012-03-21 20:45:49 +0000 | [diff] [blame] | 2610 | out << "}\n"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2611 | |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2612 | mInsideDiscontinuousLoop = wasDiscontinuous; |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2613 | mNestedLoopDepth--; |
daniel@transgaming.com | e11100c | 2012-05-31 01:20:32 +0000 | [diff] [blame] | 2614 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2615 | return false; |
| 2616 | } |
| 2617 | |
| 2618 | bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node) |
| 2619 | { |
Olli Etuaho | 8886f0f | 2017-10-10 11:59:45 +0300 | [diff] [blame] | 2620 | if (visit == PreVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2621 | { |
Olli Etuaho | 8886f0f | 2017-10-10 11:59:45 +0300 | [diff] [blame] | 2622 | TInfoSinkBase &out = getInfoSink(); |
| 2623 | |
| 2624 | switch (node->getFlowOp()) |
| 2625 | { |
| 2626 | case EOpKill: |
| 2627 | out << "discard"; |
| 2628 | break; |
| 2629 | case EOpBreak: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2630 | if (mNestedLoopDepth > 1) |
| 2631 | { |
| 2632 | mUsesNestedBreak = true; |
| 2633 | } |
Nicolas Capens | 655fe36 | 2014-04-11 13:12:34 -0400 | [diff] [blame] | 2634 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2635 | if (mExcessiveLoopIndex) |
| 2636 | { |
| 2637 | out << "{Break"; |
| 2638 | mExcessiveLoopIndex->traverse(this); |
| 2639 | out << " = true; break;}\n"; |
| 2640 | } |
| 2641 | else |
| 2642 | { |
Olli Etuaho | 8886f0f | 2017-10-10 11:59:45 +0300 | [diff] [blame] | 2643 | out << "break"; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2644 | } |
Olli Etuaho | 8886f0f | 2017-10-10 11:59:45 +0300 | [diff] [blame] | 2645 | break; |
| 2646 | case EOpContinue: |
| 2647 | out << "continue"; |
| 2648 | break; |
| 2649 | case EOpReturn: |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2650 | if (node->getExpression()) |
| 2651 | { |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 2652 | ASSERT(!mInsideMain); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2653 | out << "return "; |
| 2654 | } |
| 2655 | else |
| 2656 | { |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 2657 | if (mInsideMain && shaderNeedsGenerateOutput()) |
| 2658 | { |
| 2659 | out << "return " << generateOutputCall(); |
| 2660 | } |
| 2661 | else |
| 2662 | { |
| 2663 | out << "return"; |
| 2664 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2665 | } |
Olli Etuaho | 8886f0f | 2017-10-10 11:59:45 +0300 | [diff] [blame] | 2666 | break; |
| 2667 | default: |
| 2668 | UNREACHABLE(); |
| 2669 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | return true; |
| 2673 | } |
| 2674 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2675 | // Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2676 | // (The D3D documentation says 255 iterations, but the compiler complains at anything more than |
| 2677 | // 254). |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2678 | bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2679 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2680 | const int MAX_LOOP_ITERATIONS = 254; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2681 | |
| 2682 | // Parse loops of the form: |
| 2683 | // for(int index = initial; index [comparator] limit; index += increment) |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 2684 | TIntermSymbol *index = nullptr; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2685 | TOperator comparator = EOpNull; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2686 | int initial = 0; |
| 2687 | int limit = 0; |
| 2688 | int increment = 0; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2689 | |
| 2690 | // Parse index name and intial value |
| 2691 | if (node->getInit()) |
| 2692 | { |
Olli Etuaho | 13389b6 | 2016-10-16 11:48:18 +0100 | [diff] [blame] | 2693 | TIntermDeclaration *init = node->getInit()->getAsDeclarationNode(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2694 | |
| 2695 | if (init) |
| 2696 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 2697 | TIntermSequence *sequence = init->getSequence(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2698 | TIntermTyped *variable = (*sequence)[0]->getAsTyped(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2699 | |
| 2700 | if (variable && variable->getQualifier() == EvqTemporary) |
| 2701 | { |
| 2702 | TIntermBinary *assign = variable->getAsBinaryNode(); |
| 2703 | |
| 2704 | if (assign->getOp() == EOpInitialize) |
| 2705 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2706 | TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2707 | TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion(); |
| 2708 | |
| 2709 | if (symbol && constant) |
| 2710 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2711 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2712 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2713 | index = symbol; |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2714 | initial = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2715 | } |
| 2716 | } |
| 2717 | } |
| 2718 | } |
| 2719 | } |
| 2720 | } |
| 2721 | |
| 2722 | // Parse comparator and limit value |
Yunchao He | 4f28544 | 2017-04-21 12:15:49 +0800 | [diff] [blame] | 2723 | if (index != nullptr && node->getCondition()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2724 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2725 | TIntermBinary *test = node->getCondition()->getAsBinaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2726 | |
Olli Etuaho | b6af22b | 2017-12-15 14:05:44 +0200 | [diff] [blame] | 2727 | if (test && test->getLeft()->getAsSymbolNode()->uniqueId() == index->uniqueId()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2728 | { |
| 2729 | TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion(); |
| 2730 | |
| 2731 | if (constant) |
| 2732 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2733 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2734 | { |
| 2735 | comparator = test->getOp(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2736 | limit = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | } |
| 2741 | |
| 2742 | // Parse increment |
Yunchao He | 4f28544 | 2017-04-21 12:15:49 +0800 | [diff] [blame] | 2743 | if (index != nullptr && comparator != EOpNull && node->getExpression()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2744 | { |
alokp@chromium.org | 5281355 | 2010-11-16 18:36:09 +0000 | [diff] [blame] | 2745 | TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2746 | TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode(); |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2747 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2748 | if (binaryTerminal) |
| 2749 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2750 | TOperator op = binaryTerminal->getOp(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2751 | TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion(); |
| 2752 | |
| 2753 | if (constant) |
| 2754 | { |
shannonwoods@chromium.org | 09e0988 | 2013-05-30 00:18:25 +0000 | [diff] [blame] | 2755 | if (constant->getBasicType() == EbtInt && constant->isScalar()) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2756 | { |
shannon.woods%transgaming.com@gtempaccount.com | c0d0c22 | 2013-04-13 03:29:36 +0000 | [diff] [blame] | 2757 | int value = constant->getIConst(0); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2758 | |
| 2759 | switch (op) |
| 2760 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2761 | case EOpAddAssign: |
| 2762 | increment = value; |
| 2763 | break; |
| 2764 | case EOpSubAssign: |
| 2765 | increment = -value; |
| 2766 | break; |
| 2767 | default: |
| 2768 | UNIMPLEMENTED(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | } |
| 2772 | } |
| 2773 | else if (unaryTerminal) |
| 2774 | { |
| 2775 | TOperator op = unaryTerminal->getOp(); |
| 2776 | |
| 2777 | switch (op) |
| 2778 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2779 | case EOpPostIncrement: |
| 2780 | increment = 1; |
| 2781 | break; |
| 2782 | case EOpPostDecrement: |
| 2783 | increment = -1; |
| 2784 | break; |
| 2785 | case EOpPreIncrement: |
| 2786 | increment = 1; |
| 2787 | break; |
| 2788 | case EOpPreDecrement: |
| 2789 | increment = -1; |
| 2790 | break; |
| 2791 | default: |
| 2792 | UNIMPLEMENTED(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2793 | } |
| 2794 | } |
| 2795 | } |
| 2796 | |
Yunchao He | 4f28544 | 2017-04-21 12:15:49 +0800 | [diff] [blame] | 2797 | if (index != nullptr && comparator != EOpNull && increment != 0) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2798 | { |
| 2799 | if (comparator == EOpLessThanEqual) |
| 2800 | { |
| 2801 | comparator = EOpLessThan; |
| 2802 | limit += 1; |
| 2803 | } |
| 2804 | |
| 2805 | if (comparator == EOpLessThan) |
| 2806 | { |
daniel@transgaming.com | f1f538e | 2011-02-09 16:30:01 +0000 | [diff] [blame] | 2807 | int iterations = (limit - initial) / increment; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2808 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2809 | if (iterations <= MAX_LOOP_ITERATIONS) |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2810 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2811 | return false; // Not an excessive loop |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2812 | } |
| 2813 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 2814 | TIntermSymbol *restoreIndex = mExcessiveLoopIndex; |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2815 | mExcessiveLoopIndex = index; |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 2816 | |
daniel@transgaming.com | 0933b0c | 2012-07-11 20:37:28 +0000 | [diff] [blame] | 2817 | out << "{int "; |
| 2818 | index->traverse(this); |
daniel@transgaming.com | 8c77f85 | 2012-07-11 20:37:35 +0000 | [diff] [blame] | 2819 | out << ";\n" |
| 2820 | "bool Break"; |
| 2821 | index->traverse(this); |
| 2822 | out << " = false;\n"; |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 2823 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2824 | bool firstLoopFragment = true; |
| 2825 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2826 | while (iterations > 0) |
| 2827 | { |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2828 | int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2829 | |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2830 | if (!firstLoopFragment) |
| 2831 | { |
Jamie Madill | 3c9eeb9 | 2013-11-04 11:09:26 -0500 | [diff] [blame] | 2832 | out << "if (!Break"; |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2833 | index->traverse(this); |
| 2834 | out << ") {\n"; |
| 2835 | } |
daniel@transgaming.com | 2fe20a8 | 2012-07-11 20:37:41 +0000 | [diff] [blame] | 2836 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2837 | if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment |
daniel@transgaming.com | 2fe20a8 | 2012-07-11 20:37:41 +0000 | [diff] [blame] | 2838 | { |
Yunchao He | d7297bf | 2017-04-19 15:27:10 +0800 | [diff] [blame] | 2839 | mExcessiveLoopIndex = nullptr; // Stops setting the Break flag |
daniel@transgaming.com | 2fe20a8 | 2012-07-11 20:37:41 +0000 | [diff] [blame] | 2840 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2841 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2842 | // for(int index = initial; index < clampedLimit; index += increment) |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2843 | const char *unroll = |
| 2844 | mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : ""; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2845 | |
Corentin Wallez | 1239ee9 | 2015-03-19 14:38:02 -0700 | [diff] [blame] | 2846 | out << unroll << " for("; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2847 | index->traverse(this); |
| 2848 | out << " = "; |
| 2849 | out << initial; |
| 2850 | |
| 2851 | out << "; "; |
| 2852 | index->traverse(this); |
| 2853 | out << " < "; |
| 2854 | out << clampedLimit; |
| 2855 | |
| 2856 | out << "; "; |
| 2857 | index->traverse(this); |
| 2858 | out << " += "; |
| 2859 | out << increment; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2860 | out << ")\n"; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2861 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2862 | outputLineDirective(out, node->getLine().first_line); |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2863 | out << "{\n"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2864 | |
| 2865 | if (node->getBody()) |
| 2866 | { |
| 2867 | node->getBody()->traverse(this); |
| 2868 | } |
| 2869 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2870 | outputLineDirective(out, node->getLine().first_line); |
daniel@transgaming.com | 5b60f5e | 2012-07-11 20:37:38 +0000 | [diff] [blame] | 2871 | out << ";}\n"; |
| 2872 | |
| 2873 | if (!firstLoopFragment) |
| 2874 | { |
| 2875 | out << "}\n"; |
| 2876 | } |
| 2877 | |
| 2878 | firstLoopFragment = false; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2879 | |
daniel@transgaming.com | 06eb0d4 | 2012-05-31 01:17:14 +0000 | [diff] [blame] | 2880 | initial += MAX_LOOP_ITERATIONS * increment; |
| 2881 | iterations -= MAX_LOOP_ITERATIONS; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2882 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2883 | |
daniel@transgaming.com | c264de4 | 2012-07-11 20:37:25 +0000 | [diff] [blame] | 2884 | out << "}"; |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2885 | |
daniel@transgaming.com | e9b3f60 | 2012-07-11 20:37:31 +0000 | [diff] [blame] | 2886 | mExcessiveLoopIndex = restoreIndex; |
| 2887 | |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2888 | return true; |
| 2889 | } |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2890 | else |
| 2891 | UNIMPLEMENTED(); |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 2894 | return false; // Not handled as an excessive loop |
daniel@transgaming.com | 4a35ef2 | 2010-04-08 03:51:06 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2897 | void OutputHLSL::outputTriplet(TInfoSinkBase &out, |
| 2898 | Visit visit, |
| 2899 | const char *preString, |
| 2900 | const char *inString, |
| 2901 | const char *postString) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2902 | { |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2903 | if (visit == PreVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2904 | { |
| 2905 | out << preString; |
| 2906 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2907 | else if (visit == InVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2908 | { |
| 2909 | out << inString; |
| 2910 | } |
daniel@transgaming.com | 67de6d6 | 2010-04-29 03:35:30 +0000 | [diff] [blame] | 2911 | else if (visit == PostVisit) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 2912 | { |
| 2913 | out << postString; |
| 2914 | } |
| 2915 | } |
| 2916 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 2917 | void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2918 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2919 | if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0)) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2920 | { |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2921 | out << "\n"; |
| 2922 | out << "#line " << line; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2923 | |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2924 | if (mSourcePath) |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2925 | { |
Olli Etuaho | a3a5cc6 | 2015-02-13 13:12:22 +0200 | [diff] [blame] | 2926 | out << " \"" << mSourcePath << "\""; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2927 | } |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 2928 | |
Jamie Madill | 32aab01 | 2015-01-27 14:12:26 -0500 | [diff] [blame] | 2929 | out << "\n"; |
apatrick@chromium.org | 0f4cefe | 2011-01-26 19:30:57 +0000 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2933 | void OutputHLSL::writeParameter(const TVariable *param, TInfoSinkBase &out) |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2934 | { |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2935 | const TType &type = param->getType(); |
| 2936 | TQualifier qualifier = type.getQualifier(); |
daniel@transgaming.com | d1acd1e | 2010-04-13 03:25:57 +0000 | [diff] [blame] | 2937 | |
Olli Etuaho | d4bd963 | 2018-03-08 16:32:44 +0200 | [diff] [blame] | 2938 | TString nameStr = DecorateVariableIfNeeded(*param); |
| 2939 | ASSERT(nameStr != ""); // HLSL demands named arguments, also for prototypes |
daniel@transgaming.com | 005c739 | 2010-04-15 20:45:27 +0000 | [diff] [blame] | 2940 | |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 2941 | if (IsSampler(type.getBasicType())) |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2942 | { |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 2943 | if (mOutputType == SH_HLSL_4_1_OUTPUT) |
| 2944 | { |
| 2945 | // Samplers are passed as indices to the sampler array. |
| 2946 | ASSERT(qualifier != EvqOut && qualifier != EvqInOut); |
Olli Etuaho | 2f7c04a | 2018-01-25 14:50:37 +0200 | [diff] [blame] | 2947 | out << "const uint " << nameStr << ArrayString(type); |
| 2948 | return; |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 2949 | } |
| 2950 | if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
| 2951 | { |
Olli Etuaho | 2f7c04a | 2018-01-25 14:50:37 +0200 | [diff] [blame] | 2952 | out << QualifierString(qualifier) << " " << TextureString(type.getBasicType()) |
| 2953 | << " texture_" << nameStr << ArrayString(type) << ", " << QualifierString(qualifier) |
| 2954 | << " " << SamplerString(type.getBasicType()) << " sampler_" << nameStr |
| 2955 | << ArrayString(type); |
| 2956 | return; |
Olli Etuaho | 9b4e862 | 2015-12-22 15:53:22 +0200 | [diff] [blame] | 2957 | } |
shannon.woods@transgaming.com | 01a5cf9 | 2013-02-28 23:13:51 +0000 | [diff] [blame] | 2958 | } |
| 2959 | |
Brandon Jones | 4a22f4b | 2018-10-23 14:36:47 -0700 | [diff] [blame] | 2960 | // If the parameter is an atomic counter, we need to add an extra parameter to keep track of the |
| 2961 | // buffer offset. |
| 2962 | if (IsAtomicCounter(type.getBasicType())) |
| 2963 | { |
| 2964 | out << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr << ", int " |
| 2965 | << nameStr << "_offset"; |
| 2966 | } |
| 2967 | else |
| 2968 | { |
| 2969 | out << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr |
| 2970 | << ArrayString(type); |
| 2971 | } |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2972 | |
| 2973 | // If the structure parameter contains samplers, they need to be passed into the function as |
| 2974 | // separate parameters. HLSL doesn't natively support samplers in structs. |
| 2975 | if (type.isStructureContainingSamplers()) |
| 2976 | { |
| 2977 | ASSERT(qualifier != EvqOut && qualifier != EvqInOut); |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 2978 | TVector<const TVariable *> samplerSymbols; |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 2979 | std::string namePrefix = "angle"; |
| 2980 | namePrefix += nameStr.c_str(); |
| 2981 | type.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols, nullptr, |
| 2982 | mSymbolTable); |
Olli Etuaho | bbd9d4c | 2017-12-21 12:02:00 +0200 | [diff] [blame] | 2983 | for (const TVariable *sampler : samplerSymbols) |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2984 | { |
Olli Etuaho | 28839f0 | 2017-08-15 11:38:16 +0300 | [diff] [blame] | 2985 | const TType &samplerType = sampler->getType(); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2986 | if (mOutputType == SH_HLSL_4_1_OUTPUT) |
| 2987 | { |
Olli Etuaho | 2f7c04a | 2018-01-25 14:50:37 +0200 | [diff] [blame] | 2988 | out << ", const uint " << sampler->name() << ArrayString(samplerType); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2989 | } |
| 2990 | else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT) |
| 2991 | { |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2992 | ASSERT(IsSampler(samplerType.getBasicType())); |
Olli Etuaho | 2f7c04a | 2018-01-25 14:50:37 +0200 | [diff] [blame] | 2993 | out << ", " << QualifierString(qualifier) << " " |
| 2994 | << TextureString(samplerType.getBasicType()) << " texture_" << sampler->name() |
| 2995 | << ArrayString(samplerType) << ", " << QualifierString(qualifier) << " " |
| 2996 | << SamplerString(samplerType.getBasicType()) << " sampler_" << sampler->name() |
| 2997 | << ArrayString(samplerType); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 2998 | } |
| 2999 | else |
| 3000 | { |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 3001 | ASSERT(IsSampler(samplerType.getBasicType())); |
Olli Etuaho | 2f7c04a | 2018-01-25 14:50:37 +0200 | [diff] [blame] | 3002 | out << ", " << QualifierString(qualifier) << " " << TypeString(samplerType) << " " |
| 3003 | << sampler->name() << ArrayString(samplerType); |
Olli Etuaho | 9696316 | 2016-03-21 11:54:33 +0200 | [diff] [blame] | 3004 | } |
| 3005 | } |
| 3006 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
jchen10 | efe061b | 2018-11-13 16:44:40 +0800 | [diff] [blame^] | 3009 | TString OutputHLSL::zeroInitializer(const TType &type) const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3010 | { |
| 3011 | TString string; |
| 3012 | |
Jamie Madill | 94bf7f2 | 2013-07-08 13:31:15 -0400 | [diff] [blame] | 3013 | size_t size = type.getObjectSize(); |
jchen10 | efe061b | 2018-11-13 16:44:40 +0800 | [diff] [blame^] | 3014 | if (size >= kZeroCount) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3015 | { |
jchen10 | efe061b | 2018-11-13 16:44:40 +0800 | [diff] [blame^] | 3016 | mUseZeroArray = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3017 | } |
jchen10 | efe061b | 2018-11-13 16:44:40 +0800 | [diff] [blame^] | 3018 | string = GetZeroInitializer(size).c_str(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3019 | |
daniel@transgaming.com | ead2304 | 2010-04-29 03:35:36 +0000 | [diff] [blame] | 3020 | return "{" + string + "}"; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 3021 | } |
daniel@transgaming.com | 72d0b52 | 2010-04-13 19:53:44 +0000 | [diff] [blame] | 3022 | |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 3023 | void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node) |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 3024 | { |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 3025 | // Array constructors should have been already pruned from the code. |
| 3026 | ASSERT(!node->getType().isArray()); |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 3027 | |
| 3028 | if (visit == PreVisit) |
| 3029 | { |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 3030 | TString constructorName; |
| 3031 | if (node->getBasicType() == EbtStruct) |
| 3032 | { |
| 3033 | constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct()); |
| 3034 | } |
| 3035 | else |
| 3036 | { |
| 3037 | constructorName = |
| 3038 | mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence()); |
| 3039 | } |
Olli Etuaho | be59c2f | 2016-03-07 11:32:34 +0200 | [diff] [blame] | 3040 | out << constructorName << "("; |
Nicolas Capens | 1af18dc | 2014-06-11 11:07:32 -0400 | [diff] [blame] | 3041 | } |
| 3042 | else if (visit == InVisit) |
| 3043 | { |
| 3044 | out << ", "; |
| 3045 | } |
| 3046 | else if (visit == PostVisit) |
| 3047 | { |
| 3048 | out << ")"; |
| 3049 | } |
| 3050 | } |
| 3051 | |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3052 | const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out, |
| 3053 | const TType &type, |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3054 | const TConstantUnion *const constUnion) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3055 | { |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 3056 | ASSERT(!type.isArray()); |
| 3057 | |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3058 | const TConstantUnion *constUnionIterated = constUnion; |
| 3059 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3060 | const TStructure *structure = type.getStruct(); |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 3061 | if (structure) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3062 | { |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 3063 | out << mStructureHLSL->addStructConstructor(*structure) << "("; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3064 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3065 | const TFieldList &fields = structure->fields(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3066 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 3067 | for (size_t i = 0; i < fields.size(); i++) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3068 | { |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 3069 | const TType *fieldType = fields[i]->type(); |
Jamie Madill | 8c46ab1 | 2015-12-07 16:39:19 -0500 | [diff] [blame] | 3070 | constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3071 | |
Jamie Madill | 98493dd | 2013-07-08 14:39:03 -0400 | [diff] [blame] | 3072 | if (i != fields.size() - 1) |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3073 | { |
| 3074 | out << ", "; |
| 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | out << ")"; |
| 3079 | } |
| 3080 | else |
| 3081 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3082 | size_t size = type.getObjectSize(); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3083 | bool writeType = size > 1; |
Jamie Madill | f91ce81 | 2014-06-13 10:04:34 -0400 | [diff] [blame] | 3084 | |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3085 | if (writeType) |
| 3086 | { |
Jamie Madill | 033dae6 | 2014-06-18 12:56:28 -0400 | [diff] [blame] | 3087 | out << TypeString(type) << "("; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3088 | } |
Olli Etuaho | 56a2f95 | 2016-12-08 12:16:27 +0000 | [diff] [blame] | 3089 | constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size); |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3090 | if (writeType) |
| 3091 | { |
| 3092 | out << ")"; |
| 3093 | } |
| 3094 | } |
| 3095 | |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3096 | return constUnionIterated; |
daniel@transgaming.com | a54da4e | 2010-05-07 13:03:28 +0000 | [diff] [blame] | 3097 | } |
| 3098 | |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 3099 | void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op) |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 3100 | { |
Olli Etuaho | d68924e | 2017-01-02 17:34:40 +0000 | [diff] [blame] | 3101 | if (visit == PreVisit) |
| 3102 | { |
| 3103 | const char *opStr = GetOperatorString(op); |
| 3104 | BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr); |
| 3105 | out << "("; |
| 3106 | } |
| 3107 | else |
| 3108 | { |
| 3109 | outputTriplet(out, visit, nullptr, ", ", ")"); |
| 3110 | } |
Olli Etuaho | 5c9cd3d | 2014-12-18 13:04:25 +0200 | [diff] [blame] | 3111 | } |
| 3112 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3113 | bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, |
| 3114 | TIntermSymbol *symbolNode, |
| 3115 | TIntermTyped *expression) |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3116 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 3117 | ASSERT(symbolNode->variable().symbolType() != SymbolType::Empty); |
| 3118 | const TIntermSymbol *symbolInInitializer = FindSymbolNode(expression, symbolNode->getName()); |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3119 | |
Olli Etuaho | 4728bdc | 2017-12-20 17:51:08 +0200 | [diff] [blame] | 3120 | if (symbolInInitializer) |
Jamie Madill | 3799714 | 2015-01-28 10:06:34 -0500 | [diff] [blame] | 3121 | { |
| 3122 | // Type already printed |
| 3123 | out << "t" + str(mUniqueIndex) + " = "; |
| 3124 | expression->traverse(this); |
| 3125 | out << ", "; |
| 3126 | symbolNode->traverse(this); |
| 3127 | out << " = t" + str(mUniqueIndex); |
| 3128 | |
| 3129 | mUniqueIndex++; |
| 3130 | return true; |
| 3131 | } |
| 3132 | |
| 3133 | return false; |
| 3134 | } |
| 3135 | |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3136 | bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out, |
| 3137 | TIntermSymbol *symbolNode, |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3138 | TIntermTyped *initializer) |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3139 | { |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 3140 | if (initializer->hasConstantValue()) |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3141 | { |
| 3142 | symbolNode->traverse(this); |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 3143 | out << ArrayString(symbolNode->getType()); |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3144 | out << " = {"; |
Olli Etuaho | ea22b7a | 2018-01-04 17:09:11 +0200 | [diff] [blame] | 3145 | writeConstantUnionArray(out, initializer->getConstantValue(), |
| 3146 | initializer->getType().getObjectSize()); |
Olli Etuaho | 18b9deb | 2015-11-05 12:14:50 +0200 | [diff] [blame] | 3147 | out << "}"; |
| 3148 | return true; |
| 3149 | } |
| 3150 | return false; |
| 3151 | } |
| 3152 | |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3153 | TString OutputHLSL::addStructEqualityFunction(const TStructure &structure) |
| 3154 | { |
| 3155 | const TFieldList &fields = structure.fields(); |
| 3156 | |
| 3157 | for (const auto &eqFunction : mStructEqualityFunctions) |
| 3158 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3159 | if (eqFunction->structure == &structure) |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3160 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3161 | return eqFunction->functionName; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | const TString &structNameString = StructNameString(structure); |
| 3166 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3167 | StructEqualityFunction *function = new StructEqualityFunction(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3168 | function->structure = &structure; |
| 3169 | function->functionName = "angle_eq_" + structNameString; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3170 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3171 | TInfoSinkBase fnOut; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3172 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3173 | fnOut << "bool " << function->functionName << "(" << structNameString << " a, " |
| 3174 | << structNameString + " b)\n" |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3175 | << "{\n" |
| 3176 | " return "; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3177 | |
| 3178 | for (size_t i = 0; i < fields.size(); i++) |
| 3179 | { |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3180 | const TField *field = fields[i]; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3181 | const TType *fieldType = field->type(); |
| 3182 | |
| 3183 | const TString &fieldNameA = "a." + Decorate(field->name()); |
| 3184 | const TString &fieldNameB = "b." + Decorate(field->name()); |
| 3185 | |
| 3186 | if (i > 0) |
| 3187 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3188 | fnOut << " && "; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3189 | } |
| 3190 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3191 | fnOut << "("; |
| 3192 | outputEqual(PreVisit, *fieldType, EOpEqual, fnOut); |
| 3193 | fnOut << fieldNameA; |
| 3194 | outputEqual(InVisit, *fieldType, EOpEqual, fnOut); |
| 3195 | fnOut << fieldNameB; |
| 3196 | outputEqual(PostVisit, *fieldType, EOpEqual, fnOut); |
| 3197 | fnOut << ")"; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3198 | } |
| 3199 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3200 | fnOut << ";\n" |
| 3201 | << "}\n"; |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3202 | |
| 3203 | function->functionDefinition = fnOut.c_str(); |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3204 | |
| 3205 | mStructEqualityFunctions.push_back(function); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3206 | mEqualityFunctions.push_back(function); |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3207 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3208 | return function->functionName; |
Jamie Madill | 55e79e0 | 2015-02-09 15:35:00 -0500 | [diff] [blame] | 3209 | } |
| 3210 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3211 | TString OutputHLSL::addArrayEqualityFunction(const TType &type) |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3212 | { |
| 3213 | for (const auto &eqFunction : mArrayEqualityFunctions) |
| 3214 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3215 | if (eqFunction->type == type) |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3216 | { |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3217 | return eqFunction->functionName; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3218 | } |
| 3219 | } |
| 3220 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3221 | TType elementType(type); |
| 3222 | elementType.toArrayElementType(); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3223 | |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3224 | ArrayHelperFunction *function = new ArrayHelperFunction(); |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3225 | function->type = type; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3226 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3227 | function->functionName = ArrayHelperFunctionName("angle_eq", type); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3228 | |
| 3229 | TInfoSinkBase fnOut; |
| 3230 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3231 | const TString &typeName = TypeString(type); |
| 3232 | fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type) |
| 3233 | << ", " << typeName << " b" << ArrayString(type) << ")\n" |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3234 | << "{\n" |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3235 | " for (int i = 0; i < " |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3236 | << type.getOutermostArraySize() |
| 3237 | << "; ++i)\n" |
| 3238 | " {\n" |
| 3239 | " if ("; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3240 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3241 | outputEqual(PreVisit, elementType, EOpNotEqual, fnOut); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3242 | fnOut << "a[i]"; |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3243 | outputEqual(InVisit, elementType, EOpNotEqual, fnOut); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3244 | fnOut << "b[i]"; |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3245 | outputEqual(PostVisit, elementType, EOpNotEqual, fnOut); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3246 | |
| 3247 | fnOut << ") { return false; }\n" |
| 3248 | " }\n" |
| 3249 | " return true;\n" |
| 3250 | "}\n"; |
| 3251 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3252 | function->functionDefinition = fnOut.c_str(); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3253 | |
| 3254 | mArrayEqualityFunctions.push_back(function); |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3255 | mEqualityFunctions.push_back(function); |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3256 | |
Olli Etuaho | ae37a5c | 2015-03-20 16:50:15 +0200 | [diff] [blame] | 3257 | return function->functionName; |
Olli Etuaho | 7fb4955 | 2015-03-18 17:27:44 +0200 | [diff] [blame] | 3258 | } |
| 3259 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3260 | TString OutputHLSL::addArrayAssignmentFunction(const TType &type) |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3261 | { |
| 3262 | for (const auto &assignFunction : mArrayAssignmentFunctions) |
| 3263 | { |
| 3264 | if (assignFunction.type == type) |
| 3265 | { |
| 3266 | return assignFunction.functionName; |
| 3267 | } |
| 3268 | } |
| 3269 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3270 | TType elementType(type); |
| 3271 | elementType.toArrayElementType(); |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3272 | |
| 3273 | ArrayHelperFunction function; |
| 3274 | function.type = type; |
| 3275 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3276 | function.functionName = ArrayHelperFunctionName("angle_assign", type); |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3277 | |
| 3278 | TInfoSinkBase fnOut; |
| 3279 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3280 | const TString &typeName = TypeString(type); |
| 3281 | fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type) |
| 3282 | << ", " << typeName << " b" << ArrayString(type) << ")\n" |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3283 | << "{\n" |
| 3284 | " for (int i = 0; i < " |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3285 | << type.getOutermostArraySize() |
| 3286 | << "; ++i)\n" |
| 3287 | " {\n" |
| 3288 | " "; |
| 3289 | |
| 3290 | outputAssign(PreVisit, elementType, fnOut); |
| 3291 | fnOut << "a[i]"; |
| 3292 | outputAssign(InVisit, elementType, fnOut); |
| 3293 | fnOut << "b[i]"; |
| 3294 | outputAssign(PostVisit, elementType, fnOut); |
| 3295 | |
| 3296 | fnOut << ";\n" |
| 3297 | " }\n" |
| 3298 | "}\n"; |
Olli Etuaho | 1269076 | 2015-03-31 12:55:28 +0300 | [diff] [blame] | 3299 | |
| 3300 | function.functionDefinition = fnOut.c_str(); |
| 3301 | |
| 3302 | mArrayAssignmentFunctions.push_back(function); |
| 3303 | |
| 3304 | return function.functionName; |
| 3305 | } |
| 3306 | |
Jamie Madill | d7b1ab5 | 2016-12-12 14:42:19 -0500 | [diff] [blame] | 3307 | TString OutputHLSL::addArrayConstructIntoFunction(const TType &type) |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3308 | { |
| 3309 | for (const auto &constructIntoFunction : mArrayConstructIntoFunctions) |
| 3310 | { |
| 3311 | if (constructIntoFunction.type == type) |
| 3312 | { |
| 3313 | return constructIntoFunction.functionName; |
| 3314 | } |
| 3315 | } |
| 3316 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3317 | TType elementType(type); |
| 3318 | elementType.toArrayElementType(); |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3319 | |
| 3320 | ArrayHelperFunction function; |
| 3321 | function.type = type; |
| 3322 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3323 | function.functionName = ArrayHelperFunctionName("angle_construct_into", type); |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3324 | |
| 3325 | TInfoSinkBase fnOut; |
| 3326 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3327 | const TString &typeName = TypeString(type); |
| 3328 | fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type); |
| 3329 | for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i) |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3330 | { |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3331 | fnOut << ", " << typeName << " b" << i << ArrayString(elementType); |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3332 | } |
| 3333 | fnOut << ")\n" |
| 3334 | "{\n"; |
| 3335 | |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3336 | for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i) |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3337 | { |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 3338 | fnOut << " "; |
| 3339 | outputAssign(PreVisit, elementType, fnOut); |
| 3340 | fnOut << "a[" << i << "]"; |
| 3341 | outputAssign(InVisit, elementType, fnOut); |
| 3342 | fnOut << "b" << i; |
| 3343 | outputAssign(PostVisit, elementType, fnOut); |
| 3344 | fnOut << ";\n"; |
Olli Etuaho | 9638c35 | 2015-04-01 14:34:52 +0300 | [diff] [blame] | 3345 | } |
| 3346 | fnOut << "}\n"; |
| 3347 | |
| 3348 | function.functionDefinition = fnOut.c_str(); |
| 3349 | |
| 3350 | mArrayConstructIntoFunctions.push_back(function); |
| 3351 | |
| 3352 | return function.functionName; |
| 3353 | } |
| 3354 | |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 3355 | void OutputHLSL::ensureStructDefined(const TType &type) |
| 3356 | { |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 3357 | const TStructure *structure = type.getStruct(); |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 3358 | if (structure) |
| 3359 | { |
Olli Etuaho | bd3cd50 | 2017-11-03 15:48:52 +0200 | [diff] [blame] | 3360 | ASSERT(type.getBasicType() == EbtStruct); |
| 3361 | mStructureHLSL->ensureStructDefined(*structure); |
Jamie Madill | 2e295e2 | 2015-04-29 10:41:33 -0400 | [diff] [blame] | 3362 | } |
| 3363 | } |
| 3364 | |
Olli Etuaho | 06235df | 2018-07-20 14:26:07 +0300 | [diff] [blame] | 3365 | bool OutputHLSL::shaderNeedsGenerateOutput() const |
| 3366 | { |
| 3367 | return mShaderType == GL_VERTEX_SHADER || mShaderType == GL_FRAGMENT_SHADER; |
| 3368 | } |
| 3369 | |
| 3370 | const char *OutputHLSL::generateOutputCall() const |
| 3371 | { |
| 3372 | if (mShaderType == GL_VERTEX_SHADER) |
| 3373 | { |
| 3374 | return "generateOutput(input)"; |
| 3375 | } |
| 3376 | else |
| 3377 | { |
| 3378 | return "generateOutput()"; |
| 3379 | } |
| 3380 | } |
| 3381 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 3382 | } // namespace sh |