blob: 4ed4b42f562a4afe16e8d287fdedb285356c434f [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);
Shao6f0a0dc2016-09-27 13:51:29 +0800164 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
165 {
166 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
167 mShaderVersion);
168 }
169
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200170 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500171
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700172 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700173 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
174 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300175 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700176 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700177
Jamie Madill37997142015-01-28 10:06:34 -0500178 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500179 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200180 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500181 mInfoSinkStack.pop();
182
Jamie Madill37997142015-01-28 10:06:34 -0500183 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500184 mInfoSinkStack.pop();
185
Jamie Madill32aab012015-01-27 14:12:26 -0500186 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500187 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500188 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000189
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200190 objSink << mHeader.c_str();
191 objSink << mBody.c_str();
192 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200193
194 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000195}
196
Jamie Madill570e04d2013-06-21 09:15:33 -0400197void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
198{
199 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
200 {
201 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
202
Jamie Madill32aab012015-01-27 14:12:26 -0500203 TInfoSinkBase structInfoSink;
204 mInfoSinkStack.push(&structInfoSink);
205
Jamie Madill570e04d2013-06-21 09:15:33 -0400206 // This will mark the necessary block elements as referenced
207 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500208
209 TString structName(structInfoSink.c_str());
210 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400211
212 mFlaggedStructOriginalNames[flaggedNode] = structName;
213
214 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
215 {
216 structName.erase(pos, 1);
217 }
218
219 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
220 }
221}
222
Jamie Madill4e1fd412014-07-10 17:50:10 -0400223const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
224{
225 return mUniformHLSL->getInterfaceBlockRegisterMap();
226}
227
Jamie Madill9fe25e92014-07-18 10:33:08 -0400228const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
229{
230 return mUniformHLSL->getUniformRegisterMap();
231}
232
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000233int OutputHLSL::vectorSize(const TType &type) const
234{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000235 int elementSize = type.isMatrix() ? type.getCols() : 1;
Olli Etuaho856c4972016-08-08 11:38:39 +0300236 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000237
238 return elementSize * arraySize;
239}
240
Jamie Madill98493dd2013-07-08 14:39:03 -0400241TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400242{
243 TString init;
244
245 TString preIndentString;
246 TString fullIndentString;
247
248 for (int spaces = 0; spaces < (indent * 4); spaces++)
249 {
250 preIndentString += ' ';
251 }
252
253 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
254 {
255 fullIndentString += ' ';
256 }
257
258 init += preIndentString + "{\n";
259
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 const TFieldList &fields = structure.fields();
261 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400262 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400264 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400265 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400266
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400268 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400270 }
271 else
272 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400274 }
275 }
276
277 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
278
279 return init;
280}
281
Jamie Madill8c46ab12015-12-07 16:39:19 -0500282void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000284 TString varyings;
285 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000287
Jamie Madill829f59e2013-11-13 19:40:54 -0500288 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400289 {
290 TIntermTyped *structNode = flaggedStructIt->first;
291 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400292 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400293 const TString &originalName = mFlaggedStructOriginalNames[structNode];
294
Jamie Madill033dae62014-06-18 12:56:28 -0400295 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400296 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400297 flaggedStructs += "\n";
298 }
299
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000300 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
301 {
302 const TType &type = varying->second->getType();
303 const TString &name = varying->second->getSymbol();
304
305 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400306 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
307 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000308 }
309
310 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
311 {
312 const TType &type = attribute->second->getType();
313 const TString &name = attribute->second->getSymbol();
314
Jamie Madill033dae62014-06-18 12:56:28 -0400315 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000316 }
317
Jamie Madill8daaba12014-06-13 10:04:33 -0400318 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400319
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200320 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400321 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
322
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200323 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500324 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200325 out << "\n// Equality functions\n\n";
326 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500327 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200328 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200329 }
330 }
Olli Etuaho12690762015-03-31 12:55:28 +0300331 if (!mArrayAssignmentFunctions.empty())
332 {
333 out << "\n// Assignment functions\n\n";
334 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
335 {
336 out << assignmentFunction.functionDefinition << "\n";
337 }
338 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300339 if (!mArrayConstructIntoFunctions.empty())
340 {
341 out << "\n// Array constructor functions\n\n";
342 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
343 {
344 out << constructIntoFunction.functionDefinition << "\n";
345 }
346 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200347
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500348 if (mUsesDiscardRewriting)
349 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400350 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500351 }
352
Nicolas Capens655fe362014-04-11 13:12:34 -0400353 if (mUsesNestedBreak)
354 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400355 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400356 }
357
Arun Patole44efa0b2015-03-04 17:11:05 +0530358 if (mRequiresIEEEStrictCompiling)
359 {
360 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
361 }
362
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400363 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
364 "#define LOOP [loop]\n"
365 "#define FLATTEN [flatten]\n"
366 "#else\n"
367 "#define LOOP\n"
368 "#define FLATTEN\n"
369 "#endif\n";
370
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200371 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200373 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
374 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000375
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000376 out << "// Varyings\n";
377 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400378 out << "\n";
379
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200380 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000381 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500382 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000383 {
Jamie Madill46131a32013-06-20 11:55:50 -0400384 const TString &variableName = outputVariableIt->first;
385 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400386
Jamie Madill033dae62014-06-18 12:56:28 -0400387 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400388 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000389 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000390 }
Jamie Madill46131a32013-06-20 11:55:50 -0400391 else
392 {
393 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
394
395 out << "static float4 gl_Color[" << numColorValues << "] =\n"
396 "{\n";
397 for (unsigned int i = 0; i < numColorValues; i++)
398 {
399 out << " float4(0, 0, 0, 0)";
400 if (i + 1 != numColorValues)
401 {
402 out << ",";
403 }
404 out << "\n";
405 }
406
407 out << "};\n";
408 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000409
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400410 if (mUsesFragDepth)
411 {
412 out << "static float gl_Depth = 0.0;\n";
413 }
414
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000415 if (mUsesFragCoord)
416 {
417 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
418 }
419
420 if (mUsesPointCoord)
421 {
422 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
423 }
424
425 if (mUsesFrontFacing)
426 {
427 out << "static bool gl_FrontFacing = false;\n";
428 }
429
430 out << "\n";
431
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000432 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000433 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000434 out << "struct gl_DepthRangeParameters\n"
435 "{\n"
436 " float near;\n"
437 " float far;\n"
438 " float diff;\n"
439 "};\n"
440 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000441 }
442
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200443 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000444 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000445 out << "cbuffer DriverConstants : register(b1)\n"
446 "{\n";
447
448 if (mUsesDepthRange)
449 {
450 out << " float3 dx_DepthRange : packoffset(c0);\n";
451 }
452
453 if (mUsesFragCoord)
454 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000455 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000456 }
457
458 if (mUsesFragCoord || mUsesFrontFacing)
459 {
460 out << " float3 dx_DepthFront : packoffset(c2);\n";
461 }
462
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800463 if (mUsesFragCoord)
464 {
465 // dx_ViewScale is only used in the fragment shader to correct
466 // the value for glFragCoord if necessary
467 out << " float2 dx_ViewScale : packoffset(c3);\n";
468 }
469
Olli Etuaho618bebc2016-01-15 16:40:00 +0200470 if (mOutputType == SH_HLSL_4_1_OUTPUT)
471 {
472 mUniformHLSL->samplerMetadataUniforms(out, "c4");
473 }
474
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000475 out << "};\n";
476 }
477 else
478 {
479 if (mUsesDepthRange)
480 {
481 out << "uniform float3 dx_DepthRange : register(c0);";
482 }
483
484 if (mUsesFragCoord)
485 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000486 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000487 }
488
489 if (mUsesFragCoord || mUsesFrontFacing)
490 {
491 out << "uniform float3 dx_DepthFront : register(c2);\n";
492 }
493 }
494
495 out << "\n";
496
497 if (mUsesDepthRange)
498 {
499 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
500 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000501 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000502
Jamie Madillf91ce812014-06-13 10:04:34 -0400503 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000504 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400505 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000506 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400507 out << flaggedStructs;
508 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000509 }
510
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000511 if (usingMRTExtension && mNumRenderTargets > 1)
512 {
513 out << "#define GL_USES_MRT\n";
514 }
515
516 if (mUsesFragColor)
517 {
518 out << "#define GL_USES_FRAG_COLOR\n";
519 }
520
521 if (mUsesFragData)
522 {
523 out << "#define GL_USES_FRAG_DATA\n";
524 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000526 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000528 out << "// Attributes\n";
529 out << attributes;
530 out << "\n"
531 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400532
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000533 if (mUsesPointSize)
534 {
535 out << "static float gl_PointSize = float(1);\n";
536 }
537
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000538 if (mUsesInstanceID)
539 {
540 out << "static int gl_InstanceID;";
541 }
542
Corentin Wallezb076add2016-01-11 16:45:46 -0500543 if (mUsesVertexID)
544 {
545 out << "static int gl_VertexID;";
546 }
547
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000548 out << "\n"
549 "// Varyings\n";
550 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000551 out << "\n";
552
553 if (mUsesDepthRange)
554 {
555 out << "struct gl_DepthRangeParameters\n"
556 "{\n"
557 " float near;\n"
558 " float far;\n"
559 " float diff;\n"
560 "};\n"
561 "\n";
562 }
563
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200564 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000565 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800566 out << "cbuffer DriverConstants : register(b1)\n"
567 "{\n";
568
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000569 if (mUsesDepthRange)
570 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800571 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000572 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800573
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800574 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
575 // shaders. However, we declare it for all shaders (including Feature Level 10+).
576 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
577 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800578 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800579 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800580 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800581
Olli Etuaho618bebc2016-01-15 16:40:00 +0200582 if (mOutputType == SH_HLSL_4_1_OUTPUT)
583 {
584 mUniformHLSL->samplerMetadataUniforms(out, "c4");
585 }
586
Austin Kinross4fd18b12014-12-22 12:32:05 -0800587 out << "};\n"
588 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000589 }
590 else
591 {
592 if (mUsesDepthRange)
593 {
594 out << "uniform float3 dx_DepthRange : register(c0);\n";
595 }
596
Cooper Partine6664f02015-01-09 16:22:24 -0800597 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
598 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000599 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000600 }
601
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000602 if (mUsesDepthRange)
603 {
604 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
605 "\n";
606 }
607
Jamie Madillf91ce812014-06-13 10:04:34 -0400608 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000609 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400610 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000611 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400612 out << flaggedStructs;
613 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000614 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400615 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000616
Geoff Lang1fe74c72016-08-25 13:23:01 -0400617 bool getDimensionsIgnoresBaseLevel =
618 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
619 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000621 if (mUsesFragCoord)
622 {
623 out << "#define GL_USES_FRAG_COORD\n";
624 }
625
626 if (mUsesPointCoord)
627 {
628 out << "#define GL_USES_POINT_COORD\n";
629 }
630
631 if (mUsesFrontFacing)
632 {
633 out << "#define GL_USES_FRONT_FACING\n";
634 }
635
636 if (mUsesPointSize)
637 {
638 out << "#define GL_USES_POINT_SIZE\n";
639 }
640
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400641 if (mUsesFragDepth)
642 {
643 out << "#define GL_USES_FRAG_DEPTH\n";
644 }
645
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000646 if (mUsesDepthRange)
647 {
648 out << "#define GL_USES_DEPTH_RANGE\n";
649 }
650
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000651 if (mUsesXor)
652 {
653 out << "bool xor(bool p, bool q)\n"
654 "{\n"
655 " return (p || q) && !(p && q);\n"
656 "}\n"
657 "\n";
658 }
659
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200660 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661}
662
663void OutputHLSL::visitSymbol(TIntermSymbol *node)
664{
Jamie Madill32aab012015-01-27 14:12:26 -0500665 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
Jamie Madill570e04d2013-06-21 09:15:33 -0400667 // Handle accessing std140 structs by value
668 if (mFlaggedStructMappedNames.count(node) > 0)
669 {
670 out << mFlaggedStructMappedNames[node];
671 return;
672 }
673
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674 TString name = node->getSymbol();
675
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000676 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000677 {
678 mUsesDepthRange = true;
679 out << name;
680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681 else
682 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000683 TQualifier qualifier = node->getQualifier();
684
685 if (qualifier == EvqUniform)
686 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400687 const TType &nodeType = node->getType();
688 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400689
690 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000691 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400692 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000693 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000694 else
695 {
696 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000697 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400698
Jamie Madill2e295e22015-04-29 10:41:33 -0400699 ensureStructDefined(nodeType);
700
Olli Etuaho96963162016-03-21 11:54:33 +0200701 const TName &nameWithMetadata = node->getName();
702 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000703 }
Jamie Madill19571812013-08-12 15:26:34 -0700704 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000705 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000706 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400707 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000708 }
Jamie Madill033dae62014-06-18 12:56:28 -0400709 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000710 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000711 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400712 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000713 }
Jamie Madill19571812013-08-12 15:26:34 -0700714 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400715 {
716 mReferencedOutputVariables[name] = node;
717 out << "out_" << name;
718 }
719 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000720 {
721 out << "gl_Color[0]";
722 mUsesFragColor = true;
723 }
724 else if (qualifier == EvqFragData)
725 {
726 out << "gl_Color";
727 mUsesFragData = true;
728 }
729 else if (qualifier == EvqFragCoord)
730 {
731 mUsesFragCoord = true;
732 out << name;
733 }
734 else if (qualifier == EvqPointCoord)
735 {
736 mUsesPointCoord = true;
737 out << name;
738 }
739 else if (qualifier == EvqFrontFacing)
740 {
741 mUsesFrontFacing = true;
742 out << name;
743 }
744 else if (qualifier == EvqPointSize)
745 {
746 mUsesPointSize = true;
747 out << name;
748 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000749 else if (qualifier == EvqInstanceID)
750 {
751 mUsesInstanceID = true;
752 out << name;
753 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500754 else if (qualifier == EvqVertexID)
755 {
756 mUsesVertexID = true;
757 out << name;
758 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300759 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400760 {
761 mUsesFragDepth = true;
762 out << "gl_Depth";
763 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000764 else
765 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300766 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000767 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768 }
769}
770
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400771void OutputHLSL::visitRaw(TIntermRaw *node)
772{
Jamie Madill32aab012015-01-27 14:12:26 -0500773 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400774}
775
Olli Etuaho7fb49552015-03-18 17:27:44 +0200776void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
777{
778 if (type.isScalar() && !type.isArray())
779 {
780 if (op == EOpEqual)
781 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500782 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200783 }
784 else
785 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500786 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200787 }
788 }
789 else
790 {
791 if (visit == PreVisit && op == EOpNotEqual)
792 {
793 out << "!";
794 }
795
796 if (type.isArray())
797 {
798 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500799 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200800 }
801 else if (type.getBasicType() == EbtStruct)
802 {
803 const TStructure &structure = *type.getStruct();
804 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500805 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200806 }
807 else
808 {
809 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500810 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200811 }
812 }
813}
814
Olli Etuaho96963162016-03-21 11:54:33 +0200815bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
816{
817 // Inside InVisit the current node is already in the path.
818 const unsigned int initialN = visit == InVisit ? 1u : 0u;
819 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
820 {
821 TIntermNode *ancestor = getAncestorNode(n);
822 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
823 if (ancestorBinary == nullptr)
824 {
825 return false;
826 }
827 switch (ancestorBinary->getOp())
828 {
829 case EOpIndexDirectStruct:
830 {
831 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
832 const TIntermConstantUnion *index =
833 ancestorBinary->getRight()->getAsConstantUnion();
834 const TField *field = structure->fields()[index->getIConst(0)];
835 if (IsSampler(field->type()->getBasicType()))
836 {
837 return true;
838 }
839 break;
840 }
841 case EOpIndexDirect:
842 break;
843 default:
844 // Returning a sampler from indirect indexing is not supported.
845 return false;
846 }
847 }
848 return false;
849}
850
Olli Etuahob6fa0432016-09-28 16:28:05 +0100851bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
852{
853 TInfoSinkBase &out = getInfoSink();
854 if (visit == PostVisit)
855 {
856 out << ".";
857 node->writeOffsetsAsXYZW(&out);
858 }
859 return true;
860}
861
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
863{
Jamie Madill32aab012015-01-27 14:12:26 -0500864 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865
Jamie Madill570e04d2013-06-21 09:15:33 -0400866 // Handle accessing std140 structs by value
867 if (mFlaggedStructMappedNames.count(node) > 0)
868 {
869 out << mFlaggedStructMappedNames[node];
870 return false;
871 }
872
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873 switch (node->getOp())
874 {
Olli Etuahoe79904c2015-03-18 16:56:42 +0200875 case EOpAssign:
876 if (node->getLeft()->isArray())
877 {
Olli Etuaho9638c352015-04-01 14:34:52 +0300878 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
879 if (rightAgg != nullptr && rightAgg->isConstructor())
880 {
881 const TString &functionName = addArrayConstructIntoFunction(node->getType());
882 out << functionName << "(";
883 node->getLeft()->traverse(this);
884 TIntermSequence *seq = rightAgg->getSequence();
885 for (auto &arrayElement : *seq)
886 {
887 out << ", ";
888 arrayElement->traverse(this);
889 }
890 out << ")";
891 return false;
892 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +0300893 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
894 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
895
896 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500897 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200898 }
899 else
900 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500901 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200902 }
903 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000904 case EOpInitialize:
905 if (visit == PreVisit)
906 {
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000907 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -0500908 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000909 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000910
Olli Etuaho3d932d82016-04-12 11:10:30 +0300911 // Global initializers must be constant at this point.
Olli Etuahod4f4c112016-04-15 15:11:24 +0300912 ASSERT(symbolNode->getQualifier() != EvqGlobal || canWriteAsHLSLLiteral(expression));
913
914 // GLSL allows to write things like "float x = x;" where a new variable x is defined
915 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
916 // new variable is created before the assignment is evaluated), so we need to convert
917 // this to "float t = x, x = t;".
Olli Etuaho3d932d82016-04-12 11:10:30 +0300918 if (writeSameSymbolInitializer(out, symbolNode, expression))
Jamie Madill37997142015-01-28 10:06:34 -0500919 {
920 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000921 return false;
922 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200923 else if (writeConstantInitialization(out, symbolNode, expression))
924 {
925 return false;
926 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000927 }
928 else if (visit == InVisit)
929 {
930 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000931 }
932 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500933 case EOpAddAssign:
934 outputTriplet(out, visit, "(", " += ", ")");
935 break;
936 case EOpSubAssign:
937 outputTriplet(out, visit, "(", " -= ", ")");
938 break;
939 case EOpMulAssign:
940 outputTriplet(out, visit, "(", " *= ", ")");
941 break;
942 case EOpVectorTimesScalarAssign:
943 outputTriplet(out, visit, "(", " *= ", ")");
944 break;
945 case EOpMatrixTimesScalarAssign:
946 outputTriplet(out, visit, "(", " *= ", ")");
947 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000948 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000949 if (visit == PreVisit)
950 {
951 out << "(";
952 }
953 else if (visit == InVisit)
954 {
955 out << " = mul(";
956 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -0400957 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000958 }
959 else
960 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000961 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000962 }
963 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000964 case EOpMatrixTimesMatrixAssign:
965 if (visit == PreVisit)
966 {
967 out << "(";
968 }
969 else if (visit == InVisit)
970 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200971 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000972 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +0200973 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000974 }
975 else
976 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200977 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000978 }
979 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500980 case EOpDivAssign:
981 outputTriplet(out, visit, "(", " /= ", ")");
982 break;
983 case EOpIModAssign:
984 outputTriplet(out, visit, "(", " %= ", ")");
985 break;
986 case EOpBitShiftLeftAssign:
987 outputTriplet(out, visit, "(", " <<= ", ")");
988 break;
989 case EOpBitShiftRightAssign:
990 outputTriplet(out, visit, "(", " >>= ", ")");
991 break;
992 case EOpBitwiseAndAssign:
993 outputTriplet(out, visit, "(", " &= ", ")");
994 break;
995 case EOpBitwiseXorAssign:
996 outputTriplet(out, visit, "(", " ^= ", ")");
997 break;
998 case EOpBitwiseOrAssign:
999 outputTriplet(out, visit, "(", " |= ", ")");
1000 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001001 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001002 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001003 const TType& leftType = node->getLeft()->getType();
1004 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001005 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001006 if (visit == PreVisit)
1007 {
1008 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1009 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001010 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001011 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001012 return false;
1013 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001014 }
Olli Etuaho96963162016-03-21 11:54:33 +02001015 else if (ancestorEvaluatesToSamplerInStruct(visit))
1016 {
1017 // All parts of an expression that access a sampler in a struct need to use _ as
1018 // separator to access the sampler variable that has been moved out of the struct.
1019 outputTriplet(out, visit, "", "_", "");
1020 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001021 else
1022 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001023 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001024 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001025 }
1026 break;
1027 case EOpIndexIndirect:
1028 // We do not currently support indirect references to interface blocks
1029 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001030 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001031 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001032 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001033 {
1034 const TStructure* structure = node->getLeft()->getType().getStruct();
1035 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1036 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001037
Olli Etuaho96963162016-03-21 11:54:33 +02001038 // In cases where indexing returns a sampler, we need to access the sampler variable
1039 // that has been moved out of the struct.
1040 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1041 if (visit == PreVisit && indexingReturnsSampler)
1042 {
1043 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1044 // This prefix is only output at the beginning of the indexing expression, which
1045 // may have multiple parts.
1046 out << "angle";
1047 }
1048 if (!indexingReturnsSampler)
1049 {
1050 // All parts of an expression that access a sampler in a struct need to use _ as
1051 // separator to access the sampler variable that has been moved out of the struct.
1052 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1053 }
1054 if (visit == InVisit)
1055 {
1056 if (indexingReturnsSampler)
1057 {
1058 out << "_" + field->name();
1059 }
1060 else
1061 {
1062 out << "." + DecorateField(field->name(), *structure);
1063 }
1064
1065 return false;
1066 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001067 }
1068 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001069 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001070 if (visit == InVisit)
1071 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001072 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1073 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1074 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001075 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001076
1077 return false;
1078 }
1079 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001080 case EOpAdd:
1081 outputTriplet(out, visit, "(", " + ", ")");
1082 break;
1083 case EOpSub:
1084 outputTriplet(out, visit, "(", " - ", ")");
1085 break;
1086 case EOpMul:
1087 outputTriplet(out, visit, "(", " * ", ")");
1088 break;
1089 case EOpDiv:
1090 outputTriplet(out, visit, "(", " / ", ")");
1091 break;
1092 case EOpIMod:
1093 outputTriplet(out, visit, "(", " % ", ")");
1094 break;
1095 case EOpBitShiftLeft:
1096 outputTriplet(out, visit, "(", " << ", ")");
1097 break;
1098 case EOpBitShiftRight:
1099 outputTriplet(out, visit, "(", " >> ", ")");
1100 break;
1101 case EOpBitwiseAnd:
1102 outputTriplet(out, visit, "(", " & ", ")");
1103 break;
1104 case EOpBitwiseXor:
1105 outputTriplet(out, visit, "(", " ^ ", ")");
1106 break;
1107 case EOpBitwiseOr:
1108 outputTriplet(out, visit, "(", " | ", ")");
1109 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001110 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001111 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001112 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001113 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001114 case EOpLessThan:
1115 outputTriplet(out, visit, "(", " < ", ")");
1116 break;
1117 case EOpGreaterThan:
1118 outputTriplet(out, visit, "(", " > ", ")");
1119 break;
1120 case EOpLessThanEqual:
1121 outputTriplet(out, visit, "(", " <= ", ")");
1122 break;
1123 case EOpGreaterThanEqual:
1124 outputTriplet(out, visit, "(", " >= ", ")");
1125 break;
1126 case EOpVectorTimesScalar:
1127 outputTriplet(out, visit, "(", " * ", ")");
1128 break;
1129 case EOpMatrixTimesScalar:
1130 outputTriplet(out, visit, "(", " * ", ")");
1131 break;
1132 case EOpVectorTimesMatrix:
1133 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1134 break;
1135 case EOpMatrixTimesVector:
1136 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1137 break;
1138 case EOpMatrixTimesMatrix:
1139 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1140 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001141 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001142 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1143 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001144 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001145 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001146 case EOpLogicalXor:
1147 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001148 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001149 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001150 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001151 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1152 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001153 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001154 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155 default: UNREACHABLE();
1156 }
1157
1158 return true;
1159}
1160
1161bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1162{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001163 TInfoSinkBase &out = getInfoSink();
1164
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165 switch (node->getOp())
1166 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001167 case EOpNegative:
1168 outputTriplet(out, visit, "(-", "", ")");
1169 break;
1170 case EOpPositive:
1171 outputTriplet(out, visit, "(+", "", ")");
1172 break;
1173 case EOpVectorLogicalNot:
1174 outputTriplet(out, visit, "(!", "", ")");
1175 break;
1176 case EOpLogicalNot:
1177 outputTriplet(out, visit, "(!", "", ")");
1178 break;
1179 case EOpBitwiseNot:
1180 outputTriplet(out, visit, "(~", "", ")");
1181 break;
1182 case EOpPostIncrement:
1183 outputTriplet(out, visit, "(", "", "++)");
1184 break;
1185 case EOpPostDecrement:
1186 outputTriplet(out, visit, "(", "", "--)");
1187 break;
1188 case EOpPreIncrement:
1189 outputTriplet(out, visit, "(++", "", ")");
1190 break;
1191 case EOpPreDecrement:
1192 outputTriplet(out, visit, "(--", "", ")");
1193 break;
1194 case EOpRadians:
1195 outputTriplet(out, visit, "radians(", "", ")");
1196 break;
1197 case EOpDegrees:
1198 outputTriplet(out, visit, "degrees(", "", ")");
1199 break;
1200 case EOpSin:
1201 outputTriplet(out, visit, "sin(", "", ")");
1202 break;
1203 case EOpCos:
1204 outputTriplet(out, visit, "cos(", "", ")");
1205 break;
1206 case EOpTan:
1207 outputTriplet(out, visit, "tan(", "", ")");
1208 break;
1209 case EOpAsin:
1210 outputTriplet(out, visit, "asin(", "", ")");
1211 break;
1212 case EOpAcos:
1213 outputTriplet(out, visit, "acos(", "", ")");
1214 break;
1215 case EOpAtan:
1216 outputTriplet(out, visit, "atan(", "", ")");
1217 break;
1218 case EOpSinh:
1219 outputTriplet(out, visit, "sinh(", "", ")");
1220 break;
1221 case EOpCosh:
1222 outputTriplet(out, visit, "cosh(", "", ")");
1223 break;
1224 case EOpTanh:
1225 outputTriplet(out, visit, "tanh(", "", ")");
1226 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001227 case EOpAsinh:
1228 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001229 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001230 break;
1231 case EOpAcosh:
1232 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001233 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001234 break;
1235 case EOpAtanh:
1236 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001237 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001238 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001239 case EOpExp:
1240 outputTriplet(out, visit, "exp(", "", ")");
1241 break;
1242 case EOpLog:
1243 outputTriplet(out, visit, "log(", "", ")");
1244 break;
1245 case EOpExp2:
1246 outputTriplet(out, visit, "exp2(", "", ")");
1247 break;
1248 case EOpLog2:
1249 outputTriplet(out, visit, "log2(", "", ")");
1250 break;
1251 case EOpSqrt:
1252 outputTriplet(out, visit, "sqrt(", "", ")");
1253 break;
1254 case EOpInverseSqrt:
1255 outputTriplet(out, visit, "rsqrt(", "", ")");
1256 break;
1257 case EOpAbs:
1258 outputTriplet(out, visit, "abs(", "", ")");
1259 break;
1260 case EOpSign:
1261 outputTriplet(out, visit, "sign(", "", ")");
1262 break;
1263 case EOpFloor:
1264 outputTriplet(out, visit, "floor(", "", ")");
1265 break;
1266 case EOpTrunc:
1267 outputTriplet(out, visit, "trunc(", "", ")");
1268 break;
1269 case EOpRound:
1270 outputTriplet(out, visit, "round(", "", ")");
1271 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001272 case EOpRoundEven:
1273 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001274 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001275 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001276 case EOpCeil:
1277 outputTriplet(out, visit, "ceil(", "", ")");
1278 break;
1279 case EOpFract:
1280 outputTriplet(out, visit, "frac(", "", ")");
1281 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301282 case EOpIsNan:
Shao6f0a0dc2016-09-27 13:51:29 +08001283 if (node->getUseEmulatedFunction())
1284 writeEmulatedFunctionTriplet(out, visit, "isnan(");
1285 else
1286 outputTriplet(out, visit, "isnan(", "", ")");
1287 mRequiresIEEEStrictCompiling = true;
1288 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001289 case EOpIsInf:
1290 outputTriplet(out, visit, "isinf(", "", ")");
1291 break;
1292 case EOpFloatBitsToInt:
1293 outputTriplet(out, visit, "asint(", "", ")");
1294 break;
1295 case EOpFloatBitsToUint:
1296 outputTriplet(out, visit, "asuint(", "", ")");
1297 break;
1298 case EOpIntBitsToFloat:
1299 outputTriplet(out, visit, "asfloat(", "", ")");
1300 break;
1301 case EOpUintBitsToFloat:
1302 outputTriplet(out, visit, "asfloat(", "", ")");
1303 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001304 case EOpPackSnorm2x16:
1305 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001306 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001307 break;
1308 case EOpPackUnorm2x16:
1309 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001310 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001311 break;
1312 case EOpPackHalf2x16:
1313 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001314 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001315 break;
1316 case EOpUnpackSnorm2x16:
1317 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001318 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001319 break;
1320 case EOpUnpackUnorm2x16:
1321 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001322 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001323 break;
1324 case EOpUnpackHalf2x16:
1325 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001326 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001327 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001328 case EOpLength:
1329 outputTriplet(out, visit, "length(", "", ")");
1330 break;
1331 case EOpNormalize:
1332 outputTriplet(out, visit, "normalize(", "", ")");
1333 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001334 case EOpDFdx:
1335 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1336 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001337 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001338 }
1339 else
1340 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001341 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001342 }
1343 break;
1344 case EOpDFdy:
1345 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1346 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001347 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001348 }
1349 else
1350 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001351 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001352 }
1353 break;
1354 case EOpFwidth:
1355 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1356 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001357 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001358 }
1359 else
1360 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001361 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001362 }
1363 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001364 case EOpTranspose:
1365 outputTriplet(out, visit, "transpose(", "", ")");
1366 break;
1367 case EOpDeterminant:
1368 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1369 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001370 case EOpInverse:
1371 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001372 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001373 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001374
Jamie Madill8c46ab12015-12-07 16:39:19 -05001375 case EOpAny:
1376 outputTriplet(out, visit, "any(", "", ")");
1377 break;
1378 case EOpAll:
1379 outputTriplet(out, visit, "all(", "", ")");
1380 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381 default: UNREACHABLE();
1382 }
1383
1384 return true;
1385}
1386
Olli Etuaho96963162016-03-21 11:54:33 +02001387TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1388{
1389 if (node->getAsSymbolNode())
1390 {
1391 return node->getAsSymbolNode()->getSymbol();
1392 }
1393 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1394 switch (nodeBinary->getOp())
1395 {
1396 case EOpIndexDirect:
1397 {
1398 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1399
1400 TInfoSinkBase prefixSink;
1401 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1402 return TString(prefixSink.c_str());
1403 }
1404 case EOpIndexDirectStruct:
1405 {
1406 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1407 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1408 const TField *field = s->fields()[index];
1409
1410 TInfoSinkBase prefixSink;
1411 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1412 << field->name();
1413 return TString(prefixSink.c_str());
1414 }
1415 default:
1416 UNREACHABLE();
1417 return TString("");
1418 }
1419}
1420
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001421bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1422{
Jamie Madill32aab012015-01-27 14:12:26 -05001423 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001424
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001425 switch (node->getOp())
1426 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001427 case EOpSequence:
1428 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001429 if (mInsideFunction)
1430 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001431 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001432 out << "{\n";
1433 }
1434
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001435 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001436 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001437 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001438
Olli Etuahoa6f22092015-05-08 18:31:10 +03001439 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001440
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001441 // Don't output ; after case labels, they're terminated by :
1442 // This is needed especially since outputting a ; after a case statement would turn empty
1443 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho57961272016-09-14 13:57:46 +03001444 // Also no need to output ; after if statements or sequences. This is done just for
1445 // code clarity.
1446 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001447 !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001448 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001449 }
1450
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001451 if (mInsideFunction)
1452 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001453 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001454 out << "}\n";
1455 }
1456
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001457 return false;
1458 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001459 case EOpDeclaration:
1460 if (visit == PreVisit)
1461 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001462 TIntermSequence *sequence = node->getSequence();
1463 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001464 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001465
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001466 if (variable &&
1467 (variable->getQualifier() == EvqTemporary ||
1468 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001469 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001470 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001471
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001472 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001473 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001474 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001475 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001476 out << "static ";
1477 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001478
Olli Etuahoa6f22092015-05-08 18:31:10 +03001479 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001480
Olli Etuahoa6f22092015-05-08 18:31:10 +03001481 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001482
Olli Etuahoa6f22092015-05-08 18:31:10 +03001483 if (symbol)
1484 {
1485 symbol->traverse(this);
1486 out << ArrayString(symbol->getType());
1487 out << " = " + initializer(symbol->getType());
1488 }
1489 else
1490 {
1491 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001492 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001493 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001494 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1495 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001496 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001497 }
1498 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499 }
Jamie Madill033dae62014-06-18 12:56:28 -04001500 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001501 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001502 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001503 {
1504 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1505
1506 if (symbol)
1507 {
1508 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1509 mReferencedVaryings[symbol->getSymbol()] = symbol;
1510 }
1511 else
1512 {
1513 (*sit)->traverse(this);
1514 }
1515 }
1516 }
1517
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001518 return false;
1519 }
1520 else if (visit == InVisit)
1521 {
1522 out << ", ";
1523 }
1524 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001525 case EOpInvariantDeclaration:
1526 // Do not do any translation
1527 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001528 case EOpPrototype:
1529 if (visit == PreVisit)
1530 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001531 size_t index = mCallDag.findIndex(node);
1532 // Skip the prototype if it is not implemented (and thus not used)
1533 if (index == CallDAG::InvalidIndex)
1534 {
1535 return false;
1536 }
1537
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001538 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001539
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001540 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1541 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1542 << (mOutputLod0Function ? "Lod0(" : "(");
1543
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001544 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001545 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001546 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001547
1548 if (symbol)
1549 {
1550 out << argumentString(symbol);
1551
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001552 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001553 {
1554 out << ", ";
1555 }
1556 }
1557 else UNREACHABLE();
1558 }
1559
1560 out << ");\n";
1561
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001562 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001563 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1564 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001565 {
1566 mOutputLod0Function = true;
1567 node->traverse(this);
1568 mOutputLod0Function = false;
1569 }
1570
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001571 return false;
1572 }
1573 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001574 case EOpComma:
1575 outputTriplet(out, visit, "(", ", ", ")");
1576 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577 case EOpFunction:
1578 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001579 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001580 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001581
Corentin Wallez1239ee92015-03-19 14:38:02 -07001582 size_t index = mCallDag.findIndex(node);
1583 ASSERT(index != CallDAG::InvalidIndex);
1584 mCurrentFunctionMetadata = &mASTMetadataList[index];
1585
Jamie Madill033dae62014-06-18 12:56:28 -04001586 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001587
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001588 TIntermSequence *sequence = node->getSequence();
1589 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
1590
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001591 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001593 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001595 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001596 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001597 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001598 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001599 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001600
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001601 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001602 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001603 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001604
1605 if (symbol)
1606 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001607 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001608
1609 out << argumentString(symbol);
1610
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001611 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001612 {
1613 out << ", ";
1614 }
1615 }
1616 else UNREACHABLE();
1617 }
1618
Olli Etuaho4785fec2015-05-18 16:09:37 +03001619 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001620
Olli Etuahof51fdd22016-10-03 10:03:40 +01001621 mInsideFunction = true;
1622 ASSERT(sequence->size() == 2);
1623 TIntermNode *body = (*sequence)[1];
1624 // The function body node will output braces.
1625 ASSERT(IsSequence(body));
1626 body->traverse(this);
1627 mInsideFunction = false;
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001628
Corentin Wallez1239ee92015-03-19 14:38:02 -07001629 mCurrentFunctionMetadata = nullptr;
1630
1631 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1632 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001633 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001634 ASSERT(name != "main");
1635 mOutputLod0Function = true;
1636 node->traverse(this);
1637 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001638 }
1639
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001640 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641 }
1642 break;
1643 case EOpFunctionCall:
1644 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001645 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001646
Corentin Wallez1239ee92015-03-19 14:38:02 -07001647 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001648 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001650 if (node->isArray())
1651 {
1652 UNIMPLEMENTED();
1653 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07001654 size_t index = mCallDag.findIndex(node);
1655 ASSERT(index != CallDAG::InvalidIndex);
1656 lod0 &= mASTMetadataList[index].mNeedsLod0;
1657
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001658 out << DecorateFunctionIfNeeded(node->getNameObj());
1659 out << DisambiguateFunctionName(node->getSequence());
1660 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001661 }
Olli Etuahob741c762016-06-29 15:49:22 +03001662 else if (node->getNameObj().isInternal())
1663 {
1664 // This path is used for internal functions that don't have their definitions in the
1665 // AST, such as precision emulation functions.
1666 out << DecorateFunctionIfNeeded(node->getNameObj()) << "(";
1667 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 else
1669 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001670 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001671 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001672 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1673 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1674 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1675 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001677
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001678 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001679 {
Olli Etuaho96963162016-03-21 11:54:33 +02001680 TIntermTyped *typedArg = (*arg)->getAsTyped();
1681 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001682 {
1683 out << "texture_";
1684 (*arg)->traverse(this);
1685 out << ", sampler_";
1686 }
1687
1688 (*arg)->traverse(this);
1689
Olli Etuaho96963162016-03-21 11:54:33 +02001690 if (typedArg->getType().isStructureContainingSamplers())
1691 {
1692 const TType &argType = typedArg->getType();
1693 TVector<TIntermSymbol *> samplerSymbols;
1694 TString structName = samplerNamePrefixFromStruct(typedArg);
1695 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001696 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001697 &samplerSymbols, nullptr);
1698 for (const TIntermSymbol *sampler : samplerSymbols)
1699 {
1700 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1701 {
1702 out << ", texture_" << sampler->getSymbol();
1703 out << ", sampler_" << sampler->getSymbol();
1704 }
1705 else
1706 {
1707 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1708 // of D3D9, it's the sampler variable.
1709 out << ", " + sampler->getSymbol();
1710 }
1711 }
1712 }
1713
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001714 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001715 {
1716 out << ", ";
1717 }
1718 }
1719
1720 out << ")";
1721
1722 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723 }
1724 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001725 case EOpParameters:
1726 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1727 break;
1728 case EOpConstructFloat:
1729 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1730 break;
1731 case EOpConstructVec2:
1732 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1733 break;
1734 case EOpConstructVec3:
1735 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1736 break;
1737 case EOpConstructVec4:
1738 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1739 break;
1740 case EOpConstructBool:
1741 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1742 break;
1743 case EOpConstructBVec2:
1744 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1745 break;
1746 case EOpConstructBVec3:
1747 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1748 break;
1749 case EOpConstructBVec4:
1750 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1751 break;
1752 case EOpConstructInt:
1753 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1754 break;
1755 case EOpConstructIVec2:
1756 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1757 break;
1758 case EOpConstructIVec3:
1759 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1760 break;
1761 case EOpConstructIVec4:
1762 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1763 break;
1764 case EOpConstructUInt:
1765 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1766 break;
1767 case EOpConstructUVec2:
1768 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1769 break;
1770 case EOpConstructUVec3:
1771 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1772 break;
1773 case EOpConstructUVec4:
1774 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1775 break;
1776 case EOpConstructMat2:
1777 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1778 break;
1779 case EOpConstructMat2x3:
1780 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1781 break;
1782 case EOpConstructMat2x4:
1783 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1784 break;
1785 case EOpConstructMat3x2:
1786 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1787 break;
1788 case EOpConstructMat3:
1789 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1790 break;
1791 case EOpConstructMat3x4:
1792 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1793 break;
1794 case EOpConstructMat4x2:
1795 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1796 break;
1797 case EOpConstructMat4x3:
1798 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1799 break;
1800 case EOpConstructMat4:
1801 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1802 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001803 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001804 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001805 if (node->getType().isArray())
1806 {
1807 UNIMPLEMENTED();
1808 }
Jamie Madill033dae62014-06-18 12:56:28 -04001809 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001810 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001811 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001812 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001813 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001814 case EOpLessThan:
1815 outputTriplet(out, visit, "(", " < ", ")");
1816 break;
1817 case EOpGreaterThan:
1818 outputTriplet(out, visit, "(", " > ", ")");
1819 break;
1820 case EOpLessThanEqual:
1821 outputTriplet(out, visit, "(", " <= ", ")");
1822 break;
1823 case EOpGreaterThanEqual:
1824 outputTriplet(out, visit, "(", " >= ", ")");
1825 break;
1826 case EOpVectorEqual:
1827 outputTriplet(out, visit, "(", " == ", ")");
1828 break;
1829 case EOpVectorNotEqual:
1830 outputTriplet(out, visit, "(", " != ", ")");
1831 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001832 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001833 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001834 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001835 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001836 case EOpModf:
1837 outputTriplet(out, visit, "modf(", ", ", ")");
1838 break;
1839 case EOpPow:
1840 outputTriplet(out, visit, "pow(", ", ", ")");
1841 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001843 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02001844 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001845 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001846 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001847 case EOpMin:
1848 outputTriplet(out, visit, "min(", ", ", ")");
1849 break;
1850 case EOpMax:
1851 outputTriplet(out, visit, "max(", ", ", ")");
1852 break;
1853 case EOpClamp:
1854 outputTriplet(out, visit, "clamp(", ", ", ")");
1855 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301856 case EOpMix:
1857 {
1858 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1859 if (lastParamNode->getType().getBasicType() == EbtBool)
1860 {
1861 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1862 // so use emulated version.
1863 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001864 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301865 }
1866 else
1867 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001868 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301869 }
1870 }
1871 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001872 case EOpStep:
1873 outputTriplet(out, visit, "step(", ", ", ")");
1874 break;
1875 case EOpSmoothStep:
1876 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1877 break;
1878 case EOpDistance:
1879 outputTriplet(out, visit, "distance(", ", ", ")");
1880 break;
1881 case EOpDot:
1882 outputTriplet(out, visit, "dot(", ", ", ")");
1883 break;
1884 case EOpCross:
1885 outputTriplet(out, visit, "cross(", ", ", ")");
1886 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001887 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001888 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001889 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001890 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001891 case EOpReflect:
1892 outputTriplet(out, visit, "reflect(", ", ", ")");
1893 break;
1894 case EOpRefract:
1895 outputTriplet(out, visit, "refract(", ", ", ")");
1896 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001897 case EOpOuterProduct:
1898 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001899 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02001900 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001901 case EOpMul:
1902 outputTriplet(out, visit, "(", " * ", ")");
1903 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001904 default: UNREACHABLE();
1905 }
1906
1907 return true;
1908}
1909
Olli Etuaho57961272016-09-14 13:57:46 +03001910void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001912 out << "if (";
1913
1914 node->getCondition()->traverse(this);
1915
1916 out << ")\n";
1917
Jamie Madill8c46ab12015-12-07 16:39:19 -05001918 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001919
1920 bool discard = false;
1921
1922 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001924 // The trueBlock child node will output braces.
1925 ASSERT(IsSequence(node->getTrueBlock()));
1926
Olli Etuahoa6f22092015-05-08 18:31:10 +03001927 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001928
Olli Etuahoa6f22092015-05-08 18:31:10 +03001929 // Detect true discard
1930 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1931 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001932 else
1933 {
1934 // TODO(oetuaho): Check if the semicolon inside is necessary.
1935 // It's there as a result of conservative refactoring of the output.
1936 out << "{;}\n";
1937 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001938
Jamie Madill8c46ab12015-12-07 16:39:19 -05001939 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001940
Olli Etuahoa6f22092015-05-08 18:31:10 +03001941 if (node->getFalseBlock())
1942 {
1943 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001944
Jamie Madill8c46ab12015-12-07 16:39:19 -05001945 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001946
Olli Etuaho32db19b2016-10-04 14:43:16 +01001947 // The falseBlock child node will output braces.
1948 ASSERT(IsSequence(node->getFalseBlock()));
Olli Etuaho4785fec2015-05-18 16:09:37 +03001949
Olli Etuahoa6f22092015-05-08 18:31:10 +03001950 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001951
Jamie Madill8c46ab12015-12-07 16:39:19 -05001952 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001953
Olli Etuahoa6f22092015-05-08 18:31:10 +03001954 // Detect false discard
1955 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1956 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001957
Olli Etuahoa6f22092015-05-08 18:31:10 +03001958 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001959 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001960 {
1961 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001962 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001963}
1964
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001965bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1966{
1967 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1968 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1969 UNREACHABLE();
1970 return false;
1971}
1972
Olli Etuaho57961272016-09-14 13:57:46 +03001973bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001974{
1975 TInfoSinkBase &out = getInfoSink();
1976
Olli Etuaho3d932d82016-04-12 11:10:30 +03001977 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001978
1979 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001980 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001981 {
1982 out << "FLATTEN ";
1983 }
1984
Olli Etuaho57961272016-09-14 13:57:46 +03001985 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001986
1987 return false;
1988}
1989
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001990bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02001991{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001992 TInfoSinkBase &out = getInfoSink();
1993
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001994 if (node->getStatementList())
1995 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02001996 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05001997 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001998 // The curly braces get written when visiting the statementList aggregate
1999 }
2000 else
2001 {
2002 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002003 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002004 }
2005 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002006}
2007
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002008bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002009{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002010 TInfoSinkBase &out = getInfoSink();
2011
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002012 if (node->hasCondition())
2013 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002014 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002015 return true;
2016 }
2017 else
2018 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002019 out << "default:\n";
2020 return false;
2021 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002022}
2023
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2025{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002026 TInfoSinkBase &out = getInfoSink();
2027 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028}
2029
2030bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2031{
Nicolas Capens655fe362014-04-11 13:12:34 -04002032 mNestedLoopDepth++;
2033
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002034 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002035 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002036 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002037
Jamie Madill8c46ab12015-12-07 16:39:19 -05002038 TInfoSinkBase &out = getInfoSink();
2039
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002040 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002041 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002042 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002043 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002044 mInsideDiscontinuousLoop = wasDiscontinuous;
2045 mNestedLoopDepth--;
2046
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002047 return false;
2048 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002049 }
2050
Corentin Wallez1239ee92015-03-19 14:38:02 -07002051 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002052 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002054 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002055
Jamie Madill8c46ab12015-12-07 16:39:19 -05002056 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057 }
2058 else
2059 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002060 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002061
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 if (node->getInit())
2063 {
2064 node->getInit()->traverse(this);
2065 }
2066
2067 out << "; ";
2068
alokp@chromium.org52813552010-11-16 18:36:09 +00002069 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002071 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002072 }
2073
2074 out << "; ";
2075
alokp@chromium.org52813552010-11-16 18:36:09 +00002076 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002078 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 }
2080
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002081 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002082
Jamie Madill8c46ab12015-12-07 16:39:19 -05002083 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 }
2085
2086 if (node->getBody())
2087 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002088 // The loop body node will output braces.
2089 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002090 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002092 else
2093 {
2094 // TODO(oetuaho): Check if the semicolon inside is necessary.
2095 // It's there as a result of conservative refactoring of the output.
2096 out << "{;}\n";
2097 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098
Jamie Madill8c46ab12015-12-07 16:39:19 -05002099 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100
alokp@chromium.org52813552010-11-16 18:36:09 +00002101 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002103 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 out << "while(\n";
2105
alokp@chromium.org52813552010-11-16 18:36:09 +00002106 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107
daniel@transgaming.com73536982012-03-21 20:45:49 +00002108 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 }
2110
daniel@transgaming.com73536982012-03-21 20:45:49 +00002111 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002113 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002114 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002115
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116 return false;
2117}
2118
2119bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2120{
Jamie Madill32aab012015-01-27 14:12:26 -05002121 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122
2123 switch (node->getFlowOp())
2124 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002125 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002126 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002127 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002128 case EOpBreak:
2129 if (visit == PreVisit)
2130 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002131 if (mNestedLoopDepth > 1)
2132 {
2133 mUsesNestedBreak = true;
2134 }
2135
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002136 if (mExcessiveLoopIndex)
2137 {
2138 out << "{Break";
2139 mExcessiveLoopIndex->traverse(this);
2140 out << " = true; break;}\n";
2141 }
2142 else
2143 {
2144 out << "break;\n";
2145 }
2146 }
2147 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002148 case EOpContinue:
2149 outputTriplet(out, visit, "continue;\n", "", "");
2150 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151 case EOpReturn:
2152 if (visit == PreVisit)
2153 {
2154 if (node->getExpression())
2155 {
2156 out << "return ";
2157 }
2158 else
2159 {
2160 out << "return;\n";
2161 }
2162 }
2163 else if (visit == PostVisit)
2164 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002165 if (node->getExpression())
2166 {
2167 out << ";\n";
2168 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169 }
2170 break;
2171 default: UNREACHABLE();
2172 }
2173
2174 return true;
2175}
2176
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002177bool OutputHLSL::isSingleStatement(TIntermNode *node)
2178{
2179 TIntermAggregate *aggregate = node->getAsAggregate();
2180
2181 if (aggregate)
2182 {
2183 if (aggregate->getOp() == EOpSequence)
2184 {
2185 return false;
2186 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002187 else if (aggregate->getOp() == EOpDeclaration)
2188 {
2189 // Declaring multiple comma-separated variables must be considered multiple statements
2190 // because each individual declaration has side effects which are visible in the next.
2191 return false;
2192 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002193 else
2194 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002195 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002196 {
2197 if (!isSingleStatement(*sit))
2198 {
2199 return false;
2200 }
2201 }
2202
2203 return true;
2204 }
2205 }
2206
2207 return true;
2208}
2209
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002210// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2211// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002212bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002213{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002214 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002215
2216 // Parse loops of the form:
2217 // for(int index = initial; index [comparator] limit; index += increment)
2218 TIntermSymbol *index = NULL;
2219 TOperator comparator = EOpNull;
2220 int initial = 0;
2221 int limit = 0;
2222 int increment = 0;
2223
2224 // Parse index name and intial value
2225 if (node->getInit())
2226 {
2227 TIntermAggregate *init = node->getInit()->getAsAggregate();
2228
2229 if (init)
2230 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002231 TIntermSequence *sequence = init->getSequence();
2232 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002233
2234 if (variable && variable->getQualifier() == EvqTemporary)
2235 {
2236 TIntermBinary *assign = variable->getAsBinaryNode();
2237
2238 if (assign->getOp() == EOpInitialize)
2239 {
2240 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2241 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2242
2243 if (symbol && constant)
2244 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002245 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002246 {
2247 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002248 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002249 }
2250 }
2251 }
2252 }
2253 }
2254 }
2255
2256 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002257 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002258 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002259 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002260
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002261 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2262 {
2263 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2264
2265 if (constant)
2266 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002267 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002268 {
2269 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002270 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002271 }
2272 }
2273 }
2274 }
2275
2276 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002277 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002278 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002279 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2280 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002281
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002282 if (binaryTerminal)
2283 {
2284 TOperator op = binaryTerminal->getOp();
2285 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2286
2287 if (constant)
2288 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002289 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002290 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002291 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002292
2293 switch (op)
2294 {
2295 case EOpAddAssign: increment = value; break;
2296 case EOpSubAssign: increment = -value; break;
2297 default: UNIMPLEMENTED();
2298 }
2299 }
2300 }
2301 }
2302 else if (unaryTerminal)
2303 {
2304 TOperator op = unaryTerminal->getOp();
2305
2306 switch (op)
2307 {
2308 case EOpPostIncrement: increment = 1; break;
2309 case EOpPostDecrement: increment = -1; break;
2310 case EOpPreIncrement: increment = 1; break;
2311 case EOpPreDecrement: increment = -1; break;
2312 default: UNIMPLEMENTED();
2313 }
2314 }
2315 }
2316
2317 if (index != NULL && comparator != EOpNull && increment != 0)
2318 {
2319 if (comparator == EOpLessThanEqual)
2320 {
2321 comparator = EOpLessThan;
2322 limit += 1;
2323 }
2324
2325 if (comparator == EOpLessThan)
2326 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002327 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002328
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002329 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002330 {
2331 return false; // Not an excessive loop
2332 }
2333
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002334 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2335 mExcessiveLoopIndex = index;
2336
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002337 out << "{int ";
2338 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002339 out << ";\n"
2340 "bool Break";
2341 index->traverse(this);
2342 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002343
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002344 bool firstLoopFragment = true;
2345
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002346 while (iterations > 0)
2347 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002348 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002349
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002350 if (!firstLoopFragment)
2351 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002352 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002353 index->traverse(this);
2354 out << ") {\n";
2355 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002356
2357 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2358 {
2359 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2360 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002361
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002362 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002363 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002364
Corentin Wallez1239ee92015-03-19 14:38:02 -07002365 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002366 index->traverse(this);
2367 out << " = ";
2368 out << initial;
2369
2370 out << "; ";
2371 index->traverse(this);
2372 out << " < ";
2373 out << clampedLimit;
2374
2375 out << "; ";
2376 index->traverse(this);
2377 out << " += ";
2378 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002379 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002380
Jamie Madill8c46ab12015-12-07 16:39:19 -05002381 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002382 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002383
2384 if (node->getBody())
2385 {
2386 node->getBody()->traverse(this);
2387 }
2388
Jamie Madill8c46ab12015-12-07 16:39:19 -05002389 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002390 out << ";}\n";
2391
2392 if (!firstLoopFragment)
2393 {
2394 out << "}\n";
2395 }
2396
2397 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002398
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002399 initial += MAX_LOOP_ITERATIONS * increment;
2400 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002402
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002403 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002404
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002405 mExcessiveLoopIndex = restoreIndex;
2406
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002407 return true;
2408 }
2409 else UNIMPLEMENTED();
2410 }
2411
2412 return false; // Not handled as an excessive loop
2413}
2414
Jamie Madill8c46ab12015-12-07 16:39:19 -05002415void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2416 Visit visit,
2417 const char *preString,
2418 const char *inString,
2419 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002421 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 {
2423 out << preString;
2424 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002425 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 {
2427 out << inString;
2428 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002429 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 {
2431 out << postString;
2432 }
2433}
2434
Jamie Madill8c46ab12015-12-07 16:39:19 -05002435void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002436{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002437 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002438 {
Jamie Madill32aab012015-01-27 14:12:26 -05002439 out << "\n";
2440 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002441
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002442 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002443 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002444 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002445 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002446
Jamie Madill32aab012015-01-27 14:12:26 -05002447 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002448 }
2449}
2450
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002451TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2452{
2453 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002454 const TType &type = symbol->getType();
2455 const TName &name = symbol->getName();
2456 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002457
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002458 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002459 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002460 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002461 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002462 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002463 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002464 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002465 }
2466
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002467 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002468 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002469 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2470 {
2471 // Samplers are passed as indices to the sampler array.
2472 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2473 return "const uint " + nameStr + ArrayString(type);
2474 }
2475 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2476 {
2477 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2478 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2479 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2480 ArrayString(type);
2481 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002482 }
2483
Olli Etuaho96963162016-03-21 11:54:33 +02002484 TStringStream argString;
2485 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2486 << ArrayString(type);
2487
2488 // If the structure parameter contains samplers, they need to be passed into the function as
2489 // separate parameters. HLSL doesn't natively support samplers in structs.
2490 if (type.isStructureContainingSamplers())
2491 {
2492 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2493 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002494 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002495 for (const TIntermSymbol *sampler : samplerSymbols)
2496 {
2497 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2498 {
2499 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2500 }
2501 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2502 {
2503 const TType &samplerType = sampler->getType();
2504 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2505 type.getArraySize() == samplerType.getArraySize());
2506 ASSERT(IsSampler(samplerType.getBasicType()));
2507 argString << ", " << QualifierString(qualifier) << " "
2508 << TextureString(samplerType.getBasicType()) << " texture_"
2509 << sampler->getSymbol() << ArrayString(type) << ", "
2510 << QualifierString(qualifier) << " "
2511 << SamplerString(samplerType.getBasicType()) << " sampler_"
2512 << sampler->getSymbol() << ArrayString(type);
2513 }
2514 else
2515 {
2516 const TType &samplerType = sampler->getType();
2517 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2518 type.getArraySize() == samplerType.getArraySize());
2519 ASSERT(IsSampler(samplerType.getBasicType()));
2520 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2521 << " " << sampler->getSymbol() << ArrayString(type);
2522 }
2523 }
2524 }
2525
2526 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527}
2528
2529TString OutputHLSL::initializer(const TType &type)
2530{
2531 TString string;
2532
Jamie Madill94bf7f22013-07-08 13:31:15 -04002533 size_t size = type.getObjectSize();
2534 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002536 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537
Jamie Madill94bf7f22013-07-08 13:31:15 -04002538 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 {
2540 string += ", ";
2541 }
2542 }
2543
daniel@transgaming.comead23042010-04-29 03:35:36 +00002544 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002546
Jamie Madill8c46ab12015-12-07 16:39:19 -05002547void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2548 Visit visit,
2549 const TType &type,
2550 const char *name,
2551 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002552{
Olli Etuahof40319e2015-03-10 14:33:00 +02002553 if (type.isArray())
2554 {
2555 UNIMPLEMENTED();
2556 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002557
2558 if (visit == PreVisit)
2559 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002560 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002561
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002562 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002563 }
2564 else if (visit == InVisit)
2565 {
2566 out << ", ";
2567 }
2568 else if (visit == PostVisit)
2569 {
2570 out << ")";
2571 }
2572}
2573
Jamie Madill8c46ab12015-12-07 16:39:19 -05002574const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2575 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002576 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002577{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002578 const TConstantUnion *constUnionIterated = constUnion;
2579
Jamie Madill98493dd2013-07-08 14:39:03 -04002580 const TStructure* structure = type.getStruct();
2581 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002582 {
Jamie Madill033dae62014-06-18 12:56:28 -04002583 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002584
Jamie Madill98493dd2013-07-08 14:39:03 -04002585 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002586
Jamie Madill98493dd2013-07-08 14:39:03 -04002587 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002588 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002589 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002590 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002591
Jamie Madill98493dd2013-07-08 14:39:03 -04002592 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002593 {
2594 out << ", ";
2595 }
2596 }
2597
2598 out << ")";
2599 }
2600 else
2601 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002602 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002603 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002604
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002605 if (writeType)
2606 {
Jamie Madill033dae62014-06-18 12:56:28 -04002607 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002608 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002609 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002610 if (writeType)
2611 {
2612 out << ")";
2613 }
2614 }
2615
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002616 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002617}
2618
Jamie Madill8c46ab12015-12-07 16:39:19 -05002619void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002620{
2621 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002622 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002623}
2624
Jamie Madill37997142015-01-28 10:06:34 -05002625bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2626{
2627 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2628 expression->traverse(&searchSymbol);
2629
2630 if (searchSymbol.foundMatch())
2631 {
2632 // Type already printed
2633 out << "t" + str(mUniqueIndex) + " = ";
2634 expression->traverse(this);
2635 out << ", ";
2636 symbolNode->traverse(this);
2637 out << " = t" + str(mUniqueIndex);
2638
2639 mUniqueIndex++;
2640 return true;
2641 }
2642
2643 return false;
2644}
2645
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002646bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2647{
2648 // We support writing constant unions and constructors that only take constant unions as
2649 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002650 return expression->getAsConstantUnion() ||
2651 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002652}
2653
2654bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2655 TIntermSymbol *symbolNode,
2656 TIntermTyped *expression)
2657{
2658 if (canWriteAsHLSLLiteral(expression))
2659 {
2660 symbolNode->traverse(this);
2661 if (expression->getType().isArray())
2662 {
2663 out << "[" << expression->getType().getArraySize() << "]";
2664 }
2665 out << " = {";
2666 if (expression->getAsConstantUnion())
2667 {
2668 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2669 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2670 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2671 }
2672 else
2673 {
2674 TIntermAggregate *constructor = expression->getAsAggregate();
2675 ASSERT(constructor != nullptr);
2676 for (TIntermNode *&node : *constructor->getSequence())
2677 {
2678 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2679 ASSERT(nodeConst);
2680 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2681 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2682 if (node != constructor->getSequence()->back())
2683 {
2684 out << ", ";
2685 }
2686 }
2687 }
2688 out << "}";
2689 return true;
2690 }
2691 return false;
2692}
2693
Jamie Madill55e79e02015-02-09 15:35:00 -05002694TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2695{
2696 const TFieldList &fields = structure.fields();
2697
2698 for (const auto &eqFunction : mStructEqualityFunctions)
2699 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002700 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002701 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002702 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002703 }
2704 }
2705
2706 const TString &structNameString = StructNameString(structure);
2707
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002708 StructEqualityFunction *function = new StructEqualityFunction();
2709 function->structure = &structure;
2710 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002711
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002712 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002713
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002714 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2715 << "{\n"
2716 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002717
2718 for (size_t i = 0; i < fields.size(); i++)
2719 {
2720 const TField *field = fields[i];
2721 const TType *fieldType = field->type();
2722
2723 const TString &fieldNameA = "a." + Decorate(field->name());
2724 const TString &fieldNameB = "b." + Decorate(field->name());
2725
2726 if (i > 0)
2727 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002728 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002729 }
2730
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002731 fnOut << "(";
2732 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2733 fnOut << fieldNameA;
2734 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2735 fnOut << fieldNameB;
2736 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2737 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002738 }
2739
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002740 fnOut << ";\n" << "}\n";
2741
2742 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002743
2744 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002745 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002746
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002747 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002748}
2749
Olli Etuaho7fb49552015-03-18 17:27:44 +02002750TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2751{
2752 for (const auto &eqFunction : mArrayEqualityFunctions)
2753 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002754 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002755 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002756 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002757 }
2758 }
2759
2760 const TString &typeName = TypeString(type);
2761
Olli Etuaho12690762015-03-31 12:55:28 +03002762 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002763 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002764
2765 TInfoSinkBase fnNameOut;
2766 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002767 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002768
2769 TType nonArrayType = type;
2770 nonArrayType.clearArrayness();
2771
2772 TInfoSinkBase fnOut;
2773
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002774 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002775 << typeName << " a[" << type.getArraySize() << "], "
2776 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002777 << "{\n"
2778 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2779 " {\n"
2780 " if (";
2781
2782 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2783 fnOut << "a[i]";
2784 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2785 fnOut << "b[i]";
2786 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2787
2788 fnOut << ") { return false; }\n"
2789 " }\n"
2790 " return true;\n"
2791 "}\n";
2792
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002793 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002794
2795 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002796 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002797
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002798 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002799}
2800
Olli Etuaho12690762015-03-31 12:55:28 +03002801TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2802{
2803 for (const auto &assignFunction : mArrayAssignmentFunctions)
2804 {
2805 if (assignFunction.type == type)
2806 {
2807 return assignFunction.functionName;
2808 }
2809 }
2810
2811 const TString &typeName = TypeString(type);
2812
2813 ArrayHelperFunction function;
2814 function.type = type;
2815
2816 TInfoSinkBase fnNameOut;
2817 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2818 function.functionName = fnNameOut.c_str();
2819
2820 TInfoSinkBase fnOut;
2821
2822 fnOut << "void " << function.functionName << "(out "
2823 << typeName << " a[" << type.getArraySize() << "], "
2824 << typeName << " b[" << type.getArraySize() << "])\n"
2825 << "{\n"
2826 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2827 " {\n"
2828 " a[i] = b[i];\n"
2829 " }\n"
2830 "}\n";
2831
2832 function.functionDefinition = fnOut.c_str();
2833
2834 mArrayAssignmentFunctions.push_back(function);
2835
2836 return function.functionName;
2837}
2838
Olli Etuaho9638c352015-04-01 14:34:52 +03002839TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2840{
2841 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2842 {
2843 if (constructIntoFunction.type == type)
2844 {
2845 return constructIntoFunction.functionName;
2846 }
2847 }
2848
2849 const TString &typeName = TypeString(type);
2850
2851 ArrayHelperFunction function;
2852 function.type = type;
2853
2854 TInfoSinkBase fnNameOut;
2855 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2856 function.functionName = fnNameOut.c_str();
2857
2858 TInfoSinkBase fnOut;
2859
2860 fnOut << "void " << function.functionName << "(out "
2861 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002862 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002863 {
2864 fnOut << ", " << typeName << " b" << i;
2865 }
2866 fnOut << ")\n"
2867 "{\n";
2868
Olli Etuaho856c4972016-08-08 11:38:39 +03002869 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002870 {
2871 fnOut << " a[" << i << "] = b" << i << ";\n";
2872 }
2873 fnOut << "}\n";
2874
2875 function.functionDefinition = fnOut.c_str();
2876
2877 mArrayConstructIntoFunctions.push_back(function);
2878
2879 return function.functionName;
2880}
2881
Jamie Madill2e295e22015-04-29 10:41:33 -04002882void OutputHLSL::ensureStructDefined(const TType &type)
2883{
2884 TStructure *structure = type.getStruct();
2885
2886 if (structure)
2887 {
2888 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2889 }
2890}
2891
2892
Olli Etuaho9638c352015-04-01 14:34:52 +03002893
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002894}