blob: 2fddbd33c1b39e66984d24818c2451ae1339de20 [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 Etuaho96f6adf2017-08-16 11:18:54 +030034namespace
35{
36
37TString ArrayHelperFunctionName(const char *prefix, const TType &type)
38{
39 TStringStream fnName;
40 fnName << prefix << "_";
41 for (unsigned int arraySize : type.getArraySizes())
42 {
43 fnName << arraySize << "_";
44 }
45 fnName << TypeString(type);
46 return fnName.str();
47}
48
Olli Etuaho40dbdd62017-10-13 13:34:19 +030049bool IsDeclarationWrittenOut(TIntermDeclaration *node)
50{
51 TIntermSequence *sequence = node->getSequence();
52 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
53 ASSERT(sequence->size() == 1);
54 ASSERT(variable);
55 return (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
56 variable->getQualifier() == EvqConst);
57}
58
Olli Etuaho96f6adf2017-08-16 11:18:54 +030059} // anonymous namespace
60
Olli Etuaho56a2f952016-12-08 12:16:27 +000061void OutputHLSL::writeFloat(TInfoSinkBase &out, float f)
Olli Etuaho4785fec2015-05-18 16:09:37 +030062{
Olli Etuaho56a2f952016-12-08 12:16:27 +000063 // This is known not to work for NaN on all drivers but make the best effort to output NaNs
64 // regardless.
65 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300 &&
66 mOutputType == SH_HLSL_4_1_OUTPUT)
67 {
68 out << "asfloat(" << gl::bitCast<uint32_t>(f) << "u)";
69 }
70 else
71 {
72 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
73 }
74}
Olli Etuaho4785fec2015-05-18 16:09:37 +030075
Olli Etuaho56a2f952016-12-08 12:16:27 +000076void OutputHLSL::writeSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
Olli Etuaho18b9deb2015-11-05 12:14:50 +020077{
78 ASSERT(constUnion != nullptr);
79 switch (constUnion->getType())
80 {
81 case EbtFloat:
Olli Etuaho56a2f952016-12-08 12:16:27 +000082 writeFloat(out, constUnion->getFConst());
Olli Etuaho18b9deb2015-11-05 12:14:50 +020083 break;
84 case EbtInt:
85 out << constUnion->getIConst();
86 break;
87 case EbtUInt:
88 out << constUnion->getUConst();
89 break;
90 case EbtBool:
91 out << constUnion->getBConst();
92 break;
93 default:
94 UNREACHABLE();
95 }
96}
97
Olli Etuaho56a2f952016-12-08 12:16:27 +000098const TConstantUnion *OutputHLSL::writeConstantUnionArray(TInfoSinkBase &out,
99 const TConstantUnion *const constUnion,
100 const size_t size)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200101{
102 const TConstantUnion *constUnionIterated = constUnion;
103 for (size_t i = 0; i < size; i++, constUnionIterated++)
104 {
Olli Etuaho56a2f952016-12-08 12:16:27 +0000105 writeSingleConstant(out, constUnionIterated);
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200106
107 if (i != size - 1)
108 {
109 out << ", ";
110 }
111 }
112 return constUnionIterated;
113}
114
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800115OutputHLSL::OutputHLSL(sh::GLenum shaderType,
116 int shaderVersion,
117 const TExtensionBehavior &extensionBehavior,
118 const char *sourcePath,
119 ShShaderOutput outputType,
120 int numRenderTargets,
121 const std::vector<Uniform> &uniforms,
122 ShCompileOptions compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400123 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200124 mShaderType(shaderType),
125 mShaderVersion(shaderVersion),
126 mExtensionBehavior(extensionBehavior),
127 mSourcePath(sourcePath),
128 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700129 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000130 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700131 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000133 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000134
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500135 mUsesFragColor = false;
136 mUsesFragData = false;
137 mUsesDepthRange = false;
138 mUsesFragCoord = false;
139 mUsesPointCoord = false;
140 mUsesFrontFacing = false;
141 mUsesPointSize = false;
142 mUsesInstanceID = false;
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300143 mHasMultiviewExtensionEnabled =
144 IsExtensionEnabled(mExtensionBehavior, TExtension::OVR_multiview);
Martin Radev41ac68e2017-06-06 12:16:58 +0300145 mUsesViewID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500146 mUsesVertexID = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500147 mUsesFragDepth = false;
Xinghua Caob1239382016-12-13 15:07:05 +0800148 mUsesNumWorkGroups = false;
149 mUsesWorkGroupID = false;
150 mUsesLocalInvocationID = false;
151 mUsesGlobalInvocationID = false;
152 mUsesLocalInvocationIndex = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500153 mUsesXor = false;
154 mUsesDiscardRewriting = false;
155 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530156 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000157
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000158 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000159
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500160 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000161 mInsideDiscontinuousLoop = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500162 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000163
Yunchao Hed7297bf2017-04-19 15:27:10 +0800164 mExcessiveLoopIndex = nullptr;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000165
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500166 mStructureHLSL = new StructureHLSL;
167 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300168 mTextureFunctionHLSL = new TextureFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400169
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200170 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000171 {
Arun Patole63419392015-03-13 11:51:07 +0530172 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500173 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
174 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530175 // In both cases total 3 uniform registers need to be reserved.
176 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000177 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000178
Geoff Lang00140f42016-02-03 18:47:33 +0000179 // Reserve registers for the default uniform block and driver constants
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800180 mUniformHLSL->reserveUniformBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181}
182
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000183OutputHLSL::~OutputHLSL()
184{
Jamie Madill8daaba12014-06-13 10:04:33 -0400185 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400186 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300187 SafeDelete(mTextureFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200188 for (auto &eqFunction : mStructEqualityFunctions)
189 {
190 SafeDelete(eqFunction);
191 }
192 for (auto &eqFunction : mArrayEqualityFunctions)
193 {
194 SafeDelete(eqFunction);
195 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000196}
197
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200198void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000199{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500200 const std::vector<TIntermTyped *> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400201 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000202
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200203 BuiltInFunctionEmulator builtInFunctionEmulator;
204 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800205 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
206 {
207 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
208 mShaderVersion);
209 }
210
Olli Etuahodfa75e82017-01-23 09:43:06 -0800211 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500212
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700213 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000214 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700215 ASSERT(success == CallDAG::INITDAG_SUCCESS);
216 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700217
Jamie Madill37997142015-01-28 10:06:34 -0500218 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500219 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200220 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500221 mInfoSinkStack.pop();
222
Jamie Madill37997142015-01-28 10:06:34 -0500223 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500224 mInfoSinkStack.pop();
225
Jamie Madill32aab012015-01-27 14:12:26 -0500226 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500227 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500228 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000229
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200230 objSink << mHeader.c_str();
231 objSink << mBody.c_str();
232 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200233
Olli Etuahodfa75e82017-01-23 09:43:06 -0800234 builtInFunctionEmulator.cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000235}
236
Jamie Madill570e04d2013-06-21 09:15:33 -0400237void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
238{
239 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
240 {
241 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
242
Jamie Madill32aab012015-01-27 14:12:26 -0500243 TInfoSinkBase structInfoSink;
244 mInfoSinkStack.push(&structInfoSink);
245
Jamie Madill570e04d2013-06-21 09:15:33 -0400246 // This will mark the necessary block elements as referenced
247 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500248
249 TString structName(structInfoSink.c_str());
250 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400251
252 mFlaggedStructOriginalNames[flaggedNode] = structName;
253
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500254 for (size_t pos = structName.find('.'); pos != std::string::npos;
255 pos = structName.find('.'))
Jamie Madill570e04d2013-06-21 09:15:33 -0400256 {
257 structName.erase(pos, 1);
258 }
259
260 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
261 }
262}
263
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800264const std::map<std::string, unsigned int> &OutputHLSL::getUniformBlockRegisterMap() const
Jamie Madill4e1fd412014-07-10 17:50:10 -0400265{
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800266 return mUniformHLSL->getUniformBlockRegisterMap();
Jamie Madill4e1fd412014-07-10 17:50:10 -0400267}
268
Jamie Madill9fe25e92014-07-18 10:33:08 -0400269const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
270{
271 return mUniformHLSL->getUniformRegisterMap();
272}
273
Olli Etuahoed049ab2017-06-30 17:38:33 +0300274TString OutputHLSL::structInitializerString(int indent, const TType &type, const TString &name)
Jamie Madill570e04d2013-06-21 09:15:33 -0400275{
276 TString init;
277
Olli Etuahoed049ab2017-06-30 17:38:33 +0300278 TString indentString;
279 for (int spaces = 0; spaces < indent; spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400280 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300281 indentString += " ";
Jamie Madill570e04d2013-06-21 09:15:33 -0400282 }
283
Olli Etuahoed049ab2017-06-30 17:38:33 +0300284 if (type.isArray())
Jamie Madill570e04d2013-06-21 09:15:33 -0400285 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300286 init += indentString + "{\n";
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300287 for (unsigned int arrayIndex = 0u; arrayIndex < type.getOutermostArraySize(); ++arrayIndex)
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300289 TStringStream indexedString;
290 indexedString << name << "[" << arrayIndex << "]";
291 TType elementType = type;
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300292 elementType.toArrayElementType();
Olli Etuahoed049ab2017-06-30 17:38:33 +0300293 init += structInitializerString(indent + 1, elementType, indexedString.str());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300294 if (arrayIndex < type.getOutermostArraySize() - 1)
Olli Etuahoed049ab2017-06-30 17:38:33 +0300295 {
296 init += ",";
297 }
298 init += "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400299 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300300 init += indentString + "}";
Jamie Madill570e04d2013-06-21 09:15:33 -0400301 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300302 else if (type.getBasicType() == EbtStruct)
303 {
304 init += indentString + "{\n";
305 const TStructure &structure = *type.getStruct();
306 const TFieldList &fields = structure.fields();
307 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
308 {
309 const TField &field = *fields[fieldIndex];
310 const TString &fieldName = name + "." + Decorate(field.name());
311 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400312
Olli Etuahoed049ab2017-06-30 17:38:33 +0300313 init += structInitializerString(indent + 1, fieldType, fieldName);
314 if (fieldIndex < fields.size() - 1)
315 {
316 init += ",";
317 }
318 init += "\n";
319 }
320 init += indentString + "}";
321 }
322 else
323 {
324 init += indentString + name;
325 }
Jamie Madill570e04d2013-06-21 09:15:33 -0400326
327 return init;
328}
329
Jamie Madill8c46ab12015-12-07 16:39:19 -0500330void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000331{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000332 TString varyings;
333 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400334 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000335
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500336 for (std::map<TIntermTyped *, TString>::const_iterator flaggedStructIt =
337 mFlaggedStructMappedNames.begin();
338 flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400339 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500340 TIntermTyped *structNode = flaggedStructIt->first;
341 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400342 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400343 const TString &originalName = mFlaggedStructOriginalNames[structNode];
344
Olli Etuahoed049ab2017-06-30 17:38:33 +0300345 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName;
346 if (structNode->isArray())
347 {
348 flaggedStructs += ArrayString(structNode->getType());
349 }
350 flaggedStructs += " =\n";
351 flaggedStructs += structInitializerString(0, structNode->getType(), originalName);
352 flaggedStructs += ";\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400353 }
354
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500355 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin();
356 varying != mReferencedVaryings.end(); varying++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000357 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500358 const TType &type = varying->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000359 const TString &name = varying->second->getSymbol();
360
361 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500362 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
363 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000364 }
365
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500366 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin();
367 attribute != mReferencedAttributes.end(); attribute++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000368 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500369 const TType &type = attribute->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000370 const TString &name = attribute->second->getSymbol();
371
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500372 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
373 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000374 }
375
Jamie Madill8daaba12014-06-13 10:04:33 -0400376 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400377
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200378 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800379 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400380
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200381 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500382 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200383 out << "\n// Equality functions\n\n";
384 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500385 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200386 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200387 }
388 }
Olli Etuaho12690762015-03-31 12:55:28 +0300389 if (!mArrayAssignmentFunctions.empty())
390 {
391 out << "\n// Assignment functions\n\n";
392 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
393 {
394 out << assignmentFunction.functionDefinition << "\n";
395 }
396 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300397 if (!mArrayConstructIntoFunctions.empty())
398 {
399 out << "\n// Array constructor functions\n\n";
400 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
401 {
402 out << constructIntoFunction.functionDefinition << "\n";
403 }
404 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200405
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500406 if (mUsesDiscardRewriting)
407 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400408 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500409 }
410
Nicolas Capens655fe362014-04-11 13:12:34 -0400411 if (mUsesNestedBreak)
412 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400413 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400414 }
415
Arun Patole44efa0b2015-03-04 17:11:05 +0530416 if (mRequiresIEEEStrictCompiling)
417 {
418 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
419 }
420
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400421 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
422 "#define LOOP [loop]\n"
423 "#define FLATTEN [flatten]\n"
424 "#else\n"
425 "#define LOOP\n"
426 "#define FLATTEN\n"
427 "#endif\n";
428
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200429 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300431 const bool usingMRTExtension =
432 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000433
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000434 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500435 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400436 out << "\n";
437
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200438 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000439 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500440 for (ReferencedSymbols::const_iterator outputVariableIt =
441 mReferencedOutputVariables.begin();
442 outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000443 {
Jamie Madill46131a32013-06-20 11:55:50 -0400444 const TString &variableName = outputVariableIt->first;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500445 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400446
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500447 out << "static " + TypeString(variableType) + " out_" + variableName +
448 ArrayString(variableType) + " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000449 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000450 }
Jamie Madill46131a32013-06-20 11:55:50 -0400451 else
452 {
453 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
454
455 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500456 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400457 for (unsigned int i = 0; i < numColorValues; i++)
458 {
459 out << " float4(0, 0, 0, 0)";
460 if (i + 1 != numColorValues)
461 {
462 out << ",";
463 }
464 out << "\n";
465 }
466
467 out << "};\n";
468 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000469
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400470 if (mUsesFragDepth)
471 {
472 out << "static float gl_Depth = 0.0;\n";
473 }
474
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000475 if (mUsesFragCoord)
476 {
477 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
478 }
479
480 if (mUsesPointCoord)
481 {
482 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
483 }
484
485 if (mUsesFrontFacing)
486 {
487 out << "static bool gl_FrontFacing = false;\n";
488 }
489
490 out << "\n";
491
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000492 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000493 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000494 out << "struct gl_DepthRangeParameters\n"
495 "{\n"
496 " float near;\n"
497 " float far;\n"
498 " float diff;\n"
499 "};\n"
500 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000501 }
502
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200503 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000504 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000505 out << "cbuffer DriverConstants : register(b1)\n"
506 "{\n";
507
508 if (mUsesDepthRange)
509 {
510 out << " float3 dx_DepthRange : packoffset(c0);\n";
511 }
512
513 if (mUsesFragCoord)
514 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000515 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000516 }
517
518 if (mUsesFragCoord || mUsesFrontFacing)
519 {
520 out << " float3 dx_DepthFront : packoffset(c2);\n";
521 }
522
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800523 if (mUsesFragCoord)
524 {
525 // dx_ViewScale is only used in the fragment shader to correct
526 // the value for glFragCoord if necessary
527 out << " float2 dx_ViewScale : packoffset(c3);\n";
528 }
529
Martin Radev72b4e1e2017-08-31 15:42:56 +0300530 if (mHasMultiviewExtensionEnabled)
531 {
532 // We have to add a value which we can use to keep track of which multi-view code
533 // path is to be selected in the GS.
534 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
535 }
536
Olli Etuaho618bebc2016-01-15 16:40:00 +0200537 if (mOutputType == SH_HLSL_4_1_OUTPUT)
538 {
539 mUniformHLSL->samplerMetadataUniforms(out, "c4");
540 }
541
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000542 out << "};\n";
543 }
544 else
545 {
546 if (mUsesDepthRange)
547 {
548 out << "uniform float3 dx_DepthRange : register(c0);";
549 }
550
551 if (mUsesFragCoord)
552 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000553 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000554 }
555
556 if (mUsesFragCoord || mUsesFrontFacing)
557 {
558 out << "uniform float3 dx_DepthFront : register(c2);\n";
559 }
560 }
561
562 out << "\n";
563
564 if (mUsesDepthRange)
565 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500566 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
567 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000568 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000569 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000570
Jamie Madillf91ce812014-06-13 10:04:34 -0400571 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000572 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400573 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000574 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400575 out << flaggedStructs;
576 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000577 }
578
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000579 if (usingMRTExtension && mNumRenderTargets > 1)
580 {
581 out << "#define GL_USES_MRT\n";
582 }
583
584 if (mUsesFragColor)
585 {
586 out << "#define GL_USES_FRAG_COLOR\n";
587 }
588
589 if (mUsesFragData)
590 {
591 out << "#define GL_USES_FRAG_DATA\n";
592 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593 }
Xinghua Caob1239382016-12-13 15:07:05 +0800594 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000596 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500597 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000598 out << "\n"
599 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400600
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000601 if (mUsesPointSize)
602 {
603 out << "static float gl_PointSize = float(1);\n";
604 }
605
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000606 if (mUsesInstanceID)
607 {
608 out << "static int gl_InstanceID;";
609 }
610
Corentin Wallezb076add2016-01-11 16:45:46 -0500611 if (mUsesVertexID)
612 {
613 out << "static int gl_VertexID;";
614 }
615
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000616 out << "\n"
617 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500618 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000619 out << "\n";
620
621 if (mUsesDepthRange)
622 {
623 out << "struct gl_DepthRangeParameters\n"
624 "{\n"
625 " float near;\n"
626 " float far;\n"
627 " float diff;\n"
628 "};\n"
629 "\n";
630 }
631
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200632 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000633 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800634 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500635 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800636
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000637 if (mUsesDepthRange)
638 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800639 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000640 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800641
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800642 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
643 // shaders. However, we declare it for all shaders (including Feature Level 10+).
644 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
645 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800646 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800647 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800648 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800649
Martin Radev72b4e1e2017-08-31 15:42:56 +0300650 if (mHasMultiviewExtensionEnabled)
651 {
652 // We have to add a value which we can use to keep track of which multi-view code
653 // path is to be selected in the GS.
654 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
655 }
656
Olli Etuaho618bebc2016-01-15 16:40:00 +0200657 if (mOutputType == SH_HLSL_4_1_OUTPUT)
658 {
659 mUniformHLSL->samplerMetadataUniforms(out, "c4");
660 }
661
Austin Kinross4fd18b12014-12-22 12:32:05 -0800662 out << "};\n"
663 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000664 }
665 else
666 {
667 if (mUsesDepthRange)
668 {
669 out << "uniform float3 dx_DepthRange : register(c0);\n";
670 }
671
Cooper Partine6664f02015-01-09 16:22:24 -0800672 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
673 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000674 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000675 }
676
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000677 if (mUsesDepthRange)
678 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500679 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
680 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000681 "\n";
682 }
683
Jamie Madillf91ce812014-06-13 10:04:34 -0400684 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000685 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400686 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000687 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400688 out << flaggedStructs;
689 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000690 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400691 }
Xinghua Caob1239382016-12-13 15:07:05 +0800692 else // Compute shader
693 {
694 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800695
696 out << "cbuffer DriverConstants : register(b1)\n"
697 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800698 if (mUsesNumWorkGroups)
699 {
Xinghua Caob1239382016-12-13 15:07:05 +0800700 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800701 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800702 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
703 mUniformHLSL->samplerMetadataUniforms(out, "c1");
704 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800705
706 // Follow built-in variables would be initialized in
707 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
708 // are used in compute shader.
709 if (mUsesWorkGroupID)
710 {
711 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
712 }
713
714 if (mUsesLocalInvocationID)
715 {
716 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
717 }
718
719 if (mUsesGlobalInvocationID)
720 {
721 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
722 }
723
724 if (mUsesLocalInvocationIndex)
725 {
726 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
727 }
728 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000729
Geoff Lang1fe74c72016-08-25 13:23:01 -0400730 bool getDimensionsIgnoresBaseLevel =
731 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
732 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000734 if (mUsesFragCoord)
735 {
736 out << "#define GL_USES_FRAG_COORD\n";
737 }
738
739 if (mUsesPointCoord)
740 {
741 out << "#define GL_USES_POINT_COORD\n";
742 }
743
744 if (mUsesFrontFacing)
745 {
746 out << "#define GL_USES_FRONT_FACING\n";
747 }
748
749 if (mUsesPointSize)
750 {
751 out << "#define GL_USES_POINT_SIZE\n";
752 }
753
Martin Radev41ac68e2017-06-06 12:16:58 +0300754 if (mHasMultiviewExtensionEnabled)
755 {
756 out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n";
757 }
758
759 if (mUsesViewID)
760 {
761 out << "#define GL_USES_VIEW_ID\n";
762 }
763
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400764 if (mUsesFragDepth)
765 {
766 out << "#define GL_USES_FRAG_DEPTH\n";
767 }
768
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000769 if (mUsesDepthRange)
770 {
771 out << "#define GL_USES_DEPTH_RANGE\n";
772 }
773
Xinghua Caob1239382016-12-13 15:07:05 +0800774 if (mUsesNumWorkGroups)
775 {
776 out << "#define GL_USES_NUM_WORK_GROUPS\n";
777 }
778
779 if (mUsesWorkGroupID)
780 {
781 out << "#define GL_USES_WORK_GROUP_ID\n";
782 }
783
784 if (mUsesLocalInvocationID)
785 {
786 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
787 }
788
789 if (mUsesGlobalInvocationID)
790 {
791 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
792 }
793
794 if (mUsesLocalInvocationIndex)
795 {
796 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
797 }
798
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000799 if (mUsesXor)
800 {
801 out << "bool xor(bool p, bool q)\n"
802 "{\n"
803 " return (p || q) && !(p && q);\n"
804 "}\n"
805 "\n";
806 }
807
Olli Etuahodfa75e82017-01-23 09:43:06 -0800808 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000809}
810
811void OutputHLSL::visitSymbol(TIntermSymbol *node)
812{
Jamie Madill32aab012015-01-27 14:12:26 -0500813 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814
Jamie Madill570e04d2013-06-21 09:15:33 -0400815 // Handle accessing std140 structs by value
816 if (mFlaggedStructMappedNames.count(node) > 0)
817 {
818 out << mFlaggedStructMappedNames[node];
819 return;
820 }
821
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822 TString name = node->getSymbol();
823
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000824 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000825 {
826 mUsesDepthRange = true;
827 out << name;
828 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829 else
830 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000831 TQualifier qualifier = node->getQualifier();
832
833 if (qualifier == EvqUniform)
834 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500835 const TType &nodeType = node->getType();
Jamie Madill2e295e22015-04-29 10:41:33 -0400836 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400837
838 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000839 {
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800840 mReferencedUniformBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000841 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000842 else
843 {
844 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000845 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400846
Jamie Madill2e295e22015-04-29 10:41:33 -0400847 ensureStructDefined(nodeType);
848
Olli Etuahoff526f12017-06-30 12:26:54 +0300849 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000850 }
Jamie Madill19571812013-08-12 15:26:34 -0700851 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000852 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000853 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400854 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000855 }
Jamie Madill033dae62014-06-18 12:56:28 -0400856 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000857 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000858 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400859 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300860 if (name == "ViewID_OVR")
861 {
862 mUsesViewID = true;
863 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000864 }
Jamie Madill19571812013-08-12 15:26:34 -0700865 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400866 {
867 mReferencedOutputVariables[name] = node;
868 out << "out_" << name;
869 }
870 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000871 {
872 out << "gl_Color[0]";
873 mUsesFragColor = true;
874 }
875 else if (qualifier == EvqFragData)
876 {
877 out << "gl_Color";
878 mUsesFragData = true;
879 }
880 else if (qualifier == EvqFragCoord)
881 {
882 mUsesFragCoord = true;
883 out << name;
884 }
885 else if (qualifier == EvqPointCoord)
886 {
887 mUsesPointCoord = true;
888 out << name;
889 }
890 else if (qualifier == EvqFrontFacing)
891 {
892 mUsesFrontFacing = true;
893 out << name;
894 }
895 else if (qualifier == EvqPointSize)
896 {
897 mUsesPointSize = true;
898 out << name;
899 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000900 else if (qualifier == EvqInstanceID)
901 {
902 mUsesInstanceID = true;
903 out << name;
904 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500905 else if (qualifier == EvqVertexID)
906 {
907 mUsesVertexID = true;
908 out << name;
909 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300910 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400911 {
912 mUsesFragDepth = true;
913 out << "gl_Depth";
914 }
Xinghua Caob1239382016-12-13 15:07:05 +0800915 else if (qualifier == EvqNumWorkGroups)
916 {
917 mUsesNumWorkGroups = true;
918 out << name;
919 }
920 else if (qualifier == EvqWorkGroupID)
921 {
922 mUsesWorkGroupID = true;
923 out << name;
924 }
925 else if (qualifier == EvqLocalInvocationID)
926 {
927 mUsesLocalInvocationID = true;
928 out << name;
929 }
930 else if (qualifier == EvqGlobalInvocationID)
931 {
932 mUsesGlobalInvocationID = true;
933 out << name;
934 }
935 else if (qualifier == EvqLocalInvocationIndex)
936 {
937 mUsesLocalInvocationIndex = true;
938 out << name;
939 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000940 else
941 {
Olli Etuahoff526f12017-06-30 12:26:54 +0300942 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000943 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944 }
945}
946
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400947void OutputHLSL::visitRaw(TIntermRaw *node)
948{
Jamie Madill32aab012015-01-27 14:12:26 -0500949 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400950}
951
Olli Etuaho7fb49552015-03-18 17:27:44 +0200952void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
953{
954 if (type.isScalar() && !type.isArray())
955 {
956 if (op == EOpEqual)
957 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500958 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200959 }
960 else
961 {
Jamie Madill8c46ab12015-12-07 16:39:19 -0500962 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200963 }
964 }
965 else
966 {
967 if (visit == PreVisit && op == EOpNotEqual)
968 {
969 out << "!";
970 }
971
972 if (type.isArray())
973 {
974 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500975 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200976 }
977 else if (type.getBasicType() == EbtStruct)
978 {
979 const TStructure &structure = *type.getStruct();
980 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500981 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200982 }
983 else
984 {
985 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -0500986 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +0200987 }
988 }
989}
990
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300991void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
992{
993 if (type.isArray())
994 {
995 const TString &functionName = addArrayAssignmentFunction(type);
996 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
997 }
998 else
999 {
1000 outputTriplet(out, visit, "(", " = ", ")");
1001 }
1002}
1003
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001004bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001005{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001006 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001007 {
1008 TIntermNode *ancestor = getAncestorNode(n);
1009 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1010 if (ancestorBinary == nullptr)
1011 {
1012 return false;
1013 }
1014 switch (ancestorBinary->getOp())
1015 {
1016 case EOpIndexDirectStruct:
1017 {
1018 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1019 const TIntermConstantUnion *index =
1020 ancestorBinary->getRight()->getAsConstantUnion();
1021 const TField *field = structure->fields()[index->getIConst(0)];
1022 if (IsSampler(field->type()->getBasicType()))
1023 {
1024 return true;
1025 }
1026 break;
1027 }
1028 case EOpIndexDirect:
1029 break;
1030 default:
1031 // Returning a sampler from indirect indexing is not supported.
1032 return false;
1033 }
1034 }
1035 return false;
1036}
1037
Olli Etuahob6fa0432016-09-28 16:28:05 +01001038bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1039{
1040 TInfoSinkBase &out = getInfoSink();
1041 if (visit == PostVisit)
1042 {
1043 out << ".";
1044 node->writeOffsetsAsXYZW(&out);
1045 }
1046 return true;
1047}
1048
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001049bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1050{
Jamie Madill32aab012015-01-27 14:12:26 -05001051 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052
Jamie Madill570e04d2013-06-21 09:15:33 -04001053 // Handle accessing std140 structs by value
1054 if (mFlaggedStructMappedNames.count(node) > 0)
1055 {
1056 out << mFlaggedStructMappedNames[node];
1057 return false;
1058 }
1059
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060 switch (node->getOp())
1061 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001062 case EOpComma:
1063 outputTriplet(out, visit, "(", ", ", ")");
1064 break;
1065 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001066 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001067 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001068 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1069 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001070 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001071 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1072 out << functionName << "(";
1073 node->getLeft()->traverse(this);
1074 TIntermSequence *seq = rightAgg->getSequence();
1075 for (auto &arrayElement : *seq)
1076 {
1077 out << ", ";
1078 arrayElement->traverse(this);
1079 }
1080 out << ")";
1081 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001082 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001083 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1084 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001085 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001086 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001087 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001088 break;
1089 case EOpInitialize:
1090 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001091 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001092 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1093 ASSERT(symbolNode);
1094 TIntermTyped *expression = node->getRight();
1095
1096 // Global initializers must be constant at this point.
1097 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
1098 canWriteAsHLSLLiteral(expression));
1099
1100 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1101 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1102 // new variable is created before the assignment is evaluated), so we need to
1103 // convert
1104 // this to "float t = x, x = t;".
1105 if (writeSameSymbolInitializer(out, symbolNode, expression))
1106 {
1107 // Skip initializing the rest of the expression
1108 return false;
1109 }
1110 else if (writeConstantInitialization(out, symbolNode, expression))
1111 {
1112 return false;
1113 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001114 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001115 else if (visit == InVisit)
1116 {
1117 out << " = ";
1118 }
1119 break;
1120 case EOpAddAssign:
1121 outputTriplet(out, visit, "(", " += ", ")");
1122 break;
1123 case EOpSubAssign:
1124 outputTriplet(out, visit, "(", " -= ", ")");
1125 break;
1126 case EOpMulAssign:
1127 outputTriplet(out, visit, "(", " *= ", ")");
1128 break;
1129 case EOpVectorTimesScalarAssign:
1130 outputTriplet(out, visit, "(", " *= ", ")");
1131 break;
1132 case EOpMatrixTimesScalarAssign:
1133 outputTriplet(out, visit, "(", " *= ", ")");
1134 break;
1135 case EOpVectorTimesMatrixAssign:
1136 if (visit == PreVisit)
1137 {
1138 out << "(";
1139 }
1140 else if (visit == InVisit)
1141 {
1142 out << " = mul(";
1143 node->getLeft()->traverse(this);
1144 out << ", transpose(";
1145 }
1146 else
1147 {
1148 out << ")))";
1149 }
1150 break;
1151 case EOpMatrixTimesMatrixAssign:
1152 if (visit == PreVisit)
1153 {
1154 out << "(";
1155 }
1156 else if (visit == InVisit)
1157 {
1158 out << " = transpose(mul(transpose(";
1159 node->getLeft()->traverse(this);
1160 out << "), transpose(";
1161 }
1162 else
1163 {
1164 out << "))))";
1165 }
1166 break;
1167 case EOpDivAssign:
1168 outputTriplet(out, visit, "(", " /= ", ")");
1169 break;
1170 case EOpIModAssign:
1171 outputTriplet(out, visit, "(", " %= ", ")");
1172 break;
1173 case EOpBitShiftLeftAssign:
1174 outputTriplet(out, visit, "(", " <<= ", ")");
1175 break;
1176 case EOpBitShiftRightAssign:
1177 outputTriplet(out, visit, "(", " >>= ", ")");
1178 break;
1179 case EOpBitwiseAndAssign:
1180 outputTriplet(out, visit, "(", " &= ", ")");
1181 break;
1182 case EOpBitwiseXorAssign:
1183 outputTriplet(out, visit, "(", " ^= ", ")");
1184 break;
1185 case EOpBitwiseOrAssign:
1186 outputTriplet(out, visit, "(", " |= ", ")");
1187 break;
1188 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001189 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001190 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001191 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001192 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001193 if (visit == PreVisit)
1194 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001195 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001196 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001197 mReferencedUniformBlocks[interfaceBlock->instanceName()] =
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001198 node->getLeft()->getAsSymbolNode();
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001199 out << mUniformHLSL->uniformBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001200 return false;
1201 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001202 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001203 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001204 {
1205 // All parts of an expression that access a sampler in a struct need to use _ as
1206 // separator to access the sampler variable that has been moved out of the struct.
1207 outputTriplet(out, visit, "", "_", "");
1208 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001209 else
1210 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001211 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001212 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001213 }
1214 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001215 case EOpIndexIndirect:
1216 // We do not currently support indirect references to interface blocks
1217 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1218 outputTriplet(out, visit, "", "[", "]");
1219 break;
1220 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001221 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001222 const TStructure *structure = node->getLeft()->getType().getStruct();
1223 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1224 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001225
Olli Etuaho96963162016-03-21 11:54:33 +02001226 // In cases where indexing returns a sampler, we need to access the sampler variable
1227 // that has been moved out of the struct.
1228 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1229 if (visit == PreVisit && indexingReturnsSampler)
1230 {
1231 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1232 // This prefix is only output at the beginning of the indexing expression, which
1233 // may have multiple parts.
1234 out << "angle";
1235 }
1236 if (!indexingReturnsSampler)
1237 {
1238 // All parts of an expression that access a sampler in a struct need to use _ as
1239 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001240 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001241 }
1242 if (visit == InVisit)
1243 {
1244 if (indexingReturnsSampler)
1245 {
1246 out << "_" + field->name();
1247 }
1248 else
1249 {
1250 out << "." + DecorateField(field->name(), *structure);
1251 }
1252
1253 return false;
1254 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001255 }
1256 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001257 case EOpIndexDirectInterfaceBlock:
1258 if (visit == InVisit)
1259 {
1260 const TInterfaceBlock *interfaceBlock =
1261 node->getLeft()->getType().getInterfaceBlock();
1262 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1263 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
1264 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001265
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001266 return false;
1267 }
1268 break;
1269 case EOpAdd:
1270 outputTriplet(out, visit, "(", " + ", ")");
1271 break;
1272 case EOpSub:
1273 outputTriplet(out, visit, "(", " - ", ")");
1274 break;
1275 case EOpMul:
1276 outputTriplet(out, visit, "(", " * ", ")");
1277 break;
1278 case EOpDiv:
1279 outputTriplet(out, visit, "(", " / ", ")");
1280 break;
1281 case EOpIMod:
1282 outputTriplet(out, visit, "(", " % ", ")");
1283 break;
1284 case EOpBitShiftLeft:
1285 outputTriplet(out, visit, "(", " << ", ")");
1286 break;
1287 case EOpBitShiftRight:
1288 outputTriplet(out, visit, "(", " >> ", ")");
1289 break;
1290 case EOpBitwiseAnd:
1291 outputTriplet(out, visit, "(", " & ", ")");
1292 break;
1293 case EOpBitwiseXor:
1294 outputTriplet(out, visit, "(", " ^ ", ")");
1295 break;
1296 case EOpBitwiseOr:
1297 outputTriplet(out, visit, "(", " | ", ")");
1298 break;
1299 case EOpEqual:
1300 case EOpNotEqual:
1301 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1302 break;
1303 case EOpLessThan:
1304 outputTriplet(out, visit, "(", " < ", ")");
1305 break;
1306 case EOpGreaterThan:
1307 outputTriplet(out, visit, "(", " > ", ")");
1308 break;
1309 case EOpLessThanEqual:
1310 outputTriplet(out, visit, "(", " <= ", ")");
1311 break;
1312 case EOpGreaterThanEqual:
1313 outputTriplet(out, visit, "(", " >= ", ")");
1314 break;
1315 case EOpVectorTimesScalar:
1316 outputTriplet(out, visit, "(", " * ", ")");
1317 break;
1318 case EOpMatrixTimesScalar:
1319 outputTriplet(out, visit, "(", " * ", ")");
1320 break;
1321 case EOpVectorTimesMatrix:
1322 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1323 break;
1324 case EOpMatrixTimesVector:
1325 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1326 break;
1327 case EOpMatrixTimesMatrix:
1328 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1329 break;
1330 case EOpLogicalOr:
1331 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1332 // been unfolded.
1333 ASSERT(!node->getRight()->hasSideEffects());
1334 outputTriplet(out, visit, "(", " || ", ")");
1335 return true;
1336 case EOpLogicalXor:
1337 mUsesXor = true;
1338 outputTriplet(out, visit, "xor(", ", ", ")");
1339 break;
1340 case EOpLogicalAnd:
1341 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1342 // been unfolded.
1343 ASSERT(!node->getRight()->hasSideEffects());
1344 outputTriplet(out, visit, "(", " && ", ")");
1345 return true;
1346 default:
1347 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001348 }
1349
1350 return true;
1351}
1352
1353bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1354{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001355 TInfoSinkBase &out = getInfoSink();
1356
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357 switch (node->getOp())
1358 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001359 case EOpNegative:
1360 outputTriplet(out, visit, "(-", "", ")");
1361 break;
1362 case EOpPositive:
1363 outputTriplet(out, visit, "(+", "", ")");
1364 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001365 case EOpLogicalNot:
1366 outputTriplet(out, visit, "(!", "", ")");
1367 break;
1368 case EOpBitwiseNot:
1369 outputTriplet(out, visit, "(~", "", ")");
1370 break;
1371 case EOpPostIncrement:
1372 outputTriplet(out, visit, "(", "", "++)");
1373 break;
1374 case EOpPostDecrement:
1375 outputTriplet(out, visit, "(", "", "--)");
1376 break;
1377 case EOpPreIncrement:
1378 outputTriplet(out, visit, "(++", "", ")");
1379 break;
1380 case EOpPreDecrement:
1381 outputTriplet(out, visit, "(--", "", ")");
1382 break;
1383 case EOpRadians:
1384 outputTriplet(out, visit, "radians(", "", ")");
1385 break;
1386 case EOpDegrees:
1387 outputTriplet(out, visit, "degrees(", "", ")");
1388 break;
1389 case EOpSin:
1390 outputTriplet(out, visit, "sin(", "", ")");
1391 break;
1392 case EOpCos:
1393 outputTriplet(out, visit, "cos(", "", ")");
1394 break;
1395 case EOpTan:
1396 outputTriplet(out, visit, "tan(", "", ")");
1397 break;
1398 case EOpAsin:
1399 outputTriplet(out, visit, "asin(", "", ")");
1400 break;
1401 case EOpAcos:
1402 outputTriplet(out, visit, "acos(", "", ")");
1403 break;
1404 case EOpAtan:
1405 outputTriplet(out, visit, "atan(", "", ")");
1406 break;
1407 case EOpSinh:
1408 outputTriplet(out, visit, "sinh(", "", ")");
1409 break;
1410 case EOpCosh:
1411 outputTriplet(out, visit, "cosh(", "", ")");
1412 break;
1413 case EOpTanh:
1414 outputTriplet(out, visit, "tanh(", "", ")");
1415 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001416 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001417 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001418 case EOpAtanh:
1419 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001420 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001421 break;
1422 case EOpExp:
1423 outputTriplet(out, visit, "exp(", "", ")");
1424 break;
1425 case EOpLog:
1426 outputTriplet(out, visit, "log(", "", ")");
1427 break;
1428 case EOpExp2:
1429 outputTriplet(out, visit, "exp2(", "", ")");
1430 break;
1431 case EOpLog2:
1432 outputTriplet(out, visit, "log2(", "", ")");
1433 break;
1434 case EOpSqrt:
1435 outputTriplet(out, visit, "sqrt(", "", ")");
1436 break;
1437 case EOpInverseSqrt:
1438 outputTriplet(out, visit, "rsqrt(", "", ")");
1439 break;
1440 case EOpAbs:
1441 outputTriplet(out, visit, "abs(", "", ")");
1442 break;
1443 case EOpSign:
1444 outputTriplet(out, visit, "sign(", "", ")");
1445 break;
1446 case EOpFloor:
1447 outputTriplet(out, visit, "floor(", "", ")");
1448 break;
1449 case EOpTrunc:
1450 outputTriplet(out, visit, "trunc(", "", ")");
1451 break;
1452 case EOpRound:
1453 outputTriplet(out, visit, "round(", "", ")");
1454 break;
1455 case EOpRoundEven:
1456 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001457 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001458 break;
1459 case EOpCeil:
1460 outputTriplet(out, visit, "ceil(", "", ")");
1461 break;
1462 case EOpFract:
1463 outputTriplet(out, visit, "frac(", "", ")");
1464 break;
1465 case EOpIsNan:
1466 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001467 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001468 else
1469 outputTriplet(out, visit, "isnan(", "", ")");
1470 mRequiresIEEEStrictCompiling = true;
1471 break;
1472 case EOpIsInf:
1473 outputTriplet(out, visit, "isinf(", "", ")");
1474 break;
1475 case EOpFloatBitsToInt:
1476 outputTriplet(out, visit, "asint(", "", ")");
1477 break;
1478 case EOpFloatBitsToUint:
1479 outputTriplet(out, visit, "asuint(", "", ")");
1480 break;
1481 case EOpIntBitsToFloat:
1482 outputTriplet(out, visit, "asfloat(", "", ")");
1483 break;
1484 case EOpUintBitsToFloat:
1485 outputTriplet(out, visit, "asfloat(", "", ")");
1486 break;
1487 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001488 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001489 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001490 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001491 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001492 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001493 case EOpPackUnorm4x8:
1494 case EOpPackSnorm4x8:
1495 case EOpUnpackUnorm4x8:
1496 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001497 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001498 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001499 break;
1500 case EOpLength:
1501 outputTriplet(out, visit, "length(", "", ")");
1502 break;
1503 case EOpNormalize:
1504 outputTriplet(out, visit, "normalize(", "", ")");
1505 break;
1506 case EOpDFdx:
1507 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1508 {
1509 outputTriplet(out, visit, "(", "", ", 0.0)");
1510 }
1511 else
1512 {
1513 outputTriplet(out, visit, "ddx(", "", ")");
1514 }
1515 break;
1516 case EOpDFdy:
1517 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1518 {
1519 outputTriplet(out, visit, "(", "", ", 0.0)");
1520 }
1521 else
1522 {
1523 outputTriplet(out, visit, "ddy(", "", ")");
1524 }
1525 break;
1526 case EOpFwidth:
1527 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1528 {
1529 outputTriplet(out, visit, "(", "", ", 0.0)");
1530 }
1531 else
1532 {
1533 outputTriplet(out, visit, "fwidth(", "", ")");
1534 }
1535 break;
1536 case EOpTranspose:
1537 outputTriplet(out, visit, "transpose(", "", ")");
1538 break;
1539 case EOpDeterminant:
1540 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1541 break;
1542 case EOpInverse:
1543 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001544 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001545 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001546
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001547 case EOpAny:
1548 outputTriplet(out, visit, "any(", "", ")");
1549 break;
1550 case EOpAll:
1551 outputTriplet(out, visit, "all(", "", ")");
1552 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001553 case EOpLogicalNotComponentWise:
1554 outputTriplet(out, visit, "(!", "", ")");
1555 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001556 case EOpBitfieldReverse:
1557 outputTriplet(out, visit, "reversebits(", "", ")");
1558 break;
1559 case EOpBitCount:
1560 outputTriplet(out, visit, "countbits(", "", ")");
1561 break;
1562 case EOpFindLSB:
1563 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1564 // in GLSLTest and results are consistent with GL.
1565 outputTriplet(out, visit, "firstbitlow(", "", ")");
1566 break;
1567 case EOpFindMSB:
1568 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1569 // tested in GLSLTest and results are consistent with GL.
1570 outputTriplet(out, visit, "firstbithigh(", "", ")");
1571 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001572 default:
1573 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001574 }
1575
1576 return true;
1577}
1578
Olli Etuaho96963162016-03-21 11:54:33 +02001579TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1580{
1581 if (node->getAsSymbolNode())
1582 {
1583 return node->getAsSymbolNode()->getSymbol();
1584 }
1585 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1586 switch (nodeBinary->getOp())
1587 {
1588 case EOpIndexDirect:
1589 {
1590 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1591
1592 TInfoSinkBase prefixSink;
1593 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1594 return TString(prefixSink.c_str());
1595 }
1596 case EOpIndexDirectStruct:
1597 {
1598 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1599 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1600 const TField *field = s->fields()[index];
1601
1602 TInfoSinkBase prefixSink;
1603 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1604 << field->name();
1605 return TString(prefixSink.c_str());
1606 }
1607 default:
1608 UNREACHABLE();
1609 return TString("");
1610 }
1611}
1612
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001613bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1614{
1615 TInfoSinkBase &out = getInfoSink();
1616
1617 if (mInsideFunction)
1618 {
1619 outputLineDirective(out, node->getLine().first_line);
1620 out << "{\n";
1621 }
1622
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001623 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001624 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001625 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001626
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001627 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001628
1629 // Don't output ; after case labels, they're terminated by :
1630 // This is needed especially since outputting a ; after a case statement would turn empty
1631 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001632 // Also the output code is clearer if we don't output ; after statements where it is not
1633 // needed:
1634 // * if statements
1635 // * switch statements
1636 // * blocks
1637 // * function definitions
1638 // * loops (do-while loops output the semicolon in VisitLoop)
1639 // * declarations that don't generate output.
1640 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1641 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1642 statement->getAsSwitchNode() == nullptr &&
1643 statement->getAsFunctionDefinition() == nullptr &&
1644 (statement->getAsDeclarationNode() == nullptr ||
1645 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1646 statement->getAsInvariantDeclarationNode() == nullptr)
1647 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001648 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001649 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001650 }
1651
1652 if (mInsideFunction)
1653 {
1654 outputLineDirective(out, node->getLine().last_line);
1655 out << "}\n";
1656 }
1657
1658 return false;
1659}
1660
Olli Etuaho336b1472016-10-05 16:37:55 +01001661bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1662{
1663 TInfoSinkBase &out = getInfoSink();
1664
1665 ASSERT(mCurrentFunctionMetadata == nullptr);
1666
1667 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1668 ASSERT(index != CallDAG::InvalidIndex);
1669 mCurrentFunctionMetadata = &mASTMetadataList[index];
1670
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001671 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001672
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001673 TIntermSequence *parameters = node->getFunctionPrototype()->getSequence();
Olli Etuaho336b1472016-10-05 16:37:55 +01001674
1675 if (node->getFunctionSymbolInfo()->isMain())
1676 {
1677 out << "gl_main(";
1678 }
1679 else
1680 {
Olli Etuahoff526f12017-06-30 12:26:54 +03001681 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
Olli Etuaho336b1472016-10-05 16:37:55 +01001682 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1683 }
1684
1685 for (unsigned int i = 0; i < parameters->size(); i++)
1686 {
1687 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1688
1689 if (symbol)
1690 {
1691 ensureStructDefined(symbol->getType());
1692
1693 out << argumentString(symbol);
1694
1695 if (i < parameters->size() - 1)
1696 {
1697 out << ", ";
1698 }
1699 }
1700 else
1701 UNREACHABLE();
1702 }
1703
1704 out << ")\n";
1705
1706 mInsideFunction = true;
1707 // The function body node will output braces.
1708 node->getBody()->traverse(this);
1709 mInsideFunction = false;
1710
1711 mCurrentFunctionMetadata = nullptr;
1712
1713 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1714 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1715 {
1716 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1717 mOutputLod0Function = true;
1718 node->traverse(this);
1719 mOutputLod0Function = false;
1720 }
1721
1722 return false;
1723}
1724
Olli Etuaho13389b62016-10-16 11:48:18 +01001725bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1726{
Olli Etuaho13389b62016-10-16 11:48:18 +01001727 if (visit == PreVisit)
1728 {
1729 TIntermSequence *sequence = node->getSequence();
1730 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1731 ASSERT(sequence->size() == 1);
Olli Etuaho282847e2017-07-12 14:11:01 +03001732 ASSERT(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01001733
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001734 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001735 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001736 TInfoSinkBase &out = getInfoSink();
Olli Etuaho13389b62016-10-16 11:48:18 +01001737 ensureStructDefined(variable->getType());
1738
1739 if (!variable->getAsSymbolNode() ||
1740 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1741 {
1742 if (!mInsideFunction)
1743 {
1744 out << "static ";
1745 }
1746
1747 out << TypeString(variable->getType()) + " ";
1748
1749 TIntermSymbol *symbol = variable->getAsSymbolNode();
1750
1751 if (symbol)
1752 {
1753 symbol->traverse(this);
1754 out << ArrayString(symbol->getType());
1755 out << " = " + initializer(symbol->getType());
1756 }
1757 else
1758 {
1759 variable->traverse(this);
1760 }
1761 }
1762 else if (variable->getAsSymbolNode() &&
1763 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1764 {
1765 // Already added to constructor map
1766 }
1767 else
1768 UNREACHABLE();
1769 }
Olli Etuaho282847e2017-07-12 14:11:01 +03001770 else if (IsVaryingOut(variable->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001771 {
Olli Etuaho282847e2017-07-12 14:11:01 +03001772 TIntermSymbol *symbol = variable->getAsSymbolNode();
1773 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001774
Olli Etuaho282847e2017-07-12 14:11:01 +03001775 // Vertex outputs which are declared but not written to should still be declared to
1776 // allow successful linking.
1777 mReferencedVaryings[symbol->getSymbol()] = symbol;
Olli Etuaho13389b62016-10-16 11:48:18 +01001778 }
1779 }
1780 return false;
1781}
1782
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001783bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1784{
1785 // Do not do any translation
1786 return false;
1787}
1788
Olli Etuaho16c745a2017-01-16 17:02:27 +00001789bool OutputHLSL::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
1790{
1791 TInfoSinkBase &out = getInfoSink();
1792
1793 ASSERT(visit == PreVisit);
1794 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1795 // Skip the prototype if it is not implemented (and thus not used)
1796 if (index == CallDAG::InvalidIndex)
1797 {
1798 return false;
1799 }
1800
1801 TIntermSequence *arguments = node->getSequence();
1802
Olli Etuahoff526f12017-06-30 12:26:54 +03001803 TString name = DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001804 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1805 << (mOutputLod0Function ? "Lod0(" : "(");
1806
1807 for (unsigned int i = 0; i < arguments->size(); i++)
1808 {
1809 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1810 ASSERT(symbol != nullptr);
1811
1812 out << argumentString(symbol);
1813
1814 if (i < arguments->size() - 1)
1815 {
1816 out << ", ";
1817 }
1818 }
1819
1820 out << ");\n";
1821
1822 // Also prototype the Lod0 variant if needed
1823 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1824 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1825 {
1826 mOutputLod0Function = true;
1827 node->traverse(this);
1828 mOutputLod0Function = false;
1829 }
1830
1831 return false;
1832}
1833
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001834bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1835{
Jamie Madill32aab012015-01-27 14:12:26 -05001836 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838 switch (node->getOp())
1839 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001840 case EOpCallBuiltInFunction:
1841 case EOpCallFunctionInAST:
1842 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001843 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001844 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001845
Corentin Wallez1239ee92015-03-19 14:38:02 -07001846 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001847 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001848 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001849 if (node->isArray())
1850 {
1851 UNIMPLEMENTED();
1852 }
Olli Etuahobd674552016-10-06 13:28:42 +01001853 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001854 ASSERT(index != CallDAG::InvalidIndex);
1855 lod0 &= mASTMetadataList[index].mNeedsLod0;
1856
Olli Etuahoff526f12017-06-30 12:26:54 +03001857 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001858 out << DisambiguateFunctionName(node->getSequence());
1859 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001860 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001861 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001862 {
1863 // This path is used for internal functions that don't have their definitions in the
1864 // AST, such as precision emulation functions.
Olli Etuahoff526f12017-06-30 12:26:54 +03001865 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001866 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001867 else
1868 {
Olli Etuahoec9232b2017-03-27 17:01:37 +03001869 const TString &name = node->getFunctionSymbolInfo()->getName();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001870 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001871 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1872 if (arguments->size() > 1)
1873 {
1874 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1875 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001876 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1877 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1878 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001879 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001880
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001881 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001882 {
Olli Etuaho96963162016-03-21 11:54:33 +02001883 TIntermTyped *typedArg = (*arg)->getAsTyped();
1884 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001885 {
1886 out << "texture_";
1887 (*arg)->traverse(this);
1888 out << ", sampler_";
1889 }
1890
1891 (*arg)->traverse(this);
1892
Olli Etuaho96963162016-03-21 11:54:33 +02001893 if (typedArg->getType().isStructureContainingSamplers())
1894 {
1895 const TType &argType = typedArg->getType();
1896 TVector<TIntermSymbol *> samplerSymbols;
1897 TString structName = samplerNamePrefixFromStruct(typedArg);
1898 argType.createSamplerSymbols("angle_" + structName, "",
Olli Etuaho96963162016-03-21 11:54:33 +02001899 &samplerSymbols, nullptr);
1900 for (const TIntermSymbol *sampler : samplerSymbols)
1901 {
1902 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1903 {
1904 out << ", texture_" << sampler->getSymbol();
1905 out << ", sampler_" << sampler->getSymbol();
1906 }
1907 else
1908 {
1909 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1910 // of D3D9, it's the sampler variable.
1911 out << ", " + sampler->getSymbol();
1912 }
1913 }
1914 }
1915
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001916 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001917 {
1918 out << ", ";
1919 }
1920 }
1921
1922 out << ")";
1923
1924 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03001926 case EOpConstruct:
1927 if (node->getBasicType() == EbtStruct)
Olli Etuahof40319e2015-03-10 14:33:00 +02001928 {
Olli Etuaho8fab3202017-05-08 18:22:22 +03001929 if (node->getType().isArray())
1930 {
1931 UNIMPLEMENTED();
1932 }
1933 const TString &structName = StructNameString(*node->getType().getStruct());
1934 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
1935 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Olli Etuahof40319e2015-03-10 14:33:00 +02001936 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03001937 else
1938 {
1939 const char *name = "";
1940 if (node->getType().getNominalSize() == 1)
1941 {
1942 switch (node->getBasicType())
1943 {
1944 case EbtFloat:
1945 name = "vec1";
1946 break;
1947 case EbtInt:
1948 name = "ivec1";
1949 break;
1950 case EbtUInt:
1951 name = "uvec1";
1952 break;
1953 case EbtBool:
1954 name = "bvec1";
1955 break;
1956 default:
1957 UNREACHABLE();
1958 }
1959 }
1960 else
1961 {
1962 name = node->getType().getBuiltInTypeNameString();
1963 }
1964 outputConstructor(out, visit, node->getType(), name, node->getSequence());
1965 }
1966 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001967 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001968 outputTriplet(out, visit, "(", " == ", ")");
1969 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001970 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001971 outputTriplet(out, visit, "(", " != ", ")");
1972 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001973 case EOpLessThanComponentWise:
1974 outputTriplet(out, visit, "(", " < ", ")");
1975 break;
1976 case EOpGreaterThanComponentWise:
1977 outputTriplet(out, visit, "(", " > ", ")");
1978 break;
1979 case EOpLessThanEqualComponentWise:
1980 outputTriplet(out, visit, "(", " <= ", ")");
1981 break;
1982 case EOpGreaterThanEqualComponentWise:
1983 outputTriplet(out, visit, "(", " >= ", ")");
1984 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01001985 case EOpMod:
1986 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001987 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001988 break;
1989 case EOpModf:
1990 outputTriplet(out, visit, "modf(", ", ", ")");
1991 break;
1992 case EOpPow:
1993 outputTriplet(out, visit, "pow(", ", ", ")");
1994 break;
1995 case EOpAtan:
1996 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
1997 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001998 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01001999 break;
2000 case EOpMin:
2001 outputTriplet(out, visit, "min(", ", ", ")");
2002 break;
2003 case EOpMax:
2004 outputTriplet(out, visit, "max(", ", ", ")");
2005 break;
2006 case EOpClamp:
2007 outputTriplet(out, visit, "clamp(", ", ", ")");
2008 break;
2009 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302010 {
2011 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2012 if (lastParamNode->getType().getBasicType() == EbtBool)
2013 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002014 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2015 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302016 // so use emulated version.
2017 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002018 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302019 }
2020 else
2021 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002022 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302023 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002024 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302025 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002026 case EOpStep:
2027 outputTriplet(out, visit, "step(", ", ", ")");
2028 break;
2029 case EOpSmoothStep:
2030 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2031 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002032 case EOpFrexp:
2033 case EOpLdexp:
2034 ASSERT(node->getUseEmulatedFunction());
2035 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2036 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002037 case EOpDistance:
2038 outputTriplet(out, visit, "distance(", ", ", ")");
2039 break;
2040 case EOpDot:
2041 outputTriplet(out, visit, "dot(", ", ", ")");
2042 break;
2043 case EOpCross:
2044 outputTriplet(out, visit, "cross(", ", ", ")");
2045 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002046 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002047 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002048 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002049 break;
2050 case EOpReflect:
2051 outputTriplet(out, visit, "reflect(", ", ", ")");
2052 break;
2053 case EOpRefract:
2054 outputTriplet(out, visit, "refract(", ", ", ")");
2055 break;
2056 case EOpOuterProduct:
2057 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002058 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002059 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002060 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002061 outputTriplet(out, visit, "(", " * ", ")");
2062 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002063 case EOpBitfieldExtract:
2064 case EOpBitfieldInsert:
2065 case EOpUaddCarry:
2066 case EOpUsubBorrow:
2067 case EOpUmulExtended:
2068 case EOpImulExtended:
2069 ASSERT(node->getUseEmulatedFunction());
2070 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2071 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002072 default:
2073 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002074 }
2075
2076 return true;
2077}
2078
Olli Etuaho57961272016-09-14 13:57:46 +03002079void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002081 out << "if (";
2082
2083 node->getCondition()->traverse(this);
2084
2085 out << ")\n";
2086
Jamie Madill8c46ab12015-12-07 16:39:19 -05002087 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002088
2089 bool discard = false;
2090
2091 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002093 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002094 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002095
Olli Etuahoa6f22092015-05-08 18:31:10 +03002096 // Detect true discard
2097 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2098 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002099 else
2100 {
2101 // TODO(oetuaho): Check if the semicolon inside is necessary.
2102 // It's there as a result of conservative refactoring of the output.
2103 out << "{;}\n";
2104 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002105
Jamie Madill8c46ab12015-12-07 16:39:19 -05002106 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002107
Olli Etuahoa6f22092015-05-08 18:31:10 +03002108 if (node->getFalseBlock())
2109 {
2110 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002111
Jamie Madill8c46ab12015-12-07 16:39:19 -05002112 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002113
Olli Etuaho32db19b2016-10-04 14:43:16 +01002114 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002115 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002116
Jamie Madill8c46ab12015-12-07 16:39:19 -05002117 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002118
Olli Etuahoa6f22092015-05-08 18:31:10 +03002119 // Detect false discard
2120 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2121 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002122
Olli Etuahoa6f22092015-05-08 18:31:10 +03002123 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002124 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002125 {
2126 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002127 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002128}
2129
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002130bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2131{
2132 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2133 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2134 UNREACHABLE();
2135 return false;
2136}
2137
Olli Etuaho57961272016-09-14 13:57:46 +03002138bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002139{
2140 TInfoSinkBase &out = getInfoSink();
2141
Olli Etuaho3d932d82016-04-12 11:10:30 +03002142 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002143
2144 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002145 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002146 {
2147 out << "FLATTEN ";
2148 }
2149
Olli Etuaho57961272016-09-14 13:57:46 +03002150 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151
2152 return false;
2153}
2154
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002155bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002156{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002157 TInfoSinkBase &out = getInfoSink();
2158
Olli Etuaho923ecef2017-10-11 12:01:38 +03002159 ASSERT(node->getStatementList());
2160 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002161 {
Olli Etuaho923ecef2017-10-11 12:01:38 +03002162 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList()));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002163 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002164 outputTriplet(out, visit, "switch (", ") ", "");
2165 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002166 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002167}
2168
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002169bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002170{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002171 TInfoSinkBase &out = getInfoSink();
2172
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002173 if (node->hasCondition())
2174 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002175 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002176 return true;
2177 }
2178 else
2179 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002180 out << "default:\n";
2181 return false;
2182 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002183}
2184
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2186{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002187 TInfoSinkBase &out = getInfoSink();
2188 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189}
2190
2191bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2192{
Nicolas Capens655fe362014-04-11 13:12:34 -04002193 mNestedLoopDepth++;
2194
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002195 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002196 mInsideDiscontinuousLoop =
2197 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002198
Jamie Madill8c46ab12015-12-07 16:39:19 -05002199 TInfoSinkBase &out = getInfoSink();
2200
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002201 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002202 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002203 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002204 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002205 mInsideDiscontinuousLoop = wasDiscontinuous;
2206 mNestedLoopDepth--;
2207
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002208 return false;
2209 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002210 }
2211
Corentin Wallez1239ee92015-03-19 14:38:02 -07002212 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002213 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002214 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002215 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002216
Jamie Madill8c46ab12015-12-07 16:39:19 -05002217 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002218 }
2219 else
2220 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002221 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002222
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002223 if (node->getInit())
2224 {
2225 node->getInit()->traverse(this);
2226 }
2227
2228 out << "; ";
2229
alokp@chromium.org52813552010-11-16 18:36:09 +00002230 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002231 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002232 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002233 }
2234
2235 out << "; ";
2236
alokp@chromium.org52813552010-11-16 18:36:09 +00002237 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002239 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002240 }
2241
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002242 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002243
Jamie Madill8c46ab12015-12-07 16:39:19 -05002244 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245 }
2246
2247 if (node->getBody())
2248 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002249 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002250 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002252 else
2253 {
2254 // TODO(oetuaho): Check if the semicolon inside is necessary.
2255 // It's there as a result of conservative refactoring of the output.
2256 out << "{;}\n";
2257 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258
Jamie Madill8c46ab12015-12-07 16:39:19 -05002259 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002260
alokp@chromium.org52813552010-11-16 18:36:09 +00002261 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002263 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002264 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265
alokp@chromium.org52813552010-11-16 18:36:09 +00002266 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002268 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 }
2270
daniel@transgaming.com73536982012-03-21 20:45:49 +00002271 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002273 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002274 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002275
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 return false;
2277}
2278
2279bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2280{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002281 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002283 TInfoSinkBase &out = getInfoSink();
2284
2285 switch (node->getFlowOp())
2286 {
2287 case EOpKill:
2288 out << "discard";
2289 break;
2290 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002291 if (mNestedLoopDepth > 1)
2292 {
2293 mUsesNestedBreak = true;
2294 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002295
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002296 if (mExcessiveLoopIndex)
2297 {
2298 out << "{Break";
2299 mExcessiveLoopIndex->traverse(this);
2300 out << " = true; break;}\n";
2301 }
2302 else
2303 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002304 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002305 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002306 break;
2307 case EOpContinue:
2308 out << "continue";
2309 break;
2310 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002311 if (node->getExpression())
2312 {
2313 out << "return ";
2314 }
2315 else
2316 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002317 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002318 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002319 break;
2320 default:
2321 UNREACHABLE();
2322 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323 }
2324
2325 return true;
2326}
2327
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002328// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002329// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2330// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002331bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002332{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002333 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002334
2335 // Parse loops of the form:
2336 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002337 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002338 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002339 int initial = 0;
2340 int limit = 0;
2341 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002342
2343 // Parse index name and intial value
2344 if (node->getInit())
2345 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002346 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002347
2348 if (init)
2349 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002350 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002351 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002352
2353 if (variable && variable->getQualifier() == EvqTemporary)
2354 {
2355 TIntermBinary *assign = variable->getAsBinaryNode();
2356
2357 if (assign->getOp() == EOpInitialize)
2358 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002359 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002360 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2361
2362 if (symbol && constant)
2363 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002364 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002365 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002366 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002367 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002368 }
2369 }
2370 }
2371 }
2372 }
2373 }
2374
2375 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002376 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002377 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002378 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002379
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002380 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2381 {
2382 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2383
2384 if (constant)
2385 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002386 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002387 {
2388 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002389 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002390 }
2391 }
2392 }
2393 }
2394
2395 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002396 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002397 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002398 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002399 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002400
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401 if (binaryTerminal)
2402 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002403 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002404 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2405
2406 if (constant)
2407 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002408 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002409 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002410 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002411
2412 switch (op)
2413 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002414 case EOpAddAssign:
2415 increment = value;
2416 break;
2417 case EOpSubAssign:
2418 increment = -value;
2419 break;
2420 default:
2421 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002422 }
2423 }
2424 }
2425 }
2426 else if (unaryTerminal)
2427 {
2428 TOperator op = unaryTerminal->getOp();
2429
2430 switch (op)
2431 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002432 case EOpPostIncrement:
2433 increment = 1;
2434 break;
2435 case EOpPostDecrement:
2436 increment = -1;
2437 break;
2438 case EOpPreIncrement:
2439 increment = 1;
2440 break;
2441 case EOpPreDecrement:
2442 increment = -1;
2443 break;
2444 default:
2445 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002446 }
2447 }
2448 }
2449
Yunchao He4f285442017-04-21 12:15:49 +08002450 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002451 {
2452 if (comparator == EOpLessThanEqual)
2453 {
2454 comparator = EOpLessThan;
2455 limit += 1;
2456 }
2457
2458 if (comparator == EOpLessThan)
2459 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002460 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002461
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002462 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002463 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002464 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002465 }
2466
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002467 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002468 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002469
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002470 out << "{int ";
2471 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002472 out << ";\n"
2473 "bool Break";
2474 index->traverse(this);
2475 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002476
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002477 bool firstLoopFragment = true;
2478
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002479 while (iterations > 0)
2480 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002481 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002482
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002483 if (!firstLoopFragment)
2484 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002485 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002486 index->traverse(this);
2487 out << ") {\n";
2488 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002489
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002490 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002491 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002492 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002493 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002494
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002495 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002496 const char *unroll =
2497 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002498
Corentin Wallez1239ee92015-03-19 14:38:02 -07002499 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002500 index->traverse(this);
2501 out << " = ";
2502 out << initial;
2503
2504 out << "; ";
2505 index->traverse(this);
2506 out << " < ";
2507 out << clampedLimit;
2508
2509 out << "; ";
2510 index->traverse(this);
2511 out << " += ";
2512 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002513 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002514
Jamie Madill8c46ab12015-12-07 16:39:19 -05002515 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002516 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002517
2518 if (node->getBody())
2519 {
2520 node->getBody()->traverse(this);
2521 }
2522
Jamie Madill8c46ab12015-12-07 16:39:19 -05002523 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002524 out << ";}\n";
2525
2526 if (!firstLoopFragment)
2527 {
2528 out << "}\n";
2529 }
2530
2531 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002532
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002533 initial += MAX_LOOP_ITERATIONS * increment;
2534 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002535 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002536
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002537 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002538
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002539 mExcessiveLoopIndex = restoreIndex;
2540
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002541 return true;
2542 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002543 else
2544 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002545 }
2546
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002547 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002548}
2549
Jamie Madill8c46ab12015-12-07 16:39:19 -05002550void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2551 Visit visit,
2552 const char *preString,
2553 const char *inString,
2554 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002555{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002556 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002557 {
2558 out << preString;
2559 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002560 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561 {
2562 out << inString;
2563 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002564 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565 {
2566 out << postString;
2567 }
2568}
2569
Jamie Madill8c46ab12015-12-07 16:39:19 -05002570void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002571{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002572 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002573 {
Jamie Madill32aab012015-01-27 14:12:26 -05002574 out << "\n";
2575 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002576
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002577 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002578 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002579 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002580 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002581
Jamie Madill32aab012015-01-27 14:12:26 -05002582 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002583 }
2584}
2585
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002586TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2587{
2588 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002589 const TType &type = symbol->getType();
2590 const TName &name = symbol->getName();
2591 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002592
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002593 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002594 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002595 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002596 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002597 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002598 {
Olli Etuahoff526f12017-06-30 12:26:54 +03002599 nameStr = DecorateVariableIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002600 }
2601
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002602 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002603 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002604 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2605 {
2606 // Samplers are passed as indices to the sampler array.
2607 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2608 return "const uint " + nameStr + ArrayString(type);
2609 }
2610 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2611 {
2612 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2613 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2614 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2615 ArrayString(type);
2616 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002617 }
2618
Olli Etuaho96963162016-03-21 11:54:33 +02002619 TStringStream argString;
2620 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2621 << ArrayString(type);
2622
2623 // If the structure parameter contains samplers, they need to be passed into the function as
2624 // separate parameters. HLSL doesn't natively support samplers in structs.
2625 if (type.isStructureContainingSamplers())
2626 {
2627 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2628 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho599555b2017-08-15 11:12:42 +03002629 type.createSamplerSymbols("angle" + nameStr, "", &samplerSymbols, nullptr);
Olli Etuaho96963162016-03-21 11:54:33 +02002630 for (const TIntermSymbol *sampler : samplerSymbols)
2631 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002632 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002633 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2634 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002635 argString << ", const uint " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002636 }
2637 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2638 {
Olli Etuaho96963162016-03-21 11:54:33 +02002639 ASSERT(IsSampler(samplerType.getBasicType()));
2640 argString << ", " << QualifierString(qualifier) << " "
2641 << TextureString(samplerType.getBasicType()) << " texture_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002642 << sampler->getSymbol() << ArrayString(samplerType) << ", "
2643 << QualifierString(qualifier) << " "
Olli Etuaho96963162016-03-21 11:54:33 +02002644 << SamplerString(samplerType.getBasicType()) << " sampler_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002645 << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002646 }
2647 else
2648 {
Olli Etuaho96963162016-03-21 11:54:33 +02002649 ASSERT(IsSampler(samplerType.getBasicType()));
2650 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
Olli Etuaho28839f02017-08-15 11:38:16 +03002651 << " " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002652 }
2653 }
2654 }
2655
2656 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002657}
2658
2659TString OutputHLSL::initializer(const TType &type)
2660{
2661 TString string;
2662
Jamie Madill94bf7f22013-07-08 13:31:15 -04002663 size_t size = type.getObjectSize();
2664 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002665 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002666 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002667
Jamie Madill94bf7f22013-07-08 13:31:15 -04002668 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002669 {
2670 string += ", ";
2671 }
2672 }
2673
daniel@transgaming.comead23042010-04-29 03:35:36 +00002674 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002675}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002676
Jamie Madill8c46ab12015-12-07 16:39:19 -05002677void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2678 Visit visit,
2679 const TType &type,
2680 const char *name,
2681 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002682{
Olli Etuahof40319e2015-03-10 14:33:00 +02002683 if (type.isArray())
2684 {
2685 UNIMPLEMENTED();
2686 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002687
2688 if (visit == PreVisit)
2689 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002690 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002691
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002692 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002693 }
2694 else if (visit == InVisit)
2695 {
2696 out << ", ";
2697 }
2698 else if (visit == PostVisit)
2699 {
2700 out << ")";
2701 }
2702}
2703
Jamie Madill8c46ab12015-12-07 16:39:19 -05002704const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2705 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002706 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002707{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002708 const TConstantUnion *constUnionIterated = constUnion;
2709
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002710 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002711 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002712 {
Jamie Madill033dae62014-06-18 12:56:28 -04002713 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002714
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002715 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002716
Jamie Madill98493dd2013-07-08 14:39:03 -04002717 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002718 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002719 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002720 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002721
Jamie Madill98493dd2013-07-08 14:39:03 -04002722 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002723 {
2724 out << ", ";
2725 }
2726 }
2727
2728 out << ")";
2729 }
2730 else
2731 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002732 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002733 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002734
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002735 if (writeType)
2736 {
Jamie Madill033dae62014-06-18 12:56:28 -04002737 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002738 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002739 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002740 if (writeType)
2741 {
2742 out << ")";
2743 }
2744 }
2745
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002746 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002747}
2748
Olli Etuahod68924e2017-01-02 17:34:40 +00002749void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002750{
Olli Etuahod68924e2017-01-02 17:34:40 +00002751 if (visit == PreVisit)
2752 {
2753 const char *opStr = GetOperatorString(op);
2754 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2755 out << "(";
2756 }
2757 else
2758 {
2759 outputTriplet(out, visit, nullptr, ", ", ")");
2760 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002761}
2762
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002763bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2764 TIntermSymbol *symbolNode,
2765 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002766{
2767 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2768 expression->traverse(&searchSymbol);
2769
2770 if (searchSymbol.foundMatch())
2771 {
2772 // Type already printed
2773 out << "t" + str(mUniqueIndex) + " = ";
2774 expression->traverse(this);
2775 out << ", ";
2776 symbolNode->traverse(this);
2777 out << " = t" + str(mUniqueIndex);
2778
2779 mUniqueIndex++;
2780 return true;
2781 }
2782
2783 return false;
2784}
2785
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002786bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2787{
2788 // We support writing constant unions and constructors that only take constant unions as
2789 // parameters as HLSL literals.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002790 return !expression->getType().isArrayOfArrays() &&
2791 (expression->getAsConstantUnion() ||
2792 expression->isConstructorWithOnlyConstantUnionParameters());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002793}
2794
2795bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2796 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002797 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002798{
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002799 if (canWriteAsHLSLLiteral(initializer))
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002800 {
2801 symbolNode->traverse(this);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002802 ASSERT(!symbolNode->getType().isArrayOfArrays());
2803 if (symbolNode->getType().isArray())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002804 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002805 out << "[" << symbolNode->getType().getOutermostArraySize() << "]";
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002806 }
2807 out << " = {";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002808 if (initializer->getAsConstantUnion())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002809 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002810 TIntermConstantUnion *nodeConst = initializer->getAsConstantUnion();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002811 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002812 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002813 }
2814 else
2815 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002816 TIntermAggregate *constructor = initializer->getAsAggregate();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002817 ASSERT(constructor != nullptr);
2818 for (TIntermNode *&node : *constructor->getSequence())
2819 {
2820 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2821 ASSERT(nodeConst);
2822 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002823 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002824 if (node != constructor->getSequence()->back())
2825 {
2826 out << ", ";
2827 }
2828 }
2829 }
2830 out << "}";
2831 return true;
2832 }
2833 return false;
2834}
2835
Jamie Madill55e79e02015-02-09 15:35:00 -05002836TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2837{
2838 const TFieldList &fields = structure.fields();
2839
2840 for (const auto &eqFunction : mStructEqualityFunctions)
2841 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002842 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002843 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002844 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002845 }
2846 }
2847
2848 const TString &structNameString = StructNameString(structure);
2849
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002850 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002851 function->structure = &structure;
2852 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002853
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002854 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002855
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002856 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2857 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002858 << "{\n"
2859 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002860
2861 for (size_t i = 0; i < fields.size(); i++)
2862 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002863 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002864 const TType *fieldType = field->type();
2865
2866 const TString &fieldNameA = "a." + Decorate(field->name());
2867 const TString &fieldNameB = "b." + Decorate(field->name());
2868
2869 if (i > 0)
2870 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002871 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002872 }
2873
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002874 fnOut << "(";
2875 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2876 fnOut << fieldNameA;
2877 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2878 fnOut << fieldNameB;
2879 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2880 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002881 }
2882
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002883 fnOut << ";\n"
2884 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002885
2886 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002887
2888 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002889 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002890
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002891 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002892}
2893
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002894TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002895{
2896 for (const auto &eqFunction : mArrayEqualityFunctions)
2897 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002898 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002899 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002900 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002901 }
2902 }
2903
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002904 TType elementType(type);
2905 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002906
Olli Etuaho12690762015-03-31 12:55:28 +03002907 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002908 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002909
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002910 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002911
2912 TInfoSinkBase fnOut;
2913
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002914 const TString &typeName = TypeString(type);
2915 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2916 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002917 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002918 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002919 << type.getOutermostArraySize()
2920 << "; ++i)\n"
2921 " {\n"
2922 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002923
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002924 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002925 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002926 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002927 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002928 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002929
2930 fnOut << ") { return false; }\n"
2931 " }\n"
2932 " return true;\n"
2933 "}\n";
2934
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002935 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002936
2937 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002938 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002939
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002940 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002941}
2942
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002943TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002944{
2945 for (const auto &assignFunction : mArrayAssignmentFunctions)
2946 {
2947 if (assignFunction.type == type)
2948 {
2949 return assignFunction.functionName;
2950 }
2951 }
2952
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002953 TType elementType(type);
2954 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03002955
2956 ArrayHelperFunction function;
2957 function.type = type;
2958
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002959 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03002960
2961 TInfoSinkBase fnOut;
2962
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002963 const TString &typeName = TypeString(type);
2964 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
2965 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002966 << "{\n"
2967 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002968 << type.getOutermostArraySize()
2969 << "; ++i)\n"
2970 " {\n"
2971 " ";
2972
2973 outputAssign(PreVisit, elementType, fnOut);
2974 fnOut << "a[i]";
2975 outputAssign(InVisit, elementType, fnOut);
2976 fnOut << "b[i]";
2977 outputAssign(PostVisit, elementType, fnOut);
2978
2979 fnOut << ";\n"
2980 " }\n"
2981 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03002982
2983 function.functionDefinition = fnOut.c_str();
2984
2985 mArrayAssignmentFunctions.push_back(function);
2986
2987 return function.functionName;
2988}
2989
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002990TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03002991{
2992 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
2993 {
2994 if (constructIntoFunction.type == type)
2995 {
2996 return constructIntoFunction.functionName;
2997 }
2998 }
2999
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003000 TType elementType(type);
3001 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003002
3003 ArrayHelperFunction function;
3004 function.type = type;
3005
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003006 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003007
3008 TInfoSinkBase fnOut;
3009
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003010 const TString &typeName = TypeString(type);
3011 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3012 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003013 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003014 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003015 }
3016 fnOut << ")\n"
3017 "{\n";
3018
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003019 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003020 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003021 fnOut << " ";
3022 outputAssign(PreVisit, elementType, fnOut);
3023 fnOut << "a[" << i << "]";
3024 outputAssign(InVisit, elementType, fnOut);
3025 fnOut << "b" << i;
3026 outputAssign(PostVisit, elementType, fnOut);
3027 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003028 }
3029 fnOut << "}\n";
3030
3031 function.functionDefinition = fnOut.c_str();
3032
3033 mArrayConstructIntoFunctions.push_back(function);
3034
3035 return function.functionName;
3036}
3037
Jamie Madill2e295e22015-04-29 10:41:33 -04003038void OutputHLSL::ensureStructDefined(const TType &type)
3039{
3040 TStructure *structure = type.getStruct();
3041
3042 if (structure)
3043 {
3044 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3045 }
3046}
3047
Jamie Madill45bcc782016-11-07 13:58:48 -05003048} // namespace sh