blob: ce14d3c32c3fb16c0dabc3f422de116cd24859f3 [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
Olli Etuaho4785fec2015-05-18 16:09:37 +030031namespace
32{
33
34bool IsSequence(TIntermNode *node)
35{
36 return node->getAsAggregate() != nullptr && node->getAsAggregate()->getOp() == EOpSequence;
37}
38
Olli Etuaho18b9deb2015-11-05 12:14:50 +020039void WriteSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
40{
41 ASSERT(constUnion != nullptr);
42 switch (constUnion->getType())
43 {
44 case EbtFloat:
45 out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst()));
46 break;
47 case EbtInt:
48 out << constUnion->getIConst();
49 break;
50 case EbtUInt:
51 out << constUnion->getUConst();
52 break;
53 case EbtBool:
54 out << constUnion->getBConst();
55 break;
56 default:
57 UNREACHABLE();
58 }
59}
60
61const TConstantUnion *WriteConstantUnionArray(TInfoSinkBase &out,
62 const TConstantUnion *const constUnion,
63 const size_t size)
64{
65 const TConstantUnion *constUnionIterated = constUnion;
66 for (size_t i = 0; i < size; i++, constUnionIterated++)
67 {
68 WriteSingleConstant(out, constUnionIterated);
69
70 if (i != size - 1)
71 {
72 out << ", ";
73 }
74 }
75 return constUnionIterated;
76}
77
Olli Etuaho4785fec2015-05-18 16:09:37 +030078} // namespace
79
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080namespace sh
81{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000082
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080083OutputHLSL::OutputHLSL(sh::GLenum shaderType,
84 int shaderVersion,
85 const TExtensionBehavior &extensionBehavior,
86 const char *sourcePath,
87 ShShaderOutput outputType,
88 int numRenderTargets,
89 const std::vector<Uniform> &uniforms,
90 ShCompileOptions compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -040091 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020092 mShaderType(shaderType),
93 mShaderVersion(shaderVersion),
94 mExtensionBehavior(extensionBehavior),
95 mSourcePath(sourcePath),
96 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -070097 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +100098 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -070099 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000101 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000102
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000103 mUsesFragColor = false;
104 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000105 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000106 mUsesFragCoord = false;
107 mUsesPointCoord = false;
108 mUsesFrontFacing = false;
109 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000110 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500111 mUsesVertexID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400112 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000113 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500114 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400115 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530116 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000117
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000118 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000119
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000120 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000121 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400122 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000123
124 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000125
Jamie Madill8daaba12014-06-13 10:04:33 -0400126 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200127 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300128 mTextureFunctionHLSL = new TextureFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400129
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200130 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000131 {
Arun Patole63419392015-03-13 11:51:07 +0530132 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
133 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
134 // In both cases total 3 uniform registers need to be reserved.
135 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000136 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000137
Geoff Lang00140f42016-02-03 18:47:33 +0000138 // Reserve registers for the default uniform block and driver constants
139 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140}
141
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000142OutputHLSL::~OutputHLSL()
143{
Jamie Madill8daaba12014-06-13 10:04:33 -0400144 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400145 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300146 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200147 for (auto &eqFunction : mStructEqualityFunctions)
148 {
149 SafeDelete(eqFunction);
150 }
151 for (auto &eqFunction : mArrayEqualityFunctions)
152 {
153 SafeDelete(eqFunction);
154 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000155}
156
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200157void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000158{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200159 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400160 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000161
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200162 BuiltInFunctionEmulator builtInFunctionEmulator;
163 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200164 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500165
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700166 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700167 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
168 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300169 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700170 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700171
Jamie Madill37997142015-01-28 10:06:34 -0500172 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500173 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200174 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500175 mInfoSinkStack.pop();
176
Jamie Madill37997142015-01-28 10:06:34 -0500177 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500178 mInfoSinkStack.pop();
179
Jamie Madill32aab012015-01-27 14:12:26 -0500180 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500181 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500182 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000183
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200184 objSink << mHeader.c_str();
185 objSink << mBody.c_str();
186 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200187
188 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000189}
190
Jamie Madill570e04d2013-06-21 09:15:33 -0400191void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
192{
193 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
194 {
195 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
196
Jamie Madill32aab012015-01-27 14:12:26 -0500197 TInfoSinkBase structInfoSink;
198 mInfoSinkStack.push(&structInfoSink);
199
Jamie Madill570e04d2013-06-21 09:15:33 -0400200 // This will mark the necessary block elements as referenced
201 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500202
203 TString structName(structInfoSink.c_str());
204 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400205
206 mFlaggedStructOriginalNames[flaggedNode] = structName;
207
208 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
209 {
210 structName.erase(pos, 1);
211 }
212
213 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
214 }
215}
216
Jamie Madill4e1fd412014-07-10 17:50:10 -0400217const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
218{
219 return mUniformHLSL->getInterfaceBlockRegisterMap();
220}
221
Jamie Madill9fe25e92014-07-18 10:33:08 -0400222const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
223{
224 return mUniformHLSL->getUniformRegisterMap();
225}
226
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000227int OutputHLSL::vectorSize(const TType &type) const
228{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000229 int elementSize = type.isMatrix() ? type.getCols() : 1;
Olli Etuaho856c4972016-08-08 11:38:39 +0300230 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000231
232 return elementSize * arraySize;
233}
234
Jamie Madill98493dd2013-07-08 14:39:03 -0400235TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400236{
237 TString init;
238
239 TString preIndentString;
240 TString fullIndentString;
241
242 for (int spaces = 0; spaces < (indent * 4); spaces++)
243 {
244 preIndentString += ' ';
245 }
246
247 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
248 {
249 fullIndentString += ' ';
250 }
251
252 init += preIndentString + "{\n";
253
Jamie Madill98493dd2013-07-08 14:39:03 -0400254 const TFieldList &fields = structure.fields();
255 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400256 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400257 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400258 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400259 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400260
Jamie Madill98493dd2013-07-08 14:39:03 -0400261 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400262 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400264 }
265 else
266 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400268 }
269 }
270
271 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
272
273 return init;
274}
275
Jamie Madill8c46ab12015-12-07 16:39:19 -0500276void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000278 TString varyings;
279 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000281
Jamie Madill829f59e2013-11-13 19:40:54 -0500282 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400283 {
284 TIntermTyped *structNode = flaggedStructIt->first;
285 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400287 const TString &originalName = mFlaggedStructOriginalNames[structNode];
288
Jamie Madill033dae62014-06-18 12:56:28 -0400289 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400290 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400291 flaggedStructs += "\n";
292 }
293
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000294 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
295 {
296 const TType &type = varying->second->getType();
297 const TString &name = varying->second->getSymbol();
298
299 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400300 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
301 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000302 }
303
304 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
305 {
306 const TType &type = attribute->second->getType();
307 const TString &name = attribute->second->getSymbol();
308
Jamie Madill033dae62014-06-18 12:56:28 -0400309 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000310 }
311
Jamie Madill8daaba12014-06-13 10:04:33 -0400312 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400313
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200314 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400315 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
316
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200317 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500318 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200319 out << "\n// Equality functions\n\n";
320 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500321 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200322 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200323 }
324 }
Olli Etuaho12690762015-03-31 12:55:28 +0300325 if (!mArrayAssignmentFunctions.empty())
326 {
327 out << "\n// Assignment functions\n\n";
328 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
329 {
330 out << assignmentFunction.functionDefinition << "\n";
331 }
332 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300333 if (!mArrayConstructIntoFunctions.empty())
334 {
335 out << "\n// Array constructor functions\n\n";
336 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
337 {
338 out << constructIntoFunction.functionDefinition << "\n";
339 }
340 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200341
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500342 if (mUsesDiscardRewriting)
343 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400344 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500345 }
346
Nicolas Capens655fe362014-04-11 13:12:34 -0400347 if (mUsesNestedBreak)
348 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400349 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400350 }
351
Arun Patole44efa0b2015-03-04 17:11:05 +0530352 if (mRequiresIEEEStrictCompiling)
353 {
354 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
355 }
356
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400357 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
358 "#define LOOP [loop]\n"
359 "#define FLATTEN [flatten]\n"
360 "#else\n"
361 "#define LOOP\n"
362 "#define FLATTEN\n"
363 "#endif\n";
364
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200365 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200367 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
368 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000369
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000370 out << "// Varyings\n";
371 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400372 out << "\n";
373
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200374 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000375 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500376 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000377 {
Jamie Madill46131a32013-06-20 11:55:50 -0400378 const TString &variableName = outputVariableIt->first;
379 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400380
Jamie Madill033dae62014-06-18 12:56:28 -0400381 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400382 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000383 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000384 }
Jamie Madill46131a32013-06-20 11:55:50 -0400385 else
386 {
387 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
388
389 out << "static float4 gl_Color[" << numColorValues << "] =\n"
390 "{\n";
391 for (unsigned int i = 0; i < numColorValues; i++)
392 {
393 out << " float4(0, 0, 0, 0)";
394 if (i + 1 != numColorValues)
395 {
396 out << ",";
397 }
398 out << "\n";
399 }
400
401 out << "};\n";
402 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000403
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400404 if (mUsesFragDepth)
405 {
406 out << "static float gl_Depth = 0.0;\n";
407 }
408
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000409 if (mUsesFragCoord)
410 {
411 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
412 }
413
414 if (mUsesPointCoord)
415 {
416 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
417 }
418
419 if (mUsesFrontFacing)
420 {
421 out << "static bool gl_FrontFacing = false;\n";
422 }
423
424 out << "\n";
425
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000426 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000427 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000428 out << "struct gl_DepthRangeParameters\n"
429 "{\n"
430 " float near;\n"
431 " float far;\n"
432 " float diff;\n"
433 "};\n"
434 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000435 }
436
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200437 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000438 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000439 out << "cbuffer DriverConstants : register(b1)\n"
440 "{\n";
441
442 if (mUsesDepthRange)
443 {
444 out << " float3 dx_DepthRange : packoffset(c0);\n";
445 }
446
447 if (mUsesFragCoord)
448 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000449 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000450 }
451
452 if (mUsesFragCoord || mUsesFrontFacing)
453 {
454 out << " float3 dx_DepthFront : packoffset(c2);\n";
455 }
456
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800457 if (mUsesFragCoord)
458 {
459 // dx_ViewScale is only used in the fragment shader to correct
460 // the value for glFragCoord if necessary
461 out << " float2 dx_ViewScale : packoffset(c3);\n";
462 }
463
Olli Etuaho618bebc2016-01-15 16:40:00 +0200464 if (mOutputType == SH_HLSL_4_1_OUTPUT)
465 {
466 mUniformHLSL->samplerMetadataUniforms(out, "c4");
467 }
468
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000469 out << "};\n";
470 }
471 else
472 {
473 if (mUsesDepthRange)
474 {
475 out << "uniform float3 dx_DepthRange : register(c0);";
476 }
477
478 if (mUsesFragCoord)
479 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000480 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000481 }
482
483 if (mUsesFragCoord || mUsesFrontFacing)
484 {
485 out << "uniform float3 dx_DepthFront : register(c2);\n";
486 }
487 }
488
489 out << "\n";
490
491 if (mUsesDepthRange)
492 {
493 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
494 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000495 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000496
Jamie Madillf91ce812014-06-13 10:04:34 -0400497 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000498 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400499 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000500 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400501 out << flaggedStructs;
502 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000503 }
504
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000505 if (usingMRTExtension && mNumRenderTargets > 1)
506 {
507 out << "#define GL_USES_MRT\n";
508 }
509
510 if (mUsesFragColor)
511 {
512 out << "#define GL_USES_FRAG_COLOR\n";
513 }
514
515 if (mUsesFragData)
516 {
517 out << "#define GL_USES_FRAG_DATA\n";
518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000520 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000522 out << "// Attributes\n";
523 out << attributes;
524 out << "\n"
525 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400526
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000527 if (mUsesPointSize)
528 {
529 out << "static float gl_PointSize = float(1);\n";
530 }
531
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000532 if (mUsesInstanceID)
533 {
534 out << "static int gl_InstanceID;";
535 }
536
Corentin Wallezb076add2016-01-11 16:45:46 -0500537 if (mUsesVertexID)
538 {
539 out << "static int gl_VertexID;";
540 }
541
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000542 out << "\n"
543 "// Varyings\n";
544 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000545 out << "\n";
546
547 if (mUsesDepthRange)
548 {
549 out << "struct gl_DepthRangeParameters\n"
550 "{\n"
551 " float near;\n"
552 " float far;\n"
553 " float diff;\n"
554 "};\n"
555 "\n";
556 }
557
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200558 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000559 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800560 out << "cbuffer DriverConstants : register(b1)\n"
561 "{\n";
562
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000563 if (mUsesDepthRange)
564 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800565 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000566 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800567
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800568 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
569 // shaders. However, we declare it for all shaders (including Feature Level 10+).
570 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
571 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800572 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800573 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800574 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800575
Olli Etuaho618bebc2016-01-15 16:40:00 +0200576 if (mOutputType == SH_HLSL_4_1_OUTPUT)
577 {
578 mUniformHLSL->samplerMetadataUniforms(out, "c4");
579 }
580
Austin Kinross4fd18b12014-12-22 12:32:05 -0800581 out << "};\n"
582 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000583 }
584 else
585 {
586 if (mUsesDepthRange)
587 {
588 out << "uniform float3 dx_DepthRange : register(c0);\n";
589 }
590
Cooper Partine6664f02015-01-09 16:22:24 -0800591 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
592 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000593 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000594 }
595
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000596 if (mUsesDepthRange)
597 {
598 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
599 "\n";
600 }
601
Jamie Madillf91ce812014-06-13 10:04:34 -0400602 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000603 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400604 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000605 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400606 out << flaggedStructs;
607 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400609 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000610
Geoff Lang1fe74c72016-08-25 13:23:01 -0400611 bool getDimensionsIgnoresBaseLevel =
612 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
613 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000615 if (mUsesFragCoord)
616 {
617 out << "#define GL_USES_FRAG_COORD\n";
618 }
619
620 if (mUsesPointCoord)
621 {
622 out << "#define GL_USES_POINT_COORD\n";
623 }
624
625 if (mUsesFrontFacing)
626 {
627 out << "#define GL_USES_FRONT_FACING\n";
628 }
629
630 if (mUsesPointSize)
631 {
632 out << "#define GL_USES_POINT_SIZE\n";
633 }
634
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400635 if (mUsesFragDepth)
636 {
637 out << "#define GL_USES_FRAG_DEPTH\n";
638 }
639
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000640 if (mUsesDepthRange)
641 {
642 out << "#define GL_USES_DEPTH_RANGE\n";
643 }
644
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000645 if (mUsesXor)
646 {
647 out << "bool xor(bool p, bool q)\n"
648 "{\n"
649 " return (p || q) && !(p && q);\n"
650 "}\n"
651 "\n";
652 }
653
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200654 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655}
656
657void OutputHLSL::visitSymbol(TIntermSymbol *node)
658{
Jamie Madill32aab012015-01-27 14:12:26 -0500659 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660
Jamie Madill570e04d2013-06-21 09:15:33 -0400661 // Handle accessing std140 structs by value
662 if (mFlaggedStructMappedNames.count(node) > 0)
663 {
664 out << mFlaggedStructMappedNames[node];
665 return;
666 }
667
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668 TString name = node->getSymbol();
669
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000670 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000671 {
672 mUsesDepthRange = true;
673 out << name;
674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675 else
676 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000677 TQualifier qualifier = node->getQualifier();
678
679 if (qualifier == EvqUniform)
680 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400681 const TType &nodeType = node->getType();
682 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400683
684 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000685 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400686 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000687 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000688 else
689 {
690 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000691 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400692
Jamie Madill2e295e22015-04-29 10:41:33 -0400693 ensureStructDefined(nodeType);
694
Olli Etuaho96963162016-03-21 11:54:33 +0200695 const TName &nameWithMetadata = node->getName();
696 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000697 }
Jamie Madill19571812013-08-12 15:26:34 -0700698 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000699 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000700 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400701 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000702 }
Jamie Madill033dae62014-06-18 12:56:28 -0400703 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000704 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000705 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400706 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000707 }
Jamie Madill19571812013-08-12 15:26:34 -0700708 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400709 {
710 mReferencedOutputVariables[name] = node;
711 out << "out_" << name;
712 }
713 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000714 {
715 out << "gl_Color[0]";
716 mUsesFragColor = true;
717 }
718 else if (qualifier == EvqFragData)
719 {
720 out << "gl_Color";
721 mUsesFragData = true;
722 }
723 else if (qualifier == EvqFragCoord)
724 {
725 mUsesFragCoord = true;
726 out << name;
727 }
728 else if (qualifier == EvqPointCoord)
729 {
730 mUsesPointCoord = true;
731 out << name;
732 }
733 else if (qualifier == EvqFrontFacing)
734 {
735 mUsesFrontFacing = true;
736 out << name;
737 }
738 else if (qualifier == EvqPointSize)
739 {
740 mUsesPointSize = true;
741 out << name;
742 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000743 else if (qualifier == EvqInstanceID)
744 {
745 mUsesInstanceID = true;
746 out << name;
747 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500748 else if (qualifier == EvqVertexID)
749 {
750 mUsesVertexID = true;
751 out << name;
752 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300753 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400754 {
755 mUsesFragDepth = true;
756 out << "gl_Depth";
757 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000758 else
759 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300760 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000761 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762 }
763}
764
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400765void OutputHLSL::visitRaw(TIntermRaw *node)
766{
Jamie Madill32aab012015-01-27 14:12:26 -0500767 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400768}
769
Olli Etuaho7fb49552015-03-18 17:27:44 +0200770void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
771{
772 if (type.isScalar() && !type.isArray())
773 {
774 if (op == EOpEqual)
775 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500776 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200777 }
778 else
779 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500780 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200781 }
782 }
783 else
784 {
785 if (visit == PreVisit && op == EOpNotEqual)
786 {
787 out << "!";
788 }
789
790 if (type.isArray())
791 {
792 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500793 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200794 }
795 else if (type.getBasicType() == EbtStruct)
796 {
797 const TStructure &structure = *type.getStruct();
798 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500799 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200800 }
801 else
802 {
803 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500804 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200805 }
806 }
807}
808
Olli Etuaho96963162016-03-21 11:54:33 +0200809bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
810{
811 // Inside InVisit the current node is already in the path.
812 const unsigned int initialN = visit == InVisit ? 1u : 0u;
813 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
814 {
815 TIntermNode *ancestor = getAncestorNode(n);
816 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
817 if (ancestorBinary == nullptr)
818 {
819 return false;
820 }
821 switch (ancestorBinary->getOp())
822 {
823 case EOpIndexDirectStruct:
824 {
825 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
826 const TIntermConstantUnion *index =
827 ancestorBinary->getRight()->getAsConstantUnion();
828 const TField *field = structure->fields()[index->getIConst(0)];
829 if (IsSampler(field->type()->getBasicType()))
830 {
831 return true;
832 }
833 break;
834 }
835 case EOpIndexDirect:
836 break;
837 default:
838 // Returning a sampler from indirect indexing is not supported.
839 return false;
840 }
841 }
842 return false;
843}
844
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
846{
Jamie Madill32aab012015-01-27 14:12:26 -0500847 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848
Jamie Madill570e04d2013-06-21 09:15:33 -0400849 // Handle accessing std140 structs by value
850 if (mFlaggedStructMappedNames.count(node) > 0)
851 {
852 out << mFlaggedStructMappedNames[node];
853 return false;
854 }
855
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856 switch (node->getOp())
857 {
Olli Etuahoe79904c2015-03-18 16:56:42 +0200858 case EOpAssign:
859 if (node->getLeft()->isArray())
860 {
Olli Etuaho9638c352015-04-01 14:34:52 +0300861 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
862 if (rightAgg != nullptr && rightAgg->isConstructor())
863 {
864 const TString &functionName = addArrayConstructIntoFunction(node->getType());
865 out << functionName << "(";
866 node->getLeft()->traverse(this);
867 TIntermSequence *seq = rightAgg->getSequence();
868 for (auto &arrayElement : *seq)
869 {
870 out << ", ";
871 arrayElement->traverse(this);
872 }
873 out << ")";
874 return false;
875 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +0300876 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
877 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
878
879 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500880 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200881 }
882 else
883 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500884 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200885 }
886 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000887 case EOpInitialize:
888 if (visit == PreVisit)
889 {
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000890 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -0500891 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000892 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000893
Olli Etuaho3d932d82016-04-12 11:10:30 +0300894 // Global initializers must be constant at this point.
Olli Etuahod4f4c112016-04-15 15:11:24 +0300895 ASSERT(symbolNode->getQualifier() != EvqGlobal || canWriteAsHLSLLiteral(expression));
896
897 // GLSL allows to write things like "float x = x;" where a new variable x is defined
898 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
899 // new variable is created before the assignment is evaluated), so we need to convert
900 // this to "float t = x, x = t;".
Olli Etuaho3d932d82016-04-12 11:10:30 +0300901 if (writeSameSymbolInitializer(out, symbolNode, expression))
Jamie Madill37997142015-01-28 10:06:34 -0500902 {
903 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000904 return false;
905 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200906 else if (writeConstantInitialization(out, symbolNode, expression))
907 {
908 return false;
909 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000910 }
911 else if (visit == InVisit)
912 {
913 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000914 }
915 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500916 case EOpAddAssign:
917 outputTriplet(out, visit, "(", " += ", ")");
918 break;
919 case EOpSubAssign:
920 outputTriplet(out, visit, "(", " -= ", ")");
921 break;
922 case EOpMulAssign:
923 outputTriplet(out, visit, "(", " *= ", ")");
924 break;
925 case EOpVectorTimesScalarAssign:
926 outputTriplet(out, visit, "(", " *= ", ")");
927 break;
928 case EOpMatrixTimesScalarAssign:
929 outputTriplet(out, visit, "(", " *= ", ")");
930 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000931 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000932 if (visit == PreVisit)
933 {
934 out << "(";
935 }
936 else if (visit == InVisit)
937 {
938 out << " = mul(";
939 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -0400940 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000941 }
942 else
943 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000944 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000945 }
946 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000947 case EOpMatrixTimesMatrixAssign:
948 if (visit == PreVisit)
949 {
950 out << "(";
951 }
952 else if (visit == InVisit)
953 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200954 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000955 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +0200956 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000957 }
958 else
959 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200960 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000961 }
962 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500963 case EOpDivAssign:
964 outputTriplet(out, visit, "(", " /= ", ")");
965 break;
966 case EOpIModAssign:
967 outputTriplet(out, visit, "(", " %= ", ")");
968 break;
969 case EOpBitShiftLeftAssign:
970 outputTriplet(out, visit, "(", " <<= ", ")");
971 break;
972 case EOpBitShiftRightAssign:
973 outputTriplet(out, visit, "(", " >>= ", ")");
974 break;
975 case EOpBitwiseAndAssign:
976 outputTriplet(out, visit, "(", " &= ", ")");
977 break;
978 case EOpBitwiseXorAssign:
979 outputTriplet(out, visit, "(", " ^= ", ")");
980 break;
981 case EOpBitwiseOrAssign:
982 outputTriplet(out, visit, "(", " |= ", ")");
983 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -0400984 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -0400985 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400986 const TType& leftType = node->getLeft()->getType();
987 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -0400988 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400989 if (visit == PreVisit)
990 {
991 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
992 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -0400993 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -0400994 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -0400995 return false;
996 }
Jamie Madillb4e664b2013-06-20 11:55:54 -0400997 }
Olli Etuaho96963162016-03-21 11:54:33 +0200998 else if (ancestorEvaluatesToSamplerInStruct(visit))
999 {
1000 // All parts of an expression that access a sampler in a struct need to use _ as
1001 // separator to access the sampler variable that has been moved out of the struct.
1002 outputTriplet(out, visit, "", "_", "");
1003 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001004 else
1005 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001006 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001007 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001008 }
1009 break;
1010 case EOpIndexIndirect:
1011 // We do not currently support indirect references to interface blocks
1012 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001013 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001014 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001015 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001016 {
1017 const TStructure* structure = node->getLeft()->getType().getStruct();
1018 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1019 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001020
Olli Etuaho96963162016-03-21 11:54:33 +02001021 // In cases where indexing returns a sampler, we need to access the sampler variable
1022 // that has been moved out of the struct.
1023 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1024 if (visit == PreVisit && indexingReturnsSampler)
1025 {
1026 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1027 // This prefix is only output at the beginning of the indexing expression, which
1028 // may have multiple parts.
1029 out << "angle";
1030 }
1031 if (!indexingReturnsSampler)
1032 {
1033 // All parts of an expression that access a sampler in a struct need to use _ as
1034 // separator to access the sampler variable that has been moved out of the struct.
1035 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1036 }
1037 if (visit == InVisit)
1038 {
1039 if (indexingReturnsSampler)
1040 {
1041 out << "_" + field->name();
1042 }
1043 else
1044 {
1045 out << "." + DecorateField(field->name(), *structure);
1046 }
1047
1048 return false;
1049 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001050 }
1051 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001052 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001053 if (visit == InVisit)
1054 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001055 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1056 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1057 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001058 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001059
1060 return false;
1061 }
1062 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063 case EOpVectorSwizzle:
1064 if (visit == InVisit)
1065 {
1066 out << ".";
1067
1068 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1069
1070 if (swizzle)
1071 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001072 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001074 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075 {
1076 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1077
1078 if (element)
1079 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001080 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081
1082 switch (i)
1083 {
1084 case 0: out << "x"; break;
1085 case 1: out << "y"; break;
1086 case 2: out << "z"; break;
1087 case 3: out << "w"; break;
1088 default: UNREACHABLE();
1089 }
1090 }
1091 else UNREACHABLE();
1092 }
1093 }
1094 else UNREACHABLE();
1095
1096 return false; // Fully processed
1097 }
1098 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001099 case EOpAdd:
1100 outputTriplet(out, visit, "(", " + ", ")");
1101 break;
1102 case EOpSub:
1103 outputTriplet(out, visit, "(", " - ", ")");
1104 break;
1105 case EOpMul:
1106 outputTriplet(out, visit, "(", " * ", ")");
1107 break;
1108 case EOpDiv:
1109 outputTriplet(out, visit, "(", " / ", ")");
1110 break;
1111 case EOpIMod:
1112 outputTriplet(out, visit, "(", " % ", ")");
1113 break;
1114 case EOpBitShiftLeft:
1115 outputTriplet(out, visit, "(", " << ", ")");
1116 break;
1117 case EOpBitShiftRight:
1118 outputTriplet(out, visit, "(", " >> ", ")");
1119 break;
1120 case EOpBitwiseAnd:
1121 outputTriplet(out, visit, "(", " & ", ")");
1122 break;
1123 case EOpBitwiseXor:
1124 outputTriplet(out, visit, "(", " ^ ", ")");
1125 break;
1126 case EOpBitwiseOr:
1127 outputTriplet(out, visit, "(", " | ", ")");
1128 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001129 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001130 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001131 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001132 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001133 case EOpLessThan:
1134 outputTriplet(out, visit, "(", " < ", ")");
1135 break;
1136 case EOpGreaterThan:
1137 outputTriplet(out, visit, "(", " > ", ")");
1138 break;
1139 case EOpLessThanEqual:
1140 outputTriplet(out, visit, "(", " <= ", ")");
1141 break;
1142 case EOpGreaterThanEqual:
1143 outputTriplet(out, visit, "(", " >= ", ")");
1144 break;
1145 case EOpVectorTimesScalar:
1146 outputTriplet(out, visit, "(", " * ", ")");
1147 break;
1148 case EOpMatrixTimesScalar:
1149 outputTriplet(out, visit, "(", " * ", ")");
1150 break;
1151 case EOpVectorTimesMatrix:
1152 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1153 break;
1154 case EOpMatrixTimesVector:
1155 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1156 break;
1157 case EOpMatrixTimesMatrix:
1158 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1159 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001160 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001161 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1162 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001163 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001164 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001165 case EOpLogicalXor:
1166 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001167 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001168 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001169 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001170 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1171 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001172 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001173 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001174 default: UNREACHABLE();
1175 }
1176
1177 return true;
1178}
1179
1180bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1181{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001182 TInfoSinkBase &out = getInfoSink();
1183
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001184 switch (node->getOp())
1185 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001186 case EOpNegative:
1187 outputTriplet(out, visit, "(-", "", ")");
1188 break;
1189 case EOpPositive:
1190 outputTriplet(out, visit, "(+", "", ")");
1191 break;
1192 case EOpVectorLogicalNot:
1193 outputTriplet(out, visit, "(!", "", ")");
1194 break;
1195 case EOpLogicalNot:
1196 outputTriplet(out, visit, "(!", "", ")");
1197 break;
1198 case EOpBitwiseNot:
1199 outputTriplet(out, visit, "(~", "", ")");
1200 break;
1201 case EOpPostIncrement:
1202 outputTriplet(out, visit, "(", "", "++)");
1203 break;
1204 case EOpPostDecrement:
1205 outputTriplet(out, visit, "(", "", "--)");
1206 break;
1207 case EOpPreIncrement:
1208 outputTriplet(out, visit, "(++", "", ")");
1209 break;
1210 case EOpPreDecrement:
1211 outputTriplet(out, visit, "(--", "", ")");
1212 break;
1213 case EOpRadians:
1214 outputTriplet(out, visit, "radians(", "", ")");
1215 break;
1216 case EOpDegrees:
1217 outputTriplet(out, visit, "degrees(", "", ")");
1218 break;
1219 case EOpSin:
1220 outputTriplet(out, visit, "sin(", "", ")");
1221 break;
1222 case EOpCos:
1223 outputTriplet(out, visit, "cos(", "", ")");
1224 break;
1225 case EOpTan:
1226 outputTriplet(out, visit, "tan(", "", ")");
1227 break;
1228 case EOpAsin:
1229 outputTriplet(out, visit, "asin(", "", ")");
1230 break;
1231 case EOpAcos:
1232 outputTriplet(out, visit, "acos(", "", ")");
1233 break;
1234 case EOpAtan:
1235 outputTriplet(out, visit, "atan(", "", ")");
1236 break;
1237 case EOpSinh:
1238 outputTriplet(out, visit, "sinh(", "", ")");
1239 break;
1240 case EOpCosh:
1241 outputTriplet(out, visit, "cosh(", "", ")");
1242 break;
1243 case EOpTanh:
1244 outputTriplet(out, visit, "tanh(", "", ")");
1245 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001246 case EOpAsinh:
1247 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001248 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001249 break;
1250 case EOpAcosh:
1251 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001252 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001253 break;
1254 case EOpAtanh:
1255 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001256 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001257 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001258 case EOpExp:
1259 outputTriplet(out, visit, "exp(", "", ")");
1260 break;
1261 case EOpLog:
1262 outputTriplet(out, visit, "log(", "", ")");
1263 break;
1264 case EOpExp2:
1265 outputTriplet(out, visit, "exp2(", "", ")");
1266 break;
1267 case EOpLog2:
1268 outputTriplet(out, visit, "log2(", "", ")");
1269 break;
1270 case EOpSqrt:
1271 outputTriplet(out, visit, "sqrt(", "", ")");
1272 break;
1273 case EOpInverseSqrt:
1274 outputTriplet(out, visit, "rsqrt(", "", ")");
1275 break;
1276 case EOpAbs:
1277 outputTriplet(out, visit, "abs(", "", ")");
1278 break;
1279 case EOpSign:
1280 outputTriplet(out, visit, "sign(", "", ")");
1281 break;
1282 case EOpFloor:
1283 outputTriplet(out, visit, "floor(", "", ")");
1284 break;
1285 case EOpTrunc:
1286 outputTriplet(out, visit, "trunc(", "", ")");
1287 break;
1288 case EOpRound:
1289 outputTriplet(out, visit, "round(", "", ")");
1290 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001291 case EOpRoundEven:
1292 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001293 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001294 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001295 case EOpCeil:
1296 outputTriplet(out, visit, "ceil(", "", ")");
1297 break;
1298 case EOpFract:
1299 outputTriplet(out, visit, "frac(", "", ")");
1300 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301301 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001302 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05301303 mRequiresIEEEStrictCompiling = true;
1304 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001305 case EOpIsInf:
1306 outputTriplet(out, visit, "isinf(", "", ")");
1307 break;
1308 case EOpFloatBitsToInt:
1309 outputTriplet(out, visit, "asint(", "", ")");
1310 break;
1311 case EOpFloatBitsToUint:
1312 outputTriplet(out, visit, "asuint(", "", ")");
1313 break;
1314 case EOpIntBitsToFloat:
1315 outputTriplet(out, visit, "asfloat(", "", ")");
1316 break;
1317 case EOpUintBitsToFloat:
1318 outputTriplet(out, visit, "asfloat(", "", ")");
1319 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001320 case EOpPackSnorm2x16:
1321 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001322 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001323 break;
1324 case EOpPackUnorm2x16:
1325 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001326 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001327 break;
1328 case EOpPackHalf2x16:
1329 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001330 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001331 break;
1332 case EOpUnpackSnorm2x16:
1333 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001334 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001335 break;
1336 case EOpUnpackUnorm2x16:
1337 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001338 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001339 break;
1340 case EOpUnpackHalf2x16:
1341 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001342 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001343 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001344 case EOpLength:
1345 outputTriplet(out, visit, "length(", "", ")");
1346 break;
1347 case EOpNormalize:
1348 outputTriplet(out, visit, "normalize(", "", ")");
1349 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001350 case EOpDFdx:
1351 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1352 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001353 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001354 }
1355 else
1356 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001357 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001358 }
1359 break;
1360 case EOpDFdy:
1361 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1362 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001363 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001364 }
1365 else
1366 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001367 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001368 }
1369 break;
1370 case EOpFwidth:
1371 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1372 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001373 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001374 }
1375 else
1376 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001377 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001378 }
1379 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001380 case EOpTranspose:
1381 outputTriplet(out, visit, "transpose(", "", ")");
1382 break;
1383 case EOpDeterminant:
1384 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1385 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001386 case EOpInverse:
1387 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001388 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001389 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001390
Jamie Madill8c46ab12015-12-07 16:39:19 -05001391 case EOpAny:
1392 outputTriplet(out, visit, "any(", "", ")");
1393 break;
1394 case EOpAll:
1395 outputTriplet(out, visit, "all(", "", ")");
1396 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001397 default: UNREACHABLE();
1398 }
1399
1400 return true;
1401}
1402
Olli Etuaho96963162016-03-21 11:54:33 +02001403TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1404{
1405 if (node->getAsSymbolNode())
1406 {
1407 return node->getAsSymbolNode()->getSymbol();
1408 }
1409 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1410 switch (nodeBinary->getOp())
1411 {
1412 case EOpIndexDirect:
1413 {
1414 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1415
1416 TInfoSinkBase prefixSink;
1417 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1418 return TString(prefixSink.c_str());
1419 }
1420 case EOpIndexDirectStruct:
1421 {
1422 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1423 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1424 const TField *field = s->fields()[index];
1425
1426 TInfoSinkBase prefixSink;
1427 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1428 << field->name();
1429 return TString(prefixSink.c_str());
1430 }
1431 default:
1432 UNREACHABLE();
1433 return TString("");
1434 }
1435}
1436
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001437bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1438{
Jamie Madill32aab012015-01-27 14:12:26 -05001439 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001440
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001441 switch (node->getOp())
1442 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001443 case EOpSequence:
1444 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001445 if (mInsideFunction)
1446 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001447 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001448 out << "{\n";
1449 }
1450
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001451 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001452 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001453 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001454
Olli Etuahoa6f22092015-05-08 18:31:10 +03001455 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001456
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001457 // Don't output ; after case labels, they're terminated by :
1458 // This is needed especially since outputting a ; after a case statement would turn empty
1459 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03001460 // Also no need to output ; after selection (if) statements or sequences. This is done just
1461 // for code clarity.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001462 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsSelectionNode() == nullptr &&
1463 !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001464 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001465 }
1466
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001467 if (mInsideFunction)
1468 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001469 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001470 out << "}\n";
1471 }
1472
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001473 return false;
1474 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001475 case EOpDeclaration:
1476 if (visit == PreVisit)
1477 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001478 TIntermSequence *sequence = node->getSequence();
1479 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001480 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001481
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001482 if (variable &&
1483 (variable->getQualifier() == EvqTemporary ||
1484 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001485 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001486 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001487
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001488 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001489 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001490 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001491 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001492 out << "static ";
1493 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001494
Olli Etuahoa6f22092015-05-08 18:31:10 +03001495 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001496
Olli Etuahoa6f22092015-05-08 18:31:10 +03001497 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001498
Olli Etuahoa6f22092015-05-08 18:31:10 +03001499 if (symbol)
1500 {
1501 symbol->traverse(this);
1502 out << ArrayString(symbol->getType());
1503 out << " = " + initializer(symbol->getType());
1504 }
1505 else
1506 {
1507 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001508 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001509 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001510 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1511 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001512 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001513 }
1514 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001515 }
Jamie Madill033dae62014-06-18 12:56:28 -04001516 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001517 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001518 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001519 {
1520 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1521
1522 if (symbol)
1523 {
1524 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1525 mReferencedVaryings[symbol->getSymbol()] = symbol;
1526 }
1527 else
1528 {
1529 (*sit)->traverse(this);
1530 }
1531 }
1532 }
1533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001534 return false;
1535 }
1536 else if (visit == InVisit)
1537 {
1538 out << ", ";
1539 }
1540 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001541 case EOpInvariantDeclaration:
1542 // Do not do any translation
1543 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001544 case EOpPrototype:
1545 if (visit == PreVisit)
1546 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001547 size_t index = mCallDag.findIndex(node);
1548 // Skip the prototype if it is not implemented (and thus not used)
1549 if (index == CallDAG::InvalidIndex)
1550 {
1551 return false;
1552 }
1553
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001554 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001555
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001556 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1557 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1558 << (mOutputLod0Function ? "Lod0(" : "(");
1559
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001560 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001561 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001562 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001563
1564 if (symbol)
1565 {
1566 out << argumentString(symbol);
1567
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001568 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001569 {
1570 out << ", ";
1571 }
1572 }
1573 else UNREACHABLE();
1574 }
1575
1576 out << ");\n";
1577
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001578 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001579 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1580 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001581 {
1582 mOutputLod0Function = true;
1583 node->traverse(this);
1584 mOutputLod0Function = false;
1585 }
1586
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001587 return false;
1588 }
1589 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001590 case EOpComma:
1591 outputTriplet(out, visit, "(", ", ", ")");
1592 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593 case EOpFunction:
1594 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001595 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001596 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001597
Corentin Wallez1239ee92015-03-19 14:38:02 -07001598 size_t index = mCallDag.findIndex(node);
1599 ASSERT(index != CallDAG::InvalidIndex);
1600 mCurrentFunctionMetadata = &mASTMetadataList[index];
1601
Jamie Madill033dae62014-06-18 12:56:28 -04001602 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001603
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001604 TIntermSequence *sequence = node->getSequence();
1605 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
1606
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001607 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001608 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001609 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001610 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001611 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001612 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001613 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001614 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001615 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001616
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001617 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001618 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001619 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001620
1621 if (symbol)
1622 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001623 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001624
1625 out << argumentString(symbol);
1626
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001627 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001628 {
1629 out << ", ";
1630 }
1631 }
1632 else UNREACHABLE();
1633 }
1634
Olli Etuaho4785fec2015-05-18 16:09:37 +03001635 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001636
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001637 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001638 {
1639 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03001640 TIntermNode *body = (*sequence)[1];
1641 // The function body node will output braces.
1642 ASSERT(IsSequence(body));
1643 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001644 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001645 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001646 else
1647 {
1648 out << "{}\n";
1649 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001650
Corentin Wallez1239ee92015-03-19 14:38:02 -07001651 mCurrentFunctionMetadata = nullptr;
1652
1653 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1654 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001655 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001656 ASSERT(name != "main");
1657 mOutputLod0Function = true;
1658 node->traverse(this);
1659 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001660 }
1661
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001662 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663 }
1664 break;
1665 case EOpFunctionCall:
1666 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001667 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001668
Corentin Wallez1239ee92015-03-19 14:38:02 -07001669 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001670 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001672 if (node->isArray())
1673 {
1674 UNIMPLEMENTED();
1675 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07001676 size_t index = mCallDag.findIndex(node);
1677 ASSERT(index != CallDAG::InvalidIndex);
1678 lod0 &= mASTMetadataList[index].mNeedsLod0;
1679
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001680 out << DecorateFunctionIfNeeded(node->getNameObj());
1681 out << DisambiguateFunctionName(node->getSequence());
1682 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001683 }
Olli Etuahob741c762016-06-29 15:49:22 +03001684 else if (node->getNameObj().isInternal())
1685 {
1686 // This path is used for internal functions that don't have their definitions in the
1687 // AST, such as precision emulation functions.
1688 out << DecorateFunctionIfNeeded(node->getNameObj()) << "(";
1689 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690 else
1691 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001692 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001693 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001694 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1695 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1696 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1697 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001698 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001699
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001700 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001701 {
Olli Etuaho96963162016-03-21 11:54:33 +02001702 TIntermTyped *typedArg = (*arg)->getAsTyped();
1703 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001704 {
1705 out << "texture_";
1706 (*arg)->traverse(this);
1707 out << ", sampler_";
1708 }
1709
1710 (*arg)->traverse(this);
1711
Olli Etuaho96963162016-03-21 11:54:33 +02001712 if (typedArg->getType().isStructureContainingSamplers())
1713 {
1714 const TType &argType = typedArg->getType();
1715 TVector<TIntermSymbol *> samplerSymbols;
1716 TString structName = samplerNamePrefixFromStruct(typedArg);
1717 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001718 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001719 &samplerSymbols, nullptr);
1720 for (const TIntermSymbol *sampler : samplerSymbols)
1721 {
1722 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1723 {
1724 out << ", texture_" << sampler->getSymbol();
1725 out << ", sampler_" << sampler->getSymbol();
1726 }
1727 else
1728 {
1729 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1730 // of D3D9, it's the sampler variable.
1731 out << ", " + sampler->getSymbol();
1732 }
1733 }
1734 }
1735
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001736 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001737 {
1738 out << ", ";
1739 }
1740 }
1741
1742 out << ")";
1743
1744 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001745 }
1746 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001747 case EOpParameters:
1748 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1749 break;
1750 case EOpConstructFloat:
1751 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1752 break;
1753 case EOpConstructVec2:
1754 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1755 break;
1756 case EOpConstructVec3:
1757 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1758 break;
1759 case EOpConstructVec4:
1760 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1761 break;
1762 case EOpConstructBool:
1763 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1764 break;
1765 case EOpConstructBVec2:
1766 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1767 break;
1768 case EOpConstructBVec3:
1769 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1770 break;
1771 case EOpConstructBVec4:
1772 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1773 break;
1774 case EOpConstructInt:
1775 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1776 break;
1777 case EOpConstructIVec2:
1778 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1779 break;
1780 case EOpConstructIVec3:
1781 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1782 break;
1783 case EOpConstructIVec4:
1784 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1785 break;
1786 case EOpConstructUInt:
1787 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1788 break;
1789 case EOpConstructUVec2:
1790 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1791 break;
1792 case EOpConstructUVec3:
1793 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1794 break;
1795 case EOpConstructUVec4:
1796 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1797 break;
1798 case EOpConstructMat2:
1799 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1800 break;
1801 case EOpConstructMat2x3:
1802 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1803 break;
1804 case EOpConstructMat2x4:
1805 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1806 break;
1807 case EOpConstructMat3x2:
1808 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1809 break;
1810 case EOpConstructMat3:
1811 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1812 break;
1813 case EOpConstructMat3x4:
1814 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1815 break;
1816 case EOpConstructMat4x2:
1817 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1818 break;
1819 case EOpConstructMat4x3:
1820 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1821 break;
1822 case EOpConstructMat4:
1823 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1824 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001825 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001826 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001827 if (node->getType().isArray())
1828 {
1829 UNIMPLEMENTED();
1830 }
Jamie Madill033dae62014-06-18 12:56:28 -04001831 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001832 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001833 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001834 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001835 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001836 case EOpLessThan:
1837 outputTriplet(out, visit, "(", " < ", ")");
1838 break;
1839 case EOpGreaterThan:
1840 outputTriplet(out, visit, "(", " > ", ")");
1841 break;
1842 case EOpLessThanEqual:
1843 outputTriplet(out, visit, "(", " <= ", ")");
1844 break;
1845 case EOpGreaterThanEqual:
1846 outputTriplet(out, visit, "(", " >= ", ")");
1847 break;
1848 case EOpVectorEqual:
1849 outputTriplet(out, visit, "(", " == ", ")");
1850 break;
1851 case EOpVectorNotEqual:
1852 outputTriplet(out, visit, "(", " != ", ")");
1853 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001854 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001855 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001856 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001857 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001858 case EOpModf:
1859 outputTriplet(out, visit, "modf(", ", ", ")");
1860 break;
1861 case EOpPow:
1862 outputTriplet(out, visit, "pow(", ", ", ")");
1863 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001865 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02001866 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001867 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001868 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001869 case EOpMin:
1870 outputTriplet(out, visit, "min(", ", ", ")");
1871 break;
1872 case EOpMax:
1873 outputTriplet(out, visit, "max(", ", ", ")");
1874 break;
1875 case EOpClamp:
1876 outputTriplet(out, visit, "clamp(", ", ", ")");
1877 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301878 case EOpMix:
1879 {
1880 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1881 if (lastParamNode->getType().getBasicType() == EbtBool)
1882 {
1883 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1884 // so use emulated version.
1885 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001886 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301887 }
1888 else
1889 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001890 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301891 }
1892 }
1893 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001894 case EOpStep:
1895 outputTriplet(out, visit, "step(", ", ", ")");
1896 break;
1897 case EOpSmoothStep:
1898 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1899 break;
1900 case EOpDistance:
1901 outputTriplet(out, visit, "distance(", ", ", ")");
1902 break;
1903 case EOpDot:
1904 outputTriplet(out, visit, "dot(", ", ", ")");
1905 break;
1906 case EOpCross:
1907 outputTriplet(out, visit, "cross(", ", ", ")");
1908 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001909 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001910 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001911 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001912 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001913 case EOpReflect:
1914 outputTriplet(out, visit, "reflect(", ", ", ")");
1915 break;
1916 case EOpRefract:
1917 outputTriplet(out, visit, "refract(", ", ", ")");
1918 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001919 case EOpOuterProduct:
1920 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001921 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02001922 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001923 case EOpMul:
1924 outputTriplet(out, visit, "(", " * ", ")");
1925 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 default: UNREACHABLE();
1927 }
1928
1929 return true;
1930}
1931
Jamie Madill8c46ab12015-12-07 16:39:19 -05001932void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001934 out << "if (";
1935
1936 node->getCondition()->traverse(this);
1937
1938 out << ")\n";
1939
Jamie Madill8c46ab12015-12-07 16:39:19 -05001940 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001941
1942 bool discard = false;
1943
1944 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001946 // The trueBlock child node will output braces.
1947 ASSERT(IsSequence(node->getTrueBlock()));
1948
Olli Etuahoa6f22092015-05-08 18:31:10 +03001949 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001950
Olli Etuahoa6f22092015-05-08 18:31:10 +03001951 // Detect true discard
1952 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1953 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001954 else
1955 {
1956 // TODO(oetuaho): Check if the semicolon inside is necessary.
1957 // It's there as a result of conservative refactoring of the output.
1958 out << "{;}\n";
1959 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001960
Jamie Madill8c46ab12015-12-07 16:39:19 -05001961 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001962
Olli Etuahoa6f22092015-05-08 18:31:10 +03001963 if (node->getFalseBlock())
1964 {
1965 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001966
Jamie Madill8c46ab12015-12-07 16:39:19 -05001967 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968
Olli Etuaho4785fec2015-05-18 16:09:37 +03001969 // Either this is "else if" or the falseBlock child node will output braces.
1970 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
1971
Olli Etuahoa6f22092015-05-08 18:31:10 +03001972 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001973
Jamie Madill8c46ab12015-12-07 16:39:19 -05001974 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001975
Olli Etuahoa6f22092015-05-08 18:31:10 +03001976 // Detect false discard
1977 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1978 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001979
Olli Etuahoa6f22092015-05-08 18:31:10 +03001980 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001981 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001982 {
1983 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001984 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001985}
1986
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001987bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1988{
1989 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1990 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1991 UNREACHABLE();
1992 return false;
1993}
1994
Olli Etuahod81ed842015-05-12 12:46:35 +03001995bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1996{
1997 TInfoSinkBase &out = getInfoSink();
1998
Olli Etuaho3d932d82016-04-12 11:10:30 +03001999 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002000
2001 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002002 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002003 {
2004 out << "FLATTEN ";
2005 }
2006
Jamie Madill8c46ab12015-12-07 16:39:19 -05002007 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008
2009 return false;
2010}
2011
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002012bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002013{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002014 TInfoSinkBase &out = getInfoSink();
2015
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002016 if (node->getStatementList())
2017 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002018 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002019 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002020 // The curly braces get written when visiting the statementList aggregate
2021 }
2022 else
2023 {
2024 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002025 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002026 }
2027 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002028}
2029
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002030bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002031{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002032 TInfoSinkBase &out = getInfoSink();
2033
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002034 if (node->hasCondition())
2035 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002036 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002037 return true;
2038 }
2039 else
2040 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002041 out << "default:\n";
2042 return false;
2043 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002044}
2045
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2047{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002048 TInfoSinkBase &out = getInfoSink();
2049 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002050}
2051
2052bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2053{
Nicolas Capens655fe362014-04-11 13:12:34 -04002054 mNestedLoopDepth++;
2055
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002056 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002057 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002058 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002059
Jamie Madill8c46ab12015-12-07 16:39:19 -05002060 TInfoSinkBase &out = getInfoSink();
2061
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002062 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002063 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002064 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002065 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002066 mInsideDiscontinuousLoop = wasDiscontinuous;
2067 mNestedLoopDepth--;
2068
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002069 return false;
2070 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002071 }
2072
Corentin Wallez1239ee92015-03-19 14:38:02 -07002073 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002074 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002076 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002077
Jamie Madill8c46ab12015-12-07 16:39:19 -05002078 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 }
2080 else
2081 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002082 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002083
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 if (node->getInit())
2085 {
2086 node->getInit()->traverse(this);
2087 }
2088
2089 out << "; ";
2090
alokp@chromium.org52813552010-11-16 18:36:09 +00002091 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002093 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094 }
2095
2096 out << "; ";
2097
alokp@chromium.org52813552010-11-16 18:36:09 +00002098 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002100 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101 }
2102
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002103 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002104
Jamie Madill8c46ab12015-12-07 16:39:19 -05002105 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106 }
2107
2108 if (node->getBody())
2109 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002110 // The loop body node will output braces.
2111 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002112 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002114 else
2115 {
2116 // TODO(oetuaho): Check if the semicolon inside is necessary.
2117 // It's there as a result of conservative refactoring of the output.
2118 out << "{;}\n";
2119 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120
Jamie Madill8c46ab12015-12-07 16:39:19 -05002121 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122
alokp@chromium.org52813552010-11-16 18:36:09 +00002123 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002125 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126 out << "while(\n";
2127
alokp@chromium.org52813552010-11-16 18:36:09 +00002128 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129
daniel@transgaming.com73536982012-03-21 20:45:49 +00002130 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131 }
2132
daniel@transgaming.com73536982012-03-21 20:45:49 +00002133 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002135 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002136 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002137
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002138 return false;
2139}
2140
2141bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2142{
Jamie Madill32aab012015-01-27 14:12:26 -05002143 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144
2145 switch (node->getFlowOp())
2146 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002147 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002148 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002149 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002150 case EOpBreak:
2151 if (visit == PreVisit)
2152 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002153 if (mNestedLoopDepth > 1)
2154 {
2155 mUsesNestedBreak = true;
2156 }
2157
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002158 if (mExcessiveLoopIndex)
2159 {
2160 out << "{Break";
2161 mExcessiveLoopIndex->traverse(this);
2162 out << " = true; break;}\n";
2163 }
2164 else
2165 {
2166 out << "break;\n";
2167 }
2168 }
2169 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002170 case EOpContinue:
2171 outputTriplet(out, visit, "continue;\n", "", "");
2172 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 case EOpReturn:
2174 if (visit == PreVisit)
2175 {
2176 if (node->getExpression())
2177 {
2178 out << "return ";
2179 }
2180 else
2181 {
2182 out << "return;\n";
2183 }
2184 }
2185 else if (visit == PostVisit)
2186 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002187 if (node->getExpression())
2188 {
2189 out << ";\n";
2190 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002191 }
2192 break;
2193 default: UNREACHABLE();
2194 }
2195
2196 return true;
2197}
2198
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002199bool OutputHLSL::isSingleStatement(TIntermNode *node)
2200{
2201 TIntermAggregate *aggregate = node->getAsAggregate();
2202
2203 if (aggregate)
2204 {
2205 if (aggregate->getOp() == EOpSequence)
2206 {
2207 return false;
2208 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002209 else if (aggregate->getOp() == EOpDeclaration)
2210 {
2211 // Declaring multiple comma-separated variables must be considered multiple statements
2212 // because each individual declaration has side effects which are visible in the next.
2213 return false;
2214 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002215 else
2216 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002217 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002218 {
2219 if (!isSingleStatement(*sit))
2220 {
2221 return false;
2222 }
2223 }
2224
2225 return true;
2226 }
2227 }
2228
2229 return true;
2230}
2231
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002232// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2233// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002234bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002235{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002236 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002237
2238 // Parse loops of the form:
2239 // for(int index = initial; index [comparator] limit; index += increment)
2240 TIntermSymbol *index = NULL;
2241 TOperator comparator = EOpNull;
2242 int initial = 0;
2243 int limit = 0;
2244 int increment = 0;
2245
2246 // Parse index name and intial value
2247 if (node->getInit())
2248 {
2249 TIntermAggregate *init = node->getInit()->getAsAggregate();
2250
2251 if (init)
2252 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002253 TIntermSequence *sequence = init->getSequence();
2254 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002255
2256 if (variable && variable->getQualifier() == EvqTemporary)
2257 {
2258 TIntermBinary *assign = variable->getAsBinaryNode();
2259
2260 if (assign->getOp() == EOpInitialize)
2261 {
2262 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2263 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2264
2265 if (symbol && constant)
2266 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002267 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002268 {
2269 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002270 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002271 }
2272 }
2273 }
2274 }
2275 }
2276 }
2277
2278 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002279 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002280 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002281 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002282
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002283 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2284 {
2285 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2286
2287 if (constant)
2288 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002289 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002290 {
2291 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002292 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002293 }
2294 }
2295 }
2296 }
2297
2298 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002299 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002300 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002301 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2302 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002303
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002304 if (binaryTerminal)
2305 {
2306 TOperator op = binaryTerminal->getOp();
2307 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2308
2309 if (constant)
2310 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002311 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002312 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002313 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002314
2315 switch (op)
2316 {
2317 case EOpAddAssign: increment = value; break;
2318 case EOpSubAssign: increment = -value; break;
2319 default: UNIMPLEMENTED();
2320 }
2321 }
2322 }
2323 }
2324 else if (unaryTerminal)
2325 {
2326 TOperator op = unaryTerminal->getOp();
2327
2328 switch (op)
2329 {
2330 case EOpPostIncrement: increment = 1; break;
2331 case EOpPostDecrement: increment = -1; break;
2332 case EOpPreIncrement: increment = 1; break;
2333 case EOpPreDecrement: increment = -1; break;
2334 default: UNIMPLEMENTED();
2335 }
2336 }
2337 }
2338
2339 if (index != NULL && comparator != EOpNull && increment != 0)
2340 {
2341 if (comparator == EOpLessThanEqual)
2342 {
2343 comparator = EOpLessThan;
2344 limit += 1;
2345 }
2346
2347 if (comparator == EOpLessThan)
2348 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002349 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002350
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002351 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002352 {
2353 return false; // Not an excessive loop
2354 }
2355
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002356 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2357 mExcessiveLoopIndex = index;
2358
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002359 out << "{int ";
2360 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002361 out << ";\n"
2362 "bool Break";
2363 index->traverse(this);
2364 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002365
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002366 bool firstLoopFragment = true;
2367
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002368 while (iterations > 0)
2369 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002370 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002371
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002372 if (!firstLoopFragment)
2373 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002374 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002375 index->traverse(this);
2376 out << ") {\n";
2377 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002378
2379 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2380 {
2381 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2382 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002383
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002384 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002385 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002386
Corentin Wallez1239ee92015-03-19 14:38:02 -07002387 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002388 index->traverse(this);
2389 out << " = ";
2390 out << initial;
2391
2392 out << "; ";
2393 index->traverse(this);
2394 out << " < ";
2395 out << clampedLimit;
2396
2397 out << "; ";
2398 index->traverse(this);
2399 out << " += ";
2400 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002401 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002402
Jamie Madill8c46ab12015-12-07 16:39:19 -05002403 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002404 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002405
2406 if (node->getBody())
2407 {
2408 node->getBody()->traverse(this);
2409 }
2410
Jamie Madill8c46ab12015-12-07 16:39:19 -05002411 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002412 out << ";}\n";
2413
2414 if (!firstLoopFragment)
2415 {
2416 out << "}\n";
2417 }
2418
2419 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002420
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002421 initial += MAX_LOOP_ITERATIONS * increment;
2422 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002423 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002424
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002425 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002426
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002427 mExcessiveLoopIndex = restoreIndex;
2428
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002429 return true;
2430 }
2431 else UNIMPLEMENTED();
2432 }
2433
2434 return false; // Not handled as an excessive loop
2435}
2436
Jamie Madill8c46ab12015-12-07 16:39:19 -05002437void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2438 Visit visit,
2439 const char *preString,
2440 const char *inString,
2441 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002443 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444 {
2445 out << preString;
2446 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002447 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448 {
2449 out << inString;
2450 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002451 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452 {
2453 out << postString;
2454 }
2455}
2456
Jamie Madill8c46ab12015-12-07 16:39:19 -05002457void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002458{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002459 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002460 {
Jamie Madill32aab012015-01-27 14:12:26 -05002461 out << "\n";
2462 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002463
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002464 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002465 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002466 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002467 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002468
Jamie Madill32aab012015-01-27 14:12:26 -05002469 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002470 }
2471}
2472
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002473TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2474{
2475 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002476 const TType &type = symbol->getType();
2477 const TName &name = symbol->getName();
2478 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002479
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002480 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002481 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002482 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002483 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002484 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002485 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002486 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002487 }
2488
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002489 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002490 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002491 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2492 {
2493 // Samplers are passed as indices to the sampler array.
2494 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2495 return "const uint " + nameStr + ArrayString(type);
2496 }
2497 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2498 {
2499 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2500 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2501 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2502 ArrayString(type);
2503 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002504 }
2505
Olli Etuaho96963162016-03-21 11:54:33 +02002506 TStringStream argString;
2507 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2508 << ArrayString(type);
2509
2510 // If the structure parameter contains samplers, they need to be passed into the function as
2511 // separate parameters. HLSL doesn't natively support samplers in structs.
2512 if (type.isStructureContainingSamplers())
2513 {
2514 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2515 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002516 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002517 for (const TIntermSymbol *sampler : samplerSymbols)
2518 {
2519 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2520 {
2521 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2522 }
2523 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2524 {
2525 const TType &samplerType = sampler->getType();
2526 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2527 type.getArraySize() == samplerType.getArraySize());
2528 ASSERT(IsSampler(samplerType.getBasicType()));
2529 argString << ", " << QualifierString(qualifier) << " "
2530 << TextureString(samplerType.getBasicType()) << " texture_"
2531 << sampler->getSymbol() << ArrayString(type) << ", "
2532 << QualifierString(qualifier) << " "
2533 << SamplerString(samplerType.getBasicType()) << " sampler_"
2534 << sampler->getSymbol() << ArrayString(type);
2535 }
2536 else
2537 {
2538 const TType &samplerType = sampler->getType();
2539 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2540 type.getArraySize() == samplerType.getArraySize());
2541 ASSERT(IsSampler(samplerType.getBasicType()));
2542 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2543 << " " << sampler->getSymbol() << ArrayString(type);
2544 }
2545 }
2546 }
2547
2548 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002549}
2550
2551TString OutputHLSL::initializer(const TType &type)
2552{
2553 TString string;
2554
Jamie Madill94bf7f22013-07-08 13:31:15 -04002555 size_t size = type.getObjectSize();
2556 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002558 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559
Jamie Madill94bf7f22013-07-08 13:31:15 -04002560 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561 {
2562 string += ", ";
2563 }
2564 }
2565
daniel@transgaming.comead23042010-04-29 03:35:36 +00002566 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002568
Jamie Madill8c46ab12015-12-07 16:39:19 -05002569void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2570 Visit visit,
2571 const TType &type,
2572 const char *name,
2573 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002574{
Olli Etuahof40319e2015-03-10 14:33:00 +02002575 if (type.isArray())
2576 {
2577 UNIMPLEMENTED();
2578 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002579
2580 if (visit == PreVisit)
2581 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002582 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002583
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002584 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002585 }
2586 else if (visit == InVisit)
2587 {
2588 out << ", ";
2589 }
2590 else if (visit == PostVisit)
2591 {
2592 out << ")";
2593 }
2594}
2595
Jamie Madill8c46ab12015-12-07 16:39:19 -05002596const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2597 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002598 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002599{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002600 const TConstantUnion *constUnionIterated = constUnion;
2601
Jamie Madill98493dd2013-07-08 14:39:03 -04002602 const TStructure* structure = type.getStruct();
2603 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002604 {
Jamie Madill033dae62014-06-18 12:56:28 -04002605 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002606
Jamie Madill98493dd2013-07-08 14:39:03 -04002607 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002608
Jamie Madill98493dd2013-07-08 14:39:03 -04002609 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002610 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002611 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002612 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002613
Jamie Madill98493dd2013-07-08 14:39:03 -04002614 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002615 {
2616 out << ", ";
2617 }
2618 }
2619
2620 out << ")";
2621 }
2622 else
2623 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002624 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002625 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002626
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002627 if (writeType)
2628 {
Jamie Madill033dae62014-06-18 12:56:28 -04002629 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002630 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002631 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002632 if (writeType)
2633 {
2634 out << ")";
2635 }
2636 }
2637
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002638 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002639}
2640
Jamie Madill8c46ab12015-12-07 16:39:19 -05002641void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002642{
2643 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002644 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002645}
2646
Jamie Madill37997142015-01-28 10:06:34 -05002647bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2648{
2649 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2650 expression->traverse(&searchSymbol);
2651
2652 if (searchSymbol.foundMatch())
2653 {
2654 // Type already printed
2655 out << "t" + str(mUniqueIndex) + " = ";
2656 expression->traverse(this);
2657 out << ", ";
2658 symbolNode->traverse(this);
2659 out << " = t" + str(mUniqueIndex);
2660
2661 mUniqueIndex++;
2662 return true;
2663 }
2664
2665 return false;
2666}
2667
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002668bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2669{
2670 // We support writing constant unions and constructors that only take constant unions as
2671 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002672 return expression->getAsConstantUnion() ||
2673 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002674}
2675
2676bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2677 TIntermSymbol *symbolNode,
2678 TIntermTyped *expression)
2679{
2680 if (canWriteAsHLSLLiteral(expression))
2681 {
2682 symbolNode->traverse(this);
2683 if (expression->getType().isArray())
2684 {
2685 out << "[" << expression->getType().getArraySize() << "]";
2686 }
2687 out << " = {";
2688 if (expression->getAsConstantUnion())
2689 {
2690 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2691 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2692 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2693 }
2694 else
2695 {
2696 TIntermAggregate *constructor = expression->getAsAggregate();
2697 ASSERT(constructor != nullptr);
2698 for (TIntermNode *&node : *constructor->getSequence())
2699 {
2700 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2701 ASSERT(nodeConst);
2702 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2703 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2704 if (node != constructor->getSequence()->back())
2705 {
2706 out << ", ";
2707 }
2708 }
2709 }
2710 out << "}";
2711 return true;
2712 }
2713 return false;
2714}
2715
Jamie Madill55e79e02015-02-09 15:35:00 -05002716TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2717{
2718 const TFieldList &fields = structure.fields();
2719
2720 for (const auto &eqFunction : mStructEqualityFunctions)
2721 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002722 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002723 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002724 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002725 }
2726 }
2727
2728 const TString &structNameString = StructNameString(structure);
2729
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002730 StructEqualityFunction *function = new StructEqualityFunction();
2731 function->structure = &structure;
2732 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002733
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002734 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002735
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002736 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2737 << "{\n"
2738 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002739
2740 for (size_t i = 0; i < fields.size(); i++)
2741 {
2742 const TField *field = fields[i];
2743 const TType *fieldType = field->type();
2744
2745 const TString &fieldNameA = "a." + Decorate(field->name());
2746 const TString &fieldNameB = "b." + Decorate(field->name());
2747
2748 if (i > 0)
2749 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002750 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002751 }
2752
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002753 fnOut << "(";
2754 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2755 fnOut << fieldNameA;
2756 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2757 fnOut << fieldNameB;
2758 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2759 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002760 }
2761
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002762 fnOut << ";\n" << "}\n";
2763
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
Olli Etuaho7fb49552015-03-18 17:27:44 +02002772TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2773{
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();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002785 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
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002796 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002797 << typeName << " a[" << type.getArraySize() << "], "
2798 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002799 << "{\n"
2800 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2801 " {\n"
2802 " if (";
2803
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
Olli Etuaho12690762015-03-31 12:55:28 +03002823TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2824{
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
2844 fnOut << "void " << function.functionName << "(out "
2845 << typeName << " a[" << type.getArraySize() << "], "
2846 << typeName << " b[" << type.getArraySize() << "])\n"
2847 << "{\n"
2848 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2849 " {\n"
2850 " a[i] = b[i];\n"
2851 " }\n"
2852 "}\n";
2853
2854 function.functionDefinition = fnOut.c_str();
2855
2856 mArrayAssignmentFunctions.push_back(function);
2857
2858 return function.functionName;
2859}
2860
Olli Etuaho9638c352015-04-01 14:34:52 +03002861TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2862{
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
2882 fnOut << "void " << function.functionName << "(out "
2883 << typeName << " a[" << type.getArraySize() << "]";
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
2914
Olli Etuaho9638c352015-04-01 14:34:52 +03002915
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002916}