blob: 98f74376c838e81db2fbef76085e163963e934e0 [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 Etuaho6f6c5582016-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 Etuaho6f6c5582016-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 Etuaho6f6c5582016-04-08 13:08:46 +0300143 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200144 for (auto &eqFunction : mStructEqualityFunctions)
145 {
146 SafeDelete(eqFunction);
147 }
148 for (auto &eqFunction : mArrayEqualityFunctions)
149 {
150 SafeDelete(eqFunction);
151 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000152}
153
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200154void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000155{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200156 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400157 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000158
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200159 BuiltInFunctionEmulator builtInFunctionEmulator;
160 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200161 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500162
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700163 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700164 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
165 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300166 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700167 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700168
Jamie Madill37997142015-01-28 10:06:34 -0500169 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500170 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200171 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500172 mInfoSinkStack.pop();
173
Jamie Madill37997142015-01-28 10:06:34 -0500174 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500175 mInfoSinkStack.pop();
176
Jamie Madill32aab012015-01-27 14:12:26 -0500177 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500178 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500179 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000180
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200181 objSink << mHeader.c_str();
182 objSink << mBody.c_str();
183 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200184
185 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000186}
187
Jamie Madill570e04d2013-06-21 09:15:33 -0400188void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
189{
190 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
191 {
192 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
193
Jamie Madill32aab012015-01-27 14:12:26 -0500194 TInfoSinkBase structInfoSink;
195 mInfoSinkStack.push(&structInfoSink);
196
Jamie Madill570e04d2013-06-21 09:15:33 -0400197 // This will mark the necessary block elements as referenced
198 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500199
200 TString structName(structInfoSink.c_str());
201 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400202
203 mFlaggedStructOriginalNames[flaggedNode] = structName;
204
205 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
206 {
207 structName.erase(pos, 1);
208 }
209
210 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
211 }
212}
213
Jamie Madill4e1fd412014-07-10 17:50:10 -0400214const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
215{
216 return mUniformHLSL->getInterfaceBlockRegisterMap();
217}
218
Jamie Madill9fe25e92014-07-18 10:33:08 -0400219const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
220{
221 return mUniformHLSL->getUniformRegisterMap();
222}
223
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000224int OutputHLSL::vectorSize(const TType &type) const
225{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000226 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000227 int arraySize = type.isArray() ? type.getArraySize() : 1;
228
229 return elementSize * arraySize;
230}
231
Jamie Madill98493dd2013-07-08 14:39:03 -0400232TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400233{
234 TString init;
235
236 TString preIndentString;
237 TString fullIndentString;
238
239 for (int spaces = 0; spaces < (indent * 4); spaces++)
240 {
241 preIndentString += ' ';
242 }
243
244 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
245 {
246 fullIndentString += ' ';
247 }
248
249 init += preIndentString + "{\n";
250
Jamie Madill98493dd2013-07-08 14:39:03 -0400251 const TFieldList &fields = structure.fields();
252 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400253 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400254 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400255 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400256 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400257
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400259 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400261 }
262 else
263 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400265 }
266 }
267
268 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
269
270 return init;
271}
272
Jamie Madill8c46ab12015-12-07 16:39:19 -0500273void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000274{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000275 TString varyings;
276 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400277 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000278
Jamie Madill829f59e2013-11-13 19:40:54 -0500279 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 {
281 TIntermTyped *structNode = flaggedStructIt->first;
282 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400283 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 const TString &originalName = mFlaggedStructOriginalNames[structNode];
285
Jamie Madill033dae62014-06-18 12:56:28 -0400286 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400287 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 flaggedStructs += "\n";
289 }
290
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000291 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
292 {
293 const TType &type = varying->second->getType();
294 const TString &name = varying->second->getSymbol();
295
296 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400297 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
298 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000299 }
300
301 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
302 {
303 const TType &type = attribute->second->getType();
304 const TString &name = attribute->second->getSymbol();
305
Jamie Madill033dae62014-06-18 12:56:28 -0400306 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000307 }
308
Jamie Madill8daaba12014-06-13 10:04:33 -0400309 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400310
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200311 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400312 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
313
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200314 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500315 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200316 out << "\n// Equality functions\n\n";
317 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500318 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200319 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200320 }
321 }
Olli Etuaho12690762015-03-31 12:55:28 +0300322 if (!mArrayAssignmentFunctions.empty())
323 {
324 out << "\n// Assignment functions\n\n";
325 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
326 {
327 out << assignmentFunction.functionDefinition << "\n";
328 }
329 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300330 if (!mArrayConstructIntoFunctions.empty())
331 {
332 out << "\n// Array constructor functions\n\n";
333 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
334 {
335 out << constructIntoFunction.functionDefinition << "\n";
336 }
337 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200338
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500339 if (mUsesDiscardRewriting)
340 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400341 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500342 }
343
Nicolas Capens655fe362014-04-11 13:12:34 -0400344 if (mUsesNestedBreak)
345 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400346 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400347 }
348
Arun Patole44efa0b2015-03-04 17:11:05 +0530349 if (mRequiresIEEEStrictCompiling)
350 {
351 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
352 }
353
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400354 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
355 "#define LOOP [loop]\n"
356 "#define FLATTEN [flatten]\n"
357 "#else\n"
358 "#define LOOP\n"
359 "#define FLATTEN\n"
360 "#endif\n";
361
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200362 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200364 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
365 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000366
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000367 out << "// Varyings\n";
368 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400369 out << "\n";
370
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200371 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000372 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500373 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000374 {
Jamie Madill46131a32013-06-20 11:55:50 -0400375 const TString &variableName = outputVariableIt->first;
376 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400377
Jamie Madill033dae62014-06-18 12:56:28 -0400378 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400379 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000380 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000381 }
Jamie Madill46131a32013-06-20 11:55:50 -0400382 else
383 {
384 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
385
386 out << "static float4 gl_Color[" << numColorValues << "] =\n"
387 "{\n";
388 for (unsigned int i = 0; i < numColorValues; i++)
389 {
390 out << " float4(0, 0, 0, 0)";
391 if (i + 1 != numColorValues)
392 {
393 out << ",";
394 }
395 out << "\n";
396 }
397
398 out << "};\n";
399 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000400
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400401 if (mUsesFragDepth)
402 {
403 out << "static float gl_Depth = 0.0;\n";
404 }
405
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000406 if (mUsesFragCoord)
407 {
408 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
409 }
410
411 if (mUsesPointCoord)
412 {
413 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
414 }
415
416 if (mUsesFrontFacing)
417 {
418 out << "static bool gl_FrontFacing = false;\n";
419 }
420
421 out << "\n";
422
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000423 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000424 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000425 out << "struct gl_DepthRangeParameters\n"
426 "{\n"
427 " float near;\n"
428 " float far;\n"
429 " float diff;\n"
430 "};\n"
431 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000432 }
433
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200434 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000435 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000436 out << "cbuffer DriverConstants : register(b1)\n"
437 "{\n";
438
439 if (mUsesDepthRange)
440 {
441 out << " float3 dx_DepthRange : packoffset(c0);\n";
442 }
443
444 if (mUsesFragCoord)
445 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000446 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000447 }
448
449 if (mUsesFragCoord || mUsesFrontFacing)
450 {
451 out << " float3 dx_DepthFront : packoffset(c2);\n";
452 }
453
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800454 if (mUsesFragCoord)
455 {
456 // dx_ViewScale is only used in the fragment shader to correct
457 // the value for glFragCoord if necessary
458 out << " float2 dx_ViewScale : packoffset(c3);\n";
459 }
460
Olli Etuaho618bebc2016-01-15 16:40:00 +0200461 if (mOutputType == SH_HLSL_4_1_OUTPUT)
462 {
463 mUniformHLSL->samplerMetadataUniforms(out, "c4");
464 }
465
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000466 out << "};\n";
467 }
468 else
469 {
470 if (mUsesDepthRange)
471 {
472 out << "uniform float3 dx_DepthRange : register(c0);";
473 }
474
475 if (mUsesFragCoord)
476 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000477 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000478 }
479
480 if (mUsesFragCoord || mUsesFrontFacing)
481 {
482 out << "uniform float3 dx_DepthFront : register(c2);\n";
483 }
484 }
485
486 out << "\n";
487
488 if (mUsesDepthRange)
489 {
490 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
491 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000492 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000493
Jamie Madillf91ce812014-06-13 10:04:34 -0400494 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000495 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400496 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000497 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400498 out << flaggedStructs;
499 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000500 }
501
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000502 if (usingMRTExtension && mNumRenderTargets > 1)
503 {
504 out << "#define GL_USES_MRT\n";
505 }
506
507 if (mUsesFragColor)
508 {
509 out << "#define GL_USES_FRAG_COLOR\n";
510 }
511
512 if (mUsesFragData)
513 {
514 out << "#define GL_USES_FRAG_DATA\n";
515 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000517 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000519 out << "// Attributes\n";
520 out << attributes;
521 out << "\n"
522 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400523
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000524 if (mUsesPointSize)
525 {
526 out << "static float gl_PointSize = float(1);\n";
527 }
528
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000529 if (mUsesInstanceID)
530 {
531 out << "static int gl_InstanceID;";
532 }
533
Corentin Wallezb076add2016-01-11 16:45:46 -0500534 if (mUsesVertexID)
535 {
536 out << "static int gl_VertexID;";
537 }
538
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000539 out << "\n"
540 "// Varyings\n";
541 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000542 out << "\n";
543
544 if (mUsesDepthRange)
545 {
546 out << "struct gl_DepthRangeParameters\n"
547 "{\n"
548 " float near;\n"
549 " float far;\n"
550 " float diff;\n"
551 "};\n"
552 "\n";
553 }
554
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200555 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000556 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800557 out << "cbuffer DriverConstants : register(b1)\n"
558 "{\n";
559
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000560 if (mUsesDepthRange)
561 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800562 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000563 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800564
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800565 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
566 // shaders. However, we declare it for all shaders (including Feature Level 10+).
567 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
568 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800569 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800570 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800571 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800572
Olli Etuaho618bebc2016-01-15 16:40:00 +0200573 if (mOutputType == SH_HLSL_4_1_OUTPUT)
574 {
575 mUniformHLSL->samplerMetadataUniforms(out, "c4");
576 }
577
Austin Kinross4fd18b12014-12-22 12:32:05 -0800578 out << "};\n"
579 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000580 }
581 else
582 {
583 if (mUsesDepthRange)
584 {
585 out << "uniform float3 dx_DepthRange : register(c0);\n";
586 }
587
Cooper Partine6664f02015-01-09 16:22:24 -0800588 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
589 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000590 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000591 }
592
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000593 if (mUsesDepthRange)
594 {
595 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
596 "\n";
597 }
598
Jamie Madillf91ce812014-06-13 10:04:34 -0400599 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000600 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400601 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000602 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400603 out << flaggedStructs;
604 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000605 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400606 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000607
Olli Etuaho6f6c5582016-04-08 13:08:46 +0300608 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000610 if (mUsesFragCoord)
611 {
612 out << "#define GL_USES_FRAG_COORD\n";
613 }
614
615 if (mUsesPointCoord)
616 {
617 out << "#define GL_USES_POINT_COORD\n";
618 }
619
620 if (mUsesFrontFacing)
621 {
622 out << "#define GL_USES_FRONT_FACING\n";
623 }
624
625 if (mUsesPointSize)
626 {
627 out << "#define GL_USES_POINT_SIZE\n";
628 }
629
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400630 if (mUsesFragDepth)
631 {
632 out << "#define GL_USES_FRAG_DEPTH\n";
633 }
634
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000635 if (mUsesDepthRange)
636 {
637 out << "#define GL_USES_DEPTH_RANGE\n";
638 }
639
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000640 if (mUsesXor)
641 {
642 out << "bool xor(bool p, bool q)\n"
643 "{\n"
644 " return (p || q) && !(p && q);\n"
645 "}\n"
646 "\n";
647 }
648
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200649 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650}
651
652void OutputHLSL::visitSymbol(TIntermSymbol *node)
653{
Jamie Madill32aab012015-01-27 14:12:26 -0500654 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655
Jamie Madill570e04d2013-06-21 09:15:33 -0400656 // Handle accessing std140 structs by value
657 if (mFlaggedStructMappedNames.count(node) > 0)
658 {
659 out << mFlaggedStructMappedNames[node];
660 return;
661 }
662
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663 TString name = node->getSymbol();
664
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000665 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000666 {
667 mUsesDepthRange = true;
668 out << name;
669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670 else
671 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000672 TQualifier qualifier = node->getQualifier();
673
674 if (qualifier == EvqUniform)
675 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400676 const TType &nodeType = node->getType();
677 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400678
679 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000680 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400681 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000682 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000683 else
684 {
685 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000686 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400687
Jamie Madill2e295e22015-04-29 10:41:33 -0400688 ensureStructDefined(nodeType);
689
Olli Etuaho96963162016-03-21 11:54:33 +0200690 const TName &nameWithMetadata = node->getName();
691 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000692 }
Jamie Madill19571812013-08-12 15:26:34 -0700693 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000694 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000695 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400696 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000697 }
Jamie Madill033dae62014-06-18 12:56:28 -0400698 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000699 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000700 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400701 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000702 }
Jamie Madill19571812013-08-12 15:26:34 -0700703 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400704 {
705 mReferencedOutputVariables[name] = node;
706 out << "out_" << name;
707 }
708 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000709 {
710 out << "gl_Color[0]";
711 mUsesFragColor = true;
712 }
713 else if (qualifier == EvqFragData)
714 {
715 out << "gl_Color";
716 mUsesFragData = true;
717 }
718 else if (qualifier == EvqFragCoord)
719 {
720 mUsesFragCoord = true;
721 out << name;
722 }
723 else if (qualifier == EvqPointCoord)
724 {
725 mUsesPointCoord = true;
726 out << name;
727 }
728 else if (qualifier == EvqFrontFacing)
729 {
730 mUsesFrontFacing = true;
731 out << name;
732 }
733 else if (qualifier == EvqPointSize)
734 {
735 mUsesPointSize = true;
736 out << name;
737 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000738 else if (qualifier == EvqInstanceID)
739 {
740 mUsesInstanceID = true;
741 out << name;
742 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500743 else if (qualifier == EvqVertexID)
744 {
745 mUsesVertexID = true;
746 out << name;
747 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300748 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400749 {
750 mUsesFragDepth = true;
751 out << "gl_Depth";
752 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000753 else
754 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300755 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000756 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000757 }
758}
759
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400760void OutputHLSL::visitRaw(TIntermRaw *node)
761{
Jamie Madill32aab012015-01-27 14:12:26 -0500762 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400763}
764
Olli Etuaho7fb49552015-03-18 17:27:44 +0200765void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
766{
767 if (type.isScalar() && !type.isArray())
768 {
769 if (op == EOpEqual)
770 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500771 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200772 }
773 else
774 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500775 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200776 }
777 }
778 else
779 {
780 if (visit == PreVisit && op == EOpNotEqual)
781 {
782 out << "!";
783 }
784
785 if (type.isArray())
786 {
787 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500788 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200789 }
790 else if (type.getBasicType() == EbtStruct)
791 {
792 const TStructure &structure = *type.getStruct();
793 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500794 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200795 }
796 else
797 {
798 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500799 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200800 }
801 }
802}
803
Olli Etuaho96963162016-03-21 11:54:33 +0200804bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
805{
806 // Inside InVisit the current node is already in the path.
807 const unsigned int initialN = visit == InVisit ? 1u : 0u;
808 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
809 {
810 TIntermNode *ancestor = getAncestorNode(n);
811 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
812 if (ancestorBinary == nullptr)
813 {
814 return false;
815 }
816 switch (ancestorBinary->getOp())
817 {
818 case EOpIndexDirectStruct:
819 {
820 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
821 const TIntermConstantUnion *index =
822 ancestorBinary->getRight()->getAsConstantUnion();
823 const TField *field = structure->fields()[index->getIConst(0)];
824 if (IsSampler(field->type()->getBasicType()))
825 {
826 return true;
827 }
828 break;
829 }
830 case EOpIndexDirect:
831 break;
832 default:
833 // Returning a sampler from indirect indexing is not supported.
834 return false;
835 }
836 }
837 return false;
838}
839
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
841{
Jamie Madill32aab012015-01-27 14:12:26 -0500842 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000843
Jamie Madill570e04d2013-06-21 09:15:33 -0400844 // Handle accessing std140 structs by value
845 if (mFlaggedStructMappedNames.count(node) > 0)
846 {
847 out << mFlaggedStructMappedNames[node];
848 return false;
849 }
850
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851 switch (node->getOp())
852 {
Olli Etuahoe79904c2015-03-18 16:56:42 +0200853 case EOpAssign:
854 if (node->getLeft()->isArray())
855 {
Olli Etuaho9638c352015-04-01 14:34:52 +0300856 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
857 if (rightAgg != nullptr && rightAgg->isConstructor())
858 {
859 const TString &functionName = addArrayConstructIntoFunction(node->getType());
860 out << functionName << "(";
861 node->getLeft()->traverse(this);
862 TIntermSequence *seq = rightAgg->getSequence();
863 for (auto &arrayElement : *seq)
864 {
865 out << ", ";
866 arrayElement->traverse(this);
867 }
868 out << ")";
869 return false;
870 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +0300871 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
872 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
873
874 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500875 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200876 }
877 else
878 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500879 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200880 }
881 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000882 case EOpInitialize:
883 if (visit == PreVisit)
884 {
885 // GLSL allows to write things like "float x = x;" where a new variable x is defined
886 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
887 // new variable is created before the assignment is evaluated), so we need to convert
888 // this to "float t = x, x = t;".
889
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000890 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -0500891 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000892 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000893
Olli Etuaho3d932d82016-04-12 11:10:30 +0300894 // Global initializers must be constant at this point.
895 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
896 (expression->getQualifier() == EvqConst &&
897 expression->getAsConstantUnion() != nullptr));
898 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 Etuahoa6f22092015-05-08 18:31:10 +03001459 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
1460 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03001461 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001462 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001463 }
1464
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001465 if (mInsideFunction)
1466 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001467 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001468 out << "}\n";
1469 }
1470
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001471 return false;
1472 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001473 case EOpDeclaration:
1474 if (visit == PreVisit)
1475 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001476 TIntermSequence *sequence = node->getSequence();
1477 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03001478 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001479
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001480 if (variable &&
1481 (variable->getQualifier() == EvqTemporary ||
1482 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001483 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001484 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00001485
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001486 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001487 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001488 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001489 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03001490 out << "static ";
1491 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04001492
Olli Etuahoa6f22092015-05-08 18:31:10 +03001493 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04001494
Olli Etuahoa6f22092015-05-08 18:31:10 +03001495 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04001496
Olli Etuahoa6f22092015-05-08 18:31:10 +03001497 if (symbol)
1498 {
1499 symbol->traverse(this);
1500 out << ArrayString(symbol->getType());
1501 out << " = " + initializer(symbol->getType());
1502 }
1503 else
1504 {
1505 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001506 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001507 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001508 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1509 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00001510 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001511 }
1512 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001513 }
Jamie Madill033dae62014-06-18 12:56:28 -04001514 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001515 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001516 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001517 {
1518 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1519
1520 if (symbol)
1521 {
1522 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
1523 mReferencedVaryings[symbol->getSymbol()] = symbol;
1524 }
1525 else
1526 {
1527 (*sit)->traverse(this);
1528 }
1529 }
1530 }
1531
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001532 return false;
1533 }
1534 else if (visit == InVisit)
1535 {
1536 out << ", ";
1537 }
1538 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001539 case EOpInvariantDeclaration:
1540 // Do not do any translation
1541 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001542 case EOpPrototype:
1543 if (visit == PreVisit)
1544 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001545 size_t index = mCallDag.findIndex(node);
1546 // Skip the prototype if it is not implemented (and thus not used)
1547 if (index == CallDAG::InvalidIndex)
1548 {
1549 return false;
1550 }
1551
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001552 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001553
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001554 TString name = DecorateFunctionIfNeeded(node->getNameObj());
1555 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1556 << (mOutputLod0Function ? "Lod0(" : "(");
1557
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001558 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001559 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001560 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001561
1562 if (symbol)
1563 {
1564 out << argumentString(symbol);
1565
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001566 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001567 {
1568 out << ", ";
1569 }
1570 }
1571 else UNREACHABLE();
1572 }
1573
1574 out << ");\n";
1575
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001576 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07001577 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1578 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00001579 {
1580 mOutputLod0Function = true;
1581 node->traverse(this);
1582 mOutputLod0Function = false;
1583 }
1584
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001585 return false;
1586 }
1587 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001588 case EOpComma:
1589 outputTriplet(out, visit, "(", ", ", ")");
1590 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001591 case EOpFunction:
1592 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001593 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03001594 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001595
Corentin Wallez1239ee92015-03-19 14:38:02 -07001596 size_t index = mCallDag.findIndex(node);
1597 ASSERT(index != CallDAG::InvalidIndex);
1598 mCurrentFunctionMetadata = &mASTMetadataList[index];
1599
Jamie Madill033dae62014-06-18 12:56:28 -04001600 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001601
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001602 TIntermSequence *sequence = node->getSequence();
1603 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
1604
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001605 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001606 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001607 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001608 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001609 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001610 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001611 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001612 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001613 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001614
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001615 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001616 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001617 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001618
1619 if (symbol)
1620 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001621 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001622
1623 out << argumentString(symbol);
1624
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001625 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001626 {
1627 out << ", ";
1628 }
1629 }
1630 else UNREACHABLE();
1631 }
1632
Olli Etuaho4785fec2015-05-18 16:09:37 +03001633 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001634
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001635 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001636 {
1637 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03001638 TIntermNode *body = (*sequence)[1];
1639 // The function body node will output braces.
1640 ASSERT(IsSequence(body));
1641 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00001642 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001643 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001644 else
1645 {
1646 out << "{}\n";
1647 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001648
Corentin Wallez1239ee92015-03-19 14:38:02 -07001649 mCurrentFunctionMetadata = nullptr;
1650
1651 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1652 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001653 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001654 ASSERT(name != "main");
1655 mOutputLod0Function = true;
1656 node->traverse(this);
1657 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001658 }
1659
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001660 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001661 }
1662 break;
1663 case EOpFunctionCall:
1664 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001665 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001666
Corentin Wallez1239ee92015-03-19 14:38:02 -07001667 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001668 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001670 if (node->isArray())
1671 {
1672 UNIMPLEMENTED();
1673 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07001674 size_t index = mCallDag.findIndex(node);
1675 ASSERT(index != CallDAG::InvalidIndex);
1676 lod0 &= mASTMetadataList[index].mNeedsLod0;
1677
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001678 out << DecorateFunctionIfNeeded(node->getNameObj());
1679 out << DisambiguateFunctionName(node->getSequence());
1680 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681 }
1682 else
1683 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03001684 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001685 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho6f6c5582016-04-08 13:08:46 +03001686 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1687 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1688 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1689 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001690 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001691
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001692 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001693 {
Olli Etuaho96963162016-03-21 11:54:33 +02001694 TIntermTyped *typedArg = (*arg)->getAsTyped();
1695 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001696 {
1697 out << "texture_";
1698 (*arg)->traverse(this);
1699 out << ", sampler_";
1700 }
1701
1702 (*arg)->traverse(this);
1703
Olli Etuaho96963162016-03-21 11:54:33 +02001704 if (typedArg->getType().isStructureContainingSamplers())
1705 {
1706 const TType &argType = typedArg->getType();
1707 TVector<TIntermSymbol *> samplerSymbols;
1708 TString structName = samplerNamePrefixFromStruct(typedArg);
1709 argType.createSamplerSymbols("angle_" + structName, "",
1710 argType.isArray() ? argType.getArraySize() : 0,
1711 &samplerSymbols, nullptr);
1712 for (const TIntermSymbol *sampler : samplerSymbols)
1713 {
1714 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1715 {
1716 out << ", texture_" << sampler->getSymbol();
1717 out << ", sampler_" << sampler->getSymbol();
1718 }
1719 else
1720 {
1721 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1722 // of D3D9, it's the sampler variable.
1723 out << ", " + sampler->getSymbol();
1724 }
1725 }
1726 }
1727
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001728 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001729 {
1730 out << ", ";
1731 }
1732 }
1733
1734 out << ")";
1735
1736 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001737 }
1738 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001739 case EOpParameters:
1740 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1741 break;
1742 case EOpConstructFloat:
1743 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1744 break;
1745 case EOpConstructVec2:
1746 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1747 break;
1748 case EOpConstructVec3:
1749 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1750 break;
1751 case EOpConstructVec4:
1752 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1753 break;
1754 case EOpConstructBool:
1755 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1756 break;
1757 case EOpConstructBVec2:
1758 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1759 break;
1760 case EOpConstructBVec3:
1761 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1762 break;
1763 case EOpConstructBVec4:
1764 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1765 break;
1766 case EOpConstructInt:
1767 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1768 break;
1769 case EOpConstructIVec2:
1770 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1771 break;
1772 case EOpConstructIVec3:
1773 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1774 break;
1775 case EOpConstructIVec4:
1776 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1777 break;
1778 case EOpConstructUInt:
1779 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1780 break;
1781 case EOpConstructUVec2:
1782 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1783 break;
1784 case EOpConstructUVec3:
1785 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1786 break;
1787 case EOpConstructUVec4:
1788 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1789 break;
1790 case EOpConstructMat2:
1791 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1792 break;
1793 case EOpConstructMat2x3:
1794 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1795 break;
1796 case EOpConstructMat2x4:
1797 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1798 break;
1799 case EOpConstructMat3x2:
1800 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1801 break;
1802 case EOpConstructMat3:
1803 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1804 break;
1805 case EOpConstructMat3x4:
1806 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1807 break;
1808 case EOpConstructMat4x2:
1809 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1810 break;
1811 case EOpConstructMat4x3:
1812 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1813 break;
1814 case EOpConstructMat4:
1815 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1816 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001817 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001818 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001819 if (node->getType().isArray())
1820 {
1821 UNIMPLEMENTED();
1822 }
Jamie Madill033dae62014-06-18 12:56:28 -04001823 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001824 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001825 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001826 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001827 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001828 case EOpLessThan:
1829 outputTriplet(out, visit, "(", " < ", ")");
1830 break;
1831 case EOpGreaterThan:
1832 outputTriplet(out, visit, "(", " > ", ")");
1833 break;
1834 case EOpLessThanEqual:
1835 outputTriplet(out, visit, "(", " <= ", ")");
1836 break;
1837 case EOpGreaterThanEqual:
1838 outputTriplet(out, visit, "(", " >= ", ")");
1839 break;
1840 case EOpVectorEqual:
1841 outputTriplet(out, visit, "(", " == ", ")");
1842 break;
1843 case EOpVectorNotEqual:
1844 outputTriplet(out, visit, "(", " != ", ")");
1845 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001846 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001847 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001848 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001849 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001850 case EOpModf:
1851 outputTriplet(out, visit, "modf(", ", ", ")");
1852 break;
1853 case EOpPow:
1854 outputTriplet(out, visit, "pow(", ", ", ")");
1855 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001857 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02001858 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001859 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001861 case EOpMin:
1862 outputTriplet(out, visit, "min(", ", ", ")");
1863 break;
1864 case EOpMax:
1865 outputTriplet(out, visit, "max(", ", ", ")");
1866 break;
1867 case EOpClamp:
1868 outputTriplet(out, visit, "clamp(", ", ", ")");
1869 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301870 case EOpMix:
1871 {
1872 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1873 if (lastParamNode->getType().getBasicType() == EbtBool)
1874 {
1875 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1876 // so use emulated version.
1877 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001878 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301879 }
1880 else
1881 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001882 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301883 }
1884 }
1885 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001886 case EOpStep:
1887 outputTriplet(out, visit, "step(", ", ", ")");
1888 break;
1889 case EOpSmoothStep:
1890 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1891 break;
1892 case EOpDistance:
1893 outputTriplet(out, visit, "distance(", ", ", ")");
1894 break;
1895 case EOpDot:
1896 outputTriplet(out, visit, "dot(", ", ", ")");
1897 break;
1898 case EOpCross:
1899 outputTriplet(out, visit, "cross(", ", ", ")");
1900 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001901 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02001902 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001903 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00001904 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001905 case EOpReflect:
1906 outputTriplet(out, visit, "reflect(", ", ", ")");
1907 break;
1908 case EOpRefract:
1909 outputTriplet(out, visit, "refract(", ", ", ")");
1910 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001911 case EOpOuterProduct:
1912 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001913 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02001914 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001915 case EOpMul:
1916 outputTriplet(out, visit, "(", " * ", ")");
1917 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918 default: UNREACHABLE();
1919 }
1920
1921 return true;
1922}
1923
Jamie Madill8c46ab12015-12-07 16:39:19 -05001924void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001926 out << "if (";
1927
1928 node->getCondition()->traverse(this);
1929
1930 out << ")\n";
1931
Jamie Madill8c46ab12015-12-07 16:39:19 -05001932 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001933
1934 bool discard = false;
1935
1936 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001937 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001938 // The trueBlock child node will output braces.
1939 ASSERT(IsSequence(node->getTrueBlock()));
1940
Olli Etuahoa6f22092015-05-08 18:31:10 +03001941 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001942
Olli Etuahoa6f22092015-05-08 18:31:10 +03001943 // Detect true discard
1944 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1945 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001946 else
1947 {
1948 // TODO(oetuaho): Check if the semicolon inside is necessary.
1949 // It's there as a result of conservative refactoring of the output.
1950 out << "{;}\n";
1951 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001952
Jamie Madill8c46ab12015-12-07 16:39:19 -05001953 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001954
Olli Etuahoa6f22092015-05-08 18:31:10 +03001955 if (node->getFalseBlock())
1956 {
1957 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001958
Jamie Madill8c46ab12015-12-07 16:39:19 -05001959 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960
Olli Etuaho4785fec2015-05-18 16:09:37 +03001961 // Either this is "else if" or the falseBlock child node will output braces.
1962 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
1963
Olli Etuahoa6f22092015-05-08 18:31:10 +03001964 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001965
Jamie Madill8c46ab12015-12-07 16:39:19 -05001966 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001967
Olli Etuahoa6f22092015-05-08 18:31:10 +03001968 // Detect false discard
1969 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1970 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001971
Olli Etuahoa6f22092015-05-08 18:31:10 +03001972 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001973 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001974 {
1975 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001976 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001977}
1978
1979bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
1980{
1981 TInfoSinkBase &out = getInfoSink();
1982
1983 ASSERT(!node->usesTernaryOperator());
Olli Etuaho3d932d82016-04-12 11:10:30 +03001984 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001985
1986 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001987 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001988 {
1989 out << "FLATTEN ";
1990 }
1991
Jamie Madill8c46ab12015-12-07 16:39:19 -05001992 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993
1994 return false;
1995}
1996
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001997bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02001998{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001999 TInfoSinkBase &out = getInfoSink();
2000
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002001 if (node->getStatementList())
2002 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002003 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002004 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002005 // The curly braces get written when visiting the statementList aggregate
2006 }
2007 else
2008 {
2009 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002010 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002011 }
2012 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002013}
2014
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002015bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002016{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002017 TInfoSinkBase &out = getInfoSink();
2018
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002019 if (node->hasCondition())
2020 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002021 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002022 return true;
2023 }
2024 else
2025 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002026 out << "default:\n";
2027 return false;
2028 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002029}
2030
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2032{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002033 TInfoSinkBase &out = getInfoSink();
2034 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002035}
2036
2037bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2038{
Nicolas Capens655fe362014-04-11 13:12:34 -04002039 mNestedLoopDepth++;
2040
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002041 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002042 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002043 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002044
Jamie Madill8c46ab12015-12-07 16:39:19 -05002045 TInfoSinkBase &out = getInfoSink();
2046
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002047 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002048 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002049 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002050 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002051 mInsideDiscontinuousLoop = wasDiscontinuous;
2052 mNestedLoopDepth--;
2053
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002054 return false;
2055 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002056 }
2057
Corentin Wallez1239ee92015-03-19 14:38:02 -07002058 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002059 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002061 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002062
Jamie Madill8c46ab12015-12-07 16:39:19 -05002063 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064 }
2065 else
2066 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002067 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002068
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 if (node->getInit())
2070 {
2071 node->getInit()->traverse(this);
2072 }
2073
2074 out << "; ";
2075
alokp@chromium.org52813552010-11-16 18:36:09 +00002076 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002078 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 }
2080
2081 out << "; ";
2082
alokp@chromium.org52813552010-11-16 18:36:09 +00002083 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002085 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086 }
2087
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002088 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002089
Jamie Madill8c46ab12015-12-07 16:39:19 -05002090 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091 }
2092
2093 if (node->getBody())
2094 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002095 // The loop body node will output braces.
2096 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002097 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002099 else
2100 {
2101 // TODO(oetuaho): Check if the semicolon inside is necessary.
2102 // It's there as a result of conservative refactoring of the output.
2103 out << "{;}\n";
2104 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105
Jamie Madill8c46ab12015-12-07 16:39:19 -05002106 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107
alokp@chromium.org52813552010-11-16 18:36:09 +00002108 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002110 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 out << "while(\n";
2112
alokp@chromium.org52813552010-11-16 18:36:09 +00002113 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114
daniel@transgaming.com73536982012-03-21 20:45:49 +00002115 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116 }
2117
daniel@transgaming.com73536982012-03-21 20:45:49 +00002118 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002120 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002121 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002122
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 return false;
2124}
2125
2126bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2127{
Jamie Madill32aab012015-01-27 14:12:26 -05002128 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129
2130 switch (node->getFlowOp())
2131 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002132 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002133 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002134 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002135 case EOpBreak:
2136 if (visit == PreVisit)
2137 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002138 if (mNestedLoopDepth > 1)
2139 {
2140 mUsesNestedBreak = true;
2141 }
2142
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002143 if (mExcessiveLoopIndex)
2144 {
2145 out << "{Break";
2146 mExcessiveLoopIndex->traverse(this);
2147 out << " = true; break;}\n";
2148 }
2149 else
2150 {
2151 out << "break;\n";
2152 }
2153 }
2154 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002155 case EOpContinue:
2156 outputTriplet(out, visit, "continue;\n", "", "");
2157 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 case EOpReturn:
2159 if (visit == PreVisit)
2160 {
2161 if (node->getExpression())
2162 {
2163 out << "return ";
2164 }
2165 else
2166 {
2167 out << "return;\n";
2168 }
2169 }
2170 else if (visit == PostVisit)
2171 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002172 if (node->getExpression())
2173 {
2174 out << ";\n";
2175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176 }
2177 break;
2178 default: UNREACHABLE();
2179 }
2180
2181 return true;
2182}
2183
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002184bool OutputHLSL::isSingleStatement(TIntermNode *node)
2185{
2186 TIntermAggregate *aggregate = node->getAsAggregate();
2187
2188 if (aggregate)
2189 {
2190 if (aggregate->getOp() == EOpSequence)
2191 {
2192 return false;
2193 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002194 else if (aggregate->getOp() == EOpDeclaration)
2195 {
2196 // Declaring multiple comma-separated variables must be considered multiple statements
2197 // because each individual declaration has side effects which are visible in the next.
2198 return false;
2199 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002200 else
2201 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002202 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002203 {
2204 if (!isSingleStatement(*sit))
2205 {
2206 return false;
2207 }
2208 }
2209
2210 return true;
2211 }
2212 }
2213
2214 return true;
2215}
2216
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002217// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2218// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002219bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002220{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002221 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002222
2223 // Parse loops of the form:
2224 // for(int index = initial; index [comparator] limit; index += increment)
2225 TIntermSymbol *index = NULL;
2226 TOperator comparator = EOpNull;
2227 int initial = 0;
2228 int limit = 0;
2229 int increment = 0;
2230
2231 // Parse index name and intial value
2232 if (node->getInit())
2233 {
2234 TIntermAggregate *init = node->getInit()->getAsAggregate();
2235
2236 if (init)
2237 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002238 TIntermSequence *sequence = init->getSequence();
2239 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002240
2241 if (variable && variable->getQualifier() == EvqTemporary)
2242 {
2243 TIntermBinary *assign = variable->getAsBinaryNode();
2244
2245 if (assign->getOp() == EOpInitialize)
2246 {
2247 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2248 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2249
2250 if (symbol && constant)
2251 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002252 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002253 {
2254 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002255 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002256 }
2257 }
2258 }
2259 }
2260 }
2261 }
2262
2263 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002264 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002265 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002266 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002267
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002268 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2269 {
2270 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2271
2272 if (constant)
2273 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002274 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002275 {
2276 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002277 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002278 }
2279 }
2280 }
2281 }
2282
2283 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002284 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002285 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002286 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2287 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002288
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002289 if (binaryTerminal)
2290 {
2291 TOperator op = binaryTerminal->getOp();
2292 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2293
2294 if (constant)
2295 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002296 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002297 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002298 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002299
2300 switch (op)
2301 {
2302 case EOpAddAssign: increment = value; break;
2303 case EOpSubAssign: increment = -value; break;
2304 default: UNIMPLEMENTED();
2305 }
2306 }
2307 }
2308 }
2309 else if (unaryTerminal)
2310 {
2311 TOperator op = unaryTerminal->getOp();
2312
2313 switch (op)
2314 {
2315 case EOpPostIncrement: increment = 1; break;
2316 case EOpPostDecrement: increment = -1; break;
2317 case EOpPreIncrement: increment = 1; break;
2318 case EOpPreDecrement: increment = -1; break;
2319 default: UNIMPLEMENTED();
2320 }
2321 }
2322 }
2323
2324 if (index != NULL && comparator != EOpNull && increment != 0)
2325 {
2326 if (comparator == EOpLessThanEqual)
2327 {
2328 comparator = EOpLessThan;
2329 limit += 1;
2330 }
2331
2332 if (comparator == EOpLessThan)
2333 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002334 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002335
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002336 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002337 {
2338 return false; // Not an excessive loop
2339 }
2340
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002341 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2342 mExcessiveLoopIndex = index;
2343
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002344 out << "{int ";
2345 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002346 out << ";\n"
2347 "bool Break";
2348 index->traverse(this);
2349 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002350
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002351 bool firstLoopFragment = true;
2352
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002353 while (iterations > 0)
2354 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002355 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002356
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002357 if (!firstLoopFragment)
2358 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002359 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002360 index->traverse(this);
2361 out << ") {\n";
2362 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002363
2364 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2365 {
2366 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2367 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002368
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002369 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002370 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002371
Corentin Wallez1239ee92015-03-19 14:38:02 -07002372 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002373 index->traverse(this);
2374 out << " = ";
2375 out << initial;
2376
2377 out << "; ";
2378 index->traverse(this);
2379 out << " < ";
2380 out << clampedLimit;
2381
2382 out << "; ";
2383 index->traverse(this);
2384 out << " += ";
2385 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002386 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002387
Jamie Madill8c46ab12015-12-07 16:39:19 -05002388 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002389 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002390
2391 if (node->getBody())
2392 {
2393 node->getBody()->traverse(this);
2394 }
2395
Jamie Madill8c46ab12015-12-07 16:39:19 -05002396 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002397 out << ";}\n";
2398
2399 if (!firstLoopFragment)
2400 {
2401 out << "}\n";
2402 }
2403
2404 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002405
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002406 initial += MAX_LOOP_ITERATIONS * increment;
2407 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002408 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002409
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002410 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002411
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002412 mExcessiveLoopIndex = restoreIndex;
2413
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002414 return true;
2415 }
2416 else UNIMPLEMENTED();
2417 }
2418
2419 return false; // Not handled as an excessive loop
2420}
2421
Jamie Madill8c46ab12015-12-07 16:39:19 -05002422void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2423 Visit visit,
2424 const char *preString,
2425 const char *inString,
2426 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002428 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 {
2430 out << preString;
2431 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002432 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 {
2434 out << inString;
2435 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002436 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002437 {
2438 out << postString;
2439 }
2440}
2441
Jamie Madill8c46ab12015-12-07 16:39:19 -05002442void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002443{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002444 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002445 {
Jamie Madill32aab012015-01-27 14:12:26 -05002446 out << "\n";
2447 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002448
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002449 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002450 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002451 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002452 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002453
Jamie Madill32aab012015-01-27 14:12:26 -05002454 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002455 }
2456}
2457
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002458TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2459{
2460 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002461 const TType &type = symbol->getType();
2462 const TName &name = symbol->getName();
2463 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002464
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002465 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002466 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002467 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002468 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002469 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002470 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002471 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002472 }
2473
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002474 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002475 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002476 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2477 {
2478 // Samplers are passed as indices to the sampler array.
2479 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2480 return "const uint " + nameStr + ArrayString(type);
2481 }
2482 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2483 {
2484 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2485 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2486 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2487 ArrayString(type);
2488 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002489 }
2490
Olli Etuaho96963162016-03-21 11:54:33 +02002491 TStringStream argString;
2492 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2493 << ArrayString(type);
2494
2495 // If the structure parameter contains samplers, they need to be passed into the function as
2496 // separate parameters. HLSL doesn't natively support samplers in structs.
2497 if (type.isStructureContainingSamplers())
2498 {
2499 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2500 TVector<TIntermSymbol *> samplerSymbols;
2501 type.createSamplerSymbols("angle" + nameStr, "", 0, &samplerSymbols, nullptr);
2502 for (const TIntermSymbol *sampler : samplerSymbols)
2503 {
2504 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2505 {
2506 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2507 }
2508 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2509 {
2510 const TType &samplerType = sampler->getType();
2511 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2512 type.getArraySize() == samplerType.getArraySize());
2513 ASSERT(IsSampler(samplerType.getBasicType()));
2514 argString << ", " << QualifierString(qualifier) << " "
2515 << TextureString(samplerType.getBasicType()) << " texture_"
2516 << sampler->getSymbol() << ArrayString(type) << ", "
2517 << QualifierString(qualifier) << " "
2518 << SamplerString(samplerType.getBasicType()) << " sampler_"
2519 << sampler->getSymbol() << ArrayString(type);
2520 }
2521 else
2522 {
2523 const TType &samplerType = sampler->getType();
2524 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2525 type.getArraySize() == samplerType.getArraySize());
2526 ASSERT(IsSampler(samplerType.getBasicType()));
2527 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2528 << " " << sampler->getSymbol() << ArrayString(type);
2529 }
2530 }
2531 }
2532
2533 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534}
2535
2536TString OutputHLSL::initializer(const TType &type)
2537{
2538 TString string;
2539
Jamie Madill94bf7f22013-07-08 13:31:15 -04002540 size_t size = type.getObjectSize();
2541 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002543 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544
Jamie Madill94bf7f22013-07-08 13:31:15 -04002545 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002546 {
2547 string += ", ";
2548 }
2549 }
2550
daniel@transgaming.comead23042010-04-29 03:35:36 +00002551 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002553
Jamie Madill8c46ab12015-12-07 16:39:19 -05002554void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2555 Visit visit,
2556 const TType &type,
2557 const char *name,
2558 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002559{
Olli Etuahof40319e2015-03-10 14:33:00 +02002560 if (type.isArray())
2561 {
2562 UNIMPLEMENTED();
2563 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002564
2565 if (visit == PreVisit)
2566 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002567 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002568
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002569 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002570 }
2571 else if (visit == InVisit)
2572 {
2573 out << ", ";
2574 }
2575 else if (visit == PostVisit)
2576 {
2577 out << ")";
2578 }
2579}
2580
Jamie Madill8c46ab12015-12-07 16:39:19 -05002581const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2582 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002583 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002584{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002585 const TConstantUnion *constUnionIterated = constUnion;
2586
Jamie Madill98493dd2013-07-08 14:39:03 -04002587 const TStructure* structure = type.getStruct();
2588 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002589 {
Jamie Madill033dae62014-06-18 12:56:28 -04002590 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002591
Jamie Madill98493dd2013-07-08 14:39:03 -04002592 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002593
Jamie Madill98493dd2013-07-08 14:39:03 -04002594 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002595 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002596 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002597 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002598
Jamie Madill98493dd2013-07-08 14:39:03 -04002599 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002600 {
2601 out << ", ";
2602 }
2603 }
2604
2605 out << ")";
2606 }
2607 else
2608 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002609 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002610 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002611
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002612 if (writeType)
2613 {
Jamie Madill033dae62014-06-18 12:56:28 -04002614 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002615 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002616 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002617 if (writeType)
2618 {
2619 out << ")";
2620 }
2621 }
2622
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002623 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002624}
2625
Jamie Madill8c46ab12015-12-07 16:39:19 -05002626void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002627{
2628 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002629 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002630}
2631
Jamie Madill37997142015-01-28 10:06:34 -05002632bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2633{
2634 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2635 expression->traverse(&searchSymbol);
2636
2637 if (searchSymbol.foundMatch())
2638 {
2639 // Type already printed
2640 out << "t" + str(mUniqueIndex) + " = ";
2641 expression->traverse(this);
2642 out << ", ";
2643 symbolNode->traverse(this);
2644 out << " = t" + str(mUniqueIndex);
2645
2646 mUniqueIndex++;
2647 return true;
2648 }
2649
2650 return false;
2651}
2652
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002653bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2654{
2655 // We support writing constant unions and constructors that only take constant unions as
2656 // parameters as HLSL literals.
2657 if (expression->getAsConstantUnion())
2658 {
2659 return true;
2660 }
2661 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
2662 !expression->getAsAggregate()->isConstructor())
2663 {
2664 return false;
2665 }
2666 TIntermAggregate *constructor = expression->getAsAggregate();
2667 for (TIntermNode *&node : *constructor->getSequence())
2668 {
2669 if (!node->getAsConstantUnion())
2670 return false;
2671 }
2672 return true;
2673}
2674
2675bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2676 TIntermSymbol *symbolNode,
2677 TIntermTyped *expression)
2678{
2679 if (canWriteAsHLSLLiteral(expression))
2680 {
2681 symbolNode->traverse(this);
2682 if (expression->getType().isArray())
2683 {
2684 out << "[" << expression->getType().getArraySize() << "]";
2685 }
2686 out << " = {";
2687 if (expression->getAsConstantUnion())
2688 {
2689 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2690 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2691 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2692 }
2693 else
2694 {
2695 TIntermAggregate *constructor = expression->getAsAggregate();
2696 ASSERT(constructor != nullptr);
2697 for (TIntermNode *&node : *constructor->getSequence())
2698 {
2699 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2700 ASSERT(nodeConst);
2701 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2702 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2703 if (node != constructor->getSequence()->back())
2704 {
2705 out << ", ";
2706 }
2707 }
2708 }
2709 out << "}";
2710 return true;
2711 }
2712 return false;
2713}
2714
Jamie Madill55e79e02015-02-09 15:35:00 -05002715TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2716{
2717 const TFieldList &fields = structure.fields();
2718
2719 for (const auto &eqFunction : mStructEqualityFunctions)
2720 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002721 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002722 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002723 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002724 }
2725 }
2726
2727 const TString &structNameString = StructNameString(structure);
2728
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002729 StructEqualityFunction *function = new StructEqualityFunction();
2730 function->structure = &structure;
2731 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002732
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002733 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002734
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002735 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2736 << "{\n"
2737 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002738
2739 for (size_t i = 0; i < fields.size(); i++)
2740 {
2741 const TField *field = fields[i];
2742 const TType *fieldType = field->type();
2743
2744 const TString &fieldNameA = "a." + Decorate(field->name());
2745 const TString &fieldNameB = "b." + Decorate(field->name());
2746
2747 if (i > 0)
2748 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002749 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002750 }
2751
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002752 fnOut << "(";
2753 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2754 fnOut << fieldNameA;
2755 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2756 fnOut << fieldNameB;
2757 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2758 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002759 }
2760
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002761 fnOut << ";\n" << "}\n";
2762
2763 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002764
2765 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002766 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002767
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002768 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002769}
2770
Olli Etuaho7fb49552015-03-18 17:27:44 +02002771TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2772{
2773 for (const auto &eqFunction : mArrayEqualityFunctions)
2774 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002775 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002776 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002777 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002778 }
2779 }
2780
2781 const TString &typeName = TypeString(type);
2782
Olli Etuaho12690762015-03-31 12:55:28 +03002783 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002784 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002785
2786 TInfoSinkBase fnNameOut;
2787 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002788 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002789
2790 TType nonArrayType = type;
2791 nonArrayType.clearArrayness();
2792
2793 TInfoSinkBase fnOut;
2794
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002795 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002796 << typeName << " a[" << type.getArraySize() << "], "
2797 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002798 << "{\n"
2799 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2800 " {\n"
2801 " if (";
2802
2803 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2804 fnOut << "a[i]";
2805 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2806 fnOut << "b[i]";
2807 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2808
2809 fnOut << ") { return false; }\n"
2810 " }\n"
2811 " return true;\n"
2812 "}\n";
2813
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002814 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002815
2816 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002817 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002818
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002819 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002820}
2821
Olli Etuaho12690762015-03-31 12:55:28 +03002822TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2823{
2824 for (const auto &assignFunction : mArrayAssignmentFunctions)
2825 {
2826 if (assignFunction.type == type)
2827 {
2828 return assignFunction.functionName;
2829 }
2830 }
2831
2832 const TString &typeName = TypeString(type);
2833
2834 ArrayHelperFunction function;
2835 function.type = type;
2836
2837 TInfoSinkBase fnNameOut;
2838 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2839 function.functionName = fnNameOut.c_str();
2840
2841 TInfoSinkBase fnOut;
2842
2843 fnOut << "void " << function.functionName << "(out "
2844 << typeName << " a[" << type.getArraySize() << "], "
2845 << typeName << " b[" << type.getArraySize() << "])\n"
2846 << "{\n"
2847 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2848 " {\n"
2849 " a[i] = b[i];\n"
2850 " }\n"
2851 "}\n";
2852
2853 function.functionDefinition = fnOut.c_str();
2854
2855 mArrayAssignmentFunctions.push_back(function);
2856
2857 return function.functionName;
2858}
2859
Olli Etuaho9638c352015-04-01 14:34:52 +03002860TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2861{
2862 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2863 {
2864 if (constructIntoFunction.type == type)
2865 {
2866 return constructIntoFunction.functionName;
2867 }
2868 }
2869
2870 const TString &typeName = TypeString(type);
2871
2872 ArrayHelperFunction function;
2873 function.type = type;
2874
2875 TInfoSinkBase fnNameOut;
2876 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2877 function.functionName = fnNameOut.c_str();
2878
2879 TInfoSinkBase fnOut;
2880
2881 fnOut << "void " << function.functionName << "(out "
2882 << typeName << " a[" << type.getArraySize() << "]";
2883 for (int i = 0; i < type.getArraySize(); ++i)
2884 {
2885 fnOut << ", " << typeName << " b" << i;
2886 }
2887 fnOut << ")\n"
2888 "{\n";
2889
2890 for (int i = 0; i < type.getArraySize(); ++i)
2891 {
2892 fnOut << " a[" << i << "] = b" << i << ";\n";
2893 }
2894 fnOut << "}\n";
2895
2896 function.functionDefinition = fnOut.c_str();
2897
2898 mArrayConstructIntoFunctions.push_back(function);
2899
2900 return function.functionName;
2901}
2902
Jamie Madill2e295e22015-04-29 10:41:33 -04002903void OutputHLSL::ensureStructDefined(const TType &type)
2904{
2905 TStructure *structure = type.getStruct();
2906
2907 if (structure)
2908 {
2909 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2910 }
2911}
2912
2913
Olli Etuaho9638c352015-04-01 14:34:52 +03002914
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002915}