blob: 04ce8eb935e7ef7a8b2dc5658b0c40be7f3eb56b [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"
Olli Etuaho4728bdc2017-12-20 17:51:08 +020018#include "compiler/translator/FindSymbolNode.h"
Xinghua Cao711b7a12017-10-09 13:38:12 +080019#include "compiler/translator/ImageFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050020#include "compiler/translator/InfoSink.h"
21#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020022#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050023#include "compiler/translator/StructureHLSL.h"
Olli Etuaho5858f7e2016-04-08 13:08:46 +030024#include "compiler/translator/TextureFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050026#include "compiler/translator/UniformHLSL.h"
27#include "compiler/translator/UtilsHLSL.h"
28#include "compiler/translator/blocklayout.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 {
352 TInterfaceBlock *interfaceBlock =
353 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 Etuaho8b5e8fd2017-12-15 14:59:15 +0200374 const TString &instanceName = mappedStruct.blockDeclarator->variable().name();
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200375 unsigned int instanceStringArrayIndex = GL_INVALID_INDEX;
376 if (isInstanceArray)
377 instanceStringArrayIndex = instanceArrayIndex;
Olli Etuaho12a18ad2017-12-01 16:59:47 +0200378 TString instanceString = mUniformHLSL->UniformBlockInstanceString(
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200379 instanceName, instanceStringArrayIndex);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200380 originalName += instanceString;
381 mappedName += instanceString;
382 originalName += ".";
383 mappedName += "_";
384 }
385
386 TString fieldName = Decorate(mappedStruct.field->name());
387 originalName += fieldName;
388 mappedName += fieldName;
389
390 TType *structType = mappedStruct.field->type();
391 mappedStructs +=
Olli Etuahobed35d72017-12-20 16:36:26 +0200392 "static " + Decorate(structType->getStruct()->name()) + " " + mappedName;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200393
394 if (structType->isArray())
395 {
396 mappedStructs += ArrayString(*mappedStruct.field->type());
397 }
398
399 mappedStructs += " =\n";
400 mappedStructs += structInitializerString(0, *structType, originalName);
401 mappedStructs += ";\n";
402 }
403 }
404 return mappedStructs;
405}
406
407void OutputHLSL::header(TInfoSinkBase &out,
408 const std::vector<MappedStruct> &std140Structs,
409 const BuiltInFunctionEmulator *builtInFunctionEmulator) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000411 TString varyings;
412 TString attributes;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200413 TString mappedStructs = generateStructMapping(std140Structs);
Jamie Madill570e04d2013-06-21 09:15:33 -0400414
Olli Etuahob8cb9392017-12-20 14:23:19 +0200415 for (const auto &varying : mReferencedVaryings)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000416 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200417 const TType &type = varying.second->getType();
418 const TString &name = varying.second->name();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000419
420 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500421 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
422 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000423 }
424
Olli Etuaho93b059d2017-12-20 12:46:58 +0200425 for (const auto &attribute : mReferencedAttributes)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000426 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200427 const TType &type = attribute.second->getType();
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200428 const TString &name = attribute.second->name();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000429
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500430 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
431 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000432 }
433
Jamie Madill8daaba12014-06-13 10:04:33 -0400434 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400435
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300436 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms, mSymbolTable);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800437 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400438
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200439 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500440 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200441 out << "\n// Equality functions\n\n";
442 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500443 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200444 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200445 }
446 }
Olli Etuaho12690762015-03-31 12:55:28 +0300447 if (!mArrayAssignmentFunctions.empty())
448 {
449 out << "\n// Assignment functions\n\n";
450 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
451 {
452 out << assignmentFunction.functionDefinition << "\n";
453 }
454 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300455 if (!mArrayConstructIntoFunctions.empty())
456 {
457 out << "\n// Array constructor functions\n\n";
458 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
459 {
460 out << constructIntoFunction.functionDefinition << "\n";
461 }
462 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200463
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500464 if (mUsesDiscardRewriting)
465 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400466 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500467 }
468
Nicolas Capens655fe362014-04-11 13:12:34 -0400469 if (mUsesNestedBreak)
470 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400471 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400472 }
473
Arun Patole44efa0b2015-03-04 17:11:05 +0530474 if (mRequiresIEEEStrictCompiling)
475 {
476 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
477 }
478
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400479 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
480 "#define LOOP [loop]\n"
481 "#define FLATTEN [flatten]\n"
482 "#else\n"
483 "#define LOOP\n"
484 "#define FLATTEN\n"
485 "#endif\n";
486
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200487 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300489 const bool usingMRTExtension =
490 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000491
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000492 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500493 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400494 out << "\n";
495
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200496 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000497 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200498 for (const auto &outputVariable : mReferencedOutputVariables)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000499 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200500 const TString &variableName = outputVariable.second->name();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200501 const TType &variableType = outputVariable.second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400502
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500503 out << "static " + TypeString(variableType) + " out_" + variableName +
504 ArrayString(variableType) + " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000505 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000506 }
Jamie Madill46131a32013-06-20 11:55:50 -0400507 else
508 {
509 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
510
511 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500512 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400513 for (unsigned int i = 0; i < numColorValues; i++)
514 {
515 out << " float4(0, 0, 0, 0)";
516 if (i + 1 != numColorValues)
517 {
518 out << ",";
519 }
520 out << "\n";
521 }
522
523 out << "};\n";
524 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000525
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400526 if (mUsesFragDepth)
527 {
528 out << "static float gl_Depth = 0.0;\n";
529 }
530
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000531 if (mUsesFragCoord)
532 {
533 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
534 }
535
536 if (mUsesPointCoord)
537 {
538 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
539 }
540
541 if (mUsesFrontFacing)
542 {
543 out << "static bool gl_FrontFacing = false;\n";
544 }
545
546 out << "\n";
547
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000548 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000549 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000550 out << "struct gl_DepthRangeParameters\n"
551 "{\n"
552 " float near;\n"
553 " float far;\n"
554 " float diff;\n"
555 "};\n"
556 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000557 }
558
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200559 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000560 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000561 out << "cbuffer DriverConstants : register(b1)\n"
562 "{\n";
563
564 if (mUsesDepthRange)
565 {
566 out << " float3 dx_DepthRange : packoffset(c0);\n";
567 }
568
569 if (mUsesFragCoord)
570 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000571 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000572 }
573
574 if (mUsesFragCoord || mUsesFrontFacing)
575 {
576 out << " float3 dx_DepthFront : packoffset(c2);\n";
577 }
578
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800579 if (mUsesFragCoord)
580 {
581 // dx_ViewScale is only used in the fragment shader to correct
582 // the value for glFragCoord if necessary
583 out << " float2 dx_ViewScale : packoffset(c3);\n";
584 }
585
Martin Radev72b4e1e2017-08-31 15:42:56 +0300586 if (mHasMultiviewExtensionEnabled)
587 {
588 // We have to add a value which we can use to keep track of which multi-view code
589 // path is to be selected in the GS.
590 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
591 }
592
Olli Etuaho618bebc2016-01-15 16:40:00 +0200593 if (mOutputType == SH_HLSL_4_1_OUTPUT)
594 {
595 mUniformHLSL->samplerMetadataUniforms(out, "c4");
596 }
597
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000598 out << "};\n";
599 }
600 else
601 {
602 if (mUsesDepthRange)
603 {
604 out << "uniform float3 dx_DepthRange : register(c0);";
605 }
606
607 if (mUsesFragCoord)
608 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000609 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000610 }
611
612 if (mUsesFragCoord || mUsesFrontFacing)
613 {
614 out << "uniform float3 dx_DepthFront : register(c2);\n";
615 }
616 }
617
618 out << "\n";
619
620 if (mUsesDepthRange)
621 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500622 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
623 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000624 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000625 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000626
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200627 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000628 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200629 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000630 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200631 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400632 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000633 }
634
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000635 if (usingMRTExtension && mNumRenderTargets > 1)
636 {
637 out << "#define GL_USES_MRT\n";
638 }
639
640 if (mUsesFragColor)
641 {
642 out << "#define GL_USES_FRAG_COLOR\n";
643 }
644
645 if (mUsesFragData)
646 {
647 out << "#define GL_USES_FRAG_DATA\n";
648 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649 }
Xinghua Caob1239382016-12-13 15:07:05 +0800650 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000652 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500653 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000654 out << "\n"
655 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400656
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000657 if (mUsesPointSize)
658 {
659 out << "static float gl_PointSize = float(1);\n";
660 }
661
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000662 if (mUsesInstanceID)
663 {
664 out << "static int gl_InstanceID;";
665 }
666
Corentin Wallezb076add2016-01-11 16:45:46 -0500667 if (mUsesVertexID)
668 {
669 out << "static int gl_VertexID;";
670 }
671
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000672 out << "\n"
673 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500674 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000675 out << "\n";
676
677 if (mUsesDepthRange)
678 {
679 out << "struct gl_DepthRangeParameters\n"
680 "{\n"
681 " float near;\n"
682 " float far;\n"
683 " float diff;\n"
684 "};\n"
685 "\n";
686 }
687
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200688 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000689 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800690 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500691 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800692
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000693 if (mUsesDepthRange)
694 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800695 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000696 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800697
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800698 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
699 // shaders. However, we declare it for all shaders (including Feature Level 10+).
700 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
701 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800702 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800703 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800704 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800705
Martin Radev72b4e1e2017-08-31 15:42:56 +0300706 if (mHasMultiviewExtensionEnabled)
707 {
708 // We have to add a value which we can use to keep track of which multi-view code
709 // path is to be selected in the GS.
710 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
711 }
712
Olli Etuaho618bebc2016-01-15 16:40:00 +0200713 if (mOutputType == SH_HLSL_4_1_OUTPUT)
714 {
715 mUniformHLSL->samplerMetadataUniforms(out, "c4");
716 }
717
Austin Kinross4fd18b12014-12-22 12:32:05 -0800718 out << "};\n"
719 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000720 }
721 else
722 {
723 if (mUsesDepthRange)
724 {
725 out << "uniform float3 dx_DepthRange : register(c0);\n";
726 }
727
Cooper Partine6664f02015-01-09 16:22:24 -0800728 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
729 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000730 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000731 }
732
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000733 if (mUsesDepthRange)
734 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500735 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
736 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000737 "\n";
738 }
739
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200740 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000741 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200742 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000743 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200744 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400745 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000746 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400747 }
Xinghua Caob1239382016-12-13 15:07:05 +0800748 else // Compute shader
749 {
750 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800751
752 out << "cbuffer DriverConstants : register(b1)\n"
753 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800754 if (mUsesNumWorkGroups)
755 {
Xinghua Caob1239382016-12-13 15:07:05 +0800756 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800757 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800758 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
759 mUniformHLSL->samplerMetadataUniforms(out, "c1");
760 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800761
762 // Follow built-in variables would be initialized in
763 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
764 // are used in compute shader.
765 if (mUsesWorkGroupID)
766 {
767 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
768 }
769
770 if (mUsesLocalInvocationID)
771 {
772 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
773 }
774
775 if (mUsesGlobalInvocationID)
776 {
777 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
778 }
779
780 if (mUsesLocalInvocationIndex)
781 {
782 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
783 }
784 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000785
Geoff Lang1fe74c72016-08-25 13:23:01 -0400786 bool getDimensionsIgnoresBaseLevel =
787 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
788 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800789 mImageFunctionHLSL->imageFunctionHeader(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000791 if (mUsesFragCoord)
792 {
793 out << "#define GL_USES_FRAG_COORD\n";
794 }
795
796 if (mUsesPointCoord)
797 {
798 out << "#define GL_USES_POINT_COORD\n";
799 }
800
801 if (mUsesFrontFacing)
802 {
803 out << "#define GL_USES_FRONT_FACING\n";
804 }
805
806 if (mUsesPointSize)
807 {
808 out << "#define GL_USES_POINT_SIZE\n";
809 }
810
Martin Radev41ac68e2017-06-06 12:16:58 +0300811 if (mHasMultiviewExtensionEnabled)
812 {
813 out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n";
814 }
815
816 if (mUsesViewID)
817 {
818 out << "#define GL_USES_VIEW_ID\n";
819 }
820
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400821 if (mUsesFragDepth)
822 {
823 out << "#define GL_USES_FRAG_DEPTH\n";
824 }
825
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000826 if (mUsesDepthRange)
827 {
828 out << "#define GL_USES_DEPTH_RANGE\n";
829 }
830
Xinghua Caob1239382016-12-13 15:07:05 +0800831 if (mUsesNumWorkGroups)
832 {
833 out << "#define GL_USES_NUM_WORK_GROUPS\n";
834 }
835
836 if (mUsesWorkGroupID)
837 {
838 out << "#define GL_USES_WORK_GROUP_ID\n";
839 }
840
841 if (mUsesLocalInvocationID)
842 {
843 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
844 }
845
846 if (mUsesGlobalInvocationID)
847 {
848 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
849 }
850
851 if (mUsesLocalInvocationIndex)
852 {
853 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
854 }
855
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000856 if (mUsesXor)
857 {
858 out << "bool xor(bool p, bool q)\n"
859 "{\n"
860 " return (p || q) && !(p && q);\n"
861 "}\n"
862 "\n";
863 }
864
Olli Etuahodfa75e82017-01-23 09:43:06 -0800865 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866}
867
868void OutputHLSL::visitSymbol(TIntermSymbol *node)
869{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200870 const TVariable &variable = node->variable();
871
872 // Empty symbols can only appear in declarations and function arguments, and in either of those
873 // cases the symbol nodes are not visited.
874 ASSERT(variable.symbolType() != SymbolType::Empty);
875
Jamie Madill32aab012015-01-27 14:12:26 -0500876 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877
Jamie Madill570e04d2013-06-21 09:15:33 -0400878 // Handle accessing std140 structs by value
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200879 if (IsInStd140InterfaceBlock(node) && node->getBasicType() == EbtStruct)
Jamie Madill570e04d2013-06-21 09:15:33 -0400880 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200881 out << "map";
Jamie Madill570e04d2013-06-21 09:15:33 -0400882 }
883
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200884 const TString &name = variable.name();
885 const TSymbolUniqueId &uniqueId = variable.uniqueId();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200886
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000887 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000888 {
889 mUsesDepthRange = true;
890 out << name;
891 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892 else
893 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200894 const TType &variableType = variable.getType();
895 TQualifier qualifier = variable.getType().getQualifier();
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000896
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200897 ensureStructDefined(variableType);
Olli Etuahobd3cd502017-11-03 15:48:52 +0200898
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000899 if (qualifier == EvqUniform)
900 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200901 const TInterfaceBlock *interfaceBlock = variableType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400902
903 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000904 {
Olli Etuahoc71862a2017-12-21 12:58:29 +0200905 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
906 {
907 const TVariable *instanceVariable = nullptr;
908 if (variableType.isInterfaceBlock())
909 {
910 instanceVariable = &variable;
911 }
912 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
913 new TReferencedBlock(interfaceBlock, instanceVariable);
914 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000915 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000916 else
917 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200918 mReferencedUniforms[uniqueId.get()] = &variable;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000919 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400920
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200921 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000922 }
Jamie Madill19571812013-08-12 15:26:34 -0700923 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000924 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200925 mReferencedAttributes[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400926 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000927 }
Jamie Madill033dae62014-06-18 12:56:28 -0400928 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000929 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200930 mReferencedVaryings[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400931 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300932 if (name == "ViewID_OVR")
933 {
934 mUsesViewID = true;
935 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000936 }
Jamie Madill19571812013-08-12 15:26:34 -0700937 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400938 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200939 mReferencedOutputVariables[uniqueId.get()] = &variable;
Jamie Madill46131a32013-06-20 11:55:50 -0400940 out << "out_" << name;
941 }
942 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000943 {
944 out << "gl_Color[0]";
945 mUsesFragColor = true;
946 }
947 else if (qualifier == EvqFragData)
948 {
949 out << "gl_Color";
950 mUsesFragData = true;
951 }
952 else if (qualifier == EvqFragCoord)
953 {
954 mUsesFragCoord = true;
955 out << name;
956 }
957 else if (qualifier == EvqPointCoord)
958 {
959 mUsesPointCoord = true;
960 out << name;
961 }
962 else if (qualifier == EvqFrontFacing)
963 {
964 mUsesFrontFacing = true;
965 out << name;
966 }
967 else if (qualifier == EvqPointSize)
968 {
969 mUsesPointSize = true;
970 out << name;
971 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000972 else if (qualifier == EvqInstanceID)
973 {
974 mUsesInstanceID = true;
975 out << name;
976 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500977 else if (qualifier == EvqVertexID)
978 {
979 mUsesVertexID = true;
980 out << name;
981 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300982 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400983 {
984 mUsesFragDepth = true;
985 out << "gl_Depth";
986 }
Xinghua Caob1239382016-12-13 15:07:05 +0800987 else if (qualifier == EvqNumWorkGroups)
988 {
989 mUsesNumWorkGroups = true;
990 out << name;
991 }
992 else if (qualifier == EvqWorkGroupID)
993 {
994 mUsesWorkGroupID = true;
995 out << name;
996 }
997 else if (qualifier == EvqLocalInvocationID)
998 {
999 mUsesLocalInvocationID = true;
1000 out << name;
1001 }
1002 else if (qualifier == EvqGlobalInvocationID)
1003 {
1004 mUsesGlobalInvocationID = true;
1005 out << name;
1006 }
1007 else if (qualifier == EvqLocalInvocationIndex)
1008 {
1009 mUsesLocalInvocationIndex = true;
1010 out << name;
1011 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001012 else
1013 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001014 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001015 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001016 }
1017}
1018
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001019void OutputHLSL::visitRaw(TIntermRaw *node)
1020{
Jamie Madill32aab012015-01-27 14:12:26 -05001021 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001022}
1023
Olli Etuaho7fb49552015-03-18 17:27:44 +02001024void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1025{
1026 if (type.isScalar() && !type.isArray())
1027 {
1028 if (op == EOpEqual)
1029 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001030 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001031 }
1032 else
1033 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001034 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001035 }
1036 }
1037 else
1038 {
1039 if (visit == PreVisit && op == EOpNotEqual)
1040 {
1041 out << "!";
1042 }
1043
1044 if (type.isArray())
1045 {
1046 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001047 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001048 }
1049 else if (type.getBasicType() == EbtStruct)
1050 {
1051 const TStructure &structure = *type.getStruct();
1052 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001053 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001054 }
1055 else
1056 {
1057 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001058 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001059 }
1060 }
1061}
1062
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001063void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1064{
1065 if (type.isArray())
1066 {
1067 const TString &functionName = addArrayAssignmentFunction(type);
1068 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1069 }
1070 else
1071 {
1072 outputTriplet(out, visit, "(", " = ", ")");
1073 }
1074}
1075
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001076bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001077{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001078 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001079 {
1080 TIntermNode *ancestor = getAncestorNode(n);
1081 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1082 if (ancestorBinary == nullptr)
1083 {
1084 return false;
1085 }
1086 switch (ancestorBinary->getOp())
1087 {
1088 case EOpIndexDirectStruct:
1089 {
1090 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1091 const TIntermConstantUnion *index =
1092 ancestorBinary->getRight()->getAsConstantUnion();
1093 const TField *field = structure->fields()[index->getIConst(0)];
1094 if (IsSampler(field->type()->getBasicType()))
1095 {
1096 return true;
1097 }
1098 break;
1099 }
1100 case EOpIndexDirect:
1101 break;
1102 default:
1103 // Returning a sampler from indirect indexing is not supported.
1104 return false;
1105 }
1106 }
1107 return false;
1108}
1109
Olli Etuahob6fa0432016-09-28 16:28:05 +01001110bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1111{
1112 TInfoSinkBase &out = getInfoSink();
1113 if (visit == PostVisit)
1114 {
1115 out << ".";
1116 node->writeOffsetsAsXYZW(&out);
1117 }
1118 return true;
1119}
1120
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1122{
Jamie Madill32aab012015-01-27 14:12:26 -05001123 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001124
1125 switch (node->getOp())
1126 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001127 case EOpComma:
1128 outputTriplet(out, visit, "(", ", ", ")");
1129 break;
1130 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001131 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001132 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001133 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1134 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001135 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001136 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1137 out << functionName << "(";
1138 node->getLeft()->traverse(this);
1139 TIntermSequence *seq = rightAgg->getSequence();
1140 for (auto &arrayElement : *seq)
1141 {
1142 out << ", ";
1143 arrayElement->traverse(this);
1144 }
1145 out << ")";
1146 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001147 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001148 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1149 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001150 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001151 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001152 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001153 break;
1154 case EOpInitialize:
1155 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001156 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001157 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1158 ASSERT(symbolNode);
1159 TIntermTyped *expression = node->getRight();
1160
1161 // Global initializers must be constant at this point.
1162 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
1163 canWriteAsHLSLLiteral(expression));
1164
1165 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1166 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1167 // new variable is created before the assignment is evaluated), so we need to
1168 // convert
1169 // this to "float t = x, x = t;".
1170 if (writeSameSymbolInitializer(out, symbolNode, expression))
1171 {
1172 // Skip initializing the rest of the expression
1173 return false;
1174 }
1175 else if (writeConstantInitialization(out, symbolNode, expression))
1176 {
1177 return false;
1178 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001179 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001180 else if (visit == InVisit)
1181 {
1182 out << " = ";
1183 }
1184 break;
1185 case EOpAddAssign:
1186 outputTriplet(out, visit, "(", " += ", ")");
1187 break;
1188 case EOpSubAssign:
1189 outputTriplet(out, visit, "(", " -= ", ")");
1190 break;
1191 case EOpMulAssign:
1192 outputTriplet(out, visit, "(", " *= ", ")");
1193 break;
1194 case EOpVectorTimesScalarAssign:
1195 outputTriplet(out, visit, "(", " *= ", ")");
1196 break;
1197 case EOpMatrixTimesScalarAssign:
1198 outputTriplet(out, visit, "(", " *= ", ")");
1199 break;
1200 case EOpVectorTimesMatrixAssign:
1201 if (visit == PreVisit)
1202 {
1203 out << "(";
1204 }
1205 else if (visit == InVisit)
1206 {
1207 out << " = mul(";
1208 node->getLeft()->traverse(this);
1209 out << ", transpose(";
1210 }
1211 else
1212 {
1213 out << ")))";
1214 }
1215 break;
1216 case EOpMatrixTimesMatrixAssign:
1217 if (visit == PreVisit)
1218 {
1219 out << "(";
1220 }
1221 else if (visit == InVisit)
1222 {
1223 out << " = transpose(mul(transpose(";
1224 node->getLeft()->traverse(this);
1225 out << "), transpose(";
1226 }
1227 else
1228 {
1229 out << "))))";
1230 }
1231 break;
1232 case EOpDivAssign:
1233 outputTriplet(out, visit, "(", " /= ", ")");
1234 break;
1235 case EOpIModAssign:
1236 outputTriplet(out, visit, "(", " %= ", ")");
1237 break;
1238 case EOpBitShiftLeftAssign:
1239 outputTriplet(out, visit, "(", " <<= ", ")");
1240 break;
1241 case EOpBitShiftRightAssign:
1242 outputTriplet(out, visit, "(", " >>= ", ")");
1243 break;
1244 case EOpBitwiseAndAssign:
1245 outputTriplet(out, visit, "(", " &= ", ")");
1246 break;
1247 case EOpBitwiseXorAssign:
1248 outputTriplet(out, visit, "(", " ^= ", ")");
1249 break;
1250 case EOpBitwiseOrAssign:
1251 outputTriplet(out, visit, "(", " |= ", ")");
1252 break;
1253 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001254 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001255 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001256 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001257 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001258 if (visit == PreVisit)
1259 {
Olli Etuaho12a18ad2017-12-01 16:59:47 +02001260 TIntermSymbol *instanceArraySymbol = node->getLeft()->getAsSymbolNode();
Olli Etuahoc71862a2017-12-21 12:58:29 +02001261 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
1262 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
1263 {
1264 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
1265 new TReferencedBlock(interfaceBlock, &instanceArraySymbol->variable());
1266 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001267 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001268 out << mUniformHLSL->UniformBlockInstanceString(instanceArraySymbol->getName(),
1269 arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001270 return false;
1271 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001272 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001273 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001274 {
1275 // All parts of an expression that access a sampler in a struct need to use _ as
1276 // separator to access the sampler variable that has been moved out of the struct.
1277 outputTriplet(out, visit, "", "_", "");
1278 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001279 else
1280 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001281 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001282 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001283 }
1284 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001285 case EOpIndexIndirect:
1286 // We do not currently support indirect references to interface blocks
1287 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1288 outputTriplet(out, visit, "", "[", "]");
1289 break;
1290 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001291 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001292 const TStructure *structure = node->getLeft()->getType().getStruct();
1293 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1294 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001295
Olli Etuaho96963162016-03-21 11:54:33 +02001296 // In cases where indexing returns a sampler, we need to access the sampler variable
1297 // that has been moved out of the struct.
1298 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1299 if (visit == PreVisit && indexingReturnsSampler)
1300 {
1301 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1302 // This prefix is only output at the beginning of the indexing expression, which
1303 // may have multiple parts.
1304 out << "angle";
1305 }
1306 if (!indexingReturnsSampler)
1307 {
1308 // All parts of an expression that access a sampler in a struct need to use _ as
1309 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001310 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001311 }
1312 if (visit == InVisit)
1313 {
1314 if (indexingReturnsSampler)
1315 {
1316 out << "_" + field->name();
1317 }
1318 else
1319 {
1320 out << "." + DecorateField(field->name(), *structure);
1321 }
1322
1323 return false;
1324 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001325 }
1326 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001327 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001328 {
1329 bool structInStd140Block =
1330 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1331 if (visit == PreVisit && structInStd140Block)
1332 {
1333 out << "map";
1334 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001335 if (visit == InVisit)
1336 {
1337 const TInterfaceBlock *interfaceBlock =
1338 node->getLeft()->getType().getInterfaceBlock();
1339 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1340 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001341 if (structInStd140Block)
1342 {
1343 out << "_";
1344 }
1345 else
1346 {
1347 out << ".";
1348 }
1349 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001350
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001351 return false;
1352 }
1353 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001354 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001355 case EOpAdd:
1356 outputTriplet(out, visit, "(", " + ", ")");
1357 break;
1358 case EOpSub:
1359 outputTriplet(out, visit, "(", " - ", ")");
1360 break;
1361 case EOpMul:
1362 outputTriplet(out, visit, "(", " * ", ")");
1363 break;
1364 case EOpDiv:
1365 outputTriplet(out, visit, "(", " / ", ")");
1366 break;
1367 case EOpIMod:
1368 outputTriplet(out, visit, "(", " % ", ")");
1369 break;
1370 case EOpBitShiftLeft:
1371 outputTriplet(out, visit, "(", " << ", ")");
1372 break;
1373 case EOpBitShiftRight:
1374 outputTriplet(out, visit, "(", " >> ", ")");
1375 break;
1376 case EOpBitwiseAnd:
1377 outputTriplet(out, visit, "(", " & ", ")");
1378 break;
1379 case EOpBitwiseXor:
1380 outputTriplet(out, visit, "(", " ^ ", ")");
1381 break;
1382 case EOpBitwiseOr:
1383 outputTriplet(out, visit, "(", " | ", ")");
1384 break;
1385 case EOpEqual:
1386 case EOpNotEqual:
1387 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1388 break;
1389 case EOpLessThan:
1390 outputTriplet(out, visit, "(", " < ", ")");
1391 break;
1392 case EOpGreaterThan:
1393 outputTriplet(out, visit, "(", " > ", ")");
1394 break;
1395 case EOpLessThanEqual:
1396 outputTriplet(out, visit, "(", " <= ", ")");
1397 break;
1398 case EOpGreaterThanEqual:
1399 outputTriplet(out, visit, "(", " >= ", ")");
1400 break;
1401 case EOpVectorTimesScalar:
1402 outputTriplet(out, visit, "(", " * ", ")");
1403 break;
1404 case EOpMatrixTimesScalar:
1405 outputTriplet(out, visit, "(", " * ", ")");
1406 break;
1407 case EOpVectorTimesMatrix:
1408 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1409 break;
1410 case EOpMatrixTimesVector:
1411 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1412 break;
1413 case EOpMatrixTimesMatrix:
1414 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1415 break;
1416 case EOpLogicalOr:
1417 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1418 // been unfolded.
1419 ASSERT(!node->getRight()->hasSideEffects());
1420 outputTriplet(out, visit, "(", " || ", ")");
1421 return true;
1422 case EOpLogicalXor:
1423 mUsesXor = true;
1424 outputTriplet(out, visit, "xor(", ", ", ")");
1425 break;
1426 case EOpLogicalAnd:
1427 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1428 // been unfolded.
1429 ASSERT(!node->getRight()->hasSideEffects());
1430 outputTriplet(out, visit, "(", " && ", ")");
1431 return true;
1432 default:
1433 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001434 }
1435
1436 return true;
1437}
1438
1439bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1440{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001441 TInfoSinkBase &out = getInfoSink();
1442
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001443 switch (node->getOp())
1444 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001445 case EOpNegative:
1446 outputTriplet(out, visit, "(-", "", ")");
1447 break;
1448 case EOpPositive:
1449 outputTriplet(out, visit, "(+", "", ")");
1450 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001451 case EOpLogicalNot:
1452 outputTriplet(out, visit, "(!", "", ")");
1453 break;
1454 case EOpBitwiseNot:
1455 outputTriplet(out, visit, "(~", "", ")");
1456 break;
1457 case EOpPostIncrement:
1458 outputTriplet(out, visit, "(", "", "++)");
1459 break;
1460 case EOpPostDecrement:
1461 outputTriplet(out, visit, "(", "", "--)");
1462 break;
1463 case EOpPreIncrement:
1464 outputTriplet(out, visit, "(++", "", ")");
1465 break;
1466 case EOpPreDecrement:
1467 outputTriplet(out, visit, "(--", "", ")");
1468 break;
1469 case EOpRadians:
1470 outputTriplet(out, visit, "radians(", "", ")");
1471 break;
1472 case EOpDegrees:
1473 outputTriplet(out, visit, "degrees(", "", ")");
1474 break;
1475 case EOpSin:
1476 outputTriplet(out, visit, "sin(", "", ")");
1477 break;
1478 case EOpCos:
1479 outputTriplet(out, visit, "cos(", "", ")");
1480 break;
1481 case EOpTan:
1482 outputTriplet(out, visit, "tan(", "", ")");
1483 break;
1484 case EOpAsin:
1485 outputTriplet(out, visit, "asin(", "", ")");
1486 break;
1487 case EOpAcos:
1488 outputTriplet(out, visit, "acos(", "", ")");
1489 break;
1490 case EOpAtan:
1491 outputTriplet(out, visit, "atan(", "", ")");
1492 break;
1493 case EOpSinh:
1494 outputTriplet(out, visit, "sinh(", "", ")");
1495 break;
1496 case EOpCosh:
1497 outputTriplet(out, visit, "cosh(", "", ")");
1498 break;
1499 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001500 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001501 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001502 case EOpAtanh:
1503 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001504 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001505 break;
1506 case EOpExp:
1507 outputTriplet(out, visit, "exp(", "", ")");
1508 break;
1509 case EOpLog:
1510 outputTriplet(out, visit, "log(", "", ")");
1511 break;
1512 case EOpExp2:
1513 outputTriplet(out, visit, "exp2(", "", ")");
1514 break;
1515 case EOpLog2:
1516 outputTriplet(out, visit, "log2(", "", ")");
1517 break;
1518 case EOpSqrt:
1519 outputTriplet(out, visit, "sqrt(", "", ")");
1520 break;
1521 case EOpInverseSqrt:
1522 outputTriplet(out, visit, "rsqrt(", "", ")");
1523 break;
1524 case EOpAbs:
1525 outputTriplet(out, visit, "abs(", "", ")");
1526 break;
1527 case EOpSign:
1528 outputTriplet(out, visit, "sign(", "", ")");
1529 break;
1530 case EOpFloor:
1531 outputTriplet(out, visit, "floor(", "", ")");
1532 break;
1533 case EOpTrunc:
1534 outputTriplet(out, visit, "trunc(", "", ")");
1535 break;
1536 case EOpRound:
1537 outputTriplet(out, visit, "round(", "", ")");
1538 break;
1539 case EOpRoundEven:
1540 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001541 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001542 break;
1543 case EOpCeil:
1544 outputTriplet(out, visit, "ceil(", "", ")");
1545 break;
1546 case EOpFract:
1547 outputTriplet(out, visit, "frac(", "", ")");
1548 break;
1549 case EOpIsNan:
1550 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001551 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001552 else
1553 outputTriplet(out, visit, "isnan(", "", ")");
1554 mRequiresIEEEStrictCompiling = true;
1555 break;
1556 case EOpIsInf:
1557 outputTriplet(out, visit, "isinf(", "", ")");
1558 break;
1559 case EOpFloatBitsToInt:
1560 outputTriplet(out, visit, "asint(", "", ")");
1561 break;
1562 case EOpFloatBitsToUint:
1563 outputTriplet(out, visit, "asuint(", "", ")");
1564 break;
1565 case EOpIntBitsToFloat:
1566 outputTriplet(out, visit, "asfloat(", "", ")");
1567 break;
1568 case EOpUintBitsToFloat:
1569 outputTriplet(out, visit, "asfloat(", "", ")");
1570 break;
1571 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001572 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001573 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001574 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001575 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001576 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001577 case EOpPackUnorm4x8:
1578 case EOpPackSnorm4x8:
1579 case EOpUnpackUnorm4x8:
1580 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001581 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001582 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001583 break;
1584 case EOpLength:
1585 outputTriplet(out, visit, "length(", "", ")");
1586 break;
1587 case EOpNormalize:
1588 outputTriplet(out, visit, "normalize(", "", ")");
1589 break;
1590 case EOpDFdx:
1591 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1592 {
1593 outputTriplet(out, visit, "(", "", ", 0.0)");
1594 }
1595 else
1596 {
1597 outputTriplet(out, visit, "ddx(", "", ")");
1598 }
1599 break;
1600 case EOpDFdy:
1601 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1602 {
1603 outputTriplet(out, visit, "(", "", ", 0.0)");
1604 }
1605 else
1606 {
1607 outputTriplet(out, visit, "ddy(", "", ")");
1608 }
1609 break;
1610 case EOpFwidth:
1611 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1612 {
1613 outputTriplet(out, visit, "(", "", ", 0.0)");
1614 }
1615 else
1616 {
1617 outputTriplet(out, visit, "fwidth(", "", ")");
1618 }
1619 break;
1620 case EOpTranspose:
1621 outputTriplet(out, visit, "transpose(", "", ")");
1622 break;
1623 case EOpDeterminant:
1624 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1625 break;
1626 case EOpInverse:
1627 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001628 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001629 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001630
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001631 case EOpAny:
1632 outputTriplet(out, visit, "any(", "", ")");
1633 break;
1634 case EOpAll:
1635 outputTriplet(out, visit, "all(", "", ")");
1636 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001637 case EOpLogicalNotComponentWise:
1638 outputTriplet(out, visit, "(!", "", ")");
1639 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001640 case EOpBitfieldReverse:
1641 outputTriplet(out, visit, "reversebits(", "", ")");
1642 break;
1643 case EOpBitCount:
1644 outputTriplet(out, visit, "countbits(", "", ")");
1645 break;
1646 case EOpFindLSB:
1647 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1648 // in GLSLTest and results are consistent with GL.
1649 outputTriplet(out, visit, "firstbitlow(", "", ")");
1650 break;
1651 case EOpFindMSB:
1652 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1653 // tested in GLSLTest and results are consistent with GL.
1654 outputTriplet(out, visit, "firstbithigh(", "", ")");
1655 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001656 default:
1657 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001658 }
1659
1660 return true;
1661}
1662
Olli Etuaho96963162016-03-21 11:54:33 +02001663TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1664{
1665 if (node->getAsSymbolNode())
1666 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001667 ASSERT(node->getAsSymbolNode()->variable().symbolType() != SymbolType::Empty);
1668 return node->getAsSymbolNode()->getName();
Olli Etuaho96963162016-03-21 11:54:33 +02001669 }
1670 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1671 switch (nodeBinary->getOp())
1672 {
1673 case EOpIndexDirect:
1674 {
1675 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1676
1677 TInfoSinkBase prefixSink;
1678 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1679 return TString(prefixSink.c_str());
1680 }
1681 case EOpIndexDirectStruct:
1682 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001683 const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001684 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1685 const TField *field = s->fields()[index];
1686
1687 TInfoSinkBase prefixSink;
1688 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1689 << field->name();
1690 return TString(prefixSink.c_str());
1691 }
1692 default:
1693 UNREACHABLE();
1694 return TString("");
1695 }
1696}
1697
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001698bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1699{
1700 TInfoSinkBase &out = getInfoSink();
1701
1702 if (mInsideFunction)
1703 {
1704 outputLineDirective(out, node->getLine().first_line);
1705 out << "{\n";
1706 }
1707
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001708 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001709 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001710 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001711
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001712 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001713
1714 // Don't output ; after case labels, they're terminated by :
1715 // This is needed especially since outputting a ; after a case statement would turn empty
1716 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001717 // Also the output code is clearer if we don't output ; after statements where it is not
1718 // needed:
1719 // * if statements
1720 // * switch statements
1721 // * blocks
1722 // * function definitions
1723 // * loops (do-while loops output the semicolon in VisitLoop)
1724 // * declarations that don't generate output.
1725 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1726 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1727 statement->getAsSwitchNode() == nullptr &&
1728 statement->getAsFunctionDefinition() == nullptr &&
1729 (statement->getAsDeclarationNode() == nullptr ||
1730 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1731 statement->getAsInvariantDeclarationNode() == nullptr)
1732 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001733 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001734 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001735 }
1736
1737 if (mInsideFunction)
1738 {
1739 outputLineDirective(out, node->getLine().last_line);
1740 out << "}\n";
1741 }
1742
1743 return false;
1744}
1745
Olli Etuaho336b1472016-10-05 16:37:55 +01001746bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1747{
1748 TInfoSinkBase &out = getInfoSink();
1749
1750 ASSERT(mCurrentFunctionMetadata == nullptr);
1751
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001752 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho336b1472016-10-05 16:37:55 +01001753 ASSERT(index != CallDAG::InvalidIndex);
1754 mCurrentFunctionMetadata = &mASTMetadataList[index];
1755
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001756 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001757
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001758 TIntermSequence *parameters = node->getFunctionPrototype()->getSequence();
Olli Etuaho336b1472016-10-05 16:37:55 +01001759
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001760 if (node->getFunction()->isMain())
Olli Etuaho336b1472016-10-05 16:37:55 +01001761 {
1762 out << "gl_main(";
1763 }
1764 else
1765 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001766 out << DecorateFunctionIfNeeded(node->getFunction()) << DisambiguateFunctionName(parameters)
1767 << (mOutputLod0Function ? "Lod0(" : "(");
Olli Etuaho336b1472016-10-05 16:37:55 +01001768 }
1769
1770 for (unsigned int i = 0; i < parameters->size(); i++)
1771 {
1772 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1773
1774 if (symbol)
1775 {
1776 ensureStructDefined(symbol->getType());
1777
1778 out << argumentString(symbol);
1779
1780 if (i < parameters->size() - 1)
1781 {
1782 out << ", ";
1783 }
1784 }
1785 else
1786 UNREACHABLE();
1787 }
1788
1789 out << ")\n";
1790
1791 mInsideFunction = true;
1792 // The function body node will output braces.
1793 node->getBody()->traverse(this);
1794 mInsideFunction = false;
1795
1796 mCurrentFunctionMetadata = nullptr;
1797
1798 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1799 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1800 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001801 ASSERT(!node->getFunction()->isMain());
Olli Etuaho336b1472016-10-05 16:37:55 +01001802 mOutputLod0Function = true;
1803 node->traverse(this);
1804 mOutputLod0Function = false;
1805 }
1806
1807 return false;
1808}
1809
Olli Etuaho13389b62016-10-16 11:48:18 +01001810bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1811{
Olli Etuaho13389b62016-10-16 11:48:18 +01001812 if (visit == PreVisit)
1813 {
1814 TIntermSequence *sequence = node->getSequence();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001815 TIntermTyped *declarator = (*sequence)[0]->getAsTyped();
Olli Etuaho13389b62016-10-16 11:48:18 +01001816 ASSERT(sequence->size() == 1);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001817 ASSERT(declarator);
Olli Etuaho13389b62016-10-16 11:48:18 +01001818
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001819 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001820 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001821 TInfoSinkBase &out = getInfoSink();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001822 ensureStructDefined(declarator->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001823
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001824 if (!declarator->getAsSymbolNode() ||
1825 declarator->getAsSymbolNode()->variable().symbolType() !=
1826 SymbolType::Empty) // Variable declaration
Olli Etuaho13389b62016-10-16 11:48:18 +01001827 {
1828 if (!mInsideFunction)
1829 {
1830 out << "static ";
1831 }
1832
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001833 out << TypeString(declarator->getType()) + " ";
Olli Etuaho13389b62016-10-16 11:48:18 +01001834
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001835 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho13389b62016-10-16 11:48:18 +01001836
1837 if (symbol)
1838 {
1839 symbol->traverse(this);
1840 out << ArrayString(symbol->getType());
1841 out << " = " + initializer(symbol->getType());
1842 }
1843 else
1844 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001845 declarator->traverse(this);
Olli Etuaho13389b62016-10-16 11:48:18 +01001846 }
1847 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001848 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001849 else if (IsVaryingOut(declarator->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001850 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001851 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho282847e2017-07-12 14:11:01 +03001852 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001853
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001854 const TVariable &variable = symbol->variable();
1855
1856 if (variable.symbolType() != SymbolType::Empty)
Olli Etuaho93b059d2017-12-20 12:46:58 +02001857 {
1858 // Vertex outputs which are declared but not written to should still be declared to
1859 // allow successful linking.
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001860 mReferencedVaryings[symbol->uniqueId().get()] = &variable;
Olli Etuaho93b059d2017-12-20 12:46:58 +02001861 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001862 }
1863 }
1864 return false;
1865}
1866
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001867bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1868{
1869 // Do not do any translation
1870 return false;
1871}
1872
Olli Etuaho16c745a2017-01-16 17:02:27 +00001873bool OutputHLSL::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
1874{
1875 TInfoSinkBase &out = getInfoSink();
1876
1877 ASSERT(visit == PreVisit);
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001878 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001879 // Skip the prototype if it is not implemented (and thus not used)
1880 if (index == CallDAG::InvalidIndex)
1881 {
1882 return false;
1883 }
1884
1885 TIntermSequence *arguments = node->getSequence();
1886
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001887 TString name = DecorateFunctionIfNeeded(node->getFunction());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001888 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1889 << (mOutputLod0Function ? "Lod0(" : "(");
1890
1891 for (unsigned int i = 0; i < arguments->size(); i++)
1892 {
1893 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1894 ASSERT(symbol != nullptr);
1895
1896 out << argumentString(symbol);
1897
1898 if (i < arguments->size() - 1)
1899 {
1900 out << ", ";
1901 }
1902 }
1903
1904 out << ");\n";
1905
1906 // Also prototype the Lod0 variant if needed
1907 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1908 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1909 {
1910 mOutputLod0Function = true;
1911 node->traverse(this);
1912 mOutputLod0Function = false;
1913 }
1914
1915 return false;
1916}
1917
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1919{
Jamie Madill32aab012015-01-27 14:12:26 -05001920 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922 switch (node->getOp())
1923 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001924 case EOpCallBuiltInFunction:
1925 case EOpCallFunctionInAST:
1926 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001928 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001929
Corentin Wallez1239ee92015-03-19 14:38:02 -07001930 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001931 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001932 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001933 if (node->isArray())
1934 {
1935 UNIMPLEMENTED();
1936 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001937 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001938 ASSERT(index != CallDAG::InvalidIndex);
1939 lod0 &= mASTMetadataList[index].mNeedsLod0;
1940
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001941 out << DecorateFunctionIfNeeded(node->getFunction());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001942 out << DisambiguateFunctionName(node->getSequence());
1943 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001945 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001946 {
1947 // This path is used for internal functions that don't have their definitions in the
1948 // AST, such as precision emulation functions.
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001949 out << DecorateFunctionIfNeeded(node->getFunction()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001950 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001951 else if (node->getFunction()->isImageFunction())
Xinghua Cao711b7a12017-10-09 13:38:12 +08001952 {
Olli Etuahobed35d72017-12-20 16:36:26 +02001953 const TString &name = node->getFunction()->name();
Xinghua Cao711b7a12017-10-09 13:38:12 +08001954 TType type = (*arguments)[0]->getAsTyped()->getType();
1955 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001956 name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
Xinghua Cao711b7a12017-10-09 13:38:12 +08001957 type.getMemoryQualifier().readonly);
1958 out << imageFunctionName << "(";
1959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960 else
1961 {
Olli Etuahobed35d72017-12-20 16:36:26 +02001962 const TString &name = node->getFunction()->name();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001963 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001964 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1965 if (arguments->size() > 1)
1966 {
1967 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1968 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001969 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001970 name, samplerType, coords, arguments->size(), lod0, mShaderType);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001971 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001972 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001973
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001974 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001975 {
Olli Etuaho96963162016-03-21 11:54:33 +02001976 TIntermTyped *typedArg = (*arg)->getAsTyped();
1977 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001978 {
1979 out << "texture_";
1980 (*arg)->traverse(this);
1981 out << ", sampler_";
1982 }
1983
1984 (*arg)->traverse(this);
1985
Olli Etuaho96963162016-03-21 11:54:33 +02001986 if (typedArg->getType().isStructureContainingSamplers())
1987 {
1988 const TType &argType = typedArg->getType();
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001989 TVector<const TVariable *> samplerSymbols;
Olli Etuaho96963162016-03-21 11:54:33 +02001990 TString structName = samplerNamePrefixFromStruct(typedArg);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001991 argType.createSamplerSymbols("angle_" + structName, "", &samplerSymbols,
1992 nullptr, mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001993 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02001994 {
1995 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1996 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001997 out << ", texture_" << sampler->name();
1998 out << ", sampler_" << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02001999 }
2000 else
2001 {
2002 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
2003 // of D3D9, it's the sampler variable.
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002004 out << ", " + sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02002005 }
2006 }
2007 }
2008
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002009 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002010 {
2011 out << ", ";
2012 }
2013 }
2014
2015 out << ")";
2016
2017 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03002019 case EOpConstruct:
Olli Etuahobd3cd502017-11-03 15:48:52 +02002020 outputConstructor(out, visit, node);
Olli Etuaho8fab3202017-05-08 18:22:22 +03002021 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002022 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002023 outputTriplet(out, visit, "(", " == ", ")");
2024 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002025 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002026 outputTriplet(out, visit, "(", " != ", ")");
2027 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002028 case EOpLessThanComponentWise:
2029 outputTriplet(out, visit, "(", " < ", ")");
2030 break;
2031 case EOpGreaterThanComponentWise:
2032 outputTriplet(out, visit, "(", " > ", ")");
2033 break;
2034 case EOpLessThanEqualComponentWise:
2035 outputTriplet(out, visit, "(", " <= ", ")");
2036 break;
2037 case EOpGreaterThanEqualComponentWise:
2038 outputTriplet(out, visit, "(", " >= ", ")");
2039 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002040 case EOpMod:
2041 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002042 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002043 break;
2044 case EOpModf:
2045 outputTriplet(out, visit, "modf(", ", ", ")");
2046 break;
2047 case EOpPow:
2048 outputTriplet(out, visit, "pow(", ", ", ")");
2049 break;
2050 case EOpAtan:
2051 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2052 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002053 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002054 break;
2055 case EOpMin:
2056 outputTriplet(out, visit, "min(", ", ", ")");
2057 break;
2058 case EOpMax:
2059 outputTriplet(out, visit, "max(", ", ", ")");
2060 break;
2061 case EOpClamp:
2062 outputTriplet(out, visit, "clamp(", ", ", ")");
2063 break;
2064 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302065 {
2066 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2067 if (lastParamNode->getType().getBasicType() == EbtBool)
2068 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002069 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2070 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302071 // so use emulated version.
2072 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002073 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302074 }
2075 else
2076 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002077 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302078 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002079 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302080 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002081 case EOpStep:
2082 outputTriplet(out, visit, "step(", ", ", ")");
2083 break;
2084 case EOpSmoothStep:
2085 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2086 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002087 case EOpFrexp:
2088 case EOpLdexp:
2089 ASSERT(node->getUseEmulatedFunction());
2090 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2091 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002092 case EOpDistance:
2093 outputTriplet(out, visit, "distance(", ", ", ")");
2094 break;
2095 case EOpDot:
2096 outputTriplet(out, visit, "dot(", ", ", ")");
2097 break;
2098 case EOpCross:
2099 outputTriplet(out, visit, "cross(", ", ", ")");
2100 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002101 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002102 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002103 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002104 break;
2105 case EOpReflect:
2106 outputTriplet(out, visit, "reflect(", ", ", ")");
2107 break;
2108 case EOpRefract:
2109 outputTriplet(out, visit, "refract(", ", ", ")");
2110 break;
2111 case EOpOuterProduct:
2112 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002113 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002114 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002115 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002116 outputTriplet(out, visit, "(", " * ", ")");
2117 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002118 case EOpBitfieldExtract:
2119 case EOpBitfieldInsert:
2120 case EOpUaddCarry:
2121 case EOpUsubBorrow:
2122 case EOpUmulExtended:
2123 case EOpImulExtended:
2124 ASSERT(node->getUseEmulatedFunction());
2125 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2126 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002127 default:
2128 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129 }
2130
2131 return true;
2132}
2133
Olli Etuaho57961272016-09-14 13:57:46 +03002134void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002136 out << "if (";
2137
2138 node->getCondition()->traverse(this);
2139
2140 out << ")\n";
2141
Jamie Madill8c46ab12015-12-07 16:39:19 -05002142 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002143
2144 bool discard = false;
2145
2146 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002148 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002149 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002150
Olli Etuahoa6f22092015-05-08 18:31:10 +03002151 // Detect true discard
2152 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2153 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002154 else
2155 {
2156 // TODO(oetuaho): Check if the semicolon inside is necessary.
2157 // It's there as a result of conservative refactoring of the output.
2158 out << "{;}\n";
2159 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002160
Jamie Madill8c46ab12015-12-07 16:39:19 -05002161 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002162
Olli Etuahoa6f22092015-05-08 18:31:10 +03002163 if (node->getFalseBlock())
2164 {
2165 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002166
Jamie Madill8c46ab12015-12-07 16:39:19 -05002167 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168
Olli Etuaho32db19b2016-10-04 14:43:16 +01002169 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002170 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002171
Jamie Madill8c46ab12015-12-07 16:39:19 -05002172 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002173
Olli Etuahoa6f22092015-05-08 18:31:10 +03002174 // Detect false discard
2175 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2176 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002177
Olli Etuahoa6f22092015-05-08 18:31:10 +03002178 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002179 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002180 {
2181 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002182 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002183}
2184
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002185bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2186{
2187 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2188 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2189 UNREACHABLE();
2190 return false;
2191}
2192
Olli Etuaho57961272016-09-14 13:57:46 +03002193bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002194{
2195 TInfoSinkBase &out = getInfoSink();
2196
Olli Etuaho3d932d82016-04-12 11:10:30 +03002197 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002198
2199 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002200 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002201 {
2202 out << "FLATTEN ";
2203 }
2204
Olli Etuaho57961272016-09-14 13:57:46 +03002205 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206
2207 return false;
2208}
2209
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002210bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002211{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002212 TInfoSinkBase &out = getInfoSink();
2213
Olli Etuaho923ecef2017-10-11 12:01:38 +03002214 ASSERT(node->getStatementList());
2215 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002216 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002217 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002218 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002219 outputTriplet(out, visit, "switch (", ") ", "");
2220 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002221 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002222}
2223
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002224bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002225{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002226 TInfoSinkBase &out = getInfoSink();
2227
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002228 if (node->hasCondition())
2229 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002230 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002231 return true;
2232 }
2233 else
2234 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002235 out << "default:\n";
2236 return false;
2237 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002238}
2239
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2241{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002242 TInfoSinkBase &out = getInfoSink();
2243 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002244}
2245
2246bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2247{
Nicolas Capens655fe362014-04-11 13:12:34 -04002248 mNestedLoopDepth++;
2249
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002250 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002251 mInsideDiscontinuousLoop =
2252 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002253
Jamie Madill8c46ab12015-12-07 16:39:19 -05002254 TInfoSinkBase &out = getInfoSink();
2255
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002256 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002257 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002258 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002259 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002260 mInsideDiscontinuousLoop = wasDiscontinuous;
2261 mNestedLoopDepth--;
2262
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002263 return false;
2264 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002265 }
2266
Corentin Wallez1239ee92015-03-19 14:38:02 -07002267 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002268 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002270 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002271
Jamie Madill8c46ab12015-12-07 16:39:19 -05002272 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002273 }
2274 else
2275 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002276 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002277
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 if (node->getInit())
2279 {
2280 node->getInit()->traverse(this);
2281 }
2282
2283 out << "; ";
2284
alokp@chromium.org52813552010-11-16 18:36:09 +00002285 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002287 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002288 }
2289
2290 out << "; ";
2291
alokp@chromium.org52813552010-11-16 18:36:09 +00002292 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002294 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295 }
2296
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002297 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002298
Jamie Madill8c46ab12015-12-07 16:39:19 -05002299 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 }
2301
2302 if (node->getBody())
2303 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002304 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002305 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002307 else
2308 {
2309 // TODO(oetuaho): Check if the semicolon inside is necessary.
2310 // It's there as a result of conservative refactoring of the output.
2311 out << "{;}\n";
2312 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313
Jamie Madill8c46ab12015-12-07 16:39:19 -05002314 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315
alokp@chromium.org52813552010-11-16 18:36:09 +00002316 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002318 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002319 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320
alokp@chromium.org52813552010-11-16 18:36:09 +00002321 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002322
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002323 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002324 }
2325
daniel@transgaming.com73536982012-03-21 20:45:49 +00002326 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002327
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002328 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002329 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002330
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331 return false;
2332}
2333
2334bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2335{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002336 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002338 TInfoSinkBase &out = getInfoSink();
2339
2340 switch (node->getFlowOp())
2341 {
2342 case EOpKill:
2343 out << "discard";
2344 break;
2345 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002346 if (mNestedLoopDepth > 1)
2347 {
2348 mUsesNestedBreak = true;
2349 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002350
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002351 if (mExcessiveLoopIndex)
2352 {
2353 out << "{Break";
2354 mExcessiveLoopIndex->traverse(this);
2355 out << " = true; break;}\n";
2356 }
2357 else
2358 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002359 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002360 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002361 break;
2362 case EOpContinue:
2363 out << "continue";
2364 break;
2365 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002366 if (node->getExpression())
2367 {
2368 out << "return ";
2369 }
2370 else
2371 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002372 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002373 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002374 break;
2375 default:
2376 UNREACHABLE();
2377 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378 }
2379
2380 return true;
2381}
2382
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002383// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002384// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2385// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002386bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002387{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002388 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002389
2390 // Parse loops of the form:
2391 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002392 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002393 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002394 int initial = 0;
2395 int limit = 0;
2396 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002397
2398 // Parse index name and intial value
2399 if (node->getInit())
2400 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002401 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002402
2403 if (init)
2404 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002405 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002406 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002407
2408 if (variable && variable->getQualifier() == EvqTemporary)
2409 {
2410 TIntermBinary *assign = variable->getAsBinaryNode();
2411
2412 if (assign->getOp() == EOpInitialize)
2413 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002414 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002415 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2416
2417 if (symbol && constant)
2418 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002419 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002420 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002421 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002422 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002423 }
2424 }
2425 }
2426 }
2427 }
2428 }
2429
2430 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002431 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002432 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002433 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002434
Olli Etuahob6af22b2017-12-15 14:05:44 +02002435 if (test && test->getLeft()->getAsSymbolNode()->uniqueId() == index->uniqueId())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002436 {
2437 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2438
2439 if (constant)
2440 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002441 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002442 {
2443 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002444 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002445 }
2446 }
2447 }
2448 }
2449
2450 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002451 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002452 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002453 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002454 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002455
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002456 if (binaryTerminal)
2457 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002458 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002459 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2460
2461 if (constant)
2462 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002463 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002464 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002465 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002466
2467 switch (op)
2468 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002469 case EOpAddAssign:
2470 increment = value;
2471 break;
2472 case EOpSubAssign:
2473 increment = -value;
2474 break;
2475 default:
2476 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002477 }
2478 }
2479 }
2480 }
2481 else if (unaryTerminal)
2482 {
2483 TOperator op = unaryTerminal->getOp();
2484
2485 switch (op)
2486 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002487 case EOpPostIncrement:
2488 increment = 1;
2489 break;
2490 case EOpPostDecrement:
2491 increment = -1;
2492 break;
2493 case EOpPreIncrement:
2494 increment = 1;
2495 break;
2496 case EOpPreDecrement:
2497 increment = -1;
2498 break;
2499 default:
2500 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002501 }
2502 }
2503 }
2504
Yunchao He4f285442017-04-21 12:15:49 +08002505 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002506 {
2507 if (comparator == EOpLessThanEqual)
2508 {
2509 comparator = EOpLessThan;
2510 limit += 1;
2511 }
2512
2513 if (comparator == EOpLessThan)
2514 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002515 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002516
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002517 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002518 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002519 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002520 }
2521
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002522 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002523 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002524
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002525 out << "{int ";
2526 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002527 out << ";\n"
2528 "bool Break";
2529 index->traverse(this);
2530 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002531
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002532 bool firstLoopFragment = true;
2533
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002534 while (iterations > 0)
2535 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002536 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002537
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002538 if (!firstLoopFragment)
2539 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002540 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002541 index->traverse(this);
2542 out << ") {\n";
2543 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002544
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002545 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002546 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002547 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002548 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002549
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002550 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002551 const char *unroll =
2552 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002553
Corentin Wallez1239ee92015-03-19 14:38:02 -07002554 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002555 index->traverse(this);
2556 out << " = ";
2557 out << initial;
2558
2559 out << "; ";
2560 index->traverse(this);
2561 out << " < ";
2562 out << clampedLimit;
2563
2564 out << "; ";
2565 index->traverse(this);
2566 out << " += ";
2567 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002568 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002569
Jamie Madill8c46ab12015-12-07 16:39:19 -05002570 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002571 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572
2573 if (node->getBody())
2574 {
2575 node->getBody()->traverse(this);
2576 }
2577
Jamie Madill8c46ab12015-12-07 16:39:19 -05002578 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002579 out << ";}\n";
2580
2581 if (!firstLoopFragment)
2582 {
2583 out << "}\n";
2584 }
2585
2586 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002587
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002588 initial += MAX_LOOP_ITERATIONS * increment;
2589 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002590 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002591
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002592 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002593
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002594 mExcessiveLoopIndex = restoreIndex;
2595
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002596 return true;
2597 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002598 else
2599 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600 }
2601
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002602 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002603}
2604
Jamie Madill8c46ab12015-12-07 16:39:19 -05002605void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2606 Visit visit,
2607 const char *preString,
2608 const char *inString,
2609 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002610{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002611 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002612 {
2613 out << preString;
2614 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002615 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616 {
2617 out << inString;
2618 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002619 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002620 {
2621 out << postString;
2622 }
2623}
2624
Jamie Madill8c46ab12015-12-07 16:39:19 -05002625void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002626{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002627 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002628 {
Jamie Madill32aab012015-01-27 14:12:26 -05002629 out << "\n";
2630 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002631
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002632 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002633 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002634 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002635 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002636
Jamie Madill32aab012015-01-27 14:12:26 -05002637 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002638 }
2639}
2640
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002641TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2642{
2643 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002644 const TType &type = symbol->getType();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002645 const TVariable &variable = symbol->variable();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002646 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002647
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002648 if (variable.symbolType() ==
2649 SymbolType::Empty) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002650 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002651 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002652 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002653 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002654 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002655 nameStr = DecorateVariableIfNeeded(variable);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002656 }
2657
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002658 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002659 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002660 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2661 {
2662 // Samplers are passed as indices to the sampler array.
2663 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2664 return "const uint " + nameStr + ArrayString(type);
2665 }
2666 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2667 {
2668 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2669 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2670 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2671 ArrayString(type);
2672 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002673 }
2674
Olli Etuaho96963162016-03-21 11:54:33 +02002675 TStringStream argString;
2676 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2677 << ArrayString(type);
2678
2679 // If the structure parameter contains samplers, they need to be passed into the function as
2680 // separate parameters. HLSL doesn't natively support samplers in structs.
2681 if (type.isStructureContainingSamplers())
2682 {
2683 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002684 TVector<const TVariable *> samplerSymbols;
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002685 type.createSamplerSymbols("angle" + nameStr, "", &samplerSymbols, nullptr, mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002686 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02002687 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002688 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002689 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2690 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002691 argString << ", const uint " << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002692 }
2693 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2694 {
Olli Etuaho96963162016-03-21 11:54:33 +02002695 ASSERT(IsSampler(samplerType.getBasicType()));
2696 argString << ", " << QualifierString(qualifier) << " "
2697 << TextureString(samplerType.getBasicType()) << " texture_"
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002698 << sampler->name() << ArrayString(samplerType) << ", "
Olli Etuaho28839f02017-08-15 11:38:16 +03002699 << QualifierString(qualifier) << " "
Olli Etuaho96963162016-03-21 11:54:33 +02002700 << SamplerString(samplerType.getBasicType()) << " sampler_"
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002701 << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002702 }
2703 else
2704 {
Olli Etuaho96963162016-03-21 11:54:33 +02002705 ASSERT(IsSampler(samplerType.getBasicType()));
2706 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002707 << " " << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002708 }
2709 }
2710 }
2711
2712 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002713}
2714
2715TString OutputHLSL::initializer(const TType &type)
2716{
2717 TString string;
2718
Jamie Madill94bf7f22013-07-08 13:31:15 -04002719 size_t size = type.getObjectSize();
2720 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002722 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002723
Jamie Madill94bf7f22013-07-08 13:31:15 -04002724 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725 {
2726 string += ", ";
2727 }
2728 }
2729
daniel@transgaming.comead23042010-04-29 03:35:36 +00002730 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002731}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002732
Olli Etuahobd3cd502017-11-03 15:48:52 +02002733void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002734{
Olli Etuahobd3cd502017-11-03 15:48:52 +02002735 // Array constructors should have been already pruned from the code.
2736 ASSERT(!node->getType().isArray());
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002737
2738 if (visit == PreVisit)
2739 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002740 TString constructorName;
2741 if (node->getBasicType() == EbtStruct)
2742 {
2743 constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct());
2744 }
2745 else
2746 {
2747 constructorName =
2748 mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence());
2749 }
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002750 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002751 }
2752 else if (visit == InVisit)
2753 {
2754 out << ", ";
2755 }
2756 else if (visit == PostVisit)
2757 {
2758 out << ")";
2759 }
2760}
2761
Jamie Madill8c46ab12015-12-07 16:39:19 -05002762const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2763 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002764 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002765{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002766 const TConstantUnion *constUnionIterated = constUnion;
2767
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002768 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002769 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002770 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002771 out << mStructureHLSL->addStructConstructor(*structure) << "(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002772
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002773 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002774
Jamie Madill98493dd2013-07-08 14:39:03 -04002775 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002776 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002777 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002778 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002779
Jamie Madill98493dd2013-07-08 14:39:03 -04002780 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002781 {
2782 out << ", ";
2783 }
2784 }
2785
2786 out << ")";
2787 }
2788 else
2789 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002790 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002791 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002792
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002793 if (writeType)
2794 {
Jamie Madill033dae62014-06-18 12:56:28 -04002795 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002796 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002797 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002798 if (writeType)
2799 {
2800 out << ")";
2801 }
2802 }
2803
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002804 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002805}
2806
Olli Etuahod68924e2017-01-02 17:34:40 +00002807void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002808{
Olli Etuahod68924e2017-01-02 17:34:40 +00002809 if (visit == PreVisit)
2810 {
2811 const char *opStr = GetOperatorString(op);
2812 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2813 out << "(";
2814 }
2815 else
2816 {
2817 outputTriplet(out, visit, nullptr, ", ", ")");
2818 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002819}
2820
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002821bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2822 TIntermSymbol *symbolNode,
2823 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002824{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002825 ASSERT(symbolNode->variable().symbolType() != SymbolType::Empty);
2826 const TIntermSymbol *symbolInInitializer = FindSymbolNode(expression, symbolNode->getName());
Jamie Madill37997142015-01-28 10:06:34 -05002827
Olli Etuaho4728bdc2017-12-20 17:51:08 +02002828 if (symbolInInitializer)
Jamie Madill37997142015-01-28 10:06:34 -05002829 {
2830 // Type already printed
2831 out << "t" + str(mUniqueIndex) + " = ";
2832 expression->traverse(this);
2833 out << ", ";
2834 symbolNode->traverse(this);
2835 out << " = t" + str(mUniqueIndex);
2836
2837 mUniqueIndex++;
2838 return true;
2839 }
2840
2841 return false;
2842}
2843
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002844bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2845{
2846 // We support writing constant unions and constructors that only take constant unions as
2847 // parameters as HLSL literals.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002848 return !expression->getType().isArrayOfArrays() &&
2849 (expression->getAsConstantUnion() ||
2850 expression->isConstructorWithOnlyConstantUnionParameters());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002851}
2852
2853bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2854 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002855 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002856{
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002857 if (canWriteAsHLSLLiteral(initializer))
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002858 {
2859 symbolNode->traverse(this);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002860 ASSERT(!symbolNode->getType().isArrayOfArrays());
2861 if (symbolNode->getType().isArray())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002862 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002863 out << "[" << symbolNode->getType().getOutermostArraySize() << "]";
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002864 }
2865 out << " = {";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002866 if (initializer->getAsConstantUnion())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002867 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002868 TIntermConstantUnion *nodeConst = initializer->getAsConstantUnion();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002869 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002870 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002871 }
2872 else
2873 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002874 TIntermAggregate *constructor = initializer->getAsAggregate();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002875 ASSERT(constructor != nullptr);
2876 for (TIntermNode *&node : *constructor->getSequence())
2877 {
2878 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2879 ASSERT(nodeConst);
2880 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002881 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002882 if (node != constructor->getSequence()->back())
2883 {
2884 out << ", ";
2885 }
2886 }
2887 }
2888 out << "}";
2889 return true;
2890 }
2891 return false;
2892}
2893
Jamie Madill55e79e02015-02-09 15:35:00 -05002894TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2895{
2896 const TFieldList &fields = structure.fields();
2897
2898 for (const auto &eqFunction : mStructEqualityFunctions)
2899 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002900 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002901 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002902 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002903 }
2904 }
2905
2906 const TString &structNameString = StructNameString(structure);
2907
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002908 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002909 function->structure = &structure;
2910 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002911
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002912 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002913
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002914 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2915 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002916 << "{\n"
2917 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002918
2919 for (size_t i = 0; i < fields.size(); i++)
2920 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002921 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002922 const TType *fieldType = field->type();
2923
2924 const TString &fieldNameA = "a." + Decorate(field->name());
2925 const TString &fieldNameB = "b." + Decorate(field->name());
2926
2927 if (i > 0)
2928 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002929 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002930 }
2931
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002932 fnOut << "(";
2933 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2934 fnOut << fieldNameA;
2935 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2936 fnOut << fieldNameB;
2937 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2938 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002939 }
2940
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002941 fnOut << ";\n"
2942 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002943
2944 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002945
2946 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002947 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002948
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002949 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002950}
2951
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002952TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002953{
2954 for (const auto &eqFunction : mArrayEqualityFunctions)
2955 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002956 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002957 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002958 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002959 }
2960 }
2961
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002962 TType elementType(type);
2963 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002964
Olli Etuaho12690762015-03-31 12:55:28 +03002965 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002966 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002967
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002968 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002969
2970 TInfoSinkBase fnOut;
2971
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002972 const TString &typeName = TypeString(type);
2973 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2974 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002975 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002976 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002977 << type.getOutermostArraySize()
2978 << "; ++i)\n"
2979 " {\n"
2980 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002981
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002982 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002983 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002984 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002985 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002986 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002987
2988 fnOut << ") { return false; }\n"
2989 " }\n"
2990 " return true;\n"
2991 "}\n";
2992
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002993 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002994
2995 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002996 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002997
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002998 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002999}
3000
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003001TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03003002{
3003 for (const auto &assignFunction : mArrayAssignmentFunctions)
3004 {
3005 if (assignFunction.type == type)
3006 {
3007 return assignFunction.functionName;
3008 }
3009 }
3010
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003011 TType elementType(type);
3012 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03003013
3014 ArrayHelperFunction function;
3015 function.type = type;
3016
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003017 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03003018
3019 TInfoSinkBase fnOut;
3020
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003021 const TString &typeName = TypeString(type);
3022 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
3023 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003024 << "{\n"
3025 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003026 << type.getOutermostArraySize()
3027 << "; ++i)\n"
3028 " {\n"
3029 " ";
3030
3031 outputAssign(PreVisit, elementType, fnOut);
3032 fnOut << "a[i]";
3033 outputAssign(InVisit, elementType, fnOut);
3034 fnOut << "b[i]";
3035 outputAssign(PostVisit, elementType, fnOut);
3036
3037 fnOut << ";\n"
3038 " }\n"
3039 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03003040
3041 function.functionDefinition = fnOut.c_str();
3042
3043 mArrayAssignmentFunctions.push_back(function);
3044
3045 return function.functionName;
3046}
3047
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003048TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003049{
3050 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3051 {
3052 if (constructIntoFunction.type == type)
3053 {
3054 return constructIntoFunction.functionName;
3055 }
3056 }
3057
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003058 TType elementType(type);
3059 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003060
3061 ArrayHelperFunction function;
3062 function.type = type;
3063
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003064 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003065
3066 TInfoSinkBase fnOut;
3067
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003068 const TString &typeName = TypeString(type);
3069 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3070 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003071 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003072 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003073 }
3074 fnOut << ")\n"
3075 "{\n";
3076
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003077 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003078 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003079 fnOut << " ";
3080 outputAssign(PreVisit, elementType, fnOut);
3081 fnOut << "a[" << i << "]";
3082 outputAssign(InVisit, elementType, fnOut);
3083 fnOut << "b" << i;
3084 outputAssign(PostVisit, elementType, fnOut);
3085 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003086 }
3087 fnOut << "}\n";
3088
3089 function.functionDefinition = fnOut.c_str();
3090
3091 mArrayConstructIntoFunctions.push_back(function);
3092
3093 return function.functionName;
3094}
3095
Jamie Madill2e295e22015-04-29 10:41:33 -04003096void OutputHLSL::ensureStructDefined(const TType &type)
3097{
Olli Etuahobd3cd502017-11-03 15:48:52 +02003098 const TStructure *structure = type.getStruct();
Jamie Madill2e295e22015-04-29 10:41:33 -04003099 if (structure)
3100 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02003101 ASSERT(type.getBasicType() == EbtStruct);
3102 mStructureHLSL->ensureStructDefined(*structure);
Jamie Madill2e295e22015-04-29 10:41:33 -04003103 }
3104}
3105
Jamie Madill45bcc782016-11-07 13:58:48 -05003106} // namespace sh