blob: 3af0e173c74a4f21e5814d84565ab78b624bbbc1 [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
Olli Etuaho13389b62016-10-16 11:48:18 +01001522bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1523{
1524 TInfoSinkBase &out = getInfoSink();
1525 if (visit == PreVisit)
1526 {
1527 TIntermSequence *sequence = node->getSequence();
1528 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1529 ASSERT(sequence->size() == 1);
1530
1531 if (variable &&
1532 (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
1533 variable->getQualifier() == EvqConst))
1534 {
1535 ensureStructDefined(variable->getType());
1536
1537 if (!variable->getAsSymbolNode() ||
1538 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1539 {
1540 if (!mInsideFunction)
1541 {
1542 out << "static ";
1543 }
1544
1545 out << TypeString(variable->getType()) + " ";
1546
1547 TIntermSymbol *symbol = variable->getAsSymbolNode();
1548
1549 if (symbol)
1550 {
1551 symbol->traverse(this);
1552 out << ArrayString(symbol->getType());
1553 out << " = " + initializer(symbol->getType());
1554 }
1555 else
1556 {
1557 variable->traverse(this);
1558 }
1559 }
1560 else if (variable->getAsSymbolNode() &&
1561 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1562 {
1563 // Already added to constructor map
1564 }
1565 else
1566 UNREACHABLE();
1567 }
1568 else if (variable && IsVaryingOut(variable->getQualifier()))
1569 {
1570 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
1571 {
1572 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1573
1574 if (symbol)
1575 {
1576 // Vertex (output) varyings which are declared but not written to should
1577 // still be declared to allow successful linking
1578 mReferencedVaryings[symbol->getSymbol()] = symbol;
1579 }
1580 else
1581 {
1582 (*sit)->traverse(this);
1583 }
1584 }
1585 }
1586 }
1587 return false;
1588}
1589
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1591{
Jamie Madill32aab012015-01-27 14:12:26 -05001592 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001594 switch (node->getOp())
1595 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001596 case EOpInvariantDeclaration:
1597 // Do not do any translation
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001598 return false;
Olli Etuaho5878f832016-10-07 10:14:58 +01001599 case EOpPrototype:
1600 if (visit == PreVisit)
1601 {
Olli Etuahobd674552016-10-06 13:28:42 +01001602 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Olli Etuaho5878f832016-10-07 10:14:58 +01001603 // Skip the prototype if it is not implemented (and thus not used)
1604 if (index == CallDAG::InvalidIndex)
1605 {
1606 return false;
1607 }
1608
1609 TIntermSequence *arguments = node->getSequence();
1610
Olli Etuahobd674552016-10-06 13:28:42 +01001611 TString name =
1612 DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho5878f832016-10-07 10:14:58 +01001613 out << TypeString(node->getType()) << " " << name
1614 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
1615
1616 for (unsigned int i = 0; i < arguments->size(); i++)
1617 {
1618 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1619
1620 if (symbol)
1621 {
1622 out << argumentString(symbol);
1623
1624 if (i < arguments->size() - 1)
1625 {
1626 out << ", ";
1627 }
1628 }
1629 else
1630 UNREACHABLE();
1631 }
1632
1633 out << ");\n";
1634
1635 // Also prototype the Lod0 variant if needed
1636 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1637 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1638 {
1639 mOutputLod0Function = true;
1640 node->traverse(this);
1641 mOutputLod0Function = false;
1642 }
1643
1644 return false;
1645 }
1646 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001647 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001648 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001649 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001650
Corentin Wallez1239ee92015-03-19 14:38:02 -07001651 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001652 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001653 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001654 if (node->isArray())
1655 {
1656 UNIMPLEMENTED();
1657 }
Olli Etuahobd674552016-10-06 13:28:42 +01001658 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001659 ASSERT(index != CallDAG::InvalidIndex);
1660 lod0 &= mASTMetadataList[index].mNeedsLod0;
1661
Olli Etuahobd674552016-10-06 13:28:42 +01001662 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001663 out << DisambiguateFunctionName(node->getSequence());
1664 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001665 }
Olli Etuahobd674552016-10-06 13:28:42 +01001666 else if (node->getFunctionSymbolInfo()->getNameObj().isInternal())
Olli Etuahob741c762016-06-29 15:49:22 +03001667 {
1668 // This path is used for internal functions that don't have their definitions in the
1669 // AST, such as precision emulation functions.
Olli Etuahobd674552016-10-06 13:28:42 +01001670 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001671 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672 else
1673 {
Olli Etuahobd674552016-10-06 13:28:42 +01001674 TString name = TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001675 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001676 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1677 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1678 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1679 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001680 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001681
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001682 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001683 {
Olli Etuaho96963162016-03-21 11:54:33 +02001684 TIntermTyped *typedArg = (*arg)->getAsTyped();
1685 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001686 {
1687 out << "texture_";
1688 (*arg)->traverse(this);
1689 out << ", sampler_";
1690 }
1691
1692 (*arg)->traverse(this);
1693
Olli Etuaho96963162016-03-21 11:54:33 +02001694 if (typedArg->getType().isStructureContainingSamplers())
1695 {
1696 const TType &argType = typedArg->getType();
1697 TVector<TIntermSymbol *> samplerSymbols;
1698 TString structName = samplerNamePrefixFromStruct(typedArg);
1699 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001700 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001701 &samplerSymbols, nullptr);
1702 for (const TIntermSymbol *sampler : samplerSymbols)
1703 {
1704 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1705 {
1706 out << ", texture_" << sampler->getSymbol();
1707 out << ", sampler_" << sampler->getSymbol();
1708 }
1709 else
1710 {
1711 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1712 // of D3D9, it's the sampler variable.
1713 out << ", " + sampler->getSymbol();
1714 }
1715 }
1716 }
1717
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001718 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001719 {
1720 out << ", ";
1721 }
1722 }
1723
1724 out << ")";
1725
1726 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001727 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001728 case EOpParameters:
1729 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1730 break;
1731 case EOpConstructFloat:
1732 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1733 break;
1734 case EOpConstructVec2:
1735 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1736 break;
1737 case EOpConstructVec3:
1738 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1739 break;
1740 case EOpConstructVec4:
1741 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1742 break;
1743 case EOpConstructBool:
1744 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1745 break;
1746 case EOpConstructBVec2:
1747 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1748 break;
1749 case EOpConstructBVec3:
1750 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1751 break;
1752 case EOpConstructBVec4:
1753 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1754 break;
1755 case EOpConstructInt:
1756 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1757 break;
1758 case EOpConstructIVec2:
1759 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1760 break;
1761 case EOpConstructIVec3:
1762 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1763 break;
1764 case EOpConstructIVec4:
1765 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1766 break;
1767 case EOpConstructUInt:
1768 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1769 break;
1770 case EOpConstructUVec2:
1771 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1772 break;
1773 case EOpConstructUVec3:
1774 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1775 break;
1776 case EOpConstructUVec4:
1777 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1778 break;
1779 case EOpConstructMat2:
1780 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1781 break;
1782 case EOpConstructMat2x3:
1783 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1784 break;
1785 case EOpConstructMat2x4:
1786 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1787 break;
1788 case EOpConstructMat3x2:
1789 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1790 break;
1791 case EOpConstructMat3:
1792 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1793 break;
1794 case EOpConstructMat3x4:
1795 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1796 break;
1797 case EOpConstructMat4x2:
1798 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1799 break;
1800 case EOpConstructMat4x3:
1801 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1802 break;
1803 case EOpConstructMat4:
1804 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1805 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001806 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001807 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001808 if (node->getType().isArray())
1809 {
1810 UNIMPLEMENTED();
1811 }
Jamie Madill033dae62014-06-18 12:56:28 -04001812 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001813 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001814 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001815 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001816 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001817 case EOpLessThan:
1818 outputTriplet(out, visit, "(", " < ", ")");
1819 break;
1820 case EOpGreaterThan:
1821 outputTriplet(out, visit, "(", " > ", ")");
1822 break;
1823 case EOpLessThanEqual:
1824 outputTriplet(out, visit, "(", " <= ", ")");
1825 break;
1826 case EOpGreaterThanEqual:
1827 outputTriplet(out, visit, "(", " >= ", ")");
1828 break;
1829 case EOpVectorEqual:
1830 outputTriplet(out, visit, "(", " == ", ")");
1831 break;
1832 case EOpVectorNotEqual:
1833 outputTriplet(out, visit, "(", " != ", ")");
1834 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001835 case EOpMod:
1836 ASSERT(node->getUseEmulatedFunction());
1837 writeEmulatedFunctionTriplet(out, visit, "mod(");
1838 break;
1839 case EOpModf:
1840 outputTriplet(out, visit, "modf(", ", ", ")");
1841 break;
1842 case EOpPow:
1843 outputTriplet(out, visit, "pow(", ", ", ")");
1844 break;
1845 case EOpAtan:
1846 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1847 ASSERT(node->getUseEmulatedFunction());
1848 writeEmulatedFunctionTriplet(out, visit, "atan(");
1849 break;
1850 case EOpMin:
1851 outputTriplet(out, visit, "min(", ", ", ")");
1852 break;
1853 case EOpMax:
1854 outputTriplet(out, visit, "max(", ", ", ")");
1855 break;
1856 case EOpClamp:
1857 outputTriplet(out, visit, "clamp(", ", ", ")");
1858 break;
1859 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301860 {
1861 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1862 if (lastParamNode->getType().getBasicType() == EbtBool)
1863 {
1864 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1865 // so use emulated version.
1866 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001867 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301868 }
1869 else
1870 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001871 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301872 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001873 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301874 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001875 case EOpStep:
1876 outputTriplet(out, visit, "step(", ", ", ")");
1877 break;
1878 case EOpSmoothStep:
1879 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1880 break;
1881 case EOpDistance:
1882 outputTriplet(out, visit, "distance(", ", ", ")");
1883 break;
1884 case EOpDot:
1885 outputTriplet(out, visit, "dot(", ", ", ")");
1886 break;
1887 case EOpCross:
1888 outputTriplet(out, visit, "cross(", ", ", ")");
1889 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001890 case EOpFaceForward:
1891 ASSERT(node->getUseEmulatedFunction());
1892 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
1893 break;
1894 case EOpReflect:
1895 outputTriplet(out, visit, "reflect(", ", ", ")");
1896 break;
1897 case EOpRefract:
1898 outputTriplet(out, visit, "refract(", ", ", ")");
1899 break;
1900 case EOpOuterProduct:
1901 ASSERT(node->getUseEmulatedFunction());
1902 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
1903 break;
1904 case EOpMul:
1905 outputTriplet(out, visit, "(", " * ", ")");
1906 break;
1907 default:
1908 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001909 }
1910
1911 return true;
1912}
1913
Olli Etuaho57961272016-09-14 13:57:46 +03001914void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001915{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001916 out << "if (";
1917
1918 node->getCondition()->traverse(this);
1919
1920 out << ")\n";
1921
Jamie Madill8c46ab12015-12-07 16:39:19 -05001922 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001923
1924 bool discard = false;
1925
1926 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001928 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001929 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001930
Olli Etuahoa6f22092015-05-08 18:31:10 +03001931 // Detect true discard
1932 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1933 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001934 else
1935 {
1936 // TODO(oetuaho): Check if the semicolon inside is necessary.
1937 // It's there as a result of conservative refactoring of the output.
1938 out << "{;}\n";
1939 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001940
Jamie Madill8c46ab12015-12-07 16:39:19 -05001941 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001942
Olli Etuahoa6f22092015-05-08 18:31:10 +03001943 if (node->getFalseBlock())
1944 {
1945 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001946
Jamie Madill8c46ab12015-12-07 16:39:19 -05001947 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001948
Olli Etuaho32db19b2016-10-04 14:43:16 +01001949 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001950 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001951
Jamie Madill8c46ab12015-12-07 16:39:19 -05001952 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001953
Olli Etuahoa6f22092015-05-08 18:31:10 +03001954 // Detect false discard
1955 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1956 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001957
Olli Etuahoa6f22092015-05-08 18:31:10 +03001958 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001959 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001960 {
1961 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001962 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001963}
1964
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001965bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1966{
1967 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1968 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1969 UNREACHABLE();
1970 return false;
1971}
1972
Olli Etuaho57961272016-09-14 13:57:46 +03001973bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001974{
1975 TInfoSinkBase &out = getInfoSink();
1976
Olli Etuaho3d932d82016-04-12 11:10:30 +03001977 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001978
1979 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001980 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001981 {
1982 out << "FLATTEN ";
1983 }
1984
Olli Etuaho57961272016-09-14 13:57:46 +03001985 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001986
1987 return false;
1988}
1989
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001990bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02001991{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001992 TInfoSinkBase &out = getInfoSink();
1993
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001994 if (node->getStatementList())
1995 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02001996 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05001997 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001998 // The curly braces get written when visiting the statementList aggregate
1999 }
2000 else
2001 {
2002 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002003 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002004 }
2005 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002006}
2007
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002008bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002009{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002010 TInfoSinkBase &out = getInfoSink();
2011
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002012 if (node->hasCondition())
2013 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002014 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002015 return true;
2016 }
2017 else
2018 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002019 out << "default:\n";
2020 return false;
2021 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002022}
2023
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2025{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002026 TInfoSinkBase &out = getInfoSink();
2027 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002028}
2029
2030bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2031{
Nicolas Capens655fe362014-04-11 13:12:34 -04002032 mNestedLoopDepth++;
2033
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002034 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002035 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002036 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002037
Jamie Madill8c46ab12015-12-07 16:39:19 -05002038 TInfoSinkBase &out = getInfoSink();
2039
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002040 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002041 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002042 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002043 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002044 mInsideDiscontinuousLoop = wasDiscontinuous;
2045 mNestedLoopDepth--;
2046
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002047 return false;
2048 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002049 }
2050
Corentin Wallez1239ee92015-03-19 14:38:02 -07002051 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002052 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002054 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002055
Jamie Madill8c46ab12015-12-07 16:39:19 -05002056 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057 }
2058 else
2059 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002060 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002061
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002062 if (node->getInit())
2063 {
2064 node->getInit()->traverse(this);
2065 }
2066
2067 out << "; ";
2068
alokp@chromium.org52813552010-11-16 18:36:09 +00002069 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002070 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002071 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002072 }
2073
2074 out << "; ";
2075
alokp@chromium.org52813552010-11-16 18:36:09 +00002076 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002077 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002078 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079 }
2080
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002081 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002082
Jamie Madill8c46ab12015-12-07 16:39:19 -05002083 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084 }
2085
2086 if (node->getBody())
2087 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002088 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002089 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002091 else
2092 {
2093 // TODO(oetuaho): Check if the semicolon inside is necessary.
2094 // It's there as a result of conservative refactoring of the output.
2095 out << "{;}\n";
2096 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097
Jamie Madill8c46ab12015-12-07 16:39:19 -05002098 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002099
alokp@chromium.org52813552010-11-16 18:36:09 +00002100 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002101 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002102 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002103 out << "while(\n";
2104
alokp@chromium.org52813552010-11-16 18:36:09 +00002105 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106
daniel@transgaming.com73536982012-03-21 20:45:49 +00002107 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108 }
2109
daniel@transgaming.com73536982012-03-21 20:45:49 +00002110 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002112 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002113 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002114
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115 return false;
2116}
2117
2118bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2119{
Jamie Madill32aab012015-01-27 14:12:26 -05002120 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121
2122 switch (node->getFlowOp())
2123 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002124 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002125 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002126 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002127 case EOpBreak:
2128 if (visit == PreVisit)
2129 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002130 if (mNestedLoopDepth > 1)
2131 {
2132 mUsesNestedBreak = true;
2133 }
2134
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002135 if (mExcessiveLoopIndex)
2136 {
2137 out << "{Break";
2138 mExcessiveLoopIndex->traverse(this);
2139 out << " = true; break;}\n";
2140 }
2141 else
2142 {
2143 out << "break;\n";
2144 }
2145 }
2146 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002147 case EOpContinue:
2148 outputTriplet(out, visit, "continue;\n", "", "");
2149 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150 case EOpReturn:
2151 if (visit == PreVisit)
2152 {
2153 if (node->getExpression())
2154 {
2155 out << "return ";
2156 }
2157 else
2158 {
2159 out << "return;\n";
2160 }
2161 }
2162 else if (visit == PostVisit)
2163 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002164 if (node->getExpression())
2165 {
2166 out << ";\n";
2167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 }
2169 break;
2170 default: UNREACHABLE();
2171 }
2172
2173 return true;
2174}
2175
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002176// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2177// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002178bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002179{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002180 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002181
2182 // Parse loops of the form:
2183 // for(int index = initial; index [comparator] limit; index += increment)
2184 TIntermSymbol *index = NULL;
2185 TOperator comparator = EOpNull;
2186 int initial = 0;
2187 int limit = 0;
2188 int increment = 0;
2189
2190 // Parse index name and intial value
2191 if (node->getInit())
2192 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002193 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002194
2195 if (init)
2196 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002197 TIntermSequence *sequence = init->getSequence();
2198 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002199
2200 if (variable && variable->getQualifier() == EvqTemporary)
2201 {
2202 TIntermBinary *assign = variable->getAsBinaryNode();
2203
2204 if (assign->getOp() == EOpInitialize)
2205 {
2206 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2207 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2208
2209 if (symbol && constant)
2210 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002211 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002212 {
2213 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002214 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002215 }
2216 }
2217 }
2218 }
2219 }
2220 }
2221
2222 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002223 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002224 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002225 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002226
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002227 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2228 {
2229 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2230
2231 if (constant)
2232 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002233 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002234 {
2235 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002236 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002237 }
2238 }
2239 }
2240 }
2241
2242 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002243 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002244 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002245 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2246 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002247
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002248 if (binaryTerminal)
2249 {
2250 TOperator op = binaryTerminal->getOp();
2251 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2252
2253 if (constant)
2254 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002255 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002256 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002257 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002258
2259 switch (op)
2260 {
2261 case EOpAddAssign: increment = value; break;
2262 case EOpSubAssign: increment = -value; break;
2263 default: UNIMPLEMENTED();
2264 }
2265 }
2266 }
2267 }
2268 else if (unaryTerminal)
2269 {
2270 TOperator op = unaryTerminal->getOp();
2271
2272 switch (op)
2273 {
2274 case EOpPostIncrement: increment = 1; break;
2275 case EOpPostDecrement: increment = -1; break;
2276 case EOpPreIncrement: increment = 1; break;
2277 case EOpPreDecrement: increment = -1; break;
2278 default: UNIMPLEMENTED();
2279 }
2280 }
2281 }
2282
2283 if (index != NULL && comparator != EOpNull && increment != 0)
2284 {
2285 if (comparator == EOpLessThanEqual)
2286 {
2287 comparator = EOpLessThan;
2288 limit += 1;
2289 }
2290
2291 if (comparator == EOpLessThan)
2292 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002293 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002294
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002295 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002296 {
2297 return false; // Not an excessive loop
2298 }
2299
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002300 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2301 mExcessiveLoopIndex = index;
2302
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002303 out << "{int ";
2304 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002305 out << ";\n"
2306 "bool Break";
2307 index->traverse(this);
2308 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002309
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002310 bool firstLoopFragment = true;
2311
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002312 while (iterations > 0)
2313 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002314 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002315
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002316 if (!firstLoopFragment)
2317 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002318 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002319 index->traverse(this);
2320 out << ") {\n";
2321 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002322
2323 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2324 {
2325 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2326 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002327
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002328 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002329 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002330
Corentin Wallez1239ee92015-03-19 14:38:02 -07002331 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002332 index->traverse(this);
2333 out << " = ";
2334 out << initial;
2335
2336 out << "; ";
2337 index->traverse(this);
2338 out << " < ";
2339 out << clampedLimit;
2340
2341 out << "; ";
2342 index->traverse(this);
2343 out << " += ";
2344 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002345 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002346
Jamie Madill8c46ab12015-12-07 16:39:19 -05002347 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002348 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002349
2350 if (node->getBody())
2351 {
2352 node->getBody()->traverse(this);
2353 }
2354
Jamie Madill8c46ab12015-12-07 16:39:19 -05002355 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002356 out << ";}\n";
2357
2358 if (!firstLoopFragment)
2359 {
2360 out << "}\n";
2361 }
2362
2363 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002364
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002365 initial += MAX_LOOP_ITERATIONS * increment;
2366 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002367 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002368
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002369 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002370
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002371 mExcessiveLoopIndex = restoreIndex;
2372
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002373 return true;
2374 }
2375 else UNIMPLEMENTED();
2376 }
2377
2378 return false; // Not handled as an excessive loop
2379}
2380
Jamie Madill8c46ab12015-12-07 16:39:19 -05002381void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2382 Visit visit,
2383 const char *preString,
2384 const char *inString,
2385 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002387 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388 {
2389 out << preString;
2390 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002391 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392 {
2393 out << inString;
2394 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002395 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396 {
2397 out << postString;
2398 }
2399}
2400
Jamie Madill8c46ab12015-12-07 16:39:19 -05002401void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002402{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002403 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002404 {
Jamie Madill32aab012015-01-27 14:12:26 -05002405 out << "\n";
2406 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002407
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002408 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002409 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002410 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002411 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002412
Jamie Madill32aab012015-01-27 14:12:26 -05002413 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002414 }
2415}
2416
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002417TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2418{
2419 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002420 const TType &type = symbol->getType();
2421 const TName &name = symbol->getName();
2422 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002423
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002424 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002425 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002426 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002427 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002428 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002429 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002430 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002431 }
2432
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002433 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002434 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002435 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2436 {
2437 // Samplers are passed as indices to the sampler array.
2438 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2439 return "const uint " + nameStr + ArrayString(type);
2440 }
2441 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2442 {
2443 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2444 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2445 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2446 ArrayString(type);
2447 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002448 }
2449
Olli Etuaho96963162016-03-21 11:54:33 +02002450 TStringStream argString;
2451 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2452 << ArrayString(type);
2453
2454 // If the structure parameter contains samplers, they need to be passed into the function as
2455 // separate parameters. HLSL doesn't natively support samplers in structs.
2456 if (type.isStructureContainingSamplers())
2457 {
2458 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2459 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002460 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002461 for (const TIntermSymbol *sampler : samplerSymbols)
2462 {
2463 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2464 {
2465 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2466 }
2467 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2468 {
2469 const TType &samplerType = sampler->getType();
2470 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2471 type.getArraySize() == samplerType.getArraySize());
2472 ASSERT(IsSampler(samplerType.getBasicType()));
2473 argString << ", " << QualifierString(qualifier) << " "
2474 << TextureString(samplerType.getBasicType()) << " texture_"
2475 << sampler->getSymbol() << ArrayString(type) << ", "
2476 << QualifierString(qualifier) << " "
2477 << SamplerString(samplerType.getBasicType()) << " sampler_"
2478 << sampler->getSymbol() << ArrayString(type);
2479 }
2480 else
2481 {
2482 const TType &samplerType = sampler->getType();
2483 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2484 type.getArraySize() == samplerType.getArraySize());
2485 ASSERT(IsSampler(samplerType.getBasicType()));
2486 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2487 << " " << sampler->getSymbol() << ArrayString(type);
2488 }
2489 }
2490 }
2491
2492 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493}
2494
2495TString OutputHLSL::initializer(const TType &type)
2496{
2497 TString string;
2498
Jamie Madill94bf7f22013-07-08 13:31:15 -04002499 size_t size = type.getObjectSize();
2500 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002502 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503
Jamie Madill94bf7f22013-07-08 13:31:15 -04002504 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 {
2506 string += ", ";
2507 }
2508 }
2509
daniel@transgaming.comead23042010-04-29 03:35:36 +00002510 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002511}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002512
Jamie Madill8c46ab12015-12-07 16:39:19 -05002513void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2514 Visit visit,
2515 const TType &type,
2516 const char *name,
2517 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002518{
Olli Etuahof40319e2015-03-10 14:33:00 +02002519 if (type.isArray())
2520 {
2521 UNIMPLEMENTED();
2522 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002523
2524 if (visit == PreVisit)
2525 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002526 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002527
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002528 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002529 }
2530 else if (visit == InVisit)
2531 {
2532 out << ", ";
2533 }
2534 else if (visit == PostVisit)
2535 {
2536 out << ")";
2537 }
2538}
2539
Jamie Madill8c46ab12015-12-07 16:39:19 -05002540const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2541 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002542 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002543{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002544 const TConstantUnion *constUnionIterated = constUnion;
2545
Jamie Madill98493dd2013-07-08 14:39:03 -04002546 const TStructure* structure = type.getStruct();
2547 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002548 {
Jamie Madill033dae62014-06-18 12:56:28 -04002549 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002550
Jamie Madill98493dd2013-07-08 14:39:03 -04002551 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002552
Jamie Madill98493dd2013-07-08 14:39:03 -04002553 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002554 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002555 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002556 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002557
Jamie Madill98493dd2013-07-08 14:39:03 -04002558 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002559 {
2560 out << ", ";
2561 }
2562 }
2563
2564 out << ")";
2565 }
2566 else
2567 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002568 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002569 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002570
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002571 if (writeType)
2572 {
Jamie Madill033dae62014-06-18 12:56:28 -04002573 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002574 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002575 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002576 if (writeType)
2577 {
2578 out << ")";
2579 }
2580 }
2581
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002582 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002583}
2584
Jamie Madill8c46ab12015-12-07 16:39:19 -05002585void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002586{
2587 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002588 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002589}
2590
Jamie Madill37997142015-01-28 10:06:34 -05002591bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2592{
2593 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2594 expression->traverse(&searchSymbol);
2595
2596 if (searchSymbol.foundMatch())
2597 {
2598 // Type already printed
2599 out << "t" + str(mUniqueIndex) + " = ";
2600 expression->traverse(this);
2601 out << ", ";
2602 symbolNode->traverse(this);
2603 out << " = t" + str(mUniqueIndex);
2604
2605 mUniqueIndex++;
2606 return true;
2607 }
2608
2609 return false;
2610}
2611
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002612bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2613{
2614 // We support writing constant unions and constructors that only take constant unions as
2615 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002616 return expression->getAsConstantUnion() ||
2617 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002618}
2619
2620bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2621 TIntermSymbol *symbolNode,
2622 TIntermTyped *expression)
2623{
2624 if (canWriteAsHLSLLiteral(expression))
2625 {
2626 symbolNode->traverse(this);
2627 if (expression->getType().isArray())
2628 {
2629 out << "[" << expression->getType().getArraySize() << "]";
2630 }
2631 out << " = {";
2632 if (expression->getAsConstantUnion())
2633 {
2634 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2635 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2636 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2637 }
2638 else
2639 {
2640 TIntermAggregate *constructor = expression->getAsAggregate();
2641 ASSERT(constructor != nullptr);
2642 for (TIntermNode *&node : *constructor->getSequence())
2643 {
2644 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2645 ASSERT(nodeConst);
2646 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2647 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2648 if (node != constructor->getSequence()->back())
2649 {
2650 out << ", ";
2651 }
2652 }
2653 }
2654 out << "}";
2655 return true;
2656 }
2657 return false;
2658}
2659
Jamie Madill55e79e02015-02-09 15:35:00 -05002660TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2661{
2662 const TFieldList &fields = structure.fields();
2663
2664 for (const auto &eqFunction : mStructEqualityFunctions)
2665 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002666 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002667 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002668 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002669 }
2670 }
2671
2672 const TString &structNameString = StructNameString(structure);
2673
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002674 StructEqualityFunction *function = new StructEqualityFunction();
2675 function->structure = &structure;
2676 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002677
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002678 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002679
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002680 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2681 << "{\n"
2682 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002683
2684 for (size_t i = 0; i < fields.size(); i++)
2685 {
2686 const TField *field = fields[i];
2687 const TType *fieldType = field->type();
2688
2689 const TString &fieldNameA = "a." + Decorate(field->name());
2690 const TString &fieldNameB = "b." + Decorate(field->name());
2691
2692 if (i > 0)
2693 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002694 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002695 }
2696
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002697 fnOut << "(";
2698 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2699 fnOut << fieldNameA;
2700 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2701 fnOut << fieldNameB;
2702 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2703 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002704 }
2705
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002706 fnOut << ";\n" << "}\n";
2707
2708 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002709
2710 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002711 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002712
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002713 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002714}
2715
Olli Etuaho7fb49552015-03-18 17:27:44 +02002716TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2717{
2718 for (const auto &eqFunction : mArrayEqualityFunctions)
2719 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002720 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002721 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002722 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002723 }
2724 }
2725
2726 const TString &typeName = TypeString(type);
2727
Olli Etuaho12690762015-03-31 12:55:28 +03002728 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002729 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002730
2731 TInfoSinkBase fnNameOut;
2732 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002733 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002734
2735 TType nonArrayType = type;
2736 nonArrayType.clearArrayness();
2737
2738 TInfoSinkBase fnOut;
2739
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002740 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002741 << typeName << " a[" << type.getArraySize() << "], "
2742 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002743 << "{\n"
2744 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2745 " {\n"
2746 " if (";
2747
2748 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2749 fnOut << "a[i]";
2750 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2751 fnOut << "b[i]";
2752 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2753
2754 fnOut << ") { return false; }\n"
2755 " }\n"
2756 " return true;\n"
2757 "}\n";
2758
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002759 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002760
2761 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002762 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002763
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002764 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002765}
2766
Olli Etuaho12690762015-03-31 12:55:28 +03002767TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2768{
2769 for (const auto &assignFunction : mArrayAssignmentFunctions)
2770 {
2771 if (assignFunction.type == type)
2772 {
2773 return assignFunction.functionName;
2774 }
2775 }
2776
2777 const TString &typeName = TypeString(type);
2778
2779 ArrayHelperFunction function;
2780 function.type = type;
2781
2782 TInfoSinkBase fnNameOut;
2783 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2784 function.functionName = fnNameOut.c_str();
2785
2786 TInfoSinkBase fnOut;
2787
2788 fnOut << "void " << function.functionName << "(out "
2789 << typeName << " a[" << type.getArraySize() << "], "
2790 << typeName << " b[" << type.getArraySize() << "])\n"
2791 << "{\n"
2792 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2793 " {\n"
2794 " a[i] = b[i];\n"
2795 " }\n"
2796 "}\n";
2797
2798 function.functionDefinition = fnOut.c_str();
2799
2800 mArrayAssignmentFunctions.push_back(function);
2801
2802 return function.functionName;
2803}
2804
Olli Etuaho9638c352015-04-01 14:34:52 +03002805TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2806{
2807 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2808 {
2809 if (constructIntoFunction.type == type)
2810 {
2811 return constructIntoFunction.functionName;
2812 }
2813 }
2814
2815 const TString &typeName = TypeString(type);
2816
2817 ArrayHelperFunction function;
2818 function.type = type;
2819
2820 TInfoSinkBase fnNameOut;
2821 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2822 function.functionName = fnNameOut.c_str();
2823
2824 TInfoSinkBase fnOut;
2825
2826 fnOut << "void " << function.functionName << "(out "
2827 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002828 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002829 {
2830 fnOut << ", " << typeName << " b" << i;
2831 }
2832 fnOut << ")\n"
2833 "{\n";
2834
Olli Etuaho856c4972016-08-08 11:38:39 +03002835 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002836 {
2837 fnOut << " a[" << i << "] = b" << i << ";\n";
2838 }
2839 fnOut << "}\n";
2840
2841 function.functionDefinition = fnOut.c_str();
2842
2843 mArrayConstructIntoFunctions.push_back(function);
2844
2845 return function.functionName;
2846}
2847
Jamie Madill2e295e22015-04-29 10:41:33 -04002848void OutputHLSL::ensureStructDefined(const TType &type)
2849{
2850 TStructure *structure = type.getStruct();
2851
2852 if (structure)
2853 {
2854 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2855 }
2856}
2857
2858
Olli Etuaho9638c352015-04-01 14:34:52 +03002859
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860}