blob: 7bdb2c9ffe717a4b0713b6b243287875c45bf4a7 [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
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001013void OutputHLSL::visitRaw(TIntermRaw *node)
1014{
Jamie Madill32aab012015-01-27 14:12:26 -05001015 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001016}
1017
Olli Etuaho7fb49552015-03-18 17:27:44 +02001018void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1019{
1020 if (type.isScalar() && !type.isArray())
1021 {
1022 if (op == EOpEqual)
1023 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001024 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001025 }
1026 else
1027 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001028 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001029 }
1030 }
1031 else
1032 {
1033 if (visit == PreVisit && op == EOpNotEqual)
1034 {
1035 out << "!";
1036 }
1037
1038 if (type.isArray())
1039 {
1040 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001041 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001042 }
1043 else if (type.getBasicType() == EbtStruct)
1044 {
1045 const TStructure &structure = *type.getStruct();
1046 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001047 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001048 }
1049 else
1050 {
1051 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001052 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001053 }
1054 }
1055}
1056
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001057void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1058{
1059 if (type.isArray())
1060 {
1061 const TString &functionName = addArrayAssignmentFunction(type);
1062 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1063 }
1064 else
1065 {
1066 outputTriplet(out, visit, "(", " = ", ")");
1067 }
1068}
1069
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001070bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001071{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001072 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001073 {
1074 TIntermNode *ancestor = getAncestorNode(n);
1075 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1076 if (ancestorBinary == nullptr)
1077 {
1078 return false;
1079 }
1080 switch (ancestorBinary->getOp())
1081 {
1082 case EOpIndexDirectStruct:
1083 {
1084 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1085 const TIntermConstantUnion *index =
1086 ancestorBinary->getRight()->getAsConstantUnion();
1087 const TField *field = structure->fields()[index->getIConst(0)];
1088 if (IsSampler(field->type()->getBasicType()))
1089 {
1090 return true;
1091 }
1092 break;
1093 }
1094 case EOpIndexDirect:
1095 break;
1096 default:
1097 // Returning a sampler from indirect indexing is not supported.
1098 return false;
1099 }
1100 }
1101 return false;
1102}
1103
Olli Etuahob6fa0432016-09-28 16:28:05 +01001104bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1105{
1106 TInfoSinkBase &out = getInfoSink();
1107 if (visit == PostVisit)
1108 {
1109 out << ".";
1110 node->writeOffsetsAsXYZW(&out);
1111 }
1112 return true;
1113}
1114
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001115bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1116{
Jamie Madill32aab012015-01-27 14:12:26 -05001117 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001118
1119 switch (node->getOp())
1120 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001121 case EOpComma:
1122 outputTriplet(out, visit, "(", ", ", ")");
1123 break;
1124 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001125 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001126 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001127 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1128 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001129 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001130 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1131 out << functionName << "(";
1132 node->getLeft()->traverse(this);
1133 TIntermSequence *seq = rightAgg->getSequence();
1134 for (auto &arrayElement : *seq)
1135 {
1136 out << ", ";
1137 arrayElement->traverse(this);
1138 }
1139 out << ")";
1140 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001141 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001142 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1143 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001144 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001145 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001146 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001147 break;
1148 case EOpInitialize:
1149 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001150 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001151 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1152 ASSERT(symbolNode);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001153 TIntermTyped *initializer = node->getRight();
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001154
1155 // Global initializers must be constant at this point.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001156 ASSERT(symbolNode->getQualifier() != EvqGlobal || initializer->hasConstantValue());
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001157
1158 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1159 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1160 // new variable is created before the assignment is evaluated), so we need to
1161 // convert
1162 // this to "float t = x, x = t;".
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001163 if (writeSameSymbolInitializer(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001164 {
1165 // Skip initializing the rest of the expression
1166 return false;
1167 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001168 else if (writeConstantInitialization(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001169 {
1170 return false;
1171 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001172 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001173 else if (visit == InVisit)
1174 {
1175 out << " = ";
1176 }
1177 break;
1178 case EOpAddAssign:
1179 outputTriplet(out, visit, "(", " += ", ")");
1180 break;
1181 case EOpSubAssign:
1182 outputTriplet(out, visit, "(", " -= ", ")");
1183 break;
1184 case EOpMulAssign:
1185 outputTriplet(out, visit, "(", " *= ", ")");
1186 break;
1187 case EOpVectorTimesScalarAssign:
1188 outputTriplet(out, visit, "(", " *= ", ")");
1189 break;
1190 case EOpMatrixTimesScalarAssign:
1191 outputTriplet(out, visit, "(", " *= ", ")");
1192 break;
1193 case EOpVectorTimesMatrixAssign:
1194 if (visit == PreVisit)
1195 {
1196 out << "(";
1197 }
1198 else if (visit == InVisit)
1199 {
1200 out << " = mul(";
1201 node->getLeft()->traverse(this);
1202 out << ", transpose(";
1203 }
1204 else
1205 {
1206 out << ")))";
1207 }
1208 break;
1209 case EOpMatrixTimesMatrixAssign:
1210 if (visit == PreVisit)
1211 {
1212 out << "(";
1213 }
1214 else if (visit == InVisit)
1215 {
1216 out << " = transpose(mul(transpose(";
1217 node->getLeft()->traverse(this);
1218 out << "), transpose(";
1219 }
1220 else
1221 {
1222 out << "))))";
1223 }
1224 break;
1225 case EOpDivAssign:
1226 outputTriplet(out, visit, "(", " /= ", ")");
1227 break;
1228 case EOpIModAssign:
1229 outputTriplet(out, visit, "(", " %= ", ")");
1230 break;
1231 case EOpBitShiftLeftAssign:
1232 outputTriplet(out, visit, "(", " <<= ", ")");
1233 break;
1234 case EOpBitShiftRightAssign:
1235 outputTriplet(out, visit, "(", " >>= ", ")");
1236 break;
1237 case EOpBitwiseAndAssign:
1238 outputTriplet(out, visit, "(", " &= ", ")");
1239 break;
1240 case EOpBitwiseXorAssign:
1241 outputTriplet(out, visit, "(", " ^= ", ")");
1242 break;
1243 case EOpBitwiseOrAssign:
1244 outputTriplet(out, visit, "(", " |= ", ")");
1245 break;
1246 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001247 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001248 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001249 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001250 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001251 if (visit == PreVisit)
1252 {
Olli Etuaho12a18ad2017-12-01 16:59:47 +02001253 TIntermSymbol *instanceArraySymbol = node->getLeft()->getAsSymbolNode();
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001254 const TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Olli Etuahoc71862a2017-12-21 12:58:29 +02001255 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
1256 {
1257 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
1258 new TReferencedBlock(interfaceBlock, &instanceArraySymbol->variable());
1259 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001260 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001261 out << mUniformHLSL->UniformBlockInstanceString(instanceArraySymbol->getName(),
1262 arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001263 return false;
1264 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001265 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001266 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001267 {
1268 // All parts of an expression that access a sampler in a struct need to use _ as
1269 // separator to access the sampler variable that has been moved out of the struct.
1270 outputTriplet(out, visit, "", "_", "");
1271 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001272 else
1273 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001274 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001275 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001276 }
1277 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001278 case EOpIndexIndirect:
1279 // We do not currently support indirect references to interface blocks
1280 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1281 outputTriplet(out, visit, "", "[", "]");
1282 break;
1283 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001284 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001285 const TStructure *structure = node->getLeft()->getType().getStruct();
1286 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1287 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001288
Olli Etuaho96963162016-03-21 11:54:33 +02001289 // In cases where indexing returns a sampler, we need to access the sampler variable
1290 // that has been moved out of the struct.
1291 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1292 if (visit == PreVisit && indexingReturnsSampler)
1293 {
1294 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1295 // This prefix is only output at the beginning of the indexing expression, which
1296 // may have multiple parts.
1297 out << "angle";
1298 }
1299 if (!indexingReturnsSampler)
1300 {
1301 // All parts of an expression that access a sampler in a struct need to use _ as
1302 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001303 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001304 }
1305 if (visit == InVisit)
1306 {
1307 if (indexingReturnsSampler)
1308 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001309 out << "_" << field->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001310 }
1311 else
1312 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001313 out << "." << DecorateField(field->name(), *structure);
Olli Etuaho96963162016-03-21 11:54:33 +02001314 }
1315
1316 return false;
1317 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001318 }
1319 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001320 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001321 {
1322 bool structInStd140Block =
1323 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1324 if (visit == PreVisit && structInStd140Block)
1325 {
1326 out << "map";
1327 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001328 if (visit == InVisit)
1329 {
1330 const TInterfaceBlock *interfaceBlock =
1331 node->getLeft()->getType().getInterfaceBlock();
1332 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1333 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001334 if (structInStd140Block)
1335 {
1336 out << "_";
1337 }
1338 else
1339 {
1340 out << ".";
1341 }
1342 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001343
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001344 return false;
1345 }
1346 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001347 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001348 case EOpAdd:
1349 outputTriplet(out, visit, "(", " + ", ")");
1350 break;
1351 case EOpSub:
1352 outputTriplet(out, visit, "(", " - ", ")");
1353 break;
1354 case EOpMul:
1355 outputTriplet(out, visit, "(", " * ", ")");
1356 break;
1357 case EOpDiv:
1358 outputTriplet(out, visit, "(", " / ", ")");
1359 break;
1360 case EOpIMod:
1361 outputTriplet(out, visit, "(", " % ", ")");
1362 break;
1363 case EOpBitShiftLeft:
1364 outputTriplet(out, visit, "(", " << ", ")");
1365 break;
1366 case EOpBitShiftRight:
1367 outputTriplet(out, visit, "(", " >> ", ")");
1368 break;
1369 case EOpBitwiseAnd:
1370 outputTriplet(out, visit, "(", " & ", ")");
1371 break;
1372 case EOpBitwiseXor:
1373 outputTriplet(out, visit, "(", " ^ ", ")");
1374 break;
1375 case EOpBitwiseOr:
1376 outputTriplet(out, visit, "(", " | ", ")");
1377 break;
1378 case EOpEqual:
1379 case EOpNotEqual:
1380 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1381 break;
1382 case EOpLessThan:
1383 outputTriplet(out, visit, "(", " < ", ")");
1384 break;
1385 case EOpGreaterThan:
1386 outputTriplet(out, visit, "(", " > ", ")");
1387 break;
1388 case EOpLessThanEqual:
1389 outputTriplet(out, visit, "(", " <= ", ")");
1390 break;
1391 case EOpGreaterThanEqual:
1392 outputTriplet(out, visit, "(", " >= ", ")");
1393 break;
1394 case EOpVectorTimesScalar:
1395 outputTriplet(out, visit, "(", " * ", ")");
1396 break;
1397 case EOpMatrixTimesScalar:
1398 outputTriplet(out, visit, "(", " * ", ")");
1399 break;
1400 case EOpVectorTimesMatrix:
1401 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1402 break;
1403 case EOpMatrixTimesVector:
1404 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1405 break;
1406 case EOpMatrixTimesMatrix:
1407 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1408 break;
1409 case EOpLogicalOr:
1410 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1411 // been unfolded.
1412 ASSERT(!node->getRight()->hasSideEffects());
1413 outputTriplet(out, visit, "(", " || ", ")");
1414 return true;
1415 case EOpLogicalXor:
1416 mUsesXor = true;
1417 outputTriplet(out, visit, "xor(", ", ", ")");
1418 break;
1419 case EOpLogicalAnd:
1420 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1421 // been unfolded.
1422 ASSERT(!node->getRight()->hasSideEffects());
1423 outputTriplet(out, visit, "(", " && ", ")");
1424 return true;
1425 default:
1426 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001427 }
1428
1429 return true;
1430}
1431
1432bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1433{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001434 TInfoSinkBase &out = getInfoSink();
1435
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001436 switch (node->getOp())
1437 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001438 case EOpNegative:
1439 outputTriplet(out, visit, "(-", "", ")");
1440 break;
1441 case EOpPositive:
1442 outputTriplet(out, visit, "(+", "", ")");
1443 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001444 case EOpLogicalNot:
1445 outputTriplet(out, visit, "(!", "", ")");
1446 break;
1447 case EOpBitwiseNot:
1448 outputTriplet(out, visit, "(~", "", ")");
1449 break;
1450 case EOpPostIncrement:
1451 outputTriplet(out, visit, "(", "", "++)");
1452 break;
1453 case EOpPostDecrement:
1454 outputTriplet(out, visit, "(", "", "--)");
1455 break;
1456 case EOpPreIncrement:
1457 outputTriplet(out, visit, "(++", "", ")");
1458 break;
1459 case EOpPreDecrement:
1460 outputTriplet(out, visit, "(--", "", ")");
1461 break;
1462 case EOpRadians:
1463 outputTriplet(out, visit, "radians(", "", ")");
1464 break;
1465 case EOpDegrees:
1466 outputTriplet(out, visit, "degrees(", "", ")");
1467 break;
1468 case EOpSin:
1469 outputTriplet(out, visit, "sin(", "", ")");
1470 break;
1471 case EOpCos:
1472 outputTriplet(out, visit, "cos(", "", ")");
1473 break;
1474 case EOpTan:
1475 outputTriplet(out, visit, "tan(", "", ")");
1476 break;
1477 case EOpAsin:
1478 outputTriplet(out, visit, "asin(", "", ")");
1479 break;
1480 case EOpAcos:
1481 outputTriplet(out, visit, "acos(", "", ")");
1482 break;
1483 case EOpAtan:
1484 outputTriplet(out, visit, "atan(", "", ")");
1485 break;
1486 case EOpSinh:
1487 outputTriplet(out, visit, "sinh(", "", ")");
1488 break;
1489 case EOpCosh:
1490 outputTriplet(out, visit, "cosh(", "", ")");
1491 break;
1492 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001493 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001494 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001495 case EOpAtanh:
1496 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001497 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001498 break;
1499 case EOpExp:
1500 outputTriplet(out, visit, "exp(", "", ")");
1501 break;
1502 case EOpLog:
1503 outputTriplet(out, visit, "log(", "", ")");
1504 break;
1505 case EOpExp2:
1506 outputTriplet(out, visit, "exp2(", "", ")");
1507 break;
1508 case EOpLog2:
1509 outputTriplet(out, visit, "log2(", "", ")");
1510 break;
1511 case EOpSqrt:
1512 outputTriplet(out, visit, "sqrt(", "", ")");
1513 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001514 case EOpInversesqrt:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001515 outputTriplet(out, visit, "rsqrt(", "", ")");
1516 break;
1517 case EOpAbs:
1518 outputTriplet(out, visit, "abs(", "", ")");
1519 break;
1520 case EOpSign:
1521 outputTriplet(out, visit, "sign(", "", ")");
1522 break;
1523 case EOpFloor:
1524 outputTriplet(out, visit, "floor(", "", ")");
1525 break;
1526 case EOpTrunc:
1527 outputTriplet(out, visit, "trunc(", "", ")");
1528 break;
1529 case EOpRound:
1530 outputTriplet(out, visit, "round(", "", ")");
1531 break;
1532 case EOpRoundEven:
1533 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001534 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001535 break;
1536 case EOpCeil:
1537 outputTriplet(out, visit, "ceil(", "", ")");
1538 break;
1539 case EOpFract:
1540 outputTriplet(out, visit, "frac(", "", ")");
1541 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001542 case EOpIsnan:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001543 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001544 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001545 else
1546 outputTriplet(out, visit, "isnan(", "", ")");
1547 mRequiresIEEEStrictCompiling = true;
1548 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02001549 case EOpIsinf:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001550 outputTriplet(out, visit, "isinf(", "", ")");
1551 break;
1552 case EOpFloatBitsToInt:
1553 outputTriplet(out, visit, "asint(", "", ")");
1554 break;
1555 case EOpFloatBitsToUint:
1556 outputTriplet(out, visit, "asuint(", "", ")");
1557 break;
1558 case EOpIntBitsToFloat:
1559 outputTriplet(out, visit, "asfloat(", "", ")");
1560 break;
1561 case EOpUintBitsToFloat:
1562 outputTriplet(out, visit, "asfloat(", "", ")");
1563 break;
1564 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001565 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001566 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001567 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001568 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001569 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001570 case EOpPackUnorm4x8:
1571 case EOpPackSnorm4x8:
1572 case EOpUnpackUnorm4x8:
1573 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001574 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001575 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001576 break;
1577 case EOpLength:
1578 outputTriplet(out, visit, "length(", "", ")");
1579 break;
1580 case EOpNormalize:
1581 outputTriplet(out, visit, "normalize(", "", ")");
1582 break;
1583 case EOpDFdx:
1584 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1585 {
1586 outputTriplet(out, visit, "(", "", ", 0.0)");
1587 }
1588 else
1589 {
1590 outputTriplet(out, visit, "ddx(", "", ")");
1591 }
1592 break;
1593 case EOpDFdy:
1594 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1595 {
1596 outputTriplet(out, visit, "(", "", ", 0.0)");
1597 }
1598 else
1599 {
1600 outputTriplet(out, visit, "ddy(", "", ")");
1601 }
1602 break;
1603 case EOpFwidth:
1604 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1605 {
1606 outputTriplet(out, visit, "(", "", ", 0.0)");
1607 }
1608 else
1609 {
1610 outputTriplet(out, visit, "fwidth(", "", ")");
1611 }
1612 break;
1613 case EOpTranspose:
1614 outputTriplet(out, visit, "transpose(", "", ")");
1615 break;
1616 case EOpDeterminant:
1617 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1618 break;
1619 case EOpInverse:
1620 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001621 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001622 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001623
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001624 case EOpAny:
1625 outputTriplet(out, visit, "any(", "", ")");
1626 break;
1627 case EOpAll:
1628 outputTriplet(out, visit, "all(", "", ")");
1629 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001630 case EOpLogicalNotComponentWise:
1631 outputTriplet(out, visit, "(!", "", ")");
1632 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001633 case EOpBitfieldReverse:
1634 outputTriplet(out, visit, "reversebits(", "", ")");
1635 break;
1636 case EOpBitCount:
1637 outputTriplet(out, visit, "countbits(", "", ")");
1638 break;
1639 case EOpFindLSB:
1640 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1641 // in GLSLTest and results are consistent with GL.
1642 outputTriplet(out, visit, "firstbitlow(", "", ")");
1643 break;
1644 case EOpFindMSB:
1645 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1646 // tested in GLSLTest and results are consistent with GL.
1647 outputTriplet(out, visit, "firstbithigh(", "", ")");
1648 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001649 default:
1650 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001651 }
1652
1653 return true;
1654}
1655
Olli Etuahofbb1c792018-01-19 16:26:59 +02001656ImmutableString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
Olli Etuaho96963162016-03-21 11:54:33 +02001657{
1658 if (node->getAsSymbolNode())
1659 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001660 ASSERT(node->getAsSymbolNode()->variable().symbolType() != SymbolType::Empty);
1661 return node->getAsSymbolNode()->getName();
Olli Etuaho96963162016-03-21 11:54:33 +02001662 }
1663 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1664 switch (nodeBinary->getOp())
1665 {
1666 case EOpIndexDirect:
1667 {
1668 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1669
Olli Etuahofbb1c792018-01-19 16:26:59 +02001670 std::stringstream prefixSink;
Olli Etuaho96963162016-03-21 11:54:33 +02001671 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
Olli Etuahofbb1c792018-01-19 16:26:59 +02001672 return ImmutableString(prefixSink.str());
Olli Etuaho96963162016-03-21 11:54:33 +02001673 }
1674 case EOpIndexDirectStruct:
1675 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001676 const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001677 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1678 const TField *field = s->fields()[index];
1679
Olli Etuahofbb1c792018-01-19 16:26:59 +02001680 std::stringstream prefixSink;
Olli Etuaho96963162016-03-21 11:54:33 +02001681 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1682 << field->name();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001683 return ImmutableString(prefixSink.str());
Olli Etuaho96963162016-03-21 11:54:33 +02001684 }
1685 default:
1686 UNREACHABLE();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001687 return ImmutableString("");
Olli Etuaho96963162016-03-21 11:54:33 +02001688 }
1689}
1690
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001691bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1692{
1693 TInfoSinkBase &out = getInfoSink();
1694
1695 if (mInsideFunction)
1696 {
1697 outputLineDirective(out, node->getLine().first_line);
1698 out << "{\n";
1699 }
1700
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001701 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001702 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001703 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001704
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001705 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001706
1707 // Don't output ; after case labels, they're terminated by :
1708 // This is needed especially since outputting a ; after a case statement would turn empty
1709 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001710 // Also the output code is clearer if we don't output ; after statements where it is not
1711 // needed:
1712 // * if statements
1713 // * switch statements
1714 // * blocks
1715 // * function definitions
1716 // * loops (do-while loops output the semicolon in VisitLoop)
1717 // * declarations that don't generate output.
1718 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1719 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1720 statement->getAsSwitchNode() == nullptr &&
1721 statement->getAsFunctionDefinition() == nullptr &&
1722 (statement->getAsDeclarationNode() == nullptr ||
1723 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1724 statement->getAsInvariantDeclarationNode() == nullptr)
1725 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001726 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001727 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001728 }
1729
1730 if (mInsideFunction)
1731 {
1732 outputLineDirective(out, node->getLine().last_line);
1733 out << "}\n";
1734 }
1735
1736 return false;
1737}
1738
Olli Etuaho336b1472016-10-05 16:37:55 +01001739bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1740{
1741 TInfoSinkBase &out = getInfoSink();
1742
1743 ASSERT(mCurrentFunctionMetadata == nullptr);
1744
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001745 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho336b1472016-10-05 16:37:55 +01001746 ASSERT(index != CallDAG::InvalidIndex);
1747 mCurrentFunctionMetadata = &mASTMetadataList[index];
1748
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001749 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001750
Olli Etuahod4bd9632018-03-08 16:32:44 +02001751 const TFunction *func = node->getFunction();
Olli Etuaho336b1472016-10-05 16:37:55 +01001752
Olli Etuahod4bd9632018-03-08 16:32:44 +02001753 if (func->isMain())
Olli Etuaho336b1472016-10-05 16:37:55 +01001754 {
1755 out << "gl_main(";
1756 }
1757 else
1758 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001759 out << DecorateFunctionIfNeeded(func) << DisambiguateFunctionName(func)
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001760 << (mOutputLod0Function ? "Lod0(" : "(");
Olli Etuaho336b1472016-10-05 16:37:55 +01001761 }
1762
Olli Etuahod4bd9632018-03-08 16:32:44 +02001763 size_t paramCount = func->getParamCount();
1764 for (unsigned int i = 0; i < paramCount; i++)
Olli Etuaho336b1472016-10-05 16:37:55 +01001765 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001766 const TVariable *param = func->getParam(i);
1767 ensureStructDefined(param->getType());
Olli Etuaho336b1472016-10-05 16:37:55 +01001768
Olli Etuahod4bd9632018-03-08 16:32:44 +02001769 writeParameter(param, out);
1770
1771 if (i < paramCount - 1)
Olli Etuaho336b1472016-10-05 16:37:55 +01001772 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001773 out << ", ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001774 }
Olli Etuaho336b1472016-10-05 16:37:55 +01001775 }
1776
1777 out << ")\n";
1778
1779 mInsideFunction = true;
1780 // The function body node will output braces.
1781 node->getBody()->traverse(this);
1782 mInsideFunction = false;
1783
1784 mCurrentFunctionMetadata = nullptr;
1785
1786 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1787 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1788 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001789 ASSERT(!node->getFunction()->isMain());
Olli Etuaho336b1472016-10-05 16:37:55 +01001790 mOutputLod0Function = true;
1791 node->traverse(this);
1792 mOutputLod0Function = false;
1793 }
1794
1795 return false;
1796}
1797
Olli Etuaho13389b62016-10-16 11:48:18 +01001798bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1799{
Olli Etuaho13389b62016-10-16 11:48:18 +01001800 if (visit == PreVisit)
1801 {
1802 TIntermSequence *sequence = node->getSequence();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001803 TIntermTyped *declarator = (*sequence)[0]->getAsTyped();
Olli Etuaho13389b62016-10-16 11:48:18 +01001804 ASSERT(sequence->size() == 1);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001805 ASSERT(declarator);
Olli Etuaho13389b62016-10-16 11:48:18 +01001806
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001807 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001808 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001809 TInfoSinkBase &out = getInfoSink();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001810 ensureStructDefined(declarator->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001811
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001812 if (!declarator->getAsSymbolNode() ||
1813 declarator->getAsSymbolNode()->variable().symbolType() !=
1814 SymbolType::Empty) // Variable declaration
Olli Etuaho13389b62016-10-16 11:48:18 +01001815 {
1816 if (!mInsideFunction)
1817 {
1818 out << "static ";
1819 }
1820
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001821 out << TypeString(declarator->getType()) + " ";
Olli Etuaho13389b62016-10-16 11:48:18 +01001822
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001823 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho13389b62016-10-16 11:48:18 +01001824
1825 if (symbol)
1826 {
1827 symbol->traverse(this);
1828 out << ArrayString(symbol->getType());
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001829 out << " = " + zeroInitializer(symbol->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001830 }
1831 else
1832 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001833 declarator->traverse(this);
Olli Etuaho13389b62016-10-16 11:48:18 +01001834 }
1835 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001836 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001837 else if (IsVaryingOut(declarator->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001838 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001839 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho282847e2017-07-12 14:11:01 +03001840 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001841
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001842 const TVariable &variable = symbol->variable();
1843
1844 if (variable.symbolType() != SymbolType::Empty)
Olli Etuaho93b059d2017-12-20 12:46:58 +02001845 {
1846 // Vertex outputs which are declared but not written to should still be declared to
1847 // allow successful linking.
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001848 mReferencedVaryings[symbol->uniqueId().get()] = &variable;
Olli Etuaho93b059d2017-12-20 12:46:58 +02001849 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001850 }
1851 }
1852 return false;
1853}
1854
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001855bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1856{
1857 // Do not do any translation
1858 return false;
1859}
1860
Olli Etuahod4bd9632018-03-08 16:32:44 +02001861void OutputHLSL::visitFunctionPrototype(TIntermFunctionPrototype *node)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001862{
1863 TInfoSinkBase &out = getInfoSink();
1864
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001865 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001866 // Skip the prototype if it is not implemented (and thus not used)
1867 if (index == CallDAG::InvalidIndex)
1868 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001869 return;
Olli Etuaho16c745a2017-01-16 17:02:27 +00001870 }
1871
Olli Etuahod4bd9632018-03-08 16:32:44 +02001872 const TFunction *func = node->getFunction();
Olli Etuaho16c745a2017-01-16 17:02:27 +00001873
Olli Etuahod4bd9632018-03-08 16:32:44 +02001874 TString name = DecorateFunctionIfNeeded(func);
1875 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(func)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001876 << (mOutputLod0Function ? "Lod0(" : "(");
1877
Olli Etuahod4bd9632018-03-08 16:32:44 +02001878 size_t paramCount = func->getParamCount();
1879 for (unsigned int i = 0; i < paramCount; i++)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001880 {
Olli Etuahod4bd9632018-03-08 16:32:44 +02001881 writeParameter(func->getParam(i), out);
Olli Etuaho16c745a2017-01-16 17:02:27 +00001882
Olli Etuahod4bd9632018-03-08 16:32:44 +02001883 if (i < paramCount - 1)
Olli Etuaho16c745a2017-01-16 17:02:27 +00001884 {
1885 out << ", ";
1886 }
1887 }
1888
1889 out << ");\n";
1890
1891 // Also prototype the Lod0 variant if needed
1892 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1893 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1894 {
1895 mOutputLod0Function = true;
1896 node->traverse(this);
1897 mOutputLod0Function = false;
1898 }
Olli Etuaho16c745a2017-01-16 17:02:27 +00001899}
1900
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001901bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1902{
Jamie Madill32aab012015-01-27 14:12:26 -05001903 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001905 switch (node->getOp())
1906 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001907 case EOpCallBuiltInFunction:
1908 case EOpCallFunctionInAST:
1909 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001911 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001912
Corentin Wallez1239ee92015-03-19 14:38:02 -07001913 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001914 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001916 if (node->isArray())
1917 {
1918 UNIMPLEMENTED();
1919 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001920 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001921 ASSERT(index != CallDAG::InvalidIndex);
1922 lod0 &= mASTMetadataList[index].mNeedsLod0;
1923
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001924 out << DecorateFunctionIfNeeded(node->getFunction());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001925 out << DisambiguateFunctionName(node->getSequence());
1926 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001928 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001929 {
1930 // This path is used for internal functions that don't have their definitions in the
1931 // AST, such as precision emulation functions.
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001932 out << DecorateFunctionIfNeeded(node->getFunction()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001933 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001934 else if (node->getFunction()->isImageFunction())
Xinghua Cao711b7a12017-10-09 13:38:12 +08001935 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001936 const ImmutableString &name = node->getFunction()->name();
Xinghua Cao711b7a12017-10-09 13:38:12 +08001937 TType type = (*arguments)[0]->getAsTyped()->getType();
Olli Etuahofbb1c792018-01-19 16:26:59 +02001938 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001939 name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
Xinghua Cao711b7a12017-10-09 13:38:12 +08001940 type.getMemoryQualifier().readonly);
1941 out << imageFunctionName << "(";
1942 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001943 else
1944 {
Olli Etuahofbb1c792018-01-19 16:26:59 +02001945 const ImmutableString &name = node->getFunction()->name();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001946 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001947 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1948 if (arguments->size() > 1)
1949 {
1950 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1951 }
Olli Etuahob4cc49f2018-01-25 14:37:06 +02001952 const ImmutableString &textureFunctionName =
1953 mTextureFunctionHLSL->useTextureFunction(name, samplerType, coords,
1954 arguments->size(), lod0, mShaderType);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001955 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001956 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001957
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001958 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001959 {
Olli Etuaho96963162016-03-21 11:54:33 +02001960 TIntermTyped *typedArg = (*arg)->getAsTyped();
1961 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001962 {
1963 out << "texture_";
1964 (*arg)->traverse(this);
1965 out << ", sampler_";
1966 }
1967
1968 (*arg)->traverse(this);
1969
Olli Etuaho96963162016-03-21 11:54:33 +02001970 if (typedArg->getType().isStructureContainingSamplers())
1971 {
1972 const TType &argType = typedArg->getType();
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001973 TVector<const TVariable *> samplerSymbols;
Olli Etuahofbb1c792018-01-19 16:26:59 +02001974 ImmutableString structName = samplerNamePrefixFromStruct(typedArg);
1975 std::string namePrefix = "angle_";
1976 namePrefix += structName.data();
1977 argType.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols,
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001978 nullptr, mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001979 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02001980 {
1981 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1982 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001983 out << ", texture_" << sampler->name();
1984 out << ", sampler_" << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001985 }
1986 else
1987 {
1988 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1989 // of D3D9, it's the sampler variable.
Olli Etuahofbb1c792018-01-19 16:26:59 +02001990 out << ", " << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001991 }
1992 }
1993 }
1994
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001995 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001996 {
1997 out << ", ";
1998 }
1999 }
2000
2001 out << ")";
2002
2003 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002004 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03002005 case EOpConstruct:
Olli Etuahobd3cd502017-11-03 15:48:52 +02002006 outputConstructor(out, visit, node);
Olli Etuaho8fab3202017-05-08 18:22:22 +03002007 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002008 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002009 outputTriplet(out, visit, "(", " == ", ")");
2010 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002011 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002012 outputTriplet(out, visit, "(", " != ", ")");
2013 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002014 case EOpLessThanComponentWise:
2015 outputTriplet(out, visit, "(", " < ", ")");
2016 break;
2017 case EOpGreaterThanComponentWise:
2018 outputTriplet(out, visit, "(", " > ", ")");
2019 break;
2020 case EOpLessThanEqualComponentWise:
2021 outputTriplet(out, visit, "(", " <= ", ")");
2022 break;
2023 case EOpGreaterThanEqualComponentWise:
2024 outputTriplet(out, visit, "(", " >= ", ")");
2025 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002026 case EOpMod:
2027 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002028 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002029 break;
2030 case EOpModf:
2031 outputTriplet(out, visit, "modf(", ", ", ")");
2032 break;
2033 case EOpPow:
2034 outputTriplet(out, visit, "pow(", ", ", ")");
2035 break;
2036 case EOpAtan:
2037 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2038 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002039 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002040 break;
2041 case EOpMin:
2042 outputTriplet(out, visit, "min(", ", ", ")");
2043 break;
2044 case EOpMax:
2045 outputTriplet(out, visit, "max(", ", ", ")");
2046 break;
2047 case EOpClamp:
2048 outputTriplet(out, visit, "clamp(", ", ", ")");
2049 break;
2050 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302051 {
2052 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2053 if (lastParamNode->getType().getBasicType() == EbtBool)
2054 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002055 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2056 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302057 // so use emulated version.
2058 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002059 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302060 }
2061 else
2062 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002063 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302064 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002065 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302066 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002067 case EOpStep:
2068 outputTriplet(out, visit, "step(", ", ", ")");
2069 break;
Olli Etuahof7f0b8c2018-02-21 20:02:23 +02002070 case EOpSmoothstep:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002071 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2072 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002073 case EOpFrexp:
2074 case EOpLdexp:
2075 ASSERT(node->getUseEmulatedFunction());
2076 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2077 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002078 case EOpDistance:
2079 outputTriplet(out, visit, "distance(", ", ", ")");
2080 break;
2081 case EOpDot:
2082 outputTriplet(out, visit, "dot(", ", ", ")");
2083 break;
2084 case EOpCross:
2085 outputTriplet(out, visit, "cross(", ", ", ")");
2086 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002087 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002088 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002089 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002090 break;
2091 case EOpReflect:
2092 outputTriplet(out, visit, "reflect(", ", ", ")");
2093 break;
2094 case EOpRefract:
2095 outputTriplet(out, visit, "refract(", ", ", ")");
2096 break;
2097 case EOpOuterProduct:
2098 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002099 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002100 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002101 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002102 outputTriplet(out, visit, "(", " * ", ")");
2103 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002104 case EOpBitfieldExtract:
2105 case EOpBitfieldInsert:
2106 case EOpUaddCarry:
2107 case EOpUsubBorrow:
2108 case EOpUmulExtended:
2109 case EOpImulExtended:
2110 ASSERT(node->getUseEmulatedFunction());
2111 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2112 break;
Xinghua Cao47335852018-02-12 15:41:55 +08002113 case EOpBarrier:
2114 // barrier() is translated to GroupMemoryBarrierWithGroupSync(), which is the
2115 // cheapest *WithGroupSync() function, without any functionality loss, but
2116 // with the potential for severe performance loss.
2117 outputTriplet(out, visit, "GroupMemoryBarrierWithGroupSync(", "", ")");
2118 break;
2119 case EOpMemoryBarrierShared:
2120 outputTriplet(out, visit, "GroupMemoryBarrier(", "", ")");
2121 break;
2122 case EOpMemoryBarrierAtomicCounter:
2123 case EOpMemoryBarrierBuffer:
2124 case EOpMemoryBarrierImage:
2125 outputTriplet(out, visit, "DeviceMemoryBarrier(", "", ")");
2126 break;
2127 case EOpGroupMemoryBarrier:
2128 case EOpMemoryBarrier:
2129 outputTriplet(out, visit, "AllMemoryBarrier(", "", ")");
2130 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002131 default:
2132 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 }
2134
2135 return true;
2136}
2137
Olli Etuaho57961272016-09-14 13:57:46 +03002138void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002140 out << "if (";
2141
2142 node->getCondition()->traverse(this);
2143
2144 out << ")\n";
2145
Jamie Madill8c46ab12015-12-07 16:39:19 -05002146 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002147
2148 bool discard = false;
2149
2150 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002152 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002153 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002154
Olli Etuahoa6f22092015-05-08 18:31:10 +03002155 // Detect true discard
2156 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2157 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002158 else
2159 {
2160 // TODO(oetuaho): Check if the semicolon inside is necessary.
2161 // It's there as a result of conservative refactoring of the output.
2162 out << "{;}\n";
2163 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002164
Jamie Madill8c46ab12015-12-07 16:39:19 -05002165 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002166
Olli Etuahoa6f22092015-05-08 18:31:10 +03002167 if (node->getFalseBlock())
2168 {
2169 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002170
Jamie Madill8c46ab12015-12-07 16:39:19 -05002171 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172
Olli Etuaho32db19b2016-10-04 14:43:16 +01002173 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002174 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002175
Jamie Madill8c46ab12015-12-07 16:39:19 -05002176 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002177
Olli Etuahoa6f22092015-05-08 18:31:10 +03002178 // Detect false discard
2179 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2180 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002181
Olli Etuahoa6f22092015-05-08 18:31:10 +03002182 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002183 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002184 {
2185 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002186 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002187}
2188
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002189bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2190{
2191 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2192 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2193 UNREACHABLE();
2194 return false;
2195}
2196
Olli Etuaho57961272016-09-14 13:57:46 +03002197bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002198{
2199 TInfoSinkBase &out = getInfoSink();
2200
Olli Etuaho3d932d82016-04-12 11:10:30 +03002201 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002202
2203 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002204 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002205 {
2206 out << "FLATTEN ";
2207 }
2208
Olli Etuaho57961272016-09-14 13:57:46 +03002209 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002210
2211 return false;
2212}
2213
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002214bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002215{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002216 TInfoSinkBase &out = getInfoSink();
2217
Olli Etuaho923ecef2017-10-11 12:01:38 +03002218 ASSERT(node->getStatementList());
2219 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002220 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002221 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002222 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002223 outputTriplet(out, visit, "switch (", ") ", "");
2224 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002225 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002226}
2227
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002228bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002229{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002230 TInfoSinkBase &out = getInfoSink();
2231
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002232 if (node->hasCondition())
2233 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002234 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002235 return true;
2236 }
2237 else
2238 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002239 out << "default:\n";
2240 return false;
2241 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002242}
2243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2245{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002246 TInfoSinkBase &out = getInfoSink();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002247 writeConstantUnion(out, node->getType(), node->getConstantValue());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248}
2249
2250bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2251{
Nicolas Capens655fe362014-04-11 13:12:34 -04002252 mNestedLoopDepth++;
2253
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002254 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002255 mInsideDiscontinuousLoop =
2256 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002257
Jamie Madill8c46ab12015-12-07 16:39:19 -05002258 TInfoSinkBase &out = getInfoSink();
2259
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002260 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002261 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002262 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002263 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002264 mInsideDiscontinuousLoop = wasDiscontinuous;
2265 mNestedLoopDepth--;
2266
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002267 return false;
2268 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002269 }
2270
Corentin Wallez1239ee92015-03-19 14:38:02 -07002271 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002272 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002274 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002275
Jamie Madill8c46ab12015-12-07 16:39:19 -05002276 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002277 }
2278 else
2279 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002280 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002281
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 if (node->getInit())
2283 {
2284 node->getInit()->traverse(this);
2285 }
2286
2287 out << "; ";
2288
alokp@chromium.org52813552010-11-16 18:36:09 +00002289 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002291 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292 }
2293
2294 out << "; ";
2295
alokp@chromium.org52813552010-11-16 18:36:09 +00002296 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002298 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 }
2300
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002301 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002302
Jamie Madill8c46ab12015-12-07 16:39:19 -05002303 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304 }
2305
2306 if (node->getBody())
2307 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002308 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002309 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002311 else
2312 {
2313 // TODO(oetuaho): Check if the semicolon inside is necessary.
2314 // It's there as a result of conservative refactoring of the output.
2315 out << "{;}\n";
2316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317
Jamie Madill8c46ab12015-12-07 16:39:19 -05002318 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319
alokp@chromium.org52813552010-11-16 18:36:09 +00002320 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002322 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002323 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002324
alokp@chromium.org52813552010-11-16 18:36:09 +00002325 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002327 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328 }
2329
daniel@transgaming.com73536982012-03-21 20:45:49 +00002330 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002332 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002333 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002334
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335 return false;
2336}
2337
2338bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2339{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002340 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002342 TInfoSinkBase &out = getInfoSink();
2343
2344 switch (node->getFlowOp())
2345 {
2346 case EOpKill:
2347 out << "discard";
2348 break;
2349 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002350 if (mNestedLoopDepth > 1)
2351 {
2352 mUsesNestedBreak = true;
2353 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002354
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002355 if (mExcessiveLoopIndex)
2356 {
2357 out << "{Break";
2358 mExcessiveLoopIndex->traverse(this);
2359 out << " = true; break;}\n";
2360 }
2361 else
2362 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002363 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002364 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002365 break;
2366 case EOpContinue:
2367 out << "continue";
2368 break;
2369 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002370 if (node->getExpression())
2371 {
2372 out << "return ";
2373 }
2374 else
2375 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002376 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002377 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002378 break;
2379 default:
2380 UNREACHABLE();
2381 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382 }
2383
2384 return true;
2385}
2386
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002387// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002388// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2389// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002390bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002391{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002392 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002393
2394 // Parse loops of the form:
2395 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002396 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002397 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002398 int initial = 0;
2399 int limit = 0;
2400 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401
2402 // Parse index name and intial value
2403 if (node->getInit())
2404 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002405 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002406
2407 if (init)
2408 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002409 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002410 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002411
2412 if (variable && variable->getQualifier() == EvqTemporary)
2413 {
2414 TIntermBinary *assign = variable->getAsBinaryNode();
2415
2416 if (assign->getOp() == EOpInitialize)
2417 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002418 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002419 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2420
2421 if (symbol && constant)
2422 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002423 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002424 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002425 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002426 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002427 }
2428 }
2429 }
2430 }
2431 }
2432 }
2433
2434 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002435 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002436 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002437 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002438
Olli Etuahob6af22b2017-12-15 14:05:44 +02002439 if (test && test->getLeft()->getAsSymbolNode()->uniqueId() == index->uniqueId())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002440 {
2441 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2442
2443 if (constant)
2444 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002445 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002446 {
2447 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002448 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002449 }
2450 }
2451 }
2452 }
2453
2454 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002455 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002456 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002457 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002458 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002459
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002460 if (binaryTerminal)
2461 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002462 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002463 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2464
2465 if (constant)
2466 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002467 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002468 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002469 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002470
2471 switch (op)
2472 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002473 case EOpAddAssign:
2474 increment = value;
2475 break;
2476 case EOpSubAssign:
2477 increment = -value;
2478 break;
2479 default:
2480 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002481 }
2482 }
2483 }
2484 }
2485 else if (unaryTerminal)
2486 {
2487 TOperator op = unaryTerminal->getOp();
2488
2489 switch (op)
2490 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002491 case EOpPostIncrement:
2492 increment = 1;
2493 break;
2494 case EOpPostDecrement:
2495 increment = -1;
2496 break;
2497 case EOpPreIncrement:
2498 increment = 1;
2499 break;
2500 case EOpPreDecrement:
2501 increment = -1;
2502 break;
2503 default:
2504 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002505 }
2506 }
2507 }
2508
Yunchao He4f285442017-04-21 12:15:49 +08002509 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002510 {
2511 if (comparator == EOpLessThanEqual)
2512 {
2513 comparator = EOpLessThan;
2514 limit += 1;
2515 }
2516
2517 if (comparator == EOpLessThan)
2518 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002519 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002520
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002521 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002522 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002523 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002524 }
2525
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002526 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002527 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002528
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002529 out << "{int ";
2530 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002531 out << ";\n"
2532 "bool Break";
2533 index->traverse(this);
2534 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002535
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002536 bool firstLoopFragment = true;
2537
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002538 while (iterations > 0)
2539 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002540 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002541
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002542 if (!firstLoopFragment)
2543 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002544 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002545 index->traverse(this);
2546 out << ") {\n";
2547 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002548
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002549 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002550 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002551 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002552 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002553
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002554 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002555 const char *unroll =
2556 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002557
Corentin Wallez1239ee92015-03-19 14:38:02 -07002558 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002559 index->traverse(this);
2560 out << " = ";
2561 out << initial;
2562
2563 out << "; ";
2564 index->traverse(this);
2565 out << " < ";
2566 out << clampedLimit;
2567
2568 out << "; ";
2569 index->traverse(this);
2570 out << " += ";
2571 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002572 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002573
Jamie Madill8c46ab12015-12-07 16:39:19 -05002574 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002575 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002576
2577 if (node->getBody())
2578 {
2579 node->getBody()->traverse(this);
2580 }
2581
Jamie Madill8c46ab12015-12-07 16:39:19 -05002582 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002583 out << ";}\n";
2584
2585 if (!firstLoopFragment)
2586 {
2587 out << "}\n";
2588 }
2589
2590 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002591
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002592 initial += MAX_LOOP_ITERATIONS * increment;
2593 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002594 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002595
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002596 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002597
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002598 mExcessiveLoopIndex = restoreIndex;
2599
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600 return true;
2601 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002602 else
2603 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604 }
2605
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002606 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002607}
2608
Jamie Madill8c46ab12015-12-07 16:39:19 -05002609void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2610 Visit visit,
2611 const char *preString,
2612 const char *inString,
2613 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002615 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 {
2617 out << preString;
2618 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002619 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002620 {
2621 out << inString;
2622 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002623 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002624 {
2625 out << postString;
2626 }
2627}
2628
Jamie Madill8c46ab12015-12-07 16:39:19 -05002629void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002630{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002631 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002632 {
Jamie Madill32aab012015-01-27 14:12:26 -05002633 out << "\n";
2634 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002635
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002636 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002637 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002638 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002639 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002640
Jamie Madill32aab012015-01-27 14:12:26 -05002641 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002642 }
2643}
2644
Olli Etuahod4bd9632018-03-08 16:32:44 +02002645void OutputHLSL::writeParameter(const TVariable *param, TInfoSinkBase &out)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002646{
Olli Etuahod4bd9632018-03-08 16:32:44 +02002647 const TType &type = param->getType();
2648 TQualifier qualifier = type.getQualifier();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002649
Olli Etuahod4bd9632018-03-08 16:32:44 +02002650 TString nameStr = DecorateVariableIfNeeded(*param);
2651 ASSERT(nameStr != ""); // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002652
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002653 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002654 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002655 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2656 {
2657 // Samplers are passed as indices to the sampler array.
2658 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002659 out << "const uint " << nameStr << ArrayString(type);
2660 return;
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002661 }
2662 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2663 {
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002664 out << QualifierString(qualifier) << " " << TextureString(type.getBasicType())
2665 << " texture_" << nameStr << ArrayString(type) << ", " << QualifierString(qualifier)
2666 << " " << SamplerString(type.getBasicType()) << " sampler_" << nameStr
2667 << ArrayString(type);
2668 return;
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002669 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002670 }
2671
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002672 out << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2673 << ArrayString(type);
Olli Etuaho96963162016-03-21 11:54:33 +02002674
2675 // If the structure parameter contains samplers, they need to be passed into the function as
2676 // separate parameters. HLSL doesn't natively support samplers in structs.
2677 if (type.isStructureContainingSamplers())
2678 {
2679 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002680 TVector<const TVariable *> samplerSymbols;
Olli Etuahofbb1c792018-01-19 16:26:59 +02002681 std::string namePrefix = "angle";
2682 namePrefix += nameStr.c_str();
2683 type.createSamplerSymbols(ImmutableString(namePrefix), "", &samplerSymbols, nullptr,
2684 mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002685 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02002686 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002687 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002688 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2689 {
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002690 out << ", const uint " << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002691 }
2692 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2693 {
Olli Etuaho96963162016-03-21 11:54:33 +02002694 ASSERT(IsSampler(samplerType.getBasicType()));
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002695 out << ", " << QualifierString(qualifier) << " "
2696 << TextureString(samplerType.getBasicType()) << " texture_" << sampler->name()
2697 << ArrayString(samplerType) << ", " << QualifierString(qualifier) << " "
2698 << SamplerString(samplerType.getBasicType()) << " sampler_" << sampler->name()
2699 << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002700 }
2701 else
2702 {
Olli Etuaho96963162016-03-21 11:54:33 +02002703 ASSERT(IsSampler(samplerType.getBasicType()));
Olli Etuaho2f7c04a2018-01-25 14:50:37 +02002704 out << ", " << QualifierString(qualifier) << " " << TypeString(samplerType) << " "
2705 << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002706 }
2707 }
2708 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709}
2710
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002711TString OutputHLSL::zeroInitializer(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712{
2713 TString string;
2714
Jamie Madill94bf7f22013-07-08 13:31:15 -04002715 size_t size = type.getObjectSize();
2716 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002717 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002718 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719
Jamie Madill94bf7f22013-07-08 13:31:15 -04002720 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 {
2722 string += ", ";
2723 }
2724 }
2725
daniel@transgaming.comead23042010-04-29 03:35:36 +00002726 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002727}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002728
Olli Etuahobd3cd502017-11-03 15:48:52 +02002729void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002730{
Olli Etuahobd3cd502017-11-03 15:48:52 +02002731 // Array constructors should have been already pruned from the code.
2732 ASSERT(!node->getType().isArray());
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002733
2734 if (visit == PreVisit)
2735 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002736 TString constructorName;
2737 if (node->getBasicType() == EbtStruct)
2738 {
2739 constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct());
2740 }
2741 else
2742 {
2743 constructorName =
2744 mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence());
2745 }
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002746 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002747 }
2748 else if (visit == InVisit)
2749 {
2750 out << ", ";
2751 }
2752 else if (visit == PostVisit)
2753 {
2754 out << ")";
2755 }
2756}
2757
Jamie Madill8c46ab12015-12-07 16:39:19 -05002758const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2759 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002760 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002761{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002762 ASSERT(!type.isArray());
2763
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002764 const TConstantUnion *constUnionIterated = constUnion;
2765
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002766 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002767 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002768 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002769 out << mStructureHLSL->addStructConstructor(*structure) << "(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002770
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002771 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002772
Jamie Madill98493dd2013-07-08 14:39:03 -04002773 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002774 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002775 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002776 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002777
Jamie Madill98493dd2013-07-08 14:39:03 -04002778 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002779 {
2780 out << ", ";
2781 }
2782 }
2783
2784 out << ")";
2785 }
2786 else
2787 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002788 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002789 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002790
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002791 if (writeType)
2792 {
Jamie Madill033dae62014-06-18 12:56:28 -04002793 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002794 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002795 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002796 if (writeType)
2797 {
2798 out << ")";
2799 }
2800 }
2801
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002802 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002803}
2804
Olli Etuahod68924e2017-01-02 17:34:40 +00002805void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002806{
Olli Etuahod68924e2017-01-02 17:34:40 +00002807 if (visit == PreVisit)
2808 {
2809 const char *opStr = GetOperatorString(op);
2810 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2811 out << "(";
2812 }
2813 else
2814 {
2815 outputTriplet(out, visit, nullptr, ", ", ")");
2816 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002817}
2818
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002819bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2820 TIntermSymbol *symbolNode,
2821 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002822{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002823 ASSERT(symbolNode->variable().symbolType() != SymbolType::Empty);
2824 const TIntermSymbol *symbolInInitializer = FindSymbolNode(expression, symbolNode->getName());
Jamie Madill37997142015-01-28 10:06:34 -05002825
Olli Etuaho4728bdc2017-12-20 17:51:08 +02002826 if (symbolInInitializer)
Jamie Madill37997142015-01-28 10:06:34 -05002827 {
2828 // Type already printed
2829 out << "t" + str(mUniqueIndex) + " = ";
2830 expression->traverse(this);
2831 out << ", ";
2832 symbolNode->traverse(this);
2833 out << " = t" + str(mUniqueIndex);
2834
2835 mUniqueIndex++;
2836 return true;
2837 }
2838
2839 return false;
2840}
2841
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002842bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2843 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002844 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002845{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002846 if (initializer->hasConstantValue())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002847 {
2848 symbolNode->traverse(this);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002849 out << ArrayString(symbolNode->getType());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002850 out << " = {";
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002851 writeConstantUnionArray(out, initializer->getConstantValue(),
2852 initializer->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002853 out << "}";
2854 return true;
2855 }
2856 return false;
2857}
2858
Jamie Madill55e79e02015-02-09 15:35:00 -05002859TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2860{
2861 const TFieldList &fields = structure.fields();
2862
2863 for (const auto &eqFunction : mStructEqualityFunctions)
2864 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002865 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002866 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002867 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002868 }
2869 }
2870
2871 const TString &structNameString = StructNameString(structure);
2872
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002873 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002874 function->structure = &structure;
2875 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002876
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002877 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002878
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002879 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2880 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002881 << "{\n"
2882 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002883
2884 for (size_t i = 0; i < fields.size(); i++)
2885 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002886 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002887 const TType *fieldType = field->type();
2888
2889 const TString &fieldNameA = "a." + Decorate(field->name());
2890 const TString &fieldNameB = "b." + Decorate(field->name());
2891
2892 if (i > 0)
2893 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002894 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002895 }
2896
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002897 fnOut << "(";
2898 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2899 fnOut << fieldNameA;
2900 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2901 fnOut << fieldNameB;
2902 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2903 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002904 }
2905
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002906 fnOut << ";\n"
2907 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002908
2909 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002910
2911 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002912 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002913
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002914 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002915}
2916
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002917TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002918{
2919 for (const auto &eqFunction : mArrayEqualityFunctions)
2920 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002921 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002922 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002923 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002924 }
2925 }
2926
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002927 TType elementType(type);
2928 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002929
Olli Etuaho12690762015-03-31 12:55:28 +03002930 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002931 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002932
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002933 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002934
2935 TInfoSinkBase fnOut;
2936
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002937 const TString &typeName = TypeString(type);
2938 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2939 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002940 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002941 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002942 << type.getOutermostArraySize()
2943 << "; ++i)\n"
2944 " {\n"
2945 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002946
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002947 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002948 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002949 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002950 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002951 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002952
2953 fnOut << ") { return false; }\n"
2954 " }\n"
2955 " return true;\n"
2956 "}\n";
2957
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002958 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002959
2960 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002961 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002962
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002963 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002964}
2965
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002966TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002967{
2968 for (const auto &assignFunction : mArrayAssignmentFunctions)
2969 {
2970 if (assignFunction.type == type)
2971 {
2972 return assignFunction.functionName;
2973 }
2974 }
2975
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002976 TType elementType(type);
2977 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03002978
2979 ArrayHelperFunction function;
2980 function.type = type;
2981
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002982 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03002983
2984 TInfoSinkBase fnOut;
2985
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002986 const TString &typeName = TypeString(type);
2987 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
2988 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002989 << "{\n"
2990 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002991 << type.getOutermostArraySize()
2992 << "; ++i)\n"
2993 " {\n"
2994 " ";
2995
2996 outputAssign(PreVisit, elementType, fnOut);
2997 fnOut << "a[i]";
2998 outputAssign(InVisit, elementType, fnOut);
2999 fnOut << "b[i]";
3000 outputAssign(PostVisit, elementType, fnOut);
3001
3002 fnOut << ";\n"
3003 " }\n"
3004 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03003005
3006 function.functionDefinition = fnOut.c_str();
3007
3008 mArrayAssignmentFunctions.push_back(function);
3009
3010 return function.functionName;
3011}
3012
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003013TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003014{
3015 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3016 {
3017 if (constructIntoFunction.type == type)
3018 {
3019 return constructIntoFunction.functionName;
3020 }
3021 }
3022
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003023 TType elementType(type);
3024 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003025
3026 ArrayHelperFunction function;
3027 function.type = type;
3028
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003029 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003030
3031 TInfoSinkBase fnOut;
3032
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003033 const TString &typeName = TypeString(type);
3034 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3035 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003036 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003037 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003038 }
3039 fnOut << ")\n"
3040 "{\n";
3041
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003042 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003043 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003044 fnOut << " ";
3045 outputAssign(PreVisit, elementType, fnOut);
3046 fnOut << "a[" << i << "]";
3047 outputAssign(InVisit, elementType, fnOut);
3048 fnOut << "b" << i;
3049 outputAssign(PostVisit, elementType, fnOut);
3050 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003051 }
3052 fnOut << "}\n";
3053
3054 function.functionDefinition = fnOut.c_str();
3055
3056 mArrayConstructIntoFunctions.push_back(function);
3057
3058 return function.functionName;
3059}
3060
Jamie Madill2e295e22015-04-29 10:41:33 -04003061void OutputHLSL::ensureStructDefined(const TType &type)
3062{
Olli Etuahobd3cd502017-11-03 15:48:52 +02003063 const TStructure *structure = type.getStruct();
Jamie Madill2e295e22015-04-29 10:41:33 -04003064 if (structure)
3065 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02003066 ASSERT(type.getBasicType() == EbtStruct);
3067 mStructureHLSL->ensureStructDefined(*structure);
Jamie Madill2e295e22015-04-29 10:41:33 -04003068 }
3069}
3070
Jamie Madill45bcc782016-11-07 13:58:48 -05003071} // namespace sh