blob: 3b97c4271116e31d7bb08fb72f491084feecccf6 [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);
170 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700171
Jamie Madill37997142015-01-28 10:06:34 -0500172 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500173 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200174 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500175 mInfoSinkStack.pop();
176
Jamie Madill37997142015-01-28 10:06:34 -0500177 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500178 mInfoSinkStack.pop();
179
Jamie Madill32aab012015-01-27 14:12:26 -0500180 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500181 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500182 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000183
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200184 objSink << mHeader.c_str();
185 objSink << mBody.c_str();
186 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200187
188 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000189}
190
Jamie Madill570e04d2013-06-21 09:15:33 -0400191void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
192{
193 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
194 {
195 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
196
Jamie Madill32aab012015-01-27 14:12:26 -0500197 TInfoSinkBase structInfoSink;
198 mInfoSinkStack.push(&structInfoSink);
199
Jamie Madill570e04d2013-06-21 09:15:33 -0400200 // This will mark the necessary block elements as referenced
201 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500202
203 TString structName(structInfoSink.c_str());
204 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400205
206 mFlaggedStructOriginalNames[flaggedNode] = structName;
207
208 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
209 {
210 structName.erase(pos, 1);
211 }
212
213 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
214 }
215}
216
Jamie Madill4e1fd412014-07-10 17:50:10 -0400217const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
218{
219 return mUniformHLSL->getInterfaceBlockRegisterMap();
220}
221
Jamie Madill9fe25e92014-07-18 10:33:08 -0400222const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
223{
224 return mUniformHLSL->getUniformRegisterMap();
225}
226
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000227int OutputHLSL::vectorSize(const TType &type) const
228{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000229 int elementSize = type.isMatrix() ? type.getCols() : 1;
Olli Etuaho856c4972016-08-08 11:38:39 +0300230 unsigned int arraySize = type.isArray() ? type.getArraySize() : 1u;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000231
232 return elementSize * arraySize;
233}
234
Jamie Madill98493dd2013-07-08 14:39:03 -0400235TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400236{
237 TString init;
238
239 TString preIndentString;
240 TString fullIndentString;
241
242 for (int spaces = 0; spaces < (indent * 4); spaces++)
243 {
244 preIndentString += ' ';
245 }
246
247 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
248 {
249 fullIndentString += ' ';
250 }
251
252 init += preIndentString + "{\n";
253
Jamie Madill98493dd2013-07-08 14:39:03 -0400254 const TFieldList &fields = structure.fields();
255 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400256 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400257 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400258 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400259 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400260
Jamie Madill98493dd2013-07-08 14:39:03 -0400261 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400262 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400263 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400264 }
265 else
266 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400267 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400268 }
269 }
270
271 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
272
273 return init;
274}
275
Jamie Madill8c46ab12015-12-07 16:39:19 -0500276void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000278 TString varyings;
279 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000281
Jamie Madill829f59e2013-11-13 19:40:54 -0500282 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400283 {
284 TIntermTyped *structNode = flaggedStructIt->first;
285 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400286 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400287 const TString &originalName = mFlaggedStructOriginalNames[structNode];
288
Jamie Madill033dae62014-06-18 12:56:28 -0400289 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400290 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400291 flaggedStructs += "\n";
292 }
293
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000294 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
295 {
296 const TType &type = varying->second->getType();
297 const TString &name = varying->second->getSymbol();
298
299 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400300 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
301 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000302 }
303
304 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
305 {
306 const TType &type = attribute->second->getType();
307 const TString &name = attribute->second->getSymbol();
308
Jamie Madill033dae62014-06-18 12:56:28 -0400309 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000310 }
311
Jamie Madill8daaba12014-06-13 10:04:33 -0400312 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400313
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200314 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400315 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
316
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200317 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500318 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200319 out << "\n// Equality functions\n\n";
320 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500321 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200322 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200323 }
324 }
Olli Etuaho12690762015-03-31 12:55:28 +0300325 if (!mArrayAssignmentFunctions.empty())
326 {
327 out << "\n// Assignment functions\n\n";
328 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
329 {
330 out << assignmentFunction.functionDefinition << "\n";
331 }
332 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300333 if (!mArrayConstructIntoFunctions.empty())
334 {
335 out << "\n// Array constructor functions\n\n";
336 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
337 {
338 out << constructIntoFunction.functionDefinition << "\n";
339 }
340 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200341
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500342 if (mUsesDiscardRewriting)
343 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400344 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500345 }
346
Nicolas Capens655fe362014-04-11 13:12:34 -0400347 if (mUsesNestedBreak)
348 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400349 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400350 }
351
Arun Patole44efa0b2015-03-04 17:11:05 +0530352 if (mRequiresIEEEStrictCompiling)
353 {
354 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
355 }
356
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400357 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
358 "#define LOOP [loop]\n"
359 "#define FLATTEN [flatten]\n"
360 "#else\n"
361 "#define LOOP\n"
362 "#define FLATTEN\n"
363 "#endif\n";
364
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200365 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200367 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
368 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000369
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000370 out << "// Varyings\n";
371 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400372 out << "\n";
373
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200374 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000375 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500376 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000377 {
Jamie Madill46131a32013-06-20 11:55:50 -0400378 const TString &variableName = outputVariableIt->first;
379 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400380
Jamie Madill033dae62014-06-18 12:56:28 -0400381 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400382 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000383 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000384 }
Jamie Madill46131a32013-06-20 11:55:50 -0400385 else
386 {
387 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
388
389 out << "static float4 gl_Color[" << numColorValues << "] =\n"
390 "{\n";
391 for (unsigned int i = 0; i < numColorValues; i++)
392 {
393 out << " float4(0, 0, 0, 0)";
394 if (i + 1 != numColorValues)
395 {
396 out << ",";
397 }
398 out << "\n";
399 }
400
401 out << "};\n";
402 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000403
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400404 if (mUsesFragDepth)
405 {
406 out << "static float gl_Depth = 0.0;\n";
407 }
408
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000409 if (mUsesFragCoord)
410 {
411 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
412 }
413
414 if (mUsesPointCoord)
415 {
416 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
417 }
418
419 if (mUsesFrontFacing)
420 {
421 out << "static bool gl_FrontFacing = false;\n";
422 }
423
424 out << "\n";
425
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000426 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000427 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000428 out << "struct gl_DepthRangeParameters\n"
429 "{\n"
430 " float near;\n"
431 " float far;\n"
432 " float diff;\n"
433 "};\n"
434 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000435 }
436
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200437 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000438 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000439 out << "cbuffer DriverConstants : register(b1)\n"
440 "{\n";
441
442 if (mUsesDepthRange)
443 {
444 out << " float3 dx_DepthRange : packoffset(c0);\n";
445 }
446
447 if (mUsesFragCoord)
448 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000449 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000450 }
451
452 if (mUsesFragCoord || mUsesFrontFacing)
453 {
454 out << " float3 dx_DepthFront : packoffset(c2);\n";
455 }
456
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800457 if (mUsesFragCoord)
458 {
459 // dx_ViewScale is only used in the fragment shader to correct
460 // the value for glFragCoord if necessary
461 out << " float2 dx_ViewScale : packoffset(c3);\n";
462 }
463
Olli Etuaho618bebc2016-01-15 16:40:00 +0200464 if (mOutputType == SH_HLSL_4_1_OUTPUT)
465 {
466 mUniformHLSL->samplerMetadataUniforms(out, "c4");
467 }
468
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000469 out << "};\n";
470 }
471 else
472 {
473 if (mUsesDepthRange)
474 {
475 out << "uniform float3 dx_DepthRange : register(c0);";
476 }
477
478 if (mUsesFragCoord)
479 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000480 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000481 }
482
483 if (mUsesFragCoord || mUsesFrontFacing)
484 {
485 out << "uniform float3 dx_DepthFront : register(c2);\n";
486 }
487 }
488
489 out << "\n";
490
491 if (mUsesDepthRange)
492 {
493 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
494 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000495 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000496
Jamie Madillf91ce812014-06-13 10:04:34 -0400497 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000498 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400499 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000500 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400501 out << flaggedStructs;
502 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000503 }
504
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000505 if (usingMRTExtension && mNumRenderTargets > 1)
506 {
507 out << "#define GL_USES_MRT\n";
508 }
509
510 if (mUsesFragColor)
511 {
512 out << "#define GL_USES_FRAG_COLOR\n";
513 }
514
515 if (mUsesFragData)
516 {
517 out << "#define GL_USES_FRAG_DATA\n";
518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000520 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000522 out << "// Attributes\n";
523 out << attributes;
524 out << "\n"
525 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400526
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000527 if (mUsesPointSize)
528 {
529 out << "static float gl_PointSize = float(1);\n";
530 }
531
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000532 if (mUsesInstanceID)
533 {
534 out << "static int gl_InstanceID;";
535 }
536
Corentin Wallezb076add2016-01-11 16:45:46 -0500537 if (mUsesVertexID)
538 {
539 out << "static int gl_VertexID;";
540 }
541
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000542 out << "\n"
543 "// Varyings\n";
544 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000545 out << "\n";
546
547 if (mUsesDepthRange)
548 {
549 out << "struct gl_DepthRangeParameters\n"
550 "{\n"
551 " float near;\n"
552 " float far;\n"
553 " float diff;\n"
554 "};\n"
555 "\n";
556 }
557
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200558 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000559 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800560 out << "cbuffer DriverConstants : register(b1)\n"
561 "{\n";
562
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000563 if (mUsesDepthRange)
564 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800565 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000566 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800567
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800568 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
569 // shaders. However, we declare it for all shaders (including Feature Level 10+).
570 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
571 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800572 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800573 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800574 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800575
Olli Etuaho618bebc2016-01-15 16:40:00 +0200576 if (mOutputType == SH_HLSL_4_1_OUTPUT)
577 {
578 mUniformHLSL->samplerMetadataUniforms(out, "c4");
579 }
580
Austin Kinross4fd18b12014-12-22 12:32:05 -0800581 out << "};\n"
582 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000583 }
584 else
585 {
586 if (mUsesDepthRange)
587 {
588 out << "uniform float3 dx_DepthRange : register(c0);\n";
589 }
590
Cooper Partine6664f02015-01-09 16:22:24 -0800591 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
592 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000593 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000594 }
595
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000596 if (mUsesDepthRange)
597 {
598 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
599 "\n";
600 }
601
Jamie Madillf91ce812014-06-13 10:04:34 -0400602 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000603 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400604 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000605 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400606 out << flaggedStructs;
607 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000608 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400609 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000610
Geoff Lang1fe74c72016-08-25 13:23:01 -0400611 bool getDimensionsIgnoresBaseLevel =
612 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
613 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000615 if (mUsesFragCoord)
616 {
617 out << "#define GL_USES_FRAG_COORD\n";
618 }
619
620 if (mUsesPointCoord)
621 {
622 out << "#define GL_USES_POINT_COORD\n";
623 }
624
625 if (mUsesFrontFacing)
626 {
627 out << "#define GL_USES_FRONT_FACING\n";
628 }
629
630 if (mUsesPointSize)
631 {
632 out << "#define GL_USES_POINT_SIZE\n";
633 }
634
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400635 if (mUsesFragDepth)
636 {
637 out << "#define GL_USES_FRAG_DEPTH\n";
638 }
639
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000640 if (mUsesDepthRange)
641 {
642 out << "#define GL_USES_DEPTH_RANGE\n";
643 }
644
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000645 if (mUsesXor)
646 {
647 out << "bool xor(bool p, bool q)\n"
648 "{\n"
649 " return (p || q) && !(p && q);\n"
650 "}\n"
651 "\n";
652 }
653
Olli Etuaho95cd3c62015-03-03 16:45:32 +0200654 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000655}
656
657void OutputHLSL::visitSymbol(TIntermSymbol *node)
658{
Jamie Madill32aab012015-01-27 14:12:26 -0500659 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660
Jamie Madill570e04d2013-06-21 09:15:33 -0400661 // Handle accessing std140 structs by value
662 if (mFlaggedStructMappedNames.count(node) > 0)
663 {
664 out << mFlaggedStructMappedNames[node];
665 return;
666 }
667
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668 TString name = node->getSymbol();
669
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000670 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000671 {
672 mUsesDepthRange = true;
673 out << name;
674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675 else
676 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000677 TQualifier qualifier = node->getQualifier();
678
679 if (qualifier == EvqUniform)
680 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400681 const TType &nodeType = node->getType();
682 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400683
684 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000685 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400686 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000687 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000688 else
689 {
690 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000691 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400692
Jamie Madill2e295e22015-04-29 10:41:33 -0400693 ensureStructDefined(nodeType);
694
Olli Etuaho96963162016-03-21 11:54:33 +0200695 const TName &nameWithMetadata = node->getName();
696 out << DecorateUniform(nameWithMetadata, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000697 }
Jamie Madill19571812013-08-12 15:26:34 -0700698 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000699 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000700 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400701 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000702 }
Jamie Madill033dae62014-06-18 12:56:28 -0400703 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000704 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000705 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400706 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000707 }
Jamie Madill19571812013-08-12 15:26:34 -0700708 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400709 {
710 mReferencedOutputVariables[name] = node;
711 out << "out_" << name;
712 }
713 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000714 {
715 out << "gl_Color[0]";
716 mUsesFragColor = true;
717 }
718 else if (qualifier == EvqFragData)
719 {
720 out << "gl_Color";
721 mUsesFragData = true;
722 }
723 else if (qualifier == EvqFragCoord)
724 {
725 mUsesFragCoord = true;
726 out << name;
727 }
728 else if (qualifier == EvqPointCoord)
729 {
730 mUsesPointCoord = true;
731 out << name;
732 }
733 else if (qualifier == EvqFrontFacing)
734 {
735 mUsesFrontFacing = true;
736 out << name;
737 }
738 else if (qualifier == EvqPointSize)
739 {
740 mUsesPointSize = true;
741 out << name;
742 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000743 else if (qualifier == EvqInstanceID)
744 {
745 mUsesInstanceID = true;
746 out << name;
747 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500748 else if (qualifier == EvqVertexID)
749 {
750 mUsesVertexID = true;
751 out << name;
752 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300753 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400754 {
755 mUsesFragDepth = true;
756 out << "gl_Depth";
757 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000758 else
759 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +0300760 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000761 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762 }
763}
764
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400765void OutputHLSL::visitRaw(TIntermRaw *node)
766{
Jamie Madill32aab012015-01-27 14:12:26 -0500767 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400768}
769
Olli Etuaho7fb49552015-03-18 17:27:44 +0200770void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
771{
772 if (type.isScalar() && !type.isArray())
773 {
774 if (op == EOpEqual)
775 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500776 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200777 }
778 else
779 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500780 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200781 }
782 }
783 else
784 {
785 if (visit == PreVisit && op == EOpNotEqual)
786 {
787 out << "!";
788 }
789
790 if (type.isArray())
791 {
792 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500793 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200794 }
795 else if (type.getBasicType() == EbtStruct)
796 {
797 const TStructure &structure = *type.getStruct();
798 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500799 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200800 }
801 else
802 {
803 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500804 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200805 }
806 }
807}
808
Olli Etuaho96963162016-03-21 11:54:33 +0200809bool OutputHLSL::ancestorEvaluatesToSamplerInStruct(Visit visit)
810{
811 // Inside InVisit the current node is already in the path.
812 const unsigned int initialN = visit == InVisit ? 1u : 0u;
813 for (unsigned int n = initialN; getAncestorNode(n) != nullptr; ++n)
814 {
815 TIntermNode *ancestor = getAncestorNode(n);
816 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
817 if (ancestorBinary == nullptr)
818 {
819 return false;
820 }
821 switch (ancestorBinary->getOp())
822 {
823 case EOpIndexDirectStruct:
824 {
825 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
826 const TIntermConstantUnion *index =
827 ancestorBinary->getRight()->getAsConstantUnion();
828 const TField *field = structure->fields()[index->getIConst(0)];
829 if (IsSampler(field->type()->getBasicType()))
830 {
831 return true;
832 }
833 break;
834 }
835 case EOpIndexDirect:
836 break;
837 default:
838 // Returning a sampler from indirect indexing is not supported.
839 return false;
840 }
841 }
842 return false;
843}
844
Olli Etuahob6fa0432016-09-28 16:28:05 +0100845bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
846{
847 TInfoSinkBase &out = getInfoSink();
848 if (visit == PostVisit)
849 {
850 out << ".";
851 node->writeOffsetsAsXYZW(&out);
852 }
853 return true;
854}
855
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
857{
Jamie Madill32aab012015-01-27 14:12:26 -0500858 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859
Jamie Madill570e04d2013-06-21 09:15:33 -0400860 // Handle accessing std140 structs by value
861 if (mFlaggedStructMappedNames.count(node) > 0)
862 {
863 out << mFlaggedStructMappedNames[node];
864 return false;
865 }
866
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867 switch (node->getOp())
868 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100869 case EOpComma:
870 outputTriplet(out, visit, "(", ", ", ")");
871 break;
872 case EOpAssign:
873 if (node->getLeft()->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +0300874 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100875 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
876 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +0300877 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100878 const TString &functionName = addArrayConstructIntoFunction(node->getType());
879 out << functionName << "(";
880 node->getLeft()->traverse(this);
881 TIntermSequence *seq = rightAgg->getSequence();
882 for (auto &arrayElement : *seq)
883 {
884 out << ", ";
885 arrayElement->traverse(this);
886 }
887 out << ")";
888 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +0300889 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100890 // ArrayReturnValueToOutParameter should have eliminated expressions where a
891 // function call is assigned.
892 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
893
894 const TString &functionName = addArrayAssignmentFunction(node->getType());
895 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho9638c352015-04-01 14:34:52 +0300896 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100897 else
Jamie Madill37997142015-01-28 10:06:34 -0500898 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100899 outputTriplet(out, visit, "(", " = ", ")");
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +0000900 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100901 break;
902 case EOpInitialize:
903 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200904 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100905 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
906 ASSERT(symbolNode);
907 TIntermTyped *expression = node->getRight();
908
909 // Global initializers must be constant at this point.
910 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
911 canWriteAsHLSLLiteral(expression));
912
913 // GLSL allows to write things like "float x = x;" where a new variable x is defined
914 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
915 // new variable is created before the assignment is evaluated), so we need to
916 // convert
917 // this to "float t = x, x = t;".
918 if (writeSameSymbolInitializer(out, symbolNode, expression))
919 {
920 // Skip initializing the rest of the expression
921 return false;
922 }
923 else if (writeConstantInitialization(out, symbolNode, expression))
924 {
925 return false;
926 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200927 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100928 else if (visit == InVisit)
929 {
930 out << " = ";
931 }
932 break;
933 case EOpAddAssign:
934 outputTriplet(out, visit, "(", " += ", ")");
935 break;
936 case EOpSubAssign:
937 outputTriplet(out, visit, "(", " -= ", ")");
938 break;
939 case EOpMulAssign:
940 outputTriplet(out, visit, "(", " *= ", ")");
941 break;
942 case EOpVectorTimesScalarAssign:
943 outputTriplet(out, visit, "(", " *= ", ")");
944 break;
945 case EOpMatrixTimesScalarAssign:
946 outputTriplet(out, visit, "(", " *= ", ")");
947 break;
948 case EOpVectorTimesMatrixAssign:
949 if (visit == PreVisit)
950 {
951 out << "(";
952 }
953 else if (visit == InVisit)
954 {
955 out << " = mul(";
956 node->getLeft()->traverse(this);
957 out << ", transpose(";
958 }
959 else
960 {
961 out << ")))";
962 }
963 break;
964 case EOpMatrixTimesMatrixAssign:
965 if (visit == PreVisit)
966 {
967 out << "(";
968 }
969 else if (visit == InVisit)
970 {
971 out << " = transpose(mul(transpose(";
972 node->getLeft()->traverse(this);
973 out << "), transpose(";
974 }
975 else
976 {
977 out << "))))";
978 }
979 break;
980 case EOpDivAssign:
981 outputTriplet(out, visit, "(", " /= ", ")");
982 break;
983 case EOpIModAssign:
984 outputTriplet(out, visit, "(", " %= ", ")");
985 break;
986 case EOpBitShiftLeftAssign:
987 outputTriplet(out, visit, "(", " <<= ", ")");
988 break;
989 case EOpBitShiftRightAssign:
990 outputTriplet(out, visit, "(", " >>= ", ")");
991 break;
992 case EOpBitwiseAndAssign:
993 outputTriplet(out, visit, "(", " &= ", ")");
994 break;
995 case EOpBitwiseXorAssign:
996 outputTriplet(out, visit, "(", " ^= ", ")");
997 break;
998 case EOpBitwiseOrAssign:
999 outputTriplet(out, visit, "(", " |= ", ")");
1000 break;
1001 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001002 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001003 const TType& leftType = node->getLeft()->getType();
1004 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001005 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001006 if (visit == PreVisit)
1007 {
1008 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1009 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001010 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001011 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001012 return false;
1013 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001014 }
Olli Etuaho96963162016-03-21 11:54:33 +02001015 else if (ancestorEvaluatesToSamplerInStruct(visit))
1016 {
1017 // All parts of an expression that access a sampler in a struct need to use _ as
1018 // separator to access the sampler variable that has been moved out of the struct.
1019 outputTriplet(out, visit, "", "_", "");
1020 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001021 else
1022 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001023 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001024 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001025 }
1026 break;
1027 case EOpIndexIndirect:
1028 // We do not currently support indirect references to interface blocks
1029 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001030 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001031 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001032 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001033 {
1034 const TStructure* structure = node->getLeft()->getType().getStruct();
1035 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1036 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001037
Olli Etuaho96963162016-03-21 11:54:33 +02001038 // In cases where indexing returns a sampler, we need to access the sampler variable
1039 // that has been moved out of the struct.
1040 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1041 if (visit == PreVisit && indexingReturnsSampler)
1042 {
1043 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1044 // This prefix is only output at the beginning of the indexing expression, which
1045 // may have multiple parts.
1046 out << "angle";
1047 }
1048 if (!indexingReturnsSampler)
1049 {
1050 // All parts of an expression that access a sampler in a struct need to use _ as
1051 // separator to access the sampler variable that has been moved out of the struct.
1052 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct(visit);
1053 }
1054 if (visit == InVisit)
1055 {
1056 if (indexingReturnsSampler)
1057 {
1058 out << "_" + field->name();
1059 }
1060 else
1061 {
1062 out << "." + DecorateField(field->name(), *structure);
1063 }
1064
1065 return false;
1066 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001067 }
1068 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001069 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001070 if (visit == InVisit)
1071 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001072 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1073 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1074 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001075 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001076
1077 return false;
1078 }
1079 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001080 case EOpAdd:
1081 outputTriplet(out, visit, "(", " + ", ")");
1082 break;
1083 case EOpSub:
1084 outputTriplet(out, visit, "(", " - ", ")");
1085 break;
1086 case EOpMul:
1087 outputTriplet(out, visit, "(", " * ", ")");
1088 break;
1089 case EOpDiv:
1090 outputTriplet(out, visit, "(", " / ", ")");
1091 break;
1092 case EOpIMod:
1093 outputTriplet(out, visit, "(", " % ", ")");
1094 break;
1095 case EOpBitShiftLeft:
1096 outputTriplet(out, visit, "(", " << ", ")");
1097 break;
1098 case EOpBitShiftRight:
1099 outputTriplet(out, visit, "(", " >> ", ")");
1100 break;
1101 case EOpBitwiseAnd:
1102 outputTriplet(out, visit, "(", " & ", ")");
1103 break;
1104 case EOpBitwiseXor:
1105 outputTriplet(out, visit, "(", " ^ ", ")");
1106 break;
1107 case EOpBitwiseOr:
1108 outputTriplet(out, visit, "(", " | ", ")");
1109 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001110 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001111 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001112 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001113 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001114 case EOpLessThan:
1115 outputTriplet(out, visit, "(", " < ", ")");
1116 break;
1117 case EOpGreaterThan:
1118 outputTriplet(out, visit, "(", " > ", ")");
1119 break;
1120 case EOpLessThanEqual:
1121 outputTriplet(out, visit, "(", " <= ", ")");
1122 break;
1123 case EOpGreaterThanEqual:
1124 outputTriplet(out, visit, "(", " >= ", ")");
1125 break;
1126 case EOpVectorTimesScalar:
1127 outputTriplet(out, visit, "(", " * ", ")");
1128 break;
1129 case EOpMatrixTimesScalar:
1130 outputTriplet(out, visit, "(", " * ", ")");
1131 break;
1132 case EOpVectorTimesMatrix:
1133 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1134 break;
1135 case EOpMatrixTimesVector:
1136 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1137 break;
1138 case EOpMatrixTimesMatrix:
1139 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1140 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001141 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001142 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1143 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001144 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001145 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001146 case EOpLogicalXor:
1147 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001148 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001149 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001150 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001151 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1152 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001153 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001154 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001155 default: UNREACHABLE();
1156 }
1157
1158 return true;
1159}
1160
1161bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1162{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001163 TInfoSinkBase &out = getInfoSink();
1164
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165 switch (node->getOp())
1166 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001167 case EOpNegative:
1168 outputTriplet(out, visit, "(-", "", ")");
1169 break;
1170 case EOpPositive:
1171 outputTriplet(out, visit, "(+", "", ")");
1172 break;
1173 case EOpVectorLogicalNot:
1174 outputTriplet(out, visit, "(!", "", ")");
1175 break;
1176 case EOpLogicalNot:
1177 outputTriplet(out, visit, "(!", "", ")");
1178 break;
1179 case EOpBitwiseNot:
1180 outputTriplet(out, visit, "(~", "", ")");
1181 break;
1182 case EOpPostIncrement:
1183 outputTriplet(out, visit, "(", "", "++)");
1184 break;
1185 case EOpPostDecrement:
1186 outputTriplet(out, visit, "(", "", "--)");
1187 break;
1188 case EOpPreIncrement:
1189 outputTriplet(out, visit, "(++", "", ")");
1190 break;
1191 case EOpPreDecrement:
1192 outputTriplet(out, visit, "(--", "", ")");
1193 break;
1194 case EOpRadians:
1195 outputTriplet(out, visit, "radians(", "", ")");
1196 break;
1197 case EOpDegrees:
1198 outputTriplet(out, visit, "degrees(", "", ")");
1199 break;
1200 case EOpSin:
1201 outputTriplet(out, visit, "sin(", "", ")");
1202 break;
1203 case EOpCos:
1204 outputTriplet(out, visit, "cos(", "", ")");
1205 break;
1206 case EOpTan:
1207 outputTriplet(out, visit, "tan(", "", ")");
1208 break;
1209 case EOpAsin:
1210 outputTriplet(out, visit, "asin(", "", ")");
1211 break;
1212 case EOpAcos:
1213 outputTriplet(out, visit, "acos(", "", ")");
1214 break;
1215 case EOpAtan:
1216 outputTriplet(out, visit, "atan(", "", ")");
1217 break;
1218 case EOpSinh:
1219 outputTriplet(out, visit, "sinh(", "", ")");
1220 break;
1221 case EOpCosh:
1222 outputTriplet(out, visit, "cosh(", "", ")");
1223 break;
1224 case EOpTanh:
1225 outputTriplet(out, visit, "tanh(", "", ")");
1226 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001227 case EOpAsinh:
1228 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001229 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001230 break;
1231 case EOpAcosh:
1232 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001233 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001234 break;
1235 case EOpAtanh:
1236 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001237 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001238 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001239 case EOpExp:
1240 outputTriplet(out, visit, "exp(", "", ")");
1241 break;
1242 case EOpLog:
1243 outputTriplet(out, visit, "log(", "", ")");
1244 break;
1245 case EOpExp2:
1246 outputTriplet(out, visit, "exp2(", "", ")");
1247 break;
1248 case EOpLog2:
1249 outputTriplet(out, visit, "log2(", "", ")");
1250 break;
1251 case EOpSqrt:
1252 outputTriplet(out, visit, "sqrt(", "", ")");
1253 break;
1254 case EOpInverseSqrt:
1255 outputTriplet(out, visit, "rsqrt(", "", ")");
1256 break;
1257 case EOpAbs:
1258 outputTriplet(out, visit, "abs(", "", ")");
1259 break;
1260 case EOpSign:
1261 outputTriplet(out, visit, "sign(", "", ")");
1262 break;
1263 case EOpFloor:
1264 outputTriplet(out, visit, "floor(", "", ")");
1265 break;
1266 case EOpTrunc:
1267 outputTriplet(out, visit, "trunc(", "", ")");
1268 break;
1269 case EOpRound:
1270 outputTriplet(out, visit, "round(", "", ")");
1271 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001272 case EOpRoundEven:
1273 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001274 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001275 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001276 case EOpCeil:
1277 outputTriplet(out, visit, "ceil(", "", ")");
1278 break;
1279 case EOpFract:
1280 outputTriplet(out, visit, "frac(", "", ")");
1281 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301282 case EOpIsNan:
Shao6f0a0dc2016-09-27 13:51:29 +08001283 if (node->getUseEmulatedFunction())
1284 writeEmulatedFunctionTriplet(out, visit, "isnan(");
1285 else
1286 outputTriplet(out, visit, "isnan(", "", ")");
1287 mRequiresIEEEStrictCompiling = true;
1288 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001289 case EOpIsInf:
1290 outputTriplet(out, visit, "isinf(", "", ")");
1291 break;
1292 case EOpFloatBitsToInt:
1293 outputTriplet(out, visit, "asint(", "", ")");
1294 break;
1295 case EOpFloatBitsToUint:
1296 outputTriplet(out, visit, "asuint(", "", ")");
1297 break;
1298 case EOpIntBitsToFloat:
1299 outputTriplet(out, visit, "asfloat(", "", ")");
1300 break;
1301 case EOpUintBitsToFloat:
1302 outputTriplet(out, visit, "asfloat(", "", ")");
1303 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02001304 case EOpPackSnorm2x16:
1305 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001306 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001307 break;
1308 case EOpPackUnorm2x16:
1309 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001310 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001311 break;
1312 case EOpPackHalf2x16:
1313 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001314 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001315 break;
1316 case EOpUnpackSnorm2x16:
1317 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001318 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001319 break;
1320 case EOpUnpackUnorm2x16:
1321 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001322 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001323 break;
1324 case EOpUnpackHalf2x16:
1325 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001326 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02001327 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001328 case EOpLength:
1329 outputTriplet(out, visit, "length(", "", ")");
1330 break;
1331 case EOpNormalize:
1332 outputTriplet(out, visit, "normalize(", "", ")");
1333 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001334 case EOpDFdx:
1335 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1336 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001337 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001338 }
1339 else
1340 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001341 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001342 }
1343 break;
1344 case EOpDFdy:
1345 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1346 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001347 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001348 }
1349 else
1350 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001351 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001352 }
1353 break;
1354 case EOpFwidth:
1355 if(mInsideDiscontinuousLoop || mOutputLod0Function)
1356 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001357 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001358 }
1359 else
1360 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001361 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00001362 }
1363 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001364 case EOpTranspose:
1365 outputTriplet(out, visit, "transpose(", "", ")");
1366 break;
1367 case EOpDeterminant:
1368 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1369 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001370 case EOpInverse:
1371 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001372 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02001373 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001374
Jamie Madill8c46ab12015-12-07 16:39:19 -05001375 case EOpAny:
1376 outputTriplet(out, visit, "any(", "", ")");
1377 break;
1378 case EOpAll:
1379 outputTriplet(out, visit, "all(", "", ")");
1380 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381 default: UNREACHABLE();
1382 }
1383
1384 return true;
1385}
1386
Olli Etuaho96963162016-03-21 11:54:33 +02001387TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1388{
1389 if (node->getAsSymbolNode())
1390 {
1391 return node->getAsSymbolNode()->getSymbol();
1392 }
1393 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1394 switch (nodeBinary->getOp())
1395 {
1396 case EOpIndexDirect:
1397 {
1398 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1399
1400 TInfoSinkBase prefixSink;
1401 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1402 return TString(prefixSink.c_str());
1403 }
1404 case EOpIndexDirectStruct:
1405 {
1406 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1407 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1408 const TField *field = s->fields()[index];
1409
1410 TInfoSinkBase prefixSink;
1411 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1412 << field->name();
1413 return TString(prefixSink.c_str());
1414 }
1415 default:
1416 UNREACHABLE();
1417 return TString("");
1418 }
1419}
1420
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001421bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1422{
1423 TInfoSinkBase &out = getInfoSink();
1424
1425 if (mInsideFunction)
1426 {
1427 outputLineDirective(out, node->getLine().first_line);
1428 out << "{\n";
1429 }
1430
1431 for (TIntermSequence::iterator sit = node->getSequence()->begin();
1432 sit != node->getSequence()->end(); sit++)
1433 {
1434 outputLineDirective(out, (*sit)->getLine().first_line);
1435
1436 (*sit)->traverse(this);
1437
1438 // Don't output ; after case labels, they're terminated by :
1439 // This is needed especially since outputting a ; after a case statement would turn empty
1440 // case statements into non-empty case statements, disallowing fall-through from them.
1441 // Also no need to output ; after if statements or sequences. This is done just for
1442 // code clarity.
1443 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
1444 (*sit)->getAsBlock() == nullptr)
1445 out << ";\n";
1446 }
1447
1448 if (mInsideFunction)
1449 {
1450 outputLineDirective(out, node->getLine().last_line);
1451 out << "}\n";
1452 }
1453
1454 return false;
1455}
1456
Olli Etuaho336b1472016-10-05 16:37:55 +01001457bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1458{
1459 TInfoSinkBase &out = getInfoSink();
1460
1461 ASSERT(mCurrentFunctionMetadata == nullptr);
1462
1463 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1464 ASSERT(index != CallDAG::InvalidIndex);
1465 mCurrentFunctionMetadata = &mASTMetadataList[index];
1466
1467 out << TypeString(node->getType()) << " ";
1468
1469 TIntermSequence *parameters = node->getFunctionParameters()->getSequence();
1470
1471 if (node->getFunctionSymbolInfo()->isMain())
1472 {
1473 out << "gl_main(";
1474 }
1475 else
1476 {
1477 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
1478 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1479 }
1480
1481 for (unsigned int i = 0; i < parameters->size(); i++)
1482 {
1483 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1484
1485 if (symbol)
1486 {
1487 ensureStructDefined(symbol->getType());
1488
1489 out << argumentString(symbol);
1490
1491 if (i < parameters->size() - 1)
1492 {
1493 out << ", ";
1494 }
1495 }
1496 else
1497 UNREACHABLE();
1498 }
1499
1500 out << ")\n";
1501
1502 mInsideFunction = true;
1503 // The function body node will output braces.
1504 node->getBody()->traverse(this);
1505 mInsideFunction = false;
1506
1507 mCurrentFunctionMetadata = nullptr;
1508
1509 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1510 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1511 {
1512 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1513 mOutputLod0Function = true;
1514 node->traverse(this);
1515 mOutputLod0Function = false;
1516 }
1517
1518 return false;
1519}
1520
Olli Etuaho13389b62016-10-16 11:48:18 +01001521bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1522{
1523 TInfoSinkBase &out = getInfoSink();
1524 if (visit == PreVisit)
1525 {
1526 TIntermSequence *sequence = node->getSequence();
1527 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1528 ASSERT(sequence->size() == 1);
1529
1530 if (variable &&
1531 (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
1532 variable->getQualifier() == EvqConst))
1533 {
1534 ensureStructDefined(variable->getType());
1535
1536 if (!variable->getAsSymbolNode() ||
1537 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1538 {
1539 if (!mInsideFunction)
1540 {
1541 out << "static ";
1542 }
1543
1544 out << TypeString(variable->getType()) + " ";
1545
1546 TIntermSymbol *symbol = variable->getAsSymbolNode();
1547
1548 if (symbol)
1549 {
1550 symbol->traverse(this);
1551 out << ArrayString(symbol->getType());
1552 out << " = " + initializer(symbol->getType());
1553 }
1554 else
1555 {
1556 variable->traverse(this);
1557 }
1558 }
1559 else if (variable->getAsSymbolNode() &&
1560 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1561 {
1562 // Already added to constructor map
1563 }
1564 else
1565 UNREACHABLE();
1566 }
1567 else if (variable && IsVaryingOut(variable->getQualifier()))
1568 {
1569 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
1570 {
1571 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
1572
1573 if (symbol)
1574 {
1575 // Vertex (output) varyings which are declared but not written to should
1576 // still be declared to allow successful linking
1577 mReferencedVaryings[symbol->getSymbol()] = symbol;
1578 }
1579 else
1580 {
1581 (*sit)->traverse(this);
1582 }
1583 }
1584 }
1585 }
1586 return false;
1587}
1588
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001589bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1590{
Jamie Madill32aab012015-01-27 14:12:26 -05001591 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001593 switch (node->getOp())
1594 {
Olli Etuaho5878f832016-10-07 10:14:58 +01001595 case EOpInvariantDeclaration:
1596 // Do not do any translation
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00001597 return false;
Olli Etuaho5878f832016-10-07 10:14:58 +01001598 case EOpPrototype:
1599 if (visit == PreVisit)
1600 {
Olli Etuahobd674552016-10-06 13:28:42 +01001601 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Olli Etuaho5878f832016-10-07 10:14:58 +01001602 // Skip the prototype if it is not implemented (and thus not used)
1603 if (index == CallDAG::InvalidIndex)
1604 {
1605 return false;
1606 }
1607
1608 TIntermSequence *arguments = node->getSequence();
1609
Olli Etuahobd674552016-10-06 13:28:42 +01001610 TString name =
1611 DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho5878f832016-10-07 10:14:58 +01001612 out << TypeString(node->getType()) << " " << name
1613 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
1614
1615 for (unsigned int i = 0; i < arguments->size(); i++)
1616 {
1617 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1618
1619 if (symbol)
1620 {
1621 out << argumentString(symbol);
1622
1623 if (i < arguments->size() - 1)
1624 {
1625 out << ", ";
1626 }
1627 }
1628 else
1629 UNREACHABLE();
1630 }
1631
1632 out << ");\n";
1633
1634 // Also prototype the Lod0 variant if needed
1635 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1636 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1637 {
1638 mOutputLod0Function = true;
1639 node->traverse(this);
1640 mOutputLod0Function = false;
1641 }
1642
1643 return false;
1644 }
1645 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001646 case EOpFunctionCall:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001647 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001648 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001649
Corentin Wallez1239ee92015-03-19 14:38:02 -07001650 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001651 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001652 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001653 if (node->isArray())
1654 {
1655 UNIMPLEMENTED();
1656 }
Olli Etuahobd674552016-10-06 13:28:42 +01001657 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001658 ASSERT(index != CallDAG::InvalidIndex);
1659 lod0 &= mASTMetadataList[index].mNeedsLod0;
1660
Olli Etuahobd674552016-10-06 13:28:42 +01001661 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001662 out << DisambiguateFunctionName(node->getSequence());
1663 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001664 }
Olli Etuahobd674552016-10-06 13:28:42 +01001665 else if (node->getFunctionSymbolInfo()->getNameObj().isInternal())
Olli Etuahob741c762016-06-29 15:49:22 +03001666 {
1667 // This path is used for internal functions that don't have their definitions in the
1668 // AST, such as precision emulation functions.
Olli Etuahobd674552016-10-06 13:28:42 +01001669 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001670 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671 else
1672 {
Olli Etuahobd674552016-10-06 13:28:42 +01001673 TString name = TFunction::unmangleName(node->getFunctionSymbolInfo()->getName());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001674 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001675 int coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1676 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1677 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1678 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001679 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001680
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001681 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001682 {
Olli Etuaho96963162016-03-21 11:54:33 +02001683 TIntermTyped *typedArg = (*arg)->getAsTyped();
1684 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001685 {
1686 out << "texture_";
1687 (*arg)->traverse(this);
1688 out << ", sampler_";
1689 }
1690
1691 (*arg)->traverse(this);
1692
Olli Etuaho96963162016-03-21 11:54:33 +02001693 if (typedArg->getType().isStructureContainingSamplers())
1694 {
1695 const TType &argType = typedArg->getType();
1696 TVector<TIntermSymbol *> samplerSymbols;
1697 TString structName = samplerNamePrefixFromStruct(typedArg);
1698 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho856c4972016-08-08 11:38:39 +03001699 argType.isArray() ? argType.getArraySize() : 0u,
Olli Etuaho96963162016-03-21 11:54:33 +02001700 &samplerSymbols, nullptr);
1701 for (const TIntermSymbol *sampler : samplerSymbols)
1702 {
1703 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1704 {
1705 out << ", texture_" << sampler->getSymbol();
1706 out << ", sampler_" << sampler->getSymbol();
1707 }
1708 else
1709 {
1710 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1711 // of D3D9, it's the sampler variable.
1712 out << ", " + sampler->getSymbol();
1713 }
1714 }
1715 }
1716
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001717 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001718 {
1719 out << ", ";
1720 }
1721 }
1722
1723 out << ")";
1724
1725 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001726 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001727 case EOpParameters:
1728 outputTriplet(out, visit, "(", ", ", ")\n{\n");
1729 break;
1730 case EOpConstructFloat:
1731 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
1732 break;
1733 case EOpConstructVec2:
1734 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
1735 break;
1736 case EOpConstructVec3:
1737 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
1738 break;
1739 case EOpConstructVec4:
1740 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
1741 break;
1742 case EOpConstructBool:
1743 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
1744 break;
1745 case EOpConstructBVec2:
1746 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
1747 break;
1748 case EOpConstructBVec3:
1749 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
1750 break;
1751 case EOpConstructBVec4:
1752 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
1753 break;
1754 case EOpConstructInt:
1755 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
1756 break;
1757 case EOpConstructIVec2:
1758 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
1759 break;
1760 case EOpConstructIVec3:
1761 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
1762 break;
1763 case EOpConstructIVec4:
1764 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
1765 break;
1766 case EOpConstructUInt:
1767 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
1768 break;
1769 case EOpConstructUVec2:
1770 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
1771 break;
1772 case EOpConstructUVec3:
1773 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
1774 break;
1775 case EOpConstructUVec4:
1776 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
1777 break;
1778 case EOpConstructMat2:
1779 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
1780 break;
1781 case EOpConstructMat2x3:
1782 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
1783 break;
1784 case EOpConstructMat2x4:
1785 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
1786 break;
1787 case EOpConstructMat3x2:
1788 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
1789 break;
1790 case EOpConstructMat3:
1791 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
1792 break;
1793 case EOpConstructMat3x4:
1794 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
1795 break;
1796 case EOpConstructMat4x2:
1797 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
1798 break;
1799 case EOpConstructMat4x3:
1800 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
1801 break;
1802 case EOpConstructMat4:
1803 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
1804 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001805 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04001806 {
Olli Etuahof40319e2015-03-10 14:33:00 +02001807 if (node->getType().isArray())
1808 {
1809 UNIMPLEMENTED();
1810 }
Jamie Madill033dae62014-06-18 12:56:28 -04001811 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001812 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001813 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04001814 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00001815 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001816 case EOpLessThan:
1817 outputTriplet(out, visit, "(", " < ", ")");
1818 break;
1819 case EOpGreaterThan:
1820 outputTriplet(out, visit, "(", " > ", ")");
1821 break;
1822 case EOpLessThanEqual:
1823 outputTriplet(out, visit, "(", " <= ", ")");
1824 break;
1825 case EOpGreaterThanEqual:
1826 outputTriplet(out, visit, "(", " >= ", ")");
1827 break;
1828 case EOpVectorEqual:
1829 outputTriplet(out, visit, "(", " == ", ")");
1830 break;
1831 case EOpVectorNotEqual:
1832 outputTriplet(out, visit, "(", " != ", ")");
1833 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001834 case EOpMod:
1835 ASSERT(node->getUseEmulatedFunction());
1836 writeEmulatedFunctionTriplet(out, visit, "mod(");
1837 break;
1838 case EOpModf:
1839 outputTriplet(out, visit, "modf(", ", ", ")");
1840 break;
1841 case EOpPow:
1842 outputTriplet(out, visit, "pow(", ", ", ")");
1843 break;
1844 case EOpAtan:
1845 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1846 ASSERT(node->getUseEmulatedFunction());
1847 writeEmulatedFunctionTriplet(out, visit, "atan(");
1848 break;
1849 case EOpMin:
1850 outputTriplet(out, visit, "min(", ", ", ")");
1851 break;
1852 case EOpMax:
1853 outputTriplet(out, visit, "max(", ", ", ")");
1854 break;
1855 case EOpClamp:
1856 outputTriplet(out, visit, "clamp(", ", ", ")");
1857 break;
1858 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301859 {
1860 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1861 if (lastParamNode->getType().getBasicType() == EbtBool)
1862 {
1863 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
1864 // so use emulated version.
1865 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001866 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05301867 }
1868 else
1869 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001870 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301871 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001872 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301873 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001874 case EOpStep:
1875 outputTriplet(out, visit, "step(", ", ", ")");
1876 break;
1877 case EOpSmoothStep:
1878 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1879 break;
1880 case EOpDistance:
1881 outputTriplet(out, visit, "distance(", ", ", ")");
1882 break;
1883 case EOpDot:
1884 outputTriplet(out, visit, "dot(", ", ", ")");
1885 break;
1886 case EOpCross:
1887 outputTriplet(out, visit, "cross(", ", ", ")");
1888 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001889 case EOpFaceForward:
1890 ASSERT(node->getUseEmulatedFunction());
1891 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
1892 break;
1893 case EOpReflect:
1894 outputTriplet(out, visit, "reflect(", ", ", ")");
1895 break;
1896 case EOpRefract:
1897 outputTriplet(out, visit, "refract(", ", ", ")");
1898 break;
1899 case EOpOuterProduct:
1900 ASSERT(node->getUseEmulatedFunction());
1901 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
1902 break;
1903 case EOpMul:
1904 outputTriplet(out, visit, "(", " * ", ")");
1905 break;
1906 default:
1907 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908 }
1909
1910 return true;
1911}
1912
Olli Etuaho57961272016-09-14 13:57:46 +03001913void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914{
Olli Etuahoa6f22092015-05-08 18:31:10 +03001915 out << "if (";
1916
1917 node->getCondition()->traverse(this);
1918
1919 out << ")\n";
1920
Jamie Madill8c46ab12015-12-07 16:39:19 -05001921 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03001922
1923 bool discard = false;
1924
1925 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03001927 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001928 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00001929
Olli Etuahoa6f22092015-05-08 18:31:10 +03001930 // Detect true discard
1931 discard = (discard || FindDiscard::search(node->getTrueBlock()));
1932 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03001933 else
1934 {
1935 // TODO(oetuaho): Check if the semicolon inside is necessary.
1936 // It's there as a result of conservative refactoring of the output.
1937 out << "{;}\n";
1938 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08001939
Jamie Madill8c46ab12015-12-07 16:39:19 -05001940 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001941
Olli Etuahoa6f22092015-05-08 18:31:10 +03001942 if (node->getFalseBlock())
1943 {
1944 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001945
Jamie Madill8c46ab12015-12-07 16:39:19 -05001946 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947
Olli Etuaho32db19b2016-10-04 14:43:16 +01001948 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03001949 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05001950
Jamie Madill8c46ab12015-12-07 16:39:19 -05001951 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001952
Olli Etuahoa6f22092015-05-08 18:31:10 +03001953 // Detect false discard
1954 discard = (discard || FindDiscard::search(node->getFalseBlock()));
1955 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001956
Olli Etuahoa6f22092015-05-08 18:31:10 +03001957 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03001958 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03001959 {
1960 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00001961 }
Olli Etuahod81ed842015-05-12 12:46:35 +03001962}
1963
Olli Etuahod0bad2c2016-09-09 18:01:16 +03001964bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
1965{
1966 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
1967 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
1968 UNREACHABLE();
1969 return false;
1970}
1971
Olli Etuaho57961272016-09-14 13:57:46 +03001972bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03001973{
1974 TInfoSinkBase &out = getInfoSink();
1975
Olli Etuaho3d932d82016-04-12 11:10:30 +03001976 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03001977
1978 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07001979 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03001980 {
1981 out << "FLATTEN ";
1982 }
1983
Olli Etuaho57961272016-09-14 13:57:46 +03001984 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985
1986 return false;
1987}
1988
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001989bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02001990{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001991 TInfoSinkBase &out = getInfoSink();
1992
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001993 if (node->getStatementList())
1994 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02001995 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05001996 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02001997 // The curly braces get written when visiting the statementList aggregate
1998 }
1999 else
2000 {
2001 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002002 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002003 }
2004 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002005}
2006
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002007bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002008{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002009 TInfoSinkBase &out = getInfoSink();
2010
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002011 if (node->hasCondition())
2012 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002013 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002014 return true;
2015 }
2016 else
2017 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002018 out << "default:\n";
2019 return false;
2020 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002021}
2022
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2024{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002025 TInfoSinkBase &out = getInfoSink();
2026 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027}
2028
2029bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2030{
Nicolas Capens655fe362014-04-11 13:12:34 -04002031 mNestedLoopDepth++;
2032
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002033 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002034 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002035 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002036
Jamie Madill8c46ab12015-12-07 16:39:19 -05002037 TInfoSinkBase &out = getInfoSink();
2038
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002039 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002040 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002041 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002042 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002043 mInsideDiscontinuousLoop = wasDiscontinuous;
2044 mNestedLoopDepth--;
2045
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002046 return false;
2047 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002048 }
2049
Corentin Wallez1239ee92015-03-19 14:38:02 -07002050 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002051 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002053 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002054
Jamie Madill8c46ab12015-12-07 16:39:19 -05002055 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056 }
2057 else
2058 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002059 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002060
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002061 if (node->getInit())
2062 {
2063 node->getInit()->traverse(this);
2064 }
2065
2066 out << "; ";
2067
alokp@chromium.org52813552010-11-16 18:36:09 +00002068 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002070 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002071 }
2072
2073 out << "; ";
2074
alokp@chromium.org52813552010-11-16 18:36:09 +00002075 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002077 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078 }
2079
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002080 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002081
Jamie Madill8c46ab12015-12-07 16:39:19 -05002082 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083 }
2084
2085 if (node->getBody())
2086 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002087 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002088 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002090 else
2091 {
2092 // TODO(oetuaho): Check if the semicolon inside is necessary.
2093 // It's there as a result of conservative refactoring of the output.
2094 out << "{;}\n";
2095 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096
Jamie Madill8c46ab12015-12-07 16:39:19 -05002097 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098
alokp@chromium.org52813552010-11-16 18:36:09 +00002099 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002101 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102 out << "while(\n";
2103
alokp@chromium.org52813552010-11-16 18:36:09 +00002104 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105
daniel@transgaming.com73536982012-03-21 20:45:49 +00002106 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 }
2108
daniel@transgaming.com73536982012-03-21 20:45:49 +00002109 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002111 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002112 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002113
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002114 return false;
2115}
2116
2117bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2118{
Jamie Madill32aab012015-01-27 14:12:26 -05002119 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120
2121 switch (node->getFlowOp())
2122 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002123 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002124 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002125 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002126 case EOpBreak:
2127 if (visit == PreVisit)
2128 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002129 if (mNestedLoopDepth > 1)
2130 {
2131 mUsesNestedBreak = true;
2132 }
2133
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002134 if (mExcessiveLoopIndex)
2135 {
2136 out << "{Break";
2137 mExcessiveLoopIndex->traverse(this);
2138 out << " = true; break;}\n";
2139 }
2140 else
2141 {
2142 out << "break;\n";
2143 }
2144 }
2145 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002146 case EOpContinue:
2147 outputTriplet(out, visit, "continue;\n", "", "");
2148 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149 case EOpReturn:
2150 if (visit == PreVisit)
2151 {
2152 if (node->getExpression())
2153 {
2154 out << "return ";
2155 }
2156 else
2157 {
2158 out << "return;\n";
2159 }
2160 }
2161 else if (visit == PostVisit)
2162 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002163 if (node->getExpression())
2164 {
2165 out << ";\n";
2166 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 }
2168 break;
2169 default: UNREACHABLE();
2170 }
2171
2172 return true;
2173}
2174
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002175// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2176// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002177bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002178{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002179 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002180
2181 // Parse loops of the form:
2182 // for(int index = initial; index [comparator] limit; index += increment)
2183 TIntermSymbol *index = NULL;
2184 TOperator comparator = EOpNull;
2185 int initial = 0;
2186 int limit = 0;
2187 int increment = 0;
2188
2189 // Parse index name and intial value
2190 if (node->getInit())
2191 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002192 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002193
2194 if (init)
2195 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002196 TIntermSequence *sequence = init->getSequence();
2197 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002198
2199 if (variable && variable->getQualifier() == EvqTemporary)
2200 {
2201 TIntermBinary *assign = variable->getAsBinaryNode();
2202
2203 if (assign->getOp() == EOpInitialize)
2204 {
2205 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2206 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2207
2208 if (symbol && constant)
2209 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002210 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002211 {
2212 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002213 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002214 }
2215 }
2216 }
2217 }
2218 }
2219 }
2220
2221 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00002222 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002223 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002224 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002225
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002226 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2227 {
2228 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2229
2230 if (constant)
2231 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002232 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002233 {
2234 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002235 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002236 }
2237 }
2238 }
2239 }
2240
2241 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00002242 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002243 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002244 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
2245 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002246
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002247 if (binaryTerminal)
2248 {
2249 TOperator op = binaryTerminal->getOp();
2250 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2251
2252 if (constant)
2253 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002254 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002255 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002256 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002257
2258 switch (op)
2259 {
2260 case EOpAddAssign: increment = value; break;
2261 case EOpSubAssign: increment = -value; break;
2262 default: UNIMPLEMENTED();
2263 }
2264 }
2265 }
2266 }
2267 else if (unaryTerminal)
2268 {
2269 TOperator op = unaryTerminal->getOp();
2270
2271 switch (op)
2272 {
2273 case EOpPostIncrement: increment = 1; break;
2274 case EOpPostDecrement: increment = -1; break;
2275 case EOpPreIncrement: increment = 1; break;
2276 case EOpPreDecrement: increment = -1; break;
2277 default: UNIMPLEMENTED();
2278 }
2279 }
2280 }
2281
2282 if (index != NULL && comparator != EOpNull && increment != 0)
2283 {
2284 if (comparator == EOpLessThanEqual)
2285 {
2286 comparator = EOpLessThan;
2287 limit += 1;
2288 }
2289
2290 if (comparator == EOpLessThan)
2291 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002292 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002293
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002294 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002295 {
2296 return false; // Not an excessive loop
2297 }
2298
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002299 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
2300 mExcessiveLoopIndex = index;
2301
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002302 out << "{int ";
2303 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002304 out << ";\n"
2305 "bool Break";
2306 index->traverse(this);
2307 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002308
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002309 bool firstLoopFragment = true;
2310
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002311 while (iterations > 0)
2312 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002313 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002314
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002315 if (!firstLoopFragment)
2316 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002317 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002318 index->traverse(this);
2319 out << ") {\n";
2320 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002321
2322 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
2323 {
2324 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
2325 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002326
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002327 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07002328 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002329
Corentin Wallez1239ee92015-03-19 14:38:02 -07002330 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002331 index->traverse(this);
2332 out << " = ";
2333 out << initial;
2334
2335 out << "; ";
2336 index->traverse(this);
2337 out << " < ";
2338 out << clampedLimit;
2339
2340 out << "; ";
2341 index->traverse(this);
2342 out << " += ";
2343 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002344 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002345
Jamie Madill8c46ab12015-12-07 16:39:19 -05002346 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002347 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002348
2349 if (node->getBody())
2350 {
2351 node->getBody()->traverse(this);
2352 }
2353
Jamie Madill8c46ab12015-12-07 16:39:19 -05002354 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002355 out << ";}\n";
2356
2357 if (!firstLoopFragment)
2358 {
2359 out << "}\n";
2360 }
2361
2362 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002363
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002364 initial += MAX_LOOP_ITERATIONS * increment;
2365 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002366 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002367
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002368 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002369
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002370 mExcessiveLoopIndex = restoreIndex;
2371
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002372 return true;
2373 }
2374 else UNIMPLEMENTED();
2375 }
2376
2377 return false; // Not handled as an excessive loop
2378}
2379
Jamie Madill8c46ab12015-12-07 16:39:19 -05002380void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2381 Visit visit,
2382 const char *preString,
2383 const char *inString,
2384 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002386 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387 {
2388 out << preString;
2389 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002390 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391 {
2392 out << inString;
2393 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002394 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395 {
2396 out << postString;
2397 }
2398}
2399
Jamie Madill8c46ab12015-12-07 16:39:19 -05002400void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002401{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002402 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002403 {
Jamie Madill32aab012015-01-27 14:12:26 -05002404 out << "\n";
2405 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002406
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002407 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002408 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002409 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002410 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002411
Jamie Madill32aab012015-01-27 14:12:26 -05002412 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002413 }
2414}
2415
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002416TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2417{
2418 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002419 const TType &type = symbol->getType();
2420 const TName &name = symbol->getName();
2421 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002422
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002423 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002424 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002425 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002426 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002427 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002428 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002429 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002430 }
2431
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002432 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002433 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002434 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2435 {
2436 // Samplers are passed as indices to the sampler array.
2437 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2438 return "const uint " + nameStr + ArrayString(type);
2439 }
2440 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2441 {
2442 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2443 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2444 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2445 ArrayString(type);
2446 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002447 }
2448
Olli Etuaho96963162016-03-21 11:54:33 +02002449 TStringStream argString;
2450 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2451 << ArrayString(type);
2452
2453 // If the structure parameter contains samplers, they need to be passed into the function as
2454 // separate parameters. HLSL doesn't natively support samplers in structs.
2455 if (type.isStructureContainingSamplers())
2456 {
2457 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2458 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho856c4972016-08-08 11:38:39 +03002459 type.createSamplerSymbols("angle" + nameStr, "", 0u, &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002460 for (const TIntermSymbol *sampler : samplerSymbols)
2461 {
2462 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2463 {
2464 argString << ", const uint " << sampler->getSymbol() << ArrayString(type);
2465 }
2466 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2467 {
2468 const TType &samplerType = sampler->getType();
2469 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2470 type.getArraySize() == samplerType.getArraySize());
2471 ASSERT(IsSampler(samplerType.getBasicType()));
2472 argString << ", " << QualifierString(qualifier) << " "
2473 << TextureString(samplerType.getBasicType()) << " texture_"
2474 << sampler->getSymbol() << ArrayString(type) << ", "
2475 << QualifierString(qualifier) << " "
2476 << SamplerString(samplerType.getBasicType()) << " sampler_"
2477 << sampler->getSymbol() << ArrayString(type);
2478 }
2479 else
2480 {
2481 const TType &samplerType = sampler->getType();
2482 ASSERT((!type.isArray() && !samplerType.isArray()) ||
2483 type.getArraySize() == samplerType.getArraySize());
2484 ASSERT(IsSampler(samplerType.getBasicType()));
2485 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
2486 << " " << sampler->getSymbol() << ArrayString(type);
2487 }
2488 }
2489 }
2490
2491 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002492}
2493
2494TString OutputHLSL::initializer(const TType &type)
2495{
2496 TString string;
2497
Jamie Madill94bf7f22013-07-08 13:31:15 -04002498 size_t size = type.getObjectSize();
2499 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002500 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002501 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502
Jamie Madill94bf7f22013-07-08 13:31:15 -04002503 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002504 {
2505 string += ", ";
2506 }
2507 }
2508
daniel@transgaming.comead23042010-04-29 03:35:36 +00002509 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002510}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002511
Jamie Madill8c46ab12015-12-07 16:39:19 -05002512void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2513 Visit visit,
2514 const TType &type,
2515 const char *name,
2516 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002517{
Olli Etuahof40319e2015-03-10 14:33:00 +02002518 if (type.isArray())
2519 {
2520 UNIMPLEMENTED();
2521 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002522
2523 if (visit == PreVisit)
2524 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002525 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002526
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002527 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002528 }
2529 else if (visit == InVisit)
2530 {
2531 out << ", ";
2532 }
2533 else if (visit == PostVisit)
2534 {
2535 out << ")";
2536 }
2537}
2538
Jamie Madill8c46ab12015-12-07 16:39:19 -05002539const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2540 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002541 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002542{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002543 const TConstantUnion *constUnionIterated = constUnion;
2544
Jamie Madill98493dd2013-07-08 14:39:03 -04002545 const TStructure* structure = type.getStruct();
2546 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002547 {
Jamie Madill033dae62014-06-18 12:56:28 -04002548 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002549
Jamie Madill98493dd2013-07-08 14:39:03 -04002550 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002551
Jamie Madill98493dd2013-07-08 14:39:03 -04002552 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002553 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002554 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002555 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002556
Jamie Madill98493dd2013-07-08 14:39:03 -04002557 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002558 {
2559 out << ", ";
2560 }
2561 }
2562
2563 out << ")";
2564 }
2565 else
2566 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002567 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002568 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002569
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002570 if (writeType)
2571 {
Jamie Madill033dae62014-06-18 12:56:28 -04002572 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002573 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002574 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002575 if (writeType)
2576 {
2577 out << ")";
2578 }
2579 }
2580
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002581 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002582}
2583
Jamie Madill8c46ab12015-12-07 16:39:19 -05002584void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002585{
2586 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05002587 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002588}
2589
Jamie Madill37997142015-01-28 10:06:34 -05002590bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
2591{
2592 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2593 expression->traverse(&searchSymbol);
2594
2595 if (searchSymbol.foundMatch())
2596 {
2597 // Type already printed
2598 out << "t" + str(mUniqueIndex) + " = ";
2599 expression->traverse(this);
2600 out << ", ";
2601 symbolNode->traverse(this);
2602 out << " = t" + str(mUniqueIndex);
2603
2604 mUniqueIndex++;
2605 return true;
2606 }
2607
2608 return false;
2609}
2610
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002611bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2612{
2613 // We support writing constant unions and constructors that only take constant unions as
2614 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002615 return expression->getAsConstantUnion() ||
2616 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002617}
2618
2619bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2620 TIntermSymbol *symbolNode,
2621 TIntermTyped *expression)
2622{
2623 if (canWriteAsHLSLLiteral(expression))
2624 {
2625 symbolNode->traverse(this);
2626 if (expression->getType().isArray())
2627 {
2628 out << "[" << expression->getType().getArraySize() << "]";
2629 }
2630 out << " = {";
2631 if (expression->getAsConstantUnion())
2632 {
2633 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2634 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2635 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2636 }
2637 else
2638 {
2639 TIntermAggregate *constructor = expression->getAsAggregate();
2640 ASSERT(constructor != nullptr);
2641 for (TIntermNode *&node : *constructor->getSequence())
2642 {
2643 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2644 ASSERT(nodeConst);
2645 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
2646 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
2647 if (node != constructor->getSequence()->back())
2648 {
2649 out << ", ";
2650 }
2651 }
2652 }
2653 out << "}";
2654 return true;
2655 }
2656 return false;
2657}
2658
Jamie Madill55e79e02015-02-09 15:35:00 -05002659TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2660{
2661 const TFieldList &fields = structure.fields();
2662
2663 for (const auto &eqFunction : mStructEqualityFunctions)
2664 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002665 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002666 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002667 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002668 }
2669 }
2670
2671 const TString &structNameString = StructNameString(structure);
2672
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002673 StructEqualityFunction *function = new StructEqualityFunction();
2674 function->structure = &structure;
2675 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002676
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002677 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002678
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002679 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
2680 << "{\n"
2681 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002682
2683 for (size_t i = 0; i < fields.size(); i++)
2684 {
2685 const TField *field = fields[i];
2686 const TType *fieldType = field->type();
2687
2688 const TString &fieldNameA = "a." + Decorate(field->name());
2689 const TString &fieldNameB = "b." + Decorate(field->name());
2690
2691 if (i > 0)
2692 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002693 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002694 }
2695
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002696 fnOut << "(";
2697 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2698 fnOut << fieldNameA;
2699 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2700 fnOut << fieldNameB;
2701 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2702 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002703 }
2704
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002705 fnOut << ";\n" << "}\n";
2706
2707 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002708
2709 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002710 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002711
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002712 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002713}
2714
Olli Etuaho7fb49552015-03-18 17:27:44 +02002715TString OutputHLSL::addArrayEqualityFunction(const TType& type)
2716{
2717 for (const auto &eqFunction : mArrayEqualityFunctions)
2718 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002719 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002720 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002721 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002722 }
2723 }
2724
2725 const TString &typeName = TypeString(type);
2726
Olli Etuaho12690762015-03-31 12:55:28 +03002727 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002728 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002729
2730 TInfoSinkBase fnNameOut;
2731 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002732 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002733
2734 TType nonArrayType = type;
2735 nonArrayType.clearArrayness();
2736
2737 TInfoSinkBase fnOut;
2738
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002739 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03002740 << typeName << " a[" << type.getArraySize() << "], "
2741 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002742 << "{\n"
2743 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2744 " {\n"
2745 " if (";
2746
2747 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2748 fnOut << "a[i]";
2749 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2750 fnOut << "b[i]";
2751 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2752
2753 fnOut << ") { return false; }\n"
2754 " }\n"
2755 " return true;\n"
2756 "}\n";
2757
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002758 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002759
2760 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002761 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002762
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002763 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002764}
2765
Olli Etuaho12690762015-03-31 12:55:28 +03002766TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
2767{
2768 for (const auto &assignFunction : mArrayAssignmentFunctions)
2769 {
2770 if (assignFunction.type == type)
2771 {
2772 return assignFunction.functionName;
2773 }
2774 }
2775
2776 const TString &typeName = TypeString(type);
2777
2778 ArrayHelperFunction function;
2779 function.type = type;
2780
2781 TInfoSinkBase fnNameOut;
2782 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2783 function.functionName = fnNameOut.c_str();
2784
2785 TInfoSinkBase fnOut;
2786
2787 fnOut << "void " << function.functionName << "(out "
2788 << typeName << " a[" << type.getArraySize() << "], "
2789 << typeName << " b[" << type.getArraySize() << "])\n"
2790 << "{\n"
2791 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
2792 " {\n"
2793 " a[i] = b[i];\n"
2794 " }\n"
2795 "}\n";
2796
2797 function.functionDefinition = fnOut.c_str();
2798
2799 mArrayAssignmentFunctions.push_back(function);
2800
2801 return function.functionName;
2802}
2803
Olli Etuaho9638c352015-04-01 14:34:52 +03002804TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
2805{
2806 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2807 {
2808 if (constructIntoFunction.type == type)
2809 {
2810 return constructIntoFunction.functionName;
2811 }
2812 }
2813
2814 const TString &typeName = TypeString(type);
2815
2816 ArrayHelperFunction function;
2817 function.type = type;
2818
2819 TInfoSinkBase fnNameOut;
2820 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2821 function.functionName = fnNameOut.c_str();
2822
2823 TInfoSinkBase fnOut;
2824
2825 fnOut << "void " << function.functionName << "(out "
2826 << typeName << " a[" << type.getArraySize() << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002827 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002828 {
2829 fnOut << ", " << typeName << " b" << i;
2830 }
2831 fnOut << ")\n"
2832 "{\n";
2833
Olli Etuaho856c4972016-08-08 11:38:39 +03002834 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002835 {
2836 fnOut << " a[" << i << "] = b" << i << ";\n";
2837 }
2838 fnOut << "}\n";
2839
2840 function.functionDefinition = fnOut.c_str();
2841
2842 mArrayConstructIntoFunctions.push_back(function);
2843
2844 return function.functionName;
2845}
2846
Jamie Madill2e295e22015-04-29 10:41:33 -04002847void OutputHLSL::ensureStructDefined(const TType &type)
2848{
2849 TStructure *structure = type.getStruct();
2850
2851 if (structure)
2852 {
2853 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2854 }
2855}
2856
2857
Olli Etuaho9638c352015-04-01 14:34:52 +03002858
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002859}