blob: f3da9a485d67b4050c39b32b632ec4bd01f74fa3 [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"
Olli Etuaho4728bdc2017-12-20 17:51:08 +020018#include "compiler/translator/FindSymbolNode.h"
Xinghua Cao711b7a12017-10-09 13:38:12 +080019#include "compiler/translator/ImageFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050020#include "compiler/translator/InfoSink.h"
21#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020022#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050023#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;
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300194 mTextureFunctionHLSL = new TextureFunctionHLSL;
Xinghua Cao711b7a12017-10-09 13:38:12 +0800195 mImageFunctionHLSL = new ImageFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400196
Olli Etuahod8724a92017-12-29 18:40:36 +0200197 unsigned int firstUniformRegister =
198 ((compileOptions & SH_SKIP_D3D_CONSTANT_REGISTER_ZERO) != 0) ? 1u : 0u;
199 mUniformHLSL =
200 new UniformHLSL(shaderType, mStructureHLSL, outputType, uniforms, firstUniformRegister);
201
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200202 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000203 {
Arun Patole63419392015-03-13 11:51:07 +0530204 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500205 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
206 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530207 // In both cases total 3 uniform registers need to be reserved.
208 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000209 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000210
Geoff Lang00140f42016-02-03 18:47:33 +0000211 // Reserve registers for the default uniform block and driver constants
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800212 mUniformHLSL->reserveUniformBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000213}
214
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000215OutputHLSL::~OutputHLSL()
216{
Jamie Madill8daaba12014-06-13 10:04:33 -0400217 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400218 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300219 SafeDelete(mTextureFunctionHLSL);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800220 SafeDelete(mImageFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200221 for (auto &eqFunction : mStructEqualityFunctions)
222 {
223 SafeDelete(eqFunction);
224 }
225 for (auto &eqFunction : mArrayEqualityFunctions)
226 {
227 SafeDelete(eqFunction);
228 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000229}
230
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200231void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000232{
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200233 BuiltInFunctionEmulator builtInFunctionEmulator;
234 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800235 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
236 {
237 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
238 mShaderVersion);
239 }
240
Olli Etuahodfa75e82017-01-23 09:43:06 -0800241 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500242
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700243 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000244 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700245 ASSERT(success == CallDAG::INITDAG_SUCCESS);
246 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700247
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200248 const std::vector<MappedStruct> std140Structs = FlagStd140Structs(treeRoot);
249 // TODO(oetuaho): The std140Structs could be filtered based on which ones actually get used in
250 // the shader code. When we add shader storage blocks we might also consider an alternative
251 // solution, since the struct mapping won't work very well for shader storage blocks.
252
Jamie Madill37997142015-01-28 10:06:34 -0500253 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500254 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200255 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500256 mInfoSinkStack.pop();
257
Jamie Madill37997142015-01-28 10:06:34 -0500258 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500259 mInfoSinkStack.pop();
260
Jamie Madill32aab012015-01-27 14:12:26 -0500261 mInfoSinkStack.push(&mHeader);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200262 header(mHeader, std140Structs, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500263 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000264
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200265 objSink << mHeader.c_str();
266 objSink << mBody.c_str();
267 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200268
Olli Etuahodfa75e82017-01-23 09:43:06 -0800269 builtInFunctionEmulator.cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000270}
271
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800272const std::map<std::string, unsigned int> &OutputHLSL::getUniformBlockRegisterMap() const
Jamie Madill4e1fd412014-07-10 17:50:10 -0400273{
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800274 return mUniformHLSL->getUniformBlockRegisterMap();
Jamie Madill4e1fd412014-07-10 17:50:10 -0400275}
276
Jamie Madill9fe25e92014-07-18 10:33:08 -0400277const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
278{
279 return mUniformHLSL->getUniformRegisterMap();
280}
281
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200282TString OutputHLSL::structInitializerString(int indent,
283 const TType &type,
284 const TString &name) const
Jamie Madill570e04d2013-06-21 09:15:33 -0400285{
286 TString init;
287
Olli Etuahoed049ab2017-06-30 17:38:33 +0300288 TString indentString;
289 for (int spaces = 0; spaces < indent; spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400290 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300291 indentString += " ";
Jamie Madill570e04d2013-06-21 09:15:33 -0400292 }
293
Olli Etuahoed049ab2017-06-30 17:38:33 +0300294 if (type.isArray())
Jamie Madill570e04d2013-06-21 09:15:33 -0400295 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300296 init += indentString + "{\n";
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300297 for (unsigned int arrayIndex = 0u; arrayIndex < type.getOutermostArraySize(); ++arrayIndex)
Jamie Madill570e04d2013-06-21 09:15:33 -0400298 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300299 TStringStream indexedString;
300 indexedString << name << "[" << arrayIndex << "]";
301 TType elementType = type;
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300302 elementType.toArrayElementType();
Olli Etuahoed049ab2017-06-30 17:38:33 +0300303 init += structInitializerString(indent + 1, elementType, indexedString.str());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300304 if (arrayIndex < type.getOutermostArraySize() - 1)
Olli Etuahoed049ab2017-06-30 17:38:33 +0300305 {
306 init += ",";
307 }
308 init += "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400309 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300310 init += indentString + "}";
Jamie Madill570e04d2013-06-21 09:15:33 -0400311 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300312 else if (type.getBasicType() == EbtStruct)
313 {
314 init += indentString + "{\n";
315 const TStructure &structure = *type.getStruct();
316 const TFieldList &fields = structure.fields();
317 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
318 {
319 const TField &field = *fields[fieldIndex];
320 const TString &fieldName = name + "." + Decorate(field.name());
321 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400322
Olli Etuahoed049ab2017-06-30 17:38:33 +0300323 init += structInitializerString(indent + 1, fieldType, fieldName);
324 if (fieldIndex < fields.size() - 1)
325 {
326 init += ",";
327 }
328 init += "\n";
329 }
330 init += indentString + "}";
331 }
332 else
333 {
334 init += indentString + name;
335 }
Jamie Madill570e04d2013-06-21 09:15:33 -0400336
337 return init;
338}
339
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200340TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std140Structs) const
341{
342 TString mappedStructs;
343
344 for (auto &mappedStruct : std140Structs)
345 {
346 TInterfaceBlock *interfaceBlock =
347 mappedStruct.blockDeclarator->getType().getInterfaceBlock();
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200348 const TName &instanceName = mappedStruct.blockDeclarator->getName();
Olli Etuaho93b059d2017-12-20 12:46:58 +0200349 if (mReferencedUniformBlocks.count(interfaceBlock->uniqueId().get()) == 0)
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200350 {
351 continue;
352 }
353
354 unsigned int instanceCount = 1u;
355 bool isInstanceArray = mappedStruct.blockDeclarator->isArray();
356 if (isInstanceArray)
357 {
358 instanceCount = mappedStruct.blockDeclarator->getOutermostArraySize();
359 }
360
361 for (unsigned int instanceArrayIndex = 0; instanceArrayIndex < instanceCount;
362 ++instanceArrayIndex)
363 {
364 TString originalName;
365 TString mappedName("map");
366
367 if (instanceName.getString() != "")
368 {
369 unsigned int instanceStringArrayIndex = GL_INVALID_INDEX;
370 if (isInstanceArray)
371 instanceStringArrayIndex = instanceArrayIndex;
Olli Etuaho12a18ad2017-12-01 16:59:47 +0200372 TString instanceString = mUniformHLSL->UniformBlockInstanceString(
373 instanceName.getString(), instanceStringArrayIndex);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200374 originalName += instanceString;
375 mappedName += instanceString;
376 originalName += ".";
377 mappedName += "_";
378 }
379
380 TString fieldName = Decorate(mappedStruct.field->name());
381 originalName += fieldName;
382 mappedName += fieldName;
383
384 TType *structType = mappedStruct.field->type();
385 mappedStructs +=
Olli Etuahobed35d72017-12-20 16:36:26 +0200386 "static " + Decorate(structType->getStruct()->name()) + " " + mappedName;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200387
388 if (structType->isArray())
389 {
390 mappedStructs += ArrayString(*mappedStruct.field->type());
391 }
392
393 mappedStructs += " =\n";
394 mappedStructs += structInitializerString(0, *structType, originalName);
395 mappedStructs += ";\n";
396 }
397 }
398 return mappedStructs;
399}
400
401void OutputHLSL::header(TInfoSinkBase &out,
402 const std::vector<MappedStruct> &std140Structs,
403 const BuiltInFunctionEmulator *builtInFunctionEmulator) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000405 TString varyings;
406 TString attributes;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200407 TString mappedStructs = generateStructMapping(std140Structs);
Jamie Madill570e04d2013-06-21 09:15:33 -0400408
Olli Etuahob8cb9392017-12-20 14:23:19 +0200409 for (const auto &varying : mReferencedVaryings)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000410 {
Olli Etuahob8cb9392017-12-20 14:23:19 +0200411 const TType &type = varying.second->variable().getType();
412 const TString &name = varying.second->getSymbol();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000413
414 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500415 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
416 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000417 }
418
Olli Etuaho93b059d2017-12-20 12:46:58 +0200419 for (const auto &attribute : mReferencedAttributes)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000420 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200421 const TType &type = attribute.second->getType();
422 const TString &name = attribute.second->getSymbol();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000423
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500424 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
425 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000426 }
427
Jamie Madill8daaba12014-06-13 10:04:33 -0400428 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400429
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300430 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms, mSymbolTable);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800431 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400432
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200433 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500434 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200435 out << "\n// Equality functions\n\n";
436 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500437 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200438 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200439 }
440 }
Olli Etuaho12690762015-03-31 12:55:28 +0300441 if (!mArrayAssignmentFunctions.empty())
442 {
443 out << "\n// Assignment functions\n\n";
444 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
445 {
446 out << assignmentFunction.functionDefinition << "\n";
447 }
448 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300449 if (!mArrayConstructIntoFunctions.empty())
450 {
451 out << "\n// Array constructor functions\n\n";
452 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
453 {
454 out << constructIntoFunction.functionDefinition << "\n";
455 }
456 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200457
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500458 if (mUsesDiscardRewriting)
459 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400460 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500461 }
462
Nicolas Capens655fe362014-04-11 13:12:34 -0400463 if (mUsesNestedBreak)
464 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400465 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400466 }
467
Arun Patole44efa0b2015-03-04 17:11:05 +0530468 if (mRequiresIEEEStrictCompiling)
469 {
470 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
471 }
472
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400473 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
474 "#define LOOP [loop]\n"
475 "#define FLATTEN [flatten]\n"
476 "#else\n"
477 "#define LOOP\n"
478 "#define FLATTEN\n"
479 "#endif\n";
480
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200481 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300483 const bool usingMRTExtension =
484 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000485
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000486 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500487 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400488 out << "\n";
489
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200490 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000491 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200492 for (const auto &outputVariable : mReferencedOutputVariables)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000493 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200494 const TString &variableName = outputVariable.second->getSymbol();
495 const TType &variableType = outputVariable.second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400496
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500497 out << "static " + TypeString(variableType) + " out_" + variableName +
498 ArrayString(variableType) + " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000499 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000500 }
Jamie Madill46131a32013-06-20 11:55:50 -0400501 else
502 {
503 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
504
505 out << "static float4 gl_Color[" << numColorValues << "] =\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500506 "{\n";
Jamie Madill46131a32013-06-20 11:55:50 -0400507 for (unsigned int i = 0; i < numColorValues; i++)
508 {
509 out << " float4(0, 0, 0, 0)";
510 if (i + 1 != numColorValues)
511 {
512 out << ",";
513 }
514 out << "\n";
515 }
516
517 out << "};\n";
518 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000519
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400520 if (mUsesFragDepth)
521 {
522 out << "static float gl_Depth = 0.0;\n";
523 }
524
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000525 if (mUsesFragCoord)
526 {
527 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
528 }
529
530 if (mUsesPointCoord)
531 {
532 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
533 }
534
535 if (mUsesFrontFacing)
536 {
537 out << "static bool gl_FrontFacing = false;\n";
538 }
539
540 out << "\n";
541
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000542 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000543 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000544 out << "struct gl_DepthRangeParameters\n"
545 "{\n"
546 " float near;\n"
547 " float far;\n"
548 " float diff;\n"
549 "};\n"
550 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000551 }
552
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200553 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000554 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000555 out << "cbuffer DriverConstants : register(b1)\n"
556 "{\n";
557
558 if (mUsesDepthRange)
559 {
560 out << " float3 dx_DepthRange : packoffset(c0);\n";
561 }
562
563 if (mUsesFragCoord)
564 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000565 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000566 }
567
568 if (mUsesFragCoord || mUsesFrontFacing)
569 {
570 out << " float3 dx_DepthFront : packoffset(c2);\n";
571 }
572
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800573 if (mUsesFragCoord)
574 {
575 // dx_ViewScale is only used in the fragment shader to correct
576 // the value for glFragCoord if necessary
577 out << " float2 dx_ViewScale : packoffset(c3);\n";
578 }
579
Martin Radev72b4e1e2017-08-31 15:42:56 +0300580 if (mHasMultiviewExtensionEnabled)
581 {
582 // We have to add a value which we can use to keep track of which multi-view code
583 // path is to be selected in the GS.
584 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
585 }
586
Olli Etuaho618bebc2016-01-15 16:40:00 +0200587 if (mOutputType == SH_HLSL_4_1_OUTPUT)
588 {
589 mUniformHLSL->samplerMetadataUniforms(out, "c4");
590 }
591
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000592 out << "};\n";
593 }
594 else
595 {
596 if (mUsesDepthRange)
597 {
598 out << "uniform float3 dx_DepthRange : register(c0);";
599 }
600
601 if (mUsesFragCoord)
602 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000603 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000604 }
605
606 if (mUsesFragCoord || mUsesFrontFacing)
607 {
608 out << "uniform float3 dx_DepthFront : register(c2);\n";
609 }
610 }
611
612 out << "\n";
613
614 if (mUsesDepthRange)
615 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500616 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
617 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000618 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000619 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000620
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200621 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000622 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200623 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000624 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200625 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400626 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000627 }
628
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000629 if (usingMRTExtension && mNumRenderTargets > 1)
630 {
631 out << "#define GL_USES_MRT\n";
632 }
633
634 if (mUsesFragColor)
635 {
636 out << "#define GL_USES_FRAG_COLOR\n";
637 }
638
639 if (mUsesFragData)
640 {
641 out << "#define GL_USES_FRAG_DATA\n";
642 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643 }
Xinghua Caob1239382016-12-13 15:07:05 +0800644 else if (mShaderType == GL_VERTEX_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000646 out << "// Attributes\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500647 out << attributes;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000648 out << "\n"
649 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400650
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000651 if (mUsesPointSize)
652 {
653 out << "static float gl_PointSize = float(1);\n";
654 }
655
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000656 if (mUsesInstanceID)
657 {
658 out << "static int gl_InstanceID;";
659 }
660
Corentin Wallezb076add2016-01-11 16:45:46 -0500661 if (mUsesVertexID)
662 {
663 out << "static int gl_VertexID;";
664 }
665
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000666 out << "\n"
667 "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500668 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000669 out << "\n";
670
671 if (mUsesDepthRange)
672 {
673 out << "struct gl_DepthRangeParameters\n"
674 "{\n"
675 " float near;\n"
676 " float far;\n"
677 " float diff;\n"
678 "};\n"
679 "\n";
680 }
681
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200682 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000683 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800684 out << "cbuffer DriverConstants : register(b1)\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500685 "{\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800686
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000687 if (mUsesDepthRange)
688 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800689 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000690 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800691
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800692 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
693 // shaders. However, we declare it for all shaders (including Feature Level 10+).
694 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
695 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800696 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800697 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800698 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800699
Martin Radev72b4e1e2017-08-31 15:42:56 +0300700 if (mHasMultiviewExtensionEnabled)
701 {
702 // We have to add a value which we can use to keep track of which multi-view code
703 // path is to be selected in the GS.
704 out << " float multiviewSelectViewportIndex : packoffset(c3.z);\n";
705 }
706
Olli Etuaho618bebc2016-01-15 16:40:00 +0200707 if (mOutputType == SH_HLSL_4_1_OUTPUT)
708 {
709 mUniformHLSL->samplerMetadataUniforms(out, "c4");
710 }
711
Austin Kinross4fd18b12014-12-22 12:32:05 -0800712 out << "};\n"
713 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000714 }
715 else
716 {
717 if (mUsesDepthRange)
718 {
719 out << "uniform float3 dx_DepthRange : register(c0);\n";
720 }
721
Cooper Partine6664f02015-01-09 16:22:24 -0800722 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
723 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000724 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000725 }
726
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000727 if (mUsesDepthRange)
728 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500729 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, "
730 "dx_DepthRange.y, dx_DepthRange.z};\n"
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000731 "\n";
732 }
733
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200734 if (!mappedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000735 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200736 out << "// Structures from std140 blocks with padding removed\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000737 out << "\n";
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200738 out << mappedStructs;
Jamie Madillf91ce812014-06-13 10:04:34 -0400739 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000740 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400741 }
Xinghua Caob1239382016-12-13 15:07:05 +0800742 else // Compute shader
743 {
744 ASSERT(mShaderType == GL_COMPUTE_SHADER);
Xinghua Cao73badc02017-03-29 19:14:53 +0800745
746 out << "cbuffer DriverConstants : register(b1)\n"
747 "{\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800748 if (mUsesNumWorkGroups)
749 {
Xinghua Caob1239382016-12-13 15:07:05 +0800750 out << " uint3 gl_NumWorkGroups : packoffset(c0);\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800751 }
Xinghua Cao73badc02017-03-29 19:14:53 +0800752 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
753 mUniformHLSL->samplerMetadataUniforms(out, "c1");
754 out << "};\n";
Xinghua Caob1239382016-12-13 15:07:05 +0800755
756 // Follow built-in variables would be initialized in
757 // DynamicHLSL::generateComputeShaderLinkHLSL, if they
758 // are used in compute shader.
759 if (mUsesWorkGroupID)
760 {
761 out << "static uint3 gl_WorkGroupID = uint3(0, 0, 0);\n";
762 }
763
764 if (mUsesLocalInvocationID)
765 {
766 out << "static uint3 gl_LocalInvocationID = uint3(0, 0, 0);\n";
767 }
768
769 if (mUsesGlobalInvocationID)
770 {
771 out << "static uint3 gl_GlobalInvocationID = uint3(0, 0, 0);\n";
772 }
773
774 if (mUsesLocalInvocationIndex)
775 {
776 out << "static uint gl_LocalInvocationIndex = uint(0);\n";
777 }
778 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000779
Geoff Lang1fe74c72016-08-25 13:23:01 -0400780 bool getDimensionsIgnoresBaseLevel =
781 (mCompileOptions & SH_HLSL_GET_DIMENSIONS_IGNORES_BASE_LEVEL) != 0;
782 mTextureFunctionHLSL->textureFunctionHeader(out, mOutputType, getDimensionsIgnoresBaseLevel);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800783 mImageFunctionHLSL->imageFunctionHeader(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000785 if (mUsesFragCoord)
786 {
787 out << "#define GL_USES_FRAG_COORD\n";
788 }
789
790 if (mUsesPointCoord)
791 {
792 out << "#define GL_USES_POINT_COORD\n";
793 }
794
795 if (mUsesFrontFacing)
796 {
797 out << "#define GL_USES_FRONT_FACING\n";
798 }
799
800 if (mUsesPointSize)
801 {
802 out << "#define GL_USES_POINT_SIZE\n";
803 }
804
Martin Radev41ac68e2017-06-06 12:16:58 +0300805 if (mHasMultiviewExtensionEnabled)
806 {
807 out << "#define GL_ANGLE_MULTIVIEW_ENABLED\n";
808 }
809
810 if (mUsesViewID)
811 {
812 out << "#define GL_USES_VIEW_ID\n";
813 }
814
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400815 if (mUsesFragDepth)
816 {
817 out << "#define GL_USES_FRAG_DEPTH\n";
818 }
819
shannonwoods@chromium.org03299882013-05-30 00:05:26 +0000820 if (mUsesDepthRange)
821 {
822 out << "#define GL_USES_DEPTH_RANGE\n";
823 }
824
Xinghua Caob1239382016-12-13 15:07:05 +0800825 if (mUsesNumWorkGroups)
826 {
827 out << "#define GL_USES_NUM_WORK_GROUPS\n";
828 }
829
830 if (mUsesWorkGroupID)
831 {
832 out << "#define GL_USES_WORK_GROUP_ID\n";
833 }
834
835 if (mUsesLocalInvocationID)
836 {
837 out << "#define GL_USES_LOCAL_INVOCATION_ID\n";
838 }
839
840 if (mUsesGlobalInvocationID)
841 {
842 out << "#define GL_USES_GLOBAL_INVOCATION_ID\n";
843 }
844
845 if (mUsesLocalInvocationIndex)
846 {
847 out << "#define GL_USES_LOCAL_INVOCATION_INDEX\n";
848 }
849
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000850 if (mUsesXor)
851 {
852 out << "bool xor(bool p, bool q)\n"
853 "{\n"
854 " return (p || q) && !(p && q);\n"
855 "}\n"
856 "\n";
857 }
858
Olli Etuahodfa75e82017-01-23 09:43:06 -0800859 builtInFunctionEmulator->outputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860}
861
862void OutputHLSL::visitSymbol(TIntermSymbol *node)
863{
Jamie Madill32aab012015-01-27 14:12:26 -0500864 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865
Jamie Madill570e04d2013-06-21 09:15:33 -0400866 // Handle accessing std140 structs by value
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200867 if (IsInStd140InterfaceBlock(node) && node->getBasicType() == EbtStruct)
Jamie Madill570e04d2013-06-21 09:15:33 -0400868 {
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200869 out << "map";
Jamie Madill570e04d2013-06-21 09:15:33 -0400870 }
871
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872 TString name = node->getSymbol();
873
Olli Etuaho93b059d2017-12-20 12:46:58 +0200874 const TSymbolUniqueId &uniqueId = node->uniqueId();
875
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000876 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000877 {
878 mUsesDepthRange = true;
879 out << name;
880 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881 else
882 {
Olli Etuahobd3cd502017-11-03 15:48:52 +0200883 const TType &nodeType = node->getType();
Olli Etuahob8cb9392017-12-20 14:23:19 +0200884 TQualifier qualifier = node->variable().getType().getQualifier();
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000885
Olli Etuahobd3cd502017-11-03 15:48:52 +0200886 ensureStructDefined(nodeType);
887
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000888 if (qualifier == EvqUniform)
889 {
Jamie Madill2e295e22015-04-29 10:41:33 -0400890 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400891
892 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000893 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200894 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000895 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000896 else
897 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200898 mReferencedUniforms[uniqueId.get()] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000899 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400900
Olli Etuahoff526f12017-06-30 12:26:54 +0300901 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000902 }
Jamie Madill19571812013-08-12 15:26:34 -0700903 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000904 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200905 mReferencedAttributes[uniqueId.get()] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400906 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000907 }
Jamie Madill033dae62014-06-18 12:56:28 -0400908 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000909 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200910 mReferencedVaryings[uniqueId.get()] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400911 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300912 if (name == "ViewID_OVR")
913 {
914 mUsesViewID = true;
915 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000916 }
Jamie Madill19571812013-08-12 15:26:34 -0700917 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400918 {
Olli Etuaho93b059d2017-12-20 12:46:58 +0200919 mReferencedOutputVariables[uniqueId.get()] = node;
Jamie Madill46131a32013-06-20 11:55:50 -0400920 out << "out_" << name;
921 }
922 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000923 {
924 out << "gl_Color[0]";
925 mUsesFragColor = true;
926 }
927 else if (qualifier == EvqFragData)
928 {
929 out << "gl_Color";
930 mUsesFragData = true;
931 }
932 else if (qualifier == EvqFragCoord)
933 {
934 mUsesFragCoord = true;
935 out << name;
936 }
937 else if (qualifier == EvqPointCoord)
938 {
939 mUsesPointCoord = true;
940 out << name;
941 }
942 else if (qualifier == EvqFrontFacing)
943 {
944 mUsesFrontFacing = true;
945 out << name;
946 }
947 else if (qualifier == EvqPointSize)
948 {
949 mUsesPointSize = true;
950 out << name;
951 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000952 else if (qualifier == EvqInstanceID)
953 {
954 mUsesInstanceID = true;
955 out << name;
956 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500957 else if (qualifier == EvqVertexID)
958 {
959 mUsesVertexID = true;
960 out << name;
961 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300962 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400963 {
964 mUsesFragDepth = true;
965 out << "gl_Depth";
966 }
Xinghua Caob1239382016-12-13 15:07:05 +0800967 else if (qualifier == EvqNumWorkGroups)
968 {
969 mUsesNumWorkGroups = true;
970 out << name;
971 }
972 else if (qualifier == EvqWorkGroupID)
973 {
974 mUsesWorkGroupID = true;
975 out << name;
976 }
977 else if (qualifier == EvqLocalInvocationID)
978 {
979 mUsesLocalInvocationID = true;
980 out << name;
981 }
982 else if (qualifier == EvqGlobalInvocationID)
983 {
984 mUsesGlobalInvocationID = true;
985 out << name;
986 }
987 else if (qualifier == EvqLocalInvocationIndex)
988 {
989 mUsesLocalInvocationIndex = true;
990 out << name;
991 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000992 else
993 {
Olli Etuahoff526f12017-06-30 12:26:54 +0300994 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000995 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996 }
997}
998
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400999void OutputHLSL::visitRaw(TIntermRaw *node)
1000{
Jamie Madill32aab012015-01-27 14:12:26 -05001001 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001002}
1003
Olli Etuaho7fb49552015-03-18 17:27:44 +02001004void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1005{
1006 if (type.isScalar() && !type.isArray())
1007 {
1008 if (op == EOpEqual)
1009 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001010 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001011 }
1012 else
1013 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001014 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001015 }
1016 }
1017 else
1018 {
1019 if (visit == PreVisit && op == EOpNotEqual)
1020 {
1021 out << "!";
1022 }
1023
1024 if (type.isArray())
1025 {
1026 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001027 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001028 }
1029 else if (type.getBasicType() == EbtStruct)
1030 {
1031 const TStructure &structure = *type.getStruct();
1032 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001033 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001034 }
1035 else
1036 {
1037 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001038 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001039 }
1040 }
1041}
1042
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001043void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1044{
1045 if (type.isArray())
1046 {
1047 const TString &functionName = addArrayAssignmentFunction(type);
1048 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1049 }
1050 else
1051 {
1052 outputTriplet(out, visit, "(", " = ", ")");
1053 }
1054}
1055
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001056bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001057{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001058 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001059 {
1060 TIntermNode *ancestor = getAncestorNode(n);
1061 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1062 if (ancestorBinary == nullptr)
1063 {
1064 return false;
1065 }
1066 switch (ancestorBinary->getOp())
1067 {
1068 case EOpIndexDirectStruct:
1069 {
1070 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1071 const TIntermConstantUnion *index =
1072 ancestorBinary->getRight()->getAsConstantUnion();
1073 const TField *field = structure->fields()[index->getIConst(0)];
1074 if (IsSampler(field->type()->getBasicType()))
1075 {
1076 return true;
1077 }
1078 break;
1079 }
1080 case EOpIndexDirect:
1081 break;
1082 default:
1083 // Returning a sampler from indirect indexing is not supported.
1084 return false;
1085 }
1086 }
1087 return false;
1088}
1089
Olli Etuahob6fa0432016-09-28 16:28:05 +01001090bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1091{
1092 TInfoSinkBase &out = getInfoSink();
1093 if (visit == PostVisit)
1094 {
1095 out << ".";
1096 node->writeOffsetsAsXYZW(&out);
1097 }
1098 return true;
1099}
1100
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1102{
Jamie Madill32aab012015-01-27 14:12:26 -05001103 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
1105 switch (node->getOp())
1106 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001107 case EOpComma:
1108 outputTriplet(out, visit, "(", ", ", ")");
1109 break;
1110 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001111 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001112 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001113 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1114 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001115 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001116 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1117 out << functionName << "(";
1118 node->getLeft()->traverse(this);
1119 TIntermSequence *seq = rightAgg->getSequence();
1120 for (auto &arrayElement : *seq)
1121 {
1122 out << ", ";
1123 arrayElement->traverse(this);
1124 }
1125 out << ")";
1126 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001127 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001128 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1129 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001130 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001131 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001132 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001133 break;
1134 case EOpInitialize:
1135 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001136 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001137 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1138 ASSERT(symbolNode);
1139 TIntermTyped *expression = node->getRight();
1140
1141 // Global initializers must be constant at this point.
1142 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
1143 canWriteAsHLSLLiteral(expression));
1144
1145 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1146 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1147 // new variable is created before the assignment is evaluated), so we need to
1148 // convert
1149 // this to "float t = x, x = t;".
1150 if (writeSameSymbolInitializer(out, symbolNode, expression))
1151 {
1152 // Skip initializing the rest of the expression
1153 return false;
1154 }
1155 else if (writeConstantInitialization(out, symbolNode, expression))
1156 {
1157 return false;
1158 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001159 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001160 else if (visit == InVisit)
1161 {
1162 out << " = ";
1163 }
1164 break;
1165 case EOpAddAssign:
1166 outputTriplet(out, visit, "(", " += ", ")");
1167 break;
1168 case EOpSubAssign:
1169 outputTriplet(out, visit, "(", " -= ", ")");
1170 break;
1171 case EOpMulAssign:
1172 outputTriplet(out, visit, "(", " *= ", ")");
1173 break;
1174 case EOpVectorTimesScalarAssign:
1175 outputTriplet(out, visit, "(", " *= ", ")");
1176 break;
1177 case EOpMatrixTimesScalarAssign:
1178 outputTriplet(out, visit, "(", " *= ", ")");
1179 break;
1180 case EOpVectorTimesMatrixAssign:
1181 if (visit == PreVisit)
1182 {
1183 out << "(";
1184 }
1185 else if (visit == InVisit)
1186 {
1187 out << " = mul(";
1188 node->getLeft()->traverse(this);
1189 out << ", transpose(";
1190 }
1191 else
1192 {
1193 out << ")))";
1194 }
1195 break;
1196 case EOpMatrixTimesMatrixAssign:
1197 if (visit == PreVisit)
1198 {
1199 out << "(";
1200 }
1201 else if (visit == InVisit)
1202 {
1203 out << " = transpose(mul(transpose(";
1204 node->getLeft()->traverse(this);
1205 out << "), transpose(";
1206 }
1207 else
1208 {
1209 out << "))))";
1210 }
1211 break;
1212 case EOpDivAssign:
1213 outputTriplet(out, visit, "(", " /= ", ")");
1214 break;
1215 case EOpIModAssign:
1216 outputTriplet(out, visit, "(", " %= ", ")");
1217 break;
1218 case EOpBitShiftLeftAssign:
1219 outputTriplet(out, visit, "(", " <<= ", ")");
1220 break;
1221 case EOpBitShiftRightAssign:
1222 outputTriplet(out, visit, "(", " >>= ", ")");
1223 break;
1224 case EOpBitwiseAndAssign:
1225 outputTriplet(out, visit, "(", " &= ", ")");
1226 break;
1227 case EOpBitwiseXorAssign:
1228 outputTriplet(out, visit, "(", " ^= ", ")");
1229 break;
1230 case EOpBitwiseOrAssign:
1231 outputTriplet(out, visit, "(", " |= ", ")");
1232 break;
1233 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001234 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001235 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001236 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001237 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001238 if (visit == PreVisit)
1239 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001240 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Olli Etuaho12a18ad2017-12-01 16:59:47 +02001241 TIntermSymbol *instanceArraySymbol = node->getLeft()->getAsSymbolNode();
Olli Etuaho93b059d2017-12-20 12:46:58 +02001242 mReferencedUniformBlocks[interfaceBlock->uniqueId().get()] =
1243 instanceArraySymbol;
Jamie Madill98493dd2013-07-08 14:39:03 -04001244 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Olli Etuaho12a18ad2017-12-01 16:59:47 +02001245 out << mUniformHLSL->UniformBlockInstanceString(
1246 instanceArraySymbol->getSymbol(), arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001247 return false;
1248 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001249 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001250 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001251 {
1252 // All parts of an expression that access a sampler in a struct need to use _ as
1253 // separator to access the sampler variable that has been moved out of the struct.
1254 outputTriplet(out, visit, "", "_", "");
1255 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001256 else
1257 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001258 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001259 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001260 }
1261 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001262 case EOpIndexIndirect:
1263 // We do not currently support indirect references to interface blocks
1264 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1265 outputTriplet(out, visit, "", "[", "]");
1266 break;
1267 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001268 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001269 const TStructure *structure = node->getLeft()->getType().getStruct();
1270 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1271 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001272
Olli Etuaho96963162016-03-21 11:54:33 +02001273 // In cases where indexing returns a sampler, we need to access the sampler variable
1274 // that has been moved out of the struct.
1275 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1276 if (visit == PreVisit && indexingReturnsSampler)
1277 {
1278 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1279 // This prefix is only output at the beginning of the indexing expression, which
1280 // may have multiple parts.
1281 out << "angle";
1282 }
1283 if (!indexingReturnsSampler)
1284 {
1285 // All parts of an expression that access a sampler in a struct need to use _ as
1286 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001287 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001288 }
1289 if (visit == InVisit)
1290 {
1291 if (indexingReturnsSampler)
1292 {
1293 out << "_" + field->name();
1294 }
1295 else
1296 {
1297 out << "." + DecorateField(field->name(), *structure);
1298 }
1299
1300 return false;
1301 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001302 }
1303 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001304 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001305 {
1306 bool structInStd140Block =
1307 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1308 if (visit == PreVisit && structInStd140Block)
1309 {
1310 out << "map";
1311 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001312 if (visit == InVisit)
1313 {
1314 const TInterfaceBlock *interfaceBlock =
1315 node->getLeft()->getType().getInterfaceBlock();
1316 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1317 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001318 if (structInStd140Block)
1319 {
1320 out << "_";
1321 }
1322 else
1323 {
1324 out << ".";
1325 }
1326 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001327
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001328 return false;
1329 }
1330 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001331 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001332 case EOpAdd:
1333 outputTriplet(out, visit, "(", " + ", ")");
1334 break;
1335 case EOpSub:
1336 outputTriplet(out, visit, "(", " - ", ")");
1337 break;
1338 case EOpMul:
1339 outputTriplet(out, visit, "(", " * ", ")");
1340 break;
1341 case EOpDiv:
1342 outputTriplet(out, visit, "(", " / ", ")");
1343 break;
1344 case EOpIMod:
1345 outputTriplet(out, visit, "(", " % ", ")");
1346 break;
1347 case EOpBitShiftLeft:
1348 outputTriplet(out, visit, "(", " << ", ")");
1349 break;
1350 case EOpBitShiftRight:
1351 outputTriplet(out, visit, "(", " >> ", ")");
1352 break;
1353 case EOpBitwiseAnd:
1354 outputTriplet(out, visit, "(", " & ", ")");
1355 break;
1356 case EOpBitwiseXor:
1357 outputTriplet(out, visit, "(", " ^ ", ")");
1358 break;
1359 case EOpBitwiseOr:
1360 outputTriplet(out, visit, "(", " | ", ")");
1361 break;
1362 case EOpEqual:
1363 case EOpNotEqual:
1364 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1365 break;
1366 case EOpLessThan:
1367 outputTriplet(out, visit, "(", " < ", ")");
1368 break;
1369 case EOpGreaterThan:
1370 outputTriplet(out, visit, "(", " > ", ")");
1371 break;
1372 case EOpLessThanEqual:
1373 outputTriplet(out, visit, "(", " <= ", ")");
1374 break;
1375 case EOpGreaterThanEqual:
1376 outputTriplet(out, visit, "(", " >= ", ")");
1377 break;
1378 case EOpVectorTimesScalar:
1379 outputTriplet(out, visit, "(", " * ", ")");
1380 break;
1381 case EOpMatrixTimesScalar:
1382 outputTriplet(out, visit, "(", " * ", ")");
1383 break;
1384 case EOpVectorTimesMatrix:
1385 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1386 break;
1387 case EOpMatrixTimesVector:
1388 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1389 break;
1390 case EOpMatrixTimesMatrix:
1391 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1392 break;
1393 case EOpLogicalOr:
1394 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1395 // been unfolded.
1396 ASSERT(!node->getRight()->hasSideEffects());
1397 outputTriplet(out, visit, "(", " || ", ")");
1398 return true;
1399 case EOpLogicalXor:
1400 mUsesXor = true;
1401 outputTriplet(out, visit, "xor(", ", ", ")");
1402 break;
1403 case EOpLogicalAnd:
1404 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1405 // been unfolded.
1406 ASSERT(!node->getRight()->hasSideEffects());
1407 outputTriplet(out, visit, "(", " && ", ")");
1408 return true;
1409 default:
1410 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001411 }
1412
1413 return true;
1414}
1415
1416bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1417{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001418 TInfoSinkBase &out = getInfoSink();
1419
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001420 switch (node->getOp())
1421 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001422 case EOpNegative:
1423 outputTriplet(out, visit, "(-", "", ")");
1424 break;
1425 case EOpPositive:
1426 outputTriplet(out, visit, "(+", "", ")");
1427 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001428 case EOpLogicalNot:
1429 outputTriplet(out, visit, "(!", "", ")");
1430 break;
1431 case EOpBitwiseNot:
1432 outputTriplet(out, visit, "(~", "", ")");
1433 break;
1434 case EOpPostIncrement:
1435 outputTriplet(out, visit, "(", "", "++)");
1436 break;
1437 case EOpPostDecrement:
1438 outputTriplet(out, visit, "(", "", "--)");
1439 break;
1440 case EOpPreIncrement:
1441 outputTriplet(out, visit, "(++", "", ")");
1442 break;
1443 case EOpPreDecrement:
1444 outputTriplet(out, visit, "(--", "", ")");
1445 break;
1446 case EOpRadians:
1447 outputTriplet(out, visit, "radians(", "", ")");
1448 break;
1449 case EOpDegrees:
1450 outputTriplet(out, visit, "degrees(", "", ")");
1451 break;
1452 case EOpSin:
1453 outputTriplet(out, visit, "sin(", "", ")");
1454 break;
1455 case EOpCos:
1456 outputTriplet(out, visit, "cos(", "", ")");
1457 break;
1458 case EOpTan:
1459 outputTriplet(out, visit, "tan(", "", ")");
1460 break;
1461 case EOpAsin:
1462 outputTriplet(out, visit, "asin(", "", ")");
1463 break;
1464 case EOpAcos:
1465 outputTriplet(out, visit, "acos(", "", ")");
1466 break;
1467 case EOpAtan:
1468 outputTriplet(out, visit, "atan(", "", ")");
1469 break;
1470 case EOpSinh:
1471 outputTriplet(out, visit, "sinh(", "", ")");
1472 break;
1473 case EOpCosh:
1474 outputTriplet(out, visit, "cosh(", "", ")");
1475 break;
1476 case EOpTanh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001477 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001478 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001479 case EOpAtanh:
1480 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001481 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001482 break;
1483 case EOpExp:
1484 outputTriplet(out, visit, "exp(", "", ")");
1485 break;
1486 case EOpLog:
1487 outputTriplet(out, visit, "log(", "", ")");
1488 break;
1489 case EOpExp2:
1490 outputTriplet(out, visit, "exp2(", "", ")");
1491 break;
1492 case EOpLog2:
1493 outputTriplet(out, visit, "log2(", "", ")");
1494 break;
1495 case EOpSqrt:
1496 outputTriplet(out, visit, "sqrt(", "", ")");
1497 break;
1498 case EOpInverseSqrt:
1499 outputTriplet(out, visit, "rsqrt(", "", ")");
1500 break;
1501 case EOpAbs:
1502 outputTriplet(out, visit, "abs(", "", ")");
1503 break;
1504 case EOpSign:
1505 outputTriplet(out, visit, "sign(", "", ")");
1506 break;
1507 case EOpFloor:
1508 outputTriplet(out, visit, "floor(", "", ")");
1509 break;
1510 case EOpTrunc:
1511 outputTriplet(out, visit, "trunc(", "", ")");
1512 break;
1513 case EOpRound:
1514 outputTriplet(out, visit, "round(", "", ")");
1515 break;
1516 case EOpRoundEven:
1517 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001518 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001519 break;
1520 case EOpCeil:
1521 outputTriplet(out, visit, "ceil(", "", ")");
1522 break;
1523 case EOpFract:
1524 outputTriplet(out, visit, "frac(", "", ")");
1525 break;
1526 case EOpIsNan:
1527 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001528 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001529 else
1530 outputTriplet(out, visit, "isnan(", "", ")");
1531 mRequiresIEEEStrictCompiling = true;
1532 break;
1533 case EOpIsInf:
1534 outputTriplet(out, visit, "isinf(", "", ")");
1535 break;
1536 case EOpFloatBitsToInt:
1537 outputTriplet(out, visit, "asint(", "", ")");
1538 break;
1539 case EOpFloatBitsToUint:
1540 outputTriplet(out, visit, "asuint(", "", ")");
1541 break;
1542 case EOpIntBitsToFloat:
1543 outputTriplet(out, visit, "asfloat(", "", ")");
1544 break;
1545 case EOpUintBitsToFloat:
1546 outputTriplet(out, visit, "asfloat(", "", ")");
1547 break;
1548 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001549 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001550 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001551 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001552 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001553 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001554 case EOpPackUnorm4x8:
1555 case EOpPackSnorm4x8:
1556 case EOpUnpackUnorm4x8:
1557 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001558 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001559 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001560 break;
1561 case EOpLength:
1562 outputTriplet(out, visit, "length(", "", ")");
1563 break;
1564 case EOpNormalize:
1565 outputTriplet(out, visit, "normalize(", "", ")");
1566 break;
1567 case EOpDFdx:
1568 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1569 {
1570 outputTriplet(out, visit, "(", "", ", 0.0)");
1571 }
1572 else
1573 {
1574 outputTriplet(out, visit, "ddx(", "", ")");
1575 }
1576 break;
1577 case EOpDFdy:
1578 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1579 {
1580 outputTriplet(out, visit, "(", "", ", 0.0)");
1581 }
1582 else
1583 {
1584 outputTriplet(out, visit, "ddy(", "", ")");
1585 }
1586 break;
1587 case EOpFwidth:
1588 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1589 {
1590 outputTriplet(out, visit, "(", "", ", 0.0)");
1591 }
1592 else
1593 {
1594 outputTriplet(out, visit, "fwidth(", "", ")");
1595 }
1596 break;
1597 case EOpTranspose:
1598 outputTriplet(out, visit, "transpose(", "", ")");
1599 break;
1600 case EOpDeterminant:
1601 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1602 break;
1603 case EOpInverse:
1604 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001605 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001606 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001607
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001608 case EOpAny:
1609 outputTriplet(out, visit, "any(", "", ")");
1610 break;
1611 case EOpAll:
1612 outputTriplet(out, visit, "all(", "", ")");
1613 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001614 case EOpLogicalNotComponentWise:
1615 outputTriplet(out, visit, "(!", "", ")");
1616 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001617 case EOpBitfieldReverse:
1618 outputTriplet(out, visit, "reversebits(", "", ")");
1619 break;
1620 case EOpBitCount:
1621 outputTriplet(out, visit, "countbits(", "", ")");
1622 break;
1623 case EOpFindLSB:
1624 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1625 // in GLSLTest and results are consistent with GL.
1626 outputTriplet(out, visit, "firstbitlow(", "", ")");
1627 break;
1628 case EOpFindMSB:
1629 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1630 // tested in GLSLTest and results are consistent with GL.
1631 outputTriplet(out, visit, "firstbithigh(", "", ")");
1632 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001633 default:
1634 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001635 }
1636
1637 return true;
1638}
1639
Olli Etuaho96963162016-03-21 11:54:33 +02001640TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1641{
1642 if (node->getAsSymbolNode())
1643 {
1644 return node->getAsSymbolNode()->getSymbol();
1645 }
1646 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1647 switch (nodeBinary->getOp())
1648 {
1649 case EOpIndexDirect:
1650 {
1651 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1652
1653 TInfoSinkBase prefixSink;
1654 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1655 return TString(prefixSink.c_str());
1656 }
1657 case EOpIndexDirectStruct:
1658 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001659 const TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001660 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1661 const TField *field = s->fields()[index];
1662
1663 TInfoSinkBase prefixSink;
1664 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1665 << field->name();
1666 return TString(prefixSink.c_str());
1667 }
1668 default:
1669 UNREACHABLE();
1670 return TString("");
1671 }
1672}
1673
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001674bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1675{
1676 TInfoSinkBase &out = getInfoSink();
1677
1678 if (mInsideFunction)
1679 {
1680 outputLineDirective(out, node->getLine().first_line);
1681 out << "{\n";
1682 }
1683
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001684 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001685 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001686 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001687
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001688 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001689
1690 // Don't output ; after case labels, they're terminated by :
1691 // This is needed especially since outputting a ; after a case statement would turn empty
1692 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001693 // Also the output code is clearer if we don't output ; after statements where it is not
1694 // needed:
1695 // * if statements
1696 // * switch statements
1697 // * blocks
1698 // * function definitions
1699 // * loops (do-while loops output the semicolon in VisitLoop)
1700 // * declarations that don't generate output.
1701 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1702 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1703 statement->getAsSwitchNode() == nullptr &&
1704 statement->getAsFunctionDefinition() == nullptr &&
1705 (statement->getAsDeclarationNode() == nullptr ||
1706 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1707 statement->getAsInvariantDeclarationNode() == nullptr)
1708 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001709 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001710 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001711 }
1712
1713 if (mInsideFunction)
1714 {
1715 outputLineDirective(out, node->getLine().last_line);
1716 out << "}\n";
1717 }
1718
1719 return false;
1720}
1721
Olli Etuaho336b1472016-10-05 16:37:55 +01001722bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1723{
1724 TInfoSinkBase &out = getInfoSink();
1725
1726 ASSERT(mCurrentFunctionMetadata == nullptr);
1727
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001728 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho336b1472016-10-05 16:37:55 +01001729 ASSERT(index != CallDAG::InvalidIndex);
1730 mCurrentFunctionMetadata = &mASTMetadataList[index];
1731
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001732 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001733
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001734 TIntermSequence *parameters = node->getFunctionPrototype()->getSequence();
Olli Etuaho336b1472016-10-05 16:37:55 +01001735
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001736 if (node->getFunction()->isMain())
Olli Etuaho336b1472016-10-05 16:37:55 +01001737 {
1738 out << "gl_main(";
1739 }
1740 else
1741 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001742 out << DecorateFunctionIfNeeded(node->getFunction()) << DisambiguateFunctionName(parameters)
1743 << (mOutputLod0Function ? "Lod0(" : "(");
Olli Etuaho336b1472016-10-05 16:37:55 +01001744 }
1745
1746 for (unsigned int i = 0; i < parameters->size(); i++)
1747 {
1748 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1749
1750 if (symbol)
1751 {
1752 ensureStructDefined(symbol->getType());
1753
1754 out << argumentString(symbol);
1755
1756 if (i < parameters->size() - 1)
1757 {
1758 out << ", ";
1759 }
1760 }
1761 else
1762 UNREACHABLE();
1763 }
1764
1765 out << ")\n";
1766
1767 mInsideFunction = true;
1768 // The function body node will output braces.
1769 node->getBody()->traverse(this);
1770 mInsideFunction = false;
1771
1772 mCurrentFunctionMetadata = nullptr;
1773
1774 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1775 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1776 {
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001777 ASSERT(!node->getFunction()->isMain());
Olli Etuaho336b1472016-10-05 16:37:55 +01001778 mOutputLod0Function = true;
1779 node->traverse(this);
1780 mOutputLod0Function = false;
1781 }
1782
1783 return false;
1784}
1785
Olli Etuaho13389b62016-10-16 11:48:18 +01001786bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1787{
Olli Etuaho13389b62016-10-16 11:48:18 +01001788 if (visit == PreVisit)
1789 {
1790 TIntermSequence *sequence = node->getSequence();
1791 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1792 ASSERT(sequence->size() == 1);
Olli Etuaho282847e2017-07-12 14:11:01 +03001793 ASSERT(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01001794
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001795 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001796 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001797 TInfoSinkBase &out = getInfoSink();
Olli Etuaho13389b62016-10-16 11:48:18 +01001798 ensureStructDefined(variable->getType());
1799
1800 if (!variable->getAsSymbolNode() ||
1801 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1802 {
1803 if (!mInsideFunction)
1804 {
1805 out << "static ";
1806 }
1807
1808 out << TypeString(variable->getType()) + " ";
1809
1810 TIntermSymbol *symbol = variable->getAsSymbolNode();
1811
1812 if (symbol)
1813 {
1814 symbol->traverse(this);
1815 out << ArrayString(symbol->getType());
1816 out << " = " + initializer(symbol->getType());
1817 }
1818 else
1819 {
1820 variable->traverse(this);
1821 }
1822 }
1823 else if (variable->getAsSymbolNode() &&
1824 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1825 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02001826 ASSERT(variable->getBasicType() == EbtStruct);
1827 // ensureStructDefined has already been called.
Olli Etuaho13389b62016-10-16 11:48:18 +01001828 }
1829 else
1830 UNREACHABLE();
1831 }
Olli Etuaho282847e2017-07-12 14:11:01 +03001832 else if (IsVaryingOut(variable->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001833 {
Olli Etuaho282847e2017-07-12 14:11:01 +03001834 TIntermSymbol *symbol = variable->getAsSymbolNode();
1835 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001836
Olli Etuaho93b059d2017-12-20 12:46:58 +02001837 if (symbol->variable().symbolType() != SymbolType::Empty)
1838 {
1839 // Vertex outputs which are declared but not written to should still be declared to
1840 // allow successful linking.
1841 mReferencedVaryings[symbol->uniqueId().get()] = symbol;
1842 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001843 }
1844 }
1845 return false;
1846}
1847
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001848bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1849{
1850 // Do not do any translation
1851 return false;
1852}
1853
Olli Etuaho16c745a2017-01-16 17:02:27 +00001854bool OutputHLSL::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
1855{
1856 TInfoSinkBase &out = getInfoSink();
1857
1858 ASSERT(visit == PreVisit);
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001859 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001860 // Skip the prototype if it is not implemented (and thus not used)
1861 if (index == CallDAG::InvalidIndex)
1862 {
1863 return false;
1864 }
1865
1866 TIntermSequence *arguments = node->getSequence();
1867
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001868 TString name = DecorateFunctionIfNeeded(node->getFunction());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001869 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1870 << (mOutputLod0Function ? "Lod0(" : "(");
1871
1872 for (unsigned int i = 0; i < arguments->size(); i++)
1873 {
1874 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1875 ASSERT(symbol != nullptr);
1876
1877 out << argumentString(symbol);
1878
1879 if (i < arguments->size() - 1)
1880 {
1881 out << ", ";
1882 }
1883 }
1884
1885 out << ");\n";
1886
1887 // Also prototype the Lod0 variant if needed
1888 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1889 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1890 {
1891 mOutputLod0Function = true;
1892 node->traverse(this);
1893 mOutputLod0Function = false;
1894 }
1895
1896 return false;
1897}
1898
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001899bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1900{
Jamie Madill32aab012015-01-27 14:12:26 -05001901 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001903 switch (node->getOp())
1904 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001905 case EOpCallBuiltInFunction:
1906 case EOpCallFunctionInAST:
1907 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001908 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001909 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001910
Corentin Wallez1239ee92015-03-19 14:38:02 -07001911 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001912 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001914 if (node->isArray())
1915 {
1916 UNIMPLEMENTED();
1917 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001918 size_t index = mCallDag.findIndex(node->getFunction()->uniqueId());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001919 ASSERT(index != CallDAG::InvalidIndex);
1920 lod0 &= mASTMetadataList[index].mNeedsLod0;
1921
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001922 out << DecorateFunctionIfNeeded(node->getFunction());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001923 out << DisambiguateFunctionName(node->getSequence());
1924 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001926 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001927 {
1928 // This path is used for internal functions that don't have their definitions in the
1929 // AST, such as precision emulation functions.
Olli Etuahobeb6dc72017-12-14 16:03:03 +02001930 out << DecorateFunctionIfNeeded(node->getFunction()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001931 }
Olli Etuaho1bb85282017-12-14 13:39:53 +02001932 else if (node->getFunction()->isImageFunction())
Xinghua Cao711b7a12017-10-09 13:38:12 +08001933 {
Olli Etuahobed35d72017-12-20 16:36:26 +02001934 const TString &name = node->getFunction()->name();
Xinghua Cao711b7a12017-10-09 13:38:12 +08001935 TType type = (*arguments)[0]->getAsTyped()->getType();
1936 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001937 name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
Xinghua Cao711b7a12017-10-09 13:38:12 +08001938 type.getMemoryQualifier().readonly);
1939 out << imageFunctionName << "(";
1940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941 else
1942 {
Olli Etuahobed35d72017-12-20 16:36:26 +02001943 const TString &name = node->getFunction()->name();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001944 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001945 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1946 if (arguments->size() > 1)
1947 {
1948 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1949 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001950 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
Olli Etuahobed35d72017-12-20 16:36:26 +02001951 name, samplerType, coords, arguments->size(), lod0, mShaderType);
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001952 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001953 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001954
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001955 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001956 {
Olli Etuaho96963162016-03-21 11:54:33 +02001957 TIntermTyped *typedArg = (*arg)->getAsTyped();
1958 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001959 {
1960 out << "texture_";
1961 (*arg)->traverse(this);
1962 out << ", sampler_";
1963 }
1964
1965 (*arg)->traverse(this);
1966
Olli Etuaho96963162016-03-21 11:54:33 +02001967 if (typedArg->getType().isStructureContainingSamplers())
1968 {
1969 const TType &argType = typedArg->getType();
1970 TVector<TIntermSymbol *> samplerSymbols;
1971 TString structName = samplerNamePrefixFromStruct(typedArg);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001972 argType.createSamplerSymbols("angle_" + structName, "", &samplerSymbols,
1973 nullptr, mSymbolTable);
Olli Etuaho96963162016-03-21 11:54:33 +02001974 for (const TIntermSymbol *sampler : samplerSymbols)
1975 {
1976 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1977 {
1978 out << ", texture_" << sampler->getSymbol();
1979 out << ", sampler_" << sampler->getSymbol();
1980 }
1981 else
1982 {
1983 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1984 // of D3D9, it's the sampler variable.
1985 out << ", " + sampler->getSymbol();
1986 }
1987 }
1988 }
1989
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001990 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001991 {
1992 out << ", ";
1993 }
1994 }
1995
1996 out << ")";
1997
1998 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001999 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03002000 case EOpConstruct:
Olli Etuahobd3cd502017-11-03 15:48:52 +02002001 outputConstructor(out, visit, node);
Olli Etuaho8fab3202017-05-08 18:22:22 +03002002 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002003 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002004 outputTriplet(out, visit, "(", " == ", ")");
2005 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002006 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002007 outputTriplet(out, visit, "(", " != ", ")");
2008 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002009 case EOpLessThanComponentWise:
2010 outputTriplet(out, visit, "(", " < ", ")");
2011 break;
2012 case EOpGreaterThanComponentWise:
2013 outputTriplet(out, visit, "(", " > ", ")");
2014 break;
2015 case EOpLessThanEqualComponentWise:
2016 outputTriplet(out, visit, "(", " <= ", ")");
2017 break;
2018 case EOpGreaterThanEqualComponentWise:
2019 outputTriplet(out, visit, "(", " >= ", ")");
2020 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002021 case EOpMod:
2022 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002023 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002024 break;
2025 case EOpModf:
2026 outputTriplet(out, visit, "modf(", ", ", ")");
2027 break;
2028 case EOpPow:
2029 outputTriplet(out, visit, "pow(", ", ", ")");
2030 break;
2031 case EOpAtan:
2032 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2033 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002034 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002035 break;
2036 case EOpMin:
2037 outputTriplet(out, visit, "min(", ", ", ")");
2038 break;
2039 case EOpMax:
2040 outputTriplet(out, visit, "max(", ", ", ")");
2041 break;
2042 case EOpClamp:
2043 outputTriplet(out, visit, "clamp(", ", ", ")");
2044 break;
2045 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302046 {
2047 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2048 if (lastParamNode->getType().getBasicType() == EbtBool)
2049 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002050 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2051 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302052 // so use emulated version.
2053 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002054 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302055 }
2056 else
2057 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002058 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302059 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002060 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302061 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002062 case EOpStep:
2063 outputTriplet(out, visit, "step(", ", ", ")");
2064 break;
2065 case EOpSmoothStep:
2066 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2067 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002068 case EOpFrexp:
2069 case EOpLdexp:
2070 ASSERT(node->getUseEmulatedFunction());
2071 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2072 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002073 case EOpDistance:
2074 outputTriplet(out, visit, "distance(", ", ", ")");
2075 break;
2076 case EOpDot:
2077 outputTriplet(out, visit, "dot(", ", ", ")");
2078 break;
2079 case EOpCross:
2080 outputTriplet(out, visit, "cross(", ", ", ")");
2081 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002082 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002083 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002084 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002085 break;
2086 case EOpReflect:
2087 outputTriplet(out, visit, "reflect(", ", ", ")");
2088 break;
2089 case EOpRefract:
2090 outputTriplet(out, visit, "refract(", ", ", ")");
2091 break;
2092 case EOpOuterProduct:
2093 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002094 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002095 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002096 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002097 outputTriplet(out, visit, "(", " * ", ")");
2098 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002099 case EOpBitfieldExtract:
2100 case EOpBitfieldInsert:
2101 case EOpUaddCarry:
2102 case EOpUsubBorrow:
2103 case EOpUmulExtended:
2104 case EOpImulExtended:
2105 ASSERT(node->getUseEmulatedFunction());
2106 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2107 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002108 default:
2109 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 }
2111
2112 return true;
2113}
2114
Olli Etuaho57961272016-09-14 13:57:46 +03002115void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002117 out << "if (";
2118
2119 node->getCondition()->traverse(this);
2120
2121 out << ")\n";
2122
Jamie Madill8c46ab12015-12-07 16:39:19 -05002123 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002124
2125 bool discard = false;
2126
2127 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002128 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002129 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002130 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002131
Olli Etuahoa6f22092015-05-08 18:31:10 +03002132 // Detect true discard
2133 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2134 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002135 else
2136 {
2137 // TODO(oetuaho): Check if the semicolon inside is necessary.
2138 // It's there as a result of conservative refactoring of the output.
2139 out << "{;}\n";
2140 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002141
Jamie Madill8c46ab12015-12-07 16:39:19 -05002142 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002143
Olli Etuahoa6f22092015-05-08 18:31:10 +03002144 if (node->getFalseBlock())
2145 {
2146 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002147
Jamie Madill8c46ab12015-12-07 16:39:19 -05002148 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149
Olli Etuaho32db19b2016-10-04 14:43:16 +01002150 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002151 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002152
Jamie Madill8c46ab12015-12-07 16:39:19 -05002153 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002154
Olli Etuahoa6f22092015-05-08 18:31:10 +03002155 // Detect false discard
2156 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2157 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002158
Olli Etuahoa6f22092015-05-08 18:31:10 +03002159 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002160 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002161 {
2162 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002163 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002164}
2165
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002166bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2167{
2168 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2169 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2170 UNREACHABLE();
2171 return false;
2172}
2173
Olli Etuaho57961272016-09-14 13:57:46 +03002174bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002175{
2176 TInfoSinkBase &out = getInfoSink();
2177
Olli Etuaho3d932d82016-04-12 11:10:30 +03002178 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002179
2180 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002181 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002182 {
2183 out << "FLATTEN ";
2184 }
2185
Olli Etuaho57961272016-09-14 13:57:46 +03002186 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187
2188 return false;
2189}
2190
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002191bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002192{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002193 TInfoSinkBase &out = getInfoSink();
2194
Olli Etuaho923ecef2017-10-11 12:01:38 +03002195 ASSERT(node->getStatementList());
2196 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002197 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002198 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002199 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002200 outputTriplet(out, visit, "switch (", ") ", "");
2201 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002202 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002203}
2204
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002205bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002206{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002207 TInfoSinkBase &out = getInfoSink();
2208
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002209 if (node->hasCondition())
2210 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002211 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002212 return true;
2213 }
2214 else
2215 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002216 out << "default:\n";
2217 return false;
2218 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002219}
2220
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002221void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2222{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002223 TInfoSinkBase &out = getInfoSink();
2224 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002225}
2226
2227bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2228{
Nicolas Capens655fe362014-04-11 13:12:34 -04002229 mNestedLoopDepth++;
2230
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002231 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002232 mInsideDiscontinuousLoop =
2233 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002234
Jamie Madill8c46ab12015-12-07 16:39:19 -05002235 TInfoSinkBase &out = getInfoSink();
2236
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002237 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002238 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002239 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002240 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002241 mInsideDiscontinuousLoop = wasDiscontinuous;
2242 mNestedLoopDepth--;
2243
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002244 return false;
2245 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002246 }
2247
Corentin Wallez1239ee92015-03-19 14:38:02 -07002248 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002249 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002251 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002252
Jamie Madill8c46ab12015-12-07 16:39:19 -05002253 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254 }
2255 else
2256 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002257 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002258
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259 if (node->getInit())
2260 {
2261 node->getInit()->traverse(this);
2262 }
2263
2264 out << "; ";
2265
alokp@chromium.org52813552010-11-16 18:36:09 +00002266 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002268 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002269 }
2270
2271 out << "; ";
2272
alokp@chromium.org52813552010-11-16 18:36:09 +00002273 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002275 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 }
2277
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002278 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002279
Jamie Madill8c46ab12015-12-07 16:39:19 -05002280 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281 }
2282
2283 if (node->getBody())
2284 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002285 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002286 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002288 else
2289 {
2290 // TODO(oetuaho): Check if the semicolon inside is necessary.
2291 // It's there as a result of conservative refactoring of the output.
2292 out << "{;}\n";
2293 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294
Jamie Madill8c46ab12015-12-07 16:39:19 -05002295 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002296
alokp@chromium.org52813552010-11-16 18:36:09 +00002297 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002299 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002300 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
alokp@chromium.org52813552010-11-16 18:36:09 +00002302 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002304 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002305 }
2306
daniel@transgaming.com73536982012-03-21 20:45:49 +00002307 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002309 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002310 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002311
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 return false;
2313}
2314
2315bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2316{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002317 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002319 TInfoSinkBase &out = getInfoSink();
2320
2321 switch (node->getFlowOp())
2322 {
2323 case EOpKill:
2324 out << "discard";
2325 break;
2326 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002327 if (mNestedLoopDepth > 1)
2328 {
2329 mUsesNestedBreak = true;
2330 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002331
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002332 if (mExcessiveLoopIndex)
2333 {
2334 out << "{Break";
2335 mExcessiveLoopIndex->traverse(this);
2336 out << " = true; break;}\n";
2337 }
2338 else
2339 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002340 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002341 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002342 break;
2343 case EOpContinue:
2344 out << "continue";
2345 break;
2346 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002347 if (node->getExpression())
2348 {
2349 out << "return ";
2350 }
2351 else
2352 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002353 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002354 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002355 break;
2356 default:
2357 UNREACHABLE();
2358 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359 }
2360
2361 return true;
2362}
2363
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002364// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002365// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2366// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002367bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002368{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002369 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002370
2371 // Parse loops of the form:
2372 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002373 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002374 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002375 int initial = 0;
2376 int limit = 0;
2377 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002378
2379 // Parse index name and intial value
2380 if (node->getInit())
2381 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002382 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002383
2384 if (init)
2385 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002386 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002387 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002388
2389 if (variable && variable->getQualifier() == EvqTemporary)
2390 {
2391 TIntermBinary *assign = variable->getAsBinaryNode();
2392
2393 if (assign->getOp() == EOpInitialize)
2394 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002395 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002396 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2397
2398 if (symbol && constant)
2399 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002400 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002401 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002402 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002403 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002404 }
2405 }
2406 }
2407 }
2408 }
2409 }
2410
2411 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002412 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002413 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002414 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002415
Olli Etuahob6af22b2017-12-15 14:05:44 +02002416 if (test && test->getLeft()->getAsSymbolNode()->uniqueId() == index->uniqueId())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002417 {
2418 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2419
2420 if (constant)
2421 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002422 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002423 {
2424 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002425 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002426 }
2427 }
2428 }
2429 }
2430
2431 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002432 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002433 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002434 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002435 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002436
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002437 if (binaryTerminal)
2438 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002439 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002440 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2441
2442 if (constant)
2443 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002444 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002445 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002446 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002447
2448 switch (op)
2449 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002450 case EOpAddAssign:
2451 increment = value;
2452 break;
2453 case EOpSubAssign:
2454 increment = -value;
2455 break;
2456 default:
2457 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002458 }
2459 }
2460 }
2461 }
2462 else if (unaryTerminal)
2463 {
2464 TOperator op = unaryTerminal->getOp();
2465
2466 switch (op)
2467 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002468 case EOpPostIncrement:
2469 increment = 1;
2470 break;
2471 case EOpPostDecrement:
2472 increment = -1;
2473 break;
2474 case EOpPreIncrement:
2475 increment = 1;
2476 break;
2477 case EOpPreDecrement:
2478 increment = -1;
2479 break;
2480 default:
2481 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002482 }
2483 }
2484 }
2485
Yunchao He4f285442017-04-21 12:15:49 +08002486 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002487 {
2488 if (comparator == EOpLessThanEqual)
2489 {
2490 comparator = EOpLessThan;
2491 limit += 1;
2492 }
2493
2494 if (comparator == EOpLessThan)
2495 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002496 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002497
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002498 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002499 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002500 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002501 }
2502
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002503 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002504 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002505
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002506 out << "{int ";
2507 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002508 out << ";\n"
2509 "bool Break";
2510 index->traverse(this);
2511 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002512
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002513 bool firstLoopFragment = true;
2514
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002515 while (iterations > 0)
2516 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002517 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002518
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002519 if (!firstLoopFragment)
2520 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002521 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002522 index->traverse(this);
2523 out << ") {\n";
2524 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002525
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002526 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002527 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002528 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002529 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002530
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002531 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002532 const char *unroll =
2533 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002534
Corentin Wallez1239ee92015-03-19 14:38:02 -07002535 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002536 index->traverse(this);
2537 out << " = ";
2538 out << initial;
2539
2540 out << "; ";
2541 index->traverse(this);
2542 out << " < ";
2543 out << clampedLimit;
2544
2545 out << "; ";
2546 index->traverse(this);
2547 out << " += ";
2548 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002549 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002550
Jamie Madill8c46ab12015-12-07 16:39:19 -05002551 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002552 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002553
2554 if (node->getBody())
2555 {
2556 node->getBody()->traverse(this);
2557 }
2558
Jamie Madill8c46ab12015-12-07 16:39:19 -05002559 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002560 out << ";}\n";
2561
2562 if (!firstLoopFragment)
2563 {
2564 out << "}\n";
2565 }
2566
2567 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002568
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002569 initial += MAX_LOOP_ITERATIONS * increment;
2570 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002571 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002572
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002573 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002574
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002575 mExcessiveLoopIndex = restoreIndex;
2576
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002577 return true;
2578 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002579 else
2580 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002581 }
2582
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002583 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002584}
2585
Jamie Madill8c46ab12015-12-07 16:39:19 -05002586void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2587 Visit visit,
2588 const char *preString,
2589 const char *inString,
2590 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002592 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002593 {
2594 out << preString;
2595 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002596 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597 {
2598 out << inString;
2599 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002600 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002601 {
2602 out << postString;
2603 }
2604}
2605
Jamie Madill8c46ab12015-12-07 16:39:19 -05002606void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002607{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002608 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002609 {
Jamie Madill32aab012015-01-27 14:12:26 -05002610 out << "\n";
2611 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002612
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002613 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002614 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002615 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002616 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002617
Jamie Madill32aab012015-01-27 14:12:26 -05002618 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002619 }
2620}
2621
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002622TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2623{
2624 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002625 const TType &type = symbol->getType();
2626 const TName &name = symbol->getName();
2627 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002628
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002629 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002630 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002631 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002632 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002633 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002634 {
Olli Etuahoff526f12017-06-30 12:26:54 +03002635 nameStr = DecorateVariableIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002636 }
2637
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002638 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002639 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002640 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2641 {
2642 // Samplers are passed as indices to the sampler array.
2643 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2644 return "const uint " + nameStr + ArrayString(type);
2645 }
2646 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2647 {
2648 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2649 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2650 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2651 ArrayString(type);
2652 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002653 }
2654
Olli Etuaho96963162016-03-21 11:54:33 +02002655 TStringStream argString;
2656 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2657 << ArrayString(type);
2658
2659 // If the structure parameter contains samplers, they need to be passed into the function as
2660 // separate parameters. HLSL doesn't natively support samplers in structs.
2661 if (type.isStructureContainingSamplers())
2662 {
2663 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2664 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002665 type.createSamplerSymbols("angle" + nameStr, "", &samplerSymbols, nullptr, mSymbolTable);
Olli Etuaho96963162016-03-21 11:54:33 +02002666 for (const TIntermSymbol *sampler : samplerSymbols)
2667 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002668 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002669 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2670 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002671 argString << ", const uint " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002672 }
2673 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2674 {
Olli Etuaho96963162016-03-21 11:54:33 +02002675 ASSERT(IsSampler(samplerType.getBasicType()));
2676 argString << ", " << QualifierString(qualifier) << " "
2677 << TextureString(samplerType.getBasicType()) << " texture_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002678 << sampler->getSymbol() << ArrayString(samplerType) << ", "
2679 << QualifierString(qualifier) << " "
Olli Etuaho96963162016-03-21 11:54:33 +02002680 << SamplerString(samplerType.getBasicType()) << " sampler_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002681 << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002682 }
2683 else
2684 {
Olli Etuaho96963162016-03-21 11:54:33 +02002685 ASSERT(IsSampler(samplerType.getBasicType()));
2686 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
Olli Etuaho28839f02017-08-15 11:38:16 +03002687 << " " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002688 }
2689 }
2690 }
2691
2692 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002693}
2694
2695TString OutputHLSL::initializer(const TType &type)
2696{
2697 TString string;
2698
Jamie Madill94bf7f22013-07-08 13:31:15 -04002699 size_t size = type.getObjectSize();
2700 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002701 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002702 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002703
Jamie Madill94bf7f22013-07-08 13:31:15 -04002704 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705 {
2706 string += ", ";
2707 }
2708 }
2709
daniel@transgaming.comead23042010-04-29 03:35:36 +00002710 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002711}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002712
Olli Etuahobd3cd502017-11-03 15:48:52 +02002713void OutputHLSL::outputConstructor(TInfoSinkBase &out, Visit visit, TIntermAggregate *node)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002714{
Olli Etuahobd3cd502017-11-03 15:48:52 +02002715 // Array constructors should have been already pruned from the code.
2716 ASSERT(!node->getType().isArray());
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002717
2718 if (visit == PreVisit)
2719 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002720 TString constructorName;
2721 if (node->getBasicType() == EbtStruct)
2722 {
2723 constructorName = mStructureHLSL->addStructConstructor(*node->getType().getStruct());
2724 }
2725 else
2726 {
2727 constructorName =
2728 mStructureHLSL->addBuiltInConstructor(node->getType(), node->getSequence());
2729 }
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002730 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002731 }
2732 else if (visit == InVisit)
2733 {
2734 out << ", ";
2735 }
2736 else if (visit == PostVisit)
2737 {
2738 out << ")";
2739 }
2740}
2741
Jamie Madill8c46ab12015-12-07 16:39:19 -05002742const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2743 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002744 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002745{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002746 const TConstantUnion *constUnionIterated = constUnion;
2747
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002748 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002749 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002750 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02002751 out << mStructureHLSL->addStructConstructor(*structure) << "(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002752
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002753 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002754
Jamie Madill98493dd2013-07-08 14:39:03 -04002755 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002756 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002757 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002758 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002759
Jamie Madill98493dd2013-07-08 14:39:03 -04002760 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002761 {
2762 out << ", ";
2763 }
2764 }
2765
2766 out << ")";
2767 }
2768 else
2769 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002770 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002771 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002772
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002773 if (writeType)
2774 {
Jamie Madill033dae62014-06-18 12:56:28 -04002775 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002776 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002777 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002778 if (writeType)
2779 {
2780 out << ")";
2781 }
2782 }
2783
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002784 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002785}
2786
Olli Etuahod68924e2017-01-02 17:34:40 +00002787void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002788{
Olli Etuahod68924e2017-01-02 17:34:40 +00002789 if (visit == PreVisit)
2790 {
2791 const char *opStr = GetOperatorString(op);
2792 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2793 out << "(";
2794 }
2795 else
2796 {
2797 outputTriplet(out, visit, nullptr, ", ", ")");
2798 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002799}
2800
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002801bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2802 TIntermSymbol *symbolNode,
2803 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002804{
Olli Etuaho4728bdc2017-12-20 17:51:08 +02002805 const TIntermSymbol *symbolInInitializer = FindSymbolNode(expression, symbolNode->getSymbol());
Jamie Madill37997142015-01-28 10:06:34 -05002806
Olli Etuaho4728bdc2017-12-20 17:51:08 +02002807 if (symbolInInitializer)
Jamie Madill37997142015-01-28 10:06:34 -05002808 {
2809 // Type already printed
2810 out << "t" + str(mUniqueIndex) + " = ";
2811 expression->traverse(this);
2812 out << ", ";
2813 symbolNode->traverse(this);
2814 out << " = t" + str(mUniqueIndex);
2815
2816 mUniqueIndex++;
2817 return true;
2818 }
2819
2820 return false;
2821}
2822
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002823bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2824{
2825 // We support writing constant unions and constructors that only take constant unions as
2826 // parameters as HLSL literals.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002827 return !expression->getType().isArrayOfArrays() &&
2828 (expression->getAsConstantUnion() ||
2829 expression->isConstructorWithOnlyConstantUnionParameters());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002830}
2831
2832bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2833 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002834 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002835{
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002836 if (canWriteAsHLSLLiteral(initializer))
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002837 {
2838 symbolNode->traverse(this);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002839 ASSERT(!symbolNode->getType().isArrayOfArrays());
2840 if (symbolNode->getType().isArray())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002841 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002842 out << "[" << symbolNode->getType().getOutermostArraySize() << "]";
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002843 }
2844 out << " = {";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002845 if (initializer->getAsConstantUnion())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002846 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002847 TIntermConstantUnion *nodeConst = initializer->getAsConstantUnion();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002848 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002849 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002850 }
2851 else
2852 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002853 TIntermAggregate *constructor = initializer->getAsAggregate();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002854 ASSERT(constructor != nullptr);
2855 for (TIntermNode *&node : *constructor->getSequence())
2856 {
2857 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2858 ASSERT(nodeConst);
2859 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002860 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002861 if (node != constructor->getSequence()->back())
2862 {
2863 out << ", ";
2864 }
2865 }
2866 }
2867 out << "}";
2868 return true;
2869 }
2870 return false;
2871}
2872
Jamie Madill55e79e02015-02-09 15:35:00 -05002873TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2874{
2875 const TFieldList &fields = structure.fields();
2876
2877 for (const auto &eqFunction : mStructEqualityFunctions)
2878 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002879 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002880 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002881 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002882 }
2883 }
2884
2885 const TString &structNameString = StructNameString(structure);
2886
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002887 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002888 function->structure = &structure;
2889 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002890
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002891 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002892
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002893 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2894 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002895 << "{\n"
2896 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002897
2898 for (size_t i = 0; i < fields.size(); i++)
2899 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002900 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002901 const TType *fieldType = field->type();
2902
2903 const TString &fieldNameA = "a." + Decorate(field->name());
2904 const TString &fieldNameB = "b." + Decorate(field->name());
2905
2906 if (i > 0)
2907 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002908 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002909 }
2910
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002911 fnOut << "(";
2912 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2913 fnOut << fieldNameA;
2914 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2915 fnOut << fieldNameB;
2916 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2917 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002918 }
2919
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002920 fnOut << ";\n"
2921 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002922
2923 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002924
2925 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002926 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002927
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002928 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002929}
2930
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002931TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002932{
2933 for (const auto &eqFunction : mArrayEqualityFunctions)
2934 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002935 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002936 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002937 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002938 }
2939 }
2940
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002941 TType elementType(type);
2942 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002943
Olli Etuaho12690762015-03-31 12:55:28 +03002944 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002945 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002946
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002947 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002948
2949 TInfoSinkBase fnOut;
2950
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002951 const TString &typeName = TypeString(type);
2952 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2953 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002954 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002955 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002956 << type.getOutermostArraySize()
2957 << "; ++i)\n"
2958 " {\n"
2959 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002960
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002961 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002962 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002963 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002964 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002965 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002966
2967 fnOut << ") { return false; }\n"
2968 " }\n"
2969 " return true;\n"
2970 "}\n";
2971
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002972 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002973
2974 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002975 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002976
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002977 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002978}
2979
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002980TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03002981{
2982 for (const auto &assignFunction : mArrayAssignmentFunctions)
2983 {
2984 if (assignFunction.type == type)
2985 {
2986 return assignFunction.functionName;
2987 }
2988 }
2989
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002990 TType elementType(type);
2991 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03002992
2993 ArrayHelperFunction function;
2994 function.type = type;
2995
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002996 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03002997
2998 TInfoSinkBase fnOut;
2999
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003000 const TString &typeName = TypeString(type);
3001 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
3002 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003003 << "{\n"
3004 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003005 << type.getOutermostArraySize()
3006 << "; ++i)\n"
3007 " {\n"
3008 " ";
3009
3010 outputAssign(PreVisit, elementType, fnOut);
3011 fnOut << "a[i]";
3012 outputAssign(InVisit, elementType, fnOut);
3013 fnOut << "b[i]";
3014 outputAssign(PostVisit, elementType, fnOut);
3015
3016 fnOut << ";\n"
3017 " }\n"
3018 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03003019
3020 function.functionDefinition = fnOut.c_str();
3021
3022 mArrayAssignmentFunctions.push_back(function);
3023
3024 return function.functionName;
3025}
3026
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003027TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003028{
3029 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3030 {
3031 if (constructIntoFunction.type == type)
3032 {
3033 return constructIntoFunction.functionName;
3034 }
3035 }
3036
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003037 TType elementType(type);
3038 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003039
3040 ArrayHelperFunction function;
3041 function.type = type;
3042
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003043 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003044
3045 TInfoSinkBase fnOut;
3046
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003047 const TString &typeName = TypeString(type);
3048 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3049 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003050 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003051 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003052 }
3053 fnOut << ")\n"
3054 "{\n";
3055
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003056 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003057 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003058 fnOut << " ";
3059 outputAssign(PreVisit, elementType, fnOut);
3060 fnOut << "a[" << i << "]";
3061 outputAssign(InVisit, elementType, fnOut);
3062 fnOut << "b" << i;
3063 outputAssign(PostVisit, elementType, fnOut);
3064 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003065 }
3066 fnOut << "}\n";
3067
3068 function.functionDefinition = fnOut.c_str();
3069
3070 mArrayConstructIntoFunctions.push_back(function);
3071
3072 return function.functionName;
3073}
3074
Jamie Madill2e295e22015-04-29 10:41:33 -04003075void OutputHLSL::ensureStructDefined(const TType &type)
3076{
Olli Etuahobd3cd502017-11-03 15:48:52 +02003077 const TStructure *structure = type.getStruct();
Jamie Madill2e295e22015-04-29 10:41:33 -04003078 if (structure)
3079 {
Olli Etuahobd3cd502017-11-03 15:48:52 +02003080 ASSERT(type.getBasicType() == EbtStruct);
3081 mStructureHLSL->ensureStructDefined(*structure);
Jamie Madill2e295e22015-04-29 10:41:33 -04003082 }
3083}
3084
Jamie Madill45bcc782016-11-07 13:58:48 -05003085} // namespace sh