blob: 47c389458912461753d685f4591d4f9de9bc06ac [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
Olli Etuaho5858f7e2016-04-08 13:08:46 +030024#include "compiler/translator/TextureFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050026#include "compiler/translator/UniformHLSL.h"
27#include "compiler/translator/UtilsHLSL.h"
28#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050029#include "compiler/translator/util.h"
30
Olli Etuaho4785fec2015-05-18 16:09:37 +030031namespace
32{
33
34bool IsSequence(TIntermNode *node)
35{
36 return node->getAsAggregate() != nullptr && node->getAsAggregate()->getOp() == EOpSequence;
37}
38
Olli Etuaho18b9deb2015-11-05 12:14:50 +020039void WriteSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
40{
41 ASSERT(constUnion != nullptr);
42 switch (constUnion->getType())
43 {
44 case EbtFloat:
45 out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst()));
46 break;
47 case EbtInt:
48 out << constUnion->getIConst();
49 break;
50 case EbtUInt:
51 out << constUnion->getUConst();
52 break;
53 case EbtBool:
54 out << constUnion->getBConst();
55 break;
56 default:
57 UNREACHABLE();
58 }
59}
60
61const TConstantUnion *WriteConstantUnionArray(TInfoSinkBase &out,
62 const TConstantUnion *const constUnion,
63 const size_t size)
64{
65 const TConstantUnion *constUnionIterated = constUnion;
66 for (size_t i = 0; i < size; i++, constUnionIterated++)
67 {
68 WriteSingleConstant(out, constUnionIterated);
69
70 if (i != size - 1)
71 {
72 out << ", ";
73 }
74 }
75 return constUnionIterated;
76}
77
Olli Etuaho4785fec2015-05-18 16:09:37 +030078} // namespace
79
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000080namespace sh
81{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000082
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080083OutputHLSL::OutputHLSL(sh::GLenum shaderType,
84 int shaderVersion,
85 const TExtensionBehavior &extensionBehavior,
86 const char *sourcePath,
87 ShShaderOutput outputType,
88 int numRenderTargets,
89 const std::vector<Uniform> &uniforms,
90 ShCompileOptions compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -040091 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020092 mShaderType(shaderType),
93 mShaderVersion(shaderVersion),
94 mExtensionBehavior(extensionBehavior),
95 mSourcePath(sourcePath),
96 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -070097 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +100098 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -070099 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000100{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000101 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000102
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000103 mUsesFragColor = false;
104 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000105 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000106 mUsesFragCoord = false;
107 mUsesPointCoord = false;
108 mUsesFrontFacing = false;
109 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000110 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500111 mUsesVertexID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400112 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000113 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500114 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400115 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530116 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000117
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000118 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000119
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000120 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000121 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400122 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000123
124 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000125
Jamie Madill8daaba12014-06-13 10:04:33 -0400126 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200127 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300128 mTextureFunctionHLSL = new TextureFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400129
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200130 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000131 {
Arun Patole63419392015-03-13 11:51:07 +0530132 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
133 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
134 // In both cases total 3 uniform registers need to be reserved.
135 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000136 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000137
Geoff Lang00140f42016-02-03 18:47:33 +0000138 // Reserve registers for the default uniform block and driver constants
139 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140}
141
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000142OutputHLSL::~OutputHLSL()
143{
Jamie Madill8daaba12014-06-13 10:04:33 -0400144 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400145 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300146 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200147 for (auto &eqFunction : mStructEqualityFunctions)
148 {
149 SafeDelete(eqFunction);
150 }
151 for (auto &eqFunction : mArrayEqualityFunctions)
152 {
153 SafeDelete(eqFunction);
154 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000155}
156
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200157void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000158{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200159 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400160 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000161
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200162 BuiltInFunctionEmulator builtInFunctionEmulator;
163 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800164 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
165 {
166 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
167 mShaderVersion);
168 }
169
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200170 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500171
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700172 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700173 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
174 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300175 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700176 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700177
Jamie Madill37997142015-01-28 10:06:34 -0500178 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500179 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200180 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500181 mInfoSinkStack.pop();
182
Jamie Madill37997142015-01-28 10:06:34 -0500183 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500184 mInfoSinkStack.pop();
185
Jamie Madill32aab012015-01-27 14:12:26 -0500186 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500187 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500188 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000189
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200190 objSink << mHeader.c_str();
191 objSink << mBody.c_str();
192 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200193
194 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000195}
196
Jamie Madill570e04d2013-06-21 09:15:33 -0400197void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
198{
199 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
200 {
201 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
202
Jamie Madill32aab012015-01-27 14:12:26 -0500203 TInfoSinkBase structInfoSink;
204 mInfoSinkStack.push(&structInfoSink);
205
Jamie Madill570e04d2013-06-21 09:15:33 -0400206 // This will mark the necessary block elements as referenced
207 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500208
209 TString structName(structInfoSink.c_str());
210 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400211
212 mFlaggedStructOriginalNames[flaggedNode] = structName;
213
214 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
215 {
216 structName.erase(pos, 1);
217 }
218
219 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
220 }
221}
222
Jamie Madill4e1fd412014-07-10 17:50:10 -0400223const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
224{
225 return mUniformHLSL->getInterfaceBlockRegisterMap();
226}
227
Jamie Madill9fe25e92014-07-18 10:33:08 -0400228const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
229{
230 return mUniformHLSL->getUniformRegisterMap();
231}
232
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000233int OutputHLSL::vectorSize(const TType &type) const
234{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000235 int elementSize = type.isMatrix() ? type.getCols() : 1;
Olli Etuaho856c4972016-08-08 11:38:39 +0300236 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000237
238 return elementSize * arraySize;
239}
240
Jamie Madill98493dd2013-07-08 14:39:03 -0400241TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400242{
243 TString init;
244
245 TString preIndentString;
246 TString fullIndentString;
247
248 for (int spaces = 0; spaces < (indent * 4); spaces++)
249 {
250 preIndentString += ' ';
251 }
252
253 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
254 {
255 fullIndentString += ' ';
256 }
257
258 init += preIndentString + "{\n";
259
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 const TFieldList &fields = structure.fields();
261 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400262 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400264 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400265 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400266
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400268 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400269 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400270 }
271 else
272 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400273 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400274 }
275 }
276
277 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
278
279 return init;
280}
281
Jamie Madill8c46ab12015-12-07 16:39:19 -0500282void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000283{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000284 TString varyings;
285 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000287
Jamie Madill829f59e2013-11-13 19:40:54 -0500288 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400289 {
290 TIntermTyped *structNode = flaggedStructIt->first;
291 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400292 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400293 const TString &originalName = mFlaggedStructOriginalNames[structNode];
294
Jamie Madill033dae62014-06-18 12:56:28 -0400295 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400296 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400297 flaggedStructs += "\n";
298 }
299
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000300 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
301 {
302 const TType &type = varying->second->getType();
303 const TString &name = varying->second->getSymbol();
304
305 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400306 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
307 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000308 }
309
310 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
311 {
312 const TType &type = attribute->second->getType();
313 const TString &name = attribute->second->getSymbol();
314
Jamie Madill033dae62014-06-18 12:56:28 -0400315 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000316 }
317
Jamie Madill8daaba12014-06-13 10:04:33 -0400318 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400319
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200320 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400321 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
322
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200323 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500324 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200325 out << "\n// Equality functions\n\n";
326 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500327 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200328 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200329 }
330 }
Olli Etuaho12690762015-03-31 12:55:28 +0300331 if (!mArrayAssignmentFunctions.empty())
332 {
333 out << "\n// Assignment functions\n\n";
334 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
335 {
336 out << assignmentFunction.functionDefinition << "\n";
337 }
338 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300339 if (!mArrayConstructIntoFunctions.empty())
340 {
341 out << "\n// Array constructor functions\n\n";
342 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
343 {
344 out << constructIntoFunction.functionDefinition << "\n";
345 }
346 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200347
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500348 if (mUsesDiscardRewriting)
349 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400350 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500351 }
352
Nicolas Capens655fe362014-04-11 13:12:34 -0400353 if (mUsesNestedBreak)
354 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400355 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400356 }
357
Arun Patole44efa0b2015-03-04 17:11:05 +0530358 if (mRequiresIEEEStrictCompiling)
359 {
360 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
361 }
362
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400363 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
364 "#define LOOP [loop]\n"
365 "#define FLATTEN [flatten]\n"
366 "#else\n"
367 "#define LOOP\n"
368 "#define FLATTEN\n"
369 "#endif\n";
370
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200371 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200373 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
374 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000375
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000376 out << "// Varyings\n";
377 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400378 out << "\n";
379
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200380 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000381 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500382 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000383 {
Jamie Madill46131a32013-06-20 11:55:50 -0400384 const TString &variableName = outputVariableIt->first;
385 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400386
Jamie Madill033dae62014-06-18 12:56:28 -0400387 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400388 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000389 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000390 }
Jamie Madill46131a32013-06-20 11:55:50 -0400391 else
392 {
393 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
394
395 out << "static float4 gl_Color[" << numColorValues << "] =\n"
396 "{\n";
397 for (unsigned int i = 0; i < numColorValues; i++)
398 {
399 out << " float4(0, 0, 0, 0)";
400 if (i + 1 != numColorValues)
401 {
402 out << ",";
403 }
404 out << "\n";
405 }
406
407 out << "};\n";
408 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000409
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400410 if (mUsesFragDepth)
411 {
412 out << "static float gl_Depth = 0.0;\n";
413 }
414
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000415 if (mUsesFragCoord)
416 {
417 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
418 }
419
420 if (mUsesPointCoord)
421 {
422 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
423 }
424
425 if (mUsesFrontFacing)
426 {
427 out << "static bool gl_FrontFacing = false;\n";
428 }
429
430 out << "\n";
431
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000432 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000433 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000434 out << "struct gl_DepthRangeParameters\n"
435 "{\n"
436 " float near;\n"
437 " float far;\n"
438 " float diff;\n"
439 "};\n"
440 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000441 }
442
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200443 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000444 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000445 out << "cbuffer DriverConstants : register(b1)\n"
446 "{\n";
447
448 if (mUsesDepthRange)
449 {
450 out << " float3 dx_DepthRange : packoffset(c0);\n";
451 }
452
453 if (mUsesFragCoord)
454 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000455 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000456 }
457
458 if (mUsesFragCoord || mUsesFrontFacing)
459 {
460 out << " float3 dx_DepthFront : packoffset(c2);\n";
461 }
462
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800463 if (mUsesFragCoord)
464 {
465 // dx_ViewScale is only used in the fragment shader to correct
466 // the value for glFragCoord if necessary
467 out << " float2 dx_ViewScale : packoffset(c3);\n";
468 }
469
Olli Etuaho618bebc2016-01-15 16:40:00 +0200470 if (mOutputType == SH_HLSL_4_1_OUTPUT)
471 {
472 mUniformHLSL->samplerMetadataUniforms(out, "c4");
473 }
474
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000475 out << "};\n";
476 }
477 else
478 {
479 if (mUsesDepthRange)
480 {
481 out << "uniform float3 dx_DepthRange : register(c0);";
482 }
483
484 if (mUsesFragCoord)
485 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000486 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000487 }
488
489 if (mUsesFragCoord || mUsesFrontFacing)
490 {
491 out << "uniform float3 dx_DepthFront : register(c2);\n";
492 }
493 }
494
495 out << "\n";
496
497 if (mUsesDepthRange)
498 {
499 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
500 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000501 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000502
Jamie Madillf91ce812014-06-13 10:04:34 -0400503 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000504 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400505 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000506 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400507 out << flaggedStructs;
508 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000509 }
510
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000511 if (usingMRTExtension && mNumRenderTargets > 1)
512 {
513 out << "#define GL_USES_MRT\n";
514 }
515
516 if (mUsesFragColor)
517 {
518 out << "#define GL_USES_FRAG_COLOR\n";
519 }
520
521 if (mUsesFragData)
522 {
523 out << "#define GL_USES_FRAG_DATA\n";
524 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000526 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000528 out << "// Attributes\n";
529 out << attributes;
530 out << "\n"
531 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400532
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000533 if (mUsesPointSize)
534 {
535 out << "static float gl_PointSize = float(1);\n";
536 }
537
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000538 if (mUsesInstanceID)
539 {
540 out << "static int gl_InstanceID;";
541 }
542
Corentin Wallezb076add2016-01-11 16:45:46 -0500543 if (mUsesVertexID)
544 {
545 out << "static int gl_VertexID;";
546 }
547
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000548 out << "\n"
549 "// Varyings\n";
550 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000551 out << "\n";
552
553 if (mUsesDepthRange)
554 {
555 out << "struct gl_DepthRangeParameters\n"
556 "{\n"
557 " float near;\n"
558 " float far;\n"
559 " float diff;\n"
560 "};\n"
561 "\n";
562 }
563
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200564 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000565 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800566 out << "cbuffer DriverConstants : register(b1)\n"
567 "{\n";
568
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000569 if (mUsesDepthRange)
570 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800571 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000572 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800573
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800574 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
575 // shaders. However, we declare it for all shaders (including Feature Level 10+).
576 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
577 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800578 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800579 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800580 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800581
Olli Etuaho618bebc2016-01-15 16:40:00 +0200582 if (mOutputType == SH_HLSL_4_1_OUTPUT)
583 {
584 mUniformHLSL->samplerMetadataUniforms(out, "c4");
585 }
586
Austin Kinross4fd18b12014-12-22 12:32:05 -0800587 out << "};\n"
588 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000589 }
590 else
591 {
592 if (mUsesDepthRange)
593 {
594 out << "uniform float3 dx_DepthRange : register(c0);\n";
595 }
596
Cooper Partine6664f02015-01-09 16:22:24 -0800597 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
598 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000599 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000600 }
601
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000602 if (mUsesDepthRange)
603 {
604 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
605 "\n";
606 }
607
Jamie Madillf91ce812014-06-13 10:04:34 -0400608 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000609 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400610 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000611 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400612 out << flaggedStructs;
613 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000614 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400615 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000616
Geoff Lang1fe74c72016-08-25 13:23:01 -0400617 bool getDimensionsIgnoresBaseLevel =
618 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
619 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000621 if (mUsesFragCoord)
622 {
623 out << "#define GL_USES_FRAG_COORD\n";
624 }
625
626 if (mUsesPointCoord)
627 {
628 out << "#define GL_USES_POINT_COORD\n";
629 }
630
631 if (mUsesFrontFacing)
632 {
633 out << "#define GL_USES_FRONT_FACING\n";
634 }
635
636 if (mUsesPointSize)
637 {
638 out << "#define GL_USES_POINT_SIZE\n";
639 }
640
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400641 if (mUsesFragDepth)
642 {
643 out << "#define GL_USES_FRAG_DEPTH\n";
644 }
645
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000646 if (mUsesDepthRange)
647 {
648 out << "#define GL_USES_DEPTH_RANGE\n";
649 }
650
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000651 if (mUsesXor)
652 {
653 out << "bool xor(bool p, bool q)\n"
654 "{\n"
655 " return (p || q) && !(p && q);\n"
656 "}\n"
657 "\n";
658 }
659
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200660 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661}
662
663void OutputHLSL::visitSymbol(TIntermSymbol *node)
664{
Jamie Madill32aab012015-01-27 14:12:26 -0500665 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
Jamie Madill570e04d2013-06-21 09:15:33 -0400667 // Handle accessing std140 structs by value
668 if (mFlaggedStructMappedNames.count(node) > 0)
669 {
670 out << mFlaggedStructMappedNames[node];
671 return;
672 }
673
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674 TString name = node->getSymbol();
675
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000676 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000677 {
678 mUsesDepthRange = true;
679 out << name;
680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681 else
682 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000683 TQualifier qualifier = node->getQualifier();
684
685 if (qualifier == EvqUniform)
686 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400687 const TType &nodeType = node->getType();
688 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400689
690 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000691 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400692 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000693 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000694 else
695 {
696 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000697 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400698
Jamie Madill2e295e22015-04-29 10:41:33 -0400699 ensureStructDefined(nodeType);
700
Olli Etuaho96963162016-03-21 11:54:33 +0200701 const TName &nameWithMetadata = node->getName();
702 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000703 }
Jamie Madill19571812013-08-12 15:26:34 -0700704 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000705 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000706 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400707 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000708 }
Jamie Madill033dae62014-06-18 12:56:28 -0400709 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000710 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000711 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400712 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000713 }
Jamie Madill19571812013-08-12 15:26:34 -0700714 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400715 {
716 mReferencedOutputVariables[name] = node;
717 out << "out_" << name;
718 }
719 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000720 {
721 out << "gl_Color[0]";
722 mUsesFragColor = true;
723 }
724 else if (qualifier == EvqFragData)
725 {
726 out << "gl_Color";
727 mUsesFragData = true;
728 }
729 else if (qualifier == EvqFragCoord)
730 {
731 mUsesFragCoord = true;
732 out << name;
733 }
734 else if (qualifier == EvqPointCoord)
735 {
736 mUsesPointCoord = true;
737 out << name;
738 }
739 else if (qualifier == EvqFrontFacing)
740 {
741 mUsesFrontFacing = true;
742 out << name;
743 }
744 else if (qualifier == EvqPointSize)
745 {
746 mUsesPointSize = true;
747 out << name;
748 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000749 else if (qualifier == EvqInstanceID)
750 {
751 mUsesInstanceID = true;
752 out << name;
753 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500754 else if (qualifier == EvqVertexID)
755 {
756 mUsesVertexID = true;
757 out << name;
758 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300759 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400760 {
761 mUsesFragDepth = true;
762 out << "gl_Depth";
763 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000764 else
765 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300766 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000767 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768 }
769}
770
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400771void OutputHLSL::visitRaw(TIntermRaw *node)
772{
Jamie Madill32aab012015-01-27 14:12:26 -0500773 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400774}
775
Olli Etuaho7fb49552015-03-18 17:27:44 +0200776void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
777{
778 if (type.isScalar() && !type.isArray())
779 {
780 if (op == EOpEqual)
781 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500782 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200783 }
784 else
785 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500786 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200787 }
788 }
789 else
790 {
791 if (visit == PreVisit && op == EOpNotEqual)
792 {
793 out << "!";
794 }
795
796 if (type.isArray())
797 {
798 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500799 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200800 }
801 else if (type.getBasicType() == EbtStruct)
802 {
803 const TStructure &structure = *type.getStruct();
804 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500805 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200806 }
807 else
808 {
809 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500810 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200811 }
812 }
813}
814
Olli Etuaho96963162016-03-21 11:54:33 +0200815bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
816{
817 // Inside InVisit the current node is already in the path.
818 const unsigned int initialN = visit == InVisit ? 1u : 0u;
819 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
820 {
821 TIntermNode *ancestor = getAncestorNode(n);
822 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
823 if (ancestorBinary == nullptr)
824 {
825 return false;
826 }
827 switch (ancestorBinary->getOp())
828 {
829 case EOpIndexDirectStruct:
830 {
831 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
832 const TIntermConstantUnion *index =
833 ancestorBinary->getRight()->getAsConstantUnion();
834 const TField *field = structure->fields()[index->getIConst(0)];
835 if (IsSampler(field->type()->getBasicType()))
836 {
837 return true;
838 }
839 break;
840 }
841 case EOpIndexDirect:
842 break;
843 default:
844 // Returning a sampler from indirect indexing is not supported.
845 return false;
846 }
847 }
848 return false;
849}
850
Olli Etuahob6fa0432016-09-28 16:28:05 +0100851bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
852{
853 TInfoSinkBase &out = getInfoSink();
854 if (visit == PostVisit)
855 {
856 out << ".";
857 node->writeOffsetsAsXYZW(&out);
858 }
859 return true;
860}
861
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
863{
Jamie Madill32aab012015-01-27 14:12:26 -0500864 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865
Jamie Madill570e04d2013-06-21 09:15:33 -0400866 // Handle accessing std140 structs by value
867 if (mFlaggedStructMappedNames.count(node) > 0)
868 {
869 out << mFlaggedStructMappedNames[node];
870 return false;
871 }
872
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873 switch (node->getOp())
874 {
Olli Etuahoe79904c2015-03-18 16:56:42 +0200875 case EOpAssign:
876 if (node->getLeft()->isArray())
877 {
Olli Etuaho9638c352015-04-01 14:34:52 +0300878 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
879 if (rightAgg != nullptr && rightAgg->isConstructor())
880 {
881 const TString &functionName = addArrayConstructIntoFunction(node->getType());
882 out << functionName << "(";
883 node->getLeft()->traverse(this);
884 TIntermSequence *seq = rightAgg->getSequence();
885 for (auto &arrayElement : *seq)
886 {
887 out << ", ";
888 arrayElement->traverse(this);
889 }
890 out << ")";
891 return false;
892 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +0300893 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
894 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
895
896 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500897 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200898 }
899 else
900 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500901 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200902 }
903 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000904 case EOpInitialize:
905 if (visit == PreVisit)
906 {
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000907 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -0500908 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000909 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000910
Olli Etuaho3d932d82016-04-12 11:10:30 +0300911 // Global initializers must be constant at this point.
Olli Etuahod4f4c112016-04-15 15:11:24 +0300912 ASSERT(symbolNode->getQualifier() != EvqGlobal || canWriteAsHLSLLiteral(expression));
913
914 // GLSL allows to write things like "float x = x;" where a new variable x is defined
915 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
916 // new variable is created before the assignment is evaluated), so we need to convert
917 // this to "float t = x, x = t;".
Olli Etuaho3d932d82016-04-12 11:10:30 +0300918 if (writeSameSymbolInitializer(out, symbolNode, expression))
Jamie Madill37997142015-01-28 10:06:34 -0500919 {
920 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000921 return false;
922 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200923 else if (writeConstantInitialization(out, symbolNode, expression))
924 {
925 return false;
926 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000927 }
928 else if (visit == InVisit)
929 {
930 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000931 }
932 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500933 case EOpAddAssign:
934 outputTriplet(out, visit, "(", " += ", ")");
935 break;
936 case EOpSubAssign:
937 outputTriplet(out, visit, "(", " -= ", ")");
938 break;
939 case EOpMulAssign:
940 outputTriplet(out, visit, "(", " *= ", ")");
941 break;
942 case EOpVectorTimesScalarAssign:
943 outputTriplet(out, visit, "(", " *= ", ")");
944 break;
945 case EOpMatrixTimesScalarAssign:
946 outputTriplet(out, visit, "(", " *= ", ")");
947 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000948 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000949 if (visit == PreVisit)
950 {
951 out << "(";
952 }
953 else if (visit == InVisit)
954 {
955 out << " = mul(";
956 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -0400957 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000958 }
959 else
960 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000961 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000962 }
963 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000964 case EOpMatrixTimesMatrixAssign:
965 if (visit == PreVisit)
966 {
967 out << "(";
968 }
969 else if (visit == InVisit)
970 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200971 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000972 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +0200973 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000974 }
975 else
976 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200977 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000978 }
979 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500980 case EOpDivAssign:
981 outputTriplet(out, visit, "(", " /= ", ")");
982 break;
983 case EOpIModAssign:
984 outputTriplet(out, visit, "(", " %= ", ")");
985 break;
986 case EOpBitShiftLeftAssign:
987 outputTriplet(out, visit, "(", " <<= ", ")");
988 break;
989 case EOpBitShiftRightAssign:
990 outputTriplet(out, visit, "(", " >>= ", ")");
991 break;
992 case EOpBitwiseAndAssign:
993 outputTriplet(out, visit, "(", " &= ", ")");
994 break;
995 case EOpBitwiseXorAssign:
996 outputTriplet(out, visit, "(", " ^= ", ")");
997 break;
998 case EOpBitwiseOrAssign:
999 outputTriplet(out, visit, "(", " |= ", ")");
1000 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001001 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001002 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001003 const TType& leftType = node->getLeft()->getType();
1004 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001005 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001006 if (visit == PreVisit)
1007 {
1008 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1009 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001010 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001011 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001012 return false;
1013 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001014 }
Olli Etuaho96963162016-03-21 11:54:33 +02001015 else if (ancestorEvaluatesToSamplerInStruct(visit))
1016 {
1017 // All parts of an expression that access a sampler in a struct need to use _ as
1018 // separator to access the sampler variable that has been moved out of the struct.
1019 outputTriplet(out, visit, "", "_", "");
1020 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001021 else
1022 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001023 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001024 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001025 }
1026 break;
1027 case EOpIndexIndirect:
1028 // We do not currently support indirect references to interface blocks
1029 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001030 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001031 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001032 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001033 {
1034 const TStructure* structure = node->getLeft()->getType().getStruct();
1035 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1036 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001037
Olli Etuaho96963162016-03-21 11:54:33 +02001038 // In cases where indexing returns a sampler, we need to access the sampler variable
1039 // that has been moved out of the struct.
1040 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1041 if (visit == PreVisit && indexingReturnsSampler)
1042 {
1043 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1044 // This prefix is only output at the beginning of the indexing expression, which
1045 // may have multiple parts.
1046 out << "angle";
1047 }
1048 if (!indexingReturnsSampler)
1049 {
1050 // All parts of an expression that access a sampler in a struct need to use _ as
1051 // separator to access the sampler variable that has been moved out of the struct.
1052 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1053 }
1054 if (visit == InVisit)
1055 {
1056 if (indexingReturnsSampler)
1057 {
1058 out << "_" + field->name();
1059 }
1060 else
1061 {
1062 out << "." + DecorateField(field->name(), *structure);
1063 }
1064
1065 return false;
1066 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001067 }
1068 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001069 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001070 if (visit == InVisit)
1071 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001072 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1073 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1074 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001075 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001076
1077 return false;
1078 }
1079 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001080 case EOpAdd:
1081 outputTriplet(out, visit, "(", " + ", ")");
1082 break;
1083 case EOpSub:
1084 outputTriplet(out, visit, "(", " - ", ")");
1085 break;
1086 case EOpMul:
1087 outputTriplet(out, visit, "(", " * ", ")");
1088 break;
1089 case EOpDiv:
1090 outputTriplet(out, visit, "(", " / ", ")");
1091 break;
1092 case EOpIMod:
1093 outputTriplet(out, visit, "(", " % ", ")");
1094 break;
1095 case EOpBitShiftLeft:
1096 outputTriplet(out, visit, "(", " << ", ")");
1097 break;
1098 case EOpBitShiftRight:
1099 outputTriplet(out, visit, "(", " >> ", ")");
1100 break;
1101 case EOpBitwiseAnd:
1102 outputTriplet(out, visit, "(", " & ", ")");
1103 break;
1104 case EOpBitwiseXor:
1105 outputTriplet(out, visit, "(", " ^ ", ")");
1106 break;
1107 case EOpBitwiseOr:
1108 outputTriplet(out, visit, "(", " | ", ")");
1109 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001110 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001111 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001112 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001113 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001114 case EOpLessThan:
1115 outputTriplet(out, visit, "(", " < ", ")");
1116 break;
1117 case EOpGreaterThan:
1118 outputTriplet(out, visit, "(", " > ", ")");
1119 break;
1120 case EOpLessThanEqual:
1121 outputTriplet(out, visit, "(", " <= ", ")");
1122 break;
1123 case EOpGreaterThanEqual:
1124 outputTriplet(out, visit, "(", " >= ", ")");
1125 break;
1126 case EOpVectorTimesScalar:
1127 outputTriplet(out, visit, "(", " * ", ")");
1128 break;
1129 case EOpMatrixTimesScalar:
1130 outputTriplet(out, visit, "(", " * ", ")");
1131 break;
1132 case EOpVectorTimesMatrix:
1133 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1134 break;
1135 case EOpMatrixTimesVector:
1136 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1137 break;
1138 case EOpMatrixTimesMatrix:
1139 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1140 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001141 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001142 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1143 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001144 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001145 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001146 case EOpLogicalXor:
1147 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001148 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001149 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001150 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001151 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1152 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001153 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001154 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155 default: UNREACHABLE();
1156 }
1157
1158 return true;
1159}
1160
1161bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1162{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001163 TInfoSinkBase &out = getInfoSink();
1164
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165 switch (node->getOp())
1166 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001167 case EOpNegative:
1168 outputTriplet(out, visit, "(-", "", ")");
1169 break;
1170 case EOpPositive:
1171 outputTriplet(out, visit, "(+", "", ")");
1172 break;
1173 case EOpVectorLogicalNot:
1174 outputTriplet(out, visit, "(!", "", ")");
1175 break;
1176 case EOpLogicalNot:
1177 outputTriplet(out, visit, "(!", "", ")");
1178 break;
1179 case EOpBitwiseNot:
1180 outputTriplet(out, visit, "(~", "", ")");
1181 break;
1182 case EOpPostIncrement:
1183 outputTriplet(out, visit, "(", "", "++)");
1184 break;
1185 case EOpPostDecrement:
1186 outputTriplet(out, visit, "(", "", "--)");
1187 break;
1188 case EOpPreIncrement:
1189 outputTriplet(out, visit, "(++", "", ")");
1190 break;
1191 case EOpPreDecrement:
1192 outputTriplet(out, visit, "(--", "", ")");
1193 break;
1194 case EOpRadians:
1195 outputTriplet(out, visit, "radians(", "", ")");
1196 break;
1197 case EOpDegrees:
1198 outputTriplet(out, visit, "degrees(", "", ")");
1199 break;
1200 case EOpSin:
1201 outputTriplet(out, visit, "sin(", "", ")");
1202 break;
1203 case EOpCos:
1204 outputTriplet(out, visit, "cos(", "", ")");
1205 break;
1206 case EOpTan:
1207 outputTriplet(out, visit, "tan(", "", ")");
1208 break;
1209 case EOpAsin:
1210 outputTriplet(out, visit, "asin(", "", ")");
1211 break;
1212 case EOpAcos:
1213 outputTriplet(out, visit, "acos(", "", ")");
1214 break;
1215 case EOpAtan:
1216 outputTriplet(out, visit, "atan(", "", ")");
1217 break;
1218 case EOpSinh:
1219 outputTriplet(out, visit, "sinh(", "", ")");
1220 break;
1221 case EOpCosh:
1222 outputTriplet(out, visit, "cosh(", "", ")");
1223 break;
1224 case EOpTanh:
1225 outputTriplet(out, visit, "tanh(", "", ")");
1226 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001227 case EOpAsinh:
1228 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001229 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001230 break;
1231 case EOpAcosh:
1232 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001233 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001234 break;
1235 case EOpAtanh:
1236 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001237 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001238 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001239 case EOpExp:
1240 outputTriplet(out, visit, "exp(", "", ")");
1241 break;
1242 case EOpLog:
1243 outputTriplet(out, visit, "log(", "", ")");
1244 break;
1245 case EOpExp2:
1246 outputTriplet(out, visit, "exp2(", "", ")");
1247 break;
1248 case EOpLog2:
1249 outputTriplet(out, visit, "log2(", "", ")");
1250 break;
1251 case EOpSqrt:
1252 outputTriplet(out, visit, "sqrt(", "", ")");
1253 break;
1254 case EOpInverseSqrt:
1255 outputTriplet(out, visit, "rsqrt(", "", ")");
1256 break;
1257 case EOpAbs:
1258 outputTriplet(out, visit, "abs(", "", ")");
1259 break;
1260 case EOpSign:
1261 outputTriplet(out, visit, "sign(", "", ")");
1262 break;
1263 case EOpFloor:
1264 outputTriplet(out, visit, "floor(", "", ")");
1265 break;
1266 case EOpTrunc:
1267 outputTriplet(out, visit, "trunc(", "", ")");
1268 break;
1269 case EOpRound:
1270 outputTriplet(out, visit, "round(", "", ")");
1271 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001272 case EOpRoundEven:
1273 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001274 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001275 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001276 case EOpCeil:
1277 outputTriplet(out, visit, "ceil(", "", ")");
1278 break;
1279 case EOpFract:
1280 outputTriplet(out, visit, "frac(", "", ")");
1281 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301282 case EOpIsNan:
Shao6f0a0dc2016-09-27 13:51:29 +08001283 if (node->getUseEmulatedFunction())
1284 writeEmulatedFunctionTriplet(out, visit, "isnan(");
1285 else
1286 outputTriplet(out, visit, "isnan(", "", ")");
1287 mRequiresIEEEStrictCompiling = true;
1288 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001289 case EOpIsInf:
1290 outputTriplet(out, visit, "isinf(", "", ")");
1291 break;
1292 case EOpFloatBitsToInt:
1293 outputTriplet(out, visit, "asint(", "", ")");
1294 break;
1295 case EOpFloatBitsToUint:
1296 outputTriplet(out, visit, "asuint(", "", ")");
1297 break;
1298 case EOpIntBitsToFloat:
1299 outputTriplet(out, visit, "asfloat(", "", ")");
1300 break;
1301 case EOpUintBitsToFloat:
1302 outputTriplet(out, visit, "asfloat(", "", ")");
1303 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001304 case EOpPackSnorm2x16:
1305 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001306 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001307 break;
1308 case EOpPackUnorm2x16:
1309 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001310 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001311 break;
1312 case EOpPackHalf2x16:
1313 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001314 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001315 break;
1316 case EOpUnpackSnorm2x16:
1317 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001318 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001319 break;
1320 case EOpUnpackUnorm2x16:
1321 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001322 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001323 break;
1324 case EOpUnpackHalf2x16:
1325 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001326 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001327 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001328 case EOpLength:
1329 outputTriplet(out, visit, "length(", "", ")");
1330 break;
1331 case EOpNormalize:
1332 outputTriplet(out, visit, "normalize(", "", ")");
1333 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001334 case EOpDFdx:
1335 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1336 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001337 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001338 }
1339 else
1340 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001341 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001342 }
1343 break;
1344 case EOpDFdy:
1345 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1346 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001347 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001348 }
1349 else
1350 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001351 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001352 }
1353 break;
1354 case EOpFwidth:
1355 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1356 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001357 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001358 }
1359 else
1360 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001361 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001362 }
1363 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001364 case EOpTranspose:
1365 outputTriplet(out, visit, "transpose(", "", ")");
1366 break;
1367 case EOpDeterminant:
1368 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1369 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001370 case EOpInverse:
1371 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001372 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001373 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001374
Jamie Madill8c46ab12015-12-07 16:39:19 -05001375 case EOpAny:
1376 outputTriplet(out, visit, "any(", "", ")");
1377 break;
1378 case EOpAll:
1379 outputTriplet(out, visit, "all(", "", ")");
1380 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381 default: UNREACHABLE();
1382 }
1383
1384 return true;
1385}
1386
Olli Etuaho96963162016-03-21 11:54:33 +02001387TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1388{
1389 if (node->getAsSymbolNode())
1390 {
1391 return node->getAsSymbolNode()->getSymbol();
1392 }
1393 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1394 switch (nodeBinary->getOp())
1395 {
1396 case EOpIndexDirect:
1397 {
1398 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1399
1400 TInfoSinkBase prefixSink;
1401 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1402 return TString(prefixSink.c_str());
1403 }
1404 case EOpIndexDirectStruct:
1405 {
1406 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1407 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1408 const TField *field = s->fields()[index];
1409
1410 TInfoSinkBase prefixSink;
1411 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1412 << field->name();
1413 return TString(prefixSink.c_str());
1414 }
1415 default:
1416 UNREACHABLE();
1417 return TString("");
1418 }
1419}
1420
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001421bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1422{
Jamie Madill32aab012015-01-27 14:12:26 -05001423 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001424
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001425 switch (node->getOp())
1426 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001427 case EOpSequence:
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001428 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001429 if (mInsideFunction)
1430 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001431 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001432 out << "{\n";
1433 }
1434
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001435 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001436 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001437 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001438
Olli Etuahoa6f22092015-05-08 18:31:10 +03001439 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001440
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001441 // Don't output ; after case labels, they're terminated by :
1442 // This is needed especially since outputting a ; after a case statement would turn empty
1443 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho57961272016-09-14 13:57:46 +03001444 // Also no need to output ; after if statements or sequences. This is done just for
1445 // code clarity.
1446 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001447 !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001448 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001449 }
1450
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001451 if (mInsideFunction)
1452 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001453 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001454 out << "}\n";
1455 }
1456
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001457 return false;
1458 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001459 case EOpDeclaration:
1460 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001461 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001462 TIntermSequence *sequence = node->getSequence();
1463 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1464 ASSERT(sequence->size() == 1);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001465
Olli Etuaho5878f832016-10-07 10:14:58 +01001466 if (variable &&
1467 (variable->getQualifier() == EvqTemporary ||
1468 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001469 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001470 ensureStructDefined(variable->getType());
1471
1472 if (!variable->getAsSymbolNode() ||
1473 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001474 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001475 if (!mInsideFunction)
1476 {
1477 out << "static ";
1478 }
1479
1480 out << TypeString(variable->getType()) + " ";
1481
1482 TIntermSymbol *symbol = variable->getAsSymbolNode();
1483
1484 if (symbol)
1485 {
1486 symbol->traverse(this);
1487 out << ArrayString(symbol->getType());
1488 out << " = " + initializer(symbol->getType());
1489 }
1490 else
1491 {
1492 variable->traverse(this);
1493 }
Olli Etuahoa6f22092015-05-08 18:31:10 +03001494 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001495 else if (variable->getAsSymbolNode() &&
1496 variable->getAsSymbolNode()->getSymbol() ==
1497 "") // Type (struct) declaration
Olli Etuahoa6f22092015-05-08 18:31:10 +03001498 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001499 // Already added to constructor map
Olli Etuahoa6f22092015-05-08 18:31:10 +03001500 }
1501 else
Olli Etuaho5878f832016-10-07 10:14:58 +01001502 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001504 else if (variable && IsVaryingOut(variable->getQualifier()))
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001505 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001506 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end();
1507 sit++)
1508 {
1509 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001510
Olli Etuaho5878f832016-10-07 10:14:58 +01001511 if (symbol)
1512 {
1513 // Vertex (output) varyings which are declared but not written to should
1514 // still be declared to allow successful linking
1515 mReferencedVaryings[symbol->getSymbol()] = symbol;
1516 }
1517 else
1518 {
1519 (*sit)->traverse(this);
1520 }
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001521 }
1522 }
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001523
Corentin Wallez1239ee92015-03-19 14:38:02 -07001524 return false;
1525 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001526 else if (visit == InVisit)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001527 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001528 out << ", ";
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001529 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001530 break;
1531 case EOpInvariantDeclaration:
1532 // Do not do any translation
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001533 return false;
Olli Etuaho5878f832016-10-07 10:14:58 +01001534 case EOpPrototype:
1535 if (visit == PreVisit)
1536 {
1537 size_t index = mCallDag.findIndex(node);
1538 // Skip the prototype if it is not implemented (and thus not used)
1539 if (index == CallDAG::InvalidIndex)
1540 {
1541 return false;
1542 }
1543
1544 TIntermSequence *arguments = node->getSequence();
1545
1546 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1547 out << TypeString(node->getType()) << " " << name
1548 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
1549
1550 for (unsigned int i = 0; i < arguments->size(); i++)
1551 {
1552 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1553
1554 if (symbol)
1555 {
1556 out << argumentString(symbol);
1557
1558 if (i < arguments->size() - 1)
1559 {
1560 out << ", ";
1561 }
1562 }
1563 else
1564 UNREACHABLE();
1565 }
1566
1567 out << ");\n";
1568
1569 // Also prototype the Lod0 variant if needed
1570 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1571 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1572 {
1573 mOutputLod0Function = true;
1574 node->traverse(this);
1575 mOutputLod0Function = false;
1576 }
1577
1578 return false;
1579 }
1580 break;
1581 case EOpComma:
1582 outputTriplet(out, visit, "(", ", ", ")");
1583 break;
1584 case EOpFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001586 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001587 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001588
Corentin Wallez1239ee92015-03-19 14:38:02 -07001589 size_t index = mCallDag.findIndex(node);
1590 ASSERT(index != CallDAG::InvalidIndex);
1591 mCurrentFunctionMetadata = &mASTMetadataList[index];
1592
Jamie Madill033dae62014-06-18 12:56:28 -04001593 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001594
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001595 TIntermSequence *sequence = node->getSequence();
1596 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
1597
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001598 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001599 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001600 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001601 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001602 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001603 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001604 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001605 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001606 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001607
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001608 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001609 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001610 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001611
1612 if (symbol)
1613 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001614 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001615
1616 out << argumentString(symbol);
1617
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001618 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001619 {
1620 out << ", ";
1621 }
1622 }
1623 else UNREACHABLE();
1624 }
1625
Olli Etuaho4785fec2015-05-18 16:09:37 +03001626 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001627
Olli Etuahof51fdd22016-10-03 10:03:40 +01001628 mInsideFunction = true;
1629 ASSERT(sequence->size() == 2);
1630 TIntermNode *body = (*sequence)[1];
1631 // The function body node will output braces.
1632 ASSERT(IsSequence(body));
1633 body->traverse(this);
1634 mInsideFunction = false;
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001635
Corentin Wallez1239ee92015-03-19 14:38:02 -07001636 mCurrentFunctionMetadata = nullptr;
1637
1638 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1639 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001640 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001641 ASSERT(name != "main");
1642 mOutputLod0Function = true;
1643 node->traverse(this);
1644 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001645 }
1646
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001647 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001649 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001650 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001651 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001652
Corentin Wallez1239ee92015-03-19 14:38:02 -07001653 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001654 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001655 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001656 if (node->isArray())
1657 {
1658 UNIMPLEMENTED();
1659 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07001660 size_t index = mCallDag.findIndex(node);
1661 ASSERT(index != CallDAG::InvalidIndex);
1662 lod0 &= mASTMetadataList[index].mNeedsLod0;
1663
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001664 out << DecorateFunctionIfNeeded(node->getNameObj());
1665 out << DisambiguateFunctionName(node->getSequence());
1666 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 }
Olli Etuahob741c762016-06-29 15:49:22 +03001668 else if (node->getNameObj().isInternal())
1669 {
1670 // This path is used for internal functions that don't have their definitions in the
1671 // AST, such as precision emulation functions.
1672 out << DecorateFunctionIfNeeded(node->getNameObj()) << "(";
1673 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674 else
1675 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001676 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001677 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001678 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1679 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1680 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1681 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001682 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001683
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001684 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001685 {
Olli Etuaho96963162016-03-21 11:54:33 +02001686 TIntermTyped *typedArg = (*arg)->getAsTyped();
1687 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001688 {
1689 out << "texture_";
1690 (*arg)->traverse(this);
1691 out << ", sampler_";
1692 }
1693
1694 (*arg)->traverse(this);
1695
Olli Etuaho96963162016-03-21 11:54:33 +02001696 if (typedArg->getType().isStructureContainingSamplers())
1697 {
1698 const TType &argType = typedArg->getType();
1699 TVector<TIntermSymbol *> samplerSymbols;
1700 TString structName = samplerNamePrefixFromStruct(typedArg);
1701 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001702 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001703 &samplerSymbols, nullptr);
1704 for (const TIntermSymbol *sampler : samplerSymbols)
1705 {
1706 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1707 {
1708 out << ", texture_" << sampler->getSymbol();
1709 out << ", sampler_" << sampler->getSymbol();
1710 }
1711 else
1712 {
1713 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1714 // of D3D9, it's the sampler variable.
1715 out << ", " + sampler->getSymbol();
1716 }
1717 }
1718 }
1719
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001720 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001721 {
1722 out << ", ";
1723 }
1724 }
1725
1726 out << ")";
1727
1728 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001729 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001730 case EOpParameters:
1731 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1732 break;
1733 case EOpConstructFloat:
1734 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1735 break;
1736 case EOpConstructVec2:
1737 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1738 break;
1739 case EOpConstructVec3:
1740 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1741 break;
1742 case EOpConstructVec4:
1743 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1744 break;
1745 case EOpConstructBool:
1746 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1747 break;
1748 case EOpConstructBVec2:
1749 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1750 break;
1751 case EOpConstructBVec3:
1752 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1753 break;
1754 case EOpConstructBVec4:
1755 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1756 break;
1757 case EOpConstructInt:
1758 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1759 break;
1760 case EOpConstructIVec2:
1761 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1762 break;
1763 case EOpConstructIVec3:
1764 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1765 break;
1766 case EOpConstructIVec4:
1767 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1768 break;
1769 case EOpConstructUInt:
1770 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1771 break;
1772 case EOpConstructUVec2:
1773 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1774 break;
1775 case EOpConstructUVec3:
1776 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1777 break;
1778 case EOpConstructUVec4:
1779 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1780 break;
1781 case EOpConstructMat2:
1782 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1783 break;
1784 case EOpConstructMat2x3:
1785 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1786 break;
1787 case EOpConstructMat2x4:
1788 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1789 break;
1790 case EOpConstructMat3x2:
1791 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1792 break;
1793 case EOpConstructMat3:
1794 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1795 break;
1796 case EOpConstructMat3x4:
1797 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1798 break;
1799 case EOpConstructMat4x2:
1800 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1801 break;
1802 case EOpConstructMat4x3:
1803 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1804 break;
1805 case EOpConstructMat4:
1806 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1807 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001808 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001809 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001810 if (node->getType().isArray())
1811 {
1812 UNIMPLEMENTED();
1813 }
Jamie Madill033dae62014-06-18 12:56:28 -04001814 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001815 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001816 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001817 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001818 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001819 case EOpLessThan:
1820 outputTriplet(out, visit, "(", " < ", ")");
1821 break;
1822 case EOpGreaterThan:
1823 outputTriplet(out, visit, "(", " > ", ")");
1824 break;
1825 case EOpLessThanEqual:
1826 outputTriplet(out, visit, "(", " <= ", ")");
1827 break;
1828 case EOpGreaterThanEqual:
1829 outputTriplet(out, visit, "(", " >= ", ")");
1830 break;
1831 case EOpVectorEqual:
1832 outputTriplet(out, visit, "(", " == ", ")");
1833 break;
1834 case EOpVectorNotEqual:
1835 outputTriplet(out, visit, "(", " != ", ")");
1836 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001837 case EOpMod:
1838 ASSERT(node->getUseEmulatedFunction());
1839 writeEmulatedFunctionTriplet(out, visit, "mod(");
1840 break;
1841 case EOpModf:
1842 outputTriplet(out, visit, "modf(", ", ", ")");
1843 break;
1844 case EOpPow:
1845 outputTriplet(out, visit, "pow(", ", ", ")");
1846 break;
1847 case EOpAtan:
1848 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1849 ASSERT(node->getUseEmulatedFunction());
1850 writeEmulatedFunctionTriplet(out, visit, "atan(");
1851 break;
1852 case EOpMin:
1853 outputTriplet(out, visit, "min(", ", ", ")");
1854 break;
1855 case EOpMax:
1856 outputTriplet(out, visit, "max(", ", ", ")");
1857 break;
1858 case EOpClamp:
1859 outputTriplet(out, visit, "clamp(", ", ", ")");
1860 break;
1861 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301862 {
1863 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1864 if (lastParamNode->getType().getBasicType() == EbtBool)
1865 {
1866 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1867 // so use emulated version.
1868 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001869 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301870 }
1871 else
1872 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001873 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301874 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001875 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301876 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001877 case EOpStep:
1878 outputTriplet(out, visit, "step(", ", ", ")");
1879 break;
1880 case EOpSmoothStep:
1881 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1882 break;
1883 case EOpDistance:
1884 outputTriplet(out, visit, "distance(", ", ", ")");
1885 break;
1886 case EOpDot:
1887 outputTriplet(out, visit, "dot(", ", ", ")");
1888 break;
1889 case EOpCross:
1890 outputTriplet(out, visit, "cross(", ", ", ")");
1891 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001892 case EOpFaceForward:
1893 ASSERT(node->getUseEmulatedFunction());
1894 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
1895 break;
1896 case EOpReflect:
1897 outputTriplet(out, visit, "reflect(", ", ", ")");
1898 break;
1899 case EOpRefract:
1900 outputTriplet(out, visit, "refract(", ", ", ")");
1901 break;
1902 case EOpOuterProduct:
1903 ASSERT(node->getUseEmulatedFunction());
1904 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
1905 break;
1906 case EOpMul:
1907 outputTriplet(out, visit, "(", " * ", ")");
1908 break;
1909 default:
1910 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911 }
1912
1913 return true;
1914}
1915
Olli Etuaho57961272016-09-14 13:57:46 +03001916void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001918 out << "if (";
1919
1920 node->getCondition()->traverse(this);
1921
1922 out << ")\n";
1923
Jamie Madill8c46ab12015-12-07 16:39:19 -05001924 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001925
1926 bool discard = false;
1927
1928 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001930 // The trueBlock child node will output braces.
1931 ASSERT(IsSequence(node->getTrueBlock()));
1932
Olli Etuahoa6f22092015-05-08 18:31:10 +03001933 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001934
Olli Etuahoa6f22092015-05-08 18:31:10 +03001935 // Detect true discard
1936 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1937 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001938 else
1939 {
1940 // TODO(oetuaho): Check if the semicolon inside is necessary.
1941 // It's there as a result of conservative refactoring of the output.
1942 out << "{;}\n";
1943 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001944
Jamie Madill8c46ab12015-12-07 16:39:19 -05001945 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001946
Olli Etuahoa6f22092015-05-08 18:31:10 +03001947 if (node->getFalseBlock())
1948 {
1949 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001950
Jamie Madill8c46ab12015-12-07 16:39:19 -05001951 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952
Olli Etuaho32db19b2016-10-04 14:43:16 +01001953 // The falseBlock child node will output braces.
1954 ASSERT(IsSequence(node->getFalseBlock()));
Olli Etuaho4785fec2015-05-18 16:09:37 +03001955
Olli Etuahoa6f22092015-05-08 18:31:10 +03001956 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001957
Jamie Madill8c46ab12015-12-07 16:39:19 -05001958 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001959
Olli Etuahoa6f22092015-05-08 18:31:10 +03001960 // Detect false discard
1961 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1962 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001963
Olli Etuahoa6f22092015-05-08 18:31:10 +03001964 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001965 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001966 {
1967 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001968 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001969}
1970
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001971bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1972{
1973 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1974 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1975 UNREACHABLE();
1976 return false;
1977}
1978
Olli Etuaho57961272016-09-14 13:57:46 +03001979bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001980{
1981 TInfoSinkBase &out = getInfoSink();
1982
Olli Etuaho3d932d82016-04-12 11:10:30 +03001983 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001984
1985 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001986 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001987 {
1988 out << "FLATTEN ";
1989 }
1990
Olli Etuaho57961272016-09-14 13:57:46 +03001991 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992
1993 return false;
1994}
1995
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001996bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02001997{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001998 TInfoSinkBase &out = getInfoSink();
1999
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002000 if (node->getStatementList())
2001 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002002 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002003 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002004 // The curly braces get written when visiting the statementList aggregate
2005 }
2006 else
2007 {
2008 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002009 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002010 }
2011 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002012}
2013
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002014bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002015{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002016 TInfoSinkBase &out = getInfoSink();
2017
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002018 if (node->hasCondition())
2019 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002020 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002021 return true;
2022 }
2023 else
2024 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002025 out << "default:\n";
2026 return false;
2027 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002028}
2029
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2031{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002032 TInfoSinkBase &out = getInfoSink();
2033 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034}
2035
2036bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2037{
Nicolas Capens655fe362014-04-11 13:12:34 -04002038 mNestedLoopDepth++;
2039
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002040 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002041 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002042 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002043
Jamie Madill8c46ab12015-12-07 16:39:19 -05002044 TInfoSinkBase &out = getInfoSink();
2045
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002046 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002047 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002048 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002049 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002050 mInsideDiscontinuousLoop = wasDiscontinuous;
2051 mNestedLoopDepth--;
2052
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002053 return false;
2054 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002055 }
2056
Corentin Wallez1239ee92015-03-19 14:38:02 -07002057 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002058 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002059 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002060 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002061
Jamie Madill8c46ab12015-12-07 16:39:19 -05002062 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063 }
2064 else
2065 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002066 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002067
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068 if (node->getInit())
2069 {
2070 node->getInit()->traverse(this);
2071 }
2072
2073 out << "; ";
2074
alokp@chromium.org52813552010-11-16 18:36:09 +00002075 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002077 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078 }
2079
2080 out << "; ";
2081
alokp@chromium.org52813552010-11-16 18:36:09 +00002082 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002084 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 }
2086
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002087 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002088
Jamie Madill8c46ab12015-12-07 16:39:19 -05002089 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 }
2091
2092 if (node->getBody())
2093 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002094 // The loop body node will output braces.
2095 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002096 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002098 else
2099 {
2100 // TODO(oetuaho): Check if the semicolon inside is necessary.
2101 // It's there as a result of conservative refactoring of the output.
2102 out << "{;}\n";
2103 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104
Jamie Madill8c46ab12015-12-07 16:39:19 -05002105 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106
alokp@chromium.org52813552010-11-16 18:36:09 +00002107 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002109 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 out << "while(\n";
2111
alokp@chromium.org52813552010-11-16 18:36:09 +00002112 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113
daniel@transgaming.com73536982012-03-21 20:45:49 +00002114 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 }
2116
daniel@transgaming.com73536982012-03-21 20:45:49 +00002117 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002118
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002119 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002120 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002121
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122 return false;
2123}
2124
2125bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2126{
Jamie Madill32aab012015-01-27 14:12:26 -05002127 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128
2129 switch (node->getFlowOp())
2130 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002131 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002132 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002133 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002134 case EOpBreak:
2135 if (visit == PreVisit)
2136 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002137 if (mNestedLoopDepth > 1)
2138 {
2139 mUsesNestedBreak = true;
2140 }
2141
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002142 if (mExcessiveLoopIndex)
2143 {
2144 out << "{Break";
2145 mExcessiveLoopIndex->traverse(this);
2146 out << " = true; break;}\n";
2147 }
2148 else
2149 {
2150 out << "break;\n";
2151 }
2152 }
2153 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002154 case EOpContinue:
2155 outputTriplet(out, visit, "continue;\n", "", "");
2156 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 case EOpReturn:
2158 if (visit == PreVisit)
2159 {
2160 if (node->getExpression())
2161 {
2162 out << "return ";
2163 }
2164 else
2165 {
2166 out << "return;\n";
2167 }
2168 }
2169 else if (visit == PostVisit)
2170 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002171 if (node->getExpression())
2172 {
2173 out << ";\n";
2174 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 }
2176 break;
2177 default: UNREACHABLE();
2178 }
2179
2180 return true;
2181}
2182
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002183bool OutputHLSL::isSingleStatement(TIntermNode *node)
2184{
2185 TIntermAggregate *aggregate = node->getAsAggregate();
2186
2187 if (aggregate)
2188 {
2189 if (aggregate->getOp() == EOpSequence)
2190 {
2191 return false;
2192 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002193 else if (aggregate->getOp() == EOpDeclaration)
2194 {
2195 // Declaring multiple comma-separated variables must be considered multiple statements
2196 // because each individual declaration has side effects which are visible in the next.
2197 return false;
2198 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002199 else
2200 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002201 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002202 {
2203 if (!isSingleStatement(*sit))
2204 {
2205 return false;
2206 }
2207 }
2208
2209 return true;
2210 }
2211 }
2212
2213 return true;
2214}
2215
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002216// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2217// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002218bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002219{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002220 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002221
2222 // Parse loops of the form:
2223 // for(int index = initial; index [comparator] limit; index += increment)
2224 TIntermSymbol *index = NULL;
2225 TOperator comparator = EOpNull;
2226 int initial = 0;
2227 int limit = 0;
2228 int increment = 0;
2229
2230 // Parse index name and intial value
2231 if (node->getInit())
2232 {
2233 TIntermAggregate *init = node->getInit()->getAsAggregate();
2234
2235 if (init)
2236 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002237 TIntermSequence *sequence = init->getSequence();
2238 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002239
2240 if (variable && variable->getQualifier() == EvqTemporary)
2241 {
2242 TIntermBinary *assign = variable->getAsBinaryNode();
2243
2244 if (assign->getOp() == EOpInitialize)
2245 {
2246 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2247 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2248
2249 if (symbol && constant)
2250 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002251 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002252 {
2253 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002254 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002255 }
2256 }
2257 }
2258 }
2259 }
2260 }
2261
2262 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002263 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002264 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002265 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002266
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002267 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2268 {
2269 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2270
2271 if (constant)
2272 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002273 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002274 {
2275 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002276 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002277 }
2278 }
2279 }
2280 }
2281
2282 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002283 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002284 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002285 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2286 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002287
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002288 if (binaryTerminal)
2289 {
2290 TOperator op = binaryTerminal->getOp();
2291 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2292
2293 if (constant)
2294 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002295 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002296 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002297 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002298
2299 switch (op)
2300 {
2301 case EOpAddAssign: increment = value; break;
2302 case EOpSubAssign: increment = -value; break;
2303 default: UNIMPLEMENTED();
2304 }
2305 }
2306 }
2307 }
2308 else if (unaryTerminal)
2309 {
2310 TOperator op = unaryTerminal->getOp();
2311
2312 switch (op)
2313 {
2314 case EOpPostIncrement: increment = 1; break;
2315 case EOpPostDecrement: increment = -1; break;
2316 case EOpPreIncrement: increment = 1; break;
2317 case EOpPreDecrement: increment = -1; break;
2318 default: UNIMPLEMENTED();
2319 }
2320 }
2321 }
2322
2323 if (index != NULL && comparator != EOpNull && increment != 0)
2324 {
2325 if (comparator == EOpLessThanEqual)
2326 {
2327 comparator = EOpLessThan;
2328 limit += 1;
2329 }
2330
2331 if (comparator == EOpLessThan)
2332 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002333 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002334
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002335 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002336 {
2337 return false; // Not an excessive loop
2338 }
2339
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002340 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2341 mExcessiveLoopIndex = index;
2342
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002343 out << "{int ";
2344 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002345 out << ";\n"
2346 "bool Break";
2347 index->traverse(this);
2348 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002349
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002350 bool firstLoopFragment = true;
2351
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002352 while (iterations > 0)
2353 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002354 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002355
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002356 if (!firstLoopFragment)
2357 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002358 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002359 index->traverse(this);
2360 out << ") {\n";
2361 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002362
2363 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2364 {
2365 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2366 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002367
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002368 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002369 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002370
Corentin Wallez1239ee92015-03-19 14:38:02 -07002371 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002372 index->traverse(this);
2373 out << " = ";
2374 out << initial;
2375
2376 out << "; ";
2377 index->traverse(this);
2378 out << " < ";
2379 out << clampedLimit;
2380
2381 out << "; ";
2382 index->traverse(this);
2383 out << " += ";
2384 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002385 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002386
Jamie Madill8c46ab12015-12-07 16:39:19 -05002387 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002388 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002389
2390 if (node->getBody())
2391 {
2392 node->getBody()->traverse(this);
2393 }
2394
Jamie Madill8c46ab12015-12-07 16:39:19 -05002395 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002396 out << ";}\n";
2397
2398 if (!firstLoopFragment)
2399 {
2400 out << "}\n";
2401 }
2402
2403 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002404
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002405 initial += MAX_LOOP_ITERATIONS * increment;
2406 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002407 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002408
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002409 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002410
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002411 mExcessiveLoopIndex = restoreIndex;
2412
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002413 return true;
2414 }
2415 else UNIMPLEMENTED();
2416 }
2417
2418 return false; // Not handled as an excessive loop
2419}
2420
Jamie Madill8c46ab12015-12-07 16:39:19 -05002421void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2422 Visit visit,
2423 const char *preString,
2424 const char *inString,
2425 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002427 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002428 {
2429 out << preString;
2430 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002431 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002432 {
2433 out << inString;
2434 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002435 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436 {
2437 out << postString;
2438 }
2439}
2440
Jamie Madill8c46ab12015-12-07 16:39:19 -05002441void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002442{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002443 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002444 {
Jamie Madill32aab012015-01-27 14:12:26 -05002445 out << "\n";
2446 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002447
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002448 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002449 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002450 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002451 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002452
Jamie Madill32aab012015-01-27 14:12:26 -05002453 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002454 }
2455}
2456
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002457TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2458{
2459 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002460 const TType &type = symbol->getType();
2461 const TName &name = symbol->getName();
2462 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002463
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002464 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002465 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002466 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002467 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002468 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002469 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002470 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002471 }
2472
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002473 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002474 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002475 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2476 {
2477 // Samplers are passed as indices to the sampler array.
2478 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2479 return "const uint " + nameStr + ArrayString(type);
2480 }
2481 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2482 {
2483 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2484 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2485 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2486 ArrayString(type);
2487 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002488 }
2489
Olli Etuaho96963162016-03-21 11:54:33 +02002490 TStringStream argString;
2491 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2492 << ArrayString(type);
2493
2494 // If the structure parameter contains samplers, they need to be passed into the function as
2495 // separate parameters. HLSL doesn't natively support samplers in structs.
2496 if (type.isStructureContainingSamplers())
2497 {
2498 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2499 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002500 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002501 for (const TIntermSymbol *sampler : samplerSymbols)
2502 {
2503 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2504 {
2505 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2506 }
2507 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2508 {
2509 const TType &samplerType = sampler->getType();
2510 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2511 type.getArraySize() == samplerType.getArraySize());
2512 ASSERT(IsSampler(samplerType.getBasicType()));
2513 argString << ", " << QualifierString(qualifier) << " "
2514 << TextureString(samplerType.getBasicType()) << " texture_"
2515 << sampler->getSymbol() << ArrayString(type) << ", "
2516 << QualifierString(qualifier) << " "
2517 << SamplerString(samplerType.getBasicType()) << " sampler_"
2518 << sampler->getSymbol() << ArrayString(type);
2519 }
2520 else
2521 {
2522 const TType &samplerType = sampler->getType();
2523 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2524 type.getArraySize() == samplerType.getArraySize());
2525 ASSERT(IsSampler(samplerType.getBasicType()));
2526 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2527 << " " << sampler->getSymbol() << ArrayString(type);
2528 }
2529 }
2530 }
2531
2532 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002533}
2534
2535TString OutputHLSL::initializer(const TType &type)
2536{
2537 TString string;
2538
Jamie Madill94bf7f22013-07-08 13:31:15 -04002539 size_t size = type.getObjectSize();
2540 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002542 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002543
Jamie Madill94bf7f22013-07-08 13:31:15 -04002544 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545 {
2546 string += ", ";
2547 }
2548 }
2549
daniel@transgaming.comead23042010-04-29 03:35:36 +00002550 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002551}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002552
Jamie Madill8c46ab12015-12-07 16:39:19 -05002553void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2554 Visit visit,
2555 const TType &type,
2556 const char *name,
2557 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002558{
Olli Etuahof40319e2015-03-10 14:33:00 +02002559 if (type.isArray())
2560 {
2561 UNIMPLEMENTED();
2562 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002563
2564 if (visit == PreVisit)
2565 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002566 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002567
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002568 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002569 }
2570 else if (visit == InVisit)
2571 {
2572 out << ", ";
2573 }
2574 else if (visit == PostVisit)
2575 {
2576 out << ")";
2577 }
2578}
2579
Jamie Madill8c46ab12015-12-07 16:39:19 -05002580const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2581 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002582 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002583{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002584 const TConstantUnion *constUnionIterated = constUnion;
2585
Jamie Madill98493dd2013-07-08 14:39:03 -04002586 const TStructure* structure = type.getStruct();
2587 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002588 {
Jamie Madill033dae62014-06-18 12:56:28 -04002589 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002590
Jamie Madill98493dd2013-07-08 14:39:03 -04002591 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002592
Jamie Madill98493dd2013-07-08 14:39:03 -04002593 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002594 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002595 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002596 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002597
Jamie Madill98493dd2013-07-08 14:39:03 -04002598 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002599 {
2600 out << ", ";
2601 }
2602 }
2603
2604 out << ")";
2605 }
2606 else
2607 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002608 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002609 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002610
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002611 if (writeType)
2612 {
Jamie Madill033dae62014-06-18 12:56:28 -04002613 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002614 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002615 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002616 if (writeType)
2617 {
2618 out << ")";
2619 }
2620 }
2621
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002622 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002623}
2624
Jamie Madill8c46ab12015-12-07 16:39:19 -05002625void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002626{
2627 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002628 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002629}
2630
Jamie Madill37997142015-01-28 10:06:34 -05002631bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2632{
2633 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2634 expression->traverse(&searchSymbol);
2635
2636 if (searchSymbol.foundMatch())
2637 {
2638 // Type already printed
2639 out << "t" + str(mUniqueIndex) + " = ";
2640 expression->traverse(this);
2641 out << ", ";
2642 symbolNode->traverse(this);
2643 out << " = t" + str(mUniqueIndex);
2644
2645 mUniqueIndex++;
2646 return true;
2647 }
2648
2649 return false;
2650}
2651
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002652bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2653{
2654 // We support writing constant unions and constructors that only take constant unions as
2655 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002656 return expression->getAsConstantUnion() ||
2657 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002658}
2659
2660bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2661 TIntermSymbol *symbolNode,
2662 TIntermTyped *expression)
2663{
2664 if (canWriteAsHLSLLiteral(expression))
2665 {
2666 symbolNode->traverse(this);
2667 if (expression->getType().isArray())
2668 {
2669 out << "[" << expression->getType().getArraySize() << "]";
2670 }
2671 out << " = {";
2672 if (expression->getAsConstantUnion())
2673 {
2674 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2675 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2676 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2677 }
2678 else
2679 {
2680 TIntermAggregate *constructor = expression->getAsAggregate();
2681 ASSERT(constructor != nullptr);
2682 for (TIntermNode *&node : *constructor->getSequence())
2683 {
2684 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2685 ASSERT(nodeConst);
2686 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2687 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2688 if (node != constructor->getSequence()->back())
2689 {
2690 out << ", ";
2691 }
2692 }
2693 }
2694 out << "}";
2695 return true;
2696 }
2697 return false;
2698}
2699
Jamie Madill55e79e02015-02-09 15:35:00 -05002700TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2701{
2702 const TFieldList &fields = structure.fields();
2703
2704 for (const auto &eqFunction : mStructEqualityFunctions)
2705 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002706 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002707 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002708 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002709 }
2710 }
2711
2712 const TString &structNameString = StructNameString(structure);
2713
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002714 StructEqualityFunction *function = new StructEqualityFunction();
2715 function->structure = &structure;
2716 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002717
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002718 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002719
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002720 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2721 << "{\n"
2722 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002723
2724 for (size_t i = 0; i < fields.size(); i++)
2725 {
2726 const TField *field = fields[i];
2727 const TType *fieldType = field->type();
2728
2729 const TString &fieldNameA = "a." + Decorate(field->name());
2730 const TString &fieldNameB = "b." + Decorate(field->name());
2731
2732 if (i > 0)
2733 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002734 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002735 }
2736
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002737 fnOut << "(";
2738 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2739 fnOut << fieldNameA;
2740 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2741 fnOut << fieldNameB;
2742 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2743 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002744 }
2745
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002746 fnOut << ";\n" << "}\n";
2747
2748 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002749
2750 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002751 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002752
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002753 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002754}
2755
Olli Etuaho7fb49552015-03-18 17:27:44 +02002756TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2757{
2758 for (const auto &eqFunction : mArrayEqualityFunctions)
2759 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002760 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002761 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002762 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002763 }
2764 }
2765
2766 const TString &typeName = TypeString(type);
2767
Olli Etuaho12690762015-03-31 12:55:28 +03002768 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002769 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002770
2771 TInfoSinkBase fnNameOut;
2772 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002773 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002774
2775 TType nonArrayType = type;
2776 nonArrayType.clearArrayness();
2777
2778 TInfoSinkBase fnOut;
2779
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002780 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002781 << typeName << " a[" << type.getArraySize() << "], "
2782 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002783 << "{\n"
2784 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2785 " {\n"
2786 " if (";
2787
2788 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2789 fnOut << "a[i]";
2790 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2791 fnOut << "b[i]";
2792 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2793
2794 fnOut << ") { return false; }\n"
2795 " }\n"
2796 " return true;\n"
2797 "}\n";
2798
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002799 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002800
2801 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002802 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002803
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002804 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002805}
2806
Olli Etuaho12690762015-03-31 12:55:28 +03002807TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2808{
2809 for (const auto &assignFunction : mArrayAssignmentFunctions)
2810 {
2811 if (assignFunction.type == type)
2812 {
2813 return assignFunction.functionName;
2814 }
2815 }
2816
2817 const TString &typeName = TypeString(type);
2818
2819 ArrayHelperFunction function;
2820 function.type = type;
2821
2822 TInfoSinkBase fnNameOut;
2823 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2824 function.functionName = fnNameOut.c_str();
2825
2826 TInfoSinkBase fnOut;
2827
2828 fnOut << "void " << function.functionName << "(out "
2829 << typeName << " a[" << type.getArraySize() << "], "
2830 << typeName << " b[" << type.getArraySize() << "])\n"
2831 << "{\n"
2832 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2833 " {\n"
2834 " a[i] = b[i];\n"
2835 " }\n"
2836 "}\n";
2837
2838 function.functionDefinition = fnOut.c_str();
2839
2840 mArrayAssignmentFunctions.push_back(function);
2841
2842 return function.functionName;
2843}
2844
Olli Etuaho9638c352015-04-01 14:34:52 +03002845TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2846{
2847 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2848 {
2849 if (constructIntoFunction.type == type)
2850 {
2851 return constructIntoFunction.functionName;
2852 }
2853 }
2854
2855 const TString &typeName = TypeString(type);
2856
2857 ArrayHelperFunction function;
2858 function.type = type;
2859
2860 TInfoSinkBase fnNameOut;
2861 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2862 function.functionName = fnNameOut.c_str();
2863
2864 TInfoSinkBase fnOut;
2865
2866 fnOut << "void " << function.functionName << "(out "
2867 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002868 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002869 {
2870 fnOut << ", " << typeName << " b" << i;
2871 }
2872 fnOut << ")\n"
2873 "{\n";
2874
Olli Etuaho856c4972016-08-08 11:38:39 +03002875 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002876 {
2877 fnOut << " a[" << i << "] = b" << i << ";\n";
2878 }
2879 fnOut << "}\n";
2880
2881 function.functionDefinition = fnOut.c_str();
2882
2883 mArrayConstructIntoFunctions.push_back(function);
2884
2885 return function.functionName;
2886}
2887
Jamie Madill2e295e22015-04-29 10:41:33 -04002888void OutputHLSL::ensureStructDefined(const TType &type)
2889{
2890 TStructure *structure = type.getStruct();
2891
2892 if (structure)
2893 {
2894 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2895 }
2896}
2897
2898
Olli Etuaho9638c352015-04-01 14:34:52 +03002899
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002900}