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