blob: 3badefd5a518242117848448b470b7cff8b7265e [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
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020083OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
84 const TExtensionBehavior &extensionBehavior,
85 const char *sourcePath, ShShaderOutput outputType,
86 int numRenderTargets, const std::vector<Uniform> &uniforms,
87 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -040088 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020089 mShaderType(shaderType),
90 mShaderVersion(shaderVersion),
91 mExtensionBehavior(extensionBehavior),
92 mSourcePath(sourcePath),
93 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -070094 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +100095 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -070096 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000097{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000098 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000099
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000100 mUsesFragColor = false;
101 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000102 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000103 mUsesFragCoord = false;
104 mUsesPointCoord = false;
105 mUsesFrontFacing = false;
106 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000107 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500108 mUsesVertexID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400109 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000110 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500111 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400112 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530113 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000114
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000115 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000116
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000117 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000118 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400119 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000120
121 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000122
Jamie Madill8daaba12014-06-13 10:04:33 -0400123 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200124 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300125 mTextureFunctionHLSL = new TextureFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400126
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200127 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000128 {
Arun Patole63419392015-03-13 11:51:07 +0530129 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
130 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
131 // In both cases total 3 uniform registers need to be reserved.
132 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000133 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000134
Geoff Lang00140f42016-02-03 18:47:33 +0000135 // Reserve registers for the default uniform block and driver constants
136 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137}
138
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000139OutputHLSL::~OutputHLSL()
140{
Jamie Madill8daaba12014-06-13 10:04:33 -0400141 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400142 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300143 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200144 for (auto &eqFunction : mStructEqualityFunctions)
145 {
146 SafeDelete(eqFunction);
147 }
148 for (auto &eqFunction : mArrayEqualityFunctions)
149 {
150 SafeDelete(eqFunction);
151 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000152}
153
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200154void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000155{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200156 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400157 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000158
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200159 BuiltInFunctionEmulator builtInFunctionEmulator;
160 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200161 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500162
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700163 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700164 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
165 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300166 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700167 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700168
Jamie Madill37997142015-01-28 10:06:34 -0500169 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500170 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200171 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500172 mInfoSinkStack.pop();
173
Jamie Madill37997142015-01-28 10:06:34 -0500174 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500175 mInfoSinkStack.pop();
176
Jamie Madill32aab012015-01-27 14:12:26 -0500177 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500178 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500179 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000180
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200181 objSink << mHeader.c_str();
182 objSink << mBody.c_str();
183 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200184
185 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000186}
187
Jamie Madill570e04d2013-06-21 09:15:33 -0400188void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
189{
190 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
191 {
192 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
193
Jamie Madill32aab012015-01-27 14:12:26 -0500194 TInfoSinkBase structInfoSink;
195 mInfoSinkStack.push(&structInfoSink);
196
Jamie Madill570e04d2013-06-21 09:15:33 -0400197 // This will mark the necessary block elements as referenced
198 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500199
200 TString structName(structInfoSink.c_str());
201 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400202
203 mFlaggedStructOriginalNames[flaggedNode] = structName;
204
205 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
206 {
207 structName.erase(pos, 1);
208 }
209
210 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
211 }
212}
213
Jamie Madill4e1fd412014-07-10 17:50:10 -0400214const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
215{
216 return mUniformHLSL->getInterfaceBlockRegisterMap();
217}
218
Jamie Madill9fe25e92014-07-18 10:33:08 -0400219const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
220{
221 return mUniformHLSL->getUniformRegisterMap();
222}
223
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000224int OutputHLSL::vectorSize(const TType &type) const
225{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000226 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000227 int arraySize = type.isArray() ? type.getArraySize() : 1;
228
229 return elementSize * arraySize;
230}
231
Jamie Madill98493dd2013-07-08 14:39:03 -0400232TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400233{
234 TString init;
235
236 TString preIndentString;
237 TString fullIndentString;
238
239 for (int spaces = 0; spaces < (indent * 4); spaces++)
240 {
241 preIndentString += ' ';
242 }
243
244 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
245 {
246 fullIndentString += ' ';
247 }
248
249 init += preIndentString + "{\n";
250
Jamie Madill98493dd2013-07-08 14:39:03 -0400251 const TFieldList &fields = structure.fields();
252 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400253 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400254 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400255 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400256 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400257
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400259 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400261 }
262 else
263 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400265 }
266 }
267
268 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
269
270 return init;
271}
272
Jamie Madill8c46ab12015-12-07 16:39:19 -0500273void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000275 TString varyings;
276 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400277 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000278
Jamie Madill829f59e2013-11-13 19:40:54 -0500279 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 {
281 TIntermTyped *structNode = flaggedStructIt->first;
282 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 const TString &originalName = mFlaggedStructOriginalNames[structNode];
285
Jamie Madill033dae62014-06-18 12:56:28 -0400286 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400287 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 flaggedStructs += "\n";
289 }
290
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000291 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
292 {
293 const TType &type = varying->second->getType();
294 const TString &name = varying->second->getSymbol();
295
296 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400297 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
298 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000299 }
300
301 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
302 {
303 const TType &type = attribute->second->getType();
304 const TString &name = attribute->second->getSymbol();
305
Jamie Madill033dae62014-06-18 12:56:28 -0400306 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000307 }
308
Jamie Madill8daaba12014-06-13 10:04:33 -0400309 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400310
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200311 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400312 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
313
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200314 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500315 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200316 out << "\n// Equality functions\n\n";
317 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500318 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200319 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200320 }
321 }
Olli Etuaho12690762015-03-31 12:55:28 +0300322 if (!mArrayAssignmentFunctions.empty())
323 {
324 out << "\n// Assignment functions\n\n";
325 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
326 {
327 out << assignmentFunction.functionDefinition << "\n";
328 }
329 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300330 if (!mArrayConstructIntoFunctions.empty())
331 {
332 out << "\n// Array constructor functions\n\n";
333 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
334 {
335 out << constructIntoFunction.functionDefinition << "\n";
336 }
337 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200338
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500339 if (mUsesDiscardRewriting)
340 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400341 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500342 }
343
Nicolas Capens655fe362014-04-11 13:12:34 -0400344 if (mUsesNestedBreak)
345 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400346 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400347 }
348
Arun Patole44efa0b2015-03-04 17:11:05 +0530349 if (mRequiresIEEEStrictCompiling)
350 {
351 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
352 }
353
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400354 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
355 "#define LOOP [loop]\n"
356 "#define FLATTEN [flatten]\n"
357 "#else\n"
358 "#define LOOP\n"
359 "#define FLATTEN\n"
360 "#endif\n";
361
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200362 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200364 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
365 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000366
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000367 out << "// Varyings\n";
368 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400369 out << "\n";
370
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200371 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000372 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500373 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000374 {
Jamie Madill46131a32013-06-20 11:55:50 -0400375 const TString &variableName = outputVariableIt->first;
376 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400377
Jamie Madill033dae62014-06-18 12:56:28 -0400378 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400379 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000380 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000381 }
Jamie Madill46131a32013-06-20 11:55:50 -0400382 else
383 {
384 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
385
386 out << "static float4 gl_Color[" << numColorValues << "] =\n"
387 "{\n";
388 for (unsigned int i = 0; i < numColorValues; i++)
389 {
390 out << " float4(0, 0, 0, 0)";
391 if (i + 1 != numColorValues)
392 {
393 out << ",";
394 }
395 out << "\n";
396 }
397
398 out << "};\n";
399 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000400
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400401 if (mUsesFragDepth)
402 {
403 out << "static float gl_Depth = 0.0;\n";
404 }
405
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000406 if (mUsesFragCoord)
407 {
408 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
409 }
410
411 if (mUsesPointCoord)
412 {
413 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
414 }
415
416 if (mUsesFrontFacing)
417 {
418 out << "static bool gl_FrontFacing = false;\n";
419 }
420
421 out << "\n";
422
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000423 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000424 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000425 out << "struct gl_DepthRangeParameters\n"
426 "{\n"
427 " float near;\n"
428 " float far;\n"
429 " float diff;\n"
430 "};\n"
431 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000432 }
433
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200434 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000435 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000436 out << "cbuffer DriverConstants : register(b1)\n"
437 "{\n";
438
439 if (mUsesDepthRange)
440 {
441 out << " float3 dx_DepthRange : packoffset(c0);\n";
442 }
443
444 if (mUsesFragCoord)
445 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000446 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000447 }
448
449 if (mUsesFragCoord || mUsesFrontFacing)
450 {
451 out << " float3 dx_DepthFront : packoffset(c2);\n";
452 }
453
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800454 if (mUsesFragCoord)
455 {
456 // dx_ViewScale is only used in the fragment shader to correct
457 // the value for glFragCoord if necessary
458 out << " float2 dx_ViewScale : packoffset(c3);\n";
459 }
460
Olli Etuaho618bebc2016-01-15 16:40:00 +0200461 if (mOutputType == SH_HLSL_4_1_OUTPUT)
462 {
463 mUniformHLSL->samplerMetadataUniforms(out, "c4");
464 }
465
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000466 out << "};\n";
467 }
468 else
469 {
470 if (mUsesDepthRange)
471 {
472 out << "uniform float3 dx_DepthRange : register(c0);";
473 }
474
475 if (mUsesFragCoord)
476 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000477 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000478 }
479
480 if (mUsesFragCoord || mUsesFrontFacing)
481 {
482 out << "uniform float3 dx_DepthFront : register(c2);\n";
483 }
484 }
485
486 out << "\n";
487
488 if (mUsesDepthRange)
489 {
490 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
491 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000492 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000493
Jamie Madillf91ce812014-06-13 10:04:34 -0400494 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000495 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400496 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000497 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400498 out << flaggedStructs;
499 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000500 }
501
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000502 if (usingMRTExtension && mNumRenderTargets > 1)
503 {
504 out << "#define GL_USES_MRT\n";
505 }
506
507 if (mUsesFragColor)
508 {
509 out << "#define GL_USES_FRAG_COLOR\n";
510 }
511
512 if (mUsesFragData)
513 {
514 out << "#define GL_USES_FRAG_DATA\n";
515 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000517 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000519 out << "// Attributes\n";
520 out << attributes;
521 out << "\n"
522 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400523
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000524 if (mUsesPointSize)
525 {
526 out << "static float gl_PointSize = float(1);\n";
527 }
528
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000529 if (mUsesInstanceID)
530 {
531 out << "static int gl_InstanceID;";
532 }
533
Corentin Wallezb076add2016-01-11 16:45:46 -0500534 if (mUsesVertexID)
535 {
536 out << "static int gl_VertexID;";
537 }
538
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000539 out << "\n"
540 "// Varyings\n";
541 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000542 out << "\n";
543
544 if (mUsesDepthRange)
545 {
546 out << "struct gl_DepthRangeParameters\n"
547 "{\n"
548 " float near;\n"
549 " float far;\n"
550 " float diff;\n"
551 "};\n"
552 "\n";
553 }
554
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200555 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000556 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800557 out << "cbuffer DriverConstants : register(b1)\n"
558 "{\n";
559
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000560 if (mUsesDepthRange)
561 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800562 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000563 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800564
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800565 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
566 // shaders. However, we declare it for all shaders (including Feature Level 10+).
567 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
568 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800569 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800570 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800571 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800572
Olli Etuaho618bebc2016-01-15 16:40:00 +0200573 if (mOutputType == SH_HLSL_4_1_OUTPUT)
574 {
575 mUniformHLSL->samplerMetadataUniforms(out, "c4");
576 }
577
Austin Kinross4fd18b12014-12-22 12:32:05 -0800578 out << "};\n"
579 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000580 }
581 else
582 {
583 if (mUsesDepthRange)
584 {
585 out << "uniform float3 dx_DepthRange : register(c0);\n";
586 }
587
Cooper Partine6664f02015-01-09 16:22:24 -0800588 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
589 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000590 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000591 }
592
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000593 if (mUsesDepthRange)
594 {
595 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
596 "\n";
597 }
598
Jamie Madillf91ce812014-06-13 10:04:34 -0400599 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000600 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400601 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000602 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400603 out << flaggedStructs;
604 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000605 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400606 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000607
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300608 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000610 if (mUsesFragCoord)
611 {
612 out << "#define GL_USES_FRAG_COORD\n";
613 }
614
615 if (mUsesPointCoord)
616 {
617 out << "#define GL_USES_POINT_COORD\n";
618 }
619
620 if (mUsesFrontFacing)
621 {
622 out << "#define GL_USES_FRONT_FACING\n";
623 }
624
625 if (mUsesPointSize)
626 {
627 out << "#define GL_USES_POINT_SIZE\n";
628 }
629
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400630 if (mUsesFragDepth)
631 {
632 out << "#define GL_USES_FRAG_DEPTH\n";
633 }
634
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000635 if (mUsesDepthRange)
636 {
637 out << "#define GL_USES_DEPTH_RANGE\n";
638 }
639
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000640 if (mUsesXor)
641 {
642 out << "bool xor(bool p, bool q)\n"
643 "{\n"
644 " return (p || q) && !(p && q);\n"
645 "}\n"
646 "\n";
647 }
648
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200649 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650}
651
652void OutputHLSL::visitSymbol(TIntermSymbol *node)
653{
Jamie Madill32aab012015-01-27 14:12:26 -0500654 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655
Jamie Madill570e04d2013-06-21 09:15:33 -0400656 // Handle accessing std140 structs by value
657 if (mFlaggedStructMappedNames.count(node) > 0)
658 {
659 out << mFlaggedStructMappedNames[node];
660 return;
661 }
662
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663 TString name = node->getSymbol();
664
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000665 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000666 {
667 mUsesDepthRange = true;
668 out << name;
669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670 else
671 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000672 TQualifier qualifier = node->getQualifier();
673
674 if (qualifier == EvqUniform)
675 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400676 const TType &nodeType = node->getType();
677 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400678
679 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000680 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400681 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000682 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000683 else
684 {
685 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000686 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400687
Jamie Madill2e295e22015-04-29 10:41:33 -0400688 ensureStructDefined(nodeType);
689
Olli Etuaho96963162016-03-21 11:54:33 +0200690 const TName &nameWithMetadata = node->getName();
691 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000692 }
Jamie Madill19571812013-08-12 15:26:34 -0700693 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000694 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000695 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400696 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000697 }
Jamie Madill033dae62014-06-18 12:56:28 -0400698 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000699 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000700 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400701 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000702 }
Jamie Madill19571812013-08-12 15:26:34 -0700703 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400704 {
705 mReferencedOutputVariables[name] = node;
706 out << "out_" << name;
707 }
708 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000709 {
710 out << "gl_Color[0]";
711 mUsesFragColor = true;
712 }
713 else if (qualifier == EvqFragData)
714 {
715 out << "gl_Color";
716 mUsesFragData = true;
717 }
718 else if (qualifier == EvqFragCoord)
719 {
720 mUsesFragCoord = true;
721 out << name;
722 }
723 else if (qualifier == EvqPointCoord)
724 {
725 mUsesPointCoord = true;
726 out << name;
727 }
728 else if (qualifier == EvqFrontFacing)
729 {
730 mUsesFrontFacing = true;
731 out << name;
732 }
733 else if (qualifier == EvqPointSize)
734 {
735 mUsesPointSize = true;
736 out << name;
737 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000738 else if (qualifier == EvqInstanceID)
739 {
740 mUsesInstanceID = true;
741 out << name;
742 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500743 else if (qualifier == EvqVertexID)
744 {
745 mUsesVertexID = true;
746 out << name;
747 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300748 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400749 {
750 mUsesFragDepth = true;
751 out << "gl_Depth";
752 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000753 else
754 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300755 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000756 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757 }
758}
759
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400760void OutputHLSL::visitRaw(TIntermRaw *node)
761{
Jamie Madill32aab012015-01-27 14:12:26 -0500762 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400763}
764
Olli Etuaho7fb49552015-03-18 17:27:44 +0200765void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
766{
767 if (type.isScalar() && !type.isArray())
768 {
769 if (op == EOpEqual)
770 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500771 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200772 }
773 else
774 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500775 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200776 }
777 }
778 else
779 {
780 if (visit == PreVisit && op == EOpNotEqual)
781 {
782 out << "!";
783 }
784
785 if (type.isArray())
786 {
787 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500788 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200789 }
790 else if (type.getBasicType() == EbtStruct)
791 {
792 const TStructure &structure = *type.getStruct();
793 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500794 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200795 }
796 else
797 {
798 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500799 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200800 }
801 }
802}
803
Olli Etuaho96963162016-03-21 11:54:33 +0200804bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
805{
806 // Inside InVisit the current node is already in the path.
807 const unsigned int initialN = visit == InVisit ? 1u : 0u;
808 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
809 {
810 TIntermNode *ancestor = getAncestorNode(n);
811 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
812 if (ancestorBinary == nullptr)
813 {
814 return false;
815 }
816 switch (ancestorBinary->getOp())
817 {
818 case EOpIndexDirectStruct:
819 {
820 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
821 const TIntermConstantUnion *index =
822 ancestorBinary->getRight()->getAsConstantUnion();
823 const TField *field = structure->fields()[index->getIConst(0)];
824 if (IsSampler(field->type()->getBasicType()))
825 {
826 return true;
827 }
828 break;
829 }
830 case EOpIndexDirect:
831 break;
832 default:
833 // Returning a sampler from indirect indexing is not supported.
834 return false;
835 }
836 }
837 return false;
838}
839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
841{
Jamie Madill32aab012015-01-27 14:12:26 -0500842 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843
Jamie Madill570e04d2013-06-21 09:15:33 -0400844 // Handle accessing std140 structs by value
845 if (mFlaggedStructMappedNames.count(node) > 0)
846 {
847 out << mFlaggedStructMappedNames[node];
848 return false;
849 }
850
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851 switch (node->getOp())
852 {
Olli Etuahoe79904c2015-03-18 16:56:42 +0200853 case EOpAssign:
854 if (node->getLeft()->isArray())
855 {
Olli Etuaho9638c352015-04-01 14:34:52 +0300856 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
857 if (rightAgg != nullptr && rightAgg->isConstructor())
858 {
859 const TString &functionName = addArrayConstructIntoFunction(node->getType());
860 out << functionName << "(";
861 node->getLeft()->traverse(this);
862 TIntermSequence *seq = rightAgg->getSequence();
863 for (auto &arrayElement : *seq)
864 {
865 out << ", ";
866 arrayElement->traverse(this);
867 }
868 out << ")";
869 return false;
870 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +0300871 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
872 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
873
874 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500875 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200876 }
877 else
878 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500879 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200880 }
881 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000882 case EOpInitialize:
883 if (visit == PreVisit)
884 {
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000885 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -0500886 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000887 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000888
Olli Etuaho3d932d82016-04-12 11:10:30 +0300889 // Global initializers must be constant at this point.
Olli Etuahod4f4c112016-04-15 15:11:24 +0300890 ASSERT(symbolNode->getQualifier() != EvqGlobal || canWriteAsHLSLLiteral(expression));
891
892 // GLSL allows to write things like "float x = x;" where a new variable x is defined
893 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
894 // new variable is created before the assignment is evaluated), so we need to convert
895 // this to "float t = x, x = t;".
Olli Etuaho3d932d82016-04-12 11:10:30 +0300896 if (writeSameSymbolInitializer(out, symbolNode, expression))
Jamie Madill37997142015-01-28 10:06:34 -0500897 {
898 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000899 return false;
900 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200901 else if (writeConstantInitialization(out, symbolNode, expression))
902 {
903 return false;
904 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000905 }
906 else if (visit == InVisit)
907 {
908 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000909 }
910 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500911 case EOpAddAssign:
912 outputTriplet(out, visit, "(", " += ", ")");
913 break;
914 case EOpSubAssign:
915 outputTriplet(out, visit, "(", " -= ", ")");
916 break;
917 case EOpMulAssign:
918 outputTriplet(out, visit, "(", " *= ", ")");
919 break;
920 case EOpVectorTimesScalarAssign:
921 outputTriplet(out, visit, "(", " *= ", ")");
922 break;
923 case EOpMatrixTimesScalarAssign:
924 outputTriplet(out, visit, "(", " *= ", ")");
925 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000926 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000927 if (visit == PreVisit)
928 {
929 out << "(";
930 }
931 else if (visit == InVisit)
932 {
933 out << " = mul(";
934 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -0400935 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000936 }
937 else
938 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000939 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000940 }
941 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000942 case EOpMatrixTimesMatrixAssign:
943 if (visit == PreVisit)
944 {
945 out << "(";
946 }
947 else if (visit == InVisit)
948 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200949 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000950 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +0200951 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000952 }
953 else
954 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200955 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000956 }
957 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500958 case EOpDivAssign:
959 outputTriplet(out, visit, "(", " /= ", ")");
960 break;
961 case EOpIModAssign:
962 outputTriplet(out, visit, "(", " %= ", ")");
963 break;
964 case EOpBitShiftLeftAssign:
965 outputTriplet(out, visit, "(", " <<= ", ")");
966 break;
967 case EOpBitShiftRightAssign:
968 outputTriplet(out, visit, "(", " >>= ", ")");
969 break;
970 case EOpBitwiseAndAssign:
971 outputTriplet(out, visit, "(", " &= ", ")");
972 break;
973 case EOpBitwiseXorAssign:
974 outputTriplet(out, visit, "(", " ^= ", ")");
975 break;
976 case EOpBitwiseOrAssign:
977 outputTriplet(out, visit, "(", " |= ", ")");
978 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -0400979 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -0400980 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400981 const TType& leftType = node->getLeft()->getType();
982 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -0400983 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400984 if (visit == PreVisit)
985 {
986 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
987 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -0400988 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -0400989 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -0400990 return false;
991 }
Jamie Madillb4e664b2013-06-20 11:55:54 -0400992 }
Olli Etuaho96963162016-03-21 11:54:33 +0200993 else if (ancestorEvaluatesToSamplerInStruct(visit))
994 {
995 // All parts of an expression that access a sampler in a struct need to use _ as
996 // separator to access the sampler variable that has been moved out of the struct.
997 outputTriplet(out, visit, "", "_", "");
998 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400999 else
1000 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001001 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001002 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001003 }
1004 break;
1005 case EOpIndexIndirect:
1006 // We do not currently support indirect references to interface blocks
1007 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001008 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001009 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001010 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001011 {
1012 const TStructure* structure = node->getLeft()->getType().getStruct();
1013 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1014 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001015
Olli Etuaho96963162016-03-21 11:54:33 +02001016 // In cases where indexing returns a sampler, we need to access the sampler variable
1017 // that has been moved out of the struct.
1018 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1019 if (visit == PreVisit && indexingReturnsSampler)
1020 {
1021 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1022 // This prefix is only output at the beginning of the indexing expression, which
1023 // may have multiple parts.
1024 out << "angle";
1025 }
1026 if (!indexingReturnsSampler)
1027 {
1028 // All parts of an expression that access a sampler in a struct need to use _ as
1029 // separator to access the sampler variable that has been moved out of the struct.
1030 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1031 }
1032 if (visit == InVisit)
1033 {
1034 if (indexingReturnsSampler)
1035 {
1036 out << "_" + field->name();
1037 }
1038 else
1039 {
1040 out << "." + DecorateField(field->name(), *structure);
1041 }
1042
1043 return false;
1044 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001045 }
1046 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001047 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001048 if (visit == InVisit)
1049 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001050 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1051 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1052 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001053 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001054
1055 return false;
1056 }
1057 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058 case EOpVectorSwizzle:
1059 if (visit == InVisit)
1060 {
1061 out << ".";
1062
1063 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1064
1065 if (swizzle)
1066 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001067 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001069 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070 {
1071 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1072
1073 if (element)
1074 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001075 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076
1077 switch (i)
1078 {
1079 case 0: out << "x"; break;
1080 case 1: out << "y"; break;
1081 case 2: out << "z"; break;
1082 case 3: out << "w"; break;
1083 default: UNREACHABLE();
1084 }
1085 }
1086 else UNREACHABLE();
1087 }
1088 }
1089 else UNREACHABLE();
1090
1091 return false; // Fully processed
1092 }
1093 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001094 case EOpAdd:
1095 outputTriplet(out, visit, "(", " + ", ")");
1096 break;
1097 case EOpSub:
1098 outputTriplet(out, visit, "(", " - ", ")");
1099 break;
1100 case EOpMul:
1101 outputTriplet(out, visit, "(", " * ", ")");
1102 break;
1103 case EOpDiv:
1104 outputTriplet(out, visit, "(", " / ", ")");
1105 break;
1106 case EOpIMod:
1107 outputTriplet(out, visit, "(", " % ", ")");
1108 break;
1109 case EOpBitShiftLeft:
1110 outputTriplet(out, visit, "(", " << ", ")");
1111 break;
1112 case EOpBitShiftRight:
1113 outputTriplet(out, visit, "(", " >> ", ")");
1114 break;
1115 case EOpBitwiseAnd:
1116 outputTriplet(out, visit, "(", " & ", ")");
1117 break;
1118 case EOpBitwiseXor:
1119 outputTriplet(out, visit, "(", " ^ ", ")");
1120 break;
1121 case EOpBitwiseOr:
1122 outputTriplet(out, visit, "(", " | ", ")");
1123 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001124 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001125 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001126 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001127 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001128 case EOpLessThan:
1129 outputTriplet(out, visit, "(", " < ", ")");
1130 break;
1131 case EOpGreaterThan:
1132 outputTriplet(out, visit, "(", " > ", ")");
1133 break;
1134 case EOpLessThanEqual:
1135 outputTriplet(out, visit, "(", " <= ", ")");
1136 break;
1137 case EOpGreaterThanEqual:
1138 outputTriplet(out, visit, "(", " >= ", ")");
1139 break;
1140 case EOpVectorTimesScalar:
1141 outputTriplet(out, visit, "(", " * ", ")");
1142 break;
1143 case EOpMatrixTimesScalar:
1144 outputTriplet(out, visit, "(", " * ", ")");
1145 break;
1146 case EOpVectorTimesMatrix:
1147 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1148 break;
1149 case EOpMatrixTimesVector:
1150 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1151 break;
1152 case EOpMatrixTimesMatrix:
1153 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1154 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001155 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001156 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1157 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001158 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001159 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001160 case EOpLogicalXor:
1161 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001162 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001163 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001164 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001165 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1166 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001167 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001168 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001169 default: UNREACHABLE();
1170 }
1171
1172 return true;
1173}
1174
1175bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1176{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001177 TInfoSinkBase &out = getInfoSink();
1178
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001179 switch (node->getOp())
1180 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001181 case EOpNegative:
1182 outputTriplet(out, visit, "(-", "", ")");
1183 break;
1184 case EOpPositive:
1185 outputTriplet(out, visit, "(+", "", ")");
1186 break;
1187 case EOpVectorLogicalNot:
1188 outputTriplet(out, visit, "(!", "", ")");
1189 break;
1190 case EOpLogicalNot:
1191 outputTriplet(out, visit, "(!", "", ")");
1192 break;
1193 case EOpBitwiseNot:
1194 outputTriplet(out, visit, "(~", "", ")");
1195 break;
1196 case EOpPostIncrement:
1197 outputTriplet(out, visit, "(", "", "++)");
1198 break;
1199 case EOpPostDecrement:
1200 outputTriplet(out, visit, "(", "", "--)");
1201 break;
1202 case EOpPreIncrement:
1203 outputTriplet(out, visit, "(++", "", ")");
1204 break;
1205 case EOpPreDecrement:
1206 outputTriplet(out, visit, "(--", "", ")");
1207 break;
1208 case EOpRadians:
1209 outputTriplet(out, visit, "radians(", "", ")");
1210 break;
1211 case EOpDegrees:
1212 outputTriplet(out, visit, "degrees(", "", ")");
1213 break;
1214 case EOpSin:
1215 outputTriplet(out, visit, "sin(", "", ")");
1216 break;
1217 case EOpCos:
1218 outputTriplet(out, visit, "cos(", "", ")");
1219 break;
1220 case EOpTan:
1221 outputTriplet(out, visit, "tan(", "", ")");
1222 break;
1223 case EOpAsin:
1224 outputTriplet(out, visit, "asin(", "", ")");
1225 break;
1226 case EOpAcos:
1227 outputTriplet(out, visit, "acos(", "", ")");
1228 break;
1229 case EOpAtan:
1230 outputTriplet(out, visit, "atan(", "", ")");
1231 break;
1232 case EOpSinh:
1233 outputTriplet(out, visit, "sinh(", "", ")");
1234 break;
1235 case EOpCosh:
1236 outputTriplet(out, visit, "cosh(", "", ")");
1237 break;
1238 case EOpTanh:
1239 outputTriplet(out, visit, "tanh(", "", ")");
1240 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001241 case EOpAsinh:
1242 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001243 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001244 break;
1245 case EOpAcosh:
1246 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001247 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001248 break;
1249 case EOpAtanh:
1250 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001251 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001252 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001253 case EOpExp:
1254 outputTriplet(out, visit, "exp(", "", ")");
1255 break;
1256 case EOpLog:
1257 outputTriplet(out, visit, "log(", "", ")");
1258 break;
1259 case EOpExp2:
1260 outputTriplet(out, visit, "exp2(", "", ")");
1261 break;
1262 case EOpLog2:
1263 outputTriplet(out, visit, "log2(", "", ")");
1264 break;
1265 case EOpSqrt:
1266 outputTriplet(out, visit, "sqrt(", "", ")");
1267 break;
1268 case EOpInverseSqrt:
1269 outputTriplet(out, visit, "rsqrt(", "", ")");
1270 break;
1271 case EOpAbs:
1272 outputTriplet(out, visit, "abs(", "", ")");
1273 break;
1274 case EOpSign:
1275 outputTriplet(out, visit, "sign(", "", ")");
1276 break;
1277 case EOpFloor:
1278 outputTriplet(out, visit, "floor(", "", ")");
1279 break;
1280 case EOpTrunc:
1281 outputTriplet(out, visit, "trunc(", "", ")");
1282 break;
1283 case EOpRound:
1284 outputTriplet(out, visit, "round(", "", ")");
1285 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001286 case EOpRoundEven:
1287 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001288 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001289 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001290 case EOpCeil:
1291 outputTriplet(out, visit, "ceil(", "", ")");
1292 break;
1293 case EOpFract:
1294 outputTriplet(out, visit, "frac(", "", ")");
1295 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301296 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001297 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05301298 mRequiresIEEEStrictCompiling = true;
1299 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001300 case EOpIsInf:
1301 outputTriplet(out, visit, "isinf(", "", ")");
1302 break;
1303 case EOpFloatBitsToInt:
1304 outputTriplet(out, visit, "asint(", "", ")");
1305 break;
1306 case EOpFloatBitsToUint:
1307 outputTriplet(out, visit, "asuint(", "", ")");
1308 break;
1309 case EOpIntBitsToFloat:
1310 outputTriplet(out, visit, "asfloat(", "", ")");
1311 break;
1312 case EOpUintBitsToFloat:
1313 outputTriplet(out, visit, "asfloat(", "", ")");
1314 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001315 case EOpPackSnorm2x16:
1316 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001317 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001318 break;
1319 case EOpPackUnorm2x16:
1320 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001321 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001322 break;
1323 case EOpPackHalf2x16:
1324 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001325 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001326 break;
1327 case EOpUnpackSnorm2x16:
1328 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001329 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001330 break;
1331 case EOpUnpackUnorm2x16:
1332 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001333 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001334 break;
1335 case EOpUnpackHalf2x16:
1336 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001337 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001338 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001339 case EOpLength:
1340 outputTriplet(out, visit, "length(", "", ")");
1341 break;
1342 case EOpNormalize:
1343 outputTriplet(out, visit, "normalize(", "", ")");
1344 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001345 case EOpDFdx:
1346 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1347 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001348 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001349 }
1350 else
1351 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001352 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001353 }
1354 break;
1355 case EOpDFdy:
1356 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1357 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001358 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001359 }
1360 else
1361 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001362 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001363 }
1364 break;
1365 case EOpFwidth:
1366 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1367 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001368 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001369 }
1370 else
1371 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001372 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001373 }
1374 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001375 case EOpTranspose:
1376 outputTriplet(out, visit, "transpose(", "", ")");
1377 break;
1378 case EOpDeterminant:
1379 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1380 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001381 case EOpInverse:
1382 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001383 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001384 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001385
Jamie Madill8c46ab12015-12-07 16:39:19 -05001386 case EOpAny:
1387 outputTriplet(out, visit, "any(", "", ")");
1388 break;
1389 case EOpAll:
1390 outputTriplet(out, visit, "all(", "", ")");
1391 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001392 default: UNREACHABLE();
1393 }
1394
1395 return true;
1396}
1397
Olli Etuaho96963162016-03-21 11:54:33 +02001398TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1399{
1400 if (node->getAsSymbolNode())
1401 {
1402 return node->getAsSymbolNode()->getSymbol();
1403 }
1404 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1405 switch (nodeBinary->getOp())
1406 {
1407 case EOpIndexDirect:
1408 {
1409 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1410
1411 TInfoSinkBase prefixSink;
1412 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1413 return TString(prefixSink.c_str());
1414 }
1415 case EOpIndexDirectStruct:
1416 {
1417 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1418 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1419 const TField *field = s->fields()[index];
1420
1421 TInfoSinkBase prefixSink;
1422 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1423 << field->name();
1424 return TString(prefixSink.c_str());
1425 }
1426 default:
1427 UNREACHABLE();
1428 return TString("");
1429 }
1430}
1431
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001432bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1433{
Jamie Madill32aab012015-01-27 14:12:26 -05001434 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001435
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001436 switch (node->getOp())
1437 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001438 case EOpSequence:
1439 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001440 if (mInsideFunction)
1441 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001442 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001443 out << "{\n";
1444 }
1445
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001446 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001447 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001448 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001449
Olli Etuahoa6f22092015-05-08 18:31:10 +03001450 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001451
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001452 // Don't output ; after case labels, they're terminated by :
1453 // This is needed especially since outputting a ; after a case statement would turn empty
1454 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03001455 // Also no need to output ; after selection (if) statements or sequences. This is done just
1456 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001457 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1458 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03001459 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001460 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001461 }
1462
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001463 if (mInsideFunction)
1464 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001465 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001466 out << "}\n";
1467 }
1468
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001469 return false;
1470 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001471 case EOpDeclaration:
1472 if (visit == PreVisit)
1473 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001474 TIntermSequence *sequence = node->getSequence();
1475 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001476 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001477
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001478 if (variable &&
1479 (variable->getQualifier() == EvqTemporary ||
1480 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001481 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001482 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001483
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001484 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001485 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001486 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001487 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001488 out << "static ";
1489 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001490
Olli Etuahoa6f22092015-05-08 18:31:10 +03001491 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001492
Olli Etuahoa6f22092015-05-08 18:31:10 +03001493 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001494
Olli Etuahoa6f22092015-05-08 18:31:10 +03001495 if (symbol)
1496 {
1497 symbol->traverse(this);
1498 out << ArrayString(symbol->getType());
1499 out << " = " + initializer(symbol->getType());
1500 }
1501 else
1502 {
1503 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001504 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001505 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001506 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1507 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001508 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001509 }
1510 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001511 }
Jamie Madill033dae62014-06-18 12:56:28 -04001512 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001513 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001514 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001515 {
1516 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1517
1518 if (symbol)
1519 {
1520 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1521 mReferencedVaryings[symbol->getSymbol()] = symbol;
1522 }
1523 else
1524 {
1525 (*sit)->traverse(this);
1526 }
1527 }
1528 }
1529
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530 return false;
1531 }
1532 else if (visit == InVisit)
1533 {
1534 out << ", ";
1535 }
1536 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001537 case EOpInvariantDeclaration:
1538 // Do not do any translation
1539 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001540 case EOpPrototype:
1541 if (visit == PreVisit)
1542 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001543 size_t index = mCallDag.findIndex(node);
1544 // Skip the prototype if it is not implemented (and thus not used)
1545 if (index == CallDAG::InvalidIndex)
1546 {
1547 return false;
1548 }
1549
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001550 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001551
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001552 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1553 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1554 << (mOutputLod0Function ? "Lod0(" : "(");
1555
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001556 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001557 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001558 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001559
1560 if (symbol)
1561 {
1562 out << argumentString(symbol);
1563
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001564 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001565 {
1566 out << ", ";
1567 }
1568 }
1569 else UNREACHABLE();
1570 }
1571
1572 out << ");\n";
1573
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001574 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001575 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1576 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001577 {
1578 mOutputLod0Function = true;
1579 node->traverse(this);
1580 mOutputLod0Function = false;
1581 }
1582
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001583 return false;
1584 }
1585 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001586 case EOpComma:
1587 outputTriplet(out, visit, "(", ", ", ")");
1588 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001589 case EOpFunction:
1590 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001591 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001592 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593
Corentin Wallez1239ee92015-03-19 14:38:02 -07001594 size_t index = mCallDag.findIndex(node);
1595 ASSERT(index != CallDAG::InvalidIndex);
1596 mCurrentFunctionMetadata = &mASTMetadataList[index];
1597
Jamie Madill033dae62014-06-18 12:56:28 -04001598 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001599
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001600 TIntermSequence *sequence = node->getSequence();
1601 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
1602
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001603 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001604 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001605 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001606 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001607 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001608 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001609 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001610 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001611 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001612
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001613 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001614 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001615 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001616
1617 if (symbol)
1618 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001619 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001620
1621 out << argumentString(symbol);
1622
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001623 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001624 {
1625 out << ", ";
1626 }
1627 }
1628 else UNREACHABLE();
1629 }
1630
Olli Etuaho4785fec2015-05-18 16:09:37 +03001631 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001632
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001633 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001634 {
1635 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03001636 TIntermNode *body = (*sequence)[1];
1637 // The function body node will output braces.
1638 ASSERT(IsSequence(body));
1639 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001640 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001641 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001642 else
1643 {
1644 out << "{}\n";
1645 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001646
Corentin Wallez1239ee92015-03-19 14:38:02 -07001647 mCurrentFunctionMetadata = nullptr;
1648
1649 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1650 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001651 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001652 ASSERT(name != "main");
1653 mOutputLod0Function = true;
1654 node->traverse(this);
1655 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001656 }
1657
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001658 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001659 }
1660 break;
1661 case EOpFunctionCall:
1662 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001663 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001664
Corentin Wallez1239ee92015-03-19 14:38:02 -07001665 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001666 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001668 if (node->isArray())
1669 {
1670 UNIMPLEMENTED();
1671 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07001672 size_t index = mCallDag.findIndex(node);
1673 ASSERT(index != CallDAG::InvalidIndex);
1674 lod0 &= mASTMetadataList[index].mNeedsLod0;
1675
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001676 out << DecorateFunctionIfNeeded(node->getNameObj());
1677 out << DisambiguateFunctionName(node->getSequence());
1678 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001679 }
1680 else
1681 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001682 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001683 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001684 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1685 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1686 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1687 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001689
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001690 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001691 {
Olli Etuaho96963162016-03-21 11:54:33 +02001692 TIntermTyped *typedArg = (*arg)->getAsTyped();
1693 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001694 {
1695 out << "texture_";
1696 (*arg)->traverse(this);
1697 out << ", sampler_";
1698 }
1699
1700 (*arg)->traverse(this);
1701
Olli Etuaho96963162016-03-21 11:54:33 +02001702 if (typedArg->getType().isStructureContainingSamplers())
1703 {
1704 const TType &argType = typedArg->getType();
1705 TVector<TIntermSymbol *> samplerSymbols;
1706 TString structName = samplerNamePrefixFromStruct(typedArg);
1707 argType.createSamplerSymbols("angle_" + structName, "",
1708 argType.isArray() ? argType.getArraySize() : 0,
1709 &samplerSymbols, nullptr);
1710 for (const TIntermSymbol *sampler : samplerSymbols)
1711 {
1712 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1713 {
1714 out << ", texture_" << sampler->getSymbol();
1715 out << ", sampler_" << sampler->getSymbol();
1716 }
1717 else
1718 {
1719 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1720 // of D3D9, it's the sampler variable.
1721 out << ", " + sampler->getSymbol();
1722 }
1723 }
1724 }
1725
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001726 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001727 {
1728 out << ", ";
1729 }
1730 }
1731
1732 out << ")";
1733
1734 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001735 }
1736 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001737 case EOpParameters:
1738 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1739 break;
1740 case EOpConstructFloat:
1741 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1742 break;
1743 case EOpConstructVec2:
1744 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1745 break;
1746 case EOpConstructVec3:
1747 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1748 break;
1749 case EOpConstructVec4:
1750 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1751 break;
1752 case EOpConstructBool:
1753 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1754 break;
1755 case EOpConstructBVec2:
1756 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1757 break;
1758 case EOpConstructBVec3:
1759 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1760 break;
1761 case EOpConstructBVec4:
1762 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1763 break;
1764 case EOpConstructInt:
1765 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1766 break;
1767 case EOpConstructIVec2:
1768 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1769 break;
1770 case EOpConstructIVec3:
1771 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1772 break;
1773 case EOpConstructIVec4:
1774 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1775 break;
1776 case EOpConstructUInt:
1777 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1778 break;
1779 case EOpConstructUVec2:
1780 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1781 break;
1782 case EOpConstructUVec3:
1783 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1784 break;
1785 case EOpConstructUVec4:
1786 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1787 break;
1788 case EOpConstructMat2:
1789 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1790 break;
1791 case EOpConstructMat2x3:
1792 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1793 break;
1794 case EOpConstructMat2x4:
1795 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1796 break;
1797 case EOpConstructMat3x2:
1798 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1799 break;
1800 case EOpConstructMat3:
1801 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1802 break;
1803 case EOpConstructMat3x4:
1804 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1805 break;
1806 case EOpConstructMat4x2:
1807 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1808 break;
1809 case EOpConstructMat4x3:
1810 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1811 break;
1812 case EOpConstructMat4:
1813 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1814 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001815 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001816 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001817 if (node->getType().isArray())
1818 {
1819 UNIMPLEMENTED();
1820 }
Jamie Madill033dae62014-06-18 12:56:28 -04001821 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001822 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001823 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001824 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001825 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001826 case EOpLessThan:
1827 outputTriplet(out, visit, "(", " < ", ")");
1828 break;
1829 case EOpGreaterThan:
1830 outputTriplet(out, visit, "(", " > ", ")");
1831 break;
1832 case EOpLessThanEqual:
1833 outputTriplet(out, visit, "(", " <= ", ")");
1834 break;
1835 case EOpGreaterThanEqual:
1836 outputTriplet(out, visit, "(", " >= ", ")");
1837 break;
1838 case EOpVectorEqual:
1839 outputTriplet(out, visit, "(", " == ", ")");
1840 break;
1841 case EOpVectorNotEqual:
1842 outputTriplet(out, visit, "(", " != ", ")");
1843 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001844 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001845 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001846 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001847 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001848 case EOpModf:
1849 outputTriplet(out, visit, "modf(", ", ", ")");
1850 break;
1851 case EOpPow:
1852 outputTriplet(out, visit, "pow(", ", ", ")");
1853 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001854 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001855 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02001856 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001857 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001858 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001859 case EOpMin:
1860 outputTriplet(out, visit, "min(", ", ", ")");
1861 break;
1862 case EOpMax:
1863 outputTriplet(out, visit, "max(", ", ", ")");
1864 break;
1865 case EOpClamp:
1866 outputTriplet(out, visit, "clamp(", ", ", ")");
1867 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301868 case EOpMix:
1869 {
1870 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1871 if (lastParamNode->getType().getBasicType() == EbtBool)
1872 {
1873 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1874 // so use emulated version.
1875 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001876 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301877 }
1878 else
1879 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001880 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301881 }
1882 }
1883 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001884 case EOpStep:
1885 outputTriplet(out, visit, "step(", ", ", ")");
1886 break;
1887 case EOpSmoothStep:
1888 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1889 break;
1890 case EOpDistance:
1891 outputTriplet(out, visit, "distance(", ", ", ")");
1892 break;
1893 case EOpDot:
1894 outputTriplet(out, visit, "dot(", ", ", ")");
1895 break;
1896 case EOpCross:
1897 outputTriplet(out, visit, "cross(", ", ", ")");
1898 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001899 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001900 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001901 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001902 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001903 case EOpReflect:
1904 outputTriplet(out, visit, "reflect(", ", ", ")");
1905 break;
1906 case EOpRefract:
1907 outputTriplet(out, visit, "refract(", ", ", ")");
1908 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001909 case EOpOuterProduct:
1910 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001911 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02001912 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001913 case EOpMul:
1914 outputTriplet(out, visit, "(", " * ", ")");
1915 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916 default: UNREACHABLE();
1917 }
1918
1919 return true;
1920}
1921
Jamie Madill8c46ab12015-12-07 16:39:19 -05001922void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001924 out << "if (";
1925
1926 node->getCondition()->traverse(this);
1927
1928 out << ")\n";
1929
Jamie Madill8c46ab12015-12-07 16:39:19 -05001930 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001931
1932 bool discard = false;
1933
1934 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001936 // The trueBlock child node will output braces.
1937 ASSERT(IsSequence(node->getTrueBlock()));
1938
Olli Etuahoa6f22092015-05-08 18:31:10 +03001939 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001940
Olli Etuahoa6f22092015-05-08 18:31:10 +03001941 // Detect true discard
1942 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1943 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001944 else
1945 {
1946 // TODO(oetuaho): Check if the semicolon inside is necessary.
1947 // It's there as a result of conservative refactoring of the output.
1948 out << "{;}\n";
1949 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001950
Jamie Madill8c46ab12015-12-07 16:39:19 -05001951 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001952
Olli Etuahoa6f22092015-05-08 18:31:10 +03001953 if (node->getFalseBlock())
1954 {
1955 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001956
Jamie Madill8c46ab12015-12-07 16:39:19 -05001957 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001958
Olli Etuaho4785fec2015-05-18 16:09:37 +03001959 // Either this is "else if" or the falseBlock child node will output braces.
1960 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
1961
Olli Etuahoa6f22092015-05-08 18:31:10 +03001962 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001963
Jamie Madill8c46ab12015-12-07 16:39:19 -05001964 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001965
Olli Etuahoa6f22092015-05-08 18:31:10 +03001966 // Detect false discard
1967 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1968 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001969
Olli Etuahoa6f22092015-05-08 18:31:10 +03001970 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001971 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001972 {
1973 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001974 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001975}
1976
1977bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1978{
1979 TInfoSinkBase &out = getInfoSink();
1980
1981 ASSERT(!node->usesTernaryOperator());
Olli Etuaho3d932d82016-04-12 11:10:30 +03001982 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001983
1984 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001985 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001986 {
1987 out << "FLATTEN ";
1988 }
1989
Jamie Madill8c46ab12015-12-07 16:39:19 -05001990 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001991
1992 return false;
1993}
1994
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001995bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02001996{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001997 TInfoSinkBase &out = getInfoSink();
1998
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001999 if (node->getStatementList())
2000 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002001 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002002 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002003 // The curly braces get written when visiting the statementList aggregate
2004 }
2005 else
2006 {
2007 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002008 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002009 }
2010 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002011}
2012
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002013bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002014{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002015 TInfoSinkBase &out = getInfoSink();
2016
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002017 if (node->hasCondition())
2018 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002019 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002020 return true;
2021 }
2022 else
2023 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002024 out << "default:\n";
2025 return false;
2026 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002027}
2028
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2030{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002031 TInfoSinkBase &out = getInfoSink();
2032 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033}
2034
2035bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2036{
Nicolas Capens655fe362014-04-11 13:12:34 -04002037 mNestedLoopDepth++;
2038
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002039 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002040 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002041 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002042
Jamie Madill8c46ab12015-12-07 16:39:19 -05002043 TInfoSinkBase &out = getInfoSink();
2044
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002045 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002046 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002047 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002048 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002049 mInsideDiscontinuousLoop = wasDiscontinuous;
2050 mNestedLoopDepth--;
2051
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002052 return false;
2053 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002054 }
2055
Corentin Wallez1239ee92015-03-19 14:38:02 -07002056 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002057 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002059 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002060
Jamie Madill8c46ab12015-12-07 16:39:19 -05002061 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 }
2063 else
2064 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002065 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002066
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002067 if (node->getInit())
2068 {
2069 node->getInit()->traverse(this);
2070 }
2071
2072 out << "; ";
2073
alokp@chromium.org52813552010-11-16 18:36:09 +00002074 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002075 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002076 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 }
2078
2079 out << "; ";
2080
alokp@chromium.org52813552010-11-16 18:36:09 +00002081 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002082 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002083 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 }
2085
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002086 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002087
Jamie Madill8c46ab12015-12-07 16:39:19 -05002088 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 }
2090
2091 if (node->getBody())
2092 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002093 // The loop body node will output braces.
2094 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002095 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002097 else
2098 {
2099 // TODO(oetuaho): Check if the semicolon inside is necessary.
2100 // It's there as a result of conservative refactoring of the output.
2101 out << "{;}\n";
2102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103
Jamie Madill8c46ab12015-12-07 16:39:19 -05002104 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105
alokp@chromium.org52813552010-11-16 18:36:09 +00002106 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002108 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 out << "while(\n";
2110
alokp@chromium.org52813552010-11-16 18:36:09 +00002111 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112
daniel@transgaming.com73536982012-03-21 20:45:49 +00002113 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 }
2115
daniel@transgaming.com73536982012-03-21 20:45:49 +00002116 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002118 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002119 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002120
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121 return false;
2122}
2123
2124bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2125{
Jamie Madill32aab012015-01-27 14:12:26 -05002126 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127
2128 switch (node->getFlowOp())
2129 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002130 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002131 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002132 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002133 case EOpBreak:
2134 if (visit == PreVisit)
2135 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002136 if (mNestedLoopDepth > 1)
2137 {
2138 mUsesNestedBreak = true;
2139 }
2140
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002141 if (mExcessiveLoopIndex)
2142 {
2143 out << "{Break";
2144 mExcessiveLoopIndex->traverse(this);
2145 out << " = true; break;}\n";
2146 }
2147 else
2148 {
2149 out << "break;\n";
2150 }
2151 }
2152 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002153 case EOpContinue:
2154 outputTriplet(out, visit, "continue;\n", "", "");
2155 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 case EOpReturn:
2157 if (visit == PreVisit)
2158 {
2159 if (node->getExpression())
2160 {
2161 out << "return ";
2162 }
2163 else
2164 {
2165 out << "return;\n";
2166 }
2167 }
2168 else if (visit == PostVisit)
2169 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002170 if (node->getExpression())
2171 {
2172 out << ";\n";
2173 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174 }
2175 break;
2176 default: UNREACHABLE();
2177 }
2178
2179 return true;
2180}
2181
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002182bool OutputHLSL::isSingleStatement(TIntermNode *node)
2183{
2184 TIntermAggregate *aggregate = node->getAsAggregate();
2185
2186 if (aggregate)
2187 {
2188 if (aggregate->getOp() == EOpSequence)
2189 {
2190 return false;
2191 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002192 else if (aggregate->getOp() == EOpDeclaration)
2193 {
2194 // Declaring multiple comma-separated variables must be considered multiple statements
2195 // because each individual declaration has side effects which are visible in the next.
2196 return false;
2197 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002198 else
2199 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002200 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002201 {
2202 if (!isSingleStatement(*sit))
2203 {
2204 return false;
2205 }
2206 }
2207
2208 return true;
2209 }
2210 }
2211
2212 return true;
2213}
2214
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002215// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2216// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002217bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002218{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002219 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002220
2221 // Parse loops of the form:
2222 // for(int index = initial; index [comparator] limit; index += increment)
2223 TIntermSymbol *index = NULL;
2224 TOperator comparator = EOpNull;
2225 int initial = 0;
2226 int limit = 0;
2227 int increment = 0;
2228
2229 // Parse index name and intial value
2230 if (node->getInit())
2231 {
2232 TIntermAggregate *init = node->getInit()->getAsAggregate();
2233
2234 if (init)
2235 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002236 TIntermSequence *sequence = init->getSequence();
2237 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002238
2239 if (variable && variable->getQualifier() == EvqTemporary)
2240 {
2241 TIntermBinary *assign = variable->getAsBinaryNode();
2242
2243 if (assign->getOp() == EOpInitialize)
2244 {
2245 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2246 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2247
2248 if (symbol && constant)
2249 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002250 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002251 {
2252 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002253 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002254 }
2255 }
2256 }
2257 }
2258 }
2259 }
2260
2261 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002262 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002263 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002264 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002265
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002266 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2267 {
2268 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2269
2270 if (constant)
2271 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002272 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002273 {
2274 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002275 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002276 }
2277 }
2278 }
2279 }
2280
2281 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002282 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002283 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002284 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2285 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002286
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002287 if (binaryTerminal)
2288 {
2289 TOperator op = binaryTerminal->getOp();
2290 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2291
2292 if (constant)
2293 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002294 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002295 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002296 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002297
2298 switch (op)
2299 {
2300 case EOpAddAssign: increment = value; break;
2301 case EOpSubAssign: increment = -value; break;
2302 default: UNIMPLEMENTED();
2303 }
2304 }
2305 }
2306 }
2307 else if (unaryTerminal)
2308 {
2309 TOperator op = unaryTerminal->getOp();
2310
2311 switch (op)
2312 {
2313 case EOpPostIncrement: increment = 1; break;
2314 case EOpPostDecrement: increment = -1; break;
2315 case EOpPreIncrement: increment = 1; break;
2316 case EOpPreDecrement: increment = -1; break;
2317 default: UNIMPLEMENTED();
2318 }
2319 }
2320 }
2321
2322 if (index != NULL && comparator != EOpNull && increment != 0)
2323 {
2324 if (comparator == EOpLessThanEqual)
2325 {
2326 comparator = EOpLessThan;
2327 limit += 1;
2328 }
2329
2330 if (comparator == EOpLessThan)
2331 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002332 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002333
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002334 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002335 {
2336 return false; // Not an excessive loop
2337 }
2338
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002339 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2340 mExcessiveLoopIndex = index;
2341
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002342 out << "{int ";
2343 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002344 out << ";\n"
2345 "bool Break";
2346 index->traverse(this);
2347 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002348
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002349 bool firstLoopFragment = true;
2350
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002351 while (iterations > 0)
2352 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002353 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002354
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002355 if (!firstLoopFragment)
2356 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002357 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002358 index->traverse(this);
2359 out << ") {\n";
2360 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002361
2362 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2363 {
2364 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2365 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002366
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002367 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002368 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002369
Corentin Wallez1239ee92015-03-19 14:38:02 -07002370 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002371 index->traverse(this);
2372 out << " = ";
2373 out << initial;
2374
2375 out << "; ";
2376 index->traverse(this);
2377 out << " < ";
2378 out << clampedLimit;
2379
2380 out << "; ";
2381 index->traverse(this);
2382 out << " += ";
2383 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002384 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002385
Jamie Madill8c46ab12015-12-07 16:39:19 -05002386 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002387 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002388
2389 if (node->getBody())
2390 {
2391 node->getBody()->traverse(this);
2392 }
2393
Jamie Madill8c46ab12015-12-07 16:39:19 -05002394 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002395 out << ";}\n";
2396
2397 if (!firstLoopFragment)
2398 {
2399 out << "}\n";
2400 }
2401
2402 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002403
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002404 initial += MAX_LOOP_ITERATIONS * increment;
2405 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002406 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002407
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002408 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002409
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002410 mExcessiveLoopIndex = restoreIndex;
2411
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002412 return true;
2413 }
2414 else UNIMPLEMENTED();
2415 }
2416
2417 return false; // Not handled as an excessive loop
2418}
2419
Jamie Madill8c46ab12015-12-07 16:39:19 -05002420void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2421 Visit visit,
2422 const char *preString,
2423 const char *inString,
2424 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002426 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427 {
2428 out << preString;
2429 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002430 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431 {
2432 out << inString;
2433 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002434 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435 {
2436 out << postString;
2437 }
2438}
2439
Jamie Madill8c46ab12015-12-07 16:39:19 -05002440void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002441{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002442 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002443 {
Jamie Madill32aab012015-01-27 14:12:26 -05002444 out << "\n";
2445 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002446
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002447 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002448 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002449 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002450 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002451
Jamie Madill32aab012015-01-27 14:12:26 -05002452 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002453 }
2454}
2455
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002456TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2457{
2458 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002459 const TType &type = symbol->getType();
2460 const TName &name = symbol->getName();
2461 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002462
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002463 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002464 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002465 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002466 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002467 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002468 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002469 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002470 }
2471
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002472 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002473 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002474 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2475 {
2476 // Samplers are passed as indices to the sampler array.
2477 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2478 return "const uint " + nameStr + ArrayString(type);
2479 }
2480 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2481 {
2482 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2483 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2484 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2485 ArrayString(type);
2486 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002487 }
2488
Olli Etuaho96963162016-03-21 11:54:33 +02002489 TStringStream argString;
2490 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2491 << ArrayString(type);
2492
2493 // If the structure parameter contains samplers, they need to be passed into the function as
2494 // separate parameters. HLSL doesn't natively support samplers in structs.
2495 if (type.isStructureContainingSamplers())
2496 {
2497 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2498 TVector<TIntermSymbol *> samplerSymbols;
2499 type.createSamplerSymbols("angle" + nameStr, "", 0, &samplerSymbols, nullptr);
2500 for (const TIntermSymbol *sampler : samplerSymbols)
2501 {
2502 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2503 {
2504 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2505 }
2506 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2507 {
2508 const TType &samplerType = sampler->getType();
2509 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2510 type.getArraySize() == samplerType.getArraySize());
2511 ASSERT(IsSampler(samplerType.getBasicType()));
2512 argString << ", " << QualifierString(qualifier) << " "
2513 << TextureString(samplerType.getBasicType()) << " texture_"
2514 << sampler->getSymbol() << ArrayString(type) << ", "
2515 << QualifierString(qualifier) << " "
2516 << SamplerString(samplerType.getBasicType()) << " sampler_"
2517 << sampler->getSymbol() << ArrayString(type);
2518 }
2519 else
2520 {
2521 const TType &samplerType = sampler->getType();
2522 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2523 type.getArraySize() == samplerType.getArraySize());
2524 ASSERT(IsSampler(samplerType.getBasicType()));
2525 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2526 << " " << sampler->getSymbol() << ArrayString(type);
2527 }
2528 }
2529 }
2530
2531 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532}
2533
2534TString OutputHLSL::initializer(const TType &type)
2535{
2536 TString string;
2537
Jamie Madill94bf7f22013-07-08 13:31:15 -04002538 size_t size = type.getObjectSize();
2539 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002541 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542
Jamie Madill94bf7f22013-07-08 13:31:15 -04002543 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544 {
2545 string += ", ";
2546 }
2547 }
2548
daniel@transgaming.comead23042010-04-29 03:35:36 +00002549 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002551
Jamie Madill8c46ab12015-12-07 16:39:19 -05002552void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2553 Visit visit,
2554 const TType &type,
2555 const char *name,
2556 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002557{
Olli Etuahof40319e2015-03-10 14:33:00 +02002558 if (type.isArray())
2559 {
2560 UNIMPLEMENTED();
2561 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002562
2563 if (visit == PreVisit)
2564 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002565 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002566
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002567 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002568 }
2569 else if (visit == InVisit)
2570 {
2571 out << ", ";
2572 }
2573 else if (visit == PostVisit)
2574 {
2575 out << ")";
2576 }
2577}
2578
Jamie Madill8c46ab12015-12-07 16:39:19 -05002579const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2580 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002581 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002582{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002583 const TConstantUnion *constUnionIterated = constUnion;
2584
Jamie Madill98493dd2013-07-08 14:39:03 -04002585 const TStructure* structure = type.getStruct();
2586 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002587 {
Jamie Madill033dae62014-06-18 12:56:28 -04002588 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002589
Jamie Madill98493dd2013-07-08 14:39:03 -04002590 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002591
Jamie Madill98493dd2013-07-08 14:39:03 -04002592 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002593 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002594 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002595 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002596
Jamie Madill98493dd2013-07-08 14:39:03 -04002597 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002598 {
2599 out << ", ";
2600 }
2601 }
2602
2603 out << ")";
2604 }
2605 else
2606 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002607 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002608 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002609
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002610 if (writeType)
2611 {
Jamie Madill033dae62014-06-18 12:56:28 -04002612 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002613 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002614 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002615 if (writeType)
2616 {
2617 out << ")";
2618 }
2619 }
2620
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002621 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002622}
2623
Jamie Madill8c46ab12015-12-07 16:39:19 -05002624void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002625{
2626 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002627 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002628}
2629
Jamie Madill37997142015-01-28 10:06:34 -05002630bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2631{
2632 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2633 expression->traverse(&searchSymbol);
2634
2635 if (searchSymbol.foundMatch())
2636 {
2637 // Type already printed
2638 out << "t" + str(mUniqueIndex) + " = ";
2639 expression->traverse(this);
2640 out << ", ";
2641 symbolNode->traverse(this);
2642 out << " = t" + str(mUniqueIndex);
2643
2644 mUniqueIndex++;
2645 return true;
2646 }
2647
2648 return false;
2649}
2650
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002651bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2652{
2653 // We support writing constant unions and constructors that only take constant unions as
2654 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002655 return expression->getAsConstantUnion() ||
2656 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002657}
2658
2659bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2660 TIntermSymbol *symbolNode,
2661 TIntermTyped *expression)
2662{
2663 if (canWriteAsHLSLLiteral(expression))
2664 {
2665 symbolNode->traverse(this);
2666 if (expression->getType().isArray())
2667 {
2668 out << "[" << expression->getType().getArraySize() << "]";
2669 }
2670 out << " = {";
2671 if (expression->getAsConstantUnion())
2672 {
2673 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2674 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2675 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2676 }
2677 else
2678 {
2679 TIntermAggregate *constructor = expression->getAsAggregate();
2680 ASSERT(constructor != nullptr);
2681 for (TIntermNode *&node : *constructor->getSequence())
2682 {
2683 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2684 ASSERT(nodeConst);
2685 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2686 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2687 if (node != constructor->getSequence()->back())
2688 {
2689 out << ", ";
2690 }
2691 }
2692 }
2693 out << "}";
2694 return true;
2695 }
2696 return false;
2697}
2698
Jamie Madill55e79e02015-02-09 15:35:00 -05002699TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2700{
2701 const TFieldList &fields = structure.fields();
2702
2703 for (const auto &eqFunction : mStructEqualityFunctions)
2704 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002705 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002706 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002707 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002708 }
2709 }
2710
2711 const TString &structNameString = StructNameString(structure);
2712
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002713 StructEqualityFunction *function = new StructEqualityFunction();
2714 function->structure = &structure;
2715 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002716
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002717 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002718
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002719 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2720 << "{\n"
2721 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002722
2723 for (size_t i = 0; i < fields.size(); i++)
2724 {
2725 const TField *field = fields[i];
2726 const TType *fieldType = field->type();
2727
2728 const TString &fieldNameA = "a." + Decorate(field->name());
2729 const TString &fieldNameB = "b." + Decorate(field->name());
2730
2731 if (i > 0)
2732 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002733 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002734 }
2735
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002736 fnOut << "(";
2737 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2738 fnOut << fieldNameA;
2739 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2740 fnOut << fieldNameB;
2741 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2742 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002743 }
2744
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002745 fnOut << ";\n" << "}\n";
2746
2747 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002748
2749 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002750 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002751
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002752 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002753}
2754
Olli Etuaho7fb49552015-03-18 17:27:44 +02002755TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2756{
2757 for (const auto &eqFunction : mArrayEqualityFunctions)
2758 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002759 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002760 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002761 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002762 }
2763 }
2764
2765 const TString &typeName = TypeString(type);
2766
Olli Etuaho12690762015-03-31 12:55:28 +03002767 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002768 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002769
2770 TInfoSinkBase fnNameOut;
2771 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002772 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002773
2774 TType nonArrayType = type;
2775 nonArrayType.clearArrayness();
2776
2777 TInfoSinkBase fnOut;
2778
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002779 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002780 << typeName << " a[" << type.getArraySize() << "], "
2781 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002782 << "{\n"
2783 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2784 " {\n"
2785 " if (";
2786
2787 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2788 fnOut << "a[i]";
2789 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2790 fnOut << "b[i]";
2791 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2792
2793 fnOut << ") { return false; }\n"
2794 " }\n"
2795 " return true;\n"
2796 "}\n";
2797
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002798 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002799
2800 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002801 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002802
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002803 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002804}
2805
Olli Etuaho12690762015-03-31 12:55:28 +03002806TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2807{
2808 for (const auto &assignFunction : mArrayAssignmentFunctions)
2809 {
2810 if (assignFunction.type == type)
2811 {
2812 return assignFunction.functionName;
2813 }
2814 }
2815
2816 const TString &typeName = TypeString(type);
2817
2818 ArrayHelperFunction function;
2819 function.type = type;
2820
2821 TInfoSinkBase fnNameOut;
2822 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2823 function.functionName = fnNameOut.c_str();
2824
2825 TInfoSinkBase fnOut;
2826
2827 fnOut << "void " << function.functionName << "(out "
2828 << typeName << " a[" << type.getArraySize() << "], "
2829 << typeName << " b[" << type.getArraySize() << "])\n"
2830 << "{\n"
2831 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2832 " {\n"
2833 " a[i] = b[i];\n"
2834 " }\n"
2835 "}\n";
2836
2837 function.functionDefinition = fnOut.c_str();
2838
2839 mArrayAssignmentFunctions.push_back(function);
2840
2841 return function.functionName;
2842}
2843
Olli Etuaho9638c352015-04-01 14:34:52 +03002844TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2845{
2846 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2847 {
2848 if (constructIntoFunction.type == type)
2849 {
2850 return constructIntoFunction.functionName;
2851 }
2852 }
2853
2854 const TString &typeName = TypeString(type);
2855
2856 ArrayHelperFunction function;
2857 function.type = type;
2858
2859 TInfoSinkBase fnNameOut;
2860 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2861 function.functionName = fnNameOut.c_str();
2862
2863 TInfoSinkBase fnOut;
2864
2865 fnOut << "void " << function.functionName << "(out "
2866 << typeName << " a[" << type.getArraySize() << "]";
2867 for (int i = 0; i < type.getArraySize(); ++i)
2868 {
2869 fnOut << ", " << typeName << " b" << i;
2870 }
2871 fnOut << ")\n"
2872 "{\n";
2873
2874 for (int i = 0; i < type.getArraySize(); ++i)
2875 {
2876 fnOut << " a[" << i << "] = b" << i << ";\n";
2877 }
2878 fnOut << "}\n";
2879
2880 function.functionDefinition = fnOut.c_str();
2881
2882 mArrayConstructIntoFunctions.push_back(function);
2883
2884 return function.functionName;
2885}
2886
Jamie Madill2e295e22015-04-29 10:41:33 -04002887void OutputHLSL::ensureStructDefined(const TType &type)
2888{
2889 TStructure *structure = type.getStruct();
2890
2891 if (structure)
2892 {
2893 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2894 }
2895}
2896
2897
Olli Etuaho9638c352015-04-01 14:34:52 +03002898
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002899}