blob: 7e84761f55f17466b9baf74a66d89cca4f05f6e5 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Xinghua Cao711b7a12017-10-09 13:38:12 +080018#include "compiler/translator/ImageFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050019#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
Olli Etuaho5858f7e2016-04-08 13:08:46 +030024#include "compiler/translator/TextureFunctionHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050026#include "compiler/translator/UniformHLSL.h"
27#include "compiler/translator/UtilsHLSL.h"
28#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050029#include "compiler/translator/util.h"
30
Jamie Madill45bcc782016-11-07 13:58:48 -050031namespace sh
32{
33
Olli Etuaho96f6adf2017-08-16 11:18:54 +030034namespace
35{
36
37TString ArrayHelperFunctionName(const char *prefix, const TType &type)
38{
39 TStringStream fnName;
40 fnName << prefix << "_";
41 for (unsigned int arraySize : type.getArraySizes())
42 {
43 fnName << arraySize << "_";
44 }
45 fnName << TypeString(type);
46 return fnName.str();
47}
48
Olli Etuaho40dbdd62017-10-13 13:34:19 +030049bool IsDeclarationWrittenOut(TIntermDeclaration *node)
50{
51 TIntermSequence *sequence = node->getSequence();
52 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
53 ASSERT(sequence->size() == 1);
54 ASSERT(variable);
55 return (variable->getQualifier() == EvqTemporary || variable->getQualifier() == EvqGlobal ||
56 variable->getQualifier() == EvqConst);
57}
58
Olli Etuaho2ef23e22017-11-01 16:39:11 +020059bool IsInStd140InterfaceBlock(TIntermTyped *node)
60{
61 TIntermBinary *binaryNode = node->getAsBinaryNode();
62
63 if (binaryNode)
64 {
65 return IsInStd140InterfaceBlock(binaryNode->getLeft());
66 }
67
68 const TType &type = node->getType();
69
70 // determine if we are in the standard layout
71 const TInterfaceBlock *interfaceBlock = type.getInterfaceBlock();
72 if (interfaceBlock)
73 {
74 return (interfaceBlock->blockStorage() == EbsStd140);
75 }
76
77 return false;
78}
79
Olli Etuaho96f6adf2017-08-16 11:18:54 +030080} // anonymous namespace
81
Olli Etuaho56a2f952016-12-08 12:16:27 +000082void OutputHLSL::writeFloat(TInfoSinkBase &out, float f)
Olli Etuaho4785fec2015-05-18 16:09:37 +030083{
Olli Etuaho56a2f952016-12-08 12:16:27 +000084 // This is known not to work for NaN on all drivers but make the best effort to output NaNs
85 // regardless.
86 if ((gl::isInf(f) || gl::isNaN(f)) && mShaderVersion >= 300 &&
87 mOutputType == SH_HLSL_4_1_OUTPUT)
88 {
89 out << "asfloat(" << gl::bitCast<uint32_t>(f) << "u)";
90 }
91 else
92 {
93 out << std::min(FLT_MAX, std::max(-FLT_MAX, f));
94 }
95}
Olli Etuaho4785fec2015-05-18 16:09:37 +030096
Olli Etuaho56a2f952016-12-08 12:16:27 +000097void OutputHLSL::writeSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
Olli Etuaho18b9deb2015-11-05 12:14:50 +020098{
99 ASSERT(constUnion != nullptr);
100 switch (constUnion->getType())
101 {
102 case EbtFloat:
Olli Etuaho56a2f952016-12-08 12:16:27 +0000103 writeFloat(out, constUnion->getFConst());
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200104 break;
105 case EbtInt:
106 out << constUnion->getIConst();
107 break;
108 case EbtUInt:
109 out << constUnion->getUConst();
110 break;
111 case EbtBool:
112 out << constUnion->getBConst();
113 break;
114 default:
115 UNREACHABLE();
116 }
117}
118
Olli Etuaho56a2f952016-12-08 12:16:27 +0000119const TConstantUnion *OutputHLSL::writeConstantUnionArray(TInfoSinkBase &out,
120 const TConstantUnion *const constUnion,
121 const size_t size)
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200122{
123 const TConstantUnion *constUnionIterated = constUnion;
124 for (size_t i = 0; i < size; i++, constUnionIterated++)
125 {
Olli Etuaho56a2f952016-12-08 12:16:27 +0000126 writeSingleConstant(out, constUnionIterated);
Olli Etuaho18b9deb2015-11-05 12:14:50 +0200127
128 if (i != size - 1)
129 {
130 out << ", ";
131 }
132 }
133 return constUnionIterated;
134}
135
Qiankun Miao7ebb97f2016-09-08 18:01:50 +0800136OutputHLSL::OutputHLSL(sh::GLenum shaderType,
137 int shaderVersion,
138 const TExtensionBehavior &extensionBehavior,
139 const char *sourcePath,
140 ShShaderOutput outputType,
141 int numRenderTargets,
142 const std::vector<Uniform> &uniforms,
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300143 ShCompileOptions compileOptions,
Olli Etuaho89a69a02017-10-23 12:20:45 +0300144 TSymbolTable *symbolTable,
145 PerformanceDiagnostics *perfDiagnostics)
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300146 : TIntermTraverser(true, true, true, symbolTable),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200147 mShaderType(shaderType),
148 mShaderVersion(shaderVersion),
149 mExtensionBehavior(extensionBehavior),
150 mSourcePath(sourcePath),
151 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700152 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000153 mNumRenderTargets(numRenderTargets),
Olli Etuaho89a69a02017-10-23 12:20:45 +0300154 mCurrentFunctionMetadata(nullptr),
155 mPerfDiagnostics(perfDiagnostics)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000157 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000158
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500159 mUsesFragColor = false;
160 mUsesFragData = false;
161 mUsesDepthRange = false;
162 mUsesFragCoord = false;
163 mUsesPointCoord = false;
164 mUsesFrontFacing = false;
165 mUsesPointSize = false;
166 mUsesInstanceID = false;
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300167 mHasMultiviewExtensionEnabled =
168 IsExtensionEnabled(mExtensionBehavior, TExtension::OVR_multiview);
Martin Radev41ac68e2017-06-06 12:16:58 +0300169 mUsesViewID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500170 mUsesVertexID = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500171 mUsesFragDepth = false;
Xinghua Caob1239382016-12-13 15:07:05 +0800172 mUsesNumWorkGroups = false;
173 mUsesWorkGroupID = false;
174 mUsesLocalInvocationID = false;
175 mUsesGlobalInvocationID = false;
176 mUsesLocalInvocationIndex = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500177 mUsesXor = false;
178 mUsesDiscardRewriting = false;
179 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530180 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000181
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000182 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000183
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500184 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000185 mInsideDiscontinuousLoop = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500186 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000187
Yunchao Hed7297bf2017-04-19 15:27:10 +0800188 mExcessiveLoopIndex = nullptr;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000189
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500190 mStructureHLSL = new StructureHLSL;
Xinghua Cao711b7a12017-10-09 13:38:12 +0800191 mUniformHLSL = new UniformHLSL(shaderType, mStructureHLSL, outputType, uniforms);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300192 mTextureFunctionHLSL = new TextureFunctionHLSL;
Xinghua Cao711b7a12017-10-09 13:38:12 +0800193 mImageFunctionHLSL = new ImageFunctionHLSL;
Jamie Madill8daaba12014-06-13 10:04:33 -0400194
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200195 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000196 {
Arun Patole63419392015-03-13 11:51:07 +0530197 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500198 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and
199 // dx_ViewAdjust.
Arun Patole63419392015-03-13 11:51:07 +0530200 // In both cases total 3 uniform registers need to be reserved.
201 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000202 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000203
Geoff Lang00140f42016-02-03 18:47:33 +0000204 // Reserve registers for the default uniform block and driver constants
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800205 mUniformHLSL->reserveUniformBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206}
207
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000208OutputHLSL::~OutputHLSL()
209{
Jamie Madill8daaba12014-06-13 10:04:33 -0400210 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400211 SafeDelete(mUniformHLSL);
Olli Etuaho5858f7e2016-04-08 13:08:46 +0300212 SafeDelete(mTextureFunctionHLSL);
Xinghua Cao711b7a12017-10-09 13:38:12 +0800213 SafeDelete(mImageFunctionHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200214 for (auto &eqFunction : mStructEqualityFunctions)
215 {
216 SafeDelete(eqFunction);
217 }
218 for (auto &eqFunction : mArrayEqualityFunctions)
219 {
220 SafeDelete(eqFunction);
221 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000222}
223
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200224void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000225{
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200226 BuiltInFunctionEmulator builtInFunctionEmulator;
227 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Shao6f0a0dc2016-09-27 13:51:29 +0800228 if ((mCompileOptions & SH_EMULATE_ISNAN_FLOAT_FUNCTION) != 0)
229 {
230 InitBuiltInIsnanFunctionEmulatorForHLSLWorkarounds(&builtInFunctionEmulator,
231 mShaderVersion);
232 }
233
Olli Etuahodfa75e82017-01-23 09:43:06 -0800234 builtInFunctionEmulator.markBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500235
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700236 // Now that we are done changing the AST, do the analyses need for HLSL generation
Olli Etuaho77ba4082016-12-16 12:01:18 +0000237 CallDAG::InitResult success = mCallDag.init(treeRoot, nullptr);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700238 ASSERT(success == CallDAG::INITDAG_SUCCESS);
239 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700240
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200241 const std::vector<MappedStruct> std140Structs = FlagStd140Structs(treeRoot);
242 // TODO(oetuaho): The std140Structs could be filtered based on which ones actually get used in
243 // the shader code. When we add shader storage blocks we might also consider an alternative
244 // solution, since the struct mapping won't work very well for shader storage blocks.
245
Jamie Madill37997142015-01-28 10:06:34 -0500246 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500247 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200248 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500249 mInfoSinkStack.pop();
250
Jamie Madill37997142015-01-28 10:06:34 -0500251 mInfoSinkStack.push(&mFooter);
Jamie Madill37997142015-01-28 10:06:34 -0500252 mInfoSinkStack.pop();
253
Jamie Madill32aab012015-01-27 14:12:26 -0500254 mInfoSinkStack.push(&mHeader);
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200255 header(mHeader, std140Structs, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500256 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000257
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200258 objSink << mHeader.c_str();
259 objSink << mBody.c_str();
260 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200261
Olli Etuahodfa75e82017-01-23 09:43:06 -0800262 builtInFunctionEmulator.cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000263}
264
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800265const std::map<std::string, unsigned int> &OutputHLSL::getUniformBlockRegisterMap() const
Jamie Madill4e1fd412014-07-10 17:50:10 -0400266{
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800267 return mUniformHLSL->getUniformBlockRegisterMap();
Jamie Madill4e1fd412014-07-10 17:50:10 -0400268}
269
Jamie Madill9fe25e92014-07-18 10:33:08 -0400270const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
271{
272 return mUniformHLSL->getUniformRegisterMap();
273}
274
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200275TString OutputHLSL::structInitializerString(int indent,
276 const TType &type,
277 const TString &name) const
Jamie Madill570e04d2013-06-21 09:15:33 -0400278{
279 TString init;
280
Olli Etuahoed049ab2017-06-30 17:38:33 +0300281 TString indentString;
282 for (int spaces = 0; spaces < indent; spaces++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400283 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300284 indentString += " ";
Jamie Madill570e04d2013-06-21 09:15:33 -0400285 }
286
Olli Etuahoed049ab2017-06-30 17:38:33 +0300287 if (type.isArray())
Jamie Madill570e04d2013-06-21 09:15:33 -0400288 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300289 init += indentString + "{\n";
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300290 for (unsigned int arrayIndex = 0u; arrayIndex < type.getOutermostArraySize(); ++arrayIndex)
Jamie Madill570e04d2013-06-21 09:15:33 -0400291 {
Olli Etuahoed049ab2017-06-30 17:38:33 +0300292 TStringStream indexedString;
293 indexedString << name << "[" << arrayIndex << "]";
294 TType elementType = type;
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300295 elementType.toArrayElementType();
Olli Etuahoed049ab2017-06-30 17:38:33 +0300296 init += structInitializerString(indent + 1, elementType, indexedString.str());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300297 if (arrayIndex < type.getOutermostArraySize() - 1)
Olli Etuahoed049ab2017-06-30 17:38:33 +0300298 {
299 init += ",";
300 }
301 init += "\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400302 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300303 init += indentString + "}";
Jamie Madill570e04d2013-06-21 09:15:33 -0400304 }
Olli Etuahoed049ab2017-06-30 17:38:33 +0300305 else if (type.getBasicType() == EbtStruct)
306 {
307 init += indentString + "{\n";
308 const TStructure &structure = *type.getStruct();
309 const TFieldList &fields = structure.fields();
310 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
311 {
312 const TField &field = *fields[fieldIndex];
313 const TString &fieldName = name + "." + Decorate(field.name());
314 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400315
Olli Etuahoed049ab2017-06-30 17:38:33 +0300316 init += structInitializerString(indent + 1, fieldType, fieldName);
317 if (fieldIndex < fields.size() - 1)
318 {
319 init += ",";
320 }
321 init += "\n";
322 }
323 init += indentString + "}";
324 }
325 else
326 {
327 init += indentString + name;
328 }
Jamie Madill570e04d2013-06-21 09:15:33 -0400329
330 return init;
331}
332
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200333TString OutputHLSL::generateStructMapping(const std::vector<MappedStruct> &std140Structs) const
334{
335 TString mappedStructs;
336
337 for (auto &mappedStruct : std140Structs)
338 {
339 TInterfaceBlock *interfaceBlock =
340 mappedStruct.blockDeclarator->getType().getInterfaceBlock();
341 const TString &interfaceBlockName = interfaceBlock->name();
342 const TName &instanceName = mappedStruct.blockDeclarator->getName();
343 if (mReferencedUniformBlocks.count(interfaceBlockName) == 0 &&
344 (instanceName.getString() == "" ||
345 mReferencedUniformBlocks.count(instanceName.getString()) == 0))
346 {
347 continue;
348 }
349
350 unsigned int instanceCount = 1u;
351 bool isInstanceArray = mappedStruct.blockDeclarator->isArray();
352 if (isInstanceArray)
353 {
354 instanceCount = mappedStruct.blockDeclarator->getOutermostArraySize();
355 }
356
357 for (unsigned int instanceArrayIndex = 0; instanceArrayIndex < instanceCount;
358 ++instanceArrayIndex)
359 {
360 TString originalName;
361 TString mappedName("map");
362
363 if (instanceName.getString() != "")
364 {
365 unsigned int instanceStringArrayIndex = GL_INVALID_INDEX;
366 if (isInstanceArray)
367 instanceStringArrayIndex = instanceArrayIndex;
368 TString instanceString = mUniformHLSL->uniformBlockInstanceString(
369 *interfaceBlock, instanceStringArrayIndex);
370 originalName += instanceString;
371 mappedName += instanceString;
372 originalName += ".";
373 mappedName += "_";
374 }
375
376 TString fieldName = Decorate(mappedStruct.field->name());
377 originalName += fieldName;
378 mappedName += fieldName;
379
380 TType *structType = mappedStruct.field->type();
381 mappedStructs +=
382 "static " + Decorate(structType->getStruct()->name()) + " " + mappedName;
383
384 if (structType->isArray())
385 {
386 mappedStructs += ArrayString(*mappedStruct.field->type());
387 }
388
389 mappedStructs += " =\n";
390 mappedStructs += structInitializerString(0, *structType, originalName);
391 mappedStructs += ";\n";
392 }
393 }
394 return mappedStructs;
395}
396
397void OutputHLSL::header(TInfoSinkBase &out,
398 const std::vector<MappedStruct> &std140Structs,
399 const BuiltInFunctionEmulator *builtInFunctionEmulator) const
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000401 TString varyings;
402 TString attributes;
Olli Etuaho2ef23e22017-11-01 16:39:11 +0200403 TString mappedStructs = generateStructMapping(std140Structs);
Jamie Madill570e04d2013-06-21 09:15:33 -0400404
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500405 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin();
406 varying != mReferencedVaryings.end(); varying++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000407 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500408 const TType &type = varying->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000409 const TString &name = varying->second->getSymbol();
410
411 // Program linking depends on this exact format
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500412 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) +
413 " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000414 }
415
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500416 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin();
417 attribute != mReferencedAttributes.end(); attribute++)
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000418 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500419 const TType &type = attribute->second->getType();
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000420 const TString &name = attribute->second->getSymbol();
421
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500422 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) +
423 " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000424 }
425
Jamie Madill8daaba12014-06-13 10:04:33 -0400426 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400427
Olli Etuaho2d88e9b2017-07-21 16:52:03 +0300428 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms, mSymbolTable);
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800429 out << mUniformHLSL->uniformBlocksHeader(mReferencedUniformBlocks);
Jamie Madillf91ce812014-06-13 10:04:34 -0400430
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200431 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500432 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200433 out << "\n// Equality functions\n\n";
434 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500435 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200436 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200437 }
438 }
Olli Etuaho12690762015-03-31 12:55:28 +0300439 if (!mArrayAssignmentFunctions.empty())
440 {
441 out << "\n// Assignment functions\n\n";
442 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
443 {
444 out << assignmentFunction.functionDefinition << "\n";
445 }
446 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300447 if (!mArrayConstructIntoFunctions.empty())
448 {
449 out << "\n// Array constructor functions\n\n";
450 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
451 {
452 out << constructIntoFunction.functionDefinition << "\n";
453 }
454 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200455
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500456 if (mUsesDiscardRewriting)
457 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400458 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500459 }
460
Nicolas Capens655fe362014-04-11 13:12:34 -0400461 if (mUsesNestedBreak)
462 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400463 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400464 }
465
Arun Patole44efa0b2015-03-04 17:11:05 +0530466 if (mRequiresIEEEStrictCompiling)
467 {
468 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
469 }
470
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400471 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
472 "#define LOOP [loop]\n"
473 "#define FLATTEN [flatten]\n"
474 "#else\n"
475 "#define LOOP\n"
476 "#define FLATTEN\n"
477 "#endif\n";
478
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200479 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +0300481 const bool usingMRTExtension =
482 IsExtensionEnabled(mExtensionBehavior, TExtension::EXT_draw_buffers);
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000483
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000484 out << "// Varyings\n";
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500485 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400486 out << "\n";
487
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200488 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000489 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500490 for (ReferencedSymbols::const_iterator outputVariableIt =
491 mReferencedOutputVariables.begin();
492 outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000493 {
Jamie Madill46131a32013-06-20 11:55:50 -0400494 const TString &variableName = outputVariableIt->first;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500495 const TType &variableType = outputVariableIt->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
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000874 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000875 {
876 mUsesDepthRange = true;
877 out << name;
878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879 else
880 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000881 TQualifier qualifier = node->getQualifier();
882
883 if (qualifier == EvqUniform)
884 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500885 const TType &nodeType = node->getType();
Jamie Madill2e295e22015-04-29 10:41:33 -0400886 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -0400887
888 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000889 {
Jiajia Qin9b11ea42017-07-11 16:50:08 +0800890 mReferencedUniformBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +0000891 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000892 else
893 {
894 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000895 }
Jamie Madill98493dd2013-07-08 14:39:03 -0400896
Jamie Madill2e295e22015-04-29 10:41:33 -0400897 ensureStructDefined(nodeType);
898
Olli Etuahoff526f12017-06-30 12:26:54 +0300899 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000900 }
Jamie Madill19571812013-08-12 15:26:34 -0700901 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000902 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000903 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400904 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000905 }
Jamie Madill033dae62014-06-18 12:56:28 -0400906 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000907 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000908 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -0400909 out << Decorate(name);
Martin Radev41ac68e2017-06-06 12:16:58 +0300910 if (name == "ViewID_OVR")
911 {
912 mUsesViewID = true;
913 }
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +0000914 }
Jamie Madill19571812013-08-12 15:26:34 -0700915 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -0400916 {
917 mReferencedOutputVariables[name] = node;
918 out << "out_" << name;
919 }
920 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +0000921 {
922 out << "gl_Color[0]";
923 mUsesFragColor = true;
924 }
925 else if (qualifier == EvqFragData)
926 {
927 out << "gl_Color";
928 mUsesFragData = true;
929 }
930 else if (qualifier == EvqFragCoord)
931 {
932 mUsesFragCoord = true;
933 out << name;
934 }
935 else if (qualifier == EvqPointCoord)
936 {
937 mUsesPointCoord = true;
938 out << name;
939 }
940 else if (qualifier == EvqFrontFacing)
941 {
942 mUsesFrontFacing = true;
943 out << name;
944 }
945 else if (qualifier == EvqPointSize)
946 {
947 mUsesPointSize = true;
948 out << name;
949 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000950 else if (qualifier == EvqInstanceID)
951 {
952 mUsesInstanceID = true;
953 out << name;
954 }
Corentin Wallezb076add2016-01-11 16:45:46 -0500955 else if (qualifier == EvqVertexID)
956 {
957 mUsesVertexID = true;
958 out << name;
959 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +0300960 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400961 {
962 mUsesFragDepth = true;
963 out << "gl_Depth";
964 }
Xinghua Caob1239382016-12-13 15:07:05 +0800965 else if (qualifier == EvqNumWorkGroups)
966 {
967 mUsesNumWorkGroups = true;
968 out << name;
969 }
970 else if (qualifier == EvqWorkGroupID)
971 {
972 mUsesWorkGroupID = true;
973 out << name;
974 }
975 else if (qualifier == EvqLocalInvocationID)
976 {
977 mUsesLocalInvocationID = true;
978 out << name;
979 }
980 else if (qualifier == EvqGlobalInvocationID)
981 {
982 mUsesGlobalInvocationID = true;
983 out << name;
984 }
985 else if (qualifier == EvqLocalInvocationIndex)
986 {
987 mUsesLocalInvocationIndex = true;
988 out << name;
989 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000990 else
991 {
Olli Etuahoff526f12017-06-30 12:26:54 +0300992 out << DecorateVariableIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +0000993 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000994 }
995}
996
Jamie Madill4cfb1e82014-07-07 12:49:23 -0400997void OutputHLSL::visitRaw(TIntermRaw *node)
998{
Jamie Madill32aab012015-01-27 14:12:26 -0500999 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001000}
1001
Olli Etuaho7fb49552015-03-18 17:27:44 +02001002void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1003{
1004 if (type.isScalar() && !type.isArray())
1005 {
1006 if (op == EOpEqual)
1007 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001008 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001009 }
1010 else
1011 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001012 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001013 }
1014 }
1015 else
1016 {
1017 if (visit == PreVisit && op == EOpNotEqual)
1018 {
1019 out << "!";
1020 }
1021
1022 if (type.isArray())
1023 {
1024 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001025 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001026 }
1027 else if (type.getBasicType() == EbtStruct)
1028 {
1029 const TStructure &structure = *type.getStruct();
1030 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001031 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001032 }
1033 else
1034 {
1035 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001036 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001037 }
1038 }
1039}
1040
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001041void OutputHLSL::outputAssign(Visit visit, const TType &type, TInfoSinkBase &out)
1042{
1043 if (type.isArray())
1044 {
1045 const TString &functionName = addArrayAssignmentFunction(type);
1046 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
1047 }
1048 else
1049 {
1050 outputTriplet(out, visit, "(", " = ", ")");
1051 }
1052}
1053
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001054bool OutputHLSL::ancestorEvaluatesToSamplerInStruct()
Olli Etuaho96963162016-03-21 11:54:33 +02001055{
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001056 for (unsigned int n = 0u; getAncestorNode(n) != nullptr; ++n)
Olli Etuaho96963162016-03-21 11:54:33 +02001057 {
1058 TIntermNode *ancestor = getAncestorNode(n);
1059 const TIntermBinary *ancestorBinary = ancestor->getAsBinaryNode();
1060 if (ancestorBinary == nullptr)
1061 {
1062 return false;
1063 }
1064 switch (ancestorBinary->getOp())
1065 {
1066 case EOpIndexDirectStruct:
1067 {
1068 const TStructure *structure = ancestorBinary->getLeft()->getType().getStruct();
1069 const TIntermConstantUnion *index =
1070 ancestorBinary->getRight()->getAsConstantUnion();
1071 const TField *field = structure->fields()[index->getIConst(0)];
1072 if (IsSampler(field->type()->getBasicType()))
1073 {
1074 return true;
1075 }
1076 break;
1077 }
1078 case EOpIndexDirect:
1079 break;
1080 default:
1081 // Returning a sampler from indirect indexing is not supported.
1082 return false;
1083 }
1084 }
1085 return false;
1086}
1087
Olli Etuahob6fa0432016-09-28 16:28:05 +01001088bool OutputHLSL::visitSwizzle(Visit visit, TIntermSwizzle *node)
1089{
1090 TInfoSinkBase &out = getInfoSink();
1091 if (visit == PostVisit)
1092 {
1093 out << ".";
1094 node->writeOffsetsAsXYZW(&out);
1095 }
1096 return true;
1097}
1098
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1100{
Jamie Madill32aab012015-01-27 14:12:26 -05001101 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102
1103 switch (node->getOp())
1104 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001105 case EOpComma:
1106 outputTriplet(out, visit, "(", ", ", ")");
1107 break;
1108 case EOpAssign:
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001109 if (node->isArray())
Olli Etuaho9638c352015-04-01 14:34:52 +03001110 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001111 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1112 if (rightAgg != nullptr && rightAgg->isConstructor())
Olli Etuaho9638c352015-04-01 14:34:52 +03001113 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001114 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1115 out << functionName << "(";
1116 node->getLeft()->traverse(this);
1117 TIntermSequence *seq = rightAgg->getSequence();
1118 for (auto &arrayElement : *seq)
1119 {
1120 out << ", ";
1121 arrayElement->traverse(this);
1122 }
1123 out << ")";
1124 return false;
Olli Etuaho9638c352015-04-01 14:34:52 +03001125 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001126 // ArrayReturnValueToOutParameter should have eliminated expressions where a
1127 // function call is assigned.
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001128 ASSERT(rightAgg == nullptr);
Olli Etuaho9638c352015-04-01 14:34:52 +03001129 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001130 outputAssign(visit, node->getType(), out);
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001131 break;
1132 case EOpInitialize:
1133 if (visit == PreVisit)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001134 {
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001135 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
1136 ASSERT(symbolNode);
1137 TIntermTyped *expression = node->getRight();
1138
1139 // Global initializers must be constant at this point.
1140 ASSERT(symbolNode->getQualifier() != EvqGlobal ||
1141 canWriteAsHLSLLiteral(expression));
1142
1143 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1144 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1145 // new variable is created before the assignment is evaluated), so we need to
1146 // convert
1147 // this to "float t = x, x = t;".
1148 if (writeSameSymbolInitializer(out, symbolNode, expression))
1149 {
1150 // Skip initializing the rest of the expression
1151 return false;
1152 }
1153 else if (writeConstantInitialization(out, symbolNode, expression))
1154 {
1155 return false;
1156 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001157 }
Olli Etuaho4db7ded2016-10-13 12:23:11 +01001158 else if (visit == InVisit)
1159 {
1160 out << " = ";
1161 }
1162 break;
1163 case EOpAddAssign:
1164 outputTriplet(out, visit, "(", " += ", ")");
1165 break;
1166 case EOpSubAssign:
1167 outputTriplet(out, visit, "(", " -= ", ")");
1168 break;
1169 case EOpMulAssign:
1170 outputTriplet(out, visit, "(", " *= ", ")");
1171 break;
1172 case EOpVectorTimesScalarAssign:
1173 outputTriplet(out, visit, "(", " *= ", ")");
1174 break;
1175 case EOpMatrixTimesScalarAssign:
1176 outputTriplet(out, visit, "(", " *= ", ")");
1177 break;
1178 case EOpVectorTimesMatrixAssign:
1179 if (visit == PreVisit)
1180 {
1181 out << "(";
1182 }
1183 else if (visit == InVisit)
1184 {
1185 out << " = mul(";
1186 node->getLeft()->traverse(this);
1187 out << ", transpose(";
1188 }
1189 else
1190 {
1191 out << ")))";
1192 }
1193 break;
1194 case EOpMatrixTimesMatrixAssign:
1195 if (visit == PreVisit)
1196 {
1197 out << "(";
1198 }
1199 else if (visit == InVisit)
1200 {
1201 out << " = transpose(mul(transpose(";
1202 node->getLeft()->traverse(this);
1203 out << "), transpose(";
1204 }
1205 else
1206 {
1207 out << "))))";
1208 }
1209 break;
1210 case EOpDivAssign:
1211 outputTriplet(out, visit, "(", " /= ", ")");
1212 break;
1213 case EOpIModAssign:
1214 outputTriplet(out, visit, "(", " %= ", ")");
1215 break;
1216 case EOpBitShiftLeftAssign:
1217 outputTriplet(out, visit, "(", " <<= ", ")");
1218 break;
1219 case EOpBitShiftRightAssign:
1220 outputTriplet(out, visit, "(", " >>= ", ")");
1221 break;
1222 case EOpBitwiseAndAssign:
1223 outputTriplet(out, visit, "(", " &= ", ")");
1224 break;
1225 case EOpBitwiseXorAssign:
1226 outputTriplet(out, visit, "(", " ^= ", ")");
1227 break;
1228 case EOpBitwiseOrAssign:
1229 outputTriplet(out, visit, "(", " |= ", ")");
1230 break;
1231 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001232 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001233 const TType &leftType = node->getLeft()->getType();
Jamie Madill98493dd2013-07-08 14:39:03 -04001234 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001235 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001236 if (visit == PreVisit)
1237 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001238 TInterfaceBlock *interfaceBlock = leftType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001239 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001240 mReferencedUniformBlocks[interfaceBlock->instanceName()] =
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001241 node->getLeft()->getAsSymbolNode();
Jiajia Qin9b11ea42017-07-11 16:50:08 +08001242 out << mUniformHLSL->uniformBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001243 return false;
1244 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001245 }
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001246 else if (ancestorEvaluatesToSamplerInStruct())
Olli Etuaho96963162016-03-21 11:54:33 +02001247 {
1248 // All parts of an expression that access a sampler in a struct need to use _ as
1249 // separator to access the sampler variable that has been moved out of the struct.
1250 outputTriplet(out, visit, "", "_", "");
1251 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001252 else
1253 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001254 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001255 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001256 }
1257 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001258 case EOpIndexIndirect:
1259 // We do not currently support indirect references to interface blocks
1260 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
1261 outputTriplet(out, visit, "", "[", "]");
1262 break;
1263 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001264 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001265 const TStructure *structure = node->getLeft()->getType().getStruct();
1266 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1267 const TField *field = structure->fields()[index->getIConst(0)];
Jamie Madill98493dd2013-07-08 14:39:03 -04001268
Olli Etuaho96963162016-03-21 11:54:33 +02001269 // In cases where indexing returns a sampler, we need to access the sampler variable
1270 // that has been moved out of the struct.
1271 bool indexingReturnsSampler = IsSampler(field->type()->getBasicType());
1272 if (visit == PreVisit && indexingReturnsSampler)
1273 {
1274 // Samplers extracted from structs have "angle" prefix to avoid name conflicts.
1275 // This prefix is only output at the beginning of the indexing expression, which
1276 // may have multiple parts.
1277 out << "angle";
1278 }
1279 if (!indexingReturnsSampler)
1280 {
1281 // All parts of an expression that access a sampler in a struct need to use _ as
1282 // separator to access the sampler variable that has been moved out of the struct.
Olli Etuaho1d9dcc22017-01-19 11:25:32 +00001283 indexingReturnsSampler = ancestorEvaluatesToSamplerInStruct();
Olli Etuaho96963162016-03-21 11:54:33 +02001284 }
1285 if (visit == InVisit)
1286 {
1287 if (indexingReturnsSampler)
1288 {
1289 out << "_" + field->name();
1290 }
1291 else
1292 {
1293 out << "." + DecorateField(field->name(), *structure);
1294 }
1295
1296 return false;
1297 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001298 }
1299 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001300 case EOpIndexDirectInterfaceBlock:
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001301 {
1302 bool structInStd140Block =
1303 node->getBasicType() == EbtStruct && IsInStd140InterfaceBlock(node->getLeft());
1304 if (visit == PreVisit && structInStd140Block)
1305 {
1306 out << "map";
1307 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001308 if (visit == InVisit)
1309 {
1310 const TInterfaceBlock *interfaceBlock =
1311 node->getLeft()->getType().getInterfaceBlock();
1312 const TIntermConstantUnion *index = node->getRight()->getAsConstantUnion();
1313 const TField *field = interfaceBlock->fields()[index->getIConst(0)];
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001314 if (structInStd140Block)
1315 {
1316 out << "_";
1317 }
1318 else
1319 {
1320 out << ".";
1321 }
1322 out << Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001323
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001324 return false;
1325 }
1326 break;
Olli Etuaho2ef23e22017-11-01 16:39:11 +02001327 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001328 case EOpAdd:
1329 outputTriplet(out, visit, "(", " + ", ")");
1330 break;
1331 case EOpSub:
1332 outputTriplet(out, visit, "(", " - ", ")");
1333 break;
1334 case EOpMul:
1335 outputTriplet(out, visit, "(", " * ", ")");
1336 break;
1337 case EOpDiv:
1338 outputTriplet(out, visit, "(", " / ", ")");
1339 break;
1340 case EOpIMod:
1341 outputTriplet(out, visit, "(", " % ", ")");
1342 break;
1343 case EOpBitShiftLeft:
1344 outputTriplet(out, visit, "(", " << ", ")");
1345 break;
1346 case EOpBitShiftRight:
1347 outputTriplet(out, visit, "(", " >> ", ")");
1348 break;
1349 case EOpBitwiseAnd:
1350 outputTriplet(out, visit, "(", " & ", ")");
1351 break;
1352 case EOpBitwiseXor:
1353 outputTriplet(out, visit, "(", " ^ ", ")");
1354 break;
1355 case EOpBitwiseOr:
1356 outputTriplet(out, visit, "(", " | ", ")");
1357 break;
1358 case EOpEqual:
1359 case EOpNotEqual:
1360 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
1361 break;
1362 case EOpLessThan:
1363 outputTriplet(out, visit, "(", " < ", ")");
1364 break;
1365 case EOpGreaterThan:
1366 outputTriplet(out, visit, "(", " > ", ")");
1367 break;
1368 case EOpLessThanEqual:
1369 outputTriplet(out, visit, "(", " <= ", ")");
1370 break;
1371 case EOpGreaterThanEqual:
1372 outputTriplet(out, visit, "(", " >= ", ")");
1373 break;
1374 case EOpVectorTimesScalar:
1375 outputTriplet(out, visit, "(", " * ", ")");
1376 break;
1377 case EOpMatrixTimesScalar:
1378 outputTriplet(out, visit, "(", " * ", ")");
1379 break;
1380 case EOpVectorTimesMatrix:
1381 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1382 break;
1383 case EOpMatrixTimesVector:
1384 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1385 break;
1386 case EOpMatrixTimesMatrix:
1387 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1388 break;
1389 case EOpLogicalOr:
1390 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have
1391 // been unfolded.
1392 ASSERT(!node->getRight()->hasSideEffects());
1393 outputTriplet(out, visit, "(", " || ", ")");
1394 return true;
1395 case EOpLogicalXor:
1396 mUsesXor = true;
1397 outputTriplet(out, visit, "xor(", ", ", ")");
1398 break;
1399 case EOpLogicalAnd:
1400 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have
1401 // been unfolded.
1402 ASSERT(!node->getRight()->hasSideEffects());
1403 outputTriplet(out, visit, "(", " && ", ")");
1404 return true;
1405 default:
1406 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001407 }
1408
1409 return true;
1410}
1411
1412bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1413{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001414 TInfoSinkBase &out = getInfoSink();
1415
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001416 switch (node->getOp())
1417 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001418 case EOpNegative:
1419 outputTriplet(out, visit, "(-", "", ")");
1420 break;
1421 case EOpPositive:
1422 outputTriplet(out, visit, "(+", "", ")");
1423 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001424 case EOpLogicalNot:
1425 outputTriplet(out, visit, "(!", "", ")");
1426 break;
1427 case EOpBitwiseNot:
1428 outputTriplet(out, visit, "(~", "", ")");
1429 break;
1430 case EOpPostIncrement:
1431 outputTriplet(out, visit, "(", "", "++)");
1432 break;
1433 case EOpPostDecrement:
1434 outputTriplet(out, visit, "(", "", "--)");
1435 break;
1436 case EOpPreIncrement:
1437 outputTriplet(out, visit, "(++", "", ")");
1438 break;
1439 case EOpPreDecrement:
1440 outputTriplet(out, visit, "(--", "", ")");
1441 break;
1442 case EOpRadians:
1443 outputTriplet(out, visit, "radians(", "", ")");
1444 break;
1445 case EOpDegrees:
1446 outputTriplet(out, visit, "degrees(", "", ")");
1447 break;
1448 case EOpSin:
1449 outputTriplet(out, visit, "sin(", "", ")");
1450 break;
1451 case EOpCos:
1452 outputTriplet(out, visit, "cos(", "", ")");
1453 break;
1454 case EOpTan:
1455 outputTriplet(out, visit, "tan(", "", ")");
1456 break;
1457 case EOpAsin:
1458 outputTriplet(out, visit, "asin(", "", ")");
1459 break;
1460 case EOpAcos:
1461 outputTriplet(out, visit, "acos(", "", ")");
1462 break;
1463 case EOpAtan:
1464 outputTriplet(out, visit, "atan(", "", ")");
1465 break;
1466 case EOpSinh:
1467 outputTriplet(out, visit, "sinh(", "", ")");
1468 break;
1469 case EOpCosh:
1470 outputTriplet(out, visit, "cosh(", "", ")");
1471 break;
1472 case EOpTanh:
1473 outputTriplet(out, visit, "tanh(", "", ")");
1474 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001475 case EOpAsinh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001476 case EOpAcosh:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001477 case EOpAtanh:
1478 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001479 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001480 break;
1481 case EOpExp:
1482 outputTriplet(out, visit, "exp(", "", ")");
1483 break;
1484 case EOpLog:
1485 outputTriplet(out, visit, "log(", "", ")");
1486 break;
1487 case EOpExp2:
1488 outputTriplet(out, visit, "exp2(", "", ")");
1489 break;
1490 case EOpLog2:
1491 outputTriplet(out, visit, "log2(", "", ")");
1492 break;
1493 case EOpSqrt:
1494 outputTriplet(out, visit, "sqrt(", "", ")");
1495 break;
1496 case EOpInverseSqrt:
1497 outputTriplet(out, visit, "rsqrt(", "", ")");
1498 break;
1499 case EOpAbs:
1500 outputTriplet(out, visit, "abs(", "", ")");
1501 break;
1502 case EOpSign:
1503 outputTriplet(out, visit, "sign(", "", ")");
1504 break;
1505 case EOpFloor:
1506 outputTriplet(out, visit, "floor(", "", ")");
1507 break;
1508 case EOpTrunc:
1509 outputTriplet(out, visit, "trunc(", "", ")");
1510 break;
1511 case EOpRound:
1512 outputTriplet(out, visit, "round(", "", ")");
1513 break;
1514 case EOpRoundEven:
1515 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001516 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001517 break;
1518 case EOpCeil:
1519 outputTriplet(out, visit, "ceil(", "", ")");
1520 break;
1521 case EOpFract:
1522 outputTriplet(out, visit, "frac(", "", ")");
1523 break;
1524 case EOpIsNan:
1525 if (node->getUseEmulatedFunction())
Olli Etuahod68924e2017-01-02 17:34:40 +00001526 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001527 else
1528 outputTriplet(out, visit, "isnan(", "", ")");
1529 mRequiresIEEEStrictCompiling = true;
1530 break;
1531 case EOpIsInf:
1532 outputTriplet(out, visit, "isinf(", "", ")");
1533 break;
1534 case EOpFloatBitsToInt:
1535 outputTriplet(out, visit, "asint(", "", ")");
1536 break;
1537 case EOpFloatBitsToUint:
1538 outputTriplet(out, visit, "asuint(", "", ")");
1539 break;
1540 case EOpIntBitsToFloat:
1541 outputTriplet(out, visit, "asfloat(", "", ")");
1542 break;
1543 case EOpUintBitsToFloat:
1544 outputTriplet(out, visit, "asfloat(", "", ")");
1545 break;
1546 case EOpPackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001547 case EOpPackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001548 case EOpPackHalf2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001549 case EOpUnpackSnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001550 case EOpUnpackUnorm2x16:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001551 case EOpUnpackHalf2x16:
Olli Etuaho25aef452017-01-29 16:15:44 -08001552 case EOpPackUnorm4x8:
1553 case EOpPackSnorm4x8:
1554 case EOpUnpackUnorm4x8:
1555 case EOpUnpackSnorm4x8:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001556 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001557 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001558 break;
1559 case EOpLength:
1560 outputTriplet(out, visit, "length(", "", ")");
1561 break;
1562 case EOpNormalize:
1563 outputTriplet(out, visit, "normalize(", "", ")");
1564 break;
1565 case EOpDFdx:
1566 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1567 {
1568 outputTriplet(out, visit, "(", "", ", 0.0)");
1569 }
1570 else
1571 {
1572 outputTriplet(out, visit, "ddx(", "", ")");
1573 }
1574 break;
1575 case EOpDFdy:
1576 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1577 {
1578 outputTriplet(out, visit, "(", "", ", 0.0)");
1579 }
1580 else
1581 {
1582 outputTriplet(out, visit, "ddy(", "", ")");
1583 }
1584 break;
1585 case EOpFwidth:
1586 if (mInsideDiscontinuousLoop || mOutputLod0Function)
1587 {
1588 outputTriplet(out, visit, "(", "", ", 0.0)");
1589 }
1590 else
1591 {
1592 outputTriplet(out, visit, "fwidth(", "", ")");
1593 }
1594 break;
1595 case EOpTranspose:
1596 outputTriplet(out, visit, "transpose(", "", ")");
1597 break;
1598 case EOpDeterminant:
1599 outputTriplet(out, visit, "determinant(transpose(", "", "))");
1600 break;
1601 case EOpInverse:
1602 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00001603 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001604 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02001605
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001606 case EOpAny:
1607 outputTriplet(out, visit, "any(", "", ")");
1608 break;
1609 case EOpAll:
1610 outputTriplet(out, visit, "all(", "", ")");
1611 break;
Olli Etuahod68924e2017-01-02 17:34:40 +00001612 case EOpLogicalNotComponentWise:
1613 outputTriplet(out, visit, "(!", "", ")");
1614 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00001615 case EOpBitfieldReverse:
1616 outputTriplet(out, visit, "reversebits(", "", ")");
1617 break;
1618 case EOpBitCount:
1619 outputTriplet(out, visit, "countbits(", "", ")");
1620 break;
1621 case EOpFindLSB:
1622 // Note that it's unclear from the HLSL docs what this returns for 0, but this is tested
1623 // in GLSLTest and results are consistent with GL.
1624 outputTriplet(out, visit, "firstbitlow(", "", ")");
1625 break;
1626 case EOpFindMSB:
1627 // Note that it's unclear from the HLSL docs what this returns for 0 or -1, but this is
1628 // tested in GLSLTest and results are consistent with GL.
1629 outputTriplet(out, visit, "firstbithigh(", "", ")");
1630 break;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05001631 default:
1632 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001633 }
1634
1635 return true;
1636}
1637
Olli Etuaho96963162016-03-21 11:54:33 +02001638TString OutputHLSL::samplerNamePrefixFromStruct(TIntermTyped *node)
1639{
1640 if (node->getAsSymbolNode())
1641 {
1642 return node->getAsSymbolNode()->getSymbol();
1643 }
1644 TIntermBinary *nodeBinary = node->getAsBinaryNode();
1645 switch (nodeBinary->getOp())
1646 {
1647 case EOpIndexDirect:
1648 {
1649 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1650
1651 TInfoSinkBase prefixSink;
1652 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_" << index;
1653 return TString(prefixSink.c_str());
1654 }
1655 case EOpIndexDirectStruct:
1656 {
1657 TStructure *s = nodeBinary->getLeft()->getAsTyped()->getType().getStruct();
1658 int index = nodeBinary->getRight()->getAsConstantUnion()->getIConst(0);
1659 const TField *field = s->fields()[index];
1660
1661 TInfoSinkBase prefixSink;
1662 prefixSink << samplerNamePrefixFromStruct(nodeBinary->getLeft()) << "_"
1663 << field->name();
1664 return TString(prefixSink.c_str());
1665 }
1666 default:
1667 UNREACHABLE();
1668 return TString("");
1669 }
1670}
1671
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001672bool OutputHLSL::visitBlock(Visit visit, TIntermBlock *node)
1673{
1674 TInfoSinkBase &out = getInfoSink();
1675
1676 if (mInsideFunction)
1677 {
1678 outputLineDirective(out, node->getLine().first_line);
1679 out << "{\n";
1680 }
1681
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001682 for (TIntermNode *statement : *node->getSequence())
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001683 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001684 outputLineDirective(out, statement->getLine().first_line);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001685
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001686 statement->traverse(this);
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001687
1688 // Don't output ; after case labels, they're terminated by :
1689 // This is needed especially since outputting a ; after a case statement would turn empty
1690 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001691 // Also the output code is clearer if we don't output ; after statements where it is not
1692 // needed:
1693 // * if statements
1694 // * switch statements
1695 // * blocks
1696 // * function definitions
1697 // * loops (do-while loops output the semicolon in VisitLoop)
1698 // * declarations that don't generate output.
1699 if (statement->getAsCaseNode() == nullptr && statement->getAsIfElseNode() == nullptr &&
1700 statement->getAsBlock() == nullptr && statement->getAsLoopNode() == nullptr &&
1701 statement->getAsSwitchNode() == nullptr &&
1702 statement->getAsFunctionDefinition() == nullptr &&
1703 (statement->getAsDeclarationNode() == nullptr ||
1704 IsDeclarationWrittenOut(statement->getAsDeclarationNode())) &&
1705 statement->getAsInvariantDeclarationNode() == nullptr)
1706 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001707 out << ";\n";
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001708 }
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01001709 }
1710
1711 if (mInsideFunction)
1712 {
1713 outputLineDirective(out, node->getLine().last_line);
1714 out << "}\n";
1715 }
1716
1717 return false;
1718}
1719
Olli Etuaho336b1472016-10-05 16:37:55 +01001720bool OutputHLSL::visitFunctionDefinition(Visit visit, TIntermFunctionDefinition *node)
1721{
1722 TInfoSinkBase &out = getInfoSink();
1723
1724 ASSERT(mCurrentFunctionMetadata == nullptr);
1725
1726 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1727 ASSERT(index != CallDAG::InvalidIndex);
1728 mCurrentFunctionMetadata = &mASTMetadataList[index];
1729
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001730 out << TypeString(node->getFunctionPrototype()->getType()) << " ";
Olli Etuaho336b1472016-10-05 16:37:55 +01001731
Olli Etuaho8ad9e752017-01-16 19:55:20 +00001732 TIntermSequence *parameters = node->getFunctionPrototype()->getSequence();
Olli Etuaho336b1472016-10-05 16:37:55 +01001733
1734 if (node->getFunctionSymbolInfo()->isMain())
1735 {
1736 out << "gl_main(";
1737 }
1738 else
1739 {
Olli Etuahoff526f12017-06-30 12:26:54 +03001740 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj())
Olli Etuaho336b1472016-10-05 16:37:55 +01001741 << DisambiguateFunctionName(parameters) << (mOutputLod0Function ? "Lod0(" : "(");
1742 }
1743
1744 for (unsigned int i = 0; i < parameters->size(); i++)
1745 {
1746 TIntermSymbol *symbol = (*parameters)[i]->getAsSymbolNode();
1747
1748 if (symbol)
1749 {
1750 ensureStructDefined(symbol->getType());
1751
1752 out << argumentString(symbol);
1753
1754 if (i < parameters->size() - 1)
1755 {
1756 out << ", ";
1757 }
1758 }
1759 else
1760 UNREACHABLE();
1761 }
1762
1763 out << ")\n";
1764
1765 mInsideFunction = true;
1766 // The function body node will output braces.
1767 node->getBody()->traverse(this);
1768 mInsideFunction = false;
1769
1770 mCurrentFunctionMetadata = nullptr;
1771
1772 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1773 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1774 {
1775 ASSERT(!node->getFunctionSymbolInfo()->isMain());
1776 mOutputLod0Function = true;
1777 node->traverse(this);
1778 mOutputLod0Function = false;
1779 }
1780
1781 return false;
1782}
1783
Olli Etuaho13389b62016-10-16 11:48:18 +01001784bool OutputHLSL::visitDeclaration(Visit visit, TIntermDeclaration *node)
1785{
Olli Etuaho13389b62016-10-16 11:48:18 +01001786 if (visit == PreVisit)
1787 {
1788 TIntermSequence *sequence = node->getSequence();
1789 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
1790 ASSERT(sequence->size() == 1);
Olli Etuaho282847e2017-07-12 14:11:01 +03001791 ASSERT(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01001792
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001793 if (IsDeclarationWrittenOut(node))
Olli Etuaho13389b62016-10-16 11:48:18 +01001794 {
Olli Etuaho40dbdd62017-10-13 13:34:19 +03001795 TInfoSinkBase &out = getInfoSink();
Olli Etuaho13389b62016-10-16 11:48:18 +01001796 ensureStructDefined(variable->getType());
1797
1798 if (!variable->getAsSymbolNode() ||
1799 variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
1800 {
1801 if (!mInsideFunction)
1802 {
1803 out << "static ";
1804 }
1805
1806 out << TypeString(variable->getType()) + " ";
1807
1808 TIntermSymbol *symbol = variable->getAsSymbolNode();
1809
1810 if (symbol)
1811 {
1812 symbol->traverse(this);
1813 out << ArrayString(symbol->getType());
1814 out << " = " + initializer(symbol->getType());
1815 }
1816 else
1817 {
1818 variable->traverse(this);
1819 }
1820 }
1821 else if (variable->getAsSymbolNode() &&
1822 variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
1823 {
1824 // Already added to constructor map
1825 }
1826 else
1827 UNREACHABLE();
1828 }
Olli Etuaho282847e2017-07-12 14:11:01 +03001829 else if (IsVaryingOut(variable->getQualifier()))
Olli Etuaho13389b62016-10-16 11:48:18 +01001830 {
Olli Etuaho282847e2017-07-12 14:11:01 +03001831 TIntermSymbol *symbol = variable->getAsSymbolNode();
1832 ASSERT(symbol); // Varying declarations can't have initializers.
Olli Etuaho13389b62016-10-16 11:48:18 +01001833
Olli Etuaho282847e2017-07-12 14:11:01 +03001834 // Vertex outputs which are declared but not written to should still be declared to
1835 // allow successful linking.
1836 mReferencedVaryings[symbol->getSymbol()] = symbol;
Olli Etuaho13389b62016-10-16 11:48:18 +01001837 }
1838 }
1839 return false;
1840}
1841
Olli Etuahobf4e1b72016-12-09 11:30:15 +00001842bool OutputHLSL::visitInvariantDeclaration(Visit visit, TIntermInvariantDeclaration *node)
1843{
1844 // Do not do any translation
1845 return false;
1846}
1847
Olli Etuaho16c745a2017-01-16 17:02:27 +00001848bool OutputHLSL::visitFunctionPrototype(Visit visit, TIntermFunctionPrototype *node)
1849{
1850 TInfoSinkBase &out = getInfoSink();
1851
1852 ASSERT(visit == PreVisit);
1853 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
1854 // Skip the prototype if it is not implemented (and thus not used)
1855 if (index == CallDAG::InvalidIndex)
1856 {
1857 return false;
1858 }
1859
1860 TIntermSequence *arguments = node->getSequence();
1861
Olli Etuahoff526f12017-06-30 12:26:54 +03001862 TString name = DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuaho16c745a2017-01-16 17:02:27 +00001863 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
1864 << (mOutputLod0Function ? "Lod0(" : "(");
1865
1866 for (unsigned int i = 0; i < arguments->size(); i++)
1867 {
1868 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
1869 ASSERT(symbol != nullptr);
1870
1871 out << argumentString(symbol);
1872
1873 if (i < arguments->size() - 1)
1874 {
1875 out << ", ";
1876 }
1877 }
1878
1879 out << ");\n";
1880
1881 // Also prototype the Lod0 variant if needed
1882 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
1883 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
1884 {
1885 mOutputLod0Function = true;
1886 node->traverse(this);
1887 mOutputLod0Function = false;
1888 }
1889
1890 return false;
1891}
1892
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
1894{
Jamie Madill32aab012015-01-27 14:12:26 -05001895 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001896
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001897 switch (node->getOp())
1898 {
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001899 case EOpCallBuiltInFunction:
1900 case EOpCallFunctionInAST:
1901 case EOpCallInternalRawFunction:
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001902 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001903 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001904
Corentin Wallez1239ee92015-03-19 14:38:02 -07001905 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001906 if (node->getOp() == EOpCallFunctionInAST)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001907 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001908 if (node->isArray())
1909 {
1910 UNIMPLEMENTED();
1911 }
Olli Etuahobd674552016-10-06 13:28:42 +01001912 size_t index = mCallDag.findIndex(node->getFunctionSymbolInfo());
Corentin Wallez1239ee92015-03-19 14:38:02 -07001913 ASSERT(index != CallDAG::InvalidIndex);
1914 lod0 &= mASTMetadataList[index].mNeedsLod0;
1915
Olli Etuahoff526f12017-06-30 12:26:54 +03001916 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj());
Olli Etuahobe59c2f2016-03-07 11:32:34 +02001917 out << DisambiguateFunctionName(node->getSequence());
1918 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919 }
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08001920 else if (node->getOp() == EOpCallInternalRawFunction)
Olli Etuahob741c762016-06-29 15:49:22 +03001921 {
1922 // This path is used for internal functions that don't have their definitions in the
1923 // AST, such as precision emulation functions.
Olli Etuahoff526f12017-06-30 12:26:54 +03001924 out << DecorateFunctionIfNeeded(node->getFunctionSymbolInfo()->getNameObj()) << "(";
Olli Etuahob741c762016-06-29 15:49:22 +03001925 }
Xinghua Cao711b7a12017-10-09 13:38:12 +08001926 else if (node->getFunctionSymbolInfo()->isImageFunction())
1927 {
1928 TString name = node->getFunctionSymbolInfo()->getName();
1929 TType type = (*arguments)[0]->getAsTyped()->getType();
1930 TString imageFunctionName = mImageFunctionHLSL->useImageFunction(
1931 name, type.getBasicType(), type.getLayoutQualifier().imageInternalFormat,
1932 type.getMemoryQualifier().readonly);
1933 out << imageFunctionName << "(";
1934 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001935 else
1936 {
Olli Etuahoec9232b2017-03-27 17:01:37 +03001937 const TString &name = node->getFunctionSymbolInfo()->getName();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001938 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
Olli Etuaho92db39e2017-02-15 12:11:04 +00001939 int coords = 0; // textureSize(gsampler2DMS) doesn't have a second argument.
1940 if (arguments->size() > 1)
1941 {
1942 coords = (*arguments)[1]->getAsTyped()->getNominalSize();
1943 }
Olli Etuaho5858f7e2016-04-08 13:08:46 +03001944 TString textureFunctionName = mTextureFunctionHLSL->useTextureFunction(
1945 name, samplerType, coords, arguments->size(), lod0, mShaderType);
1946 out << textureFunctionName << "(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001948
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001949 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001950 {
Olli Etuaho96963162016-03-21 11:54:33 +02001951 TIntermTyped *typedArg = (*arg)->getAsTyped();
1952 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT && IsSampler(typedArg->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001953 {
1954 out << "texture_";
1955 (*arg)->traverse(this);
1956 out << ", sampler_";
1957 }
1958
1959 (*arg)->traverse(this);
1960
Olli Etuaho96963162016-03-21 11:54:33 +02001961 if (typedArg->getType().isStructureContainingSamplers())
1962 {
1963 const TType &argType = typedArg->getType();
1964 TVector<TIntermSymbol *> samplerSymbols;
1965 TString structName = samplerNamePrefixFromStruct(typedArg);
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03001966 argType.createSamplerSymbols("angle_" + structName, "", &samplerSymbols,
1967 nullptr, mSymbolTable);
Olli Etuaho96963162016-03-21 11:54:33 +02001968 for (const TIntermSymbol *sampler : samplerSymbols)
1969 {
1970 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
1971 {
1972 out << ", texture_" << sampler->getSymbol();
1973 out << ", sampler_" << sampler->getSymbol();
1974 }
1975 else
1976 {
1977 // In case of HLSL 4.1+, this symbol is the sampler index, and in case
1978 // of D3D9, it's the sampler variable.
1979 out << ", " + sampler->getSymbol();
1980 }
1981 }
1982 }
1983
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001984 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00001985 {
1986 out << ", ";
1987 }
1988 }
1989
1990 out << ")";
1991
1992 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03001994 case EOpConstruct:
1995 if (node->getBasicType() == EbtStruct)
Olli Etuahof40319e2015-03-10 14:33:00 +02001996 {
Olli Etuaho8fab3202017-05-08 18:22:22 +03001997 if (node->getType().isArray())
1998 {
1999 UNIMPLEMENTED();
2000 }
2001 const TString &structName = StructNameString(*node->getType().getStruct());
2002 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
2003 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Olli Etuahof40319e2015-03-10 14:33:00 +02002004 }
Olli Etuaho8fab3202017-05-08 18:22:22 +03002005 else
2006 {
2007 const char *name = "";
2008 if (node->getType().getNominalSize() == 1)
2009 {
2010 switch (node->getBasicType())
2011 {
2012 case EbtFloat:
2013 name = "vec1";
2014 break;
2015 case EbtInt:
2016 name = "ivec1";
2017 break;
2018 case EbtUInt:
2019 name = "uvec1";
2020 break;
2021 case EbtBool:
2022 name = "bvec1";
2023 break;
2024 default:
2025 UNREACHABLE();
2026 }
2027 }
2028 else
2029 {
2030 name = node->getType().getBuiltInTypeNameString();
2031 }
2032 outputConstructor(out, visit, node->getType(), name, node->getSequence());
2033 }
2034 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002035 case EOpEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002036 outputTriplet(out, visit, "(", " == ", ")");
2037 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002038 case EOpNotEqualComponentWise:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002039 outputTriplet(out, visit, "(", " != ", ")");
2040 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002041 case EOpLessThanComponentWise:
2042 outputTriplet(out, visit, "(", " < ", ")");
2043 break;
2044 case EOpGreaterThanComponentWise:
2045 outputTriplet(out, visit, "(", " > ", ")");
2046 break;
2047 case EOpLessThanEqualComponentWise:
2048 outputTriplet(out, visit, "(", " <= ", ")");
2049 break;
2050 case EOpGreaterThanEqualComponentWise:
2051 outputTriplet(out, visit, "(", " >= ", ")");
2052 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002053 case EOpMod:
2054 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002055 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002056 break;
2057 case EOpModf:
2058 outputTriplet(out, visit, "modf(", ", ", ")");
2059 break;
2060 case EOpPow:
2061 outputTriplet(out, visit, "pow(", ", ", ")");
2062 break;
2063 case EOpAtan:
2064 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
2065 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002066 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002067 break;
2068 case EOpMin:
2069 outputTriplet(out, visit, "min(", ", ", ")");
2070 break;
2071 case EOpMax:
2072 outputTriplet(out, visit, "max(", ", ", ")");
2073 break;
2074 case EOpClamp:
2075 outputTriplet(out, visit, "clamp(", ", ", ")");
2076 break;
2077 case EOpMix:
Arun Patoled94f6642015-05-18 16:25:12 +05302078 {
2079 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2080 if (lastParamNode->getType().getBasicType() == EbtBool)
2081 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002082 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType
2083 // y, genBType a)",
Arun Patoled94f6642015-05-18 16:25:12 +05302084 // so use emulated version.
2085 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002086 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Arun Patoled94f6642015-05-18 16:25:12 +05302087 }
2088 else
2089 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002090 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302091 }
Olli Etuaho5878f832016-10-07 10:14:58 +01002092 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302093 }
Jamie Madill8c46ab12015-12-07 16:39:19 -05002094 case EOpStep:
2095 outputTriplet(out, visit, "step(", ", ", ")");
2096 break;
2097 case EOpSmoothStep:
2098 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2099 break;
Olli Etuaho74da73f2017-02-01 15:37:48 +00002100 case EOpFrexp:
2101 case EOpLdexp:
2102 ASSERT(node->getUseEmulatedFunction());
2103 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2104 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002105 case EOpDistance:
2106 outputTriplet(out, visit, "distance(", ", ", ")");
2107 break;
2108 case EOpDot:
2109 outputTriplet(out, visit, "dot(", ", ", ")");
2110 break;
2111 case EOpCross:
2112 outputTriplet(out, visit, "cross(", ", ", ")");
2113 break;
Jamie Madille72595b2017-06-06 15:12:26 -04002114 case EOpFaceforward:
Olli Etuaho5878f832016-10-07 10:14:58 +01002115 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002116 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002117 break;
2118 case EOpReflect:
2119 outputTriplet(out, visit, "reflect(", ", ", ")");
2120 break;
2121 case EOpRefract:
2122 outputTriplet(out, visit, "refract(", ", ", ")");
2123 break;
2124 case EOpOuterProduct:
2125 ASSERT(node->getUseEmulatedFunction());
Olli Etuahod68924e2017-01-02 17:34:40 +00002126 writeEmulatedFunctionTriplet(out, visit, node->getOp());
Olli Etuaho5878f832016-10-07 10:14:58 +01002127 break;
Olli Etuahoe1805592017-01-02 16:41:20 +00002128 case EOpMulMatrixComponentWise:
Olli Etuaho5878f832016-10-07 10:14:58 +01002129 outputTriplet(out, visit, "(", " * ", ")");
2130 break;
Olli Etuaho9250cb22017-01-21 10:51:27 +00002131 case EOpBitfieldExtract:
2132 case EOpBitfieldInsert:
2133 case EOpUaddCarry:
2134 case EOpUsubBorrow:
2135 case EOpUmulExtended:
2136 case EOpImulExtended:
2137 ASSERT(node->getUseEmulatedFunction());
2138 writeEmulatedFunctionTriplet(out, visit, node->getOp());
2139 break;
Olli Etuaho5878f832016-10-07 10:14:58 +01002140 default:
2141 UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142 }
2143
2144 return true;
2145}
2146
Olli Etuaho57961272016-09-14 13:57:46 +03002147void OutputHLSL::writeIfElse(TInfoSinkBase &out, TIntermIfElse *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002149 out << "if (";
2150
2151 node->getCondition()->traverse(this);
2152
2153 out << ")\n";
2154
Jamie Madill8c46ab12015-12-07 16:39:19 -05002155 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002156
2157 bool discard = false;
2158
2159 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002160 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002161 // The trueBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002162 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002163
Olli Etuahoa6f22092015-05-08 18:31:10 +03002164 // Detect true discard
2165 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2166 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002167 else
2168 {
2169 // TODO(oetuaho): Check if the semicolon inside is necessary.
2170 // It's there as a result of conservative refactoring of the output.
2171 out << "{;}\n";
2172 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002173
Jamie Madill8c46ab12015-12-07 16:39:19 -05002174 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002175
Olli Etuahoa6f22092015-05-08 18:31:10 +03002176 if (node->getFalseBlock())
2177 {
2178 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002179
Jamie Madill8c46ab12015-12-07 16:39:19 -05002180 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002181
Olli Etuaho32db19b2016-10-04 14:43:16 +01002182 // The falseBlock child node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002183 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002184
Jamie Madill8c46ab12015-12-07 16:39:19 -05002185 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002186
Olli Etuahoa6f22092015-05-08 18:31:10 +03002187 // Detect false discard
2188 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2189 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002190
Olli Etuahoa6f22092015-05-08 18:31:10 +03002191 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002192 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002193 {
2194 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002195 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002196}
2197
Olli Etuahod0bad2c2016-09-09 18:01:16 +03002198bool OutputHLSL::visitTernary(Visit, TIntermTernary *)
2199{
2200 // Ternary ops should have been already converted to something else in the AST. HLSL ternary
2201 // operator doesn't short-circuit, so it's not the same as the GLSL ternary operator.
2202 UNREACHABLE();
2203 return false;
2204}
2205
Olli Etuaho57961272016-09-14 13:57:46 +03002206bool OutputHLSL::visitIfElse(Visit visit, TIntermIfElse *node)
Olli Etuahod81ed842015-05-12 12:46:35 +03002207{
2208 TInfoSinkBase &out = getInfoSink();
2209
Olli Etuaho3d932d82016-04-12 11:10:30 +03002210 ASSERT(mInsideFunction);
Olli Etuahod81ed842015-05-12 12:46:35 +03002211
2212 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002213 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002214 {
2215 out << "FLATTEN ";
2216 }
2217
Olli Etuaho57961272016-09-14 13:57:46 +03002218 writeIfElse(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002219
2220 return false;
2221}
2222
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002223bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002224{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002225 TInfoSinkBase &out = getInfoSink();
2226
Olli Etuaho923ecef2017-10-11 12:01:38 +03002227 ASSERT(node->getStatementList());
2228 if (visit == PreVisit)
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002229 {
Olli Etuaho89a69a02017-10-23 12:20:45 +03002230 node->setStatementList(RemoveSwitchFallThrough(node->getStatementList(), mPerfDiagnostics));
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002231 }
Olli Etuaho923ecef2017-10-11 12:01:38 +03002232 outputTriplet(out, visit, "switch (", ") ", "");
2233 // The curly braces get written when visiting the statementList block.
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002234 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002235}
2236
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002237bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002238{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002239 TInfoSinkBase &out = getInfoSink();
2240
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002241 if (node->hasCondition())
2242 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002243 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002244 return true;
2245 }
2246 else
2247 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002248 out << "default:\n";
2249 return false;
2250 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002251}
2252
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2254{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002255 TInfoSinkBase &out = getInfoSink();
2256 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257}
2258
2259bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2260{
Nicolas Capens655fe362014-04-11 13:12:34 -04002261 mNestedLoopDepth++;
2262
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002263 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002264 mInsideDiscontinuousLoop =
2265 mInsideDiscontinuousLoop || mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002266
Jamie Madill8c46ab12015-12-07 16:39:19 -05002267 TInfoSinkBase &out = getInfoSink();
2268
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002269 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002270 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002271 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002272 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002273 mInsideDiscontinuousLoop = wasDiscontinuous;
2274 mNestedLoopDepth--;
2275
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002276 return false;
2277 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002278 }
2279
Corentin Wallez1239ee92015-03-19 14:38:02 -07002280 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002281 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002283 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002284
Jamie Madill8c46ab12015-12-07 16:39:19 -05002285 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002286 }
2287 else
2288 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002289 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002290
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 if (node->getInit())
2292 {
2293 node->getInit()->traverse(this);
2294 }
2295
2296 out << "; ";
2297
alokp@chromium.org52813552010-11-16 18:36:09 +00002298 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002299 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002300 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301 }
2302
2303 out << "; ";
2304
alokp@chromium.org52813552010-11-16 18:36:09 +00002305 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002307 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002308 }
2309
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002310 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002311
Jamie Madill8c46ab12015-12-07 16:39:19 -05002312 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002313 }
2314
2315 if (node->getBody())
2316 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002317 // The loop body node will output braces.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002318 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002320 else
2321 {
2322 // TODO(oetuaho): Check if the semicolon inside is necessary.
2323 // It's there as a result of conservative refactoring of the output.
2324 out << "{;}\n";
2325 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326
Jamie Madill8c46ab12015-12-07 16:39:19 -05002327 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328
alokp@chromium.org52813552010-11-16 18:36:09 +00002329 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002331 outputLineDirective(out, node->getCondition()->getLine().first_line);
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002332 out << "while (";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333
alokp@chromium.org52813552010-11-16 18:36:09 +00002334 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335
Olli Etuaho40dbdd62017-10-13 13:34:19 +03002336 out << ");\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337 }
2338
daniel@transgaming.com73536982012-03-21 20:45:49 +00002339 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002341 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002342 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002343
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344 return false;
2345}
2346
2347bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2348{
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002349 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002351 TInfoSinkBase &out = getInfoSink();
2352
2353 switch (node->getFlowOp())
2354 {
2355 case EOpKill:
2356 out << "discard";
2357 break;
2358 case EOpBreak:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002359 if (mNestedLoopDepth > 1)
2360 {
2361 mUsesNestedBreak = true;
2362 }
Nicolas Capens655fe362014-04-11 13:12:34 -04002363
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002364 if (mExcessiveLoopIndex)
2365 {
2366 out << "{Break";
2367 mExcessiveLoopIndex->traverse(this);
2368 out << " = true; break;}\n";
2369 }
2370 else
2371 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002372 out << "break";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002373 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002374 break;
2375 case EOpContinue:
2376 out << "continue";
2377 break;
2378 case EOpReturn:
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002379 if (node->getExpression())
2380 {
2381 out << "return ";
2382 }
2383 else
2384 {
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002385 out << "return";
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002386 }
Olli Etuaho8886f0f2017-10-10 11:59:45 +03002387 break;
2388 default:
2389 UNREACHABLE();
2390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002391 }
2392
2393 return true;
2394}
2395
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002396// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002397// (The D3D documentation says 255 iterations, but the compiler complains at anything more than
2398// 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002399bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002400{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002401 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002402
2403 // Parse loops of the form:
2404 // for(int index = initial; index [comparator] limit; index += increment)
Yunchao Hed7297bf2017-04-19 15:27:10 +08002405 TIntermSymbol *index = nullptr;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002406 TOperator comparator = EOpNull;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002407 int initial = 0;
2408 int limit = 0;
2409 int increment = 0;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002410
2411 // Parse index name and intial value
2412 if (node->getInit())
2413 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002414 TIntermDeclaration *init = node->getInit()->getAsDeclarationNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002415
2416 if (init)
2417 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002418 TIntermSequence *sequence = init->getSequence();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002419 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002420
2421 if (variable && variable->getQualifier() == EvqTemporary)
2422 {
2423 TIntermBinary *assign = variable->getAsBinaryNode();
2424
2425 if (assign->getOp() == EOpInitialize)
2426 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002427 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002428 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2429
2430 if (symbol && constant)
2431 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002432 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002433 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002434 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002435 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002436 }
2437 }
2438 }
2439 }
2440 }
2441 }
2442
2443 // Parse comparator and limit value
Yunchao He4f285442017-04-21 12:15:49 +08002444 if (index != nullptr && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002445 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002446 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002447
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002448 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
2449 {
2450 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
2451
2452 if (constant)
2453 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002454 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002455 {
2456 comparator = test->getOp();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002457 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002458 }
2459 }
2460 }
2461 }
2462
2463 // Parse increment
Yunchao He4f285442017-04-21 12:15:49 +08002464 if (index != nullptr && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002465 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002466 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002467 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04002468
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002469 if (binaryTerminal)
2470 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002471 TOperator op = binaryTerminal->getOp();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002472 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
2473
2474 if (constant)
2475 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002476 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002477 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002478 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002479
2480 switch (op)
2481 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002482 case EOpAddAssign:
2483 increment = value;
2484 break;
2485 case EOpSubAssign:
2486 increment = -value;
2487 break;
2488 default:
2489 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002490 }
2491 }
2492 }
2493 }
2494 else if (unaryTerminal)
2495 {
2496 TOperator op = unaryTerminal->getOp();
2497
2498 switch (op)
2499 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002500 case EOpPostIncrement:
2501 increment = 1;
2502 break;
2503 case EOpPostDecrement:
2504 increment = -1;
2505 break;
2506 case EOpPreIncrement:
2507 increment = 1;
2508 break;
2509 case EOpPreDecrement:
2510 increment = -1;
2511 break;
2512 default:
2513 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002514 }
2515 }
2516 }
2517
Yunchao He4f285442017-04-21 12:15:49 +08002518 if (index != nullptr && comparator != EOpNull && increment != 0)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002519 {
2520 if (comparator == EOpLessThanEqual)
2521 {
2522 comparator = EOpLessThan;
2523 limit += 1;
2524 }
2525
2526 if (comparator == EOpLessThan)
2527 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00002528 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002529
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002530 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002531 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002532 return false; // Not an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002533 }
2534
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002535 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002536 mExcessiveLoopIndex = index;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002537
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00002538 out << "{int ";
2539 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002540 out << ";\n"
2541 "bool Break";
2542 index->traverse(this);
2543 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002544
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002545 bool firstLoopFragment = true;
2546
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002547 while (iterations > 0)
2548 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002549 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002550
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002551 if (!firstLoopFragment)
2552 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002553 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002554 index->traverse(this);
2555 out << ") {\n";
2556 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002557
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002558 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002559 {
Yunchao Hed7297bf2017-04-19 15:27:10 +08002560 mExcessiveLoopIndex = nullptr; // Stops setting the Break flag
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00002561 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002562
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002563 // for(int index = initial; index < clampedLimit; index += increment)
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002564 const char *unroll =
2565 mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002566
Corentin Wallez1239ee92015-03-19 14:38:02 -07002567 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002568 index->traverse(this);
2569 out << " = ";
2570 out << initial;
2571
2572 out << "; ";
2573 index->traverse(this);
2574 out << " < ";
2575 out << clampedLimit;
2576
2577 out << "; ";
2578 index->traverse(this);
2579 out << " += ";
2580 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002581 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002582
Jamie Madill8c46ab12015-12-07 16:39:19 -05002583 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002584 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002585
2586 if (node->getBody())
2587 {
2588 node->getBody()->traverse(this);
2589 }
2590
Jamie Madill8c46ab12015-12-07 16:39:19 -05002591 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00002592 out << ";}\n";
2593
2594 if (!firstLoopFragment)
2595 {
2596 out << "}\n";
2597 }
2598
2599 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002600
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002601 initial += MAX_LOOP_ITERATIONS * increment;
2602 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002603 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002604
daniel@transgaming.comc264de42012-07-11 20:37:25 +00002605 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002606
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00002607 mExcessiveLoopIndex = restoreIndex;
2608
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002609 return true;
2610 }
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002611 else
2612 UNIMPLEMENTED();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002613 }
2614
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002615 return false; // Not handled as an excessive loop
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002616}
2617
Jamie Madill8c46ab12015-12-07 16:39:19 -05002618void OutputHLSL::outputTriplet(TInfoSinkBase &out,
2619 Visit visit,
2620 const char *preString,
2621 const char *inString,
2622 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002623{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002624 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002625 {
2626 out << preString;
2627 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002628 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002629 {
2630 out << inString;
2631 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00002632 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002633 {
2634 out << postString;
2635 }
2636}
2637
Jamie Madill8c46ab12015-12-07 16:39:19 -05002638void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002639{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002640 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002641 {
Jamie Madill32aab012015-01-27 14:12:26 -05002642 out << "\n";
2643 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002644
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002645 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002646 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002647 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002648 }
Jamie Madillf91ce812014-06-13 10:04:34 -04002649
Jamie Madill32aab012015-01-27 14:12:26 -05002650 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002651 }
2652}
2653
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002654TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
2655{
2656 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002657 const TType &type = symbol->getType();
2658 const TName &name = symbol->getName();
2659 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002660
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002661 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002662 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002663 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002664 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03002665 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002666 {
Olli Etuahoff526f12017-06-30 12:26:54 +03002667 nameStr = DecorateVariableIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00002668 }
2669
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002670 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002671 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002672 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2673 {
2674 // Samplers are passed as indices to the sampler array.
2675 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2676 return "const uint " + nameStr + ArrayString(type);
2677 }
2678 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2679 {
2680 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
2681 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
2682 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
2683 ArrayString(type);
2684 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002685 }
2686
Olli Etuaho96963162016-03-21 11:54:33 +02002687 TStringStream argString;
2688 argString << QualifierString(qualifier) << " " << TypeString(type) << " " << nameStr
2689 << ArrayString(type);
2690
2691 // If the structure parameter contains samplers, they need to be passed into the function as
2692 // separate parameters. HLSL doesn't natively support samplers in structs.
2693 if (type.isStructureContainingSamplers())
2694 {
2695 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
2696 TVector<TIntermSymbol *> samplerSymbols;
Olli Etuaho2d88e9b2017-07-21 16:52:03 +03002697 type.createSamplerSymbols("angle" + nameStr, "", &samplerSymbols, nullptr, mSymbolTable);
Olli Etuaho96963162016-03-21 11:54:33 +02002698 for (const TIntermSymbol *sampler : samplerSymbols)
2699 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002700 const TType &samplerType = sampler->getType();
Olli Etuaho96963162016-03-21 11:54:33 +02002701 if (mOutputType == SH_HLSL_4_1_OUTPUT)
2702 {
Olli Etuaho28839f02017-08-15 11:38:16 +03002703 argString << ", const uint " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002704 }
2705 else if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
2706 {
Olli Etuaho96963162016-03-21 11:54:33 +02002707 ASSERT(IsSampler(samplerType.getBasicType()));
2708 argString << ", " << QualifierString(qualifier) << " "
2709 << TextureString(samplerType.getBasicType()) << " texture_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002710 << sampler->getSymbol() << ArrayString(samplerType) << ", "
2711 << QualifierString(qualifier) << " "
Olli Etuaho96963162016-03-21 11:54:33 +02002712 << SamplerString(samplerType.getBasicType()) << " sampler_"
Olli Etuaho28839f02017-08-15 11:38:16 +03002713 << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002714 }
2715 else
2716 {
Olli Etuaho96963162016-03-21 11:54:33 +02002717 ASSERT(IsSampler(samplerType.getBasicType()));
2718 argString << ", " << QualifierString(qualifier) << " " << TypeString(samplerType)
Olli Etuaho28839f02017-08-15 11:38:16 +03002719 << " " << sampler->getSymbol() << ArrayString(samplerType);
Olli Etuaho96963162016-03-21 11:54:33 +02002720 }
2721 }
2722 }
2723
2724 return argString.str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002725}
2726
2727TString OutputHLSL::initializer(const TType &type)
2728{
2729 TString string;
2730
Jamie Madill94bf7f22013-07-08 13:31:15 -04002731 size_t size = type.getObjectSize();
2732 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002733 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002734 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002735
Jamie Madill94bf7f22013-07-08 13:31:15 -04002736 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737 {
2738 string += ", ";
2739 }
2740 }
2741
daniel@transgaming.comead23042010-04-29 03:35:36 +00002742 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00002744
Jamie Madill8c46ab12015-12-07 16:39:19 -05002745void OutputHLSL::outputConstructor(TInfoSinkBase &out,
2746 Visit visit,
2747 const TType &type,
2748 const char *name,
2749 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002750{
Olli Etuahof40319e2015-03-10 14:33:00 +02002751 if (type.isArray())
2752 {
2753 UNIMPLEMENTED();
2754 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002755
2756 if (visit == PreVisit)
2757 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002758 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002759
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002760 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04002761 }
2762 else if (visit == InVisit)
2763 {
2764 out << ", ";
2765 }
2766 else if (visit == PostVisit)
2767 {
2768 out << ")";
2769 }
2770}
2771
Jamie Madill8c46ab12015-12-07 16:39:19 -05002772const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
2773 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002774 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002775{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002776 const TConstantUnion *constUnionIterated = constUnion;
2777
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002778 const TStructure *structure = type.getStruct();
Jamie Madill98493dd2013-07-08 14:39:03 -04002779 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002780 {
Jamie Madill033dae62014-06-18 12:56:28 -04002781 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002782
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002783 const TFieldList &fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002784
Jamie Madill98493dd2013-07-08 14:39:03 -04002785 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002786 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002787 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05002788 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002789
Jamie Madill98493dd2013-07-08 14:39:03 -04002790 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002791 {
2792 out << ", ";
2793 }
2794 }
2795
2796 out << ")";
2797 }
2798 else
2799 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002800 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002801 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04002802
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002803 if (writeType)
2804 {
Jamie Madill033dae62014-06-18 12:56:28 -04002805 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002806 }
Olli Etuaho56a2f952016-12-08 12:16:27 +00002807 constUnionIterated = writeConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002808 if (writeType)
2809 {
2810 out << ")";
2811 }
2812 }
2813
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002814 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00002815}
2816
Olli Etuahod68924e2017-01-02 17:34:40 +00002817void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, TOperator op)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002818{
Olli Etuahod68924e2017-01-02 17:34:40 +00002819 if (visit == PreVisit)
2820 {
2821 const char *opStr = GetOperatorString(op);
2822 BuiltInFunctionEmulator::WriteEmulatedFunctionName(out, opStr);
2823 out << "(";
2824 }
2825 else
2826 {
2827 outputTriplet(out, visit, nullptr, ", ", ")");
2828 }
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02002829}
2830
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002831bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out,
2832 TIntermSymbol *symbolNode,
2833 TIntermTyped *expression)
Jamie Madill37997142015-01-28 10:06:34 -05002834{
2835 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
2836 expression->traverse(&searchSymbol);
2837
2838 if (searchSymbol.foundMatch())
2839 {
2840 // Type already printed
2841 out << "t" + str(mUniqueIndex) + " = ";
2842 expression->traverse(this);
2843 out << ", ";
2844 symbolNode->traverse(this);
2845 out << " = t" + str(mUniqueIndex);
2846
2847 mUniqueIndex++;
2848 return true;
2849 }
2850
2851 return false;
2852}
2853
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002854bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
2855{
2856 // We support writing constant unions and constructors that only take constant unions as
2857 // parameters as HLSL literals.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002858 return !expression->getType().isArrayOfArrays() &&
2859 (expression->getAsConstantUnion() ||
2860 expression->isConstructorWithOnlyConstantUnionParameters());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002861}
2862
2863bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
2864 TIntermSymbol *symbolNode,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002865 TIntermTyped *initializer)
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002866{
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002867 if (canWriteAsHLSLLiteral(initializer))
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002868 {
2869 symbolNode->traverse(this);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002870 ASSERT(!symbolNode->getType().isArrayOfArrays());
2871 if (symbolNode->getType().isArray())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002872 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002873 out << "[" << symbolNode->getType().getOutermostArraySize() << "]";
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002874 }
2875 out << " = {";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002876 if (initializer->getAsConstantUnion())
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002877 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002878 TIntermConstantUnion *nodeConst = initializer->getAsConstantUnion();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002879 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002880 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002881 }
2882 else
2883 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002884 TIntermAggregate *constructor = initializer->getAsAggregate();
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002885 ASSERT(constructor != nullptr);
2886 for (TIntermNode *&node : *constructor->getSequence())
2887 {
2888 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
2889 ASSERT(nodeConst);
2890 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
Olli Etuaho56a2f952016-12-08 12:16:27 +00002891 writeConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
Olli Etuaho18b9deb2015-11-05 12:14:50 +02002892 if (node != constructor->getSequence()->back())
2893 {
2894 out << ", ";
2895 }
2896 }
2897 }
2898 out << "}";
2899 return true;
2900 }
2901 return false;
2902}
2903
Jamie Madill55e79e02015-02-09 15:35:00 -05002904TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
2905{
2906 const TFieldList &fields = structure.fields();
2907
2908 for (const auto &eqFunction : mStructEqualityFunctions)
2909 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002910 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05002911 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002912 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002913 }
2914 }
2915
2916 const TString &structNameString = StructNameString(structure);
2917
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002918 StructEqualityFunction *function = new StructEqualityFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002919 function->structure = &structure;
2920 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05002921
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002922 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05002923
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002924 fnOut << "bool " << function->functionName << "(" << structNameString << " a, "
2925 << structNameString + " b)\n"
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002926 << "{\n"
2927 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002928
2929 for (size_t i = 0; i < fields.size(); i++)
2930 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002931 const TField *field = fields[i];
Jamie Madill55e79e02015-02-09 15:35:00 -05002932 const TType *fieldType = field->type();
2933
2934 const TString &fieldNameA = "a." + Decorate(field->name());
2935 const TString &fieldNameB = "b." + Decorate(field->name());
2936
2937 if (i > 0)
2938 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002939 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05002940 }
2941
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002942 fnOut << "(";
2943 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
2944 fnOut << fieldNameA;
2945 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
2946 fnOut << fieldNameB;
2947 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
2948 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05002949 }
2950
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002951 fnOut << ";\n"
2952 << "}\n";
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002953
2954 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05002955
2956 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002957 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05002958
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002959 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05002960}
2961
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002962TString OutputHLSL::addArrayEqualityFunction(const TType &type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002963{
2964 for (const auto &eqFunction : mArrayEqualityFunctions)
2965 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002966 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02002967 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02002968 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002969 }
2970 }
2971
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002972 TType elementType(type);
2973 elementType.toArrayElementType();
Olli Etuaho7fb49552015-03-18 17:27:44 +02002974
Olli Etuaho12690762015-03-31 12:55:28 +03002975 ArrayHelperFunction *function = new ArrayHelperFunction();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002976 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02002977
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002978 function->functionName = ArrayHelperFunctionName("angle_eq", type);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002979
2980 TInfoSinkBase fnOut;
2981
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002982 const TString &typeName = TypeString(type);
2983 fnOut << "bool " << function->functionName << "(" << typeName << " a" << ArrayString(type)
2984 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02002985 << "{\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002986 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002987 << type.getOutermostArraySize()
2988 << "; ++i)\n"
2989 " {\n"
2990 " if (";
Olli Etuaho7fb49552015-03-18 17:27:44 +02002991
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002992 outputEqual(PreVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002993 fnOut << "a[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002994 outputEqual(InVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002995 fnOut << "b[i]";
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002996 outputEqual(PostVisit, elementType, EOpNotEqual, fnOut);
Olli Etuaho7fb49552015-03-18 17:27:44 +02002997
2998 fnOut << ") { return false; }\n"
2999 " }\n"
3000 " return true;\n"
3001 "}\n";
3002
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003003 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003004
3005 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003006 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003007
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003008 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003009}
3010
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003011TString OutputHLSL::addArrayAssignmentFunction(const TType &type)
Olli Etuaho12690762015-03-31 12:55:28 +03003012{
3013 for (const auto &assignFunction : mArrayAssignmentFunctions)
3014 {
3015 if (assignFunction.type == type)
3016 {
3017 return assignFunction.functionName;
3018 }
3019 }
3020
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003021 TType elementType(type);
3022 elementType.toArrayElementType();
Olli Etuaho12690762015-03-31 12:55:28 +03003023
3024 ArrayHelperFunction function;
3025 function.type = type;
3026
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003027 function.functionName = ArrayHelperFunctionName("angle_assign", type);
Olli Etuaho12690762015-03-31 12:55:28 +03003028
3029 TInfoSinkBase fnOut;
3030
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003031 const TString &typeName = TypeString(type);
3032 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type)
3033 << ", " << typeName << " b" << ArrayString(type) << ")\n"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003034 << "{\n"
3035 " for (int i = 0; i < "
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003036 << type.getOutermostArraySize()
3037 << "; ++i)\n"
3038 " {\n"
3039 " ";
3040
3041 outputAssign(PreVisit, elementType, fnOut);
3042 fnOut << "a[i]";
3043 outputAssign(InVisit, elementType, fnOut);
3044 fnOut << "b[i]";
3045 outputAssign(PostVisit, elementType, fnOut);
3046
3047 fnOut << ";\n"
3048 " }\n"
3049 "}\n";
Olli Etuaho12690762015-03-31 12:55:28 +03003050
3051 function.functionDefinition = fnOut.c_str();
3052
3053 mArrayAssignmentFunctions.push_back(function);
3054
3055 return function.functionName;
3056}
3057
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003058TString OutputHLSL::addArrayConstructIntoFunction(const TType &type)
Olli Etuaho9638c352015-04-01 14:34:52 +03003059{
3060 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3061 {
3062 if (constructIntoFunction.type == type)
3063 {
3064 return constructIntoFunction.functionName;
3065 }
3066 }
3067
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003068 TType elementType(type);
3069 elementType.toArrayElementType();
Olli Etuaho9638c352015-04-01 14:34:52 +03003070
3071 ArrayHelperFunction function;
3072 function.type = type;
3073
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003074 function.functionName = ArrayHelperFunctionName("angle_construct_into", type);
Olli Etuaho9638c352015-04-01 14:34:52 +03003075
3076 TInfoSinkBase fnOut;
3077
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003078 const TString &typeName = TypeString(type);
3079 fnOut << "void " << function.functionName << "(out " << typeName << " a" << ArrayString(type);
3080 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003081 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003082 fnOut << ", " << typeName << " b" << i << ArrayString(elementType);
Olli Etuaho9638c352015-04-01 14:34:52 +03003083 }
3084 fnOut << ")\n"
3085 "{\n";
3086
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003087 for (unsigned int i = 0u; i < type.getOutermostArraySize(); ++i)
Olli Etuaho9638c352015-04-01 14:34:52 +03003088 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003089 fnOut << " ";
3090 outputAssign(PreVisit, elementType, fnOut);
3091 fnOut << "a[" << i << "]";
3092 outputAssign(InVisit, elementType, fnOut);
3093 fnOut << "b" << i;
3094 outputAssign(PostVisit, elementType, fnOut);
3095 fnOut << ";\n";
Olli Etuaho9638c352015-04-01 14:34:52 +03003096 }
3097 fnOut << "}\n";
3098
3099 function.functionDefinition = fnOut.c_str();
3100
3101 mArrayConstructIntoFunctions.push_back(function);
3102
3103 return function.functionName;
3104}
3105
Jamie Madill2e295e22015-04-29 10:41:33 -04003106void OutputHLSL::ensureStructDefined(const TType &type)
3107{
3108 TStructure *structure = type.getStruct();
3109
3110 if (structure)
3111 {
3112 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3113 }
3114}
3115
Jamie Madill45bcc782016-11-07 13:58:48 -05003116} // namespace sh