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