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