blob: 55c57c911817a76b08d806e2c0a3b9ad237f7422 [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 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +0200352 const TInterfaceBlock *interfaceBlock =
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200353 mappedStruct.blockDeclarator->getType().getInterfaceBlock();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200354 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200355 {
356 continue;
357 }
358
359 unsigned int instanceCount = 1u;
360 bool isInstanceArray = mappedStruct.blockDeclarator->isArray();
361 if (isInstanceArray)
362 {
363 instanceCount = mappedStruct.blockDeclarator->getOutermostArraySize();
364 }
365
366 for (unsigned int instanceArrayIndex = 0; instanceArrayIndex < instanceCount;
367 ++instanceArrayIndex)
368 {
369 TString originalName;
370 TString mappedName("map");
371
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200372 if (mappedStruct.blockDeclarator->variable().symbolType() != SymbolType::Empty)
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200373 {
Olli 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) +
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200422 " " + Decorate(name) + ArrayString(type) + " = " + zeroInitializer(type) +
423 ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000424 }
425
Olli Etuaho93b059d2017-12-20 12:46:58 +0200426 for (const auto &attribute : mReferencedAttributes)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000427 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200428 const TType &type = attribute.second->getType();
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200429 const TString &name = attribute.second->name();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000430
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500431 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200432 " = " + zeroInitializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000433 }
434
Jamie Madill8daaba12014-06-13 10:04:33 -0400435 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400436
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300437 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms, mSymbolTable);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800438 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400439
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200440 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500441 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200442 out << "\n// Equality functions\n\n";
443 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500444 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200445 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200446 }
447 }
Olli Etuaho12690762015-03-31 12:55:28 +0300448 if (!mArrayAssignmentFunctions.empty())
449 {
450 out << "\n// Assignment functions\n\n";
451 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
452 {
453 out << assignmentFunction.functionDefinition << "\n";
454 }
455 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300456 if (!mArrayConstructIntoFunctions.empty())
457 {
458 out << "\n// Array constructor functions\n\n";
459 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
460 {
461 out << constructIntoFunction.functionDefinition << "\n";
462 }
463 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200464
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500465 if (mUsesDiscardRewriting)
466 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400467 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500468 }
469
Nicolas Capens655fe362014-04-11 13:12:34 -0400470 if (mUsesNestedBreak)
471 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400472 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400473 }
474
Arun Patole44efa0b2015-03-04 17:11:05 +0530475 if (mRequiresIEEEStrictCompiling)
476 {
477 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
478 }
479
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400480 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
481 "#define LOOP [loop]\n"
482 "#define FLATTEN [flatten]\n"
483 "#else\n"
484 "#define LOOP\n"
485 "#define FLATTEN\n"
486 "#endif\n";
487
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200488 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300490 const bool usingMRTExtension =
491 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000492
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000493 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500494 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400495 out << "\n";
496
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200497 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000498 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200499 for (const auto &outputVariable : mReferencedOutputVariables)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000500 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200501 const TString &variableName = outputVariable.second->name();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200502 const TType &variableType = outputVariable.second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400503
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500504 out << "static " + TypeString(variableType) + " out_" + variableName +
Olli Etuahoea22b7a2018-01-04 17:09:11 +0200505 ArrayString(variableType) + " = " + zeroInitializer(variableType) +
506 ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000507 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000508 }
Jamie Madill46131a32013-06-20 11:55:50 -0400509 else
510 {
511 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
512
513 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500514 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400515 for (unsigned int i = 0; i < numColorValues; i++)
516 {
517 out << " float4(0, 0, 0, 0)";
518 if (i + 1 != numColorValues)
519 {
520 out << ",";
521 }
522 out << "\n";
523 }
524
525 out << "};\n";
526 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000527
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400528 if (mUsesFragDepth)
529 {
530 out << "static float gl_Depth = 0.0;\n";
531 }
532
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000533 if (mUsesFragCoord)
534 {
535 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
536 }
537
538 if (mUsesPointCoord)
539 {
540 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
541 }
542
543 if (mUsesFrontFacing)
544 {
545 out << "static bool gl_FrontFacing = false;\n";
546 }
547
548 out << "\n";
549
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000550 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000551 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000552 out << "struct gl_DepthRangeParameters\n"
553 "{\n"
554 " float near;\n"
555 " float far;\n"
556 " float diff;\n"
557 "};\n"
558 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000559 }
560
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200561 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000562 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000563 out << "cbuffer DriverConstants : register(b1)\n"
564 "{\n";
565
566 if (mUsesDepthRange)
567 {
568 out << " float3 dx_DepthRange : packoffset(c0);\n";
569 }
570
571 if (mUsesFragCoord)
572 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000573 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000574 }
575
576 if (mUsesFragCoord || mUsesFrontFacing)
577 {
578 out << " float3 dx_DepthFront : packoffset(c2);\n";
579 }
580
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800581 if (mUsesFragCoord)
582 {
583 // dx_ViewScale is only used in the fragment shader to correct
584 // the value for glFragCoord if necessary
585 out << " float2 dx_ViewScale : packoffset(c3);\n";
586 }
587
Martin Radev72b4e1e2017-08-31 15:42:56 +0300588 if (mHasMultiviewExtensionEnabled)
589 {
590 // We have to add a value which we can use to keep track of which multi-view code
591 // path is to be selected in the GS.
592 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
593 }
594
Olli Etuaho618bebc2016-01-15 16:40:00 +0200595 if (mOutputType == SH_HLSL_4_1_OUTPUT)
596 {
597 mUniformHLSL->samplerMetadataUniforms(out, "c4");
598 }
599
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000600 out << "};\n";
601 }
602 else
603 {
604 if (mUsesDepthRange)
605 {
606 out << "uniform float3 dx_DepthRange : register(c0);";
607 }
608
609 if (mUsesFragCoord)
610 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000611 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000612 }
613
614 if (mUsesFragCoord || mUsesFrontFacing)
615 {
616 out << "uniform float3 dx_DepthFront : register(c2);\n";
617 }
618 }
619
620 out << "\n";
621
622 if (mUsesDepthRange)
623 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500624 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
625 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000626 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000627 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000628
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200629 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000630 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200631 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000632 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200633 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400634 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000635 }
636
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000637 if (usingMRTExtension && mNumRenderTargets > 1)
638 {
639 out << "#define GL_USES_MRT\n";
640 }
641
642 if (mUsesFragColor)
643 {
644 out << "#define GL_USES_FRAG_COLOR\n";
645 }
646
647 if (mUsesFragData)
648 {
649 out << "#define GL_USES_FRAG_DATA\n";
650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651 }
Xinghua Caob1239382016-12-13 15:07:05 +0800652 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000654 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500655 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000656 out << "\n"
657 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400658
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000659 if (mUsesPointSize)
660 {
661 out << "static float gl_PointSize = float(1);\n";
662 }
663
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000664 if (mUsesInstanceID)
665 {
666 out << "static int gl_InstanceID;";
667 }
668
Corentin Wallezb076add2016-01-11 16:45:46 -0500669 if (mUsesVertexID)
670 {
671 out << "static int gl_VertexID;";
672 }
673
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000674 out << "\n"
675 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500676 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000677 out << "\n";
678
679 if (mUsesDepthRange)
680 {
681 out << "struct gl_DepthRangeParameters\n"
682 "{\n"
683 " float near;\n"
684 " float far;\n"
685 " float diff;\n"
686 "};\n"
687 "\n";
688 }
689
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200690 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000691 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800692 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500693 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800694
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000695 if (mUsesDepthRange)
696 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800697 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000698 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800699
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800700 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
701 // shaders. However, we declare it for all shaders (including Feature Level 10+).
702 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
703 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800704 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800705 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800706 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800707
Martin Radev72b4e1e2017-08-31 15:42:56 +0300708 if (mHasMultiviewExtensionEnabled)
709 {
710 // We have to add a value which we can use to keep track of which multi-view code
711 // path is to be selected in the GS.
712 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
713 }
714
Olli Etuaho618bebc2016-01-15 16:40:00 +0200715 if (mOutputType == SH_HLSL_4_1_OUTPUT)
716 {
717 mUniformHLSL->samplerMetadataUniforms(out, "c4");
718 }
719
Austin Kinross4fd18b12014-12-22 12:32:05 -0800720 out << "};\n"
721 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000722 }
723 else
724 {
725 if (mUsesDepthRange)
726 {
727 out << "uniform float3 dx_DepthRange : register(c0);\n";
728 }
729
Cooper Partine6664f02015-01-09 16:22:24 -0800730 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
731 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000732 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000733 }
734
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000735 if (mUsesDepthRange)
736 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500737 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
738 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000739 "\n";
740 }
741
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200742 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000743 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200744 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000745 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200746 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400747 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000748 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400749 }
Xinghua Caob1239382016-12-13 15:07:05 +0800750 else // Compute shader
751 {
752 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800753
754 out << "cbuffer DriverConstants : register(b1)\n"
755 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800756 if (mUsesNumWorkGroups)
757 {
Xinghua Caob1239382016-12-13 15:07:05 +0800758 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800759 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800760 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
761 mUniformHLSL->samplerMetadataUniforms(out, "c1");
762 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800763
764 // Follow built-in variables would be initialized in
765 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
766 // are used in compute shader.
767 if (mUsesWorkGroupID)
768 {
769 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
770 }
771
772 if (mUsesLocalInvocationID)
773 {
774 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
775 }
776
777 if (mUsesGlobalInvocationID)
778 {
779 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
780 }
781
782 if (mUsesLocalInvocationIndex)
783 {
784 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
785 }
786 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000787
Geoff Lang1fe74c72016-08-25 13:23:01 -0400788 bool getDimensionsIgnoresBaseLevel =
789 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
790 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800791 mImageFunctionHLSL->imageFunctionHeader(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000792
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000793 if (mUsesFragCoord)
794 {
795 out << "#define GL_USES_FRAG_COORD\n";
796 }
797
798 if (mUsesPointCoord)
799 {
800 out << "#define GL_USES_POINT_COORD\n";
801 }
802
803 if (mUsesFrontFacing)
804 {
805 out << "#define GL_USES_FRONT_FACING\n";
806 }
807
808 if (mUsesPointSize)
809 {
810 out << "#define GL_USES_POINT_SIZE\n";
811 }
812
Martin Radev41ac68e2017-06-06 12:16:58 +0300813 if (mHasMultiviewExtensionEnabled)
814 {
815 out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n";
816 }
817
818 if (mUsesViewID)
819 {
820 out << "#define GL_USES_VIEW_ID\n";
821 }
822
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400823 if (mUsesFragDepth)
824 {
825 out << "#define GL_USES_FRAG_DEPTH\n";
826 }
827
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000828 if (mUsesDepthRange)
829 {
830 out << "#define GL_USES_DEPTH_RANGE\n";
831 }
832
Xinghua Caob1239382016-12-13 15:07:05 +0800833 if (mUsesNumWorkGroups)
834 {
835 out << "#define GL_USES_NUM_WORK_GROUPS\n";
836 }
837
838 if (mUsesWorkGroupID)
839 {
840 out << "#define GL_USES_WORK_GROUP_ID\n";
841 }
842
843 if (mUsesLocalInvocationID)
844 {
845 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
846 }
847
848 if (mUsesGlobalInvocationID)
849 {
850 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
851 }
852
853 if (mUsesLocalInvocationIndex)
854 {
855 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
856 }
857
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000858 if (mUsesXor)
859 {
860 out << "bool xor(bool p, bool q)\n"
861 "{\n"
862 " return (p || q) && !(p && q);\n"
863 "}\n"
864 "\n";
865 }
866
Olli Etuahodfa75e82017-01-23 09:43:06 -0800867 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868}
869
870void OutputHLSL::visitSymbol(TIntermSymbol *node)
871{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200872 const TVariable &variable = node->variable();
873
874 // Empty symbols can only appear in declarations and function arguments, and in either of those
875 // cases the symbol nodes are not visited.
876 ASSERT(variable.symbolType() != SymbolType::Empty);
877
Jamie Madill32aab012015-01-27 14:12:26 -0500878 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879
Jamie Madill570e04d2013-06-21 09:15:33 -0400880 // Handle accessing std140 structs by value
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200881 if (IsInStd140InterfaceBlock(node) && node->getBasicType() == EbtStruct)
Jamie Madill570e04d2013-06-21 09:15:33 -0400882 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200883 out << "map";
Jamie Madill570e04d2013-06-21 09:15:33 -0400884 }
885
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200886 const TString &name = variable.name();
887 const TSymbolUniqueId &uniqueId = variable.uniqueId();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200888
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000889 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000890 {
891 mUsesDepthRange = true;
892 out << name;
893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894 else
895 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200896 const TType &variableType = variable.getType();
897 TQualifier qualifier = variable.getType().getQualifier();
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000898
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200899 ensureStructDefined(variableType);
Olli Etuahobd3cd502017-11-03 15:48:52 +0200900
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000901 if (qualifier == EvqUniform)
902 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200903 const TInterfaceBlock *interfaceBlock = variableType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400904
905 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000906 {
Olli Etuahoc71862a2017-12-21 12:58:29 +0200907 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
908 {
909 const TVariable *instanceVariable = nullptr;
910 if (variableType.isInterfaceBlock())
911 {
912 instanceVariable = &variable;
913 }
914 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
915 new TReferencedBlock(interfaceBlock, instanceVariable);
916 }
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000917 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000918 else
919 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200920 mReferencedUniforms[uniqueId.get()] = &variable;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000921 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400922
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200923 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000924 }
Jamie Madill19571812013-08-12 15:26:34 -0700925 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000926 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200927 mReferencedAttributes[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400928 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000929 }
Jamie Madill033dae62014-06-18 12:56:28 -0400930 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000931 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200932 mReferencedVaryings[uniqueId.get()] = &variable;
Jamie Madill033dae62014-06-18 12:56:28 -0400933 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300934 if (name == "ViewID_OVR")
935 {
936 mUsesViewID = true;
937 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000938 }
Jamie Madill19571812013-08-12 15:26:34 -0700939 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400940 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +0200941 mReferencedOutputVariables[uniqueId.get()] = &variable;
Jamie Madill46131a32013-06-20 11:55:50 -0400942 out << "out_" << name;
943 }
944 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000945 {
946 out << "gl_Color[0]";
947 mUsesFragColor = true;
948 }
949 else if (qualifier == EvqFragData)
950 {
951 out << "gl_Color";
952 mUsesFragData = true;
953 }
954 else if (qualifier == EvqFragCoord)
955 {
956 mUsesFragCoord = true;
957 out << name;
958 }
959 else if (qualifier == EvqPointCoord)
960 {
961 mUsesPointCoord = true;
962 out << name;
963 }
964 else if (qualifier == EvqFrontFacing)
965 {
966 mUsesFrontFacing = true;
967 out << name;
968 }
969 else if (qualifier == EvqPointSize)
970 {
971 mUsesPointSize = true;
972 out << name;
973 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000974 else if (qualifier == EvqInstanceID)
975 {
976 mUsesInstanceID = true;
977 out << name;
978 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500979 else if (qualifier == EvqVertexID)
980 {
981 mUsesVertexID = true;
982 out << name;
983 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300984 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400985 {
986 mUsesFragDepth = true;
987 out << "gl_Depth";
988 }
Xinghua Caob1239382016-12-13 15:07:05 +0800989 else if (qualifier == EvqNumWorkGroups)
990 {
991 mUsesNumWorkGroups = true;
992 out << name;
993 }
994 else if (qualifier == EvqWorkGroupID)
995 {
996 mUsesWorkGroupID = true;
997 out << name;
998 }
999 else if (qualifier == EvqLocalInvocationID)
1000 {
1001 mUsesLocalInvocationID = true;
1002 out << name;
1003 }
1004 else if (qualifier == EvqGlobalInvocationID)
1005 {
1006 mUsesGlobalInvocationID = true;
1007 out << name;
1008 }
1009 else if (qualifier == EvqLocalInvocationIndex)
1010 {
1011 mUsesLocalInvocationIndex = true;
1012 out << name;
1013 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001014 else
1015 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001016 out << DecorateVariableIfNeeded(variable);
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001017 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018 }
1019}
1020
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001021void OutputHLSL::visitRaw(TIntermRaw *node)
1022{
Jamie Madill32aab012015-01-27 14:12:26 -05001023 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001024}
1025
Olli Etuaho7fb49552015-03-18 17:27:44 +02001026void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1027{
1028 if (type.isScalar() && !type.isArray())
1029 {
1030 if (op == EOpEqual)
1031 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001032 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001033 }
1034 else
1035 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001036 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001037 }
1038 }
1039 else
1040 {
1041 if (visit == PreVisit && op == EOpNotEqual)
1042 {
1043 out << "!";
1044 }
1045
1046 if (type.isArray())
1047 {
1048 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001049 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001050 }
1051 else if (type.getBasicType() == EbtStruct)
1052 {
1053 const TStructure &structure = *type.getStruct();
1054 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001055 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001056 }
1057 else
1058 {
1059 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001060 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001061 }
1062 }
1063}
1064
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001065void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1066{
1067 if (type.isArray())
1068 {
1069 const TString &functionName = addArrayAssignmentFunction(type);
1070 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1071 }
1072 else
1073 {
1074 outputTriplet(out, visit, "(", " = ", ")");
1075 }
1076}
1077
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001078bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001079{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001080 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001081 {
1082 TIntermNode *ancestor = getAncestorNode(n);
1083 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1084 if (ancestorBinary == nullptr)
1085 {
1086 return false;
1087 }
1088 switch (ancestorBinary->getOp())
1089 {
1090 case EOpIndexDirectStruct:
1091 {
1092 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1093 const TIntermConstantUnion *index =
1094 ancestorBinary->getRight()->getAsConstantUnion();
1095 const TField *field = structure->fields()[index->getIConst(0)];
1096 if (IsSampler(field->type()->getBasicType()))
1097 {
1098 return true;
1099 }
1100 break;
1101 }
1102 case EOpIndexDirect:
1103 break;
1104 default:
1105 // Returning a sampler from indirect indexing is not supported.
1106 return false;
1107 }
1108 }
1109 return false;
1110}
1111
Olli Etuahob6fa0432016-09-28 16:28:05 +01001112bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1113{
1114 TInfoSinkBase &out = getInfoSink();
1115 if (visit == PostVisit)
1116 {
1117 out << ".";
1118 node->writeOffsetsAsXYZW(&out);
1119 }
1120 return true;
1121}
1122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001123bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1124{
Jamie Madill32aab012015-01-27 14:12:26 -05001125 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001126
1127 switch (node->getOp())
1128 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001129 case EOpComma:
1130 outputTriplet(out, visit, "(", ", ", ")");
1131 break;
1132 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001133 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001134 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001135 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1136 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001137 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001138 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1139 out << functionName << "(";
1140 node->getLeft()->traverse(this);
1141 TIntermSequence *seq = rightAgg->getSequence();
1142 for (auto &arrayElement : *seq)
1143 {
1144 out << ", ";
1145 arrayElement->traverse(this);
1146 }
1147 out << ")";
1148 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001149 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001150 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1151 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001152 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001153 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001154 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001155 break;
1156 case EOpInitialize:
1157 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001158 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001159 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1160 ASSERT(symbolNode);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001161 TIntermTyped *initializer = node->getRight();
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001162
1163 // Global initializers must be constant at this point.
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001164 ASSERT(symbolNode->getQualifier() != EvqGlobal || initializer->hasConstantValue());
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001165
1166 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1167 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1168 // new variable is created before the assignment is evaluated), so we need to
1169 // convert
1170 // this to "float t = x, x = t;".
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001171 if (writeSameSymbolInitializer(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001172 {
1173 // Skip initializing the rest of the expression
1174 return false;
1175 }
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001176 else if (writeConstantInitialization(out, symbolNode, initializer))
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001177 {
1178 return false;
1179 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001180 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001181 else if (visit == InVisit)
1182 {
1183 out << " = ";
1184 }
1185 break;
1186 case EOpAddAssign:
1187 outputTriplet(out, visit, "(", " += ", ")");
1188 break;
1189 case EOpSubAssign:
1190 outputTriplet(out, visit, "(", " -= ", ")");
1191 break;
1192 case EOpMulAssign:
1193 outputTriplet(out, visit, "(", " *= ", ")");
1194 break;
1195 case EOpVectorTimesScalarAssign:
1196 outputTriplet(out, visit, "(", " *= ", ")");
1197 break;
1198 case EOpMatrixTimesScalarAssign:
1199 outputTriplet(out, visit, "(", " *= ", ")");
1200 break;
1201 case EOpVectorTimesMatrixAssign:
1202 if (visit == PreVisit)
1203 {
1204 out << "(";
1205 }
1206 else if (visit == InVisit)
1207 {
1208 out << " = mul(";
1209 node->getLeft()->traverse(this);
1210 out << ", transpose(";
1211 }
1212 else
1213 {
1214 out << ")))";
1215 }
1216 break;
1217 case EOpMatrixTimesMatrixAssign:
1218 if (visit == PreVisit)
1219 {
1220 out << "(";
1221 }
1222 else if (visit == InVisit)
1223 {
1224 out << " = transpose(mul(transpose(";
1225 node->getLeft()->traverse(this);
1226 out << "), transpose(";
1227 }
1228 else
1229 {
1230 out << "))))";
1231 }
1232 break;
1233 case EOpDivAssign:
1234 outputTriplet(out, visit, "(", " /= ", ")");
1235 break;
1236 case EOpIModAssign:
1237 outputTriplet(out, visit, "(", " %= ", ")");
1238 break;
1239 case EOpBitShiftLeftAssign:
1240 outputTriplet(out, visit, "(", " <<= ", ")");
1241 break;
1242 case EOpBitShiftRightAssign:
1243 outputTriplet(out, visit, "(", " >>= ", ")");
1244 break;
1245 case EOpBitwiseAndAssign:
1246 outputTriplet(out, visit, "(", " &= ", ")");
1247 break;
1248 case EOpBitwiseXorAssign:
1249 outputTriplet(out, visit, "(", " ^= ", ")");
1250 break;
1251 case EOpBitwiseOrAssign:
1252 outputTriplet(out, visit, "(", " |= ", ")");
1253 break;
1254 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001255 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001256 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001257 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001258 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001259 if (visit == PreVisit)
1260 {
Olli Etuaho12a18ad2017-12-01 16:59:47 +02001261 TIntermSymbol *instanceArraySymbol = node->getLeft()->getAsSymbolNode();
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001262 const TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Olli Etuahoc71862a2017-12-21 12:58:29 +02001263 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
1264 {
1265 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
1266 new TReferencedBlock(interfaceBlock, &instanceArraySymbol->variable());
1267 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001268 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001269 out << mUniformHLSL->UniformBlockInstanceString(instanceArraySymbol->getName(),
1270 arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001271 return false;
1272 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001273 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001274 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001275 {
1276 // All parts of an expression that access a sampler in a struct need to use _ as
1277 // separator to access the sampler variable that has been moved out of the struct.
1278 outputTriplet(out, visit, "", "_", "");
1279 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001280 else
1281 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001282 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001283 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001284 }
1285 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001286 case EOpIndexIndirect:
1287 // We do not currently support indirect references to interface blocks
1288 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1289 outputTriplet(out, visit, "", "[", "]");
1290 break;
1291 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001292 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001293 const TStructure *structure = node->getLeft()->getType().getStruct();
1294 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1295 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001296
Olli Etuaho96963162016-03-21 11:54:33 +02001297 // In cases where indexing returns a sampler, we need to access the sampler variable
1298 // that has been moved out of the struct.
1299 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1300 if (visit == PreVisit && indexingReturnsSampler)
1301 {
1302 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1303 // This prefix is only output at the beginning of the indexing expression, which
1304 // may have multiple parts.
1305 out << "angle";
1306 }
1307 if (!indexingReturnsSampler)
1308 {
1309 // All parts of an expression that access a sampler in a struct need to use _ as
1310 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001311 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001312 }
1313 if (visit == InVisit)
1314 {
1315 if (indexingReturnsSampler)
1316 {
1317 out << "_" + field->name();
1318 }
1319 else
1320 {
1321 out << "." + DecorateField(field->name(), *structure);
1322 }
1323
1324 return false;
1325 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001326 }
1327 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001328 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001329 {
1330 bool structInStd140Block =
1331 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1332 if (visit == PreVisit && structInStd140Block)
1333 {
1334 out << "map";
1335 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001336 if (visit == InVisit)
1337 {
1338 const TInterfaceBlock *interfaceBlock =
1339 node->getLeft()->getType().getInterfaceBlock();
1340 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1341 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001342 if (structInStd140Block)
1343 {
1344 out << "_";
1345 }
1346 else
1347 {
1348 out << ".";
1349 }
1350 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001351
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001352 return false;
1353 }
1354 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001355 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001356 case EOpAdd:
1357 outputTriplet(out, visit, "(", " + ", ")");
1358 break;
1359 case EOpSub:
1360 outputTriplet(out, visit, "(", " - ", ")");
1361 break;
1362 case EOpMul:
1363 outputTriplet(out, visit, "(", " * ", ")");
1364 break;
1365 case EOpDiv:
1366 outputTriplet(out, visit, "(", " / ", ")");
1367 break;
1368 case EOpIMod:
1369 outputTriplet(out, visit, "(", " % ", ")");
1370 break;
1371 case EOpBitShiftLeft:
1372 outputTriplet(out, visit, "(", " << ", ")");
1373 break;
1374 case EOpBitShiftRight:
1375 outputTriplet(out, visit, "(", " >> ", ")");
1376 break;
1377 case EOpBitwiseAnd:
1378 outputTriplet(out, visit, "(", " & ", ")");
1379 break;
1380 case EOpBitwiseXor:
1381 outputTriplet(out, visit, "(", " ^ ", ")");
1382 break;
1383 case EOpBitwiseOr:
1384 outputTriplet(out, visit, "(", " | ", ")");
1385 break;
1386 case EOpEqual:
1387 case EOpNotEqual:
1388 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1389 break;
1390 case EOpLessThan:
1391 outputTriplet(out, visit, "(", " < ", ")");
1392 break;
1393 case EOpGreaterThan:
1394 outputTriplet(out, visit, "(", " > ", ")");
1395 break;
1396 case EOpLessThanEqual:
1397 outputTriplet(out, visit, "(", " <= ", ")");
1398 break;
1399 case EOpGreaterThanEqual:
1400 outputTriplet(out, visit, "(", " >= ", ")");
1401 break;
1402 case EOpVectorTimesScalar:
1403 outputTriplet(out, visit, "(", " * ", ")");
1404 break;
1405 case EOpMatrixTimesScalar:
1406 outputTriplet(out, visit, "(", " * ", ")");
1407 break;
1408 case EOpVectorTimesMatrix:
1409 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1410 break;
1411 case EOpMatrixTimesVector:
1412 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1413 break;
1414 case EOpMatrixTimesMatrix:
1415 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1416 break;
1417 case EOpLogicalOr:
1418 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1419 // been unfolded.
1420 ASSERT(!node->getRight()->hasSideEffects());
1421 outputTriplet(out, visit, "(", " || ", ")");
1422 return true;
1423 case EOpLogicalXor:
1424 mUsesXor = true;
1425 outputTriplet(out, visit, "xor(", ", ", ")");
1426 break;
1427 case EOpLogicalAnd:
1428 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1429 // been unfolded.
1430 ASSERT(!node->getRight()->hasSideEffects());
1431 outputTriplet(out, visit, "(", " && ", ")");
1432 return true;
1433 default:
1434 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001435 }
1436
1437 return true;
1438}
1439
1440bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1441{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001442 TInfoSinkBase &out = getInfoSink();
1443
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001444 switch (node->getOp())
1445 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001446 case EOpNegative:
1447 outputTriplet(out, visit, "(-", "", ")");
1448 break;
1449 case EOpPositive:
1450 outputTriplet(out, visit, "(+", "", ")");
1451 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001452 case EOpLogicalNot:
1453 outputTriplet(out, visit, "(!", "", ")");
1454 break;
1455 case EOpBitwiseNot:
1456 outputTriplet(out, visit, "(~", "", ")");
1457 break;
1458 case EOpPostIncrement:
1459 outputTriplet(out, visit, "(", "", "++)");
1460 break;
1461 case EOpPostDecrement:
1462 outputTriplet(out, visit, "(", "", "--)");
1463 break;
1464 case EOpPreIncrement:
1465 outputTriplet(out, visit, "(++", "", ")");
1466 break;
1467 case EOpPreDecrement:
1468 outputTriplet(out, visit, "(--", "", ")");
1469 break;
1470 case EOpRadians:
1471 outputTriplet(out, visit, "radians(", "", ")");
1472 break;
1473 case EOpDegrees:
1474 outputTriplet(out, visit, "degrees(", "", ")");
1475 break;
1476 case EOpSin:
1477 outputTriplet(out, visit, "sin(", "", ")");
1478 break;
1479 case EOpCos:
1480 outputTriplet(out, visit, "cos(", "", ")");
1481 break;
1482 case EOpTan:
1483 outputTriplet(out, visit, "tan(", "", ")");
1484 break;
1485 case EOpAsin:
1486 outputTriplet(out, visit, "asin(", "", ")");
1487 break;
1488 case EOpAcos:
1489 outputTriplet(out, visit, "acos(", "", ")");
1490 break;
1491 case EOpAtan:
1492 outputTriplet(out, visit, "atan(", "", ")");
1493 break;
1494 case EOpSinh:
1495 outputTriplet(out, visit, "sinh(", "", ")");
1496 break;
1497 case EOpCosh:
1498 outputTriplet(out, visit, "cosh(", "", ")");
1499 break;
1500 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001501 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001502 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001503 case EOpAtanh:
1504 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001505 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001506 break;
1507 case EOpExp:
1508 outputTriplet(out, visit, "exp(", "", ")");
1509 break;
1510 case EOpLog:
1511 outputTriplet(out, visit, "log(", "", ")");
1512 break;
1513 case EOpExp2:
1514 outputTriplet(out, visit, "exp2(", "", ")");
1515 break;
1516 case EOpLog2:
1517 outputTriplet(out, visit, "log2(", "", ")");
1518 break;
1519 case EOpSqrt:
1520 outputTriplet(out, visit, "sqrt(", "", ")");
1521 break;
1522 case EOpInverseSqrt:
1523 outputTriplet(out, visit, "rsqrt(", "", ")");
1524 break;
1525 case EOpAbs:
1526 outputTriplet(out, visit, "abs(", "", ")");
1527 break;
1528 case EOpSign:
1529 outputTriplet(out, visit, "sign(", "", ")");
1530 break;
1531 case EOpFloor:
1532 outputTriplet(out, visit, "floor(", "", ")");
1533 break;
1534 case EOpTrunc:
1535 outputTriplet(out, visit, "trunc(", "", ")");
1536 break;
1537 case EOpRound:
1538 outputTriplet(out, visit, "round(", "", ")");
1539 break;
1540 case EOpRoundEven:
1541 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001542 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001543 break;
1544 case EOpCeil:
1545 outputTriplet(out, visit, "ceil(", "", ")");
1546 break;
1547 case EOpFract:
1548 outputTriplet(out, visit, "frac(", "", ")");
1549 break;
1550 case EOpIsNan:
1551 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001552 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001553 else
1554 outputTriplet(out, visit, "isnan(", "", ")");
1555 mRequiresIEEEStrictCompiling = true;
1556 break;
1557 case EOpIsInf:
1558 outputTriplet(out, visit, "isinf(", "", ")");
1559 break;
1560 case EOpFloatBitsToInt:
1561 outputTriplet(out, visit, "asint(", "", ")");
1562 break;
1563 case EOpFloatBitsToUint:
1564 outputTriplet(out, visit, "asuint(", "", ")");
1565 break;
1566 case EOpIntBitsToFloat:
1567 outputTriplet(out, visit, "asfloat(", "", ")");
1568 break;
1569 case EOpUintBitsToFloat:
1570 outputTriplet(out, visit, "asfloat(", "", ")");
1571 break;
1572 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001573 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001574 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001575 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001576 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001577 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001578 case EOpPackUnorm4x8:
1579 case EOpPackSnorm4x8:
1580 case EOpUnpackUnorm4x8:
1581 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001582 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001583 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001584 break;
1585 case EOpLength:
1586 outputTriplet(out, visit, "length(", "", ")");
1587 break;
1588 case EOpNormalize:
1589 outputTriplet(out, visit, "normalize(", "", ")");
1590 break;
1591 case EOpDFdx:
1592 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1593 {
1594 outputTriplet(out, visit, "(", "", ", 0.0)");
1595 }
1596 else
1597 {
1598 outputTriplet(out, visit, "ddx(", "", ")");
1599 }
1600 break;
1601 case EOpDFdy:
1602 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1603 {
1604 outputTriplet(out, visit, "(", "", ", 0.0)");
1605 }
1606 else
1607 {
1608 outputTriplet(out, visit, "ddy(", "", ")");
1609 }
1610 break;
1611 case EOpFwidth:
1612 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1613 {
1614 outputTriplet(out, visit, "(", "", ", 0.0)");
1615 }
1616 else
1617 {
1618 outputTriplet(out, visit, "fwidth(", "", ")");
1619 }
1620 break;
1621 case EOpTranspose:
1622 outputTriplet(out, visit, "transpose(", "", ")");
1623 break;
1624 case EOpDeterminant:
1625 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1626 break;
1627 case EOpInverse:
1628 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001629 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001630 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001631
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001632 case EOpAny:
1633 outputTriplet(out, visit, "any(", "", ")");
1634 break;
1635 case EOpAll:
1636 outputTriplet(out, visit, "all(", "", ")");
1637 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001638 case EOpLogicalNotComponentWise:
1639 outputTriplet(out, visit, "(!", "", ")");
1640 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001641 case EOpBitfieldReverse:
1642 outputTriplet(out, visit, "reversebits(", "", ")");
1643 break;
1644 case EOpBitCount:
1645 outputTriplet(out, visit, "countbits(", "", ")");
1646 break;
1647 case EOpFindLSB:
1648 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1649 // in GLSLTest and results are consistent with GL.
1650 outputTriplet(out, visit, "firstbitlow(", "", ")");
1651 break;
1652 case EOpFindMSB:
1653 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1654 // tested in GLSLTest and results are consistent with GL.
1655 outputTriplet(out, visit, "firstbithigh(", "", ")");
1656 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001657 default:
1658 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001659 }
1660
1661 return true;
1662}
1663
Olli Etuaho96963162016-03-21 11:54:33 +02001664TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1665{
1666 if (node->getAsSymbolNode())
1667 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001668 ASSERT(node->getAsSymbolNode()->variable().symbolType() != SymbolType::Empty);
1669 return node->getAsSymbolNode()->getName();
Olli Etuaho96963162016-03-21 11:54:33 +02001670 }
1671 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1672 switch (nodeBinary->getOp())
1673 {
1674 case EOpIndexDirect:
1675 {
1676 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1677
1678 TInfoSinkBase prefixSink;
1679 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1680 return TString(prefixSink.c_str());
1681 }
1682 case EOpIndexDirectStruct:
1683 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001684 const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001685 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1686 const TField *field = s->fields()[index];
1687
1688 TInfoSinkBase prefixSink;
1689 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1690 << field->name();
1691 return TString(prefixSink.c_str());
1692 }
1693 default:
1694 UNREACHABLE();
1695 return TString("");
1696 }
1697}
1698
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001699bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1700{
1701 TInfoSinkBase &out = getInfoSink();
1702
1703 if (mInsideFunction)
1704 {
1705 outputLineDirective(out, node->getLine().first_line);
1706 out << "{\n";
1707 }
1708
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001709 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001710 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001711 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001712
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001713 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001714
1715 // Don't output ; after case labels, they're terminated by :
1716 // This is needed especially since outputting a ; after a case statement would turn empty
1717 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001718 // Also the output code is clearer if we don't output ; after statements where it is not
1719 // needed:
1720 // * if statements
1721 // * switch statements
1722 // * blocks
1723 // * function definitions
1724 // * loops (do-while loops output the semicolon in VisitLoop)
1725 // * declarations that don't generate output.
1726 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1727 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1728 statement->getAsSwitchNode() == nullptr &&
1729 statement->getAsFunctionDefinition() == nullptr &&
1730 (statement->getAsDeclarationNode() == nullptr ||
1731 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1732 statement->getAsInvariantDeclarationNode() == nullptr)
1733 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001734 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001735 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001736 }
1737
1738 if (mInsideFunction)
1739 {
1740 outputLineDirective(out, node->getLine().last_line);
1741 out << "}\n";
1742 }
1743
1744 return false;
1745}
1746
Olli Etuaho336b1472016-10-05 16:37:55 +01001747bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1748{
1749 TInfoSinkBase &out = getInfoSink();
1750
1751 ASSERT(mCurrentFunctionMetadata == nullptr);
1752
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001753 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho336b1472016-10-05 16:37:55 +01001754 ASSERT(index != CallDAG::InvalidIndex);
1755 mCurrentFunctionMetadata = &mASTMetadataList[index];
1756
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001757 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001758
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001759 TIntermSequence *parameters = node->getFunctionPrototype()->getSequence();
Olli Etuaho336b1472016-10-05 16:37:55 +01001760
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001761 if (node->getFunction()->isMain())
Olli Etuaho336b1472016-10-05 16:37:55 +01001762 {
1763 out << "gl_main(";
1764 }
1765 else
1766 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001767 out << DecorateFunctionIfNeeded(node->getFunction()) << DisambiguateFunctionName(parameters)
1768 << (mOutputLod0Function ? "Lod0(" : "(");
Olli Etuaho336b1472016-10-05 16:37:55 +01001769 }
1770
1771 for (unsigned int i = 0; i < parameters->size(); i++)
1772 {
1773 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1774
1775 if (symbol)
1776 {
1777 ensureStructDefined(symbol->getType());
1778
1779 out << argumentString(symbol);
1780
1781 if (i < parameters->size() - 1)
1782 {
1783 out << ", ";
1784 }
1785 }
1786 else
1787 UNREACHABLE();
1788 }
1789
1790 out << ")\n";
1791
1792 mInsideFunction = true;
1793 // The function body node will output braces.
1794 node->getBody()->traverse(this);
1795 mInsideFunction = false;
1796
1797 mCurrentFunctionMetadata = nullptr;
1798
1799 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1800 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1801 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001802 ASSERT(!node->getFunction()->isMain());
Olli Etuaho336b1472016-10-05 16:37:55 +01001803 mOutputLod0Function = true;
1804 node->traverse(this);
1805 mOutputLod0Function = false;
1806 }
1807
1808 return false;
1809}
1810
Olli Etuaho13389b62016-10-16 11:48:18 +01001811bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1812{
Olli Etuaho13389b62016-10-16 11:48:18 +01001813 if (visit == PreVisit)
1814 {
1815 TIntermSequence *sequence = node->getSequence();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001816 TIntermTyped *declarator = (*sequence)[0]->getAsTyped();
Olli Etuaho13389b62016-10-16 11:48:18 +01001817 ASSERT(sequence->size() == 1);
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001818 ASSERT(declarator);
Olli Etuaho13389b62016-10-16 11:48:18 +01001819
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001820 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001821 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001822 TInfoSinkBase &out = getInfoSink();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001823 ensureStructDefined(declarator->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001824
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001825 if (!declarator->getAsSymbolNode() ||
1826 declarator->getAsSymbolNode()->variable().symbolType() !=
1827 SymbolType::Empty) // Variable declaration
Olli Etuaho13389b62016-10-16 11:48:18 +01001828 {
1829 if (!mInsideFunction)
1830 {
1831 out << "static ";
1832 }
1833
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001834 out << TypeString(declarator->getType()) + " ";
Olli Etuaho13389b62016-10-16 11:48:18 +01001835
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001836 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho13389b62016-10-16 11:48:18 +01001837
1838 if (symbol)
1839 {
1840 symbol->traverse(this);
1841 out << ArrayString(symbol->getType());
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001842 out << " = " + zeroInitializer(symbol->getType());
Olli Etuaho13389b62016-10-16 11:48:18 +01001843 }
1844 else
1845 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001846 declarator->traverse(this);
Olli Etuaho13389b62016-10-16 11:48:18 +01001847 }
1848 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001849 }
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001850 else if (IsVaryingOut(declarator->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001851 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02001852 TIntermSymbol *symbol = declarator->getAsSymbolNode();
Olli Etuaho282847e2017-07-12 14:11:01 +03001853 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001854
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001855 const TVariable &variable = symbol->variable();
1856
1857 if (variable.symbolType() != SymbolType::Empty)
Olli Etuaho93b059d2017-12-20 12:46:58 +02001858 {
1859 // Vertex outputs which are declared but not written to should still be declared to
1860 // allow successful linking.
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001861 mReferencedVaryings[symbol->uniqueId().get()] = &variable;
Olli Etuaho93b059d2017-12-20 12:46:58 +02001862 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001863 }
1864 }
1865 return false;
1866}
1867
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001868bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1869{
1870 // Do not do any translation
1871 return false;
1872}
1873
Olli Etuaho16c745a2017-01-16 17:02:27 +00001874bool OutputHLSL::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
1875{
1876 TInfoSinkBase &out = getInfoSink();
1877
1878 ASSERT(visit == PreVisit);
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001879 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001880 // Skip the prototype if it is not implemented (and thus not used)
1881 if (index == CallDAG::InvalidIndex)
1882 {
1883 return false;
1884 }
1885
1886 TIntermSequence *arguments = node->getSequence();
1887
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001888 TString name = DecorateFunctionIfNeeded(node->getFunction());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001889 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1890 << (mOutputLod0Function ? "Lod0(" : "(");
1891
1892 for (unsigned int i = 0; i < arguments->size(); i++)
1893 {
1894 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1895 ASSERT(symbol != nullptr);
1896
1897 out << argumentString(symbol);
1898
1899 if (i < arguments->size() - 1)
1900 {
1901 out << ", ";
1902 }
1903 }
1904
1905 out << ");\n";
1906
1907 // Also prototype the Lod0 variant if needed
1908 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1909 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1910 {
1911 mOutputLod0Function = true;
1912 node->traverse(this);
1913 mOutputLod0Function = false;
1914 }
1915
1916 return false;
1917}
1918
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1920{
Jamie Madill32aab012015-01-27 14:12:26 -05001921 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 switch (node->getOp())
1924 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001925 case EOpCallBuiltInFunction:
1926 case EOpCallFunctionInAST:
1927 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001929 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001930
Corentin Wallez1239ee92015-03-19 14:38:02 -07001931 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001932 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001934 if (node->isArray())
1935 {
1936 UNIMPLEMENTED();
1937 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001938 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001939 ASSERT(index != CallDAG::InvalidIndex);
1940 lod0 &= mASTMetadataList[index].mNeedsLod0;
1941
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001942 out << DecorateFunctionIfNeeded(node->getFunction());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001943 out << DisambiguateFunctionName(node->getSequence());
1944 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001946 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001947 {
1948 // This path is used for internal functions that don't have their definitions in the
1949 // AST, such as precision emulation functions.
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001950 out << DecorateFunctionIfNeeded(node->getFunction()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001951 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001952 else if (node->getFunction()->isImageFunction())
Xinghua Cao711b7a12017-10-09 13:38:12 +08001953 {
Olli Etuahobed35d72017-12-20 16:36:26 +02001954 const TString &name = node->getFunction()->name();
Xinghua Cao711b7a12017-10-09 13:38:12 +08001955 TType type = (*arguments)[0]->getAsTyped()->getType();
1956 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001957 name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
Xinghua Cao711b7a12017-10-09 13:38:12 +08001958 type.getMemoryQualifier().readonly);
1959 out << imageFunctionName << "(";
1960 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001961 else
1962 {
Olli Etuahobed35d72017-12-20 16:36:26 +02001963 const TString &name = node->getFunction()->name();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001964 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001965 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1966 if (arguments->size() > 1)
1967 {
1968 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1969 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001970 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001971 name, samplerType, coords, arguments->size(), lod0, mShaderType);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001972 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001973 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001974
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001975 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001976 {
Olli Etuaho96963162016-03-21 11:54:33 +02001977 TIntermTyped *typedArg = (*arg)->getAsTyped();
1978 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001979 {
1980 out << "texture_";
1981 (*arg)->traverse(this);
1982 out << ", sampler_";
1983 }
1984
1985 (*arg)->traverse(this);
1986
Olli Etuaho96963162016-03-21 11:54:33 +02001987 if (typedArg->getType().isStructureContainingSamplers())
1988 {
1989 const TType &argType = typedArg->getType();
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001990 TVector<const TVariable *> samplerSymbols;
Olli Etuaho96963162016-03-21 11:54:33 +02001991 TString structName = samplerNamePrefixFromStruct(typedArg);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001992 argType.createSamplerSymbols("angle_" + structName, "", &samplerSymbols,
1993 nullptr, mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001994 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02001995 {
1996 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1997 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02001998 out << ", texture_" << sampler->name();
1999 out << ", sampler_" << sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02002000 }
2001 else
2002 {
2003 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
2004 // of D3D9, it's the sampler variable.
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002005 out << ", " + sampler->name();
Olli Etuaho96963162016-03-21 11:54:33 +02002006 }
2007 }
2008 }
2009
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002010 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002011 {
2012 out << ", ";
2013 }
2014 }
2015
2016 out << ")";
2017
2018 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03002020 case EOpConstruct:
Olli Etuahobd3cd502017-11-03 15:48:52 +02002021 outputConstructor(out, visit, node);
Olli Etuaho8fab3202017-05-08 18:22:22 +03002022 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002023 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002024 outputTriplet(out, visit, "(", " == ", ")");
2025 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002026 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002027 outputTriplet(out, visit, "(", " != ", ")");
2028 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002029 case EOpLessThanComponentWise:
2030 outputTriplet(out, visit, "(", " < ", ")");
2031 break;
2032 case EOpGreaterThanComponentWise:
2033 outputTriplet(out, visit, "(", " > ", ")");
2034 break;
2035 case EOpLessThanEqualComponentWise:
2036 outputTriplet(out, visit, "(", " <= ", ")");
2037 break;
2038 case EOpGreaterThanEqualComponentWise:
2039 outputTriplet(out, visit, "(", " >= ", ")");
2040 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002041 case EOpMod:
2042 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002043 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002044 break;
2045 case EOpModf:
2046 outputTriplet(out, visit, "modf(", ", ", ")");
2047 break;
2048 case EOpPow:
2049 outputTriplet(out, visit, "pow(", ", ", ")");
2050 break;
2051 case EOpAtan:
2052 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2053 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002054 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002055 break;
2056 case EOpMin:
2057 outputTriplet(out, visit, "min(", ", ", ")");
2058 break;
2059 case EOpMax:
2060 outputTriplet(out, visit, "max(", ", ", ")");
2061 break;
2062 case EOpClamp:
2063 outputTriplet(out, visit, "clamp(", ", ", ")");
2064 break;
2065 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302066 {
2067 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2068 if (lastParamNode->getType().getBasicType() == EbtBool)
2069 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002070 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2071 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302072 // so use emulated version.
2073 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002074 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302075 }
2076 else
2077 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002078 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302079 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002080 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302081 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002082 case EOpStep:
2083 outputTriplet(out, visit, "step(", ", ", ")");
2084 break;
2085 case EOpSmoothStep:
2086 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2087 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002088 case EOpFrexp:
2089 case EOpLdexp:
2090 ASSERT(node->getUseEmulatedFunction());
2091 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2092 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002093 case EOpDistance:
2094 outputTriplet(out, visit, "distance(", ", ", ")");
2095 break;
2096 case EOpDot:
2097 outputTriplet(out, visit, "dot(", ", ", ")");
2098 break;
2099 case EOpCross:
2100 outputTriplet(out, visit, "cross(", ", ", ")");
2101 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002102 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002103 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002104 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002105 break;
2106 case EOpReflect:
2107 outputTriplet(out, visit, "reflect(", ", ", ")");
2108 break;
2109 case EOpRefract:
2110 outputTriplet(out, visit, "refract(", ", ", ")");
2111 break;
2112 case EOpOuterProduct:
2113 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002114 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002115 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002116 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002117 outputTriplet(out, visit, "(", " * ", ")");
2118 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002119 case EOpBitfieldExtract:
2120 case EOpBitfieldInsert:
2121 case EOpUaddCarry:
2122 case EOpUsubBorrow:
2123 case EOpUmulExtended:
2124 case EOpImulExtended:
2125 ASSERT(node->getUseEmulatedFunction());
2126 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2127 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002128 default:
2129 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130 }
2131
2132 return true;
2133}
2134
Olli Etuaho57961272016-09-14 13:57:46 +03002135void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002137 out << "if (";
2138
2139 node->getCondition()->traverse(this);
2140
2141 out << ")\n";
2142
Jamie Madill8c46ab12015-12-07 16:39:19 -05002143 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002144
2145 bool discard = false;
2146
2147 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002149 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002150 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002151
Olli Etuahoa6f22092015-05-08 18:31:10 +03002152 // Detect true discard
2153 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2154 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002155 else
2156 {
2157 // TODO(oetuaho): Check if the semicolon inside is necessary.
2158 // It's there as a result of conservative refactoring of the output.
2159 out << "{;}\n";
2160 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002161
Jamie Madill8c46ab12015-12-07 16:39:19 -05002162 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002163
Olli Etuahoa6f22092015-05-08 18:31:10 +03002164 if (node->getFalseBlock())
2165 {
2166 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002167
Jamie Madill8c46ab12015-12-07 16:39:19 -05002168 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169
Olli Etuaho32db19b2016-10-04 14:43:16 +01002170 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002171 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002172
Jamie Madill8c46ab12015-12-07 16:39:19 -05002173 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002174
Olli Etuahoa6f22092015-05-08 18:31:10 +03002175 // Detect false discard
2176 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2177 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002178
Olli Etuahoa6f22092015-05-08 18:31:10 +03002179 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002180 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002181 {
2182 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002183 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002184}
2185
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002186bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2187{
2188 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2189 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2190 UNREACHABLE();
2191 return false;
2192}
2193
Olli Etuaho57961272016-09-14 13:57:46 +03002194bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002195{
2196 TInfoSinkBase &out = getInfoSink();
2197
Olli Etuaho3d932d82016-04-12 11:10:30 +03002198 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002199
2200 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002201 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002202 {
2203 out << "FLATTEN ";
2204 }
2205
Olli Etuaho57961272016-09-14 13:57:46 +03002206 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002207
2208 return false;
2209}
2210
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002211bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002212{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002213 TInfoSinkBase &out = getInfoSink();
2214
Olli Etuaho923ecef2017-10-11 12:01:38 +03002215 ASSERT(node->getStatementList());
2216 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002217 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002218 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002219 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002220 outputTriplet(out, visit, "switch (", ") ", "");
2221 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002222 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002223}
2224
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002225bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002226{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002227 TInfoSinkBase &out = getInfoSink();
2228
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002229 if (node->hasCondition())
2230 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002231 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002232 return true;
2233 }
2234 else
2235 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002236 out << "default:\n";
2237 return false;
2238 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002239}
2240
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2242{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002243 TInfoSinkBase &out = getInfoSink();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002244 writeConstantUnion(out, node->getType(), node->getConstantValue());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245}
2246
2247bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2248{
Nicolas Capens655fe362014-04-11 13:12:34 -04002249 mNestedLoopDepth++;
2250
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002251 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002252 mInsideDiscontinuousLoop =
2253 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002254
Jamie Madill8c46ab12015-12-07 16:39:19 -05002255 TInfoSinkBase &out = getInfoSink();
2256
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002257 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002258 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002259 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002260 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002261 mInsideDiscontinuousLoop = wasDiscontinuous;
2262 mNestedLoopDepth--;
2263
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002264 return false;
2265 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002266 }
2267
Corentin Wallez1239ee92015-03-19 14:38:02 -07002268 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002269 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002270 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002271 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002272
Jamie Madill8c46ab12015-12-07 16:39:19 -05002273 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 }
2275 else
2276 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002277 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002278
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 if (node->getInit())
2280 {
2281 node->getInit()->traverse(this);
2282 }
2283
2284 out << "; ";
2285
alokp@chromium.org52813552010-11-16 18:36:09 +00002286 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002288 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 }
2290
2291 out << "; ";
2292
alokp@chromium.org52813552010-11-16 18:36:09 +00002293 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002295 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 }
2297
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002298 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002299
Jamie Madill8c46ab12015-12-07 16:39:19 -05002300 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 }
2302
2303 if (node->getBody())
2304 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002305 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002306 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002308 else
2309 {
2310 // TODO(oetuaho): Check if the semicolon inside is necessary.
2311 // It's there as a result of conservative refactoring of the output.
2312 out << "{;}\n";
2313 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002314
Jamie Madill8c46ab12015-12-07 16:39:19 -05002315 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316
alokp@chromium.org52813552010-11-16 18:36:09 +00002317 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002319 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002320 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321
alokp@chromium.org52813552010-11-16 18:36:09 +00002322 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002324 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325 }
2326
daniel@transgaming.com73536982012-03-21 20:45:49 +00002327 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002329 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002330 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002331
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002332 return false;
2333}
2334
2335bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2336{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002337 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002339 TInfoSinkBase &out = getInfoSink();
2340
2341 switch (node->getFlowOp())
2342 {
2343 case EOpKill:
2344 out << "discard";
2345 break;
2346 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002347 if (mNestedLoopDepth > 1)
2348 {
2349 mUsesNestedBreak = true;
2350 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002351
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002352 if (mExcessiveLoopIndex)
2353 {
2354 out << "{Break";
2355 mExcessiveLoopIndex->traverse(this);
2356 out << " = true; break;}\n";
2357 }
2358 else
2359 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002360 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002361 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002362 break;
2363 case EOpContinue:
2364 out << "continue";
2365 break;
2366 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002367 if (node->getExpression())
2368 {
2369 out << "return ";
2370 }
2371 else
2372 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002373 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002374 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002375 break;
2376 default:
2377 UNREACHABLE();
2378 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379 }
2380
2381 return true;
2382}
2383
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002384// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002385// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2386// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002387bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002388{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002389 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002390
2391 // Parse loops of the form:
2392 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002393 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002394 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002395 int initial = 0;
2396 int limit = 0;
2397 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002398
2399 // Parse index name and intial value
2400 if (node->getInit())
2401 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002402 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002403
2404 if (init)
2405 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002406 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002407 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002408
2409 if (variable && variable->getQualifier() == EvqTemporary)
2410 {
2411 TIntermBinary *assign = variable->getAsBinaryNode();
2412
2413 if (assign->getOp() == EOpInitialize)
2414 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002415 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002416 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2417
2418 if (symbol && constant)
2419 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002420 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002422 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002423 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002424 }
2425 }
2426 }
2427 }
2428 }
2429 }
2430
2431 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002432 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002433 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002434 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002435
Olli Etuahob6af22b2017-12-15 14:05:44 +02002436 if (test && test->getLeft()->getAsSymbolNode()->uniqueId() == index->uniqueId())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002437 {
2438 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2439
2440 if (constant)
2441 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002442 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002443 {
2444 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002445 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002446 }
2447 }
2448 }
2449 }
2450
2451 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002452 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002453 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002454 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002455 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002456
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002457 if (binaryTerminal)
2458 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002459 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002460 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2461
2462 if (constant)
2463 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002464 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002465 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002466 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002467
2468 switch (op)
2469 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002470 case EOpAddAssign:
2471 increment = value;
2472 break;
2473 case EOpSubAssign:
2474 increment = -value;
2475 break;
2476 default:
2477 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002478 }
2479 }
2480 }
2481 }
2482 else if (unaryTerminal)
2483 {
2484 TOperator op = unaryTerminal->getOp();
2485
2486 switch (op)
2487 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002488 case EOpPostIncrement:
2489 increment = 1;
2490 break;
2491 case EOpPostDecrement:
2492 increment = -1;
2493 break;
2494 case EOpPreIncrement:
2495 increment = 1;
2496 break;
2497 case EOpPreDecrement:
2498 increment = -1;
2499 break;
2500 default:
2501 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002502 }
2503 }
2504 }
2505
Yunchao He4f285442017-04-21 12:15:49 +08002506 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002507 {
2508 if (comparator == EOpLessThanEqual)
2509 {
2510 comparator = EOpLessThan;
2511 limit += 1;
2512 }
2513
2514 if (comparator == EOpLessThan)
2515 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002516 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002517
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002518 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002519 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002520 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002521 }
2522
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002523 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002524 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002525
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002526 out << "{int ";
2527 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002528 out << ";\n"
2529 "bool Break";
2530 index->traverse(this);
2531 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002532
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002533 bool firstLoopFragment = true;
2534
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002535 while (iterations > 0)
2536 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002537 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002538
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002539 if (!firstLoopFragment)
2540 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002541 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002542 index->traverse(this);
2543 out << ") {\n";
2544 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002545
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002546 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002547 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002548 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002549 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002550
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002551 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002552 const char *unroll =
2553 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002554
Corentin Wallez1239ee92015-03-19 14:38:02 -07002555 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002556 index->traverse(this);
2557 out << " = ";
2558 out << initial;
2559
2560 out << "; ";
2561 index->traverse(this);
2562 out << " < ";
2563 out << clampedLimit;
2564
2565 out << "; ";
2566 index->traverse(this);
2567 out << " += ";
2568 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002569 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002570
Jamie Madill8c46ab12015-12-07 16:39:19 -05002571 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002572 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002573
2574 if (node->getBody())
2575 {
2576 node->getBody()->traverse(this);
2577 }
2578
Jamie Madill8c46ab12015-12-07 16:39:19 -05002579 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002580 out << ";}\n";
2581
2582 if (!firstLoopFragment)
2583 {
2584 out << "}\n";
2585 }
2586
2587 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002588
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002589 initial += MAX_LOOP_ITERATIONS * increment;
2590 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002591 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002592
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002593 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002594
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002595 mExcessiveLoopIndex = restoreIndex;
2596
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002597 return true;
2598 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002599 else
2600 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002601 }
2602
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002603 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002604}
2605
Jamie Madill8c46ab12015-12-07 16:39:19 -05002606void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2607 Visit visit,
2608 const char *preString,
2609 const char *inString,
2610 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002611{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002612 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002613 {
2614 out << preString;
2615 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002616 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002617 {
2618 out << inString;
2619 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002620 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002621 {
2622 out << postString;
2623 }
2624}
2625
Jamie Madill8c46ab12015-12-07 16:39:19 -05002626void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002627{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002628 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002629 {
Jamie Madill32aab012015-01-27 14:12:26 -05002630 out << "\n";
2631 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002632
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002633 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002634 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002635 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002636 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002637
Jamie Madill32aab012015-01-27 14:12:26 -05002638 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002639 }
2640}
2641
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002642TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2643{
2644 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002645 const TType &type = symbol->getType();
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002646 const TVariable &variable = symbol->variable();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002647 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002648
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002649 if (variable.symbolType() ==
2650 SymbolType::Empty) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002651 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002652 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002653 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002654 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002655 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002656 nameStr = DecorateVariableIfNeeded(variable);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002657 }
2658
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002659 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002660 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002661 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2662 {
2663 // Samplers are passed as indices to the sampler array.
2664 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2665 return "const uint " + nameStr + ArrayString(type);
2666 }
2667 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2668 {
2669 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2670 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2671 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2672 ArrayString(type);
2673 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002674 }
2675
Olli Etuaho96963162016-03-21 11:54:33 +02002676 TStringStream argString;
2677 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2678 << ArrayString(type);
2679
2680 // If the structure parameter contains samplers, they need to be passed into the function as
2681 // separate parameters. HLSL doesn't natively support samplers in structs.
2682 if (type.isStructureContainingSamplers())
2683 {
2684 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002685 TVector<const TVariable *> samplerSymbols;
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002686 type.createSamplerSymbols("angle" + nameStr, "", &samplerSymbols, nullptr, mSymbolTable);
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002687 for (const TVariable *sampler : samplerSymbols)
Olli Etuaho96963162016-03-21 11:54:33 +02002688 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002689 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002690 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2691 {
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002692 argString << ", const uint " << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002693 }
2694 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2695 {
Olli Etuaho96963162016-03-21 11:54:33 +02002696 ASSERT(IsSampler(samplerType.getBasicType()));
2697 argString << ", " << QualifierString(qualifier) << " "
2698 << TextureString(samplerType.getBasicType()) << " texture_"
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002699 << sampler->name() << ArrayString(samplerType) << ", "
Olli Etuaho28839f02017-08-15 11:38:16 +03002700 << QualifierString(qualifier) << " "
Olli Etuaho96963162016-03-21 11:54:33 +02002701 << SamplerString(samplerType.getBasicType()) << " sampler_"
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002702 << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002703 }
2704 else
2705 {
Olli Etuaho96963162016-03-21 11:54:33 +02002706 ASSERT(IsSampler(samplerType.getBasicType()));
2707 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
Olli Etuahobbd9d4c2017-12-21 12:02:00 +02002708 << " " << sampler->name() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002709 }
2710 }
2711 }
2712
2713 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714}
2715
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002716TString OutputHLSL::zeroInitializer(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002717{
2718 TString string;
2719
Jamie Madill94bf7f22013-07-08 13:31:15 -04002720 size_t size = type.getObjectSize();
2721 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002722 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002723 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002724
Jamie Madill94bf7f22013-07-08 13:31:15 -04002725 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002726 {
2727 string += ", ";
2728 }
2729 }
2730
daniel@transgaming.comead23042010-04-29 03:35:36 +00002731 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002732}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002733
Olli Etuahobd3cd502017-11-03 15:48:52 +02002734void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002735{
Olli Etuahobd3cd502017-11-03 15:48:52 +02002736 // Array constructors should have been already pruned from the code.
2737 ASSERT(!node->getType().isArray());
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002738
2739 if (visit == PreVisit)
2740 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002741 TString constructorName;
2742 if (node->getBasicType() == EbtStruct)
2743 {
2744 constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct());
2745 }
2746 else
2747 {
2748 constructorName =
2749 mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence());
2750 }
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002751 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002752 }
2753 else if (visit == InVisit)
2754 {
2755 out << ", ";
2756 }
2757 else if (visit == PostVisit)
2758 {
2759 out << ")";
2760 }
2761}
2762
Jamie Madill8c46ab12015-12-07 16:39:19 -05002763const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2764 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002765 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002766{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002767 ASSERT(!type.isArray());
2768
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002769 const TConstantUnion *constUnionIterated = constUnion;
2770
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002771 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002772 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002773 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002774 out << mStructureHLSL->addStructConstructor(*structure) << "(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002775
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002776 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002777
Jamie Madill98493dd2013-07-08 14:39:03 -04002778 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002779 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002780 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002781 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002782
Jamie Madill98493dd2013-07-08 14:39:03 -04002783 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002784 {
2785 out << ", ";
2786 }
2787 }
2788
2789 out << ")";
2790 }
2791 else
2792 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002793 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002794 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002795
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002796 if (writeType)
2797 {
Jamie Madill033dae62014-06-18 12:56:28 -04002798 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002799 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002800 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002801 if (writeType)
2802 {
2803 out << ")";
2804 }
2805 }
2806
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002807 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002808}
2809
Olli Etuahod68924e2017-01-02 17:34:40 +00002810void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002811{
Olli Etuahod68924e2017-01-02 17:34:40 +00002812 if (visit == PreVisit)
2813 {
2814 const char *opStr = GetOperatorString(op);
2815 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2816 out << "(";
2817 }
2818 else
2819 {
2820 outputTriplet(out, visit, nullptr, ", ", ")");
2821 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002822}
2823
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002824bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2825 TIntermSymbol *symbolNode,
2826 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002827{
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02002828 ASSERT(symbolNode->variable().symbolType() != SymbolType::Empty);
2829 const TIntermSymbol *symbolInInitializer = FindSymbolNode(expression, symbolNode->getName());
Jamie Madill37997142015-01-28 10:06:34 -05002830
Olli Etuaho4728bdc2017-12-20 17:51:08 +02002831 if (symbolInInitializer)
Jamie Madill37997142015-01-28 10:06:34 -05002832 {
2833 // Type already printed
2834 out << "t" + str(mUniqueIndex) + " = ";
2835 expression->traverse(this);
2836 out << ", ";
2837 symbolNode->traverse(this);
2838 out << " = t" + str(mUniqueIndex);
2839
2840 mUniqueIndex++;
2841 return true;
2842 }
2843
2844 return false;
2845}
2846
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002847bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2848 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002849 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002850{
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002851 if (initializer->hasConstantValue())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002852 {
2853 symbolNode->traverse(this);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002854 out << ArrayString(symbolNode->getType());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002855 out << " = {";
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002856 writeConstantUnionArray(out, initializer->getConstantValue(),
2857 initializer->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002858 out << "}";
2859 return true;
2860 }
2861 return false;
2862}
2863
Jamie Madill55e79e02015-02-09 15:35:00 -05002864TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2865{
2866 const TFieldList &fields = structure.fields();
2867
2868 for (const auto &eqFunction : mStructEqualityFunctions)
2869 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002870 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002871 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002872 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002873 }
2874 }
2875
2876 const TString &structNameString = StructNameString(structure);
2877
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002878 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002879 function->structure = &structure;
2880 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002881
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002882 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002883
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002884 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2885 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002886 << "{\n"
2887 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002888
2889 for (size_t i = 0; i < fields.size(); i++)
2890 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002891 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002892 const TType *fieldType = field->type();
2893
2894 const TString &fieldNameA = "a." + Decorate(field->name());
2895 const TString &fieldNameB = "b." + Decorate(field->name());
2896
2897 if (i > 0)
2898 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002899 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002900 }
2901
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002902 fnOut << "(";
2903 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2904 fnOut << fieldNameA;
2905 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2906 fnOut << fieldNameB;
2907 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2908 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002909 }
2910
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002911 fnOut << ";\n"
2912 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002913
2914 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002915
2916 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002917 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002918
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002919 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002920}
2921
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002922TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002923{
2924 for (const auto &eqFunction : mArrayEqualityFunctions)
2925 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002926 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002927 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002928 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002929 }
2930 }
2931
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002932 TType elementType(type);
2933 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002934
Olli Etuaho12690762015-03-31 12:55:28 +03002935 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002936 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002937
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002938 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002939
2940 TInfoSinkBase fnOut;
2941
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002942 const TString &typeName = TypeString(type);
2943 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2944 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002945 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002946 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002947 << type.getOutermostArraySize()
2948 << "; ++i)\n"
2949 " {\n"
2950 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002951
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002952 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002953 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002954 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002955 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002956 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002957
2958 fnOut << ") { return false; }\n"
2959 " }\n"
2960 " return true;\n"
2961 "}\n";
2962
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002963 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002964
2965 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002966 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002967
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002968 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002969}
2970
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002971TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002972{
2973 for (const auto &assignFunction : mArrayAssignmentFunctions)
2974 {
2975 if (assignFunction.type == type)
2976 {
2977 return assignFunction.functionName;
2978 }
2979 }
2980
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002981 TType elementType(type);
2982 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03002983
2984 ArrayHelperFunction function;
2985 function.type = type;
2986
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002987 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03002988
2989 TInfoSinkBase fnOut;
2990
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002991 const TString &typeName = TypeString(type);
2992 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
2993 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002994 << "{\n"
2995 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002996 << type.getOutermostArraySize()
2997 << "; ++i)\n"
2998 " {\n"
2999 " ";
3000
3001 outputAssign(PreVisit, elementType, fnOut);
3002 fnOut << "a[i]";
3003 outputAssign(InVisit, elementType, fnOut);
3004 fnOut << "b[i]";
3005 outputAssign(PostVisit, elementType, fnOut);
3006
3007 fnOut << ";\n"
3008 " }\n"
3009 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03003010
3011 function.functionDefinition = fnOut.c_str();
3012
3013 mArrayAssignmentFunctions.push_back(function);
3014
3015 return function.functionName;
3016}
3017
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003018TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003019{
3020 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3021 {
3022 if (constructIntoFunction.type == type)
3023 {
3024 return constructIntoFunction.functionName;
3025 }
3026 }
3027
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003028 TType elementType(type);
3029 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003030
3031 ArrayHelperFunction function;
3032 function.type = type;
3033
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003034 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003035
3036 TInfoSinkBase fnOut;
3037
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003038 const TString &typeName = TypeString(type);
3039 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3040 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003041 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003042 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003043 }
3044 fnOut << ")\n"
3045 "{\n";
3046
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003047 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003048 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003049 fnOut << " ";
3050 outputAssign(PreVisit, elementType, fnOut);
3051 fnOut << "a[" << i << "]";
3052 outputAssign(InVisit, elementType, fnOut);
3053 fnOut << "b" << i;
3054 outputAssign(PostVisit, elementType, fnOut);
3055 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003056 }
3057 fnOut << "}\n";
3058
3059 function.functionDefinition = fnOut.c_str();
3060
3061 mArrayConstructIntoFunctions.push_back(function);
3062
3063 return function.functionName;
3064}
3065
Jamie Madill2e295e22015-04-29 10:41:33 -04003066void OutputHLSL::ensureStructDefined(const TType &type)
3067{
Olli Etuahobd3cd502017-11-03 15:48:52 +02003068 const TStructure *structure = type.getStruct();
Jamie Madill2e295e22015-04-29 10:41:33 -04003069 if (structure)
3070 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02003071 ASSERT(type.getBasicType() == EbtStruct);
3072 mStructureHLSL->ensureStructDefined(*structure);
Jamie Madill2e295e22015-04-29 10:41:33 -04003073 }
3074}
3075
Jamie Madill45bcc782016-11-07 13:58:48 -05003076} // namespace sh