blob: dc43b11e446106491f0c5e6b5bd8db00a0905a73 [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;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400161 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000162 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500163 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400164 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530165 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000166
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000167 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000168
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000169 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000170 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400171 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000172
173 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000174
Jamie Madill8daaba12014-06-13 10:04:33 -0400175 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200176 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400177
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200178 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000179 {
Arun Patole63419392015-03-13 11:51:07 +0530180 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
181 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
182 // In both cases total 3 uniform registers need to be reserved.
183 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000184 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000185
Geoff Lang00140f42016-02-03 18:47:33 +0000186 // Reserve registers for the default uniform block and driver constants
187 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000190OutputHLSL::~OutputHLSL()
191{
Jamie Madill8daaba12014-06-13 10:04:33 -0400192 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400193 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200194 for (auto &eqFunction : mStructEqualityFunctions)
195 {
196 SafeDelete(eqFunction);
197 }
198 for (auto &eqFunction : mArrayEqualityFunctions)
199 {
200 SafeDelete(eqFunction);
201 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000202}
203
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200204void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000205{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200206 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400207 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000208
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200209 BuiltInFunctionEmulator builtInFunctionEmulator;
210 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200211 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500212
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700213 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700214 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
215 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300216 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700217 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700218
Jamie Madill37997142015-01-28 10:06:34 -0500219 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500220 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200221 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500222 mInfoSinkStack.pop();
223
Jamie Madill37997142015-01-28 10:06:34 -0500224 mInfoSinkStack.push(&mFooter);
225 if (!mDeferredGlobalInitializers.empty())
226 {
227 writeDeferredGlobalInitializers(mFooter);
228 }
229 mInfoSinkStack.pop();
230
Jamie Madill32aab012015-01-27 14:12:26 -0500231 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500232 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500233 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000234
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200235 objSink << mHeader.c_str();
236 objSink << mBody.c_str();
237 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200238
239 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000240}
241
Jamie Madill570e04d2013-06-21 09:15:33 -0400242void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
243{
244 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
245 {
246 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
247
Jamie Madill32aab012015-01-27 14:12:26 -0500248 TInfoSinkBase structInfoSink;
249 mInfoSinkStack.push(&structInfoSink);
250
Jamie Madill570e04d2013-06-21 09:15:33 -0400251 // This will mark the necessary block elements as referenced
252 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500253
254 TString structName(structInfoSink.c_str());
255 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400256
257 mFlaggedStructOriginalNames[flaggedNode] = structName;
258
259 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
260 {
261 structName.erase(pos, 1);
262 }
263
264 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
265 }
266}
267
Jamie Madill4e1fd412014-07-10 17:50:10 -0400268const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
269{
270 return mUniformHLSL->getInterfaceBlockRegisterMap();
271}
272
Jamie Madill9fe25e92014-07-18 10:33:08 -0400273const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
274{
275 return mUniformHLSL->getUniformRegisterMap();
276}
277
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000278int OutputHLSL::vectorSize(const TType &type) const
279{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000280 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000281 int arraySize = type.isArray() ? type.getArraySize() : 1;
282
283 return elementSize * arraySize;
284}
285
Jamie Madill98493dd2013-07-08 14:39:03 -0400286TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400287{
288 TString init;
289
290 TString preIndentString;
291 TString fullIndentString;
292
293 for (int spaces = 0; spaces < (indent * 4); spaces++)
294 {
295 preIndentString += ' ';
296 }
297
298 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
299 {
300 fullIndentString += ' ';
301 }
302
303 init += preIndentString + "{\n";
304
Jamie Madill98493dd2013-07-08 14:39:03 -0400305 const TFieldList &fields = structure.fields();
306 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400308 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400309 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400310 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400311
Jamie Madill98493dd2013-07-08 14:39:03 -0400312 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400313 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400314 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400315 }
316 else
317 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400318 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400319 }
320 }
321
322 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
323
324 return init;
325}
326
Jamie Madill8c46ab12015-12-07 16:39:19 -0500327void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000329 TString varyings;
330 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400331 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000332
Jamie Madill829f59e2013-11-13 19:40:54 -0500333 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400334 {
335 TIntermTyped *structNode = flaggedStructIt->first;
336 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400337 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400338 const TString &originalName = mFlaggedStructOriginalNames[structNode];
339
Jamie Madill033dae62014-06-18 12:56:28 -0400340 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400341 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400342 flaggedStructs += "\n";
343 }
344
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000345 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
346 {
347 const TType &type = varying->second->getType();
348 const TString &name = varying->second->getSymbol();
349
350 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400351 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
352 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000353 }
354
355 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
356 {
357 const TType &type = attribute->second->getType();
358 const TString &name = attribute->second->getSymbol();
359
Jamie Madill033dae62014-06-18 12:56:28 -0400360 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000361 }
362
Jamie Madill8daaba12014-06-13 10:04:33 -0400363 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400364
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200365 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400366 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
367
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200368 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500369 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200370 out << "\n// Equality functions\n\n";
371 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500372 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200373 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200374 }
375 }
Olli Etuaho12690762015-03-31 12:55:28 +0300376 if (!mArrayAssignmentFunctions.empty())
377 {
378 out << "\n// Assignment functions\n\n";
379 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
380 {
381 out << assignmentFunction.functionDefinition << "\n";
382 }
383 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300384 if (!mArrayConstructIntoFunctions.empty())
385 {
386 out << "\n// Array constructor functions\n\n";
387 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
388 {
389 out << constructIntoFunction.functionDefinition << "\n";
390 }
391 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200392
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500393 if (mUsesDiscardRewriting)
394 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400395 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500396 }
397
Nicolas Capens655fe362014-04-11 13:12:34 -0400398 if (mUsesNestedBreak)
399 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400400 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400401 }
402
Arun Patole44efa0b2015-03-04 17:11:05 +0530403 if (mRequiresIEEEStrictCompiling)
404 {
405 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
406 }
407
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400408 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
409 "#define LOOP [loop]\n"
410 "#define FLATTEN [flatten]\n"
411 "#else\n"
412 "#define LOOP\n"
413 "#define FLATTEN\n"
414 "#endif\n";
415
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200416 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200418 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
419 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000420
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000421 out << "// Varyings\n";
422 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400423 out << "\n";
424
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200425 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000426 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500427 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000428 {
Jamie Madill46131a32013-06-20 11:55:50 -0400429 const TString &variableName = outputVariableIt->first;
430 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400431
Jamie Madill033dae62014-06-18 12:56:28 -0400432 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400433 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000434 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000435 }
Jamie Madill46131a32013-06-20 11:55:50 -0400436 else
437 {
438 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
439
440 out << "static float4 gl_Color[" << numColorValues << "] =\n"
441 "{\n";
442 for (unsigned int i = 0; i < numColorValues; i++)
443 {
444 out << " float4(0, 0, 0, 0)";
445 if (i + 1 != numColorValues)
446 {
447 out << ",";
448 }
449 out << "\n";
450 }
451
452 out << "};\n";
453 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000454
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400455 if (mUsesFragDepth)
456 {
457 out << "static float gl_Depth = 0.0;\n";
458 }
459
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000460 if (mUsesFragCoord)
461 {
462 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
463 }
464
465 if (mUsesPointCoord)
466 {
467 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
468 }
469
470 if (mUsesFrontFacing)
471 {
472 out << "static bool gl_FrontFacing = false;\n";
473 }
474
475 out << "\n";
476
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000477 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000478 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000479 out << "struct gl_DepthRangeParameters\n"
480 "{\n"
481 " float near;\n"
482 " float far;\n"
483 " float diff;\n"
484 "};\n"
485 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000486 }
487
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200488 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000489 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000490 out << "cbuffer DriverConstants : register(b1)\n"
491 "{\n";
492
493 if (mUsesDepthRange)
494 {
495 out << " float3 dx_DepthRange : packoffset(c0);\n";
496 }
497
498 if (mUsesFragCoord)
499 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000500 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000501 }
502
503 if (mUsesFragCoord || mUsesFrontFacing)
504 {
505 out << " float3 dx_DepthFront : packoffset(c2);\n";
506 }
507
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800508 if (mUsesFragCoord)
509 {
510 // dx_ViewScale is only used in the fragment shader to correct
511 // the value for glFragCoord if necessary
512 out << " float2 dx_ViewScale : packoffset(c3);\n";
513 }
514
Olli Etuaho618bebc2016-01-15 16:40:00 +0200515 if (mOutputType == SH_HLSL_4_1_OUTPUT)
516 {
517 mUniformHLSL->samplerMetadataUniforms(out, "c4");
518 }
519
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000520 out << "};\n";
521 }
522 else
523 {
524 if (mUsesDepthRange)
525 {
526 out << "uniform float3 dx_DepthRange : register(c0);";
527 }
528
529 if (mUsesFragCoord)
530 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000531 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000532 }
533
534 if (mUsesFragCoord || mUsesFrontFacing)
535 {
536 out << "uniform float3 dx_DepthFront : register(c2);\n";
537 }
538 }
539
540 out << "\n";
541
542 if (mUsesDepthRange)
543 {
544 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
545 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000546 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000547
Jamie Madillf91ce812014-06-13 10:04:34 -0400548 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000549 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400550 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000551 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400552 out << flaggedStructs;
553 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000554 }
555
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000556 if (usingMRTExtension && mNumRenderTargets > 1)
557 {
558 out << "#define GL_USES_MRT\n";
559 }
560
561 if (mUsesFragColor)
562 {
563 out << "#define GL_USES_FRAG_COLOR\n";
564 }
565
566 if (mUsesFragData)
567 {
568 out << "#define GL_USES_FRAG_DATA\n";
569 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000571 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000573 out << "// Attributes\n";
574 out << attributes;
575 out << "\n"
576 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400577
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000578 if (mUsesPointSize)
579 {
580 out << "static float gl_PointSize = float(1);\n";
581 }
582
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000583 if (mUsesInstanceID)
584 {
585 out << "static int gl_InstanceID;";
586 }
587
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000588 out << "\n"
589 "// Varyings\n";
590 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000591 out << "\n";
592
593 if (mUsesDepthRange)
594 {
595 out << "struct gl_DepthRangeParameters\n"
596 "{\n"
597 " float near;\n"
598 " float far;\n"
599 " float diff;\n"
600 "};\n"
601 "\n";
602 }
603
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200604 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000605 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800606 out << "cbuffer DriverConstants : register(b1)\n"
607 "{\n";
608
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000609 if (mUsesDepthRange)
610 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800611 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000612 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800613
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800614 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
615 // shaders. However, we declare it for all shaders (including Feature Level 10+).
616 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
617 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800618 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800619 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800620 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800621
Olli Etuaho618bebc2016-01-15 16:40:00 +0200622 if (mOutputType == SH_HLSL_4_1_OUTPUT)
623 {
624 mUniformHLSL->samplerMetadataUniforms(out, "c4");
625 }
626
Austin Kinross4fd18b12014-12-22 12:32:05 -0800627 out << "};\n"
628 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000629 }
630 else
631 {
632 if (mUsesDepthRange)
633 {
634 out << "uniform float3 dx_DepthRange : register(c0);\n";
635 }
636
Cooper Partine6664f02015-01-09 16:22:24 -0800637 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
638 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000639 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000640 }
641
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000642 if (mUsesDepthRange)
643 {
644 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
645 "\n";
646 }
647
Jamie Madillf91ce812014-06-13 10:04:34 -0400648 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000649 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400650 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000651 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400652 out << flaggedStructs;
653 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000654 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400655 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000656
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400657 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
658 {
659 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400660 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000661 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400662 switch(textureFunction->sampler)
663 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400664 case EbtSampler2D: out << "int2 "; break;
665 case EbtSampler3D: out << "int3 "; break;
666 case EbtSamplerCube: out << "int2 "; break;
667 case EbtSampler2DArray: out << "int3 "; break;
668 case EbtISampler2D: out << "int2 "; break;
669 case EbtISampler3D: out << "int3 "; break;
670 case EbtISamplerCube: out << "int2 "; break;
671 case EbtISampler2DArray: out << "int3 "; break;
672 case EbtUSampler2D: out << "int2 "; break;
673 case EbtUSampler3D: out << "int3 "; break;
674 case EbtUSamplerCube: out << "int2 "; break;
675 case EbtUSampler2DArray: out << "int3 "; break;
676 case EbtSampler2DShadow: out << "int2 "; break;
677 case EbtSamplerCubeShadow: out << "int2 "; break;
678 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400679 default: UNREACHABLE();
680 }
681 }
682 else // Sampling function
683 {
684 switch(textureFunction->sampler)
685 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400686 case EbtSampler2D: out << "float4 "; break;
687 case EbtSampler3D: out << "float4 "; break;
688 case EbtSamplerCube: out << "float4 "; break;
689 case EbtSampler2DArray: out << "float4 "; break;
690 case EbtISampler2D: out << "int4 "; break;
691 case EbtISampler3D: out << "int4 "; break;
692 case EbtISamplerCube: out << "int4 "; break;
693 case EbtISampler2DArray: out << "int4 "; break;
694 case EbtUSampler2D: out << "uint4 "; break;
695 case EbtUSampler3D: out << "uint4 "; break;
696 case EbtUSamplerCube: out << "uint4 "; break;
697 case EbtUSampler2DArray: out << "uint4 "; break;
698 case EbtSampler2DShadow: out << "float "; break;
699 case EbtSamplerCubeShadow: out << "float "; break;
700 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400701 default: UNREACHABLE();
702 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000703 }
704
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400705 // Function name
706 out << textureFunction->name();
707
708 // Argument list
709 int hlslCoords = 4;
710
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200711 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000712 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400713 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000714 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400715 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
716 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
717 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000718 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400719
Nicolas Capens75fb4752013-07-10 15:14:47 -0400720 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000721 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400722 case TextureFunction::IMPLICIT: break;
723 case TextureFunction::BIAS: hlslCoords = 4; break;
724 case TextureFunction::LOD: hlslCoords = 4; break;
725 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400726 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400727 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000728 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400729 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200730 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400731 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200732 hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler);
733 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400734 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200735 out << TextureString(textureFunction->sampler) << " x, "
736 << SamplerString(textureFunction->sampler) << " s";
737 }
738 else
739 {
740 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
741 out << "const uint samplerIndex";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400742 }
743 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400744
Nicolas Capensfc014542014-02-18 14:47:13 -0500745 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400746 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500747 switch(textureFunction->coords)
748 {
749 case 2: out << ", int2 t"; break;
750 case 3: out << ", int3 t"; break;
751 default: UNREACHABLE();
752 }
753 }
754 else // Floating-point coordinates (except textureSize)
755 {
756 switch(textureFunction->coords)
757 {
758 case 1: out << ", int lod"; break; // textureSize()
759 case 2: out << ", float2 t"; break;
760 case 3: out << ", float3 t"; break;
761 case 4: out << ", float4 t"; break;
762 default: UNREACHABLE();
763 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000764 }
765
Nicolas Capensd11d5492014-02-19 17:06:10 -0500766 if (textureFunction->method == TextureFunction::GRAD)
767 {
768 switch(textureFunction->sampler)
769 {
770 case EbtSampler2D:
771 case EbtISampler2D:
772 case EbtUSampler2D:
773 case EbtSampler2DArray:
774 case EbtISampler2DArray:
775 case EbtUSampler2DArray:
776 case EbtSampler2DShadow:
777 case EbtSampler2DArrayShadow:
778 out << ", float2 ddx, float2 ddy";
779 break;
780 case EbtSampler3D:
781 case EbtISampler3D:
782 case EbtUSampler3D:
783 case EbtSamplerCube:
784 case EbtISamplerCube:
785 case EbtUSamplerCube:
786 case EbtSamplerCubeShadow:
787 out << ", float3 ddx, float3 ddy";
788 break;
789 default: UNREACHABLE();
790 }
791 }
792
Nicolas Capens75fb4752013-07-10 15:14:47 -0400793 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000794 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400795 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400796 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400797 case TextureFunction::LOD: out << ", float lod"; break;
798 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400799 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400800 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500801 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500802 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400803 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000804 }
805
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500806 if (textureFunction->offset)
807 {
808 switch(textureFunction->sampler)
809 {
810 case EbtSampler2D: out << ", int2 offset"; break;
811 case EbtSampler3D: out << ", int3 offset"; break;
812 case EbtSampler2DArray: out << ", int2 offset"; break;
813 case EbtISampler2D: out << ", int2 offset"; break;
814 case EbtISampler3D: out << ", int3 offset"; break;
815 case EbtISampler2DArray: out << ", int2 offset"; break;
816 case EbtUSampler2D: out << ", int2 offset"; break;
817 case EbtUSampler3D: out << ", int3 offset"; break;
818 case EbtUSampler2DArray: out << ", int2 offset"; break;
819 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500820 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500821 default: UNREACHABLE();
822 }
823 }
824
Nicolas Capens84cfa122014-04-14 13:48:45 -0400825 if (textureFunction->method == TextureFunction::BIAS ||
826 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500827 {
828 out << ", float bias";
829 }
830
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400831 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400832 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400833
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200834 // In some cases we use a variable to store the texture/sampler objects, but to work around
835 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
836 // sampling we need to call the function directly on a reference to the array. The bug was
837 // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests.
838 TString textureReference("x");
839 TString samplerReference("s");
840 if (mOutputType == SH_HLSL_4_1_OUTPUT)
841 {
842 TString suffix = TextureGroupSuffix(textureFunction->sampler);
843 if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D)
844 {
845 textureReference = TString("textures") + suffix + "[samplerIndex]";
846 samplerReference = TString("samplers") + suffix + "[samplerIndex]";
847 }
848 else
849 {
850 out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix
851 << ";\n";
852 textureReference = TString("textures") + suffix + "[textureIndex]";
853 out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset"
854 << suffix << ";\n";
855 samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]";
856 }
857 }
858
Nicolas Capens75fb4752013-07-10 15:14:47 -0400859 if (textureFunction->method == TextureFunction::SIZE)
860 {
Olli Etuahof8bf5832016-02-11 16:21:49 +0200861 out << "int baseLevel = samplerMetadata[samplerIndex].x;\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400862 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
863 {
Olli Etuahobce743a2016-01-15 17:18:28 +0200864 if (IsSamplerArray(textureFunction->sampler) ||
865 (IsIntegerSampler(textureFunction->sampler) &&
866 IsSamplerCube(textureFunction->sampler)))
Nicolas Capens75fb4752013-07-10 15:14:47 -0400867 {
868 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
Olli Etuahobce743a2016-01-15 17:18:28 +0200869 << " " << textureReference << ".GetDimensions(baseLevel + lod, width, "
870 "height, layers, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400871 }
872 else
873 {
874 out << " uint width; uint height; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200875 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200876 << ".GetDimensions(baseLevel + lod, width, height, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400877 }
878 }
879 else if (IsSampler3D(textureFunction->sampler))
880 {
881 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200882 << " " << textureReference
Olli Etuahobce743a2016-01-15 17:18:28 +0200883 << ".GetDimensions(baseLevel + lod, width, height, depth, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400884 }
885 else UNREACHABLE();
886
887 switch(textureFunction->sampler)
888 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400889 case EbtSampler2D: out << " return int2(width, height);"; break;
890 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
891 case EbtSamplerCube: out << " return int2(width, height);"; break;
892 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
893 case EbtISampler2D: out << " return int2(width, height);"; break;
894 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
895 case EbtISamplerCube: out << " return int2(width, height);"; break;
896 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
897 case EbtUSampler2D: out << " return int2(width, height);"; break;
898 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
899 case EbtUSamplerCube: out << " return int2(width, height);"; break;
900 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
901 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
902 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
903 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400904 default: UNREACHABLE();
905 }
906 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400907 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400908 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500909 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
910 {
911 out << " float width; float height; float layers; float levels;\n";
912
913 out << " uint mip = 0;\n";
914
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200915 out << " " << textureReference
916 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500917
918 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
919 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
920 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
921 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
922
923 // FACE_POSITIVE_X = 000b
924 // FACE_NEGATIVE_X = 001b
925 // FACE_POSITIVE_Y = 010b
926 // FACE_NEGATIVE_Y = 011b
927 // FACE_POSITIVE_Z = 100b
928 // FACE_NEGATIVE_Z = 101b
929 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
930
931 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
932 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
933 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
934
935 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
936 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500937
938 // Mip level computation.
939 if (textureFunction->method == TextureFunction::IMPLICIT)
940 {
941 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
942 " float2 dx = ddx(tSized);\n"
943 " float2 dy = ddy(tSized);\n"
944 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"
945 " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200946 << " " << textureReference
947 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500948 }
Nicolas Capens0027fa92014-02-20 14:26:42 -0500949 }
950 else if (IsIntegerSampler(textureFunction->sampler) &&
951 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400952 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400953 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400954 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400955 if (IsSamplerArray(textureFunction->sampler))
956 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400957 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400958
Nicolas Capens9edebd62013-08-06 10:59:10 -0400959 if (textureFunction->method == TextureFunction::LOD0)
960 {
961 out << " uint mip = 0;\n";
962 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400963 else if (textureFunction->method == TextureFunction::LOD0BIAS)
964 {
965 out << " uint mip = bias;\n";
966 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400967 else
968 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200969
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200970 out << " " << textureReference
971 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400972 if (textureFunction->method == TextureFunction::IMPLICIT ||
973 textureFunction->method == TextureFunction::BIAS)
974 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200975 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400976 " float dx = length(ddx(tSized));\n"
977 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500978 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400979
980 if (textureFunction->method == TextureFunction::BIAS)
981 {
982 out << " lod += bias;\n";
983 }
984 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500985 else if (textureFunction->method == TextureFunction::GRAD)
986 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200987 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500988 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400989
990 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
991 }
992
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200993 out << " " << textureReference
994 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400995 }
996 else
997 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400998 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400999
Nicolas Capens9edebd62013-08-06 10:59:10 -04001000 if (textureFunction->method == TextureFunction::LOD0)
1001 {
1002 out << " uint mip = 0;\n";
1003 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001004 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1005 {
1006 out << " uint mip = bias;\n";
1007 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001008 else
1009 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001010 out << " " << textureReference
1011 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001012
Nicolas Capens9edebd62013-08-06 10:59:10 -04001013 if (textureFunction->method == TextureFunction::IMPLICIT ||
1014 textureFunction->method == TextureFunction::BIAS)
1015 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001016 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001017 " float dx = length(ddx(tSized));\n"
1018 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001019 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001020
1021 if (textureFunction->method == TextureFunction::BIAS)
1022 {
1023 out << " lod += bias;\n";
1024 }
1025 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001026 else if (textureFunction->method == TextureFunction::GRAD)
1027 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001028 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001029 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001030
1031 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1032 }
1033
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001034 out << " " << textureReference
1035 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001036 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001037 }
1038 else if (IsSampler3D(textureFunction->sampler))
1039 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001040 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001041
Nicolas Capens9edebd62013-08-06 10:59:10 -04001042 if (textureFunction->method == TextureFunction::LOD0)
1043 {
1044 out << " uint mip = 0;\n";
1045 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001046 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1047 {
1048 out << " uint mip = bias;\n";
1049 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001050 else
1051 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001052 out << " " << textureReference
1053 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001054
Nicolas Capens9edebd62013-08-06 10:59:10 -04001055 if (textureFunction->method == TextureFunction::IMPLICIT ||
1056 textureFunction->method == TextureFunction::BIAS)
1057 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001058 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1059 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001060 " float dx = length(ddx(tSized));\n"
1061 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001062 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001063
1064 if (textureFunction->method == TextureFunction::BIAS)
1065 {
1066 out << " lod += bias;\n";
1067 }
1068 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001069 else if (textureFunction->method == TextureFunction::GRAD)
1070 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001071 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001072 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001073
1074 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1075 }
1076
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001077 out << " " << textureReference
1078 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001079 }
1080 else UNREACHABLE();
1081 }
1082
1083 out << " return ";
1084
1085 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001086 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001087 {
1088 switch(textureFunction->sampler)
1089 {
1090 case EbtSampler2D: out << "tex2D"; break;
1091 case EbtSamplerCube: out << "texCUBE"; break;
1092 default: UNREACHABLE();
1093 }
1094
Nicolas Capens75fb4752013-07-10 15:14:47 -04001095 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001096 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001097 case TextureFunction::IMPLICIT:
1098 out << "(" << samplerReference << ", ";
1099 break;
1100 case TextureFunction::BIAS:
1101 out << "bias(" << samplerReference << ", ";
1102 break;
1103 case TextureFunction::LOD:
1104 out << "lod(" << samplerReference << ", ";
1105 break;
1106 case TextureFunction::LOD0:
1107 out << "lod(" << samplerReference << ", ";
1108 break;
1109 case TextureFunction::LOD0BIAS:
1110 out << "lod(" << samplerReference << ", ";
1111 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001112 default: UNREACHABLE();
1113 }
1114 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001115 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001116 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001117 if (textureFunction->method == TextureFunction::GRAD)
1118 {
1119 if (IsIntegerSampler(textureFunction->sampler))
1120 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001121 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001122 }
1123 else if (IsShadowSampler(textureFunction->sampler))
1124 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001125 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1126 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001127 }
1128 else
1129 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001130 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001131 }
1132 }
1133 else if (IsIntegerSampler(textureFunction->sampler) ||
1134 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001135 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001136 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001137 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001138 else if (IsShadowSampler(textureFunction->sampler))
1139 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001140 switch(textureFunction->method)
1141 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001142 case TextureFunction::IMPLICIT:
1143 out << "" << textureReference << ".SampleCmp(" << samplerReference
1144 << ", ";
1145 break;
1146 case TextureFunction::BIAS:
1147 out << "" << textureReference << ".SampleCmp(" << samplerReference
1148 << ", ";
1149 break;
1150 case TextureFunction::LOD:
1151 out << "" << textureReference << ".SampleCmp(" << samplerReference
1152 << ", ";
1153 break;
1154 case TextureFunction::LOD0:
1155 out << "" << textureReference << ".SampleCmpLevelZero("
1156 << samplerReference << ", ";
1157 break;
1158 case TextureFunction::LOD0BIAS:
1159 out << "" << textureReference << ".SampleCmpLevelZero("
1160 << samplerReference << ", ";
1161 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001162 default: UNREACHABLE();
1163 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001164 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001165 else
1166 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001167 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001168 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001169 case TextureFunction::IMPLICIT:
1170 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1171 break;
1172 case TextureFunction::BIAS:
1173 out << "" << textureReference << ".SampleBias(" << samplerReference
1174 << ", ";
1175 break;
1176 case TextureFunction::LOD:
1177 out << "" << textureReference << ".SampleLevel(" << samplerReference
1178 << ", ";
1179 break;
1180 case TextureFunction::LOD0:
1181 out << "" << textureReference << ".SampleLevel(" << samplerReference
1182 << ", ";
1183 break;
1184 case TextureFunction::LOD0BIAS:
1185 out << "" << textureReference << ".SampleLevel(" << samplerReference
1186 << ", ";
1187 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001188 default: UNREACHABLE();
1189 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001190 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001191 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001192 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001193
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001194 // Integer sampling requires integer addresses
1195 TString addressx = "";
1196 TString addressy = "";
1197 TString addressz = "";
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001198 TString close = "";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001199
Nicolas Capensfc014542014-02-18 14:47:13 -05001200 if (IsIntegerSampler(textureFunction->sampler) ||
1201 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001202 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001203 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001204 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001205 case 2: out << "int3("; break;
1206 case 3: out << "int4("; break;
1207 default: UNREACHABLE();
1208 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001209
Nicolas Capensfc014542014-02-18 14:47:13 -05001210 // Convert from normalized floating-point to integer
1211 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001212 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001213 addressx = "int(floor(width * frac((";
1214 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001215
Nicolas Capensfc014542014-02-18 14:47:13 -05001216 if (IsSamplerArray(textureFunction->sampler))
1217 {
1218 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1219 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001220 else if (IsSamplerCube(textureFunction->sampler))
Nicolas Capens0027fa92014-02-20 14:26:42 -05001221 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001222 addressz = "((((";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001223 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001224 else
1225 {
1226 addressz = "int(floor(depth * frac((";
1227 }
1228
1229 close = "))))";
Nicolas Capensfc014542014-02-18 14:47:13 -05001230 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001231 }
1232 else
1233 {
1234 switch(hlslCoords)
1235 {
1236 case 2: out << "float2("; break;
1237 case 3: out << "float3("; break;
1238 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001239 default: UNREACHABLE();
1240 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001241 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001242
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001243 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001244
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001245 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001246 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001247 switch(textureFunction->coords)
1248 {
1249 case 3: proj = " / t.z"; break;
1250 case 4: proj = " / t.w"; break;
1251 default: UNREACHABLE();
1252 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001253 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001254
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001255 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001256
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001257 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001258 {
1259 if (hlslCoords >= 3)
1260 {
1261 if (textureFunction->coords < 3)
1262 {
1263 out << ", 0";
1264 }
1265 else
1266 {
1267 out << ", t.z" + proj;
1268 }
1269 }
1270
1271 if (hlslCoords == 4)
1272 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001273 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001274 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001275 case TextureFunction::BIAS: out << ", bias"; break;
1276 case TextureFunction::LOD: out << ", lod"; break;
1277 case TextureFunction::LOD0: out << ", 0"; break;
1278 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001279 default: UNREACHABLE();
1280 }
1281 }
1282
1283 out << "));\n";
1284 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001285 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001286 {
1287 if (hlslCoords >= 3)
1288 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001289 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1290 {
1291 out << ", face";
1292 }
1293 else
1294 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001295 out << ", " + addressz + ("t.z" + proj) + close;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001296 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001297 }
1298
Nicolas Capensd11d5492014-02-19 17:06:10 -05001299 if (textureFunction->method == TextureFunction::GRAD)
1300 {
1301 if (IsIntegerSampler(textureFunction->sampler))
1302 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001303 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001304 }
1305 else if (IsShadowSampler(textureFunction->sampler))
1306 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001307 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001308 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001309 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001310 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1311 // The resulting third component of P' in the shadow forms is used as Dref
1312 out << "), t.z" << proj;
1313 }
1314 else
1315 {
1316 switch(textureFunction->coords)
1317 {
1318 case 3: out << "), t.z"; break;
1319 case 4: out << "), t.w"; break;
1320 default: UNREACHABLE();
1321 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001322 }
1323 }
1324 else
1325 {
1326 out << "), ddx, ddy";
1327 }
1328 }
1329 else if (IsIntegerSampler(textureFunction->sampler) ||
1330 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001331 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001332 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001333 }
1334 else if (IsShadowSampler(textureFunction->sampler))
1335 {
1336 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001337 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001338 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001339 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1340 // The resulting third component of P' in the shadow forms is used as Dref
1341 out << "), t.z" << proj;
1342 }
1343 else
1344 {
1345 switch(textureFunction->coords)
1346 {
1347 case 3: out << "), t.z"; break;
1348 case 4: out << "), t.w"; break;
1349 default: UNREACHABLE();
1350 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001351 }
1352 }
1353 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001354 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001355 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001356 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001357 case TextureFunction::IMPLICIT: out << ")"; break;
1358 case TextureFunction::BIAS: out << "), bias"; break;
1359 case TextureFunction::LOD: out << "), lod"; break;
1360 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001361 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001362 default: UNREACHABLE();
1363 }
1364 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001365
1366 if (textureFunction->offset)
1367 {
1368 out << ", offset";
1369 }
1370
1371 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001372 }
1373 else UNREACHABLE();
1374 }
1375
1376 out << "\n"
1377 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001378 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001379 }
1380
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001381 if (mUsesFragCoord)
1382 {
1383 out << "#define GL_USES_FRAG_COORD\n";
1384 }
1385
1386 if (mUsesPointCoord)
1387 {
1388 out << "#define GL_USES_POINT_COORD\n";
1389 }
1390
1391 if (mUsesFrontFacing)
1392 {
1393 out << "#define GL_USES_FRONT_FACING\n";
1394 }
1395
1396 if (mUsesPointSize)
1397 {
1398 out << "#define GL_USES_POINT_SIZE\n";
1399 }
1400
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001401 if (mUsesFragDepth)
1402 {
1403 out << "#define GL_USES_FRAG_DEPTH\n";
1404 }
1405
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001406 if (mUsesDepthRange)
1407 {
1408 out << "#define GL_USES_DEPTH_RANGE\n";
1409 }
1410
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001411 if (mUsesXor)
1412 {
1413 out << "bool xor(bool p, bool q)\n"
1414 "{\n"
1415 " return (p || q) && !(p && q);\n"
1416 "}\n"
1417 "\n";
1418 }
1419
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001420 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001421}
1422
1423void OutputHLSL::visitSymbol(TIntermSymbol *node)
1424{
Jamie Madill32aab012015-01-27 14:12:26 -05001425 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001426
Jamie Madill570e04d2013-06-21 09:15:33 -04001427 // Handle accessing std140 structs by value
1428 if (mFlaggedStructMappedNames.count(node) > 0)
1429 {
1430 out << mFlaggedStructMappedNames[node];
1431 return;
1432 }
1433
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001434 TString name = node->getSymbol();
1435
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001436 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001437 {
1438 mUsesDepthRange = true;
1439 out << name;
1440 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001441 else
1442 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001443 TQualifier qualifier = node->getQualifier();
1444
1445 if (qualifier == EvqUniform)
1446 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001447 const TType &nodeType = node->getType();
1448 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001449
1450 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001451 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001452 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001453 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001454 else
1455 {
1456 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001457 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001458
Jamie Madill2e295e22015-04-29 10:41:33 -04001459 ensureStructDefined(nodeType);
1460
Jamie Madill033dae62014-06-18 12:56:28 -04001461 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001462 }
Jamie Madill19571812013-08-12 15:26:34 -07001463 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001464 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001465 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001466 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001467 }
Jamie Madill033dae62014-06-18 12:56:28 -04001468 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001469 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001470 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001471 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001472 }
Jamie Madill19571812013-08-12 15:26:34 -07001473 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001474 {
1475 mReferencedOutputVariables[name] = node;
1476 out << "out_" << name;
1477 }
1478 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001479 {
1480 out << "gl_Color[0]";
1481 mUsesFragColor = true;
1482 }
1483 else if (qualifier == EvqFragData)
1484 {
1485 out << "gl_Color";
1486 mUsesFragData = true;
1487 }
1488 else if (qualifier == EvqFragCoord)
1489 {
1490 mUsesFragCoord = true;
1491 out << name;
1492 }
1493 else if (qualifier == EvqPointCoord)
1494 {
1495 mUsesPointCoord = true;
1496 out << name;
1497 }
1498 else if (qualifier == EvqFrontFacing)
1499 {
1500 mUsesFrontFacing = true;
1501 out << name;
1502 }
1503 else if (qualifier == EvqPointSize)
1504 {
1505 mUsesPointSize = true;
1506 out << name;
1507 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001508 else if (qualifier == EvqInstanceID)
1509 {
1510 mUsesInstanceID = true;
1511 out << name;
1512 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001513 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001514 {
1515 mUsesFragDepth = true;
1516 out << "gl_Depth";
1517 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001518 else
1519 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001520 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001521 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001522 }
1523}
1524
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001525void OutputHLSL::visitRaw(TIntermRaw *node)
1526{
Jamie Madill32aab012015-01-27 14:12:26 -05001527 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001528}
1529
Olli Etuaho7fb49552015-03-18 17:27:44 +02001530void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1531{
1532 if (type.isScalar() && !type.isArray())
1533 {
1534 if (op == EOpEqual)
1535 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001536 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001537 }
1538 else
1539 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001540 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001541 }
1542 }
1543 else
1544 {
1545 if (visit == PreVisit && op == EOpNotEqual)
1546 {
1547 out << "!";
1548 }
1549
1550 if (type.isArray())
1551 {
1552 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001553 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001554 }
1555 else if (type.getBasicType() == EbtStruct)
1556 {
1557 const TStructure &structure = *type.getStruct();
1558 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001559 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001560 }
1561 else
1562 {
1563 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001564 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001565 }
1566 }
1567}
1568
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001569bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1570{
Jamie Madill32aab012015-01-27 14:12:26 -05001571 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001572
Jamie Madill570e04d2013-06-21 09:15:33 -04001573 // Handle accessing std140 structs by value
1574 if (mFlaggedStructMappedNames.count(node) > 0)
1575 {
1576 out << mFlaggedStructMappedNames[node];
1577 return false;
1578 }
1579
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001580 switch (node->getOp())
1581 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001582 case EOpAssign:
1583 if (node->getLeft()->isArray())
1584 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001585 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1586 if (rightAgg != nullptr && rightAgg->isConstructor())
1587 {
1588 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1589 out << functionName << "(";
1590 node->getLeft()->traverse(this);
1591 TIntermSequence *seq = rightAgg->getSequence();
1592 for (auto &arrayElement : *seq)
1593 {
1594 out << ", ";
1595 arrayElement->traverse(this);
1596 }
1597 out << ")";
1598 return false;
1599 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001600 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1601 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1602
1603 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001604 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001605 }
1606 else
1607 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001608 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001609 }
1610 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001611 case EOpInitialize:
1612 if (visit == PreVisit)
1613 {
1614 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1615 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1616 // new variable is created before the assignment is evaluated), so we need to convert
1617 // this to "float t = x, x = t;".
1618
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001619 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001620 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001621 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001622
Jamie Madill37997142015-01-28 10:06:34 -05001623 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1624 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001625 {
Jamie Madill37997142015-01-28 10:06:34 -05001626 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001627 // after we initialize uniforms.
1628 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1629 deferredInit->setLeft(node->getLeft());
1630 deferredInit->setRight(node->getRight());
1631 deferredInit->setType(node->getType());
1632 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001633 const TString &initString = initializer(node->getType());
1634 node->setRight(new TIntermRaw(node->getType(), initString));
1635 }
1636 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1637 {
1638 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001639 return false;
1640 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001641 else if (writeConstantInitialization(out, symbolNode, expression))
1642 {
1643 return false;
1644 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001645 }
1646 else if (visit == InVisit)
1647 {
1648 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001649 }
1650 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001651 case EOpAddAssign:
1652 outputTriplet(out, visit, "(", " += ", ")");
1653 break;
1654 case EOpSubAssign:
1655 outputTriplet(out, visit, "(", " -= ", ")");
1656 break;
1657 case EOpMulAssign:
1658 outputTriplet(out, visit, "(", " *= ", ")");
1659 break;
1660 case EOpVectorTimesScalarAssign:
1661 outputTriplet(out, visit, "(", " *= ", ")");
1662 break;
1663 case EOpMatrixTimesScalarAssign:
1664 outputTriplet(out, visit, "(", " *= ", ")");
1665 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001666 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001667 if (visit == PreVisit)
1668 {
1669 out << "(";
1670 }
1671 else if (visit == InVisit)
1672 {
1673 out << " = mul(";
1674 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001675 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001676 }
1677 else
1678 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001679 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001680 }
1681 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001682 case EOpMatrixTimesMatrixAssign:
1683 if (visit == PreVisit)
1684 {
1685 out << "(";
1686 }
1687 else if (visit == InVisit)
1688 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001689 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001690 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001691 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001692 }
1693 else
1694 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001695 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001696 }
1697 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001698 case EOpDivAssign:
1699 outputTriplet(out, visit, "(", " /= ", ")");
1700 break;
1701 case EOpIModAssign:
1702 outputTriplet(out, visit, "(", " %= ", ")");
1703 break;
1704 case EOpBitShiftLeftAssign:
1705 outputTriplet(out, visit, "(", " <<= ", ")");
1706 break;
1707 case EOpBitShiftRightAssign:
1708 outputTriplet(out, visit, "(", " >>= ", ")");
1709 break;
1710 case EOpBitwiseAndAssign:
1711 outputTriplet(out, visit, "(", " &= ", ")");
1712 break;
1713 case EOpBitwiseXorAssign:
1714 outputTriplet(out, visit, "(", " ^= ", ")");
1715 break;
1716 case EOpBitwiseOrAssign:
1717 outputTriplet(out, visit, "(", " |= ", ")");
1718 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001719 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001720 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001721 const TType& leftType = node->getLeft()->getType();
1722 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001723 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001724 if (visit == PreVisit)
1725 {
1726 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1727 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001728 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001729 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001730 return false;
1731 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001732 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001733 else
1734 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001735 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001736 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001737 }
1738 break;
1739 case EOpIndexIndirect:
1740 // We do not currently support indirect references to interface blocks
1741 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001742 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001743 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001744 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001745 if (visit == InVisit)
1746 {
1747 const TStructure* structure = node->getLeft()->getType().getStruct();
1748 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1749 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001750 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001751
1752 return false;
1753 }
1754 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001755 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001756 if (visit == InVisit)
1757 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001758 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1759 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1760 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001761 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001762
1763 return false;
1764 }
1765 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 case EOpVectorSwizzle:
1767 if (visit == InVisit)
1768 {
1769 out << ".";
1770
1771 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1772
1773 if (swizzle)
1774 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001775 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001776
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001777 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001778 {
1779 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1780
1781 if (element)
1782 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001783 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001784
1785 switch (i)
1786 {
1787 case 0: out << "x"; break;
1788 case 1: out << "y"; break;
1789 case 2: out << "z"; break;
1790 case 3: out << "w"; break;
1791 default: UNREACHABLE();
1792 }
1793 }
1794 else UNREACHABLE();
1795 }
1796 }
1797 else UNREACHABLE();
1798
1799 return false; // Fully processed
1800 }
1801 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001802 case EOpAdd:
1803 outputTriplet(out, visit, "(", " + ", ")");
1804 break;
1805 case EOpSub:
1806 outputTriplet(out, visit, "(", " - ", ")");
1807 break;
1808 case EOpMul:
1809 outputTriplet(out, visit, "(", " * ", ")");
1810 break;
1811 case EOpDiv:
1812 outputTriplet(out, visit, "(", " / ", ")");
1813 break;
1814 case EOpIMod:
1815 outputTriplet(out, visit, "(", " % ", ")");
1816 break;
1817 case EOpBitShiftLeft:
1818 outputTriplet(out, visit, "(", " << ", ")");
1819 break;
1820 case EOpBitShiftRight:
1821 outputTriplet(out, visit, "(", " >> ", ")");
1822 break;
1823 case EOpBitwiseAnd:
1824 outputTriplet(out, visit, "(", " & ", ")");
1825 break;
1826 case EOpBitwiseXor:
1827 outputTriplet(out, visit, "(", " ^ ", ")");
1828 break;
1829 case EOpBitwiseOr:
1830 outputTriplet(out, visit, "(", " | ", ")");
1831 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001832 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001833 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001834 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001835 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001836 case EOpLessThan:
1837 outputTriplet(out, visit, "(", " < ", ")");
1838 break;
1839 case EOpGreaterThan:
1840 outputTriplet(out, visit, "(", " > ", ")");
1841 break;
1842 case EOpLessThanEqual:
1843 outputTriplet(out, visit, "(", " <= ", ")");
1844 break;
1845 case EOpGreaterThanEqual:
1846 outputTriplet(out, visit, "(", " >= ", ")");
1847 break;
1848 case EOpVectorTimesScalar:
1849 outputTriplet(out, visit, "(", " * ", ")");
1850 break;
1851 case EOpMatrixTimesScalar:
1852 outputTriplet(out, visit, "(", " * ", ")");
1853 break;
1854 case EOpVectorTimesMatrix:
1855 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1856 break;
1857 case EOpMatrixTimesVector:
1858 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1859 break;
1860 case EOpMatrixTimesMatrix:
1861 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1862 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001863 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001864 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1865 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001866 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001867 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001868 case EOpLogicalXor:
1869 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001870 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001871 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001872 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001873 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1874 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001875 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001876 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001877 default: UNREACHABLE();
1878 }
1879
1880 return true;
1881}
1882
1883bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1884{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001885 TInfoSinkBase &out = getInfoSink();
1886
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887 switch (node->getOp())
1888 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001889 case EOpNegative:
1890 outputTriplet(out, visit, "(-", "", ")");
1891 break;
1892 case EOpPositive:
1893 outputTriplet(out, visit, "(+", "", ")");
1894 break;
1895 case EOpVectorLogicalNot:
1896 outputTriplet(out, visit, "(!", "", ")");
1897 break;
1898 case EOpLogicalNot:
1899 outputTriplet(out, visit, "(!", "", ")");
1900 break;
1901 case EOpBitwiseNot:
1902 outputTriplet(out, visit, "(~", "", ")");
1903 break;
1904 case EOpPostIncrement:
1905 outputTriplet(out, visit, "(", "", "++)");
1906 break;
1907 case EOpPostDecrement:
1908 outputTriplet(out, visit, "(", "", "--)");
1909 break;
1910 case EOpPreIncrement:
1911 outputTriplet(out, visit, "(++", "", ")");
1912 break;
1913 case EOpPreDecrement:
1914 outputTriplet(out, visit, "(--", "", ")");
1915 break;
1916 case EOpRadians:
1917 outputTriplet(out, visit, "radians(", "", ")");
1918 break;
1919 case EOpDegrees:
1920 outputTriplet(out, visit, "degrees(", "", ")");
1921 break;
1922 case EOpSin:
1923 outputTriplet(out, visit, "sin(", "", ")");
1924 break;
1925 case EOpCos:
1926 outputTriplet(out, visit, "cos(", "", ")");
1927 break;
1928 case EOpTan:
1929 outputTriplet(out, visit, "tan(", "", ")");
1930 break;
1931 case EOpAsin:
1932 outputTriplet(out, visit, "asin(", "", ")");
1933 break;
1934 case EOpAcos:
1935 outputTriplet(out, visit, "acos(", "", ")");
1936 break;
1937 case EOpAtan:
1938 outputTriplet(out, visit, "atan(", "", ")");
1939 break;
1940 case EOpSinh:
1941 outputTriplet(out, visit, "sinh(", "", ")");
1942 break;
1943 case EOpCosh:
1944 outputTriplet(out, visit, "cosh(", "", ")");
1945 break;
1946 case EOpTanh:
1947 outputTriplet(out, visit, "tanh(", "", ")");
1948 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001949 case EOpAsinh:
1950 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001951 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001952 break;
1953 case EOpAcosh:
1954 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001955 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001956 break;
1957 case EOpAtanh:
1958 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001959 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001960 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001961 case EOpExp:
1962 outputTriplet(out, visit, "exp(", "", ")");
1963 break;
1964 case EOpLog:
1965 outputTriplet(out, visit, "log(", "", ")");
1966 break;
1967 case EOpExp2:
1968 outputTriplet(out, visit, "exp2(", "", ")");
1969 break;
1970 case EOpLog2:
1971 outputTriplet(out, visit, "log2(", "", ")");
1972 break;
1973 case EOpSqrt:
1974 outputTriplet(out, visit, "sqrt(", "", ")");
1975 break;
1976 case EOpInverseSqrt:
1977 outputTriplet(out, visit, "rsqrt(", "", ")");
1978 break;
1979 case EOpAbs:
1980 outputTriplet(out, visit, "abs(", "", ")");
1981 break;
1982 case EOpSign:
1983 outputTriplet(out, visit, "sign(", "", ")");
1984 break;
1985 case EOpFloor:
1986 outputTriplet(out, visit, "floor(", "", ")");
1987 break;
1988 case EOpTrunc:
1989 outputTriplet(out, visit, "trunc(", "", ")");
1990 break;
1991 case EOpRound:
1992 outputTriplet(out, visit, "round(", "", ")");
1993 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001994 case EOpRoundEven:
1995 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001996 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001997 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001998 case EOpCeil:
1999 outputTriplet(out, visit, "ceil(", "", ")");
2000 break;
2001 case EOpFract:
2002 outputTriplet(out, visit, "frac(", "", ")");
2003 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05302004 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002005 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05302006 mRequiresIEEEStrictCompiling = true;
2007 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002008 case EOpIsInf:
2009 outputTriplet(out, visit, "isinf(", "", ")");
2010 break;
2011 case EOpFloatBitsToInt:
2012 outputTriplet(out, visit, "asint(", "", ")");
2013 break;
2014 case EOpFloatBitsToUint:
2015 outputTriplet(out, visit, "asuint(", "", ")");
2016 break;
2017 case EOpIntBitsToFloat:
2018 outputTriplet(out, visit, "asfloat(", "", ")");
2019 break;
2020 case EOpUintBitsToFloat:
2021 outputTriplet(out, visit, "asfloat(", "", ")");
2022 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002023 case EOpPackSnorm2x16:
2024 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002025 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002026 break;
2027 case EOpPackUnorm2x16:
2028 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002029 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002030 break;
2031 case EOpPackHalf2x16:
2032 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002033 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002034 break;
2035 case EOpUnpackSnorm2x16:
2036 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002037 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002038 break;
2039 case EOpUnpackUnorm2x16:
2040 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002041 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002042 break;
2043 case EOpUnpackHalf2x16:
2044 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002045 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002046 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002047 case EOpLength:
2048 outputTriplet(out, visit, "length(", "", ")");
2049 break;
2050 case EOpNormalize:
2051 outputTriplet(out, visit, "normalize(", "", ")");
2052 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002053 case EOpDFdx:
2054 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2055 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002056 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002057 }
2058 else
2059 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002060 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002061 }
2062 break;
2063 case EOpDFdy:
2064 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2065 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002066 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002067 }
2068 else
2069 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002070 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002071 }
2072 break;
2073 case EOpFwidth:
2074 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2075 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002076 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002077 }
2078 else
2079 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002080 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002081 }
2082 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002083 case EOpTranspose:
2084 outputTriplet(out, visit, "transpose(", "", ")");
2085 break;
2086 case EOpDeterminant:
2087 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2088 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002089 case EOpInverse:
2090 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002091 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002092 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002093
Jamie Madill8c46ab12015-12-07 16:39:19 -05002094 case EOpAny:
2095 outputTriplet(out, visit, "any(", "", ")");
2096 break;
2097 case EOpAll:
2098 outputTriplet(out, visit, "all(", "", ")");
2099 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100 default: UNREACHABLE();
2101 }
2102
2103 return true;
2104}
2105
2106bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2107{
Jamie Madill32aab012015-01-27 14:12:26 -05002108 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110 switch (node->getOp())
2111 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002112 case EOpSequence:
2113 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002114 if (mInsideFunction)
2115 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002116 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002117 out << "{\n";
2118 }
2119
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002120 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002121 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002122 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002123
Olli Etuahoa6f22092015-05-08 18:31:10 +03002124 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002125
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002126 // Don't output ; after case labels, they're terminated by :
2127 // This is needed especially since outputting a ; after a case statement would turn empty
2128 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002129 // Also no need to output ; after selection (if) statements or sequences. This is done just
2130 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002131 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2132 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002133 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002134 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002135 }
2136
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002137 if (mInsideFunction)
2138 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002139 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002140 out << "}\n";
2141 }
2142
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002143 return false;
2144 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145 case EOpDeclaration:
2146 if (visit == PreVisit)
2147 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002148 TIntermSequence *sequence = node->getSequence();
2149 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002150 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002152 if (variable &&
2153 (variable->getQualifier() == EvqTemporary ||
2154 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002155 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002156 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002157
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002158 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002159 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002160 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002162 out << "static ";
2163 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002164
Olli Etuahoa6f22092015-05-08 18:31:10 +03002165 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002166
Olli Etuahoa6f22092015-05-08 18:31:10 +03002167 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002168
Olli Etuahoa6f22092015-05-08 18:31:10 +03002169 if (symbol)
2170 {
2171 symbol->traverse(this);
2172 out << ArrayString(symbol->getType());
2173 out << " = " + initializer(symbol->getType());
2174 }
2175 else
2176 {
2177 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002180 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2181 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002182 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002183 }
2184 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185 }
Jamie Madill033dae62014-06-18 12:56:28 -04002186 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002187 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002188 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002189 {
2190 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2191
2192 if (symbol)
2193 {
2194 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2195 mReferencedVaryings[symbol->getSymbol()] = symbol;
2196 }
2197 else
2198 {
2199 (*sit)->traverse(this);
2200 }
2201 }
2202 }
2203
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002204 return false;
2205 }
2206 else if (visit == InVisit)
2207 {
2208 out << ", ";
2209 }
2210 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002211 case EOpInvariantDeclaration:
2212 // Do not do any translation
2213 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002214 case EOpPrototype:
2215 if (visit == PreVisit)
2216 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002217 size_t index = mCallDag.findIndex(node);
2218 // Skip the prototype if it is not implemented (and thus not used)
2219 if (index == CallDAG::InvalidIndex)
2220 {
2221 return false;
2222 }
2223
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002224 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002225
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002226 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2227 out << TypeString(node->getType()) << " " << name << DisambiguateFunctionName(arguments)
2228 << (mOutputLod0Function ? "Lod0(" : "(");
2229
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002230 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002231 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002232 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002233
2234 if (symbol)
2235 {
2236 out << argumentString(symbol);
2237
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002238 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002239 {
2240 out << ", ";
2241 }
2242 }
2243 else UNREACHABLE();
2244 }
2245
2246 out << ");\n";
2247
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002248 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002249 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2250 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002251 {
2252 mOutputLod0Function = true;
2253 node->traverse(this);
2254 mOutputLod0Function = false;
2255 }
2256
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002257 return false;
2258 }
2259 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002260 case EOpComma:
2261 outputTriplet(out, visit, "(", ", ", ")");
2262 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 case EOpFunction:
2264 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002265 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002266 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267
Corentin Wallez1239ee92015-03-19 14:38:02 -07002268 size_t index = mCallDag.findIndex(node);
2269 ASSERT(index != CallDAG::InvalidIndex);
2270 mCurrentFunctionMetadata = &mASTMetadataList[index];
2271
Jamie Madill033dae62014-06-18 12:56:28 -04002272 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002273
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002274 TIntermSequence *sequence = node->getSequence();
2275 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
2276
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002277 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002278 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002279 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002280 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002281 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002283 out << DecorateFunctionIfNeeded(node->getNameObj())
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002284 << DisambiguateFunctionName(arguments) << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002285 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002286
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002287 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002288 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002289 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002290
2291 if (symbol)
2292 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002293 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002294
2295 out << argumentString(symbol);
2296
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002297 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002298 {
2299 out << ", ";
2300 }
2301 }
2302 else UNREACHABLE();
2303 }
2304
Olli Etuaho4785fec2015-05-18 16:09:37 +03002305 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002306
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002307 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002308 {
2309 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002310 TIntermNode *body = (*sequence)[1];
2311 // The function body node will output braces.
2312 ASSERT(IsSequence(body));
2313 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002314 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002316 else
2317 {
2318 out << "{}\n";
2319 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002320
Corentin Wallez1239ee92015-03-19 14:38:02 -07002321 mCurrentFunctionMetadata = nullptr;
2322
2323 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2324 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002325 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002326 ASSERT(name != "main");
2327 mOutputLod0Function = true;
2328 node->traverse(this);
2329 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002330 }
2331
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002332 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333 }
2334 break;
2335 case EOpFunctionCall:
2336 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002337 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002338
Corentin Wallez1239ee92015-03-19 14:38:02 -07002339 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002340 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002341 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002342 if (node->isArray())
2343 {
2344 UNIMPLEMENTED();
2345 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002346 size_t index = mCallDag.findIndex(node);
2347 ASSERT(index != CallDAG::InvalidIndex);
2348 lod0 &= mASTMetadataList[index].mNeedsLod0;
2349
Olli Etuahobe59c2f2016-03-07 11:32:34 +02002350 out << DecorateFunctionIfNeeded(node->getNameObj());
2351 out << DisambiguateFunctionName(node->getSequence());
2352 out << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353 }
2354 else
2355 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002356 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002357 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002358
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002359 TextureFunction textureFunction;
2360 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002361 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002362 textureFunction.method = TextureFunction::IMPLICIT;
2363 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002364 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002365
2366 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002367 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002368 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002369 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002370 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002371 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002372 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002373 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002374 }
Nicolas Capens46485082014-04-15 13:12:50 -04002375 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2376 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002377 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002378 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002379 }
Nicolas Capens46485082014-04-15 13:12:50 -04002380 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002381 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002382 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002383 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002384 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002385 else if (name == "textureSize")
2386 {
2387 textureFunction.method = TextureFunction::SIZE;
2388 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002389 else if (name == "textureOffset")
2390 {
2391 textureFunction.method = TextureFunction::IMPLICIT;
2392 textureFunction.offset = true;
2393 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002394 else if (name == "textureProjOffset")
2395 {
2396 textureFunction.method = TextureFunction::IMPLICIT;
2397 textureFunction.offset = true;
2398 textureFunction.proj = true;
2399 }
2400 else if (name == "textureLodOffset")
2401 {
2402 textureFunction.method = TextureFunction::LOD;
2403 textureFunction.offset = true;
2404 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002405 else if (name == "textureProjLodOffset")
2406 {
2407 textureFunction.method = TextureFunction::LOD;
2408 textureFunction.proj = true;
2409 textureFunction.offset = true;
2410 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002411 else if (name == "texelFetch")
2412 {
2413 textureFunction.method = TextureFunction::FETCH;
2414 }
2415 else if (name == "texelFetchOffset")
2416 {
2417 textureFunction.method = TextureFunction::FETCH;
2418 textureFunction.offset = true;
2419 }
Nicolas Capens46485082014-04-15 13:12:50 -04002420 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002421 {
2422 textureFunction.method = TextureFunction::GRAD;
2423 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002424 else if (name == "textureGradOffset")
2425 {
2426 textureFunction.method = TextureFunction::GRAD;
2427 textureFunction.offset = true;
2428 }
Nicolas Capens46485082014-04-15 13:12:50 -04002429 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002430 {
2431 textureFunction.method = TextureFunction::GRAD;
2432 textureFunction.proj = true;
2433 }
2434 else if (name == "textureProjGradOffset")
2435 {
2436 textureFunction.method = TextureFunction::GRAD;
2437 textureFunction.proj = true;
2438 textureFunction.offset = true;
2439 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002440 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002441
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002442 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002443 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002444 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2445
2446 if (textureFunction.offset)
2447 {
2448 mandatoryArgumentCount++;
2449 }
2450
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002451 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002452
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002453 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002454 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002455 if (bias)
2456 {
2457 textureFunction.method = TextureFunction::LOD0BIAS;
2458 }
2459 else
2460 {
2461 textureFunction.method = TextureFunction::LOD0;
2462 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002463 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002464 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002465 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002466 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002467 }
2468 }
2469
2470 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002471
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002472 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002474
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002475 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002476 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002477 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT &&
2478 IsSampler((*arg)->getAsTyped()->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002479 {
2480 out << "texture_";
2481 (*arg)->traverse(this);
2482 out << ", sampler_";
2483 }
2484
2485 (*arg)->traverse(this);
2486
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002487 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002488 {
2489 out << ", ";
2490 }
2491 }
2492
2493 out << ")";
2494
2495 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496 }
2497 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002498 case EOpParameters:
2499 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2500 break;
2501 case EOpConstructFloat:
2502 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2503 break;
2504 case EOpConstructVec2:
2505 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2506 break;
2507 case EOpConstructVec3:
2508 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2509 break;
2510 case EOpConstructVec4:
2511 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2512 break;
2513 case EOpConstructBool:
2514 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2515 break;
2516 case EOpConstructBVec2:
2517 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2518 break;
2519 case EOpConstructBVec3:
2520 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2521 break;
2522 case EOpConstructBVec4:
2523 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2524 break;
2525 case EOpConstructInt:
2526 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2527 break;
2528 case EOpConstructIVec2:
2529 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2530 break;
2531 case EOpConstructIVec3:
2532 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2533 break;
2534 case EOpConstructIVec4:
2535 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2536 break;
2537 case EOpConstructUInt:
2538 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2539 break;
2540 case EOpConstructUVec2:
2541 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2542 break;
2543 case EOpConstructUVec3:
2544 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2545 break;
2546 case EOpConstructUVec4:
2547 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2548 break;
2549 case EOpConstructMat2:
2550 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2551 break;
2552 case EOpConstructMat2x3:
2553 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2554 break;
2555 case EOpConstructMat2x4:
2556 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2557 break;
2558 case EOpConstructMat3x2:
2559 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2560 break;
2561 case EOpConstructMat3:
2562 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2563 break;
2564 case EOpConstructMat3x4:
2565 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2566 break;
2567 case EOpConstructMat4x2:
2568 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2569 break;
2570 case EOpConstructMat4x3:
2571 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2572 break;
2573 case EOpConstructMat4:
2574 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2575 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002576 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002577 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002578 if (node->getType().isArray())
2579 {
2580 UNIMPLEMENTED();
2581 }
Jamie Madill033dae62014-06-18 12:56:28 -04002582 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002583 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002584 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002585 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002586 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002587 case EOpLessThan:
2588 outputTriplet(out, visit, "(", " < ", ")");
2589 break;
2590 case EOpGreaterThan:
2591 outputTriplet(out, visit, "(", " > ", ")");
2592 break;
2593 case EOpLessThanEqual:
2594 outputTriplet(out, visit, "(", " <= ", ")");
2595 break;
2596 case EOpGreaterThanEqual:
2597 outputTriplet(out, visit, "(", " >= ", ")");
2598 break;
2599 case EOpVectorEqual:
2600 outputTriplet(out, visit, "(", " == ", ")");
2601 break;
2602 case EOpVectorNotEqual:
2603 outputTriplet(out, visit, "(", " != ", ")");
2604 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002605 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002606 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002607 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002608 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002609 case EOpModf:
2610 outputTriplet(out, visit, "modf(", ", ", ")");
2611 break;
2612 case EOpPow:
2613 outputTriplet(out, visit, "pow(", ", ", ")");
2614 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002615 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002616 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002617 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002618 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002619 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002620 case EOpMin:
2621 outputTriplet(out, visit, "min(", ", ", ")");
2622 break;
2623 case EOpMax:
2624 outputTriplet(out, visit, "max(", ", ", ")");
2625 break;
2626 case EOpClamp:
2627 outputTriplet(out, visit, "clamp(", ", ", ")");
2628 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302629 case EOpMix:
2630 {
2631 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2632 if (lastParamNode->getType().getBasicType() == EbtBool)
2633 {
2634 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2635 // so use emulated version.
2636 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002637 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302638 }
2639 else
2640 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002641 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302642 }
2643 }
2644 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002645 case EOpStep:
2646 outputTriplet(out, visit, "step(", ", ", ")");
2647 break;
2648 case EOpSmoothStep:
2649 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2650 break;
2651 case EOpDistance:
2652 outputTriplet(out, visit, "distance(", ", ", ")");
2653 break;
2654 case EOpDot:
2655 outputTriplet(out, visit, "dot(", ", ", ")");
2656 break;
2657 case EOpCross:
2658 outputTriplet(out, visit, "cross(", ", ", ")");
2659 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002660 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002661 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002662 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002663 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002664 case EOpReflect:
2665 outputTriplet(out, visit, "reflect(", ", ", ")");
2666 break;
2667 case EOpRefract:
2668 outputTriplet(out, visit, "refract(", ", ", ")");
2669 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002670 case EOpOuterProduct:
2671 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002672 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002673 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002674 case EOpMul:
2675 outputTriplet(out, visit, "(", " * ", ")");
2676 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002677 default: UNREACHABLE();
2678 }
2679
2680 return true;
2681}
2682
Jamie Madill8c46ab12015-12-07 16:39:19 -05002683void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002684{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002685 out << "if (";
2686
2687 node->getCondition()->traverse(this);
2688
2689 out << ")\n";
2690
Jamie Madill8c46ab12015-12-07 16:39:19 -05002691 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002692
2693 bool discard = false;
2694
2695 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002696 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002697 // The trueBlock child node will output braces.
2698 ASSERT(IsSequence(node->getTrueBlock()));
2699
Olli Etuahoa6f22092015-05-08 18:31:10 +03002700 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002701
Olli Etuahoa6f22092015-05-08 18:31:10 +03002702 // Detect true discard
2703 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2704 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002705 else
2706 {
2707 // TODO(oetuaho): Check if the semicolon inside is necessary.
2708 // It's there as a result of conservative refactoring of the output.
2709 out << "{;}\n";
2710 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002711
Jamie Madill8c46ab12015-12-07 16:39:19 -05002712 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002713
Olli Etuahoa6f22092015-05-08 18:31:10 +03002714 if (node->getFalseBlock())
2715 {
2716 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002717
Jamie Madill8c46ab12015-12-07 16:39:19 -05002718 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719
Olli Etuaho4785fec2015-05-18 16:09:37 +03002720 // Either this is "else if" or the falseBlock child node will output braces.
2721 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2722
Olli Etuahoa6f22092015-05-08 18:31:10 +03002723 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002724
Jamie Madill8c46ab12015-12-07 16:39:19 -05002725 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002726
Olli Etuahoa6f22092015-05-08 18:31:10 +03002727 // Detect false discard
2728 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2729 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002730
Olli Etuahoa6f22092015-05-08 18:31:10 +03002731 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002732 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002733 {
2734 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002735 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002736}
2737
2738bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2739{
2740 TInfoSinkBase &out = getInfoSink();
2741
2742 ASSERT(!node->usesTernaryOperator());
2743
2744 if (!mInsideFunction)
2745 {
2746 // This is part of unfolded global initialization.
2747 mDeferredGlobalInitializers.push_back(node);
2748 return false;
2749 }
2750
2751 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002752 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002753 {
2754 out << "FLATTEN ";
2755 }
2756
Jamie Madill8c46ab12015-12-07 16:39:19 -05002757 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002758
2759 return false;
2760}
2761
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002762bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002763{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002764 TInfoSinkBase &out = getInfoSink();
2765
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002766 if (node->getStatementList())
2767 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002768 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002769 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002770 // The curly braces get written when visiting the statementList aggregate
2771 }
2772 else
2773 {
2774 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002775 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002776 }
2777 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002778}
2779
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002780bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002781{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002782 TInfoSinkBase &out = getInfoSink();
2783
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002784 if (node->hasCondition())
2785 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002786 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002787 return true;
2788 }
2789 else
2790 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002791 out << "default:\n";
2792 return false;
2793 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002794}
2795
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002796void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2797{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002798 TInfoSinkBase &out = getInfoSink();
2799 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002800}
2801
2802bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2803{
Nicolas Capens655fe362014-04-11 13:12:34 -04002804 mNestedLoopDepth++;
2805
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002806 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002807 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002808 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002809
Jamie Madill8c46ab12015-12-07 16:39:19 -05002810 TInfoSinkBase &out = getInfoSink();
2811
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002812 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002813 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002814 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002815 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002816 mInsideDiscontinuousLoop = wasDiscontinuous;
2817 mNestedLoopDepth--;
2818
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002819 return false;
2820 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002821 }
2822
Corentin Wallez1239ee92015-03-19 14:38:02 -07002823 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002824 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002825 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002826 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002827
Jamie Madill8c46ab12015-12-07 16:39:19 -05002828 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 }
2830 else
2831 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002832 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002833
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002834 if (node->getInit())
2835 {
2836 node->getInit()->traverse(this);
2837 }
2838
2839 out << "; ";
2840
alokp@chromium.org52813552010-11-16 18:36:09 +00002841 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002842 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002843 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002844 }
2845
2846 out << "; ";
2847
alokp@chromium.org52813552010-11-16 18:36:09 +00002848 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002850 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002851 }
2852
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002853 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002854
Jamie Madill8c46ab12015-12-07 16:39:19 -05002855 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856 }
2857
2858 if (node->getBody())
2859 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002860 // The loop body node will output braces.
2861 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002862 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002863 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002864 else
2865 {
2866 // TODO(oetuaho): Check if the semicolon inside is necessary.
2867 // It's there as a result of conservative refactoring of the output.
2868 out << "{;}\n";
2869 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870
Jamie Madill8c46ab12015-12-07 16:39:19 -05002871 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002872
alokp@chromium.org52813552010-11-16 18:36:09 +00002873 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002875 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002876 out << "while(\n";
2877
alokp@chromium.org52813552010-11-16 18:36:09 +00002878 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879
daniel@transgaming.com73536982012-03-21 20:45:49 +00002880 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002881 }
2882
daniel@transgaming.com73536982012-03-21 20:45:49 +00002883 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002884
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002885 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002886 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002887
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002888 return false;
2889}
2890
2891bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2892{
Jamie Madill32aab012015-01-27 14:12:26 -05002893 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002894
2895 switch (node->getFlowOp())
2896 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002897 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002898 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002899 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002900 case EOpBreak:
2901 if (visit == PreVisit)
2902 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002903 if (mNestedLoopDepth > 1)
2904 {
2905 mUsesNestedBreak = true;
2906 }
2907
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002908 if (mExcessiveLoopIndex)
2909 {
2910 out << "{Break";
2911 mExcessiveLoopIndex->traverse(this);
2912 out << " = true; break;}\n";
2913 }
2914 else
2915 {
2916 out << "break;\n";
2917 }
2918 }
2919 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002920 case EOpContinue:
2921 outputTriplet(out, visit, "continue;\n", "", "");
2922 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002923 case EOpReturn:
2924 if (visit == PreVisit)
2925 {
2926 if (node->getExpression())
2927 {
2928 out << "return ";
2929 }
2930 else
2931 {
2932 out << "return;\n";
2933 }
2934 }
2935 else if (visit == PostVisit)
2936 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002937 if (node->getExpression())
2938 {
2939 out << ";\n";
2940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002941 }
2942 break;
2943 default: UNREACHABLE();
2944 }
2945
2946 return true;
2947}
2948
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002949bool OutputHLSL::isSingleStatement(TIntermNode *node)
2950{
2951 TIntermAggregate *aggregate = node->getAsAggregate();
2952
2953 if (aggregate)
2954 {
2955 if (aggregate->getOp() == EOpSequence)
2956 {
2957 return false;
2958 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002959 else if (aggregate->getOp() == EOpDeclaration)
2960 {
2961 // Declaring multiple comma-separated variables must be considered multiple statements
2962 // because each individual declaration has side effects which are visible in the next.
2963 return false;
2964 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002965 else
2966 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002967 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002968 {
2969 if (!isSingleStatement(*sit))
2970 {
2971 return false;
2972 }
2973 }
2974
2975 return true;
2976 }
2977 }
2978
2979 return true;
2980}
2981
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002982// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2983// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002984bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002985{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002986 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002987
2988 // Parse loops of the form:
2989 // for(int index = initial; index [comparator] limit; index += increment)
2990 TIntermSymbol *index = NULL;
2991 TOperator comparator = EOpNull;
2992 int initial = 0;
2993 int limit = 0;
2994 int increment = 0;
2995
2996 // Parse index name and intial value
2997 if (node->getInit())
2998 {
2999 TIntermAggregate *init = node->getInit()->getAsAggregate();
3000
3001 if (init)
3002 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07003003 TIntermSequence *sequence = init->getSequence();
3004 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003005
3006 if (variable && variable->getQualifier() == EvqTemporary)
3007 {
3008 TIntermBinary *assign = variable->getAsBinaryNode();
3009
3010 if (assign->getOp() == EOpInitialize)
3011 {
3012 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3013 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3014
3015 if (symbol && constant)
3016 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003017 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003018 {
3019 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003020 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003021 }
3022 }
3023 }
3024 }
3025 }
3026 }
3027
3028 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003029 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003030 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003031 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003032
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003033 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3034 {
3035 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3036
3037 if (constant)
3038 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003039 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003040 {
3041 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003042 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003043 }
3044 }
3045 }
3046 }
3047
3048 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003049 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003050 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003051 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3052 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003053
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003054 if (binaryTerminal)
3055 {
3056 TOperator op = binaryTerminal->getOp();
3057 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3058
3059 if (constant)
3060 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003061 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003062 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003063 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003064
3065 switch (op)
3066 {
3067 case EOpAddAssign: increment = value; break;
3068 case EOpSubAssign: increment = -value; break;
3069 default: UNIMPLEMENTED();
3070 }
3071 }
3072 }
3073 }
3074 else if (unaryTerminal)
3075 {
3076 TOperator op = unaryTerminal->getOp();
3077
3078 switch (op)
3079 {
3080 case EOpPostIncrement: increment = 1; break;
3081 case EOpPostDecrement: increment = -1; break;
3082 case EOpPreIncrement: increment = 1; break;
3083 case EOpPreDecrement: increment = -1; break;
3084 default: UNIMPLEMENTED();
3085 }
3086 }
3087 }
3088
3089 if (index != NULL && comparator != EOpNull && increment != 0)
3090 {
3091 if (comparator == EOpLessThanEqual)
3092 {
3093 comparator = EOpLessThan;
3094 limit += 1;
3095 }
3096
3097 if (comparator == EOpLessThan)
3098 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003099 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003100
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003101 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003102 {
3103 return false; // Not an excessive loop
3104 }
3105
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003106 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3107 mExcessiveLoopIndex = index;
3108
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003109 out << "{int ";
3110 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003111 out << ";\n"
3112 "bool Break";
3113 index->traverse(this);
3114 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003115
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003116 bool firstLoopFragment = true;
3117
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003118 while (iterations > 0)
3119 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003120 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003121
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003122 if (!firstLoopFragment)
3123 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003124 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003125 index->traverse(this);
3126 out << ") {\n";
3127 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003128
3129 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3130 {
3131 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3132 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003133
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003134 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003135 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003136
Corentin Wallez1239ee92015-03-19 14:38:02 -07003137 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003138 index->traverse(this);
3139 out << " = ";
3140 out << initial;
3141
3142 out << "; ";
3143 index->traverse(this);
3144 out << " < ";
3145 out << clampedLimit;
3146
3147 out << "; ";
3148 index->traverse(this);
3149 out << " += ";
3150 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003151 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003152
Jamie Madill8c46ab12015-12-07 16:39:19 -05003153 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003154 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003155
3156 if (node->getBody())
3157 {
3158 node->getBody()->traverse(this);
3159 }
3160
Jamie Madill8c46ab12015-12-07 16:39:19 -05003161 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003162 out << ";}\n";
3163
3164 if (!firstLoopFragment)
3165 {
3166 out << "}\n";
3167 }
3168
3169 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003170
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003171 initial += MAX_LOOP_ITERATIONS * increment;
3172 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003173 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003174
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003175 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003176
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003177 mExcessiveLoopIndex = restoreIndex;
3178
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003179 return true;
3180 }
3181 else UNIMPLEMENTED();
3182 }
3183
3184 return false; // Not handled as an excessive loop
3185}
3186
Jamie Madill8c46ab12015-12-07 16:39:19 -05003187void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3188 Visit visit,
3189 const char *preString,
3190 const char *inString,
3191 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003192{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003193 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003194 {
3195 out << preString;
3196 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003197 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003198 {
3199 out << inString;
3200 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003201 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003202 {
3203 out << postString;
3204 }
3205}
3206
Jamie Madill8c46ab12015-12-07 16:39:19 -05003207void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003208{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003209 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003210 {
Jamie Madill32aab012015-01-27 14:12:26 -05003211 out << "\n";
3212 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003213
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003214 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003215 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003216 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003217 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003218
Jamie Madill32aab012015-01-27 14:12:26 -05003219 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003220 }
3221}
3222
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003223TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3224{
3225 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003226 const TType &type = symbol->getType();
3227 const TName &name = symbol->getName();
3228 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003229
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003230 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003231 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003232 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003233 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003234 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003235 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003236 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003237 }
3238
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003239 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003240 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003241 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3242 {
3243 // Samplers are passed as indices to the sampler array.
3244 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3245 return "const uint " + nameStr + ArrayString(type);
3246 }
3247 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3248 {
3249 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3250 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3251 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3252 ArrayString(type);
3253 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003254 }
3255
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003256 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003257}
3258
3259TString OutputHLSL::initializer(const TType &type)
3260{
3261 TString string;
3262
Jamie Madill94bf7f22013-07-08 13:31:15 -04003263 size_t size = type.getObjectSize();
3264 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003265 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003266 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003267
Jamie Madill94bf7f22013-07-08 13:31:15 -04003268 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003269 {
3270 string += ", ";
3271 }
3272 }
3273
daniel@transgaming.comead23042010-04-29 03:35:36 +00003274 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003275}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003276
Jamie Madill8c46ab12015-12-07 16:39:19 -05003277void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3278 Visit visit,
3279 const TType &type,
3280 const char *name,
3281 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003282{
Olli Etuahof40319e2015-03-10 14:33:00 +02003283 if (type.isArray())
3284 {
3285 UNIMPLEMENTED();
3286 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003287
3288 if (visit == PreVisit)
3289 {
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003290 TString constructorName = mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003291
Olli Etuahobe59c2f2016-03-07 11:32:34 +02003292 out << constructorName << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003293 }
3294 else if (visit == InVisit)
3295 {
3296 out << ", ";
3297 }
3298 else if (visit == PostVisit)
3299 {
3300 out << ")";
3301 }
3302}
3303
Jamie Madill8c46ab12015-12-07 16:39:19 -05003304const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3305 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003306 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003307{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003308 const TConstantUnion *constUnionIterated = constUnion;
3309
Jamie Madill98493dd2013-07-08 14:39:03 -04003310 const TStructure* structure = type.getStruct();
3311 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003312 {
Jamie Madill033dae62014-06-18 12:56:28 -04003313 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003314
Jamie Madill98493dd2013-07-08 14:39:03 -04003315 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003316
Jamie Madill98493dd2013-07-08 14:39:03 -04003317 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003318 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003319 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003320 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003321
Jamie Madill98493dd2013-07-08 14:39:03 -04003322 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003323 {
3324 out << ", ";
3325 }
3326 }
3327
3328 out << ")";
3329 }
3330 else
3331 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003332 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003333 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003334
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003335 if (writeType)
3336 {
Jamie Madill033dae62014-06-18 12:56:28 -04003337 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003338 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003339 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003340 if (writeType)
3341 {
3342 out << ")";
3343 }
3344 }
3345
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003346 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003347}
3348
Jamie Madill8c46ab12015-12-07 16:39:19 -05003349void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003350{
3351 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003352 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003353}
3354
Jamie Madill37997142015-01-28 10:06:34 -05003355bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3356{
3357 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3358 expression->traverse(&searchSymbol);
3359
3360 if (searchSymbol.foundMatch())
3361 {
3362 // Type already printed
3363 out << "t" + str(mUniqueIndex) + " = ";
3364 expression->traverse(this);
3365 out << ", ";
3366 symbolNode->traverse(this);
3367 out << " = t" + str(mUniqueIndex);
3368
3369 mUniqueIndex++;
3370 return true;
3371 }
3372
3373 return false;
3374}
3375
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003376bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3377{
3378 // We support writing constant unions and constructors that only take constant unions as
3379 // parameters as HLSL literals.
3380 if (expression->getAsConstantUnion())
3381 {
3382 return true;
3383 }
3384 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3385 !expression->getAsAggregate()->isConstructor())
3386 {
3387 return false;
3388 }
3389 TIntermAggregate *constructor = expression->getAsAggregate();
3390 for (TIntermNode *&node : *constructor->getSequence())
3391 {
3392 if (!node->getAsConstantUnion())
3393 return false;
3394 }
3395 return true;
3396}
3397
3398bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3399 TIntermSymbol *symbolNode,
3400 TIntermTyped *expression)
3401{
3402 if (canWriteAsHLSLLiteral(expression))
3403 {
3404 symbolNode->traverse(this);
3405 if (expression->getType().isArray())
3406 {
3407 out << "[" << expression->getType().getArraySize() << "]";
3408 }
3409 out << " = {";
3410 if (expression->getAsConstantUnion())
3411 {
3412 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3413 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3414 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3415 }
3416 else
3417 {
3418 TIntermAggregate *constructor = expression->getAsAggregate();
3419 ASSERT(constructor != nullptr);
3420 for (TIntermNode *&node : *constructor->getSequence())
3421 {
3422 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3423 ASSERT(nodeConst);
3424 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3425 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3426 if (node != constructor->getSequence()->back())
3427 {
3428 out << ", ";
3429 }
3430 }
3431 }
3432 out << "}";
3433 return true;
3434 }
3435 return false;
3436}
3437
Jamie Madill37997142015-01-28 10:06:34 -05003438void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3439{
3440 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3441 << "\n"
3442 << "void initializeDeferredGlobals()\n"
3443 << "{\n";
3444
3445 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3446 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003447 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3448 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3449 if (binary != nullptr)
3450 {
3451 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3452 TIntermTyped *expression = binary->getRight();
3453 ASSERT(symbol);
3454 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003455
Olli Etuahod81ed842015-05-12 12:46:35 +03003456 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003457
Olli Etuahod81ed842015-05-12 12:46:35 +03003458 if (!writeSameSymbolInitializer(out, symbol, expression))
3459 {
3460 ASSERT(mInfoSinkStack.top() == &out);
3461 expression->traverse(this);
3462 }
3463 out << ";\n";
3464 }
3465 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003466 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003467 writeSelection(out, selection);
Jamie Madill37997142015-01-28 10:06:34 -05003468 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003469 else
3470 {
3471 UNREACHABLE();
3472 }
Jamie Madill37997142015-01-28 10:06:34 -05003473 }
3474
3475 out << "}\n"
3476 << "\n";
3477}
3478
Jamie Madill55e79e02015-02-09 15:35:00 -05003479TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3480{
3481 const TFieldList &fields = structure.fields();
3482
3483 for (const auto &eqFunction : mStructEqualityFunctions)
3484 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003485 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003486 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003487 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003488 }
3489 }
3490
3491 const TString &structNameString = StructNameString(structure);
3492
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003493 StructEqualityFunction *function = new StructEqualityFunction();
3494 function->structure = &structure;
3495 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003496
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003497 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003498
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003499 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3500 << "{\n"
3501 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003502
3503 for (size_t i = 0; i < fields.size(); i++)
3504 {
3505 const TField *field = fields[i];
3506 const TType *fieldType = field->type();
3507
3508 const TString &fieldNameA = "a." + Decorate(field->name());
3509 const TString &fieldNameB = "b." + Decorate(field->name());
3510
3511 if (i > 0)
3512 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003513 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003514 }
3515
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003516 fnOut << "(";
3517 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3518 fnOut << fieldNameA;
3519 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3520 fnOut << fieldNameB;
3521 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3522 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003523 }
3524
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003525 fnOut << ";\n" << "}\n";
3526
3527 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003528
3529 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003530 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003531
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003532 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003533}
3534
Olli Etuaho7fb49552015-03-18 17:27:44 +02003535TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3536{
3537 for (const auto &eqFunction : mArrayEqualityFunctions)
3538 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003539 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003540 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003541 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003542 }
3543 }
3544
3545 const TString &typeName = TypeString(type);
3546
Olli Etuaho12690762015-03-31 12:55:28 +03003547 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003548 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003549
3550 TInfoSinkBase fnNameOut;
3551 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003552 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003553
3554 TType nonArrayType = type;
3555 nonArrayType.clearArrayness();
3556
3557 TInfoSinkBase fnOut;
3558
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003559 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003560 << typeName << " a[" << type.getArraySize() << "], "
3561 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003562 << "{\n"
3563 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3564 " {\n"
3565 " if (";
3566
3567 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3568 fnOut << "a[i]";
3569 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3570 fnOut << "b[i]";
3571 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3572
3573 fnOut << ") { return false; }\n"
3574 " }\n"
3575 " return true;\n"
3576 "}\n";
3577
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003578 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003579
3580 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003581 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003582
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003583 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003584}
3585
Olli Etuaho12690762015-03-31 12:55:28 +03003586TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3587{
3588 for (const auto &assignFunction : mArrayAssignmentFunctions)
3589 {
3590 if (assignFunction.type == type)
3591 {
3592 return assignFunction.functionName;
3593 }
3594 }
3595
3596 const TString &typeName = TypeString(type);
3597
3598 ArrayHelperFunction function;
3599 function.type = type;
3600
3601 TInfoSinkBase fnNameOut;
3602 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3603 function.functionName = fnNameOut.c_str();
3604
3605 TInfoSinkBase fnOut;
3606
3607 fnOut << "void " << function.functionName << "(out "
3608 << typeName << " a[" << type.getArraySize() << "], "
3609 << typeName << " b[" << type.getArraySize() << "])\n"
3610 << "{\n"
3611 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3612 " {\n"
3613 " a[i] = b[i];\n"
3614 " }\n"
3615 "}\n";
3616
3617 function.functionDefinition = fnOut.c_str();
3618
3619 mArrayAssignmentFunctions.push_back(function);
3620
3621 return function.functionName;
3622}
3623
Olli Etuaho9638c352015-04-01 14:34:52 +03003624TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3625{
3626 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3627 {
3628 if (constructIntoFunction.type == type)
3629 {
3630 return constructIntoFunction.functionName;
3631 }
3632 }
3633
3634 const TString &typeName = TypeString(type);
3635
3636 ArrayHelperFunction function;
3637 function.type = type;
3638
3639 TInfoSinkBase fnNameOut;
3640 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3641 function.functionName = fnNameOut.c_str();
3642
3643 TInfoSinkBase fnOut;
3644
3645 fnOut << "void " << function.functionName << "(out "
3646 << typeName << " a[" << type.getArraySize() << "]";
3647 for (int i = 0; i < type.getArraySize(); ++i)
3648 {
3649 fnOut << ", " << typeName << " b" << i;
3650 }
3651 fnOut << ")\n"
3652 "{\n";
3653
3654 for (int i = 0; i < type.getArraySize(); ++i)
3655 {
3656 fnOut << " a[" << i << "] = b" << i << ";\n";
3657 }
3658 fnOut << "}\n";
3659
3660 function.functionDefinition = fnOut.c_str();
3661
3662 mArrayConstructIntoFunctions.push_back(function);
3663
3664 return function.functionName;
3665}
3666
Jamie Madill2e295e22015-04-29 10:41:33 -04003667void OutputHLSL::ensureStructDefined(const TType &type)
3668{
3669 TStructure *structure = type.getStruct();
3670
3671 if (structure)
3672 {
3673 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3674 }
3675}
3676
3677
Olli Etuaho9638c352015-04-01 14:34:52 +03003678
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003679}