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