blob: 03f043d7b913145ea335528c69e1d4e63048cd57 [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 Madill417df922017-01-12 09:23:07 -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
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001476 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001477
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001478 TIntermSequence *parameters = node->getFunctionPrototype()->getSequence();
Olli Etuaho336b1472016-10-05 16:37:55 +01001479
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
Olli Etuaho16c745a2017-01-16 17:02:27 +00001604bool OutputHLSL::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
1605{
1606 TInfoSinkBase &out = getInfoSink();
1607
1608 ASSERT(visit == PreVisit);
1609 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1610 // Skip the prototype if it is not implemented (and thus not used)
1611 if (index == CallDAG::InvalidIndex)
1612 {
1613 return false;
1614 }
1615
1616 TIntermSequence *arguments = node->getSequence();
1617
1618 TString name = DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
1619 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1620 << (mOutputLod0Function ? "Lod0(" : "(");
1621
1622 for (unsigned int i = 0; i < arguments->size(); i++)
1623 {
1624 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1625 ASSERT(symbol != nullptr);
1626
1627 out << argumentString(symbol);
1628
1629 if (i < arguments->size() - 1)
1630 {
1631 out << ", ";
1632 }
1633 }
1634
1635 out << ");\n";
1636
1637 // Also prototype the Lod0 variant if needed
1638 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1639 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1640 {
1641 mOutputLod0Function = true;
1642 node->traverse(this);
1643 mOutputLod0Function = false;
1644 }
1645
1646 return false;
1647}
1648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1650{
Jamie Madill32aab012015-01-27 14:12:26 -05001651 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653 switch (node->getOp())
1654 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001655 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001657 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001658
Corentin Wallez1239ee92015-03-19 14:38:02 -07001659 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001660 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001661 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001662 if (node->isArray())
1663 {
1664 UNIMPLEMENTED();
1665 }
Olli Etuahobd674552016-10-06 13:28:42 +01001666 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001667 ASSERT(index != CallDAG::InvalidIndex);
1668 lod0 &= mASTMetadataList[index].mNeedsLod0;
1669
Olli Etuahobd674552016-10-06 13:28:42 +01001670 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001671 out << DisambiguateFunctionName(node->getSequence());
1672 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673 }
Olli Etuahobd674552016-10-06 13:28:42 +01001674 else if (node->getFunctionSymbolInfo()->getNameObj().isInternal())
Olli Etuahob741c762016-06-29 15:49:22 +03001675 {
1676 // This path is used for internal functions that don't have their definitions in the
1677 // AST, such as precision emulation functions.
Olli Etuahobd674552016-10-06 13:28:42 +01001678 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001679 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001680 else
1681 {
Olli Etuahobd674552016-10-06 13:28:42 +01001682 TString name = TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001683 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001684 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001685 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1686 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1687 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001689
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001690 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001691 {
Olli Etuaho96963162016-03-21 11:54:33 +02001692 TIntermTyped *typedArg = (*arg)->getAsTyped();
1693 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001694 {
1695 out << "texture_";
1696 (*arg)->traverse(this);
1697 out << ", sampler_";
1698 }
1699
1700 (*arg)->traverse(this);
1701
Olli Etuaho96963162016-03-21 11:54:33 +02001702 if (typedArg->getType().isStructureContainingSamplers())
1703 {
1704 const TType &argType = typedArg->getType();
1705 TVector<TIntermSymbol *> samplerSymbols;
1706 TString structName = samplerNamePrefixFromStruct(typedArg);
1707 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001708 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001709 &samplerSymbols, nullptr);
1710 for (const TIntermSymbol *sampler : samplerSymbols)
1711 {
1712 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1713 {
1714 out << ", texture_" << sampler->getSymbol();
1715 out << ", sampler_" << sampler->getSymbol();
1716 }
1717 else
1718 {
1719 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1720 // of D3D9, it's the sampler variable.
1721 out << ", " + sampler->getSymbol();
1722 }
1723 }
1724 }
1725
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001726 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001727 {
1728 out << ", ";
1729 }
1730 }
1731
1732 out << ")";
1733
1734 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001736 case EOpConstructFloat:
1737 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1738 break;
1739 case EOpConstructVec2:
1740 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1741 break;
1742 case EOpConstructVec3:
1743 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1744 break;
1745 case EOpConstructVec4:
1746 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1747 break;
1748 case EOpConstructBool:
1749 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1750 break;
1751 case EOpConstructBVec2:
1752 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1753 break;
1754 case EOpConstructBVec3:
1755 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1756 break;
1757 case EOpConstructBVec4:
1758 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1759 break;
1760 case EOpConstructInt:
1761 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1762 break;
1763 case EOpConstructIVec2:
1764 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1765 break;
1766 case EOpConstructIVec3:
1767 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1768 break;
1769 case EOpConstructIVec4:
1770 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1771 break;
1772 case EOpConstructUInt:
1773 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1774 break;
1775 case EOpConstructUVec2:
1776 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1777 break;
1778 case EOpConstructUVec3:
1779 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1780 break;
1781 case EOpConstructUVec4:
1782 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1783 break;
1784 case EOpConstructMat2:
1785 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1786 break;
1787 case EOpConstructMat2x3:
1788 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1789 break;
1790 case EOpConstructMat2x4:
1791 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1792 break;
1793 case EOpConstructMat3x2:
1794 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1795 break;
1796 case EOpConstructMat3:
1797 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1798 break;
1799 case EOpConstructMat3x4:
1800 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1801 break;
1802 case EOpConstructMat4x2:
1803 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1804 break;
1805 case EOpConstructMat4x3:
1806 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1807 break;
1808 case EOpConstructMat4:
1809 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1810 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001811 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001812 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001813 if (node->getType().isArray())
1814 {
1815 UNIMPLEMENTED();
1816 }
Jamie Madill033dae62014-06-18 12:56:28 -04001817 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001818 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001819 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001820 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001821 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001822 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001823 outputTriplet(out, visit, "(", " == ", ")");
1824 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001825 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001826 outputTriplet(out, visit, "(", " != ", ")");
1827 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001828 case EOpLessThanComponentWise:
1829 outputTriplet(out, visit, "(", " < ", ")");
1830 break;
1831 case EOpGreaterThanComponentWise:
1832 outputTriplet(out, visit, "(", " > ", ")");
1833 break;
1834 case EOpLessThanEqualComponentWise:
1835 outputTriplet(out, visit, "(", " <= ", ")");
1836 break;
1837 case EOpGreaterThanEqualComponentWise:
1838 outputTriplet(out, visit, "(", " >= ", ")");
1839 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001840 case EOpMod:
1841 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001842 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001843 break;
1844 case EOpModf:
1845 outputTriplet(out, visit, "modf(", ", ", ")");
1846 break;
1847 case EOpPow:
1848 outputTriplet(out, visit, "pow(", ", ", ")");
1849 break;
1850 case EOpAtan:
1851 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1852 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001853 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001854 break;
1855 case EOpMin:
1856 outputTriplet(out, visit, "min(", ", ", ")");
1857 break;
1858 case EOpMax:
1859 outputTriplet(out, visit, "max(", ", ", ")");
1860 break;
1861 case EOpClamp:
1862 outputTriplet(out, visit, "clamp(", ", ", ")");
1863 break;
1864 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301865 {
1866 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1867 if (lastParamNode->getType().getBasicType() == EbtBool)
1868 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001869 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
1870 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05301871 // so use emulated version.
1872 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001873 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05301874 }
1875 else
1876 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001877 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301878 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001879 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301880 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001881 case EOpStep:
1882 outputTriplet(out, visit, "step(", ", ", ")");
1883 break;
1884 case EOpSmoothStep:
1885 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1886 break;
1887 case EOpDistance:
1888 outputTriplet(out, visit, "distance(", ", ", ")");
1889 break;
1890 case EOpDot:
1891 outputTriplet(out, visit, "dot(", ", ", ")");
1892 break;
1893 case EOpCross:
1894 outputTriplet(out, visit, "cross(", ", ", ")");
1895 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001896 case EOpFaceForward:
1897 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001898 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001899 break;
1900 case EOpReflect:
1901 outputTriplet(out, visit, "reflect(", ", ", ")");
1902 break;
1903 case EOpRefract:
1904 outputTriplet(out, visit, "refract(", ", ", ")");
1905 break;
1906 case EOpOuterProduct:
1907 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001908 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001909 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001910 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01001911 outputTriplet(out, visit, "(", " * ", ")");
1912 break;
1913 default:
1914 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915 }
1916
1917 return true;
1918}
1919
Olli Etuaho57961272016-09-14 13:57:46 +03001920void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001922 out << "if (";
1923
1924 node->getCondition()->traverse(this);
1925
1926 out << ")\n";
1927
Jamie Madill8c46ab12015-12-07 16:39:19 -05001928 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001929
1930 bool discard = false;
1931
1932 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001934 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001935 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001936
Olli Etuahoa6f22092015-05-08 18:31:10 +03001937 // Detect true discard
1938 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1939 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001940 else
1941 {
1942 // TODO(oetuaho): Check if the semicolon inside is necessary.
1943 // It's there as a result of conservative refactoring of the output.
1944 out << "{;}\n";
1945 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001946
Jamie Madill8c46ab12015-12-07 16:39:19 -05001947 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001948
Olli Etuahoa6f22092015-05-08 18:31:10 +03001949 if (node->getFalseBlock())
1950 {
1951 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001952
Jamie Madill8c46ab12015-12-07 16:39:19 -05001953 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954
Olli Etuaho32db19b2016-10-04 14:43:16 +01001955 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001956 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001957
Jamie Madill8c46ab12015-12-07 16:39:19 -05001958 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001959
Olli Etuahoa6f22092015-05-08 18:31:10 +03001960 // Detect false discard
1961 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1962 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001963
Olli Etuahoa6f22092015-05-08 18:31:10 +03001964 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001965 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001966 {
1967 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001968 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001969}
1970
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001971bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1972{
1973 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1974 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1975 UNREACHABLE();
1976 return false;
1977}
1978
Olli Etuaho57961272016-09-14 13:57:46 +03001979bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001980{
1981 TInfoSinkBase &out = getInfoSink();
1982
Olli Etuaho3d932d82016-04-12 11:10:30 +03001983 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001984
1985 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001986 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001987 {
1988 out << "FLATTEN ";
1989 }
1990
Olli Etuaho57961272016-09-14 13:57:46 +03001991 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992
1993 return false;
1994}
1995
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001996bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02001997{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001998 TInfoSinkBase &out = getInfoSink();
1999
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002000 if (node->getStatementList())
2001 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002002 node->setStatementList(
2003 RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002004 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002005 // The curly braces get written when visiting the statementList aggregate
2006 }
2007 else
2008 {
2009 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002010 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002011 }
2012 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002013}
2014
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002015bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002016{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002017 TInfoSinkBase &out = getInfoSink();
2018
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002019 if (node->hasCondition())
2020 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002021 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002022 return true;
2023 }
2024 else
2025 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002026 out << "default:\n";
2027 return false;
2028 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002029}
2030
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2032{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002033 TInfoSinkBase &out = getInfoSink();
2034 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035}
2036
2037bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2038{
Nicolas Capens655fe362014-04-11 13:12:34 -04002039 mNestedLoopDepth++;
2040
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002041 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002042 mInsideDiscontinuousLoop =
2043 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002044
Jamie Madill8c46ab12015-12-07 16:39:19 -05002045 TInfoSinkBase &out = getInfoSink();
2046
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002047 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002048 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002049 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002050 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002051 mInsideDiscontinuousLoop = wasDiscontinuous;
2052 mNestedLoopDepth--;
2053
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002054 return false;
2055 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002056 }
2057
Corentin Wallez1239ee92015-03-19 14:38:02 -07002058 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002059 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002061 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002062
Jamie Madill8c46ab12015-12-07 16:39:19 -05002063 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 }
2065 else
2066 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002067 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002068
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 if (node->getInit())
2070 {
2071 node->getInit()->traverse(this);
2072 }
2073
2074 out << "; ";
2075
alokp@chromium.org52813552010-11-16 18:36:09 +00002076 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002078 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 }
2080
2081 out << "; ";
2082
alokp@chromium.org52813552010-11-16 18:36:09 +00002083 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002085 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 }
2087
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002088 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002089
Jamie Madill8c46ab12015-12-07 16:39:19 -05002090 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091 }
2092
2093 if (node->getBody())
2094 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002095 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002096 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002098 else
2099 {
2100 // TODO(oetuaho): Check if the semicolon inside is necessary.
2101 // It's there as a result of conservative refactoring of the output.
2102 out << "{;}\n";
2103 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104
Jamie Madill8c46ab12015-12-07 16:39:19 -05002105 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106
alokp@chromium.org52813552010-11-16 18:36:09 +00002107 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002109 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 out << "while(\n";
2111
alokp@chromium.org52813552010-11-16 18:36:09 +00002112 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113
daniel@transgaming.com73536982012-03-21 20:45:49 +00002114 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 }
2116
daniel@transgaming.com73536982012-03-21 20:45:49 +00002117 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002118
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002119 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002120 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002121
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 return false;
2123}
2124
2125bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2126{
Jamie Madill32aab012015-01-27 14:12:26 -05002127 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128
2129 switch (node->getFlowOp())
2130 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002131 case EOpKill:
2132 outputTriplet(out, visit, "discard;\n", "", "");
2133 break;
2134 case EOpBreak:
2135 if (visit == PreVisit)
Nicolas Capens655fe362014-04-11 13:12:34 -04002136 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002137 if (mNestedLoopDepth > 1)
2138 {
2139 mUsesNestedBreak = true;
2140 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002141
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002142 if (mExcessiveLoopIndex)
2143 {
2144 out << "{Break";
2145 mExcessiveLoopIndex->traverse(this);
2146 out << " = true; break;}\n";
2147 }
2148 else
2149 {
2150 out << "break;\n";
2151 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002152 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002153 break;
2154 case EOpContinue:
2155 outputTriplet(out, visit, "continue;\n", "", "");
2156 break;
2157 case EOpReturn:
2158 if (visit == PreVisit)
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002159 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002160 if (node->getExpression())
2161 {
2162 out << "return ";
2163 }
2164 else
2165 {
2166 out << "return;\n";
2167 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002168 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002169 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002171 if (node->getExpression())
2172 {
2173 out << ";\n";
2174 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002176 break;
2177 default:
2178 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 }
2180
2181 return true;
2182}
2183
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002184// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002185// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2186// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002187bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002188{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002189 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002190
2191 // Parse loops of the form:
2192 // for(int index = initial; index [comparator] limit; index += increment)
2193 TIntermSymbol *index = NULL;
2194 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002195 int initial = 0;
2196 int limit = 0;
2197 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002198
2199 // Parse index name and intial value
2200 if (node->getInit())
2201 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002202 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002203
2204 if (init)
2205 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002206 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002207 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002208
2209 if (variable && variable->getQualifier() == EvqTemporary)
2210 {
2211 TIntermBinary *assign = variable->getAsBinaryNode();
2212
2213 if (assign->getOp() == EOpInitialize)
2214 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002215 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002216 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2217
2218 if (symbol && constant)
2219 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002220 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002221 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002222 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002223 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002224 }
2225 }
2226 }
2227 }
2228 }
2229 }
2230
2231 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002232 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002233 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002234 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002235
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002236 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2237 {
2238 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2239
2240 if (constant)
2241 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002242 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002243 {
2244 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002245 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002246 }
2247 }
2248 }
2249 }
2250
2251 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002252 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002253 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002254 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002255 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002256
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002257 if (binaryTerminal)
2258 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002259 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002260 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2261
2262 if (constant)
2263 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002264 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002265 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002266 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002267
2268 switch (op)
2269 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002270 case EOpAddAssign:
2271 increment = value;
2272 break;
2273 case EOpSubAssign:
2274 increment = -value;
2275 break;
2276 default:
2277 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002278 }
2279 }
2280 }
2281 }
2282 else if (unaryTerminal)
2283 {
2284 TOperator op = unaryTerminal->getOp();
2285
2286 switch (op)
2287 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002288 case EOpPostIncrement:
2289 increment = 1;
2290 break;
2291 case EOpPostDecrement:
2292 increment = -1;
2293 break;
2294 case EOpPreIncrement:
2295 increment = 1;
2296 break;
2297 case EOpPreDecrement:
2298 increment = -1;
2299 break;
2300 default:
2301 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002302 }
2303 }
2304 }
2305
2306 if (index != NULL && comparator != EOpNull && increment != 0)
2307 {
2308 if (comparator == EOpLessThanEqual)
2309 {
2310 comparator = EOpLessThan;
2311 limit += 1;
2312 }
2313
2314 if (comparator == EOpLessThan)
2315 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002316 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002317
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002318 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002319 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002320 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002321 }
2322
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002323 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002324 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002325
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002326 out << "{int ";
2327 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002328 out << ";\n"
2329 "bool Break";
2330 index->traverse(this);
2331 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002332
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002333 bool firstLoopFragment = true;
2334
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002335 while (iterations > 0)
2336 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002337 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002338
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002339 if (!firstLoopFragment)
2340 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002341 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002342 index->traverse(this);
2343 out << ") {\n";
2344 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002345
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002346 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002347 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002348 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002349 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002350
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002351 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002352 const char *unroll =
2353 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002354
Corentin Wallez1239ee92015-03-19 14:38:02 -07002355 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002356 index->traverse(this);
2357 out << " = ";
2358 out << initial;
2359
2360 out << "; ";
2361 index->traverse(this);
2362 out << " < ";
2363 out << clampedLimit;
2364
2365 out << "; ";
2366 index->traverse(this);
2367 out << " += ";
2368 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002369 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002370
Jamie Madill8c46ab12015-12-07 16:39:19 -05002371 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002372 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002373
2374 if (node->getBody())
2375 {
2376 node->getBody()->traverse(this);
2377 }
2378
Jamie Madill8c46ab12015-12-07 16:39:19 -05002379 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002380 out << ";}\n";
2381
2382 if (!firstLoopFragment)
2383 {
2384 out << "}\n";
2385 }
2386
2387 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002388
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002389 initial += MAX_LOOP_ITERATIONS * increment;
2390 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002391 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002392
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002393 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002394
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002395 mExcessiveLoopIndex = restoreIndex;
2396
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002397 return true;
2398 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002399 else
2400 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401 }
2402
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002403 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002404}
2405
Jamie Madill8c46ab12015-12-07 16:39:19 -05002406void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2407 Visit visit,
2408 const char *preString,
2409 const char *inString,
2410 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002412 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413 {
2414 out << preString;
2415 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002416 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417 {
2418 out << inString;
2419 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002420 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421 {
2422 out << postString;
2423 }
2424}
2425
Jamie Madill8c46ab12015-12-07 16:39:19 -05002426void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002427{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002428 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002429 {
Jamie Madill32aab012015-01-27 14:12:26 -05002430 out << "\n";
2431 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002432
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002433 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002434 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002435 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002436 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002437
Jamie Madill32aab012015-01-27 14:12:26 -05002438 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002439 }
2440}
2441
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002442TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2443{
2444 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002445 const TType &type = symbol->getType();
2446 const TName &name = symbol->getName();
2447 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002448
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002449 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002450 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002451 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002452 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002453 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002454 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002455 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002456 }
2457
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002458 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002459 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002460 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2461 {
2462 // Samplers are passed as indices to the sampler array.
2463 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2464 return "const uint " + nameStr + ArrayString(type);
2465 }
2466 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2467 {
2468 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2469 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2470 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2471 ArrayString(type);
2472 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002473 }
2474
Olli Etuaho96963162016-03-21 11:54:33 +02002475 TStringStream argString;
2476 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2477 << ArrayString(type);
2478
2479 // If the structure parameter contains samplers, they need to be passed into the function as
2480 // separate parameters. HLSL doesn't natively support samplers in structs.
2481 if (type.isStructureContainingSamplers())
2482 {
2483 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2484 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002485 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002486 for (const TIntermSymbol *sampler : samplerSymbols)
2487 {
2488 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2489 {
2490 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2491 }
2492 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2493 {
2494 const TType &samplerType = sampler->getType();
2495 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2496 type.getArraySize() == samplerType.getArraySize());
2497 ASSERT(IsSampler(samplerType.getBasicType()));
2498 argString << ", " << QualifierString(qualifier) << " "
2499 << TextureString(samplerType.getBasicType()) << " texture_"
2500 << sampler->getSymbol() << ArrayString(type) << ", "
2501 << QualifierString(qualifier) << " "
2502 << SamplerString(samplerType.getBasicType()) << " sampler_"
2503 << sampler->getSymbol() << ArrayString(type);
2504 }
2505 else
2506 {
2507 const TType &samplerType = sampler->getType();
2508 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2509 type.getArraySize() == samplerType.getArraySize());
2510 ASSERT(IsSampler(samplerType.getBasicType()));
2511 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2512 << " " << sampler->getSymbol() << ArrayString(type);
2513 }
2514 }
2515 }
2516
2517 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002518}
2519
2520TString OutputHLSL::initializer(const TType &type)
2521{
2522 TString string;
2523
Jamie Madill94bf7f22013-07-08 13:31:15 -04002524 size_t size = type.getObjectSize();
2525 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002526 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002527 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002528
Jamie Madill94bf7f22013-07-08 13:31:15 -04002529 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530 {
2531 string += ", ";
2532 }
2533 }
2534
daniel@transgaming.comead23042010-04-29 03:35:36 +00002535 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002536}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002537
Jamie Madill8c46ab12015-12-07 16:39:19 -05002538void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2539 Visit visit,
2540 const TType &type,
2541 const char *name,
2542 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002543{
Olli Etuahof40319e2015-03-10 14:33:00 +02002544 if (type.isArray())
2545 {
2546 UNIMPLEMENTED();
2547 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002548
2549 if (visit == PreVisit)
2550 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002551 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002552
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002553 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002554 }
2555 else if (visit == InVisit)
2556 {
2557 out << ", ";
2558 }
2559 else if (visit == PostVisit)
2560 {
2561 out << ")";
2562 }
2563}
2564
Jamie Madill8c46ab12015-12-07 16:39:19 -05002565const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2566 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002567 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002568{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002569 const TConstantUnion *constUnionIterated = constUnion;
2570
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002571 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002572 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002573 {
Jamie Madill033dae62014-06-18 12:56:28 -04002574 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002575
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002576 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002577
Jamie Madill98493dd2013-07-08 14:39:03 -04002578 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002579 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002580 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002581 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002582
Jamie Madill98493dd2013-07-08 14:39:03 -04002583 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002584 {
2585 out << ", ";
2586 }
2587 }
2588
2589 out << ")";
2590 }
2591 else
2592 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002593 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002594 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002595
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002596 if (writeType)
2597 {
Jamie Madill033dae62014-06-18 12:56:28 -04002598 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002599 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002600 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002601 if (writeType)
2602 {
2603 out << ")";
2604 }
2605 }
2606
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002607 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002608}
2609
Olli Etuahod68924e2017-01-02 17:34:40 +00002610void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002611{
Olli Etuahod68924e2017-01-02 17:34:40 +00002612 if (visit == PreVisit)
2613 {
2614 const char *opStr = GetOperatorString(op);
2615 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2616 out << "(";
2617 }
2618 else
2619 {
2620 outputTriplet(out, visit, nullptr, ", ", ")");
2621 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002622}
2623
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002624bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2625 TIntermSymbol *symbolNode,
2626 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002627{
2628 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2629 expression->traverse(&searchSymbol);
2630
2631 if (searchSymbol.foundMatch())
2632 {
2633 // Type already printed
2634 out << "t" + str(mUniqueIndex) + " = ";
2635 expression->traverse(this);
2636 out << ", ";
2637 symbolNode->traverse(this);
2638 out << " = t" + str(mUniqueIndex);
2639
2640 mUniqueIndex++;
2641 return true;
2642 }
2643
2644 return false;
2645}
2646
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002647bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2648{
2649 // We support writing constant unions and constructors that only take constant unions as
2650 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002651 return expression->getAsConstantUnion() ||
2652 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002653}
2654
2655bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2656 TIntermSymbol *symbolNode,
2657 TIntermTyped *expression)
2658{
2659 if (canWriteAsHLSLLiteral(expression))
2660 {
2661 symbolNode->traverse(this);
2662 if (expression->getType().isArray())
2663 {
2664 out << "[" << expression->getType().getArraySize() << "]";
2665 }
2666 out << " = {";
2667 if (expression->getAsConstantUnion())
2668 {
2669 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2670 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002671 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002672 }
2673 else
2674 {
2675 TIntermAggregate *constructor = expression->getAsAggregate();
2676 ASSERT(constructor != nullptr);
2677 for (TIntermNode *&node : *constructor->getSequence())
2678 {
2679 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2680 ASSERT(nodeConst);
2681 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002682 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002683 if (node != constructor->getSequence()->back())
2684 {
2685 out << ", ";
2686 }
2687 }
2688 }
2689 out << "}";
2690 return true;
2691 }
2692 return false;
2693}
2694
Jamie Madill55e79e02015-02-09 15:35:00 -05002695TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2696{
2697 const TFieldList &fields = structure.fields();
2698
2699 for (const auto &eqFunction : mStructEqualityFunctions)
2700 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002701 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002702 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002703 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002704 }
2705 }
2706
2707 const TString &structNameString = StructNameString(structure);
2708
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002709 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002710 function->structure = &structure;
2711 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002712
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002713 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002714
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002715 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2716 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002717 << "{\n"
2718 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002719
2720 for (size_t i = 0; i < fields.size(); i++)
2721 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002722 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002723 const TType *fieldType = field->type();
2724
2725 const TString &fieldNameA = "a." + Decorate(field->name());
2726 const TString &fieldNameB = "b." + Decorate(field->name());
2727
2728 if (i > 0)
2729 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002730 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002731 }
2732
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002733 fnOut << "(";
2734 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2735 fnOut << fieldNameA;
2736 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2737 fnOut << fieldNameB;
2738 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2739 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002740 }
2741
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002742 fnOut << ";\n"
2743 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002744
2745 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002746
2747 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002748 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002749
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002750 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002751}
2752
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002753TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002754{
2755 for (const auto &eqFunction : mArrayEqualityFunctions)
2756 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002757 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002758 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002759 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002760 }
2761 }
2762
2763 const TString &typeName = TypeString(type);
2764
Olli Etuaho12690762015-03-31 12:55:28 +03002765 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002766 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002767
2768 TInfoSinkBase fnNameOut;
2769 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002770 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002771
2772 TType nonArrayType = type;
2773 nonArrayType.clearArrayness();
2774
2775 TInfoSinkBase fnOut;
2776
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002777 fnOut << "bool " << function->functionName << "(" << typeName << " a[" << type.getArraySize()
2778 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002779 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002780 " for (int i = 0; i < "
2781 << type.getArraySize() << "; ++i)\n"
2782 " {\n"
2783 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002784
2785 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2786 fnOut << "a[i]";
2787 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2788 fnOut << "b[i]";
2789 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2790
2791 fnOut << ") { return false; }\n"
2792 " }\n"
2793 " return true;\n"
2794 "}\n";
2795
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002796 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002797
2798 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002799 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002800
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002801 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002802}
2803
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002804TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002805{
2806 for (const auto &assignFunction : mArrayAssignmentFunctions)
2807 {
2808 if (assignFunction.type == type)
2809 {
2810 return assignFunction.functionName;
2811 }
2812 }
2813
2814 const TString &typeName = TypeString(type);
2815
2816 ArrayHelperFunction function;
2817 function.type = type;
2818
2819 TInfoSinkBase fnNameOut;
2820 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2821 function.functionName = fnNameOut.c_str();
2822
2823 TInfoSinkBase fnOut;
2824
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002825 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2826 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
2827 << "{\n"
2828 " for (int i = 0; i < "
2829 << type.getArraySize() << "; ++i)\n"
2830 " {\n"
2831 " a[i] = b[i];\n"
2832 " }\n"
2833 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03002834
2835 function.functionDefinition = fnOut.c_str();
2836
2837 mArrayAssignmentFunctions.push_back(function);
2838
2839 return function.functionName;
2840}
2841
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002842TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03002843{
2844 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2845 {
2846 if (constructIntoFunction.type == type)
2847 {
2848 return constructIntoFunction.functionName;
2849 }
2850 }
2851
2852 const TString &typeName = TypeString(type);
2853
2854 ArrayHelperFunction function;
2855 function.type = type;
2856
2857 TInfoSinkBase fnNameOut;
2858 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2859 function.functionName = fnNameOut.c_str();
2860
2861 TInfoSinkBase fnOut;
2862
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002863 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2864 << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002865 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002866 {
2867 fnOut << ", " << typeName << " b" << i;
2868 }
2869 fnOut << ")\n"
2870 "{\n";
2871
Olli Etuaho856c4972016-08-08 11:38:39 +03002872 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002873 {
2874 fnOut << " a[" << i << "] = b" << i << ";\n";
2875 }
2876 fnOut << "}\n";
2877
2878 function.functionDefinition = fnOut.c_str();
2879
2880 mArrayConstructIntoFunctions.push_back(function);
2881
2882 return function.functionName;
2883}
2884
Jamie Madill2e295e22015-04-29 10:41:33 -04002885void OutputHLSL::ensureStructDefined(const TType &type)
2886{
2887 TStructure *structure = type.getStruct();
2888
2889 if (structure)
2890 {
2891 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2892 }
2893}
2894
Jamie Madill45bcc782016-11-07 13:58:48 -05002895} // namespace sh