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