blob: 40372c6933a6e66e3325f45fa05b85470b57ca1e [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"
Xinghua Cao711b7a12017-10-09 13:38:12 +080018#include "compiler/translator/ImageFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#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 << "_";
Kai Ninomiya57ea5332017-11-22 14:04:48 -080041 if (type.isArray())
Olli Etuaho96f6adf2017-08-16 11:18:54 +030042 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -080043 for (unsigned int arraySize : *type.getArraySizes())
44 {
45 fnName << arraySize << "_";
46 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +030047 }
48 fnName << TypeString(type);
49 return fnName.str();
50}
51
Olli Etuaho40dbdd62017-10-13 13:34:19 +030052bool IsDeclarationWrittenOut(TIntermDeclaration *node)
53{
54 TIntermSequence *sequence = node->getSequence();
55 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
56 ASSERT(sequence->size() == 1);
57 ASSERT(variable);
58 return (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
59 variable->getQualifier() == EvqConst);
60}
61
Olli Etuaho2ef23e22017-11-01 16:39:11 +020062bool IsInStd140InterfaceBlock(TIntermTyped *node)
63{
64 TIntermBinary *binaryNode = node->getAsBinaryNode();
65
66 if (binaryNode)
67 {
68 return IsInStd140InterfaceBlock(binaryNode->getLeft());
69 }
70
71 const TType &type = node->getType();
72
73 // determine if we are in the standard layout
74 const TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
75 if (interfaceBlock)
76 {
77 return (interfaceBlock->blockStorage() == EbsStd140);
78 }
79
80 return false;
81}
82
Olli Etuaho96f6adf2017-08-16 11:18:54 +030083} // anonymous namespace
84
Olli Etuaho56a2f952016-12-08 12:16:27 +000085void OutputHLSL::writeFloat(TInfoSinkBase &out, float f)
Olli Etuaho4785fec2015-05-18 16:09:37 +030086{
Olli Etuaho56a2f952016-12-08 12:16:27 +000087 // This is known not to work for NaN on all drivers but make the best effort to output NaNs
88 // regardless.
89 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300 &&
90 mOutputType == SH_HLSL_4_1_OUTPUT)
91 {
92 out << "asfloat(" << gl::bitCast<uint32_t>(f) << "u)";
93 }
94 else
95 {
96 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
97 }
98}
Olli Etuaho4785fec2015-05-18 16:09:37 +030099
Olli Etuaho56a2f952016-12-08 12:16:27 +0000100void OutputHLSL::writeSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200101{
102 ASSERT(constUnion != nullptr);
103 switch (constUnion->getType())
104 {
105 case EbtFloat:
Olli Etuaho56a2f952016-12-08 12:16:27 +0000106 writeFloat(out, constUnion->getFConst());
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200107 break;
108 case EbtInt:
109 out << constUnion->getIConst();
110 break;
111 case EbtUInt:
112 out << constUnion->getUConst();
113 break;
114 case EbtBool:
115 out << constUnion->getBConst();
116 break;
117 default:
118 UNREACHABLE();
119 }
120}
121
Olli Etuaho56a2f952016-12-08 12:16:27 +0000122const TConstantUnion *OutputHLSL::writeConstantUnionArray(TInfoSinkBase &out,
123 const TConstantUnion *const constUnion,
124 const size_t size)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200125{
126 const TConstantUnion *constUnionIterated = constUnion;
127 for (size_t i = 0; i < size; i++, constUnionIterated++)
128 {
Olli Etuaho56a2f952016-12-08 12:16:27 +0000129 writeSingleConstant(out, constUnionIterated);
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200130
131 if (i != size - 1)
132 {
133 out << ", ";
134 }
135 }
136 return constUnionIterated;
137}
138
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800139OutputHLSL::OutputHLSL(sh::GLenum shaderType,
140 int shaderVersion,
141 const TExtensionBehavior &extensionBehavior,
142 const char *sourcePath,
143 ShShaderOutput outputType,
144 int numRenderTargets,
145 const std::vector<Uniform> &uniforms,
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300146 ShCompileOptions compileOptions,
Olli Etuaho89a69a02017-10-23 12:20:45 +0300147 TSymbolTable *symbolTable,
148 PerformanceDiagnostics *perfDiagnostics)
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300149 : TIntermTraverser(true, true, true, symbolTable),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200150 mShaderType(shaderType),
151 mShaderVersion(shaderVersion),
152 mExtensionBehavior(extensionBehavior),
153 mSourcePath(sourcePath),
154 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700155 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000156 mNumRenderTargets(numRenderTargets),
Olli Etuaho89a69a02017-10-23 12:20:45 +0300157 mCurrentFunctionMetadata(nullptr),
158 mPerfDiagnostics(perfDiagnostics)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000159{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000160 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000161
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500162 mUsesFragColor = false;
163 mUsesFragData = false;
164 mUsesDepthRange = false;
165 mUsesFragCoord = false;
166 mUsesPointCoord = false;
167 mUsesFrontFacing = false;
168 mUsesPointSize = false;
169 mUsesInstanceID = false;
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300170 mHasMultiviewExtensionEnabled =
171 IsExtensionEnabled(mExtensionBehavior, TExtension::OVR_multiview);
Martin Radev41ac68e2017-06-06 12:16:58 +0300172 mUsesViewID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500173 mUsesVertexID = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500174 mUsesFragDepth = false;
Xinghua Caob1239382016-12-13 15:07:05 +0800175 mUsesNumWorkGroups = false;
176 mUsesWorkGroupID = false;
177 mUsesLocalInvocationID = false;
178 mUsesGlobalInvocationID = false;
179 mUsesLocalInvocationIndex = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500180 mUsesXor = false;
181 mUsesDiscardRewriting = false;
182 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530183 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000184
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000185 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000186
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500187 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000188 mInsideDiscontinuousLoop = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500189 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000190
Yunchao Hed7297bf2017-04-19 15:27:10 +0800191 mExcessiveLoopIndex = nullptr;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000192
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500193 mStructureHLSL = new StructureHLSL;
Xinghua Cao711b7a12017-10-09 13:38:12 +0800194 mUniformHLSL = new UniformHLSL(shaderType, mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300195 mTextureFunctionHLSL = new TextureFunctionHLSL;
Xinghua Cao711b7a12017-10-09 13:38:12 +0800196 mImageFunctionHLSL = new ImageFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400197
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200198 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000199 {
Arun Patole63419392015-03-13 11:51:07 +0530200 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500201 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
202 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530203 // In both cases total 3 uniform registers need to be reserved.
204 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000205 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000206
Geoff Lang00140f42016-02-03 18:47:33 +0000207 // Reserve registers for the default uniform block and driver constants
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800208 mUniformHLSL->reserveUniformBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000209}
210
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000211OutputHLSL::~OutputHLSL()
212{
Jamie Madill8daaba12014-06-13 10:04:33 -0400213 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400214 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300215 SafeDelete(mTextureFunctionHLSL);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800216 SafeDelete(mImageFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200217 for (auto &eqFunction : mStructEqualityFunctions)
218 {
219 SafeDelete(eqFunction);
220 }
221 for (auto &eqFunction : mArrayEqualityFunctions)
222 {
223 SafeDelete(eqFunction);
224 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000225}
226
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200227void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000228{
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200229 BuiltInFunctionEmulator builtInFunctionEmulator;
230 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800231 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
232 {
233 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
234 mShaderVersion);
235 }
236
Olli Etuahodfa75e82017-01-23 09:43:06 -0800237 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500238
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700239 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000240 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700241 ASSERT(success == CallDAG::INITDAG_SUCCESS);
242 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700243
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200244 const std::vector<MappedStruct> std140Structs = FlagStd140Structs(treeRoot);
245 // TODO(oetuaho): The std140Structs could be filtered based on which ones actually get used in
246 // the shader code. When we add shader storage blocks we might also consider an alternative
247 // solution, since the struct mapping won't work very well for shader storage blocks.
248
Jamie Madill37997142015-01-28 10:06:34 -0500249 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500250 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200251 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500252 mInfoSinkStack.pop();
253
Jamie Madill37997142015-01-28 10:06:34 -0500254 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500255 mInfoSinkStack.pop();
256
Jamie Madill32aab012015-01-27 14:12:26 -0500257 mInfoSinkStack.push(&mHeader);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200258 header(mHeader, std140Structs, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500259 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000260
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200261 objSink << mHeader.c_str();
262 objSink << mBody.c_str();
263 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200264
Olli Etuahodfa75e82017-01-23 09:43:06 -0800265 builtInFunctionEmulator.cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000266}
267
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800268const std::map<std::string, unsigned int> &OutputHLSL::getUniformBlockRegisterMap() const
Jamie Madill4e1fd412014-07-10 17:50:10 -0400269{
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800270 return mUniformHLSL->getUniformBlockRegisterMap();
Jamie Madill4e1fd412014-07-10 17:50:10 -0400271}
272
Jamie Madill9fe25e92014-07-18 10:33:08 -0400273const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
274{
275 return mUniformHLSL->getUniformRegisterMap();
276}
277
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200278TString OutputHLSL::structInitializerString(int indent,
279 const TType &type,
280 const TString &name) const
Jamie Madill570e04d2013-06-21 09:15:33 -0400281{
282 TString init;
283
Olli Etuahoed049ab2017-06-30 17:38:33 +0300284 TString indentString;
285 for (int spaces = 0; spaces < indent; spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400286 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300287 indentString += " ";
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 }
289
Olli Etuahoed049ab2017-06-30 17:38:33 +0300290 if (type.isArray())
Jamie Madill570e04d2013-06-21 09:15:33 -0400291 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300292 init += indentString + "{\n";
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300293 for (unsigned int arrayIndex = 0u; arrayIndex < type.getOutermostArraySize(); ++arrayIndex)
Jamie Madill570e04d2013-06-21 09:15:33 -0400294 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300295 TStringStream indexedString;
296 indexedString << name << "[" << arrayIndex << "]";
297 TType elementType = type;
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300298 elementType.toArrayElementType();
Olli Etuahoed049ab2017-06-30 17:38:33 +0300299 init += structInitializerString(indent + 1, elementType, indexedString.str());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300300 if (arrayIndex < type.getOutermostArraySize() - 1)
Olli Etuahoed049ab2017-06-30 17:38:33 +0300301 {
302 init += ",";
303 }
304 init += "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400305 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300306 init += indentString + "}";
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300308 else if (type.getBasicType() == EbtStruct)
309 {
310 init += indentString + "{\n";
311 const TStructure &structure = *type.getStruct();
312 const TFieldList &fields = structure.fields();
313 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
314 {
315 const TField &field = *fields[fieldIndex];
316 const TString &fieldName = name + "." + Decorate(field.name());
317 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400318
Olli Etuahoed049ab2017-06-30 17:38:33 +0300319 init += structInitializerString(indent + 1, fieldType, fieldName);
320 if (fieldIndex < fields.size() - 1)
321 {
322 init += ",";
323 }
324 init += "\n";
325 }
326 init += indentString + "}";
327 }
328 else
329 {
330 init += indentString + name;
331 }
Jamie Madill570e04d2013-06-21 09:15:33 -0400332
333 return init;
334}
335
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200336TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std140Structs) const
337{
338 TString mappedStructs;
339
340 for (auto &mappedStruct : std140Structs)
341 {
342 TInterfaceBlock *interfaceBlock =
343 mappedStruct.blockDeclarator->getType().getInterfaceBlock();
Olli Etuahoae4dbf32017-12-08 20:49:00 +0100344 const TString &interfaceBlockName = *interfaceBlock->name();
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200345 const TName &instanceName = mappedStruct.blockDeclarator->getName();
Olli Etuaho12a18ad2017-12-01 16:59:47 +0200346 if (mReferencedUniformBlocks.count(interfaceBlockName) == 0)
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200347 {
348 continue;
349 }
350
351 unsigned int instanceCount = 1u;
352 bool isInstanceArray = mappedStruct.blockDeclarator->isArray();
353 if (isInstanceArray)
354 {
355 instanceCount = mappedStruct.blockDeclarator->getOutermostArraySize();
356 }
357
358 for (unsigned int instanceArrayIndex = 0; instanceArrayIndex < instanceCount;
359 ++instanceArrayIndex)
360 {
361 TString originalName;
362 TString mappedName("map");
363
364 if (instanceName.getString() != "")
365 {
366 unsigned int instanceStringArrayIndex = GL_INVALID_INDEX;
367 if (isInstanceArray)
368 instanceStringArrayIndex = instanceArrayIndex;
Olli Etuaho12a18ad2017-12-01 16:59:47 +0200369 TString instanceString = mUniformHLSL->UniformBlockInstanceString(
370 instanceName.getString(), instanceStringArrayIndex);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200371 originalName += instanceString;
372 mappedName += instanceString;
373 originalName += ".";
374 mappedName += "_";
375 }
376
377 TString fieldName = Decorate(mappedStruct.field->name());
378 originalName += fieldName;
379 mappedName += fieldName;
380
381 TType *structType = mappedStruct.field->type();
382 mappedStructs +=
Olli Etuahoae4dbf32017-12-08 20:49:00 +0100383 "static " + Decorate(*structType->getStruct()->name()) + " " + mappedName;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200384
385 if (structType->isArray())
386 {
387 mappedStructs += ArrayString(*mappedStruct.field->type());
388 }
389
390 mappedStructs += " =\n";
391 mappedStructs += structInitializerString(0, *structType, originalName);
392 mappedStructs += ";\n";
393 }
394 }
395 return mappedStructs;
396}
397
398void OutputHLSL::header(TInfoSinkBase &out,
399 const std::vector<MappedStruct> &std140Structs,
400 const BuiltInFunctionEmulator *builtInFunctionEmulator) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000402 TString varyings;
403 TString attributes;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200404 TString mappedStructs = generateStructMapping(std140Structs);
Jamie Madill570e04d2013-06-21 09:15:33 -0400405
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500406 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin();
407 varying != mReferencedVaryings.end(); varying++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000408 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500409 const TType &type = varying->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000410 const TString &name = varying->second->getSymbol();
411
412 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500413 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
414 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000415 }
416
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500417 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin();
418 attribute != mReferencedAttributes.end(); attribute++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000419 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500420 const TType &type = attribute->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000421 const TString &name = attribute->second->getSymbol();
422
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500423 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
424 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000425 }
426
Jamie Madill8daaba12014-06-13 10:04:33 -0400427 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400428
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300429 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms, mSymbolTable);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800430 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400431
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200432 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500433 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200434 out << "\n// Equality functions\n\n";
435 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500436 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200437 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200438 }
439 }
Olli Etuaho12690762015-03-31 12:55:28 +0300440 if (!mArrayAssignmentFunctions.empty())
441 {
442 out << "\n// Assignment functions\n\n";
443 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
444 {
445 out << assignmentFunction.functionDefinition << "\n";
446 }
447 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300448 if (!mArrayConstructIntoFunctions.empty())
449 {
450 out << "\n// Array constructor functions\n\n";
451 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
452 {
453 out << constructIntoFunction.functionDefinition << "\n";
454 }
455 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200456
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500457 if (mUsesDiscardRewriting)
458 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400459 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500460 }
461
Nicolas Capens655fe362014-04-11 13:12:34 -0400462 if (mUsesNestedBreak)
463 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400464 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400465 }
466
Arun Patole44efa0b2015-03-04 17:11:05 +0530467 if (mRequiresIEEEStrictCompiling)
468 {
469 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
470 }
471
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400472 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
473 "#define LOOP [loop]\n"
474 "#define FLATTEN [flatten]\n"
475 "#else\n"
476 "#define LOOP\n"
477 "#define FLATTEN\n"
478 "#endif\n";
479
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200480 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300482 const bool usingMRTExtension =
483 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000484
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000485 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500486 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400487 out << "\n";
488
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200489 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000490 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500491 for (ReferencedSymbols::const_iterator outputVariableIt =
492 mReferencedOutputVariables.begin();
493 outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000494 {
Jamie Madill46131a32013-06-20 11:55:50 -0400495 const TString &variableName = outputVariableIt->first;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500496 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400497
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500498 out << "static " + TypeString(variableType) + " out_" + variableName +
499 ArrayString(variableType) + " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000500 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000501 }
Jamie Madill46131a32013-06-20 11:55:50 -0400502 else
503 {
504 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
505
506 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500507 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400508 for (unsigned int i = 0; i < numColorValues; i++)
509 {
510 out << " float4(0, 0, 0, 0)";
511 if (i + 1 != numColorValues)
512 {
513 out << ",";
514 }
515 out << "\n";
516 }
517
518 out << "};\n";
519 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000520
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400521 if (mUsesFragDepth)
522 {
523 out << "static float gl_Depth = 0.0;\n";
524 }
525
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000526 if (mUsesFragCoord)
527 {
528 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
529 }
530
531 if (mUsesPointCoord)
532 {
533 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
534 }
535
536 if (mUsesFrontFacing)
537 {
538 out << "static bool gl_FrontFacing = false;\n";
539 }
540
541 out << "\n";
542
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000543 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000544 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000545 out << "struct gl_DepthRangeParameters\n"
546 "{\n"
547 " float near;\n"
548 " float far;\n"
549 " float diff;\n"
550 "};\n"
551 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000552 }
553
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200554 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000555 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000556 out << "cbuffer DriverConstants : register(b1)\n"
557 "{\n";
558
559 if (mUsesDepthRange)
560 {
561 out << " float3 dx_DepthRange : packoffset(c0);\n";
562 }
563
564 if (mUsesFragCoord)
565 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000566 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000567 }
568
569 if (mUsesFragCoord || mUsesFrontFacing)
570 {
571 out << " float3 dx_DepthFront : packoffset(c2);\n";
572 }
573
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800574 if (mUsesFragCoord)
575 {
576 // dx_ViewScale is only used in the fragment shader to correct
577 // the value for glFragCoord if necessary
578 out << " float2 dx_ViewScale : packoffset(c3);\n";
579 }
580
Martin Radev72b4e1e2017-08-31 15:42:56 +0300581 if (mHasMultiviewExtensionEnabled)
582 {
583 // We have to add a value which we can use to keep track of which multi-view code
584 // path is to be selected in the GS.
585 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
586 }
587
Olli Etuaho618bebc2016-01-15 16:40:00 +0200588 if (mOutputType == SH_HLSL_4_1_OUTPUT)
589 {
590 mUniformHLSL->samplerMetadataUniforms(out, "c4");
591 }
592
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000593 out << "};\n";
594 }
595 else
596 {
597 if (mUsesDepthRange)
598 {
599 out << "uniform float3 dx_DepthRange : register(c0);";
600 }
601
602 if (mUsesFragCoord)
603 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000604 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000605 }
606
607 if (mUsesFragCoord || mUsesFrontFacing)
608 {
609 out << "uniform float3 dx_DepthFront : register(c2);\n";
610 }
611 }
612
613 out << "\n";
614
615 if (mUsesDepthRange)
616 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500617 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
618 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000619 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000620 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000621
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200622 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000623 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200624 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000625 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200626 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400627 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000628 }
629
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000630 if (usingMRTExtension && mNumRenderTargets > 1)
631 {
632 out << "#define GL_USES_MRT\n";
633 }
634
635 if (mUsesFragColor)
636 {
637 out << "#define GL_USES_FRAG_COLOR\n";
638 }
639
640 if (mUsesFragData)
641 {
642 out << "#define GL_USES_FRAG_DATA\n";
643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644 }
Xinghua Caob1239382016-12-13 15:07:05 +0800645 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000647 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500648 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000649 out << "\n"
650 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400651
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000652 if (mUsesPointSize)
653 {
654 out << "static float gl_PointSize = float(1);\n";
655 }
656
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000657 if (mUsesInstanceID)
658 {
659 out << "static int gl_InstanceID;";
660 }
661
Corentin Wallezb076add2016-01-11 16:45:46 -0500662 if (mUsesVertexID)
663 {
664 out << "static int gl_VertexID;";
665 }
666
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000667 out << "\n"
668 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500669 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000670 out << "\n";
671
672 if (mUsesDepthRange)
673 {
674 out << "struct gl_DepthRangeParameters\n"
675 "{\n"
676 " float near;\n"
677 " float far;\n"
678 " float diff;\n"
679 "};\n"
680 "\n";
681 }
682
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200683 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000684 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800685 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500686 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800687
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000688 if (mUsesDepthRange)
689 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800690 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000691 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800692
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800693 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
694 // shaders. However, we declare it for all shaders (including Feature Level 10+).
695 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
696 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800697 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800698 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800699 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800700
Martin Radev72b4e1e2017-08-31 15:42:56 +0300701 if (mHasMultiviewExtensionEnabled)
702 {
703 // We have to add a value which we can use to keep track of which multi-view code
704 // path is to be selected in the GS.
705 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
706 }
707
Olli Etuaho618bebc2016-01-15 16:40:00 +0200708 if (mOutputType == SH_HLSL_4_1_OUTPUT)
709 {
710 mUniformHLSL->samplerMetadataUniforms(out, "c4");
711 }
712
Austin Kinross4fd18b12014-12-22 12:32:05 -0800713 out << "};\n"
714 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000715 }
716 else
717 {
718 if (mUsesDepthRange)
719 {
720 out << "uniform float3 dx_DepthRange : register(c0);\n";
721 }
722
Cooper Partine6664f02015-01-09 16:22:24 -0800723 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
724 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000725 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000726 }
727
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000728 if (mUsesDepthRange)
729 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500730 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
731 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000732 "\n";
733 }
734
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200735 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000736 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200737 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000738 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200739 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400740 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000741 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400742 }
Xinghua Caob1239382016-12-13 15:07:05 +0800743 else // Compute shader
744 {
745 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800746
747 out << "cbuffer DriverConstants : register(b1)\n"
748 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800749 if (mUsesNumWorkGroups)
750 {
Xinghua Caob1239382016-12-13 15:07:05 +0800751 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800752 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800753 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
754 mUniformHLSL->samplerMetadataUniforms(out, "c1");
755 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800756
757 // Follow built-in variables would be initialized in
758 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
759 // are used in compute shader.
760 if (mUsesWorkGroupID)
761 {
762 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
763 }
764
765 if (mUsesLocalInvocationID)
766 {
767 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
768 }
769
770 if (mUsesGlobalInvocationID)
771 {
772 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
773 }
774
775 if (mUsesLocalInvocationIndex)
776 {
777 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
778 }
779 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000780
Geoff Lang1fe74c72016-08-25 13:23:01 -0400781 bool getDimensionsIgnoresBaseLevel =
782 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
783 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800784 mImageFunctionHLSL->imageFunctionHeader(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000786 if (mUsesFragCoord)
787 {
788 out << "#define GL_USES_FRAG_COORD\n";
789 }
790
791 if (mUsesPointCoord)
792 {
793 out << "#define GL_USES_POINT_COORD\n";
794 }
795
796 if (mUsesFrontFacing)
797 {
798 out << "#define GL_USES_FRONT_FACING\n";
799 }
800
801 if (mUsesPointSize)
802 {
803 out << "#define GL_USES_POINT_SIZE\n";
804 }
805
Martin Radev41ac68e2017-06-06 12:16:58 +0300806 if (mHasMultiviewExtensionEnabled)
807 {
808 out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n";
809 }
810
811 if (mUsesViewID)
812 {
813 out << "#define GL_USES_VIEW_ID\n";
814 }
815
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400816 if (mUsesFragDepth)
817 {
818 out << "#define GL_USES_FRAG_DEPTH\n";
819 }
820
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000821 if (mUsesDepthRange)
822 {
823 out << "#define GL_USES_DEPTH_RANGE\n";
824 }
825
Xinghua Caob1239382016-12-13 15:07:05 +0800826 if (mUsesNumWorkGroups)
827 {
828 out << "#define GL_USES_NUM_WORK_GROUPS\n";
829 }
830
831 if (mUsesWorkGroupID)
832 {
833 out << "#define GL_USES_WORK_GROUP_ID\n";
834 }
835
836 if (mUsesLocalInvocationID)
837 {
838 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
839 }
840
841 if (mUsesGlobalInvocationID)
842 {
843 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
844 }
845
846 if (mUsesLocalInvocationIndex)
847 {
848 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
849 }
850
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000851 if (mUsesXor)
852 {
853 out << "bool xor(bool p, bool q)\n"
854 "{\n"
855 " return (p || q) && !(p && q);\n"
856 "}\n"
857 "\n";
858 }
859
Olli Etuahodfa75e82017-01-23 09:43:06 -0800860 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861}
862
863void OutputHLSL::visitSymbol(TIntermSymbol *node)
864{
Jamie Madill32aab012015-01-27 14:12:26 -0500865 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866
Jamie Madill570e04d2013-06-21 09:15:33 -0400867 // Handle accessing std140 structs by value
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200868 if (IsInStd140InterfaceBlock(node) && node->getBasicType() == EbtStruct)
Jamie Madill570e04d2013-06-21 09:15:33 -0400869 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200870 out << "map";
Jamie Madill570e04d2013-06-21 09:15:33 -0400871 }
872
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873 TString name = node->getSymbol();
874
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000875 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000876 {
877 mUsesDepthRange = true;
878 out << name;
879 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880 else
881 {
Olli Etuahobd3cd502017-11-03 15:48:52 +0200882 const TType &nodeType = node->getType();
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000883 TQualifier qualifier = node->getQualifier();
884
Olli Etuahobd3cd502017-11-03 15:48:52 +0200885 ensureStructDefined(nodeType);
886
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000887 if (qualifier == EvqUniform)
888 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400889 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400890
891 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000892 {
Olli Etuahoae4dbf32017-12-08 20:49:00 +0100893 mReferencedUniformBlocks[*interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000894 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000895 else
896 {
897 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000898 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400899
Olli Etuahoff526f12017-06-30 12:26:54 +0300900 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000901 }
Jamie Madill19571812013-08-12 15:26:34 -0700902 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000903 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000904 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400905 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000906 }
Jamie Madill033dae62014-06-18 12:56:28 -0400907 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000908 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000909 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400910 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300911 if (name == "ViewID_OVR")
912 {
913 mUsesViewID = true;
914 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000915 }
Jamie Madill19571812013-08-12 15:26:34 -0700916 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400917 {
918 mReferencedOutputVariables[name] = node;
919 out << "out_" << name;
920 }
921 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000922 {
923 out << "gl_Color[0]";
924 mUsesFragColor = true;
925 }
926 else if (qualifier == EvqFragData)
927 {
928 out << "gl_Color";
929 mUsesFragData = true;
930 }
931 else if (qualifier == EvqFragCoord)
932 {
933 mUsesFragCoord = true;
934 out << name;
935 }
936 else if (qualifier == EvqPointCoord)
937 {
938 mUsesPointCoord = true;
939 out << name;
940 }
941 else if (qualifier == EvqFrontFacing)
942 {
943 mUsesFrontFacing = true;
944 out << name;
945 }
946 else if (qualifier == EvqPointSize)
947 {
948 mUsesPointSize = true;
949 out << name;
950 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000951 else if (qualifier == EvqInstanceID)
952 {
953 mUsesInstanceID = true;
954 out << name;
955 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500956 else if (qualifier == EvqVertexID)
957 {
958 mUsesVertexID = true;
959 out << name;
960 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300961 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400962 {
963 mUsesFragDepth = true;
964 out << "gl_Depth";
965 }
Xinghua Caob1239382016-12-13 15:07:05 +0800966 else if (qualifier == EvqNumWorkGroups)
967 {
968 mUsesNumWorkGroups = true;
969 out << name;
970 }
971 else if (qualifier == EvqWorkGroupID)
972 {
973 mUsesWorkGroupID = true;
974 out << name;
975 }
976 else if (qualifier == EvqLocalInvocationID)
977 {
978 mUsesLocalInvocationID = true;
979 out << name;
980 }
981 else if (qualifier == EvqGlobalInvocationID)
982 {
983 mUsesGlobalInvocationID = true;
984 out << name;
985 }
986 else if (qualifier == EvqLocalInvocationIndex)
987 {
988 mUsesLocalInvocationIndex = true;
989 out << name;
990 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000991 else
992 {
Olli Etuahoff526f12017-06-30 12:26:54 +0300993 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995 }
996}
997
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400998void OutputHLSL::visitRaw(TIntermRaw *node)
999{
Jamie Madill32aab012015-01-27 14:12:26 -05001000 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001001}
1002
Olli Etuaho7fb49552015-03-18 17:27:44 +02001003void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1004{
1005 if (type.isScalar() && !type.isArray())
1006 {
1007 if (op == EOpEqual)
1008 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001009 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001010 }
1011 else
1012 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001013 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001014 }
1015 }
1016 else
1017 {
1018 if (visit == PreVisit && op == EOpNotEqual)
1019 {
1020 out << "!";
1021 }
1022
1023 if (type.isArray())
1024 {
1025 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001026 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001027 }
1028 else if (type.getBasicType() == EbtStruct)
1029 {
1030 const TStructure &structure = *type.getStruct();
1031 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001032 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001033 }
1034 else
1035 {
1036 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001037 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001038 }
1039 }
1040}
1041
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001042void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1043{
1044 if (type.isArray())
1045 {
1046 const TString &functionName = addArrayAssignmentFunction(type);
1047 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1048 }
1049 else
1050 {
1051 outputTriplet(out, visit, "(", " = ", ")");
1052 }
1053}
1054
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001055bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001056{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001057 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001058 {
1059 TIntermNode *ancestor = getAncestorNode(n);
1060 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1061 if (ancestorBinary == nullptr)
1062 {
1063 return false;
1064 }
1065 switch (ancestorBinary->getOp())
1066 {
1067 case EOpIndexDirectStruct:
1068 {
1069 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1070 const TIntermConstantUnion *index =
1071 ancestorBinary->getRight()->getAsConstantUnion();
1072 const TField *field = structure->fields()[index->getIConst(0)];
1073 if (IsSampler(field->type()->getBasicType()))
1074 {
1075 return true;
1076 }
1077 break;
1078 }
1079 case EOpIndexDirect:
1080 break;
1081 default:
1082 // Returning a sampler from indirect indexing is not supported.
1083 return false;
1084 }
1085 }
1086 return false;
1087}
1088
Olli Etuahob6fa0432016-09-28 16:28:05 +01001089bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1090{
1091 TInfoSinkBase &out = getInfoSink();
1092 if (visit == PostVisit)
1093 {
1094 out << ".";
1095 node->writeOffsetsAsXYZW(&out);
1096 }
1097 return true;
1098}
1099
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1101{
Jamie Madill32aab012015-01-27 14:12:26 -05001102 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
1104 switch (node->getOp())
1105 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001106 case EOpComma:
1107 outputTriplet(out, visit, "(", ", ", ")");
1108 break;
1109 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001110 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001111 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001112 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1113 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001114 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001115 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1116 out << functionName << "(";
1117 node->getLeft()->traverse(this);
1118 TIntermSequence *seq = rightAgg->getSequence();
1119 for (auto &arrayElement : *seq)
1120 {
1121 out << ", ";
1122 arrayElement->traverse(this);
1123 }
1124 out << ")";
1125 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001126 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001127 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1128 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001129 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001130 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001131 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001132 break;
1133 case EOpInitialize:
1134 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001135 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001136 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1137 ASSERT(symbolNode);
1138 TIntermTyped *expression = node->getRight();
1139
1140 // Global initializers must be constant at this point.
1141 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
1142 canWriteAsHLSLLiteral(expression));
1143
1144 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1145 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1146 // new variable is created before the assignment is evaluated), so we need to
1147 // convert
1148 // this to "float t = x, x = t;".
1149 if (writeSameSymbolInitializer(out, symbolNode, expression))
1150 {
1151 // Skip initializing the rest of the expression
1152 return false;
1153 }
1154 else if (writeConstantInitialization(out, symbolNode, expression))
1155 {
1156 return false;
1157 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001158 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001159 else if (visit == InVisit)
1160 {
1161 out << " = ";
1162 }
1163 break;
1164 case EOpAddAssign:
1165 outputTriplet(out, visit, "(", " += ", ")");
1166 break;
1167 case EOpSubAssign:
1168 outputTriplet(out, visit, "(", " -= ", ")");
1169 break;
1170 case EOpMulAssign:
1171 outputTriplet(out, visit, "(", " *= ", ")");
1172 break;
1173 case EOpVectorTimesScalarAssign:
1174 outputTriplet(out, visit, "(", " *= ", ")");
1175 break;
1176 case EOpMatrixTimesScalarAssign:
1177 outputTriplet(out, visit, "(", " *= ", ")");
1178 break;
1179 case EOpVectorTimesMatrixAssign:
1180 if (visit == PreVisit)
1181 {
1182 out << "(";
1183 }
1184 else if (visit == InVisit)
1185 {
1186 out << " = mul(";
1187 node->getLeft()->traverse(this);
1188 out << ", transpose(";
1189 }
1190 else
1191 {
1192 out << ")))";
1193 }
1194 break;
1195 case EOpMatrixTimesMatrixAssign:
1196 if (visit == PreVisit)
1197 {
1198 out << "(";
1199 }
1200 else if (visit == InVisit)
1201 {
1202 out << " = transpose(mul(transpose(";
1203 node->getLeft()->traverse(this);
1204 out << "), transpose(";
1205 }
1206 else
1207 {
1208 out << "))))";
1209 }
1210 break;
1211 case EOpDivAssign:
1212 outputTriplet(out, visit, "(", " /= ", ")");
1213 break;
1214 case EOpIModAssign:
1215 outputTriplet(out, visit, "(", " %= ", ")");
1216 break;
1217 case EOpBitShiftLeftAssign:
1218 outputTriplet(out, visit, "(", " <<= ", ")");
1219 break;
1220 case EOpBitShiftRightAssign:
1221 outputTriplet(out, visit, "(", " >>= ", ")");
1222 break;
1223 case EOpBitwiseAndAssign:
1224 outputTriplet(out, visit, "(", " &= ", ")");
1225 break;
1226 case EOpBitwiseXorAssign:
1227 outputTriplet(out, visit, "(", " ^= ", ")");
1228 break;
1229 case EOpBitwiseOrAssign:
1230 outputTriplet(out, visit, "(", " |= ", ")");
1231 break;
1232 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001233 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001234 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001235 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001236 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001237 if (visit == PreVisit)
1238 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001239 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Olli Etuaho12a18ad2017-12-01 16:59:47 +02001240 TIntermSymbol *instanceArraySymbol = node->getLeft()->getAsSymbolNode();
Olli Etuahoae4dbf32017-12-08 20:49:00 +01001241 mReferencedUniformBlocks[*interfaceBlock->name()] = instanceArraySymbol;
Jamie Madill98493dd2013-07-08 14:39:03 -04001242 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Olli Etuaho12a18ad2017-12-01 16:59:47 +02001243 out << mUniformHLSL->UniformBlockInstanceString(
1244 instanceArraySymbol->getSymbol(), arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001245 return false;
1246 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001247 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001248 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001249 {
1250 // All parts of an expression that access a sampler in a struct need to use _ as
1251 // separator to access the sampler variable that has been moved out of the struct.
1252 outputTriplet(out, visit, "", "_", "");
1253 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001254 else
1255 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001256 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001257 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001258 }
1259 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001260 case EOpIndexIndirect:
1261 // We do not currently support indirect references to interface blocks
1262 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1263 outputTriplet(out, visit, "", "[", "]");
1264 break;
1265 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001266 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001267 const TStructure *structure = node->getLeft()->getType().getStruct();
1268 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1269 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001270
Olli Etuaho96963162016-03-21 11:54:33 +02001271 // In cases where indexing returns a sampler, we need to access the sampler variable
1272 // that has been moved out of the struct.
1273 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1274 if (visit == PreVisit && indexingReturnsSampler)
1275 {
1276 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1277 // This prefix is only output at the beginning of the indexing expression, which
1278 // may have multiple parts.
1279 out << "angle";
1280 }
1281 if (!indexingReturnsSampler)
1282 {
1283 // All parts of an expression that access a sampler in a struct need to use _ as
1284 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001285 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001286 }
1287 if (visit == InVisit)
1288 {
1289 if (indexingReturnsSampler)
1290 {
1291 out << "_" + field->name();
1292 }
1293 else
1294 {
1295 out << "." + DecorateField(field->name(), *structure);
1296 }
1297
1298 return false;
1299 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001300 }
1301 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001302 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001303 {
1304 bool structInStd140Block =
1305 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1306 if (visit == PreVisit && structInStd140Block)
1307 {
1308 out << "map";
1309 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001310 if (visit == InVisit)
1311 {
1312 const TInterfaceBlock *interfaceBlock =
1313 node->getLeft()->getType().getInterfaceBlock();
1314 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1315 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001316 if (structInStd140Block)
1317 {
1318 out << "_";
1319 }
1320 else
1321 {
1322 out << ".";
1323 }
1324 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001325
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001326 return false;
1327 }
1328 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001329 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001330 case EOpAdd:
1331 outputTriplet(out, visit, "(", " + ", ")");
1332 break;
1333 case EOpSub:
1334 outputTriplet(out, visit, "(", " - ", ")");
1335 break;
1336 case EOpMul:
1337 outputTriplet(out, visit, "(", " * ", ")");
1338 break;
1339 case EOpDiv:
1340 outputTriplet(out, visit, "(", " / ", ")");
1341 break;
1342 case EOpIMod:
1343 outputTriplet(out, visit, "(", " % ", ")");
1344 break;
1345 case EOpBitShiftLeft:
1346 outputTriplet(out, visit, "(", " << ", ")");
1347 break;
1348 case EOpBitShiftRight:
1349 outputTriplet(out, visit, "(", " >> ", ")");
1350 break;
1351 case EOpBitwiseAnd:
1352 outputTriplet(out, visit, "(", " & ", ")");
1353 break;
1354 case EOpBitwiseXor:
1355 outputTriplet(out, visit, "(", " ^ ", ")");
1356 break;
1357 case EOpBitwiseOr:
1358 outputTriplet(out, visit, "(", " | ", ")");
1359 break;
1360 case EOpEqual:
1361 case EOpNotEqual:
1362 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1363 break;
1364 case EOpLessThan:
1365 outputTriplet(out, visit, "(", " < ", ")");
1366 break;
1367 case EOpGreaterThan:
1368 outputTriplet(out, visit, "(", " > ", ")");
1369 break;
1370 case EOpLessThanEqual:
1371 outputTriplet(out, visit, "(", " <= ", ")");
1372 break;
1373 case EOpGreaterThanEqual:
1374 outputTriplet(out, visit, "(", " >= ", ")");
1375 break;
1376 case EOpVectorTimesScalar:
1377 outputTriplet(out, visit, "(", " * ", ")");
1378 break;
1379 case EOpMatrixTimesScalar:
1380 outputTriplet(out, visit, "(", " * ", ")");
1381 break;
1382 case EOpVectorTimesMatrix:
1383 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1384 break;
1385 case EOpMatrixTimesVector:
1386 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1387 break;
1388 case EOpMatrixTimesMatrix:
1389 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1390 break;
1391 case EOpLogicalOr:
1392 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1393 // been unfolded.
1394 ASSERT(!node->getRight()->hasSideEffects());
1395 outputTriplet(out, visit, "(", " || ", ")");
1396 return true;
1397 case EOpLogicalXor:
1398 mUsesXor = true;
1399 outputTriplet(out, visit, "xor(", ", ", ")");
1400 break;
1401 case EOpLogicalAnd:
1402 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1403 // been unfolded.
1404 ASSERT(!node->getRight()->hasSideEffects());
1405 outputTriplet(out, visit, "(", " && ", ")");
1406 return true;
1407 default:
1408 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001409 }
1410
1411 return true;
1412}
1413
1414bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1415{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001416 TInfoSinkBase &out = getInfoSink();
1417
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001418 switch (node->getOp())
1419 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001420 case EOpNegative:
1421 outputTriplet(out, visit, "(-", "", ")");
1422 break;
1423 case EOpPositive:
1424 outputTriplet(out, visit, "(+", "", ")");
1425 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001426 case EOpLogicalNot:
1427 outputTriplet(out, visit, "(!", "", ")");
1428 break;
1429 case EOpBitwiseNot:
1430 outputTriplet(out, visit, "(~", "", ")");
1431 break;
1432 case EOpPostIncrement:
1433 outputTriplet(out, visit, "(", "", "++)");
1434 break;
1435 case EOpPostDecrement:
1436 outputTriplet(out, visit, "(", "", "--)");
1437 break;
1438 case EOpPreIncrement:
1439 outputTriplet(out, visit, "(++", "", ")");
1440 break;
1441 case EOpPreDecrement:
1442 outputTriplet(out, visit, "(--", "", ")");
1443 break;
1444 case EOpRadians:
1445 outputTriplet(out, visit, "radians(", "", ")");
1446 break;
1447 case EOpDegrees:
1448 outputTriplet(out, visit, "degrees(", "", ")");
1449 break;
1450 case EOpSin:
1451 outputTriplet(out, visit, "sin(", "", ")");
1452 break;
1453 case EOpCos:
1454 outputTriplet(out, visit, "cos(", "", ")");
1455 break;
1456 case EOpTan:
1457 outputTriplet(out, visit, "tan(", "", ")");
1458 break;
1459 case EOpAsin:
1460 outputTriplet(out, visit, "asin(", "", ")");
1461 break;
1462 case EOpAcos:
1463 outputTriplet(out, visit, "acos(", "", ")");
1464 break;
1465 case EOpAtan:
1466 outputTriplet(out, visit, "atan(", "", ")");
1467 break;
1468 case EOpSinh:
1469 outputTriplet(out, visit, "sinh(", "", ")");
1470 break;
1471 case EOpCosh:
1472 outputTriplet(out, visit, "cosh(", "", ")");
1473 break;
1474 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001475 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001476 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001477 case EOpAtanh:
1478 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001479 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001480 break;
1481 case EOpExp:
1482 outputTriplet(out, visit, "exp(", "", ")");
1483 break;
1484 case EOpLog:
1485 outputTriplet(out, visit, "log(", "", ")");
1486 break;
1487 case EOpExp2:
1488 outputTriplet(out, visit, "exp2(", "", ")");
1489 break;
1490 case EOpLog2:
1491 outputTriplet(out, visit, "log2(", "", ")");
1492 break;
1493 case EOpSqrt:
1494 outputTriplet(out, visit, "sqrt(", "", ")");
1495 break;
1496 case EOpInverseSqrt:
1497 outputTriplet(out, visit, "rsqrt(", "", ")");
1498 break;
1499 case EOpAbs:
1500 outputTriplet(out, visit, "abs(", "", ")");
1501 break;
1502 case EOpSign:
1503 outputTriplet(out, visit, "sign(", "", ")");
1504 break;
1505 case EOpFloor:
1506 outputTriplet(out, visit, "floor(", "", ")");
1507 break;
1508 case EOpTrunc:
1509 outputTriplet(out, visit, "trunc(", "", ")");
1510 break;
1511 case EOpRound:
1512 outputTriplet(out, visit, "round(", "", ")");
1513 break;
1514 case EOpRoundEven:
1515 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001516 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001517 break;
1518 case EOpCeil:
1519 outputTriplet(out, visit, "ceil(", "", ")");
1520 break;
1521 case EOpFract:
1522 outputTriplet(out, visit, "frac(", "", ")");
1523 break;
1524 case EOpIsNan:
1525 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001526 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001527 else
1528 outputTriplet(out, visit, "isnan(", "", ")");
1529 mRequiresIEEEStrictCompiling = true;
1530 break;
1531 case EOpIsInf:
1532 outputTriplet(out, visit, "isinf(", "", ")");
1533 break;
1534 case EOpFloatBitsToInt:
1535 outputTriplet(out, visit, "asint(", "", ")");
1536 break;
1537 case EOpFloatBitsToUint:
1538 outputTriplet(out, visit, "asuint(", "", ")");
1539 break;
1540 case EOpIntBitsToFloat:
1541 outputTriplet(out, visit, "asfloat(", "", ")");
1542 break;
1543 case EOpUintBitsToFloat:
1544 outputTriplet(out, visit, "asfloat(", "", ")");
1545 break;
1546 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001547 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001548 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001549 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001550 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001551 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001552 case EOpPackUnorm4x8:
1553 case EOpPackSnorm4x8:
1554 case EOpUnpackUnorm4x8:
1555 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001556 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001557 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001558 break;
1559 case EOpLength:
1560 outputTriplet(out, visit, "length(", "", ")");
1561 break;
1562 case EOpNormalize:
1563 outputTriplet(out, visit, "normalize(", "", ")");
1564 break;
1565 case EOpDFdx:
1566 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1567 {
1568 outputTriplet(out, visit, "(", "", ", 0.0)");
1569 }
1570 else
1571 {
1572 outputTriplet(out, visit, "ddx(", "", ")");
1573 }
1574 break;
1575 case EOpDFdy:
1576 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1577 {
1578 outputTriplet(out, visit, "(", "", ", 0.0)");
1579 }
1580 else
1581 {
1582 outputTriplet(out, visit, "ddy(", "", ")");
1583 }
1584 break;
1585 case EOpFwidth:
1586 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1587 {
1588 outputTriplet(out, visit, "(", "", ", 0.0)");
1589 }
1590 else
1591 {
1592 outputTriplet(out, visit, "fwidth(", "", ")");
1593 }
1594 break;
1595 case EOpTranspose:
1596 outputTriplet(out, visit, "transpose(", "", ")");
1597 break;
1598 case EOpDeterminant:
1599 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1600 break;
1601 case EOpInverse:
1602 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001603 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001604 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001605
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001606 case EOpAny:
1607 outputTriplet(out, visit, "any(", "", ")");
1608 break;
1609 case EOpAll:
1610 outputTriplet(out, visit, "all(", "", ")");
1611 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001612 case EOpLogicalNotComponentWise:
1613 outputTriplet(out, visit, "(!", "", ")");
1614 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001615 case EOpBitfieldReverse:
1616 outputTriplet(out, visit, "reversebits(", "", ")");
1617 break;
1618 case EOpBitCount:
1619 outputTriplet(out, visit, "countbits(", "", ")");
1620 break;
1621 case EOpFindLSB:
1622 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1623 // in GLSLTest and results are consistent with GL.
1624 outputTriplet(out, visit, "firstbitlow(", "", ")");
1625 break;
1626 case EOpFindMSB:
1627 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1628 // tested in GLSLTest and results are consistent with GL.
1629 outputTriplet(out, visit, "firstbithigh(", "", ")");
1630 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001631 default:
1632 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001633 }
1634
1635 return true;
1636}
1637
Olli Etuaho96963162016-03-21 11:54:33 +02001638TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1639{
1640 if (node->getAsSymbolNode())
1641 {
1642 return node->getAsSymbolNode()->getSymbol();
1643 }
1644 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1645 switch (nodeBinary->getOp())
1646 {
1647 case EOpIndexDirect:
1648 {
1649 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1650
1651 TInfoSinkBase prefixSink;
1652 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1653 return TString(prefixSink.c_str());
1654 }
1655 case EOpIndexDirectStruct:
1656 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001657 const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001658 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1659 const TField *field = s->fields()[index];
1660
1661 TInfoSinkBase prefixSink;
1662 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1663 << field->name();
1664 return TString(prefixSink.c_str());
1665 }
1666 default:
1667 UNREACHABLE();
1668 return TString("");
1669 }
1670}
1671
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001672bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1673{
1674 TInfoSinkBase &out = getInfoSink();
1675
1676 if (mInsideFunction)
1677 {
1678 outputLineDirective(out, node->getLine().first_line);
1679 out << "{\n";
1680 }
1681
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001682 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001683 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001684 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001685
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001686 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001687
1688 // Don't output ; after case labels, they're terminated by :
1689 // This is needed especially since outputting a ; after a case statement would turn empty
1690 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001691 // Also the output code is clearer if we don't output ; after statements where it is not
1692 // needed:
1693 // * if statements
1694 // * switch statements
1695 // * blocks
1696 // * function definitions
1697 // * loops (do-while loops output the semicolon in VisitLoop)
1698 // * declarations that don't generate output.
1699 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1700 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1701 statement->getAsSwitchNode() == nullptr &&
1702 statement->getAsFunctionDefinition() == nullptr &&
1703 (statement->getAsDeclarationNode() == nullptr ||
1704 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1705 statement->getAsInvariantDeclarationNode() == nullptr)
1706 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001707 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001708 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001709 }
1710
1711 if (mInsideFunction)
1712 {
1713 outputLineDirective(out, node->getLine().last_line);
1714 out << "}\n";
1715 }
1716
1717 return false;
1718}
1719
Olli Etuaho336b1472016-10-05 16:37:55 +01001720bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1721{
1722 TInfoSinkBase &out = getInfoSink();
1723
1724 ASSERT(mCurrentFunctionMetadata == nullptr);
1725
Olli Etuaho1bb85282017-12-14 13:39:53 +02001726 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo()->getId());
Olli Etuaho336b1472016-10-05 16:37:55 +01001727 ASSERT(index != CallDAG::InvalidIndex);
1728 mCurrentFunctionMetadata = &mASTMetadataList[index];
1729
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001730 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001731
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001732 TIntermSequence *parameters = node->getFunctionPrototype()->getSequence();
Olli Etuaho336b1472016-10-05 16:37:55 +01001733
1734 if (node->getFunctionSymbolInfo()->isMain())
1735 {
1736 out << "gl_main(";
1737 }
1738 else
1739 {
Olli Etuahoff526f12017-06-30 12:26:54 +03001740 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
Olli Etuaho336b1472016-10-05 16:37:55 +01001741 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1742 }
1743
1744 for (unsigned int i = 0; i < parameters->size(); i++)
1745 {
1746 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1747
1748 if (symbol)
1749 {
1750 ensureStructDefined(symbol->getType());
1751
1752 out << argumentString(symbol);
1753
1754 if (i < parameters->size() - 1)
1755 {
1756 out << ", ";
1757 }
1758 }
1759 else
1760 UNREACHABLE();
1761 }
1762
1763 out << ")\n";
1764
1765 mInsideFunction = true;
1766 // The function body node will output braces.
1767 node->getBody()->traverse(this);
1768 mInsideFunction = false;
1769
1770 mCurrentFunctionMetadata = nullptr;
1771
1772 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1773 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1774 {
1775 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1776 mOutputLod0Function = true;
1777 node->traverse(this);
1778 mOutputLod0Function = false;
1779 }
1780
1781 return false;
1782}
1783
Olli Etuaho13389b62016-10-16 11:48:18 +01001784bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1785{
Olli Etuaho13389b62016-10-16 11:48:18 +01001786 if (visit == PreVisit)
1787 {
1788 TIntermSequence *sequence = node->getSequence();
1789 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1790 ASSERT(sequence->size() == 1);
Olli Etuaho282847e2017-07-12 14:11:01 +03001791 ASSERT(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01001792
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001793 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001794 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001795 TInfoSinkBase &out = getInfoSink();
Olli Etuaho13389b62016-10-16 11:48:18 +01001796 ensureStructDefined(variable->getType());
1797
1798 if (!variable->getAsSymbolNode() ||
1799 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1800 {
1801 if (!mInsideFunction)
1802 {
1803 out << "static ";
1804 }
1805
1806 out << TypeString(variable->getType()) + " ";
1807
1808 TIntermSymbol *symbol = variable->getAsSymbolNode();
1809
1810 if (symbol)
1811 {
1812 symbol->traverse(this);
1813 out << ArrayString(symbol->getType());
1814 out << " = " + initializer(symbol->getType());
1815 }
1816 else
1817 {
1818 variable->traverse(this);
1819 }
1820 }
1821 else if (variable->getAsSymbolNode() &&
1822 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1823 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001824 ASSERT(variable->getBasicType() == EbtStruct);
1825 // ensureStructDefined has already been called.
Olli Etuaho13389b62016-10-16 11:48:18 +01001826 }
1827 else
1828 UNREACHABLE();
1829 }
Olli Etuaho282847e2017-07-12 14:11:01 +03001830 else if (IsVaryingOut(variable->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001831 {
Olli Etuaho282847e2017-07-12 14:11:01 +03001832 TIntermSymbol *symbol = variable->getAsSymbolNode();
1833 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001834
Olli Etuaho282847e2017-07-12 14:11:01 +03001835 // Vertex outputs which are declared but not written to should still be declared to
1836 // allow successful linking.
1837 mReferencedVaryings[symbol->getSymbol()] = symbol;
Olli Etuaho13389b62016-10-16 11:48:18 +01001838 }
1839 }
1840 return false;
1841}
1842
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001843bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1844{
1845 // Do not do any translation
1846 return false;
1847}
1848
Olli Etuaho16c745a2017-01-16 17:02:27 +00001849bool OutputHLSL::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
1850{
1851 TInfoSinkBase &out = getInfoSink();
1852
1853 ASSERT(visit == PreVisit);
Olli Etuaho1bb85282017-12-14 13:39:53 +02001854 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo()->getId());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001855 // Skip the prototype if it is not implemented (and thus not used)
1856 if (index == CallDAG::InvalidIndex)
1857 {
1858 return false;
1859 }
1860
1861 TIntermSequence *arguments = node->getSequence();
1862
Olli Etuahoff526f12017-06-30 12:26:54 +03001863 TString name = DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001864 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1865 << (mOutputLod0Function ? "Lod0(" : "(");
1866
1867 for (unsigned int i = 0; i < arguments->size(); i++)
1868 {
1869 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1870 ASSERT(symbol != nullptr);
1871
1872 out << argumentString(symbol);
1873
1874 if (i < arguments->size() - 1)
1875 {
1876 out << ", ";
1877 }
1878 }
1879
1880 out << ");\n";
1881
1882 // Also prototype the Lod0 variant if needed
1883 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1884 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1885 {
1886 mOutputLod0Function = true;
1887 node->traverse(this);
1888 mOutputLod0Function = false;
1889 }
1890
1891 return false;
1892}
1893
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1895{
Jamie Madill32aab012015-01-27 14:12:26 -05001896 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 switch (node->getOp())
1899 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001900 case EOpCallBuiltInFunction:
1901 case EOpCallFunctionInAST:
1902 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001904 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001905
Corentin Wallez1239ee92015-03-19 14:38:02 -07001906 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001907 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001909 if (node->isArray())
1910 {
1911 UNIMPLEMENTED();
1912 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001913 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001914 ASSERT(index != CallDAG::InvalidIndex);
1915 lod0 &= mASTMetadataList[index].mNeedsLod0;
1916
Olli Etuaho1bb85282017-12-14 13:39:53 +02001917 out << DecorateFunctionIfNeeded(TName(node->getFunction()));
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001918 out << DisambiguateFunctionName(node->getSequence());
1919 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001921 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001922 {
1923 // This path is used for internal functions that don't have their definitions in the
1924 // AST, such as precision emulation functions.
Olli Etuaho1bb85282017-12-14 13:39:53 +02001925 out << DecorateFunctionIfNeeded(TName(node->getFunction())) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001926 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001927 else if (node->getFunction()->isImageFunction())
Xinghua Cao711b7a12017-10-09 13:38:12 +08001928 {
Olli Etuaho1bb85282017-12-14 13:39:53 +02001929 const TString *name = node->getFunction()->name();
Xinghua Cao711b7a12017-10-09 13:38:12 +08001930 TType type = (*arguments)[0]->getAsTyped()->getType();
1931 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
Olli Etuaho1bb85282017-12-14 13:39:53 +02001932 *name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
Xinghua Cao711b7a12017-10-09 13:38:12 +08001933 type.getMemoryQualifier().readonly);
1934 out << imageFunctionName << "(";
1935 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001936 else
1937 {
Olli Etuaho1bb85282017-12-14 13:39:53 +02001938 const TString *name = node->getFunction()->name();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001939 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001940 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1941 if (arguments->size() > 1)
1942 {
1943 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1944 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001945 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
Olli Etuaho1bb85282017-12-14 13:39:53 +02001946 *name, samplerType, coords, arguments->size(), lod0, mShaderType);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001947 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001948 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001949
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001950 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001951 {
Olli Etuaho96963162016-03-21 11:54:33 +02001952 TIntermTyped *typedArg = (*arg)->getAsTyped();
1953 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001954 {
1955 out << "texture_";
1956 (*arg)->traverse(this);
1957 out << ", sampler_";
1958 }
1959
1960 (*arg)->traverse(this);
1961
Olli Etuaho96963162016-03-21 11:54:33 +02001962 if (typedArg->getType().isStructureContainingSamplers())
1963 {
1964 const TType &argType = typedArg->getType();
1965 TVector<TIntermSymbol *> samplerSymbols;
1966 TString structName = samplerNamePrefixFromStruct(typedArg);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001967 argType.createSamplerSymbols("angle_" + structName, "", &samplerSymbols,
1968 nullptr, mSymbolTable);
Olli Etuaho96963162016-03-21 11:54:33 +02001969 for (const TIntermSymbol *sampler : samplerSymbols)
1970 {
1971 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1972 {
1973 out << ", texture_" << sampler->getSymbol();
1974 out << ", sampler_" << sampler->getSymbol();
1975 }
1976 else
1977 {
1978 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1979 // of D3D9, it's the sampler variable.
1980 out << ", " + sampler->getSymbol();
1981 }
1982 }
1983 }
1984
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001985 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001986 {
1987 out << ", ";
1988 }
1989 }
1990
1991 out << ")";
1992
1993 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001994 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03001995 case EOpConstruct:
Olli Etuahobd3cd502017-11-03 15:48:52 +02001996 outputConstructor(out, visit, node);
Olli Etuaho8fab3202017-05-08 18:22:22 +03001997 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00001998 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001999 outputTriplet(out, visit, "(", " == ", ")");
2000 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002001 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002002 outputTriplet(out, visit, "(", " != ", ")");
2003 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002004 case EOpLessThanComponentWise:
2005 outputTriplet(out, visit, "(", " < ", ")");
2006 break;
2007 case EOpGreaterThanComponentWise:
2008 outputTriplet(out, visit, "(", " > ", ")");
2009 break;
2010 case EOpLessThanEqualComponentWise:
2011 outputTriplet(out, visit, "(", " <= ", ")");
2012 break;
2013 case EOpGreaterThanEqualComponentWise:
2014 outputTriplet(out, visit, "(", " >= ", ")");
2015 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002016 case EOpMod:
2017 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002018 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002019 break;
2020 case EOpModf:
2021 outputTriplet(out, visit, "modf(", ", ", ")");
2022 break;
2023 case EOpPow:
2024 outputTriplet(out, visit, "pow(", ", ", ")");
2025 break;
2026 case EOpAtan:
2027 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2028 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002029 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002030 break;
2031 case EOpMin:
2032 outputTriplet(out, visit, "min(", ", ", ")");
2033 break;
2034 case EOpMax:
2035 outputTriplet(out, visit, "max(", ", ", ")");
2036 break;
2037 case EOpClamp:
2038 outputTriplet(out, visit, "clamp(", ", ", ")");
2039 break;
2040 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302041 {
2042 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2043 if (lastParamNode->getType().getBasicType() == EbtBool)
2044 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002045 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2046 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302047 // so use emulated version.
2048 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002049 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302050 }
2051 else
2052 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002053 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302054 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002055 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302056 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002057 case EOpStep:
2058 outputTriplet(out, visit, "step(", ", ", ")");
2059 break;
2060 case EOpSmoothStep:
2061 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2062 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002063 case EOpFrexp:
2064 case EOpLdexp:
2065 ASSERT(node->getUseEmulatedFunction());
2066 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2067 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002068 case EOpDistance:
2069 outputTriplet(out, visit, "distance(", ", ", ")");
2070 break;
2071 case EOpDot:
2072 outputTriplet(out, visit, "dot(", ", ", ")");
2073 break;
2074 case EOpCross:
2075 outputTriplet(out, visit, "cross(", ", ", ")");
2076 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002077 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002078 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002079 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002080 break;
2081 case EOpReflect:
2082 outputTriplet(out, visit, "reflect(", ", ", ")");
2083 break;
2084 case EOpRefract:
2085 outputTriplet(out, visit, "refract(", ", ", ")");
2086 break;
2087 case EOpOuterProduct:
2088 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002089 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002090 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002091 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002092 outputTriplet(out, visit, "(", " * ", ")");
2093 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002094 case EOpBitfieldExtract:
2095 case EOpBitfieldInsert:
2096 case EOpUaddCarry:
2097 case EOpUsubBorrow:
2098 case EOpUmulExtended:
2099 case EOpImulExtended:
2100 ASSERT(node->getUseEmulatedFunction());
2101 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2102 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002103 default:
2104 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105 }
2106
2107 return true;
2108}
2109
Olli Etuaho57961272016-09-14 13:57:46 +03002110void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002112 out << "if (";
2113
2114 node->getCondition()->traverse(this);
2115
2116 out << ")\n";
2117
Jamie Madill8c46ab12015-12-07 16:39:19 -05002118 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002119
2120 bool discard = false;
2121
2122 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002124 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002125 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002126
Olli Etuahoa6f22092015-05-08 18:31:10 +03002127 // Detect true discard
2128 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2129 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002130 else
2131 {
2132 // TODO(oetuaho): Check if the semicolon inside is necessary.
2133 // It's there as a result of conservative refactoring of the output.
2134 out << "{;}\n";
2135 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002136
Jamie Madill8c46ab12015-12-07 16:39:19 -05002137 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002138
Olli Etuahoa6f22092015-05-08 18:31:10 +03002139 if (node->getFalseBlock())
2140 {
2141 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002142
Jamie Madill8c46ab12015-12-07 16:39:19 -05002143 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144
Olli Etuaho32db19b2016-10-04 14:43:16 +01002145 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002146 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002147
Jamie Madill8c46ab12015-12-07 16:39:19 -05002148 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002149
Olli Etuahoa6f22092015-05-08 18:31:10 +03002150 // Detect false discard
2151 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2152 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002153
Olli Etuahoa6f22092015-05-08 18:31:10 +03002154 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002155 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002156 {
2157 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002158 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002159}
2160
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002161bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2162{
2163 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2164 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2165 UNREACHABLE();
2166 return false;
2167}
2168
Olli Etuaho57961272016-09-14 13:57:46 +03002169bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002170{
2171 TInfoSinkBase &out = getInfoSink();
2172
Olli Etuaho3d932d82016-04-12 11:10:30 +03002173 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002174
2175 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002176 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002177 {
2178 out << "FLATTEN ";
2179 }
2180
Olli Etuaho57961272016-09-14 13:57:46 +03002181 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002182
2183 return false;
2184}
2185
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002186bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002187{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002188 TInfoSinkBase &out = getInfoSink();
2189
Olli Etuaho923ecef2017-10-11 12:01:38 +03002190 ASSERT(node->getStatementList());
2191 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002192 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002193 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002194 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002195 outputTriplet(out, visit, "switch (", ") ", "");
2196 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002197 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002198}
2199
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002200bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002201{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002202 TInfoSinkBase &out = getInfoSink();
2203
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002204 if (node->hasCondition())
2205 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002206 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002207 return true;
2208 }
2209 else
2210 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002211 out << "default:\n";
2212 return false;
2213 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002214}
2215
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002216void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2217{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002218 TInfoSinkBase &out = getInfoSink();
2219 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002220}
2221
2222bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2223{
Nicolas Capens655fe362014-04-11 13:12:34 -04002224 mNestedLoopDepth++;
2225
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002226 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002227 mInsideDiscontinuousLoop =
2228 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002229
Jamie Madill8c46ab12015-12-07 16:39:19 -05002230 TInfoSinkBase &out = getInfoSink();
2231
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002232 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002233 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002234 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002235 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002236 mInsideDiscontinuousLoop = wasDiscontinuous;
2237 mNestedLoopDepth--;
2238
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002239 return false;
2240 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002241 }
2242
Corentin Wallez1239ee92015-03-19 14:38:02 -07002243 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002244 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002246 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002247
Jamie Madill8c46ab12015-12-07 16:39:19 -05002248 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002249 }
2250 else
2251 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002252 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002253
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 if (node->getInit())
2255 {
2256 node->getInit()->traverse(this);
2257 }
2258
2259 out << "; ";
2260
alokp@chromium.org52813552010-11-16 18:36:09 +00002261 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002263 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 }
2265
2266 out << "; ";
2267
alokp@chromium.org52813552010-11-16 18:36:09 +00002268 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002270 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002271 }
2272
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002273 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002274
Jamie Madill8c46ab12015-12-07 16:39:19 -05002275 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 }
2277
2278 if (node->getBody())
2279 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002280 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002281 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002283 else
2284 {
2285 // TODO(oetuaho): Check if the semicolon inside is necessary.
2286 // It's there as a result of conservative refactoring of the output.
2287 out << "{;}\n";
2288 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289
Jamie Madill8c46ab12015-12-07 16:39:19 -05002290 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291
alokp@chromium.org52813552010-11-16 18:36:09 +00002292 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002294 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002295 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296
alokp@chromium.org52813552010-11-16 18:36:09 +00002297 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002299 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300 }
2301
daniel@transgaming.com73536982012-03-21 20:45:49 +00002302 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002304 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002305 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002306
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307 return false;
2308}
2309
2310bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2311{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002312 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002314 TInfoSinkBase &out = getInfoSink();
2315
2316 switch (node->getFlowOp())
2317 {
2318 case EOpKill:
2319 out << "discard";
2320 break;
2321 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002322 if (mNestedLoopDepth > 1)
2323 {
2324 mUsesNestedBreak = true;
2325 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002326
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002327 if (mExcessiveLoopIndex)
2328 {
2329 out << "{Break";
2330 mExcessiveLoopIndex->traverse(this);
2331 out << " = true; break;}\n";
2332 }
2333 else
2334 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002335 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002336 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002337 break;
2338 case EOpContinue:
2339 out << "continue";
2340 break;
2341 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002342 if (node->getExpression())
2343 {
2344 out << "return ";
2345 }
2346 else
2347 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002348 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002349 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002350 break;
2351 default:
2352 UNREACHABLE();
2353 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002354 }
2355
2356 return true;
2357}
2358
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002359// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002360// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2361// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002362bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002363{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002364 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002365
2366 // Parse loops of the form:
2367 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002368 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002369 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002370 int initial = 0;
2371 int limit = 0;
2372 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002373
2374 // Parse index name and intial value
2375 if (node->getInit())
2376 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002377 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002378
2379 if (init)
2380 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002381 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002382 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002383
2384 if (variable && variable->getQualifier() == EvqTemporary)
2385 {
2386 TIntermBinary *assign = variable->getAsBinaryNode();
2387
2388 if (assign->getOp() == EOpInitialize)
2389 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002390 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002391 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2392
2393 if (symbol && constant)
2394 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002395 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002396 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002397 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002398 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002399 }
2400 }
2401 }
2402 }
2403 }
2404 }
2405
2406 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002407 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002408 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002409 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002410
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002411 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2412 {
2413 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2414
2415 if (constant)
2416 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002417 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002418 {
2419 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002420 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002421 }
2422 }
2423 }
2424 }
2425
2426 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002427 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002428 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002429 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002430 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002431
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002432 if (binaryTerminal)
2433 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002434 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002435 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2436
2437 if (constant)
2438 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002439 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002440 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002441 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002442
2443 switch (op)
2444 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002445 case EOpAddAssign:
2446 increment = value;
2447 break;
2448 case EOpSubAssign:
2449 increment = -value;
2450 break;
2451 default:
2452 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002453 }
2454 }
2455 }
2456 }
2457 else if (unaryTerminal)
2458 {
2459 TOperator op = unaryTerminal->getOp();
2460
2461 switch (op)
2462 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002463 case EOpPostIncrement:
2464 increment = 1;
2465 break;
2466 case EOpPostDecrement:
2467 increment = -1;
2468 break;
2469 case EOpPreIncrement:
2470 increment = 1;
2471 break;
2472 case EOpPreDecrement:
2473 increment = -1;
2474 break;
2475 default:
2476 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002477 }
2478 }
2479 }
2480
Yunchao He4f285442017-04-21 12:15:49 +08002481 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002482 {
2483 if (comparator == EOpLessThanEqual)
2484 {
2485 comparator = EOpLessThan;
2486 limit += 1;
2487 }
2488
2489 if (comparator == EOpLessThan)
2490 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002491 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002492
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002493 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002494 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002495 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002496 }
2497
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002498 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002499 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002500
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002501 out << "{int ";
2502 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002503 out << ";\n"
2504 "bool Break";
2505 index->traverse(this);
2506 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002507
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002508 bool firstLoopFragment = true;
2509
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002510 while (iterations > 0)
2511 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002512 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002513
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002514 if (!firstLoopFragment)
2515 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002516 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002517 index->traverse(this);
2518 out << ") {\n";
2519 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002520
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002521 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002522 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002523 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002524 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002525
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002526 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002527 const char *unroll =
2528 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002529
Corentin Wallez1239ee92015-03-19 14:38:02 -07002530 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002531 index->traverse(this);
2532 out << " = ";
2533 out << initial;
2534
2535 out << "; ";
2536 index->traverse(this);
2537 out << " < ";
2538 out << clampedLimit;
2539
2540 out << "; ";
2541 index->traverse(this);
2542 out << " += ";
2543 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002544 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002545
Jamie Madill8c46ab12015-12-07 16:39:19 -05002546 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002547 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002548
2549 if (node->getBody())
2550 {
2551 node->getBody()->traverse(this);
2552 }
2553
Jamie Madill8c46ab12015-12-07 16:39:19 -05002554 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002555 out << ";}\n";
2556
2557 if (!firstLoopFragment)
2558 {
2559 out << "}\n";
2560 }
2561
2562 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002563
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002564 initial += MAX_LOOP_ITERATIONS * increment;
2565 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002566 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002567
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002568 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002569
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002570 mExcessiveLoopIndex = restoreIndex;
2571
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002572 return true;
2573 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002574 else
2575 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002576 }
2577
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002578 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002579}
2580
Jamie Madill8c46ab12015-12-07 16:39:19 -05002581void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2582 Visit visit,
2583 const char *preString,
2584 const char *inString,
2585 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002586{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002587 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002588 {
2589 out << preString;
2590 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002591 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592 {
2593 out << inString;
2594 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002595 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002596 {
2597 out << postString;
2598 }
2599}
2600
Jamie Madill8c46ab12015-12-07 16:39:19 -05002601void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002602{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002603 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002604 {
Jamie Madill32aab012015-01-27 14:12:26 -05002605 out << "\n";
2606 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002607
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002608 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002609 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002610 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002611 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002612
Jamie Madill32aab012015-01-27 14:12:26 -05002613 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002614 }
2615}
2616
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002617TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2618{
2619 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002620 const TType &type = symbol->getType();
2621 const TName &name = symbol->getName();
2622 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002623
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002624 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002625 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002626 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002627 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002628 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002629 {
Olli Etuahoff526f12017-06-30 12:26:54 +03002630 nameStr = DecorateVariableIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002631 }
2632
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002633 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002634 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002635 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2636 {
2637 // Samplers are passed as indices to the sampler array.
2638 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2639 return "const uint " + nameStr + ArrayString(type);
2640 }
2641 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2642 {
2643 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2644 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2645 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2646 ArrayString(type);
2647 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002648 }
2649
Olli Etuaho96963162016-03-21 11:54:33 +02002650 TStringStream argString;
2651 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2652 << ArrayString(type);
2653
2654 // If the structure parameter contains samplers, they need to be passed into the function as
2655 // separate parameters. HLSL doesn't natively support samplers in structs.
2656 if (type.isStructureContainingSamplers())
2657 {
2658 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2659 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002660 type.createSamplerSymbols("angle" + nameStr, "", &samplerSymbols, nullptr, mSymbolTable);
Olli Etuaho96963162016-03-21 11:54:33 +02002661 for (const TIntermSymbol *sampler : samplerSymbols)
2662 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002663 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002664 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2665 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002666 argString << ", const uint " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002667 }
2668 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2669 {
Olli Etuaho96963162016-03-21 11:54:33 +02002670 ASSERT(IsSampler(samplerType.getBasicType()));
2671 argString << ", " << QualifierString(qualifier) << " "
2672 << TextureString(samplerType.getBasicType()) << " texture_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002673 << sampler->getSymbol() << ArrayString(samplerType) << ", "
2674 << QualifierString(qualifier) << " "
Olli Etuaho96963162016-03-21 11:54:33 +02002675 << SamplerString(samplerType.getBasicType()) << " sampler_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002676 << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002677 }
2678 else
2679 {
Olli Etuaho96963162016-03-21 11:54:33 +02002680 ASSERT(IsSampler(samplerType.getBasicType()));
2681 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
Olli Etuaho28839f02017-08-15 11:38:16 +03002682 << " " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002683 }
2684 }
2685 }
2686
2687 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688}
2689
2690TString OutputHLSL::initializer(const TType &type)
2691{
2692 TString string;
2693
Jamie Madill94bf7f22013-07-08 13:31:15 -04002694 size_t size = type.getObjectSize();
2695 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002696 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002697 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002698
Jamie Madill94bf7f22013-07-08 13:31:15 -04002699 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002700 {
2701 string += ", ";
2702 }
2703 }
2704
daniel@transgaming.comead23042010-04-29 03:35:36 +00002705 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002706}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002707
Olli Etuahobd3cd502017-11-03 15:48:52 +02002708void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002709{
Olli Etuahobd3cd502017-11-03 15:48:52 +02002710 // Array constructors should have been already pruned from the code.
2711 ASSERT(!node->getType().isArray());
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002712
2713 if (visit == PreVisit)
2714 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002715 TString constructorName;
2716 if (node->getBasicType() == EbtStruct)
2717 {
2718 constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct());
2719 }
2720 else
2721 {
2722 constructorName =
2723 mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence());
2724 }
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002725 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002726 }
2727 else if (visit == InVisit)
2728 {
2729 out << ", ";
2730 }
2731 else if (visit == PostVisit)
2732 {
2733 out << ")";
2734 }
2735}
2736
Jamie Madill8c46ab12015-12-07 16:39:19 -05002737const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2738 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002739 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002740{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002741 const TConstantUnion *constUnionIterated = constUnion;
2742
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002743 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002744 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002745 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002746 out << mStructureHLSL->addStructConstructor(*structure) << "(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002747
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002748 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002749
Jamie Madill98493dd2013-07-08 14:39:03 -04002750 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002751 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002752 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002753 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002754
Jamie Madill98493dd2013-07-08 14:39:03 -04002755 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002756 {
2757 out << ", ";
2758 }
2759 }
2760
2761 out << ")";
2762 }
2763 else
2764 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002765 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002766 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002767
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002768 if (writeType)
2769 {
Jamie Madill033dae62014-06-18 12:56:28 -04002770 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002771 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002772 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002773 if (writeType)
2774 {
2775 out << ")";
2776 }
2777 }
2778
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002779 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002780}
2781
Olli Etuahod68924e2017-01-02 17:34:40 +00002782void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002783{
Olli Etuahod68924e2017-01-02 17:34:40 +00002784 if (visit == PreVisit)
2785 {
2786 const char *opStr = GetOperatorString(op);
2787 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2788 out << "(";
2789 }
2790 else
2791 {
2792 outputTriplet(out, visit, nullptr, ", ", ")");
2793 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002794}
2795
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002796bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2797 TIntermSymbol *symbolNode,
2798 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002799{
2800 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2801 expression->traverse(&searchSymbol);
2802
2803 if (searchSymbol.foundMatch())
2804 {
2805 // Type already printed
2806 out << "t" + str(mUniqueIndex) + " = ";
2807 expression->traverse(this);
2808 out << ", ";
2809 symbolNode->traverse(this);
2810 out << " = t" + str(mUniqueIndex);
2811
2812 mUniqueIndex++;
2813 return true;
2814 }
2815
2816 return false;
2817}
2818
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002819bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2820{
2821 // We support writing constant unions and constructors that only take constant unions as
2822 // parameters as HLSL literals.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002823 return !expression->getType().isArrayOfArrays() &&
2824 (expression->getAsConstantUnion() ||
2825 expression->isConstructorWithOnlyConstantUnionParameters());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002826}
2827
2828bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2829 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002830 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002831{
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002832 if (canWriteAsHLSLLiteral(initializer))
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002833 {
2834 symbolNode->traverse(this);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002835 ASSERT(!symbolNode->getType().isArrayOfArrays());
2836 if (symbolNode->getType().isArray())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002837 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002838 out << "[" << symbolNode->getType().getOutermostArraySize() << "]";
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002839 }
2840 out << " = {";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002841 if (initializer->getAsConstantUnion())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002842 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002843 TIntermConstantUnion *nodeConst = initializer->getAsConstantUnion();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002844 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002845 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002846 }
2847 else
2848 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002849 TIntermAggregate *constructor = initializer->getAsAggregate();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002850 ASSERT(constructor != nullptr);
2851 for (TIntermNode *&node : *constructor->getSequence())
2852 {
2853 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2854 ASSERT(nodeConst);
2855 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002856 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002857 if (node != constructor->getSequence()->back())
2858 {
2859 out << ", ";
2860 }
2861 }
2862 }
2863 out << "}";
2864 return true;
2865 }
2866 return false;
2867}
2868
Jamie Madill55e79e02015-02-09 15:35:00 -05002869TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2870{
2871 const TFieldList &fields = structure.fields();
2872
2873 for (const auto &eqFunction : mStructEqualityFunctions)
2874 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002875 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002876 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002877 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002878 }
2879 }
2880
2881 const TString &structNameString = StructNameString(structure);
2882
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002883 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002884 function->structure = &structure;
2885 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002886
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002887 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002888
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002889 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2890 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002891 << "{\n"
2892 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002893
2894 for (size_t i = 0; i < fields.size(); i++)
2895 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002896 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002897 const TType *fieldType = field->type();
2898
2899 const TString &fieldNameA = "a." + Decorate(field->name());
2900 const TString &fieldNameB = "b." + Decorate(field->name());
2901
2902 if (i > 0)
2903 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002904 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002905 }
2906
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002907 fnOut << "(";
2908 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2909 fnOut << fieldNameA;
2910 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2911 fnOut << fieldNameB;
2912 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2913 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002914 }
2915
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002916 fnOut << ";\n"
2917 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002918
2919 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002920
2921 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002922 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002923
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002924 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002925}
2926
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002927TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002928{
2929 for (const auto &eqFunction : mArrayEqualityFunctions)
2930 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002931 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002932 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002933 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002934 }
2935 }
2936
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002937 TType elementType(type);
2938 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002939
Olli Etuaho12690762015-03-31 12:55:28 +03002940 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002941 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002942
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002943 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002944
2945 TInfoSinkBase fnOut;
2946
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002947 const TString &typeName = TypeString(type);
2948 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2949 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002950 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002951 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002952 << type.getOutermostArraySize()
2953 << "; ++i)\n"
2954 " {\n"
2955 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002956
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002957 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002958 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002959 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002960 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002961 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002962
2963 fnOut << ") { return false; }\n"
2964 " }\n"
2965 " return true;\n"
2966 "}\n";
2967
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002968 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002969
2970 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002971 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002972
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002973 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002974}
2975
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002976TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002977{
2978 for (const auto &assignFunction : mArrayAssignmentFunctions)
2979 {
2980 if (assignFunction.type == type)
2981 {
2982 return assignFunction.functionName;
2983 }
2984 }
2985
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002986 TType elementType(type);
2987 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03002988
2989 ArrayHelperFunction function;
2990 function.type = type;
2991
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002992 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03002993
2994 TInfoSinkBase fnOut;
2995
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002996 const TString &typeName = TypeString(type);
2997 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
2998 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002999 << "{\n"
3000 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003001 << type.getOutermostArraySize()
3002 << "; ++i)\n"
3003 " {\n"
3004 " ";
3005
3006 outputAssign(PreVisit, elementType, fnOut);
3007 fnOut << "a[i]";
3008 outputAssign(InVisit, elementType, fnOut);
3009 fnOut << "b[i]";
3010 outputAssign(PostVisit, elementType, fnOut);
3011
3012 fnOut << ";\n"
3013 " }\n"
3014 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03003015
3016 function.functionDefinition = fnOut.c_str();
3017
3018 mArrayAssignmentFunctions.push_back(function);
3019
3020 return function.functionName;
3021}
3022
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003023TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003024{
3025 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3026 {
3027 if (constructIntoFunction.type == type)
3028 {
3029 return constructIntoFunction.functionName;
3030 }
3031 }
3032
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003033 TType elementType(type);
3034 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003035
3036 ArrayHelperFunction function;
3037 function.type = type;
3038
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003039 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003040
3041 TInfoSinkBase fnOut;
3042
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003043 const TString &typeName = TypeString(type);
3044 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3045 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003046 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003047 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003048 }
3049 fnOut << ")\n"
3050 "{\n";
3051
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003052 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003053 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003054 fnOut << " ";
3055 outputAssign(PreVisit, elementType, fnOut);
3056 fnOut << "a[" << i << "]";
3057 outputAssign(InVisit, elementType, fnOut);
3058 fnOut << "b" << i;
3059 outputAssign(PostVisit, elementType, fnOut);
3060 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003061 }
3062 fnOut << "}\n";
3063
3064 function.functionDefinition = fnOut.c_str();
3065
3066 mArrayConstructIntoFunctions.push_back(function);
3067
3068 return function.functionName;
3069}
3070
Jamie Madill2e295e22015-04-29 10:41:33 -04003071void OutputHLSL::ensureStructDefined(const TType &type)
3072{
Olli Etuahobd3cd502017-11-03 15:48:52 +02003073 const TStructure *structure = type.getStruct();
Jamie Madill2e295e22015-04-29 10:41:33 -04003074 if (structure)
3075 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02003076 ASSERT(type.getBasicType() == EbtStruct);
3077 mStructureHLSL->ensureStructDefined(*structure);
Jamie Madill2e295e22015-04-29 10:41:33 -04003078 }
3079}
3080
Jamie Madill45bcc782016-11-07 13:58:48 -05003081} // namespace sh