blob: d6ef96ffd5000217f9762861377163d00af0ac1d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
Olli Etuaho5858f7e2016-04-08 13:08:46 +030024#include "compiler/translator/TextureFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050026#include "compiler/translator/UniformHLSL.h"
27#include "compiler/translator/UtilsHLSL.h"
28#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050029#include "compiler/translator/util.h"
30
Olli Etuaho4785fec2015-05-18 16:09:37 +030031namespace
32{
33
Olli Etuaho18b9deb2015-11-05 12:14:50 +020034void WriteSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
35{
36 ASSERT(constUnion != nullptr);
37 switch (constUnion->getType())
38 {
39 case EbtFloat:
40 out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst()));
41 break;
42 case EbtInt:
43 out << constUnion->getIConst();
44 break;
45 case EbtUInt:
46 out << constUnion->getUConst();
47 break;
48 case EbtBool:
49 out << constUnion->getBConst();
50 break;
51 default:
52 UNREACHABLE();
53 }
54}
55
56const TConstantUnion *WriteConstantUnionArray(TInfoSinkBase &out,
57 const TConstantUnion *const constUnion,
58 const size_t size)
59{
60 const TConstantUnion *constUnionIterated = constUnion;
61 for (size_t i = 0; i < size; i++, constUnionIterated++)
62 {
63 WriteSingleConstant(out, constUnionIterated);
64
65 if (i != size - 1)
66 {
67 out << ", ";
68 }
69 }
70 return constUnionIterated;
71}
72
Olli Etuaho4785fec2015-05-18 16:09:37 +030073} // namespace
74
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000075namespace sh
76{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000077
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080078OutputHLSL::OutputHLSL(sh::GLenum shaderType,
79 int shaderVersion,
80 const TExtensionBehavior &extensionBehavior,
81 const char *sourcePath,
82 ShShaderOutput outputType,
83 int numRenderTargets,
84 const std::vector<Uniform> &uniforms,
85 ShCompileOptions compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -040086 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020087 mShaderType(shaderType),
88 mShaderVersion(shaderVersion),
89 mExtensionBehavior(extensionBehavior),
90 mSourcePath(sourcePath),
91 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -070092 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +100093 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -070094 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000095{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +000096 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +000097
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +000098 mUsesFragColor = false;
99 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000100 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000101 mUsesFragCoord = false;
102 mUsesPointCoord = false;
103 mUsesFrontFacing = false;
104 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000105 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500106 mUsesVertexID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400107 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000108 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500109 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400110 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530111 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000112
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000113 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000114
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000115 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000116 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400117 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000118
119 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000120
Jamie Madill8daaba12014-06-13 10:04:33 -0400121 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200122 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300123 mTextureFunctionHLSL = new TextureFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400124
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200125 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000126 {
Arun Patole63419392015-03-13 11:51:07 +0530127 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
128 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
129 // In both cases total 3 uniform registers need to be reserved.
130 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000131 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000132
Geoff Lang00140f42016-02-03 18:47:33 +0000133 // Reserve registers for the default uniform block and driver constants
134 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135}
136
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000137OutputHLSL::~OutputHLSL()
138{
Jamie Madill8daaba12014-06-13 10:04:33 -0400139 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400140 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300141 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200142 for (auto &eqFunction : mStructEqualityFunctions)
143 {
144 SafeDelete(eqFunction);
145 }
146 for (auto &eqFunction : mArrayEqualityFunctions)
147 {
148 SafeDelete(eqFunction);
149 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000150}
151
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200152void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000153{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200154 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400155 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000156
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200157 BuiltInFunctionEmulator builtInFunctionEmulator;
158 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800159 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
160 {
161 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
162 mShaderVersion);
163 }
164
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200165 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500166
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700167 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700168 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
169 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300170 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700171 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700172
Jamie Madill37997142015-01-28 10:06:34 -0500173 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500174 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200175 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500176 mInfoSinkStack.pop();
177
Jamie Madill37997142015-01-28 10:06:34 -0500178 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500179 mInfoSinkStack.pop();
180
Jamie Madill32aab012015-01-27 14:12:26 -0500181 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500182 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500183 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000184
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200185 objSink << mHeader.c_str();
186 objSink << mBody.c_str();
187 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200188
189 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000190}
191
Jamie Madill570e04d2013-06-21 09:15:33 -0400192void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
193{
194 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
195 {
196 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
197
Jamie Madill32aab012015-01-27 14:12:26 -0500198 TInfoSinkBase structInfoSink;
199 mInfoSinkStack.push(&structInfoSink);
200
Jamie Madill570e04d2013-06-21 09:15:33 -0400201 // This will mark the necessary block elements as referenced
202 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500203
204 TString structName(structInfoSink.c_str());
205 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400206
207 mFlaggedStructOriginalNames[flaggedNode] = structName;
208
209 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
210 {
211 structName.erase(pos, 1);
212 }
213
214 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
215 }
216}
217
Jamie Madill4e1fd412014-07-10 17:50:10 -0400218const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
219{
220 return mUniformHLSL->getInterfaceBlockRegisterMap();
221}
222
Jamie Madill9fe25e92014-07-18 10:33:08 -0400223const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
224{
225 return mUniformHLSL->getUniformRegisterMap();
226}
227
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000228int OutputHLSL::vectorSize(const TType &type) const
229{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000230 int elementSize = type.isMatrix() ? type.getCols() : 1;
Olli Etuaho856c4972016-08-08 11:38:39 +0300231 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000232
233 return elementSize * arraySize;
234}
235
Jamie Madill98493dd2013-07-08 14:39:03 -0400236TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400237{
238 TString init;
239
240 TString preIndentString;
241 TString fullIndentString;
242
243 for (int spaces = 0; spaces < (indent * 4); spaces++)
244 {
245 preIndentString += ' ';
246 }
247
248 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
249 {
250 fullIndentString += ' ';
251 }
252
253 init += preIndentString + "{\n";
254
Jamie Madill98493dd2013-07-08 14:39:03 -0400255 const TFieldList &fields = structure.fields();
256 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400257 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400258 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400259 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400260 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400261
Jamie Madill98493dd2013-07-08 14:39:03 -0400262 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400263 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400264 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400265 }
266 else
267 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400268 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400269 }
270 }
271
272 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
273
274 return init;
275}
276
Jamie Madill8c46ab12015-12-07 16:39:19 -0500277void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000279 TString varyings;
280 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400281 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000282
Jamie Madill829f59e2013-11-13 19:40:54 -0500283 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400284 {
285 TIntermTyped *structNode = flaggedStructIt->first;
286 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400287 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 const TString &originalName = mFlaggedStructOriginalNames[structNode];
289
Jamie Madill033dae62014-06-18 12:56:28 -0400290 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400291 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400292 flaggedStructs += "\n";
293 }
294
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000295 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
296 {
297 const TType &type = varying->second->getType();
298 const TString &name = varying->second->getSymbol();
299
300 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400301 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
302 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000303 }
304
305 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
306 {
307 const TType &type = attribute->second->getType();
308 const TString &name = attribute->second->getSymbol();
309
Jamie Madill033dae62014-06-18 12:56:28 -0400310 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000311 }
312
Jamie Madill8daaba12014-06-13 10:04:33 -0400313 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400314
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200315 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400316 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
317
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200318 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500319 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200320 out << "\n// Equality functions\n\n";
321 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500322 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200323 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200324 }
325 }
Olli Etuaho12690762015-03-31 12:55:28 +0300326 if (!mArrayAssignmentFunctions.empty())
327 {
328 out << "\n// Assignment functions\n\n";
329 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
330 {
331 out << assignmentFunction.functionDefinition << "\n";
332 }
333 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300334 if (!mArrayConstructIntoFunctions.empty())
335 {
336 out << "\n// Array constructor functions\n\n";
337 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
338 {
339 out << constructIntoFunction.functionDefinition << "\n";
340 }
341 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200342
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500343 if (mUsesDiscardRewriting)
344 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400345 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500346 }
347
Nicolas Capens655fe362014-04-11 13:12:34 -0400348 if (mUsesNestedBreak)
349 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400350 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400351 }
352
Arun Patole44efa0b2015-03-04 17:11:05 +0530353 if (mRequiresIEEEStrictCompiling)
354 {
355 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
356 }
357
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400358 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
359 "#define LOOP [loop]\n"
360 "#define FLATTEN [flatten]\n"
361 "#else\n"
362 "#define LOOP\n"
363 "#define FLATTEN\n"
364 "#endif\n";
365
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200366 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200368 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
369 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000370
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000371 out << "// Varyings\n";
372 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400373 out << "\n";
374
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200375 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000376 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500377 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000378 {
Jamie Madill46131a32013-06-20 11:55:50 -0400379 const TString &variableName = outputVariableIt->first;
380 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400381
Jamie Madill033dae62014-06-18 12:56:28 -0400382 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400383 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000384 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000385 }
Jamie Madill46131a32013-06-20 11:55:50 -0400386 else
387 {
388 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
389
390 out << "static float4 gl_Color[" << numColorValues << "] =\n"
391 "{\n";
392 for (unsigned int i = 0; i < numColorValues; i++)
393 {
394 out << " float4(0, 0, 0, 0)";
395 if (i + 1 != numColorValues)
396 {
397 out << ",";
398 }
399 out << "\n";
400 }
401
402 out << "};\n";
403 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000404
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400405 if (mUsesFragDepth)
406 {
407 out << "static float gl_Depth = 0.0;\n";
408 }
409
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000410 if (mUsesFragCoord)
411 {
412 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
413 }
414
415 if (mUsesPointCoord)
416 {
417 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
418 }
419
420 if (mUsesFrontFacing)
421 {
422 out << "static bool gl_FrontFacing = false;\n";
423 }
424
425 out << "\n";
426
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000427 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000428 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000429 out << "struct gl_DepthRangeParameters\n"
430 "{\n"
431 " float near;\n"
432 " float far;\n"
433 " float diff;\n"
434 "};\n"
435 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000436 }
437
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200438 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000439 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000440 out << "cbuffer DriverConstants : register(b1)\n"
441 "{\n";
442
443 if (mUsesDepthRange)
444 {
445 out << " float3 dx_DepthRange : packoffset(c0);\n";
446 }
447
448 if (mUsesFragCoord)
449 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000450 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000451 }
452
453 if (mUsesFragCoord || mUsesFrontFacing)
454 {
455 out << " float3 dx_DepthFront : packoffset(c2);\n";
456 }
457
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800458 if (mUsesFragCoord)
459 {
460 // dx_ViewScale is only used in the fragment shader to correct
461 // the value for glFragCoord if necessary
462 out << " float2 dx_ViewScale : packoffset(c3);\n";
463 }
464
Olli Etuaho618bebc2016-01-15 16:40:00 +0200465 if (mOutputType == SH_HLSL_4_1_OUTPUT)
466 {
467 mUniformHLSL->samplerMetadataUniforms(out, "c4");
468 }
469
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000470 out << "};\n";
471 }
472 else
473 {
474 if (mUsesDepthRange)
475 {
476 out << "uniform float3 dx_DepthRange : register(c0);";
477 }
478
479 if (mUsesFragCoord)
480 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000481 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000482 }
483
484 if (mUsesFragCoord || mUsesFrontFacing)
485 {
486 out << "uniform float3 dx_DepthFront : register(c2);\n";
487 }
488 }
489
490 out << "\n";
491
492 if (mUsesDepthRange)
493 {
494 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
495 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000496 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000497
Jamie Madillf91ce812014-06-13 10:04:34 -0400498 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000499 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400500 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000501 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400502 out << flaggedStructs;
503 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000504 }
505
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000506 if (usingMRTExtension && mNumRenderTargets > 1)
507 {
508 out << "#define GL_USES_MRT\n";
509 }
510
511 if (mUsesFragColor)
512 {
513 out << "#define GL_USES_FRAG_COLOR\n";
514 }
515
516 if (mUsesFragData)
517 {
518 out << "#define GL_USES_FRAG_DATA\n";
519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000521 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000523 out << "// Attributes\n";
524 out << attributes;
525 out << "\n"
526 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400527
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000528 if (mUsesPointSize)
529 {
530 out << "static float gl_PointSize = float(1);\n";
531 }
532
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000533 if (mUsesInstanceID)
534 {
535 out << "static int gl_InstanceID;";
536 }
537
Corentin Wallezb076add2016-01-11 16:45:46 -0500538 if (mUsesVertexID)
539 {
540 out << "static int gl_VertexID;";
541 }
542
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000543 out << "\n"
544 "// Varyings\n";
545 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000546 out << "\n";
547
548 if (mUsesDepthRange)
549 {
550 out << "struct gl_DepthRangeParameters\n"
551 "{\n"
552 " float near;\n"
553 " float far;\n"
554 " float diff;\n"
555 "};\n"
556 "\n";
557 }
558
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200559 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000560 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800561 out << "cbuffer DriverConstants : register(b1)\n"
562 "{\n";
563
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000564 if (mUsesDepthRange)
565 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800566 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000567 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800568
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800569 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
570 // shaders. However, we declare it for all shaders (including Feature Level 10+).
571 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
572 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800573 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800574 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800575 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800576
Olli Etuaho618bebc2016-01-15 16:40:00 +0200577 if (mOutputType == SH_HLSL_4_1_OUTPUT)
578 {
579 mUniformHLSL->samplerMetadataUniforms(out, "c4");
580 }
581
Austin Kinross4fd18b12014-12-22 12:32:05 -0800582 out << "};\n"
583 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000584 }
585 else
586 {
587 if (mUsesDepthRange)
588 {
589 out << "uniform float3 dx_DepthRange : register(c0);\n";
590 }
591
Cooper Partine6664f02015-01-09 16:22:24 -0800592 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
593 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000594 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000595 }
596
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000597 if (mUsesDepthRange)
598 {
599 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
600 "\n";
601 }
602
Jamie Madillf91ce812014-06-13 10:04:34 -0400603 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000604 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400605 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000606 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400607 out << flaggedStructs;
608 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000609 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400610 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000611
Geoff Lang1fe74c72016-08-25 13:23:01 -0400612 bool getDimensionsIgnoresBaseLevel =
613 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
614 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000615
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000616 if (mUsesFragCoord)
617 {
618 out << "#define GL_USES_FRAG_COORD\n";
619 }
620
621 if (mUsesPointCoord)
622 {
623 out << "#define GL_USES_POINT_COORD\n";
624 }
625
626 if (mUsesFrontFacing)
627 {
628 out << "#define GL_USES_FRONT_FACING\n";
629 }
630
631 if (mUsesPointSize)
632 {
633 out << "#define GL_USES_POINT_SIZE\n";
634 }
635
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400636 if (mUsesFragDepth)
637 {
638 out << "#define GL_USES_FRAG_DEPTH\n";
639 }
640
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000641 if (mUsesDepthRange)
642 {
643 out << "#define GL_USES_DEPTH_RANGE\n";
644 }
645
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000646 if (mUsesXor)
647 {
648 out << "bool xor(bool p, bool q)\n"
649 "{\n"
650 " return (p || q) && !(p && q);\n"
651 "}\n"
652 "\n";
653 }
654
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200655 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000656}
657
658void OutputHLSL::visitSymbol(TIntermSymbol *node)
659{
Jamie Madill32aab012015-01-27 14:12:26 -0500660 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661
Jamie Madill570e04d2013-06-21 09:15:33 -0400662 // Handle accessing std140 structs by value
663 if (mFlaggedStructMappedNames.count(node) > 0)
664 {
665 out << mFlaggedStructMappedNames[node];
666 return;
667 }
668
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669 TString name = node->getSymbol();
670
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000671 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000672 {
673 mUsesDepthRange = true;
674 out << name;
675 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000676 else
677 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000678 TQualifier qualifier = node->getQualifier();
679
680 if (qualifier == EvqUniform)
681 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400682 const TType &nodeType = node->getType();
683 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400684
685 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000686 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400687 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000688 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000689 else
690 {
691 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000692 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400693
Jamie Madill2e295e22015-04-29 10:41:33 -0400694 ensureStructDefined(nodeType);
695
Olli Etuaho96963162016-03-21 11:54:33 +0200696 const TName &nameWithMetadata = node->getName();
697 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000698 }
Jamie Madill19571812013-08-12 15:26:34 -0700699 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000700 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000701 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400702 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000703 }
Jamie Madill033dae62014-06-18 12:56:28 -0400704 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000705 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000706 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400707 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000708 }
Jamie Madill19571812013-08-12 15:26:34 -0700709 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400710 {
711 mReferencedOutputVariables[name] = node;
712 out << "out_" << name;
713 }
714 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000715 {
716 out << "gl_Color[0]";
717 mUsesFragColor = true;
718 }
719 else if (qualifier == EvqFragData)
720 {
721 out << "gl_Color";
722 mUsesFragData = true;
723 }
724 else if (qualifier == EvqFragCoord)
725 {
726 mUsesFragCoord = true;
727 out << name;
728 }
729 else if (qualifier == EvqPointCoord)
730 {
731 mUsesPointCoord = true;
732 out << name;
733 }
734 else if (qualifier == EvqFrontFacing)
735 {
736 mUsesFrontFacing = true;
737 out << name;
738 }
739 else if (qualifier == EvqPointSize)
740 {
741 mUsesPointSize = true;
742 out << name;
743 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000744 else if (qualifier == EvqInstanceID)
745 {
746 mUsesInstanceID = true;
747 out << name;
748 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500749 else if (qualifier == EvqVertexID)
750 {
751 mUsesVertexID = true;
752 out << name;
753 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300754 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400755 {
756 mUsesFragDepth = true;
757 out << "gl_Depth";
758 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000759 else
760 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300761 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000762 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763 }
764}
765
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400766void OutputHLSL::visitRaw(TIntermRaw *node)
767{
Jamie Madill32aab012015-01-27 14:12:26 -0500768 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400769}
770
Olli Etuaho7fb49552015-03-18 17:27:44 +0200771void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
772{
773 if (type.isScalar() && !type.isArray())
774 {
775 if (op == EOpEqual)
776 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500777 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200778 }
779 else
780 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500781 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200782 }
783 }
784 else
785 {
786 if (visit == PreVisit && op == EOpNotEqual)
787 {
788 out << "!";
789 }
790
791 if (type.isArray())
792 {
793 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500794 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200795 }
796 else if (type.getBasicType() == EbtStruct)
797 {
798 const TStructure &structure = *type.getStruct();
799 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500800 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200801 }
802 else
803 {
804 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500805 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200806 }
807 }
808}
809
Olli Etuaho96963162016-03-21 11:54:33 +0200810bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
811{
812 // Inside InVisit the current node is already in the path.
813 const unsigned int initialN = visit == InVisit ? 1u : 0u;
814 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
815 {
816 TIntermNode *ancestor = getAncestorNode(n);
817 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
818 if (ancestorBinary == nullptr)
819 {
820 return false;
821 }
822 switch (ancestorBinary->getOp())
823 {
824 case EOpIndexDirectStruct:
825 {
826 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
827 const TIntermConstantUnion *index =
828 ancestorBinary->getRight()->getAsConstantUnion();
829 const TField *field = structure->fields()[index->getIConst(0)];
830 if (IsSampler(field->type()->getBasicType()))
831 {
832 return true;
833 }
834 break;
835 }
836 case EOpIndexDirect:
837 break;
838 default:
839 // Returning a sampler from indirect indexing is not supported.
840 return false;
841 }
842 }
843 return false;
844}
845
Olli Etuahob6fa0432016-09-28 16:28:05 +0100846bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
847{
848 TInfoSinkBase &out = getInfoSink();
849 if (visit == PostVisit)
850 {
851 out << ".";
852 node->writeOffsetsAsXYZW(&out);
853 }
854 return true;
855}
856
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
858{
Jamie Madill32aab012015-01-27 14:12:26 -0500859 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
Jamie Madill570e04d2013-06-21 09:15:33 -0400861 // Handle accessing std140 structs by value
862 if (mFlaggedStructMappedNames.count(node) > 0)
863 {
864 out << mFlaggedStructMappedNames[node];
865 return false;
866 }
867
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868 switch (node->getOp())
869 {
Olli Etuahoe79904c2015-03-18 16:56:42 +0200870 case EOpAssign:
871 if (node->getLeft()->isArray())
872 {
Olli Etuaho9638c352015-04-01 14:34:52 +0300873 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
874 if (rightAgg != nullptr && rightAgg->isConstructor())
875 {
876 const TString &functionName = addArrayConstructIntoFunction(node->getType());
877 out << functionName << "(";
878 node->getLeft()->traverse(this);
879 TIntermSequence *seq = rightAgg->getSequence();
880 for (auto &arrayElement : *seq)
881 {
882 out << ", ";
883 arrayElement->traverse(this);
884 }
885 out << ")";
886 return false;
887 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +0300888 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
889 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
890
891 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500892 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200893 }
894 else
895 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500896 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +0200897 }
898 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000899 case EOpInitialize:
900 if (visit == PreVisit)
901 {
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000902 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -0500903 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000904 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000905
Olli Etuaho3d932d82016-04-12 11:10:30 +0300906 // Global initializers must be constant at this point.
Olli Etuahod4f4c112016-04-15 15:11:24 +0300907 ASSERT(symbolNode->getQualifier() != EvqGlobal || canWriteAsHLSLLiteral(expression));
908
909 // GLSL allows to write things like "float x = x;" where a new variable x is defined
910 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
911 // new variable is created before the assignment is evaluated), so we need to convert
912 // this to "float t = x, x = t;".
Olli Etuaho3d932d82016-04-12 11:10:30 +0300913 if (writeSameSymbolInitializer(out, symbolNode, expression))
Jamie Madill37997142015-01-28 10:06:34 -0500914 {
915 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000916 return false;
917 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200918 else if (writeConstantInitialization(out, symbolNode, expression))
919 {
920 return false;
921 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000922 }
923 else if (visit == InVisit)
924 {
925 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000926 }
927 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500928 case EOpAddAssign:
929 outputTriplet(out, visit, "(", " += ", ")");
930 break;
931 case EOpSubAssign:
932 outputTriplet(out, visit, "(", " -= ", ")");
933 break;
934 case EOpMulAssign:
935 outputTriplet(out, visit, "(", " *= ", ")");
936 break;
937 case EOpVectorTimesScalarAssign:
938 outputTriplet(out, visit, "(", " *= ", ")");
939 break;
940 case EOpMatrixTimesScalarAssign:
941 outputTriplet(out, visit, "(", " *= ", ")");
942 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000943 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000944 if (visit == PreVisit)
945 {
946 out << "(";
947 }
948 else if (visit == InVisit)
949 {
950 out << " = mul(";
951 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -0400952 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000953 }
954 else
955 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +0000956 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +0000957 }
958 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000959 case EOpMatrixTimesMatrixAssign:
960 if (visit == PreVisit)
961 {
962 out << "(";
963 }
964 else if (visit == InVisit)
965 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200966 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000967 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +0200968 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000969 }
970 else
971 {
Olli Etuahofc7fab72015-03-06 12:03:18 +0200972 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +0000973 }
974 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -0500975 case EOpDivAssign:
976 outputTriplet(out, visit, "(", " /= ", ")");
977 break;
978 case EOpIModAssign:
979 outputTriplet(out, visit, "(", " %= ", ")");
980 break;
981 case EOpBitShiftLeftAssign:
982 outputTriplet(out, visit, "(", " <<= ", ")");
983 break;
984 case EOpBitShiftRightAssign:
985 outputTriplet(out, visit, "(", " >>= ", ")");
986 break;
987 case EOpBitwiseAndAssign:
988 outputTriplet(out, visit, "(", " &= ", ")");
989 break;
990 case EOpBitwiseXorAssign:
991 outputTriplet(out, visit, "(", " ^= ", ")");
992 break;
993 case EOpBitwiseOrAssign:
994 outputTriplet(out, visit, "(", " |= ", ")");
995 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -0400996 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -0400997 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400998 const TType& leftType = node->getLeft()->getType();
999 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001000 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001001 if (visit == PreVisit)
1002 {
1003 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1004 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001005 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001006 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001007 return false;
1008 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001009 }
Olli Etuaho96963162016-03-21 11:54:33 +02001010 else if (ancestorEvaluatesToSamplerInStruct(visit))
1011 {
1012 // All parts of an expression that access a sampler in a struct need to use _ as
1013 // separator to access the sampler variable that has been moved out of the struct.
1014 outputTriplet(out, visit, "", "_", "");
1015 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001016 else
1017 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001018 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001019 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001020 }
1021 break;
1022 case EOpIndexIndirect:
1023 // We do not currently support indirect references to interface blocks
1024 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001025 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001026 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001027 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001028 {
1029 const TStructure* structure = node->getLeft()->getType().getStruct();
1030 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1031 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001032
Olli Etuaho96963162016-03-21 11:54:33 +02001033 // In cases where indexing returns a sampler, we need to access the sampler variable
1034 // that has been moved out of the struct.
1035 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1036 if (visit == PreVisit && indexingReturnsSampler)
1037 {
1038 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1039 // This prefix is only output at the beginning of the indexing expression, which
1040 // may have multiple parts.
1041 out << "angle";
1042 }
1043 if (!indexingReturnsSampler)
1044 {
1045 // All parts of an expression that access a sampler in a struct need to use _ as
1046 // separator to access the sampler variable that has been moved out of the struct.
1047 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1048 }
1049 if (visit == InVisit)
1050 {
1051 if (indexingReturnsSampler)
1052 {
1053 out << "_" + field->name();
1054 }
1055 else
1056 {
1057 out << "." + DecorateField(field->name(), *structure);
1058 }
1059
1060 return false;
1061 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001062 }
1063 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001064 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001065 if (visit == InVisit)
1066 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001067 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1068 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1069 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001070 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001071
1072 return false;
1073 }
1074 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001075 case EOpAdd:
1076 outputTriplet(out, visit, "(", " + ", ")");
1077 break;
1078 case EOpSub:
1079 outputTriplet(out, visit, "(", " - ", ")");
1080 break;
1081 case EOpMul:
1082 outputTriplet(out, visit, "(", " * ", ")");
1083 break;
1084 case EOpDiv:
1085 outputTriplet(out, visit, "(", " / ", ")");
1086 break;
1087 case EOpIMod:
1088 outputTriplet(out, visit, "(", " % ", ")");
1089 break;
1090 case EOpBitShiftLeft:
1091 outputTriplet(out, visit, "(", " << ", ")");
1092 break;
1093 case EOpBitShiftRight:
1094 outputTriplet(out, visit, "(", " >> ", ")");
1095 break;
1096 case EOpBitwiseAnd:
1097 outputTriplet(out, visit, "(", " & ", ")");
1098 break;
1099 case EOpBitwiseXor:
1100 outputTriplet(out, visit, "(", " ^ ", ")");
1101 break;
1102 case EOpBitwiseOr:
1103 outputTriplet(out, visit, "(", " | ", ")");
1104 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001105 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001106 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001107 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001108 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001109 case EOpLessThan:
1110 outputTriplet(out, visit, "(", " < ", ")");
1111 break;
1112 case EOpGreaterThan:
1113 outputTriplet(out, visit, "(", " > ", ")");
1114 break;
1115 case EOpLessThanEqual:
1116 outputTriplet(out, visit, "(", " <= ", ")");
1117 break;
1118 case EOpGreaterThanEqual:
1119 outputTriplet(out, visit, "(", " >= ", ")");
1120 break;
1121 case EOpVectorTimesScalar:
1122 outputTriplet(out, visit, "(", " * ", ")");
1123 break;
1124 case EOpMatrixTimesScalar:
1125 outputTriplet(out, visit, "(", " * ", ")");
1126 break;
1127 case EOpVectorTimesMatrix:
1128 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1129 break;
1130 case EOpMatrixTimesVector:
1131 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1132 break;
1133 case EOpMatrixTimesMatrix:
1134 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1135 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001136 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001137 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1138 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001139 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001140 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001141 case EOpLogicalXor:
1142 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001143 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001144 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001145 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001146 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1147 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001148 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001149 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001150 default: UNREACHABLE();
1151 }
1152
1153 return true;
1154}
1155
1156bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1157{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001158 TInfoSinkBase &out = getInfoSink();
1159
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001160 switch (node->getOp())
1161 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001162 case EOpNegative:
1163 outputTriplet(out, visit, "(-", "", ")");
1164 break;
1165 case EOpPositive:
1166 outputTriplet(out, visit, "(+", "", ")");
1167 break;
1168 case EOpVectorLogicalNot:
1169 outputTriplet(out, visit, "(!", "", ")");
1170 break;
1171 case EOpLogicalNot:
1172 outputTriplet(out, visit, "(!", "", ")");
1173 break;
1174 case EOpBitwiseNot:
1175 outputTriplet(out, visit, "(~", "", ")");
1176 break;
1177 case EOpPostIncrement:
1178 outputTriplet(out, visit, "(", "", "++)");
1179 break;
1180 case EOpPostDecrement:
1181 outputTriplet(out, visit, "(", "", "--)");
1182 break;
1183 case EOpPreIncrement:
1184 outputTriplet(out, visit, "(++", "", ")");
1185 break;
1186 case EOpPreDecrement:
1187 outputTriplet(out, visit, "(--", "", ")");
1188 break;
1189 case EOpRadians:
1190 outputTriplet(out, visit, "radians(", "", ")");
1191 break;
1192 case EOpDegrees:
1193 outputTriplet(out, visit, "degrees(", "", ")");
1194 break;
1195 case EOpSin:
1196 outputTriplet(out, visit, "sin(", "", ")");
1197 break;
1198 case EOpCos:
1199 outputTriplet(out, visit, "cos(", "", ")");
1200 break;
1201 case EOpTan:
1202 outputTriplet(out, visit, "tan(", "", ")");
1203 break;
1204 case EOpAsin:
1205 outputTriplet(out, visit, "asin(", "", ")");
1206 break;
1207 case EOpAcos:
1208 outputTriplet(out, visit, "acos(", "", ")");
1209 break;
1210 case EOpAtan:
1211 outputTriplet(out, visit, "atan(", "", ")");
1212 break;
1213 case EOpSinh:
1214 outputTriplet(out, visit, "sinh(", "", ")");
1215 break;
1216 case EOpCosh:
1217 outputTriplet(out, visit, "cosh(", "", ")");
1218 break;
1219 case EOpTanh:
1220 outputTriplet(out, visit, "tanh(", "", ")");
1221 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001222 case EOpAsinh:
1223 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001224 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001225 break;
1226 case EOpAcosh:
1227 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001228 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001229 break;
1230 case EOpAtanh:
1231 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001232 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001233 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001234 case EOpExp:
1235 outputTriplet(out, visit, "exp(", "", ")");
1236 break;
1237 case EOpLog:
1238 outputTriplet(out, visit, "log(", "", ")");
1239 break;
1240 case EOpExp2:
1241 outputTriplet(out, visit, "exp2(", "", ")");
1242 break;
1243 case EOpLog2:
1244 outputTriplet(out, visit, "log2(", "", ")");
1245 break;
1246 case EOpSqrt:
1247 outputTriplet(out, visit, "sqrt(", "", ")");
1248 break;
1249 case EOpInverseSqrt:
1250 outputTriplet(out, visit, "rsqrt(", "", ")");
1251 break;
1252 case EOpAbs:
1253 outputTriplet(out, visit, "abs(", "", ")");
1254 break;
1255 case EOpSign:
1256 outputTriplet(out, visit, "sign(", "", ")");
1257 break;
1258 case EOpFloor:
1259 outputTriplet(out, visit, "floor(", "", ")");
1260 break;
1261 case EOpTrunc:
1262 outputTriplet(out, visit, "trunc(", "", ")");
1263 break;
1264 case EOpRound:
1265 outputTriplet(out, visit, "round(", "", ")");
1266 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001267 case EOpRoundEven:
1268 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001269 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001270 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001271 case EOpCeil:
1272 outputTriplet(out, visit, "ceil(", "", ")");
1273 break;
1274 case EOpFract:
1275 outputTriplet(out, visit, "frac(", "", ")");
1276 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301277 case EOpIsNan:
Shao6f0a0dc2016-09-27 13:51:29 +08001278 if (node->getUseEmulatedFunction())
1279 writeEmulatedFunctionTriplet(out, visit, "isnan(");
1280 else
1281 outputTriplet(out, visit, "isnan(", "", ")");
1282 mRequiresIEEEStrictCompiling = true;
1283 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001284 case EOpIsInf:
1285 outputTriplet(out, visit, "isinf(", "", ")");
1286 break;
1287 case EOpFloatBitsToInt:
1288 outputTriplet(out, visit, "asint(", "", ")");
1289 break;
1290 case EOpFloatBitsToUint:
1291 outputTriplet(out, visit, "asuint(", "", ")");
1292 break;
1293 case EOpIntBitsToFloat:
1294 outputTriplet(out, visit, "asfloat(", "", ")");
1295 break;
1296 case EOpUintBitsToFloat:
1297 outputTriplet(out, visit, "asfloat(", "", ")");
1298 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001299 case EOpPackSnorm2x16:
1300 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001301 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001302 break;
1303 case EOpPackUnorm2x16:
1304 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001305 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001306 break;
1307 case EOpPackHalf2x16:
1308 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001309 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001310 break;
1311 case EOpUnpackSnorm2x16:
1312 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001313 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001314 break;
1315 case EOpUnpackUnorm2x16:
1316 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001317 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001318 break;
1319 case EOpUnpackHalf2x16:
1320 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001321 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001322 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001323 case EOpLength:
1324 outputTriplet(out, visit, "length(", "", ")");
1325 break;
1326 case EOpNormalize:
1327 outputTriplet(out, visit, "normalize(", "", ")");
1328 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001329 case EOpDFdx:
1330 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1331 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001332 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001333 }
1334 else
1335 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001336 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001337 }
1338 break;
1339 case EOpDFdy:
1340 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1341 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001342 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001343 }
1344 else
1345 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001346 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001347 }
1348 break;
1349 case EOpFwidth:
1350 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1351 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001352 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001353 }
1354 else
1355 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001356 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001357 }
1358 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001359 case EOpTranspose:
1360 outputTriplet(out, visit, "transpose(", "", ")");
1361 break;
1362 case EOpDeterminant:
1363 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1364 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001365 case EOpInverse:
1366 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001367 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001368 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001369
Jamie Madill8c46ab12015-12-07 16:39:19 -05001370 case EOpAny:
1371 outputTriplet(out, visit, "any(", "", ")");
1372 break;
1373 case EOpAll:
1374 outputTriplet(out, visit, "all(", "", ")");
1375 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376 default: UNREACHABLE();
1377 }
1378
1379 return true;
1380}
1381
Olli Etuaho96963162016-03-21 11:54:33 +02001382TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1383{
1384 if (node->getAsSymbolNode())
1385 {
1386 return node->getAsSymbolNode()->getSymbol();
1387 }
1388 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1389 switch (nodeBinary->getOp())
1390 {
1391 case EOpIndexDirect:
1392 {
1393 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1394
1395 TInfoSinkBase prefixSink;
1396 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1397 return TString(prefixSink.c_str());
1398 }
1399 case EOpIndexDirectStruct:
1400 {
1401 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1402 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1403 const TField *field = s->fields()[index];
1404
1405 TInfoSinkBase prefixSink;
1406 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1407 << field->name();
1408 return TString(prefixSink.c_str());
1409 }
1410 default:
1411 UNREACHABLE();
1412 return TString("");
1413 }
1414}
1415
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001416bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1417{
1418 TInfoSinkBase &out = getInfoSink();
1419
1420 if (mInsideFunction)
1421 {
1422 outputLineDirective(out, node->getLine().first_line);
1423 out << "{\n";
1424 }
1425
1426 for (TIntermSequence::iterator sit = node->getSequence()->begin();
1427 sit != node->getSequence()->end(); sit++)
1428 {
1429 outputLineDirective(out, (*sit)->getLine().first_line);
1430
1431 (*sit)->traverse(this);
1432
1433 // Don't output ; after case labels, they're terminated by :
1434 // This is needed especially since outputting a ; after a case statement would turn empty
1435 // case statements into non-empty case statements, disallowing fall-through from them.
1436 // Also no need to output ; after if statements or sequences. This is done just for
1437 // code clarity.
1438 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
1439 (*sit)->getAsBlock() == nullptr)
1440 out << ";\n";
1441 }
1442
1443 if (mInsideFunction)
1444 {
1445 outputLineDirective(out, node->getLine().last_line);
1446 out << "}\n";
1447 }
1448
1449 return false;
1450}
1451
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001452bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1453{
Jamie Madill32aab012015-01-27 14:12:26 -05001454 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001455
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001456 switch (node->getOp())
1457 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001458 case EOpDeclaration:
1459 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001460 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001461 TIntermSequence *sequence = node->getSequence();
1462 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1463 ASSERT(sequence->size() == 1);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001464
Olli Etuaho5878f832016-10-07 10:14:58 +01001465 if (variable &&
1466 (variable->getQualifier() == EvqTemporary ||
1467 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001468 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001469 ensureStructDefined(variable->getType());
1470
1471 if (!variable->getAsSymbolNode() ||
1472 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001473 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001474 if (!mInsideFunction)
1475 {
1476 out << "static ";
1477 }
1478
1479 out << TypeString(variable->getType()) + " ";
1480
1481 TIntermSymbol *symbol = variable->getAsSymbolNode();
1482
1483 if (symbol)
1484 {
1485 symbol->traverse(this);
1486 out << ArrayString(symbol->getType());
1487 out << " = " + initializer(symbol->getType());
1488 }
1489 else
1490 {
1491 variable->traverse(this);
1492 }
Olli Etuahoa6f22092015-05-08 18:31:10 +03001493 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001494 else if (variable->getAsSymbolNode() &&
1495 variable->getAsSymbolNode()->getSymbol() ==
1496 "") // Type (struct) declaration
Olli Etuahoa6f22092015-05-08 18:31:10 +03001497 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001498 // Already added to constructor map
Olli Etuahoa6f22092015-05-08 18:31:10 +03001499 }
1500 else
Olli Etuaho5878f832016-10-07 10:14:58 +01001501 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001502 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001503 else if (variable && IsVaryingOut(variable->getQualifier()))
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001504 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001505 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end();
1506 sit++)
1507 {
1508 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001509
Olli Etuaho5878f832016-10-07 10:14:58 +01001510 if (symbol)
1511 {
1512 // Vertex (output) varyings which are declared but not written to should
1513 // still be declared to allow successful linking
1514 mReferencedVaryings[symbol->getSymbol()] = symbol;
1515 }
1516 else
1517 {
1518 (*sit)->traverse(this);
1519 }
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001520 }
1521 }
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001522
Corentin Wallez1239ee92015-03-19 14:38:02 -07001523 return false;
1524 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001525 else if (visit == InVisit)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001526 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001527 out << ", ";
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001528 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001529 break;
1530 case EOpInvariantDeclaration:
1531 // Do not do any translation
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001532 return false;
Olli Etuaho5878f832016-10-07 10:14:58 +01001533 case EOpPrototype:
1534 if (visit == PreVisit)
1535 {
Olli Etuahobd674552016-10-06 13:28:42 +01001536 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Olli Etuaho5878f832016-10-07 10:14:58 +01001537 // Skip the prototype if it is not implemented (and thus not used)
1538 if (index == CallDAG::InvalidIndex)
1539 {
1540 return false;
1541 }
1542
1543 TIntermSequence *arguments = node->getSequence();
1544
Olli Etuahobd674552016-10-06 13:28:42 +01001545 TString name =
1546 DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho5878f832016-10-07 10:14:58 +01001547 out << TypeString(node->getType()) << " " << name
1548 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
1549
1550 for (unsigned int i = 0; i < arguments->size(); i++)
1551 {
1552 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1553
1554 if (symbol)
1555 {
1556 out << argumentString(symbol);
1557
1558 if (i < arguments->size() - 1)
1559 {
1560 out << ", ";
1561 }
1562 }
1563 else
1564 UNREACHABLE();
1565 }
1566
1567 out << ");\n";
1568
1569 // Also prototype the Lod0 variant if needed
1570 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1571 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1572 {
1573 mOutputLod0Function = true;
1574 node->traverse(this);
1575 mOutputLod0Function = false;
1576 }
1577
1578 return false;
1579 }
1580 break;
1581 case EOpComma:
1582 outputTriplet(out, visit, "(", ", ", ")");
1583 break;
1584 case EOpFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001585 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07001586 ASSERT(mCurrentFunctionMetadata == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001587
Olli Etuahobd674552016-10-06 13:28:42 +01001588 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001589 ASSERT(index != CallDAG::InvalidIndex);
1590 mCurrentFunctionMetadata = &mASTMetadataList[index];
1591
Jamie Madill033dae62014-06-18 12:56:28 -04001592 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001593
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001594 TIntermSequence *sequence = node->getSequence();
1595 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
1596
Olli Etuahobd674552016-10-06 13:28:42 +01001597 if (node->getFunctionSymbolInfo()->isMain())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001598 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001599 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001600 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001601 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001602 {
Olli Etuahobd674552016-10-06 13:28:42 +01001603 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001604 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001605 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00001606
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001607 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001608 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001609 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001610
1611 if (symbol)
1612 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001613 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001614
1615 out << argumentString(symbol);
1616
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001617 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001618 {
1619 out << ", ";
1620 }
1621 }
1622 else UNREACHABLE();
1623 }
1624
Olli Etuaho4785fec2015-05-18 16:09:37 +03001625 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04001626
Olli Etuahof51fdd22016-10-03 10:03:40 +01001627 mInsideFunction = true;
1628 ASSERT(sequence->size() == 2);
1629 TIntermNode *body = (*sequence)[1];
1630 // The function body node will output braces.
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001631 ASSERT(body->getAsBlock() != nullptr);
Olli Etuahof51fdd22016-10-03 10:03:40 +01001632 body->traverse(this);
1633 mInsideFunction = false;
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001634
Corentin Wallez1239ee92015-03-19 14:38:02 -07001635 mCurrentFunctionMetadata = nullptr;
1636
1637 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1638 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001639 {
Olli Etuahobd674552016-10-06 13:28:42 +01001640 ASSERT(!node->getFunctionSymbolInfo()->isMain());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001641 mOutputLod0Function = true;
1642 node->traverse(this);
1643 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00001644 }
1645
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00001646 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001648 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001649 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001650 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001651
Corentin Wallez1239ee92015-03-19 14:38:02 -07001652 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001653 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001654 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001655 if (node->isArray())
1656 {
1657 UNIMPLEMENTED();
1658 }
Olli Etuahobd674552016-10-06 13:28:42 +01001659 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001660 ASSERT(index != CallDAG::InvalidIndex);
1661 lod0 &= mASTMetadataList[index].mNeedsLod0;
1662
Olli Etuahobd674552016-10-06 13:28:42 +01001663 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001664 out << DisambiguateFunctionName(node->getSequence());
1665 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001666 }
Olli Etuahobd674552016-10-06 13:28:42 +01001667 else if (node->getFunctionSymbolInfo()->getNameObj().isInternal())
Olli Etuahob741c762016-06-29 15:49:22 +03001668 {
1669 // This path is used for internal functions that don't have their definitions in the
1670 // AST, such as precision emulation functions.
Olli Etuahobd674552016-10-06 13:28:42 +01001671 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001672 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673 else
1674 {
Olli Etuahobd674552016-10-06 13:28:42 +01001675 TString name = TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001676 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001677 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1678 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1679 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1680 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001681 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001682
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001683 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001684 {
Olli Etuaho96963162016-03-21 11:54:33 +02001685 TIntermTyped *typedArg = (*arg)->getAsTyped();
1686 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001687 {
1688 out << "texture_";
1689 (*arg)->traverse(this);
1690 out << ", sampler_";
1691 }
1692
1693 (*arg)->traverse(this);
1694
Olli Etuaho96963162016-03-21 11:54:33 +02001695 if (typedArg->getType().isStructureContainingSamplers())
1696 {
1697 const TType &argType = typedArg->getType();
1698 TVector<TIntermSymbol *> samplerSymbols;
1699 TString structName = samplerNamePrefixFromStruct(typedArg);
1700 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001701 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001702 &samplerSymbols, nullptr);
1703 for (const TIntermSymbol *sampler : samplerSymbols)
1704 {
1705 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1706 {
1707 out << ", texture_" << sampler->getSymbol();
1708 out << ", sampler_" << sampler->getSymbol();
1709 }
1710 else
1711 {
1712 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1713 // of D3D9, it's the sampler variable.
1714 out << ", " + sampler->getSymbol();
1715 }
1716 }
1717 }
1718
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001719 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001720 {
1721 out << ", ";
1722 }
1723 }
1724
1725 out << ")";
1726
1727 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001728 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001729 case EOpParameters:
1730 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1731 break;
1732 case EOpConstructFloat:
1733 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1734 break;
1735 case EOpConstructVec2:
1736 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1737 break;
1738 case EOpConstructVec3:
1739 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1740 break;
1741 case EOpConstructVec4:
1742 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1743 break;
1744 case EOpConstructBool:
1745 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1746 break;
1747 case EOpConstructBVec2:
1748 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1749 break;
1750 case EOpConstructBVec3:
1751 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1752 break;
1753 case EOpConstructBVec4:
1754 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1755 break;
1756 case EOpConstructInt:
1757 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1758 break;
1759 case EOpConstructIVec2:
1760 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1761 break;
1762 case EOpConstructIVec3:
1763 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1764 break;
1765 case EOpConstructIVec4:
1766 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1767 break;
1768 case EOpConstructUInt:
1769 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1770 break;
1771 case EOpConstructUVec2:
1772 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1773 break;
1774 case EOpConstructUVec3:
1775 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1776 break;
1777 case EOpConstructUVec4:
1778 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1779 break;
1780 case EOpConstructMat2:
1781 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1782 break;
1783 case EOpConstructMat2x3:
1784 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1785 break;
1786 case EOpConstructMat2x4:
1787 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1788 break;
1789 case EOpConstructMat3x2:
1790 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1791 break;
1792 case EOpConstructMat3:
1793 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1794 break;
1795 case EOpConstructMat3x4:
1796 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1797 break;
1798 case EOpConstructMat4x2:
1799 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1800 break;
1801 case EOpConstructMat4x3:
1802 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1803 break;
1804 case EOpConstructMat4:
1805 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1806 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001807 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001808 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001809 if (node->getType().isArray())
1810 {
1811 UNIMPLEMENTED();
1812 }
Jamie Madill033dae62014-06-18 12:56:28 -04001813 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001814 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001815 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001816 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001817 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001818 case EOpLessThan:
1819 outputTriplet(out, visit, "(", " < ", ")");
1820 break;
1821 case EOpGreaterThan:
1822 outputTriplet(out, visit, "(", " > ", ")");
1823 break;
1824 case EOpLessThanEqual:
1825 outputTriplet(out, visit, "(", " <= ", ")");
1826 break;
1827 case EOpGreaterThanEqual:
1828 outputTriplet(out, visit, "(", " >= ", ")");
1829 break;
1830 case EOpVectorEqual:
1831 outputTriplet(out, visit, "(", " == ", ")");
1832 break;
1833 case EOpVectorNotEqual:
1834 outputTriplet(out, visit, "(", " != ", ")");
1835 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001836 case EOpMod:
1837 ASSERT(node->getUseEmulatedFunction());
1838 writeEmulatedFunctionTriplet(out, visit, "mod(");
1839 break;
1840 case EOpModf:
1841 outputTriplet(out, visit, "modf(", ", ", ")");
1842 break;
1843 case EOpPow:
1844 outputTriplet(out, visit, "pow(", ", ", ")");
1845 break;
1846 case EOpAtan:
1847 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1848 ASSERT(node->getUseEmulatedFunction());
1849 writeEmulatedFunctionTriplet(out, visit, "atan(");
1850 break;
1851 case EOpMin:
1852 outputTriplet(out, visit, "min(", ", ", ")");
1853 break;
1854 case EOpMax:
1855 outputTriplet(out, visit, "max(", ", ", ")");
1856 break;
1857 case EOpClamp:
1858 outputTriplet(out, visit, "clamp(", ", ", ")");
1859 break;
1860 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301861 {
1862 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1863 if (lastParamNode->getType().getBasicType() == EbtBool)
1864 {
1865 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1866 // so use emulated version.
1867 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001868 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301869 }
1870 else
1871 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001872 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301873 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001874 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301875 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001876 case EOpStep:
1877 outputTriplet(out, visit, "step(", ", ", ")");
1878 break;
1879 case EOpSmoothStep:
1880 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1881 break;
1882 case EOpDistance:
1883 outputTriplet(out, visit, "distance(", ", ", ")");
1884 break;
1885 case EOpDot:
1886 outputTriplet(out, visit, "dot(", ", ", ")");
1887 break;
1888 case EOpCross:
1889 outputTriplet(out, visit, "cross(", ", ", ")");
1890 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001891 case EOpFaceForward:
1892 ASSERT(node->getUseEmulatedFunction());
1893 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
1894 break;
1895 case EOpReflect:
1896 outputTriplet(out, visit, "reflect(", ", ", ")");
1897 break;
1898 case EOpRefract:
1899 outputTriplet(out, visit, "refract(", ", ", ")");
1900 break;
1901 case EOpOuterProduct:
1902 ASSERT(node->getUseEmulatedFunction());
1903 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
1904 break;
1905 case EOpMul:
1906 outputTriplet(out, visit, "(", " * ", ")");
1907 break;
1908 default:
1909 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910 }
1911
1912 return true;
1913}
1914
Olli Etuaho57961272016-09-14 13:57:46 +03001915void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001916{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001917 out << "if (";
1918
1919 node->getCondition()->traverse(this);
1920
1921 out << ")\n";
1922
Jamie Madill8c46ab12015-12-07 16:39:19 -05001923 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001924
1925 bool discard = false;
1926
1927 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001929 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001930 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001931
Olli Etuahoa6f22092015-05-08 18:31:10 +03001932 // Detect true discard
1933 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1934 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001935 else
1936 {
1937 // TODO(oetuaho): Check if the semicolon inside is necessary.
1938 // It's there as a result of conservative refactoring of the output.
1939 out << "{;}\n";
1940 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001941
Jamie Madill8c46ab12015-12-07 16:39:19 -05001942 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001943
Olli Etuahoa6f22092015-05-08 18:31:10 +03001944 if (node->getFalseBlock())
1945 {
1946 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001947
Jamie Madill8c46ab12015-12-07 16:39:19 -05001948 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001949
Olli Etuaho32db19b2016-10-04 14:43:16 +01001950 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001951 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001952
Jamie Madill8c46ab12015-12-07 16:39:19 -05001953 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001954
Olli Etuahoa6f22092015-05-08 18:31:10 +03001955 // Detect false discard
1956 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1957 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001958
Olli Etuahoa6f22092015-05-08 18:31:10 +03001959 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001960 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001961 {
1962 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001963 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001964}
1965
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001966bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1967{
1968 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1969 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1970 UNREACHABLE();
1971 return false;
1972}
1973
Olli Etuaho57961272016-09-14 13:57:46 +03001974bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001975{
1976 TInfoSinkBase &out = getInfoSink();
1977
Olli Etuaho3d932d82016-04-12 11:10:30 +03001978 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001979
1980 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001981 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001982 {
1983 out << "FLATTEN ";
1984 }
1985
Olli Etuaho57961272016-09-14 13:57:46 +03001986 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987
1988 return false;
1989}
1990
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001991bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02001992{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001993 TInfoSinkBase &out = getInfoSink();
1994
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001995 if (node->getStatementList())
1996 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02001997 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05001998 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001999 // The curly braces get written when visiting the statementList aggregate
2000 }
2001 else
2002 {
2003 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002004 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002005 }
2006 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002007}
2008
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002009bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002010{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002011 TInfoSinkBase &out = getInfoSink();
2012
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002013 if (node->hasCondition())
2014 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002015 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002016 return true;
2017 }
2018 else
2019 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002020 out << "default:\n";
2021 return false;
2022 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002023}
2024
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002025void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2026{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002027 TInfoSinkBase &out = getInfoSink();
2028 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002029}
2030
2031bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2032{
Nicolas Capens655fe362014-04-11 13:12:34 -04002033 mNestedLoopDepth++;
2034
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002035 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002036 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002037 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002038
Jamie Madill8c46ab12015-12-07 16:39:19 -05002039 TInfoSinkBase &out = getInfoSink();
2040
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002041 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002042 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002043 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002044 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002045 mInsideDiscontinuousLoop = wasDiscontinuous;
2046 mNestedLoopDepth--;
2047
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002048 return false;
2049 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002050 }
2051
Corentin Wallez1239ee92015-03-19 14:38:02 -07002052 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002053 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002054 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002055 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002056
Jamie Madill8c46ab12015-12-07 16:39:19 -05002057 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002058 }
2059 else
2060 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002061 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002062
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063 if (node->getInit())
2064 {
2065 node->getInit()->traverse(this);
2066 }
2067
2068 out << "; ";
2069
alokp@chromium.org52813552010-11-16 18:36:09 +00002070 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002072 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073 }
2074
2075 out << "; ";
2076
alokp@chromium.org52813552010-11-16 18:36:09 +00002077 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002079 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080 }
2081
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002082 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002083
Jamie Madill8c46ab12015-12-07 16:39:19 -05002084 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002085 }
2086
2087 if (node->getBody())
2088 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002089 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002090 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002091 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002092 else
2093 {
2094 // TODO(oetuaho): Check if the semicolon inside is necessary.
2095 // It's there as a result of conservative refactoring of the output.
2096 out << "{;}\n";
2097 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098
Jamie Madill8c46ab12015-12-07 16:39:19 -05002099 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100
alokp@chromium.org52813552010-11-16 18:36:09 +00002101 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002103 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104 out << "while(\n";
2105
alokp@chromium.org52813552010-11-16 18:36:09 +00002106 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107
daniel@transgaming.com73536982012-03-21 20:45:49 +00002108 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109 }
2110
daniel@transgaming.com73536982012-03-21 20:45:49 +00002111 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002113 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002114 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002115
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116 return false;
2117}
2118
2119bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2120{
Jamie Madill32aab012015-01-27 14:12:26 -05002121 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122
2123 switch (node->getFlowOp())
2124 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002125 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002126 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002127 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002128 case EOpBreak:
2129 if (visit == PreVisit)
2130 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002131 if (mNestedLoopDepth > 1)
2132 {
2133 mUsesNestedBreak = true;
2134 }
2135
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002136 if (mExcessiveLoopIndex)
2137 {
2138 out << "{Break";
2139 mExcessiveLoopIndex->traverse(this);
2140 out << " = true; break;}\n";
2141 }
2142 else
2143 {
2144 out << "break;\n";
2145 }
2146 }
2147 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002148 case EOpContinue:
2149 outputTriplet(out, visit, "continue;\n", "", "");
2150 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151 case EOpReturn:
2152 if (visit == PreVisit)
2153 {
2154 if (node->getExpression())
2155 {
2156 out << "return ";
2157 }
2158 else
2159 {
2160 out << "return;\n";
2161 }
2162 }
2163 else if (visit == PostVisit)
2164 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002165 if (node->getExpression())
2166 {
2167 out << ";\n";
2168 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002169 }
2170 break;
2171 default: UNREACHABLE();
2172 }
2173
2174 return true;
2175}
2176
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002177bool OutputHLSL::isSingleStatement(TIntermNode *node)
2178{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002179 if (node->getAsBlock())
2180 {
2181 return false;
2182 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002183
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002184 TIntermAggregate *aggregate = node->getAsAggregate();
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002185 if (aggregate)
2186 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002187 if (aggregate->getOp() == EOpDeclaration)
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002188 {
2189 // Declaring multiple comma-separated variables must be considered multiple statements
2190 // because each individual declaration has side effects which are visible in the next.
2191 return false;
2192 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002193 else
2194 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002195 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002196 {
2197 if (!isSingleStatement(*sit))
2198 {
2199 return false;
2200 }
2201 }
2202
2203 return true;
2204 }
2205 }
2206
2207 return true;
2208}
2209
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002210// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2211// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002212bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002213{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002214 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002215
2216 // Parse loops of the form:
2217 // for(int index = initial; index [comparator] limit; index += increment)
2218 TIntermSymbol *index = NULL;
2219 TOperator comparator = EOpNull;
2220 int initial = 0;
2221 int limit = 0;
2222 int increment = 0;
2223
2224 // Parse index name and intial value
2225 if (node->getInit())
2226 {
2227 TIntermAggregate *init = node->getInit()->getAsAggregate();
2228
2229 if (init)
2230 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002231 TIntermSequence *sequence = init->getSequence();
2232 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002233
2234 if (variable && variable->getQualifier() == EvqTemporary)
2235 {
2236 TIntermBinary *assign = variable->getAsBinaryNode();
2237
2238 if (assign->getOp() == EOpInitialize)
2239 {
2240 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2241 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2242
2243 if (symbol && constant)
2244 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002245 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002246 {
2247 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002248 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002249 }
2250 }
2251 }
2252 }
2253 }
2254 }
2255
2256 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002257 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002258 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002259 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002260
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002261 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2262 {
2263 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2264
2265 if (constant)
2266 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002267 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002268 {
2269 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002270 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002271 }
2272 }
2273 }
2274 }
2275
2276 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002277 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002278 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002279 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2280 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002281
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002282 if (binaryTerminal)
2283 {
2284 TOperator op = binaryTerminal->getOp();
2285 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2286
2287 if (constant)
2288 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002289 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002290 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002291 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002292
2293 switch (op)
2294 {
2295 case EOpAddAssign: increment = value; break;
2296 case EOpSubAssign: increment = -value; break;
2297 default: UNIMPLEMENTED();
2298 }
2299 }
2300 }
2301 }
2302 else if (unaryTerminal)
2303 {
2304 TOperator op = unaryTerminal->getOp();
2305
2306 switch (op)
2307 {
2308 case EOpPostIncrement: increment = 1; break;
2309 case EOpPostDecrement: increment = -1; break;
2310 case EOpPreIncrement: increment = 1; break;
2311 case EOpPreDecrement: increment = -1; break;
2312 default: UNIMPLEMENTED();
2313 }
2314 }
2315 }
2316
2317 if (index != NULL && comparator != EOpNull && increment != 0)
2318 {
2319 if (comparator == EOpLessThanEqual)
2320 {
2321 comparator = EOpLessThan;
2322 limit += 1;
2323 }
2324
2325 if (comparator == EOpLessThan)
2326 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002327 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002328
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002329 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002330 {
2331 return false; // Not an excessive loop
2332 }
2333
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002334 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2335 mExcessiveLoopIndex = index;
2336
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002337 out << "{int ";
2338 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002339 out << ";\n"
2340 "bool Break";
2341 index->traverse(this);
2342 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002343
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002344 bool firstLoopFragment = true;
2345
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002346 while (iterations > 0)
2347 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002348 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002349
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002350 if (!firstLoopFragment)
2351 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002352 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002353 index->traverse(this);
2354 out << ") {\n";
2355 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002356
2357 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2358 {
2359 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2360 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002361
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002362 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002363 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002364
Corentin Wallez1239ee92015-03-19 14:38:02 -07002365 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002366 index->traverse(this);
2367 out << " = ";
2368 out << initial;
2369
2370 out << "; ";
2371 index->traverse(this);
2372 out << " < ";
2373 out << clampedLimit;
2374
2375 out << "; ";
2376 index->traverse(this);
2377 out << " += ";
2378 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002379 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002380
Jamie Madill8c46ab12015-12-07 16:39:19 -05002381 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002382 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002383
2384 if (node->getBody())
2385 {
2386 node->getBody()->traverse(this);
2387 }
2388
Jamie Madill8c46ab12015-12-07 16:39:19 -05002389 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002390 out << ";}\n";
2391
2392 if (!firstLoopFragment)
2393 {
2394 out << "}\n";
2395 }
2396
2397 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002398
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002399 initial += MAX_LOOP_ITERATIONS * increment;
2400 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002402
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002403 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002404
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002405 mExcessiveLoopIndex = restoreIndex;
2406
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002407 return true;
2408 }
2409 else UNIMPLEMENTED();
2410 }
2411
2412 return false; // Not handled as an excessive loop
2413}
2414
Jamie Madill8c46ab12015-12-07 16:39:19 -05002415void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2416 Visit visit,
2417 const char *preString,
2418 const char *inString,
2419 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002421 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422 {
2423 out << preString;
2424 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002425 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002426 {
2427 out << inString;
2428 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002429 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430 {
2431 out << postString;
2432 }
2433}
2434
Jamie Madill8c46ab12015-12-07 16:39:19 -05002435void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002436{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002437 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002438 {
Jamie Madill32aab012015-01-27 14:12:26 -05002439 out << "\n";
2440 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002441
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002442 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002443 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002444 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002445 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002446
Jamie Madill32aab012015-01-27 14:12:26 -05002447 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002448 }
2449}
2450
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002451TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2452{
2453 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002454 const TType &type = symbol->getType();
2455 const TName &name = symbol->getName();
2456 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002457
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002458 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002459 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002460 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002461 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002462 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002463 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002464 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002465 }
2466
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002467 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002468 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002469 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2470 {
2471 // Samplers are passed as indices to the sampler array.
2472 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2473 return "const uint " + nameStr + ArrayString(type);
2474 }
2475 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2476 {
2477 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2478 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2479 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2480 ArrayString(type);
2481 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002482 }
2483
Olli Etuaho96963162016-03-21 11:54:33 +02002484 TStringStream argString;
2485 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2486 << ArrayString(type);
2487
2488 // If the structure parameter contains samplers, they need to be passed into the function as
2489 // separate parameters. HLSL doesn't natively support samplers in structs.
2490 if (type.isStructureContainingSamplers())
2491 {
2492 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2493 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002494 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002495 for (const TIntermSymbol *sampler : samplerSymbols)
2496 {
2497 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2498 {
2499 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2500 }
2501 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2502 {
2503 const TType &samplerType = sampler->getType();
2504 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2505 type.getArraySize() == samplerType.getArraySize());
2506 ASSERT(IsSampler(samplerType.getBasicType()));
2507 argString << ", " << QualifierString(qualifier) << " "
2508 << TextureString(samplerType.getBasicType()) << " texture_"
2509 << sampler->getSymbol() << ArrayString(type) << ", "
2510 << QualifierString(qualifier) << " "
2511 << SamplerString(samplerType.getBasicType()) << " sampler_"
2512 << sampler->getSymbol() << ArrayString(type);
2513 }
2514 else
2515 {
2516 const TType &samplerType = sampler->getType();
2517 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2518 type.getArraySize() == samplerType.getArraySize());
2519 ASSERT(IsSampler(samplerType.getBasicType()));
2520 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2521 << " " << sampler->getSymbol() << ArrayString(type);
2522 }
2523 }
2524 }
2525
2526 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527}
2528
2529TString OutputHLSL::initializer(const TType &type)
2530{
2531 TString string;
2532
Jamie Madill94bf7f22013-07-08 13:31:15 -04002533 size_t size = type.getObjectSize();
2534 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002535 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002536 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537
Jamie Madill94bf7f22013-07-08 13:31:15 -04002538 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002539 {
2540 string += ", ";
2541 }
2542 }
2543
daniel@transgaming.comead23042010-04-29 03:35:36 +00002544 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002545}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002546
Jamie Madill8c46ab12015-12-07 16:39:19 -05002547void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2548 Visit visit,
2549 const TType &type,
2550 const char *name,
2551 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002552{
Olli Etuahof40319e2015-03-10 14:33:00 +02002553 if (type.isArray())
2554 {
2555 UNIMPLEMENTED();
2556 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002557
2558 if (visit == PreVisit)
2559 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002560 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002561
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002562 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002563 }
2564 else if (visit == InVisit)
2565 {
2566 out << ", ";
2567 }
2568 else if (visit == PostVisit)
2569 {
2570 out << ")";
2571 }
2572}
2573
Jamie Madill8c46ab12015-12-07 16:39:19 -05002574const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2575 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002576 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002577{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002578 const TConstantUnion *constUnionIterated = constUnion;
2579
Jamie Madill98493dd2013-07-08 14:39:03 -04002580 const TStructure* structure = type.getStruct();
2581 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002582 {
Jamie Madill033dae62014-06-18 12:56:28 -04002583 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002584
Jamie Madill98493dd2013-07-08 14:39:03 -04002585 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002586
Jamie Madill98493dd2013-07-08 14:39:03 -04002587 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002588 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002589 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002590 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002591
Jamie Madill98493dd2013-07-08 14:39:03 -04002592 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002593 {
2594 out << ", ";
2595 }
2596 }
2597
2598 out << ")";
2599 }
2600 else
2601 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002602 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002603 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002604
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002605 if (writeType)
2606 {
Jamie Madill033dae62014-06-18 12:56:28 -04002607 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002608 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002609 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002610 if (writeType)
2611 {
2612 out << ")";
2613 }
2614 }
2615
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002616 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002617}
2618
Jamie Madill8c46ab12015-12-07 16:39:19 -05002619void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002620{
2621 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002622 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002623}
2624
Jamie Madill37997142015-01-28 10:06:34 -05002625bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2626{
2627 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2628 expression->traverse(&searchSymbol);
2629
2630 if (searchSymbol.foundMatch())
2631 {
2632 // Type already printed
2633 out << "t" + str(mUniqueIndex) + " = ";
2634 expression->traverse(this);
2635 out << ", ";
2636 symbolNode->traverse(this);
2637 out << " = t" + str(mUniqueIndex);
2638
2639 mUniqueIndex++;
2640 return true;
2641 }
2642
2643 return false;
2644}
2645
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002646bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2647{
2648 // We support writing constant unions and constructors that only take constant unions as
2649 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002650 return expression->getAsConstantUnion() ||
2651 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002652}
2653
2654bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2655 TIntermSymbol *symbolNode,
2656 TIntermTyped *expression)
2657{
2658 if (canWriteAsHLSLLiteral(expression))
2659 {
2660 symbolNode->traverse(this);
2661 if (expression->getType().isArray())
2662 {
2663 out << "[" << expression->getType().getArraySize() << "]";
2664 }
2665 out << " = {";
2666 if (expression->getAsConstantUnion())
2667 {
2668 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2669 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2670 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2671 }
2672 else
2673 {
2674 TIntermAggregate *constructor = expression->getAsAggregate();
2675 ASSERT(constructor != nullptr);
2676 for (TIntermNode *&node : *constructor->getSequence())
2677 {
2678 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2679 ASSERT(nodeConst);
2680 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2681 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2682 if (node != constructor->getSequence()->back())
2683 {
2684 out << ", ";
2685 }
2686 }
2687 }
2688 out << "}";
2689 return true;
2690 }
2691 return false;
2692}
2693
Jamie Madill55e79e02015-02-09 15:35:00 -05002694TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2695{
2696 const TFieldList &fields = structure.fields();
2697
2698 for (const auto &eqFunction : mStructEqualityFunctions)
2699 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002700 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002701 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002702 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002703 }
2704 }
2705
2706 const TString &structNameString = StructNameString(structure);
2707
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002708 StructEqualityFunction *function = new StructEqualityFunction();
2709 function->structure = &structure;
2710 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002711
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002712 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002713
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002714 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2715 << "{\n"
2716 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002717
2718 for (size_t i = 0; i < fields.size(); i++)
2719 {
2720 const TField *field = fields[i];
2721 const TType *fieldType = field->type();
2722
2723 const TString &fieldNameA = "a." + Decorate(field->name());
2724 const TString &fieldNameB = "b." + Decorate(field->name());
2725
2726 if (i > 0)
2727 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002728 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002729 }
2730
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002731 fnOut << "(";
2732 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2733 fnOut << fieldNameA;
2734 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2735 fnOut << fieldNameB;
2736 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2737 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002738 }
2739
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002740 fnOut << ";\n" << "}\n";
2741
2742 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002743
2744 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002745 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002746
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002747 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002748}
2749
Olli Etuaho7fb49552015-03-18 17:27:44 +02002750TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2751{
2752 for (const auto &eqFunction : mArrayEqualityFunctions)
2753 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002754 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002755 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002756 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002757 }
2758 }
2759
2760 const TString &typeName = TypeString(type);
2761
Olli Etuaho12690762015-03-31 12:55:28 +03002762 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002763 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002764
2765 TInfoSinkBase fnNameOut;
2766 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002767 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002768
2769 TType nonArrayType = type;
2770 nonArrayType.clearArrayness();
2771
2772 TInfoSinkBase fnOut;
2773
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002774 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002775 << typeName << " a[" << type.getArraySize() << "], "
2776 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002777 << "{\n"
2778 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2779 " {\n"
2780 " if (";
2781
2782 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2783 fnOut << "a[i]";
2784 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2785 fnOut << "b[i]";
2786 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2787
2788 fnOut << ") { return false; }\n"
2789 " }\n"
2790 " return true;\n"
2791 "}\n";
2792
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002793 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002794
2795 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002796 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002797
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002798 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002799}
2800
Olli Etuaho12690762015-03-31 12:55:28 +03002801TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2802{
2803 for (const auto &assignFunction : mArrayAssignmentFunctions)
2804 {
2805 if (assignFunction.type == type)
2806 {
2807 return assignFunction.functionName;
2808 }
2809 }
2810
2811 const TString &typeName = TypeString(type);
2812
2813 ArrayHelperFunction function;
2814 function.type = type;
2815
2816 TInfoSinkBase fnNameOut;
2817 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2818 function.functionName = fnNameOut.c_str();
2819
2820 TInfoSinkBase fnOut;
2821
2822 fnOut << "void " << function.functionName << "(out "
2823 << typeName << " a[" << type.getArraySize() << "], "
2824 << typeName << " b[" << type.getArraySize() << "])\n"
2825 << "{\n"
2826 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2827 " {\n"
2828 " a[i] = b[i];\n"
2829 " }\n"
2830 "}\n";
2831
2832 function.functionDefinition = fnOut.c_str();
2833
2834 mArrayAssignmentFunctions.push_back(function);
2835
2836 return function.functionName;
2837}
2838
Olli Etuaho9638c352015-04-01 14:34:52 +03002839TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2840{
2841 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2842 {
2843 if (constructIntoFunction.type == type)
2844 {
2845 return constructIntoFunction.functionName;
2846 }
2847 }
2848
2849 const TString &typeName = TypeString(type);
2850
2851 ArrayHelperFunction function;
2852 function.type = type;
2853
2854 TInfoSinkBase fnNameOut;
2855 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2856 function.functionName = fnNameOut.c_str();
2857
2858 TInfoSinkBase fnOut;
2859
2860 fnOut << "void " << function.functionName << "(out "
2861 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002862 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002863 {
2864 fnOut << ", " << typeName << " b" << i;
2865 }
2866 fnOut << ")\n"
2867 "{\n";
2868
Olli Etuaho856c4972016-08-08 11:38:39 +03002869 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002870 {
2871 fnOut << " a[" << i << "] = b" << i << ";\n";
2872 }
2873 fnOut << "}\n";
2874
2875 function.functionDefinition = fnOut.c_str();
2876
2877 mArrayConstructIntoFunctions.push_back(function);
2878
2879 return function.functionName;
2880}
2881
Jamie Madill2e295e22015-04-29 10:41:33 -04002882void OutputHLSL::ensureStructDefined(const TType &type)
2883{
2884 TStructure *structure = type.getStruct();
2885
2886 if (structure)
2887 {
2888 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2889 }
2890}
2891
2892
Olli Etuaho9638c352015-04-01 14:34:52 +03002893
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002894}