blob: b148dba91761724fac0b127a39bd71116aa86fb8 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
24#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/UniformHLSL.h"
26#include "compiler/translator/UtilsHLSL.h"
27#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050028#include "compiler/translator/util.h"
29
Olli Etuaho4785fec2015-05-18 16:09:37 +030030namespace
31{
32
33bool IsSequence(TIntermNode *node)
34{
35 return node->getAsAggregate() != nullptr && node->getAsAggregate()->getOp() == EOpSequence;
36}
37
Olli Etuaho18b9deb2015-11-05 12:14:50 +020038void WriteSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
39{
40 ASSERT(constUnion != nullptr);
41 switch (constUnion->getType())
42 {
43 case EbtFloat:
44 out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst()));
45 break;
46 case EbtInt:
47 out << constUnion->getIConst();
48 break;
49 case EbtUInt:
50 out << constUnion->getUConst();
51 break;
52 case EbtBool:
53 out << constUnion->getBConst();
54 break;
55 default:
56 UNREACHABLE();
57 }
58}
59
60const TConstantUnion *WriteConstantUnionArray(TInfoSinkBase &out,
61 const TConstantUnion *const constUnion,
62 const size_t size)
63{
64 const TConstantUnion *constUnionIterated = constUnion;
65 for (size_t i = 0; i < size; i++, constUnionIterated++)
66 {
67 WriteSingleConstant(out, constUnionIterated);
68
69 if (i != size - 1)
70 {
71 out << ", ";
72 }
73 }
74 return constUnionIterated;
75}
76
Olli Etuaho4785fec2015-05-18 16:09:37 +030077} // namespace
78
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079namespace sh
80{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000081
Nicolas Capense0ba27a2013-06-24 16:10:52 -040082TString OutputHLSL::TextureFunction::name() const
83{
84 TString name = "gl_texture";
85
Olli Etuaho9b4e8622015-12-22 15:53:22 +020086 // We need to include full the sampler type in the function name to make the signature unique
87 // on D3D11, where samplers are passed to texture functions as indices.
88 name += TextureTypeSuffix(this->sampler);
Nicolas Capense0ba27a2013-06-24 16:10:52 -040089
90 if (proj)
91 {
92 name += "Proj";
93 }
94
Nicolas Capensb1f45b72013-12-19 17:37:19 -050095 if (offset)
96 {
97 name += "Offset";
98 }
99
Nicolas Capens75fb4752013-07-10 15:14:47 -0400100 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400101 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500102 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400103 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500104 case LOD: name += "Lod"; break;
105 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400106 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500107 case SIZE: name += "Size"; break;
108 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500109 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400110 default: UNREACHABLE();
111 }
112
113 return name + "(";
114}
115
116bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
117{
118 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400119 if (sampler > rhs.sampler) return false;
120
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400121 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400122 if (coords > rhs.coords) return false;
123
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400124 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400125 if (proj && !rhs.proj) return false;
126
127 if (!offset && rhs.offset) return true;
128 if (offset && !rhs.offset) return false;
129
Nicolas Capens75fb4752013-07-10 15:14:47 -0400130 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400131 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400132
133 return false;
134}
135
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200136OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
137 const TExtensionBehavior &extensionBehavior,
138 const char *sourcePath, ShShaderOutput outputType,
139 int numRenderTargets, const std::vector<Uniform> &uniforms,
140 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400141 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200142 mShaderType(shaderType),
143 mShaderVersion(shaderVersion),
144 mExtensionBehavior(extensionBehavior),
145 mSourcePath(sourcePath),
146 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700147 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000148 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700149 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000151 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000152
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000153 mUsesFragColor = false;
154 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000155 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000156 mUsesFragCoord = false;
157 mUsesPointCoord = false;
158 mUsesFrontFacing = false;
159 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000160 mUsesInstanceID = false;
Corentin Wallezb076add2016-01-11 16:45:46 -0500161 mUsesVertexID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400162 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000163 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500164 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400165 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530166 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000167
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000168 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000169
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000170 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000171 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400172 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000173
174 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000175
Jamie Madill8daaba12014-06-13 10:04:33 -0400176 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200177 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400178
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200179 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000180 {
Arun Patole63419392015-03-13 11:51:07 +0530181 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
182 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
183 // In both cases total 3 uniform registers need to be reserved.
184 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000185 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000186
Geoff Lang00140f42016-02-03 18:47:33 +0000187 // Reserve registers for the default uniform block and driver constants
188 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189}
190
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000191OutputHLSL::~OutputHLSL()
192{
Jamie Madill8daaba12014-06-13 10:04:33 -0400193 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400194 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200195 for (auto &eqFunction : mStructEqualityFunctions)
196 {
197 SafeDelete(eqFunction);
198 }
199 for (auto &eqFunction : mArrayEqualityFunctions)
200 {
201 SafeDelete(eqFunction);
202 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000203}
204
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200205void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000206{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200207 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400208 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000209
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200210 BuiltInFunctionEmulator builtInFunctionEmulator;
211 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200212 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500213
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700214 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700215 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
216 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300217 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700218 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700219
Jamie Madill37997142015-01-28 10:06:34 -0500220 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500221 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200222 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500223 mInfoSinkStack.pop();
224
Jamie Madill37997142015-01-28 10:06:34 -0500225 mInfoSinkStack.push(&mFooter);
226 if (!mDeferredGlobalInitializers.empty())
227 {
228 writeDeferredGlobalInitializers(mFooter);
229 }
230 mInfoSinkStack.pop();
231
Jamie Madill32aab012015-01-27 14:12:26 -0500232 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500233 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500234 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000235
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200236 objSink << mHeader.c_str();
237 objSink << mBody.c_str();
238 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200239
240 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000241}
242
Jamie Madill570e04d2013-06-21 09:15:33 -0400243void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
244{
245 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
246 {
247 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
248
Jamie Madill32aab012015-01-27 14:12:26 -0500249 TInfoSinkBase structInfoSink;
250 mInfoSinkStack.push(&structInfoSink);
251
Jamie Madill570e04d2013-06-21 09:15:33 -0400252 // This will mark the necessary block elements as referenced
253 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500254
255 TString structName(structInfoSink.c_str());
256 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400257
258 mFlaggedStructOriginalNames[flaggedNode] = structName;
259
260 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
261 {
262 structName.erase(pos, 1);
263 }
264
265 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
266 }
267}
268
Jamie Madill4e1fd412014-07-10 17:50:10 -0400269const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
270{
271 return mUniformHLSL->getInterfaceBlockRegisterMap();
272}
273
Jamie Madill9fe25e92014-07-18 10:33:08 -0400274const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
275{
276 return mUniformHLSL->getUniformRegisterMap();
277}
278
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000279int OutputHLSL::vectorSize(const TType &type) const
280{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000281 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000282 int arraySize = type.isArray() ? type.getArraySize() : 1;
283
284 return elementSize * arraySize;
285}
286
Jamie Madill98493dd2013-07-08 14:39:03 -0400287TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400288{
289 TString init;
290
291 TString preIndentString;
292 TString fullIndentString;
293
294 for (int spaces = 0; spaces < (indent * 4); spaces++)
295 {
296 preIndentString += ' ';
297 }
298
299 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
300 {
301 fullIndentString += ' ';
302 }
303
304 init += preIndentString + "{\n";
305
Jamie Madill98493dd2013-07-08 14:39:03 -0400306 const TFieldList &fields = structure.fields();
307 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400308 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400309 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400310 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400311 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400312
Jamie Madill98493dd2013-07-08 14:39:03 -0400313 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400314 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400315 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400316 }
317 else
318 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400319 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400320 }
321 }
322
323 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
324
325 return init;
326}
327
Jamie Madill8c46ab12015-12-07 16:39:19 -0500328void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000330 TString varyings;
331 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400332 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000333
Jamie Madill829f59e2013-11-13 19:40:54 -0500334 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400335 {
336 TIntermTyped *structNode = flaggedStructIt->first;
337 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400338 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400339 const TString &originalName = mFlaggedStructOriginalNames[structNode];
340
Jamie Madill033dae62014-06-18 12:56:28 -0400341 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400342 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400343 flaggedStructs += "\n";
344 }
345
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000346 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
347 {
348 const TType &type = varying->second->getType();
349 const TString &name = varying->second->getSymbol();
350
351 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400352 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
353 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000354 }
355
356 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
357 {
358 const TType &type = attribute->second->getType();
359 const TString &name = attribute->second->getSymbol();
360
Jamie Madill033dae62014-06-18 12:56:28 -0400361 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000362 }
363
Jamie Madill8daaba12014-06-13 10:04:33 -0400364 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400365
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200366 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400367 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
368
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200369 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500370 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200371 out << "\n// Equality functions\n\n";
372 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500373 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200374 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200375 }
376 }
Olli Etuaho12690762015-03-31 12:55:28 +0300377 if (!mArrayAssignmentFunctions.empty())
378 {
379 out << "\n// Assignment functions\n\n";
380 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
381 {
382 out << assignmentFunction.functionDefinition << "\n";
383 }
384 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300385 if (!mArrayConstructIntoFunctions.empty())
386 {
387 out << "\n// Array constructor functions\n\n";
388 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
389 {
390 out << constructIntoFunction.functionDefinition << "\n";
391 }
392 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200393
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500394 if (mUsesDiscardRewriting)
395 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400396 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500397 }
398
Nicolas Capens655fe362014-04-11 13:12:34 -0400399 if (mUsesNestedBreak)
400 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400401 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400402 }
403
Arun Patole44efa0b2015-03-04 17:11:05 +0530404 if (mRequiresIEEEStrictCompiling)
405 {
406 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
407 }
408
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400409 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
410 "#define LOOP [loop]\n"
411 "#define FLATTEN [flatten]\n"
412 "#else\n"
413 "#define LOOP\n"
414 "#define FLATTEN\n"
415 "#endif\n";
416
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200417 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200419 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
420 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000421
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000422 out << "// Varyings\n";
423 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400424 out << "\n";
425
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200426 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000427 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500428 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000429 {
Jamie Madill46131a32013-06-20 11:55:50 -0400430 const TString &variableName = outputVariableIt->first;
431 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400432
Jamie Madill033dae62014-06-18 12:56:28 -0400433 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400434 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000435 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000436 }
Jamie Madill46131a32013-06-20 11:55:50 -0400437 else
438 {
439 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
440
441 out << "static float4 gl_Color[" << numColorValues << "] =\n"
442 "{\n";
443 for (unsigned int i = 0; i < numColorValues; i++)
444 {
445 out << " float4(0, 0, 0, 0)";
446 if (i + 1 != numColorValues)
447 {
448 out << ",";
449 }
450 out << "\n";
451 }
452
453 out << "};\n";
454 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000455
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400456 if (mUsesFragDepth)
457 {
458 out << "static float gl_Depth = 0.0;\n";
459 }
460
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000461 if (mUsesFragCoord)
462 {
463 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
464 }
465
466 if (mUsesPointCoord)
467 {
468 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
469 }
470
471 if (mUsesFrontFacing)
472 {
473 out << "static bool gl_FrontFacing = false;\n";
474 }
475
476 out << "\n";
477
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000478 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000479 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000480 out << "struct gl_DepthRangeParameters\n"
481 "{\n"
482 " float near;\n"
483 " float far;\n"
484 " float diff;\n"
485 "};\n"
486 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000487 }
488
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200489 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000490 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000491 out << "cbuffer DriverConstants : register(b1)\n"
492 "{\n";
493
494 if (mUsesDepthRange)
495 {
496 out << " float3 dx_DepthRange : packoffset(c0);\n";
497 }
498
499 if (mUsesFragCoord)
500 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000501 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000502 }
503
504 if (mUsesFragCoord || mUsesFrontFacing)
505 {
506 out << " float3 dx_DepthFront : packoffset(c2);\n";
507 }
508
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800509 if (mUsesFragCoord)
510 {
511 // dx_ViewScale is only used in the fragment shader to correct
512 // the value for glFragCoord if necessary
513 out << " float2 dx_ViewScale : packoffset(c3);\n";
514 }
515
Olli Etuaho618bebc2016-01-15 16:40:00 +0200516 if (mOutputType == SH_HLSL_4_1_OUTPUT)
517 {
518 mUniformHLSL->samplerMetadataUniforms(out, "c4");
519 }
520
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000521 out << "};\n";
522 }
523 else
524 {
525 if (mUsesDepthRange)
526 {
527 out << "uniform float3 dx_DepthRange : register(c0);";
528 }
529
530 if (mUsesFragCoord)
531 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000532 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000533 }
534
535 if (mUsesFragCoord || mUsesFrontFacing)
536 {
537 out << "uniform float3 dx_DepthFront : register(c2);\n";
538 }
539 }
540
541 out << "\n";
542
543 if (mUsesDepthRange)
544 {
545 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
546 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000547 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000548
Jamie Madillf91ce812014-06-13 10:04:34 -0400549 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000550 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400551 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000552 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400553 out << flaggedStructs;
554 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000555 }
556
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000557 if (usingMRTExtension && mNumRenderTargets > 1)
558 {
559 out << "#define GL_USES_MRT\n";
560 }
561
562 if (mUsesFragColor)
563 {
564 out << "#define GL_USES_FRAG_COLOR\n";
565 }
566
567 if (mUsesFragData)
568 {
569 out << "#define GL_USES_FRAG_DATA\n";
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000572 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000573 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000574 out << "// Attributes\n";
575 out << attributes;
576 out << "\n"
577 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400578
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000579 if (mUsesPointSize)
580 {
581 out << "static float gl_PointSize = float(1);\n";
582 }
583
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000584 if (mUsesInstanceID)
585 {
586 out << "static int gl_InstanceID;";
587 }
588
Corentin Wallezb076add2016-01-11 16:45:46 -0500589 if (mUsesVertexID)
590 {
591 out << "static int gl_VertexID;";
592 }
593
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000594 out << "\n"
595 "// Varyings\n";
596 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000597 out << "\n";
598
599 if (mUsesDepthRange)
600 {
601 out << "struct gl_DepthRangeParameters\n"
602 "{\n"
603 " float near;\n"
604 " float far;\n"
605 " float diff;\n"
606 "};\n"
607 "\n";
608 }
609
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200610 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000611 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800612 out << "cbuffer DriverConstants : register(b1)\n"
613 "{\n";
614
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000615 if (mUsesDepthRange)
616 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800617 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000618 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800619
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800620 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
621 // shaders. However, we declare it for all shaders (including Feature Level 10+).
622 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
623 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800624 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800625 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800626 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800627
Olli Etuaho618bebc2016-01-15 16:40:00 +0200628 if (mOutputType == SH_HLSL_4_1_OUTPUT)
629 {
630 mUniformHLSL->samplerMetadataUniforms(out, "c4");
631 }
632
Austin Kinross4fd18b12014-12-22 12:32:05 -0800633 out << "};\n"
634 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000635 }
636 else
637 {
638 if (mUsesDepthRange)
639 {
640 out << "uniform float3 dx_DepthRange : register(c0);\n";
641 }
642
Cooper Partine6664f02015-01-09 16:22:24 -0800643 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
644 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000645 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000646 }
647
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000648 if (mUsesDepthRange)
649 {
650 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
651 "\n";
652 }
653
Jamie Madillf91ce812014-06-13 10:04:34 -0400654 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000655 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400656 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000657 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400658 out << flaggedStructs;
659 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000660 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400661 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000662
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400663 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
664 {
665 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400666 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000667 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400668 switch(textureFunction->sampler)
669 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400670 case EbtSampler2D: out << "int2 "; break;
671 case EbtSampler3D: out << "int3 "; break;
672 case EbtSamplerCube: out << "int2 "; break;
673 case EbtSampler2DArray: out << "int3 "; break;
674 case EbtISampler2D: out << "int2 "; break;
675 case EbtISampler3D: out << "int3 "; break;
676 case EbtISamplerCube: out << "int2 "; break;
677 case EbtISampler2DArray: out << "int3 "; break;
678 case EbtUSampler2D: out << "int2 "; break;
679 case EbtUSampler3D: out << "int3 "; break;
680 case EbtUSamplerCube: out << "int2 "; break;
681 case EbtUSampler2DArray: out << "int3 "; break;
682 case EbtSampler2DShadow: out << "int2 "; break;
683 case EbtSamplerCubeShadow: out << "int2 "; break;
684 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400685 default: UNREACHABLE();
686 }
687 }
688 else // Sampling function
689 {
690 switch(textureFunction->sampler)
691 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400692 case EbtSampler2D: out << "float4 "; break;
693 case EbtSampler3D: out << "float4 "; break;
694 case EbtSamplerCube: out << "float4 "; break;
695 case EbtSampler2DArray: out << "float4 "; break;
696 case EbtISampler2D: out << "int4 "; break;
697 case EbtISampler3D: out << "int4 "; break;
698 case EbtISamplerCube: out << "int4 "; break;
699 case EbtISampler2DArray: out << "int4 "; break;
700 case EbtUSampler2D: out << "uint4 "; break;
701 case EbtUSampler3D: out << "uint4 "; break;
702 case EbtUSamplerCube: out << "uint4 "; break;
703 case EbtUSampler2DArray: out << "uint4 "; break;
704 case EbtSampler2DShadow: out << "float "; break;
705 case EbtSamplerCubeShadow: out << "float "; break;
706 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400707 default: UNREACHABLE();
708 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000709 }
710
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400711 // Function name
712 out << textureFunction->name();
713
714 // Argument list
715 int hlslCoords = 4;
716
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200717 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000718 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400719 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000720 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400721 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
722 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
723 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000724 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400725
Nicolas Capens75fb4752013-07-10 15:14:47 -0400726 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000727 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400728 case TextureFunction::IMPLICIT: break;
729 case TextureFunction::BIAS: hlslCoords = 4; break;
730 case TextureFunction::LOD: hlslCoords = 4; break;
731 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400732 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400733 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000734 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400735 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200736 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400737 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200738 hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler);
739 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400740 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200741 out << TextureString(textureFunction->sampler) << " x, "
742 << SamplerString(textureFunction->sampler) << " s";
743 }
744 else
745 {
746 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
747 out << "const uint samplerIndex";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400748 }
749 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400750
Nicolas Capensfc014542014-02-18 14:47:13 -0500751 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400752 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500753 switch(textureFunction->coords)
754 {
755 case 2: out << ", int2 t"; break;
756 case 3: out << ", int3 t"; break;
757 default: UNREACHABLE();
758 }
759 }
760 else // Floating-point coordinates (except textureSize)
761 {
762 switch(textureFunction->coords)
763 {
764 case 1: out << ", int lod"; break; // textureSize()
765 case 2: out << ", float2 t"; break;
766 case 3: out << ", float3 t"; break;
767 case 4: out << ", float4 t"; break;
768 default: UNREACHABLE();
769 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000770 }
771
Nicolas Capensd11d5492014-02-19 17:06:10 -0500772 if (textureFunction->method == TextureFunction::GRAD)
773 {
774 switch(textureFunction->sampler)
775 {
776 case EbtSampler2D:
777 case EbtISampler2D:
778 case EbtUSampler2D:
779 case EbtSampler2DArray:
780 case EbtISampler2DArray:
781 case EbtUSampler2DArray:
782 case EbtSampler2DShadow:
783 case EbtSampler2DArrayShadow:
784 out << ", float2 ddx, float2 ddy";
785 break;
786 case EbtSampler3D:
787 case EbtISampler3D:
788 case EbtUSampler3D:
789 case EbtSamplerCube:
790 case EbtISamplerCube:
791 case EbtUSamplerCube:
792 case EbtSamplerCubeShadow:
793 out << ", float3 ddx, float3 ddy";
794 break;
795 default: UNREACHABLE();
796 }
797 }
798
Nicolas Capens75fb4752013-07-10 15:14:47 -0400799 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000800 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400801 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400802 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400803 case TextureFunction::LOD: out << ", float lod"; break;
804 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400805 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400806 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500807 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500808 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400809 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000810 }
811
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500812 if (textureFunction->offset)
813 {
814 switch(textureFunction->sampler)
815 {
816 case EbtSampler2D: out << ", int2 offset"; break;
817 case EbtSampler3D: out << ", int3 offset"; break;
818 case EbtSampler2DArray: out << ", int2 offset"; break;
819 case EbtISampler2D: out << ", int2 offset"; break;
820 case EbtISampler3D: out << ", int3 offset"; break;
821 case EbtISampler2DArray: out << ", int2 offset"; break;
822 case EbtUSampler2D: out << ", int2 offset"; break;
823 case EbtUSampler3D: out << ", int3 offset"; break;
824 case EbtUSampler2DArray: out << ", int2 offset"; break;
825 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500826 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500827 default: UNREACHABLE();
828 }
829 }
830
Nicolas Capens84cfa122014-04-14 13:48:45 -0400831 if (textureFunction->method == TextureFunction::BIAS ||
832 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500833 {
834 out << ", float bias";
835 }
836
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400837 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400838 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400839
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200840 // In some cases we use a variable to store the texture/sampler objects, but to work around
841 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
842 // sampling we need to call the function directly on a reference to the array. The bug was
843 // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests.
844 TString textureReference("x");
845 TString samplerReference("s");
846 if (mOutputType == SH_HLSL_4_1_OUTPUT)
847 {
848 TString suffix = TextureGroupSuffix(textureFunction->sampler);
849 if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D)
850 {
851 textureReference = TString("textures") + suffix + "[samplerIndex]";
852 samplerReference = TString("samplers") + suffix + "[samplerIndex]";
853 }
854 else
855 {
856 out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix
857 << ";\n";
858 textureReference = TString("textures") + suffix + "[textureIndex]";
859 out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset"
860 << suffix << ";\n";
861 samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]";
862 }
863 }
864
Nicolas Capens75fb4752013-07-10 15:14:47 -0400865 if (textureFunction->method == TextureFunction::SIZE)
866 {
Olli Etuahof8bf5832016-02-11 16:21:49 +0200867 out << "int baseLevel = samplerMetadata[samplerIndex].x;\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400868 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
869 {
Olli Etuahobce743a2016-01-15 17:18:28 +0200870 if (IsSamplerArray(textureFunction->sampler) ||
871 (IsIntegerSampler(textureFunction->sampler) &&
872 IsSamplerCube(textureFunction->sampler)))
Nicolas Capens75fb4752013-07-10 15:14:47 -0400873 {
874 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
Olli Etuahobce743a2016-01-15 17:18:28 +0200875 << " " << textureReference << ".GetDimensions(baseLevel + lod, width, "
876 "height, layers, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400877 }
878 else
879 {
880 out << " uint width; uint height; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200881 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200882 << ".GetDimensions(baseLevel + lod, width, height, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400883 }
884 }
885 else if (IsSampler3D(textureFunction->sampler))
886 {
887 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200888 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200889 << ".GetDimensions(baseLevel + lod, width, height, depth, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400890 }
891 else UNREACHABLE();
892
893 switch(textureFunction->sampler)
894 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400895 case EbtSampler2D: out << " return int2(width, height);"; break;
896 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
897 case EbtSamplerCube: out << " return int2(width, height);"; break;
898 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
899 case EbtISampler2D: out << " return int2(width, height);"; break;
900 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
901 case EbtISamplerCube: out << " return int2(width, height);"; break;
902 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
903 case EbtUSampler2D: out << " return int2(width, height);"; break;
904 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
905 case EbtUSamplerCube: out << " return int2(width, height);"; break;
906 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
907 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
908 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
909 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400910 default: UNREACHABLE();
911 }
912 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400913 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400914 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500915 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
916 {
917 out << " float width; float height; float layers; float levels;\n";
918
919 out << " uint mip = 0;\n";
920
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200921 out << " " << textureReference
922 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500923
924 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
925 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
926 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
927 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
928
929 // FACE_POSITIVE_X = 000b
930 // FACE_NEGATIVE_X = 001b
931 // FACE_POSITIVE_Y = 010b
932 // FACE_NEGATIVE_Y = 011b
933 // FACE_POSITIVE_Z = 100b
934 // FACE_NEGATIVE_Z = 101b
935 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
936
937 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
938 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
939 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
940
941 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
942 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500943
944 // Mip level computation.
945 if (textureFunction->method == TextureFunction::IMPLICIT)
946 {
947 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
948 " float2 dx = ddx(tSized);\n"
949 " float2 dy = ddy(tSized);\n"
950 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"
951 " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200952 << " " << textureReference
953 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500954 }
Nicolas Capens0027fa92014-02-20 14:26:42 -0500955 }
956 else if (IsIntegerSampler(textureFunction->sampler) &&
957 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400958 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400959 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400960 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400961 if (IsSamplerArray(textureFunction->sampler))
962 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400963 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400964
Nicolas Capens9edebd62013-08-06 10:59:10 -0400965 if (textureFunction->method == TextureFunction::LOD0)
966 {
967 out << " uint mip = 0;\n";
968 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400969 else if (textureFunction->method == TextureFunction::LOD0BIAS)
970 {
971 out << " uint mip = bias;\n";
972 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400973 else
974 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200975
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200976 out << " " << textureReference
977 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400978 if (textureFunction->method == TextureFunction::IMPLICIT ||
979 textureFunction->method == TextureFunction::BIAS)
980 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200981 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400982 " float dx = length(ddx(tSized));\n"
983 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500984 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400985
986 if (textureFunction->method == TextureFunction::BIAS)
987 {
988 out << " lod += bias;\n";
989 }
990 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500991 else if (textureFunction->method == TextureFunction::GRAD)
992 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200993 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500994 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400995
996 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
997 }
998
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200999 out << " " << textureReference
1000 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001001 }
1002 else
1003 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001004 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001005
Nicolas Capens9edebd62013-08-06 10:59:10 -04001006 if (textureFunction->method == TextureFunction::LOD0)
1007 {
1008 out << " uint mip = 0;\n";
1009 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001010 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1011 {
1012 out << " uint mip = bias;\n";
1013 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001014 else
1015 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001016 out << " " << textureReference
1017 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001018
Nicolas Capens9edebd62013-08-06 10:59:10 -04001019 if (textureFunction->method == TextureFunction::IMPLICIT ||
1020 textureFunction->method == TextureFunction::BIAS)
1021 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001022 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001023 " float dx = length(ddx(tSized));\n"
1024 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001025 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001026
1027 if (textureFunction->method == TextureFunction::BIAS)
1028 {
1029 out << " lod += bias;\n";
1030 }
1031 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001032 else if (textureFunction->method == TextureFunction::GRAD)
1033 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001034 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001035 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001036
1037 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1038 }
1039
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001040 out << " " << textureReference
1041 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001042 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001043 }
1044 else if (IsSampler3D(textureFunction->sampler))
1045 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001046 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001047
Nicolas Capens9edebd62013-08-06 10:59:10 -04001048 if (textureFunction->method == TextureFunction::LOD0)
1049 {
1050 out << " uint mip = 0;\n";
1051 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001052 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1053 {
1054 out << " uint mip = bias;\n";
1055 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001056 else
1057 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001058 out << " " << textureReference
1059 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001060
Nicolas Capens9edebd62013-08-06 10:59:10 -04001061 if (textureFunction->method == TextureFunction::IMPLICIT ||
1062 textureFunction->method == TextureFunction::BIAS)
1063 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001064 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1065 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001066 " float dx = length(ddx(tSized));\n"
1067 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001068 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001069
1070 if (textureFunction->method == TextureFunction::BIAS)
1071 {
1072 out << " lod += bias;\n";
1073 }
1074 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001075 else if (textureFunction->method == TextureFunction::GRAD)
1076 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001077 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001078 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001079
1080 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1081 }
1082
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001083 out << " " << textureReference
1084 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001085 }
1086 else UNREACHABLE();
1087 }
1088
1089 out << " return ";
1090
1091 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001092 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001093 {
1094 switch(textureFunction->sampler)
1095 {
1096 case EbtSampler2D: out << "tex2D"; break;
1097 case EbtSamplerCube: out << "texCUBE"; break;
1098 default: UNREACHABLE();
1099 }
1100
Nicolas Capens75fb4752013-07-10 15:14:47 -04001101 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001102 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001103 case TextureFunction::IMPLICIT:
1104 out << "(" << samplerReference << ", ";
1105 break;
1106 case TextureFunction::BIAS:
1107 out << "bias(" << samplerReference << ", ";
1108 break;
1109 case TextureFunction::LOD:
1110 out << "lod(" << samplerReference << ", ";
1111 break;
1112 case TextureFunction::LOD0:
1113 out << "lod(" << samplerReference << ", ";
1114 break;
1115 case TextureFunction::LOD0BIAS:
1116 out << "lod(" << samplerReference << ", ";
1117 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001118 default: UNREACHABLE();
1119 }
1120 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001121 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001122 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001123 if (textureFunction->method == TextureFunction::GRAD)
1124 {
1125 if (IsIntegerSampler(textureFunction->sampler))
1126 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001127 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001128 }
1129 else if (IsShadowSampler(textureFunction->sampler))
1130 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001131 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1132 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001133 }
1134 else
1135 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001136 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001137 }
1138 }
1139 else if (IsIntegerSampler(textureFunction->sampler) ||
1140 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001141 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001142 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001143 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001144 else if (IsShadowSampler(textureFunction->sampler))
1145 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001146 switch(textureFunction->method)
1147 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001148 case TextureFunction::IMPLICIT:
1149 out << "" << textureReference << ".SampleCmp(" << samplerReference
1150 << ", ";
1151 break;
1152 case TextureFunction::BIAS:
1153 out << "" << textureReference << ".SampleCmp(" << samplerReference
1154 << ", ";
1155 break;
1156 case TextureFunction::LOD:
1157 out << "" << textureReference << ".SampleCmp(" << samplerReference
1158 << ", ";
1159 break;
1160 case TextureFunction::LOD0:
1161 out << "" << textureReference << ".SampleCmpLevelZero("
1162 << samplerReference << ", ";
1163 break;
1164 case TextureFunction::LOD0BIAS:
1165 out << "" << textureReference << ".SampleCmpLevelZero("
1166 << samplerReference << ", ";
1167 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001168 default: UNREACHABLE();
1169 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001170 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001171 else
1172 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001173 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001174 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001175 case TextureFunction::IMPLICIT:
1176 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1177 break;
1178 case TextureFunction::BIAS:
1179 out << "" << textureReference << ".SampleBias(" << samplerReference
1180 << ", ";
1181 break;
1182 case TextureFunction::LOD:
1183 out << "" << textureReference << ".SampleLevel(" << samplerReference
1184 << ", ";
1185 break;
1186 case TextureFunction::LOD0:
1187 out << "" << textureReference << ".SampleLevel(" << samplerReference
1188 << ", ";
1189 break;
1190 case TextureFunction::LOD0BIAS:
1191 out << "" << textureReference << ".SampleLevel(" << samplerReference
1192 << ", ";
1193 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001194 default: UNREACHABLE();
1195 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001196 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001197 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001198 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001199
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001200 // Integer sampling requires integer addresses
1201 TString addressx = "";
1202 TString addressy = "";
1203 TString addressz = "";
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001204 TString close = "";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001205
Nicolas Capensfc014542014-02-18 14:47:13 -05001206 if (IsIntegerSampler(textureFunction->sampler) ||
1207 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001208 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001209 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001210 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001211 case 2: out << "int3("; break;
1212 case 3: out << "int4("; break;
1213 default: UNREACHABLE();
1214 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001215
Nicolas Capensfc014542014-02-18 14:47:13 -05001216 // Convert from normalized floating-point to integer
1217 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001218 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001219 addressx = "int(floor(width * frac((";
1220 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001221
Nicolas Capensfc014542014-02-18 14:47:13 -05001222 if (IsSamplerArray(textureFunction->sampler))
1223 {
1224 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1225 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001226 else if (IsSamplerCube(textureFunction->sampler))
Nicolas Capens0027fa92014-02-20 14:26:42 -05001227 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001228 addressz = "((((";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001229 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001230 else
1231 {
1232 addressz = "int(floor(depth * frac((";
1233 }
1234
1235 close = "))))";
Nicolas Capensfc014542014-02-18 14:47:13 -05001236 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001237 }
1238 else
1239 {
1240 switch(hlslCoords)
1241 {
1242 case 2: out << "float2("; break;
1243 case 3: out << "float3("; break;
1244 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001245 default: UNREACHABLE();
1246 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001247 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001248
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001249 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001250
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001251 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001252 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001253 switch(textureFunction->coords)
1254 {
1255 case 3: proj = " / t.z"; break;
1256 case 4: proj = " / t.w"; break;
1257 default: UNREACHABLE();
1258 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001259 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001260
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001261 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001262
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001263 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001264 {
1265 if (hlslCoords >= 3)
1266 {
1267 if (textureFunction->coords < 3)
1268 {
1269 out << ", 0";
1270 }
1271 else
1272 {
1273 out << ", t.z" + proj;
1274 }
1275 }
1276
1277 if (hlslCoords == 4)
1278 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001279 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001280 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001281 case TextureFunction::BIAS: out << ", bias"; break;
1282 case TextureFunction::LOD: out << ", lod"; break;
1283 case TextureFunction::LOD0: out << ", 0"; break;
1284 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001285 default: UNREACHABLE();
1286 }
1287 }
1288
1289 out << "));\n";
1290 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001291 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001292 {
1293 if (hlslCoords >= 3)
1294 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001295 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1296 {
1297 out << ", face";
1298 }
1299 else
1300 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001301 out << ", " + addressz + ("t.z" + proj) + close;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001302 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001303 }
1304
Nicolas Capensd11d5492014-02-19 17:06:10 -05001305 if (textureFunction->method == TextureFunction::GRAD)
1306 {
1307 if (IsIntegerSampler(textureFunction->sampler))
1308 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001309 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001310 }
1311 else if (IsShadowSampler(textureFunction->sampler))
1312 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001313 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001314 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001315 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001316 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1317 // The resulting third component of P' in the shadow forms is used as Dref
1318 out << "), t.z" << proj;
1319 }
1320 else
1321 {
1322 switch(textureFunction->coords)
1323 {
1324 case 3: out << "), t.z"; break;
1325 case 4: out << "), t.w"; break;
1326 default: UNREACHABLE();
1327 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001328 }
1329 }
1330 else
1331 {
1332 out << "), ddx, ddy";
1333 }
1334 }
1335 else if (IsIntegerSampler(textureFunction->sampler) ||
1336 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001337 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001338 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001339 }
1340 else if (IsShadowSampler(textureFunction->sampler))
1341 {
1342 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001343 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001344 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001345 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1346 // The resulting third component of P' in the shadow forms is used as Dref
1347 out << "), t.z" << proj;
1348 }
1349 else
1350 {
1351 switch(textureFunction->coords)
1352 {
1353 case 3: out << "), t.z"; break;
1354 case 4: out << "), t.w"; break;
1355 default: UNREACHABLE();
1356 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001357 }
1358 }
1359 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001360 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001361 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001362 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001363 case TextureFunction::IMPLICIT: out << ")"; break;
1364 case TextureFunction::BIAS: out << "), bias"; break;
1365 case TextureFunction::LOD: out << "), lod"; break;
1366 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001367 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001368 default: UNREACHABLE();
1369 }
1370 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001371
1372 if (textureFunction->offset)
1373 {
1374 out << ", offset";
1375 }
1376
1377 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001378 }
1379 else UNREACHABLE();
1380 }
1381
1382 out << "\n"
1383 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001384 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001385 }
1386
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001387 if (mUsesFragCoord)
1388 {
1389 out << "#define GL_USES_FRAG_COORD\n";
1390 }
1391
1392 if (mUsesPointCoord)
1393 {
1394 out << "#define GL_USES_POINT_COORD\n";
1395 }
1396
1397 if (mUsesFrontFacing)
1398 {
1399 out << "#define GL_USES_FRONT_FACING\n";
1400 }
1401
1402 if (mUsesPointSize)
1403 {
1404 out << "#define GL_USES_POINT_SIZE\n";
1405 }
1406
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001407 if (mUsesFragDepth)
1408 {
1409 out << "#define GL_USES_FRAG_DEPTH\n";
1410 }
1411
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001412 if (mUsesDepthRange)
1413 {
1414 out << "#define GL_USES_DEPTH_RANGE\n";
1415 }
1416
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001417 if (mUsesXor)
1418 {
1419 out << "bool xor(bool p, bool q)\n"
1420 "{\n"
1421 " return (p || q) && !(p && q);\n"
1422 "}\n"
1423 "\n";
1424 }
1425
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001426 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001427}
1428
1429void OutputHLSL::visitSymbol(TIntermSymbol *node)
1430{
Jamie Madill32aab012015-01-27 14:12:26 -05001431 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001432
Jamie Madill570e04d2013-06-21 09:15:33 -04001433 // Handle accessing std140 structs by value
1434 if (mFlaggedStructMappedNames.count(node) > 0)
1435 {
1436 out << mFlaggedStructMappedNames[node];
1437 return;
1438 }
1439
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001440 TString name = node->getSymbol();
1441
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001442 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001443 {
1444 mUsesDepthRange = true;
1445 out << name;
1446 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001447 else
1448 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001449 TQualifier qualifier = node->getQualifier();
1450
1451 if (qualifier == EvqUniform)
1452 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001453 const TType &nodeType = node->getType();
1454 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001455
1456 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001457 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001458 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001459 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001460 else
1461 {
1462 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001463 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001464
Jamie Madill2e295e22015-04-29 10:41:33 -04001465 ensureStructDefined(nodeType);
1466
Jamie Madill033dae62014-06-18 12:56:28 -04001467 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001468 }
Jamie Madill19571812013-08-12 15:26:34 -07001469 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001470 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001471 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001472 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001473 }
Jamie Madill033dae62014-06-18 12:56:28 -04001474 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001475 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001476 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001477 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001478 }
Jamie Madill19571812013-08-12 15:26:34 -07001479 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001480 {
1481 mReferencedOutputVariables[name] = node;
1482 out << "out_" << name;
1483 }
1484 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001485 {
1486 out << "gl_Color[0]";
1487 mUsesFragColor = true;
1488 }
1489 else if (qualifier == EvqFragData)
1490 {
1491 out << "gl_Color";
1492 mUsesFragData = true;
1493 }
1494 else if (qualifier == EvqFragCoord)
1495 {
1496 mUsesFragCoord = true;
1497 out << name;
1498 }
1499 else if (qualifier == EvqPointCoord)
1500 {
1501 mUsesPointCoord = true;
1502 out << name;
1503 }
1504 else if (qualifier == EvqFrontFacing)
1505 {
1506 mUsesFrontFacing = true;
1507 out << name;
1508 }
1509 else if (qualifier == EvqPointSize)
1510 {
1511 mUsesPointSize = true;
1512 out << name;
1513 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001514 else if (qualifier == EvqInstanceID)
1515 {
1516 mUsesInstanceID = true;
1517 out << name;
1518 }
Corentin Wallezb076add2016-01-11 16:45:46 -05001519 else if (qualifier == EvqVertexID)
1520 {
1521 mUsesVertexID = true;
1522 out << name;
1523 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001524 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001525 {
1526 mUsesFragDepth = true;
1527 out << "gl_Depth";
1528 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001529 else
1530 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001531 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001533 }
1534}
1535
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001536void OutputHLSL::visitRaw(TIntermRaw *node)
1537{
Jamie Madill32aab012015-01-27 14:12:26 -05001538 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001539}
1540
Olli Etuaho7fb49552015-03-18 17:27:44 +02001541void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1542{
1543 if (type.isScalar() && !type.isArray())
1544 {
1545 if (op == EOpEqual)
1546 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001547 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001548 }
1549 else
1550 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001551 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001552 }
1553 }
1554 else
1555 {
1556 if (visit == PreVisit && op == EOpNotEqual)
1557 {
1558 out << "!";
1559 }
1560
1561 if (type.isArray())
1562 {
1563 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001564 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001565 }
1566 else if (type.getBasicType() == EbtStruct)
1567 {
1568 const TStructure &structure = *type.getStruct();
1569 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001570 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001571 }
1572 else
1573 {
1574 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001575 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001576 }
1577 }
1578}
1579
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001580bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1581{
Jamie Madill32aab012015-01-27 14:12:26 -05001582 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001583
Jamie Madill570e04d2013-06-21 09:15:33 -04001584 // Handle accessing std140 structs by value
1585 if (mFlaggedStructMappedNames.count(node) > 0)
1586 {
1587 out << mFlaggedStructMappedNames[node];
1588 return false;
1589 }
1590
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001591 switch (node->getOp())
1592 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001593 case EOpAssign:
1594 if (node->getLeft()->isArray())
1595 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001596 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1597 if (rightAgg != nullptr && rightAgg->isConstructor())
1598 {
1599 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1600 out << functionName << "(";
1601 node->getLeft()->traverse(this);
1602 TIntermSequence *seq = rightAgg->getSequence();
1603 for (auto &arrayElement : *seq)
1604 {
1605 out << ", ";
1606 arrayElement->traverse(this);
1607 }
1608 out << ")";
1609 return false;
1610 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001611 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1612 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1613
1614 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001615 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001616 }
1617 else
1618 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001619 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001620 }
1621 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001622 case EOpInitialize:
1623 if (visit == PreVisit)
1624 {
1625 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1626 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1627 // new variable is created before the assignment is evaluated), so we need to convert
1628 // this to "float t = x, x = t;".
1629
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001630 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001631 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001632 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001633
Jamie Madill37997142015-01-28 10:06:34 -05001634 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1635 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001636 {
Jamie Madill37997142015-01-28 10:06:34 -05001637 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001638 // after we initialize uniforms.
1639 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1640 deferredInit->setLeft(node->getLeft());
1641 deferredInit->setRight(node->getRight());
1642 deferredInit->setType(node->getType());
1643 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001644 const TString &initString = initializer(node->getType());
1645 node->setRight(new TIntermRaw(node->getType(), initString));
1646 }
1647 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1648 {
1649 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001650 return false;
1651 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001652 else if (writeConstantInitialization(out, symbolNode, expression))
1653 {
1654 return false;
1655 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001656 }
1657 else if (visit == InVisit)
1658 {
1659 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001660 }
1661 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001662 case EOpAddAssign:
1663 outputTriplet(out, visit, "(", " += ", ")");
1664 break;
1665 case EOpSubAssign:
1666 outputTriplet(out, visit, "(", " -= ", ")");
1667 break;
1668 case EOpMulAssign:
1669 outputTriplet(out, visit, "(", " *= ", ")");
1670 break;
1671 case EOpVectorTimesScalarAssign:
1672 outputTriplet(out, visit, "(", " *= ", ")");
1673 break;
1674 case EOpMatrixTimesScalarAssign:
1675 outputTriplet(out, visit, "(", " *= ", ")");
1676 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001677 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001678 if (visit == PreVisit)
1679 {
1680 out << "(";
1681 }
1682 else if (visit == InVisit)
1683 {
1684 out << " = mul(";
1685 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001686 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001687 }
1688 else
1689 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001690 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001691 }
1692 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001693 case EOpMatrixTimesMatrixAssign:
1694 if (visit == PreVisit)
1695 {
1696 out << "(";
1697 }
1698 else if (visit == InVisit)
1699 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001700 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001701 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001702 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001703 }
1704 else
1705 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001706 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001707 }
1708 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001709 case EOpDivAssign:
1710 outputTriplet(out, visit, "(", " /= ", ")");
1711 break;
1712 case EOpIModAssign:
1713 outputTriplet(out, visit, "(", " %= ", ")");
1714 break;
1715 case EOpBitShiftLeftAssign:
1716 outputTriplet(out, visit, "(", " <<= ", ")");
1717 break;
1718 case EOpBitShiftRightAssign:
1719 outputTriplet(out, visit, "(", " >>= ", ")");
1720 break;
1721 case EOpBitwiseAndAssign:
1722 outputTriplet(out, visit, "(", " &= ", ")");
1723 break;
1724 case EOpBitwiseXorAssign:
1725 outputTriplet(out, visit, "(", " ^= ", ")");
1726 break;
1727 case EOpBitwiseOrAssign:
1728 outputTriplet(out, visit, "(", " |= ", ")");
1729 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001730 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001731 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001732 const TType& leftType = node->getLeft()->getType();
1733 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001734 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001735 if (visit == PreVisit)
1736 {
1737 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1738 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001739 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001740 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001741 return false;
1742 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001743 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001744 else
1745 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001746 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001747 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001748 }
1749 break;
1750 case EOpIndexIndirect:
1751 // We do not currently support indirect references to interface blocks
1752 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001753 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001754 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001755 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001756 if (visit == InVisit)
1757 {
1758 const TStructure* structure = node->getLeft()->getType().getStruct();
1759 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1760 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001761 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001762
1763 return false;
1764 }
1765 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001766 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001767 if (visit == InVisit)
1768 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001769 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1770 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1771 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001772 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001773
1774 return false;
1775 }
1776 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777 case EOpVectorSwizzle:
1778 if (visit == InVisit)
1779 {
1780 out << ".";
1781
1782 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1783
1784 if (swizzle)
1785 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001786 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001787
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001788 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001789 {
1790 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1791
1792 if (element)
1793 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001794 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001795
1796 switch (i)
1797 {
1798 case 0: out << "x"; break;
1799 case 1: out << "y"; break;
1800 case 2: out << "z"; break;
1801 case 3: out << "w"; break;
1802 default: UNREACHABLE();
1803 }
1804 }
1805 else UNREACHABLE();
1806 }
1807 }
1808 else UNREACHABLE();
1809
1810 return false; // Fully processed
1811 }
1812 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001813 case EOpAdd:
1814 outputTriplet(out, visit, "(", " + ", ")");
1815 break;
1816 case EOpSub:
1817 outputTriplet(out, visit, "(", " - ", ")");
1818 break;
1819 case EOpMul:
1820 outputTriplet(out, visit, "(", " * ", ")");
1821 break;
1822 case EOpDiv:
1823 outputTriplet(out, visit, "(", " / ", ")");
1824 break;
1825 case EOpIMod:
1826 outputTriplet(out, visit, "(", " % ", ")");
1827 break;
1828 case EOpBitShiftLeft:
1829 outputTriplet(out, visit, "(", " << ", ")");
1830 break;
1831 case EOpBitShiftRight:
1832 outputTriplet(out, visit, "(", " >> ", ")");
1833 break;
1834 case EOpBitwiseAnd:
1835 outputTriplet(out, visit, "(", " & ", ")");
1836 break;
1837 case EOpBitwiseXor:
1838 outputTriplet(out, visit, "(", " ^ ", ")");
1839 break;
1840 case EOpBitwiseOr:
1841 outputTriplet(out, visit, "(", " | ", ")");
1842 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001843 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001844 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001845 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001846 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001847 case EOpLessThan:
1848 outputTriplet(out, visit, "(", " < ", ")");
1849 break;
1850 case EOpGreaterThan:
1851 outputTriplet(out, visit, "(", " > ", ")");
1852 break;
1853 case EOpLessThanEqual:
1854 outputTriplet(out, visit, "(", " <= ", ")");
1855 break;
1856 case EOpGreaterThanEqual:
1857 outputTriplet(out, visit, "(", " >= ", ")");
1858 break;
1859 case EOpVectorTimesScalar:
1860 outputTriplet(out, visit, "(", " * ", ")");
1861 break;
1862 case EOpMatrixTimesScalar:
1863 outputTriplet(out, visit, "(", " * ", ")");
1864 break;
1865 case EOpVectorTimesMatrix:
1866 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1867 break;
1868 case EOpMatrixTimesVector:
1869 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1870 break;
1871 case EOpMatrixTimesMatrix:
1872 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1873 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001874 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001875 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1876 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001877 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001878 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001879 case EOpLogicalXor:
1880 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001881 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001882 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001883 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001884 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1885 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001886 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001887 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888 default: UNREACHABLE();
1889 }
1890
1891 return true;
1892}
1893
1894bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1895{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001896 TInfoSinkBase &out = getInfoSink();
1897
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001898 switch (node->getOp())
1899 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001900 case EOpNegative:
1901 outputTriplet(out, visit, "(-", "", ")");
1902 break;
1903 case EOpPositive:
1904 outputTriplet(out, visit, "(+", "", ")");
1905 break;
1906 case EOpVectorLogicalNot:
1907 outputTriplet(out, visit, "(!", "", ")");
1908 break;
1909 case EOpLogicalNot:
1910 outputTriplet(out, visit, "(!", "", ")");
1911 break;
1912 case EOpBitwiseNot:
1913 outputTriplet(out, visit, "(~", "", ")");
1914 break;
1915 case EOpPostIncrement:
1916 outputTriplet(out, visit, "(", "", "++)");
1917 break;
1918 case EOpPostDecrement:
1919 outputTriplet(out, visit, "(", "", "--)");
1920 break;
1921 case EOpPreIncrement:
1922 outputTriplet(out, visit, "(++", "", ")");
1923 break;
1924 case EOpPreDecrement:
1925 outputTriplet(out, visit, "(--", "", ")");
1926 break;
1927 case EOpRadians:
1928 outputTriplet(out, visit, "radians(", "", ")");
1929 break;
1930 case EOpDegrees:
1931 outputTriplet(out, visit, "degrees(", "", ")");
1932 break;
1933 case EOpSin:
1934 outputTriplet(out, visit, "sin(", "", ")");
1935 break;
1936 case EOpCos:
1937 outputTriplet(out, visit, "cos(", "", ")");
1938 break;
1939 case EOpTan:
1940 outputTriplet(out, visit, "tan(", "", ")");
1941 break;
1942 case EOpAsin:
1943 outputTriplet(out, visit, "asin(", "", ")");
1944 break;
1945 case EOpAcos:
1946 outputTriplet(out, visit, "acos(", "", ")");
1947 break;
1948 case EOpAtan:
1949 outputTriplet(out, visit, "atan(", "", ")");
1950 break;
1951 case EOpSinh:
1952 outputTriplet(out, visit, "sinh(", "", ")");
1953 break;
1954 case EOpCosh:
1955 outputTriplet(out, visit, "cosh(", "", ")");
1956 break;
1957 case EOpTanh:
1958 outputTriplet(out, visit, "tanh(", "", ")");
1959 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001960 case EOpAsinh:
1961 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001962 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001963 break;
1964 case EOpAcosh:
1965 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001966 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001967 break;
1968 case EOpAtanh:
1969 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001970 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001971 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001972 case EOpExp:
1973 outputTriplet(out, visit, "exp(", "", ")");
1974 break;
1975 case EOpLog:
1976 outputTriplet(out, visit, "log(", "", ")");
1977 break;
1978 case EOpExp2:
1979 outputTriplet(out, visit, "exp2(", "", ")");
1980 break;
1981 case EOpLog2:
1982 outputTriplet(out, visit, "log2(", "", ")");
1983 break;
1984 case EOpSqrt:
1985 outputTriplet(out, visit, "sqrt(", "", ")");
1986 break;
1987 case EOpInverseSqrt:
1988 outputTriplet(out, visit, "rsqrt(", "", ")");
1989 break;
1990 case EOpAbs:
1991 outputTriplet(out, visit, "abs(", "", ")");
1992 break;
1993 case EOpSign:
1994 outputTriplet(out, visit, "sign(", "", ")");
1995 break;
1996 case EOpFloor:
1997 outputTriplet(out, visit, "floor(", "", ")");
1998 break;
1999 case EOpTrunc:
2000 outputTriplet(out, visit, "trunc(", "", ")");
2001 break;
2002 case EOpRound:
2003 outputTriplet(out, visit, "round(", "", ")");
2004 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08002005 case EOpRoundEven:
2006 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002007 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08002008 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002009 case EOpCeil:
2010 outputTriplet(out, visit, "ceil(", "", ")");
2011 break;
2012 case EOpFract:
2013 outputTriplet(out, visit, "frac(", "", ")");
2014 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05302015 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002016 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05302017 mRequiresIEEEStrictCompiling = true;
2018 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002019 case EOpIsInf:
2020 outputTriplet(out, visit, "isinf(", "", ")");
2021 break;
2022 case EOpFloatBitsToInt:
2023 outputTriplet(out, visit, "asint(", "", ")");
2024 break;
2025 case EOpFloatBitsToUint:
2026 outputTriplet(out, visit, "asuint(", "", ")");
2027 break;
2028 case EOpIntBitsToFloat:
2029 outputTriplet(out, visit, "asfloat(", "", ")");
2030 break;
2031 case EOpUintBitsToFloat:
2032 outputTriplet(out, visit, "asfloat(", "", ")");
2033 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002034 case EOpPackSnorm2x16:
2035 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002036 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002037 break;
2038 case EOpPackUnorm2x16:
2039 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002040 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002041 break;
2042 case EOpPackHalf2x16:
2043 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002044 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002045 break;
2046 case EOpUnpackSnorm2x16:
2047 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002048 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002049 break;
2050 case EOpUnpackUnorm2x16:
2051 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002052 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002053 break;
2054 case EOpUnpackHalf2x16:
2055 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002056 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002057 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002058 case EOpLength:
2059 outputTriplet(out, visit, "length(", "", ")");
2060 break;
2061 case EOpNormalize:
2062 outputTriplet(out, visit, "normalize(", "", ")");
2063 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002064 case EOpDFdx:
2065 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2066 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002067 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002068 }
2069 else
2070 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002071 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002072 }
2073 break;
2074 case EOpDFdy:
2075 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2076 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002077 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002078 }
2079 else
2080 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002081 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002082 }
2083 break;
2084 case EOpFwidth:
2085 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2086 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002087 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002088 }
2089 else
2090 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002091 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002092 }
2093 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002094 case EOpTranspose:
2095 outputTriplet(out, visit, "transpose(", "", ")");
2096 break;
2097 case EOpDeterminant:
2098 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2099 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002100 case EOpInverse:
2101 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002102 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002103 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002104
Jamie Madill8c46ab12015-12-07 16:39:19 -05002105 case EOpAny:
2106 outputTriplet(out, visit, "any(", "", ")");
2107 break;
2108 case EOpAll:
2109 outputTriplet(out, visit, "all(", "", ")");
2110 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111 default: UNREACHABLE();
2112 }
2113
2114 return true;
2115}
2116
2117bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2118{
Jamie Madill32aab012015-01-27 14:12:26 -05002119 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002121 switch (node->getOp())
2122 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002123 case EOpSequence:
2124 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002125 if (mInsideFunction)
2126 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002127 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002128 out << "{\n";
2129 }
2130
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002131 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002132 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002133 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002134
Olli Etuahoa6f22092015-05-08 18:31:10 +03002135 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002136
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002137 // Don't output ; after case labels, they're terminated by :
2138 // This is needed especially since outputting a ; after a case statement would turn empty
2139 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002140 // Also no need to output ; after selection (if) statements or sequences. This is done just
2141 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002142 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2143 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002144 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002145 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002146 }
2147
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002148 if (mInsideFunction)
2149 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002150 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002151 out << "}\n";
2152 }
2153
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002154 return false;
2155 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 case EOpDeclaration:
2157 if (visit == PreVisit)
2158 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002159 TIntermSequence *sequence = node->getSequence();
2160 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002161 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002162
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002163 if (variable &&
2164 (variable->getQualifier() == EvqTemporary ||
2165 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002167 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002168
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002169 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002171 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002173 out << "static ";
2174 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002175
Olli Etuahoa6f22092015-05-08 18:31:10 +03002176 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002177
Olli Etuahoa6f22092015-05-08 18:31:10 +03002178 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002179
Olli Etuahoa6f22092015-05-08 18:31:10 +03002180 if (symbol)
2181 {
2182 symbol->traverse(this);
2183 out << ArrayString(symbol->getType());
2184 out << " = " + initializer(symbol->getType());
2185 }
2186 else
2187 {
2188 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002189 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002190 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002191 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2192 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002193 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002194 }
2195 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002196 }
Jamie Madill033dae62014-06-18 12:56:28 -04002197 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002198 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002199 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002200 {
2201 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2202
2203 if (symbol)
2204 {
2205 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2206 mReferencedVaryings[symbol->getSymbol()] = symbol;
2207 }
2208 else
2209 {
2210 (*sit)->traverse(this);
2211 }
2212 }
2213 }
2214
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002215 return false;
2216 }
2217 else if (visit == InVisit)
2218 {
2219 out << ", ";
2220 }
2221 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002222 case EOpInvariantDeclaration:
2223 // Do not do any translation
2224 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002225 case EOpPrototype:
2226 if (visit == PreVisit)
2227 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002228 size_t index = mCallDag.findIndex(node);
2229 // Skip the prototype if it is not implemented (and thus not used)
2230 if (index == CallDAG::InvalidIndex)
2231 {
2232 return false;
2233 }
2234
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002235 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002236
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002237 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2238 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
2239 << (mOutputLod0Function ? "Lod0(" : "(");
2240
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002241 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002242 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002243 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002244
2245 if (symbol)
2246 {
2247 out << argumentString(symbol);
2248
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002249 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002250 {
2251 out << ", ";
2252 }
2253 }
2254 else UNREACHABLE();
2255 }
2256
2257 out << ");\n";
2258
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002259 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002260 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2261 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002262 {
2263 mOutputLod0Function = true;
2264 node->traverse(this);
2265 mOutputLod0Function = false;
2266 }
2267
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002268 return false;
2269 }
2270 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002271 case EOpComma:
2272 outputTriplet(out, visit, "(", ", ", ")");
2273 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 case EOpFunction:
2275 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002276 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002277 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278
Corentin Wallez1239ee92015-03-19 14:38:02 -07002279 size_t index = mCallDag.findIndex(node);
2280 ASSERT(index != CallDAG::InvalidIndex);
2281 mCurrentFunctionMetadata = &mASTMetadataList[index];
2282
Jamie Madill033dae62014-06-18 12:56:28 -04002283 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002284
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002285 TIntermSequence *sequence = node->getSequence();
2286 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
2287
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002288 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002290 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002291 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002292 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002294 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002295 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002296 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002297
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002298 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002299 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002300 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002301
2302 if (symbol)
2303 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002304 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002305
2306 out << argumentString(symbol);
2307
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002308 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002309 {
2310 out << ", ";
2311 }
2312 }
2313 else UNREACHABLE();
2314 }
2315
Olli Etuaho4785fec2015-05-18 16:09:37 +03002316 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002317
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002318 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002319 {
2320 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002321 TIntermNode *body = (*sequence)[1];
2322 // The function body node will output braces.
2323 ASSERT(IsSequence(body));
2324 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002325 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002327 else
2328 {
2329 out << "{}\n";
2330 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002331
Corentin Wallez1239ee92015-03-19 14:38:02 -07002332 mCurrentFunctionMetadata = nullptr;
2333
2334 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2335 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002336 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002337 ASSERT(name != "main");
2338 mOutputLod0Function = true;
2339 node->traverse(this);
2340 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002341 }
2342
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002343 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344 }
2345 break;
2346 case EOpFunctionCall:
2347 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002348 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002349
Corentin Wallez1239ee92015-03-19 14:38:02 -07002350 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002351 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002352 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002353 if (node->isArray())
2354 {
2355 UNIMPLEMENTED();
2356 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002357 size_t index = mCallDag.findIndex(node);
2358 ASSERT(index != CallDAG::InvalidIndex);
2359 lod0 &= mASTMetadataList[index].mNeedsLod0;
2360
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002361 out << DecorateFunctionIfNeeded(node->getNameObj());
2362 out << DisambiguateFunctionName(node->getSequence());
2363 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002364 }
2365 else
2366 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002367 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002368 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002369
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002370 TextureFunction textureFunction;
2371 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002372 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002373 textureFunction.method = TextureFunction::IMPLICIT;
2374 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002375 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002376
2377 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002378 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002379 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002380 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002381 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002382 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002383 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002384 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002385 }
Nicolas Capens46485082014-04-15 13:12:50 -04002386 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2387 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002388 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002389 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002390 }
Nicolas Capens46485082014-04-15 13:12:50 -04002391 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002392 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002393 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002394 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002395 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002396 else if (name == "textureSize")
2397 {
2398 textureFunction.method = TextureFunction::SIZE;
2399 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002400 else if (name == "textureOffset")
2401 {
2402 textureFunction.method = TextureFunction::IMPLICIT;
2403 textureFunction.offset = true;
2404 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002405 else if (name == "textureProjOffset")
2406 {
2407 textureFunction.method = TextureFunction::IMPLICIT;
2408 textureFunction.offset = true;
2409 textureFunction.proj = true;
2410 }
2411 else if (name == "textureLodOffset")
2412 {
2413 textureFunction.method = TextureFunction::LOD;
2414 textureFunction.offset = true;
2415 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002416 else if (name == "textureProjLodOffset")
2417 {
2418 textureFunction.method = TextureFunction::LOD;
2419 textureFunction.proj = true;
2420 textureFunction.offset = true;
2421 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002422 else if (name == "texelFetch")
2423 {
2424 textureFunction.method = TextureFunction::FETCH;
2425 }
2426 else if (name == "texelFetchOffset")
2427 {
2428 textureFunction.method = TextureFunction::FETCH;
2429 textureFunction.offset = true;
2430 }
Nicolas Capens46485082014-04-15 13:12:50 -04002431 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002432 {
2433 textureFunction.method = TextureFunction::GRAD;
2434 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002435 else if (name == "textureGradOffset")
2436 {
2437 textureFunction.method = TextureFunction::GRAD;
2438 textureFunction.offset = true;
2439 }
Nicolas Capens46485082014-04-15 13:12:50 -04002440 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002441 {
2442 textureFunction.method = TextureFunction::GRAD;
2443 textureFunction.proj = true;
2444 }
2445 else if (name == "textureProjGradOffset")
2446 {
2447 textureFunction.method = TextureFunction::GRAD;
2448 textureFunction.proj = true;
2449 textureFunction.offset = true;
2450 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002451 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002452
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002453 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002454 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002455 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2456
2457 if (textureFunction.offset)
2458 {
2459 mandatoryArgumentCount++;
2460 }
2461
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002462 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002463
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002464 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002465 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002466 if (bias)
2467 {
2468 textureFunction.method = TextureFunction::LOD0BIAS;
2469 }
2470 else
2471 {
2472 textureFunction.method = TextureFunction::LOD0;
2473 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002474 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002475 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002476 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002477 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002478 }
2479 }
2480
2481 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002482
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002483 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002484 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002485
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002486 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002487 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002488 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT &&
2489 IsSampler((*arg)->getAsTyped()->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002490 {
2491 out << "texture_";
2492 (*arg)->traverse(this);
2493 out << ", sampler_";
2494 }
2495
2496 (*arg)->traverse(this);
2497
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002498 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002499 {
2500 out << ", ";
2501 }
2502 }
2503
2504 out << ")";
2505
2506 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002507 }
2508 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002509 case EOpParameters:
2510 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2511 break;
2512 case EOpConstructFloat:
2513 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2514 break;
2515 case EOpConstructVec2:
2516 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2517 break;
2518 case EOpConstructVec3:
2519 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2520 break;
2521 case EOpConstructVec4:
2522 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2523 break;
2524 case EOpConstructBool:
2525 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2526 break;
2527 case EOpConstructBVec2:
2528 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2529 break;
2530 case EOpConstructBVec3:
2531 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2532 break;
2533 case EOpConstructBVec4:
2534 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2535 break;
2536 case EOpConstructInt:
2537 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2538 break;
2539 case EOpConstructIVec2:
2540 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2541 break;
2542 case EOpConstructIVec3:
2543 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2544 break;
2545 case EOpConstructIVec4:
2546 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2547 break;
2548 case EOpConstructUInt:
2549 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2550 break;
2551 case EOpConstructUVec2:
2552 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2553 break;
2554 case EOpConstructUVec3:
2555 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2556 break;
2557 case EOpConstructUVec4:
2558 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2559 break;
2560 case EOpConstructMat2:
2561 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2562 break;
2563 case EOpConstructMat2x3:
2564 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2565 break;
2566 case EOpConstructMat2x4:
2567 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2568 break;
2569 case EOpConstructMat3x2:
2570 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2571 break;
2572 case EOpConstructMat3:
2573 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2574 break;
2575 case EOpConstructMat3x4:
2576 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2577 break;
2578 case EOpConstructMat4x2:
2579 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2580 break;
2581 case EOpConstructMat4x3:
2582 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2583 break;
2584 case EOpConstructMat4:
2585 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2586 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002587 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002588 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002589 if (node->getType().isArray())
2590 {
2591 UNIMPLEMENTED();
2592 }
Jamie Madill033dae62014-06-18 12:56:28 -04002593 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002594 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002595 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002596 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002597 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002598 case EOpLessThan:
2599 outputTriplet(out, visit, "(", " < ", ")");
2600 break;
2601 case EOpGreaterThan:
2602 outputTriplet(out, visit, "(", " > ", ")");
2603 break;
2604 case EOpLessThanEqual:
2605 outputTriplet(out, visit, "(", " <= ", ")");
2606 break;
2607 case EOpGreaterThanEqual:
2608 outputTriplet(out, visit, "(", " >= ", ")");
2609 break;
2610 case EOpVectorEqual:
2611 outputTriplet(out, visit, "(", " == ", ")");
2612 break;
2613 case EOpVectorNotEqual:
2614 outputTriplet(out, visit, "(", " != ", ")");
2615 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002616 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002617 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002618 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002619 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002620 case EOpModf:
2621 outputTriplet(out, visit, "modf(", ", ", ")");
2622 break;
2623 case EOpPow:
2624 outputTriplet(out, visit, "pow(", ", ", ")");
2625 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002626 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002627 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002628 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002629 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002630 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002631 case EOpMin:
2632 outputTriplet(out, visit, "min(", ", ", ")");
2633 break;
2634 case EOpMax:
2635 outputTriplet(out, visit, "max(", ", ", ")");
2636 break;
2637 case EOpClamp:
2638 outputTriplet(out, visit, "clamp(", ", ", ")");
2639 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302640 case EOpMix:
2641 {
2642 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2643 if (lastParamNode->getType().getBasicType() == EbtBool)
2644 {
2645 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2646 // so use emulated version.
2647 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002648 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302649 }
2650 else
2651 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002652 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302653 }
2654 }
2655 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002656 case EOpStep:
2657 outputTriplet(out, visit, "step(", ", ", ")");
2658 break;
2659 case EOpSmoothStep:
2660 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2661 break;
2662 case EOpDistance:
2663 outputTriplet(out, visit, "distance(", ", ", ")");
2664 break;
2665 case EOpDot:
2666 outputTriplet(out, visit, "dot(", ", ", ")");
2667 break;
2668 case EOpCross:
2669 outputTriplet(out, visit, "cross(", ", ", ")");
2670 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002671 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002672 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002673 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002674 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002675 case EOpReflect:
2676 outputTriplet(out, visit, "reflect(", ", ", ")");
2677 break;
2678 case EOpRefract:
2679 outputTriplet(out, visit, "refract(", ", ", ")");
2680 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002681 case EOpOuterProduct:
2682 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002683 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002684 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002685 case EOpMul:
2686 outputTriplet(out, visit, "(", " * ", ")");
2687 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002688 default: UNREACHABLE();
2689 }
2690
2691 return true;
2692}
2693
Jamie Madill8c46ab12015-12-07 16:39:19 -05002694void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002695{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002696 out << "if (";
2697
2698 node->getCondition()->traverse(this);
2699
2700 out << ")\n";
2701
Jamie Madill8c46ab12015-12-07 16:39:19 -05002702 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002703
2704 bool discard = false;
2705
2706 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002707 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002708 // The trueBlock child node will output braces.
2709 ASSERT(IsSequence(node->getTrueBlock()));
2710
Olli Etuahoa6f22092015-05-08 18:31:10 +03002711 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002712
Olli Etuahoa6f22092015-05-08 18:31:10 +03002713 // Detect true discard
2714 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2715 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002716 else
2717 {
2718 // TODO(oetuaho): Check if the semicolon inside is necessary.
2719 // It's there as a result of conservative refactoring of the output.
2720 out << "{;}\n";
2721 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002722
Jamie Madill8c46ab12015-12-07 16:39:19 -05002723 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002724
Olli Etuahoa6f22092015-05-08 18:31:10 +03002725 if (node->getFalseBlock())
2726 {
2727 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002728
Jamie Madill8c46ab12015-12-07 16:39:19 -05002729 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002730
Olli Etuaho4785fec2015-05-18 16:09:37 +03002731 // Either this is "else if" or the falseBlock child node will output braces.
2732 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2733
Olli Etuahoa6f22092015-05-08 18:31:10 +03002734 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002735
Jamie Madill8c46ab12015-12-07 16:39:19 -05002736 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002737
Olli Etuahoa6f22092015-05-08 18:31:10 +03002738 // Detect false discard
2739 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2740 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002741
Olli Etuahoa6f22092015-05-08 18:31:10 +03002742 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002743 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002744 {
2745 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002746 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002747}
2748
2749bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2750{
2751 TInfoSinkBase &out = getInfoSink();
2752
2753 ASSERT(!node->usesTernaryOperator());
2754
2755 if (!mInsideFunction)
2756 {
2757 // This is part of unfolded global initialization.
2758 mDeferredGlobalInitializers.push_back(node);
2759 return false;
2760 }
2761
2762 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002763 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002764 {
2765 out << "FLATTEN ";
2766 }
2767
Jamie Madill8c46ab12015-12-07 16:39:19 -05002768 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002769
2770 return false;
2771}
2772
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002773bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002774{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002775 TInfoSinkBase &out = getInfoSink();
2776
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002777 if (node->getStatementList())
2778 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002779 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002780 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002781 // The curly braces get written when visiting the statementList aggregate
2782 }
2783 else
2784 {
2785 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002786 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002787 }
2788 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002789}
2790
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002791bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002792{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002793 TInfoSinkBase &out = getInfoSink();
2794
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002795 if (node->hasCondition())
2796 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002797 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002798 return true;
2799 }
2800 else
2801 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002802 out << "default:\n";
2803 return false;
2804 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002805}
2806
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2808{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002809 TInfoSinkBase &out = getInfoSink();
2810 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811}
2812
2813bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2814{
Nicolas Capens655fe362014-04-11 13:12:34 -04002815 mNestedLoopDepth++;
2816
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002817 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002818 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002819 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002820
Jamie Madill8c46ab12015-12-07 16:39:19 -05002821 TInfoSinkBase &out = getInfoSink();
2822
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002823 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002824 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002825 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002826 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002827 mInsideDiscontinuousLoop = wasDiscontinuous;
2828 mNestedLoopDepth--;
2829
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002830 return false;
2831 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002832 }
2833
Corentin Wallez1239ee92015-03-19 14:38:02 -07002834 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002835 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002836 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002837 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002838
Jamie Madill8c46ab12015-12-07 16:39:19 -05002839 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002840 }
2841 else
2842 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002843 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002844
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002845 if (node->getInit())
2846 {
2847 node->getInit()->traverse(this);
2848 }
2849
2850 out << "; ";
2851
alokp@chromium.org52813552010-11-16 18:36:09 +00002852 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002853 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002854 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002855 }
2856
2857 out << "; ";
2858
alokp@chromium.org52813552010-11-16 18:36:09 +00002859 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002861 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862 }
2863
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002864 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002865
Jamie Madill8c46ab12015-12-07 16:39:19 -05002866 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867 }
2868
2869 if (node->getBody())
2870 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002871 // The loop body node will output braces.
2872 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002873 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002875 else
2876 {
2877 // TODO(oetuaho): Check if the semicolon inside is necessary.
2878 // It's there as a result of conservative refactoring of the output.
2879 out << "{;}\n";
2880 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881
Jamie Madill8c46ab12015-12-07 16:39:19 -05002882 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883
alokp@chromium.org52813552010-11-16 18:36:09 +00002884 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002885 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002886 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002887 out << "while(\n";
2888
alokp@chromium.org52813552010-11-16 18:36:09 +00002889 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002890
daniel@transgaming.com73536982012-03-21 20:45:49 +00002891 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002892 }
2893
daniel@transgaming.com73536982012-03-21 20:45:49 +00002894 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002895
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002896 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002897 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002898
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002899 return false;
2900}
2901
2902bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2903{
Jamie Madill32aab012015-01-27 14:12:26 -05002904 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002905
2906 switch (node->getFlowOp())
2907 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002908 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002909 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002910 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002911 case EOpBreak:
2912 if (visit == PreVisit)
2913 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002914 if (mNestedLoopDepth > 1)
2915 {
2916 mUsesNestedBreak = true;
2917 }
2918
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002919 if (mExcessiveLoopIndex)
2920 {
2921 out << "{Break";
2922 mExcessiveLoopIndex->traverse(this);
2923 out << " = true; break;}\n";
2924 }
2925 else
2926 {
2927 out << "break;\n";
2928 }
2929 }
2930 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002931 case EOpContinue:
2932 outputTriplet(out, visit, "continue;\n", "", "");
2933 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002934 case EOpReturn:
2935 if (visit == PreVisit)
2936 {
2937 if (node->getExpression())
2938 {
2939 out << "return ";
2940 }
2941 else
2942 {
2943 out << "return;\n";
2944 }
2945 }
2946 else if (visit == PostVisit)
2947 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002948 if (node->getExpression())
2949 {
2950 out << ";\n";
2951 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002952 }
2953 break;
2954 default: UNREACHABLE();
2955 }
2956
2957 return true;
2958}
2959
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002960bool OutputHLSL::isSingleStatement(TIntermNode *node)
2961{
2962 TIntermAggregate *aggregate = node->getAsAggregate();
2963
2964 if (aggregate)
2965 {
2966 if (aggregate->getOp() == EOpSequence)
2967 {
2968 return false;
2969 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002970 else if (aggregate->getOp() == EOpDeclaration)
2971 {
2972 // Declaring multiple comma-separated variables must be considered multiple statements
2973 // because each individual declaration has side effects which are visible in the next.
2974 return false;
2975 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002976 else
2977 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002978 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002979 {
2980 if (!isSingleStatement(*sit))
2981 {
2982 return false;
2983 }
2984 }
2985
2986 return true;
2987 }
2988 }
2989
2990 return true;
2991}
2992
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002993// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2994// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002995bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002996{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002997 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002998
2999 // Parse loops of the form:
3000 // for(int index = initial; index [comparator] limit; index += increment)
3001 TIntermSymbol *index = NULL;
3002 TOperator comparator = EOpNull;
3003 int initial = 0;
3004 int limit = 0;
3005 int increment = 0;
3006
3007 // Parse index name and intial value
3008 if (node->getInit())
3009 {
3010 TIntermAggregate *init = node->getInit()->getAsAggregate();
3011
3012 if (init)
3013 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003014 TIntermSequence *sequence = init->getSequence();
3015 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003016
3017 if (variable && variable->getQualifier() == EvqTemporary)
3018 {
3019 TIntermBinary *assign = variable->getAsBinaryNode();
3020
3021 if (assign->getOp() == EOpInitialize)
3022 {
3023 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3024 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3025
3026 if (symbol && constant)
3027 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003028 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003029 {
3030 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003031 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003032 }
3033 }
3034 }
3035 }
3036 }
3037 }
3038
3039 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003040 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003041 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003042 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003043
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003044 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3045 {
3046 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3047
3048 if (constant)
3049 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003050 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003051 {
3052 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003053 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003054 }
3055 }
3056 }
3057 }
3058
3059 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003060 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003061 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003062 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3063 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003064
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003065 if (binaryTerminal)
3066 {
3067 TOperator op = binaryTerminal->getOp();
3068 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3069
3070 if (constant)
3071 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003072 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003073 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003074 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003075
3076 switch (op)
3077 {
3078 case EOpAddAssign: increment = value; break;
3079 case EOpSubAssign: increment = -value; break;
3080 default: UNIMPLEMENTED();
3081 }
3082 }
3083 }
3084 }
3085 else if (unaryTerminal)
3086 {
3087 TOperator op = unaryTerminal->getOp();
3088
3089 switch (op)
3090 {
3091 case EOpPostIncrement: increment = 1; break;
3092 case EOpPostDecrement: increment = -1; break;
3093 case EOpPreIncrement: increment = 1; break;
3094 case EOpPreDecrement: increment = -1; break;
3095 default: UNIMPLEMENTED();
3096 }
3097 }
3098 }
3099
3100 if (index != NULL && comparator != EOpNull && increment != 0)
3101 {
3102 if (comparator == EOpLessThanEqual)
3103 {
3104 comparator = EOpLessThan;
3105 limit += 1;
3106 }
3107
3108 if (comparator == EOpLessThan)
3109 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003110 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003111
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003112 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003113 {
3114 return false; // Not an excessive loop
3115 }
3116
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003117 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3118 mExcessiveLoopIndex = index;
3119
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003120 out << "{int ";
3121 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003122 out << ";\n"
3123 "bool Break";
3124 index->traverse(this);
3125 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003126
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003127 bool firstLoopFragment = true;
3128
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003129 while (iterations > 0)
3130 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003131 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003132
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003133 if (!firstLoopFragment)
3134 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003135 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003136 index->traverse(this);
3137 out << ") {\n";
3138 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003139
3140 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3141 {
3142 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3143 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003144
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003145 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003146 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003147
Corentin Wallez1239ee92015-03-19 14:38:02 -07003148 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003149 index->traverse(this);
3150 out << " = ";
3151 out << initial;
3152
3153 out << "; ";
3154 index->traverse(this);
3155 out << " < ";
3156 out << clampedLimit;
3157
3158 out << "; ";
3159 index->traverse(this);
3160 out << " += ";
3161 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003162 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003163
Jamie Madill8c46ab12015-12-07 16:39:19 -05003164 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003165 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003166
3167 if (node->getBody())
3168 {
3169 node->getBody()->traverse(this);
3170 }
3171
Jamie Madill8c46ab12015-12-07 16:39:19 -05003172 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003173 out << ";}\n";
3174
3175 if (!firstLoopFragment)
3176 {
3177 out << "}\n";
3178 }
3179
3180 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003181
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003182 initial += MAX_LOOP_ITERATIONS * increment;
3183 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003184 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003185
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003186 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003187
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003188 mExcessiveLoopIndex = restoreIndex;
3189
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003190 return true;
3191 }
3192 else UNIMPLEMENTED();
3193 }
3194
3195 return false; // Not handled as an excessive loop
3196}
3197
Jamie Madill8c46ab12015-12-07 16:39:19 -05003198void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3199 Visit visit,
3200 const char *preString,
3201 const char *inString,
3202 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003203{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003204 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003205 {
3206 out << preString;
3207 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003208 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003209 {
3210 out << inString;
3211 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003212 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003213 {
3214 out << postString;
3215 }
3216}
3217
Jamie Madill8c46ab12015-12-07 16:39:19 -05003218void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003219{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003220 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003221 {
Jamie Madill32aab012015-01-27 14:12:26 -05003222 out << "\n";
3223 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003224
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003225 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003226 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003227 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003228 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003229
Jamie Madill32aab012015-01-27 14:12:26 -05003230 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003231 }
3232}
3233
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003234TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3235{
3236 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003237 const TType &type = symbol->getType();
3238 const TName &name = symbol->getName();
3239 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003240
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003241 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003242 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003243 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003244 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003245 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003246 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003247 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003248 }
3249
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003250 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003251 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003252 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3253 {
3254 // Samplers are passed as indices to the sampler array.
3255 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3256 return "const uint " + nameStr + ArrayString(type);
3257 }
3258 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3259 {
3260 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3261 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3262 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3263 ArrayString(type);
3264 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003265 }
3266
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003267 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003268}
3269
3270TString OutputHLSL::initializer(const TType &type)
3271{
3272 TString string;
3273
Jamie Madill94bf7f22013-07-08 13:31:15 -04003274 size_t size = type.getObjectSize();
3275 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003276 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003277 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003278
Jamie Madill94bf7f22013-07-08 13:31:15 -04003279 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003280 {
3281 string += ", ";
3282 }
3283 }
3284
daniel@transgaming.comead23042010-04-29 03:35:36 +00003285 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003286}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003287
Jamie Madill8c46ab12015-12-07 16:39:19 -05003288void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3289 Visit visit,
3290 const TType &type,
3291 const char *name,
3292 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003293{
Olli Etuahof40319e2015-03-10 14:33:00 +02003294 if (type.isArray())
3295 {
3296 UNIMPLEMENTED();
3297 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003298
3299 if (visit == PreVisit)
3300 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003301 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003302
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003303 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003304 }
3305 else if (visit == InVisit)
3306 {
3307 out << ", ";
3308 }
3309 else if (visit == PostVisit)
3310 {
3311 out << ")";
3312 }
3313}
3314
Jamie Madill8c46ab12015-12-07 16:39:19 -05003315const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3316 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003317 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003318{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003319 const TConstantUnion *constUnionIterated = constUnion;
3320
Jamie Madill98493dd2013-07-08 14:39:03 -04003321 const TStructure* structure = type.getStruct();
3322 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003323 {
Jamie Madill033dae62014-06-18 12:56:28 -04003324 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003325
Jamie Madill98493dd2013-07-08 14:39:03 -04003326 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003327
Jamie Madill98493dd2013-07-08 14:39:03 -04003328 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003329 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003330 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003331 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003332
Jamie Madill98493dd2013-07-08 14:39:03 -04003333 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003334 {
3335 out << ", ";
3336 }
3337 }
3338
3339 out << ")";
3340 }
3341 else
3342 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003343 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003344 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003345
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003346 if (writeType)
3347 {
Jamie Madill033dae62014-06-18 12:56:28 -04003348 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003349 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003350 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003351 if (writeType)
3352 {
3353 out << ")";
3354 }
3355 }
3356
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003357 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003358}
3359
Jamie Madill8c46ab12015-12-07 16:39:19 -05003360void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003361{
3362 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003363 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003364}
3365
Jamie Madill37997142015-01-28 10:06:34 -05003366bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3367{
3368 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3369 expression->traverse(&searchSymbol);
3370
3371 if (searchSymbol.foundMatch())
3372 {
3373 // Type already printed
3374 out << "t" + str(mUniqueIndex) + " = ";
3375 expression->traverse(this);
3376 out << ", ";
3377 symbolNode->traverse(this);
3378 out << " = t" + str(mUniqueIndex);
3379
3380 mUniqueIndex++;
3381 return true;
3382 }
3383
3384 return false;
3385}
3386
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003387bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3388{
3389 // We support writing constant unions and constructors that only take constant unions as
3390 // parameters as HLSL literals.
3391 if (expression->getAsConstantUnion())
3392 {
3393 return true;
3394 }
3395 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3396 !expression->getAsAggregate()->isConstructor())
3397 {
3398 return false;
3399 }
3400 TIntermAggregate *constructor = expression->getAsAggregate();
3401 for (TIntermNode *&node : *constructor->getSequence())
3402 {
3403 if (!node->getAsConstantUnion())
3404 return false;
3405 }
3406 return true;
3407}
3408
3409bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3410 TIntermSymbol *symbolNode,
3411 TIntermTyped *expression)
3412{
3413 if (canWriteAsHLSLLiteral(expression))
3414 {
3415 symbolNode->traverse(this);
3416 if (expression->getType().isArray())
3417 {
3418 out << "[" << expression->getType().getArraySize() << "]";
3419 }
3420 out << " = {";
3421 if (expression->getAsConstantUnion())
3422 {
3423 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3424 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3425 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3426 }
3427 else
3428 {
3429 TIntermAggregate *constructor = expression->getAsAggregate();
3430 ASSERT(constructor != nullptr);
3431 for (TIntermNode *&node : *constructor->getSequence())
3432 {
3433 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3434 ASSERT(nodeConst);
3435 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3436 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3437 if (node != constructor->getSequence()->back())
3438 {
3439 out << ", ";
3440 }
3441 }
3442 }
3443 out << "}";
3444 return true;
3445 }
3446 return false;
3447}
3448
Jamie Madill37997142015-01-28 10:06:34 -05003449void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3450{
3451 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3452 << "\n"
3453 << "void initializeDeferredGlobals()\n"
3454 << "{\n";
3455
3456 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3457 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003458 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3459 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3460 if (binary != nullptr)
3461 {
3462 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3463 TIntermTyped *expression = binary->getRight();
3464 ASSERT(symbol);
3465 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003466
Olli Etuahod81ed842015-05-12 12:46:35 +03003467 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003468
Olli Etuahod81ed842015-05-12 12:46:35 +03003469 if (!writeSameSymbolInitializer(out, symbol, expression))
3470 {
3471 ASSERT(mInfoSinkStack.top() == &out);
3472 expression->traverse(this);
3473 }
3474 out << ";\n";
3475 }
3476 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003477 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003478 writeSelection(out, selection);
Jamie Madill37997142015-01-28 10:06:34 -05003479 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003480 else
3481 {
3482 UNREACHABLE();
3483 }
Jamie Madill37997142015-01-28 10:06:34 -05003484 }
3485
3486 out << "}\n"
3487 << "\n";
3488}
3489
Jamie Madill55e79e02015-02-09 15:35:00 -05003490TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3491{
3492 const TFieldList &fields = structure.fields();
3493
3494 for (const auto &eqFunction : mStructEqualityFunctions)
3495 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003496 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003497 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003498 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003499 }
3500 }
3501
3502 const TString &structNameString = StructNameString(structure);
3503
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003504 StructEqualityFunction *function = new StructEqualityFunction();
3505 function->structure = &structure;
3506 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003507
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003508 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003509
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003510 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3511 << "{\n"
3512 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003513
3514 for (size_t i = 0; i < fields.size(); i++)
3515 {
3516 const TField *field = fields[i];
3517 const TType *fieldType = field->type();
3518
3519 const TString &fieldNameA = "a." + Decorate(field->name());
3520 const TString &fieldNameB = "b." + Decorate(field->name());
3521
3522 if (i > 0)
3523 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003524 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003525 }
3526
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003527 fnOut << "(";
3528 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3529 fnOut << fieldNameA;
3530 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3531 fnOut << fieldNameB;
3532 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3533 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003534 }
3535
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003536 fnOut << ";\n" << "}\n";
3537
3538 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003539
3540 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003541 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003542
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003543 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003544}
3545
Olli Etuaho7fb49552015-03-18 17:27:44 +02003546TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3547{
3548 for (const auto &eqFunction : mArrayEqualityFunctions)
3549 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003550 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003551 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003552 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003553 }
3554 }
3555
3556 const TString &typeName = TypeString(type);
3557
Olli Etuaho12690762015-03-31 12:55:28 +03003558 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003559 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003560
3561 TInfoSinkBase fnNameOut;
3562 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003563 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003564
3565 TType nonArrayType = type;
3566 nonArrayType.clearArrayness();
3567
3568 TInfoSinkBase fnOut;
3569
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003570 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003571 << typeName << " a[" << type.getArraySize() << "], "
3572 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003573 << "{\n"
3574 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3575 " {\n"
3576 " if (";
3577
3578 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3579 fnOut << "a[i]";
3580 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3581 fnOut << "b[i]";
3582 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3583
3584 fnOut << ") { return false; }\n"
3585 " }\n"
3586 " return true;\n"
3587 "}\n";
3588
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003589 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003590
3591 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003592 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003593
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003594 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003595}
3596
Olli Etuaho12690762015-03-31 12:55:28 +03003597TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3598{
3599 for (const auto &assignFunction : mArrayAssignmentFunctions)
3600 {
3601 if (assignFunction.type == type)
3602 {
3603 return assignFunction.functionName;
3604 }
3605 }
3606
3607 const TString &typeName = TypeString(type);
3608
3609 ArrayHelperFunction function;
3610 function.type = type;
3611
3612 TInfoSinkBase fnNameOut;
3613 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3614 function.functionName = fnNameOut.c_str();
3615
3616 TInfoSinkBase fnOut;
3617
3618 fnOut << "void " << function.functionName << "(out "
3619 << typeName << " a[" << type.getArraySize() << "], "
3620 << typeName << " b[" << type.getArraySize() << "])\n"
3621 << "{\n"
3622 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3623 " {\n"
3624 " a[i] = b[i];\n"
3625 " }\n"
3626 "}\n";
3627
3628 function.functionDefinition = fnOut.c_str();
3629
3630 mArrayAssignmentFunctions.push_back(function);
3631
3632 return function.functionName;
3633}
3634
Olli Etuaho9638c352015-04-01 14:34:52 +03003635TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3636{
3637 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3638 {
3639 if (constructIntoFunction.type == type)
3640 {
3641 return constructIntoFunction.functionName;
3642 }
3643 }
3644
3645 const TString &typeName = TypeString(type);
3646
3647 ArrayHelperFunction function;
3648 function.type = type;
3649
3650 TInfoSinkBase fnNameOut;
3651 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3652 function.functionName = fnNameOut.c_str();
3653
3654 TInfoSinkBase fnOut;
3655
3656 fnOut << "void " << function.functionName << "(out "
3657 << typeName << " a[" << type.getArraySize() << "]";
3658 for (int i = 0; i < type.getArraySize(); ++i)
3659 {
3660 fnOut << ", " << typeName << " b" << i;
3661 }
3662 fnOut << ")\n"
3663 "{\n";
3664
3665 for (int i = 0; i < type.getArraySize(); ++i)
3666 {
3667 fnOut << " a[" << i << "] = b" << i << ";\n";
3668 }
3669 fnOut << "}\n";
3670
3671 function.functionDefinition = fnOut.c_str();
3672
3673 mArrayConstructIntoFunctions.push_back(function);
3674
3675 return function.functionName;
3676}
3677
Jamie Madill2e295e22015-04-29 10:41:33 -04003678void OutputHLSL::ensureStructDefined(const TType &type)
3679{
3680 TStructure *structure = type.getStruct();
3681
3682 if (structure)
3683 {
3684 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3685 }
3686}
3687
3688
Olli Etuaho9638c352015-04-01 14:34:52 +03003689
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003690}