blob: d5ff761430e104fdaafe4903bb382419878efc08 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Xinghua Cao711b7a12017-10-09 13:38:12 +080018#include "compiler/translator/ImageFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/SearchSymbol.h"
23#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 Etuaho56a2f952016-12-08 12:16:27 +000085void OutputHLSL::writeFloat(TInfoSinkBase &out, float f)
Olli Etuaho4785fec2015-05-18 16:09:37 +030086{
Olli Etuaho56a2f952016-12-08 12:16:27 +000087 // This is known not to work for NaN on all drivers but make the best effort to output NaNs
88 // regardless.
89 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300 &&
90 mOutputType == SH_HLSL_4_1_OUTPUT)
91 {
92 out << "asfloat(" << gl::bitCast<uint32_t>(f) << "u)";
93 }
94 else
95 {
96 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
97 }
98}
Olli Etuaho4785fec2015-05-18 16:09:37 +030099
Olli Etuaho56a2f952016-12-08 12:16:27 +0000100void OutputHLSL::writeSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200101{
102 ASSERT(constUnion != nullptr);
103 switch (constUnion->getType())
104 {
105 case EbtFloat:
Olli Etuaho56a2f952016-12-08 12:16:27 +0000106 writeFloat(out, constUnion->getFConst());
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200107 break;
108 case EbtInt:
109 out << constUnion->getIConst();
110 break;
111 case EbtUInt:
112 out << constUnion->getUConst();
113 break;
114 case EbtBool:
115 out << constUnion->getBConst();
116 break;
117 default:
118 UNREACHABLE();
119 }
120}
121
Olli Etuaho56a2f952016-12-08 12:16:27 +0000122const TConstantUnion *OutputHLSL::writeConstantUnionArray(TInfoSinkBase &out,
123 const TConstantUnion *const constUnion,
124 const size_t size)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200125{
126 const TConstantUnion *constUnionIterated = constUnion;
127 for (size_t i = 0; i < size; i++, constUnionIterated++)
128 {
Olli Etuaho56a2f952016-12-08 12:16:27 +0000129 writeSingleConstant(out, constUnionIterated);
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200130
131 if (i != size - 1)
132 {
133 out << ", ";
134 }
135 }
136 return constUnionIterated;
137}
138
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800139OutputHLSL::OutputHLSL(sh::GLenum shaderType,
140 int shaderVersion,
141 const TExtensionBehavior &extensionBehavior,
142 const char *sourcePath,
143 ShShaderOutput outputType,
144 int numRenderTargets,
145 const std::vector<Uniform> &uniforms,
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300146 ShCompileOptions compileOptions,
Olli Etuaho89a69a02017-10-23 12:20:45 +0300147 TSymbolTable *symbolTable,
148 PerformanceDiagnostics *perfDiagnostics)
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300149 : TIntermTraverser(true, true, true, symbolTable),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200150 mShaderType(shaderType),
151 mShaderVersion(shaderVersion),
152 mExtensionBehavior(extensionBehavior),
153 mSourcePath(sourcePath),
154 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700155 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000156 mNumRenderTargets(numRenderTargets),
Olli Etuaho89a69a02017-10-23 12:20:45 +0300157 mCurrentFunctionMetadata(nullptr),
158 mPerfDiagnostics(perfDiagnostics)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000160 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000161
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500162 mUsesFragColor = false;
163 mUsesFragData = false;
164 mUsesDepthRange = false;
165 mUsesFragCoord = false;
166 mUsesPointCoord = false;
167 mUsesFrontFacing = false;
168 mUsesPointSize = false;
169 mUsesInstanceID = false;
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300170 mHasMultiviewExtensionEnabled =
171 IsExtensionEnabled(mExtensionBehavior, TExtension::OVR_multiview);
Martin Radev41ac68e2017-06-06 12:16:58 +0300172 mUsesViewID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500173 mUsesVertexID = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500174 mUsesFragDepth = false;
Xinghua Caob1239382016-12-13 15:07:05 +0800175 mUsesNumWorkGroups = false;
176 mUsesWorkGroupID = false;
177 mUsesLocalInvocationID = false;
178 mUsesGlobalInvocationID = false;
179 mUsesLocalInvocationIndex = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500180 mUsesXor = false;
181 mUsesDiscardRewriting = false;
182 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530183 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000184
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000185 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000186
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500187 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000188 mInsideDiscontinuousLoop = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500189 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000190
Yunchao Hed7297bf2017-04-19 15:27:10 +0800191 mExcessiveLoopIndex = nullptr;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000192
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500193 mStructureHLSL = new StructureHLSL;
Xinghua Cao711b7a12017-10-09 13:38:12 +0800194 mUniformHLSL = new UniformHLSL(shaderType, mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300195 mTextureFunctionHLSL = new TextureFunctionHLSL;
Xinghua Cao711b7a12017-10-09 13:38:12 +0800196 mImageFunctionHLSL = new ImageFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400197
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200198 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000199 {
Arun Patole63419392015-03-13 11:51:07 +0530200 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500201 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
202 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530203 // In both cases total 3 uniform registers need to be reserved.
204 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000205 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000206
Geoff Lang00140f42016-02-03 18:47:33 +0000207 // Reserve registers for the default uniform block and driver constants
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800208 mUniformHLSL->reserveUniformBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209}
210
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000211OutputHLSL::~OutputHLSL()
212{
Jamie Madill8daaba12014-06-13 10:04:33 -0400213 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400214 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300215 SafeDelete(mTextureFunctionHLSL);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800216 SafeDelete(mImageFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200217 for (auto &eqFunction : mStructEqualityFunctions)
218 {
219 SafeDelete(eqFunction);
220 }
221 for (auto &eqFunction : mArrayEqualityFunctions)
222 {
223 SafeDelete(eqFunction);
224 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000225}
226
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200227void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000228{
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200229 BuiltInFunctionEmulator builtInFunctionEmulator;
230 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800231 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
232 {
233 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
234 mShaderVersion);
235 }
236
Olli Etuahodfa75e82017-01-23 09:43:06 -0800237 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500238
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700239 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000240 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700241 ASSERT(success == CallDAG::INITDAG_SUCCESS);
242 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700243
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200244 const std::vector<MappedStruct> std140Structs = FlagStd140Structs(treeRoot);
245 // TODO(oetuaho): The std140Structs could be filtered based on which ones actually get used in
246 // the shader code. When we add shader storage blocks we might also consider an alternative
247 // solution, since the struct mapping won't work very well for shader storage blocks.
248
Jamie Madill37997142015-01-28 10:06:34 -0500249 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500250 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200251 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500252 mInfoSinkStack.pop();
253
Jamie Madill37997142015-01-28 10:06:34 -0500254 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500255 mInfoSinkStack.pop();
256
Jamie Madill32aab012015-01-27 14:12:26 -0500257 mInfoSinkStack.push(&mHeader);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200258 header(mHeader, std140Structs, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500259 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000260
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200261 objSink << mHeader.c_str();
262 objSink << mBody.c_str();
263 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200264
Olli Etuahodfa75e82017-01-23 09:43:06 -0800265 builtInFunctionEmulator.cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000266}
267
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800268const std::map<std::string, unsigned int> &OutputHLSL::getUniformBlockRegisterMap() const
Jamie Madill4e1fd412014-07-10 17:50:10 -0400269{
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800270 return mUniformHLSL->getUniformBlockRegisterMap();
Jamie Madill4e1fd412014-07-10 17:50:10 -0400271}
272
Jamie Madill9fe25e92014-07-18 10:33:08 -0400273const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
274{
275 return mUniformHLSL->getUniformRegisterMap();
276}
277
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200278TString OutputHLSL::structInitializerString(int indent,
279 const TType &type,
280 const TString &name) const
Jamie Madill570e04d2013-06-21 09:15:33 -0400281{
282 TString init;
283
Olli Etuahoed049ab2017-06-30 17:38:33 +0300284 TString indentString;
285 for (int spaces = 0; spaces < indent; spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300287 indentString += " ";
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 }
289
Olli Etuahoed049ab2017-06-30 17:38:33 +0300290 if (type.isArray())
Jamie Madill570e04d2013-06-21 09:15:33 -0400291 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300292 init += indentString + "{\n";
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300293 for (unsigned int arrayIndex = 0u; arrayIndex < type.getOutermostArraySize(); ++arrayIndex)
Jamie Madill570e04d2013-06-21 09:15:33 -0400294 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300295 TStringStream indexedString;
296 indexedString << name << "[" << arrayIndex << "]";
297 TType elementType = type;
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300298 elementType.toArrayElementType();
Olli Etuahoed049ab2017-06-30 17:38:33 +0300299 init += structInitializerString(indent + 1, elementType, indexedString.str());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300300 if (arrayIndex < type.getOutermostArraySize() - 1)
Olli Etuahoed049ab2017-06-30 17:38:33 +0300301 {
302 init += ",";
303 }
304 init += "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400305 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300306 init += indentString + "}";
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300308 else if (type.getBasicType() == EbtStruct)
309 {
310 init += indentString + "{\n";
311 const TStructure &structure = *type.getStruct();
312 const TFieldList &fields = structure.fields();
313 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
314 {
315 const TField &field = *fields[fieldIndex];
316 const TString &fieldName = name + "." + Decorate(field.name());
317 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400318
Olli Etuahoed049ab2017-06-30 17:38:33 +0300319 init += structInitializerString(indent + 1, fieldType, fieldName);
320 if (fieldIndex < fields.size() - 1)
321 {
322 init += ",";
323 }
324 init += "\n";
325 }
326 init += indentString + "}";
327 }
328 else
329 {
330 init += indentString + name;
331 }
Jamie Madill570e04d2013-06-21 09:15:33 -0400332
333 return init;
334}
335
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200336TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std140Structs) const
337{
338 TString mappedStructs;
339
340 for (auto &mappedStruct : std140Structs)
341 {
342 TInterfaceBlock *interfaceBlock =
343 mappedStruct.blockDeclarator->getType().getInterfaceBlock();
344 const TString &interfaceBlockName = interfaceBlock->name();
345 const TName &instanceName = mappedStruct.blockDeclarator->getName();
346 if (mReferencedUniformBlocks.count(interfaceBlockName) == 0 &&
347 (instanceName.getString() == "" ||
348 mReferencedUniformBlocks.count(instanceName.getString()) == 0))
349 {
350 continue;
351 }
352
353 unsigned int instanceCount = 1u;
354 bool isInstanceArray = mappedStruct.blockDeclarator->isArray();
355 if (isInstanceArray)
356 {
357 instanceCount = mappedStruct.blockDeclarator->getOutermostArraySize();
358 }
359
360 for (unsigned int instanceArrayIndex = 0; instanceArrayIndex < instanceCount;
361 ++instanceArrayIndex)
362 {
363 TString originalName;
364 TString mappedName("map");
365
366 if (instanceName.getString() != "")
367 {
368 unsigned int instanceStringArrayIndex = GL_INVALID_INDEX;
369 if (isInstanceArray)
370 instanceStringArrayIndex = instanceArrayIndex;
371 TString instanceString = mUniformHLSL->uniformBlockInstanceString(
372 *interfaceBlock, instanceStringArrayIndex);
373 originalName += instanceString;
374 mappedName += instanceString;
375 originalName += ".";
376 mappedName += "_";
377 }
378
379 TString fieldName = Decorate(mappedStruct.field->name());
380 originalName += fieldName;
381 mappedName += fieldName;
382
383 TType *structType = mappedStruct.field->type();
384 mappedStructs +=
385 "static " + Decorate(structType->getStruct()->name()) + " " + mappedName;
386
387 if (structType->isArray())
388 {
389 mappedStructs += ArrayString(*mappedStruct.field->type());
390 }
391
392 mappedStructs += " =\n";
393 mappedStructs += structInitializerString(0, *structType, originalName);
394 mappedStructs += ";\n";
395 }
396 }
397 return mappedStructs;
398}
399
400void OutputHLSL::header(TInfoSinkBase &out,
401 const std::vector<MappedStruct> &std140Structs,
402 const BuiltInFunctionEmulator *builtInFunctionEmulator) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000404 TString varyings;
405 TString attributes;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200406 TString mappedStructs = generateStructMapping(std140Structs);
Jamie Madill570e04d2013-06-21 09:15:33 -0400407
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500408 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin();
409 varying != mReferencedVaryings.end(); varying++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000410 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500411 const TType &type = varying->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000412 const TString &name = varying->second->getSymbol();
413
414 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500415 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
416 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000417 }
418
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500419 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin();
420 attribute != mReferencedAttributes.end(); attribute++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000421 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500422 const TType &type = attribute->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000423 const TString &name = attribute->second->getSymbol();
424
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500425 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
426 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000427 }
428
Jamie Madill8daaba12014-06-13 10:04:33 -0400429 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400430
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300431 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms, mSymbolTable);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800432 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400433
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200434 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500435 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200436 out << "\n// Equality functions\n\n";
437 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500438 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200439 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200440 }
441 }
Olli Etuaho12690762015-03-31 12:55:28 +0300442 if (!mArrayAssignmentFunctions.empty())
443 {
444 out << "\n// Assignment functions\n\n";
445 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
446 {
447 out << assignmentFunction.functionDefinition << "\n";
448 }
449 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300450 if (!mArrayConstructIntoFunctions.empty())
451 {
452 out << "\n// Array constructor functions\n\n";
453 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
454 {
455 out << constructIntoFunction.functionDefinition << "\n";
456 }
457 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200458
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500459 if (mUsesDiscardRewriting)
460 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400461 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500462 }
463
Nicolas Capens655fe362014-04-11 13:12:34 -0400464 if (mUsesNestedBreak)
465 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400466 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400467 }
468
Arun Patole44efa0b2015-03-04 17:11:05 +0530469 if (mRequiresIEEEStrictCompiling)
470 {
471 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
472 }
473
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400474 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
475 "#define LOOP [loop]\n"
476 "#define FLATTEN [flatten]\n"
477 "#else\n"
478 "#define LOOP\n"
479 "#define FLATTEN\n"
480 "#endif\n";
481
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200482 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300484 const bool usingMRTExtension =
485 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000486
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000487 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500488 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400489 out << "\n";
490
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200491 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000492 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500493 for (ReferencedSymbols::const_iterator outputVariableIt =
494 mReferencedOutputVariables.begin();
495 outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000496 {
Jamie Madill46131a32013-06-20 11:55:50 -0400497 const TString &variableName = outputVariableIt->first;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500498 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400499
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500500 out << "static " + TypeString(variableType) + " out_" + variableName +
501 ArrayString(variableType) + " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000502 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000503 }
Jamie Madill46131a32013-06-20 11:55:50 -0400504 else
505 {
506 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
507
508 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500509 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400510 for (unsigned int i = 0; i < numColorValues; i++)
511 {
512 out << " float4(0, 0, 0, 0)";
513 if (i + 1 != numColorValues)
514 {
515 out << ",";
516 }
517 out << "\n";
518 }
519
520 out << "};\n";
521 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000522
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400523 if (mUsesFragDepth)
524 {
525 out << "static float gl_Depth = 0.0;\n";
526 }
527
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000528 if (mUsesFragCoord)
529 {
530 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
531 }
532
533 if (mUsesPointCoord)
534 {
535 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
536 }
537
538 if (mUsesFrontFacing)
539 {
540 out << "static bool gl_FrontFacing = false;\n";
541 }
542
543 out << "\n";
544
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000545 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000546 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000547 out << "struct gl_DepthRangeParameters\n"
548 "{\n"
549 " float near;\n"
550 " float far;\n"
551 " float diff;\n"
552 "};\n"
553 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000554 }
555
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200556 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000557 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000558 out << "cbuffer DriverConstants : register(b1)\n"
559 "{\n";
560
561 if (mUsesDepthRange)
562 {
563 out << " float3 dx_DepthRange : packoffset(c0);\n";
564 }
565
566 if (mUsesFragCoord)
567 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000568 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000569 }
570
571 if (mUsesFragCoord || mUsesFrontFacing)
572 {
573 out << " float3 dx_DepthFront : packoffset(c2);\n";
574 }
575
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800576 if (mUsesFragCoord)
577 {
578 // dx_ViewScale is only used in the fragment shader to correct
579 // the value for glFragCoord if necessary
580 out << " float2 dx_ViewScale : packoffset(c3);\n";
581 }
582
Martin Radev72b4e1e2017-08-31 15:42:56 +0300583 if (mHasMultiviewExtensionEnabled)
584 {
585 // We have to add a value which we can use to keep track of which multi-view code
586 // path is to be selected in the GS.
587 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
588 }
589
Olli Etuaho618bebc2016-01-15 16:40:00 +0200590 if (mOutputType == SH_HLSL_4_1_OUTPUT)
591 {
592 mUniformHLSL->samplerMetadataUniforms(out, "c4");
593 }
594
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000595 out << "};\n";
596 }
597 else
598 {
599 if (mUsesDepthRange)
600 {
601 out << "uniform float3 dx_DepthRange : register(c0);";
602 }
603
604 if (mUsesFragCoord)
605 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000606 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000607 }
608
609 if (mUsesFragCoord || mUsesFrontFacing)
610 {
611 out << "uniform float3 dx_DepthFront : register(c2);\n";
612 }
613 }
614
615 out << "\n";
616
617 if (mUsesDepthRange)
618 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500619 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
620 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000621 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000622 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000623
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200624 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000625 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200626 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000627 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200628 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400629 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000630 }
631
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000632 if (usingMRTExtension && mNumRenderTargets > 1)
633 {
634 out << "#define GL_USES_MRT\n";
635 }
636
637 if (mUsesFragColor)
638 {
639 out << "#define GL_USES_FRAG_COLOR\n";
640 }
641
642 if (mUsesFragData)
643 {
644 out << "#define GL_USES_FRAG_DATA\n";
645 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646 }
Xinghua Caob1239382016-12-13 15:07:05 +0800647 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000649 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500650 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000651 out << "\n"
652 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400653
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000654 if (mUsesPointSize)
655 {
656 out << "static float gl_PointSize = float(1);\n";
657 }
658
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000659 if (mUsesInstanceID)
660 {
661 out << "static int gl_InstanceID;";
662 }
663
Corentin Wallezb076add2016-01-11 16:45:46 -0500664 if (mUsesVertexID)
665 {
666 out << "static int gl_VertexID;";
667 }
668
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000669 out << "\n"
670 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500671 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000672 out << "\n";
673
674 if (mUsesDepthRange)
675 {
676 out << "struct gl_DepthRangeParameters\n"
677 "{\n"
678 " float near;\n"
679 " float far;\n"
680 " float diff;\n"
681 "};\n"
682 "\n";
683 }
684
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200685 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000686 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800687 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500688 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800689
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000690 if (mUsesDepthRange)
691 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800692 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000693 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800694
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800695 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
696 // shaders. However, we declare it for all shaders (including Feature Level 10+).
697 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
698 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800699 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800700 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800701 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800702
Martin Radev72b4e1e2017-08-31 15:42:56 +0300703 if (mHasMultiviewExtensionEnabled)
704 {
705 // We have to add a value which we can use to keep track of which multi-view code
706 // path is to be selected in the GS.
707 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
708 }
709
Olli Etuaho618bebc2016-01-15 16:40:00 +0200710 if (mOutputType == SH_HLSL_4_1_OUTPUT)
711 {
712 mUniformHLSL->samplerMetadataUniforms(out, "c4");
713 }
714
Austin Kinross4fd18b12014-12-22 12:32:05 -0800715 out << "};\n"
716 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000717 }
718 else
719 {
720 if (mUsesDepthRange)
721 {
722 out << "uniform float3 dx_DepthRange : register(c0);\n";
723 }
724
Cooper Partine6664f02015-01-09 16:22:24 -0800725 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
726 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000727 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000728 }
729
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000730 if (mUsesDepthRange)
731 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500732 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
733 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000734 "\n";
735 }
736
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200737 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000738 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200739 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000740 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200741 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400742 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000743 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400744 }
Xinghua Caob1239382016-12-13 15:07:05 +0800745 else // Compute shader
746 {
747 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800748
749 out << "cbuffer DriverConstants : register(b1)\n"
750 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800751 if (mUsesNumWorkGroups)
752 {
Xinghua Caob1239382016-12-13 15:07:05 +0800753 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800754 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800755 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
756 mUniformHLSL->samplerMetadataUniforms(out, "c1");
757 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800758
759 // Follow built-in variables would be initialized in
760 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
761 // are used in compute shader.
762 if (mUsesWorkGroupID)
763 {
764 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
765 }
766
767 if (mUsesLocalInvocationID)
768 {
769 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
770 }
771
772 if (mUsesGlobalInvocationID)
773 {
774 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
775 }
776
777 if (mUsesLocalInvocationIndex)
778 {
779 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
780 }
781 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000782
Geoff Lang1fe74c72016-08-25 13:23:01 -0400783 bool getDimensionsIgnoresBaseLevel =
784 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
785 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800786 mImageFunctionHLSL->imageFunctionHeader(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000788 if (mUsesFragCoord)
789 {
790 out << "#define GL_USES_FRAG_COORD\n";
791 }
792
793 if (mUsesPointCoord)
794 {
795 out << "#define GL_USES_POINT_COORD\n";
796 }
797
798 if (mUsesFrontFacing)
799 {
800 out << "#define GL_USES_FRONT_FACING\n";
801 }
802
803 if (mUsesPointSize)
804 {
805 out << "#define GL_USES_POINT_SIZE\n";
806 }
807
Martin Radev41ac68e2017-06-06 12:16:58 +0300808 if (mHasMultiviewExtensionEnabled)
809 {
810 out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n";
811 }
812
813 if (mUsesViewID)
814 {
815 out << "#define GL_USES_VIEW_ID\n";
816 }
817
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400818 if (mUsesFragDepth)
819 {
820 out << "#define GL_USES_FRAG_DEPTH\n";
821 }
822
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000823 if (mUsesDepthRange)
824 {
825 out << "#define GL_USES_DEPTH_RANGE\n";
826 }
827
Xinghua Caob1239382016-12-13 15:07:05 +0800828 if (mUsesNumWorkGroups)
829 {
830 out << "#define GL_USES_NUM_WORK_GROUPS\n";
831 }
832
833 if (mUsesWorkGroupID)
834 {
835 out << "#define GL_USES_WORK_GROUP_ID\n";
836 }
837
838 if (mUsesLocalInvocationID)
839 {
840 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
841 }
842
843 if (mUsesGlobalInvocationID)
844 {
845 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
846 }
847
848 if (mUsesLocalInvocationIndex)
849 {
850 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
851 }
852
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000853 if (mUsesXor)
854 {
855 out << "bool xor(bool p, bool q)\n"
856 "{\n"
857 " return (p || q) && !(p && q);\n"
858 "}\n"
859 "\n";
860 }
861
Olli Etuahodfa75e82017-01-23 09:43:06 -0800862 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863}
864
865void OutputHLSL::visitSymbol(TIntermSymbol *node)
866{
Jamie Madill32aab012015-01-27 14:12:26 -0500867 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868
Jamie Madill570e04d2013-06-21 09:15:33 -0400869 // Handle accessing std140 structs by value
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200870 if (IsInStd140InterfaceBlock(node) && node->getBasicType() == EbtStruct)
Jamie Madill570e04d2013-06-21 09:15:33 -0400871 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200872 out << "map";
Jamie Madill570e04d2013-06-21 09:15:33 -0400873 }
874
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000875 TString name = node->getSymbol();
876
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000877 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000878 {
879 mUsesDepthRange = true;
880 out << name;
881 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882 else
883 {
Olli Etuahobd3cd502017-11-03 15:48:52 +0200884 const TType &nodeType = node->getType();
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000885 TQualifier qualifier = node->getQualifier();
886
Olli Etuahobd3cd502017-11-03 15:48:52 +0200887 ensureStructDefined(nodeType);
888
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000889 if (qualifier == EvqUniform)
890 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400891 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400892
893 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000894 {
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800895 mReferencedUniformBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000896 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000897 else
898 {
899 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000900 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400901
Olli Etuahoff526f12017-06-30 12:26:54 +0300902 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000903 }
Jamie Madill19571812013-08-12 15:26:34 -0700904 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000905 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000906 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400907 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000908 }
Jamie Madill033dae62014-06-18 12:56:28 -0400909 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000910 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000911 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400912 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300913 if (name == "ViewID_OVR")
914 {
915 mUsesViewID = true;
916 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000917 }
Jamie Madill19571812013-08-12 15:26:34 -0700918 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400919 {
920 mReferencedOutputVariables[name] = node;
921 out << "out_" << name;
922 }
923 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000924 {
925 out << "gl_Color[0]";
926 mUsesFragColor = true;
927 }
928 else if (qualifier == EvqFragData)
929 {
930 out << "gl_Color";
931 mUsesFragData = true;
932 }
933 else if (qualifier == EvqFragCoord)
934 {
935 mUsesFragCoord = true;
936 out << name;
937 }
938 else if (qualifier == EvqPointCoord)
939 {
940 mUsesPointCoord = true;
941 out << name;
942 }
943 else if (qualifier == EvqFrontFacing)
944 {
945 mUsesFrontFacing = true;
946 out << name;
947 }
948 else if (qualifier == EvqPointSize)
949 {
950 mUsesPointSize = true;
951 out << name;
952 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000953 else if (qualifier == EvqInstanceID)
954 {
955 mUsesInstanceID = true;
956 out << name;
957 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500958 else if (qualifier == EvqVertexID)
959 {
960 mUsesVertexID = true;
961 out << name;
962 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300963 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400964 {
965 mUsesFragDepth = true;
966 out << "gl_Depth";
967 }
Xinghua Caob1239382016-12-13 15:07:05 +0800968 else if (qualifier == EvqNumWorkGroups)
969 {
970 mUsesNumWorkGroups = true;
971 out << name;
972 }
973 else if (qualifier == EvqWorkGroupID)
974 {
975 mUsesWorkGroupID = true;
976 out << name;
977 }
978 else if (qualifier == EvqLocalInvocationID)
979 {
980 mUsesLocalInvocationID = true;
981 out << name;
982 }
983 else if (qualifier == EvqGlobalInvocationID)
984 {
985 mUsesGlobalInvocationID = true;
986 out << name;
987 }
988 else if (qualifier == EvqLocalInvocationIndex)
989 {
990 mUsesLocalInvocationIndex = true;
991 out << name;
992 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000993 else
994 {
Olli Etuahoff526f12017-06-30 12:26:54 +0300995 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000996 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997 }
998}
999
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001000void OutputHLSL::visitRaw(TIntermRaw *node)
1001{
Jamie Madill32aab012015-01-27 14:12:26 -05001002 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001003}
1004
Olli Etuaho7fb49552015-03-18 17:27:44 +02001005void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1006{
1007 if (type.isScalar() && !type.isArray())
1008 {
1009 if (op == EOpEqual)
1010 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001011 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001012 }
1013 else
1014 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001015 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001016 }
1017 }
1018 else
1019 {
1020 if (visit == PreVisit && op == EOpNotEqual)
1021 {
1022 out << "!";
1023 }
1024
1025 if (type.isArray())
1026 {
1027 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001028 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001029 }
1030 else if (type.getBasicType() == EbtStruct)
1031 {
1032 const TStructure &structure = *type.getStruct();
1033 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001034 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001035 }
1036 else
1037 {
1038 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001039 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001040 }
1041 }
1042}
1043
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001044void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1045{
1046 if (type.isArray())
1047 {
1048 const TString &functionName = addArrayAssignmentFunction(type);
1049 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1050 }
1051 else
1052 {
1053 outputTriplet(out, visit, "(", " = ", ")");
1054 }
1055}
1056
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001057bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001058{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001059 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001060 {
1061 TIntermNode *ancestor = getAncestorNode(n);
1062 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1063 if (ancestorBinary == nullptr)
1064 {
1065 return false;
1066 }
1067 switch (ancestorBinary->getOp())
1068 {
1069 case EOpIndexDirectStruct:
1070 {
1071 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1072 const TIntermConstantUnion *index =
1073 ancestorBinary->getRight()->getAsConstantUnion();
1074 const TField *field = structure->fields()[index->getIConst(0)];
1075 if (IsSampler(field->type()->getBasicType()))
1076 {
1077 return true;
1078 }
1079 break;
1080 }
1081 case EOpIndexDirect:
1082 break;
1083 default:
1084 // Returning a sampler from indirect indexing is not supported.
1085 return false;
1086 }
1087 }
1088 return false;
1089}
1090
Olli Etuahob6fa0432016-09-28 16:28:05 +01001091bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1092{
1093 TInfoSinkBase &out = getInfoSink();
1094 if (visit == PostVisit)
1095 {
1096 out << ".";
1097 node->writeOffsetsAsXYZW(&out);
1098 }
1099 return true;
1100}
1101
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1103{
Jamie Madill32aab012015-01-27 14:12:26 -05001104 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105
1106 switch (node->getOp())
1107 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001108 case EOpComma:
1109 outputTriplet(out, visit, "(", ", ", ")");
1110 break;
1111 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001112 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001113 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001114 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1115 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001116 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001117 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1118 out << functionName << "(";
1119 node->getLeft()->traverse(this);
1120 TIntermSequence *seq = rightAgg->getSequence();
1121 for (auto &arrayElement : *seq)
1122 {
1123 out << ", ";
1124 arrayElement->traverse(this);
1125 }
1126 out << ")";
1127 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001128 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001129 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1130 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001131 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001132 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001133 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001134 break;
1135 case EOpInitialize:
1136 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001137 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001138 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1139 ASSERT(symbolNode);
1140 TIntermTyped *expression = node->getRight();
1141
1142 // Global initializers must be constant at this point.
1143 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
1144 canWriteAsHLSLLiteral(expression));
1145
1146 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1147 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1148 // new variable is created before the assignment is evaluated), so we need to
1149 // convert
1150 // this to "float t = x, x = t;".
1151 if (writeSameSymbolInitializer(out, symbolNode, expression))
1152 {
1153 // Skip initializing the rest of the expression
1154 return false;
1155 }
1156 else if (writeConstantInitialization(out, symbolNode, expression))
1157 {
1158 return false;
1159 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001160 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001161 else if (visit == InVisit)
1162 {
1163 out << " = ";
1164 }
1165 break;
1166 case EOpAddAssign:
1167 outputTriplet(out, visit, "(", " += ", ")");
1168 break;
1169 case EOpSubAssign:
1170 outputTriplet(out, visit, "(", " -= ", ")");
1171 break;
1172 case EOpMulAssign:
1173 outputTriplet(out, visit, "(", " *= ", ")");
1174 break;
1175 case EOpVectorTimesScalarAssign:
1176 outputTriplet(out, visit, "(", " *= ", ")");
1177 break;
1178 case EOpMatrixTimesScalarAssign:
1179 outputTriplet(out, visit, "(", " *= ", ")");
1180 break;
1181 case EOpVectorTimesMatrixAssign:
1182 if (visit == PreVisit)
1183 {
1184 out << "(";
1185 }
1186 else if (visit == InVisit)
1187 {
1188 out << " = mul(";
1189 node->getLeft()->traverse(this);
1190 out << ", transpose(";
1191 }
1192 else
1193 {
1194 out << ")))";
1195 }
1196 break;
1197 case EOpMatrixTimesMatrixAssign:
1198 if (visit == PreVisit)
1199 {
1200 out << "(";
1201 }
1202 else if (visit == InVisit)
1203 {
1204 out << " = transpose(mul(transpose(";
1205 node->getLeft()->traverse(this);
1206 out << "), transpose(";
1207 }
1208 else
1209 {
1210 out << "))))";
1211 }
1212 break;
1213 case EOpDivAssign:
1214 outputTriplet(out, visit, "(", " /= ", ")");
1215 break;
1216 case EOpIModAssign:
1217 outputTriplet(out, visit, "(", " %= ", ")");
1218 break;
1219 case EOpBitShiftLeftAssign:
1220 outputTriplet(out, visit, "(", " <<= ", ")");
1221 break;
1222 case EOpBitShiftRightAssign:
1223 outputTriplet(out, visit, "(", " >>= ", ")");
1224 break;
1225 case EOpBitwiseAndAssign:
1226 outputTriplet(out, visit, "(", " &= ", ")");
1227 break;
1228 case EOpBitwiseXorAssign:
1229 outputTriplet(out, visit, "(", " ^= ", ")");
1230 break;
1231 case EOpBitwiseOrAssign:
1232 outputTriplet(out, visit, "(", " |= ", ")");
1233 break;
1234 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001235 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001236 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001237 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001238 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001239 if (visit == PreVisit)
1240 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001241 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001242 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001243 mReferencedUniformBlocks[interfaceBlock->instanceName()] =
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001244 node->getLeft()->getAsSymbolNode();
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001245 out << mUniformHLSL->uniformBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001246 return false;
1247 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001248 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001249 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001250 {
1251 // All parts of an expression that access a sampler in a struct need to use _ as
1252 // separator to access the sampler variable that has been moved out of the struct.
1253 outputTriplet(out, visit, "", "_", "");
1254 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001255 else
1256 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001257 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001258 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001259 }
1260 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001261 case EOpIndexIndirect:
1262 // We do not currently support indirect references to interface blocks
1263 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1264 outputTriplet(out, visit, "", "[", "]");
1265 break;
1266 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001267 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001268 const TStructure *structure = node->getLeft()->getType().getStruct();
1269 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1270 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001271
Olli Etuaho96963162016-03-21 11:54:33 +02001272 // In cases where indexing returns a sampler, we need to access the sampler variable
1273 // that has been moved out of the struct.
1274 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1275 if (visit == PreVisit && indexingReturnsSampler)
1276 {
1277 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1278 // This prefix is only output at the beginning of the indexing expression, which
1279 // may have multiple parts.
1280 out << "angle";
1281 }
1282 if (!indexingReturnsSampler)
1283 {
1284 // All parts of an expression that access a sampler in a struct need to use _ as
1285 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001286 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001287 }
1288 if (visit == InVisit)
1289 {
1290 if (indexingReturnsSampler)
1291 {
1292 out << "_" + field->name();
1293 }
1294 else
1295 {
1296 out << "." + DecorateField(field->name(), *structure);
1297 }
1298
1299 return false;
1300 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001301 }
1302 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001303 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001304 {
1305 bool structInStd140Block =
1306 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1307 if (visit == PreVisit && structInStd140Block)
1308 {
1309 out << "map";
1310 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001311 if (visit == InVisit)
1312 {
1313 const TInterfaceBlock *interfaceBlock =
1314 node->getLeft()->getType().getInterfaceBlock();
1315 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1316 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001317 if (structInStd140Block)
1318 {
1319 out << "_";
1320 }
1321 else
1322 {
1323 out << ".";
1324 }
1325 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001326
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001327 return false;
1328 }
1329 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001330 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001331 case EOpAdd:
1332 outputTriplet(out, visit, "(", " + ", ")");
1333 break;
1334 case EOpSub:
1335 outputTriplet(out, visit, "(", " - ", ")");
1336 break;
1337 case EOpMul:
1338 outputTriplet(out, visit, "(", " * ", ")");
1339 break;
1340 case EOpDiv:
1341 outputTriplet(out, visit, "(", " / ", ")");
1342 break;
1343 case EOpIMod:
1344 outputTriplet(out, visit, "(", " % ", ")");
1345 break;
1346 case EOpBitShiftLeft:
1347 outputTriplet(out, visit, "(", " << ", ")");
1348 break;
1349 case EOpBitShiftRight:
1350 outputTriplet(out, visit, "(", " >> ", ")");
1351 break;
1352 case EOpBitwiseAnd:
1353 outputTriplet(out, visit, "(", " & ", ")");
1354 break;
1355 case EOpBitwiseXor:
1356 outputTriplet(out, visit, "(", " ^ ", ")");
1357 break;
1358 case EOpBitwiseOr:
1359 outputTriplet(out, visit, "(", " | ", ")");
1360 break;
1361 case EOpEqual:
1362 case EOpNotEqual:
1363 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1364 break;
1365 case EOpLessThan:
1366 outputTriplet(out, visit, "(", " < ", ")");
1367 break;
1368 case EOpGreaterThan:
1369 outputTriplet(out, visit, "(", " > ", ")");
1370 break;
1371 case EOpLessThanEqual:
1372 outputTriplet(out, visit, "(", " <= ", ")");
1373 break;
1374 case EOpGreaterThanEqual:
1375 outputTriplet(out, visit, "(", " >= ", ")");
1376 break;
1377 case EOpVectorTimesScalar:
1378 outputTriplet(out, visit, "(", " * ", ")");
1379 break;
1380 case EOpMatrixTimesScalar:
1381 outputTriplet(out, visit, "(", " * ", ")");
1382 break;
1383 case EOpVectorTimesMatrix:
1384 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1385 break;
1386 case EOpMatrixTimesVector:
1387 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1388 break;
1389 case EOpMatrixTimesMatrix:
1390 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1391 break;
1392 case EOpLogicalOr:
1393 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1394 // been unfolded.
1395 ASSERT(!node->getRight()->hasSideEffects());
1396 outputTriplet(out, visit, "(", " || ", ")");
1397 return true;
1398 case EOpLogicalXor:
1399 mUsesXor = true;
1400 outputTriplet(out, visit, "xor(", ", ", ")");
1401 break;
1402 case EOpLogicalAnd:
1403 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1404 // been unfolded.
1405 ASSERT(!node->getRight()->hasSideEffects());
1406 outputTriplet(out, visit, "(", " && ", ")");
1407 return true;
1408 default:
1409 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001410 }
1411
1412 return true;
1413}
1414
1415bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1416{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001417 TInfoSinkBase &out = getInfoSink();
1418
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001419 switch (node->getOp())
1420 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001421 case EOpNegative:
1422 outputTriplet(out, visit, "(-", "", ")");
1423 break;
1424 case EOpPositive:
1425 outputTriplet(out, visit, "(+", "", ")");
1426 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001427 case EOpLogicalNot:
1428 outputTriplet(out, visit, "(!", "", ")");
1429 break;
1430 case EOpBitwiseNot:
1431 outputTriplet(out, visit, "(~", "", ")");
1432 break;
1433 case EOpPostIncrement:
1434 outputTriplet(out, visit, "(", "", "++)");
1435 break;
1436 case EOpPostDecrement:
1437 outputTriplet(out, visit, "(", "", "--)");
1438 break;
1439 case EOpPreIncrement:
1440 outputTriplet(out, visit, "(++", "", ")");
1441 break;
1442 case EOpPreDecrement:
1443 outputTriplet(out, visit, "(--", "", ")");
1444 break;
1445 case EOpRadians:
1446 outputTriplet(out, visit, "radians(", "", ")");
1447 break;
1448 case EOpDegrees:
1449 outputTriplet(out, visit, "degrees(", "", ")");
1450 break;
1451 case EOpSin:
1452 outputTriplet(out, visit, "sin(", "", ")");
1453 break;
1454 case EOpCos:
1455 outputTriplet(out, visit, "cos(", "", ")");
1456 break;
1457 case EOpTan:
1458 outputTriplet(out, visit, "tan(", "", ")");
1459 break;
1460 case EOpAsin:
1461 outputTriplet(out, visit, "asin(", "", ")");
1462 break;
1463 case EOpAcos:
1464 outputTriplet(out, visit, "acos(", "", ")");
1465 break;
1466 case EOpAtan:
1467 outputTriplet(out, visit, "atan(", "", ")");
1468 break;
1469 case EOpSinh:
1470 outputTriplet(out, visit, "sinh(", "", ")");
1471 break;
1472 case EOpCosh:
1473 outputTriplet(out, visit, "cosh(", "", ")");
1474 break;
1475 case EOpTanh:
1476 outputTriplet(out, visit, "tanh(", "", ")");
1477 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001478 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001479 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001480 case EOpAtanh:
1481 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001482 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001483 break;
1484 case EOpExp:
1485 outputTriplet(out, visit, "exp(", "", ")");
1486 break;
1487 case EOpLog:
1488 outputTriplet(out, visit, "log(", "", ")");
1489 break;
1490 case EOpExp2:
1491 outputTriplet(out, visit, "exp2(", "", ")");
1492 break;
1493 case EOpLog2:
1494 outputTriplet(out, visit, "log2(", "", ")");
1495 break;
1496 case EOpSqrt:
1497 outputTriplet(out, visit, "sqrt(", "", ")");
1498 break;
1499 case EOpInverseSqrt:
1500 outputTriplet(out, visit, "rsqrt(", "", ")");
1501 break;
1502 case EOpAbs:
1503 outputTriplet(out, visit, "abs(", "", ")");
1504 break;
1505 case EOpSign:
1506 outputTriplet(out, visit, "sign(", "", ")");
1507 break;
1508 case EOpFloor:
1509 outputTriplet(out, visit, "floor(", "", ")");
1510 break;
1511 case EOpTrunc:
1512 outputTriplet(out, visit, "trunc(", "", ")");
1513 break;
1514 case EOpRound:
1515 outputTriplet(out, visit, "round(", "", ")");
1516 break;
1517 case EOpRoundEven:
1518 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001519 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001520 break;
1521 case EOpCeil:
1522 outputTriplet(out, visit, "ceil(", "", ")");
1523 break;
1524 case EOpFract:
1525 outputTriplet(out, visit, "frac(", "", ")");
1526 break;
1527 case EOpIsNan:
1528 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001529 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001530 else
1531 outputTriplet(out, visit, "isnan(", "", ")");
1532 mRequiresIEEEStrictCompiling = true;
1533 break;
1534 case EOpIsInf:
1535 outputTriplet(out, visit, "isinf(", "", ")");
1536 break;
1537 case EOpFloatBitsToInt:
1538 outputTriplet(out, visit, "asint(", "", ")");
1539 break;
1540 case EOpFloatBitsToUint:
1541 outputTriplet(out, visit, "asuint(", "", ")");
1542 break;
1543 case EOpIntBitsToFloat:
1544 outputTriplet(out, visit, "asfloat(", "", ")");
1545 break;
1546 case EOpUintBitsToFloat:
1547 outputTriplet(out, visit, "asfloat(", "", ")");
1548 break;
1549 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001550 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001551 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001552 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001553 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001554 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001555 case EOpPackUnorm4x8:
1556 case EOpPackSnorm4x8:
1557 case EOpUnpackUnorm4x8:
1558 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001559 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001560 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001561 break;
1562 case EOpLength:
1563 outputTriplet(out, visit, "length(", "", ")");
1564 break;
1565 case EOpNormalize:
1566 outputTriplet(out, visit, "normalize(", "", ")");
1567 break;
1568 case EOpDFdx:
1569 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1570 {
1571 outputTriplet(out, visit, "(", "", ", 0.0)");
1572 }
1573 else
1574 {
1575 outputTriplet(out, visit, "ddx(", "", ")");
1576 }
1577 break;
1578 case EOpDFdy:
1579 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1580 {
1581 outputTriplet(out, visit, "(", "", ", 0.0)");
1582 }
1583 else
1584 {
1585 outputTriplet(out, visit, "ddy(", "", ")");
1586 }
1587 break;
1588 case EOpFwidth:
1589 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1590 {
1591 outputTriplet(out, visit, "(", "", ", 0.0)");
1592 }
1593 else
1594 {
1595 outputTriplet(out, visit, "fwidth(", "", ")");
1596 }
1597 break;
1598 case EOpTranspose:
1599 outputTriplet(out, visit, "transpose(", "", ")");
1600 break;
1601 case EOpDeterminant:
1602 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1603 break;
1604 case EOpInverse:
1605 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001606 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001607 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001608
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001609 case EOpAny:
1610 outputTriplet(out, visit, "any(", "", ")");
1611 break;
1612 case EOpAll:
1613 outputTriplet(out, visit, "all(", "", ")");
1614 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001615 case EOpLogicalNotComponentWise:
1616 outputTriplet(out, visit, "(!", "", ")");
1617 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001618 case EOpBitfieldReverse:
1619 outputTriplet(out, visit, "reversebits(", "", ")");
1620 break;
1621 case EOpBitCount:
1622 outputTriplet(out, visit, "countbits(", "", ")");
1623 break;
1624 case EOpFindLSB:
1625 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1626 // in GLSLTest and results are consistent with GL.
1627 outputTriplet(out, visit, "firstbitlow(", "", ")");
1628 break;
1629 case EOpFindMSB:
1630 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1631 // tested in GLSLTest and results are consistent with GL.
1632 outputTriplet(out, visit, "firstbithigh(", "", ")");
1633 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001634 default:
1635 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001636 }
1637
1638 return true;
1639}
1640
Olli Etuaho96963162016-03-21 11:54:33 +02001641TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1642{
1643 if (node->getAsSymbolNode())
1644 {
1645 return node->getAsSymbolNode()->getSymbol();
1646 }
1647 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1648 switch (nodeBinary->getOp())
1649 {
1650 case EOpIndexDirect:
1651 {
1652 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1653
1654 TInfoSinkBase prefixSink;
1655 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1656 return TString(prefixSink.c_str());
1657 }
1658 case EOpIndexDirectStruct:
1659 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001660 const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001661 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1662 const TField *field = s->fields()[index];
1663
1664 TInfoSinkBase prefixSink;
1665 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1666 << field->name();
1667 return TString(prefixSink.c_str());
1668 }
1669 default:
1670 UNREACHABLE();
1671 return TString("");
1672 }
1673}
1674
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001675bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1676{
1677 TInfoSinkBase &out = getInfoSink();
1678
1679 if (mInsideFunction)
1680 {
1681 outputLineDirective(out, node->getLine().first_line);
1682 out << "{\n";
1683 }
1684
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001685 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001686 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001687 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001688
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001689 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001690
1691 // Don't output ; after case labels, they're terminated by :
1692 // This is needed especially since outputting a ; after a case statement would turn empty
1693 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001694 // Also the output code is clearer if we don't output ; after statements where it is not
1695 // needed:
1696 // * if statements
1697 // * switch statements
1698 // * blocks
1699 // * function definitions
1700 // * loops (do-while loops output the semicolon in VisitLoop)
1701 // * declarations that don't generate output.
1702 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1703 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1704 statement->getAsSwitchNode() == nullptr &&
1705 statement->getAsFunctionDefinition() == nullptr &&
1706 (statement->getAsDeclarationNode() == nullptr ||
1707 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1708 statement->getAsInvariantDeclarationNode() == nullptr)
1709 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001710 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001711 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001712 }
1713
1714 if (mInsideFunction)
1715 {
1716 outputLineDirective(out, node->getLine().last_line);
1717 out << "}\n";
1718 }
1719
1720 return false;
1721}
1722
Olli Etuaho336b1472016-10-05 16:37:55 +01001723bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1724{
1725 TInfoSinkBase &out = getInfoSink();
1726
1727 ASSERT(mCurrentFunctionMetadata == nullptr);
1728
1729 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1730 ASSERT(index != CallDAG::InvalidIndex);
1731 mCurrentFunctionMetadata = &mASTMetadataList[index];
1732
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001733 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001734
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001735 TIntermSequence *parameters = node->getFunctionPrototype()->getSequence();
Olli Etuaho336b1472016-10-05 16:37:55 +01001736
1737 if (node->getFunctionSymbolInfo()->isMain())
1738 {
1739 out << "gl_main(";
1740 }
1741 else
1742 {
Olli Etuahoff526f12017-06-30 12:26:54 +03001743 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
Olli Etuaho336b1472016-10-05 16:37:55 +01001744 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1745 }
1746
1747 for (unsigned int i = 0; i < parameters->size(); i++)
1748 {
1749 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1750
1751 if (symbol)
1752 {
1753 ensureStructDefined(symbol->getType());
1754
1755 out << argumentString(symbol);
1756
1757 if (i < parameters->size() - 1)
1758 {
1759 out << ", ";
1760 }
1761 }
1762 else
1763 UNREACHABLE();
1764 }
1765
1766 out << ")\n";
1767
1768 mInsideFunction = true;
1769 // The function body node will output braces.
1770 node->getBody()->traverse(this);
1771 mInsideFunction = false;
1772
1773 mCurrentFunctionMetadata = nullptr;
1774
1775 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1776 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1777 {
1778 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1779 mOutputLod0Function = true;
1780 node->traverse(this);
1781 mOutputLod0Function = false;
1782 }
1783
1784 return false;
1785}
1786
Olli Etuaho13389b62016-10-16 11:48:18 +01001787bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1788{
Olli Etuaho13389b62016-10-16 11:48:18 +01001789 if (visit == PreVisit)
1790 {
1791 TIntermSequence *sequence = node->getSequence();
1792 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1793 ASSERT(sequence->size() == 1);
Olli Etuaho282847e2017-07-12 14:11:01 +03001794 ASSERT(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01001795
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001796 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001797 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001798 TInfoSinkBase &out = getInfoSink();
Olli Etuaho13389b62016-10-16 11:48:18 +01001799 ensureStructDefined(variable->getType());
1800
1801 if (!variable->getAsSymbolNode() ||
1802 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1803 {
1804 if (!mInsideFunction)
1805 {
1806 out << "static ";
1807 }
1808
1809 out << TypeString(variable->getType()) + " ";
1810
1811 TIntermSymbol *symbol = variable->getAsSymbolNode();
1812
1813 if (symbol)
1814 {
1815 symbol->traverse(this);
1816 out << ArrayString(symbol->getType());
1817 out << " = " + initializer(symbol->getType());
1818 }
1819 else
1820 {
1821 variable->traverse(this);
1822 }
1823 }
1824 else if (variable->getAsSymbolNode() &&
1825 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1826 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001827 ASSERT(variable->getBasicType() == EbtStruct);
1828 // ensureStructDefined has already been called.
Olli Etuaho13389b62016-10-16 11:48:18 +01001829 }
1830 else
1831 UNREACHABLE();
1832 }
Olli Etuaho282847e2017-07-12 14:11:01 +03001833 else if (IsVaryingOut(variable->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001834 {
Olli Etuaho282847e2017-07-12 14:11:01 +03001835 TIntermSymbol *symbol = variable->getAsSymbolNode();
1836 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001837
Olli Etuaho282847e2017-07-12 14:11:01 +03001838 // Vertex outputs which are declared but not written to should still be declared to
1839 // allow successful linking.
1840 mReferencedVaryings[symbol->getSymbol()] = symbol;
Olli Etuaho13389b62016-10-16 11:48:18 +01001841 }
1842 }
1843 return false;
1844}
1845
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001846bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1847{
1848 // Do not do any translation
1849 return false;
1850}
1851
Olli Etuaho16c745a2017-01-16 17:02:27 +00001852bool OutputHLSL::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
1853{
1854 TInfoSinkBase &out = getInfoSink();
1855
1856 ASSERT(visit == PreVisit);
1857 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1858 // Skip the prototype if it is not implemented (and thus not used)
1859 if (index == CallDAG::InvalidIndex)
1860 {
1861 return false;
1862 }
1863
1864 TIntermSequence *arguments = node->getSequence();
1865
Olli Etuahoff526f12017-06-30 12:26:54 +03001866 TString name = DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001867 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1868 << (mOutputLod0Function ? "Lod0(" : "(");
1869
1870 for (unsigned int i = 0; i < arguments->size(); i++)
1871 {
1872 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1873 ASSERT(symbol != nullptr);
1874
1875 out << argumentString(symbol);
1876
1877 if (i < arguments->size() - 1)
1878 {
1879 out << ", ";
1880 }
1881 }
1882
1883 out << ");\n";
1884
1885 // Also prototype the Lod0 variant if needed
1886 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1887 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1888 {
1889 mOutputLod0Function = true;
1890 node->traverse(this);
1891 mOutputLod0Function = false;
1892 }
1893
1894 return false;
1895}
1896
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1898{
Jamie Madill32aab012015-01-27 14:12:26 -05001899 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001900
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001901 switch (node->getOp())
1902 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001903 case EOpCallBuiltInFunction:
1904 case EOpCallFunctionInAST:
1905 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001907 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001908
Corentin Wallez1239ee92015-03-19 14:38:02 -07001909 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001910 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001912 if (node->isArray())
1913 {
1914 UNIMPLEMENTED();
1915 }
Olli Etuahobd674552016-10-06 13:28:42 +01001916 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001917 ASSERT(index != CallDAG::InvalidIndex);
1918 lod0 &= mASTMetadataList[index].mNeedsLod0;
1919
Olli Etuahoff526f12017-06-30 12:26:54 +03001920 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001921 out << DisambiguateFunctionName(node->getSequence());
1922 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001924 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001925 {
1926 // This path is used for internal functions that don't have their definitions in the
1927 // AST, such as precision emulation functions.
Olli Etuahoff526f12017-06-30 12:26:54 +03001928 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001929 }
Xinghua Cao711b7a12017-10-09 13:38:12 +08001930 else if (node->getFunctionSymbolInfo()->isImageFunction())
1931 {
1932 TString name = node->getFunctionSymbolInfo()->getName();
1933 TType type = (*arguments)[0]->getAsTyped()->getType();
1934 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
1935 name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
1936 type.getMemoryQualifier().readonly);
1937 out << imageFunctionName << "(";
1938 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 else
1940 {
Olli Etuahoec9232b2017-03-27 17:01:37 +03001941 const TString &name = node->getFunctionSymbolInfo()->getName();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001942 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001943 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1944 if (arguments->size() > 1)
1945 {
1946 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1947 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001948 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1949 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1950 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001952
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001953 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001954 {
Olli Etuaho96963162016-03-21 11:54:33 +02001955 TIntermTyped *typedArg = (*arg)->getAsTyped();
1956 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001957 {
1958 out << "texture_";
1959 (*arg)->traverse(this);
1960 out << ", sampler_";
1961 }
1962
1963 (*arg)->traverse(this);
1964
Olli Etuaho96963162016-03-21 11:54:33 +02001965 if (typedArg->getType().isStructureContainingSamplers())
1966 {
1967 const TType &argType = typedArg->getType();
1968 TVector<TIntermSymbol *> samplerSymbols;
1969 TString structName = samplerNamePrefixFromStruct(typedArg);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001970 argType.createSamplerSymbols("angle_" + structName, "", &samplerSymbols,
1971 nullptr, mSymbolTable);
Olli Etuaho96963162016-03-21 11:54:33 +02001972 for (const TIntermSymbol *sampler : samplerSymbols)
1973 {
1974 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1975 {
1976 out << ", texture_" << sampler->getSymbol();
1977 out << ", sampler_" << sampler->getSymbol();
1978 }
1979 else
1980 {
1981 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1982 // of D3D9, it's the sampler variable.
1983 out << ", " + sampler->getSymbol();
1984 }
1985 }
1986 }
1987
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001988 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001989 {
1990 out << ", ";
1991 }
1992 }
1993
1994 out << ")";
1995
1996 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03001998 case EOpConstruct:
Olli Etuahobd3cd502017-11-03 15:48:52 +02001999 outputConstructor(out, visit, node);
Olli Etuaho8fab3202017-05-08 18:22:22 +03002000 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002001 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002002 outputTriplet(out, visit, "(", " == ", ")");
2003 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002004 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002005 outputTriplet(out, visit, "(", " != ", ")");
2006 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002007 case EOpLessThanComponentWise:
2008 outputTriplet(out, visit, "(", " < ", ")");
2009 break;
2010 case EOpGreaterThanComponentWise:
2011 outputTriplet(out, visit, "(", " > ", ")");
2012 break;
2013 case EOpLessThanEqualComponentWise:
2014 outputTriplet(out, visit, "(", " <= ", ")");
2015 break;
2016 case EOpGreaterThanEqualComponentWise:
2017 outputTriplet(out, visit, "(", " >= ", ")");
2018 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002019 case EOpMod:
2020 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002021 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002022 break;
2023 case EOpModf:
2024 outputTriplet(out, visit, "modf(", ", ", ")");
2025 break;
2026 case EOpPow:
2027 outputTriplet(out, visit, "pow(", ", ", ")");
2028 break;
2029 case EOpAtan:
2030 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2031 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002032 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002033 break;
2034 case EOpMin:
2035 outputTriplet(out, visit, "min(", ", ", ")");
2036 break;
2037 case EOpMax:
2038 outputTriplet(out, visit, "max(", ", ", ")");
2039 break;
2040 case EOpClamp:
2041 outputTriplet(out, visit, "clamp(", ", ", ")");
2042 break;
2043 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302044 {
2045 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2046 if (lastParamNode->getType().getBasicType() == EbtBool)
2047 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002048 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2049 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302050 // so use emulated version.
2051 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002052 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302053 }
2054 else
2055 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002056 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302057 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002058 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302059 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002060 case EOpStep:
2061 outputTriplet(out, visit, "step(", ", ", ")");
2062 break;
2063 case EOpSmoothStep:
2064 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2065 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002066 case EOpFrexp:
2067 case EOpLdexp:
2068 ASSERT(node->getUseEmulatedFunction());
2069 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2070 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002071 case EOpDistance:
2072 outputTriplet(out, visit, "distance(", ", ", ")");
2073 break;
2074 case EOpDot:
2075 outputTriplet(out, visit, "dot(", ", ", ")");
2076 break;
2077 case EOpCross:
2078 outputTriplet(out, visit, "cross(", ", ", ")");
2079 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002080 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002081 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002082 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002083 break;
2084 case EOpReflect:
2085 outputTriplet(out, visit, "reflect(", ", ", ")");
2086 break;
2087 case EOpRefract:
2088 outputTriplet(out, visit, "refract(", ", ", ")");
2089 break;
2090 case EOpOuterProduct:
2091 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002092 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002093 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002094 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002095 outputTriplet(out, visit, "(", " * ", ")");
2096 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002097 case EOpBitfieldExtract:
2098 case EOpBitfieldInsert:
2099 case EOpUaddCarry:
2100 case EOpUsubBorrow:
2101 case EOpUmulExtended:
2102 case EOpImulExtended:
2103 ASSERT(node->getUseEmulatedFunction());
2104 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2105 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002106 default:
2107 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 }
2109
2110 return true;
2111}
2112
Olli Etuaho57961272016-09-14 13:57:46 +03002113void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002115 out << "if (";
2116
2117 node->getCondition()->traverse(this);
2118
2119 out << ")\n";
2120
Jamie Madill8c46ab12015-12-07 16:39:19 -05002121 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002122
2123 bool discard = false;
2124
2125 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002127 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002128 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002129
Olli Etuahoa6f22092015-05-08 18:31:10 +03002130 // Detect true discard
2131 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2132 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002133 else
2134 {
2135 // TODO(oetuaho): Check if the semicolon inside is necessary.
2136 // It's there as a result of conservative refactoring of the output.
2137 out << "{;}\n";
2138 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002139
Jamie Madill8c46ab12015-12-07 16:39:19 -05002140 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002141
Olli Etuahoa6f22092015-05-08 18:31:10 +03002142 if (node->getFalseBlock())
2143 {
2144 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002145
Jamie Madill8c46ab12015-12-07 16:39:19 -05002146 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147
Olli Etuaho32db19b2016-10-04 14:43:16 +01002148 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002149 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002150
Jamie Madill8c46ab12015-12-07 16:39:19 -05002151 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002152
Olli Etuahoa6f22092015-05-08 18:31:10 +03002153 // Detect false discard
2154 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2155 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002156
Olli Etuahoa6f22092015-05-08 18:31:10 +03002157 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002158 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002159 {
2160 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002161 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002162}
2163
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002164bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2165{
2166 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2167 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2168 UNREACHABLE();
2169 return false;
2170}
2171
Olli Etuaho57961272016-09-14 13:57:46 +03002172bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002173{
2174 TInfoSinkBase &out = getInfoSink();
2175
Olli Etuaho3d932d82016-04-12 11:10:30 +03002176 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002177
2178 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002179 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002180 {
2181 out << "FLATTEN ";
2182 }
2183
Olli Etuaho57961272016-09-14 13:57:46 +03002184 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185
2186 return false;
2187}
2188
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002189bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002190{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002191 TInfoSinkBase &out = getInfoSink();
2192
Olli Etuaho923ecef2017-10-11 12:01:38 +03002193 ASSERT(node->getStatementList());
2194 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002195 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002196 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002197 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002198 outputTriplet(out, visit, "switch (", ") ", "");
2199 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002200 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002201}
2202
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002203bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002204{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002205 TInfoSinkBase &out = getInfoSink();
2206
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002207 if (node->hasCondition())
2208 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002209 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002210 return true;
2211 }
2212 else
2213 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002214 out << "default:\n";
2215 return false;
2216 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002217}
2218
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002219void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2220{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002221 TInfoSinkBase &out = getInfoSink();
2222 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223}
2224
2225bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2226{
Nicolas Capens655fe362014-04-11 13:12:34 -04002227 mNestedLoopDepth++;
2228
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002229 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002230 mInsideDiscontinuousLoop =
2231 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002232
Jamie Madill8c46ab12015-12-07 16:39:19 -05002233 TInfoSinkBase &out = getInfoSink();
2234
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002235 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002236 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002237 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002238 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002239 mInsideDiscontinuousLoop = wasDiscontinuous;
2240 mNestedLoopDepth--;
2241
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002242 return false;
2243 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002244 }
2245
Corentin Wallez1239ee92015-03-19 14:38:02 -07002246 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002247 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002249 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002250
Jamie Madill8c46ab12015-12-07 16:39:19 -05002251 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252 }
2253 else
2254 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002255 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002256
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 if (node->getInit())
2258 {
2259 node->getInit()->traverse(this);
2260 }
2261
2262 out << "; ";
2263
alokp@chromium.org52813552010-11-16 18:36:09 +00002264 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002266 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267 }
2268
2269 out << "; ";
2270
alokp@chromium.org52813552010-11-16 18:36:09 +00002271 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002273 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 }
2275
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002276 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002277
Jamie Madill8c46ab12015-12-07 16:39:19 -05002278 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279 }
2280
2281 if (node->getBody())
2282 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002283 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002284 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002285 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002286 else
2287 {
2288 // TODO(oetuaho): Check if the semicolon inside is necessary.
2289 // It's there as a result of conservative refactoring of the output.
2290 out << "{;}\n";
2291 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002292
Jamie Madill8c46ab12015-12-07 16:39:19 -05002293 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294
alokp@chromium.org52813552010-11-16 18:36:09 +00002295 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002297 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002298 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299
alokp@chromium.org52813552010-11-16 18:36:09 +00002300 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002302 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 }
2304
daniel@transgaming.com73536982012-03-21 20:45:49 +00002305 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002307 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002308 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002309
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310 return false;
2311}
2312
2313bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2314{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002315 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002316 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002317 TInfoSinkBase &out = getInfoSink();
2318
2319 switch (node->getFlowOp())
2320 {
2321 case EOpKill:
2322 out << "discard";
2323 break;
2324 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002325 if (mNestedLoopDepth > 1)
2326 {
2327 mUsesNestedBreak = true;
2328 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002329
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002330 if (mExcessiveLoopIndex)
2331 {
2332 out << "{Break";
2333 mExcessiveLoopIndex->traverse(this);
2334 out << " = true; break;}\n";
2335 }
2336 else
2337 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002338 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002339 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002340 break;
2341 case EOpContinue:
2342 out << "continue";
2343 break;
2344 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002345 if (node->getExpression())
2346 {
2347 out << "return ";
2348 }
2349 else
2350 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002351 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002352 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002353 break;
2354 default:
2355 UNREACHABLE();
2356 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357 }
2358
2359 return true;
2360}
2361
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002362// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002363// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2364// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002365bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002366{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002367 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002368
2369 // Parse loops of the form:
2370 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002371 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002372 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002373 int initial = 0;
2374 int limit = 0;
2375 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002376
2377 // Parse index name and intial value
2378 if (node->getInit())
2379 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002380 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002381
2382 if (init)
2383 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002384 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002385 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002386
2387 if (variable && variable->getQualifier() == EvqTemporary)
2388 {
2389 TIntermBinary *assign = variable->getAsBinaryNode();
2390
2391 if (assign->getOp() == EOpInitialize)
2392 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002393 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002394 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2395
2396 if (symbol && constant)
2397 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002398 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002399 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002400 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002401 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002402 }
2403 }
2404 }
2405 }
2406 }
2407 }
2408
2409 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002410 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002411 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002412 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002413
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002414 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2415 {
2416 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2417
2418 if (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 {
2422 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002423 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002424 }
2425 }
2426 }
2427 }
2428
2429 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002430 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002431 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002432 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002433 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002434
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002435 if (binaryTerminal)
2436 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002437 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002438 TIntermConstantUnion *constant = binaryTerminal->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 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002444 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002445
2446 switch (op)
2447 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002448 case EOpAddAssign:
2449 increment = value;
2450 break;
2451 case EOpSubAssign:
2452 increment = -value;
2453 break;
2454 default:
2455 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002456 }
2457 }
2458 }
2459 }
2460 else if (unaryTerminal)
2461 {
2462 TOperator op = unaryTerminal->getOp();
2463
2464 switch (op)
2465 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002466 case EOpPostIncrement:
2467 increment = 1;
2468 break;
2469 case EOpPostDecrement:
2470 increment = -1;
2471 break;
2472 case EOpPreIncrement:
2473 increment = 1;
2474 break;
2475 case EOpPreDecrement:
2476 increment = -1;
2477 break;
2478 default:
2479 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002480 }
2481 }
2482 }
2483
Yunchao He4f285442017-04-21 12:15:49 +08002484 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002485 {
2486 if (comparator == EOpLessThanEqual)
2487 {
2488 comparator = EOpLessThan;
2489 limit += 1;
2490 }
2491
2492 if (comparator == EOpLessThan)
2493 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002494 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002495
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002496 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002497 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002498 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002499 }
2500
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002501 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002502 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002503
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002504 out << "{int ";
2505 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002506 out << ";\n"
2507 "bool Break";
2508 index->traverse(this);
2509 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002510
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002511 bool firstLoopFragment = true;
2512
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002513 while (iterations > 0)
2514 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002515 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002516
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002517 if (!firstLoopFragment)
2518 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002519 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002520 index->traverse(this);
2521 out << ") {\n";
2522 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002523
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002524 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002525 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002526 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002527 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002528
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002529 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002530 const char *unroll =
2531 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002532
Corentin Wallez1239ee92015-03-19 14:38:02 -07002533 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002534 index->traverse(this);
2535 out << " = ";
2536 out << initial;
2537
2538 out << "; ";
2539 index->traverse(this);
2540 out << " < ";
2541 out << clampedLimit;
2542
2543 out << "; ";
2544 index->traverse(this);
2545 out << " += ";
2546 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002547 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002548
Jamie Madill8c46ab12015-12-07 16:39:19 -05002549 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002550 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002551
2552 if (node->getBody())
2553 {
2554 node->getBody()->traverse(this);
2555 }
2556
Jamie Madill8c46ab12015-12-07 16:39:19 -05002557 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002558 out << ";}\n";
2559
2560 if (!firstLoopFragment)
2561 {
2562 out << "}\n";
2563 }
2564
2565 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002566
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002567 initial += MAX_LOOP_ITERATIONS * increment;
2568 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002569 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002570
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002571 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002573 mExcessiveLoopIndex = restoreIndex;
2574
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002575 return true;
2576 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002577 else
2578 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002579 }
2580
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002581 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002582}
2583
Jamie Madill8c46ab12015-12-07 16:39:19 -05002584void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2585 Visit visit,
2586 const char *preString,
2587 const char *inString,
2588 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002589{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002590 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591 {
2592 out << preString;
2593 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002594 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 {
2596 out << inString;
2597 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002598 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002599 {
2600 out << postString;
2601 }
2602}
2603
Jamie Madill8c46ab12015-12-07 16:39:19 -05002604void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002605{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002606 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002607 {
Jamie Madill32aab012015-01-27 14:12:26 -05002608 out << "\n";
2609 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002610
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002611 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002612 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002613 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002614 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002615
Jamie Madill32aab012015-01-27 14:12:26 -05002616 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002617 }
2618}
2619
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002620TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2621{
2622 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002623 const TType &type = symbol->getType();
2624 const TName &name = symbol->getName();
2625 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002626
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002627 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002628 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002629 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002630 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002631 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002632 {
Olli Etuahoff526f12017-06-30 12:26:54 +03002633 nameStr = DecorateVariableIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002634 }
2635
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002636 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002637 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002638 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2639 {
2640 // Samplers are passed as indices to the sampler array.
2641 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2642 return "const uint " + nameStr + ArrayString(type);
2643 }
2644 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2645 {
2646 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2647 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2648 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2649 ArrayString(type);
2650 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002651 }
2652
Olli Etuaho96963162016-03-21 11:54:33 +02002653 TStringStream argString;
2654 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2655 << ArrayString(type);
2656
2657 // If the structure parameter contains samplers, they need to be passed into the function as
2658 // separate parameters. HLSL doesn't natively support samplers in structs.
2659 if (type.isStructureContainingSamplers())
2660 {
2661 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2662 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002663 type.createSamplerSymbols("angle" + nameStr, "", &samplerSymbols, nullptr, mSymbolTable);
Olli Etuaho96963162016-03-21 11:54:33 +02002664 for (const TIntermSymbol *sampler : samplerSymbols)
2665 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002666 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002667 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2668 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002669 argString << ", const uint " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002670 }
2671 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2672 {
Olli Etuaho96963162016-03-21 11:54:33 +02002673 ASSERT(IsSampler(samplerType.getBasicType()));
2674 argString << ", " << QualifierString(qualifier) << " "
2675 << TextureString(samplerType.getBasicType()) << " texture_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002676 << sampler->getSymbol() << ArrayString(samplerType) << ", "
2677 << QualifierString(qualifier) << " "
Olli Etuaho96963162016-03-21 11:54:33 +02002678 << SamplerString(samplerType.getBasicType()) << " sampler_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002679 << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002680 }
2681 else
2682 {
Olli Etuaho96963162016-03-21 11:54:33 +02002683 ASSERT(IsSampler(samplerType.getBasicType()));
2684 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
Olli Etuaho28839f02017-08-15 11:38:16 +03002685 << " " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002686 }
2687 }
2688 }
2689
2690 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691}
2692
2693TString OutputHLSL::initializer(const TType &type)
2694{
2695 TString string;
2696
Jamie Madill94bf7f22013-07-08 13:31:15 -04002697 size_t size = type.getObjectSize();
2698 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002699 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002700 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701
Jamie Madill94bf7f22013-07-08 13:31:15 -04002702 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703 {
2704 string += ", ";
2705 }
2706 }
2707
daniel@transgaming.comead23042010-04-29 03:35:36 +00002708 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002709}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002710
Olli Etuahobd3cd502017-11-03 15:48:52 +02002711void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002712{
Olli Etuahobd3cd502017-11-03 15:48:52 +02002713 // Array constructors should have been already pruned from the code.
2714 ASSERT(!node->getType().isArray());
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002715
2716 if (visit == PreVisit)
2717 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002718 TString constructorName;
2719 if (node->getBasicType() == EbtStruct)
2720 {
2721 constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct());
2722 }
2723 else
2724 {
2725 constructorName =
2726 mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence());
2727 }
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002728 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002729 }
2730 else if (visit == InVisit)
2731 {
2732 out << ", ";
2733 }
2734 else if (visit == PostVisit)
2735 {
2736 out << ")";
2737 }
2738}
2739
Jamie Madill8c46ab12015-12-07 16:39:19 -05002740const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2741 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002742 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002743{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002744 const TConstantUnion *constUnionIterated = constUnion;
2745
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002746 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002747 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002748 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002749 out << mStructureHLSL->addStructConstructor(*structure) << "(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002750
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002751 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002752
Jamie Madill98493dd2013-07-08 14:39:03 -04002753 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002754 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002755 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002756 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002757
Jamie Madill98493dd2013-07-08 14:39:03 -04002758 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002759 {
2760 out << ", ";
2761 }
2762 }
2763
2764 out << ")";
2765 }
2766 else
2767 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002768 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002769 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002770
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002771 if (writeType)
2772 {
Jamie Madill033dae62014-06-18 12:56:28 -04002773 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002774 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002775 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002776 if (writeType)
2777 {
2778 out << ")";
2779 }
2780 }
2781
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002782 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002783}
2784
Olli Etuahod68924e2017-01-02 17:34:40 +00002785void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002786{
Olli Etuahod68924e2017-01-02 17:34:40 +00002787 if (visit == PreVisit)
2788 {
2789 const char *opStr = GetOperatorString(op);
2790 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2791 out << "(";
2792 }
2793 else
2794 {
2795 outputTriplet(out, visit, nullptr, ", ", ")");
2796 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002797}
2798
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002799bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2800 TIntermSymbol *symbolNode,
2801 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002802{
2803 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2804 expression->traverse(&searchSymbol);
2805
2806 if (searchSymbol.foundMatch())
2807 {
2808 // Type already printed
2809 out << "t" + str(mUniqueIndex) + " = ";
2810 expression->traverse(this);
2811 out << ", ";
2812 symbolNode->traverse(this);
2813 out << " = t" + str(mUniqueIndex);
2814
2815 mUniqueIndex++;
2816 return true;
2817 }
2818
2819 return false;
2820}
2821
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002822bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2823{
2824 // We support writing constant unions and constructors that only take constant unions as
2825 // parameters as HLSL literals.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002826 return !expression->getType().isArrayOfArrays() &&
2827 (expression->getAsConstantUnion() ||
2828 expression->isConstructorWithOnlyConstantUnionParameters());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002829}
2830
2831bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2832 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002833 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002834{
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002835 if (canWriteAsHLSLLiteral(initializer))
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002836 {
2837 symbolNode->traverse(this);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002838 ASSERT(!symbolNode->getType().isArrayOfArrays());
2839 if (symbolNode->getType().isArray())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002840 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002841 out << "[" << symbolNode->getType().getOutermostArraySize() << "]";
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002842 }
2843 out << " = {";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002844 if (initializer->getAsConstantUnion())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002845 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002846 TIntermConstantUnion *nodeConst = initializer->getAsConstantUnion();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002847 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002848 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002849 }
2850 else
2851 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002852 TIntermAggregate *constructor = initializer->getAsAggregate();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002853 ASSERT(constructor != nullptr);
2854 for (TIntermNode *&node : *constructor->getSequence())
2855 {
2856 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2857 ASSERT(nodeConst);
2858 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002859 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002860 if (node != constructor->getSequence()->back())
2861 {
2862 out << ", ";
2863 }
2864 }
2865 }
2866 out << "}";
2867 return true;
2868 }
2869 return false;
2870}
2871
Jamie Madill55e79e02015-02-09 15:35:00 -05002872TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2873{
2874 const TFieldList &fields = structure.fields();
2875
2876 for (const auto &eqFunction : mStructEqualityFunctions)
2877 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002878 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002879 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002880 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002881 }
2882 }
2883
2884 const TString &structNameString = StructNameString(structure);
2885
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002886 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002887 function->structure = &structure;
2888 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002889
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002890 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002891
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002892 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2893 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002894 << "{\n"
2895 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002896
2897 for (size_t i = 0; i < fields.size(); i++)
2898 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002899 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002900 const TType *fieldType = field->type();
2901
2902 const TString &fieldNameA = "a." + Decorate(field->name());
2903 const TString &fieldNameB = "b." + Decorate(field->name());
2904
2905 if (i > 0)
2906 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002907 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002908 }
2909
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002910 fnOut << "(";
2911 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2912 fnOut << fieldNameA;
2913 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2914 fnOut << fieldNameB;
2915 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2916 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002917 }
2918
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002919 fnOut << ";\n"
2920 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002921
2922 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002923
2924 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002925 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002926
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002927 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002928}
2929
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002930TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002931{
2932 for (const auto &eqFunction : mArrayEqualityFunctions)
2933 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002934 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002935 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002936 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002937 }
2938 }
2939
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002940 TType elementType(type);
2941 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002942
Olli Etuaho12690762015-03-31 12:55:28 +03002943 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002944 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002945
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002946 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002947
2948 TInfoSinkBase fnOut;
2949
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002950 const TString &typeName = TypeString(type);
2951 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2952 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002953 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002954 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002955 << type.getOutermostArraySize()
2956 << "; ++i)\n"
2957 " {\n"
2958 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002959
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002960 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002961 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002962 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002963 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002964 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002965
2966 fnOut << ") { return false; }\n"
2967 " }\n"
2968 " return true;\n"
2969 "}\n";
2970
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002971 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002972
2973 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002974 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002975
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002976 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002977}
2978
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002979TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002980{
2981 for (const auto &assignFunction : mArrayAssignmentFunctions)
2982 {
2983 if (assignFunction.type == type)
2984 {
2985 return assignFunction.functionName;
2986 }
2987 }
2988
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002989 TType elementType(type);
2990 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03002991
2992 ArrayHelperFunction function;
2993 function.type = type;
2994
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002995 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03002996
2997 TInfoSinkBase fnOut;
2998
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002999 const TString &typeName = TypeString(type);
3000 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
3001 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003002 << "{\n"
3003 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003004 << type.getOutermostArraySize()
3005 << "; ++i)\n"
3006 " {\n"
3007 " ";
3008
3009 outputAssign(PreVisit, elementType, fnOut);
3010 fnOut << "a[i]";
3011 outputAssign(InVisit, elementType, fnOut);
3012 fnOut << "b[i]";
3013 outputAssign(PostVisit, elementType, fnOut);
3014
3015 fnOut << ";\n"
3016 " }\n"
3017 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03003018
3019 function.functionDefinition = fnOut.c_str();
3020
3021 mArrayAssignmentFunctions.push_back(function);
3022
3023 return function.functionName;
3024}
3025
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003026TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003027{
3028 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3029 {
3030 if (constructIntoFunction.type == type)
3031 {
3032 return constructIntoFunction.functionName;
3033 }
3034 }
3035
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003036 TType elementType(type);
3037 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003038
3039 ArrayHelperFunction function;
3040 function.type = type;
3041
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003042 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003043
3044 TInfoSinkBase fnOut;
3045
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003046 const TString &typeName = TypeString(type);
3047 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3048 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003049 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003050 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003051 }
3052 fnOut << ")\n"
3053 "{\n";
3054
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003055 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003056 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003057 fnOut << " ";
3058 outputAssign(PreVisit, elementType, fnOut);
3059 fnOut << "a[" << i << "]";
3060 outputAssign(InVisit, elementType, fnOut);
3061 fnOut << "b" << i;
3062 outputAssign(PostVisit, elementType, fnOut);
3063 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003064 }
3065 fnOut << "}\n";
3066
3067 function.functionDefinition = fnOut.c_str();
3068
3069 mArrayConstructIntoFunctions.push_back(function);
3070
3071 return function.functionName;
3072}
3073
Jamie Madill2e295e22015-04-29 10:41:33 -04003074void OutputHLSL::ensureStructDefined(const TType &type)
3075{
Olli Etuahobd3cd502017-11-03 15:48:52 +02003076 const TStructure *structure = type.getStruct();
Jamie Madill2e295e22015-04-29 10:41:33 -04003077 if (structure)
3078 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02003079 ASSERT(type.getBasicType() == EbtStruct);
3080 mStructureHLSL->ensureStructDefined(*structure);
Jamie Madill2e295e22015-04-29 10:41:33 -04003081 }
3082}
3083
Jamie Madill45bcc782016-11-07 13:58:48 -05003084} // namespace sh