blob: 253b96696c89376ebddc0cf08bfd187862351106 [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
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000515 out << "};\n";
516 }
517 else
518 {
519 if (mUsesDepthRange)
520 {
521 out << "uniform float3 dx_DepthRange : register(c0);";
522 }
523
524 if (mUsesFragCoord)
525 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000526 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000527 }
528
529 if (mUsesFragCoord || mUsesFrontFacing)
530 {
531 out << "uniform float3 dx_DepthFront : register(c2);\n";
532 }
533 }
534
535 out << "\n";
536
537 if (mUsesDepthRange)
538 {
539 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
540 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000541 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000542
Jamie Madillf91ce812014-06-13 10:04:34 -0400543 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000544 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400545 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000546 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400547 out << flaggedStructs;
548 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000549 }
550
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000551 if (usingMRTExtension && mNumRenderTargets > 1)
552 {
553 out << "#define GL_USES_MRT\n";
554 }
555
556 if (mUsesFragColor)
557 {
558 out << "#define GL_USES_FRAG_COLOR\n";
559 }
560
561 if (mUsesFragData)
562 {
563 out << "#define GL_USES_FRAG_DATA\n";
564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000566 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000568 out << "// Attributes\n";
569 out << attributes;
570 out << "\n"
571 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400572
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000573 if (mUsesPointSize)
574 {
575 out << "static float gl_PointSize = float(1);\n";
576 }
577
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000578 if (mUsesInstanceID)
579 {
580 out << "static int gl_InstanceID;";
581 }
582
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000583 out << "\n"
584 "// Varyings\n";
585 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000586 out << "\n";
587
588 if (mUsesDepthRange)
589 {
590 out << "struct gl_DepthRangeParameters\n"
591 "{\n"
592 " float near;\n"
593 " float far;\n"
594 " float diff;\n"
595 "};\n"
596 "\n";
597 }
598
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200599 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000600 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800601 out << "cbuffer DriverConstants : register(b1)\n"
602 "{\n";
603
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000604 if (mUsesDepthRange)
605 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800606 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000607 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800608
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800609 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
610 // shaders. However, we declare it for all shaders (including Feature Level 10+).
611 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
612 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800613 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800614 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800615 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800616
617 out << "};\n"
618 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000619 }
620 else
621 {
622 if (mUsesDepthRange)
623 {
624 out << "uniform float3 dx_DepthRange : register(c0);\n";
625 }
626
Cooper Partine6664f02015-01-09 16:22:24 -0800627 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
628 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000629 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000630 }
631
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000632 if (mUsesDepthRange)
633 {
634 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
635 "\n";
636 }
637
Jamie Madillf91ce812014-06-13 10:04:34 -0400638 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000639 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400640 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000641 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400642 out << flaggedStructs;
643 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000644 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400645 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000646
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400647 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
648 {
649 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400650 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000651 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400652 switch(textureFunction->sampler)
653 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400654 case EbtSampler2D: out << "int2 "; break;
655 case EbtSampler3D: out << "int3 "; break;
656 case EbtSamplerCube: out << "int2 "; break;
657 case EbtSampler2DArray: out << "int3 "; break;
658 case EbtISampler2D: out << "int2 "; break;
659 case EbtISampler3D: out << "int3 "; break;
660 case EbtISamplerCube: out << "int2 "; break;
661 case EbtISampler2DArray: out << "int3 "; break;
662 case EbtUSampler2D: out << "int2 "; break;
663 case EbtUSampler3D: out << "int3 "; break;
664 case EbtUSamplerCube: out << "int2 "; break;
665 case EbtUSampler2DArray: out << "int3 "; break;
666 case EbtSampler2DShadow: out << "int2 "; break;
667 case EbtSamplerCubeShadow: out << "int2 "; break;
668 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400669 default: UNREACHABLE();
670 }
671 }
672 else // Sampling function
673 {
674 switch(textureFunction->sampler)
675 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400676 case EbtSampler2D: out << "float4 "; break;
677 case EbtSampler3D: out << "float4 "; break;
678 case EbtSamplerCube: out << "float4 "; break;
679 case EbtSampler2DArray: out << "float4 "; break;
680 case EbtISampler2D: out << "int4 "; break;
681 case EbtISampler3D: out << "int4 "; break;
682 case EbtISamplerCube: out << "int4 "; break;
683 case EbtISampler2DArray: out << "int4 "; break;
684 case EbtUSampler2D: out << "uint4 "; break;
685 case EbtUSampler3D: out << "uint4 "; break;
686 case EbtUSamplerCube: out << "uint4 "; break;
687 case EbtUSampler2DArray: out << "uint4 "; break;
688 case EbtSampler2DShadow: out << "float "; break;
689 case EbtSamplerCubeShadow: out << "float "; break;
690 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400691 default: UNREACHABLE();
692 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000693 }
694
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400695 // Function name
696 out << textureFunction->name();
697
698 // Argument list
699 int hlslCoords = 4;
700
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200701 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000702 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400703 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000704 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400705 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
706 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
707 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000708 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400709
Nicolas Capens75fb4752013-07-10 15:14:47 -0400710 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000711 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400712 case TextureFunction::IMPLICIT: break;
713 case TextureFunction::BIAS: hlslCoords = 4; break;
714 case TextureFunction::LOD: hlslCoords = 4; break;
715 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400716 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400717 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000718 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400719 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200720 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400721 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200722 hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler);
723 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400724 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200725 out << TextureString(textureFunction->sampler) << " x, "
726 << SamplerString(textureFunction->sampler) << " s";
727 }
728 else
729 {
730 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
731 out << "const uint samplerIndex";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400732 }
733 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400734
Nicolas Capensfc014542014-02-18 14:47:13 -0500735 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400736 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500737 switch(textureFunction->coords)
738 {
739 case 2: out << ", int2 t"; break;
740 case 3: out << ", int3 t"; break;
741 default: UNREACHABLE();
742 }
743 }
744 else // Floating-point coordinates (except textureSize)
745 {
746 switch(textureFunction->coords)
747 {
748 case 1: out << ", int lod"; break; // textureSize()
749 case 2: out << ", float2 t"; break;
750 case 3: out << ", float3 t"; break;
751 case 4: out << ", float4 t"; break;
752 default: UNREACHABLE();
753 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000754 }
755
Nicolas Capensd11d5492014-02-19 17:06:10 -0500756 if (textureFunction->method == TextureFunction::GRAD)
757 {
758 switch(textureFunction->sampler)
759 {
760 case EbtSampler2D:
761 case EbtISampler2D:
762 case EbtUSampler2D:
763 case EbtSampler2DArray:
764 case EbtISampler2DArray:
765 case EbtUSampler2DArray:
766 case EbtSampler2DShadow:
767 case EbtSampler2DArrayShadow:
768 out << ", float2 ddx, float2 ddy";
769 break;
770 case EbtSampler3D:
771 case EbtISampler3D:
772 case EbtUSampler3D:
773 case EbtSamplerCube:
774 case EbtISamplerCube:
775 case EbtUSamplerCube:
776 case EbtSamplerCubeShadow:
777 out << ", float3 ddx, float3 ddy";
778 break;
779 default: UNREACHABLE();
780 }
781 }
782
Nicolas Capens75fb4752013-07-10 15:14:47 -0400783 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000784 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400785 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400786 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400787 case TextureFunction::LOD: out << ", float lod"; break;
788 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400789 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400790 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500791 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500792 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400793 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000794 }
795
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500796 if (textureFunction->offset)
797 {
798 switch(textureFunction->sampler)
799 {
800 case EbtSampler2D: out << ", int2 offset"; break;
801 case EbtSampler3D: out << ", int3 offset"; break;
802 case EbtSampler2DArray: out << ", int2 offset"; break;
803 case EbtISampler2D: out << ", int2 offset"; break;
804 case EbtISampler3D: out << ", int3 offset"; break;
805 case EbtISampler2DArray: out << ", int2 offset"; break;
806 case EbtUSampler2D: out << ", int2 offset"; break;
807 case EbtUSampler3D: out << ", int3 offset"; break;
808 case EbtUSampler2DArray: out << ", int2 offset"; break;
809 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500810 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500811 default: UNREACHABLE();
812 }
813 }
814
Nicolas Capens84cfa122014-04-14 13:48:45 -0400815 if (textureFunction->method == TextureFunction::BIAS ||
816 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500817 {
818 out << ", float bias";
819 }
820
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400821 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400822 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400823
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200824 // In some cases we use a variable to store the texture/sampler objects, but to work around
825 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
826 // sampling we need to call the function directly on a reference to the array. The bug was
827 // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests.
828 TString textureReference("x");
829 TString samplerReference("s");
830 if (mOutputType == SH_HLSL_4_1_OUTPUT)
831 {
832 TString suffix = TextureGroupSuffix(textureFunction->sampler);
833 if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D)
834 {
835 textureReference = TString("textures") + suffix + "[samplerIndex]";
836 samplerReference = TString("samplers") + suffix + "[samplerIndex]";
837 }
838 else
839 {
840 out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix
841 << ";\n";
842 textureReference = TString("textures") + suffix + "[textureIndex]";
843 out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset"
844 << suffix << ";\n";
845 samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]";
846 }
847 }
848
Nicolas Capens75fb4752013-07-10 15:14:47 -0400849 if (textureFunction->method == TextureFunction::SIZE)
850 {
851 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
852 {
853 if (IsSamplerArray(textureFunction->sampler))
854 {
855 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200856 << " " << textureReference
857 << ".GetDimensions(lod, width, height, layers, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400858 }
859 else
860 {
861 out << " uint width; uint height; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200862 << " " << textureReference
863 << ".GetDimensions(lod, width, height, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400864 }
865 }
866 else if (IsSampler3D(textureFunction->sampler))
867 {
868 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200869 << " " << textureReference
870 << ".GetDimensions(lod, width, height, depth, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400871 }
872 else UNREACHABLE();
873
874 switch(textureFunction->sampler)
875 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400876 case EbtSampler2D: out << " return int2(width, height);"; break;
877 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
878 case EbtSamplerCube: out << " return int2(width, height);"; break;
879 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
880 case EbtISampler2D: out << " return int2(width, height);"; break;
881 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
882 case EbtISamplerCube: out << " return int2(width, height);"; break;
883 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
884 case EbtUSampler2D: out << " return int2(width, height);"; break;
885 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
886 case EbtUSamplerCube: out << " return int2(width, height);"; break;
887 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
888 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
889 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
890 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400891 default: UNREACHABLE();
892 }
893 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400894 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400895 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500896 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
897 {
898 out << " float width; float height; float layers; float levels;\n";
899
900 out << " uint mip = 0;\n";
901
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200902 out << " " << textureReference
903 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500904
905 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
906 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
907 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
908 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
909
910 // FACE_POSITIVE_X = 000b
911 // FACE_NEGATIVE_X = 001b
912 // FACE_POSITIVE_Y = 010b
913 // FACE_NEGATIVE_Y = 011b
914 // FACE_POSITIVE_Z = 100b
915 // FACE_NEGATIVE_Z = 101b
916 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
917
918 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
919 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
920 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
921
922 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
923 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500924
925 // Mip level computation.
926 if (textureFunction->method == TextureFunction::IMPLICIT)
927 {
928 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
929 " float2 dx = ddx(tSized);\n"
930 " float2 dy = ddy(tSized);\n"
931 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"
932 " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200933 << " " << textureReference
934 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500935 }
Nicolas Capens0027fa92014-02-20 14:26:42 -0500936 }
937 else if (IsIntegerSampler(textureFunction->sampler) &&
938 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400939 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400940 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400941 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400942 if (IsSamplerArray(textureFunction->sampler))
943 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400944 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400945
Nicolas Capens9edebd62013-08-06 10:59:10 -0400946 if (textureFunction->method == TextureFunction::LOD0)
947 {
948 out << " uint mip = 0;\n";
949 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400950 else if (textureFunction->method == TextureFunction::LOD0BIAS)
951 {
952 out << " uint mip = bias;\n";
953 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400954 else
955 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200956
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200957 out << " " << textureReference
958 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400959 if (textureFunction->method == TextureFunction::IMPLICIT ||
960 textureFunction->method == TextureFunction::BIAS)
961 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200962 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400963 " float dx = length(ddx(tSized));\n"
964 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500965 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400966
967 if (textureFunction->method == TextureFunction::BIAS)
968 {
969 out << " lod += bias;\n";
970 }
971 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500972 else if (textureFunction->method == TextureFunction::GRAD)
973 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200974 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500975 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400976
977 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
978 }
979
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200980 out << " " << textureReference
981 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400982 }
983 else
984 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400985 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400986
Nicolas Capens9edebd62013-08-06 10:59:10 -0400987 if (textureFunction->method == TextureFunction::LOD0)
988 {
989 out << " uint mip = 0;\n";
990 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400991 else if (textureFunction->method == TextureFunction::LOD0BIAS)
992 {
993 out << " uint mip = bias;\n";
994 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400995 else
996 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200997 out << " " << textureReference
998 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200999
Nicolas Capens9edebd62013-08-06 10:59:10 -04001000 if (textureFunction->method == TextureFunction::IMPLICIT ||
1001 textureFunction->method == TextureFunction::BIAS)
1002 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001003 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001004 " float dx = length(ddx(tSized));\n"
1005 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001006 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001007
1008 if (textureFunction->method == TextureFunction::BIAS)
1009 {
1010 out << " lod += bias;\n";
1011 }
1012 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001013 else if (textureFunction->method == TextureFunction::GRAD)
1014 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001015 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001016 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001017
1018 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1019 }
1020
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001021 out << " " << textureReference
1022 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001023 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001024 }
1025 else if (IsSampler3D(textureFunction->sampler))
1026 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001027 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001028
Nicolas Capens9edebd62013-08-06 10:59:10 -04001029 if (textureFunction->method == TextureFunction::LOD0)
1030 {
1031 out << " uint mip = 0;\n";
1032 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001033 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1034 {
1035 out << " uint mip = bias;\n";
1036 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001037 else
1038 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001039 out << " " << textureReference
1040 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001041
Nicolas Capens9edebd62013-08-06 10:59:10 -04001042 if (textureFunction->method == TextureFunction::IMPLICIT ||
1043 textureFunction->method == TextureFunction::BIAS)
1044 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001045 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1046 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001047 " float dx = length(ddx(tSized));\n"
1048 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001049 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001050
1051 if (textureFunction->method == TextureFunction::BIAS)
1052 {
1053 out << " lod += bias;\n";
1054 }
1055 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001056 else if (textureFunction->method == TextureFunction::GRAD)
1057 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001058 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001059 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001060
1061 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1062 }
1063
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001064 out << " " << textureReference
1065 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001066 }
1067 else UNREACHABLE();
1068 }
1069
1070 out << " return ";
1071
1072 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001073 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001074 {
1075 switch(textureFunction->sampler)
1076 {
1077 case EbtSampler2D: out << "tex2D"; break;
1078 case EbtSamplerCube: out << "texCUBE"; break;
1079 default: UNREACHABLE();
1080 }
1081
Nicolas Capens75fb4752013-07-10 15:14:47 -04001082 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001083 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001084 case TextureFunction::IMPLICIT:
1085 out << "(" << samplerReference << ", ";
1086 break;
1087 case TextureFunction::BIAS:
1088 out << "bias(" << samplerReference << ", ";
1089 break;
1090 case TextureFunction::LOD:
1091 out << "lod(" << samplerReference << ", ";
1092 break;
1093 case TextureFunction::LOD0:
1094 out << "lod(" << samplerReference << ", ";
1095 break;
1096 case TextureFunction::LOD0BIAS:
1097 out << "lod(" << samplerReference << ", ";
1098 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001099 default: UNREACHABLE();
1100 }
1101 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001102 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001103 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001104 if (textureFunction->method == TextureFunction::GRAD)
1105 {
1106 if (IsIntegerSampler(textureFunction->sampler))
1107 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001108 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001109 }
1110 else if (IsShadowSampler(textureFunction->sampler))
1111 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001112 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1113 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001114 }
1115 else
1116 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001117 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001118 }
1119 }
1120 else if (IsIntegerSampler(textureFunction->sampler) ||
1121 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001122 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001123 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001124 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001125 else if (IsShadowSampler(textureFunction->sampler))
1126 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001127 switch(textureFunction->method)
1128 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001129 case TextureFunction::IMPLICIT:
1130 out << "" << textureReference << ".SampleCmp(" << samplerReference
1131 << ", ";
1132 break;
1133 case TextureFunction::BIAS:
1134 out << "" << textureReference << ".SampleCmp(" << samplerReference
1135 << ", ";
1136 break;
1137 case TextureFunction::LOD:
1138 out << "" << textureReference << ".SampleCmp(" << samplerReference
1139 << ", ";
1140 break;
1141 case TextureFunction::LOD0:
1142 out << "" << textureReference << ".SampleCmpLevelZero("
1143 << samplerReference << ", ";
1144 break;
1145 case TextureFunction::LOD0BIAS:
1146 out << "" << textureReference << ".SampleCmpLevelZero("
1147 << samplerReference << ", ";
1148 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001149 default: UNREACHABLE();
1150 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001151 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001152 else
1153 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001154 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001155 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001156 case TextureFunction::IMPLICIT:
1157 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1158 break;
1159 case TextureFunction::BIAS:
1160 out << "" << textureReference << ".SampleBias(" << samplerReference
1161 << ", ";
1162 break;
1163 case TextureFunction::LOD:
1164 out << "" << textureReference << ".SampleLevel(" << samplerReference
1165 << ", ";
1166 break;
1167 case TextureFunction::LOD0:
1168 out << "" << textureReference << ".SampleLevel(" << samplerReference
1169 << ", ";
1170 break;
1171 case TextureFunction::LOD0BIAS:
1172 out << "" << textureReference << ".SampleLevel(" << samplerReference
1173 << ", ";
1174 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001175 default: UNREACHABLE();
1176 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001177 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001178 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001179 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001180
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001181 // Integer sampling requires integer addresses
1182 TString addressx = "";
1183 TString addressy = "";
1184 TString addressz = "";
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001185 TString close = "";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001186
Nicolas Capensfc014542014-02-18 14:47:13 -05001187 if (IsIntegerSampler(textureFunction->sampler) ||
1188 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001189 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001190 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001191 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001192 case 2: out << "int3("; break;
1193 case 3: out << "int4("; break;
1194 default: UNREACHABLE();
1195 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001196
Nicolas Capensfc014542014-02-18 14:47:13 -05001197 // Convert from normalized floating-point to integer
1198 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001199 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001200 addressx = "int(floor(width * frac((";
1201 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001202
Nicolas Capensfc014542014-02-18 14:47:13 -05001203 if (IsSamplerArray(textureFunction->sampler))
1204 {
1205 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1206 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001207 else if (IsSamplerCube(textureFunction->sampler))
Nicolas Capens0027fa92014-02-20 14:26:42 -05001208 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001209 addressz = "((((";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001210 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001211 else
1212 {
1213 addressz = "int(floor(depth * frac((";
1214 }
1215
1216 close = "))))";
Nicolas Capensfc014542014-02-18 14:47:13 -05001217 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001218 }
1219 else
1220 {
1221 switch(hlslCoords)
1222 {
1223 case 2: out << "float2("; break;
1224 case 3: out << "float3("; break;
1225 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001226 default: UNREACHABLE();
1227 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001228 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001229
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001230 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001231
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001232 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001233 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001234 switch(textureFunction->coords)
1235 {
1236 case 3: proj = " / t.z"; break;
1237 case 4: proj = " / t.w"; break;
1238 default: UNREACHABLE();
1239 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001240 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001241
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001242 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001243
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001244 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001245 {
1246 if (hlslCoords >= 3)
1247 {
1248 if (textureFunction->coords < 3)
1249 {
1250 out << ", 0";
1251 }
1252 else
1253 {
1254 out << ", t.z" + proj;
1255 }
1256 }
1257
1258 if (hlslCoords == 4)
1259 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001260 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001261 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001262 case TextureFunction::BIAS: out << ", bias"; break;
1263 case TextureFunction::LOD: out << ", lod"; break;
1264 case TextureFunction::LOD0: out << ", 0"; break;
1265 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001266 default: UNREACHABLE();
1267 }
1268 }
1269
1270 out << "));\n";
1271 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001272 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001273 {
1274 if (hlslCoords >= 3)
1275 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001276 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1277 {
1278 out << ", face";
1279 }
1280 else
1281 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001282 out << ", " + addressz + ("t.z" + proj) + close;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001283 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001284 }
1285
Nicolas Capensd11d5492014-02-19 17:06:10 -05001286 if (textureFunction->method == TextureFunction::GRAD)
1287 {
1288 if (IsIntegerSampler(textureFunction->sampler))
1289 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001290 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001291 }
1292 else if (IsShadowSampler(textureFunction->sampler))
1293 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001294 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001295 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001296 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001297 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1298 // The resulting third component of P' in the shadow forms is used as Dref
1299 out << "), t.z" << proj;
1300 }
1301 else
1302 {
1303 switch(textureFunction->coords)
1304 {
1305 case 3: out << "), t.z"; break;
1306 case 4: out << "), t.w"; break;
1307 default: UNREACHABLE();
1308 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001309 }
1310 }
1311 else
1312 {
1313 out << "), ddx, ddy";
1314 }
1315 }
1316 else if (IsIntegerSampler(textureFunction->sampler) ||
1317 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001318 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001319 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001320 }
1321 else if (IsShadowSampler(textureFunction->sampler))
1322 {
1323 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001324 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001325 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001326 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1327 // The resulting third component of P' in the shadow forms is used as Dref
1328 out << "), t.z" << proj;
1329 }
1330 else
1331 {
1332 switch(textureFunction->coords)
1333 {
1334 case 3: out << "), t.z"; break;
1335 case 4: out << "), t.w"; break;
1336 default: UNREACHABLE();
1337 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001338 }
1339 }
1340 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001341 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001342 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001343 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001344 case TextureFunction::IMPLICIT: out << ")"; break;
1345 case TextureFunction::BIAS: out << "), bias"; break;
1346 case TextureFunction::LOD: out << "), lod"; break;
1347 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001348 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001349 default: UNREACHABLE();
1350 }
1351 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001352
1353 if (textureFunction->offset)
1354 {
1355 out << ", offset";
1356 }
1357
1358 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001359 }
1360 else UNREACHABLE();
1361 }
1362
1363 out << "\n"
1364 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001365 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001366 }
1367
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001368 if (mUsesFragCoord)
1369 {
1370 out << "#define GL_USES_FRAG_COORD\n";
1371 }
1372
1373 if (mUsesPointCoord)
1374 {
1375 out << "#define GL_USES_POINT_COORD\n";
1376 }
1377
1378 if (mUsesFrontFacing)
1379 {
1380 out << "#define GL_USES_FRONT_FACING\n";
1381 }
1382
1383 if (mUsesPointSize)
1384 {
1385 out << "#define GL_USES_POINT_SIZE\n";
1386 }
1387
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001388 if (mUsesFragDepth)
1389 {
1390 out << "#define GL_USES_FRAG_DEPTH\n";
1391 }
1392
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001393 if (mUsesDepthRange)
1394 {
1395 out << "#define GL_USES_DEPTH_RANGE\n";
1396 }
1397
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001398 if (mUsesXor)
1399 {
1400 out << "bool xor(bool p, bool q)\n"
1401 "{\n"
1402 " return (p || q) && !(p && q);\n"
1403 "}\n"
1404 "\n";
1405 }
1406
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001407 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001408}
1409
1410void OutputHLSL::visitSymbol(TIntermSymbol *node)
1411{
Jamie Madill32aab012015-01-27 14:12:26 -05001412 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413
Jamie Madill570e04d2013-06-21 09:15:33 -04001414 // Handle accessing std140 structs by value
1415 if (mFlaggedStructMappedNames.count(node) > 0)
1416 {
1417 out << mFlaggedStructMappedNames[node];
1418 return;
1419 }
1420
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001421 TString name = node->getSymbol();
1422
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001423 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001424 {
1425 mUsesDepthRange = true;
1426 out << name;
1427 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001428 else
1429 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001430 TQualifier qualifier = node->getQualifier();
1431
1432 if (qualifier == EvqUniform)
1433 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001434 const TType &nodeType = node->getType();
1435 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001436
1437 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001438 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001439 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001440 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001441 else
1442 {
1443 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001444 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001445
Jamie Madill2e295e22015-04-29 10:41:33 -04001446 ensureStructDefined(nodeType);
1447
Jamie Madill033dae62014-06-18 12:56:28 -04001448 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001449 }
Jamie Madill19571812013-08-12 15:26:34 -07001450 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001451 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001452 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001453 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001454 }
Jamie Madill033dae62014-06-18 12:56:28 -04001455 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001456 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001457 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001458 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001459 }
Jamie Madill19571812013-08-12 15:26:34 -07001460 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001461 {
1462 mReferencedOutputVariables[name] = node;
1463 out << "out_" << name;
1464 }
1465 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001466 {
1467 out << "gl_Color[0]";
1468 mUsesFragColor = true;
1469 }
1470 else if (qualifier == EvqFragData)
1471 {
1472 out << "gl_Color";
1473 mUsesFragData = true;
1474 }
1475 else if (qualifier == EvqFragCoord)
1476 {
1477 mUsesFragCoord = true;
1478 out << name;
1479 }
1480 else if (qualifier == EvqPointCoord)
1481 {
1482 mUsesPointCoord = true;
1483 out << name;
1484 }
1485 else if (qualifier == EvqFrontFacing)
1486 {
1487 mUsesFrontFacing = true;
1488 out << name;
1489 }
1490 else if (qualifier == EvqPointSize)
1491 {
1492 mUsesPointSize = true;
1493 out << name;
1494 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001495 else if (qualifier == EvqInstanceID)
1496 {
1497 mUsesInstanceID = true;
1498 out << name;
1499 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001500 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001501 {
1502 mUsesFragDepth = true;
1503 out << "gl_Depth";
1504 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001505 else
1506 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001507 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001508 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001509 }
1510}
1511
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001512void OutputHLSL::visitRaw(TIntermRaw *node)
1513{
Jamie Madill32aab012015-01-27 14:12:26 -05001514 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001515}
1516
Olli Etuaho7fb49552015-03-18 17:27:44 +02001517void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1518{
1519 if (type.isScalar() && !type.isArray())
1520 {
1521 if (op == EOpEqual)
1522 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001523 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001524 }
1525 else
1526 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001527 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001528 }
1529 }
1530 else
1531 {
1532 if (visit == PreVisit && op == EOpNotEqual)
1533 {
1534 out << "!";
1535 }
1536
1537 if (type.isArray())
1538 {
1539 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001540 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001541 }
1542 else if (type.getBasicType() == EbtStruct)
1543 {
1544 const TStructure &structure = *type.getStruct();
1545 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001546 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001547 }
1548 else
1549 {
1550 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001551 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001552 }
1553 }
1554}
1555
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001556bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1557{
Jamie Madill32aab012015-01-27 14:12:26 -05001558 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001559
Jamie Madill570e04d2013-06-21 09:15:33 -04001560 // Handle accessing std140 structs by value
1561 if (mFlaggedStructMappedNames.count(node) > 0)
1562 {
1563 out << mFlaggedStructMappedNames[node];
1564 return false;
1565 }
1566
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001567 switch (node->getOp())
1568 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001569 case EOpAssign:
1570 if (node->getLeft()->isArray())
1571 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001572 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1573 if (rightAgg != nullptr && rightAgg->isConstructor())
1574 {
1575 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1576 out << functionName << "(";
1577 node->getLeft()->traverse(this);
1578 TIntermSequence *seq = rightAgg->getSequence();
1579 for (auto &arrayElement : *seq)
1580 {
1581 out << ", ";
1582 arrayElement->traverse(this);
1583 }
1584 out << ")";
1585 return false;
1586 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001587 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1588 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1589
1590 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001591 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001592 }
1593 else
1594 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001595 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001596 }
1597 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001598 case EOpInitialize:
1599 if (visit == PreVisit)
1600 {
1601 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1602 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1603 // new variable is created before the assignment is evaluated), so we need to convert
1604 // this to "float t = x, x = t;".
1605
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001606 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001607 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001608 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001609
Jamie Madill37997142015-01-28 10:06:34 -05001610 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1611 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001612 {
Jamie Madill37997142015-01-28 10:06:34 -05001613 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001614 // after we initialize uniforms.
1615 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1616 deferredInit->setLeft(node->getLeft());
1617 deferredInit->setRight(node->getRight());
1618 deferredInit->setType(node->getType());
1619 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001620 const TString &initString = initializer(node->getType());
1621 node->setRight(new TIntermRaw(node->getType(), initString));
1622 }
1623 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1624 {
1625 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001626 return false;
1627 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001628 else if (writeConstantInitialization(out, symbolNode, expression))
1629 {
1630 return false;
1631 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001632 }
1633 else if (visit == InVisit)
1634 {
1635 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001636 }
1637 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001638 case EOpAddAssign:
1639 outputTriplet(out, visit, "(", " += ", ")");
1640 break;
1641 case EOpSubAssign:
1642 outputTriplet(out, visit, "(", " -= ", ")");
1643 break;
1644 case EOpMulAssign:
1645 outputTriplet(out, visit, "(", " *= ", ")");
1646 break;
1647 case EOpVectorTimesScalarAssign:
1648 outputTriplet(out, visit, "(", " *= ", ")");
1649 break;
1650 case EOpMatrixTimesScalarAssign:
1651 outputTriplet(out, visit, "(", " *= ", ")");
1652 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001653 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001654 if (visit == PreVisit)
1655 {
1656 out << "(";
1657 }
1658 else if (visit == InVisit)
1659 {
1660 out << " = mul(";
1661 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001662 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001663 }
1664 else
1665 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001666 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001667 }
1668 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001669 case EOpMatrixTimesMatrixAssign:
1670 if (visit == PreVisit)
1671 {
1672 out << "(";
1673 }
1674 else if (visit == InVisit)
1675 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001676 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001677 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001678 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001679 }
1680 else
1681 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001682 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001683 }
1684 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001685 case EOpDivAssign:
1686 outputTriplet(out, visit, "(", " /= ", ")");
1687 break;
1688 case EOpIModAssign:
1689 outputTriplet(out, visit, "(", " %= ", ")");
1690 break;
1691 case EOpBitShiftLeftAssign:
1692 outputTriplet(out, visit, "(", " <<= ", ")");
1693 break;
1694 case EOpBitShiftRightAssign:
1695 outputTriplet(out, visit, "(", " >>= ", ")");
1696 break;
1697 case EOpBitwiseAndAssign:
1698 outputTriplet(out, visit, "(", " &= ", ")");
1699 break;
1700 case EOpBitwiseXorAssign:
1701 outputTriplet(out, visit, "(", " ^= ", ")");
1702 break;
1703 case EOpBitwiseOrAssign:
1704 outputTriplet(out, visit, "(", " |= ", ")");
1705 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001706 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001707 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001708 const TType& leftType = node->getLeft()->getType();
1709 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001710 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001711 if (visit == PreVisit)
1712 {
1713 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1714 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001715 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001716 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001717 return false;
1718 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001719 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001720 else
1721 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001722 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001723 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001724 }
1725 break;
1726 case EOpIndexIndirect:
1727 // We do not currently support indirect references to interface blocks
1728 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001729 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001730 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001731 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001732 if (visit == InVisit)
1733 {
1734 const TStructure* structure = node->getLeft()->getType().getStruct();
1735 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1736 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001737 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001738
1739 return false;
1740 }
1741 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001742 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001743 if (visit == InVisit)
1744 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001745 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1746 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1747 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001748 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001749
1750 return false;
1751 }
1752 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753 case EOpVectorSwizzle:
1754 if (visit == InVisit)
1755 {
1756 out << ".";
1757
1758 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1759
1760 if (swizzle)
1761 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001762 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001764 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001765 {
1766 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1767
1768 if (element)
1769 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001770 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001771
1772 switch (i)
1773 {
1774 case 0: out << "x"; break;
1775 case 1: out << "y"; break;
1776 case 2: out << "z"; break;
1777 case 3: out << "w"; break;
1778 default: UNREACHABLE();
1779 }
1780 }
1781 else UNREACHABLE();
1782 }
1783 }
1784 else UNREACHABLE();
1785
1786 return false; // Fully processed
1787 }
1788 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001789 case EOpAdd:
1790 outputTriplet(out, visit, "(", " + ", ")");
1791 break;
1792 case EOpSub:
1793 outputTriplet(out, visit, "(", " - ", ")");
1794 break;
1795 case EOpMul:
1796 outputTriplet(out, visit, "(", " * ", ")");
1797 break;
1798 case EOpDiv:
1799 outputTriplet(out, visit, "(", " / ", ")");
1800 break;
1801 case EOpIMod:
1802 outputTriplet(out, visit, "(", " % ", ")");
1803 break;
1804 case EOpBitShiftLeft:
1805 outputTriplet(out, visit, "(", " << ", ")");
1806 break;
1807 case EOpBitShiftRight:
1808 outputTriplet(out, visit, "(", " >> ", ")");
1809 break;
1810 case EOpBitwiseAnd:
1811 outputTriplet(out, visit, "(", " & ", ")");
1812 break;
1813 case EOpBitwiseXor:
1814 outputTriplet(out, visit, "(", " ^ ", ")");
1815 break;
1816 case EOpBitwiseOr:
1817 outputTriplet(out, visit, "(", " | ", ")");
1818 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001819 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001820 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001821 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001822 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001823 case EOpLessThan:
1824 outputTriplet(out, visit, "(", " < ", ")");
1825 break;
1826 case EOpGreaterThan:
1827 outputTriplet(out, visit, "(", " > ", ")");
1828 break;
1829 case EOpLessThanEqual:
1830 outputTriplet(out, visit, "(", " <= ", ")");
1831 break;
1832 case EOpGreaterThanEqual:
1833 outputTriplet(out, visit, "(", " >= ", ")");
1834 break;
1835 case EOpVectorTimesScalar:
1836 outputTriplet(out, visit, "(", " * ", ")");
1837 break;
1838 case EOpMatrixTimesScalar:
1839 outputTriplet(out, visit, "(", " * ", ")");
1840 break;
1841 case EOpVectorTimesMatrix:
1842 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1843 break;
1844 case EOpMatrixTimesVector:
1845 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1846 break;
1847 case EOpMatrixTimesMatrix:
1848 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1849 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001850 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001851 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1852 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001853 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001854 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001855 case EOpLogicalXor:
1856 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001857 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001858 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001859 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001860 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1861 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001862 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001863 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001864 default: UNREACHABLE();
1865 }
1866
1867 return true;
1868}
1869
1870bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1871{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001872 TInfoSinkBase &out = getInfoSink();
1873
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 switch (node->getOp())
1875 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001876 case EOpNegative:
1877 outputTriplet(out, visit, "(-", "", ")");
1878 break;
1879 case EOpPositive:
1880 outputTriplet(out, visit, "(+", "", ")");
1881 break;
1882 case EOpVectorLogicalNot:
1883 outputTriplet(out, visit, "(!", "", ")");
1884 break;
1885 case EOpLogicalNot:
1886 outputTriplet(out, visit, "(!", "", ")");
1887 break;
1888 case EOpBitwiseNot:
1889 outputTriplet(out, visit, "(~", "", ")");
1890 break;
1891 case EOpPostIncrement:
1892 outputTriplet(out, visit, "(", "", "++)");
1893 break;
1894 case EOpPostDecrement:
1895 outputTriplet(out, visit, "(", "", "--)");
1896 break;
1897 case EOpPreIncrement:
1898 outputTriplet(out, visit, "(++", "", ")");
1899 break;
1900 case EOpPreDecrement:
1901 outputTriplet(out, visit, "(--", "", ")");
1902 break;
1903 case EOpRadians:
1904 outputTriplet(out, visit, "radians(", "", ")");
1905 break;
1906 case EOpDegrees:
1907 outputTriplet(out, visit, "degrees(", "", ")");
1908 break;
1909 case EOpSin:
1910 outputTriplet(out, visit, "sin(", "", ")");
1911 break;
1912 case EOpCos:
1913 outputTriplet(out, visit, "cos(", "", ")");
1914 break;
1915 case EOpTan:
1916 outputTriplet(out, visit, "tan(", "", ")");
1917 break;
1918 case EOpAsin:
1919 outputTriplet(out, visit, "asin(", "", ")");
1920 break;
1921 case EOpAcos:
1922 outputTriplet(out, visit, "acos(", "", ")");
1923 break;
1924 case EOpAtan:
1925 outputTriplet(out, visit, "atan(", "", ")");
1926 break;
1927 case EOpSinh:
1928 outputTriplet(out, visit, "sinh(", "", ")");
1929 break;
1930 case EOpCosh:
1931 outputTriplet(out, visit, "cosh(", "", ")");
1932 break;
1933 case EOpTanh:
1934 outputTriplet(out, visit, "tanh(", "", ")");
1935 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001936 case EOpAsinh:
1937 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001938 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001939 break;
1940 case EOpAcosh:
1941 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001942 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001943 break;
1944 case EOpAtanh:
1945 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001946 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001947 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001948 case EOpExp:
1949 outputTriplet(out, visit, "exp(", "", ")");
1950 break;
1951 case EOpLog:
1952 outputTriplet(out, visit, "log(", "", ")");
1953 break;
1954 case EOpExp2:
1955 outputTriplet(out, visit, "exp2(", "", ")");
1956 break;
1957 case EOpLog2:
1958 outputTriplet(out, visit, "log2(", "", ")");
1959 break;
1960 case EOpSqrt:
1961 outputTriplet(out, visit, "sqrt(", "", ")");
1962 break;
1963 case EOpInverseSqrt:
1964 outputTriplet(out, visit, "rsqrt(", "", ")");
1965 break;
1966 case EOpAbs:
1967 outputTriplet(out, visit, "abs(", "", ")");
1968 break;
1969 case EOpSign:
1970 outputTriplet(out, visit, "sign(", "", ")");
1971 break;
1972 case EOpFloor:
1973 outputTriplet(out, visit, "floor(", "", ")");
1974 break;
1975 case EOpTrunc:
1976 outputTriplet(out, visit, "trunc(", "", ")");
1977 break;
1978 case EOpRound:
1979 outputTriplet(out, visit, "round(", "", ")");
1980 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001981 case EOpRoundEven:
1982 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001983 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001984 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001985 case EOpCeil:
1986 outputTriplet(out, visit, "ceil(", "", ")");
1987 break;
1988 case EOpFract:
1989 outputTriplet(out, visit, "frac(", "", ")");
1990 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301991 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001992 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05301993 mRequiresIEEEStrictCompiling = true;
1994 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001995 case EOpIsInf:
1996 outputTriplet(out, visit, "isinf(", "", ")");
1997 break;
1998 case EOpFloatBitsToInt:
1999 outputTriplet(out, visit, "asint(", "", ")");
2000 break;
2001 case EOpFloatBitsToUint:
2002 outputTriplet(out, visit, "asuint(", "", ")");
2003 break;
2004 case EOpIntBitsToFloat:
2005 outputTriplet(out, visit, "asfloat(", "", ")");
2006 break;
2007 case EOpUintBitsToFloat:
2008 outputTriplet(out, visit, "asfloat(", "", ")");
2009 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002010 case EOpPackSnorm2x16:
2011 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002012 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002013 break;
2014 case EOpPackUnorm2x16:
2015 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002016 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002017 break;
2018 case EOpPackHalf2x16:
2019 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002020 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002021 break;
2022 case EOpUnpackSnorm2x16:
2023 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002024 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002025 break;
2026 case EOpUnpackUnorm2x16:
2027 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002028 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002029 break;
2030 case EOpUnpackHalf2x16:
2031 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002032 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002033 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002034 case EOpLength:
2035 outputTriplet(out, visit, "length(", "", ")");
2036 break;
2037 case EOpNormalize:
2038 outputTriplet(out, visit, "normalize(", "", ")");
2039 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002040 case EOpDFdx:
2041 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2042 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002043 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002044 }
2045 else
2046 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002047 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002048 }
2049 break;
2050 case EOpDFdy:
2051 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2052 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002053 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002054 }
2055 else
2056 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002057 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002058 }
2059 break;
2060 case EOpFwidth:
2061 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2062 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002063 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002064 }
2065 else
2066 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002067 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002068 }
2069 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002070 case EOpTranspose:
2071 outputTriplet(out, visit, "transpose(", "", ")");
2072 break;
2073 case EOpDeterminant:
2074 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2075 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002076 case EOpInverse:
2077 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002078 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002079 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002080
Jamie Madill8c46ab12015-12-07 16:39:19 -05002081 case EOpAny:
2082 outputTriplet(out, visit, "any(", "", ")");
2083 break;
2084 case EOpAll:
2085 outputTriplet(out, visit, "all(", "", ")");
2086 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087 default: UNREACHABLE();
2088 }
2089
2090 return true;
2091}
2092
2093bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2094{
Jamie Madill32aab012015-01-27 14:12:26 -05002095 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002096
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097 switch (node->getOp())
2098 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002099 case EOpSequence:
2100 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002101 if (mInsideFunction)
2102 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002103 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002104 out << "{\n";
2105 }
2106
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002107 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002108 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002109 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002110
Olli Etuahoa6f22092015-05-08 18:31:10 +03002111 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002112
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002113 // Don't output ; after case labels, they're terminated by :
2114 // This is needed especially since outputting a ; after a case statement would turn empty
2115 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002116 // Also no need to output ; after selection (if) statements or sequences. This is done just
2117 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002118 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2119 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002120 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002121 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002122 }
2123
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002124 if (mInsideFunction)
2125 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002126 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002127 out << "}\n";
2128 }
2129
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002130 return false;
2131 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132 case EOpDeclaration:
2133 if (visit == PreVisit)
2134 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002135 TIntermSequence *sequence = node->getSequence();
2136 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002137 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002138
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002139 if (variable &&
2140 (variable->getQualifier() == EvqTemporary ||
2141 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002143 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002144
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002145 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002147 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002149 out << "static ";
2150 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002151
Olli Etuahoa6f22092015-05-08 18:31:10 +03002152 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002153
Olli Etuahoa6f22092015-05-08 18:31:10 +03002154 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002155
Olli Etuahoa6f22092015-05-08 18:31:10 +03002156 if (symbol)
2157 {
2158 symbol->traverse(this);
2159 out << ArrayString(symbol->getType());
2160 out << " = " + initializer(symbol->getType());
2161 }
2162 else
2163 {
2164 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002165 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002167 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2168 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002169 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002170 }
2171 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002172 }
Jamie Madill033dae62014-06-18 12:56:28 -04002173 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002174 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002175 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002176 {
2177 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2178
2179 if (symbol)
2180 {
2181 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2182 mReferencedVaryings[symbol->getSymbol()] = symbol;
2183 }
2184 else
2185 {
2186 (*sit)->traverse(this);
2187 }
2188 }
2189 }
2190
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002191 return false;
2192 }
2193 else if (visit == InVisit)
2194 {
2195 out << ", ";
2196 }
2197 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002198 case EOpInvariantDeclaration:
2199 // Do not do any translation
2200 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002201 case EOpPrototype:
2202 if (visit == PreVisit)
2203 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002204 size_t index = mCallDag.findIndex(node);
2205 // Skip the prototype if it is not implemented (and thus not used)
2206 if (index == CallDAG::InvalidIndex)
2207 {
2208 return false;
2209 }
2210
Olli Etuaho59f9a642015-08-06 20:38:26 +03002211 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2212 out << TypeString(node->getType()) << " " << name
2213 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002214
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002215 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002216
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002217 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002218 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002219 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002220
2221 if (symbol)
2222 {
2223 out << argumentString(symbol);
2224
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002225 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002226 {
2227 out << ", ";
2228 }
2229 }
2230 else UNREACHABLE();
2231 }
2232
2233 out << ");\n";
2234
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002235 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002236 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2237 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002238 {
2239 mOutputLod0Function = true;
2240 node->traverse(this);
2241 mOutputLod0Function = false;
2242 }
2243
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002244 return false;
2245 }
2246 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002247 case EOpComma:
2248 outputTriplet(out, visit, "(", ", ", ")");
2249 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250 case EOpFunction:
2251 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002252 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002253 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002254
Corentin Wallez1239ee92015-03-19 14:38:02 -07002255 size_t index = mCallDag.findIndex(node);
2256 ASSERT(index != CallDAG::InvalidIndex);
2257 mCurrentFunctionMetadata = &mASTMetadataList[index];
2258
Jamie Madill033dae62014-06-18 12:56:28 -04002259 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002260
2261 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002263 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002265 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002266 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002267 out << DecorateFunctionIfNeeded(node->getNameObj())
2268 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002269 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002270
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002271 TIntermSequence *sequence = node->getSequence();
2272 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002273
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002274 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002275 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002276 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002277
2278 if (symbol)
2279 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002280 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002281
2282 out << argumentString(symbol);
2283
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002284 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002285 {
2286 out << ", ";
2287 }
2288 }
2289 else UNREACHABLE();
2290 }
2291
Olli Etuaho4785fec2015-05-18 16:09:37 +03002292 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002293
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002294 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002295 {
2296 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002297 TIntermNode *body = (*sequence)[1];
2298 // The function body node will output braces.
2299 ASSERT(IsSequence(body));
2300 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002301 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002303 else
2304 {
2305 out << "{}\n";
2306 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002307
Corentin Wallez1239ee92015-03-19 14:38:02 -07002308 mCurrentFunctionMetadata = nullptr;
2309
2310 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2311 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002312 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002313 ASSERT(name != "main");
2314 mOutputLod0Function = true;
2315 node->traverse(this);
2316 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002317 }
2318
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002319 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002320 }
2321 break;
2322 case EOpFunctionCall:
2323 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002324 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002325
Corentin Wallez1239ee92015-03-19 14:38:02 -07002326 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002327 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002329 if (node->isArray())
2330 {
2331 UNIMPLEMENTED();
2332 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002333 size_t index = mCallDag.findIndex(node);
2334 ASSERT(index != CallDAG::InvalidIndex);
2335 lod0 &= mASTMetadataList[index].mNeedsLod0;
2336
Olli Etuaho59f9a642015-08-06 20:38:26 +03002337 out << DecorateFunctionIfNeeded(node->getNameObj()) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338 }
2339 else
2340 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002341 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002342 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002343
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002344 TextureFunction textureFunction;
2345 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002346 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002347 textureFunction.method = TextureFunction::IMPLICIT;
2348 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002349 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002350
2351 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002352 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002353 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002354 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002355 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002356 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002357 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002358 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002359 }
Nicolas Capens46485082014-04-15 13:12:50 -04002360 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2361 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002362 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002363 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002364 }
Nicolas Capens46485082014-04-15 13:12:50 -04002365 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002366 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002367 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002368 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002369 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002370 else if (name == "textureSize")
2371 {
2372 textureFunction.method = TextureFunction::SIZE;
2373 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002374 else if (name == "textureOffset")
2375 {
2376 textureFunction.method = TextureFunction::IMPLICIT;
2377 textureFunction.offset = true;
2378 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002379 else if (name == "textureProjOffset")
2380 {
2381 textureFunction.method = TextureFunction::IMPLICIT;
2382 textureFunction.offset = true;
2383 textureFunction.proj = true;
2384 }
2385 else if (name == "textureLodOffset")
2386 {
2387 textureFunction.method = TextureFunction::LOD;
2388 textureFunction.offset = true;
2389 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002390 else if (name == "textureProjLodOffset")
2391 {
2392 textureFunction.method = TextureFunction::LOD;
2393 textureFunction.proj = true;
2394 textureFunction.offset = true;
2395 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002396 else if (name == "texelFetch")
2397 {
2398 textureFunction.method = TextureFunction::FETCH;
2399 }
2400 else if (name == "texelFetchOffset")
2401 {
2402 textureFunction.method = TextureFunction::FETCH;
2403 textureFunction.offset = true;
2404 }
Nicolas Capens46485082014-04-15 13:12:50 -04002405 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002406 {
2407 textureFunction.method = TextureFunction::GRAD;
2408 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002409 else if (name == "textureGradOffset")
2410 {
2411 textureFunction.method = TextureFunction::GRAD;
2412 textureFunction.offset = true;
2413 }
Nicolas Capens46485082014-04-15 13:12:50 -04002414 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002415 {
2416 textureFunction.method = TextureFunction::GRAD;
2417 textureFunction.proj = true;
2418 }
2419 else if (name == "textureProjGradOffset")
2420 {
2421 textureFunction.method = TextureFunction::GRAD;
2422 textureFunction.proj = true;
2423 textureFunction.offset = true;
2424 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002425 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002426
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002427 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002428 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002429 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2430
2431 if (textureFunction.offset)
2432 {
2433 mandatoryArgumentCount++;
2434 }
2435
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002436 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002437
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002438 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002439 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002440 if (bias)
2441 {
2442 textureFunction.method = TextureFunction::LOD0BIAS;
2443 }
2444 else
2445 {
2446 textureFunction.method = TextureFunction::LOD0;
2447 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002448 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002449 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002450 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002451 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002452 }
2453 }
2454
2455 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002456
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002457 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002459
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002460 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002461 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002462 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT &&
2463 IsSampler((*arg)->getAsTyped()->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002464 {
2465 out << "texture_";
2466 (*arg)->traverse(this);
2467 out << ", sampler_";
2468 }
2469
2470 (*arg)->traverse(this);
2471
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002472 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002473 {
2474 out << ", ";
2475 }
2476 }
2477
2478 out << ")";
2479
2480 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481 }
2482 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002483 case EOpParameters:
2484 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2485 break;
2486 case EOpConstructFloat:
2487 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2488 break;
2489 case EOpConstructVec2:
2490 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2491 break;
2492 case EOpConstructVec3:
2493 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2494 break;
2495 case EOpConstructVec4:
2496 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2497 break;
2498 case EOpConstructBool:
2499 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2500 break;
2501 case EOpConstructBVec2:
2502 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2503 break;
2504 case EOpConstructBVec3:
2505 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2506 break;
2507 case EOpConstructBVec4:
2508 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2509 break;
2510 case EOpConstructInt:
2511 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2512 break;
2513 case EOpConstructIVec2:
2514 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2515 break;
2516 case EOpConstructIVec3:
2517 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2518 break;
2519 case EOpConstructIVec4:
2520 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2521 break;
2522 case EOpConstructUInt:
2523 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2524 break;
2525 case EOpConstructUVec2:
2526 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2527 break;
2528 case EOpConstructUVec3:
2529 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2530 break;
2531 case EOpConstructUVec4:
2532 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2533 break;
2534 case EOpConstructMat2:
2535 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2536 break;
2537 case EOpConstructMat2x3:
2538 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2539 break;
2540 case EOpConstructMat2x4:
2541 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2542 break;
2543 case EOpConstructMat3x2:
2544 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2545 break;
2546 case EOpConstructMat3:
2547 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2548 break;
2549 case EOpConstructMat3x4:
2550 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2551 break;
2552 case EOpConstructMat4x2:
2553 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2554 break;
2555 case EOpConstructMat4x3:
2556 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2557 break;
2558 case EOpConstructMat4:
2559 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2560 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002561 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002562 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002563 if (node->getType().isArray())
2564 {
2565 UNIMPLEMENTED();
2566 }
Jamie Madill033dae62014-06-18 12:56:28 -04002567 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002568 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002569 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002570 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002571 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002572 case EOpLessThan:
2573 outputTriplet(out, visit, "(", " < ", ")");
2574 break;
2575 case EOpGreaterThan:
2576 outputTriplet(out, visit, "(", " > ", ")");
2577 break;
2578 case EOpLessThanEqual:
2579 outputTriplet(out, visit, "(", " <= ", ")");
2580 break;
2581 case EOpGreaterThanEqual:
2582 outputTriplet(out, visit, "(", " >= ", ")");
2583 break;
2584 case EOpVectorEqual:
2585 outputTriplet(out, visit, "(", " == ", ")");
2586 break;
2587 case EOpVectorNotEqual:
2588 outputTriplet(out, visit, "(", " != ", ")");
2589 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002590 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002591 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002592 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002593 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002594 case EOpModf:
2595 outputTriplet(out, visit, "modf(", ", ", ")");
2596 break;
2597 case EOpPow:
2598 outputTriplet(out, visit, "pow(", ", ", ")");
2599 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002601 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002602 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002603 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002605 case EOpMin:
2606 outputTriplet(out, visit, "min(", ", ", ")");
2607 break;
2608 case EOpMax:
2609 outputTriplet(out, visit, "max(", ", ", ")");
2610 break;
2611 case EOpClamp:
2612 outputTriplet(out, visit, "clamp(", ", ", ")");
2613 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302614 case EOpMix:
2615 {
2616 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2617 if (lastParamNode->getType().getBasicType() == EbtBool)
2618 {
2619 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2620 // so use emulated version.
2621 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002622 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302623 }
2624 else
2625 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002626 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302627 }
2628 }
2629 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002630 case EOpStep:
2631 outputTriplet(out, visit, "step(", ", ", ")");
2632 break;
2633 case EOpSmoothStep:
2634 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2635 break;
2636 case EOpDistance:
2637 outputTriplet(out, visit, "distance(", ", ", ")");
2638 break;
2639 case EOpDot:
2640 outputTriplet(out, visit, "dot(", ", ", ")");
2641 break;
2642 case EOpCross:
2643 outputTriplet(out, visit, "cross(", ", ", ")");
2644 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002645 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002646 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002647 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002648 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002649 case EOpReflect:
2650 outputTriplet(out, visit, "reflect(", ", ", ")");
2651 break;
2652 case EOpRefract:
2653 outputTriplet(out, visit, "refract(", ", ", ")");
2654 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002655 case EOpOuterProduct:
2656 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002657 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002658 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002659 case EOpMul:
2660 outputTriplet(out, visit, "(", " * ", ")");
2661 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002662 default: UNREACHABLE();
2663 }
2664
2665 return true;
2666}
2667
Jamie Madill8c46ab12015-12-07 16:39:19 -05002668void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002669{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002670 out << "if (";
2671
2672 node->getCondition()->traverse(this);
2673
2674 out << ")\n";
2675
Jamie Madill8c46ab12015-12-07 16:39:19 -05002676 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002677
2678 bool discard = false;
2679
2680 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002682 // The trueBlock child node will output braces.
2683 ASSERT(IsSequence(node->getTrueBlock()));
2684
Olli Etuahoa6f22092015-05-08 18:31:10 +03002685 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002686
Olli Etuahoa6f22092015-05-08 18:31:10 +03002687 // Detect true discard
2688 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2689 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002690 else
2691 {
2692 // TODO(oetuaho): Check if the semicolon inside is necessary.
2693 // It's there as a result of conservative refactoring of the output.
2694 out << "{;}\n";
2695 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002696
Jamie Madill8c46ab12015-12-07 16:39:19 -05002697 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002698
Olli Etuahoa6f22092015-05-08 18:31:10 +03002699 if (node->getFalseBlock())
2700 {
2701 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002702
Jamie Madill8c46ab12015-12-07 16:39:19 -05002703 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002704
Olli Etuaho4785fec2015-05-18 16:09:37 +03002705 // Either this is "else if" or the falseBlock child node will output braces.
2706 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2707
Olli Etuahoa6f22092015-05-08 18:31:10 +03002708 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002709
Jamie Madill8c46ab12015-12-07 16:39:19 -05002710 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002711
Olli Etuahoa6f22092015-05-08 18:31:10 +03002712 // Detect false discard
2713 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2714 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002715
Olli Etuahoa6f22092015-05-08 18:31:10 +03002716 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002717 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002718 {
2719 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002720 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002721}
2722
2723bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2724{
2725 TInfoSinkBase &out = getInfoSink();
2726
2727 ASSERT(!node->usesTernaryOperator());
2728
2729 if (!mInsideFunction)
2730 {
2731 // This is part of unfolded global initialization.
2732 mDeferredGlobalInitializers.push_back(node);
2733 return false;
2734 }
2735
2736 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002737 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002738 {
2739 out << "FLATTEN ";
2740 }
2741
Jamie Madill8c46ab12015-12-07 16:39:19 -05002742 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002743
2744 return false;
2745}
2746
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002747bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002748{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002749 TInfoSinkBase &out = getInfoSink();
2750
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002751 if (node->getStatementList())
2752 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002753 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002754 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002755 // The curly braces get written when visiting the statementList aggregate
2756 }
2757 else
2758 {
2759 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002760 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002761 }
2762 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002763}
2764
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002765bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002766{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002767 TInfoSinkBase &out = getInfoSink();
2768
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002769 if (node->hasCondition())
2770 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002771 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002772 return true;
2773 }
2774 else
2775 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002776 out << "default:\n";
2777 return false;
2778 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002779}
2780
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002781void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2782{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002783 TInfoSinkBase &out = getInfoSink();
2784 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002785}
2786
2787bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2788{
Nicolas Capens655fe362014-04-11 13:12:34 -04002789 mNestedLoopDepth++;
2790
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002791 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002792 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002793 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002794
Jamie Madill8c46ab12015-12-07 16:39:19 -05002795 TInfoSinkBase &out = getInfoSink();
2796
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002797 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002798 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002799 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002800 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002801 mInsideDiscontinuousLoop = wasDiscontinuous;
2802 mNestedLoopDepth--;
2803
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002804 return false;
2805 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002806 }
2807
Corentin Wallez1239ee92015-03-19 14:38:02 -07002808 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002809 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002811 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002812
Jamie Madill8c46ab12015-12-07 16:39:19 -05002813 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002814 }
2815 else
2816 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002817 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002818
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002819 if (node->getInit())
2820 {
2821 node->getInit()->traverse(this);
2822 }
2823
2824 out << "; ";
2825
alokp@chromium.org52813552010-11-16 18:36:09 +00002826 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002827 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002828 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 }
2830
2831 out << "; ";
2832
alokp@chromium.org52813552010-11-16 18:36:09 +00002833 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002834 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002835 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002836 }
2837
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002838 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002839
Jamie Madill8c46ab12015-12-07 16:39:19 -05002840 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002841 }
2842
2843 if (node->getBody())
2844 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002845 // The loop body node will output braces.
2846 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002847 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002848 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002849 else
2850 {
2851 // TODO(oetuaho): Check if the semicolon inside is necessary.
2852 // It's there as a result of conservative refactoring of the output.
2853 out << "{;}\n";
2854 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002855
Jamie Madill8c46ab12015-12-07 16:39:19 -05002856 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002857
alokp@chromium.org52813552010-11-16 18:36:09 +00002858 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002859 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002860 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002861 out << "while(\n";
2862
alokp@chromium.org52813552010-11-16 18:36:09 +00002863 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864
daniel@transgaming.com73536982012-03-21 20:45:49 +00002865 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002866 }
2867
daniel@transgaming.com73536982012-03-21 20:45:49 +00002868 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002870 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002871 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002872
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002873 return false;
2874}
2875
2876bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2877{
Jamie Madill32aab012015-01-27 14:12:26 -05002878 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879
2880 switch (node->getFlowOp())
2881 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002882 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002883 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002884 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002885 case EOpBreak:
2886 if (visit == PreVisit)
2887 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002888 if (mNestedLoopDepth > 1)
2889 {
2890 mUsesNestedBreak = true;
2891 }
2892
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002893 if (mExcessiveLoopIndex)
2894 {
2895 out << "{Break";
2896 mExcessiveLoopIndex->traverse(this);
2897 out << " = true; break;}\n";
2898 }
2899 else
2900 {
2901 out << "break;\n";
2902 }
2903 }
2904 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002905 case EOpContinue:
2906 outputTriplet(out, visit, "continue;\n", "", "");
2907 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002908 case EOpReturn:
2909 if (visit == PreVisit)
2910 {
2911 if (node->getExpression())
2912 {
2913 out << "return ";
2914 }
2915 else
2916 {
2917 out << "return;\n";
2918 }
2919 }
2920 else if (visit == PostVisit)
2921 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002922 if (node->getExpression())
2923 {
2924 out << ";\n";
2925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002926 }
2927 break;
2928 default: UNREACHABLE();
2929 }
2930
2931 return true;
2932}
2933
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002934bool OutputHLSL::isSingleStatement(TIntermNode *node)
2935{
2936 TIntermAggregate *aggregate = node->getAsAggregate();
2937
2938 if (aggregate)
2939 {
2940 if (aggregate->getOp() == EOpSequence)
2941 {
2942 return false;
2943 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002944 else if (aggregate->getOp() == EOpDeclaration)
2945 {
2946 // Declaring multiple comma-separated variables must be considered multiple statements
2947 // because each individual declaration has side effects which are visible in the next.
2948 return false;
2949 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002950 else
2951 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002952 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002953 {
2954 if (!isSingleStatement(*sit))
2955 {
2956 return false;
2957 }
2958 }
2959
2960 return true;
2961 }
2962 }
2963
2964 return true;
2965}
2966
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002967// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2968// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002969bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002970{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002971 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002972
2973 // Parse loops of the form:
2974 // for(int index = initial; index [comparator] limit; index += increment)
2975 TIntermSymbol *index = NULL;
2976 TOperator comparator = EOpNull;
2977 int initial = 0;
2978 int limit = 0;
2979 int increment = 0;
2980
2981 // Parse index name and intial value
2982 if (node->getInit())
2983 {
2984 TIntermAggregate *init = node->getInit()->getAsAggregate();
2985
2986 if (init)
2987 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002988 TIntermSequence *sequence = init->getSequence();
2989 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002990
2991 if (variable && variable->getQualifier() == EvqTemporary)
2992 {
2993 TIntermBinary *assign = variable->getAsBinaryNode();
2994
2995 if (assign->getOp() == EOpInitialize)
2996 {
2997 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2998 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2999
3000 if (symbol && constant)
3001 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003002 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003003 {
3004 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003005 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003006 }
3007 }
3008 }
3009 }
3010 }
3011 }
3012
3013 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003014 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003015 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003016 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003017
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003018 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3019 {
3020 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3021
3022 if (constant)
3023 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003024 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003025 {
3026 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003027 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003028 }
3029 }
3030 }
3031 }
3032
3033 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003034 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003035 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003036 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3037 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003038
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003039 if (binaryTerminal)
3040 {
3041 TOperator op = binaryTerminal->getOp();
3042 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3043
3044 if (constant)
3045 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003046 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003047 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003048 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003049
3050 switch (op)
3051 {
3052 case EOpAddAssign: increment = value; break;
3053 case EOpSubAssign: increment = -value; break;
3054 default: UNIMPLEMENTED();
3055 }
3056 }
3057 }
3058 }
3059 else if (unaryTerminal)
3060 {
3061 TOperator op = unaryTerminal->getOp();
3062
3063 switch (op)
3064 {
3065 case EOpPostIncrement: increment = 1; break;
3066 case EOpPostDecrement: increment = -1; break;
3067 case EOpPreIncrement: increment = 1; break;
3068 case EOpPreDecrement: increment = -1; break;
3069 default: UNIMPLEMENTED();
3070 }
3071 }
3072 }
3073
3074 if (index != NULL && comparator != EOpNull && increment != 0)
3075 {
3076 if (comparator == EOpLessThanEqual)
3077 {
3078 comparator = EOpLessThan;
3079 limit += 1;
3080 }
3081
3082 if (comparator == EOpLessThan)
3083 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003084 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003085
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003086 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003087 {
3088 return false; // Not an excessive loop
3089 }
3090
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003091 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3092 mExcessiveLoopIndex = index;
3093
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003094 out << "{int ";
3095 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003096 out << ";\n"
3097 "bool Break";
3098 index->traverse(this);
3099 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003100
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003101 bool firstLoopFragment = true;
3102
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003103 while (iterations > 0)
3104 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003105 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003106
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003107 if (!firstLoopFragment)
3108 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003109 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003110 index->traverse(this);
3111 out << ") {\n";
3112 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003113
3114 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3115 {
3116 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3117 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003118
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003119 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003120 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003121
Corentin Wallez1239ee92015-03-19 14:38:02 -07003122 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003123 index->traverse(this);
3124 out << " = ";
3125 out << initial;
3126
3127 out << "; ";
3128 index->traverse(this);
3129 out << " < ";
3130 out << clampedLimit;
3131
3132 out << "; ";
3133 index->traverse(this);
3134 out << " += ";
3135 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003136 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003137
Jamie Madill8c46ab12015-12-07 16:39:19 -05003138 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003139 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003140
3141 if (node->getBody())
3142 {
3143 node->getBody()->traverse(this);
3144 }
3145
Jamie Madill8c46ab12015-12-07 16:39:19 -05003146 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003147 out << ";}\n";
3148
3149 if (!firstLoopFragment)
3150 {
3151 out << "}\n";
3152 }
3153
3154 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003155
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003156 initial += MAX_LOOP_ITERATIONS * increment;
3157 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003158 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003159
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003160 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003161
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003162 mExcessiveLoopIndex = restoreIndex;
3163
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003164 return true;
3165 }
3166 else UNIMPLEMENTED();
3167 }
3168
3169 return false; // Not handled as an excessive loop
3170}
3171
Jamie Madill8c46ab12015-12-07 16:39:19 -05003172void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3173 Visit visit,
3174 const char *preString,
3175 const char *inString,
3176 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003177{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003178 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003179 {
3180 out << preString;
3181 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003182 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003183 {
3184 out << inString;
3185 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003186 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003187 {
3188 out << postString;
3189 }
3190}
3191
Jamie Madill8c46ab12015-12-07 16:39:19 -05003192void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003193{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003194 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003195 {
Jamie Madill32aab012015-01-27 14:12:26 -05003196 out << "\n";
3197 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003198
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003199 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003200 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003201 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003202 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003203
Jamie Madill32aab012015-01-27 14:12:26 -05003204 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003205 }
3206}
3207
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003208TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3209{
3210 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003211 const TType &type = symbol->getType();
3212 const TName &name = symbol->getName();
3213 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003214
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003215 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003216 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003217 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003218 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003219 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003220 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003221 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003222 }
3223
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003224 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003225 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003226 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3227 {
3228 // Samplers are passed as indices to the sampler array.
3229 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3230 return "const uint " + nameStr + ArrayString(type);
3231 }
3232 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3233 {
3234 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3235 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3236 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3237 ArrayString(type);
3238 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003239 }
3240
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003241 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003242}
3243
3244TString OutputHLSL::initializer(const TType &type)
3245{
3246 TString string;
3247
Jamie Madill94bf7f22013-07-08 13:31:15 -04003248 size_t size = type.getObjectSize();
3249 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003250 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003251 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003252
Jamie Madill94bf7f22013-07-08 13:31:15 -04003253 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003254 {
3255 string += ", ";
3256 }
3257 }
3258
daniel@transgaming.comead23042010-04-29 03:35:36 +00003259 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003260}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003261
Jamie Madill8c46ab12015-12-07 16:39:19 -05003262void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3263 Visit visit,
3264 const TType &type,
3265 const char *name,
3266 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003267{
Olli Etuahof40319e2015-03-10 14:33:00 +02003268 if (type.isArray())
3269 {
3270 UNIMPLEMENTED();
3271 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003272
3273 if (visit == PreVisit)
3274 {
Jamie Madill8daaba12014-06-13 10:04:33 -04003275 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003276
Daniel Bratell29190082015-02-20 16:42:54 +01003277 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003278 }
3279 else if (visit == InVisit)
3280 {
3281 out << ", ";
3282 }
3283 else if (visit == PostVisit)
3284 {
3285 out << ")";
3286 }
3287}
3288
Jamie Madill8c46ab12015-12-07 16:39:19 -05003289const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3290 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003291 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003292{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003293 const TConstantUnion *constUnionIterated = constUnion;
3294
Jamie Madill98493dd2013-07-08 14:39:03 -04003295 const TStructure* structure = type.getStruct();
3296 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003297 {
Jamie Madill033dae62014-06-18 12:56:28 -04003298 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003299
Jamie Madill98493dd2013-07-08 14:39:03 -04003300 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003301
Jamie Madill98493dd2013-07-08 14:39:03 -04003302 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003303 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003304 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003305 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003306
Jamie Madill98493dd2013-07-08 14:39:03 -04003307 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003308 {
3309 out << ", ";
3310 }
3311 }
3312
3313 out << ")";
3314 }
3315 else
3316 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003317 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003318 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003319
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003320 if (writeType)
3321 {
Jamie Madill033dae62014-06-18 12:56:28 -04003322 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003323 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003324 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003325 if (writeType)
3326 {
3327 out << ")";
3328 }
3329 }
3330
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003331 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003332}
3333
Jamie Madill8c46ab12015-12-07 16:39:19 -05003334void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003335{
3336 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003337 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003338}
3339
Jamie Madill37997142015-01-28 10:06:34 -05003340bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3341{
3342 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3343 expression->traverse(&searchSymbol);
3344
3345 if (searchSymbol.foundMatch())
3346 {
3347 // Type already printed
3348 out << "t" + str(mUniqueIndex) + " = ";
3349 expression->traverse(this);
3350 out << ", ";
3351 symbolNode->traverse(this);
3352 out << " = t" + str(mUniqueIndex);
3353
3354 mUniqueIndex++;
3355 return true;
3356 }
3357
3358 return false;
3359}
3360
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003361bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3362{
3363 // We support writing constant unions and constructors that only take constant unions as
3364 // parameters as HLSL literals.
3365 if (expression->getAsConstantUnion())
3366 {
3367 return true;
3368 }
3369 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3370 !expression->getAsAggregate()->isConstructor())
3371 {
3372 return false;
3373 }
3374 TIntermAggregate *constructor = expression->getAsAggregate();
3375 for (TIntermNode *&node : *constructor->getSequence())
3376 {
3377 if (!node->getAsConstantUnion())
3378 return false;
3379 }
3380 return true;
3381}
3382
3383bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3384 TIntermSymbol *symbolNode,
3385 TIntermTyped *expression)
3386{
3387 if (canWriteAsHLSLLiteral(expression))
3388 {
3389 symbolNode->traverse(this);
3390 if (expression->getType().isArray())
3391 {
3392 out << "[" << expression->getType().getArraySize() << "]";
3393 }
3394 out << " = {";
3395 if (expression->getAsConstantUnion())
3396 {
3397 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3398 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3399 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3400 }
3401 else
3402 {
3403 TIntermAggregate *constructor = expression->getAsAggregate();
3404 ASSERT(constructor != nullptr);
3405 for (TIntermNode *&node : *constructor->getSequence())
3406 {
3407 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3408 ASSERT(nodeConst);
3409 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3410 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3411 if (node != constructor->getSequence()->back())
3412 {
3413 out << ", ";
3414 }
3415 }
3416 }
3417 out << "}";
3418 return true;
3419 }
3420 return false;
3421}
3422
Jamie Madill37997142015-01-28 10:06:34 -05003423void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3424{
3425 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3426 << "\n"
3427 << "void initializeDeferredGlobals()\n"
3428 << "{\n";
3429
3430 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3431 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003432 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3433 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3434 if (binary != nullptr)
3435 {
3436 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3437 TIntermTyped *expression = binary->getRight();
3438 ASSERT(symbol);
3439 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003440
Olli Etuahod81ed842015-05-12 12:46:35 +03003441 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003442
Olli Etuahod81ed842015-05-12 12:46:35 +03003443 if (!writeSameSymbolInitializer(out, symbol, expression))
3444 {
3445 ASSERT(mInfoSinkStack.top() == &out);
3446 expression->traverse(this);
3447 }
3448 out << ";\n";
3449 }
3450 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003451 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003452 writeSelection(out, selection);
Jamie Madill37997142015-01-28 10:06:34 -05003453 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003454 else
3455 {
3456 UNREACHABLE();
3457 }
Jamie Madill37997142015-01-28 10:06:34 -05003458 }
3459
3460 out << "}\n"
3461 << "\n";
3462}
3463
Jamie Madill55e79e02015-02-09 15:35:00 -05003464TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3465{
3466 const TFieldList &fields = structure.fields();
3467
3468 for (const auto &eqFunction : mStructEqualityFunctions)
3469 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003470 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003471 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003472 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003473 }
3474 }
3475
3476 const TString &structNameString = StructNameString(structure);
3477
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003478 StructEqualityFunction *function = new StructEqualityFunction();
3479 function->structure = &structure;
3480 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003481
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003482 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003483
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003484 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3485 << "{\n"
3486 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003487
3488 for (size_t i = 0; i < fields.size(); i++)
3489 {
3490 const TField *field = fields[i];
3491 const TType *fieldType = field->type();
3492
3493 const TString &fieldNameA = "a." + Decorate(field->name());
3494 const TString &fieldNameB = "b." + Decorate(field->name());
3495
3496 if (i > 0)
3497 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003498 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003499 }
3500
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003501 fnOut << "(";
3502 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3503 fnOut << fieldNameA;
3504 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3505 fnOut << fieldNameB;
3506 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3507 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003508 }
3509
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003510 fnOut << ";\n" << "}\n";
3511
3512 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003513
3514 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003515 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003516
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003517 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003518}
3519
Olli Etuaho7fb49552015-03-18 17:27:44 +02003520TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3521{
3522 for (const auto &eqFunction : mArrayEqualityFunctions)
3523 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003524 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003525 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003526 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003527 }
3528 }
3529
3530 const TString &typeName = TypeString(type);
3531
Olli Etuaho12690762015-03-31 12:55:28 +03003532 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003533 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003534
3535 TInfoSinkBase fnNameOut;
3536 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003537 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003538
3539 TType nonArrayType = type;
3540 nonArrayType.clearArrayness();
3541
3542 TInfoSinkBase fnOut;
3543
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003544 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003545 << typeName << " a[" << type.getArraySize() << "], "
3546 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003547 << "{\n"
3548 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3549 " {\n"
3550 " if (";
3551
3552 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3553 fnOut << "a[i]";
3554 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3555 fnOut << "b[i]";
3556 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3557
3558 fnOut << ") { return false; }\n"
3559 " }\n"
3560 " return true;\n"
3561 "}\n";
3562
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003563 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003564
3565 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003566 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003567
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003568 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003569}
3570
Olli Etuaho12690762015-03-31 12:55:28 +03003571TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3572{
3573 for (const auto &assignFunction : mArrayAssignmentFunctions)
3574 {
3575 if (assignFunction.type == type)
3576 {
3577 return assignFunction.functionName;
3578 }
3579 }
3580
3581 const TString &typeName = TypeString(type);
3582
3583 ArrayHelperFunction function;
3584 function.type = type;
3585
3586 TInfoSinkBase fnNameOut;
3587 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3588 function.functionName = fnNameOut.c_str();
3589
3590 TInfoSinkBase fnOut;
3591
3592 fnOut << "void " << function.functionName << "(out "
3593 << typeName << " a[" << type.getArraySize() << "], "
3594 << typeName << " b[" << type.getArraySize() << "])\n"
3595 << "{\n"
3596 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3597 " {\n"
3598 " a[i] = b[i];\n"
3599 " }\n"
3600 "}\n";
3601
3602 function.functionDefinition = fnOut.c_str();
3603
3604 mArrayAssignmentFunctions.push_back(function);
3605
3606 return function.functionName;
3607}
3608
Olli Etuaho9638c352015-04-01 14:34:52 +03003609TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3610{
3611 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3612 {
3613 if (constructIntoFunction.type == type)
3614 {
3615 return constructIntoFunction.functionName;
3616 }
3617 }
3618
3619 const TString &typeName = TypeString(type);
3620
3621 ArrayHelperFunction function;
3622 function.type = type;
3623
3624 TInfoSinkBase fnNameOut;
3625 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3626 function.functionName = fnNameOut.c_str();
3627
3628 TInfoSinkBase fnOut;
3629
3630 fnOut << "void " << function.functionName << "(out "
3631 << typeName << " a[" << type.getArraySize() << "]";
3632 for (int i = 0; i < type.getArraySize(); ++i)
3633 {
3634 fnOut << ", " << typeName << " b" << i;
3635 }
3636 fnOut << ")\n"
3637 "{\n";
3638
3639 for (int i = 0; i < type.getArraySize(); ++i)
3640 {
3641 fnOut << " a[" << i << "] = b" << i << ";\n";
3642 }
3643 fnOut << "}\n";
3644
3645 function.functionDefinition = fnOut.c_str();
3646
3647 mArrayConstructIntoFunctions.push_back(function);
3648
3649 return function.functionName;
3650}
3651
Jamie Madill2e295e22015-04-29 10:41:33 -04003652void OutputHLSL::ensureStructDefined(const TType &type)
3653{
3654 TStructure *structure = type.getStruct();
3655
3656 if (structure)
3657 {
3658 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3659 }
3660}
3661
3662
Olli Etuaho9638c352015-04-01 14:34:52 +03003663
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003664}