blob: 9d77da23a963699649d20509b9ee16f024405b1c [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;
Olli Etuaho856c4972016-08-08 11:38:39 +0300227 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000228
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
Geoff Lang1fe74c72016-08-25 13:23:01 -0400608 bool getDimensionsIgnoresBaseLevel =
609 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
610 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000612 if (mUsesFragCoord)
613 {
614 out << "#define GL_USES_FRAG_COORD\n";
615 }
616
617 if (mUsesPointCoord)
618 {
619 out << "#define GL_USES_POINT_COORD\n";
620 }
621
622 if (mUsesFrontFacing)
623 {
624 out << "#define GL_USES_FRONT_FACING\n";
625 }
626
627 if (mUsesPointSize)
628 {
629 out << "#define GL_USES_POINT_SIZE\n";
630 }
631
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400632 if (mUsesFragDepth)
633 {
634 out << "#define GL_USES_FRAG_DEPTH\n";
635 }
636
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000637 if (mUsesDepthRange)
638 {
639 out << "#define GL_USES_DEPTH_RANGE\n";
640 }
641
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000642 if (mUsesXor)
643 {
644 out << "bool xor(bool p, bool q)\n"
645 "{\n"
646 " return (p || q) && !(p && q);\n"
647 "}\n"
648 "\n";
649 }
650
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200651 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652}
653
654void OutputHLSL::visitSymbol(TIntermSymbol *node)
655{
Jamie Madill32aab012015-01-27 14:12:26 -0500656 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657
Jamie Madill570e04d2013-06-21 09:15:33 -0400658 // Handle accessing std140 structs by value
659 if (mFlaggedStructMappedNames.count(node) > 0)
660 {
661 out << mFlaggedStructMappedNames[node];
662 return;
663 }
664
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665 TString name = node->getSymbol();
666
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000667 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000668 {
669 mUsesDepthRange = true;
670 out << name;
671 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000672 else
673 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000674 TQualifier qualifier = node->getQualifier();
675
676 if (qualifier == EvqUniform)
677 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400678 const TType &nodeType = node->getType();
679 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400680
681 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000682 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400683 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000684 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000685 else
686 {
687 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000688 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400689
Jamie Madill2e295e22015-04-29 10:41:33 -0400690 ensureStructDefined(nodeType);
691
Olli Etuaho96963162016-03-21 11:54:33 +0200692 const TName &nameWithMetadata = node->getName();
693 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000694 }
Jamie Madill19571812013-08-12 15:26:34 -0700695 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000696 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000697 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400698 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000699 }
Jamie Madill033dae62014-06-18 12:56:28 -0400700 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000701 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000702 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400703 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000704 }
Jamie Madill19571812013-08-12 15:26:34 -0700705 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400706 {
707 mReferencedOutputVariables[name] = node;
708 out << "out_" << name;
709 }
710 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000711 {
712 out << "gl_Color[0]";
713 mUsesFragColor = true;
714 }
715 else if (qualifier == EvqFragData)
716 {
717 out << "gl_Color";
718 mUsesFragData = true;
719 }
720 else if (qualifier == EvqFragCoord)
721 {
722 mUsesFragCoord = true;
723 out << name;
724 }
725 else if (qualifier == EvqPointCoord)
726 {
727 mUsesPointCoord = true;
728 out << name;
729 }
730 else if (qualifier == EvqFrontFacing)
731 {
732 mUsesFrontFacing = true;
733 out << name;
734 }
735 else if (qualifier == EvqPointSize)
736 {
737 mUsesPointSize = true;
738 out << name;
739 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000740 else if (qualifier == EvqInstanceID)
741 {
742 mUsesInstanceID = true;
743 out << name;
744 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500745 else if (qualifier == EvqVertexID)
746 {
747 mUsesVertexID = true;
748 out << name;
749 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300750 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400751 {
752 mUsesFragDepth = true;
753 out << "gl_Depth";
754 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000755 else
756 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300757 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000758 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759 }
760}
761
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400762void OutputHLSL::visitRaw(TIntermRaw *node)
763{
Jamie Madill32aab012015-01-27 14:12:26 -0500764 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400765}
766
Olli Etuaho7fb49552015-03-18 17:27:44 +0200767void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
768{
769 if (type.isScalar() && !type.isArray())
770 {
771 if (op == EOpEqual)
772 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500773 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200774 }
775 else
776 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500777 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200778 }
779 }
780 else
781 {
782 if (visit == PreVisit && op == EOpNotEqual)
783 {
784 out << "!";
785 }
786
787 if (type.isArray())
788 {
789 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500790 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200791 }
792 else if (type.getBasicType() == EbtStruct)
793 {
794 const TStructure &structure = *type.getStruct();
795 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500796 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200797 }
798 else
799 {
800 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500801 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200802 }
803 }
804}
805
Olli Etuaho96963162016-03-21 11:54:33 +0200806bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
807{
808 // Inside InVisit the current node is already in the path.
809 const unsigned int initialN = visit == InVisit ? 1u : 0u;
810 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
811 {
812 TIntermNode *ancestor = getAncestorNode(n);
813 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
814 if (ancestorBinary == nullptr)
815 {
816 return false;
817 }
818 switch (ancestorBinary->getOp())
819 {
820 case EOpIndexDirectStruct:
821 {
822 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
823 const TIntermConstantUnion *index =
824 ancestorBinary->getRight()->getAsConstantUnion();
825 const TField *field = structure->fields()[index->getIConst(0)];
826 if (IsSampler(field->type()->getBasicType()))
827 {
828 return true;
829 }
830 break;
831 }
832 case EOpIndexDirect:
833 break;
834 default:
835 // Returning a sampler from indirect indexing is not supported.
836 return false;
837 }
838 }
839 return false;
840}
841
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
843{
Jamie Madill32aab012015-01-27 14:12:26 -0500844 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845
Jamie Madill570e04d2013-06-21 09:15:33 -0400846 // Handle accessing std140 structs by value
847 if (mFlaggedStructMappedNames.count(node) > 0)
848 {
849 out << mFlaggedStructMappedNames[node];
850 return false;
851 }
852
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853 switch (node->getOp())
854 {
Olli Etuahoe79904c2015-03-18 16:56:42 +0200855 case EOpAssign:
856 if (node->getLeft()->isArray())
857 {
Olli Etuaho9638c352015-04-01 14:34:52 +0300858 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
859 if (rightAgg != nullptr && rightAgg->isConstructor())
860 {
861 const TString &functionName = addArrayConstructIntoFunction(node->getType());
862 out << functionName << "(";
863 node->getLeft()->traverse(this);
864 TIntermSequence *seq = rightAgg->getSequence();
865 for (auto &arrayElement : *seq)
866 {
867 out << ", ";
868 arrayElement->traverse(this);
869 }
870 out << ")";
871 return false;
872 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +0300873 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
874 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
875
876 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500877 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200878 }
879 else
880 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500881 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200882 }
883 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000884 case EOpInitialize:
885 if (visit == PreVisit)
886 {
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000887 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -0500888 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000889 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000890
Olli Etuaho3d932d82016-04-12 11:10:30 +0300891 // Global initializers must be constant at this point.
Olli Etuahod4f4c112016-04-15 15:11:24 +0300892 ASSERT(symbolNode->getQualifier() != EvqGlobal || canWriteAsHLSLLiteral(expression));
893
894 // GLSL allows to write things like "float x = x;" where a new variable x is defined
895 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
896 // new variable is created before the assignment is evaluated), so we need to convert
897 // this to "float t = x, x = t;".
Olli Etuaho3d932d82016-04-12 11:10:30 +0300898 if (writeSameSymbolInitializer(out, symbolNode, expression))
Jamie Madill37997142015-01-28 10:06:34 -0500899 {
900 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000901 return false;
902 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200903 else if (writeConstantInitialization(out, symbolNode, expression))
904 {
905 return false;
906 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000907 }
908 else if (visit == InVisit)
909 {
910 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000911 }
912 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500913 case EOpAddAssign:
914 outputTriplet(out, visit, "(", " += ", ")");
915 break;
916 case EOpSubAssign:
917 outputTriplet(out, visit, "(", " -= ", ")");
918 break;
919 case EOpMulAssign:
920 outputTriplet(out, visit, "(", " *= ", ")");
921 break;
922 case EOpVectorTimesScalarAssign:
923 outputTriplet(out, visit, "(", " *= ", ")");
924 break;
925 case EOpMatrixTimesScalarAssign:
926 outputTriplet(out, visit, "(", " *= ", ")");
927 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000928 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000929 if (visit == PreVisit)
930 {
931 out << "(";
932 }
933 else if (visit == InVisit)
934 {
935 out << " = mul(";
936 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -0400937 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000938 }
939 else
940 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000941 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000942 }
943 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000944 case EOpMatrixTimesMatrixAssign:
945 if (visit == PreVisit)
946 {
947 out << "(";
948 }
949 else if (visit == InVisit)
950 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200951 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000952 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +0200953 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000954 }
955 else
956 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200957 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000958 }
959 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500960 case EOpDivAssign:
961 outputTriplet(out, visit, "(", " /= ", ")");
962 break;
963 case EOpIModAssign:
964 outputTriplet(out, visit, "(", " %= ", ")");
965 break;
966 case EOpBitShiftLeftAssign:
967 outputTriplet(out, visit, "(", " <<= ", ")");
968 break;
969 case EOpBitShiftRightAssign:
970 outputTriplet(out, visit, "(", " >>= ", ")");
971 break;
972 case EOpBitwiseAndAssign:
973 outputTriplet(out, visit, "(", " &= ", ")");
974 break;
975 case EOpBitwiseXorAssign:
976 outputTriplet(out, visit, "(", " ^= ", ")");
977 break;
978 case EOpBitwiseOrAssign:
979 outputTriplet(out, visit, "(", " |= ", ")");
980 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -0400981 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -0400982 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400983 const TType& leftType = node->getLeft()->getType();
984 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -0400985 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400986 if (visit == PreVisit)
987 {
988 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
989 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -0400990 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -0400991 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -0400992 return false;
993 }
Jamie Madillb4e664b2013-06-20 11:55:54 -0400994 }
Olli Etuaho96963162016-03-21 11:54:33 +0200995 else if (ancestorEvaluatesToSamplerInStruct(visit))
996 {
997 // All parts of an expression that access a sampler in a struct need to use _ as
998 // separator to access the sampler variable that has been moved out of the struct.
999 outputTriplet(out, visit, "", "_", "");
1000 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001001 else
1002 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001003 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001004 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001005 }
1006 break;
1007 case EOpIndexIndirect:
1008 // We do not currently support indirect references to interface blocks
1009 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001010 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001011 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001012 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001013 {
1014 const TStructure* structure = node->getLeft()->getType().getStruct();
1015 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1016 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001017
Olli Etuaho96963162016-03-21 11:54:33 +02001018 // In cases where indexing returns a sampler, we need to access the sampler variable
1019 // that has been moved out of the struct.
1020 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1021 if (visit == PreVisit && indexingReturnsSampler)
1022 {
1023 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1024 // This prefix is only output at the beginning of the indexing expression, which
1025 // may have multiple parts.
1026 out << "angle";
1027 }
1028 if (!indexingReturnsSampler)
1029 {
1030 // All parts of an expression that access a sampler in a struct need to use _ as
1031 // separator to access the sampler variable that has been moved out of the struct.
1032 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1033 }
1034 if (visit == InVisit)
1035 {
1036 if (indexingReturnsSampler)
1037 {
1038 out << "_" + field->name();
1039 }
1040 else
1041 {
1042 out << "." + DecorateField(field->name(), *structure);
1043 }
1044
1045 return false;
1046 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001047 }
1048 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001049 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001050 if (visit == InVisit)
1051 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001052 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1053 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1054 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001055 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001056
1057 return false;
1058 }
1059 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060 case EOpVectorSwizzle:
1061 if (visit == InVisit)
1062 {
1063 out << ".";
1064
1065 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1066
1067 if (swizzle)
1068 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001069 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001071 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001072 {
1073 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1074
1075 if (element)
1076 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001077 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078
1079 switch (i)
1080 {
1081 case 0: out << "x"; break;
1082 case 1: out << "y"; break;
1083 case 2: out << "z"; break;
1084 case 3: out << "w"; break;
1085 default: UNREACHABLE();
1086 }
1087 }
1088 else UNREACHABLE();
1089 }
1090 }
1091 else UNREACHABLE();
1092
1093 return false; // Fully processed
1094 }
1095 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001096 case EOpAdd:
1097 outputTriplet(out, visit, "(", " + ", ")");
1098 break;
1099 case EOpSub:
1100 outputTriplet(out, visit, "(", " - ", ")");
1101 break;
1102 case EOpMul:
1103 outputTriplet(out, visit, "(", " * ", ")");
1104 break;
1105 case EOpDiv:
1106 outputTriplet(out, visit, "(", " / ", ")");
1107 break;
1108 case EOpIMod:
1109 outputTriplet(out, visit, "(", " % ", ")");
1110 break;
1111 case EOpBitShiftLeft:
1112 outputTriplet(out, visit, "(", " << ", ")");
1113 break;
1114 case EOpBitShiftRight:
1115 outputTriplet(out, visit, "(", " >> ", ")");
1116 break;
1117 case EOpBitwiseAnd:
1118 outputTriplet(out, visit, "(", " & ", ")");
1119 break;
1120 case EOpBitwiseXor:
1121 outputTriplet(out, visit, "(", " ^ ", ")");
1122 break;
1123 case EOpBitwiseOr:
1124 outputTriplet(out, visit, "(", " | ", ")");
1125 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001126 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001127 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001128 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001129 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001130 case EOpLessThan:
1131 outputTriplet(out, visit, "(", " < ", ")");
1132 break;
1133 case EOpGreaterThan:
1134 outputTriplet(out, visit, "(", " > ", ")");
1135 break;
1136 case EOpLessThanEqual:
1137 outputTriplet(out, visit, "(", " <= ", ")");
1138 break;
1139 case EOpGreaterThanEqual:
1140 outputTriplet(out, visit, "(", " >= ", ")");
1141 break;
1142 case EOpVectorTimesScalar:
1143 outputTriplet(out, visit, "(", " * ", ")");
1144 break;
1145 case EOpMatrixTimesScalar:
1146 outputTriplet(out, visit, "(", " * ", ")");
1147 break;
1148 case EOpVectorTimesMatrix:
1149 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1150 break;
1151 case EOpMatrixTimesVector:
1152 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1153 break;
1154 case EOpMatrixTimesMatrix:
1155 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1156 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001157 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001158 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1159 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001160 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001161 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001162 case EOpLogicalXor:
1163 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001164 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001165 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001166 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001167 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1168 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001169 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001170 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171 default: UNREACHABLE();
1172 }
1173
1174 return true;
1175}
1176
1177bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1178{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001179 TInfoSinkBase &out = getInfoSink();
1180
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001181 switch (node->getOp())
1182 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001183 case EOpNegative:
1184 outputTriplet(out, visit, "(-", "", ")");
1185 break;
1186 case EOpPositive:
1187 outputTriplet(out, visit, "(+", "", ")");
1188 break;
1189 case EOpVectorLogicalNot:
1190 outputTriplet(out, visit, "(!", "", ")");
1191 break;
1192 case EOpLogicalNot:
1193 outputTriplet(out, visit, "(!", "", ")");
1194 break;
1195 case EOpBitwiseNot:
1196 outputTriplet(out, visit, "(~", "", ")");
1197 break;
1198 case EOpPostIncrement:
1199 outputTriplet(out, visit, "(", "", "++)");
1200 break;
1201 case EOpPostDecrement:
1202 outputTriplet(out, visit, "(", "", "--)");
1203 break;
1204 case EOpPreIncrement:
1205 outputTriplet(out, visit, "(++", "", ")");
1206 break;
1207 case EOpPreDecrement:
1208 outputTriplet(out, visit, "(--", "", ")");
1209 break;
1210 case EOpRadians:
1211 outputTriplet(out, visit, "radians(", "", ")");
1212 break;
1213 case EOpDegrees:
1214 outputTriplet(out, visit, "degrees(", "", ")");
1215 break;
1216 case EOpSin:
1217 outputTriplet(out, visit, "sin(", "", ")");
1218 break;
1219 case EOpCos:
1220 outputTriplet(out, visit, "cos(", "", ")");
1221 break;
1222 case EOpTan:
1223 outputTriplet(out, visit, "tan(", "", ")");
1224 break;
1225 case EOpAsin:
1226 outputTriplet(out, visit, "asin(", "", ")");
1227 break;
1228 case EOpAcos:
1229 outputTriplet(out, visit, "acos(", "", ")");
1230 break;
1231 case EOpAtan:
1232 outputTriplet(out, visit, "atan(", "", ")");
1233 break;
1234 case EOpSinh:
1235 outputTriplet(out, visit, "sinh(", "", ")");
1236 break;
1237 case EOpCosh:
1238 outputTriplet(out, visit, "cosh(", "", ")");
1239 break;
1240 case EOpTanh:
1241 outputTriplet(out, visit, "tanh(", "", ")");
1242 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001243 case EOpAsinh:
1244 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001245 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001246 break;
1247 case EOpAcosh:
1248 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001249 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001250 break;
1251 case EOpAtanh:
1252 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001253 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001254 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001255 case EOpExp:
1256 outputTriplet(out, visit, "exp(", "", ")");
1257 break;
1258 case EOpLog:
1259 outputTriplet(out, visit, "log(", "", ")");
1260 break;
1261 case EOpExp2:
1262 outputTriplet(out, visit, "exp2(", "", ")");
1263 break;
1264 case EOpLog2:
1265 outputTriplet(out, visit, "log2(", "", ")");
1266 break;
1267 case EOpSqrt:
1268 outputTriplet(out, visit, "sqrt(", "", ")");
1269 break;
1270 case EOpInverseSqrt:
1271 outputTriplet(out, visit, "rsqrt(", "", ")");
1272 break;
1273 case EOpAbs:
1274 outputTriplet(out, visit, "abs(", "", ")");
1275 break;
1276 case EOpSign:
1277 outputTriplet(out, visit, "sign(", "", ")");
1278 break;
1279 case EOpFloor:
1280 outputTriplet(out, visit, "floor(", "", ")");
1281 break;
1282 case EOpTrunc:
1283 outputTriplet(out, visit, "trunc(", "", ")");
1284 break;
1285 case EOpRound:
1286 outputTriplet(out, visit, "round(", "", ")");
1287 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001288 case EOpRoundEven:
1289 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001290 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001291 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001292 case EOpCeil:
1293 outputTriplet(out, visit, "ceil(", "", ")");
1294 break;
1295 case EOpFract:
1296 outputTriplet(out, visit, "frac(", "", ")");
1297 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301298 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001299 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05301300 mRequiresIEEEStrictCompiling = true;
1301 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001302 case EOpIsInf:
1303 outputTriplet(out, visit, "isinf(", "", ")");
1304 break;
1305 case EOpFloatBitsToInt:
1306 outputTriplet(out, visit, "asint(", "", ")");
1307 break;
1308 case EOpFloatBitsToUint:
1309 outputTriplet(out, visit, "asuint(", "", ")");
1310 break;
1311 case EOpIntBitsToFloat:
1312 outputTriplet(out, visit, "asfloat(", "", ")");
1313 break;
1314 case EOpUintBitsToFloat:
1315 outputTriplet(out, visit, "asfloat(", "", ")");
1316 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001317 case EOpPackSnorm2x16:
1318 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001319 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001320 break;
1321 case EOpPackUnorm2x16:
1322 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001323 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001324 break;
1325 case EOpPackHalf2x16:
1326 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001327 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001328 break;
1329 case EOpUnpackSnorm2x16:
1330 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001331 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001332 break;
1333 case EOpUnpackUnorm2x16:
1334 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001335 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001336 break;
1337 case EOpUnpackHalf2x16:
1338 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001339 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001340 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001341 case EOpLength:
1342 outputTriplet(out, visit, "length(", "", ")");
1343 break;
1344 case EOpNormalize:
1345 outputTriplet(out, visit, "normalize(", "", ")");
1346 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001347 case EOpDFdx:
1348 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1349 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001350 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001351 }
1352 else
1353 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001354 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001355 }
1356 break;
1357 case EOpDFdy:
1358 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1359 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001360 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001361 }
1362 else
1363 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001364 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001365 }
1366 break;
1367 case EOpFwidth:
1368 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1369 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001370 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001371 }
1372 else
1373 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001374 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001375 }
1376 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001377 case EOpTranspose:
1378 outputTriplet(out, visit, "transpose(", "", ")");
1379 break;
1380 case EOpDeterminant:
1381 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1382 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001383 case EOpInverse:
1384 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001385 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001386 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001387
Jamie Madill8c46ab12015-12-07 16:39:19 -05001388 case EOpAny:
1389 outputTriplet(out, visit, "any(", "", ")");
1390 break;
1391 case EOpAll:
1392 outputTriplet(out, visit, "all(", "", ")");
1393 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001394 default: UNREACHABLE();
1395 }
1396
1397 return true;
1398}
1399
Olli Etuaho96963162016-03-21 11:54:33 +02001400TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1401{
1402 if (node->getAsSymbolNode())
1403 {
1404 return node->getAsSymbolNode()->getSymbol();
1405 }
1406 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1407 switch (nodeBinary->getOp())
1408 {
1409 case EOpIndexDirect:
1410 {
1411 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1412
1413 TInfoSinkBase prefixSink;
1414 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1415 return TString(prefixSink.c_str());
1416 }
1417 case EOpIndexDirectStruct:
1418 {
1419 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1420 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1421 const TField *field = s->fields()[index];
1422
1423 TInfoSinkBase prefixSink;
1424 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1425 << field->name();
1426 return TString(prefixSink.c_str());
1427 }
1428 default:
1429 UNREACHABLE();
1430 return TString("");
1431 }
1432}
1433
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001434bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1435{
Jamie Madill32aab012015-01-27 14:12:26 -05001436 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001437
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001438 switch (node->getOp())
1439 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001440 case EOpSequence:
1441 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001442 if (mInsideFunction)
1443 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001444 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001445 out << "{\n";
1446 }
1447
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001448 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001449 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001450 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00001451
Olli Etuahoa6f22092015-05-08 18:31:10 +03001452 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001453
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001454 // Don't output ; after case labels, they're terminated by :
1455 // This is needed especially since outputting a ; after a case statement would turn empty
1456 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03001457 // Also no need to output ; after selection (if) statements or sequences. This is done just
1458 // for code clarity.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001459 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsSelectionNode() == nullptr &&
1460 !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001461 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001462 }
1463
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001464 if (mInsideFunction)
1465 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001466 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001467 out << "}\n";
1468 }
1469
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001470 return false;
1471 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001472 case EOpDeclaration:
1473 if (visit == PreVisit)
1474 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001475 TIntermSequence *sequence = node->getSequence();
1476 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001477 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001478
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001479 if (variable &&
1480 (variable->getQualifier() == EvqTemporary ||
1481 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001482 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001483 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001484
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001485 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001486 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001487 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001488 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001489 out << "static ";
1490 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001491
Olli Etuahoa6f22092015-05-08 18:31:10 +03001492 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001493
Olli Etuahoa6f22092015-05-08 18:31:10 +03001494 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001495
Olli Etuahoa6f22092015-05-08 18:31:10 +03001496 if (symbol)
1497 {
1498 symbol->traverse(this);
1499 out << ArrayString(symbol->getType());
1500 out << " = " + initializer(symbol->getType());
1501 }
1502 else
1503 {
1504 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001505 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001506 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001507 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1508 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001509 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001510 }
1511 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001512 }
Jamie Madill033dae62014-06-18 12:56:28 -04001513 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001514 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001515 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001516 {
1517 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1518
1519 if (symbol)
1520 {
1521 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1522 mReferencedVaryings[symbol->getSymbol()] = symbol;
1523 }
1524 else
1525 {
1526 (*sit)->traverse(this);
1527 }
1528 }
1529 }
1530
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531 return false;
1532 }
1533 else if (visit == InVisit)
1534 {
1535 out << ", ";
1536 }
1537 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001538 case EOpInvariantDeclaration:
1539 // Do not do any translation
1540 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001541 case EOpPrototype:
1542 if (visit == PreVisit)
1543 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001544 size_t index = mCallDag.findIndex(node);
1545 // Skip the prototype if it is not implemented (and thus not used)
1546 if (index == CallDAG::InvalidIndex)
1547 {
1548 return false;
1549 }
1550
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001551 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001552
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001553 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1554 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1555 << (mOutputLod0Function ? "Lod0(" : "(");
1556
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001557 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001558 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001559 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001560
1561 if (symbol)
1562 {
1563 out << argumentString(symbol);
1564
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001565 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001566 {
1567 out << ", ";
1568 }
1569 }
1570 else UNREACHABLE();
1571 }
1572
1573 out << ");\n";
1574
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001575 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001576 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1577 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001578 {
1579 mOutputLod0Function = true;
1580 node->traverse(this);
1581 mOutputLod0Function = false;
1582 }
1583
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001584 return false;
1585 }
1586 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001587 case EOpComma:
1588 outputTriplet(out, visit, "(", ", ", ")");
1589 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590 case EOpFunction:
1591 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001592 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001593 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594
Corentin Wallez1239ee92015-03-19 14:38:02 -07001595 size_t index = mCallDag.findIndex(node);
1596 ASSERT(index != CallDAG::InvalidIndex);
1597 mCurrentFunctionMetadata = &mASTMetadataList[index];
1598
Jamie Madill033dae62014-06-18 12:56:28 -04001599 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001600
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001601 TIntermSequence *sequence = node->getSequence();
1602 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
1603
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001604 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001605 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001606 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001607 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001608 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001609 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001610 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001611 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001612 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001613
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001614 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001615 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001616 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001617
1618 if (symbol)
1619 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001620 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001621
1622 out << argumentString(symbol);
1623
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001624 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001625 {
1626 out << ", ";
1627 }
1628 }
1629 else UNREACHABLE();
1630 }
1631
Olli Etuaho4785fec2015-05-18 16:09:37 +03001632 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001633
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001634 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001635 {
1636 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03001637 TIntermNode *body = (*sequence)[1];
1638 // The function body node will output braces.
1639 ASSERT(IsSequence(body));
1640 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001641 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001642 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001643 else
1644 {
1645 out << "{}\n";
1646 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001647
Corentin Wallez1239ee92015-03-19 14:38:02 -07001648 mCurrentFunctionMetadata = nullptr;
1649
1650 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1651 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001652 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001653 ASSERT(name != "main");
1654 mOutputLod0Function = true;
1655 node->traverse(this);
1656 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001657 }
1658
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001659 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001660 }
1661 break;
1662 case EOpFunctionCall:
1663 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001664 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001665
Corentin Wallez1239ee92015-03-19 14:38:02 -07001666 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001667 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001668 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001669 if (node->isArray())
1670 {
1671 UNIMPLEMENTED();
1672 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07001673 size_t index = mCallDag.findIndex(node);
1674 ASSERT(index != CallDAG::InvalidIndex);
1675 lod0 &= mASTMetadataList[index].mNeedsLod0;
1676
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001677 out << DecorateFunctionIfNeeded(node->getNameObj());
1678 out << DisambiguateFunctionName(node->getSequence());
1679 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001680 }
Olli Etuahob741c762016-06-29 15:49:22 +03001681 else if (node->getNameObj().isInternal())
1682 {
1683 // This path is used for internal functions that don't have their definitions in the
1684 // AST, such as precision emulation functions.
1685 out << DecorateFunctionIfNeeded(node->getNameObj()) << "(";
1686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001687 else
1688 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001689 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001690 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001691 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1692 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1693 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1694 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001696
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001697 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001698 {
Olli Etuaho96963162016-03-21 11:54:33 +02001699 TIntermTyped *typedArg = (*arg)->getAsTyped();
1700 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001701 {
1702 out << "texture_";
1703 (*arg)->traverse(this);
1704 out << ", sampler_";
1705 }
1706
1707 (*arg)->traverse(this);
1708
Olli Etuaho96963162016-03-21 11:54:33 +02001709 if (typedArg->getType().isStructureContainingSamplers())
1710 {
1711 const TType &argType = typedArg->getType();
1712 TVector<TIntermSymbol *> samplerSymbols;
1713 TString structName = samplerNamePrefixFromStruct(typedArg);
1714 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001715 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001716 &samplerSymbols, nullptr);
1717 for (const TIntermSymbol *sampler : samplerSymbols)
1718 {
1719 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1720 {
1721 out << ", texture_" << sampler->getSymbol();
1722 out << ", sampler_" << sampler->getSymbol();
1723 }
1724 else
1725 {
1726 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1727 // of D3D9, it's the sampler variable.
1728 out << ", " + sampler->getSymbol();
1729 }
1730 }
1731 }
1732
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001733 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001734 {
1735 out << ", ";
1736 }
1737 }
1738
1739 out << ")";
1740
1741 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001742 }
1743 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001744 case EOpParameters:
1745 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1746 break;
1747 case EOpConstructFloat:
1748 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1749 break;
1750 case EOpConstructVec2:
1751 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1752 break;
1753 case EOpConstructVec3:
1754 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1755 break;
1756 case EOpConstructVec4:
1757 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1758 break;
1759 case EOpConstructBool:
1760 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1761 break;
1762 case EOpConstructBVec2:
1763 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1764 break;
1765 case EOpConstructBVec3:
1766 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1767 break;
1768 case EOpConstructBVec4:
1769 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1770 break;
1771 case EOpConstructInt:
1772 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1773 break;
1774 case EOpConstructIVec2:
1775 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1776 break;
1777 case EOpConstructIVec3:
1778 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1779 break;
1780 case EOpConstructIVec4:
1781 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1782 break;
1783 case EOpConstructUInt:
1784 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1785 break;
1786 case EOpConstructUVec2:
1787 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1788 break;
1789 case EOpConstructUVec3:
1790 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1791 break;
1792 case EOpConstructUVec4:
1793 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1794 break;
1795 case EOpConstructMat2:
1796 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1797 break;
1798 case EOpConstructMat2x3:
1799 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1800 break;
1801 case EOpConstructMat2x4:
1802 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1803 break;
1804 case EOpConstructMat3x2:
1805 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1806 break;
1807 case EOpConstructMat3:
1808 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1809 break;
1810 case EOpConstructMat3x4:
1811 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1812 break;
1813 case EOpConstructMat4x2:
1814 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1815 break;
1816 case EOpConstructMat4x3:
1817 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1818 break;
1819 case EOpConstructMat4:
1820 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1821 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001822 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001823 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001824 if (node->getType().isArray())
1825 {
1826 UNIMPLEMENTED();
1827 }
Jamie Madill033dae62014-06-18 12:56:28 -04001828 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001829 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001830 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001831 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001832 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001833 case EOpLessThan:
1834 outputTriplet(out, visit, "(", " < ", ")");
1835 break;
1836 case EOpGreaterThan:
1837 outputTriplet(out, visit, "(", " > ", ")");
1838 break;
1839 case EOpLessThanEqual:
1840 outputTriplet(out, visit, "(", " <= ", ")");
1841 break;
1842 case EOpGreaterThanEqual:
1843 outputTriplet(out, visit, "(", " >= ", ")");
1844 break;
1845 case EOpVectorEqual:
1846 outputTriplet(out, visit, "(", " == ", ")");
1847 break;
1848 case EOpVectorNotEqual:
1849 outputTriplet(out, visit, "(", " != ", ")");
1850 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001851 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001852 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001853 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001854 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001855 case EOpModf:
1856 outputTriplet(out, visit, "modf(", ", ", ")");
1857 break;
1858 case EOpPow:
1859 outputTriplet(out, visit, "pow(", ", ", ")");
1860 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001861 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001862 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02001863 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001864 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001866 case EOpMin:
1867 outputTriplet(out, visit, "min(", ", ", ")");
1868 break;
1869 case EOpMax:
1870 outputTriplet(out, visit, "max(", ", ", ")");
1871 break;
1872 case EOpClamp:
1873 outputTriplet(out, visit, "clamp(", ", ", ")");
1874 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301875 case EOpMix:
1876 {
1877 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1878 if (lastParamNode->getType().getBasicType() == EbtBool)
1879 {
1880 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1881 // so use emulated version.
1882 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001883 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301884 }
1885 else
1886 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001887 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301888 }
1889 }
1890 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001891 case EOpStep:
1892 outputTriplet(out, visit, "step(", ", ", ")");
1893 break;
1894 case EOpSmoothStep:
1895 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1896 break;
1897 case EOpDistance:
1898 outputTriplet(out, visit, "distance(", ", ", ")");
1899 break;
1900 case EOpDot:
1901 outputTriplet(out, visit, "dot(", ", ", ")");
1902 break;
1903 case EOpCross:
1904 outputTriplet(out, visit, "cross(", ", ", ")");
1905 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001906 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001907 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001908 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001909 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001910 case EOpReflect:
1911 outputTriplet(out, visit, "reflect(", ", ", ")");
1912 break;
1913 case EOpRefract:
1914 outputTriplet(out, visit, "refract(", ", ", ")");
1915 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001916 case EOpOuterProduct:
1917 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001918 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02001919 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001920 case EOpMul:
1921 outputTriplet(out, visit, "(", " * ", ")");
1922 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923 default: UNREACHABLE();
1924 }
1925
1926 return true;
1927}
1928
Jamie Madill8c46ab12015-12-07 16:39:19 -05001929void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001930{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001931 out << "if (";
1932
1933 node->getCondition()->traverse(this);
1934
1935 out << ")\n";
1936
Jamie Madill8c46ab12015-12-07 16:39:19 -05001937 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001938
1939 bool discard = false;
1940
1941 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001943 // The trueBlock child node will output braces.
1944 ASSERT(IsSequence(node->getTrueBlock()));
1945
Olli Etuahoa6f22092015-05-08 18:31:10 +03001946 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001947
Olli Etuahoa6f22092015-05-08 18:31:10 +03001948 // Detect true discard
1949 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1950 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001951 else
1952 {
1953 // TODO(oetuaho): Check if the semicolon inside is necessary.
1954 // It's there as a result of conservative refactoring of the output.
1955 out << "{;}\n";
1956 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001957
Jamie Madill8c46ab12015-12-07 16:39:19 -05001958 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001959
Olli Etuahoa6f22092015-05-08 18:31:10 +03001960 if (node->getFalseBlock())
1961 {
1962 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001963
Jamie Madill8c46ab12015-12-07 16:39:19 -05001964 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001965
Olli Etuaho4785fec2015-05-18 16:09:37 +03001966 // Either this is "else if" or the falseBlock child node will output braces.
1967 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
1968
Olli Etuahoa6f22092015-05-08 18:31:10 +03001969 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001970
Jamie Madill8c46ab12015-12-07 16:39:19 -05001971 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001972
Olli Etuahoa6f22092015-05-08 18:31:10 +03001973 // Detect false discard
1974 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1975 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001976
Olli Etuahoa6f22092015-05-08 18:31:10 +03001977 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001978 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001979 {
1980 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001981 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001982}
1983
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001984bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1985{
1986 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1987 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1988 UNREACHABLE();
1989 return false;
1990}
1991
Olli Etuahod81ed842015-05-12 12:46:35 +03001992bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1993{
1994 TInfoSinkBase &out = getInfoSink();
1995
Olli Etuaho3d932d82016-04-12 11:10:30 +03001996 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001997
1998 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001999 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002000 {
2001 out << "FLATTEN ";
2002 }
2003
Jamie Madill8c46ab12015-12-07 16:39:19 -05002004 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005
2006 return false;
2007}
2008
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002009bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002010{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002011 TInfoSinkBase &out = getInfoSink();
2012
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002013 if (node->getStatementList())
2014 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002015 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002016 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002017 // The curly braces get written when visiting the statementList aggregate
2018 }
2019 else
2020 {
2021 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002022 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002023 }
2024 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002025}
2026
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002027bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002028{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002029 TInfoSinkBase &out = getInfoSink();
2030
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002031 if (node->hasCondition())
2032 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002033 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002034 return true;
2035 }
2036 else
2037 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002038 out << "default:\n";
2039 return false;
2040 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002041}
2042
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002043void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2044{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002045 TInfoSinkBase &out = getInfoSink();
2046 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002047}
2048
2049bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2050{
Nicolas Capens655fe362014-04-11 13:12:34 -04002051 mNestedLoopDepth++;
2052
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002053 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002054 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002055 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002056
Jamie Madill8c46ab12015-12-07 16:39:19 -05002057 TInfoSinkBase &out = getInfoSink();
2058
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002059 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002060 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002061 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002062 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002063 mInsideDiscontinuousLoop = wasDiscontinuous;
2064 mNestedLoopDepth--;
2065
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002066 return false;
2067 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002068 }
2069
Corentin Wallez1239ee92015-03-19 14:38:02 -07002070 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002071 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002072 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002073 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002074
Jamie Madill8c46ab12015-12-07 16:39:19 -05002075 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076 }
2077 else
2078 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002079 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002080
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 if (node->getInit())
2082 {
2083 node->getInit()->traverse(this);
2084 }
2085
2086 out << "; ";
2087
alokp@chromium.org52813552010-11-16 18:36:09 +00002088 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002090 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091 }
2092
2093 out << "; ";
2094
alokp@chromium.org52813552010-11-16 18:36:09 +00002095 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002097 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098 }
2099
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002100 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002101
Jamie Madill8c46ab12015-12-07 16:39:19 -05002102 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 }
2104
2105 if (node->getBody())
2106 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002107 // The loop body node will output braces.
2108 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002109 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002111 else
2112 {
2113 // TODO(oetuaho): Check if the semicolon inside is necessary.
2114 // It's there as a result of conservative refactoring of the output.
2115 out << "{;}\n";
2116 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117
Jamie Madill8c46ab12015-12-07 16:39:19 -05002118 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119
alokp@chromium.org52813552010-11-16 18:36:09 +00002120 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002122 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 out << "while(\n";
2124
alokp@chromium.org52813552010-11-16 18:36:09 +00002125 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002126
daniel@transgaming.com73536982012-03-21 20:45:49 +00002127 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 }
2129
daniel@transgaming.com73536982012-03-21 20:45:49 +00002130 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002132 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002133 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002134
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135 return false;
2136}
2137
2138bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2139{
Jamie Madill32aab012015-01-27 14:12:26 -05002140 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141
2142 switch (node->getFlowOp())
2143 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002144 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002145 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002146 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002147 case EOpBreak:
2148 if (visit == PreVisit)
2149 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002150 if (mNestedLoopDepth > 1)
2151 {
2152 mUsesNestedBreak = true;
2153 }
2154
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002155 if (mExcessiveLoopIndex)
2156 {
2157 out << "{Break";
2158 mExcessiveLoopIndex->traverse(this);
2159 out << " = true; break;}\n";
2160 }
2161 else
2162 {
2163 out << "break;\n";
2164 }
2165 }
2166 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002167 case EOpContinue:
2168 outputTriplet(out, visit, "continue;\n", "", "");
2169 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170 case EOpReturn:
2171 if (visit == PreVisit)
2172 {
2173 if (node->getExpression())
2174 {
2175 out << "return ";
2176 }
2177 else
2178 {
2179 out << "return;\n";
2180 }
2181 }
2182 else if (visit == PostVisit)
2183 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002184 if (node->getExpression())
2185 {
2186 out << ";\n";
2187 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188 }
2189 break;
2190 default: UNREACHABLE();
2191 }
2192
2193 return true;
2194}
2195
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002196bool OutputHLSL::isSingleStatement(TIntermNode *node)
2197{
2198 TIntermAggregate *aggregate = node->getAsAggregate();
2199
2200 if (aggregate)
2201 {
2202 if (aggregate->getOp() == EOpSequence)
2203 {
2204 return false;
2205 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002206 else if (aggregate->getOp() == EOpDeclaration)
2207 {
2208 // Declaring multiple comma-separated variables must be considered multiple statements
2209 // because each individual declaration has side effects which are visible in the next.
2210 return false;
2211 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002212 else
2213 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002214 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002215 {
2216 if (!isSingleStatement(*sit))
2217 {
2218 return false;
2219 }
2220 }
2221
2222 return true;
2223 }
2224 }
2225
2226 return true;
2227}
2228
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002229// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2230// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002231bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002232{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002233 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002234
2235 // Parse loops of the form:
2236 // for(int index = initial; index [comparator] limit; index += increment)
2237 TIntermSymbol *index = NULL;
2238 TOperator comparator = EOpNull;
2239 int initial = 0;
2240 int limit = 0;
2241 int increment = 0;
2242
2243 // Parse index name and intial value
2244 if (node->getInit())
2245 {
2246 TIntermAggregate *init = node->getInit()->getAsAggregate();
2247
2248 if (init)
2249 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002250 TIntermSequence *sequence = init->getSequence();
2251 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002252
2253 if (variable && variable->getQualifier() == EvqTemporary)
2254 {
2255 TIntermBinary *assign = variable->getAsBinaryNode();
2256
2257 if (assign->getOp() == EOpInitialize)
2258 {
2259 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2260 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2261
2262 if (symbol && constant)
2263 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002264 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002265 {
2266 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002267 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002268 }
2269 }
2270 }
2271 }
2272 }
2273 }
2274
2275 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002276 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002277 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002278 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002279
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002280 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2281 {
2282 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2283
2284 if (constant)
2285 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002286 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002287 {
2288 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002289 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002290 }
2291 }
2292 }
2293 }
2294
2295 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002296 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002297 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002298 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2299 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002300
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002301 if (binaryTerminal)
2302 {
2303 TOperator op = binaryTerminal->getOp();
2304 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2305
2306 if (constant)
2307 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002308 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002309 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002310 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002311
2312 switch (op)
2313 {
2314 case EOpAddAssign: increment = value; break;
2315 case EOpSubAssign: increment = -value; break;
2316 default: UNIMPLEMENTED();
2317 }
2318 }
2319 }
2320 }
2321 else if (unaryTerminal)
2322 {
2323 TOperator op = unaryTerminal->getOp();
2324
2325 switch (op)
2326 {
2327 case EOpPostIncrement: increment = 1; break;
2328 case EOpPostDecrement: increment = -1; break;
2329 case EOpPreIncrement: increment = 1; break;
2330 case EOpPreDecrement: increment = -1; break;
2331 default: UNIMPLEMENTED();
2332 }
2333 }
2334 }
2335
2336 if (index != NULL && comparator != EOpNull && increment != 0)
2337 {
2338 if (comparator == EOpLessThanEqual)
2339 {
2340 comparator = EOpLessThan;
2341 limit += 1;
2342 }
2343
2344 if (comparator == EOpLessThan)
2345 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002346 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002347
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002348 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002349 {
2350 return false; // Not an excessive loop
2351 }
2352
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002353 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2354 mExcessiveLoopIndex = index;
2355
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002356 out << "{int ";
2357 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002358 out << ";\n"
2359 "bool Break";
2360 index->traverse(this);
2361 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002362
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002363 bool firstLoopFragment = true;
2364
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002365 while (iterations > 0)
2366 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002367 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002368
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002369 if (!firstLoopFragment)
2370 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002371 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002372 index->traverse(this);
2373 out << ") {\n";
2374 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002375
2376 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2377 {
2378 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2379 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002380
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002381 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002382 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002383
Corentin Wallez1239ee92015-03-19 14:38:02 -07002384 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002385 index->traverse(this);
2386 out << " = ";
2387 out << initial;
2388
2389 out << "; ";
2390 index->traverse(this);
2391 out << " < ";
2392 out << clampedLimit;
2393
2394 out << "; ";
2395 index->traverse(this);
2396 out << " += ";
2397 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002398 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002399
Jamie Madill8c46ab12015-12-07 16:39:19 -05002400 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002401 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002402
2403 if (node->getBody())
2404 {
2405 node->getBody()->traverse(this);
2406 }
2407
Jamie Madill8c46ab12015-12-07 16:39:19 -05002408 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002409 out << ";}\n";
2410
2411 if (!firstLoopFragment)
2412 {
2413 out << "}\n";
2414 }
2415
2416 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002417
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002418 initial += MAX_LOOP_ITERATIONS * increment;
2419 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002420 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002421
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002422 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002423
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002424 mExcessiveLoopIndex = restoreIndex;
2425
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002426 return true;
2427 }
2428 else UNIMPLEMENTED();
2429 }
2430
2431 return false; // Not handled as an excessive loop
2432}
2433
Jamie Madill8c46ab12015-12-07 16:39:19 -05002434void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2435 Visit visit,
2436 const char *preString,
2437 const char *inString,
2438 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002440 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441 {
2442 out << preString;
2443 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002444 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002445 {
2446 out << inString;
2447 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002448 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 {
2450 out << postString;
2451 }
2452}
2453
Jamie Madill8c46ab12015-12-07 16:39:19 -05002454void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002455{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002456 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002457 {
Jamie Madill32aab012015-01-27 14:12:26 -05002458 out << "\n";
2459 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002460
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002461 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002462 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002463 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002464 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002465
Jamie Madill32aab012015-01-27 14:12:26 -05002466 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002467 }
2468}
2469
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002470TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2471{
2472 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002473 const TType &type = symbol->getType();
2474 const TName &name = symbol->getName();
2475 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002476
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002477 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002478 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002479 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002480 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002481 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002482 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002483 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002484 }
2485
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002486 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002487 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002488 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2489 {
2490 // Samplers are passed as indices to the sampler array.
2491 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2492 return "const uint " + nameStr + ArrayString(type);
2493 }
2494 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2495 {
2496 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2497 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2498 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2499 ArrayString(type);
2500 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002501 }
2502
Olli Etuaho96963162016-03-21 11:54:33 +02002503 TStringStream argString;
2504 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2505 << ArrayString(type);
2506
2507 // If the structure parameter contains samplers, they need to be passed into the function as
2508 // separate parameters. HLSL doesn't natively support samplers in structs.
2509 if (type.isStructureContainingSamplers())
2510 {
2511 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2512 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002513 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002514 for (const TIntermSymbol *sampler : samplerSymbols)
2515 {
2516 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2517 {
2518 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2519 }
2520 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2521 {
2522 const TType &samplerType = sampler->getType();
2523 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2524 type.getArraySize() == samplerType.getArraySize());
2525 ASSERT(IsSampler(samplerType.getBasicType()));
2526 argString << ", " << QualifierString(qualifier) << " "
2527 << TextureString(samplerType.getBasicType()) << " texture_"
2528 << sampler->getSymbol() << ArrayString(type) << ", "
2529 << QualifierString(qualifier) << " "
2530 << SamplerString(samplerType.getBasicType()) << " sampler_"
2531 << sampler->getSymbol() << ArrayString(type);
2532 }
2533 else
2534 {
2535 const TType &samplerType = sampler->getType();
2536 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2537 type.getArraySize() == samplerType.getArraySize());
2538 ASSERT(IsSampler(samplerType.getBasicType()));
2539 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2540 << " " << sampler->getSymbol() << ArrayString(type);
2541 }
2542 }
2543 }
2544
2545 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546}
2547
2548TString OutputHLSL::initializer(const TType &type)
2549{
2550 TString string;
2551
Jamie Madill94bf7f22013-07-08 13:31:15 -04002552 size_t size = type.getObjectSize();
2553 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002554 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002555 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556
Jamie Madill94bf7f22013-07-08 13:31:15 -04002557 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558 {
2559 string += ", ";
2560 }
2561 }
2562
daniel@transgaming.comead23042010-04-29 03:35:36 +00002563 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002564}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002565
Jamie Madill8c46ab12015-12-07 16:39:19 -05002566void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2567 Visit visit,
2568 const TType &type,
2569 const char *name,
2570 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002571{
Olli Etuahof40319e2015-03-10 14:33:00 +02002572 if (type.isArray())
2573 {
2574 UNIMPLEMENTED();
2575 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002576
2577 if (visit == PreVisit)
2578 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002579 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002580
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002581 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002582 }
2583 else if (visit == InVisit)
2584 {
2585 out << ", ";
2586 }
2587 else if (visit == PostVisit)
2588 {
2589 out << ")";
2590 }
2591}
2592
Jamie Madill8c46ab12015-12-07 16:39:19 -05002593const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2594 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002595 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002596{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002597 const TConstantUnion *constUnionIterated = constUnion;
2598
Jamie Madill98493dd2013-07-08 14:39:03 -04002599 const TStructure* structure = type.getStruct();
2600 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002601 {
Jamie Madill033dae62014-06-18 12:56:28 -04002602 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002603
Jamie Madill98493dd2013-07-08 14:39:03 -04002604 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002605
Jamie Madill98493dd2013-07-08 14:39:03 -04002606 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002607 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002608 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002609 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002610
Jamie Madill98493dd2013-07-08 14:39:03 -04002611 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002612 {
2613 out << ", ";
2614 }
2615 }
2616
2617 out << ")";
2618 }
2619 else
2620 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002621 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002622 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002623
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002624 if (writeType)
2625 {
Jamie Madill033dae62014-06-18 12:56:28 -04002626 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002627 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002628 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002629 if (writeType)
2630 {
2631 out << ")";
2632 }
2633 }
2634
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002635 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002636}
2637
Jamie Madill8c46ab12015-12-07 16:39:19 -05002638void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002639{
2640 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002641 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002642}
2643
Jamie Madill37997142015-01-28 10:06:34 -05002644bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2645{
2646 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2647 expression->traverse(&searchSymbol);
2648
2649 if (searchSymbol.foundMatch())
2650 {
2651 // Type already printed
2652 out << "t" + str(mUniqueIndex) + " = ";
2653 expression->traverse(this);
2654 out << ", ";
2655 symbolNode->traverse(this);
2656 out << " = t" + str(mUniqueIndex);
2657
2658 mUniqueIndex++;
2659 return true;
2660 }
2661
2662 return false;
2663}
2664
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002665bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2666{
2667 // We support writing constant unions and constructors that only take constant unions as
2668 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002669 return expression->getAsConstantUnion() ||
2670 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002671}
2672
2673bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2674 TIntermSymbol *symbolNode,
2675 TIntermTyped *expression)
2676{
2677 if (canWriteAsHLSLLiteral(expression))
2678 {
2679 symbolNode->traverse(this);
2680 if (expression->getType().isArray())
2681 {
2682 out << "[" << expression->getType().getArraySize() << "]";
2683 }
2684 out << " = {";
2685 if (expression->getAsConstantUnion())
2686 {
2687 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2688 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2689 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2690 }
2691 else
2692 {
2693 TIntermAggregate *constructor = expression->getAsAggregate();
2694 ASSERT(constructor != nullptr);
2695 for (TIntermNode *&node : *constructor->getSequence())
2696 {
2697 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2698 ASSERT(nodeConst);
2699 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2700 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2701 if (node != constructor->getSequence()->back())
2702 {
2703 out << ", ";
2704 }
2705 }
2706 }
2707 out << "}";
2708 return true;
2709 }
2710 return false;
2711}
2712
Jamie Madill55e79e02015-02-09 15:35:00 -05002713TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2714{
2715 const TFieldList &fields = structure.fields();
2716
2717 for (const auto &eqFunction : mStructEqualityFunctions)
2718 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002719 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002720 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002721 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002722 }
2723 }
2724
2725 const TString &structNameString = StructNameString(structure);
2726
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002727 StructEqualityFunction *function = new StructEqualityFunction();
2728 function->structure = &structure;
2729 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002730
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002731 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002732
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002733 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2734 << "{\n"
2735 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002736
2737 for (size_t i = 0; i < fields.size(); i++)
2738 {
2739 const TField *field = fields[i];
2740 const TType *fieldType = field->type();
2741
2742 const TString &fieldNameA = "a." + Decorate(field->name());
2743 const TString &fieldNameB = "b." + Decorate(field->name());
2744
2745 if (i > 0)
2746 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002747 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002748 }
2749
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002750 fnOut << "(";
2751 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2752 fnOut << fieldNameA;
2753 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2754 fnOut << fieldNameB;
2755 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2756 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002757 }
2758
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002759 fnOut << ";\n" << "}\n";
2760
2761 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002762
2763 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002764 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002765
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002766 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002767}
2768
Olli Etuaho7fb49552015-03-18 17:27:44 +02002769TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2770{
2771 for (const auto &eqFunction : mArrayEqualityFunctions)
2772 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002773 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002774 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002775 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002776 }
2777 }
2778
2779 const TString &typeName = TypeString(type);
2780
Olli Etuaho12690762015-03-31 12:55:28 +03002781 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002782 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002783
2784 TInfoSinkBase fnNameOut;
2785 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002786 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002787
2788 TType nonArrayType = type;
2789 nonArrayType.clearArrayness();
2790
2791 TInfoSinkBase fnOut;
2792
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002793 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002794 << typeName << " a[" << type.getArraySize() << "], "
2795 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002796 << "{\n"
2797 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2798 " {\n"
2799 " if (";
2800
2801 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2802 fnOut << "a[i]";
2803 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2804 fnOut << "b[i]";
2805 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2806
2807 fnOut << ") { return false; }\n"
2808 " }\n"
2809 " return true;\n"
2810 "}\n";
2811
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002812 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002813
2814 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002815 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002816
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002817 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002818}
2819
Olli Etuaho12690762015-03-31 12:55:28 +03002820TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2821{
2822 for (const auto &assignFunction : mArrayAssignmentFunctions)
2823 {
2824 if (assignFunction.type == type)
2825 {
2826 return assignFunction.functionName;
2827 }
2828 }
2829
2830 const TString &typeName = TypeString(type);
2831
2832 ArrayHelperFunction function;
2833 function.type = type;
2834
2835 TInfoSinkBase fnNameOut;
2836 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2837 function.functionName = fnNameOut.c_str();
2838
2839 TInfoSinkBase fnOut;
2840
2841 fnOut << "void " << function.functionName << "(out "
2842 << typeName << " a[" << type.getArraySize() << "], "
2843 << typeName << " b[" << type.getArraySize() << "])\n"
2844 << "{\n"
2845 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2846 " {\n"
2847 " a[i] = b[i];\n"
2848 " }\n"
2849 "}\n";
2850
2851 function.functionDefinition = fnOut.c_str();
2852
2853 mArrayAssignmentFunctions.push_back(function);
2854
2855 return function.functionName;
2856}
2857
Olli Etuaho9638c352015-04-01 14:34:52 +03002858TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2859{
2860 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2861 {
2862 if (constructIntoFunction.type == type)
2863 {
2864 return constructIntoFunction.functionName;
2865 }
2866 }
2867
2868 const TString &typeName = TypeString(type);
2869
2870 ArrayHelperFunction function;
2871 function.type = type;
2872
2873 TInfoSinkBase fnNameOut;
2874 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2875 function.functionName = fnNameOut.c_str();
2876
2877 TInfoSinkBase fnOut;
2878
2879 fnOut << "void " << function.functionName << "(out "
2880 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002881 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002882 {
2883 fnOut << ", " << typeName << " b" << i;
2884 }
2885 fnOut << ")\n"
2886 "{\n";
2887
Olli Etuaho856c4972016-08-08 11:38:39 +03002888 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002889 {
2890 fnOut << " a[" << i << "] = b" << i << ";\n";
2891 }
2892 fnOut << "}\n";
2893
2894 function.functionDefinition = fnOut.c_str();
2895
2896 mArrayConstructIntoFunctions.push_back(function);
2897
2898 return function.functionName;
2899}
2900
Jamie Madill2e295e22015-04-29 10:41:33 -04002901void OutputHLSL::ensureStructDefined(const TType &type)
2902{
2903 TStructure *structure = type.getStruct();
2904
2905 if (structure)
2906 {
2907 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2908 }
2909}
2910
2911
Olli Etuaho9638c352015-04-01 14:34:52 +03002912
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002913}