blob: f07144e63bfe0261302af200a058ab47e5c92501 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
Olli Etuaho5858f7e2016-04-08 13:08:46 +030024#include "compiler/translator/TextureFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050026#include "compiler/translator/UniformHLSL.h"
27#include "compiler/translator/UtilsHLSL.h"
28#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050029#include "compiler/translator/util.h"
30
Jamie Madill45bcc782016-11-07 13:58:48 -050031namespace sh
32{
33
Olli Etuaho56a2f952016-12-08 12:16:27 +000034void OutputHLSL::writeFloat(TInfoSinkBase &out, float f)
Olli Etuaho4785fec2015-05-18 16:09:37 +030035{
Olli Etuaho56a2f952016-12-08 12:16:27 +000036 // This is known not to work for NaN on all drivers but make the best effort to output NaNs
37 // regardless.
38 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300 &&
39 mOutputType == SH_HLSL_4_1_OUTPUT)
40 {
41 out << "asfloat(" << gl::bitCast<uint32_t>(f) << "u)";
42 }
43 else
44 {
45 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
46 }
47}
Olli Etuaho4785fec2015-05-18 16:09:37 +030048
Olli Etuaho56a2f952016-12-08 12:16:27 +000049void OutputHLSL::writeSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
Olli Etuaho18b9deb2015-11-05 12:14:50 +020050{
51 ASSERT(constUnion != nullptr);
52 switch (constUnion->getType())
53 {
54 case EbtFloat:
Olli Etuaho56a2f952016-12-08 12:16:27 +000055 writeFloat(out, constUnion->getFConst());
Olli Etuaho18b9deb2015-11-05 12:14:50 +020056 break;
57 case EbtInt:
58 out << constUnion->getIConst();
59 break;
60 case EbtUInt:
61 out << constUnion->getUConst();
62 break;
63 case EbtBool:
64 out << constUnion->getBConst();
65 break;
66 default:
67 UNREACHABLE();
68 }
69}
70
Olli Etuaho56a2f952016-12-08 12:16:27 +000071const TConstantUnion *OutputHLSL::writeConstantUnionArray(TInfoSinkBase &out,
72 const TConstantUnion *const constUnion,
73 const size_t size)
Olli Etuaho18b9deb2015-11-05 12:14:50 +020074{
75 const TConstantUnion *constUnionIterated = constUnion;
76 for (size_t i = 0; i < size; i++, constUnionIterated++)
77 {
Olli Etuaho56a2f952016-12-08 12:16:27 +000078 writeSingleConstant(out, constUnionIterated);
Olli Etuaho18b9deb2015-11-05 12:14:50 +020079
80 if (i != size - 1)
81 {
82 out << ", ";
83 }
84 }
85 return constUnionIterated;
86}
87
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080088OutputHLSL::OutputHLSL(sh::GLenum shaderType,
89 int shaderVersion,
90 const TExtensionBehavior &extensionBehavior,
91 const char *sourcePath,
92 ShShaderOutput outputType,
93 int numRenderTargets,
94 const std::vector<Uniform> &uniforms,
95 ShCompileOptions compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -040096 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020097 mShaderType(shaderType),
98 mShaderVersion(shaderVersion),
99 mExtensionBehavior(extensionBehavior),
100 mSourcePath(sourcePath),
101 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700102 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000103 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700104 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000106 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000107
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500108 mUsesFragColor = false;
109 mUsesFragData = false;
110 mUsesDepthRange = false;
111 mUsesFragCoord = false;
112 mUsesPointCoord = false;
113 mUsesFrontFacing = false;
114 mUsesPointSize = false;
115 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500116 mUsesVertexID = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500117 mUsesFragDepth = false;
118 mUsesXor = false;
119 mUsesDiscardRewriting = false;
120 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530121 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000122
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000123 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000124
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500125 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000126 mInsideDiscontinuousLoop = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500127 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000128
129 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000130
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500131 mStructureHLSL = new StructureHLSL;
132 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300133 mTextureFunctionHLSL = new TextureFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400134
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200135 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000136 {
Arun Patole63419392015-03-13 11:51:07 +0530137 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500138 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
139 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530140 // In both cases total 3 uniform registers need to be reserved.
141 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000142 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000143
Geoff Lang00140f42016-02-03 18:47:33 +0000144 // Reserve registers for the default uniform block and driver constants
145 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146}
147
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000148OutputHLSL::~OutputHLSL()
149{
Jamie Madill8daaba12014-06-13 10:04:33 -0400150 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400151 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300152 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200153 for (auto &eqFunction : mStructEqualityFunctions)
154 {
155 SafeDelete(eqFunction);
156 }
157 for (auto &eqFunction : mArrayEqualityFunctions)
158 {
159 SafeDelete(eqFunction);
160 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000161}
162
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200163void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000164{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500165 const std::vector<TIntermTyped *> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400166 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000167
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200168 BuiltInFunctionEmulator builtInFunctionEmulator;
169 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800170 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
171 {
172 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
173 mShaderVersion);
174 }
175
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200176 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500177
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700178 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000179 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700180 ASSERT(success == CallDAG::INITDAG_SUCCESS);
181 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700182
Jamie Madill37997142015-01-28 10:06:34 -0500183 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500184 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200185 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500186 mInfoSinkStack.pop();
187
Jamie Madill37997142015-01-28 10:06:34 -0500188 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500189 mInfoSinkStack.pop();
190
Jamie Madill32aab012015-01-27 14:12:26 -0500191 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500192 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500193 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000194
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200195 objSink << mHeader.c_str();
196 objSink << mBody.c_str();
197 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200198
199 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000200}
201
Jamie Madill570e04d2013-06-21 09:15:33 -0400202void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
203{
204 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
205 {
206 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
207
Jamie Madill32aab012015-01-27 14:12:26 -0500208 TInfoSinkBase structInfoSink;
209 mInfoSinkStack.push(&structInfoSink);
210
Jamie Madill570e04d2013-06-21 09:15:33 -0400211 // This will mark the necessary block elements as referenced
212 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500213
214 TString structName(structInfoSink.c_str());
215 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400216
217 mFlaggedStructOriginalNames[flaggedNode] = structName;
218
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500219 for (size_t pos = structName.find('.'); pos != std::string::npos;
220 pos = structName.find('.'))
Jamie Madill570e04d2013-06-21 09:15:33 -0400221 {
222 structName.erase(pos, 1);
223 }
224
225 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
226 }
227}
228
Jamie Madill4e1fd412014-07-10 17:50:10 -0400229const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
230{
231 return mUniformHLSL->getInterfaceBlockRegisterMap();
232}
233
Jamie Madill9fe25e92014-07-18 10:33:08 -0400234const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
235{
236 return mUniformHLSL->getUniformRegisterMap();
237}
238
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000239int OutputHLSL::vectorSize(const TType &type) const
240{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500241 int elementSize = type.isMatrix() ? type.getCols() : 1;
Olli Etuaho856c4972016-08-08 11:38:39 +0300242 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000243
244 return elementSize * arraySize;
245}
246
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500247TString OutputHLSL::structInitializerString(int indent,
248 const TStructure &structure,
249 const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400250{
251 TString init;
252
253 TString preIndentString;
254 TString fullIndentString;
255
256 for (int spaces = 0; spaces < (indent * 4); spaces++)
257 {
258 preIndentString += ' ';
259 }
260
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500261 for (int spaces = 0; spaces < ((indent + 1) * 4); spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400262 {
263 fullIndentString += ' ';
264 }
265
266 init += preIndentString + "{\n";
267
Jamie Madill98493dd2013-07-08 14:39:03 -0400268 const TFieldList &fields = structure.fields();
269 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400270 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500271 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400272 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500273 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400274
Jamie Madill98493dd2013-07-08 14:39:03 -0400275 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400276 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400277 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400278 }
279 else
280 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400281 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400282 }
283 }
284
285 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
286
287 return init;
288}
289
Jamie Madill8c46ab12015-12-07 16:39:19 -0500290void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000292 TString varyings;
293 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400294 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000295
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500296 for (std::map<TIntermTyped *, TString>::const_iterator flaggedStructIt =
297 mFlaggedStructMappedNames.begin();
298 flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400299 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500300 TIntermTyped *structNode = flaggedStructIt->first;
301 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400302 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400303 const TString &originalName = mFlaggedStructOriginalNames[structNode];
304
Jamie Madill033dae62014-06-18 12:56:28 -0400305 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400306 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 flaggedStructs += "\n";
308 }
309
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500310 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin();
311 varying != mReferencedVaryings.end(); varying++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000312 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500313 const TType &type = varying->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000314 const TString &name = varying->second->getSymbol();
315
316 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500317 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
318 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000319 }
320
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500321 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin();
322 attribute != mReferencedAttributes.end(); attribute++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000323 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500324 const TType &type = attribute->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000325 const TString &name = attribute->second->getSymbol();
326
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500327 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
328 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000329 }
330
Jamie Madill8daaba12014-06-13 10:04:33 -0400331 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400332
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200333 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400334 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
335
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200336 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500337 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200338 out << "\n// Equality functions\n\n";
339 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500340 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200341 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200342 }
343 }
Olli Etuaho12690762015-03-31 12:55:28 +0300344 if (!mArrayAssignmentFunctions.empty())
345 {
346 out << "\n// Assignment functions\n\n";
347 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
348 {
349 out << assignmentFunction.functionDefinition << "\n";
350 }
351 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300352 if (!mArrayConstructIntoFunctions.empty())
353 {
354 out << "\n// Array constructor functions\n\n";
355 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
356 {
357 out << constructIntoFunction.functionDefinition << "\n";
358 }
359 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200360
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500361 if (mUsesDiscardRewriting)
362 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400363 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500364 }
365
Nicolas Capens655fe362014-04-11 13:12:34 -0400366 if (mUsesNestedBreak)
367 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400368 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400369 }
370
Arun Patole44efa0b2015-03-04 17:11:05 +0530371 if (mRequiresIEEEStrictCompiling)
372 {
373 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
374 }
375
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400376 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
377 "#define LOOP [loop]\n"
378 "#define FLATTEN [flatten]\n"
379 "#else\n"
380 "#define LOOP\n"
381 "#define FLATTEN\n"
382 "#endif\n";
383
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200384 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200386 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500387 const bool usingMRTExtension = (iter != mExtensionBehavior.end() &&
388 (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000389
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000390 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500391 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400392 out << "\n";
393
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200394 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000395 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500396 for (ReferencedSymbols::const_iterator outputVariableIt =
397 mReferencedOutputVariables.begin();
398 outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000399 {
Jamie Madill46131a32013-06-20 11:55:50 -0400400 const TString &variableName = outputVariableIt->first;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500401 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400402
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500403 out << "static " + TypeString(variableType) + " out_" + variableName +
404 ArrayString(variableType) + " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000405 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000406 }
Jamie Madill46131a32013-06-20 11:55:50 -0400407 else
408 {
409 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
410
411 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500412 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400413 for (unsigned int i = 0; i < numColorValues; i++)
414 {
415 out << " float4(0, 0, 0, 0)";
416 if (i + 1 != numColorValues)
417 {
418 out << ",";
419 }
420 out << "\n";
421 }
422
423 out << "};\n";
424 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000425
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400426 if (mUsesFragDepth)
427 {
428 out << "static float gl_Depth = 0.0;\n";
429 }
430
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000431 if (mUsesFragCoord)
432 {
433 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
434 }
435
436 if (mUsesPointCoord)
437 {
438 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
439 }
440
441 if (mUsesFrontFacing)
442 {
443 out << "static bool gl_FrontFacing = false;\n";
444 }
445
446 out << "\n";
447
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000448 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000449 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000450 out << "struct gl_DepthRangeParameters\n"
451 "{\n"
452 " float near;\n"
453 " float far;\n"
454 " float diff;\n"
455 "};\n"
456 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000457 }
458
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200459 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000460 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000461 out << "cbuffer DriverConstants : register(b1)\n"
462 "{\n";
463
464 if (mUsesDepthRange)
465 {
466 out << " float3 dx_DepthRange : packoffset(c0);\n";
467 }
468
469 if (mUsesFragCoord)
470 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000471 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000472 }
473
474 if (mUsesFragCoord || mUsesFrontFacing)
475 {
476 out << " float3 dx_DepthFront : packoffset(c2);\n";
477 }
478
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800479 if (mUsesFragCoord)
480 {
481 // dx_ViewScale is only used in the fragment shader to correct
482 // the value for glFragCoord if necessary
483 out << " float2 dx_ViewScale : packoffset(c3);\n";
484 }
485
Olli Etuaho618bebc2016-01-15 16:40:00 +0200486 if (mOutputType == SH_HLSL_4_1_OUTPUT)
487 {
488 mUniformHLSL->samplerMetadataUniforms(out, "c4");
489 }
490
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000491 out << "};\n";
492 }
493 else
494 {
495 if (mUsesDepthRange)
496 {
497 out << "uniform float3 dx_DepthRange : register(c0);";
498 }
499
500 if (mUsesFragCoord)
501 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000502 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000503 }
504
505 if (mUsesFragCoord || mUsesFrontFacing)
506 {
507 out << "uniform float3 dx_DepthFront : register(c2);\n";
508 }
509 }
510
511 out << "\n";
512
513 if (mUsesDepthRange)
514 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500515 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
516 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000517 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000518 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000519
Jamie Madillf91ce812014-06-13 10:04:34 -0400520 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000521 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400522 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000523 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400524 out << flaggedStructs;
525 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000526 }
527
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000528 if (usingMRTExtension && mNumRenderTargets > 1)
529 {
530 out << "#define GL_USES_MRT\n";
531 }
532
533 if (mUsesFragColor)
534 {
535 out << "#define GL_USES_FRAG_COLOR\n";
536 }
537
538 if (mUsesFragData)
539 {
540 out << "#define GL_USES_FRAG_DATA\n";
541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500543 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000545 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500546 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000547 out << "\n"
548 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400549
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000550 if (mUsesPointSize)
551 {
552 out << "static float gl_PointSize = float(1);\n";
553 }
554
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000555 if (mUsesInstanceID)
556 {
557 out << "static int gl_InstanceID;";
558 }
559
Corentin Wallezb076add2016-01-11 16:45:46 -0500560 if (mUsesVertexID)
561 {
562 out << "static int gl_VertexID;";
563 }
564
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000565 out << "\n"
566 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500567 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000568 out << "\n";
569
570 if (mUsesDepthRange)
571 {
572 out << "struct gl_DepthRangeParameters\n"
573 "{\n"
574 " float near;\n"
575 " float far;\n"
576 " float diff;\n"
577 "};\n"
578 "\n";
579 }
580
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200581 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000582 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800583 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500584 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800585
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000586 if (mUsesDepthRange)
587 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800588 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000589 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800590
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800591 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
592 // shaders. However, we declare it for all shaders (including Feature Level 10+).
593 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
594 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800595 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800596 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800597 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800598
Olli Etuaho618bebc2016-01-15 16:40:00 +0200599 if (mOutputType == SH_HLSL_4_1_OUTPUT)
600 {
601 mUniformHLSL->samplerMetadataUniforms(out, "c4");
602 }
603
Austin Kinross4fd18b12014-12-22 12:32:05 -0800604 out << "};\n"
605 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000606 }
607 else
608 {
609 if (mUsesDepthRange)
610 {
611 out << "uniform float3 dx_DepthRange : register(c0);\n";
612 }
613
Cooper Partine6664f02015-01-09 16:22:24 -0800614 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
615 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000616 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000617 }
618
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000619 if (mUsesDepthRange)
620 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500621 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
622 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000623 "\n";
624 }
625
Jamie Madillf91ce812014-06-13 10:04:34 -0400626 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000627 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400628 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000629 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400630 out << flaggedStructs;
631 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000632 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400633 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000634
Geoff Lang1fe74c72016-08-25 13:23:01 -0400635 bool getDimensionsIgnoresBaseLevel =
636 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
637 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000638
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000639 if (mUsesFragCoord)
640 {
641 out << "#define GL_USES_FRAG_COORD\n";
642 }
643
644 if (mUsesPointCoord)
645 {
646 out << "#define GL_USES_POINT_COORD\n";
647 }
648
649 if (mUsesFrontFacing)
650 {
651 out << "#define GL_USES_FRONT_FACING\n";
652 }
653
654 if (mUsesPointSize)
655 {
656 out << "#define GL_USES_POINT_SIZE\n";
657 }
658
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400659 if (mUsesFragDepth)
660 {
661 out << "#define GL_USES_FRAG_DEPTH\n";
662 }
663
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000664 if (mUsesDepthRange)
665 {
666 out << "#define GL_USES_DEPTH_RANGE\n";
667 }
668
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000669 if (mUsesXor)
670 {
671 out << "bool xor(bool p, bool q)\n"
672 "{\n"
673 " return (p || q) && !(p && q);\n"
674 "}\n"
675 "\n";
676 }
677
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200678 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679}
680
681void OutputHLSL::visitSymbol(TIntermSymbol *node)
682{
Jamie Madill32aab012015-01-27 14:12:26 -0500683 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684
Jamie Madill570e04d2013-06-21 09:15:33 -0400685 // Handle accessing std140 structs by value
686 if (mFlaggedStructMappedNames.count(node) > 0)
687 {
688 out << mFlaggedStructMappedNames[node];
689 return;
690 }
691
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692 TString name = node->getSymbol();
693
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000694 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000695 {
696 mUsesDepthRange = true;
697 out << name;
698 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699 else
700 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000701 TQualifier qualifier = node->getQualifier();
702
703 if (qualifier == EvqUniform)
704 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500705 const TType &nodeType = node->getType();
Jamie Madill2e295e22015-04-29 10:41:33 -0400706 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400707
708 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000709 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400710 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000711 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000712 else
713 {
714 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000715 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400716
Jamie Madill2e295e22015-04-29 10:41:33 -0400717 ensureStructDefined(nodeType);
718
Olli Etuaho96963162016-03-21 11:54:33 +0200719 const TName &nameWithMetadata = node->getName();
720 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000721 }
Jamie Madill19571812013-08-12 15:26:34 -0700722 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000723 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000724 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400725 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000726 }
Jamie Madill033dae62014-06-18 12:56:28 -0400727 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000728 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000729 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400730 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000731 }
Jamie Madill19571812013-08-12 15:26:34 -0700732 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400733 {
734 mReferencedOutputVariables[name] = node;
735 out << "out_" << name;
736 }
737 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000738 {
739 out << "gl_Color[0]";
740 mUsesFragColor = true;
741 }
742 else if (qualifier == EvqFragData)
743 {
744 out << "gl_Color";
745 mUsesFragData = true;
746 }
747 else if (qualifier == EvqFragCoord)
748 {
749 mUsesFragCoord = true;
750 out << name;
751 }
752 else if (qualifier == EvqPointCoord)
753 {
754 mUsesPointCoord = true;
755 out << name;
756 }
757 else if (qualifier == EvqFrontFacing)
758 {
759 mUsesFrontFacing = true;
760 out << name;
761 }
762 else if (qualifier == EvqPointSize)
763 {
764 mUsesPointSize = true;
765 out << name;
766 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000767 else if (qualifier == EvqInstanceID)
768 {
769 mUsesInstanceID = true;
770 out << name;
771 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500772 else if (qualifier == EvqVertexID)
773 {
774 mUsesVertexID = true;
775 out << name;
776 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300777 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400778 {
779 mUsesFragDepth = true;
780 out << "gl_Depth";
781 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000782 else
783 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300784 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000785 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786 }
787}
788
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400789void OutputHLSL::visitRaw(TIntermRaw *node)
790{
Jamie Madill32aab012015-01-27 14:12:26 -0500791 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400792}
793
Olli Etuaho7fb49552015-03-18 17:27:44 +0200794void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
795{
796 if (type.isScalar() && !type.isArray())
797 {
798 if (op == EOpEqual)
799 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500800 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200801 }
802 else
803 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500804 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200805 }
806 }
807 else
808 {
809 if (visit == PreVisit && op == EOpNotEqual)
810 {
811 out << "!";
812 }
813
814 if (type.isArray())
815 {
816 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500817 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200818 }
819 else if (type.getBasicType() == EbtStruct)
820 {
821 const TStructure &structure = *type.getStruct();
822 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500823 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200824 }
825 else
826 {
827 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500828 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200829 }
830 }
831}
832
Olli Etuaho96963162016-03-21 11:54:33 +0200833bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
834{
835 // Inside InVisit the current node is already in the path.
836 const unsigned int initialN = visit == InVisit ? 1u : 0u;
837 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
838 {
839 TIntermNode *ancestor = getAncestorNode(n);
840 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
841 if (ancestorBinary == nullptr)
842 {
843 return false;
844 }
845 switch (ancestorBinary->getOp())
846 {
847 case EOpIndexDirectStruct:
848 {
849 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
850 const TIntermConstantUnion *index =
851 ancestorBinary->getRight()->getAsConstantUnion();
852 const TField *field = structure->fields()[index->getIConst(0)];
853 if (IsSampler(field->type()->getBasicType()))
854 {
855 return true;
856 }
857 break;
858 }
859 case EOpIndexDirect:
860 break;
861 default:
862 // Returning a sampler from indirect indexing is not supported.
863 return false;
864 }
865 }
866 return false;
867}
868
Olli Etuahob6fa0432016-09-28 16:28:05 +0100869bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
870{
871 TInfoSinkBase &out = getInfoSink();
872 if (visit == PostVisit)
873 {
874 out << ".";
875 node->writeOffsetsAsXYZW(&out);
876 }
877 return true;
878}
879
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
881{
Jamie Madill32aab012015-01-27 14:12:26 -0500882 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000883
Jamie Madill570e04d2013-06-21 09:15:33 -0400884 // Handle accessing std140 structs by value
885 if (mFlaggedStructMappedNames.count(node) > 0)
886 {
887 out << mFlaggedStructMappedNames[node];
888 return false;
889 }
890
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891 switch (node->getOp())
892 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100893 case EOpComma:
894 outputTriplet(out, visit, "(", ", ", ")");
895 break;
896 case EOpAssign:
897 if (node->getLeft()->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +0300898 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100899 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
900 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +0300901 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100902 const TString &functionName = addArrayConstructIntoFunction(node->getType());
903 out << functionName << "(";
904 node->getLeft()->traverse(this);
905 TIntermSequence *seq = rightAgg->getSequence();
906 for (auto &arrayElement : *seq)
907 {
908 out << ", ";
909 arrayElement->traverse(this);
910 }
911 out << ")";
912 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +0300913 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100914 // ArrayReturnValueToOutParameter should have eliminated expressions where a
915 // function call is assigned.
916 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
917
918 const TString &functionName = addArrayAssignmentFunction(node->getType());
919 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho9638c352015-04-01 14:34:52 +0300920 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100921 else
Jamie Madill37997142015-01-28 10:06:34 -0500922 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100923 outputTriplet(out, visit, "(", " = ", ")");
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000924 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100925 break;
926 case EOpInitialize:
927 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200928 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100929 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
930 ASSERT(symbolNode);
931 TIntermTyped *expression = node->getRight();
932
933 // Global initializers must be constant at this point.
934 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
935 canWriteAsHLSLLiteral(expression));
936
937 // GLSL allows to write things like "float x = x;" where a new variable x is defined
938 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
939 // new variable is created before the assignment is evaluated), so we need to
940 // convert
941 // this to "float t = x, x = t;".
942 if (writeSameSymbolInitializer(out, symbolNode, expression))
943 {
944 // Skip initializing the rest of the expression
945 return false;
946 }
947 else if (writeConstantInitialization(out, symbolNode, expression))
948 {
949 return false;
950 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200951 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100952 else if (visit == InVisit)
953 {
954 out << " = ";
955 }
956 break;
957 case EOpAddAssign:
958 outputTriplet(out, visit, "(", " += ", ")");
959 break;
960 case EOpSubAssign:
961 outputTriplet(out, visit, "(", " -= ", ")");
962 break;
963 case EOpMulAssign:
964 outputTriplet(out, visit, "(", " *= ", ")");
965 break;
966 case EOpVectorTimesScalarAssign:
967 outputTriplet(out, visit, "(", " *= ", ")");
968 break;
969 case EOpMatrixTimesScalarAssign:
970 outputTriplet(out, visit, "(", " *= ", ")");
971 break;
972 case EOpVectorTimesMatrixAssign:
973 if (visit == PreVisit)
974 {
975 out << "(";
976 }
977 else if (visit == InVisit)
978 {
979 out << " = mul(";
980 node->getLeft()->traverse(this);
981 out << ", transpose(";
982 }
983 else
984 {
985 out << ")))";
986 }
987 break;
988 case EOpMatrixTimesMatrixAssign:
989 if (visit == PreVisit)
990 {
991 out << "(";
992 }
993 else if (visit == InVisit)
994 {
995 out << " = transpose(mul(transpose(";
996 node->getLeft()->traverse(this);
997 out << "), transpose(";
998 }
999 else
1000 {
1001 out << "))))";
1002 }
1003 break;
1004 case EOpDivAssign:
1005 outputTriplet(out, visit, "(", " /= ", ")");
1006 break;
1007 case EOpIModAssign:
1008 outputTriplet(out, visit, "(", " %= ", ")");
1009 break;
1010 case EOpBitShiftLeftAssign:
1011 outputTriplet(out, visit, "(", " <<= ", ")");
1012 break;
1013 case EOpBitShiftRightAssign:
1014 outputTriplet(out, visit, "(", " >>= ", ")");
1015 break;
1016 case EOpBitwiseAndAssign:
1017 outputTriplet(out, visit, "(", " &= ", ")");
1018 break;
1019 case EOpBitwiseXorAssign:
1020 outputTriplet(out, visit, "(", " ^= ", ")");
1021 break;
1022 case EOpBitwiseOrAssign:
1023 outputTriplet(out, visit, "(", " |= ", ")");
1024 break;
1025 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001026 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001027 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001028 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001029 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001030 if (visit == PreVisit)
1031 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001032 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001033 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001034 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] =
1035 node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001036 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001037 return false;
1038 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001039 }
Olli Etuaho96963162016-03-21 11:54:33 +02001040 else if (ancestorEvaluatesToSamplerInStruct(visit))
1041 {
1042 // All parts of an expression that access a sampler in a struct need to use _ as
1043 // separator to access the sampler variable that has been moved out of the struct.
1044 outputTriplet(out, visit, "", "_", "");
1045 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001046 else
1047 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001048 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001049 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001050 }
1051 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001052 case EOpIndexIndirect:
1053 // We do not currently support indirect references to interface blocks
1054 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1055 outputTriplet(out, visit, "", "[", "]");
1056 break;
1057 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001058 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001059 const TStructure *structure = node->getLeft()->getType().getStruct();
1060 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1061 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001062
Olli Etuaho96963162016-03-21 11:54:33 +02001063 // In cases where indexing returns a sampler, we need to access the sampler variable
1064 // that has been moved out of the struct.
1065 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1066 if (visit == PreVisit && indexingReturnsSampler)
1067 {
1068 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1069 // This prefix is only output at the beginning of the indexing expression, which
1070 // may have multiple parts.
1071 out << "angle";
1072 }
1073 if (!indexingReturnsSampler)
1074 {
1075 // All parts of an expression that access a sampler in a struct need to use _ as
1076 // separator to access the sampler variable that has been moved out of the struct.
1077 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1078 }
1079 if (visit == InVisit)
1080 {
1081 if (indexingReturnsSampler)
1082 {
1083 out << "_" + field->name();
1084 }
1085 else
1086 {
1087 out << "." + DecorateField(field->name(), *structure);
1088 }
1089
1090 return false;
1091 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001092 }
1093 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001094 case EOpIndexDirectInterfaceBlock:
1095 if (visit == InVisit)
1096 {
1097 const TInterfaceBlock *interfaceBlock =
1098 node->getLeft()->getType().getInterfaceBlock();
1099 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1100 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
1101 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001102
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001103 return false;
1104 }
1105 break;
1106 case EOpAdd:
1107 outputTriplet(out, visit, "(", " + ", ")");
1108 break;
1109 case EOpSub:
1110 outputTriplet(out, visit, "(", " - ", ")");
1111 break;
1112 case EOpMul:
1113 outputTriplet(out, visit, "(", " * ", ")");
1114 break;
1115 case EOpDiv:
1116 outputTriplet(out, visit, "(", " / ", ")");
1117 break;
1118 case EOpIMod:
1119 outputTriplet(out, visit, "(", " % ", ")");
1120 break;
1121 case EOpBitShiftLeft:
1122 outputTriplet(out, visit, "(", " << ", ")");
1123 break;
1124 case EOpBitShiftRight:
1125 outputTriplet(out, visit, "(", " >> ", ")");
1126 break;
1127 case EOpBitwiseAnd:
1128 outputTriplet(out, visit, "(", " & ", ")");
1129 break;
1130 case EOpBitwiseXor:
1131 outputTriplet(out, visit, "(", " ^ ", ")");
1132 break;
1133 case EOpBitwiseOr:
1134 outputTriplet(out, visit, "(", " | ", ")");
1135 break;
1136 case EOpEqual:
1137 case EOpNotEqual:
1138 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1139 break;
1140 case EOpLessThan:
1141 outputTriplet(out, visit, "(", " < ", ")");
1142 break;
1143 case EOpGreaterThan:
1144 outputTriplet(out, visit, "(", " > ", ")");
1145 break;
1146 case EOpLessThanEqual:
1147 outputTriplet(out, visit, "(", " <= ", ")");
1148 break;
1149 case EOpGreaterThanEqual:
1150 outputTriplet(out, visit, "(", " >= ", ")");
1151 break;
1152 case EOpVectorTimesScalar:
1153 outputTriplet(out, visit, "(", " * ", ")");
1154 break;
1155 case EOpMatrixTimesScalar:
1156 outputTriplet(out, visit, "(", " * ", ")");
1157 break;
1158 case EOpVectorTimesMatrix:
1159 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1160 break;
1161 case EOpMatrixTimesVector:
1162 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1163 break;
1164 case EOpMatrixTimesMatrix:
1165 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1166 break;
1167 case EOpLogicalOr:
1168 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1169 // been unfolded.
1170 ASSERT(!node->getRight()->hasSideEffects());
1171 outputTriplet(out, visit, "(", " || ", ")");
1172 return true;
1173 case EOpLogicalXor:
1174 mUsesXor = true;
1175 outputTriplet(out, visit, "xor(", ", ", ")");
1176 break;
1177 case EOpLogicalAnd:
1178 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1179 // been unfolded.
1180 ASSERT(!node->getRight()->hasSideEffects());
1181 outputTriplet(out, visit, "(", " && ", ")");
1182 return true;
1183 default:
1184 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185 }
1186
1187 return true;
1188}
1189
1190bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1191{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001192 TInfoSinkBase &out = getInfoSink();
1193
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001194 switch (node->getOp())
1195 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001196 case EOpNegative:
1197 outputTriplet(out, visit, "(-", "", ")");
1198 break;
1199 case EOpPositive:
1200 outputTriplet(out, visit, "(+", "", ")");
1201 break;
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
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001619bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1620{
1621 // Do not do any translation
1622 return false;
1623}
1624
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001625bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1626{
Jamie Madill32aab012015-01-27 14:12:26 -05001627 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001629 switch (node->getOp())
1630 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001631 case EOpPrototype:
1632 if (visit == PreVisit)
1633 {
Olli Etuahobd674552016-10-06 13:28:42 +01001634 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Olli Etuaho5878f832016-10-07 10:14:58 +01001635 // Skip the prototype if it is not implemented (and thus not used)
1636 if (index == CallDAG::InvalidIndex)
1637 {
1638 return false;
1639 }
1640
1641 TIntermSequence *arguments = node->getSequence();
1642
Olli Etuahobd674552016-10-06 13:28:42 +01001643 TString name =
1644 DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho5878f832016-10-07 10:14:58 +01001645 out << TypeString(node->getType()) << " " << name
1646 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
1647
1648 for (unsigned int i = 0; i < arguments->size(); i++)
1649 {
1650 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1651
1652 if (symbol)
1653 {
1654 out << argumentString(symbol);
1655
1656 if (i < arguments->size() - 1)
1657 {
1658 out << ", ";
1659 }
1660 }
1661 else
1662 UNREACHABLE();
1663 }
1664
1665 out << ");\n";
1666
1667 // Also prototype the Lod0 variant if needed
1668 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1669 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1670 {
1671 mOutputLod0Function = true;
1672 node->traverse(this);
1673 mOutputLod0Function = false;
1674 }
1675
1676 return false;
1677 }
1678 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001679 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001680 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001681 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001682
Corentin Wallez1239ee92015-03-19 14:38:02 -07001683 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001684 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001685 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001686 if (node->isArray())
1687 {
1688 UNIMPLEMENTED();
1689 }
Olli Etuahobd674552016-10-06 13:28:42 +01001690 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001691 ASSERT(index != CallDAG::InvalidIndex);
1692 lod0 &= mASTMetadataList[index].mNeedsLod0;
1693
Olli Etuahobd674552016-10-06 13:28:42 +01001694 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001695 out << DisambiguateFunctionName(node->getSequence());
1696 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001697 }
Olli Etuahobd674552016-10-06 13:28:42 +01001698 else if (node->getFunctionSymbolInfo()->getNameObj().isInternal())
Olli Etuahob741c762016-06-29 15:49:22 +03001699 {
1700 // This path is used for internal functions that don't have their definitions in the
1701 // AST, such as precision emulation functions.
Olli Etuahobd674552016-10-06 13:28:42 +01001702 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001704 else
1705 {
Olli Etuahobd674552016-10-06 13:28:42 +01001706 TString name = TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001707 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001708 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001709 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1710 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1711 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001712 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001713
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001714 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001715 {
Olli Etuaho96963162016-03-21 11:54:33 +02001716 TIntermTyped *typedArg = (*arg)->getAsTyped();
1717 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001718 {
1719 out << "texture_";
1720 (*arg)->traverse(this);
1721 out << ", sampler_";
1722 }
1723
1724 (*arg)->traverse(this);
1725
Olli Etuaho96963162016-03-21 11:54:33 +02001726 if (typedArg->getType().isStructureContainingSamplers())
1727 {
1728 const TType &argType = typedArg->getType();
1729 TVector<TIntermSymbol *> samplerSymbols;
1730 TString structName = samplerNamePrefixFromStruct(typedArg);
1731 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001732 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001733 &samplerSymbols, nullptr);
1734 for (const TIntermSymbol *sampler : samplerSymbols)
1735 {
1736 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1737 {
1738 out << ", texture_" << sampler->getSymbol();
1739 out << ", sampler_" << sampler->getSymbol();
1740 }
1741 else
1742 {
1743 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1744 // of D3D9, it's the sampler variable.
1745 out << ", " + sampler->getSymbol();
1746 }
1747 }
1748 }
1749
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001750 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001751 {
1752 out << ", ";
1753 }
1754 }
1755
1756 out << ")";
1757
1758 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001760 case EOpParameters:
1761 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1762 break;
1763 case EOpConstructFloat:
1764 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1765 break;
1766 case EOpConstructVec2:
1767 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1768 break;
1769 case EOpConstructVec3:
1770 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1771 break;
1772 case EOpConstructVec4:
1773 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1774 break;
1775 case EOpConstructBool:
1776 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1777 break;
1778 case EOpConstructBVec2:
1779 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1780 break;
1781 case EOpConstructBVec3:
1782 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1783 break;
1784 case EOpConstructBVec4:
1785 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1786 break;
1787 case EOpConstructInt:
1788 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1789 break;
1790 case EOpConstructIVec2:
1791 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1792 break;
1793 case EOpConstructIVec3:
1794 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1795 break;
1796 case EOpConstructIVec4:
1797 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1798 break;
1799 case EOpConstructUInt:
1800 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1801 break;
1802 case EOpConstructUVec2:
1803 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1804 break;
1805 case EOpConstructUVec3:
1806 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1807 break;
1808 case EOpConstructUVec4:
1809 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1810 break;
1811 case EOpConstructMat2:
1812 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1813 break;
1814 case EOpConstructMat2x3:
1815 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1816 break;
1817 case EOpConstructMat2x4:
1818 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1819 break;
1820 case EOpConstructMat3x2:
1821 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1822 break;
1823 case EOpConstructMat3:
1824 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1825 break;
1826 case EOpConstructMat3x4:
1827 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1828 break;
1829 case EOpConstructMat4x2:
1830 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1831 break;
1832 case EOpConstructMat4x3:
1833 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1834 break;
1835 case EOpConstructMat4:
1836 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1837 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001838 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001839 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001840 if (node->getType().isArray())
1841 {
1842 UNIMPLEMENTED();
1843 }
Jamie Madill033dae62014-06-18 12:56:28 -04001844 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001845 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001846 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001847 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001848 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001849 case EOpLessThan:
1850 outputTriplet(out, visit, "(", " < ", ")");
1851 break;
1852 case EOpGreaterThan:
1853 outputTriplet(out, visit, "(", " > ", ")");
1854 break;
1855 case EOpLessThanEqual:
1856 outputTriplet(out, visit, "(", " <= ", ")");
1857 break;
1858 case EOpGreaterThanEqual:
1859 outputTriplet(out, visit, "(", " >= ", ")");
1860 break;
1861 case EOpVectorEqual:
1862 outputTriplet(out, visit, "(", " == ", ")");
1863 break;
1864 case EOpVectorNotEqual:
1865 outputTriplet(out, visit, "(", " != ", ")");
1866 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001867 case EOpMod:
1868 ASSERT(node->getUseEmulatedFunction());
1869 writeEmulatedFunctionTriplet(out, visit, "mod(");
1870 break;
1871 case EOpModf:
1872 outputTriplet(out, visit, "modf(", ", ", ")");
1873 break;
1874 case EOpPow:
1875 outputTriplet(out, visit, "pow(", ", ", ")");
1876 break;
1877 case EOpAtan:
1878 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1879 ASSERT(node->getUseEmulatedFunction());
1880 writeEmulatedFunctionTriplet(out, visit, "atan(");
1881 break;
1882 case EOpMin:
1883 outputTriplet(out, visit, "min(", ", ", ")");
1884 break;
1885 case EOpMax:
1886 outputTriplet(out, visit, "max(", ", ", ")");
1887 break;
1888 case EOpClamp:
1889 outputTriplet(out, visit, "clamp(", ", ", ")");
1890 break;
1891 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301892 {
1893 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1894 if (lastParamNode->getType().getBasicType() == EbtBool)
1895 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001896 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
1897 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05301898 // so use emulated version.
1899 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001900 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301901 }
1902 else
1903 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001904 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301905 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001906 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301907 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001908 case EOpStep:
1909 outputTriplet(out, visit, "step(", ", ", ")");
1910 break;
1911 case EOpSmoothStep:
1912 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1913 break;
1914 case EOpDistance:
1915 outputTriplet(out, visit, "distance(", ", ", ")");
1916 break;
1917 case EOpDot:
1918 outputTriplet(out, visit, "dot(", ", ", ")");
1919 break;
1920 case EOpCross:
1921 outputTriplet(out, visit, "cross(", ", ", ")");
1922 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001923 case EOpFaceForward:
1924 ASSERT(node->getUseEmulatedFunction());
1925 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
1926 break;
1927 case EOpReflect:
1928 outputTriplet(out, visit, "reflect(", ", ", ")");
1929 break;
1930 case EOpRefract:
1931 outputTriplet(out, visit, "refract(", ", ", ")");
1932 break;
1933 case EOpOuterProduct:
1934 ASSERT(node->getUseEmulatedFunction());
1935 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
1936 break;
1937 case EOpMul:
1938 outputTriplet(out, visit, "(", " * ", ")");
1939 break;
1940 default:
1941 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942 }
1943
1944 return true;
1945}
1946
Olli Etuaho57961272016-09-14 13:57:46 +03001947void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001948{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001949 out << "if (";
1950
1951 node->getCondition()->traverse(this);
1952
1953 out << ")\n";
1954
Jamie Madill8c46ab12015-12-07 16:39:19 -05001955 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001956
1957 bool discard = false;
1958
1959 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001961 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001962 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001963
Olli Etuahoa6f22092015-05-08 18:31:10 +03001964 // Detect true discard
1965 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1966 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001967 else
1968 {
1969 // TODO(oetuaho): Check if the semicolon inside is necessary.
1970 // It's there as a result of conservative refactoring of the output.
1971 out << "{;}\n";
1972 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001973
Jamie Madill8c46ab12015-12-07 16:39:19 -05001974 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001975
Olli Etuahoa6f22092015-05-08 18:31:10 +03001976 if (node->getFalseBlock())
1977 {
1978 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001979
Jamie Madill8c46ab12015-12-07 16:39:19 -05001980 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981
Olli Etuaho32db19b2016-10-04 14:43:16 +01001982 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001983 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001984
Jamie Madill8c46ab12015-12-07 16:39:19 -05001985 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001986
Olli Etuahoa6f22092015-05-08 18:31:10 +03001987 // Detect false discard
1988 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1989 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001990
Olli Etuahoa6f22092015-05-08 18:31:10 +03001991 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001992 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001993 {
1994 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001995 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001996}
1997
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001998bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1999{
2000 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2001 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2002 UNREACHABLE();
2003 return false;
2004}
2005
Olli Etuaho57961272016-09-14 13:57:46 +03002006bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002007{
2008 TInfoSinkBase &out = getInfoSink();
2009
Olli Etuaho3d932d82016-04-12 11:10:30 +03002010 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002011
2012 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002013 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002014 {
2015 out << "FLATTEN ";
2016 }
2017
Olli Etuaho57961272016-09-14 13:57:46 +03002018 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019
2020 return false;
2021}
2022
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002023bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002024{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002025 TInfoSinkBase &out = getInfoSink();
2026
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002027 if (node->getStatementList())
2028 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002029 node->setStatementList(
2030 RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002031 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002032 // The curly braces get written when visiting the statementList aggregate
2033 }
2034 else
2035 {
2036 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002037 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002038 }
2039 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002040}
2041
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002042bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002043{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002044 TInfoSinkBase &out = getInfoSink();
2045
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002046 if (node->hasCondition())
2047 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002048 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002049 return true;
2050 }
2051 else
2052 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002053 out << "default:\n";
2054 return false;
2055 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002056}
2057
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2059{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002060 TInfoSinkBase &out = getInfoSink();
2061 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062}
2063
2064bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2065{
Nicolas Capens655fe362014-04-11 13:12:34 -04002066 mNestedLoopDepth++;
2067
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002068 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002069 mInsideDiscontinuousLoop =
2070 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002071
Jamie Madill8c46ab12015-12-07 16:39:19 -05002072 TInfoSinkBase &out = getInfoSink();
2073
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002074 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002075 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002076 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002077 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002078 mInsideDiscontinuousLoop = wasDiscontinuous;
2079 mNestedLoopDepth--;
2080
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002081 return false;
2082 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002083 }
2084
Corentin Wallez1239ee92015-03-19 14:38:02 -07002085 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002086 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002088 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002089
Jamie Madill8c46ab12015-12-07 16:39:19 -05002090 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091 }
2092 else
2093 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002094 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002095
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096 if (node->getInit())
2097 {
2098 node->getInit()->traverse(this);
2099 }
2100
2101 out << "; ";
2102
alokp@chromium.org52813552010-11-16 18:36:09 +00002103 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002105 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 }
2107
2108 out << "; ";
2109
alokp@chromium.org52813552010-11-16 18:36:09 +00002110 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002112 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113 }
2114
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002115 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002116
Jamie Madill8c46ab12015-12-07 16:39:19 -05002117 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002118 }
2119
2120 if (node->getBody())
2121 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002122 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002123 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002125 else
2126 {
2127 // TODO(oetuaho): Check if the semicolon inside is necessary.
2128 // It's there as a result of conservative refactoring of the output.
2129 out << "{;}\n";
2130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131
Jamie Madill8c46ab12015-12-07 16:39:19 -05002132 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133
alokp@chromium.org52813552010-11-16 18:36:09 +00002134 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002136 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137 out << "while(\n";
2138
alokp@chromium.org52813552010-11-16 18:36:09 +00002139 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002140
daniel@transgaming.com73536982012-03-21 20:45:49 +00002141 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142 }
2143
daniel@transgaming.com73536982012-03-21 20:45:49 +00002144 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002146 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002147 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002148
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149 return false;
2150}
2151
2152bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2153{
Jamie Madill32aab012015-01-27 14:12:26 -05002154 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155
2156 switch (node->getFlowOp())
2157 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002158 case EOpKill:
2159 outputTriplet(out, visit, "discard;\n", "", "");
2160 break;
2161 case EOpBreak:
2162 if (visit == PreVisit)
Nicolas Capens655fe362014-04-11 13:12:34 -04002163 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002164 if (mNestedLoopDepth > 1)
2165 {
2166 mUsesNestedBreak = true;
2167 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002168
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002169 if (mExcessiveLoopIndex)
2170 {
2171 out << "{Break";
2172 mExcessiveLoopIndex->traverse(this);
2173 out << " = true; break;}\n";
2174 }
2175 else
2176 {
2177 out << "break;\n";
2178 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002179 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002180 break;
2181 case EOpContinue:
2182 outputTriplet(out, visit, "continue;\n", "", "");
2183 break;
2184 case EOpReturn:
2185 if (visit == PreVisit)
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002186 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002187 if (node->getExpression())
2188 {
2189 out << "return ";
2190 }
2191 else
2192 {
2193 out << "return;\n";
2194 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002195 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002196 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002198 if (node->getExpression())
2199 {
2200 out << ";\n";
2201 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002202 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002203 break;
2204 default:
2205 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206 }
2207
2208 return true;
2209}
2210
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002211// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002212// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2213// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002214bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002215{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002216 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002217
2218 // Parse loops of the form:
2219 // for(int index = initial; index [comparator] limit; index += increment)
2220 TIntermSymbol *index = NULL;
2221 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002222 int initial = 0;
2223 int limit = 0;
2224 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002225
2226 // Parse index name and intial value
2227 if (node->getInit())
2228 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002229 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002230
2231 if (init)
2232 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002233 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002234 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002235
2236 if (variable && variable->getQualifier() == EvqTemporary)
2237 {
2238 TIntermBinary *assign = variable->getAsBinaryNode();
2239
2240 if (assign->getOp() == EOpInitialize)
2241 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002242 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002243 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2244
2245 if (symbol && constant)
2246 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002247 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002248 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002249 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002250 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002251 }
2252 }
2253 }
2254 }
2255 }
2256 }
2257
2258 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002259 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002260 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002261 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002262
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002263 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2264 {
2265 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2266
2267 if (constant)
2268 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002269 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002270 {
2271 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002272 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002273 }
2274 }
2275 }
2276 }
2277
2278 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002279 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002280 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002281 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002282 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002283
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002284 if (binaryTerminal)
2285 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002286 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002287 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2288
2289 if (constant)
2290 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002291 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002292 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002293 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002294
2295 switch (op)
2296 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002297 case EOpAddAssign:
2298 increment = value;
2299 break;
2300 case EOpSubAssign:
2301 increment = -value;
2302 break;
2303 default:
2304 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002305 }
2306 }
2307 }
2308 }
2309 else if (unaryTerminal)
2310 {
2311 TOperator op = unaryTerminal->getOp();
2312
2313 switch (op)
2314 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002315 case EOpPostIncrement:
2316 increment = 1;
2317 break;
2318 case EOpPostDecrement:
2319 increment = -1;
2320 break;
2321 case EOpPreIncrement:
2322 increment = 1;
2323 break;
2324 case EOpPreDecrement:
2325 increment = -1;
2326 break;
2327 default:
2328 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002329 }
2330 }
2331 }
2332
2333 if (index != NULL && comparator != EOpNull && increment != 0)
2334 {
2335 if (comparator == EOpLessThanEqual)
2336 {
2337 comparator = EOpLessThan;
2338 limit += 1;
2339 }
2340
2341 if (comparator == EOpLessThan)
2342 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002343 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002344
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002345 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002346 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002347 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002348 }
2349
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002350 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002351 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002352
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002353 out << "{int ";
2354 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002355 out << ";\n"
2356 "bool Break";
2357 index->traverse(this);
2358 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002359
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002360 bool firstLoopFragment = true;
2361
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002362 while (iterations > 0)
2363 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002364 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002365
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002366 if (!firstLoopFragment)
2367 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002368 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002369 index->traverse(this);
2370 out << ") {\n";
2371 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002372
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002373 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002374 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002375 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002376 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002377
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002378 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002379 const char *unroll =
2380 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002381
Corentin Wallez1239ee92015-03-19 14:38:02 -07002382 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002383 index->traverse(this);
2384 out << " = ";
2385 out << initial;
2386
2387 out << "; ";
2388 index->traverse(this);
2389 out << " < ";
2390 out << clampedLimit;
2391
2392 out << "; ";
2393 index->traverse(this);
2394 out << " += ";
2395 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002396 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002397
Jamie Madill8c46ab12015-12-07 16:39:19 -05002398 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002399 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002400
2401 if (node->getBody())
2402 {
2403 node->getBody()->traverse(this);
2404 }
2405
Jamie Madill8c46ab12015-12-07 16:39:19 -05002406 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002407 out << ";}\n";
2408
2409 if (!firstLoopFragment)
2410 {
2411 out << "}\n";
2412 }
2413
2414 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002415
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002416 initial += MAX_LOOP_ITERATIONS * increment;
2417 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002418 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002419
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002420 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002422 mExcessiveLoopIndex = restoreIndex;
2423
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002424 return true;
2425 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002426 else
2427 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002428 }
2429
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002430 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002431}
2432
Jamie Madill8c46ab12015-12-07 16:39:19 -05002433void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2434 Visit visit,
2435 const char *preString,
2436 const char *inString,
2437 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002439 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002440 {
2441 out << preString;
2442 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002443 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 {
2445 out << inString;
2446 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002447 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 {
2449 out << postString;
2450 }
2451}
2452
Jamie Madill8c46ab12015-12-07 16:39:19 -05002453void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002454{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002455 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002456 {
Jamie Madill32aab012015-01-27 14:12:26 -05002457 out << "\n";
2458 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002459
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002460 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002461 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002462 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002463 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002464
Jamie Madill32aab012015-01-27 14:12:26 -05002465 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002466 }
2467}
2468
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002469TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2470{
2471 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002472 const TType &type = symbol->getType();
2473 const TName &name = symbol->getName();
2474 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002475
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002476 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002477 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002478 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002479 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002480 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002481 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002482 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002483 }
2484
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002485 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002486 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002487 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2488 {
2489 // Samplers are passed as indices to the sampler array.
2490 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2491 return "const uint " + nameStr + ArrayString(type);
2492 }
2493 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2494 {
2495 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2496 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2497 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2498 ArrayString(type);
2499 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002500 }
2501
Olli Etuaho96963162016-03-21 11:54:33 +02002502 TStringStream argString;
2503 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2504 << ArrayString(type);
2505
2506 // If the structure parameter contains samplers, they need to be passed into the function as
2507 // separate parameters. HLSL doesn't natively support samplers in structs.
2508 if (type.isStructureContainingSamplers())
2509 {
2510 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2511 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002512 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002513 for (const TIntermSymbol *sampler : samplerSymbols)
2514 {
2515 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2516 {
2517 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2518 }
2519 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2520 {
2521 const TType &samplerType = sampler->getType();
2522 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2523 type.getArraySize() == samplerType.getArraySize());
2524 ASSERT(IsSampler(samplerType.getBasicType()));
2525 argString << ", " << QualifierString(qualifier) << " "
2526 << TextureString(samplerType.getBasicType()) << " texture_"
2527 << sampler->getSymbol() << ArrayString(type) << ", "
2528 << QualifierString(qualifier) << " "
2529 << SamplerString(samplerType.getBasicType()) << " sampler_"
2530 << sampler->getSymbol() << ArrayString(type);
2531 }
2532 else
2533 {
2534 const TType &samplerType = sampler->getType();
2535 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2536 type.getArraySize() == samplerType.getArraySize());
2537 ASSERT(IsSampler(samplerType.getBasicType()));
2538 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2539 << " " << sampler->getSymbol() << ArrayString(type);
2540 }
2541 }
2542 }
2543
2544 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545}
2546
2547TString OutputHLSL::initializer(const TType &type)
2548{
2549 TString string;
2550
Jamie Madill94bf7f22013-07-08 13:31:15 -04002551 size_t size = type.getObjectSize();
2552 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002553 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002554 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555
Jamie Madill94bf7f22013-07-08 13:31:15 -04002556 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 {
2558 string += ", ";
2559 }
2560 }
2561
daniel@transgaming.comead23042010-04-29 03:35:36 +00002562 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002563}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002564
Jamie Madill8c46ab12015-12-07 16:39:19 -05002565void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2566 Visit visit,
2567 const TType &type,
2568 const char *name,
2569 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002570{
Olli Etuahof40319e2015-03-10 14:33:00 +02002571 if (type.isArray())
2572 {
2573 UNIMPLEMENTED();
2574 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002575
2576 if (visit == PreVisit)
2577 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002578 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002579
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002580 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002581 }
2582 else if (visit == InVisit)
2583 {
2584 out << ", ";
2585 }
2586 else if (visit == PostVisit)
2587 {
2588 out << ")";
2589 }
2590}
2591
Jamie Madill8c46ab12015-12-07 16:39:19 -05002592const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2593 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002594 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002595{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002596 const TConstantUnion *constUnionIterated = constUnion;
2597
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002598 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002599 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002600 {
Jamie Madill033dae62014-06-18 12:56:28 -04002601 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002602
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002603 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002604
Jamie Madill98493dd2013-07-08 14:39:03 -04002605 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002606 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002607 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002608 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002609
Jamie Madill98493dd2013-07-08 14:39:03 -04002610 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002611 {
2612 out << ", ";
2613 }
2614 }
2615
2616 out << ")";
2617 }
2618 else
2619 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002620 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002621 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002622
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002623 if (writeType)
2624 {
Jamie Madill033dae62014-06-18 12:56:28 -04002625 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002626 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002627 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002628 if (writeType)
2629 {
2630 out << ")";
2631 }
2632 }
2633
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002634 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002635}
2636
Jamie Madill8c46ab12015-12-07 16:39:19 -05002637void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002638{
2639 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002640 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002641}
2642
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002643bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2644 TIntermSymbol *symbolNode,
2645 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002646{
2647 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2648 expression->traverse(&searchSymbol);
2649
2650 if (searchSymbol.foundMatch())
2651 {
2652 // Type already printed
2653 out << "t" + str(mUniqueIndex) + " = ";
2654 expression->traverse(this);
2655 out << ", ";
2656 symbolNode->traverse(this);
2657 out << " = t" + str(mUniqueIndex);
2658
2659 mUniqueIndex++;
2660 return true;
2661 }
2662
2663 return false;
2664}
2665
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002666bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2667{
2668 // We support writing constant unions and constructors that only take constant unions as
2669 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002670 return expression->getAsConstantUnion() ||
2671 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002672}
2673
2674bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2675 TIntermSymbol *symbolNode,
2676 TIntermTyped *expression)
2677{
2678 if (canWriteAsHLSLLiteral(expression))
2679 {
2680 symbolNode->traverse(this);
2681 if (expression->getType().isArray())
2682 {
2683 out << "[" << expression->getType().getArraySize() << "]";
2684 }
2685 out << " = {";
2686 if (expression->getAsConstantUnion())
2687 {
2688 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2689 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002690 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002691 }
2692 else
2693 {
2694 TIntermAggregate *constructor = expression->getAsAggregate();
2695 ASSERT(constructor != nullptr);
2696 for (TIntermNode *&node : *constructor->getSequence())
2697 {
2698 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2699 ASSERT(nodeConst);
2700 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002701 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002702 if (node != constructor->getSequence()->back())
2703 {
2704 out << ", ";
2705 }
2706 }
2707 }
2708 out << "}";
2709 return true;
2710 }
2711 return false;
2712}
2713
Jamie Madill55e79e02015-02-09 15:35:00 -05002714TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2715{
2716 const TFieldList &fields = structure.fields();
2717
2718 for (const auto &eqFunction : mStructEqualityFunctions)
2719 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002720 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002721 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002722 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002723 }
2724 }
2725
2726 const TString &structNameString = StructNameString(structure);
2727
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002728 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002729 function->structure = &structure;
2730 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002731
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002732 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002733
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002734 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2735 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002736 << "{\n"
2737 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002738
2739 for (size_t i = 0; i < fields.size(); i++)
2740 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002741 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002742 const TType *fieldType = field->type();
2743
2744 const TString &fieldNameA = "a." + Decorate(field->name());
2745 const TString &fieldNameB = "b." + Decorate(field->name());
2746
2747 if (i > 0)
2748 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002749 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002750 }
2751
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002752 fnOut << "(";
2753 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2754 fnOut << fieldNameA;
2755 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2756 fnOut << fieldNameB;
2757 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2758 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002759 }
2760
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002761 fnOut << ";\n"
2762 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002763
2764 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002765
2766 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002767 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002768
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002769 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002770}
2771
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002772TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002773{
2774 for (const auto &eqFunction : mArrayEqualityFunctions)
2775 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002776 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002777 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002778 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002779 }
2780 }
2781
2782 const TString &typeName = TypeString(type);
2783
Olli Etuaho12690762015-03-31 12:55:28 +03002784 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002785 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002786
2787 TInfoSinkBase fnNameOut;
2788 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002789 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002790
2791 TType nonArrayType = type;
2792 nonArrayType.clearArrayness();
2793
2794 TInfoSinkBase fnOut;
2795
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002796 fnOut << "bool " << function->functionName << "(" << typeName << " a[" << type.getArraySize()
2797 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002798 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002799 " for (int i = 0; i < "
2800 << type.getArraySize() << "; ++i)\n"
2801 " {\n"
2802 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002803
2804 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2805 fnOut << "a[i]";
2806 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2807 fnOut << "b[i]";
2808 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2809
2810 fnOut << ") { return false; }\n"
2811 " }\n"
2812 " return true;\n"
2813 "}\n";
2814
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002815 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002816
2817 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002818 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002819
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002820 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002821}
2822
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002823TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002824{
2825 for (const auto &assignFunction : mArrayAssignmentFunctions)
2826 {
2827 if (assignFunction.type == type)
2828 {
2829 return assignFunction.functionName;
2830 }
2831 }
2832
2833 const TString &typeName = TypeString(type);
2834
2835 ArrayHelperFunction function;
2836 function.type = type;
2837
2838 TInfoSinkBase fnNameOut;
2839 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2840 function.functionName = fnNameOut.c_str();
2841
2842 TInfoSinkBase fnOut;
2843
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002844 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2845 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
2846 << "{\n"
2847 " for (int i = 0; i < "
2848 << type.getArraySize() << "; ++i)\n"
2849 " {\n"
2850 " a[i] = b[i];\n"
2851 " }\n"
2852 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03002853
2854 function.functionDefinition = fnOut.c_str();
2855
2856 mArrayAssignmentFunctions.push_back(function);
2857
2858 return function.functionName;
2859}
2860
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002861TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03002862{
2863 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2864 {
2865 if (constructIntoFunction.type == type)
2866 {
2867 return constructIntoFunction.functionName;
2868 }
2869 }
2870
2871 const TString &typeName = TypeString(type);
2872
2873 ArrayHelperFunction function;
2874 function.type = type;
2875
2876 TInfoSinkBase fnNameOut;
2877 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2878 function.functionName = fnNameOut.c_str();
2879
2880 TInfoSinkBase fnOut;
2881
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002882 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2883 << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002884 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002885 {
2886 fnOut << ", " << typeName << " b" << i;
2887 }
2888 fnOut << ")\n"
2889 "{\n";
2890
Olli Etuaho856c4972016-08-08 11:38:39 +03002891 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002892 {
2893 fnOut << " a[" << i << "] = b" << i << ";\n";
2894 }
2895 fnOut << "}\n";
2896
2897 function.functionDefinition = fnOut.c_str();
2898
2899 mArrayConstructIntoFunctions.push_back(function);
2900
2901 return function.functionName;
2902}
2903
Jamie Madill2e295e22015-04-29 10:41:33 -04002904void OutputHLSL::ensureStructDefined(const TType &type)
2905{
2906 TStructure *structure = type.getStruct();
2907
2908 if (structure)
2909 {
2910 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2911 }
2912}
2913
Jamie Madill45bcc782016-11-07 13:58:48 -05002914} // namespace sh