blob: eda90a0d42423b473565003359da2883a925e3db [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
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500108 mUsesFragColor = false;
109 mUsesFragData = false;
110 mUsesDepthRange = false;
111 mUsesFragCoord = false;
112 mUsesPointCoord = false;
113 mUsesFrontFacing = false;
114 mUsesPointSize = false;
115 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500116 mUsesVertexID = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500117 mUsesFragDepth = false;
118 mUsesXor = false;
119 mUsesDiscardRewriting = false;
120 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
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500125 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000126 mInsideDiscontinuousLoop = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500127 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000128
129 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000130
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500131 mStructureHLSL = new StructureHLSL;
132 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.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500138 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
139 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530140 // In both cases total 3 uniform registers need to be reserved.
141 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000142 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000143
Geoff Lang00140f42016-02-03 18:47:33 +0000144 // Reserve registers for the default uniform block and driver constants
145 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146}
147
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000148OutputHLSL::~OutputHLSL()
149{
Jamie Madill8daaba12014-06-13 10:04:33 -0400150 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400151 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300152 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200153 for (auto &eqFunction : mStructEqualityFunctions)
154 {
155 SafeDelete(eqFunction);
156 }
157 for (auto &eqFunction : mArrayEqualityFunctions)
158 {
159 SafeDelete(eqFunction);
160 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000161}
162
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200163void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000164{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500165 const std::vector<TIntermTyped *> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400166 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000167
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200168 BuiltInFunctionEmulator builtInFunctionEmulator;
169 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800170 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
171 {
172 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
173 mShaderVersion);
174 }
175
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200176 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500177
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700178 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000179 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700180 ASSERT(success == CallDAG::INITDAG_SUCCESS);
181 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700182
Jamie Madill37997142015-01-28 10:06:34 -0500183 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500184 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200185 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500186 mInfoSinkStack.pop();
187
Jamie Madill37997142015-01-28 10:06:34 -0500188 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500189 mInfoSinkStack.pop();
190
Jamie Madill32aab012015-01-27 14:12:26 -0500191 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500192 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500193 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000194
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200195 objSink << mHeader.c_str();
196 objSink << mBody.c_str();
197 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200198
199 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000200}
201
Jamie Madill570e04d2013-06-21 09:15:33 -0400202void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
203{
204 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
205 {
206 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
207
Jamie Madill32aab012015-01-27 14:12:26 -0500208 TInfoSinkBase structInfoSink;
209 mInfoSinkStack.push(&structInfoSink);
210
Jamie Madill570e04d2013-06-21 09:15:33 -0400211 // This will mark the necessary block elements as referenced
212 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500213
214 TString structName(structInfoSink.c_str());
215 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400216
217 mFlaggedStructOriginalNames[flaggedNode] = structName;
218
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500219 for (size_t pos = structName.find('.'); pos != std::string::npos;
220 pos = structName.find('.'))
Jamie Madill570e04d2013-06-21 09:15:33 -0400221 {
222 structName.erase(pos, 1);
223 }
224
225 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
226 }
227}
228
Jamie Madill4e1fd412014-07-10 17:50:10 -0400229const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
230{
231 return mUniformHLSL->getInterfaceBlockRegisterMap();
232}
233
Jamie Madill9fe25e92014-07-18 10:33:08 -0400234const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
235{
236 return mUniformHLSL->getUniformRegisterMap();
237}
238
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000239int OutputHLSL::vectorSize(const TType &type) const
240{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500241 int elementSize = type.isMatrix() ? type.getCols() : 1;
Olli Etuaho856c4972016-08-08 11:38:39 +0300242 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000243
244 return elementSize * arraySize;
245}
246
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500247TString OutputHLSL::structInitializerString(int indent,
248 const TStructure &structure,
249 const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400250{
251 TString init;
252
253 TString preIndentString;
254 TString fullIndentString;
255
256 for (int spaces = 0; spaces < (indent * 4); spaces++)
257 {
258 preIndentString += ' ';
259 }
260
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500261 for (int spaces = 0; spaces < ((indent + 1) * 4); spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400262 {
263 fullIndentString += ' ';
264 }
265
266 init += preIndentString + "{\n";
267
Jamie Madill98493dd2013-07-08 14:39:03 -0400268 const TFieldList &fields = structure.fields();
269 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400270 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500271 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400272 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500273 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400274
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400276 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400278 }
279 else
280 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400282 }
283 }
284
285 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
286
287 return init;
288}
289
Jamie Madill8c46ab12015-12-07 16:39:19 -0500290void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000292 TString varyings;
293 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400294 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000295
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500296 for (std::map<TIntermTyped *, TString>::const_iterator flaggedStructIt =
297 mFlaggedStructMappedNames.begin();
298 flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400299 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500300 TIntermTyped *structNode = flaggedStructIt->first;
301 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400302 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400303 const TString &originalName = mFlaggedStructOriginalNames[structNode];
304
Jamie Madill033dae62014-06-18 12:56:28 -0400305 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400306 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 flaggedStructs += "\n";
308 }
309
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500310 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin();
311 varying != mReferencedVaryings.end(); varying++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000312 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500313 const TType &type = varying->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000314 const TString &name = varying->second->getSymbol();
315
316 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500317 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
318 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000319 }
320
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500321 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin();
322 attribute != mReferencedAttributes.end(); attribute++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000323 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324 const TType &type = attribute->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000325 const TString &name = attribute->second->getSymbol();
326
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500327 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
328 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000329 }
330
Jamie Madill8daaba12014-06-13 10:04:33 -0400331 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400332
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200333 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400334 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
335
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200336 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500337 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200338 out << "\n// Equality functions\n\n";
339 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500340 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200341 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200342 }
343 }
Olli Etuaho12690762015-03-31 12:55:28 +0300344 if (!mArrayAssignmentFunctions.empty())
345 {
346 out << "\n// Assignment functions\n\n";
347 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
348 {
349 out << assignmentFunction.functionDefinition << "\n";
350 }
351 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300352 if (!mArrayConstructIntoFunctions.empty())
353 {
354 out << "\n// Array constructor functions\n\n";
355 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
356 {
357 out << constructIntoFunction.functionDefinition << "\n";
358 }
359 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200360
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500361 if (mUsesDiscardRewriting)
362 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400363 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500364 }
365
Nicolas Capens655fe362014-04-11 13:12:34 -0400366 if (mUsesNestedBreak)
367 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400368 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400369 }
370
Arun Patole44efa0b2015-03-04 17:11:05 +0530371 if (mRequiresIEEEStrictCompiling)
372 {
373 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
374 }
375
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400376 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
377 "#define LOOP [loop]\n"
378 "#define FLATTEN [flatten]\n"
379 "#else\n"
380 "#define LOOP\n"
381 "#define FLATTEN\n"
382 "#endif\n";
383
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200384 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200386 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500387 const bool usingMRTExtension = (iter != mExtensionBehavior.end() &&
388 (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000389
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000390 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500391 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400392 out << "\n";
393
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200394 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000395 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500396 for (ReferencedSymbols::const_iterator outputVariableIt =
397 mReferencedOutputVariables.begin();
398 outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000399 {
Jamie Madill46131a32013-06-20 11:55:50 -0400400 const TString &variableName = outputVariableIt->first;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500401 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400402
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500403 out << "static " + TypeString(variableType) + " out_" + variableName +
404 ArrayString(variableType) + " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000405 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000406 }
Jamie Madill46131a32013-06-20 11:55:50 -0400407 else
408 {
409 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
410
411 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500412 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400413 for (unsigned int i = 0; i < numColorValues; i++)
414 {
415 out << " float4(0, 0, 0, 0)";
416 if (i + 1 != numColorValues)
417 {
418 out << ",";
419 }
420 out << "\n";
421 }
422
423 out << "};\n";
424 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000425
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400426 if (mUsesFragDepth)
427 {
428 out << "static float gl_Depth = 0.0;\n";
429 }
430
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000431 if (mUsesFragCoord)
432 {
433 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
434 }
435
436 if (mUsesPointCoord)
437 {
438 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
439 }
440
441 if (mUsesFrontFacing)
442 {
443 out << "static bool gl_FrontFacing = false;\n";
444 }
445
446 out << "\n";
447
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000448 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000449 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000450 out << "struct gl_DepthRangeParameters\n"
451 "{\n"
452 " float near;\n"
453 " float far;\n"
454 " float diff;\n"
455 "};\n"
456 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000457 }
458
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200459 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000460 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000461 out << "cbuffer DriverConstants : register(b1)\n"
462 "{\n";
463
464 if (mUsesDepthRange)
465 {
466 out << " float3 dx_DepthRange : packoffset(c0);\n";
467 }
468
469 if (mUsesFragCoord)
470 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000471 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000472 }
473
474 if (mUsesFragCoord || mUsesFrontFacing)
475 {
476 out << " float3 dx_DepthFront : packoffset(c2);\n";
477 }
478
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800479 if (mUsesFragCoord)
480 {
481 // dx_ViewScale is only used in the fragment shader to correct
482 // the value for glFragCoord if necessary
483 out << " float2 dx_ViewScale : packoffset(c3);\n";
484 }
485
Olli Etuaho618bebc2016-01-15 16:40:00 +0200486 if (mOutputType == SH_HLSL_4_1_OUTPUT)
487 {
488 mUniformHLSL->samplerMetadataUniforms(out, "c4");
489 }
490
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000491 out << "};\n";
492 }
493 else
494 {
495 if (mUsesDepthRange)
496 {
497 out << "uniform float3 dx_DepthRange : register(c0);";
498 }
499
500 if (mUsesFragCoord)
501 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000502 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000503 }
504
505 if (mUsesFragCoord || mUsesFrontFacing)
506 {
507 out << "uniform float3 dx_DepthFront : register(c2);\n";
508 }
509 }
510
511 out << "\n";
512
513 if (mUsesDepthRange)
514 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500515 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
516 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000517 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000518 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000519
Jamie Madillf91ce812014-06-13 10:04:34 -0400520 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000521 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400522 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000523 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400524 out << flaggedStructs;
525 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000526 }
527
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000528 if (usingMRTExtension && mNumRenderTargets > 1)
529 {
530 out << "#define GL_USES_MRT\n";
531 }
532
533 if (mUsesFragColor)
534 {
535 out << "#define GL_USES_FRAG_COLOR\n";
536 }
537
538 if (mUsesFragData)
539 {
540 out << "#define GL_USES_FRAG_DATA\n";
541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500543 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000545 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500546 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000547 out << "\n"
548 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400549
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000550 if (mUsesPointSize)
551 {
552 out << "static float gl_PointSize = float(1);\n";
553 }
554
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000555 if (mUsesInstanceID)
556 {
557 out << "static int gl_InstanceID;";
558 }
559
Corentin Wallezb076add2016-01-11 16:45:46 -0500560 if (mUsesVertexID)
561 {
562 out << "static int gl_VertexID;";
563 }
564
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000565 out << "\n"
566 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500567 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000568 out << "\n";
569
570 if (mUsesDepthRange)
571 {
572 out << "struct gl_DepthRangeParameters\n"
573 "{\n"
574 " float near;\n"
575 " float far;\n"
576 " float diff;\n"
577 "};\n"
578 "\n";
579 }
580
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200581 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000582 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800583 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500584 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800585
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000586 if (mUsesDepthRange)
587 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800588 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000589 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800590
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800591 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
592 // shaders. However, we declare it for all shaders (including Feature Level 10+).
593 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
594 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800595 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800596 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800597 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800598
Olli Etuaho618bebc2016-01-15 16:40:00 +0200599 if (mOutputType == SH_HLSL_4_1_OUTPUT)
600 {
601 mUniformHLSL->samplerMetadataUniforms(out, "c4");
602 }
603
Austin Kinross4fd18b12014-12-22 12:32:05 -0800604 out << "};\n"
605 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000606 }
607 else
608 {
609 if (mUsesDepthRange)
610 {
611 out << "uniform float3 dx_DepthRange : register(c0);\n";
612 }
613
Cooper Partine6664f02015-01-09 16:22:24 -0800614 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
615 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000616 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000617 }
618
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000619 if (mUsesDepthRange)
620 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500621 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
622 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000623 "\n";
624 }
625
Jamie Madillf91ce812014-06-13 10:04:34 -0400626 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000627 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400628 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000629 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400630 out << flaggedStructs;
631 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000632 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400633 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000634
Geoff Lang1fe74c72016-08-25 13:23:01 -0400635 bool getDimensionsIgnoresBaseLevel =
636 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
637 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000638
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000639 if (mUsesFragCoord)
640 {
641 out << "#define GL_USES_FRAG_COORD\n";
642 }
643
644 if (mUsesPointCoord)
645 {
646 out << "#define GL_USES_POINT_COORD\n";
647 }
648
649 if (mUsesFrontFacing)
650 {
651 out << "#define GL_USES_FRONT_FACING\n";
652 }
653
654 if (mUsesPointSize)
655 {
656 out << "#define GL_USES_POINT_SIZE\n";
657 }
658
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400659 if (mUsesFragDepth)
660 {
661 out << "#define GL_USES_FRAG_DEPTH\n";
662 }
663
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000664 if (mUsesDepthRange)
665 {
666 out << "#define GL_USES_DEPTH_RANGE\n";
667 }
668
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000669 if (mUsesXor)
670 {
671 out << "bool xor(bool p, bool q)\n"
672 "{\n"
673 " return (p || q) && !(p && q);\n"
674 "}\n"
675 "\n";
676 }
677
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200678 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679}
680
681void OutputHLSL::visitSymbol(TIntermSymbol *node)
682{
Jamie Madill32aab012015-01-27 14:12:26 -0500683 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684
Jamie Madill570e04d2013-06-21 09:15:33 -0400685 // Handle accessing std140 structs by value
686 if (mFlaggedStructMappedNames.count(node) > 0)
687 {
688 out << mFlaggedStructMappedNames[node];
689 return;
690 }
691
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692 TString name = node->getSymbol();
693
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000694 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000695 {
696 mUsesDepthRange = true;
697 out << name;
698 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699 else
700 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000701 TQualifier qualifier = node->getQualifier();
702
703 if (qualifier == EvqUniform)
704 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500705 const TType &nodeType = node->getType();
Jamie Madill2e295e22015-04-29 10:41:33 -0400706 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400707
708 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000709 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400710 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000711 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000712 else
713 {
714 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000715 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400716
Jamie Madill2e295e22015-04-29 10:41:33 -0400717 ensureStructDefined(nodeType);
718
Olli Etuaho96963162016-03-21 11:54:33 +0200719 const TName &nameWithMetadata = node->getName();
720 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000721 }
Jamie Madill19571812013-08-12 15:26:34 -0700722 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000723 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000724 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400725 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000726 }
Jamie Madill033dae62014-06-18 12:56:28 -0400727 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000728 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000729 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400730 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000731 }
Jamie Madill19571812013-08-12 15:26:34 -0700732 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400733 {
734 mReferencedOutputVariables[name] = node;
735 out << "out_" << name;
736 }
737 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000738 {
739 out << "gl_Color[0]";
740 mUsesFragColor = true;
741 }
742 else if (qualifier == EvqFragData)
743 {
744 out << "gl_Color";
745 mUsesFragData = true;
746 }
747 else if (qualifier == EvqFragCoord)
748 {
749 mUsesFragCoord = true;
750 out << name;
751 }
752 else if (qualifier == EvqPointCoord)
753 {
754 mUsesPointCoord = true;
755 out << name;
756 }
757 else if (qualifier == EvqFrontFacing)
758 {
759 mUsesFrontFacing = true;
760 out << name;
761 }
762 else if (qualifier == EvqPointSize)
763 {
764 mUsesPointSize = true;
765 out << name;
766 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000767 else if (qualifier == EvqInstanceID)
768 {
769 mUsesInstanceID = true;
770 out << name;
771 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500772 else if (qualifier == EvqVertexID)
773 {
774 mUsesVertexID = true;
775 out << name;
776 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300777 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400778 {
779 mUsesFragDepth = true;
780 out << "gl_Depth";
781 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000782 else
783 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300784 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786 }
787}
788
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400789void OutputHLSL::visitRaw(TIntermRaw *node)
790{
Jamie Madill32aab012015-01-27 14:12:26 -0500791 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400792}
793
Olli Etuaho7fb49552015-03-18 17:27:44 +0200794void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
795{
796 if (type.isScalar() && !type.isArray())
797 {
798 if (op == EOpEqual)
799 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500800 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200801 }
802 else
803 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500804 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200805 }
806 }
807 else
808 {
809 if (visit == PreVisit && op == EOpNotEqual)
810 {
811 out << "!";
812 }
813
814 if (type.isArray())
815 {
816 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500817 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200818 }
819 else if (type.getBasicType() == EbtStruct)
820 {
821 const TStructure &structure = *type.getStruct();
822 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500823 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200824 }
825 else
826 {
827 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500828 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200829 }
830 }
831}
832
Olli Etuaho96963162016-03-21 11:54:33 +0200833bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
834{
835 // Inside InVisit the current node is already in the path.
836 const unsigned int initialN = visit == InVisit ? 1u : 0u;
837 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
838 {
839 TIntermNode *ancestor = getAncestorNode(n);
840 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
841 if (ancestorBinary == nullptr)
842 {
843 return false;
844 }
845 switch (ancestorBinary->getOp())
846 {
847 case EOpIndexDirectStruct:
848 {
849 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
850 const TIntermConstantUnion *index =
851 ancestorBinary->getRight()->getAsConstantUnion();
852 const TField *field = structure->fields()[index->getIConst(0)];
853 if (IsSampler(field->type()->getBasicType()))
854 {
855 return true;
856 }
857 break;
858 }
859 case EOpIndexDirect:
860 break;
861 default:
862 // Returning a sampler from indirect indexing is not supported.
863 return false;
864 }
865 }
866 return false;
867}
868
Olli Etuahob6fa0432016-09-28 16:28:05 +0100869bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
870{
871 TInfoSinkBase &out = getInfoSink();
872 if (visit == PostVisit)
873 {
874 out << ".";
875 node->writeOffsetsAsXYZW(&out);
876 }
877 return true;
878}
879
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
881{
Jamie Madill32aab012015-01-27 14:12:26 -0500882 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883
Jamie Madill570e04d2013-06-21 09:15:33 -0400884 // Handle accessing std140 structs by value
885 if (mFlaggedStructMappedNames.count(node) > 0)
886 {
887 out << mFlaggedStructMappedNames[node];
888 return false;
889 }
890
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891 switch (node->getOp())
892 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100893 case EOpComma:
894 outputTriplet(out, visit, "(", ", ", ")");
895 break;
896 case EOpAssign:
897 if (node->getLeft()->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +0300898 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100899 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
900 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +0300901 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100902 const TString &functionName = addArrayConstructIntoFunction(node->getType());
903 out << functionName << "(";
904 node->getLeft()->traverse(this);
905 TIntermSequence *seq = rightAgg->getSequence();
906 for (auto &arrayElement : *seq)
907 {
908 out << ", ";
909 arrayElement->traverse(this);
910 }
911 out << ")";
912 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +0300913 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100914 // ArrayReturnValueToOutParameter should have eliminated expressions where a
915 // function call is assigned.
916 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
917
918 const TString &functionName = addArrayAssignmentFunction(node->getType());
919 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho9638c352015-04-01 14:34:52 +0300920 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100921 else
Jamie Madill37997142015-01-28 10:06:34 -0500922 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100923 outputTriplet(out, visit, "(", " = ", ")");
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000924 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100925 break;
926 case EOpInitialize:
927 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200928 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100929 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
930 ASSERT(symbolNode);
931 TIntermTyped *expression = node->getRight();
932
933 // Global initializers must be constant at this point.
934 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
935 canWriteAsHLSLLiteral(expression));
936
937 // GLSL allows to write things like "float x = x;" where a new variable x is defined
938 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
939 // new variable is created before the assignment is evaluated), so we need to
940 // convert
941 // this to "float t = x, x = t;".
942 if (writeSameSymbolInitializer(out, symbolNode, expression))
943 {
944 // Skip initializing the rest of the expression
945 return false;
946 }
947 else if (writeConstantInitialization(out, symbolNode, expression))
948 {
949 return false;
950 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200951 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100952 else if (visit == InVisit)
953 {
954 out << " = ";
955 }
956 break;
957 case EOpAddAssign:
958 outputTriplet(out, visit, "(", " += ", ")");
959 break;
960 case EOpSubAssign:
961 outputTriplet(out, visit, "(", " -= ", ")");
962 break;
963 case EOpMulAssign:
964 outputTriplet(out, visit, "(", " *= ", ")");
965 break;
966 case EOpVectorTimesScalarAssign:
967 outputTriplet(out, visit, "(", " *= ", ")");
968 break;
969 case EOpMatrixTimesScalarAssign:
970 outputTriplet(out, visit, "(", " *= ", ")");
971 break;
972 case EOpVectorTimesMatrixAssign:
973 if (visit == PreVisit)
974 {
975 out << "(";
976 }
977 else if (visit == InVisit)
978 {
979 out << " = mul(";
980 node->getLeft()->traverse(this);
981 out << ", transpose(";
982 }
983 else
984 {
985 out << ")))";
986 }
987 break;
988 case EOpMatrixTimesMatrixAssign:
989 if (visit == PreVisit)
990 {
991 out << "(";
992 }
993 else if (visit == InVisit)
994 {
995 out << " = transpose(mul(transpose(";
996 node->getLeft()->traverse(this);
997 out << "), transpose(";
998 }
999 else
1000 {
1001 out << "))))";
1002 }
1003 break;
1004 case EOpDivAssign:
1005 outputTriplet(out, visit, "(", " /= ", ")");
1006 break;
1007 case EOpIModAssign:
1008 outputTriplet(out, visit, "(", " %= ", ")");
1009 break;
1010 case EOpBitShiftLeftAssign:
1011 outputTriplet(out, visit, "(", " <<= ", ")");
1012 break;
1013 case EOpBitShiftRightAssign:
1014 outputTriplet(out, visit, "(", " >>= ", ")");
1015 break;
1016 case EOpBitwiseAndAssign:
1017 outputTriplet(out, visit, "(", " &= ", ")");
1018 break;
1019 case EOpBitwiseXorAssign:
1020 outputTriplet(out, visit, "(", " ^= ", ")");
1021 break;
1022 case EOpBitwiseOrAssign:
1023 outputTriplet(out, visit, "(", " |= ", ")");
1024 break;
1025 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001026 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001027 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001028 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001029 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001030 if (visit == PreVisit)
1031 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001032 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001033 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001034 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] =
1035 node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001036 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001037 return false;
1038 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001039 }
Olli Etuaho96963162016-03-21 11:54:33 +02001040 else if (ancestorEvaluatesToSamplerInStruct(visit))
1041 {
1042 // All parts of an expression that access a sampler in a struct need to use _ as
1043 // separator to access the sampler variable that has been moved out of the struct.
1044 outputTriplet(out, visit, "", "_", "");
1045 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001046 else
1047 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001048 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001049 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001050 }
1051 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001052 case EOpIndexIndirect:
1053 // We do not currently support indirect references to interface blocks
1054 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1055 outputTriplet(out, visit, "", "[", "]");
1056 break;
1057 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001058 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001059 const TStructure *structure = node->getLeft()->getType().getStruct();
1060 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1061 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001062
Olli Etuaho96963162016-03-21 11:54:33 +02001063 // In cases where indexing returns a sampler, we need to access the sampler variable
1064 // that has been moved out of the struct.
1065 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1066 if (visit == PreVisit && indexingReturnsSampler)
1067 {
1068 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1069 // This prefix is only output at the beginning of the indexing expression, which
1070 // may have multiple parts.
1071 out << "angle";
1072 }
1073 if (!indexingReturnsSampler)
1074 {
1075 // All parts of an expression that access a sampler in a struct need to use _ as
1076 // separator to access the sampler variable that has been moved out of the struct.
1077 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1078 }
1079 if (visit == InVisit)
1080 {
1081 if (indexingReturnsSampler)
1082 {
1083 out << "_" + field->name();
1084 }
1085 else
1086 {
1087 out << "." + DecorateField(field->name(), *structure);
1088 }
1089
1090 return false;
1091 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001092 }
1093 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001094 case EOpIndexDirectInterfaceBlock:
1095 if (visit == InVisit)
1096 {
1097 const TInterfaceBlock *interfaceBlock =
1098 node->getLeft()->getType().getInterfaceBlock();
1099 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1100 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
1101 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001102
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001103 return false;
1104 }
1105 break;
1106 case EOpAdd:
1107 outputTriplet(out, visit, "(", " + ", ")");
1108 break;
1109 case EOpSub:
1110 outputTriplet(out, visit, "(", " - ", ")");
1111 break;
1112 case EOpMul:
1113 outputTriplet(out, visit, "(", " * ", ")");
1114 break;
1115 case EOpDiv:
1116 outputTriplet(out, visit, "(", " / ", ")");
1117 break;
1118 case EOpIMod:
1119 outputTriplet(out, visit, "(", " % ", ")");
1120 break;
1121 case EOpBitShiftLeft:
1122 outputTriplet(out, visit, "(", " << ", ")");
1123 break;
1124 case EOpBitShiftRight:
1125 outputTriplet(out, visit, "(", " >> ", ")");
1126 break;
1127 case EOpBitwiseAnd:
1128 outputTriplet(out, visit, "(", " & ", ")");
1129 break;
1130 case EOpBitwiseXor:
1131 outputTriplet(out, visit, "(", " ^ ", ")");
1132 break;
1133 case EOpBitwiseOr:
1134 outputTriplet(out, visit, "(", " | ", ")");
1135 break;
1136 case EOpEqual:
1137 case EOpNotEqual:
1138 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1139 break;
1140 case EOpLessThan:
1141 outputTriplet(out, visit, "(", " < ", ")");
1142 break;
1143 case EOpGreaterThan:
1144 outputTriplet(out, visit, "(", " > ", ")");
1145 break;
1146 case EOpLessThanEqual:
1147 outputTriplet(out, visit, "(", " <= ", ")");
1148 break;
1149 case EOpGreaterThanEqual:
1150 outputTriplet(out, visit, "(", " >= ", ")");
1151 break;
1152 case EOpVectorTimesScalar:
1153 outputTriplet(out, visit, "(", " * ", ")");
1154 break;
1155 case EOpMatrixTimesScalar:
1156 outputTriplet(out, visit, "(", " * ", ")");
1157 break;
1158 case EOpVectorTimesMatrix:
1159 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1160 break;
1161 case EOpMatrixTimesVector:
1162 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1163 break;
1164 case EOpMatrixTimesMatrix:
1165 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1166 break;
1167 case EOpLogicalOr:
1168 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1169 // been unfolded.
1170 ASSERT(!node->getRight()->hasSideEffects());
1171 outputTriplet(out, visit, "(", " || ", ")");
1172 return true;
1173 case EOpLogicalXor:
1174 mUsesXor = true;
1175 outputTriplet(out, visit, "xor(", ", ", ")");
1176 break;
1177 case EOpLogicalAnd:
1178 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1179 // been unfolded.
1180 ASSERT(!node->getRight()->hasSideEffects());
1181 outputTriplet(out, visit, "(", " && ", ")");
1182 return true;
1183 default:
1184 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185 }
1186
1187 return true;
1188}
1189
1190bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1191{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001192 TInfoSinkBase &out = getInfoSink();
1193
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001194 switch (node->getOp())
1195 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001196 case EOpNegative:
1197 outputTriplet(out, visit, "(-", "", ")");
1198 break;
1199 case EOpPositive:
1200 outputTriplet(out, visit, "(+", "", ")");
1201 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001202 case EOpLogicalNot:
1203 outputTriplet(out, visit, "(!", "", ")");
1204 break;
1205 case EOpBitwiseNot:
1206 outputTriplet(out, visit, "(~", "", ")");
1207 break;
1208 case EOpPostIncrement:
1209 outputTriplet(out, visit, "(", "", "++)");
1210 break;
1211 case EOpPostDecrement:
1212 outputTriplet(out, visit, "(", "", "--)");
1213 break;
1214 case EOpPreIncrement:
1215 outputTriplet(out, visit, "(++", "", ")");
1216 break;
1217 case EOpPreDecrement:
1218 outputTriplet(out, visit, "(--", "", ")");
1219 break;
1220 case EOpRadians:
1221 outputTriplet(out, visit, "radians(", "", ")");
1222 break;
1223 case EOpDegrees:
1224 outputTriplet(out, visit, "degrees(", "", ")");
1225 break;
1226 case EOpSin:
1227 outputTriplet(out, visit, "sin(", "", ")");
1228 break;
1229 case EOpCos:
1230 outputTriplet(out, visit, "cos(", "", ")");
1231 break;
1232 case EOpTan:
1233 outputTriplet(out, visit, "tan(", "", ")");
1234 break;
1235 case EOpAsin:
1236 outputTriplet(out, visit, "asin(", "", ")");
1237 break;
1238 case EOpAcos:
1239 outputTriplet(out, visit, "acos(", "", ")");
1240 break;
1241 case EOpAtan:
1242 outputTriplet(out, visit, "atan(", "", ")");
1243 break;
1244 case EOpSinh:
1245 outputTriplet(out, visit, "sinh(", "", ")");
1246 break;
1247 case EOpCosh:
1248 outputTriplet(out, visit, "cosh(", "", ")");
1249 break;
1250 case EOpTanh:
1251 outputTriplet(out, visit, "tanh(", "", ")");
1252 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001253 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001254 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001255 case EOpAtanh:
1256 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001257 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001258 break;
1259 case EOpExp:
1260 outputTriplet(out, visit, "exp(", "", ")");
1261 break;
1262 case EOpLog:
1263 outputTriplet(out, visit, "log(", "", ")");
1264 break;
1265 case EOpExp2:
1266 outputTriplet(out, visit, "exp2(", "", ")");
1267 break;
1268 case EOpLog2:
1269 outputTriplet(out, visit, "log2(", "", ")");
1270 break;
1271 case EOpSqrt:
1272 outputTriplet(out, visit, "sqrt(", "", ")");
1273 break;
1274 case EOpInverseSqrt:
1275 outputTriplet(out, visit, "rsqrt(", "", ")");
1276 break;
1277 case EOpAbs:
1278 outputTriplet(out, visit, "abs(", "", ")");
1279 break;
1280 case EOpSign:
1281 outputTriplet(out, visit, "sign(", "", ")");
1282 break;
1283 case EOpFloor:
1284 outputTriplet(out, visit, "floor(", "", ")");
1285 break;
1286 case EOpTrunc:
1287 outputTriplet(out, visit, "trunc(", "", ")");
1288 break;
1289 case EOpRound:
1290 outputTriplet(out, visit, "round(", "", ")");
1291 break;
1292 case EOpRoundEven:
1293 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001294 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001295 break;
1296 case EOpCeil:
1297 outputTriplet(out, visit, "ceil(", "", ")");
1298 break;
1299 case EOpFract:
1300 outputTriplet(out, visit, "frac(", "", ")");
1301 break;
1302 case EOpIsNan:
1303 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001304 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001305 else
1306 outputTriplet(out, visit, "isnan(", "", ")");
1307 mRequiresIEEEStrictCompiling = true;
1308 break;
1309 case EOpIsInf:
1310 outputTriplet(out, visit, "isinf(", "", ")");
1311 break;
1312 case EOpFloatBitsToInt:
1313 outputTriplet(out, visit, "asint(", "", ")");
1314 break;
1315 case EOpFloatBitsToUint:
1316 outputTriplet(out, visit, "asuint(", "", ")");
1317 break;
1318 case EOpIntBitsToFloat:
1319 outputTriplet(out, visit, "asfloat(", "", ")");
1320 break;
1321 case EOpUintBitsToFloat:
1322 outputTriplet(out, visit, "asfloat(", "", ")");
1323 break;
1324 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001325 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001326 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001327 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001328 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001329 case EOpUnpackHalf2x16:
1330 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001331 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001332 break;
1333 case EOpLength:
1334 outputTriplet(out, visit, "length(", "", ")");
1335 break;
1336 case EOpNormalize:
1337 outputTriplet(out, visit, "normalize(", "", ")");
1338 break;
1339 case EOpDFdx:
1340 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1341 {
1342 outputTriplet(out, visit, "(", "", ", 0.0)");
1343 }
1344 else
1345 {
1346 outputTriplet(out, visit, "ddx(", "", ")");
1347 }
1348 break;
1349 case EOpDFdy:
1350 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1351 {
1352 outputTriplet(out, visit, "(", "", ", 0.0)");
1353 }
1354 else
1355 {
1356 outputTriplet(out, visit, "ddy(", "", ")");
1357 }
1358 break;
1359 case EOpFwidth:
1360 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1361 {
1362 outputTriplet(out, visit, "(", "", ", 0.0)");
1363 }
1364 else
1365 {
1366 outputTriplet(out, visit, "fwidth(", "", ")");
1367 }
1368 break;
1369 case EOpTranspose:
1370 outputTriplet(out, visit, "transpose(", "", ")");
1371 break;
1372 case EOpDeterminant:
1373 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1374 break;
1375 case EOpInverse:
1376 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001377 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001378 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001379
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001380 case EOpAny:
1381 outputTriplet(out, visit, "any(", "", ")");
1382 break;
1383 case EOpAll:
1384 outputTriplet(out, visit, "all(", "", ")");
1385 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001386 case EOpLogicalNotComponentWise:
1387 outputTriplet(out, visit, "(!", "", ")");
1388 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001389 default:
1390 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001391 }
1392
1393 return true;
1394}
1395
Olli Etuaho96963162016-03-21 11:54:33 +02001396TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1397{
1398 if (node->getAsSymbolNode())
1399 {
1400 return node->getAsSymbolNode()->getSymbol();
1401 }
1402 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1403 switch (nodeBinary->getOp())
1404 {
1405 case EOpIndexDirect:
1406 {
1407 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1408
1409 TInfoSinkBase prefixSink;
1410 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1411 return TString(prefixSink.c_str());
1412 }
1413 case EOpIndexDirectStruct:
1414 {
1415 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1416 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1417 const TField *field = s->fields()[index];
1418
1419 TInfoSinkBase prefixSink;
1420 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1421 << field->name();
1422 return TString(prefixSink.c_str());
1423 }
1424 default:
1425 UNREACHABLE();
1426 return TString("");
1427 }
1428}
1429
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001430bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1431{
1432 TInfoSinkBase &out = getInfoSink();
1433
1434 if (mInsideFunction)
1435 {
1436 outputLineDirective(out, node->getLine().first_line);
1437 out << "{\n";
1438 }
1439
1440 for (TIntermSequence::iterator sit = node->getSequence()->begin();
1441 sit != node->getSequence()->end(); sit++)
1442 {
1443 outputLineDirective(out, (*sit)->getLine().first_line);
1444
1445 (*sit)->traverse(this);
1446
1447 // Don't output ; after case labels, they're terminated by :
1448 // This is needed especially since outputting a ; after a case statement would turn empty
1449 // case statements into non-empty case statements, disallowing fall-through from them.
1450 // Also no need to output ; after if statements or sequences. This is done just for
1451 // code clarity.
1452 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
1453 (*sit)->getAsBlock() == nullptr)
1454 out << ";\n";
1455 }
1456
1457 if (mInsideFunction)
1458 {
1459 outputLineDirective(out, node->getLine().last_line);
1460 out << "}\n";
1461 }
1462
1463 return false;
1464}
1465
Olli Etuaho336b1472016-10-05 16:37:55 +01001466bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1467{
1468 TInfoSinkBase &out = getInfoSink();
1469
1470 ASSERT(mCurrentFunctionMetadata == nullptr);
1471
1472 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1473 ASSERT(index != CallDAG::InvalidIndex);
1474 mCurrentFunctionMetadata = &mASTMetadataList[index];
1475
1476 out << TypeString(node->getType()) << " ";
1477
1478 TIntermSequence *parameters = node->getFunctionParameters()->getSequence();
1479
1480 if (node->getFunctionSymbolInfo()->isMain())
1481 {
1482 out << "gl_main(";
1483 }
1484 else
1485 {
1486 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
1487 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1488 }
1489
1490 for (unsigned int i = 0; i < parameters->size(); i++)
1491 {
1492 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1493
1494 if (symbol)
1495 {
1496 ensureStructDefined(symbol->getType());
1497
1498 out << argumentString(symbol);
1499
1500 if (i < parameters->size() - 1)
1501 {
1502 out << ", ";
1503 }
1504 }
1505 else
1506 UNREACHABLE();
1507 }
1508
1509 out << ")\n";
1510
1511 mInsideFunction = true;
1512 // The function body node will output braces.
1513 node->getBody()->traverse(this);
1514 mInsideFunction = false;
1515
1516 mCurrentFunctionMetadata = nullptr;
1517
1518 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1519 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1520 {
1521 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1522 mOutputLod0Function = true;
1523 node->traverse(this);
1524 mOutputLod0Function = false;
1525 }
1526
1527 return false;
1528}
1529
Olli Etuaho13389b62016-10-16 11:48:18 +01001530bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1531{
1532 TInfoSinkBase &out = getInfoSink();
1533 if (visit == PreVisit)
1534 {
1535 TIntermSequence *sequence = node->getSequence();
1536 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1537 ASSERT(sequence->size() == 1);
1538
1539 if (variable &&
1540 (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
1541 variable->getQualifier() == EvqConst))
1542 {
1543 ensureStructDefined(variable->getType());
1544
1545 if (!variable->getAsSymbolNode() ||
1546 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1547 {
1548 if (!mInsideFunction)
1549 {
1550 out << "static ";
1551 }
1552
1553 out << TypeString(variable->getType()) + " ";
1554
1555 TIntermSymbol *symbol = variable->getAsSymbolNode();
1556
1557 if (symbol)
1558 {
1559 symbol->traverse(this);
1560 out << ArrayString(symbol->getType());
1561 out << " = " + initializer(symbol->getType());
1562 }
1563 else
1564 {
1565 variable->traverse(this);
1566 }
1567 }
1568 else if (variable->getAsSymbolNode() &&
1569 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1570 {
1571 // Already added to constructor map
1572 }
1573 else
1574 UNREACHABLE();
1575 }
1576 else if (variable && IsVaryingOut(variable->getQualifier()))
1577 {
1578 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
1579 {
1580 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1581
1582 if (symbol)
1583 {
1584 // Vertex (output) varyings which are declared but not written to should
1585 // still be declared to allow successful linking
1586 mReferencedVaryings[symbol->getSymbol()] = symbol;
1587 }
1588 else
1589 {
1590 (*sit)->traverse(this);
1591 }
1592 }
1593 }
1594 }
1595 return false;
1596}
1597
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001598bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1599{
1600 // Do not do any translation
1601 return false;
1602}
1603
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001604bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1605{
Jamie Madill32aab012015-01-27 14:12:26 -05001606 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001607
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001608 switch (node->getOp())
1609 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001610 case EOpPrototype:
1611 if (visit == PreVisit)
1612 {
Olli Etuahobd674552016-10-06 13:28:42 +01001613 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Olli Etuaho5878f832016-10-07 10:14:58 +01001614 // Skip the prototype if it is not implemented (and thus not used)
1615 if (index == CallDAG::InvalidIndex)
1616 {
1617 return false;
1618 }
1619
1620 TIntermSequence *arguments = node->getSequence();
1621
Olli Etuahobd674552016-10-06 13:28:42 +01001622 TString name =
1623 DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho5878f832016-10-07 10:14:58 +01001624 out << TypeString(node->getType()) << " " << name
1625 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
1626
1627 for (unsigned int i = 0; i < arguments->size(); i++)
1628 {
1629 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1630
1631 if (symbol)
1632 {
1633 out << argumentString(symbol);
1634
1635 if (i < arguments->size() - 1)
1636 {
1637 out << ", ";
1638 }
1639 }
1640 else
1641 UNREACHABLE();
1642 }
1643
1644 out << ");\n";
1645
1646 // Also prototype the Lod0 variant if needed
1647 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1648 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1649 {
1650 mOutputLod0Function = true;
1651 node->traverse(this);
1652 mOutputLod0Function = false;
1653 }
1654
1655 return false;
1656 }
1657 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001658 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001659 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001660 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001661
Corentin Wallez1239ee92015-03-19 14:38:02 -07001662 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001663 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001665 if (node->isArray())
1666 {
1667 UNIMPLEMENTED();
1668 }
Olli Etuahobd674552016-10-06 13:28:42 +01001669 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001670 ASSERT(index != CallDAG::InvalidIndex);
1671 lod0 &= mASTMetadataList[index].mNeedsLod0;
1672
Olli Etuahobd674552016-10-06 13:28:42 +01001673 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001674 out << DisambiguateFunctionName(node->getSequence());
1675 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 }
Olli Etuahobd674552016-10-06 13:28:42 +01001677 else if (node->getFunctionSymbolInfo()->getNameObj().isInternal())
Olli Etuahob741c762016-06-29 15:49:22 +03001678 {
1679 // This path is used for internal functions that don't have their definitions in the
1680 // AST, such as precision emulation functions.
Olli Etuahobd674552016-10-06 13:28:42 +01001681 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001682 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683 else
1684 {
Olli Etuahobd674552016-10-06 13:28:42 +01001685 TString name = TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001686 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001687 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001688 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1689 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1690 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001692
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001693 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001694 {
Olli Etuaho96963162016-03-21 11:54:33 +02001695 TIntermTyped *typedArg = (*arg)->getAsTyped();
1696 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001697 {
1698 out << "texture_";
1699 (*arg)->traverse(this);
1700 out << ", sampler_";
1701 }
1702
1703 (*arg)->traverse(this);
1704
Olli Etuaho96963162016-03-21 11:54:33 +02001705 if (typedArg->getType().isStructureContainingSamplers())
1706 {
1707 const TType &argType = typedArg->getType();
1708 TVector<TIntermSymbol *> samplerSymbols;
1709 TString structName = samplerNamePrefixFromStruct(typedArg);
1710 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001711 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001712 &samplerSymbols, nullptr);
1713 for (const TIntermSymbol *sampler : samplerSymbols)
1714 {
1715 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1716 {
1717 out << ", texture_" << sampler->getSymbol();
1718 out << ", sampler_" << sampler->getSymbol();
1719 }
1720 else
1721 {
1722 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1723 // of D3D9, it's the sampler variable.
1724 out << ", " + sampler->getSymbol();
1725 }
1726 }
1727 }
1728
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001729 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001730 {
1731 out << ", ";
1732 }
1733 }
1734
1735 out << ")";
1736
1737 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001738 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001739 case EOpParameters:
1740 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1741 break;
1742 case EOpConstructFloat:
1743 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1744 break;
1745 case EOpConstructVec2:
1746 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1747 break;
1748 case EOpConstructVec3:
1749 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1750 break;
1751 case EOpConstructVec4:
1752 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1753 break;
1754 case EOpConstructBool:
1755 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1756 break;
1757 case EOpConstructBVec2:
1758 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1759 break;
1760 case EOpConstructBVec3:
1761 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1762 break;
1763 case EOpConstructBVec4:
1764 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1765 break;
1766 case EOpConstructInt:
1767 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1768 break;
1769 case EOpConstructIVec2:
1770 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1771 break;
1772 case EOpConstructIVec3:
1773 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1774 break;
1775 case EOpConstructIVec4:
1776 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1777 break;
1778 case EOpConstructUInt:
1779 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1780 break;
1781 case EOpConstructUVec2:
1782 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1783 break;
1784 case EOpConstructUVec3:
1785 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1786 break;
1787 case EOpConstructUVec4:
1788 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1789 break;
1790 case EOpConstructMat2:
1791 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1792 break;
1793 case EOpConstructMat2x3:
1794 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1795 break;
1796 case EOpConstructMat2x4:
1797 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1798 break;
1799 case EOpConstructMat3x2:
1800 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1801 break;
1802 case EOpConstructMat3:
1803 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1804 break;
1805 case EOpConstructMat3x4:
1806 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1807 break;
1808 case EOpConstructMat4x2:
1809 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1810 break;
1811 case EOpConstructMat4x3:
1812 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1813 break;
1814 case EOpConstructMat4:
1815 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1816 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001817 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001818 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001819 if (node->getType().isArray())
1820 {
1821 UNIMPLEMENTED();
1822 }
Jamie Madill033dae62014-06-18 12:56:28 -04001823 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001824 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001825 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001826 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001827 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001828 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001829 outputTriplet(out, visit, "(", " == ", ")");
1830 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001831 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001832 outputTriplet(out, visit, "(", " != ", ")");
1833 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001834 case EOpLessThanComponentWise:
1835 outputTriplet(out, visit, "(", " < ", ")");
1836 break;
1837 case EOpGreaterThanComponentWise:
1838 outputTriplet(out, visit, "(", " > ", ")");
1839 break;
1840 case EOpLessThanEqualComponentWise:
1841 outputTriplet(out, visit, "(", " <= ", ")");
1842 break;
1843 case EOpGreaterThanEqualComponentWise:
1844 outputTriplet(out, visit, "(", " >= ", ")");
1845 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001846 case EOpMod:
1847 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001848 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001849 break;
1850 case EOpModf:
1851 outputTriplet(out, visit, "modf(", ", ", ")");
1852 break;
1853 case EOpPow:
1854 outputTriplet(out, visit, "pow(", ", ", ")");
1855 break;
1856 case EOpAtan:
1857 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1858 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001859 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001860 break;
1861 case EOpMin:
1862 outputTriplet(out, visit, "min(", ", ", ")");
1863 break;
1864 case EOpMax:
1865 outputTriplet(out, visit, "max(", ", ", ")");
1866 break;
1867 case EOpClamp:
1868 outputTriplet(out, visit, "clamp(", ", ", ")");
1869 break;
1870 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301871 {
1872 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1873 if (lastParamNode->getType().getBasicType() == EbtBool)
1874 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001875 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
1876 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05301877 // so use emulated version.
1878 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001879 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05301880 }
1881 else
1882 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001883 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301884 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001885 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301886 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001887 case EOpStep:
1888 outputTriplet(out, visit, "step(", ", ", ")");
1889 break;
1890 case EOpSmoothStep:
1891 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1892 break;
1893 case EOpDistance:
1894 outputTriplet(out, visit, "distance(", ", ", ")");
1895 break;
1896 case EOpDot:
1897 outputTriplet(out, visit, "dot(", ", ", ")");
1898 break;
1899 case EOpCross:
1900 outputTriplet(out, visit, "cross(", ", ", ")");
1901 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001902 case EOpFaceForward:
1903 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001904 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001905 break;
1906 case EOpReflect:
1907 outputTriplet(out, visit, "reflect(", ", ", ")");
1908 break;
1909 case EOpRefract:
1910 outputTriplet(out, visit, "refract(", ", ", ")");
1911 break;
1912 case EOpOuterProduct:
1913 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001914 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001915 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001916 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01001917 outputTriplet(out, visit, "(", " * ", ")");
1918 break;
1919 default:
1920 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921 }
1922
1923 return true;
1924}
1925
Olli Etuaho57961272016-09-14 13:57:46 +03001926void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001928 out << "if (";
1929
1930 node->getCondition()->traverse(this);
1931
1932 out << ")\n";
1933
Jamie Madill8c46ab12015-12-07 16:39:19 -05001934 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001935
1936 bool discard = false;
1937
1938 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001940 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001941 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001942
Olli Etuahoa6f22092015-05-08 18:31:10 +03001943 // Detect true discard
1944 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1945 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001946 else
1947 {
1948 // TODO(oetuaho): Check if the semicolon inside is necessary.
1949 // It's there as a result of conservative refactoring of the output.
1950 out << "{;}\n";
1951 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001952
Jamie Madill8c46ab12015-12-07 16:39:19 -05001953 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001954
Olli Etuahoa6f22092015-05-08 18:31:10 +03001955 if (node->getFalseBlock())
1956 {
1957 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001958
Jamie Madill8c46ab12015-12-07 16:39:19 -05001959 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960
Olli Etuaho32db19b2016-10-04 14:43:16 +01001961 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001962 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001963
Jamie Madill8c46ab12015-12-07 16:39:19 -05001964 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001965
Olli Etuahoa6f22092015-05-08 18:31:10 +03001966 // Detect false discard
1967 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1968 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001969
Olli Etuahoa6f22092015-05-08 18:31:10 +03001970 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001971 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001972 {
1973 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001974 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001975}
1976
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001977bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1978{
1979 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1980 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1981 UNREACHABLE();
1982 return false;
1983}
1984
Olli Etuaho57961272016-09-14 13:57:46 +03001985bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001986{
1987 TInfoSinkBase &out = getInfoSink();
1988
Olli Etuaho3d932d82016-04-12 11:10:30 +03001989 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001990
1991 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001992 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001993 {
1994 out << "FLATTEN ";
1995 }
1996
Olli Etuaho57961272016-09-14 13:57:46 +03001997 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998
1999 return false;
2000}
2001
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002002bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002003{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002004 TInfoSinkBase &out = getInfoSink();
2005
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002006 if (node->getStatementList())
2007 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002008 node->setStatementList(
2009 RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002010 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002011 // The curly braces get written when visiting the statementList aggregate
2012 }
2013 else
2014 {
2015 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002016 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002017 }
2018 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002019}
2020
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002021bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002022{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002023 TInfoSinkBase &out = getInfoSink();
2024
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002025 if (node->hasCondition())
2026 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002027 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002028 return true;
2029 }
2030 else
2031 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002032 out << "default:\n";
2033 return false;
2034 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002035}
2036
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2038{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002039 TInfoSinkBase &out = getInfoSink();
2040 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002041}
2042
2043bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2044{
Nicolas Capens655fe362014-04-11 13:12:34 -04002045 mNestedLoopDepth++;
2046
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002047 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002048 mInsideDiscontinuousLoop =
2049 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002050
Jamie Madill8c46ab12015-12-07 16:39:19 -05002051 TInfoSinkBase &out = getInfoSink();
2052
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002053 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002054 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002055 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002056 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002057 mInsideDiscontinuousLoop = wasDiscontinuous;
2058 mNestedLoopDepth--;
2059
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002060 return false;
2061 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002062 }
2063
Corentin Wallez1239ee92015-03-19 14:38:02 -07002064 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002065 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002067 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002068
Jamie Madill8c46ab12015-12-07 16:39:19 -05002069 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 }
2071 else
2072 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002073 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002074
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 if (node->getInit())
2076 {
2077 node->getInit()->traverse(this);
2078 }
2079
2080 out << "; ";
2081
alokp@chromium.org52813552010-11-16 18:36:09 +00002082 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002084 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 }
2086
2087 out << "; ";
2088
alokp@chromium.org52813552010-11-16 18:36:09 +00002089 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002091 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 }
2093
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002094 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002095
Jamie Madill8c46ab12015-12-07 16:39:19 -05002096 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097 }
2098
2099 if (node->getBody())
2100 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002101 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002102 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002104 else
2105 {
2106 // TODO(oetuaho): Check if the semicolon inside is necessary.
2107 // It's there as a result of conservative refactoring of the output.
2108 out << "{;}\n";
2109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110
Jamie Madill8c46ab12015-12-07 16:39:19 -05002111 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112
alokp@chromium.org52813552010-11-16 18:36:09 +00002113 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002115 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116 out << "while(\n";
2117
alokp@chromium.org52813552010-11-16 18:36:09 +00002118 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119
daniel@transgaming.com73536982012-03-21 20:45:49 +00002120 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121 }
2122
daniel@transgaming.com73536982012-03-21 20:45:49 +00002123 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002125 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002126 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002127
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 return false;
2129}
2130
2131bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2132{
Jamie Madill32aab012015-01-27 14:12:26 -05002133 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134
2135 switch (node->getFlowOp())
2136 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002137 case EOpKill:
2138 outputTriplet(out, visit, "discard;\n", "", "");
2139 break;
2140 case EOpBreak:
2141 if (visit == PreVisit)
Nicolas Capens655fe362014-04-11 13:12:34 -04002142 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002143 if (mNestedLoopDepth > 1)
2144 {
2145 mUsesNestedBreak = true;
2146 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002147
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002148 if (mExcessiveLoopIndex)
2149 {
2150 out << "{Break";
2151 mExcessiveLoopIndex->traverse(this);
2152 out << " = true; break;}\n";
2153 }
2154 else
2155 {
2156 out << "break;\n";
2157 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002158 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002159 break;
2160 case EOpContinue:
2161 outputTriplet(out, visit, "continue;\n", "", "");
2162 break;
2163 case EOpReturn:
2164 if (visit == PreVisit)
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002165 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002166 if (node->getExpression())
2167 {
2168 out << "return ";
2169 }
2170 else
2171 {
2172 out << "return;\n";
2173 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002174 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002175 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002177 if (node->getExpression())
2178 {
2179 out << ";\n";
2180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002182 break;
2183 default:
2184 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 }
2186
2187 return true;
2188}
2189
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002190// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002191// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2192// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002193bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002194{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002195 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002196
2197 // Parse loops of the form:
2198 // for(int index = initial; index [comparator] limit; index += increment)
2199 TIntermSymbol *index = NULL;
2200 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002201 int initial = 0;
2202 int limit = 0;
2203 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002204
2205 // Parse index name and intial value
2206 if (node->getInit())
2207 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002208 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002209
2210 if (init)
2211 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002212 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002213 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002214
2215 if (variable && variable->getQualifier() == EvqTemporary)
2216 {
2217 TIntermBinary *assign = variable->getAsBinaryNode();
2218
2219 if (assign->getOp() == EOpInitialize)
2220 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002221 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002222 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2223
2224 if (symbol && constant)
2225 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002226 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002227 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002228 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002229 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002230 }
2231 }
2232 }
2233 }
2234 }
2235 }
2236
2237 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002238 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002239 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002240 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002241
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002242 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2243 {
2244 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2245
2246 if (constant)
2247 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002248 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002249 {
2250 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002251 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002252 }
2253 }
2254 }
2255 }
2256
2257 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002258 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002259 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002260 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002261 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002262
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002263 if (binaryTerminal)
2264 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002265 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002266 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2267
2268 if (constant)
2269 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002270 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002271 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002272 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002273
2274 switch (op)
2275 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002276 case EOpAddAssign:
2277 increment = value;
2278 break;
2279 case EOpSubAssign:
2280 increment = -value;
2281 break;
2282 default:
2283 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002284 }
2285 }
2286 }
2287 }
2288 else if (unaryTerminal)
2289 {
2290 TOperator op = unaryTerminal->getOp();
2291
2292 switch (op)
2293 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002294 case EOpPostIncrement:
2295 increment = 1;
2296 break;
2297 case EOpPostDecrement:
2298 increment = -1;
2299 break;
2300 case EOpPreIncrement:
2301 increment = 1;
2302 break;
2303 case EOpPreDecrement:
2304 increment = -1;
2305 break;
2306 default:
2307 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002308 }
2309 }
2310 }
2311
2312 if (index != NULL && comparator != EOpNull && increment != 0)
2313 {
2314 if (comparator == EOpLessThanEqual)
2315 {
2316 comparator = EOpLessThan;
2317 limit += 1;
2318 }
2319
2320 if (comparator == EOpLessThan)
2321 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002322 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002323
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002324 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002325 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002326 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002327 }
2328
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002329 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002330 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002331
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002332 out << "{int ";
2333 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002334 out << ";\n"
2335 "bool Break";
2336 index->traverse(this);
2337 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002338
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002339 bool firstLoopFragment = true;
2340
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002341 while (iterations > 0)
2342 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002343 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002344
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002345 if (!firstLoopFragment)
2346 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002347 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002348 index->traverse(this);
2349 out << ") {\n";
2350 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002351
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002352 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002353 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002354 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002355 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002356
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002357 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002358 const char *unroll =
2359 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002360
Corentin Wallez1239ee92015-03-19 14:38:02 -07002361 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002362 index->traverse(this);
2363 out << " = ";
2364 out << initial;
2365
2366 out << "; ";
2367 index->traverse(this);
2368 out << " < ";
2369 out << clampedLimit;
2370
2371 out << "; ";
2372 index->traverse(this);
2373 out << " += ";
2374 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002375 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002376
Jamie Madill8c46ab12015-12-07 16:39:19 -05002377 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002378 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002379
2380 if (node->getBody())
2381 {
2382 node->getBody()->traverse(this);
2383 }
2384
Jamie Madill8c46ab12015-12-07 16:39:19 -05002385 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002386 out << ";}\n";
2387
2388 if (!firstLoopFragment)
2389 {
2390 out << "}\n";
2391 }
2392
2393 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002394
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002395 initial += MAX_LOOP_ITERATIONS * increment;
2396 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002397 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002398
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002399 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002400
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002401 mExcessiveLoopIndex = restoreIndex;
2402
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002403 return true;
2404 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002405 else
2406 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002407 }
2408
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002409 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002410}
2411
Jamie Madill8c46ab12015-12-07 16:39:19 -05002412void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2413 Visit visit,
2414 const char *preString,
2415 const char *inString,
2416 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002418 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419 {
2420 out << preString;
2421 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002422 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423 {
2424 out << inString;
2425 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002426 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 {
2428 out << postString;
2429 }
2430}
2431
Jamie Madill8c46ab12015-12-07 16:39:19 -05002432void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002433{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002434 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002435 {
Jamie Madill32aab012015-01-27 14:12:26 -05002436 out << "\n";
2437 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002438
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002439 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002440 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002441 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002442 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002443
Jamie Madill32aab012015-01-27 14:12:26 -05002444 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002445 }
2446}
2447
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002448TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2449{
2450 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002451 const TType &type = symbol->getType();
2452 const TName &name = symbol->getName();
2453 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002454
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002455 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002456 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002457 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002458 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002459 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002460 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002461 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002462 }
2463
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002464 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002465 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002466 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2467 {
2468 // Samplers are passed as indices to the sampler array.
2469 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2470 return "const uint " + nameStr + ArrayString(type);
2471 }
2472 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2473 {
2474 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2475 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2476 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2477 ArrayString(type);
2478 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002479 }
2480
Olli Etuaho96963162016-03-21 11:54:33 +02002481 TStringStream argString;
2482 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2483 << ArrayString(type);
2484
2485 // If the structure parameter contains samplers, they need to be passed into the function as
2486 // separate parameters. HLSL doesn't natively support samplers in structs.
2487 if (type.isStructureContainingSamplers())
2488 {
2489 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2490 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002491 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002492 for (const TIntermSymbol *sampler : samplerSymbols)
2493 {
2494 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2495 {
2496 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2497 }
2498 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2499 {
2500 const TType &samplerType = sampler->getType();
2501 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2502 type.getArraySize() == samplerType.getArraySize());
2503 ASSERT(IsSampler(samplerType.getBasicType()));
2504 argString << ", " << QualifierString(qualifier) << " "
2505 << TextureString(samplerType.getBasicType()) << " texture_"
2506 << sampler->getSymbol() << ArrayString(type) << ", "
2507 << QualifierString(qualifier) << " "
2508 << SamplerString(samplerType.getBasicType()) << " sampler_"
2509 << sampler->getSymbol() << ArrayString(type);
2510 }
2511 else
2512 {
2513 const TType &samplerType = sampler->getType();
2514 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2515 type.getArraySize() == samplerType.getArraySize());
2516 ASSERT(IsSampler(samplerType.getBasicType()));
2517 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2518 << " " << sampler->getSymbol() << ArrayString(type);
2519 }
2520 }
2521 }
2522
2523 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524}
2525
2526TString OutputHLSL::initializer(const TType &type)
2527{
2528 TString string;
2529
Jamie Madill94bf7f22013-07-08 13:31:15 -04002530 size_t size = type.getObjectSize();
2531 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002533 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534
Jamie Madill94bf7f22013-07-08 13:31:15 -04002535 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002536 {
2537 string += ", ";
2538 }
2539 }
2540
daniel@transgaming.comead23042010-04-29 03:35:36 +00002541 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002543
Jamie Madill8c46ab12015-12-07 16:39:19 -05002544void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2545 Visit visit,
2546 const TType &type,
2547 const char *name,
2548 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002549{
Olli Etuahof40319e2015-03-10 14:33:00 +02002550 if (type.isArray())
2551 {
2552 UNIMPLEMENTED();
2553 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002554
2555 if (visit == PreVisit)
2556 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002557 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002558
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002559 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002560 }
2561 else if (visit == InVisit)
2562 {
2563 out << ", ";
2564 }
2565 else if (visit == PostVisit)
2566 {
2567 out << ")";
2568 }
2569}
2570
Jamie Madill8c46ab12015-12-07 16:39:19 -05002571const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2572 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002573 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002574{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002575 const TConstantUnion *constUnionIterated = constUnion;
2576
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002577 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002578 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002579 {
Jamie Madill033dae62014-06-18 12:56:28 -04002580 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002581
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002582 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002583
Jamie Madill98493dd2013-07-08 14:39:03 -04002584 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002585 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002586 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002587 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002588
Jamie Madill98493dd2013-07-08 14:39:03 -04002589 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002590 {
2591 out << ", ";
2592 }
2593 }
2594
2595 out << ")";
2596 }
2597 else
2598 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002599 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002600 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002601
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002602 if (writeType)
2603 {
Jamie Madill033dae62014-06-18 12:56:28 -04002604 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002605 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002606 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002607 if (writeType)
2608 {
2609 out << ")";
2610 }
2611 }
2612
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002613 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002614}
2615
Olli Etuahod68924e2017-01-02 17:34:40 +00002616void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002617{
Olli Etuahod68924e2017-01-02 17:34:40 +00002618 if (visit == PreVisit)
2619 {
2620 const char *opStr = GetOperatorString(op);
2621 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2622 out << "(";
2623 }
2624 else
2625 {
2626 outputTriplet(out, visit, nullptr, ", ", ")");
2627 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002628}
2629
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002630bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2631 TIntermSymbol *symbolNode,
2632 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002633{
2634 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2635 expression->traverse(&searchSymbol);
2636
2637 if (searchSymbol.foundMatch())
2638 {
2639 // Type already printed
2640 out << "t" + str(mUniqueIndex) + " = ";
2641 expression->traverse(this);
2642 out << ", ";
2643 symbolNode->traverse(this);
2644 out << " = t" + str(mUniqueIndex);
2645
2646 mUniqueIndex++;
2647 return true;
2648 }
2649
2650 return false;
2651}
2652
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002653bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2654{
2655 // We support writing constant unions and constructors that only take constant unions as
2656 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002657 return expression->getAsConstantUnion() ||
2658 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002659}
2660
2661bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2662 TIntermSymbol *symbolNode,
2663 TIntermTyped *expression)
2664{
2665 if (canWriteAsHLSLLiteral(expression))
2666 {
2667 symbolNode->traverse(this);
2668 if (expression->getType().isArray())
2669 {
2670 out << "[" << expression->getType().getArraySize() << "]";
2671 }
2672 out << " = {";
2673 if (expression->getAsConstantUnion())
2674 {
2675 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2676 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002677 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002678 }
2679 else
2680 {
2681 TIntermAggregate *constructor = expression->getAsAggregate();
2682 ASSERT(constructor != nullptr);
2683 for (TIntermNode *&node : *constructor->getSequence())
2684 {
2685 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2686 ASSERT(nodeConst);
2687 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002688 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002689 if (node != constructor->getSequence()->back())
2690 {
2691 out << ", ";
2692 }
2693 }
2694 }
2695 out << "}";
2696 return true;
2697 }
2698 return false;
2699}
2700
Jamie Madill55e79e02015-02-09 15:35:00 -05002701TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2702{
2703 const TFieldList &fields = structure.fields();
2704
2705 for (const auto &eqFunction : mStructEqualityFunctions)
2706 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002707 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002708 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002709 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002710 }
2711 }
2712
2713 const TString &structNameString = StructNameString(structure);
2714
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002715 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002716 function->structure = &structure;
2717 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002718
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002719 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002720
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002721 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2722 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002723 << "{\n"
2724 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002725
2726 for (size_t i = 0; i < fields.size(); i++)
2727 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002728 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002729 const TType *fieldType = field->type();
2730
2731 const TString &fieldNameA = "a." + Decorate(field->name());
2732 const TString &fieldNameB = "b." + Decorate(field->name());
2733
2734 if (i > 0)
2735 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002736 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002737 }
2738
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002739 fnOut << "(";
2740 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2741 fnOut << fieldNameA;
2742 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2743 fnOut << fieldNameB;
2744 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2745 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002746 }
2747
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002748 fnOut << ";\n"
2749 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002750
2751 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002752
2753 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002754 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002755
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002756 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002757}
2758
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002759TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002760{
2761 for (const auto &eqFunction : mArrayEqualityFunctions)
2762 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002763 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002764 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002765 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002766 }
2767 }
2768
2769 const TString &typeName = TypeString(type);
2770
Olli Etuaho12690762015-03-31 12:55:28 +03002771 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002772 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002773
2774 TInfoSinkBase fnNameOut;
2775 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002776 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002777
2778 TType nonArrayType = type;
2779 nonArrayType.clearArrayness();
2780
2781 TInfoSinkBase fnOut;
2782
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002783 fnOut << "bool " << function->functionName << "(" << typeName << " a[" << type.getArraySize()
2784 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002785 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002786 " for (int i = 0; i < "
2787 << type.getArraySize() << "; ++i)\n"
2788 " {\n"
2789 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002790
2791 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2792 fnOut << "a[i]";
2793 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2794 fnOut << "b[i]";
2795 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2796
2797 fnOut << ") { return false; }\n"
2798 " }\n"
2799 " return true;\n"
2800 "}\n";
2801
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002802 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002803
2804 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002805 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002806
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002807 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002808}
2809
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002810TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002811{
2812 for (const auto &assignFunction : mArrayAssignmentFunctions)
2813 {
2814 if (assignFunction.type == type)
2815 {
2816 return assignFunction.functionName;
2817 }
2818 }
2819
2820 const TString &typeName = TypeString(type);
2821
2822 ArrayHelperFunction function;
2823 function.type = type;
2824
2825 TInfoSinkBase fnNameOut;
2826 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2827 function.functionName = fnNameOut.c_str();
2828
2829 TInfoSinkBase fnOut;
2830
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002831 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2832 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
2833 << "{\n"
2834 " for (int i = 0; i < "
2835 << type.getArraySize() << "; ++i)\n"
2836 " {\n"
2837 " a[i] = b[i];\n"
2838 " }\n"
2839 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03002840
2841 function.functionDefinition = fnOut.c_str();
2842
2843 mArrayAssignmentFunctions.push_back(function);
2844
2845 return function.functionName;
2846}
2847
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002848TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03002849{
2850 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2851 {
2852 if (constructIntoFunction.type == type)
2853 {
2854 return constructIntoFunction.functionName;
2855 }
2856 }
2857
2858 const TString &typeName = TypeString(type);
2859
2860 ArrayHelperFunction function;
2861 function.type = type;
2862
2863 TInfoSinkBase fnNameOut;
2864 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2865 function.functionName = fnNameOut.c_str();
2866
2867 TInfoSinkBase fnOut;
2868
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002869 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2870 << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002871 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002872 {
2873 fnOut << ", " << typeName << " b" << i;
2874 }
2875 fnOut << ")\n"
2876 "{\n";
2877
Olli Etuaho856c4972016-08-08 11:38:39 +03002878 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002879 {
2880 fnOut << " a[" << i << "] = b" << i << ";\n";
2881 }
2882 fnOut << "}\n";
2883
2884 function.functionDefinition = fnOut.c_str();
2885
2886 mArrayConstructIntoFunctions.push_back(function);
2887
2888 return function.functionName;
2889}
2890
Jamie Madill2e295e22015-04-29 10:41:33 -04002891void OutputHLSL::ensureStructDefined(const TType &type)
2892{
2893 TStructure *structure = type.getStruct();
2894
2895 if (structure)
2896 {
2897 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2898 }
2899}
2900
Jamie Madill45bcc782016-11-07 13:58:48 -05002901} // namespace sh