blob: 0dc1c561ca8fd2ab9f34496def975e1d57e793c1 [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
Jamie Madill45bcc782016-11-07 13:58:48 -050031namespace sh
32{
33
Olli Etuaho56a2f952016-12-08 12:16:27 +000034void OutputHLSL::writeFloat(TInfoSinkBase &out, float f)
Olli Etuaho4785fec2015-05-18 16:09:37 +030035{
Olli Etuaho56a2f952016-12-08 12:16:27 +000036 // This is known not to work for NaN on all drivers but make the best effort to output NaNs
37 // regardless.
38 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300 &&
39 mOutputType == SH_HLSL_4_1_OUTPUT)
40 {
41 out << "asfloat(" << gl::bitCast<uint32_t>(f) << "u)";
42 }
43 else
44 {
45 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
46 }
47}
Olli Etuaho4785fec2015-05-18 16:09:37 +030048
Olli Etuaho56a2f952016-12-08 12:16:27 +000049void OutputHLSL::writeSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
Olli Etuaho18b9deb2015-11-05 12:14:50 +020050{
51 ASSERT(constUnion != nullptr);
52 switch (constUnion->getType())
53 {
54 case EbtFloat:
Olli Etuaho56a2f952016-12-08 12:16:27 +000055 writeFloat(out, constUnion->getFConst());
Olli Etuaho18b9deb2015-11-05 12:14:50 +020056 break;
57 case EbtInt:
58 out << constUnion->getIConst();
59 break;
60 case EbtUInt:
61 out << constUnion->getUConst();
62 break;
63 case EbtBool:
64 out << constUnion->getBConst();
65 break;
66 default:
67 UNREACHABLE();
68 }
69}
70
Olli Etuaho56a2f952016-12-08 12:16:27 +000071const TConstantUnion *OutputHLSL::writeConstantUnionArray(TInfoSinkBase &out,
72 const TConstantUnion *const constUnion,
73 const size_t size)
Olli Etuaho18b9deb2015-11-05 12:14:50 +020074{
75 const TConstantUnion *constUnionIterated = constUnion;
76 for (size_t i = 0; i < size; i++, constUnionIterated++)
77 {
Olli Etuaho56a2f952016-12-08 12:16:27 +000078 writeSingleConstant(out, constUnionIterated);
Olli Etuaho18b9deb2015-11-05 12:14:50 +020079
80 if (i != size - 1)
81 {
82 out << ", ";
83 }
84 }
85 return constUnionIterated;
86}
87
Qiankun Miao7ebb97f2016-09-08 18:01:50 +080088OutputHLSL::OutputHLSL(sh::GLenum shaderType,
89 int shaderVersion,
90 const TExtensionBehavior &extensionBehavior,
91 const char *sourcePath,
92 ShShaderOutput outputType,
93 int numRenderTargets,
94 const std::vector<Uniform> &uniforms,
95 ShCompileOptions compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -040096 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020097 mShaderType(shaderType),
98 mShaderVersion(shaderVersion),
99 mExtensionBehavior(extensionBehavior),
100 mSourcePath(sourcePath),
101 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700102 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000103 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700104 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000106 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000107
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500108 mUsesFragColor = false;
109 mUsesFragData = false;
110 mUsesDepthRange = false;
111 mUsesFragCoord = false;
112 mUsesPointCoord = false;
113 mUsesFrontFacing = false;
114 mUsesPointSize = false;
115 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500116 mUsesVertexID = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500117 mUsesFragDepth = false;
Xinghua Caob1239382016-12-13 15:07:05 +0800118 mUsesNumWorkGroups = false;
119 mUsesWorkGroupID = false;
120 mUsesLocalInvocationID = false;
121 mUsesGlobalInvocationID = false;
122 mUsesLocalInvocationIndex = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500123 mUsesXor = false;
124 mUsesDiscardRewriting = false;
125 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530126 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000127
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000128 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000129
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500130 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000131 mInsideDiscontinuousLoop = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500132 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000133
Yunchao Hed7297bf2017-04-19 15:27:10 +0800134 mExcessiveLoopIndex = nullptr;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000135
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500136 mStructureHLSL = new StructureHLSL;
137 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300138 mTextureFunctionHLSL = new TextureFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400139
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200140 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000141 {
Arun Patole63419392015-03-13 11:51:07 +0530142 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500143 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
144 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530145 // In both cases total 3 uniform registers need to be reserved.
146 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000147 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000148
Geoff Lang00140f42016-02-03 18:47:33 +0000149 // Reserve registers for the default uniform block and driver constants
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800150 mUniformHLSL->reserveUniformBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000151}
152
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000153OutputHLSL::~OutputHLSL()
154{
Jamie Madill8daaba12014-06-13 10:04:33 -0400155 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400156 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300157 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200158 for (auto &eqFunction : mStructEqualityFunctions)
159 {
160 SafeDelete(eqFunction);
161 }
162 for (auto &eqFunction : mArrayEqualityFunctions)
163 {
164 SafeDelete(eqFunction);
165 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000166}
167
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200168void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000169{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500170 const std::vector<TIntermTyped *> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400171 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000172
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200173 BuiltInFunctionEmulator builtInFunctionEmulator;
174 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800175 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
176 {
177 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
178 mShaderVersion);
179 }
180
Olli Etuahodfa75e82017-01-23 09:43:06 -0800181 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500182
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700183 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000184 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700185 ASSERT(success == CallDAG::INITDAG_SUCCESS);
186 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700187
Jamie Madill37997142015-01-28 10:06:34 -0500188 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500189 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200190 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500191 mInfoSinkStack.pop();
192
Jamie Madill37997142015-01-28 10:06:34 -0500193 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500194 mInfoSinkStack.pop();
195
Jamie Madill32aab012015-01-27 14:12:26 -0500196 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500197 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500198 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000199
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200200 objSink << mHeader.c_str();
201 objSink << mBody.c_str();
202 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200203
Olli Etuahodfa75e82017-01-23 09:43:06 -0800204 builtInFunctionEmulator.cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000205}
206
Jamie Madill570e04d2013-06-21 09:15:33 -0400207void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
208{
209 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
210 {
211 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
212
Jamie Madill32aab012015-01-27 14:12:26 -0500213 TInfoSinkBase structInfoSink;
214 mInfoSinkStack.push(&structInfoSink);
215
Jamie Madill570e04d2013-06-21 09:15:33 -0400216 // This will mark the necessary block elements as referenced
217 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500218
219 TString structName(structInfoSink.c_str());
220 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400221
222 mFlaggedStructOriginalNames[flaggedNode] = structName;
223
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500224 for (size_t pos = structName.find('.'); pos != std::string::npos;
225 pos = structName.find('.'))
Jamie Madill570e04d2013-06-21 09:15:33 -0400226 {
227 structName.erase(pos, 1);
228 }
229
230 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
231 }
232}
233
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800234const std::map<std::string, unsigned int> &OutputHLSL::getUniformBlockRegisterMap() const
Jamie Madill4e1fd412014-07-10 17:50:10 -0400235{
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800236 return mUniformHLSL->getUniformBlockRegisterMap();
Jamie Madill4e1fd412014-07-10 17:50:10 -0400237}
238
Jamie Madill9fe25e92014-07-18 10:33:08 -0400239const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
240{
241 return mUniformHLSL->getUniformRegisterMap();
242}
243
Olli Etuahoed049ab2017-06-30 17:38:33 +0300244TString OutputHLSL::structInitializerString(int indent, const TType &type, const TString &name)
Jamie Madill570e04d2013-06-21 09:15:33 -0400245{
246 TString init;
247
Olli Etuahoed049ab2017-06-30 17:38:33 +0300248 TString indentString;
249 for (int spaces = 0; spaces < indent; spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400250 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300251 indentString += " ";
Jamie Madill570e04d2013-06-21 09:15:33 -0400252 }
253
Olli Etuahoed049ab2017-06-30 17:38:33 +0300254 if (type.isArray())
Jamie Madill570e04d2013-06-21 09:15:33 -0400255 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300256 init += indentString + "{\n";
257 for (unsigned int arrayIndex = 0u; arrayIndex < type.getArraySize(); ++arrayIndex)
Jamie Madill570e04d2013-06-21 09:15:33 -0400258 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300259 TStringStream indexedString;
260 indexedString << name << "[" << arrayIndex << "]";
261 TType elementType = type;
262 elementType.clearArrayness();
263 init += structInitializerString(indent + 1, elementType, indexedString.str());
264 if (arrayIndex < type.getArraySize() - 1)
265 {
266 init += ",";
267 }
268 init += "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400269 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300270 init += indentString + "}";
Jamie Madill570e04d2013-06-21 09:15:33 -0400271 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300272 else if (type.getBasicType() == EbtStruct)
273 {
274 init += indentString + "{\n";
275 const TStructure &structure = *type.getStruct();
276 const TFieldList &fields = structure.fields();
277 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
278 {
279 const TField &field = *fields[fieldIndex];
280 const TString &fieldName = name + "." + Decorate(field.name());
281 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400282
Olli Etuahoed049ab2017-06-30 17:38:33 +0300283 init += structInitializerString(indent + 1, fieldType, fieldName);
284 if (fieldIndex < fields.size() - 1)
285 {
286 init += ",";
287 }
288 init += "\n";
289 }
290 init += indentString + "}";
291 }
292 else
293 {
294 init += indentString + name;
295 }
Jamie Madill570e04d2013-06-21 09:15:33 -0400296
297 return init;
298}
299
Jamie Madill8c46ab12015-12-07 16:39:19 -0500300void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000302 TString varyings;
303 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400304 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000305
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500306 for (std::map<TIntermTyped *, TString>::const_iterator flaggedStructIt =
307 mFlaggedStructMappedNames.begin();
308 flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400309 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500310 TIntermTyped *structNode = flaggedStructIt->first;
311 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400312 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400313 const TString &originalName = mFlaggedStructOriginalNames[structNode];
314
Olli Etuahoed049ab2017-06-30 17:38:33 +0300315 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName;
316 if (structNode->isArray())
317 {
318 flaggedStructs += ArrayString(structNode->getType());
319 }
320 flaggedStructs += " =\n";
321 flaggedStructs += structInitializerString(0, structNode->getType(), originalName);
322 flaggedStructs += ";\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400323 }
324
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500325 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin();
326 varying != mReferencedVaryings.end(); varying++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000327 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500328 const TType &type = varying->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000329 const TString &name = varying->second->getSymbol();
330
331 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500332 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
333 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000334 }
335
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500336 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin();
337 attribute != mReferencedAttributes.end(); attribute++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000338 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500339 const TType &type = attribute->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000340 const TString &name = attribute->second->getSymbol();
341
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500342 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
343 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000344 }
345
Jamie Madill8daaba12014-06-13 10:04:33 -0400346 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400347
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200348 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800349 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400350
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200351 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500352 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200353 out << "\n// Equality functions\n\n";
354 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500355 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200356 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200357 }
358 }
Olli Etuaho12690762015-03-31 12:55:28 +0300359 if (!mArrayAssignmentFunctions.empty())
360 {
361 out << "\n// Assignment functions\n\n";
362 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
363 {
364 out << assignmentFunction.functionDefinition << "\n";
365 }
366 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300367 if (!mArrayConstructIntoFunctions.empty())
368 {
369 out << "\n// Array constructor functions\n\n";
370 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
371 {
372 out << constructIntoFunction.functionDefinition << "\n";
373 }
374 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200375
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500376 if (mUsesDiscardRewriting)
377 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400378 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500379 }
380
Nicolas Capens655fe362014-04-11 13:12:34 -0400381 if (mUsesNestedBreak)
382 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400383 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400384 }
385
Arun Patole44efa0b2015-03-04 17:11:05 +0530386 if (mRequiresIEEEStrictCompiling)
387 {
388 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
389 }
390
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400391 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
392 "#define LOOP [loop]\n"
393 "#define FLATTEN [flatten]\n"
394 "#else\n"
395 "#define LOOP\n"
396 "#define FLATTEN\n"
397 "#endif\n";
398
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200399 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200401 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500402 const bool usingMRTExtension = (iter != mExtensionBehavior.end() &&
403 (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000404
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000405 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500406 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400407 out << "\n";
408
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200409 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000410 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500411 for (ReferencedSymbols::const_iterator outputVariableIt =
412 mReferencedOutputVariables.begin();
413 outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000414 {
Jamie Madill46131a32013-06-20 11:55:50 -0400415 const TString &variableName = outputVariableIt->first;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500416 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400417
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500418 out << "static " + TypeString(variableType) + " out_" + variableName +
419 ArrayString(variableType) + " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000420 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000421 }
Jamie Madill46131a32013-06-20 11:55:50 -0400422 else
423 {
424 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
425
426 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500427 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400428 for (unsigned int i = 0; i < numColorValues; i++)
429 {
430 out << " float4(0, 0, 0, 0)";
431 if (i + 1 != numColorValues)
432 {
433 out << ",";
434 }
435 out << "\n";
436 }
437
438 out << "};\n";
439 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000440
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400441 if (mUsesFragDepth)
442 {
443 out << "static float gl_Depth = 0.0;\n";
444 }
445
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000446 if (mUsesFragCoord)
447 {
448 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
449 }
450
451 if (mUsesPointCoord)
452 {
453 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
454 }
455
456 if (mUsesFrontFacing)
457 {
458 out << "static bool gl_FrontFacing = false;\n";
459 }
460
461 out << "\n";
462
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000463 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000464 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000465 out << "struct gl_DepthRangeParameters\n"
466 "{\n"
467 " float near;\n"
468 " float far;\n"
469 " float diff;\n"
470 "};\n"
471 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000472 }
473
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200474 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000475 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000476 out << "cbuffer DriverConstants : register(b1)\n"
477 "{\n";
478
479 if (mUsesDepthRange)
480 {
481 out << " float3 dx_DepthRange : packoffset(c0);\n";
482 }
483
484 if (mUsesFragCoord)
485 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000486 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000487 }
488
489 if (mUsesFragCoord || mUsesFrontFacing)
490 {
491 out << " float3 dx_DepthFront : packoffset(c2);\n";
492 }
493
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800494 if (mUsesFragCoord)
495 {
496 // dx_ViewScale is only used in the fragment shader to correct
497 // the value for glFragCoord if necessary
498 out << " float2 dx_ViewScale : packoffset(c3);\n";
499 }
500
Olli Etuaho618bebc2016-01-15 16:40:00 +0200501 if (mOutputType == SH_HLSL_4_1_OUTPUT)
502 {
503 mUniformHLSL->samplerMetadataUniforms(out, "c4");
504 }
505
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000506 out << "};\n";
507 }
508 else
509 {
510 if (mUsesDepthRange)
511 {
512 out << "uniform float3 dx_DepthRange : register(c0);";
513 }
514
515 if (mUsesFragCoord)
516 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000517 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000518 }
519
520 if (mUsesFragCoord || mUsesFrontFacing)
521 {
522 out << "uniform float3 dx_DepthFront : register(c2);\n";
523 }
524 }
525
526 out << "\n";
527
528 if (mUsesDepthRange)
529 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500530 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
531 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000532 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000533 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000534
Jamie Madillf91ce812014-06-13 10:04:34 -0400535 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000536 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400537 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000538 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400539 out << flaggedStructs;
540 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000541 }
542
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000543 if (usingMRTExtension && mNumRenderTargets > 1)
544 {
545 out << "#define GL_USES_MRT\n";
546 }
547
548 if (mUsesFragColor)
549 {
550 out << "#define GL_USES_FRAG_COLOR\n";
551 }
552
553 if (mUsesFragData)
554 {
555 out << "#define GL_USES_FRAG_DATA\n";
556 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557 }
Xinghua Caob1239382016-12-13 15:07:05 +0800558 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000559 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000560 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500561 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000562 out << "\n"
563 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400564
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000565 if (mUsesPointSize)
566 {
567 out << "static float gl_PointSize = float(1);\n";
568 }
569
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000570 if (mUsesInstanceID)
571 {
572 out << "static int gl_InstanceID;";
573 }
574
Corentin Wallezb076add2016-01-11 16:45:46 -0500575 if (mUsesVertexID)
576 {
577 out << "static int gl_VertexID;";
578 }
579
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000580 out << "\n"
581 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500582 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000583 out << "\n";
584
585 if (mUsesDepthRange)
586 {
587 out << "struct gl_DepthRangeParameters\n"
588 "{\n"
589 " float near;\n"
590 " float far;\n"
591 " float diff;\n"
592 "};\n"
593 "\n";
594 }
595
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200596 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000597 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800598 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500599 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800600
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000601 if (mUsesDepthRange)
602 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800603 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000604 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800605
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800606 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
607 // shaders. However, we declare it for all shaders (including Feature Level 10+).
608 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
609 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800610 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800611 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800612 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800613
Olli Etuaho618bebc2016-01-15 16:40:00 +0200614 if (mOutputType == SH_HLSL_4_1_OUTPUT)
615 {
616 mUniformHLSL->samplerMetadataUniforms(out, "c4");
617 }
618
Austin Kinross4fd18b12014-12-22 12:32:05 -0800619 out << "};\n"
620 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000621 }
622 else
623 {
624 if (mUsesDepthRange)
625 {
626 out << "uniform float3 dx_DepthRange : register(c0);\n";
627 }
628
Cooper Partine6664f02015-01-09 16:22:24 -0800629 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
630 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000631 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000632 }
633
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000634 if (mUsesDepthRange)
635 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500636 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
637 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000638 "\n";
639 }
640
Jamie Madillf91ce812014-06-13 10:04:34 -0400641 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000642 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400643 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000644 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400645 out << flaggedStructs;
646 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000647 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400648 }
Xinghua Caob1239382016-12-13 15:07:05 +0800649 else // Compute shader
650 {
651 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800652
653 out << "cbuffer DriverConstants : register(b1)\n"
654 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800655 if (mUsesNumWorkGroups)
656 {
Xinghua Caob1239382016-12-13 15:07:05 +0800657 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800658 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800659 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
660 mUniformHLSL->samplerMetadataUniforms(out, "c1");
661 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800662
663 // Follow built-in variables would be initialized in
664 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
665 // are used in compute shader.
666 if (mUsesWorkGroupID)
667 {
668 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
669 }
670
671 if (mUsesLocalInvocationID)
672 {
673 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
674 }
675
676 if (mUsesGlobalInvocationID)
677 {
678 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
679 }
680
681 if (mUsesLocalInvocationIndex)
682 {
683 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
684 }
685 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000686
Geoff Lang1fe74c72016-08-25 13:23:01 -0400687 bool getDimensionsIgnoresBaseLevel =
688 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
689 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000691 if (mUsesFragCoord)
692 {
693 out << "#define GL_USES_FRAG_COORD\n";
694 }
695
696 if (mUsesPointCoord)
697 {
698 out << "#define GL_USES_POINT_COORD\n";
699 }
700
701 if (mUsesFrontFacing)
702 {
703 out << "#define GL_USES_FRONT_FACING\n";
704 }
705
706 if (mUsesPointSize)
707 {
708 out << "#define GL_USES_POINT_SIZE\n";
709 }
710
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400711 if (mUsesFragDepth)
712 {
713 out << "#define GL_USES_FRAG_DEPTH\n";
714 }
715
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000716 if (mUsesDepthRange)
717 {
718 out << "#define GL_USES_DEPTH_RANGE\n";
719 }
720
Xinghua Caob1239382016-12-13 15:07:05 +0800721 if (mUsesNumWorkGroups)
722 {
723 out << "#define GL_USES_NUM_WORK_GROUPS\n";
724 }
725
726 if (mUsesWorkGroupID)
727 {
728 out << "#define GL_USES_WORK_GROUP_ID\n";
729 }
730
731 if (mUsesLocalInvocationID)
732 {
733 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
734 }
735
736 if (mUsesGlobalInvocationID)
737 {
738 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
739 }
740
741 if (mUsesLocalInvocationIndex)
742 {
743 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
744 }
745
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000746 if (mUsesXor)
747 {
748 out << "bool xor(bool p, bool q)\n"
749 "{\n"
750 " return (p || q) && !(p && q);\n"
751 "}\n"
752 "\n";
753 }
754
Olli Etuahodfa75e82017-01-23 09:43:06 -0800755 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756}
757
758void OutputHLSL::visitSymbol(TIntermSymbol *node)
759{
Jamie Madill32aab012015-01-27 14:12:26 -0500760 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761
Jamie Madill570e04d2013-06-21 09:15:33 -0400762 // Handle accessing std140 structs by value
763 if (mFlaggedStructMappedNames.count(node) > 0)
764 {
765 out << mFlaggedStructMappedNames[node];
766 return;
767 }
768
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769 TString name = node->getSymbol();
770
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000771 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000772 {
773 mUsesDepthRange = true;
774 out << name;
775 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776 else
777 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000778 TQualifier qualifier = node->getQualifier();
779
780 if (qualifier == EvqUniform)
781 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500782 const TType &nodeType = node->getType();
Jamie Madill2e295e22015-04-29 10:41:33 -0400783 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400784
785 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000786 {
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800787 mReferencedUniformBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000788 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000789 else
790 {
791 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000792 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400793
Jamie Madill2e295e22015-04-29 10:41:33 -0400794 ensureStructDefined(nodeType);
795
Olli Etuahoff526f12017-06-30 12:26:54 +0300796 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000797 }
Jamie Madill19571812013-08-12 15:26:34 -0700798 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000799 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000800 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400801 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000802 }
Jamie Madill033dae62014-06-18 12:56:28 -0400803 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000804 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000805 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400806 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000807 }
Jamie Madill19571812013-08-12 15:26:34 -0700808 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400809 {
810 mReferencedOutputVariables[name] = node;
811 out << "out_" << name;
812 }
813 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000814 {
815 out << "gl_Color[0]";
816 mUsesFragColor = true;
817 }
818 else if (qualifier == EvqFragData)
819 {
820 out << "gl_Color";
821 mUsesFragData = true;
822 }
823 else if (qualifier == EvqFragCoord)
824 {
825 mUsesFragCoord = true;
826 out << name;
827 }
828 else if (qualifier == EvqPointCoord)
829 {
830 mUsesPointCoord = true;
831 out << name;
832 }
833 else if (qualifier == EvqFrontFacing)
834 {
835 mUsesFrontFacing = true;
836 out << name;
837 }
838 else if (qualifier == EvqPointSize)
839 {
840 mUsesPointSize = true;
841 out << name;
842 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000843 else if (qualifier == EvqInstanceID)
844 {
845 mUsesInstanceID = true;
846 out << name;
847 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500848 else if (qualifier == EvqVertexID)
849 {
850 mUsesVertexID = true;
851 out << name;
852 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300853 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400854 {
855 mUsesFragDepth = true;
856 out << "gl_Depth";
857 }
Xinghua Caob1239382016-12-13 15:07:05 +0800858 else if (qualifier == EvqNumWorkGroups)
859 {
860 mUsesNumWorkGroups = true;
861 out << name;
862 }
863 else if (qualifier == EvqWorkGroupID)
864 {
865 mUsesWorkGroupID = true;
866 out << name;
867 }
868 else if (qualifier == EvqLocalInvocationID)
869 {
870 mUsesLocalInvocationID = true;
871 out << name;
872 }
873 else if (qualifier == EvqGlobalInvocationID)
874 {
875 mUsesGlobalInvocationID = true;
876 out << name;
877 }
878 else if (qualifier == EvqLocalInvocationIndex)
879 {
880 mUsesLocalInvocationIndex = true;
881 out << name;
882 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000883 else
884 {
Olli Etuahoff526f12017-06-30 12:26:54 +0300885 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000886 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887 }
888}
889
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400890void OutputHLSL::visitRaw(TIntermRaw *node)
891{
Jamie Madill32aab012015-01-27 14:12:26 -0500892 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400893}
894
Olli Etuaho7fb49552015-03-18 17:27:44 +0200895void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
896{
897 if (type.isScalar() && !type.isArray())
898 {
899 if (op == EOpEqual)
900 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500901 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200902 }
903 else
904 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500905 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200906 }
907 }
908 else
909 {
910 if (visit == PreVisit && op == EOpNotEqual)
911 {
912 out << "!";
913 }
914
915 if (type.isArray())
916 {
917 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500918 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200919 }
920 else if (type.getBasicType() == EbtStruct)
921 {
922 const TStructure &structure = *type.getStruct();
923 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500924 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200925 }
926 else
927 {
928 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500929 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200930 }
931 }
932}
933
Olli Etuaho1d9dcc22017-01-19 11:25:32 +0000934bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +0200935{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +0000936 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +0200937 {
938 TIntermNode *ancestor = getAncestorNode(n);
939 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
940 if (ancestorBinary == nullptr)
941 {
942 return false;
943 }
944 switch (ancestorBinary->getOp())
945 {
946 case EOpIndexDirectStruct:
947 {
948 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
949 const TIntermConstantUnion *index =
950 ancestorBinary->getRight()->getAsConstantUnion();
951 const TField *field = structure->fields()[index->getIConst(0)];
952 if (IsSampler(field->type()->getBasicType()))
953 {
954 return true;
955 }
956 break;
957 }
958 case EOpIndexDirect:
959 break;
960 default:
961 // Returning a sampler from indirect indexing is not supported.
962 return false;
963 }
964 }
965 return false;
966}
967
Olli Etuahob6fa0432016-09-28 16:28:05 +0100968bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
969{
970 TInfoSinkBase &out = getInfoSink();
971 if (visit == PostVisit)
972 {
973 out << ".";
974 node->writeOffsetsAsXYZW(&out);
975 }
976 return true;
977}
978
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
980{
Jamie Madill32aab012015-01-27 14:12:26 -0500981 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982
Jamie Madill570e04d2013-06-21 09:15:33 -0400983 // Handle accessing std140 structs by value
984 if (mFlaggedStructMappedNames.count(node) > 0)
985 {
986 out << mFlaggedStructMappedNames[node];
987 return false;
988 }
989
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000990 switch (node->getOp())
991 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100992 case EOpComma:
993 outputTriplet(out, visit, "(", ", ", ")");
994 break;
995 case EOpAssign:
996 if (node->getLeft()->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +0300997 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +0100998 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
999 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001000 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001001 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1002 out << functionName << "(";
1003 node->getLeft()->traverse(this);
1004 TIntermSequence *seq = rightAgg->getSequence();
1005 for (auto &arrayElement : *seq)
1006 {
1007 out << ", ";
1008 arrayElement->traverse(this);
1009 }
1010 out << ")";
1011 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001012 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001013 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1014 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001015 ASSERT(rightAgg == nullptr);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001016
1017 const TString &functionName = addArrayAssignmentFunction(node->getType());
1018 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho9638c352015-04-01 14:34:52 +03001019 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001020 else
Jamie Madill37997142015-01-28 10:06:34 -05001021 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001022 outputTriplet(out, visit, "(", " = ", ")");
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001023 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001024 break;
1025 case EOpInitialize:
1026 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001027 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001028 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1029 ASSERT(symbolNode);
1030 TIntermTyped *expression = node->getRight();
1031
1032 // Global initializers must be constant at this point.
1033 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
1034 canWriteAsHLSLLiteral(expression));
1035
1036 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1037 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1038 // new variable is created before the assignment is evaluated), so we need to
1039 // convert
1040 // this to "float t = x, x = t;".
1041 if (writeSameSymbolInitializer(out, symbolNode, expression))
1042 {
1043 // Skip initializing the rest of the expression
1044 return false;
1045 }
1046 else if (writeConstantInitialization(out, symbolNode, expression))
1047 {
1048 return false;
1049 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001050 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001051 else if (visit == InVisit)
1052 {
1053 out << " = ";
1054 }
1055 break;
1056 case EOpAddAssign:
1057 outputTriplet(out, visit, "(", " += ", ")");
1058 break;
1059 case EOpSubAssign:
1060 outputTriplet(out, visit, "(", " -= ", ")");
1061 break;
1062 case EOpMulAssign:
1063 outputTriplet(out, visit, "(", " *= ", ")");
1064 break;
1065 case EOpVectorTimesScalarAssign:
1066 outputTriplet(out, visit, "(", " *= ", ")");
1067 break;
1068 case EOpMatrixTimesScalarAssign:
1069 outputTriplet(out, visit, "(", " *= ", ")");
1070 break;
1071 case EOpVectorTimesMatrixAssign:
1072 if (visit == PreVisit)
1073 {
1074 out << "(";
1075 }
1076 else if (visit == InVisit)
1077 {
1078 out << " = mul(";
1079 node->getLeft()->traverse(this);
1080 out << ", transpose(";
1081 }
1082 else
1083 {
1084 out << ")))";
1085 }
1086 break;
1087 case EOpMatrixTimesMatrixAssign:
1088 if (visit == PreVisit)
1089 {
1090 out << "(";
1091 }
1092 else if (visit == InVisit)
1093 {
1094 out << " = transpose(mul(transpose(";
1095 node->getLeft()->traverse(this);
1096 out << "), transpose(";
1097 }
1098 else
1099 {
1100 out << "))))";
1101 }
1102 break;
1103 case EOpDivAssign:
1104 outputTriplet(out, visit, "(", " /= ", ")");
1105 break;
1106 case EOpIModAssign:
1107 outputTriplet(out, visit, "(", " %= ", ")");
1108 break;
1109 case EOpBitShiftLeftAssign:
1110 outputTriplet(out, visit, "(", " <<= ", ")");
1111 break;
1112 case EOpBitShiftRightAssign:
1113 outputTriplet(out, visit, "(", " >>= ", ")");
1114 break;
1115 case EOpBitwiseAndAssign:
1116 outputTriplet(out, visit, "(", " &= ", ")");
1117 break;
1118 case EOpBitwiseXorAssign:
1119 outputTriplet(out, visit, "(", " ^= ", ")");
1120 break;
1121 case EOpBitwiseOrAssign:
1122 outputTriplet(out, visit, "(", " |= ", ")");
1123 break;
1124 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001125 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001126 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001127 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001128 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001129 if (visit == PreVisit)
1130 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001131 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001132 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001133 mReferencedUniformBlocks[interfaceBlock->instanceName()] =
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001134 node->getLeft()->getAsSymbolNode();
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001135 out << mUniformHLSL->uniformBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001136 return false;
1137 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001138 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001139 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001140 {
1141 // All parts of an expression that access a sampler in a struct need to use _ as
1142 // separator to access the sampler variable that has been moved out of the struct.
1143 outputTriplet(out, visit, "", "_", "");
1144 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001145 else
1146 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001147 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001148 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001149 }
1150 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001151 case EOpIndexIndirect:
1152 // We do not currently support indirect references to interface blocks
1153 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1154 outputTriplet(out, visit, "", "[", "]");
1155 break;
1156 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001157 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001158 const TStructure *structure = node->getLeft()->getType().getStruct();
1159 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1160 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001161
Olli Etuaho96963162016-03-21 11:54:33 +02001162 // In cases where indexing returns a sampler, we need to access the sampler variable
1163 // that has been moved out of the struct.
1164 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1165 if (visit == PreVisit && indexingReturnsSampler)
1166 {
1167 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1168 // This prefix is only output at the beginning of the indexing expression, which
1169 // may have multiple parts.
1170 out << "angle";
1171 }
1172 if (!indexingReturnsSampler)
1173 {
1174 // All parts of an expression that access a sampler in a struct need to use _ as
1175 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001176 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001177 }
1178 if (visit == InVisit)
1179 {
1180 if (indexingReturnsSampler)
1181 {
1182 out << "_" + field->name();
1183 }
1184 else
1185 {
1186 out << "." + DecorateField(field->name(), *structure);
1187 }
1188
1189 return false;
1190 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001191 }
1192 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001193 case EOpIndexDirectInterfaceBlock:
1194 if (visit == InVisit)
1195 {
1196 const TInterfaceBlock *interfaceBlock =
1197 node->getLeft()->getType().getInterfaceBlock();
1198 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1199 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
1200 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001201
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001202 return false;
1203 }
1204 break;
1205 case EOpAdd:
1206 outputTriplet(out, visit, "(", " + ", ")");
1207 break;
1208 case EOpSub:
1209 outputTriplet(out, visit, "(", " - ", ")");
1210 break;
1211 case EOpMul:
1212 outputTriplet(out, visit, "(", " * ", ")");
1213 break;
1214 case EOpDiv:
1215 outputTriplet(out, visit, "(", " / ", ")");
1216 break;
1217 case EOpIMod:
1218 outputTriplet(out, visit, "(", " % ", ")");
1219 break;
1220 case EOpBitShiftLeft:
1221 outputTriplet(out, visit, "(", " << ", ")");
1222 break;
1223 case EOpBitShiftRight:
1224 outputTriplet(out, visit, "(", " >> ", ")");
1225 break;
1226 case EOpBitwiseAnd:
1227 outputTriplet(out, visit, "(", " & ", ")");
1228 break;
1229 case EOpBitwiseXor:
1230 outputTriplet(out, visit, "(", " ^ ", ")");
1231 break;
1232 case EOpBitwiseOr:
1233 outputTriplet(out, visit, "(", " | ", ")");
1234 break;
1235 case EOpEqual:
1236 case EOpNotEqual:
1237 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1238 break;
1239 case EOpLessThan:
1240 outputTriplet(out, visit, "(", " < ", ")");
1241 break;
1242 case EOpGreaterThan:
1243 outputTriplet(out, visit, "(", " > ", ")");
1244 break;
1245 case EOpLessThanEqual:
1246 outputTriplet(out, visit, "(", " <= ", ")");
1247 break;
1248 case EOpGreaterThanEqual:
1249 outputTriplet(out, visit, "(", " >= ", ")");
1250 break;
1251 case EOpVectorTimesScalar:
1252 outputTriplet(out, visit, "(", " * ", ")");
1253 break;
1254 case EOpMatrixTimesScalar:
1255 outputTriplet(out, visit, "(", " * ", ")");
1256 break;
1257 case EOpVectorTimesMatrix:
1258 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1259 break;
1260 case EOpMatrixTimesVector:
1261 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1262 break;
1263 case EOpMatrixTimesMatrix:
1264 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1265 break;
1266 case EOpLogicalOr:
1267 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1268 // been unfolded.
1269 ASSERT(!node->getRight()->hasSideEffects());
1270 outputTriplet(out, visit, "(", " || ", ")");
1271 return true;
1272 case EOpLogicalXor:
1273 mUsesXor = true;
1274 outputTriplet(out, visit, "xor(", ", ", ")");
1275 break;
1276 case EOpLogicalAnd:
1277 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1278 // been unfolded.
1279 ASSERT(!node->getRight()->hasSideEffects());
1280 outputTriplet(out, visit, "(", " && ", ")");
1281 return true;
1282 default:
1283 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001284 }
1285
1286 return true;
1287}
1288
1289bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1290{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001291 TInfoSinkBase &out = getInfoSink();
1292
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001293 switch (node->getOp())
1294 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001295 case EOpNegative:
1296 outputTriplet(out, visit, "(-", "", ")");
1297 break;
1298 case EOpPositive:
1299 outputTriplet(out, visit, "(+", "", ")");
1300 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001301 case EOpLogicalNot:
1302 outputTriplet(out, visit, "(!", "", ")");
1303 break;
1304 case EOpBitwiseNot:
1305 outputTriplet(out, visit, "(~", "", ")");
1306 break;
1307 case EOpPostIncrement:
1308 outputTriplet(out, visit, "(", "", "++)");
1309 break;
1310 case EOpPostDecrement:
1311 outputTriplet(out, visit, "(", "", "--)");
1312 break;
1313 case EOpPreIncrement:
1314 outputTriplet(out, visit, "(++", "", ")");
1315 break;
1316 case EOpPreDecrement:
1317 outputTriplet(out, visit, "(--", "", ")");
1318 break;
1319 case EOpRadians:
1320 outputTriplet(out, visit, "radians(", "", ")");
1321 break;
1322 case EOpDegrees:
1323 outputTriplet(out, visit, "degrees(", "", ")");
1324 break;
1325 case EOpSin:
1326 outputTriplet(out, visit, "sin(", "", ")");
1327 break;
1328 case EOpCos:
1329 outputTriplet(out, visit, "cos(", "", ")");
1330 break;
1331 case EOpTan:
1332 outputTriplet(out, visit, "tan(", "", ")");
1333 break;
1334 case EOpAsin:
1335 outputTriplet(out, visit, "asin(", "", ")");
1336 break;
1337 case EOpAcos:
1338 outputTriplet(out, visit, "acos(", "", ")");
1339 break;
1340 case EOpAtan:
1341 outputTriplet(out, visit, "atan(", "", ")");
1342 break;
1343 case EOpSinh:
1344 outputTriplet(out, visit, "sinh(", "", ")");
1345 break;
1346 case EOpCosh:
1347 outputTriplet(out, visit, "cosh(", "", ")");
1348 break;
1349 case EOpTanh:
1350 outputTriplet(out, visit, "tanh(", "", ")");
1351 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001352 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001353 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001354 case EOpAtanh:
1355 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001356 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001357 break;
1358 case EOpExp:
1359 outputTriplet(out, visit, "exp(", "", ")");
1360 break;
1361 case EOpLog:
1362 outputTriplet(out, visit, "log(", "", ")");
1363 break;
1364 case EOpExp2:
1365 outputTriplet(out, visit, "exp2(", "", ")");
1366 break;
1367 case EOpLog2:
1368 outputTriplet(out, visit, "log2(", "", ")");
1369 break;
1370 case EOpSqrt:
1371 outputTriplet(out, visit, "sqrt(", "", ")");
1372 break;
1373 case EOpInverseSqrt:
1374 outputTriplet(out, visit, "rsqrt(", "", ")");
1375 break;
1376 case EOpAbs:
1377 outputTriplet(out, visit, "abs(", "", ")");
1378 break;
1379 case EOpSign:
1380 outputTriplet(out, visit, "sign(", "", ")");
1381 break;
1382 case EOpFloor:
1383 outputTriplet(out, visit, "floor(", "", ")");
1384 break;
1385 case EOpTrunc:
1386 outputTriplet(out, visit, "trunc(", "", ")");
1387 break;
1388 case EOpRound:
1389 outputTriplet(out, visit, "round(", "", ")");
1390 break;
1391 case EOpRoundEven:
1392 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001393 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001394 break;
1395 case EOpCeil:
1396 outputTriplet(out, visit, "ceil(", "", ")");
1397 break;
1398 case EOpFract:
1399 outputTriplet(out, visit, "frac(", "", ")");
1400 break;
1401 case EOpIsNan:
1402 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001403 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001404 else
1405 outputTriplet(out, visit, "isnan(", "", ")");
1406 mRequiresIEEEStrictCompiling = true;
1407 break;
1408 case EOpIsInf:
1409 outputTriplet(out, visit, "isinf(", "", ")");
1410 break;
1411 case EOpFloatBitsToInt:
1412 outputTriplet(out, visit, "asint(", "", ")");
1413 break;
1414 case EOpFloatBitsToUint:
1415 outputTriplet(out, visit, "asuint(", "", ")");
1416 break;
1417 case EOpIntBitsToFloat:
1418 outputTriplet(out, visit, "asfloat(", "", ")");
1419 break;
1420 case EOpUintBitsToFloat:
1421 outputTriplet(out, visit, "asfloat(", "", ")");
1422 break;
1423 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001424 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001425 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001426 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001427 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001428 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001429 case EOpPackUnorm4x8:
1430 case EOpPackSnorm4x8:
1431 case EOpUnpackUnorm4x8:
1432 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001433 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001434 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001435 break;
1436 case EOpLength:
1437 outputTriplet(out, visit, "length(", "", ")");
1438 break;
1439 case EOpNormalize:
1440 outputTriplet(out, visit, "normalize(", "", ")");
1441 break;
1442 case EOpDFdx:
1443 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1444 {
1445 outputTriplet(out, visit, "(", "", ", 0.0)");
1446 }
1447 else
1448 {
1449 outputTriplet(out, visit, "ddx(", "", ")");
1450 }
1451 break;
1452 case EOpDFdy:
1453 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1454 {
1455 outputTriplet(out, visit, "(", "", ", 0.0)");
1456 }
1457 else
1458 {
1459 outputTriplet(out, visit, "ddy(", "", ")");
1460 }
1461 break;
1462 case EOpFwidth:
1463 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1464 {
1465 outputTriplet(out, visit, "(", "", ", 0.0)");
1466 }
1467 else
1468 {
1469 outputTriplet(out, visit, "fwidth(", "", ")");
1470 }
1471 break;
1472 case EOpTranspose:
1473 outputTriplet(out, visit, "transpose(", "", ")");
1474 break;
1475 case EOpDeterminant:
1476 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1477 break;
1478 case EOpInverse:
1479 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001480 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001481 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001482
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001483 case EOpAny:
1484 outputTriplet(out, visit, "any(", "", ")");
1485 break;
1486 case EOpAll:
1487 outputTriplet(out, visit, "all(", "", ")");
1488 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001489 case EOpLogicalNotComponentWise:
1490 outputTriplet(out, visit, "(!", "", ")");
1491 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001492 case EOpBitfieldReverse:
1493 outputTriplet(out, visit, "reversebits(", "", ")");
1494 break;
1495 case EOpBitCount:
1496 outputTriplet(out, visit, "countbits(", "", ")");
1497 break;
1498 case EOpFindLSB:
1499 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1500 // in GLSLTest and results are consistent with GL.
1501 outputTriplet(out, visit, "firstbitlow(", "", ")");
1502 break;
1503 case EOpFindMSB:
1504 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1505 // tested in GLSLTest and results are consistent with GL.
1506 outputTriplet(out, visit, "firstbithigh(", "", ")");
1507 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001508 default:
1509 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001510 }
1511
1512 return true;
1513}
1514
Olli Etuaho96963162016-03-21 11:54:33 +02001515TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1516{
1517 if (node->getAsSymbolNode())
1518 {
1519 return node->getAsSymbolNode()->getSymbol();
1520 }
1521 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1522 switch (nodeBinary->getOp())
1523 {
1524 case EOpIndexDirect:
1525 {
1526 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1527
1528 TInfoSinkBase prefixSink;
1529 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1530 return TString(prefixSink.c_str());
1531 }
1532 case EOpIndexDirectStruct:
1533 {
1534 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1535 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1536 const TField *field = s->fields()[index];
1537
1538 TInfoSinkBase prefixSink;
1539 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1540 << field->name();
1541 return TString(prefixSink.c_str());
1542 }
1543 default:
1544 UNREACHABLE();
1545 return TString("");
1546 }
1547}
1548
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001549bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1550{
1551 TInfoSinkBase &out = getInfoSink();
1552
1553 if (mInsideFunction)
1554 {
1555 outputLineDirective(out, node->getLine().first_line);
1556 out << "{\n";
1557 }
1558
1559 for (TIntermSequence::iterator sit = node->getSequence()->begin();
1560 sit != node->getSequence()->end(); sit++)
1561 {
1562 outputLineDirective(out, (*sit)->getLine().first_line);
1563
1564 (*sit)->traverse(this);
1565
1566 // Don't output ; after case labels, they're terminated by :
1567 // This is needed especially since outputting a ; after a case statement would turn empty
1568 // case statements into non-empty case statements, disallowing fall-through from them.
1569 // Also no need to output ; after if statements or sequences. This is done just for
1570 // code clarity.
1571 if ((*sit)->getAsCaseNode() == nullptr && (*sit)->getAsIfElseNode() == nullptr &&
1572 (*sit)->getAsBlock() == nullptr)
1573 out << ";\n";
1574 }
1575
1576 if (mInsideFunction)
1577 {
1578 outputLineDirective(out, node->getLine().last_line);
1579 out << "}\n";
1580 }
1581
1582 return false;
1583}
1584
Olli Etuaho336b1472016-10-05 16:37:55 +01001585bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1586{
1587 TInfoSinkBase &out = getInfoSink();
1588
1589 ASSERT(mCurrentFunctionMetadata == nullptr);
1590
1591 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1592 ASSERT(index != CallDAG::InvalidIndex);
1593 mCurrentFunctionMetadata = &mASTMetadataList[index];
1594
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001595 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001596
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001597 TIntermSequence *parameters = node->getFunctionPrototype()->getSequence();
Olli Etuaho336b1472016-10-05 16:37:55 +01001598
1599 if (node->getFunctionSymbolInfo()->isMain())
1600 {
1601 out << "gl_main(";
1602 }
1603 else
1604 {
Olli Etuahoff526f12017-06-30 12:26:54 +03001605 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
Olli Etuaho336b1472016-10-05 16:37:55 +01001606 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1607 }
1608
1609 for (unsigned int i = 0; i < parameters->size(); i++)
1610 {
1611 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1612
1613 if (symbol)
1614 {
1615 ensureStructDefined(symbol->getType());
1616
1617 out << argumentString(symbol);
1618
1619 if (i < parameters->size() - 1)
1620 {
1621 out << ", ";
1622 }
1623 }
1624 else
1625 UNREACHABLE();
1626 }
1627
1628 out << ")\n";
1629
1630 mInsideFunction = true;
1631 // The function body node will output braces.
1632 node->getBody()->traverse(this);
1633 mInsideFunction = false;
1634
1635 mCurrentFunctionMetadata = nullptr;
1636
1637 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1638 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1639 {
1640 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1641 mOutputLod0Function = true;
1642 node->traverse(this);
1643 mOutputLod0Function = false;
1644 }
1645
1646 return false;
1647}
1648
Olli Etuaho13389b62016-10-16 11:48:18 +01001649bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1650{
1651 TInfoSinkBase &out = getInfoSink();
1652 if (visit == PreVisit)
1653 {
1654 TIntermSequence *sequence = node->getSequence();
1655 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1656 ASSERT(sequence->size() == 1);
Olli Etuaho282847e2017-07-12 14:11:01 +03001657 ASSERT(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01001658
Olli Etuaho282847e2017-07-12 14:11:01 +03001659 if ((variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
Olli Etuaho13389b62016-10-16 11:48:18 +01001660 variable->getQualifier() == EvqConst))
1661 {
1662 ensureStructDefined(variable->getType());
1663
1664 if (!variable->getAsSymbolNode() ||
1665 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1666 {
1667 if (!mInsideFunction)
1668 {
1669 out << "static ";
1670 }
1671
1672 out << TypeString(variable->getType()) + " ";
1673
1674 TIntermSymbol *symbol = variable->getAsSymbolNode();
1675
1676 if (symbol)
1677 {
1678 symbol->traverse(this);
1679 out << ArrayString(symbol->getType());
1680 out << " = " + initializer(symbol->getType());
1681 }
1682 else
1683 {
1684 variable->traverse(this);
1685 }
1686 }
1687 else if (variable->getAsSymbolNode() &&
1688 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1689 {
1690 // Already added to constructor map
1691 }
1692 else
1693 UNREACHABLE();
1694 }
Olli Etuaho282847e2017-07-12 14:11:01 +03001695 else if (IsVaryingOut(variable->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001696 {
Olli Etuaho282847e2017-07-12 14:11:01 +03001697 TIntermSymbol *symbol = variable->getAsSymbolNode();
1698 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001699
Olli Etuaho282847e2017-07-12 14:11:01 +03001700 // Vertex outputs which are declared but not written to should still be declared to
1701 // allow successful linking.
1702 mReferencedVaryings[symbol->getSymbol()] = symbol;
Olli Etuaho13389b62016-10-16 11:48:18 +01001703 }
1704 }
1705 return false;
1706}
1707
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001708bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1709{
1710 // Do not do any translation
1711 return false;
1712}
1713
Olli Etuaho16c745a2017-01-16 17:02:27 +00001714bool OutputHLSL::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
1715{
1716 TInfoSinkBase &out = getInfoSink();
1717
1718 ASSERT(visit == PreVisit);
1719 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1720 // Skip the prototype if it is not implemented (and thus not used)
1721 if (index == CallDAG::InvalidIndex)
1722 {
1723 return false;
1724 }
1725
1726 TIntermSequence *arguments = node->getSequence();
1727
Olli Etuahoff526f12017-06-30 12:26:54 +03001728 TString name = DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001729 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1730 << (mOutputLod0Function ? "Lod0(" : "(");
1731
1732 for (unsigned int i = 0; i < arguments->size(); i++)
1733 {
1734 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1735 ASSERT(symbol != nullptr);
1736
1737 out << argumentString(symbol);
1738
1739 if (i < arguments->size() - 1)
1740 {
1741 out << ", ";
1742 }
1743 }
1744
1745 out << ");\n";
1746
1747 // Also prototype the Lod0 variant if needed
1748 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1749 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1750 {
1751 mOutputLod0Function = true;
1752 node->traverse(this);
1753 mOutputLod0Function = false;
1754 }
1755
1756 return false;
1757}
1758
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001759bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1760{
Jamie Madill32aab012015-01-27 14:12:26 -05001761 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 switch (node->getOp())
1764 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001765 case EOpCallBuiltInFunction:
1766 case EOpCallFunctionInAST:
1767 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001768 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001769 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001770
Corentin Wallez1239ee92015-03-19 14:38:02 -07001771 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001772 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001774 if (node->isArray())
1775 {
1776 UNIMPLEMENTED();
1777 }
Olli Etuahobd674552016-10-06 13:28:42 +01001778 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001779 ASSERT(index != CallDAG::InvalidIndex);
1780 lod0 &= mASTMetadataList[index].mNeedsLod0;
1781
Olli Etuahoff526f12017-06-30 12:26:54 +03001782 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001783 out << DisambiguateFunctionName(node->getSequence());
1784 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001785 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001786 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001787 {
1788 // This path is used for internal functions that don't have their definitions in the
1789 // AST, such as precision emulation functions.
Olli Etuahoff526f12017-06-30 12:26:54 +03001790 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001791 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792 else
1793 {
Olli Etuahoec9232b2017-03-27 17:01:37 +03001794 const TString &name = node->getFunctionSymbolInfo()->getName();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001795 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001796 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1797 if (arguments->size() > 1)
1798 {
1799 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1800 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001801 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1802 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1803 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001804 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001805
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001806 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001807 {
Olli Etuaho96963162016-03-21 11:54:33 +02001808 TIntermTyped *typedArg = (*arg)->getAsTyped();
1809 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001810 {
1811 out << "texture_";
1812 (*arg)->traverse(this);
1813 out << ", sampler_";
1814 }
1815
1816 (*arg)->traverse(this);
1817
Olli Etuaho96963162016-03-21 11:54:33 +02001818 if (typedArg->getType().isStructureContainingSamplers())
1819 {
1820 const TType &argType = typedArg->getType();
1821 TVector<TIntermSymbol *> samplerSymbols;
1822 TString structName = samplerNamePrefixFromStruct(typedArg);
1823 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho96963162016-03-21 11:54:33 +02001824 &samplerSymbols, nullptr);
1825 for (const TIntermSymbol *sampler : samplerSymbols)
1826 {
1827 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1828 {
1829 out << ", texture_" << sampler->getSymbol();
1830 out << ", sampler_" << sampler->getSymbol();
1831 }
1832 else
1833 {
1834 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1835 // of D3D9, it's the sampler variable.
1836 out << ", " + sampler->getSymbol();
1837 }
1838 }
1839 }
1840
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001841 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001842 {
1843 out << ", ";
1844 }
1845 }
1846
1847 out << ")";
1848
1849 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001850 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03001851 case EOpConstruct:
1852 if (node->getBasicType() == EbtStruct)
Olli Etuahof40319e2015-03-10 14:33:00 +02001853 {
Olli Etuaho8fab3202017-05-08 18:22:22 +03001854 if (node->getType().isArray())
1855 {
1856 UNIMPLEMENTED();
1857 }
1858 const TString &structName = StructNameString(*node->getType().getStruct());
1859 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
1860 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Olli Etuahof40319e2015-03-10 14:33:00 +02001861 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03001862 else
1863 {
1864 const char *name = "";
1865 if (node->getType().getNominalSize() == 1)
1866 {
1867 switch (node->getBasicType())
1868 {
1869 case EbtFloat:
1870 name = "vec1";
1871 break;
1872 case EbtInt:
1873 name = "ivec1";
1874 break;
1875 case EbtUInt:
1876 name = "uvec1";
1877 break;
1878 case EbtBool:
1879 name = "bvec1";
1880 break;
1881 default:
1882 UNREACHABLE();
1883 }
1884 }
1885 else
1886 {
1887 name = node->getType().getBuiltInTypeNameString();
1888 }
1889 outputConstructor(out, visit, node->getType(), name, node->getSequence());
1890 }
1891 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001892 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001893 outputTriplet(out, visit, "(", " == ", ")");
1894 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001895 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001896 outputTriplet(out, visit, "(", " != ", ")");
1897 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001898 case EOpLessThanComponentWise:
1899 outputTriplet(out, visit, "(", " < ", ")");
1900 break;
1901 case EOpGreaterThanComponentWise:
1902 outputTriplet(out, visit, "(", " > ", ")");
1903 break;
1904 case EOpLessThanEqualComponentWise:
1905 outputTriplet(out, visit, "(", " <= ", ")");
1906 break;
1907 case EOpGreaterThanEqualComponentWise:
1908 outputTriplet(out, visit, "(", " >= ", ")");
1909 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001910 case EOpMod:
1911 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001912 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001913 break;
1914 case EOpModf:
1915 outputTriplet(out, visit, "modf(", ", ", ")");
1916 break;
1917 case EOpPow:
1918 outputTriplet(out, visit, "pow(", ", ", ")");
1919 break;
1920 case EOpAtan:
1921 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1922 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001923 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001924 break;
1925 case EOpMin:
1926 outputTriplet(out, visit, "min(", ", ", ")");
1927 break;
1928 case EOpMax:
1929 outputTriplet(out, visit, "max(", ", ", ")");
1930 break;
1931 case EOpClamp:
1932 outputTriplet(out, visit, "clamp(", ", ", ")");
1933 break;
1934 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05301935 {
1936 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
1937 if (lastParamNode->getType().getBasicType() == EbtBool)
1938 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001939 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
1940 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05301941 // so use emulated version.
1942 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001943 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05301944 }
1945 else
1946 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001947 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05301948 }
Olli Etuaho5878f832016-10-07 10:14:58 +01001949 break;
Arun Patoled94f6642015-05-18 16:25:12 +05301950 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05001951 case EOpStep:
1952 outputTriplet(out, visit, "step(", ", ", ")");
1953 break;
1954 case EOpSmoothStep:
1955 outputTriplet(out, visit, "smoothstep(", ", ", ")");
1956 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00001957 case EOpFrexp:
1958 case EOpLdexp:
1959 ASSERT(node->getUseEmulatedFunction());
1960 writeEmulatedFunctionTriplet(out, visit, node->getOp());
1961 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001962 case EOpDistance:
1963 outputTriplet(out, visit, "distance(", ", ", ")");
1964 break;
1965 case EOpDot:
1966 outputTriplet(out, visit, "dot(", ", ", ")");
1967 break;
1968 case EOpCross:
1969 outputTriplet(out, visit, "cross(", ", ", ")");
1970 break;
Jamie Madille72595b2017-06-06 15:12:26 -04001971 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01001972 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001973 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001974 break;
1975 case EOpReflect:
1976 outputTriplet(out, visit, "reflect(", ", ", ")");
1977 break;
1978 case EOpRefract:
1979 outputTriplet(out, visit, "refract(", ", ", ")");
1980 break;
1981 case EOpOuterProduct:
1982 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001983 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001984 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001985 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01001986 outputTriplet(out, visit, "(", " * ", ")");
1987 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001988 case EOpBitfieldExtract:
1989 case EOpBitfieldInsert:
1990 case EOpUaddCarry:
1991 case EOpUsubBorrow:
1992 case EOpUmulExtended:
1993 case EOpImulExtended:
1994 ASSERT(node->getUseEmulatedFunction());
1995 writeEmulatedFunctionTriplet(out, visit, node->getOp());
1996 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001997 default:
1998 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 }
2000
2001 return true;
2002}
2003
Olli Etuaho57961272016-09-14 13:57:46 +03002004void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002006 out << "if (";
2007
2008 node->getCondition()->traverse(this);
2009
2010 out << ")\n";
2011
Jamie Madill8c46ab12015-12-07 16:39:19 -05002012 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002013
2014 bool discard = false;
2015
2016 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002017 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002018 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002019 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002020
Olli Etuahoa6f22092015-05-08 18:31:10 +03002021 // Detect true discard
2022 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2023 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002024 else
2025 {
2026 // TODO(oetuaho): Check if the semicolon inside is necessary.
2027 // It's there as a result of conservative refactoring of the output.
2028 out << "{;}\n";
2029 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002030
Jamie Madill8c46ab12015-12-07 16:39:19 -05002031 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002032
Olli Etuahoa6f22092015-05-08 18:31:10 +03002033 if (node->getFalseBlock())
2034 {
2035 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002036
Jamie Madill8c46ab12015-12-07 16:39:19 -05002037 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038
Olli Etuaho32db19b2016-10-04 14:43:16 +01002039 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002040 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002041
Jamie Madill8c46ab12015-12-07 16:39:19 -05002042 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002043
Olli Etuahoa6f22092015-05-08 18:31:10 +03002044 // Detect false discard
2045 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2046 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002047
Olli Etuahoa6f22092015-05-08 18:31:10 +03002048 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002049 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002050 {
2051 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002052 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002053}
2054
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002055bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2056{
2057 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2058 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2059 UNREACHABLE();
2060 return false;
2061}
2062
Olli Etuaho57961272016-09-14 13:57:46 +03002063bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002064{
2065 TInfoSinkBase &out = getInfoSink();
2066
Olli Etuaho3d932d82016-04-12 11:10:30 +03002067 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002068
2069 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002070 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002071 {
2072 out << "FLATTEN ";
2073 }
2074
Olli Etuaho57961272016-09-14 13:57:46 +03002075 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076
2077 return false;
2078}
2079
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002080bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002081{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002082 TInfoSinkBase &out = getInfoSink();
2083
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002084 if (node->getStatementList())
2085 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002086 node->setStatementList(
2087 RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002088 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002089 // The curly braces get written when visiting the statementList aggregate
2090 }
2091 else
2092 {
2093 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002094 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002095 }
2096 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002097}
2098
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002099bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002100{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002101 TInfoSinkBase &out = getInfoSink();
2102
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002103 if (node->hasCondition())
2104 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002105 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002106 return true;
2107 }
2108 else
2109 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002110 out << "default:\n";
2111 return false;
2112 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002113}
2114
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002115void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2116{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002117 TInfoSinkBase &out = getInfoSink();
2118 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119}
2120
2121bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2122{
Nicolas Capens655fe362014-04-11 13:12:34 -04002123 mNestedLoopDepth++;
2124
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002125 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002126 mInsideDiscontinuousLoop =
2127 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002128
Jamie Madill8c46ab12015-12-07 16:39:19 -05002129 TInfoSinkBase &out = getInfoSink();
2130
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002131 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002132 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002133 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002134 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002135 mInsideDiscontinuousLoop = wasDiscontinuous;
2136 mNestedLoopDepth--;
2137
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002138 return false;
2139 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002140 }
2141
Corentin Wallez1239ee92015-03-19 14:38:02 -07002142 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002143 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002145 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002146
Jamie Madill8c46ab12015-12-07 16:39:19 -05002147 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 }
2149 else
2150 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002151 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002152
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153 if (node->getInit())
2154 {
2155 node->getInit()->traverse(this);
2156 }
2157
2158 out << "; ";
2159
alokp@chromium.org52813552010-11-16 18:36:09 +00002160 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002162 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 }
2164
2165 out << "; ";
2166
alokp@chromium.org52813552010-11-16 18:36:09 +00002167 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002169 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170 }
2171
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002172 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002173
Jamie Madill8c46ab12015-12-07 16:39:19 -05002174 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 }
2176
2177 if (node->getBody())
2178 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002179 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002180 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002182 else
2183 {
2184 // TODO(oetuaho): Check if the semicolon inside is necessary.
2185 // It's there as a result of conservative refactoring of the output.
2186 out << "{;}\n";
2187 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188
Jamie Madill8c46ab12015-12-07 16:39:19 -05002189 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190
alokp@chromium.org52813552010-11-16 18:36:09 +00002191 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002193 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002194 out << "while(\n";
2195
alokp@chromium.org52813552010-11-16 18:36:09 +00002196 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002197
daniel@transgaming.com73536982012-03-21 20:45:49 +00002198 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002199 }
2200
daniel@transgaming.com73536982012-03-21 20:45:49 +00002201 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002202
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002203 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002204 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002205
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206 return false;
2207}
2208
2209bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2210{
Jamie Madill32aab012015-01-27 14:12:26 -05002211 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002212
2213 switch (node->getFlowOp())
2214 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002215 case EOpKill:
2216 outputTriplet(out, visit, "discard;\n", "", "");
2217 break;
2218 case EOpBreak:
2219 if (visit == PreVisit)
Nicolas Capens655fe362014-04-11 13:12:34 -04002220 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002221 if (mNestedLoopDepth > 1)
2222 {
2223 mUsesNestedBreak = true;
2224 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002225
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002226 if (mExcessiveLoopIndex)
2227 {
2228 out << "{Break";
2229 mExcessiveLoopIndex->traverse(this);
2230 out << " = true; break;}\n";
2231 }
2232 else
2233 {
2234 out << "break;\n";
2235 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002236 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002237 break;
2238 case EOpContinue:
2239 outputTriplet(out, visit, "continue;\n", "", "");
2240 break;
2241 case EOpReturn:
2242 if (visit == PreVisit)
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002243 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002244 if (node->getExpression())
2245 {
2246 out << "return ";
2247 }
2248 else
2249 {
2250 out << "return;\n";
2251 }
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002252 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002253 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002255 if (node->getExpression())
2256 {
2257 out << ";\n";
2258 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002260 break;
2261 default:
2262 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 }
2264
2265 return true;
2266}
2267
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002268// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002269// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2270// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002271bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002272{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002273 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002274
2275 // Parse loops of the form:
2276 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002277 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002278 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002279 int initial = 0;
2280 int limit = 0;
2281 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002282
2283 // Parse index name and intial value
2284 if (node->getInit())
2285 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002286 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002287
2288 if (init)
2289 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002290 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002291 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002292
2293 if (variable && variable->getQualifier() == EvqTemporary)
2294 {
2295 TIntermBinary *assign = variable->getAsBinaryNode();
2296
2297 if (assign->getOp() == EOpInitialize)
2298 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002299 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002300 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2301
2302 if (symbol && constant)
2303 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002304 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002305 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002306 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002307 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002308 }
2309 }
2310 }
2311 }
2312 }
2313 }
2314
2315 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002316 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002317 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002318 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002319
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002320 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2321 {
2322 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2323
2324 if (constant)
2325 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002326 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002327 {
2328 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002329 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002330 }
2331 }
2332 }
2333 }
2334
2335 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002336 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002337 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002338 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002339 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002340
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002341 if (binaryTerminal)
2342 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002343 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002344 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2345
2346 if (constant)
2347 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002348 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002349 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002350 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002351
2352 switch (op)
2353 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002354 case EOpAddAssign:
2355 increment = value;
2356 break;
2357 case EOpSubAssign:
2358 increment = -value;
2359 break;
2360 default:
2361 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002362 }
2363 }
2364 }
2365 }
2366 else if (unaryTerminal)
2367 {
2368 TOperator op = unaryTerminal->getOp();
2369
2370 switch (op)
2371 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002372 case EOpPostIncrement:
2373 increment = 1;
2374 break;
2375 case EOpPostDecrement:
2376 increment = -1;
2377 break;
2378 case EOpPreIncrement:
2379 increment = 1;
2380 break;
2381 case EOpPreDecrement:
2382 increment = -1;
2383 break;
2384 default:
2385 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002386 }
2387 }
2388 }
2389
Yunchao He4f285442017-04-21 12:15:49 +08002390 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002391 {
2392 if (comparator == EOpLessThanEqual)
2393 {
2394 comparator = EOpLessThan;
2395 limit += 1;
2396 }
2397
2398 if (comparator == EOpLessThan)
2399 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002400 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002402 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002403 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002404 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002405 }
2406
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002407 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002408 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002409
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002410 out << "{int ";
2411 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002412 out << ";\n"
2413 "bool Break";
2414 index->traverse(this);
2415 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002416
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002417 bool firstLoopFragment = true;
2418
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002419 while (iterations > 0)
2420 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002421 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002422
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002423 if (!firstLoopFragment)
2424 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002425 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002426 index->traverse(this);
2427 out << ") {\n";
2428 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002429
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002430 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002431 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002432 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002433 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002434
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002435 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002436 const char *unroll =
2437 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002438
Corentin Wallez1239ee92015-03-19 14:38:02 -07002439 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002440 index->traverse(this);
2441 out << " = ";
2442 out << initial;
2443
2444 out << "; ";
2445 index->traverse(this);
2446 out << " < ";
2447 out << clampedLimit;
2448
2449 out << "; ";
2450 index->traverse(this);
2451 out << " += ";
2452 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002453 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002454
Jamie Madill8c46ab12015-12-07 16:39:19 -05002455 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002456 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002457
2458 if (node->getBody())
2459 {
2460 node->getBody()->traverse(this);
2461 }
2462
Jamie Madill8c46ab12015-12-07 16:39:19 -05002463 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002464 out << ";}\n";
2465
2466 if (!firstLoopFragment)
2467 {
2468 out << "}\n";
2469 }
2470
2471 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002472
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002473 initial += MAX_LOOP_ITERATIONS * increment;
2474 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002475 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002476
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002477 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002478
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002479 mExcessiveLoopIndex = restoreIndex;
2480
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002481 return true;
2482 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002483 else
2484 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002485 }
2486
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002487 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002488}
2489
Jamie Madill8c46ab12015-12-07 16:39:19 -05002490void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2491 Visit visit,
2492 const char *preString,
2493 const char *inString,
2494 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002496 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497 {
2498 out << preString;
2499 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002500 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002501 {
2502 out << inString;
2503 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002504 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505 {
2506 out << postString;
2507 }
2508}
2509
Jamie Madill8c46ab12015-12-07 16:39:19 -05002510void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002511{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002512 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002513 {
Jamie Madill32aab012015-01-27 14:12:26 -05002514 out << "\n";
2515 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002516
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002517 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002518 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002519 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002520 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002521
Jamie Madill32aab012015-01-27 14:12:26 -05002522 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002523 }
2524}
2525
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002526TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2527{
2528 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002529 const TType &type = symbol->getType();
2530 const TName &name = symbol->getName();
2531 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002532
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002533 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002534 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002535 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002536 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002537 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002538 {
Olli Etuahoff526f12017-06-30 12:26:54 +03002539 nameStr = DecorateVariableIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002540 }
2541
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002542 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002543 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002544 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2545 {
2546 // Samplers are passed as indices to the sampler array.
2547 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2548 return "const uint " + nameStr + ArrayString(type);
2549 }
2550 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2551 {
2552 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2553 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2554 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2555 ArrayString(type);
2556 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002557 }
2558
Olli Etuaho96963162016-03-21 11:54:33 +02002559 TStringStream argString;
2560 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2561 << ArrayString(type);
2562
2563 // If the structure parameter contains samplers, they need to be passed into the function as
2564 // separate parameters. HLSL doesn't natively support samplers in structs.
2565 if (type.isStructureContainingSamplers())
2566 {
2567 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2568 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho599555b2017-08-15 11:12:42 +03002569 type.createSamplerSymbols("angle" + nameStr, "", &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002570 for (const TIntermSymbol *sampler : samplerSymbols)
2571 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002572 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002573 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2574 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002575 argString << ", const uint " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002576 }
2577 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2578 {
Olli Etuaho96963162016-03-21 11:54:33 +02002579 ASSERT(IsSampler(samplerType.getBasicType()));
2580 argString << ", " << QualifierString(qualifier) << " "
2581 << TextureString(samplerType.getBasicType()) << " texture_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002582 << sampler->getSymbol() << ArrayString(samplerType) << ", "
2583 << QualifierString(qualifier) << " "
Olli Etuaho96963162016-03-21 11:54:33 +02002584 << SamplerString(samplerType.getBasicType()) << " sampler_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002585 << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002586 }
2587 else
2588 {
Olli Etuaho96963162016-03-21 11:54:33 +02002589 ASSERT(IsSampler(samplerType.getBasicType()));
2590 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
Olli Etuaho28839f02017-08-15 11:38:16 +03002591 << " " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002592 }
2593 }
2594 }
2595
2596 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597}
2598
2599TString OutputHLSL::initializer(const TType &type)
2600{
2601 TString string;
2602
Jamie Madill94bf7f22013-07-08 13:31:15 -04002603 size_t size = type.getObjectSize();
2604 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002606 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002607
Jamie Madill94bf7f22013-07-08 13:31:15 -04002608 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002609 {
2610 string += ", ";
2611 }
2612 }
2613
daniel@transgaming.comead23042010-04-29 03:35:36 +00002614 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002616
Jamie Madill8c46ab12015-12-07 16:39:19 -05002617void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2618 Visit visit,
2619 const TType &type,
2620 const char *name,
2621 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002622{
Olli Etuahof40319e2015-03-10 14:33:00 +02002623 if (type.isArray())
2624 {
2625 UNIMPLEMENTED();
2626 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002627
2628 if (visit == PreVisit)
2629 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002630 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002631
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002632 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002633 }
2634 else if (visit == InVisit)
2635 {
2636 out << ", ";
2637 }
2638 else if (visit == PostVisit)
2639 {
2640 out << ")";
2641 }
2642}
2643
Jamie Madill8c46ab12015-12-07 16:39:19 -05002644const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2645 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002646 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002647{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002648 const TConstantUnion *constUnionIterated = constUnion;
2649
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002650 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002651 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002652 {
Jamie Madill033dae62014-06-18 12:56:28 -04002653 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002654
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002655 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002656
Jamie Madill98493dd2013-07-08 14:39:03 -04002657 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002658 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002659 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002660 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002661
Jamie Madill98493dd2013-07-08 14:39:03 -04002662 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002663 {
2664 out << ", ";
2665 }
2666 }
2667
2668 out << ")";
2669 }
2670 else
2671 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002672 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002673 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002674
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002675 if (writeType)
2676 {
Jamie Madill033dae62014-06-18 12:56:28 -04002677 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002678 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002679 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002680 if (writeType)
2681 {
2682 out << ")";
2683 }
2684 }
2685
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002686 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002687}
2688
Olli Etuahod68924e2017-01-02 17:34:40 +00002689void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002690{
Olli Etuahod68924e2017-01-02 17:34:40 +00002691 if (visit == PreVisit)
2692 {
2693 const char *opStr = GetOperatorString(op);
2694 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2695 out << "(";
2696 }
2697 else
2698 {
2699 outputTriplet(out, visit, nullptr, ", ", ")");
2700 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002701}
2702
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002703bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2704 TIntermSymbol *symbolNode,
2705 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002706{
2707 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2708 expression->traverse(&searchSymbol);
2709
2710 if (searchSymbol.foundMatch())
2711 {
2712 // Type already printed
2713 out << "t" + str(mUniqueIndex) + " = ";
2714 expression->traverse(this);
2715 out << ", ";
2716 symbolNode->traverse(this);
2717 out << " = t" + str(mUniqueIndex);
2718
2719 mUniqueIndex++;
2720 return true;
2721 }
2722
2723 return false;
2724}
2725
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002726bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2727{
2728 // We support writing constant unions and constructors that only take constant unions as
2729 // parameters as HLSL literals.
Olli Etuahod4f4c112016-04-15 15:11:24 +03002730 return expression->getAsConstantUnion() ||
2731 expression->isConstructorWithOnlyConstantUnionParameters();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002732}
2733
2734bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2735 TIntermSymbol *symbolNode,
2736 TIntermTyped *expression)
2737{
2738 if (canWriteAsHLSLLiteral(expression))
2739 {
2740 symbolNode->traverse(this);
2741 if (expression->getType().isArray())
2742 {
2743 out << "[" << expression->getType().getArraySize() << "]";
2744 }
2745 out << " = {";
2746 if (expression->getAsConstantUnion())
2747 {
2748 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
2749 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002750 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002751 }
2752 else
2753 {
2754 TIntermAggregate *constructor = expression->getAsAggregate();
2755 ASSERT(constructor != nullptr);
2756 for (TIntermNode *&node : *constructor->getSequence())
2757 {
2758 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2759 ASSERT(nodeConst);
2760 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002761 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002762 if (node != constructor->getSequence()->back())
2763 {
2764 out << ", ";
2765 }
2766 }
2767 }
2768 out << "}";
2769 return true;
2770 }
2771 return false;
2772}
2773
Jamie Madill55e79e02015-02-09 15:35:00 -05002774TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2775{
2776 const TFieldList &fields = structure.fields();
2777
2778 for (const auto &eqFunction : mStructEqualityFunctions)
2779 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002780 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002781 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002782 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002783 }
2784 }
2785
2786 const TString &structNameString = StructNameString(structure);
2787
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002788 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002789 function->structure = &structure;
2790 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002791
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002792 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002793
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002794 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2795 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002796 << "{\n"
2797 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002798
2799 for (size_t i = 0; i < fields.size(); i++)
2800 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002801 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002802 const TType *fieldType = field->type();
2803
2804 const TString &fieldNameA = "a." + Decorate(field->name());
2805 const TString &fieldNameB = "b." + Decorate(field->name());
2806
2807 if (i > 0)
2808 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002809 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002810 }
2811
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002812 fnOut << "(";
2813 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2814 fnOut << fieldNameA;
2815 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2816 fnOut << fieldNameB;
2817 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2818 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002819 }
2820
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002821 fnOut << ";\n"
2822 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002823
2824 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002825
2826 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002827 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002828
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002829 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002830}
2831
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002832TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002833{
2834 for (const auto &eqFunction : mArrayEqualityFunctions)
2835 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002836 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002837 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002838 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002839 }
2840 }
2841
2842 const TString &typeName = TypeString(type);
2843
Olli Etuaho12690762015-03-31 12:55:28 +03002844 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002845 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002846
2847 TInfoSinkBase fnNameOut;
2848 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002849 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002850
2851 TType nonArrayType = type;
2852 nonArrayType.clearArrayness();
2853
2854 TInfoSinkBase fnOut;
2855
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002856 fnOut << "bool " << function->functionName << "(" << typeName << " a[" << type.getArraySize()
2857 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002858 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002859 " for (int i = 0; i < "
2860 << type.getArraySize() << "; ++i)\n"
2861 " {\n"
2862 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002863
2864 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
2865 fnOut << "a[i]";
2866 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
2867 fnOut << "b[i]";
2868 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
2869
2870 fnOut << ") { return false; }\n"
2871 " }\n"
2872 " return true;\n"
2873 "}\n";
2874
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002875 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002876
2877 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002878 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002879
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002880 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002881}
2882
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002883TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002884{
2885 for (const auto &assignFunction : mArrayAssignmentFunctions)
2886 {
2887 if (assignFunction.type == type)
2888 {
2889 return assignFunction.functionName;
2890 }
2891 }
2892
2893 const TString &typeName = TypeString(type);
2894
2895 ArrayHelperFunction function;
2896 function.type = type;
2897
2898 TInfoSinkBase fnNameOut;
2899 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
2900 function.functionName = fnNameOut.c_str();
2901
2902 TInfoSinkBase fnOut;
2903
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002904 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2905 << "], " << typeName << " b[" << type.getArraySize() << "])\n"
2906 << "{\n"
2907 " for (int i = 0; i < "
2908 << type.getArraySize() << "; ++i)\n"
2909 " {\n"
2910 " a[i] = b[i];\n"
2911 " }\n"
2912 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03002913
2914 function.functionDefinition = fnOut.c_str();
2915
2916 mArrayAssignmentFunctions.push_back(function);
2917
2918 return function.functionName;
2919}
2920
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002921TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03002922{
2923 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2924 {
2925 if (constructIntoFunction.type == type)
2926 {
2927 return constructIntoFunction.functionName;
2928 }
2929 }
2930
2931 const TString &typeName = TypeString(type);
2932
2933 ArrayHelperFunction function;
2934 function.type = type;
2935
2936 TInfoSinkBase fnNameOut;
2937 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
2938 function.functionName = fnNameOut.c_str();
2939
2940 TInfoSinkBase fnOut;
2941
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002942 fnOut << "void " << function.functionName << "(out " << typeName << " a[" << type.getArraySize()
2943 << "]";
Olli Etuaho856c4972016-08-08 11:38:39 +03002944 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002945 {
2946 fnOut << ", " << typeName << " b" << i;
2947 }
2948 fnOut << ")\n"
2949 "{\n";
2950
Olli Etuaho856c4972016-08-08 11:38:39 +03002951 for (unsigned int i = 0u; i < type.getArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03002952 {
2953 fnOut << " a[" << i << "] = b" << i << ";\n";
2954 }
2955 fnOut << "}\n";
2956
2957 function.functionDefinition = fnOut.c_str();
2958
2959 mArrayConstructIntoFunctions.push_back(function);
2960
2961 return function.functionName;
2962}
2963
Jamie Madill2e295e22015-04-29 10:41:33 -04002964void OutputHLSL::ensureStructDefined(const TType &type)
2965{
2966 TStructure *structure = type.getStruct();
2967
2968 if (structure)
2969 {
2970 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
2971 }
2972}
2973
Jamie Madill45bcc782016-11-07 13:58:48 -05002974} // namespace sh