blob: feae684d580bc26d436868917704c31e1bc1fa18 [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 Etuaho4db7ded2016-10-13 12:23:11 +0100870 case EOpComma:
871 outputTriplet(out, visit, "(", ", ", ")");
872 break;
873 case EOpAssign:
874 if (node->getLeft()->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +0300875 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100876 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
877 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +0300878 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100879 const TString &functionName = addArrayConstructIntoFunction(node->getType());
880 out << functionName << "(";
881 node->getLeft()->traverse(this);
882 TIntermSequence *seq = rightAgg->getSequence();
883 for (auto &arrayElement : *seq)
884 {
885 out << ", ";
886 arrayElement->traverse(this);
887 }
888 out << ")";
889 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +0300890 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100891 // ArrayReturnValueToOutParameter should have eliminated expressions where a
892 // function call is assigned.
893 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
894
895 const TString &functionName = addArrayAssignmentFunction(node->getType());
896 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho9638c352015-04-01 14:34:52 +0300897 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100898 else
Jamie Madill37997142015-01-28 10:06:34 -0500899 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100900 outputTriplet(out, visit, "(", " = ", ")");
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000901 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100902 break;
903 case EOpInitialize:
904 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200905 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100906 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
907 ASSERT(symbolNode);
908 TIntermTyped *expression = node->getRight();
909
910 // Global initializers must be constant at this point.
911 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
912 canWriteAsHLSLLiteral(expression));
913
914 // GLSL allows to write things like "float x = x;" where a new variable x is defined
915 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
916 // new variable is created before the assignment is evaluated), so we need to
917 // convert
918 // this to "float t = x, x = t;".
919 if (writeSameSymbolInitializer(out, symbolNode, expression))
920 {
921 // Skip initializing the rest of the expression
922 return false;
923 }
924 else if (writeConstantInitialization(out, symbolNode, expression))
925 {
926 return false;
927 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200928 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100929 else if (visit == InVisit)
930 {
931 out << " = ";
932 }
933 break;
934 case EOpAddAssign:
935 outputTriplet(out, visit, "(", " += ", ")");
936 break;
937 case EOpSubAssign:
938 outputTriplet(out, visit, "(", " -= ", ")");
939 break;
940 case EOpMulAssign:
941 outputTriplet(out, visit, "(", " *= ", ")");
942 break;
943 case EOpVectorTimesScalarAssign:
944 outputTriplet(out, visit, "(", " *= ", ")");
945 break;
946 case EOpMatrixTimesScalarAssign:
947 outputTriplet(out, visit, "(", " *= ", ")");
948 break;
949 case EOpVectorTimesMatrixAssign:
950 if (visit == PreVisit)
951 {
952 out << "(";
953 }
954 else if (visit == InVisit)
955 {
956 out << " = mul(";
957 node->getLeft()->traverse(this);
958 out << ", transpose(";
959 }
960 else
961 {
962 out << ")))";
963 }
964 break;
965 case EOpMatrixTimesMatrixAssign:
966 if (visit == PreVisit)
967 {
968 out << "(";
969 }
970 else if (visit == InVisit)
971 {
972 out << " = transpose(mul(transpose(";
973 node->getLeft()->traverse(this);
974 out << "), transpose(";
975 }
976 else
977 {
978 out << "))))";
979 }
980 break;
981 case EOpDivAssign:
982 outputTriplet(out, visit, "(", " /= ", ")");
983 break;
984 case EOpIModAssign:
985 outputTriplet(out, visit, "(", " %= ", ")");
986 break;
987 case EOpBitShiftLeftAssign:
988 outputTriplet(out, visit, "(", " <<= ", ")");
989 break;
990 case EOpBitShiftRightAssign:
991 outputTriplet(out, visit, "(", " >>= ", ")");
992 break;
993 case EOpBitwiseAndAssign:
994 outputTriplet(out, visit, "(", " &= ", ")");
995 break;
996 case EOpBitwiseXorAssign:
997 outputTriplet(out, visit, "(", " ^= ", ")");
998 break;
999 case EOpBitwiseOrAssign:
1000 outputTriplet(out, visit, "(", " |= ", ")");
1001 break;
1002 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001003 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001004 const TType& leftType = node->getLeft()->getType();
1005 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001006 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001007 if (visit == PreVisit)
1008 {
1009 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1010 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001011 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001012 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001013 return false;
1014 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001015 }
Olli Etuaho96963162016-03-21 11:54:33 +02001016 else if (ancestorEvaluatesToSamplerInStruct(visit))
1017 {
1018 // All parts of an expression that access a sampler in a struct need to use _ as
1019 // separator to access the sampler variable that has been moved out of the struct.
1020 outputTriplet(out, visit, "", "_", "");
1021 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001022 else
1023 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001024 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001025 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001026 }
1027 break;
1028 case EOpIndexIndirect:
1029 // We do not currently support indirect references to interface blocks
1030 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001031 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001032 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001033 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001034 {
1035 const TStructure* structure = node->getLeft()->getType().getStruct();
1036 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1037 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001038
Olli Etuaho96963162016-03-21 11:54:33 +02001039 // In cases where indexing returns a sampler, we need to access the sampler variable
1040 // that has been moved out of the struct.
1041 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1042 if (visit == PreVisit && indexingReturnsSampler)
1043 {
1044 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1045 // This prefix is only output at the beginning of the indexing expression, which
1046 // may have multiple parts.
1047 out << "angle";
1048 }
1049 if (!indexingReturnsSampler)
1050 {
1051 // All parts of an expression that access a sampler in a struct need to use _ as
1052 // separator to access the sampler variable that has been moved out of the struct.
1053 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1054 }
1055 if (visit == InVisit)
1056 {
1057 if (indexingReturnsSampler)
1058 {
1059 out << "_" + field->name();
1060 }
1061 else
1062 {
1063 out << "." + DecorateField(field->name(), *structure);
1064 }
1065
1066 return false;
1067 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001068 }
1069 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001070 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001071 if (visit == InVisit)
1072 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001073 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1074 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1075 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001076 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001077
1078 return false;
1079 }
1080 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001081 case EOpAdd:
1082 outputTriplet(out, visit, "(", " + ", ")");
1083 break;
1084 case EOpSub:
1085 outputTriplet(out, visit, "(", " - ", ")");
1086 break;
1087 case EOpMul:
1088 outputTriplet(out, visit, "(", " * ", ")");
1089 break;
1090 case EOpDiv:
1091 outputTriplet(out, visit, "(", " / ", ")");
1092 break;
1093 case EOpIMod:
1094 outputTriplet(out, visit, "(", " % ", ")");
1095 break;
1096 case EOpBitShiftLeft:
1097 outputTriplet(out, visit, "(", " << ", ")");
1098 break;
1099 case EOpBitShiftRight:
1100 outputTriplet(out, visit, "(", " >> ", ")");
1101 break;
1102 case EOpBitwiseAnd:
1103 outputTriplet(out, visit, "(", " & ", ")");
1104 break;
1105 case EOpBitwiseXor:
1106 outputTriplet(out, visit, "(", " ^ ", ")");
1107 break;
1108 case EOpBitwiseOr:
1109 outputTriplet(out, visit, "(", " | ", ")");
1110 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001111 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001112 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001113 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001114 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001115 case EOpLessThan:
1116 outputTriplet(out, visit, "(", " < ", ")");
1117 break;
1118 case EOpGreaterThan:
1119 outputTriplet(out, visit, "(", " > ", ")");
1120 break;
1121 case EOpLessThanEqual:
1122 outputTriplet(out, visit, "(", " <= ", ")");
1123 break;
1124 case EOpGreaterThanEqual:
1125 outputTriplet(out, visit, "(", " >= ", ")");
1126 break;
1127 case EOpVectorTimesScalar:
1128 outputTriplet(out, visit, "(", " * ", ")");
1129 break;
1130 case EOpMatrixTimesScalar:
1131 outputTriplet(out, visit, "(", " * ", ")");
1132 break;
1133 case EOpVectorTimesMatrix:
1134 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1135 break;
1136 case EOpMatrixTimesVector:
1137 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1138 break;
1139 case EOpMatrixTimesMatrix:
1140 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1141 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001142 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001143 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1144 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001145 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001146 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001147 case EOpLogicalXor:
1148 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001149 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001150 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001151 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001152 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1153 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001154 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001155 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001156 default: UNREACHABLE();
1157 }
1158
1159 return true;
1160}
1161
1162bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1163{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001164 TInfoSinkBase &out = getInfoSink();
1165
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001166 switch (node->getOp())
1167 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001168 case EOpNegative:
1169 outputTriplet(out, visit, "(-", "", ")");
1170 break;
1171 case EOpPositive:
1172 outputTriplet(out, visit, "(+", "", ")");
1173 break;
1174 case EOpVectorLogicalNot:
1175 outputTriplet(out, visit, "(!", "", ")");
1176 break;
1177 case EOpLogicalNot:
1178 outputTriplet(out, visit, "(!", "", ")");
1179 break;
1180 case EOpBitwiseNot:
1181 outputTriplet(out, visit, "(~", "", ")");
1182 break;
1183 case EOpPostIncrement:
1184 outputTriplet(out, visit, "(", "", "++)");
1185 break;
1186 case EOpPostDecrement:
1187 outputTriplet(out, visit, "(", "", "--)");
1188 break;
1189 case EOpPreIncrement:
1190 outputTriplet(out, visit, "(++", "", ")");
1191 break;
1192 case EOpPreDecrement:
1193 outputTriplet(out, visit, "(--", "", ")");
1194 break;
1195 case EOpRadians:
1196 outputTriplet(out, visit, "radians(", "", ")");
1197 break;
1198 case EOpDegrees:
1199 outputTriplet(out, visit, "degrees(", "", ")");
1200 break;
1201 case EOpSin:
1202 outputTriplet(out, visit, "sin(", "", ")");
1203 break;
1204 case EOpCos:
1205 outputTriplet(out, visit, "cos(", "", ")");
1206 break;
1207 case EOpTan:
1208 outputTriplet(out, visit, "tan(", "", ")");
1209 break;
1210 case EOpAsin:
1211 outputTriplet(out, visit, "asin(", "", ")");
1212 break;
1213 case EOpAcos:
1214 outputTriplet(out, visit, "acos(", "", ")");
1215 break;
1216 case EOpAtan:
1217 outputTriplet(out, visit, "atan(", "", ")");
1218 break;
1219 case EOpSinh:
1220 outputTriplet(out, visit, "sinh(", "", ")");
1221 break;
1222 case EOpCosh:
1223 outputTriplet(out, visit, "cosh(", "", ")");
1224 break;
1225 case EOpTanh:
1226 outputTriplet(out, visit, "tanh(", "", ")");
1227 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001228 case EOpAsinh:
1229 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001230 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001231 break;
1232 case EOpAcosh:
1233 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001234 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001235 break;
1236 case EOpAtanh:
1237 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001238 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001239 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001240 case EOpExp:
1241 outputTriplet(out, visit, "exp(", "", ")");
1242 break;
1243 case EOpLog:
1244 outputTriplet(out, visit, "log(", "", ")");
1245 break;
1246 case EOpExp2:
1247 outputTriplet(out, visit, "exp2(", "", ")");
1248 break;
1249 case EOpLog2:
1250 outputTriplet(out, visit, "log2(", "", ")");
1251 break;
1252 case EOpSqrt:
1253 outputTriplet(out, visit, "sqrt(", "", ")");
1254 break;
1255 case EOpInverseSqrt:
1256 outputTriplet(out, visit, "rsqrt(", "", ")");
1257 break;
1258 case EOpAbs:
1259 outputTriplet(out, visit, "abs(", "", ")");
1260 break;
1261 case EOpSign:
1262 outputTriplet(out, visit, "sign(", "", ")");
1263 break;
1264 case EOpFloor:
1265 outputTriplet(out, visit, "floor(", "", ")");
1266 break;
1267 case EOpTrunc:
1268 outputTriplet(out, visit, "trunc(", "", ")");
1269 break;
1270 case EOpRound:
1271 outputTriplet(out, visit, "round(", "", ")");
1272 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001273 case EOpRoundEven:
1274 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001275 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001276 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001277 case EOpCeil:
1278 outputTriplet(out, visit, "ceil(", "", ")");
1279 break;
1280 case EOpFract:
1281 outputTriplet(out, visit, "frac(", "", ")");
1282 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301283 case EOpIsNan:
Shao6f0a0dc2016-09-27 13:51:29 +08001284 if (node->getUseEmulatedFunction())
1285 writeEmulatedFunctionTriplet(out, visit, "isnan(");
1286 else
1287 outputTriplet(out, visit, "isnan(", "", ")");
1288 mRequiresIEEEStrictCompiling = true;
1289 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001290 case EOpIsInf:
1291 outputTriplet(out, visit, "isinf(", "", ")");
1292 break;
1293 case EOpFloatBitsToInt:
1294 outputTriplet(out, visit, "asint(", "", ")");
1295 break;
1296 case EOpFloatBitsToUint:
1297 outputTriplet(out, visit, "asuint(", "", ")");
1298 break;
1299 case EOpIntBitsToFloat:
1300 outputTriplet(out, visit, "asfloat(", "", ")");
1301 break;
1302 case EOpUintBitsToFloat:
1303 outputTriplet(out, visit, "asfloat(", "", ")");
1304 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001305 case EOpPackSnorm2x16:
1306 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001307 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001308 break;
1309 case EOpPackUnorm2x16:
1310 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001311 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001312 break;
1313 case EOpPackHalf2x16:
1314 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001315 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001316 break;
1317 case EOpUnpackSnorm2x16:
1318 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001319 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001320 break;
1321 case EOpUnpackUnorm2x16:
1322 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001323 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001324 break;
1325 case EOpUnpackHalf2x16:
1326 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001327 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001328 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001329 case EOpLength:
1330 outputTriplet(out, visit, "length(", "", ")");
1331 break;
1332 case EOpNormalize:
1333 outputTriplet(out, visit, "normalize(", "", ")");
1334 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001335 case EOpDFdx:
1336 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1337 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001338 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001339 }
1340 else
1341 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001342 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001343 }
1344 break;
1345 case EOpDFdy:
1346 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1347 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001348 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001349 }
1350 else
1351 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001352 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001353 }
1354 break;
1355 case EOpFwidth:
1356 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1357 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001358 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001359 }
1360 else
1361 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001362 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001363 }
1364 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001365 case EOpTranspose:
1366 outputTriplet(out, visit, "transpose(", "", ")");
1367 break;
1368 case EOpDeterminant:
1369 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1370 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001371 case EOpInverse:
1372 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001373 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001374 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001375
Jamie Madill8c46ab12015-12-07 16:39:19 -05001376 case EOpAny:
1377 outputTriplet(out, visit, "any(", "", ")");
1378 break;
1379 case EOpAll:
1380 outputTriplet(out, visit, "all(", "", ")");
1381 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001382 default: UNREACHABLE();
1383 }
1384
1385 return true;
1386}
1387
Olli Etuaho96963162016-03-21 11:54:33 +02001388TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1389{
1390 if (node->getAsSymbolNode())
1391 {
1392 return node->getAsSymbolNode()->getSymbol();
1393 }
1394 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1395 switch (nodeBinary->getOp())
1396 {
1397 case EOpIndexDirect:
1398 {
1399 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1400
1401 TInfoSinkBase prefixSink;
1402 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1403 return TString(prefixSink.c_str());
1404 }
1405 case EOpIndexDirectStruct:
1406 {
1407 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1408 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1409 const TField *field = s->fields()[index];
1410
1411 TInfoSinkBase prefixSink;
1412 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1413 << field->name();
1414 return TString(prefixSink.c_str());
1415 }
1416 default:
1417 UNREACHABLE();
1418 return TString("");
1419 }
1420}
1421
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001422bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1423{
1424 TInfoSinkBase &out = getInfoSink();
1425
1426 if (mInsideFunction)
1427 {
1428 outputLineDirective(out, node->getLine().first_line);
1429 out << "{\n";
1430 }
1431
1432 for (TIntermSequence::iterator sit = node->getSequence()->begin();
1433 sit != node->getSequence()->end(); sit++)
1434 {
1435 outputLineDirective(out, (*sit)->getLine().first_line);
1436
1437 (*sit)->traverse(this);
1438
1439 // Don't output ; after case labels, they're terminated by :
1440 // This is needed especially since outputting a ; after a case statement would turn empty
1441 // case statements into non-empty case statements, disallowing fall-through from them.
1442 // Also no need to output ; after if statements or sequences. This is done just for
1443 // code clarity.
1444 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
1445 (*sit)->getAsBlock() == nullptr)
1446 out << ";\n";
1447 }
1448
1449 if (mInsideFunction)
1450 {
1451 outputLineDirective(out, node->getLine().last_line);
1452 out << "}\n";
1453 }
1454
1455 return false;
1456}
1457
Olli Etuaho336b1472016-10-05 16:37:55 +01001458bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1459{
1460 TInfoSinkBase &out = getInfoSink();
1461
1462 ASSERT(mCurrentFunctionMetadata == nullptr);
1463
1464 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1465 ASSERT(index != CallDAG::InvalidIndex);
1466 mCurrentFunctionMetadata = &mASTMetadataList[index];
1467
1468 out << TypeString(node->getType()) << " ";
1469
1470 TIntermSequence *parameters = node->getFunctionParameters()->getSequence();
1471
1472 if (node->getFunctionSymbolInfo()->isMain())
1473 {
1474 out << "gl_main(";
1475 }
1476 else
1477 {
1478 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
1479 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1480 }
1481
1482 for (unsigned int i = 0; i < parameters->size(); i++)
1483 {
1484 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1485
1486 if (symbol)
1487 {
1488 ensureStructDefined(symbol->getType());
1489
1490 out << argumentString(symbol);
1491
1492 if (i < parameters->size() - 1)
1493 {
1494 out << ", ";
1495 }
1496 }
1497 else
1498 UNREACHABLE();
1499 }
1500
1501 out << ")\n";
1502
1503 mInsideFunction = true;
1504 // The function body node will output braces.
1505 node->getBody()->traverse(this);
1506 mInsideFunction = false;
1507
1508 mCurrentFunctionMetadata = nullptr;
1509
1510 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1511 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1512 {
1513 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1514 mOutputLod0Function = true;
1515 node->traverse(this);
1516 mOutputLod0Function = false;
1517 }
1518
1519 return false;
1520}
1521
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1523{
Jamie Madill32aab012015-01-27 14:12:26 -05001524 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001525
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001526 switch (node->getOp())
1527 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001528 case EOpDeclaration:
1529 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001531 TIntermSequence *sequence = node->getSequence();
1532 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1533 ASSERT(sequence->size() == 1);
daniel@transgaming.comead23042010-04-29 03:35:36 +00001534
Olli Etuaho5878f832016-10-07 10:14:58 +01001535 if (variable &&
1536 (variable->getQualifier() == EvqTemporary ||
1537 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001538 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001539 ensureStructDefined(variable->getType());
1540
1541 if (!variable->getAsSymbolNode() ||
1542 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001543 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001544 if (!mInsideFunction)
1545 {
1546 out << "static ";
1547 }
1548
1549 out << TypeString(variable->getType()) + " ";
1550
1551 TIntermSymbol *symbol = variable->getAsSymbolNode();
1552
1553 if (symbol)
1554 {
1555 symbol->traverse(this);
1556 out << ArrayString(symbol->getType());
1557 out << " = " + initializer(symbol->getType());
1558 }
1559 else
1560 {
1561 variable->traverse(this);
1562 }
Olli Etuahoa6f22092015-05-08 18:31:10 +03001563 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001564 else if (variable->getAsSymbolNode() &&
1565 variable->getAsSymbolNode()->getSymbol() ==
1566 "") // Type (struct) declaration
Olli Etuahoa6f22092015-05-08 18:31:10 +03001567 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001568 // Already added to constructor map
Olli Etuahoa6f22092015-05-08 18:31:10 +03001569 }
1570 else
Olli Etuaho5878f832016-10-07 10:14:58 +01001571 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001573 else if (variable && IsVaryingOut(variable->getQualifier()))
daniel@transgaming.comfe565152010-04-10 05:29:07 +00001574 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001575 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end();
1576 sit++)
1577 {
1578 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001579
Olli Etuaho5878f832016-10-07 10:14:58 +01001580 if (symbol)
1581 {
1582 // Vertex (output) varyings which are declared but not written to should
1583 // still be declared to allow successful linking
1584 mReferencedVaryings[symbol->getSymbol()] = symbol;
1585 }
1586 else
1587 {
1588 (*sit)->traverse(this);
1589 }
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001590 }
1591 }
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00001592
Corentin Wallez1239ee92015-03-19 14:38:02 -07001593 return false;
1594 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001595 else if (visit == InVisit)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001596 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001597 out << ", ";
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001598 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001599 break;
1600 case EOpInvariantDeclaration:
1601 // Do not do any translation
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001602 return false;
Olli Etuaho5878f832016-10-07 10:14:58 +01001603 case EOpPrototype:
1604 if (visit == PreVisit)
1605 {
Olli Etuahobd674552016-10-06 13:28:42 +01001606 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Olli Etuaho5878f832016-10-07 10:14:58 +01001607 // Skip the prototype if it is not implemented (and thus not used)
1608 if (index == CallDAG::InvalidIndex)
1609 {
1610 return false;
1611 }
1612
1613 TIntermSequence *arguments = node->getSequence();
1614
Olli Etuahobd674552016-10-06 13:28:42 +01001615 TString name =
1616 DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho5878f832016-10-07 10:14:58 +01001617 out << TypeString(node->getType()) << " " << name
1618 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
1619
1620 for (unsigned int i = 0; i < arguments->size(); i++)
1621 {
1622 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1623
1624 if (symbol)
1625 {
1626 out << argumentString(symbol);
1627
1628 if (i < arguments->size() - 1)
1629 {
1630 out << ", ";
1631 }
1632 }
1633 else
1634 UNREACHABLE();
1635 }
1636
1637 out << ");\n";
1638
1639 // Also prototype the Lod0 variant if needed
1640 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1641 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1642 {
1643 mOutputLod0Function = true;
1644 node->traverse(this);
1645 mOutputLod0Function = false;
1646 }
1647
1648 return false;
1649 }
1650 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001651 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001653 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001654
Corentin Wallez1239ee92015-03-19 14:38:02 -07001655 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001656 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001658 if (node->isArray())
1659 {
1660 UNIMPLEMENTED();
1661 }
Olli Etuahobd674552016-10-06 13:28:42 +01001662 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001663 ASSERT(index != CallDAG::InvalidIndex);
1664 lod0 &= mASTMetadataList[index].mNeedsLod0;
1665
Olli Etuahobd674552016-10-06 13:28:42 +01001666 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001667 out << DisambiguateFunctionName(node->getSequence());
1668 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669 }
Olli Etuahobd674552016-10-06 13:28:42 +01001670 else if (node->getFunctionSymbolInfo()->getNameObj().isInternal())
Olli Etuahob741c762016-06-29 15:49:22 +03001671 {
1672 // This path is used for internal functions that don't have their definitions in the
1673 // AST, such as precision emulation functions.
Olli Etuahobd674552016-10-06 13:28:42 +01001674 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001675 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001676 else
1677 {
Olli Etuahobd674552016-10-06 13:28:42 +01001678 TString name = TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001679 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001680 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1681 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1682 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1683 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001685
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001686 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001687 {
Olli Etuaho96963162016-03-21 11:54:33 +02001688 TIntermTyped *typedArg = (*arg)->getAsTyped();
1689 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001690 {
1691 out << "texture_";
1692 (*arg)->traverse(this);
1693 out << ", sampler_";
1694 }
1695
1696 (*arg)->traverse(this);
1697
Olli Etuaho96963162016-03-21 11:54:33 +02001698 if (typedArg->getType().isStructureContainingSamplers())
1699 {
1700 const TType &argType = typedArg->getType();
1701 TVector<TIntermSymbol *> samplerSymbols;
1702 TString structName = samplerNamePrefixFromStruct(typedArg);
1703 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001704 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001705 &samplerSymbols, nullptr);
1706 for (const TIntermSymbol *sampler : samplerSymbols)
1707 {
1708 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1709 {
1710 out << ", texture_" << sampler->getSymbol();
1711 out << ", sampler_" << sampler->getSymbol();
1712 }
1713 else
1714 {
1715 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1716 // of D3D9, it's the sampler variable.
1717 out << ", " + sampler->getSymbol();
1718 }
1719 }
1720 }
1721
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001722 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001723 {
1724 out << ", ";
1725 }
1726 }
1727
1728 out << ")";
1729
1730 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001731 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001732 case EOpParameters:
1733 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1734 break;
1735 case EOpConstructFloat:
1736 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1737 break;
1738 case EOpConstructVec2:
1739 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1740 break;
1741 case EOpConstructVec3:
1742 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1743 break;
1744 case EOpConstructVec4:
1745 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1746 break;
1747 case EOpConstructBool:
1748 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1749 break;
1750 case EOpConstructBVec2:
1751 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1752 break;
1753 case EOpConstructBVec3:
1754 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1755 break;
1756 case EOpConstructBVec4:
1757 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1758 break;
1759 case EOpConstructInt:
1760 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1761 break;
1762 case EOpConstructIVec2:
1763 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1764 break;
1765 case EOpConstructIVec3:
1766 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1767 break;
1768 case EOpConstructIVec4:
1769 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1770 break;
1771 case EOpConstructUInt:
1772 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1773 break;
1774 case EOpConstructUVec2:
1775 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1776 break;
1777 case EOpConstructUVec3:
1778 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1779 break;
1780 case EOpConstructUVec4:
1781 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1782 break;
1783 case EOpConstructMat2:
1784 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1785 break;
1786 case EOpConstructMat2x3:
1787 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1788 break;
1789 case EOpConstructMat2x4:
1790 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1791 break;
1792 case EOpConstructMat3x2:
1793 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1794 break;
1795 case EOpConstructMat3:
1796 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1797 break;
1798 case EOpConstructMat3x4:
1799 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1800 break;
1801 case EOpConstructMat4x2:
1802 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1803 break;
1804 case EOpConstructMat4x3:
1805 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1806 break;
1807 case EOpConstructMat4:
1808 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1809 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001810 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001811 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001812 if (node->getType().isArray())
1813 {
1814 UNIMPLEMENTED();
1815 }
Jamie Madill033dae62014-06-18 12:56:28 -04001816 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001817 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001818 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001819 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001820 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001821 case EOpLessThan:
1822 outputTriplet(out, visit, "(", " < ", ")");
1823 break;
1824 case EOpGreaterThan:
1825 outputTriplet(out, visit, "(", " > ", ")");
1826 break;
1827 case EOpLessThanEqual:
1828 outputTriplet(out, visit, "(", " <= ", ")");
1829 break;
1830 case EOpGreaterThanEqual:
1831 outputTriplet(out, visit, "(", " >= ", ")");
1832 break;
1833 case EOpVectorEqual:
1834 outputTriplet(out, visit, "(", " == ", ")");
1835 break;
1836 case EOpVectorNotEqual:
1837 outputTriplet(out, visit, "(", " != ", ")");
1838 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001839 case EOpMod:
1840 ASSERT(node->getUseEmulatedFunction());
1841 writeEmulatedFunctionTriplet(out, visit, "mod(");
1842 break;
1843 case EOpModf:
1844 outputTriplet(out, visit, "modf(", ", ", ")");
1845 break;
1846 case EOpPow:
1847 outputTriplet(out, visit, "pow(", ", ", ")");
1848 break;
1849 case EOpAtan:
1850 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1851 ASSERT(node->getUseEmulatedFunction());
1852 writeEmulatedFunctionTriplet(out, visit, "atan(");
1853 break;
1854 case EOpMin:
1855 outputTriplet(out, visit, "min(", ", ", ")");
1856 break;
1857 case EOpMax:
1858 outputTriplet(out, visit, "max(", ", ", ")");
1859 break;
1860 case EOpClamp:
1861 outputTriplet(out, visit, "clamp(", ", ", ")");
1862 break;
1863 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301864 {
1865 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1866 if (lastParamNode->getType().getBasicType() == EbtBool)
1867 {
1868 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1869 // so use emulated version.
1870 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001871 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301872 }
1873 else
1874 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001875 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301876 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001877 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301878 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001879 case EOpStep:
1880 outputTriplet(out, visit, "step(", ", ", ")");
1881 break;
1882 case EOpSmoothStep:
1883 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1884 break;
1885 case EOpDistance:
1886 outputTriplet(out, visit, "distance(", ", ", ")");
1887 break;
1888 case EOpDot:
1889 outputTriplet(out, visit, "dot(", ", ", ")");
1890 break;
1891 case EOpCross:
1892 outputTriplet(out, visit, "cross(", ", ", ")");
1893 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001894 case EOpFaceForward:
1895 ASSERT(node->getUseEmulatedFunction());
1896 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
1897 break;
1898 case EOpReflect:
1899 outputTriplet(out, visit, "reflect(", ", ", ")");
1900 break;
1901 case EOpRefract:
1902 outputTriplet(out, visit, "refract(", ", ", ")");
1903 break;
1904 case EOpOuterProduct:
1905 ASSERT(node->getUseEmulatedFunction());
1906 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
1907 break;
1908 case EOpMul:
1909 outputTriplet(out, visit, "(", " * ", ")");
1910 break;
1911 default:
1912 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913 }
1914
1915 return true;
1916}
1917
Olli Etuaho57961272016-09-14 13:57:46 +03001918void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001920 out << "if (";
1921
1922 node->getCondition()->traverse(this);
1923
1924 out << ")\n";
1925
Jamie Madill8c46ab12015-12-07 16:39:19 -05001926 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001927
1928 bool discard = false;
1929
1930 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001932 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001933 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001934
Olli Etuahoa6f22092015-05-08 18:31:10 +03001935 // Detect true discard
1936 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1937 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001938 else
1939 {
1940 // TODO(oetuaho): Check if the semicolon inside is necessary.
1941 // It's there as a result of conservative refactoring of the output.
1942 out << "{;}\n";
1943 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001944
Jamie Madill8c46ab12015-12-07 16:39:19 -05001945 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001946
Olli Etuahoa6f22092015-05-08 18:31:10 +03001947 if (node->getFalseBlock())
1948 {
1949 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001950
Jamie Madill8c46ab12015-12-07 16:39:19 -05001951 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952
Olli Etuaho32db19b2016-10-04 14:43:16 +01001953 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001954 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001955
Jamie Madill8c46ab12015-12-07 16:39:19 -05001956 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001957
Olli Etuahoa6f22092015-05-08 18:31:10 +03001958 // Detect false discard
1959 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1960 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001961
Olli Etuahoa6f22092015-05-08 18:31:10 +03001962 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001963 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001964 {
1965 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001966 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001967}
1968
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001969bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1970{
1971 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1972 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1973 UNREACHABLE();
1974 return false;
1975}
1976
Olli Etuaho57961272016-09-14 13:57:46 +03001977bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001978{
1979 TInfoSinkBase &out = getInfoSink();
1980
Olli Etuaho3d932d82016-04-12 11:10:30 +03001981 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001982
1983 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001984 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001985 {
1986 out << "FLATTEN ";
1987 }
1988
Olli Etuaho57961272016-09-14 13:57:46 +03001989 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990
1991 return false;
1992}
1993
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001994bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02001995{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001996 TInfoSinkBase &out = getInfoSink();
1997
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001998 if (node->getStatementList())
1999 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002000 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002001 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002002 // The curly braces get written when visiting the statementList aggregate
2003 }
2004 else
2005 {
2006 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002007 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002008 }
2009 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002010}
2011
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002012bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002013{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002014 TInfoSinkBase &out = getInfoSink();
2015
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002016 if (node->hasCondition())
2017 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002018 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002019 return true;
2020 }
2021 else
2022 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002023 out << "default:\n";
2024 return false;
2025 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002026}
2027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2029{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002030 TInfoSinkBase &out = getInfoSink();
2031 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002032}
2033
2034bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2035{
Nicolas Capens655fe362014-04-11 13:12:34 -04002036 mNestedLoopDepth++;
2037
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002038 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002039 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002040 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002041
Jamie Madill8c46ab12015-12-07 16:39:19 -05002042 TInfoSinkBase &out = getInfoSink();
2043
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002044 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002045 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002046 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002047 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002048 mInsideDiscontinuousLoop = wasDiscontinuous;
2049 mNestedLoopDepth--;
2050
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002051 return false;
2052 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002053 }
2054
Corentin Wallez1239ee92015-03-19 14:38:02 -07002055 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002056 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002058 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002059
Jamie Madill8c46ab12015-12-07 16:39:19 -05002060 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061 }
2062 else
2063 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002064 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002065
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066 if (node->getInit())
2067 {
2068 node->getInit()->traverse(this);
2069 }
2070
2071 out << "; ";
2072
alokp@chromium.org52813552010-11-16 18:36:09 +00002073 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002075 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076 }
2077
2078 out << "; ";
2079
alokp@chromium.org52813552010-11-16 18:36:09 +00002080 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002081 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002082 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 }
2084
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002085 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002086
Jamie Madill8c46ab12015-12-07 16:39:19 -05002087 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 }
2089
2090 if (node->getBody())
2091 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002092 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002093 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002095 else
2096 {
2097 // TODO(oetuaho): Check if the semicolon inside is necessary.
2098 // It's there as a result of conservative refactoring of the output.
2099 out << "{;}\n";
2100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101
Jamie Madill8c46ab12015-12-07 16:39:19 -05002102 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103
alokp@chromium.org52813552010-11-16 18:36:09 +00002104 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002106 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 out << "while(\n";
2108
alokp@chromium.org52813552010-11-16 18:36:09 +00002109 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110
daniel@transgaming.com73536982012-03-21 20:45:49 +00002111 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112 }
2113
daniel@transgaming.com73536982012-03-21 20:45:49 +00002114 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002116 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002117 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119 return false;
2120}
2121
2122bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2123{
Jamie Madill32aab012015-01-27 14:12:26 -05002124 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002125
2126 switch (node->getFlowOp())
2127 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002128 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002129 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002130 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002131 case EOpBreak:
2132 if (visit == PreVisit)
2133 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002134 if (mNestedLoopDepth > 1)
2135 {
2136 mUsesNestedBreak = true;
2137 }
2138
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002139 if (mExcessiveLoopIndex)
2140 {
2141 out << "{Break";
2142 mExcessiveLoopIndex->traverse(this);
2143 out << " = true; break;}\n";
2144 }
2145 else
2146 {
2147 out << "break;\n";
2148 }
2149 }
2150 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002151 case EOpContinue:
2152 outputTriplet(out, visit, "continue;\n", "", "");
2153 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154 case EOpReturn:
2155 if (visit == PreVisit)
2156 {
2157 if (node->getExpression())
2158 {
2159 out << "return ";
2160 }
2161 else
2162 {
2163 out << "return;\n";
2164 }
2165 }
2166 else if (visit == PostVisit)
2167 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002168 if (node->getExpression())
2169 {
2170 out << ";\n";
2171 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 }
2173 break;
2174 default: UNREACHABLE();
2175 }
2176
2177 return true;
2178}
2179
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002180bool OutputHLSL::isSingleStatement(TIntermNode *node)
2181{
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002182 if (node->getAsBlock())
2183 {
2184 return false;
2185 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002186
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002187 TIntermAggregate *aggregate = node->getAsAggregate();
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002188 if (aggregate)
2189 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002190 if (aggregate->getOp() == EOpDeclaration)
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002191 {
2192 // Declaring multiple comma-separated variables must be considered multiple statements
2193 // because each individual declaration has side effects which are visible in the next.
2194 return false;
2195 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002196 else
2197 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002198 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002199 {
2200 if (!isSingleStatement(*sit))
2201 {
2202 return false;
2203 }
2204 }
2205
2206 return true;
2207 }
2208 }
2209
2210 return true;
2211}
2212
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002213// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2214// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002215bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002216{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002217 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002218
2219 // Parse loops of the form:
2220 // for(int index = initial; index [comparator] limit; index += increment)
2221 TIntermSymbol *index = NULL;
2222 TOperator comparator = EOpNull;
2223 int initial = 0;
2224 int limit = 0;
2225 int increment = 0;
2226
2227 // Parse index name and intial value
2228 if (node->getInit())
2229 {
2230 TIntermAggregate *init = node->getInit()->getAsAggregate();
2231
2232 if (init)
2233 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002234 TIntermSequence *sequence = init->getSequence();
2235 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002236
2237 if (variable && variable->getQualifier() == EvqTemporary)
2238 {
2239 TIntermBinary *assign = variable->getAsBinaryNode();
2240
2241 if (assign->getOp() == EOpInitialize)
2242 {
2243 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2244 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2245
2246 if (symbol && constant)
2247 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002248 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002249 {
2250 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002251 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002252 }
2253 }
2254 }
2255 }
2256 }
2257 }
2258
2259 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002260 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002261 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002262 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002263
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002264 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2265 {
2266 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2267
2268 if (constant)
2269 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002270 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002271 {
2272 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002273 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002274 }
2275 }
2276 }
2277 }
2278
2279 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002280 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002281 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002282 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2283 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002284
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002285 if (binaryTerminal)
2286 {
2287 TOperator op = binaryTerminal->getOp();
2288 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2289
2290 if (constant)
2291 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002292 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002293 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002294 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002295
2296 switch (op)
2297 {
2298 case EOpAddAssign: increment = value; break;
2299 case EOpSubAssign: increment = -value; break;
2300 default: UNIMPLEMENTED();
2301 }
2302 }
2303 }
2304 }
2305 else if (unaryTerminal)
2306 {
2307 TOperator op = unaryTerminal->getOp();
2308
2309 switch (op)
2310 {
2311 case EOpPostIncrement: increment = 1; break;
2312 case EOpPostDecrement: increment = -1; break;
2313 case EOpPreIncrement: increment = 1; break;
2314 case EOpPreDecrement: increment = -1; break;
2315 default: UNIMPLEMENTED();
2316 }
2317 }
2318 }
2319
2320 if (index != NULL && comparator != EOpNull && increment != 0)
2321 {
2322 if (comparator == EOpLessThanEqual)
2323 {
2324 comparator = EOpLessThan;
2325 limit += 1;
2326 }
2327
2328 if (comparator == EOpLessThan)
2329 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002330 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002331
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002332 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002333 {
2334 return false; // Not an excessive loop
2335 }
2336
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002337 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2338 mExcessiveLoopIndex = index;
2339
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002340 out << "{int ";
2341 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002342 out << ";\n"
2343 "bool Break";
2344 index->traverse(this);
2345 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002346
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002347 bool firstLoopFragment = true;
2348
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002349 while (iterations > 0)
2350 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002351 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002352
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002353 if (!firstLoopFragment)
2354 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002355 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002356 index->traverse(this);
2357 out << ") {\n";
2358 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002359
2360 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2361 {
2362 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2363 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002364
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002365 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002366 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002367
Corentin Wallez1239ee92015-03-19 14:38:02 -07002368 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002369 index->traverse(this);
2370 out << " = ";
2371 out << initial;
2372
2373 out << "; ";
2374 index->traverse(this);
2375 out << " < ";
2376 out << clampedLimit;
2377
2378 out << "; ";
2379 index->traverse(this);
2380 out << " += ";
2381 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002382 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002383
Jamie Madill8c46ab12015-12-07 16:39:19 -05002384 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002385 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002386
2387 if (node->getBody())
2388 {
2389 node->getBody()->traverse(this);
2390 }
2391
Jamie Madill8c46ab12015-12-07 16:39:19 -05002392 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002393 out << ";}\n";
2394
2395 if (!firstLoopFragment)
2396 {
2397 out << "}\n";
2398 }
2399
2400 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002402 initial += MAX_LOOP_ITERATIONS * increment;
2403 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002404 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002405
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002406 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002407
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002408 mExcessiveLoopIndex = restoreIndex;
2409
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002410 return true;
2411 }
2412 else UNIMPLEMENTED();
2413 }
2414
2415 return false; // Not handled as an excessive loop
2416}
2417
Jamie Madill8c46ab12015-12-07 16:39:19 -05002418void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2419 Visit visit,
2420 const char *preString,
2421 const char *inString,
2422 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002424 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425 {
2426 out << preString;
2427 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002428 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429 {
2430 out << inString;
2431 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002432 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433 {
2434 out << postString;
2435 }
2436}
2437
Jamie Madill8c46ab12015-12-07 16:39:19 -05002438void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002439{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002440 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002441 {
Jamie Madill32aab012015-01-27 14:12:26 -05002442 out << "\n";
2443 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002444
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002445 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002446 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002447 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002448 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002449
Jamie Madill32aab012015-01-27 14:12:26 -05002450 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002451 }
2452}
2453
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002454TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2455{
2456 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002457 const TType &type = symbol->getType();
2458 const TName &name = symbol->getName();
2459 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002460
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002461 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002462 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002463 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002464 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002465 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002466 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002467 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002468 }
2469
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002470 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002471 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002472 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2473 {
2474 // Samplers are passed as indices to the sampler array.
2475 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2476 return "const uint " + nameStr + ArrayString(type);
2477 }
2478 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2479 {
2480 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2481 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2482 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2483 ArrayString(type);
2484 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002485 }
2486
Olli Etuaho96963162016-03-21 11:54:33 +02002487 TStringStream argString;
2488 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2489 << ArrayString(type);
2490
2491 // If the structure parameter contains samplers, they need to be passed into the function as
2492 // separate parameters. HLSL doesn't natively support samplers in structs.
2493 if (type.isStructureContainingSamplers())
2494 {
2495 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2496 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002497 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002498 for (const TIntermSymbol *sampler : samplerSymbols)
2499 {
2500 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2501 {
2502 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2503 }
2504 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2505 {
2506 const TType &samplerType = sampler->getType();
2507 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2508 type.getArraySize() == samplerType.getArraySize());
2509 ASSERT(IsSampler(samplerType.getBasicType()));
2510 argString << ", " << QualifierString(qualifier) << " "
2511 << TextureString(samplerType.getBasicType()) << " texture_"
2512 << sampler->getSymbol() << ArrayString(type) << ", "
2513 << QualifierString(qualifier) << " "
2514 << SamplerString(samplerType.getBasicType()) << " sampler_"
2515 << sampler->getSymbol() << ArrayString(type);
2516 }
2517 else
2518 {
2519 const TType &samplerType = sampler->getType();
2520 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2521 type.getArraySize() == samplerType.getArraySize());
2522 ASSERT(IsSampler(samplerType.getBasicType()));
2523 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2524 << " " << sampler->getSymbol() << ArrayString(type);
2525 }
2526 }
2527 }
2528
2529 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530}
2531
2532TString OutputHLSL::initializer(const TType &type)
2533{
2534 TString string;
2535
Jamie Madill94bf7f22013-07-08 13:31:15 -04002536 size_t size = type.getObjectSize();
2537 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002538 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002539 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540
Jamie Madill94bf7f22013-07-08 13:31:15 -04002541 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002542 {
2543 string += ", ";
2544 }
2545 }
2546
daniel@transgaming.comead23042010-04-29 03:35:36 +00002547 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002548}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002549
Jamie Madill8c46ab12015-12-07 16:39:19 -05002550void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2551 Visit visit,
2552 const TType &type,
2553 const char *name,
2554 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002555{
Olli Etuahof40319e2015-03-10 14:33:00 +02002556 if (type.isArray())
2557 {
2558 UNIMPLEMENTED();
2559 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002560
2561 if (visit == PreVisit)
2562 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002563 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002564
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002565 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002566 }
2567 else if (visit == InVisit)
2568 {
2569 out << ", ";
2570 }
2571 else if (visit == PostVisit)
2572 {
2573 out << ")";
2574 }
2575}
2576
Jamie Madill8c46ab12015-12-07 16:39:19 -05002577const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2578 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002579 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002580{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002581 const TConstantUnion *constUnionIterated = constUnion;
2582
Jamie Madill98493dd2013-07-08 14:39:03 -04002583 const TStructure* structure = type.getStruct();
2584 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002585 {
Jamie Madill033dae62014-06-18 12:56:28 -04002586 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002587
Jamie Madill98493dd2013-07-08 14:39:03 -04002588 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002589
Jamie Madill98493dd2013-07-08 14:39:03 -04002590 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002591 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002592 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002593 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002594
Jamie Madill98493dd2013-07-08 14:39:03 -04002595 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002596 {
2597 out << ", ";
2598 }
2599 }
2600
2601 out << ")";
2602 }
2603 else
2604 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002605 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002606 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002607
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002608 if (writeType)
2609 {
Jamie Madill033dae62014-06-18 12:56:28 -04002610 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002611 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002612 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002613 if (writeType)
2614 {
2615 out << ")";
2616 }
2617 }
2618
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002619 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002620}
2621
Jamie Madill8c46ab12015-12-07 16:39:19 -05002622void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002623{
2624 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002625 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002626}
2627
Jamie Madill37997142015-01-28 10:06:34 -05002628bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2629{
2630 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2631 expression->traverse(&searchSymbol);
2632
2633 if (searchSymbol.foundMatch())
2634 {
2635 // Type already printed
2636 out << "t" + str(mUniqueIndex) + " = ";
2637 expression->traverse(this);
2638 out << ", ";
2639 symbolNode->traverse(this);
2640 out << " = t" + str(mUniqueIndex);
2641
2642 mUniqueIndex++;
2643 return true;
2644 }
2645
2646 return false;
2647}
2648
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002649bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2650{
2651 // We support writing constant unions and constructors that only take constant unions as
2652 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002653 return expression->getAsConstantUnion() ||
2654 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002655}
2656
2657bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2658 TIntermSymbol *symbolNode,
2659 TIntermTyped *expression)
2660{
2661 if (canWriteAsHLSLLiteral(expression))
2662 {
2663 symbolNode->traverse(this);
2664 if (expression->getType().isArray())
2665 {
2666 out << "[" << expression->getType().getArraySize() << "]";
2667 }
2668 out << " = {";
2669 if (expression->getAsConstantUnion())
2670 {
2671 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2672 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2673 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2674 }
2675 else
2676 {
2677 TIntermAggregate *constructor = expression->getAsAggregate();
2678 ASSERT(constructor != nullptr);
2679 for (TIntermNode *&node : *constructor->getSequence())
2680 {
2681 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2682 ASSERT(nodeConst);
2683 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2684 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2685 if (node != constructor->getSequence()->back())
2686 {
2687 out << ", ";
2688 }
2689 }
2690 }
2691 out << "}";
2692 return true;
2693 }
2694 return false;
2695}
2696
Jamie Madill55e79e02015-02-09 15:35:00 -05002697TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2698{
2699 const TFieldList &fields = structure.fields();
2700
2701 for (const auto &eqFunction : mStructEqualityFunctions)
2702 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002703 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002704 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002705 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002706 }
2707 }
2708
2709 const TString &structNameString = StructNameString(structure);
2710
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002711 StructEqualityFunction *function = new StructEqualityFunction();
2712 function->structure = &structure;
2713 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002714
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002715 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002716
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002717 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2718 << "{\n"
2719 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002720
2721 for (size_t i = 0; i < fields.size(); i++)
2722 {
2723 const TField *field = fields[i];
2724 const TType *fieldType = field->type();
2725
2726 const TString &fieldNameA = "a." + Decorate(field->name());
2727 const TString &fieldNameB = "b." + Decorate(field->name());
2728
2729 if (i > 0)
2730 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002731 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002732 }
2733
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002734 fnOut << "(";
2735 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2736 fnOut << fieldNameA;
2737 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2738 fnOut << fieldNameB;
2739 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2740 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002741 }
2742
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002743 fnOut << ";\n" << "}\n";
2744
2745 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002746
2747 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002748 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002749
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002750 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002751}
2752
Olli Etuaho7fb49552015-03-18 17:27:44 +02002753TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2754{
2755 for (const auto &eqFunction : mArrayEqualityFunctions)
2756 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002757 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002758 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002759 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002760 }
2761 }
2762
2763 const TString &typeName = TypeString(type);
2764
Olli Etuaho12690762015-03-31 12:55:28 +03002765 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002766 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002767
2768 TInfoSinkBase fnNameOut;
2769 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002770 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002771
2772 TType nonArrayType = type;
2773 nonArrayType.clearArrayness();
2774
2775 TInfoSinkBase fnOut;
2776
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002777 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002778 << typeName << " a[" << type.getArraySize() << "], "
2779 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002780 << "{\n"
2781 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2782 " {\n"
2783 " if (";
2784
2785 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2786 fnOut << "a[i]";
2787 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2788 fnOut << "b[i]";
2789 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2790
2791 fnOut << ") { return false; }\n"
2792 " }\n"
2793 " return true;\n"
2794 "}\n";
2795
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002796 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002797
2798 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002799 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002800
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002801 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002802}
2803
Olli Etuaho12690762015-03-31 12:55:28 +03002804TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2805{
2806 for (const auto &assignFunction : mArrayAssignmentFunctions)
2807 {
2808 if (assignFunction.type == type)
2809 {
2810 return assignFunction.functionName;
2811 }
2812 }
2813
2814 const TString &typeName = TypeString(type);
2815
2816 ArrayHelperFunction function;
2817 function.type = type;
2818
2819 TInfoSinkBase fnNameOut;
2820 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2821 function.functionName = fnNameOut.c_str();
2822
2823 TInfoSinkBase fnOut;
2824
2825 fnOut << "void " << function.functionName << "(out "
2826 << typeName << " a[" << type.getArraySize() << "], "
2827 << typeName << " b[" << type.getArraySize() << "])\n"
2828 << "{\n"
2829 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2830 " {\n"
2831 " a[i] = b[i];\n"
2832 " }\n"
2833 "}\n";
2834
2835 function.functionDefinition = fnOut.c_str();
2836
2837 mArrayAssignmentFunctions.push_back(function);
2838
2839 return function.functionName;
2840}
2841
Olli Etuaho9638c352015-04-01 14:34:52 +03002842TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2843{
2844 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2845 {
2846 if (constructIntoFunction.type == type)
2847 {
2848 return constructIntoFunction.functionName;
2849 }
2850 }
2851
2852 const TString &typeName = TypeString(type);
2853
2854 ArrayHelperFunction function;
2855 function.type = type;
2856
2857 TInfoSinkBase fnNameOut;
2858 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2859 function.functionName = fnNameOut.c_str();
2860
2861 TInfoSinkBase fnOut;
2862
2863 fnOut << "void " << function.functionName << "(out "
2864 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002865 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002866 {
2867 fnOut << ", " << typeName << " b" << i;
2868 }
2869 fnOut << ")\n"
2870 "{\n";
2871
Olli Etuaho856c4972016-08-08 11:38:39 +03002872 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002873 {
2874 fnOut << " a[" << i << "] = b" << i << ";\n";
2875 }
2876 fnOut << "}\n";
2877
2878 function.functionDefinition = fnOut.c_str();
2879
2880 mArrayConstructIntoFunctions.push_back(function);
2881
2882 return function.functionName;
2883}
2884
Jamie Madill2e295e22015-04-29 10:41:33 -04002885void OutputHLSL::ensureStructDefined(const TType &type)
2886{
2887 TStructure *structure = type.getStruct();
2888
2889 if (structure)
2890 {
2891 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2892 }
2893}
2894
2895
Olli Etuaho9638c352015-04-01 14:34:52 +03002896
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002897}