blob: 1f2b0662b9f57163e36030d7dfab412b73f16531 [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;
205 mUniformHLSL =
206 new UniformHLSL(shaderType, mStructureHLSL, outputType, uniforms, firstUniformRegister);
207
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200208 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000209 {
Arun Patole63419392015-03-13 11:51:07 +0530210 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500211 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
212 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530213 // In both cases total 3 uniform registers need to be reserved.
214 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000215 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000216
Geoff Lang00140f42016-02-03 18:47:33 +0000217 // Reserve registers for the default uniform block and driver constants
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800218 mUniformHLSL->reserveUniformBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219}
220
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000221OutputHLSL::~OutputHLSL()
222{
Jamie Madill8daaba12014-06-13 10:04:33 -0400223 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400224 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300225 SafeDelete(mTextureFunctionHLSL);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800226 SafeDelete(mImageFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200227 for (auto &eqFunction : mStructEqualityFunctions)
228 {
229 SafeDelete(eqFunction);
230 }
231 for (auto &eqFunction : mArrayEqualityFunctions)
232 {
233 SafeDelete(eqFunction);
234 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000235}
236
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200237void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000238{
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200239 BuiltInFunctionEmulator builtInFunctionEmulator;
240 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800241 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
242 {
243 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
244 mShaderVersion);
245 }
246
Olli Etuahodfa75e82017-01-23 09:43:06 -0800247 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500248
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700249 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000250 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700251 ASSERT(success == CallDAG::INITDAG_SUCCESS);
252 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700253
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200254 const std::vector<MappedStruct> std140Structs = FlagStd140Structs(treeRoot);
255 // TODO(oetuaho): The std140Structs could be filtered based on which ones actually get used in
256 // the shader code. When we add shader storage blocks we might also consider an alternative
257 // solution, since the struct mapping won't work very well for shader storage blocks.
258
Jamie Madill37997142015-01-28 10:06:34 -0500259 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500260 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200261 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500262 mInfoSinkStack.pop();
263
Jamie Madill37997142015-01-28 10:06:34 -0500264 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500265 mInfoSinkStack.pop();
266
Jamie Madill32aab012015-01-27 14:12:26 -0500267 mInfoSinkStack.push(&mHeader);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200268 header(mHeader, std140Structs, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500269 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000270
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200271 objSink << mHeader.c_str();
272 objSink << mBody.c_str();
273 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200274
Olli Etuahodfa75e82017-01-23 09:43:06 -0800275 builtInFunctionEmulator.cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000276}
277
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800278const std::map<std::string, unsigned int> &OutputHLSL::getUniformBlockRegisterMap() const
Jamie Madill4e1fd412014-07-10 17:50:10 -0400279{
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800280 return mUniformHLSL->getUniformBlockRegisterMap();
Jamie Madill4e1fd412014-07-10 17:50:10 -0400281}
282
Jamie Madill9fe25e92014-07-18 10:33:08 -0400283const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
284{
285 return mUniformHLSL->getUniformRegisterMap();
286}
287
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200288TString OutputHLSL::structInitializerString(int indent,
289 const TType &type,
290 const TString &name) const
Jamie Madill570e04d2013-06-21 09:15:33 -0400291{
292 TString init;
293
Olli Etuahoed049ab2017-06-30 17:38:33 +0300294 TString indentString;
295 for (int spaces = 0; spaces < indent; spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400296 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300297 indentString += " ";
Jamie Madill570e04d2013-06-21 09:15:33 -0400298 }
299
Olli Etuahoed049ab2017-06-30 17:38:33 +0300300 if (type.isArray())
Jamie Madill570e04d2013-06-21 09:15:33 -0400301 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300302 init += indentString + "{\n";
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300303 for (unsigned int arrayIndex = 0u; arrayIndex < type.getOutermostArraySize(); ++arrayIndex)
Jamie Madill570e04d2013-06-21 09:15:33 -0400304 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300305 TStringStream indexedString;
306 indexedString << name << "[" << arrayIndex << "]";
307 TType elementType = type;
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300308 elementType.toArrayElementType();
Olli Etuahoed049ab2017-06-30 17:38:33 +0300309 init += structInitializerString(indent + 1, elementType, indexedString.str());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300310 if (arrayIndex < type.getOutermostArraySize() - 1)
Olli Etuahoed049ab2017-06-30 17:38:33 +0300311 {
312 init += ",";
313 }
314 init += "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400315 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300316 init += indentString + "}";
Jamie Madill570e04d2013-06-21 09:15:33 -0400317 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300318 else if (type.getBasicType() == EbtStruct)
319 {
320 init += indentString + "{\n";
321 const TStructure &structure = *type.getStruct();
322 const TFieldList &fields = structure.fields();
323 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
324 {
325 const TField &field = *fields[fieldIndex];
326 const TString &fieldName = name + "." + Decorate(field.name());
327 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400328
Olli Etuahoed049ab2017-06-30 17:38:33 +0300329 init += structInitializerString(indent + 1, fieldType, fieldName);
330 if (fieldIndex < fields.size() - 1)
331 {
332 init += ",";
333 }
334 init += "\n";
335 }
336 init += indentString + "}";
337 }
338 else
339 {
340 init += indentString + name;
341 }
Jamie Madill570e04d2013-06-21 09:15:33 -0400342
343 return init;
344}
345
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200346TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std140Structs) const
347{
348 TString mappedStructs;
349
350 for (auto &mappedStruct : std140Structs)
351 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200352 const TInterfaceBlock *interfaceBlock =
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200353 mappedStruct.blockDeclarator->getType().getInterfaceBlock();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200354 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200355 {
356 continue;
357 }
358
359 unsigned int instanceCount = 1u;
360 bool isInstanceArray = mappedStruct.blockDeclarator->isArray();
361 if (isInstanceArray)
362 {
363 instanceCount = mappedStruct.blockDeclarator->getOutermostArraySize();
364 }
365
366 for (unsigned int instanceArrayIndex = 0; instanceArrayIndex < instanceCount;
367 ++instanceArrayIndex)
368 {
369 TString originalName;
370 TString mappedName("map");
371
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200372 if (mappedStruct.blockDeclarator->variable().symbolType() != SymbolType::Empty)
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200373 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200374 const ImmutableString &instanceName =
375 mappedStruct.blockDeclarator->variable().name();
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200376 unsigned int instanceStringArrayIndex = GL_INVALID_INDEX;
377 if (isInstanceArray)
378 instanceStringArrayIndex = instanceArrayIndex;
Olli Etuaho12a18ad2017-12-01 16:59:47 +0200379 TString instanceString = mUniformHLSL->UniformBlockInstanceString(
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200380 instanceName, instanceStringArrayIndex);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200381 originalName += instanceString;
382 mappedName += instanceString;
383 originalName += ".";
384 mappedName += "_";
385 }
386
387 TString fieldName = Decorate(mappedStruct.field->name());
388 originalName += fieldName;
389 mappedName += fieldName;
390
391 TType *structType = mappedStruct.field->type();
392 mappedStructs +=
Olli Etuahobed35d72017-12-20 16:36:26 +0200393 "static " + Decorate(structType->getStruct()->name()) + " " + mappedName;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200394
395 if (structType->isArray())
396 {
397 mappedStructs += ArrayString(*mappedStruct.field->type());
398 }
399
400 mappedStructs += " =\n";
401 mappedStructs += structInitializerString(0, *structType, originalName);
402 mappedStructs += ";\n";
403 }
404 }
405 return mappedStructs;
406}
407
408void OutputHLSL::header(TInfoSinkBase &out,
409 const std::vector<MappedStruct> &std140Structs,
410 const BuiltInFunctionEmulator *builtInFunctionEmulator) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000412 TString varyings;
413 TString attributes;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200414 TString mappedStructs = generateStructMapping(std140Structs);
Jamie Madill570e04d2013-06-21 09:15:33 -0400415
Olli Etuahob8cb9392017-12-20 14:23:19 +0200416 for (const auto &varying : mReferencedVaryings)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000417 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200418 const TType &type = varying.second->getType();
Olli Etuahofbb1c792018-01-19 16:26:59 +0200419 const ImmutableString &name = varying.second->name();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000420
421 // Program linking depends on this exact format
Olli Etuaho2f7c04a2018-01-25 14:50:37 +0200422 varyings += TString("static ") + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200423 " " + Decorate(name) + ArrayString(type) + " = " + zeroInitializer(type) +
424 ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000425 }
426
Olli Etuaho93b059d2017-12-20 12:46:58 +0200427 for (const auto &attribute : mReferencedAttributes)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000428 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200429 const TType &type = attribute.second->getType();
Olli Etuahofbb1c792018-01-19 16:26:59 +0200430 const ImmutableString &name = attribute.second->name();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000431
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500432 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200433 " = " + zeroInitializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000434 }
435
Jamie Madill8daaba12014-06-13 10:04:33 -0400436 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400437
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300438 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms, mSymbolTable);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800439 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400440
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200441 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500442 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200443 out << "\n// Equality functions\n\n";
444 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500445 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200446 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200447 }
448 }
Olli Etuaho12690762015-03-31 12:55:28 +0300449 if (!mArrayAssignmentFunctions.empty())
450 {
451 out << "\n// Assignment functions\n\n";
452 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
453 {
454 out << assignmentFunction.functionDefinition << "\n";
455 }
456 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300457 if (!mArrayConstructIntoFunctions.empty())
458 {
459 out << "\n// Array constructor functions\n\n";
460 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
461 {
462 out << constructIntoFunction.functionDefinition << "\n";
463 }
464 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200465
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500466 if (mUsesDiscardRewriting)
467 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400468 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500469 }
470
Nicolas Capens655fe362014-04-11 13:12:34 -0400471 if (mUsesNestedBreak)
472 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400473 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400474 }
475
Arun Patole44efa0b2015-03-04 17:11:05 +0530476 if (mRequiresIEEEStrictCompiling)
477 {
478 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
479 }
480
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400481 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
482 "#define LOOP [loop]\n"
483 "#define FLATTEN [flatten]\n"
484 "#else\n"
485 "#define LOOP\n"
486 "#define FLATTEN\n"
487 "#endif\n";
488
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200489 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300491 const bool usingMRTExtension =
492 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000493
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000494 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500495 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400496 out << "\n";
497
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200498 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000499 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200500 for (const auto &outputVariable : mReferencedOutputVariables)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000501 {
Olli Etuahofbb1c792018-01-19 16:26:59 +0200502 const ImmutableString &variableName = outputVariable.second->name();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200503 const TType &variableType = outputVariable.second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400504
Olli Etuahofbb1c792018-01-19 16:26:59 +0200505 out << "static " << TypeString(variableType) << " out_" << variableName
506 << ArrayString(variableType) << " = " << zeroInitializer(variableType) << ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000507 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000508 }
Jamie Madill46131a32013-06-20 11:55:50 -0400509 else
510 {
511 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
512
513 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500514 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400515 for (unsigned int i = 0; i < numColorValues; i++)
516 {
517 out << " float4(0, 0, 0, 0)";
518 if (i + 1 != numColorValues)
519 {
520 out << ",";
521 }
522 out << "\n";
523 }
524
525 out << "};\n";
526 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000527
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400528 if (mUsesFragDepth)
529 {
530 out << "static float gl_Depth = 0.0;\n";
531 }
532
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000533 if (mUsesFragCoord)
534 {
535 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
536 }
537
538 if (mUsesPointCoord)
539 {
540 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
541 }
542
543 if (mUsesFrontFacing)
544 {
545 out << "static bool gl_FrontFacing = false;\n";
546 }
547
548 out << "\n";
549
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000550 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000551 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000552 out << "struct gl_DepthRangeParameters\n"
553 "{\n"
554 " float near;\n"
555 " float far;\n"
556 " float diff;\n"
557 "};\n"
558 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000559 }
560
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200561 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000562 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000563 out << "cbuffer DriverConstants : register(b1)\n"
564 "{\n";
565
566 if (mUsesDepthRange)
567 {
568 out << " float3 dx_DepthRange : packoffset(c0);\n";
569 }
570
571 if (mUsesFragCoord)
572 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000573 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000574 }
575
576 if (mUsesFragCoord || mUsesFrontFacing)
577 {
578 out << " float3 dx_DepthFront : packoffset(c2);\n";
579 }
580
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800581 if (mUsesFragCoord)
582 {
583 // dx_ViewScale is only used in the fragment shader to correct
584 // the value for glFragCoord if necessary
585 out << " float2 dx_ViewScale : packoffset(c3);\n";
586 }
587
Martin Radev72b4e1e2017-08-31 15:42:56 +0300588 if (mHasMultiviewExtensionEnabled)
589 {
590 // We have to add a value which we can use to keep track of which multi-view code
591 // path is to be selected in the GS.
592 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
593 }
594
Olli Etuaho618bebc2016-01-15 16:40:00 +0200595 if (mOutputType == SH_HLSL_4_1_OUTPUT)
596 {
597 mUniformHLSL->samplerMetadataUniforms(out, "c4");
598 }
599
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000600 out << "};\n";
601 }
602 else
603 {
604 if (mUsesDepthRange)
605 {
606 out << "uniform float3 dx_DepthRange : register(c0);";
607 }
608
609 if (mUsesFragCoord)
610 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000611 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000612 }
613
614 if (mUsesFragCoord || mUsesFrontFacing)
615 {
616 out << "uniform float3 dx_DepthFront : register(c2);\n";
617 }
618 }
619
620 out << "\n";
621
622 if (mUsesDepthRange)
623 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500624 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
625 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000626 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000627 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000628
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000629 if (usingMRTExtension && mNumRenderTargets > 1)
630 {
631 out << "#define GL_USES_MRT\n";
632 }
633
634 if (mUsesFragColor)
635 {
636 out << "#define GL_USES_FRAG_COLOR\n";
637 }
638
639 if (mUsesFragData)
640 {
641 out << "#define GL_USES_FRAG_DATA\n";
642 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643 }
Xinghua Caob1239382016-12-13 15:07:05 +0800644 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000646 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500647 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000648 out << "\n"
649 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400650
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000651 if (mUsesPointSize)
652 {
653 out << "static float gl_PointSize = float(1);\n";
654 }
655
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000656 if (mUsesInstanceID)
657 {
658 out << "static int gl_InstanceID;";
659 }
660
Corentin Wallezb076add2016-01-11 16:45:46 -0500661 if (mUsesVertexID)
662 {
663 out << "static int gl_VertexID;";
664 }
665
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000666 out << "\n"
667 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500668 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000669 out << "\n";
670
671 if (mUsesDepthRange)
672 {
673 out << "struct gl_DepthRangeParameters\n"
674 "{\n"
675 " float near;\n"
676 " float far;\n"
677 " float diff;\n"
678 "};\n"
679 "\n";
680 }
681
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200682 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000683 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800684 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500685 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800686
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000687 if (mUsesDepthRange)
688 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800689 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000690 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800691
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800692 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
693 // shaders. However, we declare it for all shaders (including Feature Level 10+).
694 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
695 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800696 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800697 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800698 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800699
Martin Radev72b4e1e2017-08-31 15:42:56 +0300700 if (mHasMultiviewExtensionEnabled)
701 {
702 // We have to add a value which we can use to keep track of which multi-view code
703 // path is to be selected in the GS.
704 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
705 }
706
Olli Etuaho618bebc2016-01-15 16:40:00 +0200707 if (mOutputType == SH_HLSL_4_1_OUTPUT)
708 {
709 mUniformHLSL->samplerMetadataUniforms(out, "c4");
710 }
711
Austin Kinross4fd18b12014-12-22 12:32:05 -0800712 out << "};\n"
713 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000714 }
715 else
716 {
717 if (mUsesDepthRange)
718 {
719 out << "uniform float3 dx_DepthRange : register(c0);\n";
720 }
721
Cooper Partine6664f02015-01-09 16:22:24 -0800722 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
723 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000724 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000725 }
726
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000727 if (mUsesDepthRange)
728 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500729 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
730 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000731 "\n";
732 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400733 }
Xinghua Caob1239382016-12-13 15:07:05 +0800734 else // Compute shader
735 {
736 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800737
738 out << "cbuffer DriverConstants : register(b1)\n"
739 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800740 if (mUsesNumWorkGroups)
741 {
Xinghua Caob1239382016-12-13 15:07:05 +0800742 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800743 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800744 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
745 mUniformHLSL->samplerMetadataUniforms(out, "c1");
746 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800747
748 // Follow built-in variables would be initialized in
749 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
750 // are used in compute shader.
751 if (mUsesWorkGroupID)
752 {
753 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
754 }
755
756 if (mUsesLocalInvocationID)
757 {
758 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
759 }
760
761 if (mUsesGlobalInvocationID)
762 {
763 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
764 }
765
766 if (mUsesLocalInvocationIndex)
767 {
768 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
769 }
770 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000771
Qin Jiajia2a12b3d2018-05-23 13:42:13 +0800772 if (!mappedStructs.empty())
773 {
774 out << "// Structures from std140 blocks with padding removed\n";
775 out << "\n";
776 out << mappedStructs;
777 out << "\n";
778 }
779
Geoff Lang1fe74c72016-08-25 13:23:01 -0400780 bool getDimensionsIgnoresBaseLevel =
781 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
782 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800783 mImageFunctionHLSL->imageFunctionHeader(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000785 if (mUsesFragCoord)
786 {
787 out << "#define GL_USES_FRAG_COORD\n";
788 }
789
790 if (mUsesPointCoord)
791 {
792 out << "#define GL_USES_POINT_COORD\n";
793 }
794
795 if (mUsesFrontFacing)
796 {
797 out << "#define GL_USES_FRONT_FACING\n";
798 }
799
800 if (mUsesPointSize)
801 {
802 out << "#define GL_USES_POINT_SIZE\n";
803 }
804
Martin Radev41ac68e2017-06-06 12:16:58 +0300805 if (mHasMultiviewExtensionEnabled)
806 {
807 out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n";
808 }
809
810 if (mUsesViewID)
811 {
812 out << "#define GL_USES_VIEW_ID\n";
813 }
814
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400815 if (mUsesFragDepth)
816 {
817 out << "#define GL_USES_FRAG_DEPTH\n";
818 }
819
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000820 if (mUsesDepthRange)
821 {
822 out << "#define GL_USES_DEPTH_RANGE\n";
823 }
824
Xinghua Caob1239382016-12-13 15:07:05 +0800825 if (mUsesNumWorkGroups)
826 {
827 out << "#define GL_USES_NUM_WORK_GROUPS\n";
828 }
829
830 if (mUsesWorkGroupID)
831 {
832 out << "#define GL_USES_WORK_GROUP_ID\n";
833 }
834
835 if (mUsesLocalInvocationID)
836 {
837 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
838 }
839
840 if (mUsesGlobalInvocationID)
841 {
842 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
843 }
844
845 if (mUsesLocalInvocationIndex)
846 {
847 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
848 }
849
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000850 if (mUsesXor)
851 {
852 out << "bool xor(bool p, bool q)\n"
853 "{\n"
854 " return (p || q) && !(p && q);\n"
855 "}\n"
856 "\n";
857 }
858
Olli Etuahodfa75e82017-01-23 09:43:06 -0800859 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void OutputHLSL::visitSymbol(TIntermSymbol *node)
863{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200864 const TVariable &variable = node->variable();
865
866 // Empty symbols can only appear in declarations and function arguments, and in either of those
867 // cases the symbol nodes are not visited.
868 ASSERT(variable.symbolType() != SymbolType::Empty);
869
Jamie Madill32aab012015-01-27 14:12:26 -0500870 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000871
Jamie Madill570e04d2013-06-21 09:15:33 -0400872 // Handle accessing std140 structs by value
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200873 if (IsInStd140InterfaceBlock(node) && node->getBasicType() == EbtStruct)
Jamie Madill570e04d2013-06-21 09:15:33 -0400874 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200875 out << "map";
Jamie Madill570e04d2013-06-21 09:15:33 -0400876 }
877
Olli Etuahofbb1c792018-01-19 16:26:59 +0200878 const ImmutableString &name = variable.name();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200879 const TSymbolUniqueId &uniqueId = variable.uniqueId();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200880
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000881 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000882 {
883 mUsesDepthRange = true;
884 out << name;
885 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886 else
887 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200888 const TType &variableType = variable.getType();
889 TQualifier qualifier = variable.getType().getQualifier();
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000890
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200891 ensureStructDefined(variableType);
Olli Etuahobd3cd502017-11-03 15:48:52 +0200892
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000893 if (qualifier == EvqUniform)
894 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200895 const TInterfaceBlock *interfaceBlock = variableType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400896
897 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000898 {
Olli Etuahoc71862a2017-12-21 12:58:29 +0200899 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
900 {
901 const TVariable *instanceVariable = nullptr;
902 if (variableType.isInterfaceBlock())
903 {
904 instanceVariable = &variable;
905 }
906 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
907 new TReferencedBlock(interfaceBlock, instanceVariable);
908 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000909 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000910 else
911 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200912 mReferencedUniforms[uniqueId.get()] = &variable;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000913 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400914
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200915 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000916 }
Jamie Madill19571812013-08-12 15:26:34 -0700917 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000918 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200919 mReferencedAttributes[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400920 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000921 }
Jamie Madill033dae62014-06-18 12:56:28 -0400922 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000923 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200924 mReferencedVaryings[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400925 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300926 if (name == "ViewID_OVR")
927 {
928 mUsesViewID = true;
929 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000930 }
Jamie Madill19571812013-08-12 15:26:34 -0700931 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400932 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200933 mReferencedOutputVariables[uniqueId.get()] = &variable;
Jamie Madill46131a32013-06-20 11:55:50 -0400934 out << "out_" << name;
935 }
936 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000937 {
938 out << "gl_Color[0]";
939 mUsesFragColor = true;
940 }
941 else if (qualifier == EvqFragData)
942 {
943 out << "gl_Color";
944 mUsesFragData = true;
945 }
946 else if (qualifier == EvqFragCoord)
947 {
948 mUsesFragCoord = true;
949 out << name;
950 }
951 else if (qualifier == EvqPointCoord)
952 {
953 mUsesPointCoord = true;
954 out << name;
955 }
956 else if (qualifier == EvqFrontFacing)
957 {
958 mUsesFrontFacing = true;
959 out << name;
960 }
961 else if (qualifier == EvqPointSize)
962 {
963 mUsesPointSize = true;
964 out << name;
965 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000966 else if (qualifier == EvqInstanceID)
967 {
968 mUsesInstanceID = true;
969 out << name;
970 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500971 else if (qualifier == EvqVertexID)
972 {
973 mUsesVertexID = true;
974 out << name;
975 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300976 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400977 {
978 mUsesFragDepth = true;
979 out << "gl_Depth";
980 }
Xinghua Caob1239382016-12-13 15:07:05 +0800981 else if (qualifier == EvqNumWorkGroups)
982 {
983 mUsesNumWorkGroups = true;
984 out << name;
985 }
986 else if (qualifier == EvqWorkGroupID)
987 {
988 mUsesWorkGroupID = true;
989 out << name;
990 }
991 else if (qualifier == EvqLocalInvocationID)
992 {
993 mUsesLocalInvocationID = true;
994 out << name;
995 }
996 else if (qualifier == EvqGlobalInvocationID)
997 {
998 mUsesGlobalInvocationID = true;
999 out << name;
1000 }
1001 else if (qualifier == EvqLocalInvocationIndex)
1002 {
1003 mUsesLocalInvocationIndex = true;
1004 out << name;
1005 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001006 else
1007 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001008 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001009 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010 }
1011}
1012
Olli Etuaho7fb49552015-03-18 17:27:44 +02001013void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1014{
1015 if (type.isScalar() && !type.isArray())
1016 {
1017 if (op == EOpEqual)
1018 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001019 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001020 }
1021 else
1022 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001023 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001024 }
1025 }
1026 else
1027 {
1028 if (visit == PreVisit && op == EOpNotEqual)
1029 {
1030 out << "!";
1031 }
1032
1033 if (type.isArray())
1034 {
1035 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001036 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001037 }
1038 else if (type.getBasicType() == EbtStruct)
1039 {
1040 const TStructure &structure = *type.getStruct();
1041 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001042 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001043 }
1044 else
1045 {
1046 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001047 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001048 }
1049 }
1050}
1051
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001052void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1053{
1054 if (type.isArray())
1055 {
1056 const TString &functionName = addArrayAssignmentFunction(type);
1057 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1058 }
1059 else
1060 {
1061 outputTriplet(out, visit, "(", " = ", ")");
1062 }
1063}
1064
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001065bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001066{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001067 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001068 {
1069 TIntermNode *ancestor = getAncestorNode(n);
1070 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1071 if (ancestorBinary == nullptr)
1072 {
1073 return false;
1074 }
1075 switch (ancestorBinary->getOp())
1076 {
1077 case EOpIndexDirectStruct:
1078 {
1079 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1080 const TIntermConstantUnion *index =
1081 ancestorBinary->getRight()->getAsConstantUnion();
1082 const TField *field = structure->fields()[index->getIConst(0)];
1083 if (IsSampler(field->type()->getBasicType()))
1084 {
1085 return true;
1086 }
1087 break;
1088 }
1089 case EOpIndexDirect:
1090 break;
1091 default:
1092 // Returning a sampler from indirect indexing is not supported.
1093 return false;
1094 }
1095 }
1096 return false;
1097}
1098
Olli Etuahob6fa0432016-09-28 16:28:05 +01001099bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1100{
1101 TInfoSinkBase &out = getInfoSink();
1102 if (visit == PostVisit)
1103 {
1104 out << ".";
1105 node->writeOffsetsAsXYZW(&out);
1106 }
1107 return true;
1108}
1109
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1111{
Jamie Madill32aab012015-01-27 14:12:26 -05001112 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113
1114 switch (node->getOp())
1115 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001116 case EOpComma:
1117 outputTriplet(out, visit, "(", ", ", ")");
1118 break;
1119 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001120 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001121 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001122 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1123 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001124 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001125 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1126 out << functionName << "(";
1127 node->getLeft()->traverse(this);
1128 TIntermSequence *seq = rightAgg->getSequence();
1129 for (auto &arrayElement : *seq)
1130 {
1131 out << ", ";
1132 arrayElement->traverse(this);
1133 }
1134 out << ")";
1135 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001136 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001137 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1138 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001139 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001140 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001141 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001142 break;
1143 case EOpInitialize:
1144 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001145 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001146 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1147 ASSERT(symbolNode);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001148 TIntermTyped *initializer = node->getRight();
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001149
1150 // Global initializers must be constant at this point.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001151 ASSERT(symbolNode->getQualifier() != EvqGlobal || initializer->hasConstantValue());
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001152
1153 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1154 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1155 // new variable is created before the assignment is evaluated), so we need to
1156 // convert
1157 // this to "float t = x, x = t;".
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001158 if (writeSameSymbolInitializer(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001159 {
1160 // Skip initializing the rest of the expression
1161 return false;
1162 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001163 else if (writeConstantInitialization(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001164 {
1165 return false;
1166 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001167 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001168 else if (visit == InVisit)
1169 {
1170 out << " = ";
1171 }
1172 break;
1173 case EOpAddAssign:
1174 outputTriplet(out, visit, "(", " += ", ")");
1175 break;
1176 case EOpSubAssign:
1177 outputTriplet(out, visit, "(", " -= ", ")");
1178 break;
1179 case EOpMulAssign:
1180 outputTriplet(out, visit, "(", " *= ", ")");
1181 break;
1182 case EOpVectorTimesScalarAssign:
1183 outputTriplet(out, visit, "(", " *= ", ")");
1184 break;
1185 case EOpMatrixTimesScalarAssign:
1186 outputTriplet(out, visit, "(", " *= ", ")");
1187 break;
1188 case EOpVectorTimesMatrixAssign:
1189 if (visit == PreVisit)
1190 {
1191 out << "(";
1192 }
1193 else if (visit == InVisit)
1194 {
1195 out << " = mul(";
1196 node->getLeft()->traverse(this);
1197 out << ", transpose(";
1198 }
1199 else
1200 {
1201 out << ")))";
1202 }
1203 break;
1204 case EOpMatrixTimesMatrixAssign:
1205 if (visit == PreVisit)
1206 {
1207 out << "(";
1208 }
1209 else if (visit == InVisit)
1210 {
1211 out << " = transpose(mul(transpose(";
1212 node->getLeft()->traverse(this);
1213 out << "), transpose(";
1214 }
1215 else
1216 {
1217 out << "))))";
1218 }
1219 break;
1220 case EOpDivAssign:
1221 outputTriplet(out, visit, "(", " /= ", ")");
1222 break;
1223 case EOpIModAssign:
1224 outputTriplet(out, visit, "(", " %= ", ")");
1225 break;
1226 case EOpBitShiftLeftAssign:
1227 outputTriplet(out, visit, "(", " <<= ", ")");
1228 break;
1229 case EOpBitShiftRightAssign:
1230 outputTriplet(out, visit, "(", " >>= ", ")");
1231 break;
1232 case EOpBitwiseAndAssign:
1233 outputTriplet(out, visit, "(", " &= ", ")");
1234 break;
1235 case EOpBitwiseXorAssign:
1236 outputTriplet(out, visit, "(", " ^= ", ")");
1237 break;
1238 case EOpBitwiseOrAssign:
1239 outputTriplet(out, visit, "(", " |= ", ")");
1240 break;
1241 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001242 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001243 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001244 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001245 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001246 if (visit == PreVisit)
1247 {
Olli Etuaho12a18ad2017-12-01 16:59:47 +02001248 TIntermSymbol *instanceArraySymbol = node->getLeft()->getAsSymbolNode();
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001249 const TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Olli Etuahoc71862a2017-12-21 12:58:29 +02001250 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
1251 {
1252 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
1253 new TReferencedBlock(interfaceBlock, &instanceArraySymbol->variable());
1254 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001255 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001256 out << mUniformHLSL->UniformBlockInstanceString(instanceArraySymbol->getName(),
1257 arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001258 return false;
1259 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001260 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001261 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001262 {
1263 // All parts of an expression that access a sampler in a struct need to use _ as
1264 // separator to access the sampler variable that has been moved out of the struct.
1265 outputTriplet(out, visit, "", "_", "");
1266 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001267 else
1268 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001269 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001270 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001271 }
1272 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001273 case EOpIndexIndirect:
1274 // We do not currently support indirect references to interface blocks
1275 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1276 outputTriplet(out, visit, "", "[", "]");
1277 break;
1278 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001279 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001280 const TStructure *structure = node->getLeft()->getType().getStruct();
1281 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1282 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001283
Olli Etuaho96963162016-03-21 11:54:33 +02001284 // In cases where indexing returns a sampler, we need to access the sampler variable
1285 // that has been moved out of the struct.
1286 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1287 if (visit == PreVisit && indexingReturnsSampler)
1288 {
1289 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1290 // This prefix is only output at the beginning of the indexing expression, which
1291 // may have multiple parts.
1292 out << "angle";
1293 }
1294 if (!indexingReturnsSampler)
1295 {
1296 // All parts of an expression that access a sampler in a struct need to use _ as
1297 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001298 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001299 }
1300 if (visit == InVisit)
1301 {
1302 if (indexingReturnsSampler)
1303 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001304 out << "_" << field->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001305 }
1306 else
1307 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001308 out << "." << DecorateField(field->name(), *structure);
Olli Etuaho96963162016-03-21 11:54:33 +02001309 }
1310
1311 return false;
1312 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001313 }
1314 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001315 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001316 {
1317 bool structInStd140Block =
1318 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1319 if (visit == PreVisit && structInStd140Block)
1320 {
1321 out << "map";
1322 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001323 if (visit == InVisit)
1324 {
1325 const TInterfaceBlock *interfaceBlock =
1326 node->getLeft()->getType().getInterfaceBlock();
1327 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1328 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001329 if (structInStd140Block)
1330 {
1331 out << "_";
1332 }
1333 else
1334 {
1335 out << ".";
1336 }
1337 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001338
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001339 return false;
1340 }
1341 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001342 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001343 case EOpAdd:
1344 outputTriplet(out, visit, "(", " + ", ")");
1345 break;
1346 case EOpSub:
1347 outputTriplet(out, visit, "(", " - ", ")");
1348 break;
1349 case EOpMul:
1350 outputTriplet(out, visit, "(", " * ", ")");
1351 break;
1352 case EOpDiv:
1353 outputTriplet(out, visit, "(", " / ", ")");
1354 break;
1355 case EOpIMod:
1356 outputTriplet(out, visit, "(", " % ", ")");
1357 break;
1358 case EOpBitShiftLeft:
1359 outputTriplet(out, visit, "(", " << ", ")");
1360 break;
1361 case EOpBitShiftRight:
1362 outputTriplet(out, visit, "(", " >> ", ")");
1363 break;
1364 case EOpBitwiseAnd:
1365 outputTriplet(out, visit, "(", " & ", ")");
1366 break;
1367 case EOpBitwiseXor:
1368 outputTriplet(out, visit, "(", " ^ ", ")");
1369 break;
1370 case EOpBitwiseOr:
1371 outputTriplet(out, visit, "(", " | ", ")");
1372 break;
1373 case EOpEqual:
1374 case EOpNotEqual:
1375 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1376 break;
1377 case EOpLessThan:
1378 outputTriplet(out, visit, "(", " < ", ")");
1379 break;
1380 case EOpGreaterThan:
1381 outputTriplet(out, visit, "(", " > ", ")");
1382 break;
1383 case EOpLessThanEqual:
1384 outputTriplet(out, visit, "(", " <= ", ")");
1385 break;
1386 case EOpGreaterThanEqual:
1387 outputTriplet(out, visit, "(", " >= ", ")");
1388 break;
1389 case EOpVectorTimesScalar:
1390 outputTriplet(out, visit, "(", " * ", ")");
1391 break;
1392 case EOpMatrixTimesScalar:
1393 outputTriplet(out, visit, "(", " * ", ")");
1394 break;
1395 case EOpVectorTimesMatrix:
1396 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1397 break;
1398 case EOpMatrixTimesVector:
1399 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1400 break;
1401 case EOpMatrixTimesMatrix:
1402 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1403 break;
1404 case EOpLogicalOr:
1405 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1406 // been unfolded.
1407 ASSERT(!node->getRight()->hasSideEffects());
1408 outputTriplet(out, visit, "(", " || ", ")");
1409 return true;
1410 case EOpLogicalXor:
1411 mUsesXor = true;
1412 outputTriplet(out, visit, "xor(", ", ", ")");
1413 break;
1414 case EOpLogicalAnd:
1415 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1416 // been unfolded.
1417 ASSERT(!node->getRight()->hasSideEffects());
1418 outputTriplet(out, visit, "(", " && ", ")");
1419 return true;
1420 default:
1421 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001422 }
1423
1424 return true;
1425}
1426
1427bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1428{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001429 TInfoSinkBase &out = getInfoSink();
1430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001431 switch (node->getOp())
1432 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001433 case EOpNegative:
1434 outputTriplet(out, visit, "(-", "", ")");
1435 break;
1436 case EOpPositive:
1437 outputTriplet(out, visit, "(+", "", ")");
1438 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001439 case EOpLogicalNot:
1440 outputTriplet(out, visit, "(!", "", ")");
1441 break;
1442 case EOpBitwiseNot:
1443 outputTriplet(out, visit, "(~", "", ")");
1444 break;
1445 case EOpPostIncrement:
1446 outputTriplet(out, visit, "(", "", "++)");
1447 break;
1448 case EOpPostDecrement:
1449 outputTriplet(out, visit, "(", "", "--)");
1450 break;
1451 case EOpPreIncrement:
1452 outputTriplet(out, visit, "(++", "", ")");
1453 break;
1454 case EOpPreDecrement:
1455 outputTriplet(out, visit, "(--", "", ")");
1456 break;
1457 case EOpRadians:
1458 outputTriplet(out, visit, "radians(", "", ")");
1459 break;
1460 case EOpDegrees:
1461 outputTriplet(out, visit, "degrees(", "", ")");
1462 break;
1463 case EOpSin:
1464 outputTriplet(out, visit, "sin(", "", ")");
1465 break;
1466 case EOpCos:
1467 outputTriplet(out, visit, "cos(", "", ")");
1468 break;
1469 case EOpTan:
1470 outputTriplet(out, visit, "tan(", "", ")");
1471 break;
1472 case EOpAsin:
1473 outputTriplet(out, visit, "asin(", "", ")");
1474 break;
1475 case EOpAcos:
1476 outputTriplet(out, visit, "acos(", "", ")");
1477 break;
1478 case EOpAtan:
1479 outputTriplet(out, visit, "atan(", "", ")");
1480 break;
1481 case EOpSinh:
1482 outputTriplet(out, visit, "sinh(", "", ")");
1483 break;
1484 case EOpCosh:
1485 outputTriplet(out, visit, "cosh(", "", ")");
1486 break;
1487 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001488 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001489 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001490 case EOpAtanh:
1491 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001492 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001493 break;
1494 case EOpExp:
1495 outputTriplet(out, visit, "exp(", "", ")");
1496 break;
1497 case EOpLog:
1498 outputTriplet(out, visit, "log(", "", ")");
1499 break;
1500 case EOpExp2:
1501 outputTriplet(out, visit, "exp2(", "", ")");
1502 break;
1503 case EOpLog2:
1504 outputTriplet(out, visit, "log2(", "", ")");
1505 break;
1506 case EOpSqrt:
1507 outputTriplet(out, visit, "sqrt(", "", ")");
1508 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001509 case EOpInversesqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001510 outputTriplet(out, visit, "rsqrt(", "", ")");
1511 break;
1512 case EOpAbs:
1513 outputTriplet(out, visit, "abs(", "", ")");
1514 break;
1515 case EOpSign:
1516 outputTriplet(out, visit, "sign(", "", ")");
1517 break;
1518 case EOpFloor:
1519 outputTriplet(out, visit, "floor(", "", ")");
1520 break;
1521 case EOpTrunc:
1522 outputTriplet(out, visit, "trunc(", "", ")");
1523 break;
1524 case EOpRound:
1525 outputTriplet(out, visit, "round(", "", ")");
1526 break;
1527 case EOpRoundEven:
1528 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001529 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001530 break;
1531 case EOpCeil:
1532 outputTriplet(out, visit, "ceil(", "", ")");
1533 break;
1534 case EOpFract:
1535 outputTriplet(out, visit, "frac(", "", ")");
1536 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001537 case EOpIsnan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001538 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001539 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001540 else
1541 outputTriplet(out, visit, "isnan(", "", ")");
1542 mRequiresIEEEStrictCompiling = true;
1543 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001544 case EOpIsinf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001545 outputTriplet(out, visit, "isinf(", "", ")");
1546 break;
1547 case EOpFloatBitsToInt:
1548 outputTriplet(out, visit, "asint(", "", ")");
1549 break;
1550 case EOpFloatBitsToUint:
1551 outputTriplet(out, visit, "asuint(", "", ")");
1552 break;
1553 case EOpIntBitsToFloat:
1554 outputTriplet(out, visit, "asfloat(", "", ")");
1555 break;
1556 case EOpUintBitsToFloat:
1557 outputTriplet(out, visit, "asfloat(", "", ")");
1558 break;
1559 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001560 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001561 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001562 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001563 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001564 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001565 case EOpPackUnorm4x8:
1566 case EOpPackSnorm4x8:
1567 case EOpUnpackUnorm4x8:
1568 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001569 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001570 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001571 break;
1572 case EOpLength:
1573 outputTriplet(out, visit, "length(", "", ")");
1574 break;
1575 case EOpNormalize:
1576 outputTriplet(out, visit, "normalize(", "", ")");
1577 break;
1578 case EOpDFdx:
1579 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1580 {
1581 outputTriplet(out, visit, "(", "", ", 0.0)");
1582 }
1583 else
1584 {
1585 outputTriplet(out, visit, "ddx(", "", ")");
1586 }
1587 break;
1588 case EOpDFdy:
1589 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1590 {
1591 outputTriplet(out, visit, "(", "", ", 0.0)");
1592 }
1593 else
1594 {
1595 outputTriplet(out, visit, "ddy(", "", ")");
1596 }
1597 break;
1598 case EOpFwidth:
1599 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1600 {
1601 outputTriplet(out, visit, "(", "", ", 0.0)");
1602 }
1603 else
1604 {
1605 outputTriplet(out, visit, "fwidth(", "", ")");
1606 }
1607 break;
1608 case EOpTranspose:
1609 outputTriplet(out, visit, "transpose(", "", ")");
1610 break;
1611 case EOpDeterminant:
1612 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1613 break;
1614 case EOpInverse:
1615 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001616 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001617 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001618
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001619 case EOpAny:
1620 outputTriplet(out, visit, "any(", "", ")");
1621 break;
1622 case EOpAll:
1623 outputTriplet(out, visit, "all(", "", ")");
1624 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001625 case EOpLogicalNotComponentWise:
1626 outputTriplet(out, visit, "(!", "", ")");
1627 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001628 case EOpBitfieldReverse:
1629 outputTriplet(out, visit, "reversebits(", "", ")");
1630 break;
1631 case EOpBitCount:
1632 outputTriplet(out, visit, "countbits(", "", ")");
1633 break;
1634 case EOpFindLSB:
1635 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1636 // in GLSLTest and results are consistent with GL.
1637 outputTriplet(out, visit, "firstbitlow(", "", ")");
1638 break;
1639 case EOpFindMSB:
1640 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1641 // tested in GLSLTest and results are consistent with GL.
1642 outputTriplet(out, visit, "firstbithigh(", "", ")");
1643 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001644 default:
1645 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001646 }
1647
1648 return true;
1649}
1650
Olli Etuahofbb1c792018-01-19 16:26:59 +02001651ImmutableString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
Olli Etuaho96963162016-03-21 11:54:33 +02001652{
1653 if (node->getAsSymbolNode())
1654 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001655 ASSERT(node->getAsSymbolNode()->variable().symbolType() != SymbolType::Empty);
1656 return node->getAsSymbolNode()->getName();
Olli Etuaho96963162016-03-21 11:54:33 +02001657 }
1658 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1659 switch (nodeBinary->getOp())
1660 {
1661 case EOpIndexDirect:
1662 {
1663 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1664
Olli Etuahofbb1c792018-01-19 16:26:59 +02001665 std::stringstream prefixSink;
Olli Etuaho96963162016-03-21 11:54:33 +02001666 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
Olli Etuahofbb1c792018-01-19 16:26:59 +02001667 return ImmutableString(prefixSink.str());
Olli Etuaho96963162016-03-21 11:54:33 +02001668 }
1669 case EOpIndexDirectStruct:
1670 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001671 const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001672 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1673 const TField *field = s->fields()[index];
1674
Olli Etuahofbb1c792018-01-19 16:26:59 +02001675 std::stringstream prefixSink;
Olli Etuaho96963162016-03-21 11:54:33 +02001676 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1677 << field->name();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001678 return ImmutableString(prefixSink.str());
Olli Etuaho96963162016-03-21 11:54:33 +02001679 }
1680 default:
1681 UNREACHABLE();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001682 return ImmutableString("");
Olli Etuaho96963162016-03-21 11:54:33 +02001683 }
1684}
1685
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001686bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1687{
1688 TInfoSinkBase &out = getInfoSink();
1689
1690 if (mInsideFunction)
1691 {
1692 outputLineDirective(out, node->getLine().first_line);
1693 out << "{\n";
1694 }
1695
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001696 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001697 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001698 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001699
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001700 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001701
1702 // Don't output ; after case labels, they're terminated by :
1703 // This is needed especially since outputting a ; after a case statement would turn empty
1704 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001705 // Also the output code is clearer if we don't output ; after statements where it is not
1706 // needed:
1707 // * if statements
1708 // * switch statements
1709 // * blocks
1710 // * function definitions
1711 // * loops (do-while loops output the semicolon in VisitLoop)
1712 // * declarations that don't generate output.
1713 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1714 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1715 statement->getAsSwitchNode() == nullptr &&
1716 statement->getAsFunctionDefinition() == nullptr &&
1717 (statement->getAsDeclarationNode() == nullptr ||
1718 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1719 statement->getAsInvariantDeclarationNode() == nullptr)
1720 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001721 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001722 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001723 }
1724
1725 if (mInsideFunction)
1726 {
1727 outputLineDirective(out, node->getLine().last_line);
1728 out << "}\n";
1729 }
1730
1731 return false;
1732}
1733
Olli Etuaho336b1472016-10-05 16:37:55 +01001734bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1735{
1736 TInfoSinkBase &out = getInfoSink();
1737
1738 ASSERT(mCurrentFunctionMetadata == nullptr);
1739
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001740 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho336b1472016-10-05 16:37:55 +01001741 ASSERT(index != CallDAG::InvalidIndex);
1742 mCurrentFunctionMetadata = &mASTMetadataList[index];
1743
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001744 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001745
Olli Etuahod4bd9632018-03-08 16:32:44 +02001746 const TFunction *func = node->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001747
Olli Etuahod4bd9632018-03-08 16:32:44 +02001748 if (func->isMain())
Olli Etuaho336b1472016-10-05 16:37:55 +01001749 {
1750 out << "gl_main(";
1751 }
1752 else
1753 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001754 out << DecorateFunctionIfNeeded(func) << DisambiguateFunctionName(func)
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001755 << (mOutputLod0Function ? "Lod0(" : "(");
Olli Etuaho336b1472016-10-05 16:37:55 +01001756 }
1757
Olli Etuahod4bd9632018-03-08 16:32:44 +02001758 size_t paramCount = func->getParamCount();
1759 for (unsigned int i = 0; i < paramCount; i++)
Olli Etuaho336b1472016-10-05 16:37:55 +01001760 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001761 const TVariable *param = func->getParam(i);
1762 ensureStructDefined(param->getType());
Olli Etuaho336b1472016-10-05 16:37:55 +01001763
Olli Etuahod4bd9632018-03-08 16:32:44 +02001764 writeParameter(param, out);
1765
1766 if (i < paramCount - 1)
Olli Etuaho336b1472016-10-05 16:37:55 +01001767 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001768 out << ", ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001769 }
Olli Etuaho336b1472016-10-05 16:37:55 +01001770 }
1771
1772 out << ")\n";
1773
1774 mInsideFunction = true;
1775 // The function body node will output braces.
1776 node->getBody()->traverse(this);
1777 mInsideFunction = false;
1778
1779 mCurrentFunctionMetadata = nullptr;
1780
1781 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1782 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1783 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001784 ASSERT(!node->getFunction()->isMain());
Olli Etuaho336b1472016-10-05 16:37:55 +01001785 mOutputLod0Function = true;
1786 node->traverse(this);
1787 mOutputLod0Function = false;
1788 }
1789
1790 return false;
1791}
1792
Olli Etuaho13389b62016-10-16 11:48:18 +01001793bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1794{
Olli Etuaho13389b62016-10-16 11:48:18 +01001795 if (visit == PreVisit)
1796 {
1797 TIntermSequence *sequence = node->getSequence();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001798 TIntermTyped *declarator = (*sequence)[0]->getAsTyped();
Olli Etuaho13389b62016-10-16 11:48:18 +01001799 ASSERT(sequence->size() == 1);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001800 ASSERT(declarator);
Olli Etuaho13389b62016-10-16 11:48:18 +01001801
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001802 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001803 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001804 TInfoSinkBase &out = getInfoSink();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001805 ensureStructDefined(declarator->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001806
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001807 if (!declarator->getAsSymbolNode() ||
1808 declarator->getAsSymbolNode()->variable().symbolType() !=
1809 SymbolType::Empty) // Variable declaration
Olli Etuaho13389b62016-10-16 11:48:18 +01001810 {
1811 if (!mInsideFunction)
1812 {
1813 out << "static ";
1814 }
1815
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001816 out << TypeString(declarator->getType()) + " ";
Olli Etuaho13389b62016-10-16 11:48:18 +01001817
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001818 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho13389b62016-10-16 11:48:18 +01001819
1820 if (symbol)
1821 {
1822 symbol->traverse(this);
1823 out << ArrayString(symbol->getType());
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001824 out << " = " + zeroInitializer(symbol->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001825 }
1826 else
1827 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001828 declarator->traverse(this);
Olli Etuaho13389b62016-10-16 11:48:18 +01001829 }
1830 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001831 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001832 else if (IsVaryingOut(declarator->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001833 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001834 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho282847e2017-07-12 14:11:01 +03001835 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001836
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001837 const TVariable &variable = symbol->variable();
1838
1839 if (variable.symbolType() != SymbolType::Empty)
Olli Etuaho93b059d2017-12-20 12:46:58 +02001840 {
1841 // Vertex outputs which are declared but not written to should still be declared to
1842 // allow successful linking.
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001843 mReferencedVaryings[symbol->uniqueId().get()] = &variable;
Olli Etuaho93b059d2017-12-20 12:46:58 +02001844 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001845 }
1846 }
1847 return false;
1848}
1849
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001850bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1851{
1852 // Do not do any translation
1853 return false;
1854}
1855
Olli Etuahod4bd9632018-03-08 16:32:44 +02001856void OutputHLSL::visitFunctionPrototype(TIntermFunctionPrototype *node)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001857{
1858 TInfoSinkBase &out = getInfoSink();
1859
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001860 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001861 // Skip the prototype if it is not implemented (and thus not used)
1862 if (index == CallDAG::InvalidIndex)
1863 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001864 return;
Olli Etuaho16c745a2017-01-16 17:02:27 +00001865 }
1866
Olli Etuahod4bd9632018-03-08 16:32:44 +02001867 const TFunction *func = node->getFunction();
Olli Etuaho16c745a2017-01-16 17:02:27 +00001868
Olli Etuahod4bd9632018-03-08 16:32:44 +02001869 TString name = DecorateFunctionIfNeeded(func);
1870 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(func)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001871 << (mOutputLod0Function ? "Lod0(" : "(");
1872
Olli Etuahod4bd9632018-03-08 16:32:44 +02001873 size_t paramCount = func->getParamCount();
1874 for (unsigned int i = 0; i < paramCount; i++)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001875 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001876 writeParameter(func->getParam(i), out);
Olli Etuaho16c745a2017-01-16 17:02:27 +00001877
Olli Etuahod4bd9632018-03-08 16:32:44 +02001878 if (i < paramCount - 1)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001879 {
1880 out << ", ";
1881 }
1882 }
1883
1884 out << ");\n";
1885
1886 // Also prototype the Lod0 variant if needed
1887 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1888 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1889 {
1890 mOutputLod0Function = true;
1891 node->traverse(this);
1892 mOutputLod0Function = false;
1893 }
Olli Etuaho16c745a2017-01-16 17:02:27 +00001894}
1895
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1897{
Jamie Madill32aab012015-01-27 14:12:26 -05001898 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001899
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900 switch (node->getOp())
1901 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001902 case EOpCallBuiltInFunction:
1903 case EOpCallFunctionInAST:
1904 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001906 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001907
Corentin Wallez1239ee92015-03-19 14:38:02 -07001908 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001909 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001911 if (node->isArray())
1912 {
1913 UNIMPLEMENTED();
1914 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001915 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001916 ASSERT(index != CallDAG::InvalidIndex);
1917 lod0 &= mASTMetadataList[index].mNeedsLod0;
1918
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001919 out << DecorateFunctionIfNeeded(node->getFunction());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001920 out << DisambiguateFunctionName(node->getSequence());
1921 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001923 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001924 {
1925 // This path is used for internal functions that don't have their definitions in the
1926 // AST, such as precision emulation functions.
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001927 out << DecorateFunctionIfNeeded(node->getFunction()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001928 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001929 else if (node->getFunction()->isImageFunction())
Xinghua Cao711b7a12017-10-09 13:38:12 +08001930 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001931 const ImmutableString &name = node->getFunction()->name();
Xinghua Cao711b7a12017-10-09 13:38:12 +08001932 TType type = (*arguments)[0]->getAsTyped()->getType();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001933 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001934 name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
Xinghua Cao711b7a12017-10-09 13:38:12 +08001935 type.getMemoryQualifier().readonly);
1936 out << imageFunctionName << "(";
1937 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001938 else
1939 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001940 const ImmutableString &name = node->getFunction()->name();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001941 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001942 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1943 if (arguments->size() > 1)
1944 {
1945 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1946 }
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001947 const ImmutableString &textureFunctionName =
1948 mTextureFunctionHLSL->useTextureFunction(name, samplerType, coords,
1949 arguments->size(), lod0, mShaderType);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001950 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001952
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001953 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001954 {
Olli Etuaho96963162016-03-21 11:54:33 +02001955 TIntermTyped *typedArg = (*arg)->getAsTyped();
1956 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001957 {
1958 out << "texture_";
1959 (*arg)->traverse(this);
1960 out << ", sampler_";
1961 }
1962
1963 (*arg)->traverse(this);
1964
Olli Etuaho96963162016-03-21 11:54:33 +02001965 if (typedArg->getType().isStructureContainingSamplers())
1966 {
1967 const TType &argType = typedArg->getType();
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001968 TVector<const TVariable *> samplerSymbols;
Olli Etuahofbb1c792018-01-19 16:26:59 +02001969 ImmutableString structName = samplerNamePrefixFromStruct(typedArg);
1970 std::string namePrefix = "angle_";
1971 namePrefix += structName.data();
1972 argType.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols,
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001973 nullptr, mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001974 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02001975 {
1976 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1977 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001978 out << ", texture_" << sampler->name();
1979 out << ", sampler_" << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001980 }
1981 else
1982 {
1983 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1984 // of D3D9, it's the sampler variable.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001985 out << ", " << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001986 }
1987 }
1988 }
1989
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001990 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001991 {
1992 out << ", ";
1993 }
1994 }
1995
1996 out << ")";
1997
1998 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03002000 case EOpConstruct:
Olli Etuahobd3cd502017-11-03 15:48:52 +02002001 outputConstructor(out, visit, node);
Olli Etuaho8fab3202017-05-08 18:22:22 +03002002 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002003 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002004 outputTriplet(out, visit, "(", " == ", ")");
2005 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002006 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002007 outputTriplet(out, visit, "(", " != ", ")");
2008 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002009 case EOpLessThanComponentWise:
2010 outputTriplet(out, visit, "(", " < ", ")");
2011 break;
2012 case EOpGreaterThanComponentWise:
2013 outputTriplet(out, visit, "(", " > ", ")");
2014 break;
2015 case EOpLessThanEqualComponentWise:
2016 outputTriplet(out, visit, "(", " <= ", ")");
2017 break;
2018 case EOpGreaterThanEqualComponentWise:
2019 outputTriplet(out, visit, "(", " >= ", ")");
2020 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002021 case EOpMod:
2022 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002023 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002024 break;
2025 case EOpModf:
2026 outputTriplet(out, visit, "modf(", ", ", ")");
2027 break;
2028 case EOpPow:
2029 outputTriplet(out, visit, "pow(", ", ", ")");
2030 break;
2031 case EOpAtan:
2032 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2033 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002034 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002035 break;
2036 case EOpMin:
2037 outputTriplet(out, visit, "min(", ", ", ")");
2038 break;
2039 case EOpMax:
2040 outputTriplet(out, visit, "max(", ", ", ")");
2041 break;
2042 case EOpClamp:
2043 outputTriplet(out, visit, "clamp(", ", ", ")");
2044 break;
2045 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302046 {
2047 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2048 if (lastParamNode->getType().getBasicType() == EbtBool)
2049 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002050 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2051 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302052 // so use emulated version.
2053 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002054 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302055 }
2056 else
2057 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002058 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302059 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002060 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302061 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002062 case EOpStep:
2063 outputTriplet(out, visit, "step(", ", ", ")");
2064 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02002065 case EOpSmoothstep:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002066 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2067 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002068 case EOpFrexp:
2069 case EOpLdexp:
2070 ASSERT(node->getUseEmulatedFunction());
2071 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2072 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002073 case EOpDistance:
2074 outputTriplet(out, visit, "distance(", ", ", ")");
2075 break;
2076 case EOpDot:
2077 outputTriplet(out, visit, "dot(", ", ", ")");
2078 break;
2079 case EOpCross:
2080 outputTriplet(out, visit, "cross(", ", ", ")");
2081 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002082 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002083 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002084 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002085 break;
2086 case EOpReflect:
2087 outputTriplet(out, visit, "reflect(", ", ", ")");
2088 break;
2089 case EOpRefract:
2090 outputTriplet(out, visit, "refract(", ", ", ")");
2091 break;
2092 case EOpOuterProduct:
2093 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002094 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002095 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002096 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002097 outputTriplet(out, visit, "(", " * ", ")");
2098 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002099 case EOpBitfieldExtract:
2100 case EOpBitfieldInsert:
2101 case EOpUaddCarry:
2102 case EOpUsubBorrow:
2103 case EOpUmulExtended:
2104 case EOpImulExtended:
2105 ASSERT(node->getUseEmulatedFunction());
2106 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2107 break;
Xinghua Cao47335852018-02-12 15:41:55 +08002108 case EOpBarrier:
2109 // barrier() is translated to GroupMemoryBarrierWithGroupSync(), which is the
2110 // cheapest *WithGroupSync() function, without any functionality loss, but
2111 // with the potential for severe performance loss.
2112 outputTriplet(out, visit, "GroupMemoryBarrierWithGroupSync(", "", ")");
2113 break;
2114 case EOpMemoryBarrierShared:
2115 outputTriplet(out, visit, "GroupMemoryBarrier(", "", ")");
2116 break;
2117 case EOpMemoryBarrierAtomicCounter:
2118 case EOpMemoryBarrierBuffer:
2119 case EOpMemoryBarrierImage:
2120 outputTriplet(out, visit, "DeviceMemoryBarrier(", "", ")");
2121 break;
2122 case EOpGroupMemoryBarrier:
2123 case EOpMemoryBarrier:
2124 outputTriplet(out, visit, "AllMemoryBarrier(", "", ")");
2125 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002126 default:
2127 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 }
2129
2130 return true;
2131}
2132
Olli Etuaho57961272016-09-14 13:57:46 +03002133void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002135 out << "if (";
2136
2137 node->getCondition()->traverse(this);
2138
2139 out << ")\n";
2140
Jamie Madill8c46ab12015-12-07 16:39:19 -05002141 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002142
2143 bool discard = false;
2144
2145 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002147 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002148 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002149
Olli Etuahoa6f22092015-05-08 18:31:10 +03002150 // Detect true discard
2151 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2152 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002153 else
2154 {
2155 // TODO(oetuaho): Check if the semicolon inside is necessary.
2156 // It's there as a result of conservative refactoring of the output.
2157 out << "{;}\n";
2158 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002159
Jamie Madill8c46ab12015-12-07 16:39:19 -05002160 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002161
Olli Etuahoa6f22092015-05-08 18:31:10 +03002162 if (node->getFalseBlock())
2163 {
2164 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002165
Jamie Madill8c46ab12015-12-07 16:39:19 -05002166 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167
Olli Etuaho32db19b2016-10-04 14:43:16 +01002168 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002169 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002170
Jamie Madill8c46ab12015-12-07 16:39:19 -05002171 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002172
Olli Etuahoa6f22092015-05-08 18:31:10 +03002173 // Detect false discard
2174 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2175 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002176
Olli Etuahoa6f22092015-05-08 18:31:10 +03002177 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002178 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002179 {
2180 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002181 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002182}
2183
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002184bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2185{
2186 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2187 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2188 UNREACHABLE();
2189 return false;
2190}
2191
Olli Etuaho57961272016-09-14 13:57:46 +03002192bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002193{
2194 TInfoSinkBase &out = getInfoSink();
2195
Olli Etuaho3d932d82016-04-12 11:10:30 +03002196 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002197
2198 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002199 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002200 {
2201 out << "FLATTEN ";
2202 }
2203
Olli Etuaho57961272016-09-14 13:57:46 +03002204 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002205
2206 return false;
2207}
2208
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002209bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002210{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002211 TInfoSinkBase &out = getInfoSink();
2212
Olli Etuaho923ecef2017-10-11 12:01:38 +03002213 ASSERT(node->getStatementList());
2214 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002215 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002216 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002217 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002218 outputTriplet(out, visit, "switch (", ") ", "");
2219 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002220 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002221}
2222
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002223bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002224{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002225 TInfoSinkBase &out = getInfoSink();
2226
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002227 if (node->hasCondition())
2228 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002229 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002230 return true;
2231 }
2232 else
2233 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002234 out << "default:\n";
2235 return false;
2236 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002237}
2238
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002239void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2240{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002241 TInfoSinkBase &out = getInfoSink();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002242 writeConstantUnion(out, node->getType(), node->getConstantValue());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002243}
2244
2245bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2246{
Nicolas Capens655fe362014-04-11 13:12:34 -04002247 mNestedLoopDepth++;
2248
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002249 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002250 mInsideDiscontinuousLoop =
2251 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002252
Jamie Madill8c46ab12015-12-07 16:39:19 -05002253 TInfoSinkBase &out = getInfoSink();
2254
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002255 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002256 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002257 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002258 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002259 mInsideDiscontinuousLoop = wasDiscontinuous;
2260 mNestedLoopDepth--;
2261
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002262 return false;
2263 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002264 }
2265
Corentin Wallez1239ee92015-03-19 14:38:02 -07002266 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002267 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002268 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002269 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002270
Jamie Madill8c46ab12015-12-07 16:39:19 -05002271 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 }
2273 else
2274 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002275 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002276
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277 if (node->getInit())
2278 {
2279 node->getInit()->traverse(this);
2280 }
2281
2282 out << "; ";
2283
alokp@chromium.org52813552010-11-16 18:36:09 +00002284 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002286 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 }
2288
2289 out << "; ";
2290
alokp@chromium.org52813552010-11-16 18:36:09 +00002291 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002293 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 }
2295
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002296 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002297
Jamie Madill8c46ab12015-12-07 16:39:19 -05002298 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 }
2300
2301 if (node->getBody())
2302 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002303 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002304 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002306 else
2307 {
2308 // TODO(oetuaho): Check if the semicolon inside is necessary.
2309 // It's there as a result of conservative refactoring of the output.
2310 out << "{;}\n";
2311 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312
Jamie Madill8c46ab12015-12-07 16:39:19 -05002313 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314
alokp@chromium.org52813552010-11-16 18:36:09 +00002315 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002317 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002318 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319
alokp@chromium.org52813552010-11-16 18:36:09 +00002320 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002322 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323 }
2324
daniel@transgaming.com73536982012-03-21 20:45:49 +00002325 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002327 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002328 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002329
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330 return false;
2331}
2332
2333bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2334{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002335 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002337 TInfoSinkBase &out = getInfoSink();
2338
2339 switch (node->getFlowOp())
2340 {
2341 case EOpKill:
2342 out << "discard";
2343 break;
2344 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002345 if (mNestedLoopDepth > 1)
2346 {
2347 mUsesNestedBreak = true;
2348 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002349
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002350 if (mExcessiveLoopIndex)
2351 {
2352 out << "{Break";
2353 mExcessiveLoopIndex->traverse(this);
2354 out << " = true; break;}\n";
2355 }
2356 else
2357 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002358 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002359 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002360 break;
2361 case EOpContinue:
2362 out << "continue";
2363 break;
2364 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002365 if (node->getExpression())
2366 {
2367 out << "return ";
2368 }
2369 else
2370 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002371 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002372 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002373 break;
2374 default:
2375 UNREACHABLE();
2376 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377 }
2378
2379 return true;
2380}
2381
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002382// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002383// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2384// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002385bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002386{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002387 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002388
2389 // Parse loops of the form:
2390 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002391 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002392 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002393 int initial = 0;
2394 int limit = 0;
2395 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002396
2397 // Parse index name and intial value
2398 if (node->getInit())
2399 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002400 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401
2402 if (init)
2403 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002404 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002405 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002406
2407 if (variable && variable->getQualifier() == EvqTemporary)
2408 {
2409 TIntermBinary *assign = variable->getAsBinaryNode();
2410
2411 if (assign->getOp() == EOpInitialize)
2412 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002413 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002414 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2415
2416 if (symbol && constant)
2417 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002418 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002419 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002420 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002421 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002422 }
2423 }
2424 }
2425 }
2426 }
2427 }
2428
2429 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002430 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002431 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002432 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002433
Olli Etuahob6af22b2017-12-15 14:05:44 +02002434 if (test && test->getLeft()->getAsSymbolNode()->uniqueId() == index->uniqueId())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002435 {
2436 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2437
2438 if (constant)
2439 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002440 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002441 {
2442 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002443 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002444 }
2445 }
2446 }
2447 }
2448
2449 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002450 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002451 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002452 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002453 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002454
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002455 if (binaryTerminal)
2456 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002457 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002458 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2459
2460 if (constant)
2461 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002462 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002463 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002464 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002465
2466 switch (op)
2467 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002468 case EOpAddAssign:
2469 increment = value;
2470 break;
2471 case EOpSubAssign:
2472 increment = -value;
2473 break;
2474 default:
2475 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002476 }
2477 }
2478 }
2479 }
2480 else if (unaryTerminal)
2481 {
2482 TOperator op = unaryTerminal->getOp();
2483
2484 switch (op)
2485 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002486 case EOpPostIncrement:
2487 increment = 1;
2488 break;
2489 case EOpPostDecrement:
2490 increment = -1;
2491 break;
2492 case EOpPreIncrement:
2493 increment = 1;
2494 break;
2495 case EOpPreDecrement:
2496 increment = -1;
2497 break;
2498 default:
2499 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002500 }
2501 }
2502 }
2503
Yunchao He4f285442017-04-21 12:15:49 +08002504 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002505 {
2506 if (comparator == EOpLessThanEqual)
2507 {
2508 comparator = EOpLessThan;
2509 limit += 1;
2510 }
2511
2512 if (comparator == EOpLessThan)
2513 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002514 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002515
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002516 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002517 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002518 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002519 }
2520
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002521 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002522 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002523
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002524 out << "{int ";
2525 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002526 out << ";\n"
2527 "bool Break";
2528 index->traverse(this);
2529 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002530
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002531 bool firstLoopFragment = true;
2532
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002533 while (iterations > 0)
2534 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002535 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002536
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002537 if (!firstLoopFragment)
2538 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002539 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002540 index->traverse(this);
2541 out << ") {\n";
2542 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002543
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002544 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002545 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002546 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002547 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002548
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002549 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002550 const char *unroll =
2551 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002552
Corentin Wallez1239ee92015-03-19 14:38:02 -07002553 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002554 index->traverse(this);
2555 out << " = ";
2556 out << initial;
2557
2558 out << "; ";
2559 index->traverse(this);
2560 out << " < ";
2561 out << clampedLimit;
2562
2563 out << "; ";
2564 index->traverse(this);
2565 out << " += ";
2566 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002567 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002568
Jamie Madill8c46ab12015-12-07 16:39:19 -05002569 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002570 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002571
2572 if (node->getBody())
2573 {
2574 node->getBody()->traverse(this);
2575 }
2576
Jamie Madill8c46ab12015-12-07 16:39:19 -05002577 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002578 out << ";}\n";
2579
2580 if (!firstLoopFragment)
2581 {
2582 out << "}\n";
2583 }
2584
2585 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002586
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002587 initial += MAX_LOOP_ITERATIONS * increment;
2588 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002589 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002590
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002591 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002592
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002593 mExcessiveLoopIndex = restoreIndex;
2594
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002595 return true;
2596 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002597 else
2598 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002599 }
2600
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002601 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002602}
2603
Jamie Madill8c46ab12015-12-07 16:39:19 -05002604void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2605 Visit visit,
2606 const char *preString,
2607 const char *inString,
2608 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002610 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611 {
2612 out << preString;
2613 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002614 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615 {
2616 out << inString;
2617 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002618 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619 {
2620 out << postString;
2621 }
2622}
2623
Jamie Madill8c46ab12015-12-07 16:39:19 -05002624void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002625{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002626 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002627 {
Jamie Madill32aab012015-01-27 14:12:26 -05002628 out << "\n";
2629 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002630
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002631 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002632 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002633 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002634 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002635
Jamie Madill32aab012015-01-27 14:12:26 -05002636 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002637 }
2638}
2639
Olli Etuahod4bd9632018-03-08 16:32:44 +02002640void OutputHLSL::writeParameter(const TVariable *param, TInfoSinkBase &out)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002641{
Olli Etuahod4bd9632018-03-08 16:32:44 +02002642 const TType &type = param->getType();
2643 TQualifier qualifier = type.getQualifier();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002644
Olli Etuahod4bd9632018-03-08 16:32:44 +02002645 TString nameStr = DecorateVariableIfNeeded(*param);
2646 ASSERT(nameStr != ""); // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002647
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002648 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002649 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002650 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2651 {
2652 // Samplers are passed as indices to the sampler array.
2653 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002654 out << "const uint " << nameStr << ArrayString(type);
2655 return;
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002656 }
2657 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2658 {
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002659 out << QualifierString(qualifier) << " " << TextureString(type.getBasicType())
2660 << " texture_" << nameStr << ArrayString(type) << ", " << QualifierString(qualifier)
2661 << " " << SamplerString(type.getBasicType()) << " sampler_" << nameStr
2662 << ArrayString(type);
2663 return;
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002664 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002665 }
2666
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002667 out << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2668 << ArrayString(type);
Olli Etuaho96963162016-03-21 11:54:33 +02002669
2670 // If the structure parameter contains samplers, they need to be passed into the function as
2671 // separate parameters. HLSL doesn't natively support samplers in structs.
2672 if (type.isStructureContainingSamplers())
2673 {
2674 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002675 TVector<const TVariable *> samplerSymbols;
Olli Etuahofbb1c792018-01-19 16:26:59 +02002676 std::string namePrefix = "angle";
2677 namePrefix += nameStr.c_str();
2678 type.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols, nullptr,
2679 mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002680 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02002681 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002682 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002683 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2684 {
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002685 out << ", const uint " << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002686 }
2687 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2688 {
Olli Etuaho96963162016-03-21 11:54:33 +02002689 ASSERT(IsSampler(samplerType.getBasicType()));
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002690 out << ", " << QualifierString(qualifier) << " "
2691 << TextureString(samplerType.getBasicType()) << " texture_" << sampler->name()
2692 << ArrayString(samplerType) << ", " << QualifierString(qualifier) << " "
2693 << SamplerString(samplerType.getBasicType()) << " sampler_" << sampler->name()
2694 << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002695 }
2696 else
2697 {
Olli Etuaho96963162016-03-21 11:54:33 +02002698 ASSERT(IsSampler(samplerType.getBasicType()));
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002699 out << ", " << QualifierString(qualifier) << " " << TypeString(samplerType) << " "
2700 << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002701 }
2702 }
2703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002704}
2705
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002706TString OutputHLSL::zeroInitializer(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707{
2708 TString string;
2709
Jamie Madill94bf7f22013-07-08 13:31:15 -04002710 size_t size = type.getObjectSize();
2711 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002713 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714
Jamie Madill94bf7f22013-07-08 13:31:15 -04002715 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002716 {
2717 string += ", ";
2718 }
2719 }
2720
daniel@transgaming.comead23042010-04-29 03:35:36 +00002721 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002722}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002723
Olli Etuahobd3cd502017-11-03 15:48:52 +02002724void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002725{
Olli Etuahobd3cd502017-11-03 15:48:52 +02002726 // Array constructors should have been already pruned from the code.
2727 ASSERT(!node->getType().isArray());
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002728
2729 if (visit == PreVisit)
2730 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002731 TString constructorName;
2732 if (node->getBasicType() == EbtStruct)
2733 {
2734 constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct());
2735 }
2736 else
2737 {
2738 constructorName =
2739 mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence());
2740 }
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002741 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002742 }
2743 else if (visit == InVisit)
2744 {
2745 out << ", ";
2746 }
2747 else if (visit == PostVisit)
2748 {
2749 out << ")";
2750 }
2751}
2752
Jamie Madill8c46ab12015-12-07 16:39:19 -05002753const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2754 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002755 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002756{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002757 ASSERT(!type.isArray());
2758
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002759 const TConstantUnion *constUnionIterated = constUnion;
2760
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002761 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002762 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002763 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002764 out << mStructureHLSL->addStructConstructor(*structure) << "(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002765
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002766 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002767
Jamie Madill98493dd2013-07-08 14:39:03 -04002768 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002769 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002770 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002771 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002772
Jamie Madill98493dd2013-07-08 14:39:03 -04002773 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002774 {
2775 out << ", ";
2776 }
2777 }
2778
2779 out << ")";
2780 }
2781 else
2782 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002783 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002784 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002785
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002786 if (writeType)
2787 {
Jamie Madill033dae62014-06-18 12:56:28 -04002788 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002789 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002790 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002791 if (writeType)
2792 {
2793 out << ")";
2794 }
2795 }
2796
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002797 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002798}
2799
Olli Etuahod68924e2017-01-02 17:34:40 +00002800void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002801{
Olli Etuahod68924e2017-01-02 17:34:40 +00002802 if (visit == PreVisit)
2803 {
2804 const char *opStr = GetOperatorString(op);
2805 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2806 out << "(";
2807 }
2808 else
2809 {
2810 outputTriplet(out, visit, nullptr, ", ", ")");
2811 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002812}
2813
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002814bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2815 TIntermSymbol *symbolNode,
2816 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002817{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002818 ASSERT(symbolNode->variable().symbolType() != SymbolType::Empty);
2819 const TIntermSymbol *symbolInInitializer = FindSymbolNode(expression, symbolNode->getName());
Jamie Madill37997142015-01-28 10:06:34 -05002820
Olli Etuaho4728bdc2017-12-20 17:51:08 +02002821 if (symbolInInitializer)
Jamie Madill37997142015-01-28 10:06:34 -05002822 {
2823 // Type already printed
2824 out << "t" + str(mUniqueIndex) + " = ";
2825 expression->traverse(this);
2826 out << ", ";
2827 symbolNode->traverse(this);
2828 out << " = t" + str(mUniqueIndex);
2829
2830 mUniqueIndex++;
2831 return true;
2832 }
2833
2834 return false;
2835}
2836
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002837bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2838 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002839 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002840{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002841 if (initializer->hasConstantValue())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002842 {
2843 symbolNode->traverse(this);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002844 out << ArrayString(symbolNode->getType());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002845 out << " = {";
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002846 writeConstantUnionArray(out, initializer->getConstantValue(),
2847 initializer->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002848 out << "}";
2849 return true;
2850 }
2851 return false;
2852}
2853
Jamie Madill55e79e02015-02-09 15:35:00 -05002854TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2855{
2856 const TFieldList &fields = structure.fields();
2857
2858 for (const auto &eqFunction : mStructEqualityFunctions)
2859 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002860 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002861 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002862 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002863 }
2864 }
2865
2866 const TString &structNameString = StructNameString(structure);
2867
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002868 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002869 function->structure = &structure;
2870 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002871
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002872 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002873
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002874 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2875 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002876 << "{\n"
2877 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002878
2879 for (size_t i = 0; i < fields.size(); i++)
2880 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002881 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002882 const TType *fieldType = field->type();
2883
2884 const TString &fieldNameA = "a." + Decorate(field->name());
2885 const TString &fieldNameB = "b." + Decorate(field->name());
2886
2887 if (i > 0)
2888 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002889 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002890 }
2891
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002892 fnOut << "(";
2893 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2894 fnOut << fieldNameA;
2895 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2896 fnOut << fieldNameB;
2897 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2898 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002899 }
2900
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002901 fnOut << ";\n"
2902 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002903
2904 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002905
2906 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002907 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002908
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002909 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002910}
2911
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002912TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002913{
2914 for (const auto &eqFunction : mArrayEqualityFunctions)
2915 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002916 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002917 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002918 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002919 }
2920 }
2921
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002922 TType elementType(type);
2923 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002924
Olli Etuaho12690762015-03-31 12:55:28 +03002925 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002926 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002927
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002928 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002929
2930 TInfoSinkBase fnOut;
2931
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002932 const TString &typeName = TypeString(type);
2933 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2934 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002935 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002936 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002937 << type.getOutermostArraySize()
2938 << "; ++i)\n"
2939 " {\n"
2940 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002941
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002942 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002943 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002944 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002945 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002946 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002947
2948 fnOut << ") { return false; }\n"
2949 " }\n"
2950 " return true;\n"
2951 "}\n";
2952
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002953 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002954
2955 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002956 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002957
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002958 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002959}
2960
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002961TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002962{
2963 for (const auto &assignFunction : mArrayAssignmentFunctions)
2964 {
2965 if (assignFunction.type == type)
2966 {
2967 return assignFunction.functionName;
2968 }
2969 }
2970
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002971 TType elementType(type);
2972 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03002973
2974 ArrayHelperFunction function;
2975 function.type = type;
2976
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002977 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03002978
2979 TInfoSinkBase fnOut;
2980
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002981 const TString &typeName = TypeString(type);
2982 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
2983 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002984 << "{\n"
2985 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002986 << type.getOutermostArraySize()
2987 << "; ++i)\n"
2988 " {\n"
2989 " ";
2990
2991 outputAssign(PreVisit, elementType, fnOut);
2992 fnOut << "a[i]";
2993 outputAssign(InVisit, elementType, fnOut);
2994 fnOut << "b[i]";
2995 outputAssign(PostVisit, elementType, fnOut);
2996
2997 fnOut << ";\n"
2998 " }\n"
2999 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03003000
3001 function.functionDefinition = fnOut.c_str();
3002
3003 mArrayAssignmentFunctions.push_back(function);
3004
3005 return function.functionName;
3006}
3007
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003008TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003009{
3010 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3011 {
3012 if (constructIntoFunction.type == type)
3013 {
3014 return constructIntoFunction.functionName;
3015 }
3016 }
3017
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003018 TType elementType(type);
3019 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003020
3021 ArrayHelperFunction function;
3022 function.type = type;
3023
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003024 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003025
3026 TInfoSinkBase fnOut;
3027
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003028 const TString &typeName = TypeString(type);
3029 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3030 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003031 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003032 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003033 }
3034 fnOut << ")\n"
3035 "{\n";
3036
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003037 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003038 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003039 fnOut << " ";
3040 outputAssign(PreVisit, elementType, fnOut);
3041 fnOut << "a[" << i << "]";
3042 outputAssign(InVisit, elementType, fnOut);
3043 fnOut << "b" << i;
3044 outputAssign(PostVisit, elementType, fnOut);
3045 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003046 }
3047 fnOut << "}\n";
3048
3049 function.functionDefinition = fnOut.c_str();
3050
3051 mArrayConstructIntoFunctions.push_back(function);
3052
3053 return function.functionName;
3054}
3055
Jamie Madill2e295e22015-04-29 10:41:33 -04003056void OutputHLSL::ensureStructDefined(const TType &type)
3057{
Olli Etuahobd3cd502017-11-03 15:48:52 +02003058 const TStructure *structure = type.getStruct();
Jamie Madill2e295e22015-04-29 10:41:33 -04003059 if (structure)
3060 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02003061 ASSERT(type.getBasicType() == EbtStruct);
3062 mStructureHLSL->ensureStructDefined(*structure);
Jamie Madill2e295e22015-04-29 10:41:33 -04003063 }
3064}
3065
Jamie Madill45bcc782016-11-07 13:58:48 -05003066} // namespace sh