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