blob: 6835c2e68a14b25732f42685130e1384bca8ab52 [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 Etuaho57961272016-09-14 13:57:46 +03001460 // Also no need to output ; after if statements or sequences. This is done just for
1461 // code clarity.
1462 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001463 !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
Olli Etuaho57961272016-09-14 13:57:46 +03001932void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *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.
Olli Etuaho57961272016-09-14 13:57:46 +03001970 ASSERT(IsSequence(node->getFalseBlock()) ||
1971 node->getFalseBlock()->getAsIfElseNode() != nullptr);
Olli Etuaho4785fec2015-05-18 16:09:37 +03001972
Olli Etuahoa6f22092015-05-08 18:31:10 +03001973 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001974
Jamie Madill8c46ab12015-12-07 16:39:19 -05001975 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001976
Olli Etuahoa6f22092015-05-08 18:31:10 +03001977 // Detect false discard
1978 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1979 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001980
Olli Etuahoa6f22092015-05-08 18:31:10 +03001981 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001982 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001983 {
1984 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001985 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001986}
1987
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001988bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1989{
1990 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1991 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1992 UNREACHABLE();
1993 return false;
1994}
1995
Olli Etuaho57961272016-09-14 13:57:46 +03001996bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001997{
1998 TInfoSinkBase &out = getInfoSink();
1999
Olli Etuaho3d932d82016-04-12 11:10:30 +03002000 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002001
2002 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002003 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002004 {
2005 out << "FLATTEN ";
2006 }
2007
Olli Etuaho57961272016-09-14 13:57:46 +03002008 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002009
2010 return false;
2011}
2012
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002013bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002014{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002015 TInfoSinkBase &out = getInfoSink();
2016
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002017 if (node->getStatementList())
2018 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002019 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002020 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002021 // The curly braces get written when visiting the statementList aggregate
2022 }
2023 else
2024 {
2025 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002026 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002027 }
2028 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002029}
2030
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002031bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002032{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002033 TInfoSinkBase &out = getInfoSink();
2034
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002035 if (node->hasCondition())
2036 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002037 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002038 return true;
2039 }
2040 else
2041 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002042 out << "default:\n";
2043 return false;
2044 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002045}
2046
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002047void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2048{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002049 TInfoSinkBase &out = getInfoSink();
2050 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002051}
2052
2053bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2054{
Nicolas Capens655fe362014-04-11 13:12:34 -04002055 mNestedLoopDepth++;
2056
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002057 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002058 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002059 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002060
Jamie Madill8c46ab12015-12-07 16:39:19 -05002061 TInfoSinkBase &out = getInfoSink();
2062
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002063 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002064 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002065 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002066 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002067 mInsideDiscontinuousLoop = wasDiscontinuous;
2068 mNestedLoopDepth--;
2069
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002070 return false;
2071 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002072 }
2073
Corentin Wallez1239ee92015-03-19 14:38:02 -07002074 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002075 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002077 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002078
Jamie Madill8c46ab12015-12-07 16:39:19 -05002079 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080 }
2081 else
2082 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002083 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002084
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 if (node->getInit())
2086 {
2087 node->getInit()->traverse(this);
2088 }
2089
2090 out << "; ";
2091
alokp@chromium.org52813552010-11-16 18:36:09 +00002092 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002094 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095 }
2096
2097 out << "; ";
2098
alokp@chromium.org52813552010-11-16 18:36:09 +00002099 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002101 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 }
2103
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002104 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002105
Jamie Madill8c46ab12015-12-07 16:39:19 -05002106 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 }
2108
2109 if (node->getBody())
2110 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002111 // The loop body node will output braces.
2112 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002113 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002115 else
2116 {
2117 // TODO(oetuaho): Check if the semicolon inside is necessary.
2118 // It's there as a result of conservative refactoring of the output.
2119 out << "{;}\n";
2120 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121
Jamie Madill8c46ab12015-12-07 16:39:19 -05002122 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123
alokp@chromium.org52813552010-11-16 18:36:09 +00002124 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002126 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 out << "while(\n";
2128
alokp@chromium.org52813552010-11-16 18:36:09 +00002129 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130
daniel@transgaming.com73536982012-03-21 20:45:49 +00002131 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132 }
2133
daniel@transgaming.com73536982012-03-21 20:45:49 +00002134 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002136 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002137 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002138
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139 return false;
2140}
2141
2142bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2143{
Jamie Madill32aab012015-01-27 14:12:26 -05002144 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145
2146 switch (node->getFlowOp())
2147 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002148 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002149 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002150 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002151 case EOpBreak:
2152 if (visit == PreVisit)
2153 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002154 if (mNestedLoopDepth > 1)
2155 {
2156 mUsesNestedBreak = true;
2157 }
2158
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002159 if (mExcessiveLoopIndex)
2160 {
2161 out << "{Break";
2162 mExcessiveLoopIndex->traverse(this);
2163 out << " = true; break;}\n";
2164 }
2165 else
2166 {
2167 out << "break;\n";
2168 }
2169 }
2170 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002171 case EOpContinue:
2172 outputTriplet(out, visit, "continue;\n", "", "");
2173 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 case EOpReturn:
2175 if (visit == PreVisit)
2176 {
2177 if (node->getExpression())
2178 {
2179 out << "return ";
2180 }
2181 else
2182 {
2183 out << "return;\n";
2184 }
2185 }
2186 else if (visit == PostVisit)
2187 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002188 if (node->getExpression())
2189 {
2190 out << ";\n";
2191 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 }
2193 break;
2194 default: UNREACHABLE();
2195 }
2196
2197 return true;
2198}
2199
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002200bool OutputHLSL::isSingleStatement(TIntermNode *node)
2201{
2202 TIntermAggregate *aggregate = node->getAsAggregate();
2203
2204 if (aggregate)
2205 {
2206 if (aggregate->getOp() == EOpSequence)
2207 {
2208 return false;
2209 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002210 else if (aggregate->getOp() == EOpDeclaration)
2211 {
2212 // Declaring multiple comma-separated variables must be considered multiple statements
2213 // because each individual declaration has side effects which are visible in the next.
2214 return false;
2215 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002216 else
2217 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002218 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002219 {
2220 if (!isSingleStatement(*sit))
2221 {
2222 return false;
2223 }
2224 }
2225
2226 return true;
2227 }
2228 }
2229
2230 return true;
2231}
2232
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002233// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2234// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002235bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002236{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002237 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002238
2239 // Parse loops of the form:
2240 // for(int index = initial; index [comparator] limit; index += increment)
2241 TIntermSymbol *index = NULL;
2242 TOperator comparator = EOpNull;
2243 int initial = 0;
2244 int limit = 0;
2245 int increment = 0;
2246
2247 // Parse index name and intial value
2248 if (node->getInit())
2249 {
2250 TIntermAggregate *init = node->getInit()->getAsAggregate();
2251
2252 if (init)
2253 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002254 TIntermSequence *sequence = init->getSequence();
2255 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002256
2257 if (variable && variable->getQualifier() == EvqTemporary)
2258 {
2259 TIntermBinary *assign = variable->getAsBinaryNode();
2260
2261 if (assign->getOp() == EOpInitialize)
2262 {
2263 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2264 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2265
2266 if (symbol && constant)
2267 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002268 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002269 {
2270 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002271 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002272 }
2273 }
2274 }
2275 }
2276 }
2277 }
2278
2279 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002280 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002281 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002282 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002283
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002284 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2285 {
2286 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2287
2288 if (constant)
2289 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002290 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002291 {
2292 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002293 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002294 }
2295 }
2296 }
2297 }
2298
2299 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002300 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002301 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002302 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2303 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002304
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002305 if (binaryTerminal)
2306 {
2307 TOperator op = binaryTerminal->getOp();
2308 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2309
2310 if (constant)
2311 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002312 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002313 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002314 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002315
2316 switch (op)
2317 {
2318 case EOpAddAssign: increment = value; break;
2319 case EOpSubAssign: increment = -value; break;
2320 default: UNIMPLEMENTED();
2321 }
2322 }
2323 }
2324 }
2325 else if (unaryTerminal)
2326 {
2327 TOperator op = unaryTerminal->getOp();
2328
2329 switch (op)
2330 {
2331 case EOpPostIncrement: increment = 1; break;
2332 case EOpPostDecrement: increment = -1; break;
2333 case EOpPreIncrement: increment = 1; break;
2334 case EOpPreDecrement: increment = -1; break;
2335 default: UNIMPLEMENTED();
2336 }
2337 }
2338 }
2339
2340 if (index != NULL && comparator != EOpNull && increment != 0)
2341 {
2342 if (comparator == EOpLessThanEqual)
2343 {
2344 comparator = EOpLessThan;
2345 limit += 1;
2346 }
2347
2348 if (comparator == EOpLessThan)
2349 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002350 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002351
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002352 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002353 {
2354 return false; // Not an excessive loop
2355 }
2356
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002357 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2358 mExcessiveLoopIndex = index;
2359
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002360 out << "{int ";
2361 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002362 out << ";\n"
2363 "bool Break";
2364 index->traverse(this);
2365 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002366
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002367 bool firstLoopFragment = true;
2368
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002369 while (iterations > 0)
2370 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002371 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002372
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002373 if (!firstLoopFragment)
2374 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002375 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002376 index->traverse(this);
2377 out << ") {\n";
2378 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002379
2380 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2381 {
2382 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2383 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002384
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002385 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002386 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002387
Corentin Wallez1239ee92015-03-19 14:38:02 -07002388 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002389 index->traverse(this);
2390 out << " = ";
2391 out << initial;
2392
2393 out << "; ";
2394 index->traverse(this);
2395 out << " < ";
2396 out << clampedLimit;
2397
2398 out << "; ";
2399 index->traverse(this);
2400 out << " += ";
2401 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002402 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002403
Jamie Madill8c46ab12015-12-07 16:39:19 -05002404 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002405 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002406
2407 if (node->getBody())
2408 {
2409 node->getBody()->traverse(this);
2410 }
2411
Jamie Madill8c46ab12015-12-07 16:39:19 -05002412 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002413 out << ";}\n";
2414
2415 if (!firstLoopFragment)
2416 {
2417 out << "}\n";
2418 }
2419
2420 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002422 initial += MAX_LOOP_ITERATIONS * increment;
2423 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002424 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002425
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002426 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002427
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002428 mExcessiveLoopIndex = restoreIndex;
2429
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002430 return true;
2431 }
2432 else UNIMPLEMENTED();
2433 }
2434
2435 return false; // Not handled as an excessive loop
2436}
2437
Jamie Madill8c46ab12015-12-07 16:39:19 -05002438void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2439 Visit visit,
2440 const char *preString,
2441 const char *inString,
2442 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002443{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002444 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 {
2446 out << preString;
2447 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002448 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 {
2450 out << inString;
2451 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002452 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453 {
2454 out << postString;
2455 }
2456}
2457
Jamie Madill8c46ab12015-12-07 16:39:19 -05002458void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002459{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002460 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002461 {
Jamie Madill32aab012015-01-27 14:12:26 -05002462 out << "\n";
2463 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002464
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002465 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002466 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002467 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002468 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002469
Jamie Madill32aab012015-01-27 14:12:26 -05002470 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002471 }
2472}
2473
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002474TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2475{
2476 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002477 const TType &type = symbol->getType();
2478 const TName &name = symbol->getName();
2479 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002480
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002481 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002482 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002483 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002484 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002485 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002486 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002487 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002488 }
2489
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002490 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002491 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002492 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2493 {
2494 // Samplers are passed as indices to the sampler array.
2495 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2496 return "const uint " + nameStr + ArrayString(type);
2497 }
2498 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2499 {
2500 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2501 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2502 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2503 ArrayString(type);
2504 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002505 }
2506
Olli Etuaho96963162016-03-21 11:54:33 +02002507 TStringStream argString;
2508 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2509 << ArrayString(type);
2510
2511 // If the structure parameter contains samplers, they need to be passed into the function as
2512 // separate parameters. HLSL doesn't natively support samplers in structs.
2513 if (type.isStructureContainingSamplers())
2514 {
2515 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2516 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002517 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002518 for (const TIntermSymbol *sampler : samplerSymbols)
2519 {
2520 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2521 {
2522 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2523 }
2524 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2525 {
2526 const TType &samplerType = sampler->getType();
2527 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2528 type.getArraySize() == samplerType.getArraySize());
2529 ASSERT(IsSampler(samplerType.getBasicType()));
2530 argString << ", " << QualifierString(qualifier) << " "
2531 << TextureString(samplerType.getBasicType()) << " texture_"
2532 << sampler->getSymbol() << ArrayString(type) << ", "
2533 << QualifierString(qualifier) << " "
2534 << SamplerString(samplerType.getBasicType()) << " sampler_"
2535 << sampler->getSymbol() << ArrayString(type);
2536 }
2537 else
2538 {
2539 const TType &samplerType = sampler->getType();
2540 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2541 type.getArraySize() == samplerType.getArraySize());
2542 ASSERT(IsSampler(samplerType.getBasicType()));
2543 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2544 << " " << sampler->getSymbol() << ArrayString(type);
2545 }
2546 }
2547 }
2548
2549 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550}
2551
2552TString OutputHLSL::initializer(const TType &type)
2553{
2554 TString string;
2555
Jamie Madill94bf7f22013-07-08 13:31:15 -04002556 size_t size = type.getObjectSize();
2557 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002559 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002560
Jamie Madill94bf7f22013-07-08 13:31:15 -04002561 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562 {
2563 string += ", ";
2564 }
2565 }
2566
daniel@transgaming.comead23042010-04-29 03:35:36 +00002567 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002568}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002569
Jamie Madill8c46ab12015-12-07 16:39:19 -05002570void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2571 Visit visit,
2572 const TType &type,
2573 const char *name,
2574 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002575{
Olli Etuahof40319e2015-03-10 14:33:00 +02002576 if (type.isArray())
2577 {
2578 UNIMPLEMENTED();
2579 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002580
2581 if (visit == PreVisit)
2582 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002583 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002584
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002585 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002586 }
2587 else if (visit == InVisit)
2588 {
2589 out << ", ";
2590 }
2591 else if (visit == PostVisit)
2592 {
2593 out << ")";
2594 }
2595}
2596
Jamie Madill8c46ab12015-12-07 16:39:19 -05002597const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2598 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002599 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002600{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002601 const TConstantUnion *constUnionIterated = constUnion;
2602
Jamie Madill98493dd2013-07-08 14:39:03 -04002603 const TStructure* structure = type.getStruct();
2604 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002605 {
Jamie Madill033dae62014-06-18 12:56:28 -04002606 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002607
Jamie Madill98493dd2013-07-08 14:39:03 -04002608 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002609
Jamie Madill98493dd2013-07-08 14:39:03 -04002610 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002611 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002612 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002613 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002614
Jamie Madill98493dd2013-07-08 14:39:03 -04002615 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002616 {
2617 out << ", ";
2618 }
2619 }
2620
2621 out << ")";
2622 }
2623 else
2624 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002625 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002626 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002627
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002628 if (writeType)
2629 {
Jamie Madill033dae62014-06-18 12:56:28 -04002630 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002631 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002632 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002633 if (writeType)
2634 {
2635 out << ")";
2636 }
2637 }
2638
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002639 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002640}
2641
Jamie Madill8c46ab12015-12-07 16:39:19 -05002642void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002643{
2644 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002645 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002646}
2647
Jamie Madill37997142015-01-28 10:06:34 -05002648bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2649{
2650 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2651 expression->traverse(&searchSymbol);
2652
2653 if (searchSymbol.foundMatch())
2654 {
2655 // Type already printed
2656 out << "t" + str(mUniqueIndex) + " = ";
2657 expression->traverse(this);
2658 out << ", ";
2659 symbolNode->traverse(this);
2660 out << " = t" + str(mUniqueIndex);
2661
2662 mUniqueIndex++;
2663 return true;
2664 }
2665
2666 return false;
2667}
2668
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002669bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2670{
2671 // We support writing constant unions and constructors that only take constant unions as
2672 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002673 return expression->getAsConstantUnion() ||
2674 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002675}
2676
2677bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2678 TIntermSymbol *symbolNode,
2679 TIntermTyped *expression)
2680{
2681 if (canWriteAsHLSLLiteral(expression))
2682 {
2683 symbolNode->traverse(this);
2684 if (expression->getType().isArray())
2685 {
2686 out << "[" << expression->getType().getArraySize() << "]";
2687 }
2688 out << " = {";
2689 if (expression->getAsConstantUnion())
2690 {
2691 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2692 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2693 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2694 }
2695 else
2696 {
2697 TIntermAggregate *constructor = expression->getAsAggregate();
2698 ASSERT(constructor != nullptr);
2699 for (TIntermNode *&node : *constructor->getSequence())
2700 {
2701 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2702 ASSERT(nodeConst);
2703 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2704 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2705 if (node != constructor->getSequence()->back())
2706 {
2707 out << ", ";
2708 }
2709 }
2710 }
2711 out << "}";
2712 return true;
2713 }
2714 return false;
2715}
2716
Jamie Madill55e79e02015-02-09 15:35:00 -05002717TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2718{
2719 const TFieldList &fields = structure.fields();
2720
2721 for (const auto &eqFunction : mStructEqualityFunctions)
2722 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002723 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002724 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002725 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002726 }
2727 }
2728
2729 const TString &structNameString = StructNameString(structure);
2730
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002731 StructEqualityFunction *function = new StructEqualityFunction();
2732 function->structure = &structure;
2733 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002734
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002735 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002736
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002737 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2738 << "{\n"
2739 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002740
2741 for (size_t i = 0; i < fields.size(); i++)
2742 {
2743 const TField *field = fields[i];
2744 const TType *fieldType = field->type();
2745
2746 const TString &fieldNameA = "a." + Decorate(field->name());
2747 const TString &fieldNameB = "b." + Decorate(field->name());
2748
2749 if (i > 0)
2750 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002751 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002752 }
2753
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002754 fnOut << "(";
2755 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2756 fnOut << fieldNameA;
2757 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2758 fnOut << fieldNameB;
2759 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2760 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002761 }
2762
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002763 fnOut << ";\n" << "}\n";
2764
2765 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002766
2767 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002768 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002769
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002770 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002771}
2772
Olli Etuaho7fb49552015-03-18 17:27:44 +02002773TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2774{
2775 for (const auto &eqFunction : mArrayEqualityFunctions)
2776 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002777 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002778 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002779 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002780 }
2781 }
2782
2783 const TString &typeName = TypeString(type);
2784
Olli Etuaho12690762015-03-31 12:55:28 +03002785 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002786 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002787
2788 TInfoSinkBase fnNameOut;
2789 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002790 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002791
2792 TType nonArrayType = type;
2793 nonArrayType.clearArrayness();
2794
2795 TInfoSinkBase fnOut;
2796
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002797 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002798 << typeName << " a[" << type.getArraySize() << "], "
2799 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002800 << "{\n"
2801 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2802 " {\n"
2803 " if (";
2804
2805 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2806 fnOut << "a[i]";
2807 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2808 fnOut << "b[i]";
2809 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2810
2811 fnOut << ") { return false; }\n"
2812 " }\n"
2813 " return true;\n"
2814 "}\n";
2815
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002816 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002817
2818 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002819 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002820
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002821 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002822}
2823
Olli Etuaho12690762015-03-31 12:55:28 +03002824TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2825{
2826 for (const auto &assignFunction : mArrayAssignmentFunctions)
2827 {
2828 if (assignFunction.type == type)
2829 {
2830 return assignFunction.functionName;
2831 }
2832 }
2833
2834 const TString &typeName = TypeString(type);
2835
2836 ArrayHelperFunction function;
2837 function.type = type;
2838
2839 TInfoSinkBase fnNameOut;
2840 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2841 function.functionName = fnNameOut.c_str();
2842
2843 TInfoSinkBase fnOut;
2844
2845 fnOut << "void " << function.functionName << "(out "
2846 << typeName << " a[" << type.getArraySize() << "], "
2847 << typeName << " b[" << type.getArraySize() << "])\n"
2848 << "{\n"
2849 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2850 " {\n"
2851 " a[i] = b[i];\n"
2852 " }\n"
2853 "}\n";
2854
2855 function.functionDefinition = fnOut.c_str();
2856
2857 mArrayAssignmentFunctions.push_back(function);
2858
2859 return function.functionName;
2860}
2861
Olli Etuaho9638c352015-04-01 14:34:52 +03002862TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2863{
2864 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2865 {
2866 if (constructIntoFunction.type == type)
2867 {
2868 return constructIntoFunction.functionName;
2869 }
2870 }
2871
2872 const TString &typeName = TypeString(type);
2873
2874 ArrayHelperFunction function;
2875 function.type = type;
2876
2877 TInfoSinkBase fnNameOut;
2878 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2879 function.functionName = fnNameOut.c_str();
2880
2881 TInfoSinkBase fnOut;
2882
2883 fnOut << "void " << function.functionName << "(out "
2884 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002885 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002886 {
2887 fnOut << ", " << typeName << " b" << i;
2888 }
2889 fnOut << ")\n"
2890 "{\n";
2891
Olli Etuaho856c4972016-08-08 11:38:39 +03002892 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002893 {
2894 fnOut << " a[" << i << "] = b" << i << ";\n";
2895 }
2896 fnOut << "}\n";
2897
2898 function.functionDefinition = fnOut.c_str();
2899
2900 mArrayConstructIntoFunctions.push_back(function);
2901
2902 return function.functionName;
2903}
2904
Jamie Madill2e295e22015-04-29 10:41:33 -04002905void OutputHLSL::ensureStructDefined(const TType &type)
2906{
2907 TStructure *structure = type.getStruct();
2908
2909 if (structure)
2910 {
2911 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2912 }
2913}
2914
2915
Olli Etuaho9638c352015-04-01 14:34:52 +03002916
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002917}