blob: ff1c1a38a7b09d126d71ed49386914eeee7036db [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 }
Olli Etuahob741c762016-06-29 15:49:22 +03001680 else if (node->getNameObj().isInternal())
1681 {
1682 // This path is used for internal functions that don't have their definitions in the
1683 // AST, such as precision emulation functions.
1684 out << DecorateFunctionIfNeeded(node->getNameObj()) << "(";
1685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001686 else
1687 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001688 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001689 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001690 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1691 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1692 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1693 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001695
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001696 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001697 {
Olli Etuaho96963162016-03-21 11:54:33 +02001698 TIntermTyped *typedArg = (*arg)->getAsTyped();
1699 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001700 {
1701 out << "texture_";
1702 (*arg)->traverse(this);
1703 out << ", sampler_";
1704 }
1705
1706 (*arg)->traverse(this);
1707
Olli Etuaho96963162016-03-21 11:54:33 +02001708 if (typedArg->getType().isStructureContainingSamplers())
1709 {
1710 const TType &argType = typedArg->getType();
1711 TVector<TIntermSymbol *> samplerSymbols;
1712 TString structName = samplerNamePrefixFromStruct(typedArg);
1713 argType.createSamplerSymbols("angle_" + structName, "",
1714 argType.isArray() ? argType.getArraySize() : 0,
1715 &samplerSymbols, nullptr);
1716 for (const TIntermSymbol *sampler : samplerSymbols)
1717 {
1718 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1719 {
1720 out << ", texture_" << sampler->getSymbol();
1721 out << ", sampler_" << sampler->getSymbol();
1722 }
1723 else
1724 {
1725 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1726 // of D3D9, it's the sampler variable.
1727 out << ", " + sampler->getSymbol();
1728 }
1729 }
1730 }
1731
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001732 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001733 {
1734 out << ", ";
1735 }
1736 }
1737
1738 out << ")";
1739
1740 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001741 }
1742 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001743 case EOpParameters:
1744 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1745 break;
1746 case EOpConstructFloat:
1747 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1748 break;
1749 case EOpConstructVec2:
1750 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1751 break;
1752 case EOpConstructVec3:
1753 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1754 break;
1755 case EOpConstructVec4:
1756 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1757 break;
1758 case EOpConstructBool:
1759 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1760 break;
1761 case EOpConstructBVec2:
1762 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1763 break;
1764 case EOpConstructBVec3:
1765 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1766 break;
1767 case EOpConstructBVec4:
1768 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1769 break;
1770 case EOpConstructInt:
1771 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1772 break;
1773 case EOpConstructIVec2:
1774 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1775 break;
1776 case EOpConstructIVec3:
1777 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1778 break;
1779 case EOpConstructIVec4:
1780 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1781 break;
1782 case EOpConstructUInt:
1783 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1784 break;
1785 case EOpConstructUVec2:
1786 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1787 break;
1788 case EOpConstructUVec3:
1789 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1790 break;
1791 case EOpConstructUVec4:
1792 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1793 break;
1794 case EOpConstructMat2:
1795 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1796 break;
1797 case EOpConstructMat2x3:
1798 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1799 break;
1800 case EOpConstructMat2x4:
1801 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1802 break;
1803 case EOpConstructMat3x2:
1804 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1805 break;
1806 case EOpConstructMat3:
1807 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1808 break;
1809 case EOpConstructMat3x4:
1810 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1811 break;
1812 case EOpConstructMat4x2:
1813 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1814 break;
1815 case EOpConstructMat4x3:
1816 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1817 break;
1818 case EOpConstructMat4:
1819 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1820 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001821 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001822 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001823 if (node->getType().isArray())
1824 {
1825 UNIMPLEMENTED();
1826 }
Jamie Madill033dae62014-06-18 12:56:28 -04001827 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001828 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001829 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001830 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001831 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001832 case EOpLessThan:
1833 outputTriplet(out, visit, "(", " < ", ")");
1834 break;
1835 case EOpGreaterThan:
1836 outputTriplet(out, visit, "(", " > ", ")");
1837 break;
1838 case EOpLessThanEqual:
1839 outputTriplet(out, visit, "(", " <= ", ")");
1840 break;
1841 case EOpGreaterThanEqual:
1842 outputTriplet(out, visit, "(", " >= ", ")");
1843 break;
1844 case EOpVectorEqual:
1845 outputTriplet(out, visit, "(", " == ", ")");
1846 break;
1847 case EOpVectorNotEqual:
1848 outputTriplet(out, visit, "(", " != ", ")");
1849 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001850 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001851 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001852 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001853 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001854 case EOpModf:
1855 outputTriplet(out, visit, "modf(", ", ", ")");
1856 break;
1857 case EOpPow:
1858 outputTriplet(out, visit, "pow(", ", ", ")");
1859 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001861 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02001862 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001863 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001865 case EOpMin:
1866 outputTriplet(out, visit, "min(", ", ", ")");
1867 break;
1868 case EOpMax:
1869 outputTriplet(out, visit, "max(", ", ", ")");
1870 break;
1871 case EOpClamp:
1872 outputTriplet(out, visit, "clamp(", ", ", ")");
1873 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301874 case EOpMix:
1875 {
1876 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1877 if (lastParamNode->getType().getBasicType() == EbtBool)
1878 {
1879 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1880 // so use emulated version.
1881 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001882 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301883 }
1884 else
1885 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001886 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301887 }
1888 }
1889 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001890 case EOpStep:
1891 outputTriplet(out, visit, "step(", ", ", ")");
1892 break;
1893 case EOpSmoothStep:
1894 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1895 break;
1896 case EOpDistance:
1897 outputTriplet(out, visit, "distance(", ", ", ")");
1898 break;
1899 case EOpDot:
1900 outputTriplet(out, visit, "dot(", ", ", ")");
1901 break;
1902 case EOpCross:
1903 outputTriplet(out, visit, "cross(", ", ", ")");
1904 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001905 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001906 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001907 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001908 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001909 case EOpReflect:
1910 outputTriplet(out, visit, "reflect(", ", ", ")");
1911 break;
1912 case EOpRefract:
1913 outputTriplet(out, visit, "refract(", ", ", ")");
1914 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001915 case EOpOuterProduct:
1916 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001917 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02001918 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001919 case EOpMul:
1920 outputTriplet(out, visit, "(", " * ", ")");
1921 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922 default: UNREACHABLE();
1923 }
1924
1925 return true;
1926}
1927
Jamie Madill8c46ab12015-12-07 16:39:19 -05001928void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001930 out << "if (";
1931
1932 node->getCondition()->traverse(this);
1933
1934 out << ")\n";
1935
Jamie Madill8c46ab12015-12-07 16:39:19 -05001936 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001937
1938 bool discard = false;
1939
1940 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001942 // The trueBlock child node will output braces.
1943 ASSERT(IsSequence(node->getTrueBlock()));
1944
Olli Etuahoa6f22092015-05-08 18:31:10 +03001945 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001946
Olli Etuahoa6f22092015-05-08 18:31:10 +03001947 // Detect true discard
1948 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1949 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001950 else
1951 {
1952 // TODO(oetuaho): Check if the semicolon inside is necessary.
1953 // It's there as a result of conservative refactoring of the output.
1954 out << "{;}\n";
1955 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001956
Jamie Madill8c46ab12015-12-07 16:39:19 -05001957 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001958
Olli Etuahoa6f22092015-05-08 18:31:10 +03001959 if (node->getFalseBlock())
1960 {
1961 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001962
Jamie Madill8c46ab12015-12-07 16:39:19 -05001963 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001964
Olli Etuaho4785fec2015-05-18 16:09:37 +03001965 // Either this is "else if" or the falseBlock child node will output braces.
1966 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
1967
Olli Etuahoa6f22092015-05-08 18:31:10 +03001968 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001969
Jamie Madill8c46ab12015-12-07 16:39:19 -05001970 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001971
Olli Etuahoa6f22092015-05-08 18:31:10 +03001972 // Detect false discard
1973 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1974 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001975
Olli Etuahoa6f22092015-05-08 18:31:10 +03001976 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001977 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001978 {
1979 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001980 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001981}
1982
1983bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1984{
1985 TInfoSinkBase &out = getInfoSink();
1986
1987 ASSERT(!node->usesTernaryOperator());
Olli Etuaho3d932d82016-04-12 11:10:30 +03001988 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001989
1990 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001991 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001992 {
1993 out << "FLATTEN ";
1994 }
1995
Jamie Madill8c46ab12015-12-07 16:39:19 -05001996 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997
1998 return false;
1999}
2000
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002001bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002002{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002003 TInfoSinkBase &out = getInfoSink();
2004
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002005 if (node->getStatementList())
2006 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002007 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002008 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002009 // The curly braces get written when visiting the statementList aggregate
2010 }
2011 else
2012 {
2013 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002014 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002015 }
2016 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002017}
2018
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002019bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002020{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002021 TInfoSinkBase &out = getInfoSink();
2022
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002023 if (node->hasCondition())
2024 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002025 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002026 return true;
2027 }
2028 else
2029 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002030 out << "default:\n";
2031 return false;
2032 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002033}
2034
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2036{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002037 TInfoSinkBase &out = getInfoSink();
2038 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002039}
2040
2041bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2042{
Nicolas Capens655fe362014-04-11 13:12:34 -04002043 mNestedLoopDepth++;
2044
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002045 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002046 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002047 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002048
Jamie Madill8c46ab12015-12-07 16:39:19 -05002049 TInfoSinkBase &out = getInfoSink();
2050
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002051 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002052 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002053 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002054 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002055 mInsideDiscontinuousLoop = wasDiscontinuous;
2056 mNestedLoopDepth--;
2057
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002058 return false;
2059 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002060 }
2061
Corentin Wallez1239ee92015-03-19 14:38:02 -07002062 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002063 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002065 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002066
Jamie Madill8c46ab12015-12-07 16:39:19 -05002067 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068 }
2069 else
2070 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002071 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002072
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 if (node->getInit())
2074 {
2075 node->getInit()->traverse(this);
2076 }
2077
2078 out << "; ";
2079
alokp@chromium.org52813552010-11-16 18:36:09 +00002080 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002082 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 }
2084
2085 out << "; ";
2086
alokp@chromium.org52813552010-11-16 18:36:09 +00002087 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002089 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 }
2091
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002092 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002093
Jamie Madill8c46ab12015-12-07 16:39:19 -05002094 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095 }
2096
2097 if (node->getBody())
2098 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002099 // The loop body node will output braces.
2100 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002101 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002103 else
2104 {
2105 // TODO(oetuaho): Check if the semicolon inside is necessary.
2106 // It's there as a result of conservative refactoring of the output.
2107 out << "{;}\n";
2108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109
Jamie Madill8c46ab12015-12-07 16:39:19 -05002110 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111
alokp@chromium.org52813552010-11-16 18:36:09 +00002112 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002114 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 out << "while(\n";
2116
alokp@chromium.org52813552010-11-16 18:36:09 +00002117 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002118
daniel@transgaming.com73536982012-03-21 20:45:49 +00002119 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120 }
2121
daniel@transgaming.com73536982012-03-21 20:45:49 +00002122 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002124 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002125 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002126
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127 return false;
2128}
2129
2130bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2131{
Jamie Madill32aab012015-01-27 14:12:26 -05002132 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133
2134 switch (node->getFlowOp())
2135 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002136 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002137 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002138 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002139 case EOpBreak:
2140 if (visit == PreVisit)
2141 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002142 if (mNestedLoopDepth > 1)
2143 {
2144 mUsesNestedBreak = true;
2145 }
2146
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002147 if (mExcessiveLoopIndex)
2148 {
2149 out << "{Break";
2150 mExcessiveLoopIndex->traverse(this);
2151 out << " = true; break;}\n";
2152 }
2153 else
2154 {
2155 out << "break;\n";
2156 }
2157 }
2158 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002159 case EOpContinue:
2160 outputTriplet(out, visit, "continue;\n", "", "");
2161 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162 case EOpReturn:
2163 if (visit == PreVisit)
2164 {
2165 if (node->getExpression())
2166 {
2167 out << "return ";
2168 }
2169 else
2170 {
2171 out << "return;\n";
2172 }
2173 }
2174 else if (visit == PostVisit)
2175 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002176 if (node->getExpression())
2177 {
2178 out << ";\n";
2179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180 }
2181 break;
2182 default: UNREACHABLE();
2183 }
2184
2185 return true;
2186}
2187
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002188bool OutputHLSL::isSingleStatement(TIntermNode *node)
2189{
2190 TIntermAggregate *aggregate = node->getAsAggregate();
2191
2192 if (aggregate)
2193 {
2194 if (aggregate->getOp() == EOpSequence)
2195 {
2196 return false;
2197 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002198 else if (aggregate->getOp() == EOpDeclaration)
2199 {
2200 // Declaring multiple comma-separated variables must be considered multiple statements
2201 // because each individual declaration has side effects which are visible in the next.
2202 return false;
2203 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002204 else
2205 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002206 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002207 {
2208 if (!isSingleStatement(*sit))
2209 {
2210 return false;
2211 }
2212 }
2213
2214 return true;
2215 }
2216 }
2217
2218 return true;
2219}
2220
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002221// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2222// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002223bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002224{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002225 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002226
2227 // Parse loops of the form:
2228 // for(int index = initial; index [comparator] limit; index += increment)
2229 TIntermSymbol *index = NULL;
2230 TOperator comparator = EOpNull;
2231 int initial = 0;
2232 int limit = 0;
2233 int increment = 0;
2234
2235 // Parse index name and intial value
2236 if (node->getInit())
2237 {
2238 TIntermAggregate *init = node->getInit()->getAsAggregate();
2239
2240 if (init)
2241 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002242 TIntermSequence *sequence = init->getSequence();
2243 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002244
2245 if (variable && variable->getQualifier() == EvqTemporary)
2246 {
2247 TIntermBinary *assign = variable->getAsBinaryNode();
2248
2249 if (assign->getOp() == EOpInitialize)
2250 {
2251 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2252 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2253
2254 if (symbol && constant)
2255 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002256 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002257 {
2258 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002259 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002260 }
2261 }
2262 }
2263 }
2264 }
2265 }
2266
2267 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002268 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002269 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002270 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002271
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002272 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2273 {
2274 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2275
2276 if (constant)
2277 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002278 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002279 {
2280 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002281 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002282 }
2283 }
2284 }
2285 }
2286
2287 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002288 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002289 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002290 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2291 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002292
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002293 if (binaryTerminal)
2294 {
2295 TOperator op = binaryTerminal->getOp();
2296 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2297
2298 if (constant)
2299 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002300 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002301 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002302 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002303
2304 switch (op)
2305 {
2306 case EOpAddAssign: increment = value; break;
2307 case EOpSubAssign: increment = -value; break;
2308 default: UNIMPLEMENTED();
2309 }
2310 }
2311 }
2312 }
2313 else if (unaryTerminal)
2314 {
2315 TOperator op = unaryTerminal->getOp();
2316
2317 switch (op)
2318 {
2319 case EOpPostIncrement: increment = 1; break;
2320 case EOpPostDecrement: increment = -1; break;
2321 case EOpPreIncrement: increment = 1; break;
2322 case EOpPreDecrement: increment = -1; break;
2323 default: UNIMPLEMENTED();
2324 }
2325 }
2326 }
2327
2328 if (index != NULL && comparator != EOpNull && increment != 0)
2329 {
2330 if (comparator == EOpLessThanEqual)
2331 {
2332 comparator = EOpLessThan;
2333 limit += 1;
2334 }
2335
2336 if (comparator == EOpLessThan)
2337 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002338 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002339
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002340 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002341 {
2342 return false; // Not an excessive loop
2343 }
2344
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002345 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2346 mExcessiveLoopIndex = index;
2347
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002348 out << "{int ";
2349 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002350 out << ";\n"
2351 "bool Break";
2352 index->traverse(this);
2353 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002354
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002355 bool firstLoopFragment = true;
2356
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002357 while (iterations > 0)
2358 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002359 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002360
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002361 if (!firstLoopFragment)
2362 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002363 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002364 index->traverse(this);
2365 out << ") {\n";
2366 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002367
2368 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2369 {
2370 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2371 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002372
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002373 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002374 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002375
Corentin Wallez1239ee92015-03-19 14:38:02 -07002376 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002377 index->traverse(this);
2378 out << " = ";
2379 out << initial;
2380
2381 out << "; ";
2382 index->traverse(this);
2383 out << " < ";
2384 out << clampedLimit;
2385
2386 out << "; ";
2387 index->traverse(this);
2388 out << " += ";
2389 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002390 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002391
Jamie Madill8c46ab12015-12-07 16:39:19 -05002392 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002393 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002394
2395 if (node->getBody())
2396 {
2397 node->getBody()->traverse(this);
2398 }
2399
Jamie Madill8c46ab12015-12-07 16:39:19 -05002400 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002401 out << ";}\n";
2402
2403 if (!firstLoopFragment)
2404 {
2405 out << "}\n";
2406 }
2407
2408 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002409
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002410 initial += MAX_LOOP_ITERATIONS * increment;
2411 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002412 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002413
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002414 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002415
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002416 mExcessiveLoopIndex = restoreIndex;
2417
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002418 return true;
2419 }
2420 else UNIMPLEMENTED();
2421 }
2422
2423 return false; // Not handled as an excessive loop
2424}
2425
Jamie Madill8c46ab12015-12-07 16:39:19 -05002426void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2427 Visit visit,
2428 const char *preString,
2429 const char *inString,
2430 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002432 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 {
2434 out << preString;
2435 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002436 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437 {
2438 out << inString;
2439 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002440 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 {
2442 out << postString;
2443 }
2444}
2445
Jamie Madill8c46ab12015-12-07 16:39:19 -05002446void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002447{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002448 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002449 {
Jamie Madill32aab012015-01-27 14:12:26 -05002450 out << "\n";
2451 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002452
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002453 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002454 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002455 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002456 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002457
Jamie Madill32aab012015-01-27 14:12:26 -05002458 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002459 }
2460}
2461
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002462TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2463{
2464 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002465 const TType &type = symbol->getType();
2466 const TName &name = symbol->getName();
2467 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002468
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002469 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002470 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002471 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002472 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002473 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002474 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002475 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002476 }
2477
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002478 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002479 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002480 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2481 {
2482 // Samplers are passed as indices to the sampler array.
2483 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2484 return "const uint " + nameStr + ArrayString(type);
2485 }
2486 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2487 {
2488 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2489 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2490 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2491 ArrayString(type);
2492 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002493 }
2494
Olli Etuaho96963162016-03-21 11:54:33 +02002495 TStringStream argString;
2496 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2497 << ArrayString(type);
2498
2499 // If the structure parameter contains samplers, they need to be passed into the function as
2500 // separate parameters. HLSL doesn't natively support samplers in structs.
2501 if (type.isStructureContainingSamplers())
2502 {
2503 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2504 TVector<TIntermSymbol *> samplerSymbols;
2505 type.createSamplerSymbols("angle" + nameStr, "", 0, &samplerSymbols, nullptr);
2506 for (const TIntermSymbol *sampler : samplerSymbols)
2507 {
2508 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2509 {
2510 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2511 }
2512 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2513 {
2514 const TType &samplerType = sampler->getType();
2515 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2516 type.getArraySize() == samplerType.getArraySize());
2517 ASSERT(IsSampler(samplerType.getBasicType()));
2518 argString << ", " << QualifierString(qualifier) << " "
2519 << TextureString(samplerType.getBasicType()) << " texture_"
2520 << sampler->getSymbol() << ArrayString(type) << ", "
2521 << QualifierString(qualifier) << " "
2522 << SamplerString(samplerType.getBasicType()) << " sampler_"
2523 << sampler->getSymbol() << ArrayString(type);
2524 }
2525 else
2526 {
2527 const TType &samplerType = sampler->getType();
2528 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2529 type.getArraySize() == samplerType.getArraySize());
2530 ASSERT(IsSampler(samplerType.getBasicType()));
2531 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2532 << " " << sampler->getSymbol() << ArrayString(type);
2533 }
2534 }
2535 }
2536
2537 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538}
2539
2540TString OutputHLSL::initializer(const TType &type)
2541{
2542 TString string;
2543
Jamie Madill94bf7f22013-07-08 13:31:15 -04002544 size_t size = type.getObjectSize();
2545 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002547 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548
Jamie Madill94bf7f22013-07-08 13:31:15 -04002549 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550 {
2551 string += ", ";
2552 }
2553 }
2554
daniel@transgaming.comead23042010-04-29 03:35:36 +00002555 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002557
Jamie Madill8c46ab12015-12-07 16:39:19 -05002558void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2559 Visit visit,
2560 const TType &type,
2561 const char *name,
2562 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002563{
Olli Etuahof40319e2015-03-10 14:33:00 +02002564 if (type.isArray())
2565 {
2566 UNIMPLEMENTED();
2567 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002568
2569 if (visit == PreVisit)
2570 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002571 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002572
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002573 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002574 }
2575 else if (visit == InVisit)
2576 {
2577 out << ", ";
2578 }
2579 else if (visit == PostVisit)
2580 {
2581 out << ")";
2582 }
2583}
2584
Jamie Madill8c46ab12015-12-07 16:39:19 -05002585const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2586 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002587 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002588{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002589 const TConstantUnion *constUnionIterated = constUnion;
2590
Jamie Madill98493dd2013-07-08 14:39:03 -04002591 const TStructure* structure = type.getStruct();
2592 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002593 {
Jamie Madill033dae62014-06-18 12:56:28 -04002594 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002595
Jamie Madill98493dd2013-07-08 14:39:03 -04002596 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002597
Jamie Madill98493dd2013-07-08 14:39:03 -04002598 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002599 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002600 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002601 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002602
Jamie Madill98493dd2013-07-08 14:39:03 -04002603 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002604 {
2605 out << ", ";
2606 }
2607 }
2608
2609 out << ")";
2610 }
2611 else
2612 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002613 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002614 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002615
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002616 if (writeType)
2617 {
Jamie Madill033dae62014-06-18 12:56:28 -04002618 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002619 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002620 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002621 if (writeType)
2622 {
2623 out << ")";
2624 }
2625 }
2626
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002627 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002628}
2629
Jamie Madill8c46ab12015-12-07 16:39:19 -05002630void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002631{
2632 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002633 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002634}
2635
Jamie Madill37997142015-01-28 10:06:34 -05002636bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2637{
2638 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2639 expression->traverse(&searchSymbol);
2640
2641 if (searchSymbol.foundMatch())
2642 {
2643 // Type already printed
2644 out << "t" + str(mUniqueIndex) + " = ";
2645 expression->traverse(this);
2646 out << ", ";
2647 symbolNode->traverse(this);
2648 out << " = t" + str(mUniqueIndex);
2649
2650 mUniqueIndex++;
2651 return true;
2652 }
2653
2654 return false;
2655}
2656
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002657bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2658{
2659 // We support writing constant unions and constructors that only take constant unions as
2660 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002661 return expression->getAsConstantUnion() ||
2662 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002663}
2664
2665bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2666 TIntermSymbol *symbolNode,
2667 TIntermTyped *expression)
2668{
2669 if (canWriteAsHLSLLiteral(expression))
2670 {
2671 symbolNode->traverse(this);
2672 if (expression->getType().isArray())
2673 {
2674 out << "[" << expression->getType().getArraySize() << "]";
2675 }
2676 out << " = {";
2677 if (expression->getAsConstantUnion())
2678 {
2679 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2680 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2681 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2682 }
2683 else
2684 {
2685 TIntermAggregate *constructor = expression->getAsAggregate();
2686 ASSERT(constructor != nullptr);
2687 for (TIntermNode *&node : *constructor->getSequence())
2688 {
2689 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2690 ASSERT(nodeConst);
2691 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2692 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2693 if (node != constructor->getSequence()->back())
2694 {
2695 out << ", ";
2696 }
2697 }
2698 }
2699 out << "}";
2700 return true;
2701 }
2702 return false;
2703}
2704
Jamie Madill55e79e02015-02-09 15:35:00 -05002705TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2706{
2707 const TFieldList &fields = structure.fields();
2708
2709 for (const auto &eqFunction : mStructEqualityFunctions)
2710 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002711 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002712 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002713 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002714 }
2715 }
2716
2717 const TString &structNameString = StructNameString(structure);
2718
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002719 StructEqualityFunction *function = new StructEqualityFunction();
2720 function->structure = &structure;
2721 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002722
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002723 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002724
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002725 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2726 << "{\n"
2727 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002728
2729 for (size_t i = 0; i < fields.size(); i++)
2730 {
2731 const TField *field = fields[i];
2732 const TType *fieldType = field->type();
2733
2734 const TString &fieldNameA = "a." + Decorate(field->name());
2735 const TString &fieldNameB = "b." + Decorate(field->name());
2736
2737 if (i > 0)
2738 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002739 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002740 }
2741
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002742 fnOut << "(";
2743 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2744 fnOut << fieldNameA;
2745 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2746 fnOut << fieldNameB;
2747 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2748 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002749 }
2750
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002751 fnOut << ";\n" << "}\n";
2752
2753 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002754
2755 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002756 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002757
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002758 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002759}
2760
Olli Etuaho7fb49552015-03-18 17:27:44 +02002761TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2762{
2763 for (const auto &eqFunction : mArrayEqualityFunctions)
2764 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002765 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002766 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002767 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002768 }
2769 }
2770
2771 const TString &typeName = TypeString(type);
2772
Olli Etuaho12690762015-03-31 12:55:28 +03002773 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002774 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002775
2776 TInfoSinkBase fnNameOut;
2777 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002778 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002779
2780 TType nonArrayType = type;
2781 nonArrayType.clearArrayness();
2782
2783 TInfoSinkBase fnOut;
2784
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002785 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002786 << typeName << " a[" << type.getArraySize() << "], "
2787 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002788 << "{\n"
2789 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2790 " {\n"
2791 " if (";
2792
2793 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2794 fnOut << "a[i]";
2795 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2796 fnOut << "b[i]";
2797 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2798
2799 fnOut << ") { return false; }\n"
2800 " }\n"
2801 " return true;\n"
2802 "}\n";
2803
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002804 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002805
2806 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002807 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002808
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002809 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002810}
2811
Olli Etuaho12690762015-03-31 12:55:28 +03002812TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2813{
2814 for (const auto &assignFunction : mArrayAssignmentFunctions)
2815 {
2816 if (assignFunction.type == type)
2817 {
2818 return assignFunction.functionName;
2819 }
2820 }
2821
2822 const TString &typeName = TypeString(type);
2823
2824 ArrayHelperFunction function;
2825 function.type = type;
2826
2827 TInfoSinkBase fnNameOut;
2828 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2829 function.functionName = fnNameOut.c_str();
2830
2831 TInfoSinkBase fnOut;
2832
2833 fnOut << "void " << function.functionName << "(out "
2834 << typeName << " a[" << type.getArraySize() << "], "
2835 << typeName << " b[" << type.getArraySize() << "])\n"
2836 << "{\n"
2837 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2838 " {\n"
2839 " a[i] = b[i];\n"
2840 " }\n"
2841 "}\n";
2842
2843 function.functionDefinition = fnOut.c_str();
2844
2845 mArrayAssignmentFunctions.push_back(function);
2846
2847 return function.functionName;
2848}
2849
Olli Etuaho9638c352015-04-01 14:34:52 +03002850TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2851{
2852 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2853 {
2854 if (constructIntoFunction.type == type)
2855 {
2856 return constructIntoFunction.functionName;
2857 }
2858 }
2859
2860 const TString &typeName = TypeString(type);
2861
2862 ArrayHelperFunction function;
2863 function.type = type;
2864
2865 TInfoSinkBase fnNameOut;
2866 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2867 function.functionName = fnNameOut.c_str();
2868
2869 TInfoSinkBase fnOut;
2870
2871 fnOut << "void " << function.functionName << "(out "
2872 << typeName << " a[" << type.getArraySize() << "]";
2873 for (int i = 0; i < type.getArraySize(); ++i)
2874 {
2875 fnOut << ", " << typeName << " b" << i;
2876 }
2877 fnOut << ")\n"
2878 "{\n";
2879
2880 for (int i = 0; i < type.getArraySize(); ++i)
2881 {
2882 fnOut << " a[" << i << "] = b" << i << ";\n";
2883 }
2884 fnOut << "}\n";
2885
2886 function.functionDefinition = fnOut.c_str();
2887
2888 mArrayConstructIntoFunctions.push_back(function);
2889
2890 return function.functionName;
2891}
2892
Jamie Madill2e295e22015-04-29 10:41:33 -04002893void OutputHLSL::ensureStructDefined(const TType &type)
2894{
2895 TStructure *structure = type.getStruct();
2896
2897 if (structure)
2898 {
2899 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2900 }
2901}
2902
2903
Olli Etuaho9638c352015-04-01 14:34:52 +03002904
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002905}