blob: 9da8720d62af64fb574877bc9c707abe559004ef [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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
852{
Jamie Madill32aab012015-01-27 14:12:26 -0500853 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854
Jamie Madill570e04d2013-06-21 09:15:33 -0400855 // Handle accessing std140 structs by value
856 if (mFlaggedStructMappedNames.count(node) > 0)
857 {
858 out << mFlaggedStructMappedNames[node];
859 return false;
860 }
861
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862 switch (node->getOp())
863 {
Olli Etuahoe79904c2015-03-18 16:56:42 +0200864 case EOpAssign:
865 if (node->getLeft()->isArray())
866 {
Olli Etuaho9638c352015-04-01 14:34:52 +0300867 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
868 if (rightAgg != nullptr && rightAgg->isConstructor())
869 {
870 const TString &functionName = addArrayConstructIntoFunction(node->getType());
871 out << functionName << "(";
872 node->getLeft()->traverse(this);
873 TIntermSequence *seq = rightAgg->getSequence();
874 for (auto &arrayElement : *seq)
875 {
876 out << ", ";
877 arrayElement->traverse(this);
878 }
879 out << ")";
880 return false;
881 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +0300882 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
883 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
884
885 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500886 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200887 }
888 else
889 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500890 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200891 }
892 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000893 case EOpInitialize:
894 if (visit == PreVisit)
895 {
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000896 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -0500897 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000898 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000899
Olli Etuaho3d932d82016-04-12 11:10:30 +0300900 // Global initializers must be constant at this point.
Olli Etuahod4f4c112016-04-15 15:11:24 +0300901 ASSERT(symbolNode->getQualifier() != EvqGlobal || canWriteAsHLSLLiteral(expression));
902
903 // GLSL allows to write things like "float x = x;" where a new variable x is defined
904 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
905 // new variable is created before the assignment is evaluated), so we need to convert
906 // this to "float t = x, x = t;".
Olli Etuaho3d932d82016-04-12 11:10:30 +0300907 if (writeSameSymbolInitializer(out, symbolNode, expression))
Jamie Madill37997142015-01-28 10:06:34 -0500908 {
909 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000910 return false;
911 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200912 else if (writeConstantInitialization(out, symbolNode, expression))
913 {
914 return false;
915 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000916 }
917 else if (visit == InVisit)
918 {
919 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000920 }
921 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500922 case EOpAddAssign:
923 outputTriplet(out, visit, "(", " += ", ")");
924 break;
925 case EOpSubAssign:
926 outputTriplet(out, visit, "(", " -= ", ")");
927 break;
928 case EOpMulAssign:
929 outputTriplet(out, visit, "(", " *= ", ")");
930 break;
931 case EOpVectorTimesScalarAssign:
932 outputTriplet(out, visit, "(", " *= ", ")");
933 break;
934 case EOpMatrixTimesScalarAssign:
935 outputTriplet(out, visit, "(", " *= ", ")");
936 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000937 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000938 if (visit == PreVisit)
939 {
940 out << "(";
941 }
942 else if (visit == InVisit)
943 {
944 out << " = mul(";
945 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -0400946 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000947 }
948 else
949 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000950 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000951 }
952 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000953 case EOpMatrixTimesMatrixAssign:
954 if (visit == PreVisit)
955 {
956 out << "(";
957 }
958 else if (visit == InVisit)
959 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200960 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000961 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +0200962 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000963 }
964 else
965 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200966 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000967 }
968 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500969 case EOpDivAssign:
970 outputTriplet(out, visit, "(", " /= ", ")");
971 break;
972 case EOpIModAssign:
973 outputTriplet(out, visit, "(", " %= ", ")");
974 break;
975 case EOpBitShiftLeftAssign:
976 outputTriplet(out, visit, "(", " <<= ", ")");
977 break;
978 case EOpBitShiftRightAssign:
979 outputTriplet(out, visit, "(", " >>= ", ")");
980 break;
981 case EOpBitwiseAndAssign:
982 outputTriplet(out, visit, "(", " &= ", ")");
983 break;
984 case EOpBitwiseXorAssign:
985 outputTriplet(out, visit, "(", " ^= ", ")");
986 break;
987 case EOpBitwiseOrAssign:
988 outputTriplet(out, visit, "(", " |= ", ")");
989 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -0400990 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -0400991 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400992 const TType& leftType = node->getLeft()->getType();
993 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -0400994 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400995 if (visit == PreVisit)
996 {
997 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
998 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -0400999 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001000 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001001 return false;
1002 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001003 }
Olli Etuaho96963162016-03-21 11:54:33 +02001004 else if (ancestorEvaluatesToSamplerInStruct(visit))
1005 {
1006 // All parts of an expression that access a sampler in a struct need to use _ as
1007 // separator to access the sampler variable that has been moved out of the struct.
1008 outputTriplet(out, visit, "", "_", "");
1009 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001010 else
1011 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001012 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001013 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001014 }
1015 break;
1016 case EOpIndexIndirect:
1017 // We do not currently support indirect references to interface blocks
1018 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001019 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001020 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001021 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001022 {
1023 const TStructure* structure = node->getLeft()->getType().getStruct();
1024 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1025 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001026
Olli Etuaho96963162016-03-21 11:54:33 +02001027 // In cases where indexing returns a sampler, we need to access the sampler variable
1028 // that has been moved out of the struct.
1029 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1030 if (visit == PreVisit && indexingReturnsSampler)
1031 {
1032 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1033 // This prefix is only output at the beginning of the indexing expression, which
1034 // may have multiple parts.
1035 out << "angle";
1036 }
1037 if (!indexingReturnsSampler)
1038 {
1039 // All parts of an expression that access a sampler in a struct need to use _ as
1040 // separator to access the sampler variable that has been moved out of the struct.
1041 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1042 }
1043 if (visit == InVisit)
1044 {
1045 if (indexingReturnsSampler)
1046 {
1047 out << "_" + field->name();
1048 }
1049 else
1050 {
1051 out << "." + DecorateField(field->name(), *structure);
1052 }
1053
1054 return false;
1055 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001056 }
1057 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001058 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001059 if (visit == InVisit)
1060 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001061 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1062 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1063 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001064 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001065
1066 return false;
1067 }
1068 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069 case EOpVectorSwizzle:
1070 if (visit == InVisit)
1071 {
1072 out << ".";
1073
1074 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1075
1076 if (swizzle)
1077 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001078 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001080 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081 {
1082 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1083
1084 if (element)
1085 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001086 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001087
1088 switch (i)
1089 {
1090 case 0: out << "x"; break;
1091 case 1: out << "y"; break;
1092 case 2: out << "z"; break;
1093 case 3: out << "w"; break;
1094 default: UNREACHABLE();
1095 }
1096 }
1097 else UNREACHABLE();
1098 }
1099 }
1100 else UNREACHABLE();
1101
1102 return false; // Fully processed
1103 }
1104 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001105 case EOpAdd:
1106 outputTriplet(out, visit, "(", " + ", ")");
1107 break;
1108 case EOpSub:
1109 outputTriplet(out, visit, "(", " - ", ")");
1110 break;
1111 case EOpMul:
1112 outputTriplet(out, visit, "(", " * ", ")");
1113 break;
1114 case EOpDiv:
1115 outputTriplet(out, visit, "(", " / ", ")");
1116 break;
1117 case EOpIMod:
1118 outputTriplet(out, visit, "(", " % ", ")");
1119 break;
1120 case EOpBitShiftLeft:
1121 outputTriplet(out, visit, "(", " << ", ")");
1122 break;
1123 case EOpBitShiftRight:
1124 outputTriplet(out, visit, "(", " >> ", ")");
1125 break;
1126 case EOpBitwiseAnd:
1127 outputTriplet(out, visit, "(", " & ", ")");
1128 break;
1129 case EOpBitwiseXor:
1130 outputTriplet(out, visit, "(", " ^ ", ")");
1131 break;
1132 case EOpBitwiseOr:
1133 outputTriplet(out, visit, "(", " | ", ")");
1134 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001135 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001136 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001137 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001138 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001139 case EOpLessThan:
1140 outputTriplet(out, visit, "(", " < ", ")");
1141 break;
1142 case EOpGreaterThan:
1143 outputTriplet(out, visit, "(", " > ", ")");
1144 break;
1145 case EOpLessThanEqual:
1146 outputTriplet(out, visit, "(", " <= ", ")");
1147 break;
1148 case EOpGreaterThanEqual:
1149 outputTriplet(out, visit, "(", " >= ", ")");
1150 break;
1151 case EOpVectorTimesScalar:
1152 outputTriplet(out, visit, "(", " * ", ")");
1153 break;
1154 case EOpMatrixTimesScalar:
1155 outputTriplet(out, visit, "(", " * ", ")");
1156 break;
1157 case EOpVectorTimesMatrix:
1158 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1159 break;
1160 case EOpMatrixTimesVector:
1161 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1162 break;
1163 case EOpMatrixTimesMatrix:
1164 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1165 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001166 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001167 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1168 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001169 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001170 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001171 case EOpLogicalXor:
1172 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001173 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001174 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001175 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001176 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1177 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001178 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001179 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001180 default: UNREACHABLE();
1181 }
1182
1183 return true;
1184}
1185
1186bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1187{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001188 TInfoSinkBase &out = getInfoSink();
1189
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001190 switch (node->getOp())
1191 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001192 case EOpNegative:
1193 outputTriplet(out, visit, "(-", "", ")");
1194 break;
1195 case EOpPositive:
1196 outputTriplet(out, visit, "(+", "", ")");
1197 break;
1198 case EOpVectorLogicalNot:
1199 outputTriplet(out, visit, "(!", "", ")");
1200 break;
1201 case EOpLogicalNot:
1202 outputTriplet(out, visit, "(!", "", ")");
1203 break;
1204 case EOpBitwiseNot:
1205 outputTriplet(out, visit, "(~", "", ")");
1206 break;
1207 case EOpPostIncrement:
1208 outputTriplet(out, visit, "(", "", "++)");
1209 break;
1210 case EOpPostDecrement:
1211 outputTriplet(out, visit, "(", "", "--)");
1212 break;
1213 case EOpPreIncrement:
1214 outputTriplet(out, visit, "(++", "", ")");
1215 break;
1216 case EOpPreDecrement:
1217 outputTriplet(out, visit, "(--", "", ")");
1218 break;
1219 case EOpRadians:
1220 outputTriplet(out, visit, "radians(", "", ")");
1221 break;
1222 case EOpDegrees:
1223 outputTriplet(out, visit, "degrees(", "", ")");
1224 break;
1225 case EOpSin:
1226 outputTriplet(out, visit, "sin(", "", ")");
1227 break;
1228 case EOpCos:
1229 outputTriplet(out, visit, "cos(", "", ")");
1230 break;
1231 case EOpTan:
1232 outputTriplet(out, visit, "tan(", "", ")");
1233 break;
1234 case EOpAsin:
1235 outputTriplet(out, visit, "asin(", "", ")");
1236 break;
1237 case EOpAcos:
1238 outputTriplet(out, visit, "acos(", "", ")");
1239 break;
1240 case EOpAtan:
1241 outputTriplet(out, visit, "atan(", "", ")");
1242 break;
1243 case EOpSinh:
1244 outputTriplet(out, visit, "sinh(", "", ")");
1245 break;
1246 case EOpCosh:
1247 outputTriplet(out, visit, "cosh(", "", ")");
1248 break;
1249 case EOpTanh:
1250 outputTriplet(out, visit, "tanh(", "", ")");
1251 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001252 case EOpAsinh:
1253 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001254 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001255 break;
1256 case EOpAcosh:
1257 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001258 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001259 break;
1260 case EOpAtanh:
1261 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001262 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001263 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001264 case EOpExp:
1265 outputTriplet(out, visit, "exp(", "", ")");
1266 break;
1267 case EOpLog:
1268 outputTriplet(out, visit, "log(", "", ")");
1269 break;
1270 case EOpExp2:
1271 outputTriplet(out, visit, "exp2(", "", ")");
1272 break;
1273 case EOpLog2:
1274 outputTriplet(out, visit, "log2(", "", ")");
1275 break;
1276 case EOpSqrt:
1277 outputTriplet(out, visit, "sqrt(", "", ")");
1278 break;
1279 case EOpInverseSqrt:
1280 outputTriplet(out, visit, "rsqrt(", "", ")");
1281 break;
1282 case EOpAbs:
1283 outputTriplet(out, visit, "abs(", "", ")");
1284 break;
1285 case EOpSign:
1286 outputTriplet(out, visit, "sign(", "", ")");
1287 break;
1288 case EOpFloor:
1289 outputTriplet(out, visit, "floor(", "", ")");
1290 break;
1291 case EOpTrunc:
1292 outputTriplet(out, visit, "trunc(", "", ")");
1293 break;
1294 case EOpRound:
1295 outputTriplet(out, visit, "round(", "", ")");
1296 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001297 case EOpRoundEven:
1298 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001299 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001300 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001301 case EOpCeil:
1302 outputTriplet(out, visit, "ceil(", "", ")");
1303 break;
1304 case EOpFract:
1305 outputTriplet(out, visit, "frac(", "", ")");
1306 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301307 case EOpIsNan:
Shao6f0a0dc2016-09-27 13:51:29 +08001308 if (node->getUseEmulatedFunction())
1309 writeEmulatedFunctionTriplet(out, visit, "isnan(");
1310 else
1311 outputTriplet(out, visit, "isnan(", "", ")");
1312 mRequiresIEEEStrictCompiling = true;
1313 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001314 case EOpIsInf:
1315 outputTriplet(out, visit, "isinf(", "", ")");
1316 break;
1317 case EOpFloatBitsToInt:
1318 outputTriplet(out, visit, "asint(", "", ")");
1319 break;
1320 case EOpFloatBitsToUint:
1321 outputTriplet(out, visit, "asuint(", "", ")");
1322 break;
1323 case EOpIntBitsToFloat:
1324 outputTriplet(out, visit, "asfloat(", "", ")");
1325 break;
1326 case EOpUintBitsToFloat:
1327 outputTriplet(out, visit, "asfloat(", "", ")");
1328 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001329 case EOpPackSnorm2x16:
1330 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001331 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001332 break;
1333 case EOpPackUnorm2x16:
1334 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001335 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001336 break;
1337 case EOpPackHalf2x16:
1338 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001339 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001340 break;
1341 case EOpUnpackSnorm2x16:
1342 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001343 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001344 break;
1345 case EOpUnpackUnorm2x16:
1346 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001347 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001348 break;
1349 case EOpUnpackHalf2x16:
1350 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001351 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001352 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001353 case EOpLength:
1354 outputTriplet(out, visit, "length(", "", ")");
1355 break;
1356 case EOpNormalize:
1357 outputTriplet(out, visit, "normalize(", "", ")");
1358 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001359 case EOpDFdx:
1360 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1361 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001362 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001363 }
1364 else
1365 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001366 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001367 }
1368 break;
1369 case EOpDFdy:
1370 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1371 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001372 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001373 }
1374 else
1375 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001376 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001377 }
1378 break;
1379 case EOpFwidth:
1380 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1381 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001382 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001383 }
1384 else
1385 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001386 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001387 }
1388 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001389 case EOpTranspose:
1390 outputTriplet(out, visit, "transpose(", "", ")");
1391 break;
1392 case EOpDeterminant:
1393 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1394 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001395 case EOpInverse:
1396 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001397 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001398 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001399
Jamie Madill8c46ab12015-12-07 16:39:19 -05001400 case EOpAny:
1401 outputTriplet(out, visit, "any(", "", ")");
1402 break;
1403 case EOpAll:
1404 outputTriplet(out, visit, "all(", "", ")");
1405 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001406 default: UNREACHABLE();
1407 }
1408
1409 return true;
1410}
1411
Olli Etuaho96963162016-03-21 11:54:33 +02001412TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1413{
1414 if (node->getAsSymbolNode())
1415 {
1416 return node->getAsSymbolNode()->getSymbol();
1417 }
1418 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1419 switch (nodeBinary->getOp())
1420 {
1421 case EOpIndexDirect:
1422 {
1423 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1424
1425 TInfoSinkBase prefixSink;
1426 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1427 return TString(prefixSink.c_str());
1428 }
1429 case EOpIndexDirectStruct:
1430 {
1431 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1432 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1433 const TField *field = s->fields()[index];
1434
1435 TInfoSinkBase prefixSink;
1436 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1437 << field->name();
1438 return TString(prefixSink.c_str());
1439 }
1440 default:
1441 UNREACHABLE();
1442 return TString("");
1443 }
1444}
1445
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001446bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1447{
Jamie Madill32aab012015-01-27 14:12:26 -05001448 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001449
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001450 switch (node->getOp())
1451 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001452 case EOpSequence:
1453 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001454 if (mInsideFunction)
1455 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001456 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001457 out << "{\n";
1458 }
1459
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001460 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001461 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001462 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001463
Olli Etuahoa6f22092015-05-08 18:31:10 +03001464 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001465
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001466 // Don't output ; after case labels, they're terminated by :
1467 // This is needed especially since outputting a ; after a case statement would turn empty
1468 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho57961272016-09-14 13:57:46 +03001469 // Also no need to output ; after if statements or sequences. This is done just for
1470 // code clarity.
1471 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001472 !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001473 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001474 }
1475
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001476 if (mInsideFunction)
1477 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001478 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001479 out << "}\n";
1480 }
1481
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001482 return false;
1483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001484 case EOpDeclaration:
1485 if (visit == PreVisit)
1486 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001487 TIntermSequence *sequence = node->getSequence();
1488 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001489 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001490
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001491 if (variable &&
1492 (variable->getQualifier() == EvqTemporary ||
1493 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001494 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001495 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001496
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001497 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001498 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001499 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001501 out << "static ";
1502 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001503
Olli Etuahoa6f22092015-05-08 18:31:10 +03001504 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001505
Olli Etuahoa6f22092015-05-08 18:31:10 +03001506 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001507
Olli Etuahoa6f22092015-05-08 18:31:10 +03001508 if (symbol)
1509 {
1510 symbol->traverse(this);
1511 out << ArrayString(symbol->getType());
1512 out << " = " + initializer(symbol->getType());
1513 }
1514 else
1515 {
1516 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001517 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001518 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001519 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1520 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001521 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001522 }
1523 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001524 }
Jamie Madill033dae62014-06-18 12:56:28 -04001525 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001526 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001527 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001528 {
1529 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1530
1531 if (symbol)
1532 {
1533 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1534 mReferencedVaryings[symbol->getSymbol()] = symbol;
1535 }
1536 else
1537 {
1538 (*sit)->traverse(this);
1539 }
1540 }
1541 }
1542
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001543 return false;
1544 }
1545 else if (visit == InVisit)
1546 {
1547 out << ", ";
1548 }
1549 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001550 case EOpInvariantDeclaration:
1551 // Do not do any translation
1552 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001553 case EOpPrototype:
1554 if (visit == PreVisit)
1555 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001556 size_t index = mCallDag.findIndex(node);
1557 // Skip the prototype if it is not implemented (and thus not used)
1558 if (index == CallDAG::InvalidIndex)
1559 {
1560 return false;
1561 }
1562
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001563 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001564
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001565 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1566 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1567 << (mOutputLod0Function ? "Lod0(" : "(");
1568
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001569 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001570 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001571 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001572
1573 if (symbol)
1574 {
1575 out << argumentString(symbol);
1576
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001577 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001578 {
1579 out << ", ";
1580 }
1581 }
1582 else UNREACHABLE();
1583 }
1584
1585 out << ");\n";
1586
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001587 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001588 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1589 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001590 {
1591 mOutputLod0Function = true;
1592 node->traverse(this);
1593 mOutputLod0Function = false;
1594 }
1595
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001596 return false;
1597 }
1598 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001599 case EOpComma:
1600 outputTriplet(out, visit, "(", ", ", ")");
1601 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 case EOpFunction:
1603 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001604 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001605 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001606
Corentin Wallez1239ee92015-03-19 14:38:02 -07001607 size_t index = mCallDag.findIndex(node);
1608 ASSERT(index != CallDAG::InvalidIndex);
1609 mCurrentFunctionMetadata = &mASTMetadataList[index];
1610
Jamie Madill033dae62014-06-18 12:56:28 -04001611 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001612
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001613 TIntermSequence *sequence = node->getSequence();
1614 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
1615
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001616 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001617 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001618 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001619 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001620 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001621 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001622 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001623 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001624 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001625
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001626 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001627 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001628 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001629
1630 if (symbol)
1631 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001632 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001633
1634 out << argumentString(symbol);
1635
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001636 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001637 {
1638 out << ", ";
1639 }
1640 }
1641 else UNREACHABLE();
1642 }
1643
Olli Etuaho4785fec2015-05-18 16:09:37 +03001644 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001645
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001646 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001647 {
1648 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03001649 TIntermNode *body = (*sequence)[1];
1650 // The function body node will output braces.
1651 ASSERT(IsSequence(body));
1652 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001653 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001655 else
1656 {
1657 out << "{}\n";
1658 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001659
Corentin Wallez1239ee92015-03-19 14:38:02 -07001660 mCurrentFunctionMetadata = nullptr;
1661
1662 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1663 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001664 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001665 ASSERT(name != "main");
1666 mOutputLod0Function = true;
1667 node->traverse(this);
1668 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001669 }
1670
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001671 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 }
1673 break;
1674 case EOpFunctionCall:
1675 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001676 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001677
Corentin Wallez1239ee92015-03-19 14:38:02 -07001678 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001679 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001680 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001681 if (node->isArray())
1682 {
1683 UNIMPLEMENTED();
1684 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07001685 size_t index = mCallDag.findIndex(node);
1686 ASSERT(index != CallDAG::InvalidIndex);
1687 lod0 &= mASTMetadataList[index].mNeedsLod0;
1688
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001689 out << DecorateFunctionIfNeeded(node->getNameObj());
1690 out << DisambiguateFunctionName(node->getSequence());
1691 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001692 }
Olli Etuahob741c762016-06-29 15:49:22 +03001693 else if (node->getNameObj().isInternal())
1694 {
1695 // This path is used for internal functions that don't have their definitions in the
1696 // AST, such as precision emulation functions.
1697 out << DecorateFunctionIfNeeded(node->getNameObj()) << "(";
1698 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001699 else
1700 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001701 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001702 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001703 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1704 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1705 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1706 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001707 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001708
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001709 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001710 {
Olli Etuaho96963162016-03-21 11:54:33 +02001711 TIntermTyped *typedArg = (*arg)->getAsTyped();
1712 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001713 {
1714 out << "texture_";
1715 (*arg)->traverse(this);
1716 out << ", sampler_";
1717 }
1718
1719 (*arg)->traverse(this);
1720
Olli Etuaho96963162016-03-21 11:54:33 +02001721 if (typedArg->getType().isStructureContainingSamplers())
1722 {
1723 const TType &argType = typedArg->getType();
1724 TVector<TIntermSymbol *> samplerSymbols;
1725 TString structName = samplerNamePrefixFromStruct(typedArg);
1726 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001727 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001728 &samplerSymbols, nullptr);
1729 for (const TIntermSymbol *sampler : samplerSymbols)
1730 {
1731 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1732 {
1733 out << ", texture_" << sampler->getSymbol();
1734 out << ", sampler_" << sampler->getSymbol();
1735 }
1736 else
1737 {
1738 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1739 // of D3D9, it's the sampler variable.
1740 out << ", " + sampler->getSymbol();
1741 }
1742 }
1743 }
1744
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001745 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001746 {
1747 out << ", ";
1748 }
1749 }
1750
1751 out << ")";
1752
1753 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 }
1755 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001756 case EOpParameters:
1757 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1758 break;
1759 case EOpConstructFloat:
1760 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1761 break;
1762 case EOpConstructVec2:
1763 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1764 break;
1765 case EOpConstructVec3:
1766 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1767 break;
1768 case EOpConstructVec4:
1769 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1770 break;
1771 case EOpConstructBool:
1772 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1773 break;
1774 case EOpConstructBVec2:
1775 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1776 break;
1777 case EOpConstructBVec3:
1778 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1779 break;
1780 case EOpConstructBVec4:
1781 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1782 break;
1783 case EOpConstructInt:
1784 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1785 break;
1786 case EOpConstructIVec2:
1787 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1788 break;
1789 case EOpConstructIVec3:
1790 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1791 break;
1792 case EOpConstructIVec4:
1793 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1794 break;
1795 case EOpConstructUInt:
1796 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1797 break;
1798 case EOpConstructUVec2:
1799 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1800 break;
1801 case EOpConstructUVec3:
1802 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1803 break;
1804 case EOpConstructUVec4:
1805 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1806 break;
1807 case EOpConstructMat2:
1808 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1809 break;
1810 case EOpConstructMat2x3:
1811 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1812 break;
1813 case EOpConstructMat2x4:
1814 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1815 break;
1816 case EOpConstructMat3x2:
1817 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1818 break;
1819 case EOpConstructMat3:
1820 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1821 break;
1822 case EOpConstructMat3x4:
1823 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1824 break;
1825 case EOpConstructMat4x2:
1826 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1827 break;
1828 case EOpConstructMat4x3:
1829 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1830 break;
1831 case EOpConstructMat4:
1832 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1833 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001834 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001835 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001836 if (node->getType().isArray())
1837 {
1838 UNIMPLEMENTED();
1839 }
Jamie Madill033dae62014-06-18 12:56:28 -04001840 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001841 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001842 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001843 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001844 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001845 case EOpLessThan:
1846 outputTriplet(out, visit, "(", " < ", ")");
1847 break;
1848 case EOpGreaterThan:
1849 outputTriplet(out, visit, "(", " > ", ")");
1850 break;
1851 case EOpLessThanEqual:
1852 outputTriplet(out, visit, "(", " <= ", ")");
1853 break;
1854 case EOpGreaterThanEqual:
1855 outputTriplet(out, visit, "(", " >= ", ")");
1856 break;
1857 case EOpVectorEqual:
1858 outputTriplet(out, visit, "(", " == ", ")");
1859 break;
1860 case EOpVectorNotEqual:
1861 outputTriplet(out, visit, "(", " != ", ")");
1862 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001863 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001864 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001865 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001866 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001867 case EOpModf:
1868 outputTriplet(out, visit, "modf(", ", ", ")");
1869 break;
1870 case EOpPow:
1871 outputTriplet(out, visit, "pow(", ", ", ")");
1872 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001873 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001874 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02001875 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001876 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001878 case EOpMin:
1879 outputTriplet(out, visit, "min(", ", ", ")");
1880 break;
1881 case EOpMax:
1882 outputTriplet(out, visit, "max(", ", ", ")");
1883 break;
1884 case EOpClamp:
1885 outputTriplet(out, visit, "clamp(", ", ", ")");
1886 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301887 case EOpMix:
1888 {
1889 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1890 if (lastParamNode->getType().getBasicType() == EbtBool)
1891 {
1892 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1893 // so use emulated version.
1894 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001895 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301896 }
1897 else
1898 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001899 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301900 }
1901 }
1902 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001903 case EOpStep:
1904 outputTriplet(out, visit, "step(", ", ", ")");
1905 break;
1906 case EOpSmoothStep:
1907 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1908 break;
1909 case EOpDistance:
1910 outputTriplet(out, visit, "distance(", ", ", ")");
1911 break;
1912 case EOpDot:
1913 outputTriplet(out, visit, "dot(", ", ", ")");
1914 break;
1915 case EOpCross:
1916 outputTriplet(out, visit, "cross(", ", ", ")");
1917 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001918 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001919 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001920 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001921 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001922 case EOpReflect:
1923 outputTriplet(out, visit, "reflect(", ", ", ")");
1924 break;
1925 case EOpRefract:
1926 outputTriplet(out, visit, "refract(", ", ", ")");
1927 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001928 case EOpOuterProduct:
1929 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001930 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02001931 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001932 case EOpMul:
1933 outputTriplet(out, visit, "(", " * ", ")");
1934 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 default: UNREACHABLE();
1936 }
1937
1938 return true;
1939}
1940
Olli Etuaho57961272016-09-14 13:57:46 +03001941void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001943 out << "if (";
1944
1945 node->getCondition()->traverse(this);
1946
1947 out << ")\n";
1948
Jamie Madill8c46ab12015-12-07 16:39:19 -05001949 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001950
1951 bool discard = false;
1952
1953 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001955 // The trueBlock child node will output braces.
1956 ASSERT(IsSequence(node->getTrueBlock()));
1957
Olli Etuahoa6f22092015-05-08 18:31:10 +03001958 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001959
Olli Etuahoa6f22092015-05-08 18:31:10 +03001960 // Detect true discard
1961 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1962 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001963 else
1964 {
1965 // TODO(oetuaho): Check if the semicolon inside is necessary.
1966 // It's there as a result of conservative refactoring of the output.
1967 out << "{;}\n";
1968 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001969
Jamie Madill8c46ab12015-12-07 16:39:19 -05001970 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001971
Olli Etuahoa6f22092015-05-08 18:31:10 +03001972 if (node->getFalseBlock())
1973 {
1974 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001975
Jamie Madill8c46ab12015-12-07 16:39:19 -05001976 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001977
Olli Etuaho4785fec2015-05-18 16:09:37 +03001978 // Either this is "else if" or the falseBlock child node will output braces.
Olli Etuaho57961272016-09-14 13:57:46 +03001979 ASSERT(IsSequence(node->getFalseBlock()) ||
1980 node->getFalseBlock()->getAsIfElseNode() != nullptr);
Olli Etuaho4785fec2015-05-18 16:09:37 +03001981
Olli Etuahoa6f22092015-05-08 18:31:10 +03001982 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001983
Jamie Madill8c46ab12015-12-07 16:39:19 -05001984 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001985
Olli Etuahoa6f22092015-05-08 18:31:10 +03001986 // Detect false discard
1987 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1988 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001989
Olli Etuahoa6f22092015-05-08 18:31:10 +03001990 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001991 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001992 {
1993 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001994 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001995}
1996
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001997bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1998{
1999 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2000 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2001 UNREACHABLE();
2002 return false;
2003}
2004
Olli Etuaho57961272016-09-14 13:57:46 +03002005bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002006{
2007 TInfoSinkBase &out = getInfoSink();
2008
Olli Etuaho3d932d82016-04-12 11:10:30 +03002009 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002010
2011 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002012 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002013 {
2014 out << "FLATTEN ";
2015 }
2016
Olli Etuaho57961272016-09-14 13:57:46 +03002017 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018
2019 return false;
2020}
2021
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002022bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002023{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002024 TInfoSinkBase &out = getInfoSink();
2025
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002026 if (node->getStatementList())
2027 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002028 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002029 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002030 // The curly braces get written when visiting the statementList aggregate
2031 }
2032 else
2033 {
2034 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002035 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002036 }
2037 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002038}
2039
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002040bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002041{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002042 TInfoSinkBase &out = getInfoSink();
2043
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002044 if (node->hasCondition())
2045 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002046 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002047 return true;
2048 }
2049 else
2050 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002051 out << "default:\n";
2052 return false;
2053 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002054}
2055
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2057{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002058 TInfoSinkBase &out = getInfoSink();
2059 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060}
2061
2062bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2063{
Nicolas Capens655fe362014-04-11 13:12:34 -04002064 mNestedLoopDepth++;
2065
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002066 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002067 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002068 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002069
Jamie Madill8c46ab12015-12-07 16:39:19 -05002070 TInfoSinkBase &out = getInfoSink();
2071
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002072 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002073 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002074 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002075 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002076 mInsideDiscontinuousLoop = wasDiscontinuous;
2077 mNestedLoopDepth--;
2078
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002079 return false;
2080 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002081 }
2082
Corentin Wallez1239ee92015-03-19 14:38:02 -07002083 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002084 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002086 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002087
Jamie Madill8c46ab12015-12-07 16:39:19 -05002088 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 }
2090 else
2091 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002092 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002093
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094 if (node->getInit())
2095 {
2096 node->getInit()->traverse(this);
2097 }
2098
2099 out << "; ";
2100
alokp@chromium.org52813552010-11-16 18:36:09 +00002101 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002103 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 }
2105
2106 out << "; ";
2107
alokp@chromium.org52813552010-11-16 18:36:09 +00002108 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002110 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 }
2112
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002113 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002114
Jamie Madill8c46ab12015-12-07 16:39:19 -05002115 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116 }
2117
2118 if (node->getBody())
2119 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002120 // The loop body node will output braces.
2121 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002122 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002124 else
2125 {
2126 // TODO(oetuaho): Check if the semicolon inside is necessary.
2127 // It's there as a result of conservative refactoring of the output.
2128 out << "{;}\n";
2129 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002130
Jamie Madill8c46ab12015-12-07 16:39:19 -05002131 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132
alokp@chromium.org52813552010-11-16 18:36:09 +00002133 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002135 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136 out << "while(\n";
2137
alokp@chromium.org52813552010-11-16 18:36:09 +00002138 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139
daniel@transgaming.com73536982012-03-21 20:45:49 +00002140 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141 }
2142
daniel@transgaming.com73536982012-03-21 20:45:49 +00002143 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002145 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002146 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002147
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 return false;
2149}
2150
2151bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2152{
Jamie Madill32aab012015-01-27 14:12:26 -05002153 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154
2155 switch (node->getFlowOp())
2156 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002157 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002158 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002159 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002160 case EOpBreak:
2161 if (visit == PreVisit)
2162 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002163 if (mNestedLoopDepth > 1)
2164 {
2165 mUsesNestedBreak = true;
2166 }
2167
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002168 if (mExcessiveLoopIndex)
2169 {
2170 out << "{Break";
2171 mExcessiveLoopIndex->traverse(this);
2172 out << " = true; break;}\n";
2173 }
2174 else
2175 {
2176 out << "break;\n";
2177 }
2178 }
2179 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002180 case EOpContinue:
2181 outputTriplet(out, visit, "continue;\n", "", "");
2182 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002183 case EOpReturn:
2184 if (visit == PreVisit)
2185 {
2186 if (node->getExpression())
2187 {
2188 out << "return ";
2189 }
2190 else
2191 {
2192 out << "return;\n";
2193 }
2194 }
2195 else if (visit == PostVisit)
2196 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002197 if (node->getExpression())
2198 {
2199 out << ";\n";
2200 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201 }
2202 break;
2203 default: UNREACHABLE();
2204 }
2205
2206 return true;
2207}
2208
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002209bool OutputHLSL::isSingleStatement(TIntermNode *node)
2210{
2211 TIntermAggregate *aggregate = node->getAsAggregate();
2212
2213 if (aggregate)
2214 {
2215 if (aggregate->getOp() == EOpSequence)
2216 {
2217 return false;
2218 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002219 else if (aggregate->getOp() == EOpDeclaration)
2220 {
2221 // Declaring multiple comma-separated variables must be considered multiple statements
2222 // because each individual declaration has side effects which are visible in the next.
2223 return false;
2224 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002225 else
2226 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002227 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002228 {
2229 if (!isSingleStatement(*sit))
2230 {
2231 return false;
2232 }
2233 }
2234
2235 return true;
2236 }
2237 }
2238
2239 return true;
2240}
2241
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002242// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2243// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002244bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002245{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002246 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002247
2248 // Parse loops of the form:
2249 // for(int index = initial; index [comparator] limit; index += increment)
2250 TIntermSymbol *index = NULL;
2251 TOperator comparator = EOpNull;
2252 int initial = 0;
2253 int limit = 0;
2254 int increment = 0;
2255
2256 // Parse index name and intial value
2257 if (node->getInit())
2258 {
2259 TIntermAggregate *init = node->getInit()->getAsAggregate();
2260
2261 if (init)
2262 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002263 TIntermSequence *sequence = init->getSequence();
2264 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002265
2266 if (variable && variable->getQualifier() == EvqTemporary)
2267 {
2268 TIntermBinary *assign = variable->getAsBinaryNode();
2269
2270 if (assign->getOp() == EOpInitialize)
2271 {
2272 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2273 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2274
2275 if (symbol && constant)
2276 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002277 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002278 {
2279 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002280 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002281 }
2282 }
2283 }
2284 }
2285 }
2286 }
2287
2288 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002289 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002290 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002291 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002292
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002293 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2294 {
2295 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2296
2297 if (constant)
2298 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002299 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002300 {
2301 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002302 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002303 }
2304 }
2305 }
2306 }
2307
2308 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002309 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002310 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002311 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2312 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002313
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002314 if (binaryTerminal)
2315 {
2316 TOperator op = binaryTerminal->getOp();
2317 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2318
2319 if (constant)
2320 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002321 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002322 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002323 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002324
2325 switch (op)
2326 {
2327 case EOpAddAssign: increment = value; break;
2328 case EOpSubAssign: increment = -value; break;
2329 default: UNIMPLEMENTED();
2330 }
2331 }
2332 }
2333 }
2334 else if (unaryTerminal)
2335 {
2336 TOperator op = unaryTerminal->getOp();
2337
2338 switch (op)
2339 {
2340 case EOpPostIncrement: increment = 1; break;
2341 case EOpPostDecrement: increment = -1; break;
2342 case EOpPreIncrement: increment = 1; break;
2343 case EOpPreDecrement: increment = -1; break;
2344 default: UNIMPLEMENTED();
2345 }
2346 }
2347 }
2348
2349 if (index != NULL && comparator != EOpNull && increment != 0)
2350 {
2351 if (comparator == EOpLessThanEqual)
2352 {
2353 comparator = EOpLessThan;
2354 limit += 1;
2355 }
2356
2357 if (comparator == EOpLessThan)
2358 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002359 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002360
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002361 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002362 {
2363 return false; // Not an excessive loop
2364 }
2365
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002366 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2367 mExcessiveLoopIndex = index;
2368
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002369 out << "{int ";
2370 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002371 out << ";\n"
2372 "bool Break";
2373 index->traverse(this);
2374 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002375
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002376 bool firstLoopFragment = true;
2377
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002378 while (iterations > 0)
2379 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002380 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002381
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002382 if (!firstLoopFragment)
2383 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002384 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002385 index->traverse(this);
2386 out << ") {\n";
2387 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002388
2389 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2390 {
2391 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2392 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002393
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002394 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002395 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002396
Corentin Wallez1239ee92015-03-19 14:38:02 -07002397 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002398 index->traverse(this);
2399 out << " = ";
2400 out << initial;
2401
2402 out << "; ";
2403 index->traverse(this);
2404 out << " < ";
2405 out << clampedLimit;
2406
2407 out << "; ";
2408 index->traverse(this);
2409 out << " += ";
2410 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002411 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002412
Jamie Madill8c46ab12015-12-07 16:39:19 -05002413 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002414 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002415
2416 if (node->getBody())
2417 {
2418 node->getBody()->traverse(this);
2419 }
2420
Jamie Madill8c46ab12015-12-07 16:39:19 -05002421 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002422 out << ";}\n";
2423
2424 if (!firstLoopFragment)
2425 {
2426 out << "}\n";
2427 }
2428
2429 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002430
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002431 initial += MAX_LOOP_ITERATIONS * increment;
2432 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002433 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002434
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002435 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002436
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002437 mExcessiveLoopIndex = restoreIndex;
2438
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002439 return true;
2440 }
2441 else UNIMPLEMENTED();
2442 }
2443
2444 return false; // Not handled as an excessive loop
2445}
2446
Jamie Madill8c46ab12015-12-07 16:39:19 -05002447void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2448 Visit visit,
2449 const char *preString,
2450 const char *inString,
2451 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002452{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002453 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454 {
2455 out << preString;
2456 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002457 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 {
2459 out << inString;
2460 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002461 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462 {
2463 out << postString;
2464 }
2465}
2466
Jamie Madill8c46ab12015-12-07 16:39:19 -05002467void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002468{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002469 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002470 {
Jamie Madill32aab012015-01-27 14:12:26 -05002471 out << "\n";
2472 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002473
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002474 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002475 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002476 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002477 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002478
Jamie Madill32aab012015-01-27 14:12:26 -05002479 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002480 }
2481}
2482
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002483TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2484{
2485 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002486 const TType &type = symbol->getType();
2487 const TName &name = symbol->getName();
2488 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002489
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002490 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002491 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002492 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002493 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002494 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002495 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002496 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002497 }
2498
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002499 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002500 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002501 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2502 {
2503 // Samplers are passed as indices to the sampler array.
2504 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2505 return "const uint " + nameStr + ArrayString(type);
2506 }
2507 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2508 {
2509 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2510 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2511 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2512 ArrayString(type);
2513 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002514 }
2515
Olli Etuaho96963162016-03-21 11:54:33 +02002516 TStringStream argString;
2517 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2518 << ArrayString(type);
2519
2520 // If the structure parameter contains samplers, they need to be passed into the function as
2521 // separate parameters. HLSL doesn't natively support samplers in structs.
2522 if (type.isStructureContainingSamplers())
2523 {
2524 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2525 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002526 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002527 for (const TIntermSymbol *sampler : samplerSymbols)
2528 {
2529 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2530 {
2531 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2532 }
2533 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2534 {
2535 const TType &samplerType = sampler->getType();
2536 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2537 type.getArraySize() == samplerType.getArraySize());
2538 ASSERT(IsSampler(samplerType.getBasicType()));
2539 argString << ", " << QualifierString(qualifier) << " "
2540 << TextureString(samplerType.getBasicType()) << " texture_"
2541 << sampler->getSymbol() << ArrayString(type) << ", "
2542 << QualifierString(qualifier) << " "
2543 << SamplerString(samplerType.getBasicType()) << " sampler_"
2544 << sampler->getSymbol() << ArrayString(type);
2545 }
2546 else
2547 {
2548 const TType &samplerType = sampler->getType();
2549 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2550 type.getArraySize() == samplerType.getArraySize());
2551 ASSERT(IsSampler(samplerType.getBasicType()));
2552 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2553 << " " << sampler->getSymbol() << ArrayString(type);
2554 }
2555 }
2556 }
2557
2558 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559}
2560
2561TString OutputHLSL::initializer(const TType &type)
2562{
2563 TString string;
2564
Jamie Madill94bf7f22013-07-08 13:31:15 -04002565 size_t size = type.getObjectSize();
2566 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002567 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002568 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002569
Jamie Madill94bf7f22013-07-08 13:31:15 -04002570 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571 {
2572 string += ", ";
2573 }
2574 }
2575
daniel@transgaming.comead23042010-04-29 03:35:36 +00002576 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002578
Jamie Madill8c46ab12015-12-07 16:39:19 -05002579void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2580 Visit visit,
2581 const TType &type,
2582 const char *name,
2583 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002584{
Olli Etuahof40319e2015-03-10 14:33:00 +02002585 if (type.isArray())
2586 {
2587 UNIMPLEMENTED();
2588 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002589
2590 if (visit == PreVisit)
2591 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002592 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002593
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002594 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002595 }
2596 else if (visit == InVisit)
2597 {
2598 out << ", ";
2599 }
2600 else if (visit == PostVisit)
2601 {
2602 out << ")";
2603 }
2604}
2605
Jamie Madill8c46ab12015-12-07 16:39:19 -05002606const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2607 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002608 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002609{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002610 const TConstantUnion *constUnionIterated = constUnion;
2611
Jamie Madill98493dd2013-07-08 14:39:03 -04002612 const TStructure* structure = type.getStruct();
2613 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002614 {
Jamie Madill033dae62014-06-18 12:56:28 -04002615 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002616
Jamie Madill98493dd2013-07-08 14:39:03 -04002617 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002618
Jamie Madill98493dd2013-07-08 14:39:03 -04002619 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002620 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002621 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002622 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002623
Jamie Madill98493dd2013-07-08 14:39:03 -04002624 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002625 {
2626 out << ", ";
2627 }
2628 }
2629
2630 out << ")";
2631 }
2632 else
2633 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002634 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002635 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002636
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002637 if (writeType)
2638 {
Jamie Madill033dae62014-06-18 12:56:28 -04002639 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002640 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002641 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002642 if (writeType)
2643 {
2644 out << ")";
2645 }
2646 }
2647
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002648 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002649}
2650
Jamie Madill8c46ab12015-12-07 16:39:19 -05002651void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002652{
2653 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002654 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002655}
2656
Jamie Madill37997142015-01-28 10:06:34 -05002657bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2658{
2659 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2660 expression->traverse(&searchSymbol);
2661
2662 if (searchSymbol.foundMatch())
2663 {
2664 // Type already printed
2665 out << "t" + str(mUniqueIndex) + " = ";
2666 expression->traverse(this);
2667 out << ", ";
2668 symbolNode->traverse(this);
2669 out << " = t" + str(mUniqueIndex);
2670
2671 mUniqueIndex++;
2672 return true;
2673 }
2674
2675 return false;
2676}
2677
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002678bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2679{
2680 // We support writing constant unions and constructors that only take constant unions as
2681 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002682 return expression->getAsConstantUnion() ||
2683 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002684}
2685
2686bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2687 TIntermSymbol *symbolNode,
2688 TIntermTyped *expression)
2689{
2690 if (canWriteAsHLSLLiteral(expression))
2691 {
2692 symbolNode->traverse(this);
2693 if (expression->getType().isArray())
2694 {
2695 out << "[" << expression->getType().getArraySize() << "]";
2696 }
2697 out << " = {";
2698 if (expression->getAsConstantUnion())
2699 {
2700 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2701 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2702 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2703 }
2704 else
2705 {
2706 TIntermAggregate *constructor = expression->getAsAggregate();
2707 ASSERT(constructor != nullptr);
2708 for (TIntermNode *&node : *constructor->getSequence())
2709 {
2710 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2711 ASSERT(nodeConst);
2712 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2713 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2714 if (node != constructor->getSequence()->back())
2715 {
2716 out << ", ";
2717 }
2718 }
2719 }
2720 out << "}";
2721 return true;
2722 }
2723 return false;
2724}
2725
Jamie Madill55e79e02015-02-09 15:35:00 -05002726TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2727{
2728 const TFieldList &fields = structure.fields();
2729
2730 for (const auto &eqFunction : mStructEqualityFunctions)
2731 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002732 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002733 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002734 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002735 }
2736 }
2737
2738 const TString &structNameString = StructNameString(structure);
2739
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002740 StructEqualityFunction *function = new StructEqualityFunction();
2741 function->structure = &structure;
2742 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002743
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002744 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002745
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002746 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2747 << "{\n"
2748 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002749
2750 for (size_t i = 0; i < fields.size(); i++)
2751 {
2752 const TField *field = fields[i];
2753 const TType *fieldType = field->type();
2754
2755 const TString &fieldNameA = "a." + Decorate(field->name());
2756 const TString &fieldNameB = "b." + Decorate(field->name());
2757
2758 if (i > 0)
2759 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002760 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002761 }
2762
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002763 fnOut << "(";
2764 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2765 fnOut << fieldNameA;
2766 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2767 fnOut << fieldNameB;
2768 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2769 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002770 }
2771
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002772 fnOut << ";\n" << "}\n";
2773
2774 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002775
2776 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002777 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002778
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002779 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002780}
2781
Olli Etuaho7fb49552015-03-18 17:27:44 +02002782TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2783{
2784 for (const auto &eqFunction : mArrayEqualityFunctions)
2785 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002786 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002787 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002788 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002789 }
2790 }
2791
2792 const TString &typeName = TypeString(type);
2793
Olli Etuaho12690762015-03-31 12:55:28 +03002794 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002795 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002796
2797 TInfoSinkBase fnNameOut;
2798 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002799 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002800
2801 TType nonArrayType = type;
2802 nonArrayType.clearArrayness();
2803
2804 TInfoSinkBase fnOut;
2805
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002806 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002807 << typeName << " a[" << type.getArraySize() << "], "
2808 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002809 << "{\n"
2810 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2811 " {\n"
2812 " if (";
2813
2814 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2815 fnOut << "a[i]";
2816 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2817 fnOut << "b[i]";
2818 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2819
2820 fnOut << ") { return false; }\n"
2821 " }\n"
2822 " return true;\n"
2823 "}\n";
2824
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002825 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002826
2827 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002828 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002829
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002830 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002831}
2832
Olli Etuaho12690762015-03-31 12:55:28 +03002833TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2834{
2835 for (const auto &assignFunction : mArrayAssignmentFunctions)
2836 {
2837 if (assignFunction.type == type)
2838 {
2839 return assignFunction.functionName;
2840 }
2841 }
2842
2843 const TString &typeName = TypeString(type);
2844
2845 ArrayHelperFunction function;
2846 function.type = type;
2847
2848 TInfoSinkBase fnNameOut;
2849 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2850 function.functionName = fnNameOut.c_str();
2851
2852 TInfoSinkBase fnOut;
2853
2854 fnOut << "void " << function.functionName << "(out "
2855 << typeName << " a[" << type.getArraySize() << "], "
2856 << typeName << " b[" << type.getArraySize() << "])\n"
2857 << "{\n"
2858 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2859 " {\n"
2860 " a[i] = b[i];\n"
2861 " }\n"
2862 "}\n";
2863
2864 function.functionDefinition = fnOut.c_str();
2865
2866 mArrayAssignmentFunctions.push_back(function);
2867
2868 return function.functionName;
2869}
2870
Olli Etuaho9638c352015-04-01 14:34:52 +03002871TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2872{
2873 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2874 {
2875 if (constructIntoFunction.type == type)
2876 {
2877 return constructIntoFunction.functionName;
2878 }
2879 }
2880
2881 const TString &typeName = TypeString(type);
2882
2883 ArrayHelperFunction function;
2884 function.type = type;
2885
2886 TInfoSinkBase fnNameOut;
2887 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2888 function.functionName = fnNameOut.c_str();
2889
2890 TInfoSinkBase fnOut;
2891
2892 fnOut << "void " << function.functionName << "(out "
2893 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002894 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002895 {
2896 fnOut << ", " << typeName << " b" << i;
2897 }
2898 fnOut << ")\n"
2899 "{\n";
2900
Olli Etuaho856c4972016-08-08 11:38:39 +03002901 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002902 {
2903 fnOut << " a[" << i << "] = b" << i << ";\n";
2904 }
2905 fnOut << "}\n";
2906
2907 function.functionDefinition = fnOut.c_str();
2908
2909 mArrayConstructIntoFunctions.push_back(function);
2910
2911 return function.functionName;
2912}
2913
Jamie Madill2e295e22015-04-29 10:41:33 -04002914void OutputHLSL::ensureStructDefined(const TType &type)
2915{
2916 TStructure *structure = type.getStruct();
2917
2918 if (structure)
2919 {
2920 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2921 }
2922}
2923
2924
Olli Etuaho9638c352015-04-01 14:34:52 +03002925
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002926}