blob: 1c7e090aef2e25ea21506f984f8818c5cf7148ec [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
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
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 ||
59 variable->getQualifier() == EvqConst);
60}
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
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500168 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 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200417 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
Olli Etuaho2f7c04a2018-01-25 14:50:37 +0200421 varyings += TString("static ") + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200422 " " + Decorate(name) + ArrayString(type) + " = " + zeroInitializer(type) +
423 ";\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 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200428 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();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200502 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
512 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500513 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400514 for (unsigned int i = 0; i < numColorValues; i++)
515 {
516 out << " float4(0, 0, 0, 0)";
517 if (i + 1 != numColorValues)
518 {
519 out << ",";
520 }
521 out << "\n";
522 }
523
524 out << "};\n";
525 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000526
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400527 if (mUsesFragDepth)
528 {
529 out << "static float gl_Depth = 0.0;\n";
530 }
531
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000532 if (mUsesFragCoord)
533 {
534 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
535 }
536
537 if (mUsesPointCoord)
538 {
539 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
540 }
541
542 if (mUsesFrontFacing)
543 {
544 out << "static bool gl_FrontFacing = false;\n";
545 }
546
547 out << "\n";
548
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000549 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000550 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000551 out << "struct gl_DepthRangeParameters\n"
552 "{\n"
553 " float near;\n"
554 " float far;\n"
555 " float diff;\n"
556 "};\n"
557 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000558 }
559
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200560 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000561 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000562 out << "cbuffer DriverConstants : register(b1)\n"
563 "{\n";
564
565 if (mUsesDepthRange)
566 {
567 out << " float3 dx_DepthRange : packoffset(c0);\n";
568 }
569
570 if (mUsesFragCoord)
571 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000572 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000573 }
574
575 if (mUsesFragCoord || mUsesFrontFacing)
576 {
577 out << " float3 dx_DepthFront : packoffset(c2);\n";
578 }
579
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800580 if (mUsesFragCoord)
581 {
582 // dx_ViewScale is only used in the fragment shader to correct
583 // the value for glFragCoord if necessary
584 out << " float2 dx_ViewScale : packoffset(c3);\n";
585 }
586
Martin Radev72b4e1e2017-08-31 15:42:56 +0300587 if (mHasMultiviewExtensionEnabled)
588 {
589 // We have to add a value which we can use to keep track of which multi-view code
590 // path is to be selected in the GS.
591 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
592 }
593
Olli Etuaho618bebc2016-01-15 16:40:00 +0200594 if (mOutputType == SH_HLSL_4_1_OUTPUT)
595 {
596 mUniformHLSL->samplerMetadataUniforms(out, "c4");
597 }
598
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000599 out << "};\n";
600 }
601 else
602 {
603 if (mUsesDepthRange)
604 {
605 out << "uniform float3 dx_DepthRange : register(c0);";
606 }
607
608 if (mUsesFragCoord)
609 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000610 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000611 }
612
613 if (mUsesFragCoord || mUsesFrontFacing)
614 {
615 out << "uniform float3 dx_DepthFront : register(c2);\n";
616 }
617 }
618
619 out << "\n";
620
621 if (mUsesDepthRange)
622 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500623 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
624 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000625 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000626 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000627
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000628 if (usingMRTExtension && mNumRenderTargets > 1)
629 {
630 out << "#define GL_USES_MRT\n";
631 }
632
633 if (mUsesFragColor)
634 {
635 out << "#define GL_USES_FRAG_COLOR\n";
636 }
637
638 if (mUsesFragData)
639 {
640 out << "#define GL_USES_FRAG_DATA\n";
641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642 }
Xinghua Caob1239382016-12-13 15:07:05 +0800643 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000645 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500646 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000647 out << "\n"
648 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400649
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000650 if (mUsesPointSize)
651 {
652 out << "static float gl_PointSize = float(1);\n";
653 }
654
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000655 if (mUsesInstanceID)
656 {
657 out << "static int gl_InstanceID;";
658 }
659
Corentin Wallezb076add2016-01-11 16:45:46 -0500660 if (mUsesVertexID)
661 {
662 out << "static int gl_VertexID;";
663 }
664
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000665 out << "\n"
666 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500667 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000668 out << "\n";
669
670 if (mUsesDepthRange)
671 {
672 out << "struct gl_DepthRangeParameters\n"
673 "{\n"
674 " float near;\n"
675 " float far;\n"
676 " float diff;\n"
677 "};\n"
678 "\n";
679 }
680
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200681 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000682 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800683 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500684 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800685
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000686 if (mUsesDepthRange)
687 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800688 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000689 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800690
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800691 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
692 // shaders. However, we declare it for all shaders (including Feature Level 10+).
693 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
694 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800695 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800696 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800697 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800698
Martin Radev72b4e1e2017-08-31 15:42:56 +0300699 if (mHasMultiviewExtensionEnabled)
700 {
701 // We have to add a value which we can use to keep track of which multi-view code
702 // path is to be selected in the GS.
703 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
704 }
705
Olli Etuaho618bebc2016-01-15 16:40:00 +0200706 if (mOutputType == SH_HLSL_4_1_OUTPUT)
707 {
708 mUniformHLSL->samplerMetadataUniforms(out, "c4");
709 }
710
Austin Kinross4fd18b12014-12-22 12:32:05 -0800711 out << "};\n"
712 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000713 }
714 else
715 {
716 if (mUsesDepthRange)
717 {
718 out << "uniform float3 dx_DepthRange : register(c0);\n";
719 }
720
Cooper Partine6664f02015-01-09 16:22:24 -0800721 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
722 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000723 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000724 }
725
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000726 if (mUsesDepthRange)
727 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500728 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
729 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000730 "\n";
731 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400732 }
Xinghua Caob1239382016-12-13 15:07:05 +0800733 else // Compute shader
734 {
735 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800736
737 out << "cbuffer DriverConstants : register(b1)\n"
738 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800739 if (mUsesNumWorkGroups)
740 {
Xinghua Caob1239382016-12-13 15:07:05 +0800741 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800742 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800743 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
744 mUniformHLSL->samplerMetadataUniforms(out, "c1");
745 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800746
747 // Follow built-in variables would be initialized in
748 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
749 // are used in compute shader.
750 if (mUsesWorkGroupID)
751 {
752 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
753 }
754
755 if (mUsesLocalInvocationID)
756 {
757 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
758 }
759
760 if (mUsesGlobalInvocationID)
761 {
762 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
763 }
764
765 if (mUsesLocalInvocationIndex)
766 {
767 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
768 }
769 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000770
Qin Jiajia2a12b3d2018-05-23 13:42:13 +0800771 if (!mappedStructs.empty())
772 {
773 out << "// Structures from std140 blocks with padding removed\n";
774 out << "\n";
775 out << mappedStructs;
776 out << "\n";
777 }
778
Geoff Lang1fe74c72016-08-25 13:23:01 -0400779 bool getDimensionsIgnoresBaseLevel =
780 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
781 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800782 mImageFunctionHLSL->imageFunctionHeader(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000784 if (mUsesFragCoord)
785 {
786 out << "#define GL_USES_FRAG_COORD\n";
787 }
788
789 if (mUsesPointCoord)
790 {
791 out << "#define GL_USES_POINT_COORD\n";
792 }
793
794 if (mUsesFrontFacing)
795 {
796 out << "#define GL_USES_FRONT_FACING\n";
797 }
798
799 if (mUsesPointSize)
800 {
801 out << "#define GL_USES_POINT_SIZE\n";
802 }
803
Martin Radev41ac68e2017-06-06 12:16:58 +0300804 if (mHasMultiviewExtensionEnabled)
805 {
806 out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n";
807 }
808
809 if (mUsesViewID)
810 {
811 out << "#define GL_USES_VIEW_ID\n";
812 }
813
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400814 if (mUsesFragDepth)
815 {
816 out << "#define GL_USES_FRAG_DEPTH\n";
817 }
818
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000819 if (mUsesDepthRange)
820 {
821 out << "#define GL_USES_DEPTH_RANGE\n";
822 }
823
Xinghua Caob1239382016-12-13 15:07:05 +0800824 if (mUsesNumWorkGroups)
825 {
826 out << "#define GL_USES_NUM_WORK_GROUPS\n";
827 }
828
829 if (mUsesWorkGroupID)
830 {
831 out << "#define GL_USES_WORK_GROUP_ID\n";
832 }
833
834 if (mUsesLocalInvocationID)
835 {
836 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
837 }
838
839 if (mUsesGlobalInvocationID)
840 {
841 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
842 }
843
844 if (mUsesLocalInvocationIndex)
845 {
846 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
847 }
848
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000849 if (mUsesXor)
850 {
851 out << "bool xor(bool p, bool q)\n"
852 "{\n"
853 " return (p || q) && !(p && q);\n"
854 "}\n"
855 "\n";
856 }
857
Olli Etuahodfa75e82017-01-23 09:43:06 -0800858 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
861void OutputHLSL::visitSymbol(TIntermSymbol *node)
862{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200863 const TVariable &variable = node->variable();
864
865 // Empty symbols can only appear in declarations and function arguments, and in either of those
866 // cases the symbol nodes are not visited.
867 ASSERT(variable.symbolType() != SymbolType::Empty);
868
Jamie Madill32aab012015-01-27 14:12:26 -0500869 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870
Jamie Madill570e04d2013-06-21 09:15:33 -0400871 // Handle accessing std140 structs by value
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200872 if (IsInStd140InterfaceBlock(node) && node->getBasicType() == EbtStruct)
Jamie Madill570e04d2013-06-21 09:15:33 -0400873 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200874 out << "map";
Jamie Madill570e04d2013-06-21 09:15:33 -0400875 }
876
Olli Etuahofbb1c792018-01-19 16:26:59 +0200877 const ImmutableString &name = variable.name();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200878 const TSymbolUniqueId &uniqueId = variable.uniqueId();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200879
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000880 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000881 {
882 mUsesDepthRange = true;
883 out << name;
884 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885 else
886 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200887 const TType &variableType = variable.getType();
888 TQualifier qualifier = variable.getType().getQualifier();
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000889
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200890 ensureStructDefined(variableType);
Olli Etuahobd3cd502017-11-03 15:48:52 +0200891
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000892 if (qualifier == EvqUniform)
893 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200894 const TInterfaceBlock *interfaceBlock = variableType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400895
896 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000897 {
Olli Etuahoc71862a2017-12-21 12:58:29 +0200898 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
899 {
900 const TVariable *instanceVariable = nullptr;
901 if (variableType.isInterfaceBlock())
902 {
903 instanceVariable = &variable;
904 }
905 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
906 new TReferencedBlock(interfaceBlock, instanceVariable);
907 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000908 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000909 else
910 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200911 mReferencedUniforms[uniqueId.get()] = &variable;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000912 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400913
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200914 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000915 }
Jamie Madill19571812013-08-12 15:26:34 -0700916 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000917 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200918 mReferencedAttributes[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400919 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000920 }
Jamie Madill033dae62014-06-18 12:56:28 -0400921 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000922 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200923 mReferencedVaryings[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400924 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300925 if (name == "ViewID_OVR")
926 {
927 mUsesViewID = true;
928 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000929 }
Jamie Madill19571812013-08-12 15:26:34 -0700930 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400931 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200932 mReferencedOutputVariables[uniqueId.get()] = &variable;
Jamie Madill46131a32013-06-20 11:55:50 -0400933 out << "out_" << name;
934 }
935 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000936 {
937 out << "gl_Color[0]";
938 mUsesFragColor = true;
939 }
940 else if (qualifier == EvqFragData)
941 {
942 out << "gl_Color";
943 mUsesFragData = true;
944 }
945 else if (qualifier == EvqFragCoord)
946 {
947 mUsesFragCoord = true;
948 out << name;
949 }
950 else if (qualifier == EvqPointCoord)
951 {
952 mUsesPointCoord = true;
953 out << name;
954 }
955 else if (qualifier == EvqFrontFacing)
956 {
957 mUsesFrontFacing = true;
958 out << name;
959 }
960 else if (qualifier == EvqPointSize)
961 {
962 mUsesPointSize = true;
963 out << name;
964 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000965 else if (qualifier == EvqInstanceID)
966 {
967 mUsesInstanceID = true;
968 out << name;
969 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500970 else if (qualifier == EvqVertexID)
971 {
972 mUsesVertexID = true;
973 out << name;
974 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300975 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400976 {
977 mUsesFragDepth = true;
978 out << "gl_Depth";
979 }
Xinghua Caob1239382016-12-13 15:07:05 +0800980 else if (qualifier == EvqNumWorkGroups)
981 {
982 mUsesNumWorkGroups = true;
983 out << name;
984 }
985 else if (qualifier == EvqWorkGroupID)
986 {
987 mUsesWorkGroupID = true;
988 out << name;
989 }
990 else if (qualifier == EvqLocalInvocationID)
991 {
992 mUsesLocalInvocationID = true;
993 out << name;
994 }
995 else if (qualifier == EvqGlobalInvocationID)
996 {
997 mUsesGlobalInvocationID = true;
998 out << name;
999 }
1000 else if (qualifier == EvqLocalInvocationIndex)
1001 {
1002 mUsesLocalInvocationIndex = true;
1003 out << name;
1004 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001005 else
1006 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001007 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001008 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009 }
1010}
1011
Olli Etuaho7fb49552015-03-18 17:27:44 +02001012void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1013{
1014 if (type.isScalar() && !type.isArray())
1015 {
1016 if (op == EOpEqual)
1017 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001018 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001019 }
1020 else
1021 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001022 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001023 }
1024 }
1025 else
1026 {
1027 if (visit == PreVisit && op == EOpNotEqual)
1028 {
1029 out << "!";
1030 }
1031
1032 if (type.isArray())
1033 {
1034 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001035 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001036 }
1037 else if (type.getBasicType() == EbtStruct)
1038 {
1039 const TStructure &structure = *type.getStruct();
1040 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001041 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001042 }
1043 else
1044 {
1045 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001046 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001047 }
1048 }
1049}
1050
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001051void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1052{
1053 if (type.isArray())
1054 {
1055 const TString &functionName = addArrayAssignmentFunction(type);
1056 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1057 }
1058 else
1059 {
1060 outputTriplet(out, visit, "(", " = ", ")");
1061 }
1062}
1063
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001064bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001065{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001066 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001067 {
1068 TIntermNode *ancestor = getAncestorNode(n);
1069 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1070 if (ancestorBinary == nullptr)
1071 {
1072 return false;
1073 }
1074 switch (ancestorBinary->getOp())
1075 {
1076 case EOpIndexDirectStruct:
1077 {
1078 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1079 const TIntermConstantUnion *index =
1080 ancestorBinary->getRight()->getAsConstantUnion();
1081 const TField *field = structure->fields()[index->getIConst(0)];
1082 if (IsSampler(field->type()->getBasicType()))
1083 {
1084 return true;
1085 }
1086 break;
1087 }
1088 case EOpIndexDirect:
1089 break;
1090 default:
1091 // Returning a sampler from indirect indexing is not supported.
1092 return false;
1093 }
1094 }
1095 return false;
1096}
1097
Olli Etuahob6fa0432016-09-28 16:28:05 +01001098bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1099{
1100 TInfoSinkBase &out = getInfoSink();
1101 if (visit == PostVisit)
1102 {
1103 out << ".";
1104 node->writeOffsetsAsXYZW(&out);
1105 }
1106 return true;
1107}
1108
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1110{
Jamie Madill32aab012015-01-27 14:12:26 -05001111 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
1113 switch (node->getOp())
1114 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001115 case EOpComma:
1116 outputTriplet(out, visit, "(", ", ", ")");
1117 break;
1118 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001119 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001120 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001121 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1122 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001123 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001124 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1125 out << functionName << "(";
1126 node->getLeft()->traverse(this);
1127 TIntermSequence *seq = rightAgg->getSequence();
1128 for (auto &arrayElement : *seq)
1129 {
1130 out << ", ";
1131 arrayElement->traverse(this);
1132 }
1133 out << ")";
1134 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001135 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001136 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1137 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001138 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001139 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001140 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001141 break;
1142 case EOpInitialize:
1143 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001144 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001145 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1146 ASSERT(symbolNode);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001147 TIntermTyped *initializer = node->getRight();
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001148
1149 // Global initializers must be constant at this point.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001150 ASSERT(symbolNode->getQualifier() != EvqGlobal || initializer->hasConstantValue());
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001151
1152 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1153 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1154 // new variable is created before the assignment is evaluated), so we need to
1155 // convert
1156 // this to "float t = x, x = t;".
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001157 if (writeSameSymbolInitializer(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001158 {
1159 // Skip initializing the rest of the expression
1160 return false;
1161 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001162 else if (writeConstantInitialization(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001163 {
1164 return false;
1165 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001166 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001167 else if (visit == InVisit)
1168 {
1169 out << " = ";
1170 }
1171 break;
1172 case EOpAddAssign:
1173 outputTriplet(out, visit, "(", " += ", ")");
1174 break;
1175 case EOpSubAssign:
1176 outputTriplet(out, visit, "(", " -= ", ")");
1177 break;
1178 case EOpMulAssign:
1179 outputTriplet(out, visit, "(", " *= ", ")");
1180 break;
1181 case EOpVectorTimesScalarAssign:
1182 outputTriplet(out, visit, "(", " *= ", ")");
1183 break;
1184 case EOpMatrixTimesScalarAssign:
1185 outputTriplet(out, visit, "(", " *= ", ")");
1186 break;
1187 case EOpVectorTimesMatrixAssign:
1188 if (visit == PreVisit)
1189 {
1190 out << "(";
1191 }
1192 else if (visit == InVisit)
1193 {
1194 out << " = mul(";
1195 node->getLeft()->traverse(this);
1196 out << ", transpose(";
1197 }
1198 else
1199 {
1200 out << ")))";
1201 }
1202 break;
1203 case EOpMatrixTimesMatrixAssign:
1204 if (visit == PreVisit)
1205 {
1206 out << "(";
1207 }
1208 else if (visit == InVisit)
1209 {
1210 out << " = transpose(mul(transpose(";
1211 node->getLeft()->traverse(this);
1212 out << "), transpose(";
1213 }
1214 else
1215 {
1216 out << "))))";
1217 }
1218 break;
1219 case EOpDivAssign:
1220 outputTriplet(out, visit, "(", " /= ", ")");
1221 break;
1222 case EOpIModAssign:
1223 outputTriplet(out, visit, "(", " %= ", ")");
1224 break;
1225 case EOpBitShiftLeftAssign:
1226 outputTriplet(out, visit, "(", " <<= ", ")");
1227 break;
1228 case EOpBitShiftRightAssign:
1229 outputTriplet(out, visit, "(", " >>= ", ")");
1230 break;
1231 case EOpBitwiseAndAssign:
1232 outputTriplet(out, visit, "(", " &= ", ")");
1233 break;
1234 case EOpBitwiseXorAssign:
1235 outputTriplet(out, visit, "(", " ^= ", ")");
1236 break;
1237 case EOpBitwiseOrAssign:
1238 outputTriplet(out, visit, "(", " |= ", ")");
1239 break;
1240 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001241 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001242 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001243 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001244 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001245 if (visit == PreVisit)
1246 {
Olli Etuaho12a18ad2017-12-01 16:59:47 +02001247 TIntermSymbol *instanceArraySymbol = node->getLeft()->getAsSymbolNode();
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001248 const TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Olli Etuahoc71862a2017-12-21 12:58:29 +02001249 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
1250 {
1251 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
1252 new TReferencedBlock(interfaceBlock, &instanceArraySymbol->variable());
1253 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001254 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001255 out << mUniformHLSL->UniformBlockInstanceString(instanceArraySymbol->getName(),
1256 arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001257 return false;
1258 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001259 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001260 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001261 {
1262 // All parts of an expression that access a sampler in a struct need to use _ as
1263 // separator to access the sampler variable that has been moved out of the struct.
1264 outputTriplet(out, visit, "", "_", "");
1265 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001266 else
1267 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001268 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001269 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001270 }
1271 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001272 case EOpIndexIndirect:
1273 // We do not currently support indirect references to interface blocks
1274 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1275 outputTriplet(out, visit, "", "[", "]");
1276 break;
1277 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001278 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001279 const TStructure *structure = node->getLeft()->getType().getStruct();
1280 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1281 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001282
Olli Etuaho96963162016-03-21 11:54:33 +02001283 // In cases where indexing returns a sampler, we need to access the sampler variable
1284 // that has been moved out of the struct.
1285 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1286 if (visit == PreVisit && indexingReturnsSampler)
1287 {
1288 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1289 // This prefix is only output at the beginning of the indexing expression, which
1290 // may have multiple parts.
1291 out << "angle";
1292 }
1293 if (!indexingReturnsSampler)
1294 {
1295 // All parts of an expression that access a sampler in a struct need to use _ as
1296 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001297 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001298 }
1299 if (visit == InVisit)
1300 {
1301 if (indexingReturnsSampler)
1302 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001303 out << "_" << field->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001304 }
1305 else
1306 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001307 out << "." << DecorateField(field->name(), *structure);
Olli Etuaho96963162016-03-21 11:54:33 +02001308 }
1309
1310 return false;
1311 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001312 }
1313 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001314 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001315 {
1316 bool structInStd140Block =
1317 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1318 if (visit == PreVisit && structInStd140Block)
1319 {
1320 out << "map";
1321 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001322 if (visit == InVisit)
1323 {
1324 const TInterfaceBlock *interfaceBlock =
1325 node->getLeft()->getType().getInterfaceBlock();
1326 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1327 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001328 if (structInStd140Block)
1329 {
1330 out << "_";
1331 }
1332 else
1333 {
1334 out << ".";
1335 }
1336 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001337
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001338 return false;
1339 }
1340 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001341 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001342 case EOpAdd:
1343 outputTriplet(out, visit, "(", " + ", ")");
1344 break;
1345 case EOpSub:
1346 outputTriplet(out, visit, "(", " - ", ")");
1347 break;
1348 case EOpMul:
1349 outputTriplet(out, visit, "(", " * ", ")");
1350 break;
1351 case EOpDiv:
1352 outputTriplet(out, visit, "(", " / ", ")");
1353 break;
1354 case EOpIMod:
1355 outputTriplet(out, visit, "(", " % ", ")");
1356 break;
1357 case EOpBitShiftLeft:
1358 outputTriplet(out, visit, "(", " << ", ")");
1359 break;
1360 case EOpBitShiftRight:
1361 outputTriplet(out, visit, "(", " >> ", ")");
1362 break;
1363 case EOpBitwiseAnd:
1364 outputTriplet(out, visit, "(", " & ", ")");
1365 break;
1366 case EOpBitwiseXor:
1367 outputTriplet(out, visit, "(", " ^ ", ")");
1368 break;
1369 case EOpBitwiseOr:
1370 outputTriplet(out, visit, "(", " | ", ")");
1371 break;
1372 case EOpEqual:
1373 case EOpNotEqual:
1374 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1375 break;
1376 case EOpLessThan:
1377 outputTriplet(out, visit, "(", " < ", ")");
1378 break;
1379 case EOpGreaterThan:
1380 outputTriplet(out, visit, "(", " > ", ")");
1381 break;
1382 case EOpLessThanEqual:
1383 outputTriplet(out, visit, "(", " <= ", ")");
1384 break;
1385 case EOpGreaterThanEqual:
1386 outputTriplet(out, visit, "(", " >= ", ")");
1387 break;
1388 case EOpVectorTimesScalar:
1389 outputTriplet(out, visit, "(", " * ", ")");
1390 break;
1391 case EOpMatrixTimesScalar:
1392 outputTriplet(out, visit, "(", " * ", ")");
1393 break;
1394 case EOpVectorTimesMatrix:
1395 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1396 break;
1397 case EOpMatrixTimesVector:
1398 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1399 break;
1400 case EOpMatrixTimesMatrix:
1401 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1402 break;
1403 case EOpLogicalOr:
1404 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1405 // been unfolded.
1406 ASSERT(!node->getRight()->hasSideEffects());
1407 outputTriplet(out, visit, "(", " || ", ")");
1408 return true;
1409 case EOpLogicalXor:
1410 mUsesXor = true;
1411 outputTriplet(out, visit, "xor(", ", ", ")");
1412 break;
1413 case EOpLogicalAnd:
1414 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1415 // been unfolded.
1416 ASSERT(!node->getRight()->hasSideEffects());
1417 outputTriplet(out, visit, "(", " && ", ")");
1418 return true;
1419 default:
1420 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001421 }
1422
1423 return true;
1424}
1425
1426bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1427{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001428 TInfoSinkBase &out = getInfoSink();
1429
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001430 switch (node->getOp())
1431 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001432 case EOpNegative:
1433 outputTriplet(out, visit, "(-", "", ")");
1434 break;
1435 case EOpPositive:
1436 outputTriplet(out, visit, "(+", "", ")");
1437 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001438 case EOpLogicalNot:
1439 outputTriplet(out, visit, "(!", "", ")");
1440 break;
1441 case EOpBitwiseNot:
1442 outputTriplet(out, visit, "(~", "", ")");
1443 break;
1444 case EOpPostIncrement:
1445 outputTriplet(out, visit, "(", "", "++)");
1446 break;
1447 case EOpPostDecrement:
1448 outputTriplet(out, visit, "(", "", "--)");
1449 break;
1450 case EOpPreIncrement:
1451 outputTriplet(out, visit, "(++", "", ")");
1452 break;
1453 case EOpPreDecrement:
1454 outputTriplet(out, visit, "(--", "", ")");
1455 break;
1456 case EOpRadians:
1457 outputTriplet(out, visit, "radians(", "", ")");
1458 break;
1459 case EOpDegrees:
1460 outputTriplet(out, visit, "degrees(", "", ")");
1461 break;
1462 case EOpSin:
1463 outputTriplet(out, visit, "sin(", "", ")");
1464 break;
1465 case EOpCos:
1466 outputTriplet(out, visit, "cos(", "", ")");
1467 break;
1468 case EOpTan:
1469 outputTriplet(out, visit, "tan(", "", ")");
1470 break;
1471 case EOpAsin:
1472 outputTriplet(out, visit, "asin(", "", ")");
1473 break;
1474 case EOpAcos:
1475 outputTriplet(out, visit, "acos(", "", ")");
1476 break;
1477 case EOpAtan:
1478 outputTriplet(out, visit, "atan(", "", ")");
1479 break;
1480 case EOpSinh:
1481 outputTriplet(out, visit, "sinh(", "", ")");
1482 break;
1483 case EOpCosh:
1484 outputTriplet(out, visit, "cosh(", "", ")");
1485 break;
1486 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001487 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001488 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001489 case EOpAtanh:
1490 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001491 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001492 break;
1493 case EOpExp:
1494 outputTriplet(out, visit, "exp(", "", ")");
1495 break;
1496 case EOpLog:
1497 outputTriplet(out, visit, "log(", "", ")");
1498 break;
1499 case EOpExp2:
1500 outputTriplet(out, visit, "exp2(", "", ")");
1501 break;
1502 case EOpLog2:
1503 outputTriplet(out, visit, "log2(", "", ")");
1504 break;
1505 case EOpSqrt:
1506 outputTriplet(out, visit, "sqrt(", "", ")");
1507 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001508 case EOpInversesqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001509 outputTriplet(out, visit, "rsqrt(", "", ")");
1510 break;
1511 case EOpAbs:
1512 outputTriplet(out, visit, "abs(", "", ")");
1513 break;
1514 case EOpSign:
1515 outputTriplet(out, visit, "sign(", "", ")");
1516 break;
1517 case EOpFloor:
1518 outputTriplet(out, visit, "floor(", "", ")");
1519 break;
1520 case EOpTrunc:
1521 outputTriplet(out, visit, "trunc(", "", ")");
1522 break;
1523 case EOpRound:
1524 outputTriplet(out, visit, "round(", "", ")");
1525 break;
1526 case EOpRoundEven:
1527 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001528 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001529 break;
1530 case EOpCeil:
1531 outputTriplet(out, visit, "ceil(", "", ")");
1532 break;
1533 case EOpFract:
1534 outputTriplet(out, visit, "frac(", "", ")");
1535 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001536 case EOpIsnan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001537 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001538 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001539 else
1540 outputTriplet(out, visit, "isnan(", "", ")");
1541 mRequiresIEEEStrictCompiling = true;
1542 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001543 case EOpIsinf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001544 outputTriplet(out, visit, "isinf(", "", ")");
1545 break;
1546 case EOpFloatBitsToInt:
1547 outputTriplet(out, visit, "asint(", "", ")");
1548 break;
1549 case EOpFloatBitsToUint:
1550 outputTriplet(out, visit, "asuint(", "", ")");
1551 break;
1552 case EOpIntBitsToFloat:
1553 outputTriplet(out, visit, "asfloat(", "", ")");
1554 break;
1555 case EOpUintBitsToFloat:
1556 outputTriplet(out, visit, "asfloat(", "", ")");
1557 break;
1558 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001559 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001560 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001561 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001562 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001563 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001564 case EOpPackUnorm4x8:
1565 case EOpPackSnorm4x8:
1566 case EOpUnpackUnorm4x8:
1567 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001568 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001569 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001570 break;
1571 case EOpLength:
1572 outputTriplet(out, visit, "length(", "", ")");
1573 break;
1574 case EOpNormalize:
1575 outputTriplet(out, visit, "normalize(", "", ")");
1576 break;
1577 case EOpDFdx:
1578 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1579 {
1580 outputTriplet(out, visit, "(", "", ", 0.0)");
1581 }
1582 else
1583 {
1584 outputTriplet(out, visit, "ddx(", "", ")");
1585 }
1586 break;
1587 case EOpDFdy:
1588 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1589 {
1590 outputTriplet(out, visit, "(", "", ", 0.0)");
1591 }
1592 else
1593 {
1594 outputTriplet(out, visit, "ddy(", "", ")");
1595 }
1596 break;
1597 case EOpFwidth:
1598 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1599 {
1600 outputTriplet(out, visit, "(", "", ", 0.0)");
1601 }
1602 else
1603 {
1604 outputTriplet(out, visit, "fwidth(", "", ")");
1605 }
1606 break;
1607 case EOpTranspose:
1608 outputTriplet(out, visit, "transpose(", "", ")");
1609 break;
1610 case EOpDeterminant:
1611 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1612 break;
1613 case EOpInverse:
1614 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001615 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001616 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001617
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001618 case EOpAny:
1619 outputTriplet(out, visit, "any(", "", ")");
1620 break;
1621 case EOpAll:
1622 outputTriplet(out, visit, "all(", "", ")");
1623 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001624 case EOpLogicalNotComponentWise:
1625 outputTriplet(out, visit, "(!", "", ")");
1626 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001627 case EOpBitfieldReverse:
1628 outputTriplet(out, visit, "reversebits(", "", ")");
1629 break;
1630 case EOpBitCount:
1631 outputTriplet(out, visit, "countbits(", "", ")");
1632 break;
1633 case EOpFindLSB:
1634 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1635 // in GLSLTest and results are consistent with GL.
1636 outputTriplet(out, visit, "firstbitlow(", "", ")");
1637 break;
1638 case EOpFindMSB:
1639 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1640 // tested in GLSLTest and results are consistent with GL.
1641 outputTriplet(out, visit, "firstbithigh(", "", ")");
1642 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001643 default:
1644 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001645 }
1646
1647 return true;
1648}
1649
Olli Etuahofbb1c792018-01-19 16:26:59 +02001650ImmutableString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
Olli Etuaho96963162016-03-21 11:54:33 +02001651{
1652 if (node->getAsSymbolNode())
1653 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001654 ASSERT(node->getAsSymbolNode()->variable().symbolType() != SymbolType::Empty);
1655 return node->getAsSymbolNode()->getName();
Olli Etuaho96963162016-03-21 11:54:33 +02001656 }
1657 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1658 switch (nodeBinary->getOp())
1659 {
1660 case EOpIndexDirect:
1661 {
1662 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1663
Olli Etuahofbb1c792018-01-19 16:26:59 +02001664 std::stringstream prefixSink;
Olli Etuaho96963162016-03-21 11:54:33 +02001665 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
Olli Etuahofbb1c792018-01-19 16:26:59 +02001666 return ImmutableString(prefixSink.str());
Olli Etuaho96963162016-03-21 11:54:33 +02001667 }
1668 case EOpIndexDirectStruct:
1669 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001670 const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001671 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1672 const TField *field = s->fields()[index];
1673
Olli Etuahofbb1c792018-01-19 16:26:59 +02001674 std::stringstream prefixSink;
Olli Etuaho96963162016-03-21 11:54:33 +02001675 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1676 << field->name();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001677 return ImmutableString(prefixSink.str());
Olli Etuaho96963162016-03-21 11:54:33 +02001678 }
1679 default:
1680 UNREACHABLE();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001681 return ImmutableString("");
Olli Etuaho96963162016-03-21 11:54:33 +02001682 }
1683}
1684
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001685bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1686{
1687 TInfoSinkBase &out = getInfoSink();
1688
1689 if (mInsideFunction)
1690 {
1691 outputLineDirective(out, node->getLine().first_line);
1692 out << "{\n";
1693 }
1694
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001695 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001696 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001697 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001698
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001699 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001700
1701 // Don't output ; after case labels, they're terminated by :
1702 // This is needed especially since outputting a ; after a case statement would turn empty
1703 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001704 // Also the output code is clearer if we don't output ; after statements where it is not
1705 // needed:
1706 // * if statements
1707 // * switch statements
1708 // * blocks
1709 // * function definitions
1710 // * loops (do-while loops output the semicolon in VisitLoop)
1711 // * declarations that don't generate output.
1712 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1713 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1714 statement->getAsSwitchNode() == nullptr &&
1715 statement->getAsFunctionDefinition() == nullptr &&
1716 (statement->getAsDeclarationNode() == nullptr ||
1717 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1718 statement->getAsInvariantDeclarationNode() == nullptr)
1719 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001720 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001721 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001722 }
1723
1724 if (mInsideFunction)
1725 {
1726 outputLineDirective(out, node->getLine().last_line);
1727 out << "}\n";
1728 }
1729
1730 return false;
1731}
1732
Olli Etuaho336b1472016-10-05 16:37:55 +01001733bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1734{
1735 TInfoSinkBase &out = getInfoSink();
1736
1737 ASSERT(mCurrentFunctionMetadata == nullptr);
1738
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001739 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho336b1472016-10-05 16:37:55 +01001740 ASSERT(index != CallDAG::InvalidIndex);
1741 mCurrentFunctionMetadata = &mASTMetadataList[index];
1742
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001743 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001744
Olli Etuahod4bd9632018-03-08 16:32:44 +02001745 const TFunction *func = node->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001746
Olli Etuahod4bd9632018-03-08 16:32:44 +02001747 if (func->isMain())
Olli Etuaho336b1472016-10-05 16:37:55 +01001748 {
1749 out << "gl_main(";
1750 }
1751 else
1752 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001753 out << DecorateFunctionIfNeeded(func) << DisambiguateFunctionName(func)
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001754 << (mOutputLod0Function ? "Lod0(" : "(");
Olli Etuaho336b1472016-10-05 16:37:55 +01001755 }
1756
Olli Etuahod4bd9632018-03-08 16:32:44 +02001757 size_t paramCount = func->getParamCount();
1758 for (unsigned int i = 0; i < paramCount; i++)
Olli Etuaho336b1472016-10-05 16:37:55 +01001759 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001760 const TVariable *param = func->getParam(i);
1761 ensureStructDefined(param->getType());
Olli Etuaho336b1472016-10-05 16:37:55 +01001762
Olli Etuahod4bd9632018-03-08 16:32:44 +02001763 writeParameter(param, out);
1764
1765 if (i < paramCount - 1)
Olli Etuaho336b1472016-10-05 16:37:55 +01001766 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001767 out << ", ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001768 }
Olli Etuaho336b1472016-10-05 16:37:55 +01001769 }
1770
1771 out << ")\n";
1772
1773 mInsideFunction = true;
1774 // The function body node will output braces.
1775 node->getBody()->traverse(this);
1776 mInsideFunction = false;
1777
1778 mCurrentFunctionMetadata = nullptr;
1779
1780 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1781 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1782 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001783 ASSERT(!node->getFunction()->isMain());
Olli Etuaho336b1472016-10-05 16:37:55 +01001784 mOutputLod0Function = true;
1785 node->traverse(this);
1786 mOutputLod0Function = false;
1787 }
1788
1789 return false;
1790}
1791
Olli Etuaho13389b62016-10-16 11:48:18 +01001792bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1793{
Olli Etuaho13389b62016-10-16 11:48:18 +01001794 if (visit == PreVisit)
1795 {
1796 TIntermSequence *sequence = node->getSequence();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001797 TIntermTyped *declarator = (*sequence)[0]->getAsTyped();
Olli Etuaho13389b62016-10-16 11:48:18 +01001798 ASSERT(sequence->size() == 1);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001799 ASSERT(declarator);
Olli Etuaho13389b62016-10-16 11:48:18 +01001800
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001801 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001802 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001803 TInfoSinkBase &out = getInfoSink();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001804 ensureStructDefined(declarator->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001805
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001806 if (!declarator->getAsSymbolNode() ||
1807 declarator->getAsSymbolNode()->variable().symbolType() !=
1808 SymbolType::Empty) // Variable declaration
Olli Etuaho13389b62016-10-16 11:48:18 +01001809 {
1810 if (!mInsideFunction)
1811 {
1812 out << "static ";
1813 }
1814
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001815 out << TypeString(declarator->getType()) + " ";
Olli Etuaho13389b62016-10-16 11:48:18 +01001816
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001817 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho13389b62016-10-16 11:48:18 +01001818
1819 if (symbol)
1820 {
1821 symbol->traverse(this);
1822 out << ArrayString(symbol->getType());
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001823 out << " = " + zeroInitializer(symbol->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001824 }
1825 else
1826 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001827 declarator->traverse(this);
Olli Etuaho13389b62016-10-16 11:48:18 +01001828 }
1829 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001830 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001831 else if (IsVaryingOut(declarator->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001832 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001833 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho282847e2017-07-12 14:11:01 +03001834 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001835
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001836 const TVariable &variable = symbol->variable();
1837
1838 if (variable.symbolType() != SymbolType::Empty)
Olli Etuaho93b059d2017-12-20 12:46:58 +02001839 {
1840 // Vertex outputs which are declared but not written to should still be declared to
1841 // allow successful linking.
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001842 mReferencedVaryings[symbol->uniqueId().get()] = &variable;
Olli Etuaho93b059d2017-12-20 12:46:58 +02001843 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001844 }
1845 }
1846 return false;
1847}
1848
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001849bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1850{
1851 // Do not do any translation
1852 return false;
1853}
1854
Olli Etuahod4bd9632018-03-08 16:32:44 +02001855void OutputHLSL::visitFunctionPrototype(TIntermFunctionPrototype *node)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001856{
1857 TInfoSinkBase &out = getInfoSink();
1858
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001859 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001860 // Skip the prototype if it is not implemented (and thus not used)
1861 if (index == CallDAG::InvalidIndex)
1862 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001863 return;
Olli Etuaho16c745a2017-01-16 17:02:27 +00001864 }
1865
Olli Etuahod4bd9632018-03-08 16:32:44 +02001866 const TFunction *func = node->getFunction();
Olli Etuaho16c745a2017-01-16 17:02:27 +00001867
Olli Etuahod4bd9632018-03-08 16:32:44 +02001868 TString name = DecorateFunctionIfNeeded(func);
1869 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(func)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001870 << (mOutputLod0Function ? "Lod0(" : "(");
1871
Olli Etuahod4bd9632018-03-08 16:32:44 +02001872 size_t paramCount = func->getParamCount();
1873 for (unsigned int i = 0; i < paramCount; i++)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001874 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001875 writeParameter(func->getParam(i), out);
Olli Etuaho16c745a2017-01-16 17:02:27 +00001876
Olli Etuahod4bd9632018-03-08 16:32:44 +02001877 if (i < paramCount - 1)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001878 {
1879 out << ", ";
1880 }
1881 }
1882
1883 out << ");\n";
1884
1885 // Also prototype the Lod0 variant if needed
1886 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1887 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1888 {
1889 mOutputLod0Function = true;
1890 node->traverse(this);
1891 mOutputLod0Function = false;
1892 }
Olli Etuaho16c745a2017-01-16 17:02:27 +00001893}
1894
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001895bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1896{
Jamie Madill32aab012015-01-27 14:12:26 -05001897 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001899 switch (node->getOp())
1900 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001901 case EOpCallBuiltInFunction:
1902 case EOpCallFunctionInAST:
1903 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001905 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001906
Corentin Wallez1239ee92015-03-19 14:38:02 -07001907 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001908 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001910 if (node->isArray())
1911 {
1912 UNIMPLEMENTED();
1913 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001914 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001915 ASSERT(index != CallDAG::InvalidIndex);
1916 lod0 &= mASTMetadataList[index].mNeedsLod0;
1917
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001918 out << DecorateFunctionIfNeeded(node->getFunction());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001919 out << DisambiguateFunctionName(node->getSequence());
1920 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001922 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001923 {
1924 // This path is used for internal functions that don't have their definitions in the
1925 // AST, such as precision emulation functions.
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001926 out << DecorateFunctionIfNeeded(node->getFunction()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001927 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001928 else if (node->getFunction()->isImageFunction())
Xinghua Cao711b7a12017-10-09 13:38:12 +08001929 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001930 const ImmutableString &name = node->getFunction()->name();
Xinghua Cao711b7a12017-10-09 13:38:12 +08001931 TType type = (*arguments)[0]->getAsTyped()->getType();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001932 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001933 name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
Xinghua Cao711b7a12017-10-09 13:38:12 +08001934 type.getMemoryQualifier().readonly);
1935 out << imageFunctionName << "(";
1936 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937 else
1938 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001939 const ImmutableString &name = node->getFunction()->name();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001940 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001941 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1942 if (arguments->size() > 1)
1943 {
1944 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1945 }
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001946 const ImmutableString &textureFunctionName =
1947 mTextureFunctionHLSL->useTextureFunction(name, samplerType, coords,
1948 arguments->size(), lod0, mShaderType);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001949 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001950 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001951
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001952 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001953 {
Olli Etuaho96963162016-03-21 11:54:33 +02001954 TIntermTyped *typedArg = (*arg)->getAsTyped();
1955 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001956 {
1957 out << "texture_";
1958 (*arg)->traverse(this);
1959 out << ", sampler_";
1960 }
1961
1962 (*arg)->traverse(this);
1963
Olli Etuaho96963162016-03-21 11:54:33 +02001964 if (typedArg->getType().isStructureContainingSamplers())
1965 {
1966 const TType &argType = typedArg->getType();
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001967 TVector<const TVariable *> samplerSymbols;
Olli Etuahofbb1c792018-01-19 16:26:59 +02001968 ImmutableString structName = samplerNamePrefixFromStruct(typedArg);
1969 std::string namePrefix = "angle_";
1970 namePrefix += structName.data();
1971 argType.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols,
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001972 nullptr, mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001973 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02001974 {
1975 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1976 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001977 out << ", texture_" << sampler->name();
1978 out << ", sampler_" << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001979 }
1980 else
1981 {
1982 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1983 // of D3D9, it's the sampler variable.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001984 out << ", " << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001985 }
1986 }
1987 }
1988
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001989 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001990 {
1991 out << ", ";
1992 }
1993 }
1994
1995 out << ")";
1996
1997 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03001999 case EOpConstruct:
Olli Etuahobd3cd502017-11-03 15:48:52 +02002000 outputConstructor(out, visit, node);
Olli Etuaho8fab3202017-05-08 18:22:22 +03002001 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002002 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002003 outputTriplet(out, visit, "(", " == ", ")");
2004 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002005 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002006 outputTriplet(out, visit, "(", " != ", ")");
2007 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002008 case EOpLessThanComponentWise:
2009 outputTriplet(out, visit, "(", " < ", ")");
2010 break;
2011 case EOpGreaterThanComponentWise:
2012 outputTriplet(out, visit, "(", " > ", ")");
2013 break;
2014 case EOpLessThanEqualComponentWise:
2015 outputTriplet(out, visit, "(", " <= ", ")");
2016 break;
2017 case EOpGreaterThanEqualComponentWise:
2018 outputTriplet(out, visit, "(", " >= ", ")");
2019 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002020 case EOpMod:
2021 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002022 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002023 break;
2024 case EOpModf:
2025 outputTriplet(out, visit, "modf(", ", ", ")");
2026 break;
2027 case EOpPow:
2028 outputTriplet(out, visit, "pow(", ", ", ")");
2029 break;
2030 case EOpAtan:
2031 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2032 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002033 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002034 break;
2035 case EOpMin:
2036 outputTriplet(out, visit, "min(", ", ", ")");
2037 break;
2038 case EOpMax:
2039 outputTriplet(out, visit, "max(", ", ", ")");
2040 break;
2041 case EOpClamp:
2042 outputTriplet(out, visit, "clamp(", ", ", ")");
2043 break;
2044 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302045 {
2046 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2047 if (lastParamNode->getType().getBasicType() == EbtBool)
2048 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002049 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2050 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302051 // so use emulated version.
2052 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002053 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302054 }
2055 else
2056 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002057 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302058 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002059 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302060 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002061 case EOpStep:
2062 outputTriplet(out, visit, "step(", ", ", ")");
2063 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02002064 case EOpSmoothstep:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002065 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2066 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002067 case EOpFrexp:
2068 case EOpLdexp:
2069 ASSERT(node->getUseEmulatedFunction());
2070 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2071 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002072 case EOpDistance:
2073 outputTriplet(out, visit, "distance(", ", ", ")");
2074 break;
2075 case EOpDot:
2076 outputTriplet(out, visit, "dot(", ", ", ")");
2077 break;
2078 case EOpCross:
2079 outputTriplet(out, visit, "cross(", ", ", ")");
2080 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002081 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002082 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002083 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002084 break;
2085 case EOpReflect:
2086 outputTriplet(out, visit, "reflect(", ", ", ")");
2087 break;
2088 case EOpRefract:
2089 outputTriplet(out, visit, "refract(", ", ", ")");
2090 break;
2091 case EOpOuterProduct:
2092 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002093 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002094 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002095 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002096 outputTriplet(out, visit, "(", " * ", ")");
2097 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002098 case EOpBitfieldExtract:
2099 case EOpBitfieldInsert:
2100 case EOpUaddCarry:
2101 case EOpUsubBorrow:
2102 case EOpUmulExtended:
2103 case EOpImulExtended:
2104 ASSERT(node->getUseEmulatedFunction());
2105 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2106 break;
Xinghua Cao47335852018-02-12 15:41:55 +08002107 case EOpBarrier:
2108 // barrier() is translated to GroupMemoryBarrierWithGroupSync(), which is the
2109 // cheapest *WithGroupSync() function, without any functionality loss, but
2110 // with the potential for severe performance loss.
2111 outputTriplet(out, visit, "GroupMemoryBarrierWithGroupSync(", "", ")");
2112 break;
2113 case EOpMemoryBarrierShared:
2114 outputTriplet(out, visit, "GroupMemoryBarrier(", "", ")");
2115 break;
2116 case EOpMemoryBarrierAtomicCounter:
2117 case EOpMemoryBarrierBuffer:
2118 case EOpMemoryBarrierImage:
2119 outputTriplet(out, visit, "DeviceMemoryBarrier(", "", ")");
2120 break;
2121 case EOpGroupMemoryBarrier:
2122 case EOpMemoryBarrier:
2123 outputTriplet(out, visit, "AllMemoryBarrier(", "", ")");
2124 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002125 default:
2126 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 }
2128
2129 return true;
2130}
2131
Olli Etuaho57961272016-09-14 13:57:46 +03002132void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002134 out << "if (";
2135
2136 node->getCondition()->traverse(this);
2137
2138 out << ")\n";
2139
Jamie Madill8c46ab12015-12-07 16:39:19 -05002140 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002141
2142 bool discard = false;
2143
2144 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002146 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002147 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002148
Olli Etuahoa6f22092015-05-08 18:31:10 +03002149 // Detect true discard
2150 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2151 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002152 else
2153 {
2154 // TODO(oetuaho): Check if the semicolon inside is necessary.
2155 // It's there as a result of conservative refactoring of the output.
2156 out << "{;}\n";
2157 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002158
Jamie Madill8c46ab12015-12-07 16:39:19 -05002159 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002160
Olli Etuahoa6f22092015-05-08 18:31:10 +03002161 if (node->getFalseBlock())
2162 {
2163 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002164
Jamie Madill8c46ab12015-12-07 16:39:19 -05002165 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166
Olli Etuaho32db19b2016-10-04 14:43:16 +01002167 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002168 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002169
Jamie Madill8c46ab12015-12-07 16:39:19 -05002170 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002171
Olli Etuahoa6f22092015-05-08 18:31:10 +03002172 // Detect false discard
2173 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2174 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002175
Olli Etuahoa6f22092015-05-08 18:31:10 +03002176 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002177 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002178 {
2179 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002180 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002181}
2182
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002183bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2184{
2185 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2186 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2187 UNREACHABLE();
2188 return false;
2189}
2190
Olli Etuaho57961272016-09-14 13:57:46 +03002191bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002192{
2193 TInfoSinkBase &out = getInfoSink();
2194
Olli Etuaho3d932d82016-04-12 11:10:30 +03002195 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002196
2197 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002198 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002199 {
2200 out << "FLATTEN ";
2201 }
2202
Olli Etuaho57961272016-09-14 13:57:46 +03002203 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204
2205 return false;
2206}
2207
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002208bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002209{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002210 TInfoSinkBase &out = getInfoSink();
2211
Olli Etuaho923ecef2017-10-11 12:01:38 +03002212 ASSERT(node->getStatementList());
2213 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002214 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002215 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002216 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002217 outputTriplet(out, visit, "switch (", ") ", "");
2218 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002219 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002220}
2221
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002222bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002223{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002224 TInfoSinkBase &out = getInfoSink();
2225
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002226 if (node->hasCondition())
2227 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002228 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002229 return true;
2230 }
2231 else
2232 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002233 out << "default:\n";
2234 return false;
2235 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002236}
2237
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2239{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002240 TInfoSinkBase &out = getInfoSink();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002241 writeConstantUnion(out, node->getType(), node->getConstantValue());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242}
2243
2244bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2245{
Nicolas Capens655fe362014-04-11 13:12:34 -04002246 mNestedLoopDepth++;
2247
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002248 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002249 mInsideDiscontinuousLoop =
2250 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002251
Jamie Madill8c46ab12015-12-07 16:39:19 -05002252 TInfoSinkBase &out = getInfoSink();
2253
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002254 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002255 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002256 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002257 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002258 mInsideDiscontinuousLoop = wasDiscontinuous;
2259 mNestedLoopDepth--;
2260
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002261 return false;
2262 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002263 }
2264
Corentin Wallez1239ee92015-03-19 14:38:02 -07002265 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002266 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002268 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002269
Jamie Madill8c46ab12015-12-07 16:39:19 -05002270 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 }
2272 else
2273 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002274 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002275
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 if (node->getInit())
2277 {
2278 node->getInit()->traverse(this);
2279 }
2280
2281 out << "; ";
2282
alokp@chromium.org52813552010-11-16 18:36:09 +00002283 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002284 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002285 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 }
2287
2288 out << "; ";
2289
alokp@chromium.org52813552010-11-16 18:36:09 +00002290 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002292 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 }
2294
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002295 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002296
Jamie Madill8c46ab12015-12-07 16:39:19 -05002297 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298 }
2299
2300 if (node->getBody())
2301 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002302 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002303 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002305 else
2306 {
2307 // TODO(oetuaho): Check if the semicolon inside is necessary.
2308 // It's there as a result of conservative refactoring of the output.
2309 out << "{;}\n";
2310 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311
Jamie Madill8c46ab12015-12-07 16:39:19 -05002312 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313
alokp@chromium.org52813552010-11-16 18:36:09 +00002314 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002316 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002317 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318
alokp@chromium.org52813552010-11-16 18:36:09 +00002319 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002321 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322 }
2323
daniel@transgaming.com73536982012-03-21 20:45:49 +00002324 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002326 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002327 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002328
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329 return false;
2330}
2331
2332bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2333{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002334 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002336 TInfoSinkBase &out = getInfoSink();
2337
2338 switch (node->getFlowOp())
2339 {
2340 case EOpKill:
2341 out << "discard";
2342 break;
2343 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002344 if (mNestedLoopDepth > 1)
2345 {
2346 mUsesNestedBreak = true;
2347 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002348
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002349 if (mExcessiveLoopIndex)
2350 {
2351 out << "{Break";
2352 mExcessiveLoopIndex->traverse(this);
2353 out << " = true; break;}\n";
2354 }
2355 else
2356 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002357 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002358 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002359 break;
2360 case EOpContinue:
2361 out << "continue";
2362 break;
2363 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002364 if (node->getExpression())
2365 {
2366 out << "return ";
2367 }
2368 else
2369 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002370 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002371 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002372 break;
2373 default:
2374 UNREACHABLE();
2375 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002376 }
2377
2378 return true;
2379}
2380
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002381// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002382// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2383// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002384bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002385{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002386 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002387
2388 // Parse loops of the form:
2389 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002390 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002391 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002392 int initial = 0;
2393 int limit = 0;
2394 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002395
2396 // Parse index name and intial value
2397 if (node->getInit())
2398 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002399 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002400
2401 if (init)
2402 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002403 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002404 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002405
2406 if (variable && variable->getQualifier() == EvqTemporary)
2407 {
2408 TIntermBinary *assign = variable->getAsBinaryNode();
2409
2410 if (assign->getOp() == EOpInitialize)
2411 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002412 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002413 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2414
2415 if (symbol && constant)
2416 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002417 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002418 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002419 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002420 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421 }
2422 }
2423 }
2424 }
2425 }
2426 }
2427
2428 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002429 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002430 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002431 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002432
Olli Etuahob6af22b2017-12-15 14:05:44 +02002433 if (test && test->getLeft()->getAsSymbolNode()->uniqueId() == index->uniqueId())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002434 {
2435 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2436
2437 if (constant)
2438 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002439 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002440 {
2441 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002442 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002443 }
2444 }
2445 }
2446 }
2447
2448 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002449 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002450 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002451 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002452 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002453
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002454 if (binaryTerminal)
2455 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002456 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002457 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2458
2459 if (constant)
2460 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002461 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002462 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002463 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002464
2465 switch (op)
2466 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002467 case EOpAddAssign:
2468 increment = value;
2469 break;
2470 case EOpSubAssign:
2471 increment = -value;
2472 break;
2473 default:
2474 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002475 }
2476 }
2477 }
2478 }
2479 else if (unaryTerminal)
2480 {
2481 TOperator op = unaryTerminal->getOp();
2482
2483 switch (op)
2484 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002485 case EOpPostIncrement:
2486 increment = 1;
2487 break;
2488 case EOpPostDecrement:
2489 increment = -1;
2490 break;
2491 case EOpPreIncrement:
2492 increment = 1;
2493 break;
2494 case EOpPreDecrement:
2495 increment = -1;
2496 break;
2497 default:
2498 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002499 }
2500 }
2501 }
2502
Yunchao He4f285442017-04-21 12:15:49 +08002503 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002504 {
2505 if (comparator == EOpLessThanEqual)
2506 {
2507 comparator = EOpLessThan;
2508 limit += 1;
2509 }
2510
2511 if (comparator == EOpLessThan)
2512 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002513 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002514
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002515 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002516 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002517 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002518 }
2519
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002520 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002521 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002522
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002523 out << "{int ";
2524 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002525 out << ";\n"
2526 "bool Break";
2527 index->traverse(this);
2528 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002529
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002530 bool firstLoopFragment = true;
2531
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002532 while (iterations > 0)
2533 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002534 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002535
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002536 if (!firstLoopFragment)
2537 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002538 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002539 index->traverse(this);
2540 out << ") {\n";
2541 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002542
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002543 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002544 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002545 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002546 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002547
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002548 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002549 const char *unroll =
2550 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002551
Corentin Wallez1239ee92015-03-19 14:38:02 -07002552 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002553 index->traverse(this);
2554 out << " = ";
2555 out << initial;
2556
2557 out << "; ";
2558 index->traverse(this);
2559 out << " < ";
2560 out << clampedLimit;
2561
2562 out << "; ";
2563 index->traverse(this);
2564 out << " += ";
2565 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002566 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002567
Jamie Madill8c46ab12015-12-07 16:39:19 -05002568 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002569 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002570
2571 if (node->getBody())
2572 {
2573 node->getBody()->traverse(this);
2574 }
2575
Jamie Madill8c46ab12015-12-07 16:39:19 -05002576 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002577 out << ";}\n";
2578
2579 if (!firstLoopFragment)
2580 {
2581 out << "}\n";
2582 }
2583
2584 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002585
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002586 initial += MAX_LOOP_ITERATIONS * increment;
2587 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002588 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002589
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002590 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002591
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002592 mExcessiveLoopIndex = restoreIndex;
2593
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002594 return true;
2595 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002596 else
2597 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002598 }
2599
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002600 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002601}
2602
Jamie Madill8c46ab12015-12-07 16:39:19 -05002603void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2604 Visit visit,
2605 const char *preString,
2606 const char *inString,
2607 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002608{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002609 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002610 {
2611 out << preString;
2612 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002613 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 {
2615 out << inString;
2616 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002617 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002618 {
2619 out << postString;
2620 }
2621}
2622
Jamie Madill8c46ab12015-12-07 16:39:19 -05002623void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002624{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002625 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002626 {
Jamie Madill32aab012015-01-27 14:12:26 -05002627 out << "\n";
2628 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002629
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002630 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002631 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002632 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002633 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002634
Jamie Madill32aab012015-01-27 14:12:26 -05002635 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002636 }
2637}
2638
Olli Etuahod4bd9632018-03-08 16:32:44 +02002639void OutputHLSL::writeParameter(const TVariable *param, TInfoSinkBase &out)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002640{
Olli Etuahod4bd9632018-03-08 16:32:44 +02002641 const TType &type = param->getType();
2642 TQualifier qualifier = type.getQualifier();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002643
Olli Etuahod4bd9632018-03-08 16:32:44 +02002644 TString nameStr = DecorateVariableIfNeeded(*param);
2645 ASSERT(nameStr != ""); // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002646
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002647 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002648 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002649 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2650 {
2651 // Samplers are passed as indices to the sampler array.
2652 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002653 out << "const uint " << nameStr << ArrayString(type);
2654 return;
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002655 }
2656 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2657 {
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002658 out << QualifierString(qualifier) << " " << TextureString(type.getBasicType())
2659 << " texture_" << nameStr << ArrayString(type) << ", " << QualifierString(qualifier)
2660 << " " << SamplerString(type.getBasicType()) << " sampler_" << nameStr
2661 << ArrayString(type);
2662 return;
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002663 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002664 }
2665
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002666 out << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2667 << ArrayString(type);
Olli Etuaho96963162016-03-21 11:54:33 +02002668
2669 // If the structure parameter contains samplers, they need to be passed into the function as
2670 // separate parameters. HLSL doesn't natively support samplers in structs.
2671 if (type.isStructureContainingSamplers())
2672 {
2673 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002674 TVector<const TVariable *> samplerSymbols;
Olli Etuahofbb1c792018-01-19 16:26:59 +02002675 std::string namePrefix = "angle";
2676 namePrefix += nameStr.c_str();
2677 type.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols, nullptr,
2678 mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002679 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02002680 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002681 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002682 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2683 {
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002684 out << ", const uint " << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002685 }
2686 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2687 {
Olli Etuaho96963162016-03-21 11:54:33 +02002688 ASSERT(IsSampler(samplerType.getBasicType()));
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002689 out << ", " << QualifierString(qualifier) << " "
2690 << TextureString(samplerType.getBasicType()) << " texture_" << sampler->name()
2691 << ArrayString(samplerType) << ", " << QualifierString(qualifier) << " "
2692 << SamplerString(samplerType.getBasicType()) << " sampler_" << sampler->name()
2693 << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002694 }
2695 else
2696 {
Olli Etuaho96963162016-03-21 11:54:33 +02002697 ASSERT(IsSampler(samplerType.getBasicType()));
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002698 out << ", " << QualifierString(qualifier) << " " << TypeString(samplerType) << " "
2699 << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002700 }
2701 }
2702 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703}
2704
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002705TString OutputHLSL::zeroInitializer(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002706{
2707 TString string;
2708
Jamie Madill94bf7f22013-07-08 13:31:15 -04002709 size_t size = type.getObjectSize();
2710 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002712 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713
Jamie Madill94bf7f22013-07-08 13:31:15 -04002714 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002715 {
2716 string += ", ";
2717 }
2718 }
2719
daniel@transgaming.comead23042010-04-29 03:35:36 +00002720 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002722
Olli Etuahobd3cd502017-11-03 15:48:52 +02002723void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002724{
Olli Etuahobd3cd502017-11-03 15:48:52 +02002725 // Array constructors should have been already pruned from the code.
2726 ASSERT(!node->getType().isArray());
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002727
2728 if (visit == PreVisit)
2729 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002730 TString constructorName;
2731 if (node->getBasicType() == EbtStruct)
2732 {
2733 constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct());
2734 }
2735 else
2736 {
2737 constructorName =
2738 mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence());
2739 }
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002740 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002741 }
2742 else if (visit == InVisit)
2743 {
2744 out << ", ";
2745 }
2746 else if (visit == PostVisit)
2747 {
2748 out << ")";
2749 }
2750}
2751
Jamie Madill8c46ab12015-12-07 16:39:19 -05002752const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2753 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002754 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002755{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002756 ASSERT(!type.isArray());
2757
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002758 const TConstantUnion *constUnionIterated = constUnion;
2759
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002760 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002761 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002762 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002763 out << mStructureHLSL->addStructConstructor(*structure) << "(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002764
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002765 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002766
Jamie Madill98493dd2013-07-08 14:39:03 -04002767 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002768 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002769 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002770 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002771
Jamie Madill98493dd2013-07-08 14:39:03 -04002772 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002773 {
2774 out << ", ";
2775 }
2776 }
2777
2778 out << ")";
2779 }
2780 else
2781 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002782 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002783 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002784
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002785 if (writeType)
2786 {
Jamie Madill033dae62014-06-18 12:56:28 -04002787 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002788 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002789 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002790 if (writeType)
2791 {
2792 out << ")";
2793 }
2794 }
2795
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002796 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002797}
2798
Olli Etuahod68924e2017-01-02 17:34:40 +00002799void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002800{
Olli Etuahod68924e2017-01-02 17:34:40 +00002801 if (visit == PreVisit)
2802 {
2803 const char *opStr = GetOperatorString(op);
2804 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2805 out << "(";
2806 }
2807 else
2808 {
2809 outputTriplet(out, visit, nullptr, ", ", ")");
2810 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002811}
2812
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002813bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2814 TIntermSymbol *symbolNode,
2815 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002816{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002817 ASSERT(symbolNode->variable().symbolType() != SymbolType::Empty);
2818 const TIntermSymbol *symbolInInitializer = FindSymbolNode(expression, symbolNode->getName());
Jamie Madill37997142015-01-28 10:06:34 -05002819
Olli Etuaho4728bdc2017-12-20 17:51:08 +02002820 if (symbolInInitializer)
Jamie Madill37997142015-01-28 10:06:34 -05002821 {
2822 // Type already printed
2823 out << "t" + str(mUniqueIndex) + " = ";
2824 expression->traverse(this);
2825 out << ", ";
2826 symbolNode->traverse(this);
2827 out << " = t" + str(mUniqueIndex);
2828
2829 mUniqueIndex++;
2830 return true;
2831 }
2832
2833 return false;
2834}
2835
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002836bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2837 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002838 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002839{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002840 if (initializer->hasConstantValue())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002841 {
2842 symbolNode->traverse(this);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002843 out << ArrayString(symbolNode->getType());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002844 out << " = {";
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002845 writeConstantUnionArray(out, initializer->getConstantValue(),
2846 initializer->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002847 out << "}";
2848 return true;
2849 }
2850 return false;
2851}
2852
Jamie Madill55e79e02015-02-09 15:35:00 -05002853TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2854{
2855 const TFieldList &fields = structure.fields();
2856
2857 for (const auto &eqFunction : mStructEqualityFunctions)
2858 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002859 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002860 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002861 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002862 }
2863 }
2864
2865 const TString &structNameString = StructNameString(structure);
2866
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002867 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002868 function->structure = &structure;
2869 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002870
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002871 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002872
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002873 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2874 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002875 << "{\n"
2876 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002877
2878 for (size_t i = 0; i < fields.size(); i++)
2879 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002880 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002881 const TType *fieldType = field->type();
2882
2883 const TString &fieldNameA = "a." + Decorate(field->name());
2884 const TString &fieldNameB = "b." + Decorate(field->name());
2885
2886 if (i > 0)
2887 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002888 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002889 }
2890
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002891 fnOut << "(";
2892 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2893 fnOut << fieldNameA;
2894 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2895 fnOut << fieldNameB;
2896 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2897 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002898 }
2899
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002900 fnOut << ";\n"
2901 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002902
2903 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002904
2905 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002906 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002907
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002908 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002909}
2910
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002911TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002912{
2913 for (const auto &eqFunction : mArrayEqualityFunctions)
2914 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002915 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002916 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002917 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002918 }
2919 }
2920
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002921 TType elementType(type);
2922 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002923
Olli Etuaho12690762015-03-31 12:55:28 +03002924 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002925 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002926
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002927 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002928
2929 TInfoSinkBase fnOut;
2930
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002931 const TString &typeName = TypeString(type);
2932 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2933 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002934 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002935 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002936 << type.getOutermostArraySize()
2937 << "; ++i)\n"
2938 " {\n"
2939 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002940
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002941 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002942 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002943 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002944 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002945 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002946
2947 fnOut << ") { return false; }\n"
2948 " }\n"
2949 " return true;\n"
2950 "}\n";
2951
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002952 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002953
2954 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002955 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002956
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002957 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002958}
2959
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002960TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002961{
2962 for (const auto &assignFunction : mArrayAssignmentFunctions)
2963 {
2964 if (assignFunction.type == type)
2965 {
2966 return assignFunction.functionName;
2967 }
2968 }
2969
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002970 TType elementType(type);
2971 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03002972
2973 ArrayHelperFunction function;
2974 function.type = type;
2975
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002976 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03002977
2978 TInfoSinkBase fnOut;
2979
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002980 const TString &typeName = TypeString(type);
2981 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
2982 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002983 << "{\n"
2984 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002985 << type.getOutermostArraySize()
2986 << "; ++i)\n"
2987 " {\n"
2988 " ";
2989
2990 outputAssign(PreVisit, elementType, fnOut);
2991 fnOut << "a[i]";
2992 outputAssign(InVisit, elementType, fnOut);
2993 fnOut << "b[i]";
2994 outputAssign(PostVisit, elementType, fnOut);
2995
2996 fnOut << ";\n"
2997 " }\n"
2998 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03002999
3000 function.functionDefinition = fnOut.c_str();
3001
3002 mArrayAssignmentFunctions.push_back(function);
3003
3004 return function.functionName;
3005}
3006
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003007TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003008{
3009 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3010 {
3011 if (constructIntoFunction.type == type)
3012 {
3013 return constructIntoFunction.functionName;
3014 }
3015 }
3016
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003017 TType elementType(type);
3018 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003019
3020 ArrayHelperFunction function;
3021 function.type = type;
3022
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003023 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003024
3025 TInfoSinkBase fnOut;
3026
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003027 const TString &typeName = TypeString(type);
3028 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3029 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003030 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003031 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003032 }
3033 fnOut << ")\n"
3034 "{\n";
3035
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003036 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003037 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003038 fnOut << " ";
3039 outputAssign(PreVisit, elementType, fnOut);
3040 fnOut << "a[" << i << "]";
3041 outputAssign(InVisit, elementType, fnOut);
3042 fnOut << "b" << i;
3043 outputAssign(PostVisit, elementType, fnOut);
3044 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003045 }
3046 fnOut << "}\n";
3047
3048 function.functionDefinition = fnOut.c_str();
3049
3050 mArrayConstructIntoFunctions.push_back(function);
3051
3052 return function.functionName;
3053}
3054
Jamie Madill2e295e22015-04-29 10:41:33 -04003055void OutputHLSL::ensureStructDefined(const TType &type)
3056{
Olli Etuahobd3cd502017-11-03 15:48:52 +02003057 const TStructure *structure = type.getStruct();
Jamie Madill2e295e22015-04-29 10:41:33 -04003058 if (structure)
3059 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02003060 ASSERT(type.getBasicType() == EbtStruct);
3061 mStructureHLSL->ensureStructDefined(*structure);
Jamie Madill2e295e22015-04-29 10:41:33 -04003062 }
3063}
3064
Jamie Madill45bcc782016-11-07 13:58:48 -05003065} // namespace sh