blob: ecc338162b6412e9e51afe191f16cac42bb17724 [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
Corentin Wallez1239ee92015-03-19 14:38:02 -0700179 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
180 ASSERT(success == CallDAG::INITDAG_SUCCESS);
181 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700182
Jamie Madill37997142015-01-28 10:06:34 -0500183 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500184 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200185 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500186 mInfoSinkStack.pop();
187
Jamie Madill37997142015-01-28 10:06:34 -0500188 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500189 mInfoSinkStack.pop();
190
Jamie Madill32aab012015-01-27 14:12:26 -0500191 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500192 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500193 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000194
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200195 objSink << mHeader.c_str();
196 objSink << mBody.c_str();
197 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200198
199 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000200}
201
Jamie Madill570e04d2013-06-21 09:15:33 -0400202void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
203{
204 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
205 {
206 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
207
Jamie Madill32aab012015-01-27 14:12:26 -0500208 TInfoSinkBase structInfoSink;
209 mInfoSinkStack.push(&structInfoSink);
210
Jamie Madill570e04d2013-06-21 09:15:33 -0400211 // This will mark the necessary block elements as referenced
212 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500213
214 TString structName(structInfoSink.c_str());
215 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400216
217 mFlaggedStructOriginalNames[flaggedNode] = structName;
218
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500219 for (size_t pos = structName.find('.'); pos != std::string::npos;
220 pos = structName.find('.'))
Jamie Madill570e04d2013-06-21 09:15:33 -0400221 {
222 structName.erase(pos, 1);
223 }
224
225 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
226 }
227}
228
Jamie Madill4e1fd412014-07-10 17:50:10 -0400229const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
230{
231 return mUniformHLSL->getInterfaceBlockRegisterMap();
232}
233
Jamie Madill9fe25e92014-07-18 10:33:08 -0400234const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
235{
236 return mUniformHLSL->getUniformRegisterMap();
237}
238
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000239int OutputHLSL::vectorSize(const TType &type) const
240{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500241 int elementSize = type.isMatrix() ? type.getCols() : 1;
Olli Etuaho856c4972016-08-08 11:38:39 +0300242 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000243
244 return elementSize * arraySize;
245}
246
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500247TString OutputHLSL::structInitializerString(int indent,
248 const TStructure &structure,
249 const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400250{
251 TString init;
252
253 TString preIndentString;
254 TString fullIndentString;
255
256 for (int spaces = 0; spaces < (indent * 4); spaces++)
257 {
258 preIndentString += ' ';
259 }
260
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500261 for (int spaces = 0; spaces < ((indent + 1) * 4); spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400262 {
263 fullIndentString += ' ';
264 }
265
266 init += preIndentString + "{\n";
267
Jamie Madill98493dd2013-07-08 14:39:03 -0400268 const TFieldList &fields = structure.fields();
269 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400270 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500271 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400272 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500273 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400274
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400276 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400278 }
279 else
280 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400282 }
283 }
284
285 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
286
287 return init;
288}
289
Jamie Madill8c46ab12015-12-07 16:39:19 -0500290void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000292 TString varyings;
293 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400294 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000295
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500296 for (std::map<TIntermTyped *, TString>::const_iterator flaggedStructIt =
297 mFlaggedStructMappedNames.begin();
298 flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400299 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500300 TIntermTyped *structNode = flaggedStructIt->first;
301 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400302 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400303 const TString &originalName = mFlaggedStructOriginalNames[structNode];
304
Jamie Madill033dae62014-06-18 12:56:28 -0400305 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400306 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 flaggedStructs += "\n";
308 }
309
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500310 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin();
311 varying != mReferencedVaryings.end(); varying++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000312 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500313 const TType &type = varying->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000314 const TString &name = varying->second->getSymbol();
315
316 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500317 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
318 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000319 }
320
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500321 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin();
322 attribute != mReferencedAttributes.end(); attribute++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000323 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324 const TType &type = attribute->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000325 const TString &name = attribute->second->getSymbol();
326
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500327 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
328 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000329 }
330
Jamie Madill8daaba12014-06-13 10:04:33 -0400331 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400332
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200333 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400334 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
335
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200336 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500337 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200338 out << "\n// Equality functions\n\n";
339 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500340 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200341 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200342 }
343 }
Olli Etuaho12690762015-03-31 12:55:28 +0300344 if (!mArrayAssignmentFunctions.empty())
345 {
346 out << "\n// Assignment functions\n\n";
347 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
348 {
349 out << assignmentFunction.functionDefinition << "\n";
350 }
351 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300352 if (!mArrayConstructIntoFunctions.empty())
353 {
354 out << "\n// Array constructor functions\n\n";
355 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
356 {
357 out << constructIntoFunction.functionDefinition << "\n";
358 }
359 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200360
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500361 if (mUsesDiscardRewriting)
362 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400363 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500364 }
365
Nicolas Capens655fe362014-04-11 13:12:34 -0400366 if (mUsesNestedBreak)
367 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400368 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400369 }
370
Arun Patole44efa0b2015-03-04 17:11:05 +0530371 if (mRequiresIEEEStrictCompiling)
372 {
373 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
374 }
375
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400376 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
377 "#define LOOP [loop]\n"
378 "#define FLATTEN [flatten]\n"
379 "#else\n"
380 "#define LOOP\n"
381 "#define FLATTEN\n"
382 "#endif\n";
383
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200384 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200386 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500387 const bool usingMRTExtension = (iter != mExtensionBehavior.end() &&
388 (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000389
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000390 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500391 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400392 out << "\n";
393
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200394 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000395 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500396 for (ReferencedSymbols::const_iterator outputVariableIt =
397 mReferencedOutputVariables.begin();
398 outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000399 {
Jamie Madill46131a32013-06-20 11:55:50 -0400400 const TString &variableName = outputVariableIt->first;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500401 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400402
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500403 out << "static " + TypeString(variableType) + " out_" + variableName +
404 ArrayString(variableType) + " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000405 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000406 }
Jamie Madill46131a32013-06-20 11:55:50 -0400407 else
408 {
409 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
410
411 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500412 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400413 for (unsigned int i = 0; i < numColorValues; i++)
414 {
415 out << " float4(0, 0, 0, 0)";
416 if (i + 1 != numColorValues)
417 {
418 out << ",";
419 }
420 out << "\n";
421 }
422
423 out << "};\n";
424 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000425
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400426 if (mUsesFragDepth)
427 {
428 out << "static float gl_Depth = 0.0;\n";
429 }
430
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000431 if (mUsesFragCoord)
432 {
433 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
434 }
435
436 if (mUsesPointCoord)
437 {
438 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
439 }
440
441 if (mUsesFrontFacing)
442 {
443 out << "static bool gl_FrontFacing = false;\n";
444 }
445
446 out << "\n";
447
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000448 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000449 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000450 out << "struct gl_DepthRangeParameters\n"
451 "{\n"
452 " float near;\n"
453 " float far;\n"
454 " float diff;\n"
455 "};\n"
456 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000457 }
458
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200459 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000460 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000461 out << "cbuffer DriverConstants : register(b1)\n"
462 "{\n";
463
464 if (mUsesDepthRange)
465 {
466 out << " float3 dx_DepthRange : packoffset(c0);\n";
467 }
468
469 if (mUsesFragCoord)
470 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000471 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000472 }
473
474 if (mUsesFragCoord || mUsesFrontFacing)
475 {
476 out << " float3 dx_DepthFront : packoffset(c2);\n";
477 }
478
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800479 if (mUsesFragCoord)
480 {
481 // dx_ViewScale is only used in the fragment shader to correct
482 // the value for glFragCoord if necessary
483 out << " float2 dx_ViewScale : packoffset(c3);\n";
484 }
485
Olli Etuaho618bebc2016-01-15 16:40:00 +0200486 if (mOutputType == SH_HLSL_4_1_OUTPUT)
487 {
488 mUniformHLSL->samplerMetadataUniforms(out, "c4");
489 }
490
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000491 out << "};\n";
492 }
493 else
494 {
495 if (mUsesDepthRange)
496 {
497 out << "uniform float3 dx_DepthRange : register(c0);";
498 }
499
500 if (mUsesFragCoord)
501 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000502 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000503 }
504
505 if (mUsesFragCoord || mUsesFrontFacing)
506 {
507 out << "uniform float3 dx_DepthFront : register(c2);\n";
508 }
509 }
510
511 out << "\n";
512
513 if (mUsesDepthRange)
514 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500515 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
516 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000517 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000518 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000519
Jamie Madillf91ce812014-06-13 10:04:34 -0400520 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000521 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400522 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000523 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400524 out << flaggedStructs;
525 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000526 }
527
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000528 if (usingMRTExtension && mNumRenderTargets > 1)
529 {
530 out << "#define GL_USES_MRT\n";
531 }
532
533 if (mUsesFragColor)
534 {
535 out << "#define GL_USES_FRAG_COLOR\n";
536 }
537
538 if (mUsesFragData)
539 {
540 out << "#define GL_USES_FRAG_DATA\n";
541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500543 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000545 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500546 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000547 out << "\n"
548 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400549
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000550 if (mUsesPointSize)
551 {
552 out << "static float gl_PointSize = float(1);\n";
553 }
554
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000555 if (mUsesInstanceID)
556 {
557 out << "static int gl_InstanceID;";
558 }
559
Corentin Wallezb076add2016-01-11 16:45:46 -0500560 if (mUsesVertexID)
561 {
562 out << "static int gl_VertexID;";
563 }
564
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000565 out << "\n"
566 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500567 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000568 out << "\n";
569
570 if (mUsesDepthRange)
571 {
572 out << "struct gl_DepthRangeParameters\n"
573 "{\n"
574 " float near;\n"
575 " float far;\n"
576 " float diff;\n"
577 "};\n"
578 "\n";
579 }
580
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200581 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000582 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800583 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500584 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800585
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000586 if (mUsesDepthRange)
587 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800588 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000589 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800590
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800591 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
592 // shaders. However, we declare it for all shaders (including Feature Level 10+).
593 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
594 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800595 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800596 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800597 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800598
Olli Etuaho618bebc2016-01-15 16:40:00 +0200599 if (mOutputType == SH_HLSL_4_1_OUTPUT)
600 {
601 mUniformHLSL->samplerMetadataUniforms(out, "c4");
602 }
603
Austin Kinross4fd18b12014-12-22 12:32:05 -0800604 out << "};\n"
605 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000606 }
607 else
608 {
609 if (mUsesDepthRange)
610 {
611 out << "uniform float3 dx_DepthRange : register(c0);\n";
612 }
613
Cooper Partine6664f02015-01-09 16:22:24 -0800614 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
615 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000616 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000617 }
618
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000619 if (mUsesDepthRange)
620 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500621 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
622 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000623 "\n";
624 }
625
Jamie Madillf91ce812014-06-13 10:04:34 -0400626 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000627 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400628 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000629 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400630 out << flaggedStructs;
631 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000632 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400633 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000634
Geoff Lang1fe74c72016-08-25 13:23:01 -0400635 bool getDimensionsIgnoresBaseLevel =
636 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
637 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000638
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000639 if (mUsesFragCoord)
640 {
641 out << "#define GL_USES_FRAG_COORD\n";
642 }
643
644 if (mUsesPointCoord)
645 {
646 out << "#define GL_USES_POINT_COORD\n";
647 }
648
649 if (mUsesFrontFacing)
650 {
651 out << "#define GL_USES_FRONT_FACING\n";
652 }
653
654 if (mUsesPointSize)
655 {
656 out << "#define GL_USES_POINT_SIZE\n";
657 }
658
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400659 if (mUsesFragDepth)
660 {
661 out << "#define GL_USES_FRAG_DEPTH\n";
662 }
663
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000664 if (mUsesDepthRange)
665 {
666 out << "#define GL_USES_DEPTH_RANGE\n";
667 }
668
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000669 if (mUsesXor)
670 {
671 out << "bool xor(bool p, bool q)\n"
672 "{\n"
673 " return (p || q) && !(p && q);\n"
674 "}\n"
675 "\n";
676 }
677
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200678 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679}
680
681void OutputHLSL::visitSymbol(TIntermSymbol *node)
682{
Jamie Madill32aab012015-01-27 14:12:26 -0500683 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684
Jamie Madill570e04d2013-06-21 09:15:33 -0400685 // Handle accessing std140 structs by value
686 if (mFlaggedStructMappedNames.count(node) > 0)
687 {
688 out << mFlaggedStructMappedNames[node];
689 return;
690 }
691
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692 TString name = node->getSymbol();
693
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000694 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000695 {
696 mUsesDepthRange = true;
697 out << name;
698 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699 else
700 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000701 TQualifier qualifier = node->getQualifier();
702
703 if (qualifier == EvqUniform)
704 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500705 const TType &nodeType = node->getType();
Jamie Madill2e295e22015-04-29 10:41:33 -0400706 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400707
708 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000709 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400710 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000711 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000712 else
713 {
714 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000715 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400716
Jamie Madill2e295e22015-04-29 10:41:33 -0400717 ensureStructDefined(nodeType);
718
Olli Etuaho96963162016-03-21 11:54:33 +0200719 const TName &nameWithMetadata = node->getName();
720 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000721 }
Jamie Madill19571812013-08-12 15:26:34 -0700722 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000723 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000724 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400725 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000726 }
Jamie Madill033dae62014-06-18 12:56:28 -0400727 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000728 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000729 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400730 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000731 }
Jamie Madill19571812013-08-12 15:26:34 -0700732 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400733 {
734 mReferencedOutputVariables[name] = node;
735 out << "out_" << name;
736 }
737 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000738 {
739 out << "gl_Color[0]";
740 mUsesFragColor = true;
741 }
742 else if (qualifier == EvqFragData)
743 {
744 out << "gl_Color";
745 mUsesFragData = true;
746 }
747 else if (qualifier == EvqFragCoord)
748 {
749 mUsesFragCoord = true;
750 out << name;
751 }
752 else if (qualifier == EvqPointCoord)
753 {
754 mUsesPointCoord = true;
755 out << name;
756 }
757 else if (qualifier == EvqFrontFacing)
758 {
759 mUsesFrontFacing = true;
760 out << name;
761 }
762 else if (qualifier == EvqPointSize)
763 {
764 mUsesPointSize = true;
765 out << name;
766 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000767 else if (qualifier == EvqInstanceID)
768 {
769 mUsesInstanceID = true;
770 out << name;
771 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500772 else if (qualifier == EvqVertexID)
773 {
774 mUsesVertexID = true;
775 out << name;
776 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300777 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400778 {
779 mUsesFragDepth = true;
780 out << "gl_Depth";
781 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000782 else
783 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300784 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786 }
787}
788
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400789void OutputHLSL::visitRaw(TIntermRaw *node)
790{
Jamie Madill32aab012015-01-27 14:12:26 -0500791 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400792}
793
Olli Etuaho7fb49552015-03-18 17:27:44 +0200794void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
795{
796 if (type.isScalar() && !type.isArray())
797 {
798 if (op == EOpEqual)
799 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500800 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200801 }
802 else
803 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500804 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200805 }
806 }
807 else
808 {
809 if (visit == PreVisit && op == EOpNotEqual)
810 {
811 out << "!";
812 }
813
814 if (type.isArray())
815 {
816 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500817 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200818 }
819 else if (type.getBasicType() == EbtStruct)
820 {
821 const TStructure &structure = *type.getStruct();
822 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500823 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200824 }
825 else
826 {
827 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500828 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200829 }
830 }
831}
832
Olli Etuaho96963162016-03-21 11:54:33 +0200833bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
834{
835 // Inside InVisit the current node is already in the path.
836 const unsigned int initialN = visit == InVisit ? 1u : 0u;
837 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
838 {
839 TIntermNode *ancestor = getAncestorNode(n);
840 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
841 if (ancestorBinary == nullptr)
842 {
843 return false;
844 }
845 switch (ancestorBinary->getOp())
846 {
847 case EOpIndexDirectStruct:
848 {
849 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
850 const TIntermConstantUnion *index =
851 ancestorBinary->getRight()->getAsConstantUnion();
852 const TField *field = structure->fields()[index->getIConst(0)];
853 if (IsSampler(field->type()->getBasicType()))
854 {
855 return true;
856 }
857 break;
858 }
859 case EOpIndexDirect:
860 break;
861 default:
862 // Returning a sampler from indirect indexing is not supported.
863 return false;
864 }
865 }
866 return false;
867}
868
Olli Etuahob6fa0432016-09-28 16:28:05 +0100869bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
870{
871 TInfoSinkBase &out = getInfoSink();
872 if (visit == PostVisit)
873 {
874 out << ".";
875 node->writeOffsetsAsXYZW(&out);
876 }
877 return true;
878}
879
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
881{
Jamie Madill32aab012015-01-27 14:12:26 -0500882 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883
Jamie Madill570e04d2013-06-21 09:15:33 -0400884 // Handle accessing std140 structs by value
885 if (mFlaggedStructMappedNames.count(node) > 0)
886 {
887 out << mFlaggedStructMappedNames[node];
888 return false;
889 }
890
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891 switch (node->getOp())
892 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100893 case EOpComma:
894 outputTriplet(out, visit, "(", ", ", ")");
895 break;
896 case EOpAssign:
897 if (node->getLeft()->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +0300898 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100899 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
900 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +0300901 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100902 const TString &functionName = addArrayConstructIntoFunction(node->getType());
903 out << functionName << "(";
904 node->getLeft()->traverse(this);
905 TIntermSequence *seq = rightAgg->getSequence();
906 for (auto &arrayElement : *seq)
907 {
908 out << ", ";
909 arrayElement->traverse(this);
910 }
911 out << ")";
912 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +0300913 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100914 // ArrayReturnValueToOutParameter should have eliminated expressions where a
915 // function call is assigned.
916 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
917
918 const TString &functionName = addArrayAssignmentFunction(node->getType());
919 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho9638c352015-04-01 14:34:52 +0300920 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100921 else
Jamie Madill37997142015-01-28 10:06:34 -0500922 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100923 outputTriplet(out, visit, "(", " = ", ")");
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000924 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100925 break;
926 case EOpInitialize:
927 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200928 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100929 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
930 ASSERT(symbolNode);
931 TIntermTyped *expression = node->getRight();
932
933 // Global initializers must be constant at this point.
934 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
935 canWriteAsHLSLLiteral(expression));
936
937 // GLSL allows to write things like "float x = x;" where a new variable x is defined
938 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
939 // new variable is created before the assignment is evaluated), so we need to
940 // convert
941 // this to "float t = x, x = t;".
942 if (writeSameSymbolInitializer(out, symbolNode, expression))
943 {
944 // Skip initializing the rest of the expression
945 return false;
946 }
947 else if (writeConstantInitialization(out, symbolNode, expression))
948 {
949 return false;
950 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200951 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100952 else if (visit == InVisit)
953 {
954 out << " = ";
955 }
956 break;
957 case EOpAddAssign:
958 outputTriplet(out, visit, "(", " += ", ")");
959 break;
960 case EOpSubAssign:
961 outputTriplet(out, visit, "(", " -= ", ")");
962 break;
963 case EOpMulAssign:
964 outputTriplet(out, visit, "(", " *= ", ")");
965 break;
966 case EOpVectorTimesScalarAssign:
967 outputTriplet(out, visit, "(", " *= ", ")");
968 break;
969 case EOpMatrixTimesScalarAssign:
970 outputTriplet(out, visit, "(", " *= ", ")");
971 break;
972 case EOpVectorTimesMatrixAssign:
973 if (visit == PreVisit)
974 {
975 out << "(";
976 }
977 else if (visit == InVisit)
978 {
979 out << " = mul(";
980 node->getLeft()->traverse(this);
981 out << ", transpose(";
982 }
983 else
984 {
985 out << ")))";
986 }
987 break;
988 case EOpMatrixTimesMatrixAssign:
989 if (visit == PreVisit)
990 {
991 out << "(";
992 }
993 else if (visit == InVisit)
994 {
995 out << " = transpose(mul(transpose(";
996 node->getLeft()->traverse(this);
997 out << "), transpose(";
998 }
999 else
1000 {
1001 out << "))))";
1002 }
1003 break;
1004 case EOpDivAssign:
1005 outputTriplet(out, visit, "(", " /= ", ")");
1006 break;
1007 case EOpIModAssign:
1008 outputTriplet(out, visit, "(", " %= ", ")");
1009 break;
1010 case EOpBitShiftLeftAssign:
1011 outputTriplet(out, visit, "(", " <<= ", ")");
1012 break;
1013 case EOpBitShiftRightAssign:
1014 outputTriplet(out, visit, "(", " >>= ", ")");
1015 break;
1016 case EOpBitwiseAndAssign:
1017 outputTriplet(out, visit, "(", " &= ", ")");
1018 break;
1019 case EOpBitwiseXorAssign:
1020 outputTriplet(out, visit, "(", " ^= ", ")");
1021 break;
1022 case EOpBitwiseOrAssign:
1023 outputTriplet(out, visit, "(", " |= ", ")");
1024 break;
1025 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001026 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001027 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001028 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001029 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001030 if (visit == PreVisit)
1031 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001032 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001033 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001034 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] =
1035 node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001036 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001037 return false;
1038 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001039 }
Olli Etuaho96963162016-03-21 11:54:33 +02001040 else if (ancestorEvaluatesToSamplerInStruct(visit))
1041 {
1042 // All parts of an expression that access a sampler in a struct need to use _ as
1043 // separator to access the sampler variable that has been moved out of the struct.
1044 outputTriplet(out, visit, "", "_", "");
1045 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001046 else
1047 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001048 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001049 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001050 }
1051 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001052 case EOpIndexIndirect:
1053 // We do not currently support indirect references to interface blocks
1054 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1055 outputTriplet(out, visit, "", "[", "]");
1056 break;
1057 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001058 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001059 const TStructure *structure = node->getLeft()->getType().getStruct();
1060 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1061 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001062
Olli Etuaho96963162016-03-21 11:54:33 +02001063 // In cases where indexing returns a sampler, we need to access the sampler variable
1064 // that has been moved out of the struct.
1065 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1066 if (visit == PreVisit && indexingReturnsSampler)
1067 {
1068 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1069 // This prefix is only output at the beginning of the indexing expression, which
1070 // may have multiple parts.
1071 out << "angle";
1072 }
1073 if (!indexingReturnsSampler)
1074 {
1075 // All parts of an expression that access a sampler in a struct need to use _ as
1076 // separator to access the sampler variable that has been moved out of the struct.
1077 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1078 }
1079 if (visit == InVisit)
1080 {
1081 if (indexingReturnsSampler)
1082 {
1083 out << "_" + field->name();
1084 }
1085 else
1086 {
1087 out << "." + DecorateField(field->name(), *structure);
1088 }
1089
1090 return false;
1091 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001092 }
1093 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001094 case EOpIndexDirectInterfaceBlock:
1095 if (visit == InVisit)
1096 {
1097 const TInterfaceBlock *interfaceBlock =
1098 node->getLeft()->getType().getInterfaceBlock();
1099 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1100 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
1101 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001102
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001103 return false;
1104 }
1105 break;
1106 case EOpAdd:
1107 outputTriplet(out, visit, "(", " + ", ")");
1108 break;
1109 case EOpSub:
1110 outputTriplet(out, visit, "(", " - ", ")");
1111 break;
1112 case EOpMul:
1113 outputTriplet(out, visit, "(", " * ", ")");
1114 break;
1115 case EOpDiv:
1116 outputTriplet(out, visit, "(", " / ", ")");
1117 break;
1118 case EOpIMod:
1119 outputTriplet(out, visit, "(", " % ", ")");
1120 break;
1121 case EOpBitShiftLeft:
1122 outputTriplet(out, visit, "(", " << ", ")");
1123 break;
1124 case EOpBitShiftRight:
1125 outputTriplet(out, visit, "(", " >> ", ")");
1126 break;
1127 case EOpBitwiseAnd:
1128 outputTriplet(out, visit, "(", " & ", ")");
1129 break;
1130 case EOpBitwiseXor:
1131 outputTriplet(out, visit, "(", " ^ ", ")");
1132 break;
1133 case EOpBitwiseOr:
1134 outputTriplet(out, visit, "(", " | ", ")");
1135 break;
1136 case EOpEqual:
1137 case EOpNotEqual:
1138 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1139 break;
1140 case EOpLessThan:
1141 outputTriplet(out, visit, "(", " < ", ")");
1142 break;
1143 case EOpGreaterThan:
1144 outputTriplet(out, visit, "(", " > ", ")");
1145 break;
1146 case EOpLessThanEqual:
1147 outputTriplet(out, visit, "(", " <= ", ")");
1148 break;
1149 case EOpGreaterThanEqual:
1150 outputTriplet(out, visit, "(", " >= ", ")");
1151 break;
1152 case EOpVectorTimesScalar:
1153 outputTriplet(out, visit, "(", " * ", ")");
1154 break;
1155 case EOpMatrixTimesScalar:
1156 outputTriplet(out, visit, "(", " * ", ")");
1157 break;
1158 case EOpVectorTimesMatrix:
1159 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1160 break;
1161 case EOpMatrixTimesVector:
1162 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1163 break;
1164 case EOpMatrixTimesMatrix:
1165 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1166 break;
1167 case EOpLogicalOr:
1168 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1169 // been unfolded.
1170 ASSERT(!node->getRight()->hasSideEffects());
1171 outputTriplet(out, visit, "(", " || ", ")");
1172 return true;
1173 case EOpLogicalXor:
1174 mUsesXor = true;
1175 outputTriplet(out, visit, "xor(", ", ", ")");
1176 break;
1177 case EOpLogicalAnd:
1178 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1179 // been unfolded.
1180 ASSERT(!node->getRight()->hasSideEffects());
1181 outputTriplet(out, visit, "(", " && ", ")");
1182 return true;
1183 default:
1184 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185 }
1186
1187 return true;
1188}
1189
1190bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1191{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001192 TInfoSinkBase &out = getInfoSink();
1193
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001194 switch (node->getOp())
1195 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001196 case EOpNegative:
1197 outputTriplet(out, visit, "(-", "", ")");
1198 break;
1199 case EOpPositive:
1200 outputTriplet(out, visit, "(+", "", ")");
1201 break;
1202 case EOpVectorLogicalNot:
1203 outputTriplet(out, visit, "(!", "", ")");
1204 break;
1205 case EOpLogicalNot:
1206 outputTriplet(out, visit, "(!", "", ")");
1207 break;
1208 case EOpBitwiseNot:
1209 outputTriplet(out, visit, "(~", "", ")");
1210 break;
1211 case EOpPostIncrement:
1212 outputTriplet(out, visit, "(", "", "++)");
1213 break;
1214 case EOpPostDecrement:
1215 outputTriplet(out, visit, "(", "", "--)");
1216 break;
1217 case EOpPreIncrement:
1218 outputTriplet(out, visit, "(++", "", ")");
1219 break;
1220 case EOpPreDecrement:
1221 outputTriplet(out, visit, "(--", "", ")");
1222 break;
1223 case EOpRadians:
1224 outputTriplet(out, visit, "radians(", "", ")");
1225 break;
1226 case EOpDegrees:
1227 outputTriplet(out, visit, "degrees(", "", ")");
1228 break;
1229 case EOpSin:
1230 outputTriplet(out, visit, "sin(", "", ")");
1231 break;
1232 case EOpCos:
1233 outputTriplet(out, visit, "cos(", "", ")");
1234 break;
1235 case EOpTan:
1236 outputTriplet(out, visit, "tan(", "", ")");
1237 break;
1238 case EOpAsin:
1239 outputTriplet(out, visit, "asin(", "", ")");
1240 break;
1241 case EOpAcos:
1242 outputTriplet(out, visit, "acos(", "", ")");
1243 break;
1244 case EOpAtan:
1245 outputTriplet(out, visit, "atan(", "", ")");
1246 break;
1247 case EOpSinh:
1248 outputTriplet(out, visit, "sinh(", "", ")");
1249 break;
1250 case EOpCosh:
1251 outputTriplet(out, visit, "cosh(", "", ")");
1252 break;
1253 case EOpTanh:
1254 outputTriplet(out, visit, "tanh(", "", ")");
1255 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001256 case EOpAsinh:
1257 ASSERT(node->getUseEmulatedFunction());
1258 writeEmulatedFunctionTriplet(out, visit, "asinh(");
1259 break;
1260 case EOpAcosh:
1261 ASSERT(node->getUseEmulatedFunction());
1262 writeEmulatedFunctionTriplet(out, visit, "acosh(");
1263 break;
1264 case EOpAtanh:
1265 ASSERT(node->getUseEmulatedFunction());
1266 writeEmulatedFunctionTriplet(out, visit, "atanh(");
1267 break;
1268 case EOpExp:
1269 outputTriplet(out, visit, "exp(", "", ")");
1270 break;
1271 case EOpLog:
1272 outputTriplet(out, visit, "log(", "", ")");
1273 break;
1274 case EOpExp2:
1275 outputTriplet(out, visit, "exp2(", "", ")");
1276 break;
1277 case EOpLog2:
1278 outputTriplet(out, visit, "log2(", "", ")");
1279 break;
1280 case EOpSqrt:
1281 outputTriplet(out, visit, "sqrt(", "", ")");
1282 break;
1283 case EOpInverseSqrt:
1284 outputTriplet(out, visit, "rsqrt(", "", ")");
1285 break;
1286 case EOpAbs:
1287 outputTriplet(out, visit, "abs(", "", ")");
1288 break;
1289 case EOpSign:
1290 outputTriplet(out, visit, "sign(", "", ")");
1291 break;
1292 case EOpFloor:
1293 outputTriplet(out, visit, "floor(", "", ")");
1294 break;
1295 case EOpTrunc:
1296 outputTriplet(out, visit, "trunc(", "", ")");
1297 break;
1298 case EOpRound:
1299 outputTriplet(out, visit, "round(", "", ")");
1300 break;
1301 case EOpRoundEven:
1302 ASSERT(node->getUseEmulatedFunction());
1303 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
1304 break;
1305 case EOpCeil:
1306 outputTriplet(out, visit, "ceil(", "", ")");
1307 break;
1308 case EOpFract:
1309 outputTriplet(out, visit, "frac(", "", ")");
1310 break;
1311 case EOpIsNan:
1312 if (node->getUseEmulatedFunction())
1313 writeEmulatedFunctionTriplet(out, visit, "isnan(");
1314 else
1315 outputTriplet(out, visit, "isnan(", "", ")");
1316 mRequiresIEEEStrictCompiling = true;
1317 break;
1318 case EOpIsInf:
1319 outputTriplet(out, visit, "isinf(", "", ")");
1320 break;
1321 case EOpFloatBitsToInt:
1322 outputTriplet(out, visit, "asint(", "", ")");
1323 break;
1324 case EOpFloatBitsToUint:
1325 outputTriplet(out, visit, "asuint(", "", ")");
1326 break;
1327 case EOpIntBitsToFloat:
1328 outputTriplet(out, visit, "asfloat(", "", ")");
1329 break;
1330 case EOpUintBitsToFloat:
1331 outputTriplet(out, visit, "asfloat(", "", ")");
1332 break;
1333 case EOpPackSnorm2x16:
1334 ASSERT(node->getUseEmulatedFunction());
1335 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
1336 break;
1337 case EOpPackUnorm2x16:
1338 ASSERT(node->getUseEmulatedFunction());
1339 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
1340 break;
1341 case EOpPackHalf2x16:
1342 ASSERT(node->getUseEmulatedFunction());
1343 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
1344 break;
1345 case EOpUnpackSnorm2x16:
1346 ASSERT(node->getUseEmulatedFunction());
1347 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
1348 break;
1349 case EOpUnpackUnorm2x16:
1350 ASSERT(node->getUseEmulatedFunction());
1351 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
1352 break;
1353 case EOpUnpackHalf2x16:
1354 ASSERT(node->getUseEmulatedFunction());
1355 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
1356 break;
1357 case EOpLength:
1358 outputTriplet(out, visit, "length(", "", ")");
1359 break;
1360 case EOpNormalize:
1361 outputTriplet(out, visit, "normalize(", "", ")");
1362 break;
1363 case EOpDFdx:
1364 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1365 {
1366 outputTriplet(out, visit, "(", "", ", 0.0)");
1367 }
1368 else
1369 {
1370 outputTriplet(out, visit, "ddx(", "", ")");
1371 }
1372 break;
1373 case EOpDFdy:
1374 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1375 {
1376 outputTriplet(out, visit, "(", "", ", 0.0)");
1377 }
1378 else
1379 {
1380 outputTriplet(out, visit, "ddy(", "", ")");
1381 }
1382 break;
1383 case EOpFwidth:
1384 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1385 {
1386 outputTriplet(out, visit, "(", "", ", 0.0)");
1387 }
1388 else
1389 {
1390 outputTriplet(out, visit, "fwidth(", "", ")");
1391 }
1392 break;
1393 case EOpTranspose:
1394 outputTriplet(out, visit, "transpose(", "", ")");
1395 break;
1396 case EOpDeterminant:
1397 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1398 break;
1399 case EOpInverse:
1400 ASSERT(node->getUseEmulatedFunction());
1401 writeEmulatedFunctionTriplet(out, visit, "inverse(");
1402 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001403
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001404 case EOpAny:
1405 outputTriplet(out, visit, "any(", "", ")");
1406 break;
1407 case EOpAll:
1408 outputTriplet(out, visit, "all(", "", ")");
1409 break;
1410 default:
1411 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001412 }
1413
1414 return true;
1415}
1416
Olli Etuaho96963162016-03-21 11:54:33 +02001417TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1418{
1419 if (node->getAsSymbolNode())
1420 {
1421 return node->getAsSymbolNode()->getSymbol();
1422 }
1423 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1424 switch (nodeBinary->getOp())
1425 {
1426 case EOpIndexDirect:
1427 {
1428 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1429
1430 TInfoSinkBase prefixSink;
1431 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1432 return TString(prefixSink.c_str());
1433 }
1434 case EOpIndexDirectStruct:
1435 {
1436 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1437 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1438 const TField *field = s->fields()[index];
1439
1440 TInfoSinkBase prefixSink;
1441 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1442 << field->name();
1443 return TString(prefixSink.c_str());
1444 }
1445 default:
1446 UNREACHABLE();
1447 return TString("");
1448 }
1449}
1450
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001451bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1452{
1453 TInfoSinkBase &out = getInfoSink();
1454
1455 if (mInsideFunction)
1456 {
1457 outputLineDirective(out, node->getLine().first_line);
1458 out << "{\n";
1459 }
1460
1461 for (TIntermSequence::iterator sit = node->getSequence()->begin();
1462 sit != node->getSequence()->end(); sit++)
1463 {
1464 outputLineDirective(out, (*sit)->getLine().first_line);
1465
1466 (*sit)->traverse(this);
1467
1468 // Don't output ; after case labels, they're terminated by :
1469 // This is needed especially since outputting a ; after a case statement would turn empty
1470 // case statements into non-empty case statements, disallowing fall-through from them.
1471 // Also no need to output ; after if statements or sequences. This is done just for
1472 // code clarity.
1473 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
1474 (*sit)->getAsBlock() == nullptr)
1475 out << ";\n";
1476 }
1477
1478 if (mInsideFunction)
1479 {
1480 outputLineDirective(out, node->getLine().last_line);
1481 out << "}\n";
1482 }
1483
1484 return false;
1485}
1486
Olli Etuaho336b1472016-10-05 16:37:55 +01001487bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1488{
1489 TInfoSinkBase &out = getInfoSink();
1490
1491 ASSERT(mCurrentFunctionMetadata == nullptr);
1492
1493 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1494 ASSERT(index != CallDAG::InvalidIndex);
1495 mCurrentFunctionMetadata = &mASTMetadataList[index];
1496
1497 out << TypeString(node->getType()) << " ";
1498
1499 TIntermSequence *parameters = node->getFunctionParameters()->getSequence();
1500
1501 if (node->getFunctionSymbolInfo()->isMain())
1502 {
1503 out << "gl_main(";
1504 }
1505 else
1506 {
1507 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
1508 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1509 }
1510
1511 for (unsigned int i = 0; i < parameters->size(); i++)
1512 {
1513 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1514
1515 if (symbol)
1516 {
1517 ensureStructDefined(symbol->getType());
1518
1519 out << argumentString(symbol);
1520
1521 if (i < parameters->size() - 1)
1522 {
1523 out << ", ";
1524 }
1525 }
1526 else
1527 UNREACHABLE();
1528 }
1529
1530 out << ")\n";
1531
1532 mInsideFunction = true;
1533 // The function body node will output braces.
1534 node->getBody()->traverse(this);
1535 mInsideFunction = false;
1536
1537 mCurrentFunctionMetadata = nullptr;
1538
1539 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1540 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1541 {
1542 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1543 mOutputLod0Function = true;
1544 node->traverse(this);
1545 mOutputLod0Function = false;
1546 }
1547
1548 return false;
1549}
1550
Olli Etuaho13389b62016-10-16 11:48:18 +01001551bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1552{
1553 TInfoSinkBase &out = getInfoSink();
1554 if (visit == PreVisit)
1555 {
1556 TIntermSequence *sequence = node->getSequence();
1557 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1558 ASSERT(sequence->size() == 1);
1559
1560 if (variable &&
1561 (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
1562 variable->getQualifier() == EvqConst))
1563 {
1564 ensureStructDefined(variable->getType());
1565
1566 if (!variable->getAsSymbolNode() ||
1567 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1568 {
1569 if (!mInsideFunction)
1570 {
1571 out << "static ";
1572 }
1573
1574 out << TypeString(variable->getType()) + " ";
1575
1576 TIntermSymbol *symbol = variable->getAsSymbolNode();
1577
1578 if (symbol)
1579 {
1580 symbol->traverse(this);
1581 out << ArrayString(symbol->getType());
1582 out << " = " + initializer(symbol->getType());
1583 }
1584 else
1585 {
1586 variable->traverse(this);
1587 }
1588 }
1589 else if (variable->getAsSymbolNode() &&
1590 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1591 {
1592 // Already added to constructor map
1593 }
1594 else
1595 UNREACHABLE();
1596 }
1597 else if (variable && IsVaryingOut(variable->getQualifier()))
1598 {
1599 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
1600 {
1601 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1602
1603 if (symbol)
1604 {
1605 // Vertex (output) varyings which are declared but not written to should
1606 // still be declared to allow successful linking
1607 mReferencedVaryings[symbol->getSymbol()] = symbol;
1608 }
1609 else
1610 {
1611 (*sit)->traverse(this);
1612 }
1613 }
1614 }
1615 }
1616 return false;
1617}
1618
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1620{
Jamie Madill32aab012015-01-27 14:12:26 -05001621 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001622
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001623 switch (node->getOp())
1624 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001625 case EOpInvariantDeclaration:
1626 // Do not do any translation
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001627 return false;
Olli Etuaho5878f832016-10-07 10:14:58 +01001628 case EOpPrototype:
1629 if (visit == PreVisit)
1630 {
Olli Etuahobd674552016-10-06 13:28:42 +01001631 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Olli Etuaho5878f832016-10-07 10:14:58 +01001632 // Skip the prototype if it is not implemented (and thus not used)
1633 if (index == CallDAG::InvalidIndex)
1634 {
1635 return false;
1636 }
1637
1638 TIntermSequence *arguments = node->getSequence();
1639
Olli Etuahobd674552016-10-06 13:28:42 +01001640 TString name =
1641 DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho5878f832016-10-07 10:14:58 +01001642 out << TypeString(node->getType()) << " " << name
1643 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
1644
1645 for (unsigned int i = 0; i < arguments->size(); i++)
1646 {
1647 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1648
1649 if (symbol)
1650 {
1651 out << argumentString(symbol);
1652
1653 if (i < arguments->size() - 1)
1654 {
1655 out << ", ";
1656 }
1657 }
1658 else
1659 UNREACHABLE();
1660 }
1661
1662 out << ");\n";
1663
1664 // Also prototype the Lod0 variant if needed
1665 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1666 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1667 {
1668 mOutputLod0Function = true;
1669 node->traverse(this);
1670 mOutputLod0Function = false;
1671 }
1672
1673 return false;
1674 }
1675 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001676 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001677 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001678 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001679
Corentin Wallez1239ee92015-03-19 14:38:02 -07001680 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001681 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001682 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001683 if (node->isArray())
1684 {
1685 UNIMPLEMENTED();
1686 }
Olli Etuahobd674552016-10-06 13:28:42 +01001687 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001688 ASSERT(index != CallDAG::InvalidIndex);
1689 lod0 &= mASTMetadataList[index].mNeedsLod0;
1690
Olli Etuahobd674552016-10-06 13:28:42 +01001691 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001692 out << DisambiguateFunctionName(node->getSequence());
1693 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694 }
Olli Etuahobd674552016-10-06 13:28:42 +01001695 else if (node->getFunctionSymbolInfo()->getNameObj().isInternal())
Olli Etuahob741c762016-06-29 15:49:22 +03001696 {
1697 // This path is used for internal functions that don't have their definitions in the
1698 // AST, such as precision emulation functions.
Olli Etuahobd674552016-10-06 13:28:42 +01001699 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001700 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001701 else
1702 {
Olli Etuahobd674552016-10-06 13:28:42 +01001703 TString name = TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001704 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001705 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001706 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1707 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1708 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001709 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001710
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001711 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001712 {
Olli Etuaho96963162016-03-21 11:54:33 +02001713 TIntermTyped *typedArg = (*arg)->getAsTyped();
1714 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001715 {
1716 out << "texture_";
1717 (*arg)->traverse(this);
1718 out << ", sampler_";
1719 }
1720
1721 (*arg)->traverse(this);
1722
Olli Etuaho96963162016-03-21 11:54:33 +02001723 if (typedArg->getType().isStructureContainingSamplers())
1724 {
1725 const TType &argType = typedArg->getType();
1726 TVector<TIntermSymbol *> samplerSymbols;
1727 TString structName = samplerNamePrefixFromStruct(typedArg);
1728 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001729 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001730 &samplerSymbols, nullptr);
1731 for (const TIntermSymbol *sampler : samplerSymbols)
1732 {
1733 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1734 {
1735 out << ", texture_" << sampler->getSymbol();
1736 out << ", sampler_" << sampler->getSymbol();
1737 }
1738 else
1739 {
1740 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1741 // of D3D9, it's the sampler variable.
1742 out << ", " + sampler->getSymbol();
1743 }
1744 }
1745 }
1746
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001747 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001748 {
1749 out << ", ";
1750 }
1751 }
1752
1753 out << ")";
1754
1755 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001757 case EOpParameters:
1758 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1759 break;
1760 case EOpConstructFloat:
1761 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1762 break;
1763 case EOpConstructVec2:
1764 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1765 break;
1766 case EOpConstructVec3:
1767 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1768 break;
1769 case EOpConstructVec4:
1770 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1771 break;
1772 case EOpConstructBool:
1773 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1774 break;
1775 case EOpConstructBVec2:
1776 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1777 break;
1778 case EOpConstructBVec3:
1779 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1780 break;
1781 case EOpConstructBVec4:
1782 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1783 break;
1784 case EOpConstructInt:
1785 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1786 break;
1787 case EOpConstructIVec2:
1788 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1789 break;
1790 case EOpConstructIVec3:
1791 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1792 break;
1793 case EOpConstructIVec4:
1794 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1795 break;
1796 case EOpConstructUInt:
1797 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1798 break;
1799 case EOpConstructUVec2:
1800 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1801 break;
1802 case EOpConstructUVec3:
1803 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1804 break;
1805 case EOpConstructUVec4:
1806 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1807 break;
1808 case EOpConstructMat2:
1809 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1810 break;
1811 case EOpConstructMat2x3:
1812 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1813 break;
1814 case EOpConstructMat2x4:
1815 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1816 break;
1817 case EOpConstructMat3x2:
1818 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1819 break;
1820 case EOpConstructMat3:
1821 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1822 break;
1823 case EOpConstructMat3x4:
1824 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1825 break;
1826 case EOpConstructMat4x2:
1827 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1828 break;
1829 case EOpConstructMat4x3:
1830 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1831 break;
1832 case EOpConstructMat4:
1833 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1834 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001835 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001836 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001837 if (node->getType().isArray())
1838 {
1839 UNIMPLEMENTED();
1840 }
Jamie Madill033dae62014-06-18 12:56:28 -04001841 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001842 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001843 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001844 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001845 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001846 case EOpLessThan:
1847 outputTriplet(out, visit, "(", " < ", ")");
1848 break;
1849 case EOpGreaterThan:
1850 outputTriplet(out, visit, "(", " > ", ")");
1851 break;
1852 case EOpLessThanEqual:
1853 outputTriplet(out, visit, "(", " <= ", ")");
1854 break;
1855 case EOpGreaterThanEqual:
1856 outputTriplet(out, visit, "(", " >= ", ")");
1857 break;
1858 case EOpVectorEqual:
1859 outputTriplet(out, visit, "(", " == ", ")");
1860 break;
1861 case EOpVectorNotEqual:
1862 outputTriplet(out, visit, "(", " != ", ")");
1863 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001864 case EOpMod:
1865 ASSERT(node->getUseEmulatedFunction());
1866 writeEmulatedFunctionTriplet(out, visit, "mod(");
1867 break;
1868 case EOpModf:
1869 outputTriplet(out, visit, "modf(", ", ", ")");
1870 break;
1871 case EOpPow:
1872 outputTriplet(out, visit, "pow(", ", ", ")");
1873 break;
1874 case EOpAtan:
1875 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1876 ASSERT(node->getUseEmulatedFunction());
1877 writeEmulatedFunctionTriplet(out, visit, "atan(");
1878 break;
1879 case EOpMin:
1880 outputTriplet(out, visit, "min(", ", ", ")");
1881 break;
1882 case EOpMax:
1883 outputTriplet(out, visit, "max(", ", ", ")");
1884 break;
1885 case EOpClamp:
1886 outputTriplet(out, visit, "clamp(", ", ", ")");
1887 break;
1888 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301889 {
1890 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1891 if (lastParamNode->getType().getBasicType() == EbtBool)
1892 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001893 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
1894 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05301895 // so use emulated version.
1896 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001897 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301898 }
1899 else
1900 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001901 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301902 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001903 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301904 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001905 case EOpStep:
1906 outputTriplet(out, visit, "step(", ", ", ")");
1907 break;
1908 case EOpSmoothStep:
1909 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1910 break;
1911 case EOpDistance:
1912 outputTriplet(out, visit, "distance(", ", ", ")");
1913 break;
1914 case EOpDot:
1915 outputTriplet(out, visit, "dot(", ", ", ")");
1916 break;
1917 case EOpCross:
1918 outputTriplet(out, visit, "cross(", ", ", ")");
1919 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001920 case EOpFaceForward:
1921 ASSERT(node->getUseEmulatedFunction());
1922 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
1923 break;
1924 case EOpReflect:
1925 outputTriplet(out, visit, "reflect(", ", ", ")");
1926 break;
1927 case EOpRefract:
1928 outputTriplet(out, visit, "refract(", ", ", ")");
1929 break;
1930 case EOpOuterProduct:
1931 ASSERT(node->getUseEmulatedFunction());
1932 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
1933 break;
1934 case EOpMul:
1935 outputTriplet(out, visit, "(", " * ", ")");
1936 break;
1937 default:
1938 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001939 }
1940
1941 return true;
1942}
1943
Olli Etuaho57961272016-09-14 13:57:46 +03001944void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001946 out << "if (";
1947
1948 node->getCondition()->traverse(this);
1949
1950 out << ")\n";
1951
Jamie Madill8c46ab12015-12-07 16:39:19 -05001952 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001953
1954 bool discard = false;
1955
1956 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001958 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001959 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001960
Olli Etuahoa6f22092015-05-08 18:31:10 +03001961 // Detect true discard
1962 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1963 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001964 else
1965 {
1966 // TODO(oetuaho): Check if the semicolon inside is necessary.
1967 // It's there as a result of conservative refactoring of the output.
1968 out << "{;}\n";
1969 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001970
Jamie Madill8c46ab12015-12-07 16:39:19 -05001971 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001972
Olli Etuahoa6f22092015-05-08 18:31:10 +03001973 if (node->getFalseBlock())
1974 {
1975 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001976
Jamie Madill8c46ab12015-12-07 16:39:19 -05001977 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978
Olli Etuaho32db19b2016-10-04 14:43:16 +01001979 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001980 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001981
Jamie Madill8c46ab12015-12-07 16:39:19 -05001982 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001983
Olli Etuahoa6f22092015-05-08 18:31:10 +03001984 // Detect false discard
1985 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1986 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001987
Olli Etuahoa6f22092015-05-08 18:31:10 +03001988 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001989 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001990 {
1991 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001992 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001993}
1994
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001995bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1996{
1997 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1998 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1999 UNREACHABLE();
2000 return false;
2001}
2002
Olli Etuaho57961272016-09-14 13:57:46 +03002003bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002004{
2005 TInfoSinkBase &out = getInfoSink();
2006
Olli Etuaho3d932d82016-04-12 11:10:30 +03002007 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002008
2009 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002010 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002011 {
2012 out << "FLATTEN ";
2013 }
2014
Olli Etuaho57961272016-09-14 13:57:46 +03002015 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016
2017 return false;
2018}
2019
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002020bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002021{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002022 TInfoSinkBase &out = getInfoSink();
2023
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002024 if (node->getStatementList())
2025 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002026 node->setStatementList(
2027 RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002028 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002029 // The curly braces get written when visiting the statementList aggregate
2030 }
2031 else
2032 {
2033 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002034 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002035 }
2036 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002037}
2038
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002039bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002040{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002041 TInfoSinkBase &out = getInfoSink();
2042
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002043 if (node->hasCondition())
2044 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002045 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002046 return true;
2047 }
2048 else
2049 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002050 out << "default:\n";
2051 return false;
2052 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002053}
2054
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002055void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2056{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002057 TInfoSinkBase &out = getInfoSink();
2058 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059}
2060
2061bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2062{
Nicolas Capens655fe362014-04-11 13:12:34 -04002063 mNestedLoopDepth++;
2064
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002065 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002066 mInsideDiscontinuousLoop =
2067 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002068
Jamie Madill8c46ab12015-12-07 16:39:19 -05002069 TInfoSinkBase &out = getInfoSink();
2070
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002071 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002072 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002073 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002074 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002075 mInsideDiscontinuousLoop = wasDiscontinuous;
2076 mNestedLoopDepth--;
2077
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002078 return false;
2079 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002080 }
2081
Corentin Wallez1239ee92015-03-19 14:38:02 -07002082 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002083 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002085 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002086
Jamie Madill8c46ab12015-12-07 16:39:19 -05002087 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 }
2089 else
2090 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002091 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002092
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 if (node->getInit())
2094 {
2095 node->getInit()->traverse(this);
2096 }
2097
2098 out << "; ";
2099
alokp@chromium.org52813552010-11-16 18:36:09 +00002100 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002102 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 }
2104
2105 out << "; ";
2106
alokp@chromium.org52813552010-11-16 18:36:09 +00002107 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002109 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 }
2111
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002112 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002113
Jamie Madill8c46ab12015-12-07 16:39:19 -05002114 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 }
2116
2117 if (node->getBody())
2118 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002119 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002120 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002122 else
2123 {
2124 // TODO(oetuaho): Check if the semicolon inside is necessary.
2125 // It's there as a result of conservative refactoring of the output.
2126 out << "{;}\n";
2127 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128
Jamie Madill8c46ab12015-12-07 16:39:19 -05002129 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130
alokp@chromium.org52813552010-11-16 18:36:09 +00002131 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002133 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134 out << "while(\n";
2135
alokp@chromium.org52813552010-11-16 18:36:09 +00002136 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137
daniel@transgaming.com73536982012-03-21 20:45:49 +00002138 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139 }
2140
daniel@transgaming.com73536982012-03-21 20:45:49 +00002141 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002143 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002144 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002145
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146 return false;
2147}
2148
2149bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2150{
Jamie Madill32aab012015-01-27 14:12:26 -05002151 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152
2153 switch (node->getFlowOp())
2154 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002155 case EOpKill:
2156 outputTriplet(out, visit, "discard;\n", "", "");
2157 break;
2158 case EOpBreak:
2159 if (visit == PreVisit)
Nicolas Capens655fe362014-04-11 13:12:34 -04002160 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002161 if (mNestedLoopDepth > 1)
2162 {
2163 mUsesNestedBreak = true;
2164 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002165
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002166 if (mExcessiveLoopIndex)
2167 {
2168 out << "{Break";
2169 mExcessiveLoopIndex->traverse(this);
2170 out << " = true; break;}\n";
2171 }
2172 else
2173 {
2174 out << "break;\n";
2175 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002176 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002177 break;
2178 case EOpContinue:
2179 outputTriplet(out, visit, "continue;\n", "", "");
2180 break;
2181 case EOpReturn:
2182 if (visit == PreVisit)
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002183 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002184 if (node->getExpression())
2185 {
2186 out << "return ";
2187 }
2188 else
2189 {
2190 out << "return;\n";
2191 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002192 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002193 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002194 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002195 if (node->getExpression())
2196 {
2197 out << ";\n";
2198 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002200 break;
2201 default:
2202 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203 }
2204
2205 return true;
2206}
2207
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002208// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002209// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2210// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002211bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002212{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002213 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002214
2215 // Parse loops of the form:
2216 // for(int index = initial; index [comparator] limit; index += increment)
2217 TIntermSymbol *index = NULL;
2218 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002219 int initial = 0;
2220 int limit = 0;
2221 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002222
2223 // Parse index name and intial value
2224 if (node->getInit())
2225 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002226 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002227
2228 if (init)
2229 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002230 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002231 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002232
2233 if (variable && variable->getQualifier() == EvqTemporary)
2234 {
2235 TIntermBinary *assign = variable->getAsBinaryNode();
2236
2237 if (assign->getOp() == EOpInitialize)
2238 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002239 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002240 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2241
2242 if (symbol && constant)
2243 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002244 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002245 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002246 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002247 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002248 }
2249 }
2250 }
2251 }
2252 }
2253 }
2254
2255 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002256 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002257 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002258 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002259
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002260 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2261 {
2262 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2263
2264 if (constant)
2265 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002266 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002267 {
2268 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002269 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002270 }
2271 }
2272 }
2273 }
2274
2275 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002276 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002277 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002278 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002279 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002280
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002281 if (binaryTerminal)
2282 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002283 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002284 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2285
2286 if (constant)
2287 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002288 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002289 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002290 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002291
2292 switch (op)
2293 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002294 case EOpAddAssign:
2295 increment = value;
2296 break;
2297 case EOpSubAssign:
2298 increment = -value;
2299 break;
2300 default:
2301 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002302 }
2303 }
2304 }
2305 }
2306 else if (unaryTerminal)
2307 {
2308 TOperator op = unaryTerminal->getOp();
2309
2310 switch (op)
2311 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002312 case EOpPostIncrement:
2313 increment = 1;
2314 break;
2315 case EOpPostDecrement:
2316 increment = -1;
2317 break;
2318 case EOpPreIncrement:
2319 increment = 1;
2320 break;
2321 case EOpPreDecrement:
2322 increment = -1;
2323 break;
2324 default:
2325 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002326 }
2327 }
2328 }
2329
2330 if (index != NULL && comparator != EOpNull && increment != 0)
2331 {
2332 if (comparator == EOpLessThanEqual)
2333 {
2334 comparator = EOpLessThan;
2335 limit += 1;
2336 }
2337
2338 if (comparator == EOpLessThan)
2339 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002340 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002341
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002342 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002343 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002344 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002345 }
2346
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002347 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002348 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002349
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002350 out << "{int ";
2351 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002352 out << ";\n"
2353 "bool Break";
2354 index->traverse(this);
2355 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002356
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002357 bool firstLoopFragment = true;
2358
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002359 while (iterations > 0)
2360 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002361 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002362
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002363 if (!firstLoopFragment)
2364 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002365 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002366 index->traverse(this);
2367 out << ") {\n";
2368 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002369
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002370 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002371 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002372 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002373 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002374
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002375 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002376 const char *unroll =
2377 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002378
Corentin Wallez1239ee92015-03-19 14:38:02 -07002379 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002380 index->traverse(this);
2381 out << " = ";
2382 out << initial;
2383
2384 out << "; ";
2385 index->traverse(this);
2386 out << " < ";
2387 out << clampedLimit;
2388
2389 out << "; ";
2390 index->traverse(this);
2391 out << " += ";
2392 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002393 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002394
Jamie Madill8c46ab12015-12-07 16:39:19 -05002395 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002396 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002397
2398 if (node->getBody())
2399 {
2400 node->getBody()->traverse(this);
2401 }
2402
Jamie Madill8c46ab12015-12-07 16:39:19 -05002403 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002404 out << ";}\n";
2405
2406 if (!firstLoopFragment)
2407 {
2408 out << "}\n";
2409 }
2410
2411 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002412
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002413 initial += MAX_LOOP_ITERATIONS * increment;
2414 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002415 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002416
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002417 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002418
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002419 mExcessiveLoopIndex = restoreIndex;
2420
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421 return true;
2422 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002423 else
2424 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002425 }
2426
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002427 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002428}
2429
Jamie Madill8c46ab12015-12-07 16:39:19 -05002430void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2431 Visit visit,
2432 const char *preString,
2433 const char *inString,
2434 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002436 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437 {
2438 out << preString;
2439 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002440 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 {
2442 out << inString;
2443 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002444 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 {
2446 out << postString;
2447 }
2448}
2449
Jamie Madill8c46ab12015-12-07 16:39:19 -05002450void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002451{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002452 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002453 {
Jamie Madill32aab012015-01-27 14:12:26 -05002454 out << "\n";
2455 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002456
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002457 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002458 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002459 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002460 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002461
Jamie Madill32aab012015-01-27 14:12:26 -05002462 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002463 }
2464}
2465
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002466TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2467{
2468 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002469 const TType &type = symbol->getType();
2470 const TName &name = symbol->getName();
2471 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002472
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002473 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002474 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002475 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002476 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002477 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002478 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002479 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002480 }
2481
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002482 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002483 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002484 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2485 {
2486 // Samplers are passed as indices to the sampler array.
2487 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2488 return "const uint " + nameStr + ArrayString(type);
2489 }
2490 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2491 {
2492 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2493 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2494 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2495 ArrayString(type);
2496 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002497 }
2498
Olli Etuaho96963162016-03-21 11:54:33 +02002499 TStringStream argString;
2500 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2501 << ArrayString(type);
2502
2503 // If the structure parameter contains samplers, they need to be passed into the function as
2504 // separate parameters. HLSL doesn't natively support samplers in structs.
2505 if (type.isStructureContainingSamplers())
2506 {
2507 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2508 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002509 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002510 for (const TIntermSymbol *sampler : samplerSymbols)
2511 {
2512 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2513 {
2514 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2515 }
2516 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2517 {
2518 const TType &samplerType = sampler->getType();
2519 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2520 type.getArraySize() == samplerType.getArraySize());
2521 ASSERT(IsSampler(samplerType.getBasicType()));
2522 argString << ", " << QualifierString(qualifier) << " "
2523 << TextureString(samplerType.getBasicType()) << " texture_"
2524 << sampler->getSymbol() << ArrayString(type) << ", "
2525 << QualifierString(qualifier) << " "
2526 << SamplerString(samplerType.getBasicType()) << " sampler_"
2527 << sampler->getSymbol() << ArrayString(type);
2528 }
2529 else
2530 {
2531 const TType &samplerType = sampler->getType();
2532 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2533 type.getArraySize() == samplerType.getArraySize());
2534 ASSERT(IsSampler(samplerType.getBasicType()));
2535 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2536 << " " << sampler->getSymbol() << ArrayString(type);
2537 }
2538 }
2539 }
2540
2541 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542}
2543
2544TString OutputHLSL::initializer(const TType &type)
2545{
2546 TString string;
2547
Jamie Madill94bf7f22013-07-08 13:31:15 -04002548 size_t size = type.getObjectSize();
2549 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002551 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552
Jamie Madill94bf7f22013-07-08 13:31:15 -04002553 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554 {
2555 string += ", ";
2556 }
2557 }
2558
daniel@transgaming.comead23042010-04-29 03:35:36 +00002559 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002561
Jamie Madill8c46ab12015-12-07 16:39:19 -05002562void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2563 Visit visit,
2564 const TType &type,
2565 const char *name,
2566 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002567{
Olli Etuahof40319e2015-03-10 14:33:00 +02002568 if (type.isArray())
2569 {
2570 UNIMPLEMENTED();
2571 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002572
2573 if (visit == PreVisit)
2574 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002575 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002576
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002577 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002578 }
2579 else if (visit == InVisit)
2580 {
2581 out << ", ";
2582 }
2583 else if (visit == PostVisit)
2584 {
2585 out << ")";
2586 }
2587}
2588
Jamie Madill8c46ab12015-12-07 16:39:19 -05002589const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2590 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002591 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002592{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002593 const TConstantUnion *constUnionIterated = constUnion;
2594
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002595 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002596 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002597 {
Jamie Madill033dae62014-06-18 12:56:28 -04002598 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002599
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002600 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002601
Jamie Madill98493dd2013-07-08 14:39:03 -04002602 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002603 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002604 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002605 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002606
Jamie Madill98493dd2013-07-08 14:39:03 -04002607 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002608 {
2609 out << ", ";
2610 }
2611 }
2612
2613 out << ")";
2614 }
2615 else
2616 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002617 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002618 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002619
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002620 if (writeType)
2621 {
Jamie Madill033dae62014-06-18 12:56:28 -04002622 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002623 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002624 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002625 if (writeType)
2626 {
2627 out << ")";
2628 }
2629 }
2630
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002631 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002632}
2633
Jamie Madill8c46ab12015-12-07 16:39:19 -05002634void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002635{
2636 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002637 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002638}
2639
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002640bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2641 TIntermSymbol *symbolNode,
2642 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002643{
2644 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2645 expression->traverse(&searchSymbol);
2646
2647 if (searchSymbol.foundMatch())
2648 {
2649 // Type already printed
2650 out << "t" + str(mUniqueIndex) + " = ";
2651 expression->traverse(this);
2652 out << ", ";
2653 symbolNode->traverse(this);
2654 out << " = t" + str(mUniqueIndex);
2655
2656 mUniqueIndex++;
2657 return true;
2658 }
2659
2660 return false;
2661}
2662
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002663bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2664{
2665 // We support writing constant unions and constructors that only take constant unions as
2666 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002667 return expression->getAsConstantUnion() ||
2668 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002669}
2670
2671bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2672 TIntermSymbol *symbolNode,
2673 TIntermTyped *expression)
2674{
2675 if (canWriteAsHLSLLiteral(expression))
2676 {
2677 symbolNode->traverse(this);
2678 if (expression->getType().isArray())
2679 {
2680 out << "[" << expression->getType().getArraySize() << "]";
2681 }
2682 out << " = {";
2683 if (expression->getAsConstantUnion())
2684 {
2685 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2686 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002687 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002688 }
2689 else
2690 {
2691 TIntermAggregate *constructor = expression->getAsAggregate();
2692 ASSERT(constructor != nullptr);
2693 for (TIntermNode *&node : *constructor->getSequence())
2694 {
2695 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2696 ASSERT(nodeConst);
2697 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002698 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002699 if (node != constructor->getSequence()->back())
2700 {
2701 out << ", ";
2702 }
2703 }
2704 }
2705 out << "}";
2706 return true;
2707 }
2708 return false;
2709}
2710
Jamie Madill55e79e02015-02-09 15:35:00 -05002711TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2712{
2713 const TFieldList &fields = structure.fields();
2714
2715 for (const auto &eqFunction : mStructEqualityFunctions)
2716 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002717 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002718 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002719 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002720 }
2721 }
2722
2723 const TString &structNameString = StructNameString(structure);
2724
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002725 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002726 function->structure = &structure;
2727 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002728
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002729 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002730
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002731 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2732 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002733 << "{\n"
2734 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002735
2736 for (size_t i = 0; i < fields.size(); i++)
2737 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002738 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002739 const TType *fieldType = field->type();
2740
2741 const TString &fieldNameA = "a." + Decorate(field->name());
2742 const TString &fieldNameB = "b." + Decorate(field->name());
2743
2744 if (i > 0)
2745 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002746 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002747 }
2748
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002749 fnOut << "(";
2750 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2751 fnOut << fieldNameA;
2752 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2753 fnOut << fieldNameB;
2754 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2755 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002756 }
2757
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002758 fnOut << ";\n"
2759 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002760
2761 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002762
2763 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002764 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002765
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002766 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002767}
2768
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002769TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002770{
2771 for (const auto &eqFunction : mArrayEqualityFunctions)
2772 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002773 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002774 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002775 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002776 }
2777 }
2778
2779 const TString &typeName = TypeString(type);
2780
Olli Etuaho12690762015-03-31 12:55:28 +03002781 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002782 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002783
2784 TInfoSinkBase fnNameOut;
2785 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002786 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002787
2788 TType nonArrayType = type;
2789 nonArrayType.clearArrayness();
2790
2791 TInfoSinkBase fnOut;
2792
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002793 fnOut << "bool " << function->functionName << "(" << typeName << " a[" << type.getArraySize()
2794 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002795 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002796 " for (int i = 0; i < "
2797 << type.getArraySize() << "; ++i)\n"
2798 " {\n"
2799 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002800
2801 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2802 fnOut << "a[i]";
2803 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2804 fnOut << "b[i]";
2805 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2806
2807 fnOut << ") { return false; }\n"
2808 " }\n"
2809 " return true;\n"
2810 "}\n";
2811
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002812 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002813
2814 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002815 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002816
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002817 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002818}
2819
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002820TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002821{
2822 for (const auto &assignFunction : mArrayAssignmentFunctions)
2823 {
2824 if (assignFunction.type == type)
2825 {
2826 return assignFunction.functionName;
2827 }
2828 }
2829
2830 const TString &typeName = TypeString(type);
2831
2832 ArrayHelperFunction function;
2833 function.type = type;
2834
2835 TInfoSinkBase fnNameOut;
2836 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2837 function.functionName = fnNameOut.c_str();
2838
2839 TInfoSinkBase fnOut;
2840
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002841 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2842 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
2843 << "{\n"
2844 " for (int i = 0; i < "
2845 << type.getArraySize() << "; ++i)\n"
2846 " {\n"
2847 " a[i] = b[i];\n"
2848 " }\n"
2849 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03002850
2851 function.functionDefinition = fnOut.c_str();
2852
2853 mArrayAssignmentFunctions.push_back(function);
2854
2855 return function.functionName;
2856}
2857
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002858TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03002859{
2860 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2861 {
2862 if (constructIntoFunction.type == type)
2863 {
2864 return constructIntoFunction.functionName;
2865 }
2866 }
2867
2868 const TString &typeName = TypeString(type);
2869
2870 ArrayHelperFunction function;
2871 function.type = type;
2872
2873 TInfoSinkBase fnNameOut;
2874 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2875 function.functionName = fnNameOut.c_str();
2876
2877 TInfoSinkBase fnOut;
2878
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002879 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2880 << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002881 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002882 {
2883 fnOut << ", " << typeName << " b" << i;
2884 }
2885 fnOut << ")\n"
2886 "{\n";
2887
Olli Etuaho856c4972016-08-08 11:38:39 +03002888 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002889 {
2890 fnOut << " a[" << i << "] = b" << i << ";\n";
2891 }
2892 fnOut << "}\n";
2893
2894 function.functionDefinition = fnOut.c_str();
2895
2896 mArrayConstructIntoFunctions.push_back(function);
2897
2898 return function.functionName;
2899}
2900
Jamie Madill2e295e22015-04-29 10:41:33 -04002901void OutputHLSL::ensureStructDefined(const TType &type)
2902{
2903 TStructure *structure = type.getStruct();
2904
2905 if (structure)
2906 {
2907 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2908 }
2909}
2910
Jamie Madill45bcc782016-11-07 13:58:48 -05002911} // namespace sh