blob: 121dd11b78f2468bab2ba00836ca3165edd10656 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +08009#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000010#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000011#include <cfloat>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Xinghua Cao711b7a12017-10-09 13:38:12 +080018#include "compiler/translator/ImageFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#include "compiler/translator/InfoSink.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050020#include "compiler/translator/StructureHLSL.h"
Olli Etuaho5858f7e2016-04-08 13:08:46 +030021#include "compiler/translator/TextureFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050023#include "compiler/translator/UniformHLSL.h"
24#include "compiler/translator/UtilsHLSL.h"
25#include "compiler/translator/blocklayout.h"
Olli Etuahoa07b4212018-03-22 16:13:13 +020026#include "compiler/translator/tree_ops/RemoveSwitchFallThrough.h"
Olli Etuahoc26214d2018-03-16 10:43:11 +020027#include "compiler/translator/tree_util/FindSymbolNode.h"
28#include "compiler/translator/tree_util/NodeSearch.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050029#include "compiler/translator/util.h"
30
Jamie Madill45bcc782016-11-07 13:58:48 -050031namespace sh
32{
33
Olli Etuaho96f6adf2017-08-16 11:18:54 +030034namespace
35{
36
37TString ArrayHelperFunctionName(const char *prefix, const TType &type)
38{
39 TStringStream fnName;
40 fnName << prefix << "_";
Kai Ninomiya57ea5332017-11-22 14:04:48 -080041 if (type.isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +030042 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -080043 for (unsigned int arraySize : *type.getArraySizes())
44 {
45 fnName << arraySize << "_";
46 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +030047 }
48 fnName << TypeString(type);
49 return fnName.str();
50}
51
Olli Etuaho40dbdd62017-10-13 13:34:19 +030052bool IsDeclarationWrittenOut(TIntermDeclaration *node)
53{
54 TIntermSequence *sequence = node->getSequence();
55 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
56 ASSERT(sequence->size() == 1);
57 ASSERT(variable);
58 return (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +080059 variable->getQualifier() == EvqConst || variable->getQualifier() == EvqShared);
Olli Etuaho40dbdd62017-10-13 13:34:19 +030060}
61
Olli Etuaho2ef23e22017-11-01 16:39:11 +020062bool 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 Etuaho96f6adf2017-08-16 11:18:54 +030083} // anonymous namespace
84
Olli Etuahoc71862a2017-12-21 12:58:29 +020085TReferencedBlock::TReferencedBlock(const TInterfaceBlock *aBlock,
86 const TVariable *aInstanceVariable)
87 : block(aBlock), instanceVariable(aInstanceVariable)
88{
89}
90
Olli Etuaho56a2f952016-12-08 12:16:27 +000091void OutputHLSL::writeFloat(TInfoSinkBase &out, float f)
Olli Etuaho4785fec2015-05-18 16:09:37 +030092{
Olli Etuaho56a2f952016-12-08 12:16:27 +000093 // 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 Etuaho4785fec2015-05-18 16:09:37 +0300105
Olli Etuaho56a2f952016-12-08 12:16:27 +0000106void OutputHLSL::writeSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200107{
108 ASSERT(constUnion != nullptr);
109 switch (constUnion->getType())
110 {
111 case EbtFloat:
Olli Etuaho56a2f952016-12-08 12:16:27 +0000112 writeFloat(out, constUnion->getFConst());
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200113 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 Etuaho56a2f952016-12-08 12:16:27 +0000128const TConstantUnion *OutputHLSL::writeConstantUnionArray(TInfoSinkBase &out,
129 const TConstantUnion *const constUnion,
130 const size_t size)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200131{
132 const TConstantUnion *constUnionIterated = constUnion;
133 for (size_t i = 0; i < size; i++, constUnionIterated++)
134 {
Olli Etuaho56a2f952016-12-08 12:16:27 +0000135 writeSingleConstant(out, constUnionIterated);
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200136
137 if (i != size - 1)
138 {
139 out << ", ";
140 }
141 }
142 return constUnionIterated;
143}
144
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800145OutputHLSL::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 Etuaho2d88e9b2017-07-21 16:52:03 +0300152 ShCompileOptions compileOptions,
Olli Etuaho89a69a02017-10-23 12:20:45 +0300153 TSymbolTable *symbolTable,
154 PerformanceDiagnostics *perfDiagnostics)
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300155 : TIntermTraverser(true, true, true, symbolTable),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200156 mShaderType(shaderType),
157 mShaderVersion(shaderVersion),
158 mExtensionBehavior(extensionBehavior),
159 mSourcePath(sourcePath),
160 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700161 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000162 mNumRenderTargets(numRenderTargets),
Olli Etuaho89a69a02017-10-23 12:20:45 +0300163 mCurrentFunctionMetadata(nullptr),
164 mPerfDiagnostics(perfDiagnostics)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000165{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000166 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000167
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +0800168 mUsesFragColor = false;
169 mUsesFragData = false;
170 mUsesDepthRange = false;
171 mUsesFragCoord = false;
172 mUsesPointCoord = false;
173 mUsesFrontFacing = false;
174 mUsesPointSize = false;
175 mUsesInstanceID = false;
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300176 mHasMultiviewExtensionEnabled =
177 IsExtensionEnabled(mExtensionBehavior, TExtension::OVR_multiview);
Martin Radev41ac68e2017-06-06 12:16:58 +0300178 mUsesViewID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500179 mUsesVertexID = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500180 mUsesFragDepth = false;
Xinghua Caob1239382016-12-13 15:07:05 +0800181 mUsesNumWorkGroups = false;
182 mUsesWorkGroupID = false;
183 mUsesLocalInvocationID = false;
184 mUsesGlobalInvocationID = false;
185 mUsesLocalInvocationIndex = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500186 mUsesXor = false;
187 mUsesDiscardRewriting = false;
188 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530189 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000190
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000191 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000192
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500193 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000194 mInsideDiscontinuousLoop = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500195 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000196
Yunchao Hed7297bf2017-04-19 15:27:10 +0800197 mExcessiveLoopIndex = nullptr;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000198
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500199 mStructureHLSL = new StructureHLSL;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300200 mTextureFunctionHLSL = new TextureFunctionHLSL;
Xinghua Cao711b7a12017-10-09 13:38:12 +0800201 mImageFunctionHLSL = new ImageFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400202
Olli Etuahod8724a92017-12-29 18:40:36 +0200203 unsigned int firstUniformRegister =
204 ((compileOptions & SH_SKIP_D3D_CONSTANT_REGISTER_ZERO) != 0) ? 1u : 0u;
Xinghua Cao06a22622018-05-18 16:48:41 +0800205 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms, firstUniformRegister);
Olli Etuahod8724a92017-12-29 18:40:36 +0200206
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200207 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000208 {
Arun Patole63419392015-03-13 11:51:07 +0530209 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500210 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
211 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530212 // In both cases total 3 uniform registers need to be reserved.
213 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000214 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000215
Geoff Lang00140f42016-02-03 18:47:33 +0000216 // Reserve registers for the default uniform block and driver constants
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800217 mUniformHLSL->reserveUniformBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218}
219
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000220OutputHLSL::~OutputHLSL()
221{
Jamie Madill8daaba12014-06-13 10:04:33 -0400222 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400223 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300224 SafeDelete(mTextureFunctionHLSL);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800225 SafeDelete(mImageFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200226 for (auto &eqFunction : mStructEqualityFunctions)
227 {
228 SafeDelete(eqFunction);
229 }
230 for (auto &eqFunction : mArrayEqualityFunctions)
231 {
232 SafeDelete(eqFunction);
233 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000234}
235
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200236void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000237{
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200238 BuiltInFunctionEmulator builtInFunctionEmulator;
239 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800240 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
241 {
242 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
243 mShaderVersion);
244 }
245
Olli Etuahodfa75e82017-01-23 09:43:06 -0800246 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500247
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700248 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000249 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700250 ASSERT(success == CallDAG::INITDAG_SUCCESS);
251 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700252
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200253 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 Madill37997142015-01-28 10:06:34 -0500258 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500259 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200260 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500261 mInfoSinkStack.pop();
262
Jamie Madill37997142015-01-28 10:06:34 -0500263 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500264 mInfoSinkStack.pop();
265
Jamie Madill32aab012015-01-27 14:12:26 -0500266 mInfoSinkStack.push(&mHeader);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200267 header(mHeader, std140Structs, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500268 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000269
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200270 objSink << mHeader.c_str();
271 objSink << mBody.c_str();
272 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200273
Olli Etuahodfa75e82017-01-23 09:43:06 -0800274 builtInFunctionEmulator.cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000275}
276
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800277const std::map<std::string, unsigned int> &OutputHLSL::getUniformBlockRegisterMap() const
Jamie Madill4e1fd412014-07-10 17:50:10 -0400278{
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800279 return mUniformHLSL->getUniformBlockRegisterMap();
Jamie Madill4e1fd412014-07-10 17:50:10 -0400280}
281
Jamie Madill9fe25e92014-07-18 10:33:08 -0400282const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
283{
284 return mUniformHLSL->getUniformRegisterMap();
285}
286
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200287TString OutputHLSL::structInitializerString(int indent,
288 const TType &type,
289 const TString &name) const
Jamie Madill570e04d2013-06-21 09:15:33 -0400290{
291 TString init;
292
Olli Etuahoed049ab2017-06-30 17:38:33 +0300293 TString indentString;
294 for (int spaces = 0; spaces < indent; spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400295 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300296 indentString += " ";
Jamie Madill570e04d2013-06-21 09:15:33 -0400297 }
298
Olli Etuahoed049ab2017-06-30 17:38:33 +0300299 if (type.isArray())
Jamie Madill570e04d2013-06-21 09:15:33 -0400300 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300301 init += indentString + "{\n";
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300302 for (unsigned int arrayIndex = 0u; arrayIndex < type.getOutermostArraySize(); ++arrayIndex)
Jamie Madill570e04d2013-06-21 09:15:33 -0400303 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300304 TStringStream indexedString;
305 indexedString << name << "[" << arrayIndex << "]";
306 TType elementType = type;
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300307 elementType.toArrayElementType();
Olli Etuahoed049ab2017-06-30 17:38:33 +0300308 init += structInitializerString(indent + 1, elementType, indexedString.str());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300309 if (arrayIndex < type.getOutermostArraySize() - 1)
Olli Etuahoed049ab2017-06-30 17:38:33 +0300310 {
311 init += ",";
312 }
313 init += "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400314 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300315 init += indentString + "}";
Jamie Madill570e04d2013-06-21 09:15:33 -0400316 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300317 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 Madill570e04d2013-06-21 09:15:33 -0400327
Olli Etuahoed049ab2017-06-30 17:38:33 +0300328 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 Madill570e04d2013-06-21 09:15:33 -0400341
342 return init;
343}
344
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200345TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std140Structs) const
346{
347 TString mappedStructs;
348
349 for (auto &mappedStruct : std140Structs)
350 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200351 const TInterfaceBlock *interfaceBlock =
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200352 mappedStruct.blockDeclarator->getType().getInterfaceBlock();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200353 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200354 {
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 Etuaho8b5e8fd2017-12-15 14:59:15 +0200371 if (mappedStruct.blockDeclarator->variable().symbolType() != SymbolType::Empty)
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200372 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200373 const ImmutableString &instanceName =
374 mappedStruct.blockDeclarator->variable().name();
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200375 unsigned int instanceStringArrayIndex = GL_INVALID_INDEX;
376 if (isInstanceArray)
377 instanceStringArrayIndex = instanceArrayIndex;
Olli Etuaho12a18ad2017-12-01 16:59:47 +0200378 TString instanceString = mUniformHLSL->UniformBlockInstanceString(
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200379 instanceName, instanceStringArrayIndex);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200380 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 Etuahobed35d72017-12-20 16:36:26 +0200392 "static " + Decorate(structType->getStruct()->name()) + " " + mappedName;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200393
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
407void OutputHLSL::header(TInfoSinkBase &out,
408 const std::vector<MappedStruct> &std140Structs,
409 const BuiltInFunctionEmulator *builtInFunctionEmulator) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000411 TString varyings;
412 TString attributes;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200413 TString mappedStructs = generateStructMapping(std140Structs);
Jamie Madill570e04d2013-06-21 09:15:33 -0400414
Olli Etuahob8cb9392017-12-20 14:23:19 +0200415 for (const auto &varying : mReferencedVaryings)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000416 {
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +0800417 const TType &type = varying.second->getType();
Olli Etuahofbb1c792018-01-19 16:26:59 +0200418 const ImmutableString &name = varying.second->name();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000419
420 // Program linking depends on this exact format
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +0800421 varyings += TString("static ") + InterpolationString(type.getQualifier()) + " " +
422 TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " +
423 zeroInitializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000424 }
425
Olli Etuaho93b059d2017-12-20 12:46:58 +0200426 for (const auto &attribute : mReferencedAttributes)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000427 {
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +0800428 const TType &type = attribute.second->getType();
Olli Etuahofbb1c792018-01-19 16:26:59 +0200429 const ImmutableString &name = attribute.second->name();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000430
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500431 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200432 " = " + zeroInitializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000433 }
434
Jamie Madill8daaba12014-06-13 10:04:33 -0400435 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400436
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300437 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms, mSymbolTable);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800438 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400439
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200440 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500441 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200442 out << "\n// Equality functions\n\n";
443 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500444 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200445 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200446 }
447 }
Olli Etuaho12690762015-03-31 12:55:28 +0300448 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 Etuaho9638c352015-04-01 14:34:52 +0300456 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 Etuaho7fb49552015-03-18 17:27:44 +0200464
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500465 if (mUsesDiscardRewriting)
466 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400467 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500468 }
469
Nicolas Capens655fe362014-04-11 13:12:34 -0400470 if (mUsesNestedBreak)
471 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400472 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400473 }
474
Arun Patole44efa0b2015-03-04 17:11:05 +0530475 if (mRequiresIEEEStrictCompiling)
476 {
477 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
478 }
479
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400480 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 Etuahoa3a5cc62015-02-13 13:12:22 +0200488 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300490 const bool usingMRTExtension =
491 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000492
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000493 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500494 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400495 out << "\n";
496
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200497 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000498 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200499 for (const auto &outputVariable : mReferencedOutputVariables)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000500 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200501 const ImmutableString &variableName = outputVariable.second->name();
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +0800502 const TType &variableType = outputVariable.second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400503
Olli Etuahofbb1c792018-01-19 16:26:59 +0200504 out << "static " << TypeString(variableType) << " out_" << variableName
505 << ArrayString(variableType) << " = " << zeroInitializer(variableType) << ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000506 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000507 }
Jamie Madill46131a32013-06-20 11:55:50 -0400508 else
509 {
510 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
511
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +0800512 out << "static float4 gl_Color[" << numColorValues
513 << "] =\n"
514 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400515 for (unsigned int i = 0; i < numColorValues; i++)
516 {
517 out << " float4(0, 0, 0, 0)";
518 if (i + 1 != numColorValues)
519 {
520 out << ",";
521 }
522 out << "\n";
523 }
524
525 out << "};\n";
526 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000527
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400528 if (mUsesFragDepth)
529 {
530 out << "static float gl_Depth = 0.0;\n";
531 }
532
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000533 if (mUsesFragCoord)
534 {
535 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
536 }
537
538 if (mUsesPointCoord)
539 {
540 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
541 }
542
543 if (mUsesFrontFacing)
544 {
545 out << "static bool gl_FrontFacing = false;\n";
546 }
547
548 out << "\n";
549
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000550 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000551 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000552 out << "struct gl_DepthRangeParameters\n"
553 "{\n"
554 " float near;\n"
555 " float far;\n"
556 " float diff;\n"
557 "};\n"
558 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000559 }
560
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200561 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000562 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000563 out << "cbuffer DriverConstants : register(b1)\n"
564 "{\n";
565
566 if (mUsesDepthRange)
567 {
568 out << " float3 dx_DepthRange : packoffset(c0);\n";
569 }
570
571 if (mUsesFragCoord)
572 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000573 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000574 }
575
576 if (mUsesFragCoord || mUsesFrontFacing)
577 {
578 out << " float3 dx_DepthFront : packoffset(c2);\n";
579 }
580
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800581 if (mUsesFragCoord)
582 {
583 // dx_ViewScale is only used in the fragment shader to correct
584 // the value for glFragCoord if necessary
585 out << " float2 dx_ViewScale : packoffset(c3);\n";
586 }
587
Martin Radev72b4e1e2017-08-31 15:42:56 +0300588 if (mHasMultiviewExtensionEnabled)
589 {
590 // We have to add a value which we can use to keep track of which multi-view code
591 // path is to be selected in the GS.
592 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
593 }
594
Olli Etuaho618bebc2016-01-15 16:40:00 +0200595 if (mOutputType == SH_HLSL_4_1_OUTPUT)
596 {
597 mUniformHLSL->samplerMetadataUniforms(out, "c4");
598 }
599
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000600 out << "};\n";
601 }
602 else
603 {
604 if (mUsesDepthRange)
605 {
606 out << "uniform float3 dx_DepthRange : register(c0);";
607 }
608
609 if (mUsesFragCoord)
610 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000611 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000612 }
613
614 if (mUsesFragCoord || mUsesFrontFacing)
615 {
616 out << "uniform float3 dx_DepthFront : register(c2);\n";
617 }
618 }
619
620 out << "\n";
621
622 if (mUsesDepthRange)
623 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500624 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
625 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000626 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000627 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000628
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000629 if (usingMRTExtension && mNumRenderTargets > 1)
630 {
631 out << "#define GL_USES_MRT\n";
632 }
633
634 if (mUsesFragColor)
635 {
636 out << "#define GL_USES_FRAG_COLOR\n";
637 }
638
639 if (mUsesFragData)
640 {
641 out << "#define GL_USES_FRAG_DATA\n";
642 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643 }
Xinghua Caob1239382016-12-13 15:07:05 +0800644 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000646 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500647 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000648 out << "\n"
649 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400650
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000651 if (mUsesPointSize)
652 {
653 out << "static float gl_PointSize = float(1);\n";
654 }
655
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000656 if (mUsesInstanceID)
657 {
658 out << "static int gl_InstanceID;";
659 }
660
Corentin Wallezb076add2016-01-11 16:45:46 -0500661 if (mUsesVertexID)
662 {
663 out << "static int gl_VertexID;";
664 }
665
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000666 out << "\n"
667 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500668 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000669 out << "\n";
670
671 if (mUsesDepthRange)
672 {
673 out << "struct gl_DepthRangeParameters\n"
674 "{\n"
675 " float near;\n"
676 " float far;\n"
677 " float diff;\n"
678 "};\n"
679 "\n";
680 }
681
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200682 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000683 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800684 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500685 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800686
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000687 if (mUsesDepthRange)
688 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800689 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000690 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800691
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800692 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
693 // shaders. However, we declare it for all shaders (including Feature Level 10+).
694 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
695 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800696 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800697 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800698 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800699
Martin Radev72b4e1e2017-08-31 15:42:56 +0300700 if (mHasMultiviewExtensionEnabled)
701 {
702 // We have to add a value which we can use to keep track of which multi-view code
703 // path is to be selected in the GS.
704 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
705 }
706
Olli Etuaho618bebc2016-01-15 16:40:00 +0200707 if (mOutputType == SH_HLSL_4_1_OUTPUT)
708 {
709 mUniformHLSL->samplerMetadataUniforms(out, "c4");
710 }
711
Austin Kinross4fd18b12014-12-22 12:32:05 -0800712 out << "};\n"
713 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000714 }
715 else
716 {
717 if (mUsesDepthRange)
718 {
719 out << "uniform float3 dx_DepthRange : register(c0);\n";
720 }
721
Cooper Partine6664f02015-01-09 16:22:24 -0800722 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
723 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000724 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000725 }
726
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000727 if (mUsesDepthRange)
728 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500729 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
730 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000731 "\n";
732 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400733 }
Xinghua Caob1239382016-12-13 15:07:05 +0800734 else // Compute shader
735 {
736 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800737
738 out << "cbuffer DriverConstants : register(b1)\n"
739 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800740 if (mUsesNumWorkGroups)
741 {
Xinghua Caob1239382016-12-13 15:07:05 +0800742 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800743 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800744 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
745 mUniformHLSL->samplerMetadataUniforms(out, "c1");
746 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800747
748 // Follow built-in variables would be initialized in
749 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
750 // are used in compute shader.
751 if (mUsesWorkGroupID)
752 {
753 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
754 }
755
756 if (mUsesLocalInvocationID)
757 {
758 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
759 }
760
761 if (mUsesGlobalInvocationID)
762 {
763 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
764 }
765
766 if (mUsesLocalInvocationIndex)
767 {
768 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
769 }
770 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000771
Qin Jiajia2a12b3d2018-05-23 13:42:13 +0800772 if (!mappedStructs.empty())
773 {
774 out << "// Structures from std140 blocks with padding removed\n";
775 out << "\n";
776 out << mappedStructs;
777 out << "\n";
778 }
779
Geoff Lang1fe74c72016-08-25 13:23:01 -0400780 bool getDimensionsIgnoresBaseLevel =
781 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
782 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800783 mImageFunctionHLSL->imageFunctionHeader(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000785 if (mUsesFragCoord)
786 {
787 out << "#define GL_USES_FRAG_COORD\n";
788 }
789
790 if (mUsesPointCoord)
791 {
792 out << "#define GL_USES_POINT_COORD\n";
793 }
794
795 if (mUsesFrontFacing)
796 {
797 out << "#define GL_USES_FRONT_FACING\n";
798 }
799
800 if (mUsesPointSize)
801 {
802 out << "#define GL_USES_POINT_SIZE\n";
803 }
804
Martin Radev41ac68e2017-06-06 12:16:58 +0300805 if (mHasMultiviewExtensionEnabled)
806 {
807 out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n";
808 }
809
810 if (mUsesViewID)
811 {
812 out << "#define GL_USES_VIEW_ID\n";
813 }
814
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400815 if (mUsesFragDepth)
816 {
817 out << "#define GL_USES_FRAG_DEPTH\n";
818 }
819
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000820 if (mUsesDepthRange)
821 {
822 out << "#define GL_USES_DEPTH_RANGE\n";
823 }
824
Xinghua Caob1239382016-12-13 15:07:05 +0800825 if (mUsesNumWorkGroups)
826 {
827 out << "#define GL_USES_NUM_WORK_GROUPS\n";
828 }
829
830 if (mUsesWorkGroupID)
831 {
832 out << "#define GL_USES_WORK_GROUP_ID\n";
833 }
834
835 if (mUsesLocalInvocationID)
836 {
837 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
838 }
839
840 if (mUsesGlobalInvocationID)
841 {
842 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
843 }
844
845 if (mUsesLocalInvocationIndex)
846 {
847 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
848 }
849
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000850 if (mUsesXor)
851 {
852 out << "bool xor(bool p, bool q)\n"
853 "{\n"
854 " return (p || q) && !(p && q);\n"
855 "}\n"
856 "\n";
857 }
858
Olli Etuahodfa75e82017-01-23 09:43:06 -0800859 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void OutputHLSL::visitSymbol(TIntermSymbol *node)
863{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200864 const TVariable &variable = node->variable();
865
866 // Empty symbols can only appear in declarations and function arguments, and in either of those
867 // cases the symbol nodes are not visited.
868 ASSERT(variable.symbolType() != SymbolType::Empty);
869
Jamie Madill32aab012015-01-27 14:12:26 -0500870 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871
Jamie Madill570e04d2013-06-21 09:15:33 -0400872 // Handle accessing std140 structs by value
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200873 if (IsInStd140InterfaceBlock(node) && node->getBasicType() == EbtStruct)
Jamie Madill570e04d2013-06-21 09:15:33 -0400874 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200875 out << "map";
Jamie Madill570e04d2013-06-21 09:15:33 -0400876 }
877
Olli Etuahofbb1c792018-01-19 16:26:59 +0200878 const ImmutableString &name = variable.name();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200879 const TSymbolUniqueId &uniqueId = variable.uniqueId();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200880
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000881 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000882 {
883 mUsesDepthRange = true;
884 out << name;
885 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886 else
887 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200888 const TType &variableType = variable.getType();
889 TQualifier qualifier = variable.getType().getQualifier();
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000890
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200891 ensureStructDefined(variableType);
Olli Etuahobd3cd502017-11-03 15:48:52 +0200892
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000893 if (qualifier == EvqUniform)
894 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200895 const TInterfaceBlock *interfaceBlock = variableType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400896
897 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000898 {
Olli Etuahoc71862a2017-12-21 12:58:29 +0200899 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
900 {
901 const TVariable *instanceVariable = nullptr;
902 if (variableType.isInterfaceBlock())
903 {
904 instanceVariable = &variable;
905 }
906 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
907 new TReferencedBlock(interfaceBlock, instanceVariable);
908 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000909 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000910 else
911 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200912 mReferencedUniforms[uniqueId.get()] = &variable;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000913 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400914
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200915 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000916 }
Jamie Madill19571812013-08-12 15:26:34 -0700917 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000918 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200919 mReferencedAttributes[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400920 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000921 }
Jamie Madill033dae62014-06-18 12:56:28 -0400922 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000923 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200924 mReferencedVaryings[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400925 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300926 if (name == "ViewID_OVR")
927 {
928 mUsesViewID = true;
929 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000930 }
Jamie Madill19571812013-08-12 15:26:34 -0700931 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400932 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200933 mReferencedOutputVariables[uniqueId.get()] = &variable;
Jamie Madill46131a32013-06-20 11:55:50 -0400934 out << "out_" << name;
935 }
936 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000937 {
938 out << "gl_Color[0]";
939 mUsesFragColor = true;
940 }
941 else if (qualifier == EvqFragData)
942 {
943 out << "gl_Color";
944 mUsesFragData = true;
945 }
946 else if (qualifier == EvqFragCoord)
947 {
948 mUsesFragCoord = true;
949 out << name;
950 }
951 else if (qualifier == EvqPointCoord)
952 {
953 mUsesPointCoord = true;
954 out << name;
955 }
956 else if (qualifier == EvqFrontFacing)
957 {
958 mUsesFrontFacing = true;
959 out << name;
960 }
961 else if (qualifier == EvqPointSize)
962 {
963 mUsesPointSize = true;
964 out << name;
965 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000966 else if (qualifier == EvqInstanceID)
967 {
968 mUsesInstanceID = true;
969 out << name;
970 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500971 else if (qualifier == EvqVertexID)
972 {
973 mUsesVertexID = true;
974 out << name;
975 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300976 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400977 {
978 mUsesFragDepth = true;
979 out << "gl_Depth";
980 }
Xinghua Caob1239382016-12-13 15:07:05 +0800981 else if (qualifier == EvqNumWorkGroups)
982 {
983 mUsesNumWorkGroups = true;
984 out << name;
985 }
986 else if (qualifier == EvqWorkGroupID)
987 {
988 mUsesWorkGroupID = true;
989 out << name;
990 }
991 else if (qualifier == EvqLocalInvocationID)
992 {
993 mUsesLocalInvocationID = true;
994 out << name;
995 }
996 else if (qualifier == EvqGlobalInvocationID)
997 {
998 mUsesGlobalInvocationID = true;
999 out << name;
1000 }
1001 else if (qualifier == EvqLocalInvocationIndex)
1002 {
1003 mUsesLocalInvocationIndex = true;
1004 out << name;
1005 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001006 else
1007 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001008 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001009 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010 }
1011}
1012
Olli Etuaho7fb49552015-03-18 17:27:44 +02001013void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1014{
1015 if (type.isScalar() && !type.isArray())
1016 {
1017 if (op == EOpEqual)
1018 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001019 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001020 }
1021 else
1022 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001023 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001024 }
1025 }
1026 else
1027 {
1028 if (visit == PreVisit && op == EOpNotEqual)
1029 {
1030 out << "!";
1031 }
1032
1033 if (type.isArray())
1034 {
1035 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001036 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001037 }
1038 else if (type.getBasicType() == EbtStruct)
1039 {
1040 const TStructure &structure = *type.getStruct();
1041 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001042 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001043 }
1044 else
1045 {
1046 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001047 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001048 }
1049 }
1050}
1051
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001052void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1053{
1054 if (type.isArray())
1055 {
1056 const TString &functionName = addArrayAssignmentFunction(type);
1057 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1058 }
1059 else
1060 {
1061 outputTriplet(out, visit, "(", " = ", ")");
1062 }
1063}
1064
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001065bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001066{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001067 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001068 {
1069 TIntermNode *ancestor = getAncestorNode(n);
1070 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1071 if (ancestorBinary == nullptr)
1072 {
1073 return false;
1074 }
1075 switch (ancestorBinary->getOp())
1076 {
1077 case EOpIndexDirectStruct:
1078 {
1079 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1080 const TIntermConstantUnion *index =
1081 ancestorBinary->getRight()->getAsConstantUnion();
1082 const TField *field = structure->fields()[index->getIConst(0)];
1083 if (IsSampler(field->type()->getBasicType()))
1084 {
1085 return true;
1086 }
1087 break;
1088 }
1089 case EOpIndexDirect:
1090 break;
1091 default:
1092 // Returning a sampler from indirect indexing is not supported.
1093 return false;
1094 }
1095 }
1096 return false;
1097}
1098
Olli Etuahob6fa0432016-09-28 16:28:05 +01001099bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1100{
1101 TInfoSinkBase &out = getInfoSink();
1102 if (visit == PostVisit)
1103 {
1104 out << ".";
1105 node->writeOffsetsAsXYZW(&out);
1106 }
1107 return true;
1108}
1109
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1111{
Jamie Madill32aab012015-01-27 14:12:26 -05001112 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113
1114 switch (node->getOp())
1115 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001116 case EOpComma:
1117 outputTriplet(out, visit, "(", ", ", ")");
1118 break;
1119 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001120 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001121 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001122 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1123 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001124 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001125 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1126 out << functionName << "(";
1127 node->getLeft()->traverse(this);
1128 TIntermSequence *seq = rightAgg->getSequence();
1129 for (auto &arrayElement : *seq)
1130 {
1131 out << ", ";
1132 arrayElement->traverse(this);
1133 }
1134 out << ")";
1135 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001136 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001137 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1138 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001139 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001140 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001141 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001142 break;
1143 case EOpInitialize:
1144 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001145 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001146 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1147 ASSERT(symbolNode);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001148 TIntermTyped *initializer = node->getRight();
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001149
1150 // Global initializers must be constant at this point.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001151 ASSERT(symbolNode->getQualifier() != EvqGlobal || initializer->hasConstantValue());
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001152
1153 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1154 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1155 // new variable is created before the assignment is evaluated), so we need to
1156 // convert
1157 // this to "float t = x, x = t;".
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001158 if (writeSameSymbolInitializer(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001159 {
1160 // Skip initializing the rest of the expression
1161 return false;
1162 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001163 else if (writeConstantInitialization(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001164 {
1165 return false;
1166 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001167 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001168 else if (visit == InVisit)
1169 {
1170 out << " = ";
1171 }
1172 break;
1173 case EOpAddAssign:
1174 outputTriplet(out, visit, "(", " += ", ")");
1175 break;
1176 case EOpSubAssign:
1177 outputTriplet(out, visit, "(", " -= ", ")");
1178 break;
1179 case EOpMulAssign:
1180 outputTriplet(out, visit, "(", " *= ", ")");
1181 break;
1182 case EOpVectorTimesScalarAssign:
1183 outputTriplet(out, visit, "(", " *= ", ")");
1184 break;
1185 case EOpMatrixTimesScalarAssign:
1186 outputTriplet(out, visit, "(", " *= ", ")");
1187 break;
1188 case EOpVectorTimesMatrixAssign:
1189 if (visit == PreVisit)
1190 {
1191 out << "(";
1192 }
1193 else if (visit == InVisit)
1194 {
1195 out << " = mul(";
1196 node->getLeft()->traverse(this);
1197 out << ", transpose(";
1198 }
1199 else
1200 {
1201 out << ")))";
1202 }
1203 break;
1204 case EOpMatrixTimesMatrixAssign:
1205 if (visit == PreVisit)
1206 {
1207 out << "(";
1208 }
1209 else if (visit == InVisit)
1210 {
1211 out << " = transpose(mul(transpose(";
1212 node->getLeft()->traverse(this);
1213 out << "), transpose(";
1214 }
1215 else
1216 {
1217 out << "))))";
1218 }
1219 break;
1220 case EOpDivAssign:
1221 outputTriplet(out, visit, "(", " /= ", ")");
1222 break;
1223 case EOpIModAssign:
1224 outputTriplet(out, visit, "(", " %= ", ")");
1225 break;
1226 case EOpBitShiftLeftAssign:
1227 outputTriplet(out, visit, "(", " <<= ", ")");
1228 break;
1229 case EOpBitShiftRightAssign:
1230 outputTriplet(out, visit, "(", " >>= ", ")");
1231 break;
1232 case EOpBitwiseAndAssign:
1233 outputTriplet(out, visit, "(", " &= ", ")");
1234 break;
1235 case EOpBitwiseXorAssign:
1236 outputTriplet(out, visit, "(", " ^= ", ")");
1237 break;
1238 case EOpBitwiseOrAssign:
1239 outputTriplet(out, visit, "(", " |= ", ")");
1240 break;
1241 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001242 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001243 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001244 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001245 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001246 if (visit == PreVisit)
1247 {
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +08001248 TIntermSymbol *instanceArraySymbol = node->getLeft()->getAsSymbolNode();
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001249 const TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Olli Etuahoc71862a2017-12-21 12:58:29 +02001250 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
1251 {
1252 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
1253 new TReferencedBlock(interfaceBlock, &instanceArraySymbol->variable());
1254 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001255 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001256 out << mUniformHLSL->UniformBlockInstanceString(instanceArraySymbol->getName(),
1257 arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001258 return false;
1259 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001260 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001261 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001262 {
1263 // All parts of an expression that access a sampler in a struct need to use _ as
1264 // separator to access the sampler variable that has been moved out of the struct.
1265 outputTriplet(out, visit, "", "_", "");
1266 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001267 else
1268 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001269 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001270 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001271 }
1272 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001273 case EOpIndexIndirect:
1274 // We do not currently support indirect references to interface blocks
1275 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1276 outputTriplet(out, visit, "", "[", "]");
1277 break;
1278 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001279 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001280 const TStructure *structure = node->getLeft()->getType().getStruct();
1281 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1282 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001283
Olli Etuaho96963162016-03-21 11:54:33 +02001284 // In cases where indexing returns a sampler, we need to access the sampler variable
1285 // that has been moved out of the struct.
1286 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1287 if (visit == PreVisit && indexingReturnsSampler)
1288 {
1289 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1290 // This prefix is only output at the beginning of the indexing expression, which
1291 // may have multiple parts.
1292 out << "angle";
1293 }
1294 if (!indexingReturnsSampler)
1295 {
1296 // All parts of an expression that access a sampler in a struct need to use _ as
1297 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001298 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001299 }
1300 if (visit == InVisit)
1301 {
1302 if (indexingReturnsSampler)
1303 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001304 out << "_" << field->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001305 }
1306 else
1307 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001308 out << "." << DecorateField(field->name(), *structure);
Olli Etuaho96963162016-03-21 11:54:33 +02001309 }
1310
1311 return false;
1312 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001313 }
1314 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001315 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001316 {
1317 bool structInStd140Block =
1318 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1319 if (visit == PreVisit && structInStd140Block)
1320 {
1321 out << "map";
1322 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001323 if (visit == InVisit)
1324 {
1325 const TInterfaceBlock *interfaceBlock =
1326 node->getLeft()->getType().getInterfaceBlock();
1327 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1328 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001329 if (structInStd140Block)
1330 {
1331 out << "_";
1332 }
1333 else
1334 {
1335 out << ".";
1336 }
1337 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001338
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001339 return false;
1340 }
1341 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001342 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001343 case EOpAdd:
1344 outputTriplet(out, visit, "(", " + ", ")");
1345 break;
1346 case EOpSub:
1347 outputTriplet(out, visit, "(", " - ", ")");
1348 break;
1349 case EOpMul:
1350 outputTriplet(out, visit, "(", " * ", ")");
1351 break;
1352 case EOpDiv:
1353 outputTriplet(out, visit, "(", " / ", ")");
1354 break;
1355 case EOpIMod:
1356 outputTriplet(out, visit, "(", " % ", ")");
1357 break;
1358 case EOpBitShiftLeft:
1359 outputTriplet(out, visit, "(", " << ", ")");
1360 break;
1361 case EOpBitShiftRight:
1362 outputTriplet(out, visit, "(", " >> ", ")");
1363 break;
1364 case EOpBitwiseAnd:
1365 outputTriplet(out, visit, "(", " & ", ")");
1366 break;
1367 case EOpBitwiseXor:
1368 outputTriplet(out, visit, "(", " ^ ", ")");
1369 break;
1370 case EOpBitwiseOr:
1371 outputTriplet(out, visit, "(", " | ", ")");
1372 break;
1373 case EOpEqual:
1374 case EOpNotEqual:
1375 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1376 break;
1377 case EOpLessThan:
1378 outputTriplet(out, visit, "(", " < ", ")");
1379 break;
1380 case EOpGreaterThan:
1381 outputTriplet(out, visit, "(", " > ", ")");
1382 break;
1383 case EOpLessThanEqual:
1384 outputTriplet(out, visit, "(", " <= ", ")");
1385 break;
1386 case EOpGreaterThanEqual:
1387 outputTriplet(out, visit, "(", " >= ", ")");
1388 break;
1389 case EOpVectorTimesScalar:
1390 outputTriplet(out, visit, "(", " * ", ")");
1391 break;
1392 case EOpMatrixTimesScalar:
1393 outputTriplet(out, visit, "(", " * ", ")");
1394 break;
1395 case EOpVectorTimesMatrix:
1396 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1397 break;
1398 case EOpMatrixTimesVector:
1399 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1400 break;
1401 case EOpMatrixTimesMatrix:
1402 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1403 break;
1404 case EOpLogicalOr:
1405 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1406 // been unfolded.
1407 ASSERT(!node->getRight()->hasSideEffects());
1408 outputTriplet(out, visit, "(", " || ", ")");
1409 return true;
1410 case EOpLogicalXor:
1411 mUsesXor = true;
1412 outputTriplet(out, visit, "xor(", ", ", ")");
1413 break;
1414 case EOpLogicalAnd:
1415 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1416 // been unfolded.
1417 ASSERT(!node->getRight()->hasSideEffects());
1418 outputTriplet(out, visit, "(", " && ", ")");
1419 return true;
1420 default:
1421 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001422 }
1423
1424 return true;
1425}
1426
1427bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1428{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001429 TInfoSinkBase &out = getInfoSink();
1430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001431 switch (node->getOp())
1432 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001433 case EOpNegative:
1434 outputTriplet(out, visit, "(-", "", ")");
1435 break;
1436 case EOpPositive:
1437 outputTriplet(out, visit, "(+", "", ")");
1438 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001439 case EOpLogicalNot:
1440 outputTriplet(out, visit, "(!", "", ")");
1441 break;
1442 case EOpBitwiseNot:
1443 outputTriplet(out, visit, "(~", "", ")");
1444 break;
1445 case EOpPostIncrement:
1446 outputTriplet(out, visit, "(", "", "++)");
1447 break;
1448 case EOpPostDecrement:
1449 outputTriplet(out, visit, "(", "", "--)");
1450 break;
1451 case EOpPreIncrement:
1452 outputTriplet(out, visit, "(++", "", ")");
1453 break;
1454 case EOpPreDecrement:
1455 outputTriplet(out, visit, "(--", "", ")");
1456 break;
1457 case EOpRadians:
1458 outputTriplet(out, visit, "radians(", "", ")");
1459 break;
1460 case EOpDegrees:
1461 outputTriplet(out, visit, "degrees(", "", ")");
1462 break;
1463 case EOpSin:
1464 outputTriplet(out, visit, "sin(", "", ")");
1465 break;
1466 case EOpCos:
1467 outputTriplet(out, visit, "cos(", "", ")");
1468 break;
1469 case EOpTan:
1470 outputTriplet(out, visit, "tan(", "", ")");
1471 break;
1472 case EOpAsin:
1473 outputTriplet(out, visit, "asin(", "", ")");
1474 break;
1475 case EOpAcos:
1476 outputTriplet(out, visit, "acos(", "", ")");
1477 break;
1478 case EOpAtan:
1479 outputTriplet(out, visit, "atan(", "", ")");
1480 break;
1481 case EOpSinh:
1482 outputTriplet(out, visit, "sinh(", "", ")");
1483 break;
1484 case EOpCosh:
1485 outputTriplet(out, visit, "cosh(", "", ")");
1486 break;
1487 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001488 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001489 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001490 case EOpAtanh:
1491 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001492 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001493 break;
1494 case EOpExp:
1495 outputTriplet(out, visit, "exp(", "", ")");
1496 break;
1497 case EOpLog:
1498 outputTriplet(out, visit, "log(", "", ")");
1499 break;
1500 case EOpExp2:
1501 outputTriplet(out, visit, "exp2(", "", ")");
1502 break;
1503 case EOpLog2:
1504 outputTriplet(out, visit, "log2(", "", ")");
1505 break;
1506 case EOpSqrt:
1507 outputTriplet(out, visit, "sqrt(", "", ")");
1508 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001509 case EOpInversesqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001510 outputTriplet(out, visit, "rsqrt(", "", ")");
1511 break;
1512 case EOpAbs:
1513 outputTriplet(out, visit, "abs(", "", ")");
1514 break;
1515 case EOpSign:
1516 outputTriplet(out, visit, "sign(", "", ")");
1517 break;
1518 case EOpFloor:
1519 outputTriplet(out, visit, "floor(", "", ")");
1520 break;
1521 case EOpTrunc:
1522 outputTriplet(out, visit, "trunc(", "", ")");
1523 break;
1524 case EOpRound:
1525 outputTriplet(out, visit, "round(", "", ")");
1526 break;
1527 case EOpRoundEven:
1528 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001529 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001530 break;
1531 case EOpCeil:
1532 outputTriplet(out, visit, "ceil(", "", ")");
1533 break;
1534 case EOpFract:
1535 outputTriplet(out, visit, "frac(", "", ")");
1536 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001537 case EOpIsnan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001538 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001539 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001540 else
1541 outputTriplet(out, visit, "isnan(", "", ")");
1542 mRequiresIEEEStrictCompiling = true;
1543 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001544 case EOpIsinf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001545 outputTriplet(out, visit, "isinf(", "", ")");
1546 break;
1547 case EOpFloatBitsToInt:
1548 outputTriplet(out, visit, "asint(", "", ")");
1549 break;
1550 case EOpFloatBitsToUint:
1551 outputTriplet(out, visit, "asuint(", "", ")");
1552 break;
1553 case EOpIntBitsToFloat:
1554 outputTriplet(out, visit, "asfloat(", "", ")");
1555 break;
1556 case EOpUintBitsToFloat:
1557 outputTriplet(out, visit, "asfloat(", "", ")");
1558 break;
1559 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001560 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001561 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001562 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001563 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001564 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001565 case EOpPackUnorm4x8:
1566 case EOpPackSnorm4x8:
1567 case EOpUnpackUnorm4x8:
1568 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001569 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001570 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001571 break;
1572 case EOpLength:
1573 outputTriplet(out, visit, "length(", "", ")");
1574 break;
1575 case EOpNormalize:
1576 outputTriplet(out, visit, "normalize(", "", ")");
1577 break;
1578 case EOpDFdx:
1579 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1580 {
1581 outputTriplet(out, visit, "(", "", ", 0.0)");
1582 }
1583 else
1584 {
1585 outputTriplet(out, visit, "ddx(", "", ")");
1586 }
1587 break;
1588 case EOpDFdy:
1589 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1590 {
1591 outputTriplet(out, visit, "(", "", ", 0.0)");
1592 }
1593 else
1594 {
1595 outputTriplet(out, visit, "ddy(", "", ")");
1596 }
1597 break;
1598 case EOpFwidth:
1599 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1600 {
1601 outputTriplet(out, visit, "(", "", ", 0.0)");
1602 }
1603 else
1604 {
1605 outputTriplet(out, visit, "fwidth(", "", ")");
1606 }
1607 break;
1608 case EOpTranspose:
1609 outputTriplet(out, visit, "transpose(", "", ")");
1610 break;
1611 case EOpDeterminant:
1612 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1613 break;
1614 case EOpInverse:
1615 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001616 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001617 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001618
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001619 case EOpAny:
1620 outputTriplet(out, visit, "any(", "", ")");
1621 break;
1622 case EOpAll:
1623 outputTriplet(out, visit, "all(", "", ")");
1624 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001625 case EOpLogicalNotComponentWise:
1626 outputTriplet(out, visit, "(!", "", ")");
1627 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001628 case EOpBitfieldReverse:
1629 outputTriplet(out, visit, "reversebits(", "", ")");
1630 break;
1631 case EOpBitCount:
1632 outputTriplet(out, visit, "countbits(", "", ")");
1633 break;
1634 case EOpFindLSB:
1635 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1636 // in GLSLTest and results are consistent with GL.
1637 outputTriplet(out, visit, "firstbitlow(", "", ")");
1638 break;
1639 case EOpFindMSB:
1640 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1641 // tested in GLSLTest and results are consistent with GL.
1642 outputTriplet(out, visit, "firstbithigh(", "", ")");
1643 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001644 default:
1645 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646 }
1647
1648 return true;
1649}
1650
Olli Etuahofbb1c792018-01-19 16:26:59 +02001651ImmutableString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
Olli Etuaho96963162016-03-21 11:54:33 +02001652{
1653 if (node->getAsSymbolNode())
1654 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001655 ASSERT(node->getAsSymbolNode()->variable().symbolType() != SymbolType::Empty);
1656 return node->getAsSymbolNode()->getName();
Olli Etuaho96963162016-03-21 11:54:33 +02001657 }
1658 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1659 switch (nodeBinary->getOp())
1660 {
1661 case EOpIndexDirect:
1662 {
1663 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1664
Olli Etuahofbb1c792018-01-19 16:26:59 +02001665 std::stringstream prefixSink;
Olli Etuaho96963162016-03-21 11:54:33 +02001666 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
Olli Etuahofbb1c792018-01-19 16:26:59 +02001667 return ImmutableString(prefixSink.str());
Olli Etuaho96963162016-03-21 11:54:33 +02001668 }
1669 case EOpIndexDirectStruct:
1670 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001671 const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001672 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1673 const TField *field = s->fields()[index];
1674
Olli Etuahofbb1c792018-01-19 16:26:59 +02001675 std::stringstream prefixSink;
Olli Etuaho96963162016-03-21 11:54:33 +02001676 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1677 << field->name();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001678 return ImmutableString(prefixSink.str());
Olli Etuaho96963162016-03-21 11:54:33 +02001679 }
1680 default:
1681 UNREACHABLE();
Jamie Madillb779b122018-06-20 11:46:43 -04001682 return kEmptyImmutableString;
Olli Etuaho96963162016-03-21 11:54:33 +02001683 }
1684}
1685
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001686bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1687{
1688 TInfoSinkBase &out = getInfoSink();
1689
1690 if (mInsideFunction)
1691 {
1692 outputLineDirective(out, node->getLine().first_line);
1693 out << "{\n";
1694 }
1695
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001696 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001697 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001698 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001699
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001700 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001701
1702 // Don't output ; after case labels, they're terminated by :
1703 // This is needed especially since outputting a ; after a case statement would turn empty
1704 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001705 // Also the output code is clearer if we don't output ; after statements where it is not
1706 // needed:
1707 // * if statements
1708 // * switch statements
1709 // * blocks
1710 // * function definitions
1711 // * loops (do-while loops output the semicolon in VisitLoop)
1712 // * declarations that don't generate output.
1713 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1714 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1715 statement->getAsSwitchNode() == nullptr &&
1716 statement->getAsFunctionDefinition() == nullptr &&
1717 (statement->getAsDeclarationNode() == nullptr ||
1718 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1719 statement->getAsInvariantDeclarationNode() == nullptr)
1720 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001721 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001722 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001723 }
1724
1725 if (mInsideFunction)
1726 {
1727 outputLineDirective(out, node->getLine().last_line);
1728 out << "}\n";
1729 }
1730
1731 return false;
1732}
1733
Olli Etuaho336b1472016-10-05 16:37:55 +01001734bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1735{
1736 TInfoSinkBase &out = getInfoSink();
1737
1738 ASSERT(mCurrentFunctionMetadata == nullptr);
1739
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001740 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho336b1472016-10-05 16:37:55 +01001741 ASSERT(index != CallDAG::InvalidIndex);
1742 mCurrentFunctionMetadata = &mASTMetadataList[index];
1743
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001744 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001745
Olli Etuahod4bd9632018-03-08 16:32:44 +02001746 const TFunction *func = node->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001747
Olli Etuahod4bd9632018-03-08 16:32:44 +02001748 if (func->isMain())
Olli Etuaho336b1472016-10-05 16:37:55 +01001749 {
1750 out << "gl_main(";
1751 }
1752 else
1753 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001754 out << DecorateFunctionIfNeeded(func) << DisambiguateFunctionName(func)
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001755 << (mOutputLod0Function ? "Lod0(" : "(");
Olli Etuaho336b1472016-10-05 16:37:55 +01001756 }
1757
Olli Etuahod4bd9632018-03-08 16:32:44 +02001758 size_t paramCount = func->getParamCount();
1759 for (unsigned int i = 0; i < paramCount; i++)
Olli Etuaho336b1472016-10-05 16:37:55 +01001760 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001761 const TVariable *param = func->getParam(i);
1762 ensureStructDefined(param->getType());
Olli Etuaho336b1472016-10-05 16:37:55 +01001763
Olli Etuahod4bd9632018-03-08 16:32:44 +02001764 writeParameter(param, out);
1765
1766 if (i < paramCount - 1)
Olli Etuaho336b1472016-10-05 16:37:55 +01001767 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001768 out << ", ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001769 }
Olli Etuaho336b1472016-10-05 16:37:55 +01001770 }
1771
1772 out << ")\n";
1773
1774 mInsideFunction = true;
1775 // The function body node will output braces.
1776 node->getBody()->traverse(this);
1777 mInsideFunction = false;
1778
1779 mCurrentFunctionMetadata = nullptr;
1780
1781 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1782 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1783 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001784 ASSERT(!node->getFunction()->isMain());
Olli Etuaho336b1472016-10-05 16:37:55 +01001785 mOutputLod0Function = true;
1786 node->traverse(this);
1787 mOutputLod0Function = false;
1788 }
1789
1790 return false;
1791}
1792
Olli Etuaho13389b62016-10-16 11:48:18 +01001793bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1794{
Olli Etuaho13389b62016-10-16 11:48:18 +01001795 if (visit == PreVisit)
1796 {
1797 TIntermSequence *sequence = node->getSequence();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001798 TIntermTyped *declarator = (*sequence)[0]->getAsTyped();
Olli Etuaho13389b62016-10-16 11:48:18 +01001799 ASSERT(sequence->size() == 1);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001800 ASSERT(declarator);
Olli Etuaho13389b62016-10-16 11:48:18 +01001801
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001802 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001803 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001804 TInfoSinkBase &out = getInfoSink();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001805 ensureStructDefined(declarator->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001806
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001807 if (!declarator->getAsSymbolNode() ||
1808 declarator->getAsSymbolNode()->variable().symbolType() !=
1809 SymbolType::Empty) // Variable declaration
Olli Etuaho13389b62016-10-16 11:48:18 +01001810 {
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +08001811 if (declarator->getQualifier() == EvqShared)
1812 {
1813 out << "groupshared ";
1814 }
1815 else if (!mInsideFunction)
Olli Etuaho13389b62016-10-16 11:48:18 +01001816 {
1817 out << "static ";
1818 }
1819
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001820 out << TypeString(declarator->getType()) + " ";
Olli Etuaho13389b62016-10-16 11:48:18 +01001821
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001822 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho13389b62016-10-16 11:48:18 +01001823
1824 if (symbol)
1825 {
1826 symbol->traverse(this);
1827 out << ArrayString(symbol->getType());
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +08001828 // We don't initialize shared variables because:
1829 // 1. It is very slow for D3D11 drivers to compile a compute shader if we add
1830 // code to initialize a groupshared array variable with a large array size.
1831 // 2. It is unnecessary to initialize shared variables, as GLSL even does not
1832 // allow initializing shared variables at all.
1833 if (declarator->getQualifier() != EvqShared)
1834 {
1835 out << " = " + zeroInitializer(symbol->getType());
1836 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001837 }
1838 else
1839 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001840 declarator->traverse(this);
Olli Etuaho13389b62016-10-16 11:48:18 +01001841 }
1842 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001843 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001844 else if (IsVaryingOut(declarator->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001845 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001846 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho282847e2017-07-12 14:11:01 +03001847 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001848
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001849 const TVariable &variable = symbol->variable();
1850
1851 if (variable.symbolType() != SymbolType::Empty)
Olli Etuaho93b059d2017-12-20 12:46:58 +02001852 {
1853 // Vertex outputs which are declared but not written to should still be declared to
1854 // allow successful linking.
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001855 mReferencedVaryings[symbol->uniqueId().get()] = &variable;
Olli Etuaho93b059d2017-12-20 12:46:58 +02001856 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001857 }
1858 }
1859 return false;
1860}
1861
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001862bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1863{
1864 // Do not do any translation
1865 return false;
1866}
1867
Olli Etuahod4bd9632018-03-08 16:32:44 +02001868void OutputHLSL::visitFunctionPrototype(TIntermFunctionPrototype *node)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001869{
1870 TInfoSinkBase &out = getInfoSink();
1871
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001872 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001873 // Skip the prototype if it is not implemented (and thus not used)
1874 if (index == CallDAG::InvalidIndex)
1875 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001876 return;
Olli Etuaho16c745a2017-01-16 17:02:27 +00001877 }
1878
Olli Etuahod4bd9632018-03-08 16:32:44 +02001879 const TFunction *func = node->getFunction();
Olli Etuaho16c745a2017-01-16 17:02:27 +00001880
Olli Etuahod4bd9632018-03-08 16:32:44 +02001881 TString name = DecorateFunctionIfNeeded(func);
1882 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(func)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001883 << (mOutputLod0Function ? "Lod0(" : "(");
1884
Olli Etuahod4bd9632018-03-08 16:32:44 +02001885 size_t paramCount = func->getParamCount();
1886 for (unsigned int i = 0; i < paramCount; i++)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001887 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001888 writeParameter(func->getParam(i), out);
Olli Etuaho16c745a2017-01-16 17:02:27 +00001889
Olli Etuahod4bd9632018-03-08 16:32:44 +02001890 if (i < paramCount - 1)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001891 {
1892 out << ", ";
1893 }
1894 }
1895
1896 out << ");\n";
1897
1898 // Also prototype the Lod0 variant if needed
1899 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1900 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1901 {
1902 mOutputLod0Function = true;
1903 node->traverse(this);
1904 mOutputLod0Function = false;
1905 }
Olli Etuaho16c745a2017-01-16 17:02:27 +00001906}
1907
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1909{
Jamie Madill32aab012015-01-27 14:12:26 -05001910 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912 switch (node->getOp())
1913 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001914 case EOpCallBuiltInFunction:
1915 case EOpCallFunctionInAST:
1916 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001918 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001919
Corentin Wallez1239ee92015-03-19 14:38:02 -07001920 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001921 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001923 if (node->isArray())
1924 {
1925 UNIMPLEMENTED();
1926 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001927 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001928 ASSERT(index != CallDAG::InvalidIndex);
1929 lod0 &= mASTMetadataList[index].mNeedsLod0;
1930
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001931 out << DecorateFunctionIfNeeded(node->getFunction());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001932 out << DisambiguateFunctionName(node->getSequence());
1933 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001935 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001936 {
1937 // This path is used for internal functions that don't have their definitions in the
1938 // AST, such as precision emulation functions.
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001939 out << DecorateFunctionIfNeeded(node->getFunction()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001940 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001941 else if (node->getFunction()->isImageFunction())
Xinghua Cao711b7a12017-10-09 13:38:12 +08001942 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001943 const ImmutableString &name = node->getFunction()->name();
Jiawei Shaoa75aa3b2018-06-21 10:38:28 +08001944 TType type = (*arguments)[0]->getAsTyped()->getType();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001945 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001946 name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
Xinghua Cao711b7a12017-10-09 13:38:12 +08001947 type.getMemoryQualifier().readonly);
1948 out << imageFunctionName << "(";
1949 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001950 else
1951 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001952 const ImmutableString &name = node->getFunction()->name();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001953 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001954 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1955 if (arguments->size() > 1)
1956 {
1957 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1958 }
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001959 const ImmutableString &textureFunctionName =
1960 mTextureFunctionHLSL->useTextureFunction(name, samplerType, coords,
1961 arguments->size(), lod0, mShaderType);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001962 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001963 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001964
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001965 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001966 {
Olli Etuaho96963162016-03-21 11:54:33 +02001967 TIntermTyped *typedArg = (*arg)->getAsTyped();
1968 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001969 {
1970 out << "texture_";
1971 (*arg)->traverse(this);
1972 out << ", sampler_";
1973 }
1974
1975 (*arg)->traverse(this);
1976
Olli Etuaho96963162016-03-21 11:54:33 +02001977 if (typedArg->getType().isStructureContainingSamplers())
1978 {
1979 const TType &argType = typedArg->getType();
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001980 TVector<const TVariable *> samplerSymbols;
Olli Etuahofbb1c792018-01-19 16:26:59 +02001981 ImmutableString structName = samplerNamePrefixFromStruct(typedArg);
1982 std::string namePrefix = "angle_";
1983 namePrefix += structName.data();
1984 argType.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols,
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001985 nullptr, mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001986 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02001987 {
1988 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1989 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001990 out << ", texture_" << sampler->name();
1991 out << ", sampler_" << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001992 }
1993 else
1994 {
1995 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1996 // of D3D9, it's the sampler variable.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001997 out << ", " << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001998 }
1999 }
2000 }
2001
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002002 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002003 {
2004 out << ", ";
2005 }
2006 }
2007
2008 out << ")";
2009
2010 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002011 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03002012 case EOpConstruct:
Olli Etuahobd3cd502017-11-03 15:48:52 +02002013 outputConstructor(out, visit, node);
Olli Etuaho8fab3202017-05-08 18:22:22 +03002014 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002015 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002016 outputTriplet(out, visit, "(", " == ", ")");
2017 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002018 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002019 outputTriplet(out, visit, "(", " != ", ")");
2020 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002021 case EOpLessThanComponentWise:
2022 outputTriplet(out, visit, "(", " < ", ")");
2023 break;
2024 case EOpGreaterThanComponentWise:
2025 outputTriplet(out, visit, "(", " > ", ")");
2026 break;
2027 case EOpLessThanEqualComponentWise:
2028 outputTriplet(out, visit, "(", " <= ", ")");
2029 break;
2030 case EOpGreaterThanEqualComponentWise:
2031 outputTriplet(out, visit, "(", " >= ", ")");
2032 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002033 case EOpMod:
2034 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002035 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002036 break;
2037 case EOpModf:
2038 outputTriplet(out, visit, "modf(", ", ", ")");
2039 break;
2040 case EOpPow:
2041 outputTriplet(out, visit, "pow(", ", ", ")");
2042 break;
2043 case EOpAtan:
2044 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2045 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002046 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002047 break;
2048 case EOpMin:
2049 outputTriplet(out, visit, "min(", ", ", ")");
2050 break;
2051 case EOpMax:
2052 outputTriplet(out, visit, "max(", ", ", ")");
2053 break;
2054 case EOpClamp:
2055 outputTriplet(out, visit, "clamp(", ", ", ")");
2056 break;
2057 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302058 {
2059 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2060 if (lastParamNode->getType().getBasicType() == EbtBool)
2061 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002062 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2063 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302064 // so use emulated version.
2065 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002066 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302067 }
2068 else
2069 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002070 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302071 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002072 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302073 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002074 case EOpStep:
2075 outputTriplet(out, visit, "step(", ", ", ")");
2076 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02002077 case EOpSmoothstep:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002078 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2079 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002080 case EOpFrexp:
2081 case EOpLdexp:
2082 ASSERT(node->getUseEmulatedFunction());
2083 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2084 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002085 case EOpDistance:
2086 outputTriplet(out, visit, "distance(", ", ", ")");
2087 break;
2088 case EOpDot:
2089 outputTriplet(out, visit, "dot(", ", ", ")");
2090 break;
2091 case EOpCross:
2092 outputTriplet(out, visit, "cross(", ", ", ")");
2093 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002094 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002095 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002096 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002097 break;
2098 case EOpReflect:
2099 outputTriplet(out, visit, "reflect(", ", ", ")");
2100 break;
2101 case EOpRefract:
2102 outputTriplet(out, visit, "refract(", ", ", ")");
2103 break;
2104 case EOpOuterProduct:
2105 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002106 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002107 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002108 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002109 outputTriplet(out, visit, "(", " * ", ")");
2110 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002111 case EOpBitfieldExtract:
2112 case EOpBitfieldInsert:
2113 case EOpUaddCarry:
2114 case EOpUsubBorrow:
2115 case EOpUmulExtended:
2116 case EOpImulExtended:
2117 ASSERT(node->getUseEmulatedFunction());
2118 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2119 break;
Xinghua Cao47335852018-02-12 15:41:55 +08002120 case EOpBarrier:
2121 // barrier() is translated to GroupMemoryBarrierWithGroupSync(), which is the
2122 // cheapest *WithGroupSync() function, without any functionality loss, but
2123 // with the potential for severe performance loss.
2124 outputTriplet(out, visit, "GroupMemoryBarrierWithGroupSync(", "", ")");
2125 break;
2126 case EOpMemoryBarrierShared:
2127 outputTriplet(out, visit, "GroupMemoryBarrier(", "", ")");
2128 break;
2129 case EOpMemoryBarrierAtomicCounter:
2130 case EOpMemoryBarrierBuffer:
2131 case EOpMemoryBarrierImage:
2132 outputTriplet(out, visit, "DeviceMemoryBarrier(", "", ")");
2133 break;
2134 case EOpGroupMemoryBarrier:
2135 case EOpMemoryBarrier:
2136 outputTriplet(out, visit, "AllMemoryBarrier(", "", ")");
2137 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002138 default:
2139 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002140 }
2141
2142 return true;
2143}
2144
Olli Etuaho57961272016-09-14 13:57:46 +03002145void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002147 out << "if (";
2148
2149 node->getCondition()->traverse(this);
2150
2151 out << ")\n";
2152
Jamie Madill8c46ab12015-12-07 16:39:19 -05002153 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002154
2155 bool discard = false;
2156
2157 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002159 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002160 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002161
Olli Etuahoa6f22092015-05-08 18:31:10 +03002162 // Detect true discard
2163 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2164 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002165 else
2166 {
2167 // TODO(oetuaho): Check if the semicolon inside is necessary.
2168 // It's there as a result of conservative refactoring of the output.
2169 out << "{;}\n";
2170 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002171
Jamie Madill8c46ab12015-12-07 16:39:19 -05002172 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002173
Olli Etuahoa6f22092015-05-08 18:31:10 +03002174 if (node->getFalseBlock())
2175 {
2176 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002177
Jamie Madill8c46ab12015-12-07 16:39:19 -05002178 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179
Olli Etuaho32db19b2016-10-04 14:43:16 +01002180 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002181 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002182
Jamie Madill8c46ab12015-12-07 16:39:19 -05002183 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002184
Olli Etuahoa6f22092015-05-08 18:31:10 +03002185 // Detect false discard
2186 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2187 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002188
Olli Etuahoa6f22092015-05-08 18:31:10 +03002189 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002190 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002191 {
2192 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002193 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002194}
2195
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002196bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2197{
2198 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2199 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2200 UNREACHABLE();
2201 return false;
2202}
2203
Olli Etuaho57961272016-09-14 13:57:46 +03002204bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002205{
2206 TInfoSinkBase &out = getInfoSink();
2207
Olli Etuaho3d932d82016-04-12 11:10:30 +03002208 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002209
2210 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002211 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002212 {
2213 out << "FLATTEN ";
2214 }
2215
Olli Etuaho57961272016-09-14 13:57:46 +03002216 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002217
2218 return false;
2219}
2220
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002221bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002222{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002223 TInfoSinkBase &out = getInfoSink();
2224
Olli Etuaho923ecef2017-10-11 12:01:38 +03002225 ASSERT(node->getStatementList());
2226 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002227 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002228 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002229 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002230 outputTriplet(out, visit, "switch (", ") ", "");
2231 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002232 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002233}
2234
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002235bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002236{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002237 TInfoSinkBase &out = getInfoSink();
2238
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002239 if (node->hasCondition())
2240 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002241 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002242 return true;
2243 }
2244 else
2245 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002246 out << "default:\n";
2247 return false;
2248 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002249}
2250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2252{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002253 TInfoSinkBase &out = getInfoSink();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002254 writeConstantUnion(out, node->getType(), node->getConstantValue());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255}
2256
2257bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2258{
Nicolas Capens655fe362014-04-11 13:12:34 -04002259 mNestedLoopDepth++;
2260
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002261 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002262 mInsideDiscontinuousLoop =
2263 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002264
Jamie Madill8c46ab12015-12-07 16:39:19 -05002265 TInfoSinkBase &out = getInfoSink();
2266
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002267 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002268 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002269 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002270 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002271 mInsideDiscontinuousLoop = wasDiscontinuous;
2272 mNestedLoopDepth--;
2273
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002274 return false;
2275 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002276 }
2277
Corentin Wallez1239ee92015-03-19 14:38:02 -07002278 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002279 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002281 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002282
Jamie Madill8c46ab12015-12-07 16:39:19 -05002283 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284 }
2285 else
2286 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002287 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002288
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 if (node->getInit())
2290 {
2291 node->getInit()->traverse(this);
2292 }
2293
2294 out << "; ";
2295
alokp@chromium.org52813552010-11-16 18:36:09 +00002296 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002298 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 }
2300
2301 out << "; ";
2302
alokp@chromium.org52813552010-11-16 18:36:09 +00002303 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002305 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 }
2307
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002308 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002309
Jamie Madill8c46ab12015-12-07 16:39:19 -05002310 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 }
2312
2313 if (node->getBody())
2314 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002315 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002316 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002318 else
2319 {
2320 // TODO(oetuaho): Check if the semicolon inside is necessary.
2321 // It's there as a result of conservative refactoring of the output.
2322 out << "{;}\n";
2323 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002324
Jamie Madill8c46ab12015-12-07 16:39:19 -05002325 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326
alokp@chromium.org52813552010-11-16 18:36:09 +00002327 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002329 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002330 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331
alokp@chromium.org52813552010-11-16 18:36:09 +00002332 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002334 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 }
2336
daniel@transgaming.com73536982012-03-21 20:45:49 +00002337 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002339 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002340 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002341
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002342 return false;
2343}
2344
2345bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2346{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002347 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002349 TInfoSinkBase &out = getInfoSink();
2350
2351 switch (node->getFlowOp())
2352 {
2353 case EOpKill:
2354 out << "discard";
2355 break;
2356 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002357 if (mNestedLoopDepth > 1)
2358 {
2359 mUsesNestedBreak = true;
2360 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002361
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002362 if (mExcessiveLoopIndex)
2363 {
2364 out << "{Break";
2365 mExcessiveLoopIndex->traverse(this);
2366 out << " = true; break;}\n";
2367 }
2368 else
2369 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002370 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002371 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002372 break;
2373 case EOpContinue:
2374 out << "continue";
2375 break;
2376 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002377 if (node->getExpression())
2378 {
2379 out << "return ";
2380 }
2381 else
2382 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002383 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002384 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002385 break;
2386 default:
2387 UNREACHABLE();
2388 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389 }
2390
2391 return true;
2392}
2393
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002394// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002395// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2396// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002397bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002398{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002399 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002400
2401 // Parse loops of the form:
2402 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002403 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002404 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002405 int initial = 0;
2406 int limit = 0;
2407 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002408
2409 // Parse index name and intial value
2410 if (node->getInit())
2411 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002412 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002413
2414 if (init)
2415 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002416 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002417 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002418
2419 if (variable && variable->getQualifier() == EvqTemporary)
2420 {
2421 TIntermBinary *assign = variable->getAsBinaryNode();
2422
2423 if (assign->getOp() == EOpInitialize)
2424 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002425 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002426 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2427
2428 if (symbol && constant)
2429 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002430 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002431 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002432 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002433 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002434 }
2435 }
2436 }
2437 }
2438 }
2439 }
2440
2441 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002442 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002443 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002444 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002445
Olli Etuahob6af22b2017-12-15 14:05:44 +02002446 if (test && test->getLeft()->getAsSymbolNode()->uniqueId() == index->uniqueId())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002447 {
2448 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2449
2450 if (constant)
2451 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002452 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002453 {
2454 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002455 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002456 }
2457 }
2458 }
2459 }
2460
2461 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002462 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002463 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002464 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002465 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002466
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002467 if (binaryTerminal)
2468 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002469 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002470 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2471
2472 if (constant)
2473 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002474 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002475 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002476 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002477
2478 switch (op)
2479 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002480 case EOpAddAssign:
2481 increment = value;
2482 break;
2483 case EOpSubAssign:
2484 increment = -value;
2485 break;
2486 default:
2487 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002488 }
2489 }
2490 }
2491 }
2492 else if (unaryTerminal)
2493 {
2494 TOperator op = unaryTerminal->getOp();
2495
2496 switch (op)
2497 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002498 case EOpPostIncrement:
2499 increment = 1;
2500 break;
2501 case EOpPostDecrement:
2502 increment = -1;
2503 break;
2504 case EOpPreIncrement:
2505 increment = 1;
2506 break;
2507 case EOpPreDecrement:
2508 increment = -1;
2509 break;
2510 default:
2511 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002512 }
2513 }
2514 }
2515
Yunchao He4f285442017-04-21 12:15:49 +08002516 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002517 {
2518 if (comparator == EOpLessThanEqual)
2519 {
2520 comparator = EOpLessThan;
2521 limit += 1;
2522 }
2523
2524 if (comparator == EOpLessThan)
2525 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002526 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002527
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002528 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002529 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002530 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002531 }
2532
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002533 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002534 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002535
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002536 out << "{int ";
2537 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002538 out << ";\n"
2539 "bool Break";
2540 index->traverse(this);
2541 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002542
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002543 bool firstLoopFragment = true;
2544
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002545 while (iterations > 0)
2546 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002547 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002548
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002549 if (!firstLoopFragment)
2550 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002551 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002552 index->traverse(this);
2553 out << ") {\n";
2554 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002555
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002556 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002557 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002558 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002559 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002560
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002561 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002562 const char *unroll =
2563 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002564
Corentin Wallez1239ee92015-03-19 14:38:02 -07002565 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002566 index->traverse(this);
2567 out << " = ";
2568 out << initial;
2569
2570 out << "; ";
2571 index->traverse(this);
2572 out << " < ";
2573 out << clampedLimit;
2574
2575 out << "; ";
2576 index->traverse(this);
2577 out << " += ";
2578 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002579 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002580
Jamie Madill8c46ab12015-12-07 16:39:19 -05002581 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002582 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002583
2584 if (node->getBody())
2585 {
2586 node->getBody()->traverse(this);
2587 }
2588
Jamie Madill8c46ab12015-12-07 16:39:19 -05002589 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002590 out << ";}\n";
2591
2592 if (!firstLoopFragment)
2593 {
2594 out << "}\n";
2595 }
2596
2597 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002598
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002599 initial += MAX_LOOP_ITERATIONS * increment;
2600 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002601 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002602
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002603 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002605 mExcessiveLoopIndex = restoreIndex;
2606
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002607 return true;
2608 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002609 else
2610 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002611 }
2612
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002613 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002614}
2615
Jamie Madill8c46ab12015-12-07 16:39:19 -05002616void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2617 Visit visit,
2618 const char *preString,
2619 const char *inString,
2620 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002622 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623 {
2624 out << preString;
2625 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002626 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002627 {
2628 out << inString;
2629 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002630 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002631 {
2632 out << postString;
2633 }
2634}
2635
Jamie Madill8c46ab12015-12-07 16:39:19 -05002636void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002637{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002638 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002639 {
Jamie Madill32aab012015-01-27 14:12:26 -05002640 out << "\n";
2641 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002642
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002643 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002644 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002645 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002646 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002647
Jamie Madill32aab012015-01-27 14:12:26 -05002648 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002649 }
2650}
2651
Olli Etuahod4bd9632018-03-08 16:32:44 +02002652void OutputHLSL::writeParameter(const TVariable *param, TInfoSinkBase &out)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002653{
Olli Etuahod4bd9632018-03-08 16:32:44 +02002654 const TType &type = param->getType();
2655 TQualifier qualifier = type.getQualifier();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002656
Olli Etuahod4bd9632018-03-08 16:32:44 +02002657 TString nameStr = DecorateVariableIfNeeded(*param);
2658 ASSERT(nameStr != ""); // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002659
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002660 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002661 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002662 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2663 {
2664 // Samplers are passed as indices to the sampler array.
2665 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002666 out << "const uint " << nameStr << ArrayString(type);
2667 return;
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002668 }
2669 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2670 {
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002671 out << QualifierString(qualifier) << " " << TextureString(type.getBasicType())
2672 << " texture_" << nameStr << ArrayString(type) << ", " << QualifierString(qualifier)
2673 << " " << SamplerString(type.getBasicType()) << " sampler_" << nameStr
2674 << ArrayString(type);
2675 return;
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002676 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002677 }
2678
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002679 out << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2680 << ArrayString(type);
Olli Etuaho96963162016-03-21 11:54:33 +02002681
2682 // If the structure parameter contains samplers, they need to be passed into the function as
2683 // separate parameters. HLSL doesn't natively support samplers in structs.
2684 if (type.isStructureContainingSamplers())
2685 {
2686 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002687 TVector<const TVariable *> samplerSymbols;
Olli Etuahofbb1c792018-01-19 16:26:59 +02002688 std::string namePrefix = "angle";
2689 namePrefix += nameStr.c_str();
2690 type.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols, nullptr,
2691 mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002692 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02002693 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002694 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002695 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2696 {
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002697 out << ", const uint " << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002698 }
2699 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2700 {
Olli Etuaho96963162016-03-21 11:54:33 +02002701 ASSERT(IsSampler(samplerType.getBasicType()));
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002702 out << ", " << QualifierString(qualifier) << " "
2703 << TextureString(samplerType.getBasicType()) << " texture_" << sampler->name()
2704 << ArrayString(samplerType) << ", " << QualifierString(qualifier) << " "
2705 << SamplerString(samplerType.getBasicType()) << " sampler_" << sampler->name()
2706 << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002707 }
2708 else
2709 {
Olli Etuaho96963162016-03-21 11:54:33 +02002710 ASSERT(IsSampler(samplerType.getBasicType()));
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002711 out << ", " << QualifierString(qualifier) << " " << TypeString(samplerType) << " "
2712 << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002713 }
2714 }
2715 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002716}
2717
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002718TString OutputHLSL::zeroInitializer(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719{
2720 TString string;
2721
Jamie Madill94bf7f22013-07-08 13:31:15 -04002722 size_t size = type.getObjectSize();
2723 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002725 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726
Jamie Madill94bf7f22013-07-08 13:31:15 -04002727 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002728 {
2729 string += ", ";
2730 }
2731 }
2732
daniel@transgaming.comead23042010-04-29 03:35:36 +00002733 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002735
Olli Etuahobd3cd502017-11-03 15:48:52 +02002736void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002737{
Olli Etuahobd3cd502017-11-03 15:48:52 +02002738 // Array constructors should have been already pruned from the code.
2739 ASSERT(!node->getType().isArray());
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002740
2741 if (visit == PreVisit)
2742 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002743 TString constructorName;
2744 if (node->getBasicType() == EbtStruct)
2745 {
2746 constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct());
2747 }
2748 else
2749 {
2750 constructorName =
2751 mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence());
2752 }
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002753 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002754 }
2755 else if (visit == InVisit)
2756 {
2757 out << ", ";
2758 }
2759 else if (visit == PostVisit)
2760 {
2761 out << ")";
2762 }
2763}
2764
Jamie Madill8c46ab12015-12-07 16:39:19 -05002765const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2766 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002767 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002768{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002769 ASSERT(!type.isArray());
2770
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002771 const TConstantUnion *constUnionIterated = constUnion;
2772
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002773 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002774 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002775 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002776 out << mStructureHLSL->addStructConstructor(*structure) << "(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002777
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002778 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002779
Jamie Madill98493dd2013-07-08 14:39:03 -04002780 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002781 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002782 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002783 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002784
Jamie Madill98493dd2013-07-08 14:39:03 -04002785 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002786 {
2787 out << ", ";
2788 }
2789 }
2790
2791 out << ")";
2792 }
2793 else
2794 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002795 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002796 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002797
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002798 if (writeType)
2799 {
Jamie Madill033dae62014-06-18 12:56:28 -04002800 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002801 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002802 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002803 if (writeType)
2804 {
2805 out << ")";
2806 }
2807 }
2808
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002809 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002810}
2811
Olli Etuahod68924e2017-01-02 17:34:40 +00002812void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002813{
Olli Etuahod68924e2017-01-02 17:34:40 +00002814 if (visit == PreVisit)
2815 {
2816 const char *opStr = GetOperatorString(op);
2817 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2818 out << "(";
2819 }
2820 else
2821 {
2822 outputTriplet(out, visit, nullptr, ", ", ")");
2823 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002824}
2825
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002826bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2827 TIntermSymbol *symbolNode,
2828 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002829{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002830 ASSERT(symbolNode->variable().symbolType() != SymbolType::Empty);
2831 const TIntermSymbol *symbolInInitializer = FindSymbolNode(expression, symbolNode->getName());
Jamie Madill37997142015-01-28 10:06:34 -05002832
Olli Etuaho4728bdc2017-12-20 17:51:08 +02002833 if (symbolInInitializer)
Jamie Madill37997142015-01-28 10:06:34 -05002834 {
2835 // Type already printed
2836 out << "t" + str(mUniqueIndex) + " = ";
2837 expression->traverse(this);
2838 out << ", ";
2839 symbolNode->traverse(this);
2840 out << " = t" + str(mUniqueIndex);
2841
2842 mUniqueIndex++;
2843 return true;
2844 }
2845
2846 return false;
2847}
2848
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002849bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2850 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002851 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002852{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002853 if (initializer->hasConstantValue())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002854 {
2855 symbolNode->traverse(this);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002856 out << ArrayString(symbolNode->getType());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002857 out << " = {";
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002858 writeConstantUnionArray(out, initializer->getConstantValue(),
2859 initializer->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002860 out << "}";
2861 return true;
2862 }
2863 return false;
2864}
2865
Jamie Madill55e79e02015-02-09 15:35:00 -05002866TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2867{
2868 const TFieldList &fields = structure.fields();
2869
2870 for (const auto &eqFunction : mStructEqualityFunctions)
2871 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002872 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002873 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002874 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002875 }
2876 }
2877
2878 const TString &structNameString = StructNameString(structure);
2879
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002880 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002881 function->structure = &structure;
2882 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002883
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002884 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002885
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002886 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2887 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002888 << "{\n"
2889 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002890
2891 for (size_t i = 0; i < fields.size(); i++)
2892 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002893 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002894 const TType *fieldType = field->type();
2895
2896 const TString &fieldNameA = "a." + Decorate(field->name());
2897 const TString &fieldNameB = "b." + Decorate(field->name());
2898
2899 if (i > 0)
2900 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002901 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002902 }
2903
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002904 fnOut << "(";
2905 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2906 fnOut << fieldNameA;
2907 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2908 fnOut << fieldNameB;
2909 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2910 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002911 }
2912
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002913 fnOut << ";\n"
2914 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002915
2916 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002917
2918 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002919 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002920
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002921 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002922}
2923
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002924TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002925{
2926 for (const auto &eqFunction : mArrayEqualityFunctions)
2927 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002928 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002929 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002930 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002931 }
2932 }
2933
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002934 TType elementType(type);
2935 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002936
Olli Etuaho12690762015-03-31 12:55:28 +03002937 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002938 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002939
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002940 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002941
2942 TInfoSinkBase fnOut;
2943
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002944 const TString &typeName = TypeString(type);
2945 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2946 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002947 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002948 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002949 << type.getOutermostArraySize()
2950 << "; ++i)\n"
2951 " {\n"
2952 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002953
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002954 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002955 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002956 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002957 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002958 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002959
2960 fnOut << ") { return false; }\n"
2961 " }\n"
2962 " return true;\n"
2963 "}\n";
2964
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002965 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002966
2967 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002968 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002969
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002970 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002971}
2972
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002973TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002974{
2975 for (const auto &assignFunction : mArrayAssignmentFunctions)
2976 {
2977 if (assignFunction.type == type)
2978 {
2979 return assignFunction.functionName;
2980 }
2981 }
2982
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002983 TType elementType(type);
2984 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03002985
2986 ArrayHelperFunction function;
2987 function.type = type;
2988
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002989 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03002990
2991 TInfoSinkBase fnOut;
2992
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002993 const TString &typeName = TypeString(type);
2994 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
2995 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002996 << "{\n"
2997 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002998 << type.getOutermostArraySize()
2999 << "; ++i)\n"
3000 " {\n"
3001 " ";
3002
3003 outputAssign(PreVisit, elementType, fnOut);
3004 fnOut << "a[i]";
3005 outputAssign(InVisit, elementType, fnOut);
3006 fnOut << "b[i]";
3007 outputAssign(PostVisit, elementType, fnOut);
3008
3009 fnOut << ";\n"
3010 " }\n"
3011 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03003012
3013 function.functionDefinition = fnOut.c_str();
3014
3015 mArrayAssignmentFunctions.push_back(function);
3016
3017 return function.functionName;
3018}
3019
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003020TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003021{
3022 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3023 {
3024 if (constructIntoFunction.type == type)
3025 {
3026 return constructIntoFunction.functionName;
3027 }
3028 }
3029
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003030 TType elementType(type);
3031 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003032
3033 ArrayHelperFunction function;
3034 function.type = type;
3035
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003036 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003037
3038 TInfoSinkBase fnOut;
3039
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003040 const TString &typeName = TypeString(type);
3041 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3042 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003043 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003044 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003045 }
3046 fnOut << ")\n"
3047 "{\n";
3048
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003049 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003050 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003051 fnOut << " ";
3052 outputAssign(PreVisit, elementType, fnOut);
3053 fnOut << "a[" << i << "]";
3054 outputAssign(InVisit, elementType, fnOut);
3055 fnOut << "b" << i;
3056 outputAssign(PostVisit, elementType, fnOut);
3057 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003058 }
3059 fnOut << "}\n";
3060
3061 function.functionDefinition = fnOut.c_str();
3062
3063 mArrayConstructIntoFunctions.push_back(function);
3064
3065 return function.functionName;
3066}
3067
Jamie Madill2e295e22015-04-29 10:41:33 -04003068void OutputHLSL::ensureStructDefined(const TType &type)
3069{
Olli Etuahobd3cd502017-11-03 15:48:52 +02003070 const TStructure *structure = type.getStruct();
Jamie Madill2e295e22015-04-29 10:41:33 -04003071 if (structure)
3072 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02003073 ASSERT(type.getBasicType() == EbtStruct);
3074 mStructureHLSL->ensureStructDefined(*structure);
Jamie Madill2e295e22015-04-29 10:41:33 -04003075 }
3076}
3077
Jamie Madill45bcc782016-11-07 13:58:48 -05003078} // namespace sh