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