blob: 59b62fcbba092ad715744bcbb1e0c9fb1d1a8de6 [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"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#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 Etuaho56a2f952016-12-08 12:16:27 +000034void OutputHLSL::writeFloat(TInfoSinkBase &out, float f)
Olli Etuaho4785fec2015-05-18 16:09:37 +030035{
Olli Etuaho56a2f952016-12-08 12:16:27 +000036 // This is known not to work for NaN on all drivers but make the best effort to output NaNs
37 // regardless.
38 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300 &&
39 mOutputType == SH_HLSL_4_1_OUTPUT)
40 {
41 out << "asfloat(" << gl::bitCast<uint32_t>(f) << "u)";
42 }
43 else
44 {
45 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
46 }
47}
Olli Etuaho4785fec2015-05-18 16:09:37 +030048
Olli Etuaho56a2f952016-12-08 12:16:27 +000049void OutputHLSL::writeSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
Olli Etuaho18b9deb2015-11-05 12:14:50 +020050{
51 ASSERT(constUnion != nullptr);
52 switch (constUnion->getType())
53 {
54 case EbtFloat:
Olli Etuaho56a2f952016-12-08 12:16:27 +000055 writeFloat(out, constUnion->getFConst());
Olli Etuaho18b9deb2015-11-05 12:14:50 +020056 break;
57 case EbtInt:
58 out << constUnion->getIConst();
59 break;
60 case EbtUInt:
61 out << constUnion->getUConst();
62 break;
63 case EbtBool:
64 out << constUnion->getBConst();
65 break;
66 default:
67 UNREACHABLE();
68 }
69}
70
Olli Etuaho56a2f952016-12-08 12:16:27 +000071const TConstantUnion *OutputHLSL::writeConstantUnionArray(TInfoSinkBase &out,
72 const TConstantUnion *const constUnion,
73 const size_t size)
Olli Etuaho18b9deb2015-11-05 12:14:50 +020074{
75 const TConstantUnion *constUnionIterated = constUnion;
76 for (size_t i = 0; i < size; i++, constUnionIterated++)
77 {
Olli Etuaho56a2f952016-12-08 12:16:27 +000078 writeSingleConstant(out, constUnionIterated);
Olli Etuaho18b9deb2015-11-05 12:14:50 +020079
80 if (i != size - 1)
81 {
82 out << ", ";
83 }
84 }
85 return constUnionIterated;
86}
87
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080088OutputHLSL::OutputHLSL(sh::GLenum shaderType,
89 int shaderVersion,
90 const TExtensionBehavior &extensionBehavior,
91 const char *sourcePath,
92 ShShaderOutput outputType,
93 int numRenderTargets,
94 const std::vector<Uniform> &uniforms,
95 ShCompileOptions compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -040096 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020097 mShaderType(shaderType),
98 mShaderVersion(shaderVersion),
99 mExtensionBehavior(extensionBehavior),
100 mSourcePath(sourcePath),
101 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700102 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000103 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700104 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000106 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000107
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000108 mUsesFragColor = false;
109 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000110 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000111 mUsesFragCoord = false;
112 mUsesPointCoord = false;
113 mUsesFrontFacing = false;
114 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000115 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500116 mUsesVertexID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400117 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000118 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500119 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400120 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530121 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000122
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000123 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000124
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000125 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000126 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400127 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000128
129 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000130
Jamie Madill8daaba12014-06-13 10:04:33 -0400131 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200132 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300133 mTextureFunctionHLSL = new TextureFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400134
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200135 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000136 {
Arun Patole63419392015-03-13 11:51:07 +0530137 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
138 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
139 // In both cases total 3 uniform registers need to be reserved.
140 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000141 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000142
Geoff Lang00140f42016-02-03 18:47:33 +0000143 // Reserve registers for the default uniform block and driver constants
144 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145}
146
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000147OutputHLSL::~OutputHLSL()
148{
Jamie Madill8daaba12014-06-13 10:04:33 -0400149 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400150 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300151 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200152 for (auto &eqFunction : mStructEqualityFunctions)
153 {
154 SafeDelete(eqFunction);
155 }
156 for (auto &eqFunction : mArrayEqualityFunctions)
157 {
158 SafeDelete(eqFunction);
159 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000160}
161
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200162void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000163{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200164 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400165 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000166
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200167 BuiltInFunctionEmulator builtInFunctionEmulator;
168 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800169 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
170 {
171 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
172 mShaderVersion);
173 }
174
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200175 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500176
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700177 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700178 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
179 ASSERT(success == CallDAG::INITDAG_SUCCESS);
180 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700181
Jamie Madill37997142015-01-28 10:06:34 -0500182 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500183 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200184 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500185 mInfoSinkStack.pop();
186
Jamie Madill37997142015-01-28 10:06:34 -0500187 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500188 mInfoSinkStack.pop();
189
Jamie Madill32aab012015-01-27 14:12:26 -0500190 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500191 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500192 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000193
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200194 objSink << mHeader.c_str();
195 objSink << mBody.c_str();
196 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200197
198 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000199}
200
Jamie Madill570e04d2013-06-21 09:15:33 -0400201void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
202{
203 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
204 {
205 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
206
Jamie Madill32aab012015-01-27 14:12:26 -0500207 TInfoSinkBase structInfoSink;
208 mInfoSinkStack.push(&structInfoSink);
209
Jamie Madill570e04d2013-06-21 09:15:33 -0400210 // This will mark the necessary block elements as referenced
211 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500212
213 TString structName(structInfoSink.c_str());
214 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400215
216 mFlaggedStructOriginalNames[flaggedNode] = structName;
217
218 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
219 {
220 structName.erase(pos, 1);
221 }
222
223 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
224 }
225}
226
Jamie Madill4e1fd412014-07-10 17:50:10 -0400227const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
228{
229 return mUniformHLSL->getInterfaceBlockRegisterMap();
230}
231
Jamie Madill9fe25e92014-07-18 10:33:08 -0400232const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
233{
234 return mUniformHLSL->getUniformRegisterMap();
235}
236
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000237int OutputHLSL::vectorSize(const TType &type) const
238{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000239 int elementSize = type.isMatrix() ? type.getCols() : 1;
Olli Etuaho856c4972016-08-08 11:38:39 +0300240 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000241
242 return elementSize * arraySize;
243}
244
Jamie Madill98493dd2013-07-08 14:39:03 -0400245TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400246{
247 TString init;
248
249 TString preIndentString;
250 TString fullIndentString;
251
252 for (int spaces = 0; spaces < (indent * 4); spaces++)
253 {
254 preIndentString += ' ';
255 }
256
257 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
258 {
259 fullIndentString += ' ';
260 }
261
262 init += preIndentString + "{\n";
263
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 const TFieldList &fields = structure.fields();
265 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400266 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400268 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400270
Jamie Madill98493dd2013-07-08 14:39:03 -0400271 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400272 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400274 }
275 else
276 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400278 }
279 }
280
281 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
282
283 return init;
284}
285
Jamie Madill8c46ab12015-12-07 16:39:19 -0500286void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000288 TString varyings;
289 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400290 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000291
Jamie Madill829f59e2013-11-13 19:40:54 -0500292 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400293 {
294 TIntermTyped *structNode = flaggedStructIt->first;
295 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400296 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400297 const TString &originalName = mFlaggedStructOriginalNames[structNode];
298
Jamie Madill033dae62014-06-18 12:56:28 -0400299 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400300 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400301 flaggedStructs += "\n";
302 }
303
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000304 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
305 {
306 const TType &type = varying->second->getType();
307 const TString &name = varying->second->getSymbol();
308
309 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400310 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
311 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000312 }
313
314 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
315 {
316 const TType &type = attribute->second->getType();
317 const TString &name = attribute->second->getSymbol();
318
Jamie Madill033dae62014-06-18 12:56:28 -0400319 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000320 }
321
Jamie Madill8daaba12014-06-13 10:04:33 -0400322 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400323
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200324 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400325 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
326
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200327 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500328 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200329 out << "\n// Equality functions\n\n";
330 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500331 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200332 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200333 }
334 }
Olli Etuaho12690762015-03-31 12:55:28 +0300335 if (!mArrayAssignmentFunctions.empty())
336 {
337 out << "\n// Assignment functions\n\n";
338 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
339 {
340 out << assignmentFunction.functionDefinition << "\n";
341 }
342 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300343 if (!mArrayConstructIntoFunctions.empty())
344 {
345 out << "\n// Array constructor functions\n\n";
346 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
347 {
348 out << constructIntoFunction.functionDefinition << "\n";
349 }
350 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200351
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500352 if (mUsesDiscardRewriting)
353 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400354 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500355 }
356
Nicolas Capens655fe362014-04-11 13:12:34 -0400357 if (mUsesNestedBreak)
358 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400359 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400360 }
361
Arun Patole44efa0b2015-03-04 17:11:05 +0530362 if (mRequiresIEEEStrictCompiling)
363 {
364 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
365 }
366
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400367 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
368 "#define LOOP [loop]\n"
369 "#define FLATTEN [flatten]\n"
370 "#else\n"
371 "#define LOOP\n"
372 "#define FLATTEN\n"
373 "#endif\n";
374
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200375 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200377 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
378 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000379
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000380 out << "// Varyings\n";
381 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400382 out << "\n";
383
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200384 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000385 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500386 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000387 {
Jamie Madill46131a32013-06-20 11:55:50 -0400388 const TString &variableName = outputVariableIt->first;
389 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400390
Jamie Madill033dae62014-06-18 12:56:28 -0400391 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400392 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000393 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000394 }
Jamie Madill46131a32013-06-20 11:55:50 -0400395 else
396 {
397 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
398
399 out << "static float4 gl_Color[" << numColorValues << "] =\n"
400 "{\n";
401 for (unsigned int i = 0; i < numColorValues; i++)
402 {
403 out << " float4(0, 0, 0, 0)";
404 if (i + 1 != numColorValues)
405 {
406 out << ",";
407 }
408 out << "\n";
409 }
410
411 out << "};\n";
412 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000413
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400414 if (mUsesFragDepth)
415 {
416 out << "static float gl_Depth = 0.0;\n";
417 }
418
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000419 if (mUsesFragCoord)
420 {
421 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
422 }
423
424 if (mUsesPointCoord)
425 {
426 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
427 }
428
429 if (mUsesFrontFacing)
430 {
431 out << "static bool gl_FrontFacing = false;\n";
432 }
433
434 out << "\n";
435
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000436 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000437 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000438 out << "struct gl_DepthRangeParameters\n"
439 "{\n"
440 " float near;\n"
441 " float far;\n"
442 " float diff;\n"
443 "};\n"
444 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000445 }
446
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200447 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000448 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000449 out << "cbuffer DriverConstants : register(b1)\n"
450 "{\n";
451
452 if (mUsesDepthRange)
453 {
454 out << " float3 dx_DepthRange : packoffset(c0);\n";
455 }
456
457 if (mUsesFragCoord)
458 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000459 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000460 }
461
462 if (mUsesFragCoord || mUsesFrontFacing)
463 {
464 out << " float3 dx_DepthFront : packoffset(c2);\n";
465 }
466
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800467 if (mUsesFragCoord)
468 {
469 // dx_ViewScale is only used in the fragment shader to correct
470 // the value for glFragCoord if necessary
471 out << " float2 dx_ViewScale : packoffset(c3);\n";
472 }
473
Olli Etuaho618bebc2016-01-15 16:40:00 +0200474 if (mOutputType == SH_HLSL_4_1_OUTPUT)
475 {
476 mUniformHLSL->samplerMetadataUniforms(out, "c4");
477 }
478
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000479 out << "};\n";
480 }
481 else
482 {
483 if (mUsesDepthRange)
484 {
485 out << "uniform float3 dx_DepthRange : register(c0);";
486 }
487
488 if (mUsesFragCoord)
489 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000490 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000491 }
492
493 if (mUsesFragCoord || mUsesFrontFacing)
494 {
495 out << "uniform float3 dx_DepthFront : register(c2);\n";
496 }
497 }
498
499 out << "\n";
500
501 if (mUsesDepthRange)
502 {
503 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
504 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000505 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000506
Jamie Madillf91ce812014-06-13 10:04:34 -0400507 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000508 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400509 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000510 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400511 out << flaggedStructs;
512 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000513 }
514
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000515 if (usingMRTExtension && mNumRenderTargets > 1)
516 {
517 out << "#define GL_USES_MRT\n";
518 }
519
520 if (mUsesFragColor)
521 {
522 out << "#define GL_USES_FRAG_COLOR\n";
523 }
524
525 if (mUsesFragData)
526 {
527 out << "#define GL_USES_FRAG_DATA\n";
528 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000530 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000532 out << "// Attributes\n";
533 out << attributes;
534 out << "\n"
535 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400536
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000537 if (mUsesPointSize)
538 {
539 out << "static float gl_PointSize = float(1);\n";
540 }
541
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000542 if (mUsesInstanceID)
543 {
544 out << "static int gl_InstanceID;";
545 }
546
Corentin Wallezb076add2016-01-11 16:45:46 -0500547 if (mUsesVertexID)
548 {
549 out << "static int gl_VertexID;";
550 }
551
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000552 out << "\n"
553 "// Varyings\n";
554 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000555 out << "\n";
556
557 if (mUsesDepthRange)
558 {
559 out << "struct gl_DepthRangeParameters\n"
560 "{\n"
561 " float near;\n"
562 " float far;\n"
563 " float diff;\n"
564 "};\n"
565 "\n";
566 }
567
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200568 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000569 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800570 out << "cbuffer DriverConstants : register(b1)\n"
571 "{\n";
572
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000573 if (mUsesDepthRange)
574 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800575 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000576 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800577
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800578 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
579 // shaders. However, we declare it for all shaders (including Feature Level 10+).
580 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
581 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800582 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800583 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800584 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800585
Olli Etuaho618bebc2016-01-15 16:40:00 +0200586 if (mOutputType == SH_HLSL_4_1_OUTPUT)
587 {
588 mUniformHLSL->samplerMetadataUniforms(out, "c4");
589 }
590
Austin Kinross4fd18b12014-12-22 12:32:05 -0800591 out << "};\n"
592 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000593 }
594 else
595 {
596 if (mUsesDepthRange)
597 {
598 out << "uniform float3 dx_DepthRange : register(c0);\n";
599 }
600
Cooper Partine6664f02015-01-09 16:22:24 -0800601 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
602 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000603 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000604 }
605
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000606 if (mUsesDepthRange)
607 {
608 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
609 "\n";
610 }
611
Jamie Madillf91ce812014-06-13 10:04:34 -0400612 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000613 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400614 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000615 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400616 out << flaggedStructs;
617 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000618 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400619 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000620
Geoff Lang1fe74c72016-08-25 13:23:01 -0400621 bool getDimensionsIgnoresBaseLevel =
622 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
623 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000625 if (mUsesFragCoord)
626 {
627 out << "#define GL_USES_FRAG_COORD\n";
628 }
629
630 if (mUsesPointCoord)
631 {
632 out << "#define GL_USES_POINT_COORD\n";
633 }
634
635 if (mUsesFrontFacing)
636 {
637 out << "#define GL_USES_FRONT_FACING\n";
638 }
639
640 if (mUsesPointSize)
641 {
642 out << "#define GL_USES_POINT_SIZE\n";
643 }
644
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400645 if (mUsesFragDepth)
646 {
647 out << "#define GL_USES_FRAG_DEPTH\n";
648 }
649
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000650 if (mUsesDepthRange)
651 {
652 out << "#define GL_USES_DEPTH_RANGE\n";
653 }
654
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000655 if (mUsesXor)
656 {
657 out << "bool xor(bool p, bool q)\n"
658 "{\n"
659 " return (p || q) && !(p && q);\n"
660 "}\n"
661 "\n";
662 }
663
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200664 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665}
666
667void OutputHLSL::visitSymbol(TIntermSymbol *node)
668{
Jamie Madill32aab012015-01-27 14:12:26 -0500669 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670
Jamie Madill570e04d2013-06-21 09:15:33 -0400671 // Handle accessing std140 structs by value
672 if (mFlaggedStructMappedNames.count(node) > 0)
673 {
674 out << mFlaggedStructMappedNames[node];
675 return;
676 }
677
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678 TString name = node->getSymbol();
679
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000680 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000681 {
682 mUsesDepthRange = true;
683 out << name;
684 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685 else
686 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000687 TQualifier qualifier = node->getQualifier();
688
689 if (qualifier == EvqUniform)
690 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400691 const TType &nodeType = node->getType();
692 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400693
694 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000695 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400696 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000697 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000698 else
699 {
700 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000701 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400702
Jamie Madill2e295e22015-04-29 10:41:33 -0400703 ensureStructDefined(nodeType);
704
Olli Etuaho96963162016-03-21 11:54:33 +0200705 const TName &nameWithMetadata = node->getName();
706 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000707 }
Jamie Madill19571812013-08-12 15:26:34 -0700708 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000709 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000710 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400711 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000712 }
Jamie Madill033dae62014-06-18 12:56:28 -0400713 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000714 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000715 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400716 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000717 }
Jamie Madill19571812013-08-12 15:26:34 -0700718 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400719 {
720 mReferencedOutputVariables[name] = node;
721 out << "out_" << name;
722 }
723 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000724 {
725 out << "gl_Color[0]";
726 mUsesFragColor = true;
727 }
728 else if (qualifier == EvqFragData)
729 {
730 out << "gl_Color";
731 mUsesFragData = true;
732 }
733 else if (qualifier == EvqFragCoord)
734 {
735 mUsesFragCoord = true;
736 out << name;
737 }
738 else if (qualifier == EvqPointCoord)
739 {
740 mUsesPointCoord = true;
741 out << name;
742 }
743 else if (qualifier == EvqFrontFacing)
744 {
745 mUsesFrontFacing = true;
746 out << name;
747 }
748 else if (qualifier == EvqPointSize)
749 {
750 mUsesPointSize = true;
751 out << name;
752 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000753 else if (qualifier == EvqInstanceID)
754 {
755 mUsesInstanceID = true;
756 out << name;
757 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500758 else if (qualifier == EvqVertexID)
759 {
760 mUsesVertexID = true;
761 out << name;
762 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300763 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400764 {
765 mUsesFragDepth = true;
766 out << "gl_Depth";
767 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000768 else
769 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300770 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000771 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000772 }
773}
774
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400775void OutputHLSL::visitRaw(TIntermRaw *node)
776{
Jamie Madill32aab012015-01-27 14:12:26 -0500777 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400778}
779
Olli Etuaho7fb49552015-03-18 17:27:44 +0200780void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
781{
782 if (type.isScalar() && !type.isArray())
783 {
784 if (op == EOpEqual)
785 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500786 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200787 }
788 else
789 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500790 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200791 }
792 }
793 else
794 {
795 if (visit == PreVisit && op == EOpNotEqual)
796 {
797 out << "!";
798 }
799
800 if (type.isArray())
801 {
802 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500803 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200804 }
805 else if (type.getBasicType() == EbtStruct)
806 {
807 const TStructure &structure = *type.getStruct();
808 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500809 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200810 }
811 else
812 {
813 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500814 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200815 }
816 }
817}
818
Olli Etuaho96963162016-03-21 11:54:33 +0200819bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
820{
821 // Inside InVisit the current node is already in the path.
822 const unsigned int initialN = visit == InVisit ? 1u : 0u;
823 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
824 {
825 TIntermNode *ancestor = getAncestorNode(n);
826 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
827 if (ancestorBinary == nullptr)
828 {
829 return false;
830 }
831 switch (ancestorBinary->getOp())
832 {
833 case EOpIndexDirectStruct:
834 {
835 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
836 const TIntermConstantUnion *index =
837 ancestorBinary->getRight()->getAsConstantUnion();
838 const TField *field = structure->fields()[index->getIConst(0)];
839 if (IsSampler(field->type()->getBasicType()))
840 {
841 return true;
842 }
843 break;
844 }
845 case EOpIndexDirect:
846 break;
847 default:
848 // Returning a sampler from indirect indexing is not supported.
849 return false;
850 }
851 }
852 return false;
853}
854
Olli Etuahob6fa0432016-09-28 16:28:05 +0100855bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
856{
857 TInfoSinkBase &out = getInfoSink();
858 if (visit == PostVisit)
859 {
860 out << ".";
861 node->writeOffsetsAsXYZW(&out);
862 }
863 return true;
864}
865
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
867{
Jamie Madill32aab012015-01-27 14:12:26 -0500868 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869
Jamie Madill570e04d2013-06-21 09:15:33 -0400870 // Handle accessing std140 structs by value
871 if (mFlaggedStructMappedNames.count(node) > 0)
872 {
873 out << mFlaggedStructMappedNames[node];
874 return false;
875 }
876
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877 switch (node->getOp())
878 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100879 case EOpComma:
880 outputTriplet(out, visit, "(", ", ", ")");
881 break;
882 case EOpAssign:
883 if (node->getLeft()->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +0300884 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100885 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
886 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +0300887 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100888 const TString &functionName = addArrayConstructIntoFunction(node->getType());
889 out << functionName << "(";
890 node->getLeft()->traverse(this);
891 TIntermSequence *seq = rightAgg->getSequence();
892 for (auto &arrayElement : *seq)
893 {
894 out << ", ";
895 arrayElement->traverse(this);
896 }
897 out << ")";
898 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +0300899 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100900 // ArrayReturnValueToOutParameter should have eliminated expressions where a
901 // function call is assigned.
902 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
903
904 const TString &functionName = addArrayAssignmentFunction(node->getType());
905 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho9638c352015-04-01 14:34:52 +0300906 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100907 else
Jamie Madill37997142015-01-28 10:06:34 -0500908 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100909 outputTriplet(out, visit, "(", " = ", ")");
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000910 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100911 break;
912 case EOpInitialize:
913 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200914 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100915 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
916 ASSERT(symbolNode);
917 TIntermTyped *expression = node->getRight();
918
919 // Global initializers must be constant at this point.
920 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
921 canWriteAsHLSLLiteral(expression));
922
923 // GLSL allows to write things like "float x = x;" where a new variable x is defined
924 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
925 // new variable is created before the assignment is evaluated), so we need to
926 // convert
927 // this to "float t = x, x = t;".
928 if (writeSameSymbolInitializer(out, symbolNode, expression))
929 {
930 // Skip initializing the rest of the expression
931 return false;
932 }
933 else if (writeConstantInitialization(out, symbolNode, expression))
934 {
935 return false;
936 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200937 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100938 else if (visit == InVisit)
939 {
940 out << " = ";
941 }
942 break;
943 case EOpAddAssign:
944 outputTriplet(out, visit, "(", " += ", ")");
945 break;
946 case EOpSubAssign:
947 outputTriplet(out, visit, "(", " -= ", ")");
948 break;
949 case EOpMulAssign:
950 outputTriplet(out, visit, "(", " *= ", ")");
951 break;
952 case EOpVectorTimesScalarAssign:
953 outputTriplet(out, visit, "(", " *= ", ")");
954 break;
955 case EOpMatrixTimesScalarAssign:
956 outputTriplet(out, visit, "(", " *= ", ")");
957 break;
958 case EOpVectorTimesMatrixAssign:
959 if (visit == PreVisit)
960 {
961 out << "(";
962 }
963 else if (visit == InVisit)
964 {
965 out << " = mul(";
966 node->getLeft()->traverse(this);
967 out << ", transpose(";
968 }
969 else
970 {
971 out << ")))";
972 }
973 break;
974 case EOpMatrixTimesMatrixAssign:
975 if (visit == PreVisit)
976 {
977 out << "(";
978 }
979 else if (visit == InVisit)
980 {
981 out << " = transpose(mul(transpose(";
982 node->getLeft()->traverse(this);
983 out << "), transpose(";
984 }
985 else
986 {
987 out << "))))";
988 }
989 break;
990 case EOpDivAssign:
991 outputTriplet(out, visit, "(", " /= ", ")");
992 break;
993 case EOpIModAssign:
994 outputTriplet(out, visit, "(", " %= ", ")");
995 break;
996 case EOpBitShiftLeftAssign:
997 outputTriplet(out, visit, "(", " <<= ", ")");
998 break;
999 case EOpBitShiftRightAssign:
1000 outputTriplet(out, visit, "(", " >>= ", ")");
1001 break;
1002 case EOpBitwiseAndAssign:
1003 outputTriplet(out, visit, "(", " &= ", ")");
1004 break;
1005 case EOpBitwiseXorAssign:
1006 outputTriplet(out, visit, "(", " ^= ", ")");
1007 break;
1008 case EOpBitwiseOrAssign:
1009 outputTriplet(out, visit, "(", " |= ", ")");
1010 break;
1011 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001012 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001013 const TType& leftType = node->getLeft()->getType();
1014 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001015 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001016 if (visit == PreVisit)
1017 {
1018 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1019 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001020 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001021 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001022 return false;
1023 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001024 }
Olli Etuaho96963162016-03-21 11:54:33 +02001025 else if (ancestorEvaluatesToSamplerInStruct(visit))
1026 {
1027 // All parts of an expression that access a sampler in a struct need to use _ as
1028 // separator to access the sampler variable that has been moved out of the struct.
1029 outputTriplet(out, visit, "", "_", "");
1030 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001031 else
1032 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001033 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001034 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001035 }
1036 break;
1037 case EOpIndexIndirect:
1038 // We do not currently support indirect references to interface blocks
1039 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001040 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001041 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001042 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001043 {
1044 const TStructure* structure = node->getLeft()->getType().getStruct();
1045 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1046 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001047
Olli Etuaho96963162016-03-21 11:54:33 +02001048 // In cases where indexing returns a sampler, we need to access the sampler variable
1049 // that has been moved out of the struct.
1050 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1051 if (visit == PreVisit && indexingReturnsSampler)
1052 {
1053 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1054 // This prefix is only output at the beginning of the indexing expression, which
1055 // may have multiple parts.
1056 out << "angle";
1057 }
1058 if (!indexingReturnsSampler)
1059 {
1060 // All parts of an expression that access a sampler in a struct need to use _ as
1061 // separator to access the sampler variable that has been moved out of the struct.
1062 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1063 }
1064 if (visit == InVisit)
1065 {
1066 if (indexingReturnsSampler)
1067 {
1068 out << "_" + field->name();
1069 }
1070 else
1071 {
1072 out << "." + DecorateField(field->name(), *structure);
1073 }
1074
1075 return false;
1076 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001077 }
1078 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001079 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001080 if (visit == InVisit)
1081 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001082 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1083 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1084 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001085 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001086
1087 return false;
1088 }
1089 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001090 case EOpAdd:
1091 outputTriplet(out, visit, "(", " + ", ")");
1092 break;
1093 case EOpSub:
1094 outputTriplet(out, visit, "(", " - ", ")");
1095 break;
1096 case EOpMul:
1097 outputTriplet(out, visit, "(", " * ", ")");
1098 break;
1099 case EOpDiv:
1100 outputTriplet(out, visit, "(", " / ", ")");
1101 break;
1102 case EOpIMod:
1103 outputTriplet(out, visit, "(", " % ", ")");
1104 break;
1105 case EOpBitShiftLeft:
1106 outputTriplet(out, visit, "(", " << ", ")");
1107 break;
1108 case EOpBitShiftRight:
1109 outputTriplet(out, visit, "(", " >> ", ")");
1110 break;
1111 case EOpBitwiseAnd:
1112 outputTriplet(out, visit, "(", " & ", ")");
1113 break;
1114 case EOpBitwiseXor:
1115 outputTriplet(out, visit, "(", " ^ ", ")");
1116 break;
1117 case EOpBitwiseOr:
1118 outputTriplet(out, visit, "(", " | ", ")");
1119 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001120 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001121 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001122 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001123 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001124 case EOpLessThan:
1125 outputTriplet(out, visit, "(", " < ", ")");
1126 break;
1127 case EOpGreaterThan:
1128 outputTriplet(out, visit, "(", " > ", ")");
1129 break;
1130 case EOpLessThanEqual:
1131 outputTriplet(out, visit, "(", " <= ", ")");
1132 break;
1133 case EOpGreaterThanEqual:
1134 outputTriplet(out, visit, "(", " >= ", ")");
1135 break;
1136 case EOpVectorTimesScalar:
1137 outputTriplet(out, visit, "(", " * ", ")");
1138 break;
1139 case EOpMatrixTimesScalar:
1140 outputTriplet(out, visit, "(", " * ", ")");
1141 break;
1142 case EOpVectorTimesMatrix:
1143 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1144 break;
1145 case EOpMatrixTimesVector:
1146 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1147 break;
1148 case EOpMatrixTimesMatrix:
1149 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1150 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001151 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001152 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1153 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001154 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001155 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001156 case EOpLogicalXor:
1157 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001158 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001159 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001160 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001161 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1162 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001163 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001164 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165 default: UNREACHABLE();
1166 }
1167
1168 return true;
1169}
1170
1171bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1172{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001173 TInfoSinkBase &out = getInfoSink();
1174
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001175 switch (node->getOp())
1176 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001177 case EOpNegative:
1178 outputTriplet(out, visit, "(-", "", ")");
1179 break;
1180 case EOpPositive:
1181 outputTriplet(out, visit, "(+", "", ")");
1182 break;
1183 case EOpVectorLogicalNot:
1184 outputTriplet(out, visit, "(!", "", ")");
1185 break;
1186 case EOpLogicalNot:
1187 outputTriplet(out, visit, "(!", "", ")");
1188 break;
1189 case EOpBitwiseNot:
1190 outputTriplet(out, visit, "(~", "", ")");
1191 break;
1192 case EOpPostIncrement:
1193 outputTriplet(out, visit, "(", "", "++)");
1194 break;
1195 case EOpPostDecrement:
1196 outputTriplet(out, visit, "(", "", "--)");
1197 break;
1198 case EOpPreIncrement:
1199 outputTriplet(out, visit, "(++", "", ")");
1200 break;
1201 case EOpPreDecrement:
1202 outputTriplet(out, visit, "(--", "", ")");
1203 break;
1204 case EOpRadians:
1205 outputTriplet(out, visit, "radians(", "", ")");
1206 break;
1207 case EOpDegrees:
1208 outputTriplet(out, visit, "degrees(", "", ")");
1209 break;
1210 case EOpSin:
1211 outputTriplet(out, visit, "sin(", "", ")");
1212 break;
1213 case EOpCos:
1214 outputTriplet(out, visit, "cos(", "", ")");
1215 break;
1216 case EOpTan:
1217 outputTriplet(out, visit, "tan(", "", ")");
1218 break;
1219 case EOpAsin:
1220 outputTriplet(out, visit, "asin(", "", ")");
1221 break;
1222 case EOpAcos:
1223 outputTriplet(out, visit, "acos(", "", ")");
1224 break;
1225 case EOpAtan:
1226 outputTriplet(out, visit, "atan(", "", ")");
1227 break;
1228 case EOpSinh:
1229 outputTriplet(out, visit, "sinh(", "", ")");
1230 break;
1231 case EOpCosh:
1232 outputTriplet(out, visit, "cosh(", "", ")");
1233 break;
1234 case EOpTanh:
1235 outputTriplet(out, visit, "tanh(", "", ")");
1236 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001237 case EOpAsinh:
1238 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001239 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001240 break;
1241 case EOpAcosh:
1242 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001243 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001244 break;
1245 case EOpAtanh:
1246 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001247 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001248 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001249 case EOpExp:
1250 outputTriplet(out, visit, "exp(", "", ")");
1251 break;
1252 case EOpLog:
1253 outputTriplet(out, visit, "log(", "", ")");
1254 break;
1255 case EOpExp2:
1256 outputTriplet(out, visit, "exp2(", "", ")");
1257 break;
1258 case EOpLog2:
1259 outputTriplet(out, visit, "log2(", "", ")");
1260 break;
1261 case EOpSqrt:
1262 outputTriplet(out, visit, "sqrt(", "", ")");
1263 break;
1264 case EOpInverseSqrt:
1265 outputTriplet(out, visit, "rsqrt(", "", ")");
1266 break;
1267 case EOpAbs:
1268 outputTriplet(out, visit, "abs(", "", ")");
1269 break;
1270 case EOpSign:
1271 outputTriplet(out, visit, "sign(", "", ")");
1272 break;
1273 case EOpFloor:
1274 outputTriplet(out, visit, "floor(", "", ")");
1275 break;
1276 case EOpTrunc:
1277 outputTriplet(out, visit, "trunc(", "", ")");
1278 break;
1279 case EOpRound:
1280 outputTriplet(out, visit, "round(", "", ")");
1281 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001282 case EOpRoundEven:
1283 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001284 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001285 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001286 case EOpCeil:
1287 outputTriplet(out, visit, "ceil(", "", ")");
1288 break;
1289 case EOpFract:
1290 outputTriplet(out, visit, "frac(", "", ")");
1291 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301292 case EOpIsNan:
Shao6f0a0dc2016-09-27 13:51:29 +08001293 if (node->getUseEmulatedFunction())
1294 writeEmulatedFunctionTriplet(out, visit, "isnan(");
1295 else
1296 outputTriplet(out, visit, "isnan(", "", ")");
1297 mRequiresIEEEStrictCompiling = true;
1298 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001299 case EOpIsInf:
1300 outputTriplet(out, visit, "isinf(", "", ")");
1301 break;
1302 case EOpFloatBitsToInt:
1303 outputTriplet(out, visit, "asint(", "", ")");
1304 break;
1305 case EOpFloatBitsToUint:
1306 outputTriplet(out, visit, "asuint(", "", ")");
1307 break;
1308 case EOpIntBitsToFloat:
1309 outputTriplet(out, visit, "asfloat(", "", ")");
1310 break;
1311 case EOpUintBitsToFloat:
1312 outputTriplet(out, visit, "asfloat(", "", ")");
1313 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001314 case EOpPackSnorm2x16:
1315 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001316 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001317 break;
1318 case EOpPackUnorm2x16:
1319 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001320 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001321 break;
1322 case EOpPackHalf2x16:
1323 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001324 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001325 break;
1326 case EOpUnpackSnorm2x16:
1327 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001328 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001329 break;
1330 case EOpUnpackUnorm2x16:
1331 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001332 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001333 break;
1334 case EOpUnpackHalf2x16:
1335 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001336 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001337 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001338 case EOpLength:
1339 outputTriplet(out, visit, "length(", "", ")");
1340 break;
1341 case EOpNormalize:
1342 outputTriplet(out, visit, "normalize(", "", ")");
1343 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001344 case EOpDFdx:
1345 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1346 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001347 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001348 }
1349 else
1350 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001351 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001352 }
1353 break;
1354 case EOpDFdy:
1355 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1356 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001357 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001358 }
1359 else
1360 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001361 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001362 }
1363 break;
1364 case EOpFwidth:
1365 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1366 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001367 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001368 }
1369 else
1370 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001371 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001372 }
1373 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001374 case EOpTranspose:
1375 outputTriplet(out, visit, "transpose(", "", ")");
1376 break;
1377 case EOpDeterminant:
1378 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1379 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001380 case EOpInverse:
1381 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001382 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001383 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001384
Jamie Madill8c46ab12015-12-07 16:39:19 -05001385 case EOpAny:
1386 outputTriplet(out, visit, "any(", "", ")");
1387 break;
1388 case EOpAll:
1389 outputTriplet(out, visit, "all(", "", ")");
1390 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001391 default: UNREACHABLE();
1392 }
1393
1394 return true;
1395}
1396
Olli Etuaho96963162016-03-21 11:54:33 +02001397TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1398{
1399 if (node->getAsSymbolNode())
1400 {
1401 return node->getAsSymbolNode()->getSymbol();
1402 }
1403 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1404 switch (nodeBinary->getOp())
1405 {
1406 case EOpIndexDirect:
1407 {
1408 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1409
1410 TInfoSinkBase prefixSink;
1411 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1412 return TString(prefixSink.c_str());
1413 }
1414 case EOpIndexDirectStruct:
1415 {
1416 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1417 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1418 const TField *field = s->fields()[index];
1419
1420 TInfoSinkBase prefixSink;
1421 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1422 << field->name();
1423 return TString(prefixSink.c_str());
1424 }
1425 default:
1426 UNREACHABLE();
1427 return TString("");
1428 }
1429}
1430
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001431bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1432{
1433 TInfoSinkBase &out = getInfoSink();
1434
1435 if (mInsideFunction)
1436 {
1437 outputLineDirective(out, node->getLine().first_line);
1438 out << "{\n";
1439 }
1440
1441 for (TIntermSequence::iterator sit = node->getSequence()->begin();
1442 sit != node->getSequence()->end(); sit++)
1443 {
1444 outputLineDirective(out, (*sit)->getLine().first_line);
1445
1446 (*sit)->traverse(this);
1447
1448 // Don't output ; after case labels, they're terminated by :
1449 // This is needed especially since outputting a ; after a case statement would turn empty
1450 // case statements into non-empty case statements, disallowing fall-through from them.
1451 // Also no need to output ; after if statements or sequences. This is done just for
1452 // code clarity.
1453 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
1454 (*sit)->getAsBlock() == nullptr)
1455 out << ";\n";
1456 }
1457
1458 if (mInsideFunction)
1459 {
1460 outputLineDirective(out, node->getLine().last_line);
1461 out << "}\n";
1462 }
1463
1464 return false;
1465}
1466
Olli Etuaho336b1472016-10-05 16:37:55 +01001467bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1468{
1469 TInfoSinkBase &out = getInfoSink();
1470
1471 ASSERT(mCurrentFunctionMetadata == nullptr);
1472
1473 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1474 ASSERT(index != CallDAG::InvalidIndex);
1475 mCurrentFunctionMetadata = &mASTMetadataList[index];
1476
1477 out << TypeString(node->getType()) << " ";
1478
1479 TIntermSequence *parameters = node->getFunctionParameters()->getSequence();
1480
1481 if (node->getFunctionSymbolInfo()->isMain())
1482 {
1483 out << "gl_main(";
1484 }
1485 else
1486 {
1487 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
1488 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1489 }
1490
1491 for (unsigned int i = 0; i < parameters->size(); i++)
1492 {
1493 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1494
1495 if (symbol)
1496 {
1497 ensureStructDefined(symbol->getType());
1498
1499 out << argumentString(symbol);
1500
1501 if (i < parameters->size() - 1)
1502 {
1503 out << ", ";
1504 }
1505 }
1506 else
1507 UNREACHABLE();
1508 }
1509
1510 out << ")\n";
1511
1512 mInsideFunction = true;
1513 // The function body node will output braces.
1514 node->getBody()->traverse(this);
1515 mInsideFunction = false;
1516
1517 mCurrentFunctionMetadata = nullptr;
1518
1519 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1520 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1521 {
1522 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1523 mOutputLod0Function = true;
1524 node->traverse(this);
1525 mOutputLod0Function = false;
1526 }
1527
1528 return false;
1529}
1530
Olli Etuaho13389b62016-10-16 11:48:18 +01001531bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1532{
1533 TInfoSinkBase &out = getInfoSink();
1534 if (visit == PreVisit)
1535 {
1536 TIntermSequence *sequence = node->getSequence();
1537 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1538 ASSERT(sequence->size() == 1);
1539
1540 if (variable &&
1541 (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
1542 variable->getQualifier() == EvqConst))
1543 {
1544 ensureStructDefined(variable->getType());
1545
1546 if (!variable->getAsSymbolNode() ||
1547 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1548 {
1549 if (!mInsideFunction)
1550 {
1551 out << "static ";
1552 }
1553
1554 out << TypeString(variable->getType()) + " ";
1555
1556 TIntermSymbol *symbol = variable->getAsSymbolNode();
1557
1558 if (symbol)
1559 {
1560 symbol->traverse(this);
1561 out << ArrayString(symbol->getType());
1562 out << " = " + initializer(symbol->getType());
1563 }
1564 else
1565 {
1566 variable->traverse(this);
1567 }
1568 }
1569 else if (variable->getAsSymbolNode() &&
1570 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1571 {
1572 // Already added to constructor map
1573 }
1574 else
1575 UNREACHABLE();
1576 }
1577 else if (variable && IsVaryingOut(variable->getQualifier()))
1578 {
1579 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
1580 {
1581 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1582
1583 if (symbol)
1584 {
1585 // Vertex (output) varyings which are declared but not written to should
1586 // still be declared to allow successful linking
1587 mReferencedVaryings[symbol->getSymbol()] = symbol;
1588 }
1589 else
1590 {
1591 (*sit)->traverse(this);
1592 }
1593 }
1594 }
1595 }
1596 return false;
1597}
1598
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001599bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1600{
Jamie Madill32aab012015-01-27 14:12:26 -05001601 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603 switch (node->getOp())
1604 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001605 case EOpInvariantDeclaration:
1606 // Do not do any translation
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001607 return false;
Olli Etuaho5878f832016-10-07 10:14:58 +01001608 case EOpPrototype:
1609 if (visit == PreVisit)
1610 {
Olli Etuahobd674552016-10-06 13:28:42 +01001611 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Olli Etuaho5878f832016-10-07 10:14:58 +01001612 // Skip the prototype if it is not implemented (and thus not used)
1613 if (index == CallDAG::InvalidIndex)
1614 {
1615 return false;
1616 }
1617
1618 TIntermSequence *arguments = node->getSequence();
1619
Olli Etuahobd674552016-10-06 13:28:42 +01001620 TString name =
1621 DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho5878f832016-10-07 10:14:58 +01001622 out << TypeString(node->getType()) << " " << name
1623 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
1624
1625 for (unsigned int i = 0; i < arguments->size(); i++)
1626 {
1627 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1628
1629 if (symbol)
1630 {
1631 out << argumentString(symbol);
1632
1633 if (i < arguments->size() - 1)
1634 {
1635 out << ", ";
1636 }
1637 }
1638 else
1639 UNREACHABLE();
1640 }
1641
1642 out << ");\n";
1643
1644 // Also prototype the Lod0 variant if needed
1645 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1646 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1647 {
1648 mOutputLod0Function = true;
1649 node->traverse(this);
1650 mOutputLod0Function = false;
1651 }
1652
1653 return false;
1654 }
1655 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001656 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001658 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001659
Corentin Wallez1239ee92015-03-19 14:38:02 -07001660 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001661 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001662 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001663 if (node->isArray())
1664 {
1665 UNIMPLEMENTED();
1666 }
Olli Etuahobd674552016-10-06 13:28:42 +01001667 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001668 ASSERT(index != CallDAG::InvalidIndex);
1669 lod0 &= mASTMetadataList[index].mNeedsLod0;
1670
Olli Etuahobd674552016-10-06 13:28:42 +01001671 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001672 out << DisambiguateFunctionName(node->getSequence());
1673 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674 }
Olli Etuahobd674552016-10-06 13:28:42 +01001675 else if (node->getFunctionSymbolInfo()->getNameObj().isInternal())
Olli Etuahob741c762016-06-29 15:49:22 +03001676 {
1677 // This path is used for internal functions that don't have their definitions in the
1678 // AST, such as precision emulation functions.
Olli Etuahobd674552016-10-06 13:28:42 +01001679 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681 else
1682 {
Olli Etuahobd674552016-10-06 13:28:42 +01001683 TString name = TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001684 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001685 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1686 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1687 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1688 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001689 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001690
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001691 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001692 {
Olli Etuaho96963162016-03-21 11:54:33 +02001693 TIntermTyped *typedArg = (*arg)->getAsTyped();
1694 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001695 {
1696 out << "texture_";
1697 (*arg)->traverse(this);
1698 out << ", sampler_";
1699 }
1700
1701 (*arg)->traverse(this);
1702
Olli Etuaho96963162016-03-21 11:54:33 +02001703 if (typedArg->getType().isStructureContainingSamplers())
1704 {
1705 const TType &argType = typedArg->getType();
1706 TVector<TIntermSymbol *> samplerSymbols;
1707 TString structName = samplerNamePrefixFromStruct(typedArg);
1708 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001709 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001710 &samplerSymbols, nullptr);
1711 for (const TIntermSymbol *sampler : samplerSymbols)
1712 {
1713 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1714 {
1715 out << ", texture_" << sampler->getSymbol();
1716 out << ", sampler_" << sampler->getSymbol();
1717 }
1718 else
1719 {
1720 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1721 // of D3D9, it's the sampler variable.
1722 out << ", " + sampler->getSymbol();
1723 }
1724 }
1725 }
1726
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001727 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001728 {
1729 out << ", ";
1730 }
1731 }
1732
1733 out << ")";
1734
1735 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001736 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001737 case EOpParameters:
1738 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1739 break;
1740 case EOpConstructFloat:
1741 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1742 break;
1743 case EOpConstructVec2:
1744 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1745 break;
1746 case EOpConstructVec3:
1747 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1748 break;
1749 case EOpConstructVec4:
1750 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1751 break;
1752 case EOpConstructBool:
1753 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1754 break;
1755 case EOpConstructBVec2:
1756 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1757 break;
1758 case EOpConstructBVec3:
1759 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1760 break;
1761 case EOpConstructBVec4:
1762 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1763 break;
1764 case EOpConstructInt:
1765 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1766 break;
1767 case EOpConstructIVec2:
1768 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1769 break;
1770 case EOpConstructIVec3:
1771 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1772 break;
1773 case EOpConstructIVec4:
1774 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1775 break;
1776 case EOpConstructUInt:
1777 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1778 break;
1779 case EOpConstructUVec2:
1780 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1781 break;
1782 case EOpConstructUVec3:
1783 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1784 break;
1785 case EOpConstructUVec4:
1786 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1787 break;
1788 case EOpConstructMat2:
1789 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1790 break;
1791 case EOpConstructMat2x3:
1792 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1793 break;
1794 case EOpConstructMat2x4:
1795 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1796 break;
1797 case EOpConstructMat3x2:
1798 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1799 break;
1800 case EOpConstructMat3:
1801 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1802 break;
1803 case EOpConstructMat3x4:
1804 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1805 break;
1806 case EOpConstructMat4x2:
1807 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1808 break;
1809 case EOpConstructMat4x3:
1810 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1811 break;
1812 case EOpConstructMat4:
1813 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1814 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001815 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001816 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001817 if (node->getType().isArray())
1818 {
1819 UNIMPLEMENTED();
1820 }
Jamie Madill033dae62014-06-18 12:56:28 -04001821 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001822 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001823 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001824 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001825 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001826 case EOpLessThan:
1827 outputTriplet(out, visit, "(", " < ", ")");
1828 break;
1829 case EOpGreaterThan:
1830 outputTriplet(out, visit, "(", " > ", ")");
1831 break;
1832 case EOpLessThanEqual:
1833 outputTriplet(out, visit, "(", " <= ", ")");
1834 break;
1835 case EOpGreaterThanEqual:
1836 outputTriplet(out, visit, "(", " >= ", ")");
1837 break;
1838 case EOpVectorEqual:
1839 outputTriplet(out, visit, "(", " == ", ")");
1840 break;
1841 case EOpVectorNotEqual:
1842 outputTriplet(out, visit, "(", " != ", ")");
1843 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001844 case EOpMod:
1845 ASSERT(node->getUseEmulatedFunction());
1846 writeEmulatedFunctionTriplet(out, visit, "mod(");
1847 break;
1848 case EOpModf:
1849 outputTriplet(out, visit, "modf(", ", ", ")");
1850 break;
1851 case EOpPow:
1852 outputTriplet(out, visit, "pow(", ", ", ")");
1853 break;
1854 case EOpAtan:
1855 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1856 ASSERT(node->getUseEmulatedFunction());
1857 writeEmulatedFunctionTriplet(out, visit, "atan(");
1858 break;
1859 case EOpMin:
1860 outputTriplet(out, visit, "min(", ", ", ")");
1861 break;
1862 case EOpMax:
1863 outputTriplet(out, visit, "max(", ", ", ")");
1864 break;
1865 case EOpClamp:
1866 outputTriplet(out, visit, "clamp(", ", ", ")");
1867 break;
1868 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301869 {
1870 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1871 if (lastParamNode->getType().getBasicType() == EbtBool)
1872 {
1873 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1874 // so use emulated version.
1875 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001876 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301877 }
1878 else
1879 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001880 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301881 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001882 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301883 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001884 case EOpStep:
1885 outputTriplet(out, visit, "step(", ", ", ")");
1886 break;
1887 case EOpSmoothStep:
1888 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1889 break;
1890 case EOpDistance:
1891 outputTriplet(out, visit, "distance(", ", ", ")");
1892 break;
1893 case EOpDot:
1894 outputTriplet(out, visit, "dot(", ", ", ")");
1895 break;
1896 case EOpCross:
1897 outputTriplet(out, visit, "cross(", ", ", ")");
1898 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001899 case EOpFaceForward:
1900 ASSERT(node->getUseEmulatedFunction());
1901 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
1902 break;
1903 case EOpReflect:
1904 outputTriplet(out, visit, "reflect(", ", ", ")");
1905 break;
1906 case EOpRefract:
1907 outputTriplet(out, visit, "refract(", ", ", ")");
1908 break;
1909 case EOpOuterProduct:
1910 ASSERT(node->getUseEmulatedFunction());
1911 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
1912 break;
1913 case EOpMul:
1914 outputTriplet(out, visit, "(", " * ", ")");
1915 break;
1916 default:
1917 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 }
1919
1920 return true;
1921}
1922
Olli Etuaho57961272016-09-14 13:57:46 +03001923void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001925 out << "if (";
1926
1927 node->getCondition()->traverse(this);
1928
1929 out << ")\n";
1930
Jamie Madill8c46ab12015-12-07 16:39:19 -05001931 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001932
1933 bool discard = false;
1934
1935 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001936 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001937 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001938 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001939
Olli Etuahoa6f22092015-05-08 18:31:10 +03001940 // Detect true discard
1941 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1942 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001943 else
1944 {
1945 // TODO(oetuaho): Check if the semicolon inside is necessary.
1946 // It's there as a result of conservative refactoring of the output.
1947 out << "{;}\n";
1948 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001949
Jamie Madill8c46ab12015-12-07 16:39:19 -05001950 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001951
Olli Etuahoa6f22092015-05-08 18:31:10 +03001952 if (node->getFalseBlock())
1953 {
1954 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001955
Jamie Madill8c46ab12015-12-07 16:39:19 -05001956 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957
Olli Etuaho32db19b2016-10-04 14:43:16 +01001958 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001959 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001960
Jamie Madill8c46ab12015-12-07 16:39:19 -05001961 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001962
Olli Etuahoa6f22092015-05-08 18:31:10 +03001963 // Detect false discard
1964 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1965 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001966
Olli Etuahoa6f22092015-05-08 18:31:10 +03001967 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001968 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001969 {
1970 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001971 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001972}
1973
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001974bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1975{
1976 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1977 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1978 UNREACHABLE();
1979 return false;
1980}
1981
Olli Etuaho57961272016-09-14 13:57:46 +03001982bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001983{
1984 TInfoSinkBase &out = getInfoSink();
1985
Olli Etuaho3d932d82016-04-12 11:10:30 +03001986 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001987
1988 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001989 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001990 {
1991 out << "FLATTEN ";
1992 }
1993
Olli Etuaho57961272016-09-14 13:57:46 +03001994 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001995
1996 return false;
1997}
1998
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001999bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002000{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002001 TInfoSinkBase &out = getInfoSink();
2002
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002003 if (node->getStatementList())
2004 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002005 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002006 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002007 // The curly braces get written when visiting the statementList aggregate
2008 }
2009 else
2010 {
2011 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002012 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002013 }
2014 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002015}
2016
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002017bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002018{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002019 TInfoSinkBase &out = getInfoSink();
2020
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002021 if (node->hasCondition())
2022 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002023 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002024 return true;
2025 }
2026 else
2027 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002028 out << "default:\n";
2029 return false;
2030 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002031}
2032
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2034{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002035 TInfoSinkBase &out = getInfoSink();
2036 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037}
2038
2039bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2040{
Nicolas Capens655fe362014-04-11 13:12:34 -04002041 mNestedLoopDepth++;
2042
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002043 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002044 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002045 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002046
Jamie Madill8c46ab12015-12-07 16:39:19 -05002047 TInfoSinkBase &out = getInfoSink();
2048
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002049 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002050 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002051 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002052 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002053 mInsideDiscontinuousLoop = wasDiscontinuous;
2054 mNestedLoopDepth--;
2055
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002056 return false;
2057 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002058 }
2059
Corentin Wallez1239ee92015-03-19 14:38:02 -07002060 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002061 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002063 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002064
Jamie Madill8c46ab12015-12-07 16:39:19 -05002065 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 }
2067 else
2068 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002069 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002070
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071 if (node->getInit())
2072 {
2073 node->getInit()->traverse(this);
2074 }
2075
2076 out << "; ";
2077
alokp@chromium.org52813552010-11-16 18:36:09 +00002078 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002080 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 }
2082
2083 out << "; ";
2084
alokp@chromium.org52813552010-11-16 18:36:09 +00002085 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002087 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 }
2089
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002090 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002091
Jamie Madill8c46ab12015-12-07 16:39:19 -05002092 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 }
2094
2095 if (node->getBody())
2096 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002097 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002098 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002100 else
2101 {
2102 // TODO(oetuaho): Check if the semicolon inside is necessary.
2103 // It's there as a result of conservative refactoring of the output.
2104 out << "{;}\n";
2105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106
Jamie Madill8c46ab12015-12-07 16:39:19 -05002107 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108
alokp@chromium.org52813552010-11-16 18:36:09 +00002109 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002111 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 out << "while(\n";
2113
alokp@chromium.org52813552010-11-16 18:36:09 +00002114 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115
daniel@transgaming.com73536982012-03-21 20:45:49 +00002116 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117 }
2118
daniel@transgaming.com73536982012-03-21 20:45:49 +00002119 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002121 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002122 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002123
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124 return false;
2125}
2126
2127bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2128{
Jamie Madill32aab012015-01-27 14:12:26 -05002129 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130
2131 switch (node->getFlowOp())
2132 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002133 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002134 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002135 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002136 case EOpBreak:
2137 if (visit == PreVisit)
2138 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002139 if (mNestedLoopDepth > 1)
2140 {
2141 mUsesNestedBreak = true;
2142 }
2143
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002144 if (mExcessiveLoopIndex)
2145 {
2146 out << "{Break";
2147 mExcessiveLoopIndex->traverse(this);
2148 out << " = true; break;}\n";
2149 }
2150 else
2151 {
2152 out << "break;\n";
2153 }
2154 }
2155 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002156 case EOpContinue:
2157 outputTriplet(out, visit, "continue;\n", "", "");
2158 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159 case EOpReturn:
2160 if (visit == PreVisit)
2161 {
2162 if (node->getExpression())
2163 {
2164 out << "return ";
2165 }
2166 else
2167 {
2168 out << "return;\n";
2169 }
2170 }
2171 else if (visit == PostVisit)
2172 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002173 if (node->getExpression())
2174 {
2175 out << ";\n";
2176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177 }
2178 break;
2179 default: UNREACHABLE();
2180 }
2181
2182 return true;
2183}
2184
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002185// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2186// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002187bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002188{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002189 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002190
2191 // Parse loops of the form:
2192 // for(int index = initial; index [comparator] limit; index += increment)
2193 TIntermSymbol *index = NULL;
2194 TOperator comparator = EOpNull;
2195 int initial = 0;
2196 int limit = 0;
2197 int increment = 0;
2198
2199 // Parse index name and intial value
2200 if (node->getInit())
2201 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002202 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002203
2204 if (init)
2205 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002206 TIntermSequence *sequence = init->getSequence();
2207 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002208
2209 if (variable && variable->getQualifier() == EvqTemporary)
2210 {
2211 TIntermBinary *assign = variable->getAsBinaryNode();
2212
2213 if (assign->getOp() == EOpInitialize)
2214 {
2215 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2216 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2217
2218 if (symbol && constant)
2219 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002220 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002221 {
2222 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002223 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002224 }
2225 }
2226 }
2227 }
2228 }
2229 }
2230
2231 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002232 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002233 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002234 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002235
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002236 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2237 {
2238 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2239
2240 if (constant)
2241 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002242 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002243 {
2244 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002245 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002246 }
2247 }
2248 }
2249 }
2250
2251 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002252 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002253 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002254 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2255 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002256
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002257 if (binaryTerminal)
2258 {
2259 TOperator op = binaryTerminal->getOp();
2260 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2261
2262 if (constant)
2263 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002264 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002265 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002266 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002267
2268 switch (op)
2269 {
2270 case EOpAddAssign: increment = value; break;
2271 case EOpSubAssign: increment = -value; break;
2272 default: UNIMPLEMENTED();
2273 }
2274 }
2275 }
2276 }
2277 else if (unaryTerminal)
2278 {
2279 TOperator op = unaryTerminal->getOp();
2280
2281 switch (op)
2282 {
2283 case EOpPostIncrement: increment = 1; break;
2284 case EOpPostDecrement: increment = -1; break;
2285 case EOpPreIncrement: increment = 1; break;
2286 case EOpPreDecrement: increment = -1; break;
2287 default: UNIMPLEMENTED();
2288 }
2289 }
2290 }
2291
2292 if (index != NULL && comparator != EOpNull && increment != 0)
2293 {
2294 if (comparator == EOpLessThanEqual)
2295 {
2296 comparator = EOpLessThan;
2297 limit += 1;
2298 }
2299
2300 if (comparator == EOpLessThan)
2301 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002302 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002303
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002304 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002305 {
2306 return false; // Not an excessive loop
2307 }
2308
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002309 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2310 mExcessiveLoopIndex = index;
2311
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002312 out << "{int ";
2313 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002314 out << ";\n"
2315 "bool Break";
2316 index->traverse(this);
2317 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002318
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002319 bool firstLoopFragment = true;
2320
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002321 while (iterations > 0)
2322 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002323 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002324
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002325 if (!firstLoopFragment)
2326 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002327 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002328 index->traverse(this);
2329 out << ") {\n";
2330 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002331
2332 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2333 {
2334 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2335 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002336
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002337 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002338 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002339
Corentin Wallez1239ee92015-03-19 14:38:02 -07002340 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002341 index->traverse(this);
2342 out << " = ";
2343 out << initial;
2344
2345 out << "; ";
2346 index->traverse(this);
2347 out << " < ";
2348 out << clampedLimit;
2349
2350 out << "; ";
2351 index->traverse(this);
2352 out << " += ";
2353 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002354 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002355
Jamie Madill8c46ab12015-12-07 16:39:19 -05002356 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002357 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002358
2359 if (node->getBody())
2360 {
2361 node->getBody()->traverse(this);
2362 }
2363
Jamie Madill8c46ab12015-12-07 16:39:19 -05002364 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002365 out << ";}\n";
2366
2367 if (!firstLoopFragment)
2368 {
2369 out << "}\n";
2370 }
2371
2372 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002373
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002374 initial += MAX_LOOP_ITERATIONS * increment;
2375 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002376 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002377
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002378 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002379
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002380 mExcessiveLoopIndex = restoreIndex;
2381
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002382 return true;
2383 }
2384 else UNIMPLEMENTED();
2385 }
2386
2387 return false; // Not handled as an excessive loop
2388}
2389
Jamie Madill8c46ab12015-12-07 16:39:19 -05002390void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2391 Visit visit,
2392 const char *preString,
2393 const char *inString,
2394 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002396 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397 {
2398 out << preString;
2399 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002400 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401 {
2402 out << inString;
2403 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002404 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405 {
2406 out << postString;
2407 }
2408}
2409
Jamie Madill8c46ab12015-12-07 16:39:19 -05002410void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002411{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002412 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002413 {
Jamie Madill32aab012015-01-27 14:12:26 -05002414 out << "\n";
2415 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002416
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002417 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002418 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002419 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002420 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002421
Jamie Madill32aab012015-01-27 14:12:26 -05002422 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002423 }
2424}
2425
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002426TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2427{
2428 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002429 const TType &type = symbol->getType();
2430 const TName &name = symbol->getName();
2431 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002432
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002433 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002434 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002435 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002436 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002437 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002438 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002439 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002440 }
2441
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002442 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002443 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002444 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2445 {
2446 // Samplers are passed as indices to the sampler array.
2447 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2448 return "const uint " + nameStr + ArrayString(type);
2449 }
2450 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2451 {
2452 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2453 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2454 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2455 ArrayString(type);
2456 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002457 }
2458
Olli Etuaho96963162016-03-21 11:54:33 +02002459 TStringStream argString;
2460 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2461 << ArrayString(type);
2462
2463 // If the structure parameter contains samplers, they need to be passed into the function as
2464 // separate parameters. HLSL doesn't natively support samplers in structs.
2465 if (type.isStructureContainingSamplers())
2466 {
2467 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2468 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002469 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002470 for (const TIntermSymbol *sampler : samplerSymbols)
2471 {
2472 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2473 {
2474 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2475 }
2476 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2477 {
2478 const TType &samplerType = sampler->getType();
2479 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2480 type.getArraySize() == samplerType.getArraySize());
2481 ASSERT(IsSampler(samplerType.getBasicType()));
2482 argString << ", " << QualifierString(qualifier) << " "
2483 << TextureString(samplerType.getBasicType()) << " texture_"
2484 << sampler->getSymbol() << ArrayString(type) << ", "
2485 << QualifierString(qualifier) << " "
2486 << SamplerString(samplerType.getBasicType()) << " sampler_"
2487 << sampler->getSymbol() << ArrayString(type);
2488 }
2489 else
2490 {
2491 const TType &samplerType = sampler->getType();
2492 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2493 type.getArraySize() == samplerType.getArraySize());
2494 ASSERT(IsSampler(samplerType.getBasicType()));
2495 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2496 << " " << sampler->getSymbol() << ArrayString(type);
2497 }
2498 }
2499 }
2500
2501 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502}
2503
2504TString OutputHLSL::initializer(const TType &type)
2505{
2506 TString string;
2507
Jamie Madill94bf7f22013-07-08 13:31:15 -04002508 size_t size = type.getObjectSize();
2509 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002510 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002511 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512
Jamie Madill94bf7f22013-07-08 13:31:15 -04002513 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514 {
2515 string += ", ";
2516 }
2517 }
2518
daniel@transgaming.comead23042010-04-29 03:35:36 +00002519 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002520}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002521
Jamie Madill8c46ab12015-12-07 16:39:19 -05002522void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2523 Visit visit,
2524 const TType &type,
2525 const char *name,
2526 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002527{
Olli Etuahof40319e2015-03-10 14:33:00 +02002528 if (type.isArray())
2529 {
2530 UNIMPLEMENTED();
2531 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002532
2533 if (visit == PreVisit)
2534 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002535 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002536
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002537 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002538 }
2539 else if (visit == InVisit)
2540 {
2541 out << ", ";
2542 }
2543 else if (visit == PostVisit)
2544 {
2545 out << ")";
2546 }
2547}
2548
Jamie Madill8c46ab12015-12-07 16:39:19 -05002549const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2550 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002551 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002552{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002553 const TConstantUnion *constUnionIterated = constUnion;
2554
Jamie Madill98493dd2013-07-08 14:39:03 -04002555 const TStructure* structure = type.getStruct();
2556 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002557 {
Jamie Madill033dae62014-06-18 12:56:28 -04002558 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002559
Jamie Madill98493dd2013-07-08 14:39:03 -04002560 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002561
Jamie Madill98493dd2013-07-08 14:39:03 -04002562 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002563 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002564 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002565 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002566
Jamie Madill98493dd2013-07-08 14:39:03 -04002567 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002568 {
2569 out << ", ";
2570 }
2571 }
2572
2573 out << ")";
2574 }
2575 else
2576 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002577 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002578 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002579
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002580 if (writeType)
2581 {
Jamie Madill033dae62014-06-18 12:56:28 -04002582 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002583 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002584 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002585 if (writeType)
2586 {
2587 out << ")";
2588 }
2589 }
2590
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002591 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002592}
2593
Jamie Madill8c46ab12015-12-07 16:39:19 -05002594void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002595{
2596 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002597 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002598}
2599
Jamie Madill37997142015-01-28 10:06:34 -05002600bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2601{
2602 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2603 expression->traverse(&searchSymbol);
2604
2605 if (searchSymbol.foundMatch())
2606 {
2607 // Type already printed
2608 out << "t" + str(mUniqueIndex) + " = ";
2609 expression->traverse(this);
2610 out << ", ";
2611 symbolNode->traverse(this);
2612 out << " = t" + str(mUniqueIndex);
2613
2614 mUniqueIndex++;
2615 return true;
2616 }
2617
2618 return false;
2619}
2620
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002621bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2622{
2623 // We support writing constant unions and constructors that only take constant unions as
2624 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002625 return expression->getAsConstantUnion() ||
2626 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002627}
2628
2629bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2630 TIntermSymbol *symbolNode,
2631 TIntermTyped *expression)
2632{
2633 if (canWriteAsHLSLLiteral(expression))
2634 {
2635 symbolNode->traverse(this);
2636 if (expression->getType().isArray())
2637 {
2638 out << "[" << expression->getType().getArraySize() << "]";
2639 }
2640 out << " = {";
2641 if (expression->getAsConstantUnion())
2642 {
2643 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2644 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002645 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002646 }
2647 else
2648 {
2649 TIntermAggregate *constructor = expression->getAsAggregate();
2650 ASSERT(constructor != nullptr);
2651 for (TIntermNode *&node : *constructor->getSequence())
2652 {
2653 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2654 ASSERT(nodeConst);
2655 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002656 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002657 if (node != constructor->getSequence()->back())
2658 {
2659 out << ", ";
2660 }
2661 }
2662 }
2663 out << "}";
2664 return true;
2665 }
2666 return false;
2667}
2668
Jamie Madill55e79e02015-02-09 15:35:00 -05002669TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2670{
2671 const TFieldList &fields = structure.fields();
2672
2673 for (const auto &eqFunction : mStructEqualityFunctions)
2674 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002675 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002676 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002677 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002678 }
2679 }
2680
2681 const TString &structNameString = StructNameString(structure);
2682
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002683 StructEqualityFunction *function = new StructEqualityFunction();
2684 function->structure = &structure;
2685 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002686
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002687 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002688
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002689 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2690 << "{\n"
2691 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002692
2693 for (size_t i = 0; i < fields.size(); i++)
2694 {
2695 const TField *field = fields[i];
2696 const TType *fieldType = field->type();
2697
2698 const TString &fieldNameA = "a." + Decorate(field->name());
2699 const TString &fieldNameB = "b." + Decorate(field->name());
2700
2701 if (i > 0)
2702 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002703 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002704 }
2705
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002706 fnOut << "(";
2707 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2708 fnOut << fieldNameA;
2709 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2710 fnOut << fieldNameB;
2711 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2712 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002713 }
2714
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002715 fnOut << ";\n" << "}\n";
2716
2717 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002718
2719 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002720 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002721
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002722 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002723}
2724
Olli Etuaho7fb49552015-03-18 17:27:44 +02002725TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2726{
2727 for (const auto &eqFunction : mArrayEqualityFunctions)
2728 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002729 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002730 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002731 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002732 }
2733 }
2734
2735 const TString &typeName = TypeString(type);
2736
Olli Etuaho12690762015-03-31 12:55:28 +03002737 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002738 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002739
2740 TInfoSinkBase fnNameOut;
2741 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002742 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002743
2744 TType nonArrayType = type;
2745 nonArrayType.clearArrayness();
2746
2747 TInfoSinkBase fnOut;
2748
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002749 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002750 << typeName << " a[" << type.getArraySize() << "], "
2751 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002752 << "{\n"
2753 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2754 " {\n"
2755 " if (";
2756
2757 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2758 fnOut << "a[i]";
2759 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2760 fnOut << "b[i]";
2761 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2762
2763 fnOut << ") { return false; }\n"
2764 " }\n"
2765 " return true;\n"
2766 "}\n";
2767
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002768 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002769
2770 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002771 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002772
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002773 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002774}
2775
Olli Etuaho12690762015-03-31 12:55:28 +03002776TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2777{
2778 for (const auto &assignFunction : mArrayAssignmentFunctions)
2779 {
2780 if (assignFunction.type == type)
2781 {
2782 return assignFunction.functionName;
2783 }
2784 }
2785
2786 const TString &typeName = TypeString(type);
2787
2788 ArrayHelperFunction function;
2789 function.type = type;
2790
2791 TInfoSinkBase fnNameOut;
2792 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2793 function.functionName = fnNameOut.c_str();
2794
2795 TInfoSinkBase fnOut;
2796
2797 fnOut << "void " << function.functionName << "(out "
2798 << typeName << " a[" << type.getArraySize() << "], "
2799 << typeName << " b[" << type.getArraySize() << "])\n"
2800 << "{\n"
2801 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2802 " {\n"
2803 " a[i] = b[i];\n"
2804 " }\n"
2805 "}\n";
2806
2807 function.functionDefinition = fnOut.c_str();
2808
2809 mArrayAssignmentFunctions.push_back(function);
2810
2811 return function.functionName;
2812}
2813
Olli Etuaho9638c352015-04-01 14:34:52 +03002814TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2815{
2816 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2817 {
2818 if (constructIntoFunction.type == type)
2819 {
2820 return constructIntoFunction.functionName;
2821 }
2822 }
2823
2824 const TString &typeName = TypeString(type);
2825
2826 ArrayHelperFunction function;
2827 function.type = type;
2828
2829 TInfoSinkBase fnNameOut;
2830 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2831 function.functionName = fnNameOut.c_str();
2832
2833 TInfoSinkBase fnOut;
2834
2835 fnOut << "void " << function.functionName << "(out "
2836 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002837 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002838 {
2839 fnOut << ", " << typeName << " b" << i;
2840 }
2841 fnOut << ")\n"
2842 "{\n";
2843
Olli Etuaho856c4972016-08-08 11:38:39 +03002844 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002845 {
2846 fnOut << " a[" << i << "] = b" << i << ";\n";
2847 }
2848 fnOut << "}\n";
2849
2850 function.functionDefinition = fnOut.c_str();
2851
2852 mArrayConstructIntoFunctions.push_back(function);
2853
2854 return function.functionName;
2855}
2856
Jamie Madill2e295e22015-04-29 10:41:33 -04002857void OutputHLSL::ensureStructDefined(const TType &type)
2858{
2859 TStructure *structure = type.getStruct();
2860
2861 if (structure)
2862 {
2863 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2864 }
2865}
2866
Jamie Madill45bcc782016-11-07 13:58:48 -05002867} // namespace sh