blob: 602c839a317a1183054420009e7eb4915a6db9d6 [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
Olli Etuaho30268292016-01-15 16:40:00 +0200186 // Reserve registers for the default uniform block, driver constants and sampler metadata
187 mUniformHLSL->reserveInterfaceBlockRegisters(3);
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
508 out << "};\n";
Olli Etuaho30268292016-01-15 16:40:00 +0200509
510 if (mOutputType == SH_HLSL_4_1_OUTPUT)
511 {
512 mUniformHLSL->samplerMetadataUniforms(out);
513 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000514 }
515 else
516 {
517 if (mUsesDepthRange)
518 {
519 out << "uniform float3 dx_DepthRange : register(c0);";
520 }
521
522 if (mUsesFragCoord)
523 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000524 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000525 }
526
527 if (mUsesFragCoord || mUsesFrontFacing)
528 {
529 out << "uniform float3 dx_DepthFront : register(c2);\n";
530 }
531 }
532
533 out << "\n";
534
535 if (mUsesDepthRange)
536 {
537 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
538 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000539 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000540
Jamie Madillf91ce812014-06-13 10:04:34 -0400541 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000542 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400543 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000544 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400545 out << flaggedStructs;
546 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000547 }
548
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000549 if (usingMRTExtension && mNumRenderTargets > 1)
550 {
551 out << "#define GL_USES_MRT\n";
552 }
553
554 if (mUsesFragColor)
555 {
556 out << "#define GL_USES_FRAG_COLOR\n";
557 }
558
559 if (mUsesFragData)
560 {
561 out << "#define GL_USES_FRAG_DATA\n";
562 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000563 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000564 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000566 out << "// Attributes\n";
567 out << attributes;
568 out << "\n"
569 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400570
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000571 if (mUsesPointSize)
572 {
573 out << "static float gl_PointSize = float(1);\n";
574 }
575
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000576 if (mUsesInstanceID)
577 {
578 out << "static int gl_InstanceID;";
579 }
580
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000581 out << "\n"
582 "// Varyings\n";
583 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000584 out << "\n";
585
586 if (mUsesDepthRange)
587 {
588 out << "struct gl_DepthRangeParameters\n"
589 "{\n"
590 " float near;\n"
591 " float far;\n"
592 " float diff;\n"
593 "};\n"
594 "\n";
595 }
596
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200597 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000598 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800599 out << "cbuffer DriverConstants : register(b1)\n"
600 "{\n";
601
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000602 if (mUsesDepthRange)
603 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800604 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000605 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800606
Cooper Partine6664f02015-01-09 16:22:24 -0800607 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800608 // However, we declare it for all shaders (including Feature Level 10+).
609 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
610 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800611 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800612
613 out << "};\n"
614 "\n";
Olli Etuaho30268292016-01-15 16:40:00 +0200615
616 if (mOutputType == SH_HLSL_4_1_OUTPUT)
617 {
618 mUniformHLSL->samplerMetadataUniforms(out);
619 }
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000620 }
621 else
622 {
623 if (mUsesDepthRange)
624 {
625 out << "uniform float3 dx_DepthRange : register(c0);\n";
626 }
627
Cooper Partine6664f02015-01-09 16:22:24 -0800628 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
629 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000630 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000631 }
632
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000633 if (mUsesDepthRange)
634 {
635 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
636 "\n";
637 }
638
Jamie Madillf91ce812014-06-13 10:04:34 -0400639 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000640 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400641 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000642 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400643 out << flaggedStructs;
644 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000645 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400646 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000647
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400648 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
649 {
650 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400651 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000652 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400653 switch(textureFunction->sampler)
654 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400655 case EbtSampler2D: out << "int2 "; break;
656 case EbtSampler3D: out << "int3 "; break;
657 case EbtSamplerCube: out << "int2 "; break;
658 case EbtSampler2DArray: out << "int3 "; break;
659 case EbtISampler2D: out << "int2 "; break;
660 case EbtISampler3D: out << "int3 "; break;
661 case EbtISamplerCube: out << "int2 "; break;
662 case EbtISampler2DArray: out << "int3 "; break;
663 case EbtUSampler2D: out << "int2 "; break;
664 case EbtUSampler3D: out << "int3 "; break;
665 case EbtUSamplerCube: out << "int2 "; break;
666 case EbtUSampler2DArray: out << "int3 "; break;
667 case EbtSampler2DShadow: out << "int2 "; break;
668 case EbtSamplerCubeShadow: out << "int2 "; break;
669 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400670 default: UNREACHABLE();
671 }
672 }
673 else // Sampling function
674 {
675 switch(textureFunction->sampler)
676 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400677 case EbtSampler2D: out << "float4 "; break;
678 case EbtSampler3D: out << "float4 "; break;
679 case EbtSamplerCube: out << "float4 "; break;
680 case EbtSampler2DArray: out << "float4 "; break;
681 case EbtISampler2D: out << "int4 "; break;
682 case EbtISampler3D: out << "int4 "; break;
683 case EbtISamplerCube: out << "int4 "; break;
684 case EbtISampler2DArray: out << "int4 "; break;
685 case EbtUSampler2D: out << "uint4 "; break;
686 case EbtUSampler3D: out << "uint4 "; break;
687 case EbtUSamplerCube: out << "uint4 "; break;
688 case EbtUSampler2DArray: out << "uint4 "; break;
689 case EbtSampler2DShadow: out << "float "; break;
690 case EbtSamplerCubeShadow: out << "float "; break;
691 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400692 default: UNREACHABLE();
693 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000694 }
695
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400696 // Function name
697 out << textureFunction->name();
698
699 // Argument list
700 int hlslCoords = 4;
701
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200702 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000703 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400704 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000705 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400706 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
707 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
708 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000709 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400710
Nicolas Capens75fb4752013-07-10 15:14:47 -0400711 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000712 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400713 case TextureFunction::IMPLICIT: break;
714 case TextureFunction::BIAS: hlslCoords = 4; break;
715 case TextureFunction::LOD: hlslCoords = 4; break;
716 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400717 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400718 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000719 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400720 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200721 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400722 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200723 hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler);
724 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400725 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200726 out << TextureString(textureFunction->sampler) << " x, "
727 << SamplerString(textureFunction->sampler) << " s";
728 }
729 else
730 {
731 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
732 out << "const uint samplerIndex";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400733 }
734 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400735
Nicolas Capensfc014542014-02-18 14:47:13 -0500736 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400737 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500738 switch(textureFunction->coords)
739 {
740 case 2: out << ", int2 t"; break;
741 case 3: out << ", int3 t"; break;
742 default: UNREACHABLE();
743 }
744 }
745 else // Floating-point coordinates (except textureSize)
746 {
747 switch(textureFunction->coords)
748 {
749 case 1: out << ", int lod"; break; // textureSize()
750 case 2: out << ", float2 t"; break;
751 case 3: out << ", float3 t"; break;
752 case 4: out << ", float4 t"; break;
753 default: UNREACHABLE();
754 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000755 }
756
Nicolas Capensd11d5492014-02-19 17:06:10 -0500757 if (textureFunction->method == TextureFunction::GRAD)
758 {
759 switch(textureFunction->sampler)
760 {
761 case EbtSampler2D:
762 case EbtISampler2D:
763 case EbtUSampler2D:
764 case EbtSampler2DArray:
765 case EbtISampler2DArray:
766 case EbtUSampler2DArray:
767 case EbtSampler2DShadow:
768 case EbtSampler2DArrayShadow:
769 out << ", float2 ddx, float2 ddy";
770 break;
771 case EbtSampler3D:
772 case EbtISampler3D:
773 case EbtUSampler3D:
774 case EbtSamplerCube:
775 case EbtISamplerCube:
776 case EbtUSamplerCube:
777 case EbtSamplerCubeShadow:
778 out << ", float3 ddx, float3 ddy";
779 break;
780 default: UNREACHABLE();
781 }
782 }
783
Nicolas Capens75fb4752013-07-10 15:14:47 -0400784 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000785 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400786 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400787 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400788 case TextureFunction::LOD: out << ", float lod"; break;
789 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400790 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400791 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500792 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500793 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400794 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000795 }
796
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500797 if (textureFunction->offset)
798 {
799 switch(textureFunction->sampler)
800 {
801 case EbtSampler2D: out << ", int2 offset"; break;
802 case EbtSampler3D: out << ", int3 offset"; break;
803 case EbtSampler2DArray: out << ", int2 offset"; break;
804 case EbtISampler2D: out << ", int2 offset"; break;
805 case EbtISampler3D: out << ", int3 offset"; break;
806 case EbtISampler2DArray: out << ", int2 offset"; break;
807 case EbtUSampler2D: out << ", int2 offset"; break;
808 case EbtUSampler3D: out << ", int3 offset"; break;
809 case EbtUSampler2DArray: out << ", int2 offset"; break;
810 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500811 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500812 default: UNREACHABLE();
813 }
814 }
815
Nicolas Capens84cfa122014-04-14 13:48:45 -0400816 if (textureFunction->method == TextureFunction::BIAS ||
817 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500818 {
819 out << ", float bias";
820 }
821
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400822 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400823 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400824
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200825 // In some cases we use a variable to store the texture/sampler objects, but to work around
826 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
827 // sampling we need to call the function directly on a reference to the array. The bug was
828 // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests.
829 TString textureReference("x");
830 TString samplerReference("s");
831 if (mOutputType == SH_HLSL_4_1_OUTPUT)
832 {
833 TString suffix = TextureGroupSuffix(textureFunction->sampler);
834 if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D)
835 {
836 textureReference = TString("textures") + suffix + "[samplerIndex]";
837 samplerReference = TString("samplers") + suffix + "[samplerIndex]";
838 }
839 else
840 {
841 out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix
842 << ";\n";
843 textureReference = TString("textures") + suffix + "[textureIndex]";
844 out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset"
845 << suffix << ";\n";
846 samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]";
847 }
848 }
849
Nicolas Capens75fb4752013-07-10 15:14:47 -0400850 if (textureFunction->method == TextureFunction::SIZE)
851 {
852 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
853 {
854 if (IsSamplerArray(textureFunction->sampler))
855 {
856 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200857 << " " << textureReference
858 << ".GetDimensions(lod, width, height, layers, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400859 }
860 else
861 {
862 out << " uint width; uint height; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200863 << " " << textureReference
864 << ".GetDimensions(lod, width, height, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400865 }
866 }
867 else if (IsSampler3D(textureFunction->sampler))
868 {
869 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200870 << " " << textureReference
871 << ".GetDimensions(lod, width, height, depth, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400872 }
873 else UNREACHABLE();
874
875 switch(textureFunction->sampler)
876 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400877 case EbtSampler2D: out << " return int2(width, height);"; break;
878 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
879 case EbtSamplerCube: out << " return int2(width, height);"; break;
880 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
881 case EbtISampler2D: out << " return int2(width, height);"; break;
882 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
883 case EbtISamplerCube: out << " return int2(width, height);"; break;
884 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
885 case EbtUSampler2D: out << " return int2(width, height);"; break;
886 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
887 case EbtUSamplerCube: out << " return int2(width, height);"; break;
888 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
889 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
890 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
891 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400892 default: UNREACHABLE();
893 }
894 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400895 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400896 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500897 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
898 {
899 out << " float width; float height; float layers; float levels;\n";
900
901 out << " uint mip = 0;\n";
902
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200903 out << " " << textureReference
904 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500905
906 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
907 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
908 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
909 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
910
911 // FACE_POSITIVE_X = 000b
912 // FACE_NEGATIVE_X = 001b
913 // FACE_POSITIVE_Y = 010b
914 // FACE_NEGATIVE_Y = 011b
915 // FACE_POSITIVE_Z = 100b
916 // FACE_NEGATIVE_Z = 101b
917 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
918
919 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
920 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
921 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
922
923 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
924 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500925
926 // Mip level computation.
927 if (textureFunction->method == TextureFunction::IMPLICIT)
928 {
929 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
930 " float2 dx = ddx(tSized);\n"
931 " float2 dy = ddy(tSized);\n"
932 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"
933 " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200934 << " " << textureReference
935 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500936 }
Nicolas Capens0027fa92014-02-20 14:26:42 -0500937 }
938 else if (IsIntegerSampler(textureFunction->sampler) &&
939 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400940 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400941 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400942 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400943 if (IsSamplerArray(textureFunction->sampler))
944 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400945 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400946
Nicolas Capens9edebd62013-08-06 10:59:10 -0400947 if (textureFunction->method == TextureFunction::LOD0)
948 {
949 out << " uint mip = 0;\n";
950 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400951 else if (textureFunction->method == TextureFunction::LOD0BIAS)
952 {
953 out << " uint mip = bias;\n";
954 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400955 else
956 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200957
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200958 out << " " << textureReference
959 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400960 if (textureFunction->method == TextureFunction::IMPLICIT ||
961 textureFunction->method == TextureFunction::BIAS)
962 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200963 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400964 " float dx = length(ddx(tSized));\n"
965 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500966 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400967
968 if (textureFunction->method == TextureFunction::BIAS)
969 {
970 out << " lod += bias;\n";
971 }
972 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500973 else if (textureFunction->method == TextureFunction::GRAD)
974 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200975 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500976 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400977
978 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
979 }
980
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200981 out << " " << textureReference
982 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400983 }
984 else
985 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400986 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400987
Nicolas Capens9edebd62013-08-06 10:59:10 -0400988 if (textureFunction->method == TextureFunction::LOD0)
989 {
990 out << " uint mip = 0;\n";
991 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400992 else if (textureFunction->method == TextureFunction::LOD0BIAS)
993 {
994 out << " uint mip = bias;\n";
995 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400996 else
997 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200998 out << " " << textureReference
999 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001000
Nicolas Capens9edebd62013-08-06 10:59:10 -04001001 if (textureFunction->method == TextureFunction::IMPLICIT ||
1002 textureFunction->method == TextureFunction::BIAS)
1003 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001004 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001005 " float dx = length(ddx(tSized));\n"
1006 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001007 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001008
1009 if (textureFunction->method == TextureFunction::BIAS)
1010 {
1011 out << " lod += bias;\n";
1012 }
1013 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001014 else if (textureFunction->method == TextureFunction::GRAD)
1015 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001016 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001017 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001018
1019 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1020 }
1021
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001022 out << " " << textureReference
1023 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001024 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001025 }
1026 else if (IsSampler3D(textureFunction->sampler))
1027 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001028 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001029
Nicolas Capens9edebd62013-08-06 10:59:10 -04001030 if (textureFunction->method == TextureFunction::LOD0)
1031 {
1032 out << " uint mip = 0;\n";
1033 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001034 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1035 {
1036 out << " uint mip = bias;\n";
1037 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001038 else
1039 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001040 out << " " << textureReference
1041 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001042
Nicolas Capens9edebd62013-08-06 10:59:10 -04001043 if (textureFunction->method == TextureFunction::IMPLICIT ||
1044 textureFunction->method == TextureFunction::BIAS)
1045 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001046 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1047 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001048 " float dx = length(ddx(tSized));\n"
1049 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001050 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001051
1052 if (textureFunction->method == TextureFunction::BIAS)
1053 {
1054 out << " lod += bias;\n";
1055 }
1056 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001057 else if (textureFunction->method == TextureFunction::GRAD)
1058 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001059 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001060 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001061
1062 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1063 }
1064
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001065 out << " " << textureReference
1066 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001067 }
1068 else UNREACHABLE();
1069 }
1070
1071 out << " return ";
1072
1073 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001074 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001075 {
1076 switch(textureFunction->sampler)
1077 {
1078 case EbtSampler2D: out << "tex2D"; break;
1079 case EbtSamplerCube: out << "texCUBE"; break;
1080 default: UNREACHABLE();
1081 }
1082
Nicolas Capens75fb4752013-07-10 15:14:47 -04001083 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001084 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001085 case TextureFunction::IMPLICIT:
1086 out << "(" << samplerReference << ", ";
1087 break;
1088 case TextureFunction::BIAS:
1089 out << "bias(" << samplerReference << ", ";
1090 break;
1091 case TextureFunction::LOD:
1092 out << "lod(" << samplerReference << ", ";
1093 break;
1094 case TextureFunction::LOD0:
1095 out << "lod(" << samplerReference << ", ";
1096 break;
1097 case TextureFunction::LOD0BIAS:
1098 out << "lod(" << samplerReference << ", ";
1099 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001100 default: UNREACHABLE();
1101 }
1102 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001103 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001104 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001105 if (textureFunction->method == TextureFunction::GRAD)
1106 {
1107 if (IsIntegerSampler(textureFunction->sampler))
1108 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001109 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001110 }
1111 else if (IsShadowSampler(textureFunction->sampler))
1112 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001113 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1114 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001115 }
1116 else
1117 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001118 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001119 }
1120 }
1121 else if (IsIntegerSampler(textureFunction->sampler) ||
1122 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001123 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001124 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001125 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001126 else if (IsShadowSampler(textureFunction->sampler))
1127 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001128 switch(textureFunction->method)
1129 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001130 case TextureFunction::IMPLICIT:
1131 out << "" << textureReference << ".SampleCmp(" << samplerReference
1132 << ", ";
1133 break;
1134 case TextureFunction::BIAS:
1135 out << "" << textureReference << ".SampleCmp(" << samplerReference
1136 << ", ";
1137 break;
1138 case TextureFunction::LOD:
1139 out << "" << textureReference << ".SampleCmp(" << samplerReference
1140 << ", ";
1141 break;
1142 case TextureFunction::LOD0:
1143 out << "" << textureReference << ".SampleCmpLevelZero("
1144 << samplerReference << ", ";
1145 break;
1146 case TextureFunction::LOD0BIAS:
1147 out << "" << textureReference << ".SampleCmpLevelZero("
1148 << samplerReference << ", ";
1149 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001150 default: UNREACHABLE();
1151 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001152 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001153 else
1154 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001155 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001156 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001157 case TextureFunction::IMPLICIT:
1158 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1159 break;
1160 case TextureFunction::BIAS:
1161 out << "" << textureReference << ".SampleBias(" << samplerReference
1162 << ", ";
1163 break;
1164 case TextureFunction::LOD:
1165 out << "" << textureReference << ".SampleLevel(" << samplerReference
1166 << ", ";
1167 break;
1168 case TextureFunction::LOD0:
1169 out << "" << textureReference << ".SampleLevel(" << samplerReference
1170 << ", ";
1171 break;
1172 case TextureFunction::LOD0BIAS:
1173 out << "" << textureReference << ".SampleLevel(" << samplerReference
1174 << ", ";
1175 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001176 default: UNREACHABLE();
1177 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001178 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001179 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001180 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001181
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001182 // Integer sampling requires integer addresses
1183 TString addressx = "";
1184 TString addressy = "";
1185 TString addressz = "";
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001186 TString close = "";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001187
Nicolas Capensfc014542014-02-18 14:47:13 -05001188 if (IsIntegerSampler(textureFunction->sampler) ||
1189 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001190 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001191 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001192 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001193 case 2: out << "int3("; break;
1194 case 3: out << "int4("; break;
1195 default: UNREACHABLE();
1196 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001197
Nicolas Capensfc014542014-02-18 14:47:13 -05001198 // Convert from normalized floating-point to integer
1199 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001200 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001201 addressx = "int(floor(width * frac((";
1202 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001203
Nicolas Capensfc014542014-02-18 14:47:13 -05001204 if (IsSamplerArray(textureFunction->sampler))
1205 {
1206 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1207 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001208 else if (IsSamplerCube(textureFunction->sampler))
Nicolas Capens0027fa92014-02-20 14:26:42 -05001209 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001210 addressz = "((((";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001211 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001212 else
1213 {
1214 addressz = "int(floor(depth * frac((";
1215 }
1216
1217 close = "))))";
Nicolas Capensfc014542014-02-18 14:47:13 -05001218 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001219 }
1220 else
1221 {
1222 switch(hlslCoords)
1223 {
1224 case 2: out << "float2("; break;
1225 case 3: out << "float3("; break;
1226 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001227 default: UNREACHABLE();
1228 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001229 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001230
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001231 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001232
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001233 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001234 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001235 switch(textureFunction->coords)
1236 {
1237 case 3: proj = " / t.z"; break;
1238 case 4: proj = " / t.w"; break;
1239 default: UNREACHABLE();
1240 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001241 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001242
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001243 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001244
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001245 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001246 {
1247 if (hlslCoords >= 3)
1248 {
1249 if (textureFunction->coords < 3)
1250 {
1251 out << ", 0";
1252 }
1253 else
1254 {
1255 out << ", t.z" + proj;
1256 }
1257 }
1258
1259 if (hlslCoords == 4)
1260 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001261 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001262 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001263 case TextureFunction::BIAS: out << ", bias"; break;
1264 case TextureFunction::LOD: out << ", lod"; break;
1265 case TextureFunction::LOD0: out << ", 0"; break;
1266 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001267 default: UNREACHABLE();
1268 }
1269 }
1270
1271 out << "));\n";
1272 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001273 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001274 {
1275 if (hlslCoords >= 3)
1276 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001277 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1278 {
1279 out << ", face";
1280 }
1281 else
1282 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001283 out << ", " + addressz + ("t.z" + proj) + close;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001284 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001285 }
1286
Nicolas Capensd11d5492014-02-19 17:06:10 -05001287 if (textureFunction->method == TextureFunction::GRAD)
1288 {
1289 if (IsIntegerSampler(textureFunction->sampler))
1290 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001291 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001292 }
1293 else if (IsShadowSampler(textureFunction->sampler))
1294 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001295 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001296 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001297 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001298 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1299 // The resulting third component of P' in the shadow forms is used as Dref
1300 out << "), t.z" << proj;
1301 }
1302 else
1303 {
1304 switch(textureFunction->coords)
1305 {
1306 case 3: out << "), t.z"; break;
1307 case 4: out << "), t.w"; break;
1308 default: UNREACHABLE();
1309 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001310 }
1311 }
1312 else
1313 {
1314 out << "), ddx, ddy";
1315 }
1316 }
1317 else if (IsIntegerSampler(textureFunction->sampler) ||
1318 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001319 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001320 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001321 }
1322 else if (IsShadowSampler(textureFunction->sampler))
1323 {
1324 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001325 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001326 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001327 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1328 // The resulting third component of P' in the shadow forms is used as Dref
1329 out << "), t.z" << proj;
1330 }
1331 else
1332 {
1333 switch(textureFunction->coords)
1334 {
1335 case 3: out << "), t.z"; break;
1336 case 4: out << "), t.w"; break;
1337 default: UNREACHABLE();
1338 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001339 }
1340 }
1341 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001342 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001343 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001344 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001345 case TextureFunction::IMPLICIT: out << ")"; break;
1346 case TextureFunction::BIAS: out << "), bias"; break;
1347 case TextureFunction::LOD: out << "), lod"; break;
1348 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001349 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001350 default: UNREACHABLE();
1351 }
1352 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001353
1354 if (textureFunction->offset)
1355 {
1356 out << ", offset";
1357 }
1358
1359 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001360 }
1361 else UNREACHABLE();
1362 }
1363
1364 out << "\n"
1365 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001366 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001367 }
1368
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001369 if (mUsesFragCoord)
1370 {
1371 out << "#define GL_USES_FRAG_COORD\n";
1372 }
1373
1374 if (mUsesPointCoord)
1375 {
1376 out << "#define GL_USES_POINT_COORD\n";
1377 }
1378
1379 if (mUsesFrontFacing)
1380 {
1381 out << "#define GL_USES_FRONT_FACING\n";
1382 }
1383
1384 if (mUsesPointSize)
1385 {
1386 out << "#define GL_USES_POINT_SIZE\n";
1387 }
1388
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001389 if (mUsesFragDepth)
1390 {
1391 out << "#define GL_USES_FRAG_DEPTH\n";
1392 }
1393
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001394 if (mUsesDepthRange)
1395 {
1396 out << "#define GL_USES_DEPTH_RANGE\n";
1397 }
1398
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001399 if (mUsesXor)
1400 {
1401 out << "bool xor(bool p, bool q)\n"
1402 "{\n"
1403 " return (p || q) && !(p && q);\n"
1404 "}\n"
1405 "\n";
1406 }
1407
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001408 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001409}
1410
1411void OutputHLSL::visitSymbol(TIntermSymbol *node)
1412{
Jamie Madill32aab012015-01-27 14:12:26 -05001413 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001414
Jamie Madill570e04d2013-06-21 09:15:33 -04001415 // Handle accessing std140 structs by value
1416 if (mFlaggedStructMappedNames.count(node) > 0)
1417 {
1418 out << mFlaggedStructMappedNames[node];
1419 return;
1420 }
1421
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001422 TString name = node->getSymbol();
1423
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001424 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001425 {
1426 mUsesDepthRange = true;
1427 out << name;
1428 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001429 else
1430 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001431 TQualifier qualifier = node->getQualifier();
1432
1433 if (qualifier == EvqUniform)
1434 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001435 const TType &nodeType = node->getType();
1436 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001437
1438 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001439 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001440 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001441 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001442 else
1443 {
1444 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001445 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001446
Jamie Madill2e295e22015-04-29 10:41:33 -04001447 ensureStructDefined(nodeType);
1448
Jamie Madill033dae62014-06-18 12:56:28 -04001449 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001450 }
Jamie Madill19571812013-08-12 15:26:34 -07001451 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001452 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001453 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001454 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001455 }
Jamie Madill033dae62014-06-18 12:56:28 -04001456 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001457 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001458 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001459 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001460 }
Jamie Madill19571812013-08-12 15:26:34 -07001461 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001462 {
1463 mReferencedOutputVariables[name] = node;
1464 out << "out_" << name;
1465 }
1466 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001467 {
1468 out << "gl_Color[0]";
1469 mUsesFragColor = true;
1470 }
1471 else if (qualifier == EvqFragData)
1472 {
1473 out << "gl_Color";
1474 mUsesFragData = true;
1475 }
1476 else if (qualifier == EvqFragCoord)
1477 {
1478 mUsesFragCoord = true;
1479 out << name;
1480 }
1481 else if (qualifier == EvqPointCoord)
1482 {
1483 mUsesPointCoord = true;
1484 out << name;
1485 }
1486 else if (qualifier == EvqFrontFacing)
1487 {
1488 mUsesFrontFacing = true;
1489 out << name;
1490 }
1491 else if (qualifier == EvqPointSize)
1492 {
1493 mUsesPointSize = true;
1494 out << name;
1495 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001496 else if (qualifier == EvqInstanceID)
1497 {
1498 mUsesInstanceID = true;
1499 out << name;
1500 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001501 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001502 {
1503 mUsesFragDepth = true;
1504 out << "gl_Depth";
1505 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001506 else
1507 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001508 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001509 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001510 }
1511}
1512
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001513void OutputHLSL::visitRaw(TIntermRaw *node)
1514{
Jamie Madill32aab012015-01-27 14:12:26 -05001515 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001516}
1517
Olli Etuaho7fb49552015-03-18 17:27:44 +02001518void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1519{
1520 if (type.isScalar() && !type.isArray())
1521 {
1522 if (op == EOpEqual)
1523 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001524 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001525 }
1526 else
1527 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001528 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001529 }
1530 }
1531 else
1532 {
1533 if (visit == PreVisit && op == EOpNotEqual)
1534 {
1535 out << "!";
1536 }
1537
1538 if (type.isArray())
1539 {
1540 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001541 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001542 }
1543 else if (type.getBasicType() == EbtStruct)
1544 {
1545 const TStructure &structure = *type.getStruct();
1546 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001547 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001548 }
1549 else
1550 {
1551 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001552 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001553 }
1554 }
1555}
1556
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001557bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1558{
Jamie Madill32aab012015-01-27 14:12:26 -05001559 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001560
Jamie Madill570e04d2013-06-21 09:15:33 -04001561 // Handle accessing std140 structs by value
1562 if (mFlaggedStructMappedNames.count(node) > 0)
1563 {
1564 out << mFlaggedStructMappedNames[node];
1565 return false;
1566 }
1567
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001568 switch (node->getOp())
1569 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001570 case EOpAssign:
1571 if (node->getLeft()->isArray())
1572 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001573 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1574 if (rightAgg != nullptr && rightAgg->isConstructor())
1575 {
1576 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1577 out << functionName << "(";
1578 node->getLeft()->traverse(this);
1579 TIntermSequence *seq = rightAgg->getSequence();
1580 for (auto &arrayElement : *seq)
1581 {
1582 out << ", ";
1583 arrayElement->traverse(this);
1584 }
1585 out << ")";
1586 return false;
1587 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001588 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1589 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1590
1591 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001592 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001593 }
1594 else
1595 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001596 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001597 }
1598 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001599 case EOpInitialize:
1600 if (visit == PreVisit)
1601 {
1602 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1603 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1604 // new variable is created before the assignment is evaluated), so we need to convert
1605 // this to "float t = x, x = t;".
1606
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001607 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001608 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001609 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001610
Jamie Madill37997142015-01-28 10:06:34 -05001611 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1612 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001613 {
Jamie Madill37997142015-01-28 10:06:34 -05001614 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001615 // after we initialize uniforms.
1616 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1617 deferredInit->setLeft(node->getLeft());
1618 deferredInit->setRight(node->getRight());
1619 deferredInit->setType(node->getType());
1620 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001621 const TString &initString = initializer(node->getType());
1622 node->setRight(new TIntermRaw(node->getType(), initString));
1623 }
1624 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1625 {
1626 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001627 return false;
1628 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001629 else if (writeConstantInitialization(out, symbolNode, expression))
1630 {
1631 return false;
1632 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001633 }
1634 else if (visit == InVisit)
1635 {
1636 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001637 }
1638 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001639 case EOpAddAssign:
1640 outputTriplet(out, visit, "(", " += ", ")");
1641 break;
1642 case EOpSubAssign:
1643 outputTriplet(out, visit, "(", " -= ", ")");
1644 break;
1645 case EOpMulAssign:
1646 outputTriplet(out, visit, "(", " *= ", ")");
1647 break;
1648 case EOpVectorTimesScalarAssign:
1649 outputTriplet(out, visit, "(", " *= ", ")");
1650 break;
1651 case EOpMatrixTimesScalarAssign:
1652 outputTriplet(out, visit, "(", " *= ", ")");
1653 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001654 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001655 if (visit == PreVisit)
1656 {
1657 out << "(";
1658 }
1659 else if (visit == InVisit)
1660 {
1661 out << " = mul(";
1662 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001663 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001664 }
1665 else
1666 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001667 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001668 }
1669 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001670 case EOpMatrixTimesMatrixAssign:
1671 if (visit == PreVisit)
1672 {
1673 out << "(";
1674 }
1675 else if (visit == InVisit)
1676 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001677 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001678 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001679 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001680 }
1681 else
1682 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001683 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001684 }
1685 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001686 case EOpDivAssign:
1687 outputTriplet(out, visit, "(", " /= ", ")");
1688 break;
1689 case EOpIModAssign:
1690 outputTriplet(out, visit, "(", " %= ", ")");
1691 break;
1692 case EOpBitShiftLeftAssign:
1693 outputTriplet(out, visit, "(", " <<= ", ")");
1694 break;
1695 case EOpBitShiftRightAssign:
1696 outputTriplet(out, visit, "(", " >>= ", ")");
1697 break;
1698 case EOpBitwiseAndAssign:
1699 outputTriplet(out, visit, "(", " &= ", ")");
1700 break;
1701 case EOpBitwiseXorAssign:
1702 outputTriplet(out, visit, "(", " ^= ", ")");
1703 break;
1704 case EOpBitwiseOrAssign:
1705 outputTriplet(out, visit, "(", " |= ", ")");
1706 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001707 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001708 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001709 const TType& leftType = node->getLeft()->getType();
1710 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001711 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001712 if (visit == PreVisit)
1713 {
1714 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1715 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001716 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001717 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001718 return false;
1719 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001720 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001721 else
1722 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001723 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001724 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001725 }
1726 break;
1727 case EOpIndexIndirect:
1728 // We do not currently support indirect references to interface blocks
1729 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001730 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001731 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001732 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001733 if (visit == InVisit)
1734 {
1735 const TStructure* structure = node->getLeft()->getType().getStruct();
1736 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1737 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001738 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001739
1740 return false;
1741 }
1742 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001743 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001744 if (visit == InVisit)
1745 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001746 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1747 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1748 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001749 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001750
1751 return false;
1752 }
1753 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754 case EOpVectorSwizzle:
1755 if (visit == InVisit)
1756 {
1757 out << ".";
1758
1759 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1760
1761 if (swizzle)
1762 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001763 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001764
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001765 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001766 {
1767 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1768
1769 if (element)
1770 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001771 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001772
1773 switch (i)
1774 {
1775 case 0: out << "x"; break;
1776 case 1: out << "y"; break;
1777 case 2: out << "z"; break;
1778 case 3: out << "w"; break;
1779 default: UNREACHABLE();
1780 }
1781 }
1782 else UNREACHABLE();
1783 }
1784 }
1785 else UNREACHABLE();
1786
1787 return false; // Fully processed
1788 }
1789 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001790 case EOpAdd:
1791 outputTriplet(out, visit, "(", " + ", ")");
1792 break;
1793 case EOpSub:
1794 outputTriplet(out, visit, "(", " - ", ")");
1795 break;
1796 case EOpMul:
1797 outputTriplet(out, visit, "(", " * ", ")");
1798 break;
1799 case EOpDiv:
1800 outputTriplet(out, visit, "(", " / ", ")");
1801 break;
1802 case EOpIMod:
1803 outputTriplet(out, visit, "(", " % ", ")");
1804 break;
1805 case EOpBitShiftLeft:
1806 outputTriplet(out, visit, "(", " << ", ")");
1807 break;
1808 case EOpBitShiftRight:
1809 outputTriplet(out, visit, "(", " >> ", ")");
1810 break;
1811 case EOpBitwiseAnd:
1812 outputTriplet(out, visit, "(", " & ", ")");
1813 break;
1814 case EOpBitwiseXor:
1815 outputTriplet(out, visit, "(", " ^ ", ")");
1816 break;
1817 case EOpBitwiseOr:
1818 outputTriplet(out, visit, "(", " | ", ")");
1819 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001820 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001821 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001822 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001823 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001824 case EOpLessThan:
1825 outputTriplet(out, visit, "(", " < ", ")");
1826 break;
1827 case EOpGreaterThan:
1828 outputTriplet(out, visit, "(", " > ", ")");
1829 break;
1830 case EOpLessThanEqual:
1831 outputTriplet(out, visit, "(", " <= ", ")");
1832 break;
1833 case EOpGreaterThanEqual:
1834 outputTriplet(out, visit, "(", " >= ", ")");
1835 break;
1836 case EOpVectorTimesScalar:
1837 outputTriplet(out, visit, "(", " * ", ")");
1838 break;
1839 case EOpMatrixTimesScalar:
1840 outputTriplet(out, visit, "(", " * ", ")");
1841 break;
1842 case EOpVectorTimesMatrix:
1843 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1844 break;
1845 case EOpMatrixTimesVector:
1846 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1847 break;
1848 case EOpMatrixTimesMatrix:
1849 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1850 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001851 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001852 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1853 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001854 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001855 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001856 case EOpLogicalXor:
1857 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001858 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001859 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001860 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001861 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1862 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001863 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001864 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865 default: UNREACHABLE();
1866 }
1867
1868 return true;
1869}
1870
1871bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1872{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001873 TInfoSinkBase &out = getInfoSink();
1874
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001875 switch (node->getOp())
1876 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001877 case EOpNegative:
1878 outputTriplet(out, visit, "(-", "", ")");
1879 break;
1880 case EOpPositive:
1881 outputTriplet(out, visit, "(+", "", ")");
1882 break;
1883 case EOpVectorLogicalNot:
1884 outputTriplet(out, visit, "(!", "", ")");
1885 break;
1886 case EOpLogicalNot:
1887 outputTriplet(out, visit, "(!", "", ")");
1888 break;
1889 case EOpBitwiseNot:
1890 outputTriplet(out, visit, "(~", "", ")");
1891 break;
1892 case EOpPostIncrement:
1893 outputTriplet(out, visit, "(", "", "++)");
1894 break;
1895 case EOpPostDecrement:
1896 outputTriplet(out, visit, "(", "", "--)");
1897 break;
1898 case EOpPreIncrement:
1899 outputTriplet(out, visit, "(++", "", ")");
1900 break;
1901 case EOpPreDecrement:
1902 outputTriplet(out, visit, "(--", "", ")");
1903 break;
1904 case EOpRadians:
1905 outputTriplet(out, visit, "radians(", "", ")");
1906 break;
1907 case EOpDegrees:
1908 outputTriplet(out, visit, "degrees(", "", ")");
1909 break;
1910 case EOpSin:
1911 outputTriplet(out, visit, "sin(", "", ")");
1912 break;
1913 case EOpCos:
1914 outputTriplet(out, visit, "cos(", "", ")");
1915 break;
1916 case EOpTan:
1917 outputTriplet(out, visit, "tan(", "", ")");
1918 break;
1919 case EOpAsin:
1920 outputTriplet(out, visit, "asin(", "", ")");
1921 break;
1922 case EOpAcos:
1923 outputTriplet(out, visit, "acos(", "", ")");
1924 break;
1925 case EOpAtan:
1926 outputTriplet(out, visit, "atan(", "", ")");
1927 break;
1928 case EOpSinh:
1929 outputTriplet(out, visit, "sinh(", "", ")");
1930 break;
1931 case EOpCosh:
1932 outputTriplet(out, visit, "cosh(", "", ")");
1933 break;
1934 case EOpTanh:
1935 outputTriplet(out, visit, "tanh(", "", ")");
1936 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001937 case EOpAsinh:
1938 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001939 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001940 break;
1941 case EOpAcosh:
1942 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001943 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001944 break;
1945 case EOpAtanh:
1946 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001947 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001948 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001949 case EOpExp:
1950 outputTriplet(out, visit, "exp(", "", ")");
1951 break;
1952 case EOpLog:
1953 outputTriplet(out, visit, "log(", "", ")");
1954 break;
1955 case EOpExp2:
1956 outputTriplet(out, visit, "exp2(", "", ")");
1957 break;
1958 case EOpLog2:
1959 outputTriplet(out, visit, "log2(", "", ")");
1960 break;
1961 case EOpSqrt:
1962 outputTriplet(out, visit, "sqrt(", "", ")");
1963 break;
1964 case EOpInverseSqrt:
1965 outputTriplet(out, visit, "rsqrt(", "", ")");
1966 break;
1967 case EOpAbs:
1968 outputTriplet(out, visit, "abs(", "", ")");
1969 break;
1970 case EOpSign:
1971 outputTriplet(out, visit, "sign(", "", ")");
1972 break;
1973 case EOpFloor:
1974 outputTriplet(out, visit, "floor(", "", ")");
1975 break;
1976 case EOpTrunc:
1977 outputTriplet(out, visit, "trunc(", "", ")");
1978 break;
1979 case EOpRound:
1980 outputTriplet(out, visit, "round(", "", ")");
1981 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001982 case EOpRoundEven:
1983 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001984 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001985 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001986 case EOpCeil:
1987 outputTriplet(out, visit, "ceil(", "", ")");
1988 break;
1989 case EOpFract:
1990 outputTriplet(out, visit, "frac(", "", ")");
1991 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301992 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001993 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05301994 mRequiresIEEEStrictCompiling = true;
1995 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001996 case EOpIsInf:
1997 outputTriplet(out, visit, "isinf(", "", ")");
1998 break;
1999 case EOpFloatBitsToInt:
2000 outputTriplet(out, visit, "asint(", "", ")");
2001 break;
2002 case EOpFloatBitsToUint:
2003 outputTriplet(out, visit, "asuint(", "", ")");
2004 break;
2005 case EOpIntBitsToFloat:
2006 outputTriplet(out, visit, "asfloat(", "", ")");
2007 break;
2008 case EOpUintBitsToFloat:
2009 outputTriplet(out, visit, "asfloat(", "", ")");
2010 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002011 case EOpPackSnorm2x16:
2012 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002013 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002014 break;
2015 case EOpPackUnorm2x16:
2016 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002017 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002018 break;
2019 case EOpPackHalf2x16:
2020 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002021 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002022 break;
2023 case EOpUnpackSnorm2x16:
2024 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002025 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002026 break;
2027 case EOpUnpackUnorm2x16:
2028 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002029 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002030 break;
2031 case EOpUnpackHalf2x16:
2032 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002033 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002034 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002035 case EOpLength:
2036 outputTriplet(out, visit, "length(", "", ")");
2037 break;
2038 case EOpNormalize:
2039 outputTriplet(out, visit, "normalize(", "", ")");
2040 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002041 case EOpDFdx:
2042 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2043 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002044 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002045 }
2046 else
2047 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002048 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002049 }
2050 break;
2051 case EOpDFdy:
2052 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2053 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002054 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002055 }
2056 else
2057 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002058 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002059 }
2060 break;
2061 case EOpFwidth:
2062 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2063 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002064 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002065 }
2066 else
2067 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002068 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002069 }
2070 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002071 case EOpTranspose:
2072 outputTriplet(out, visit, "transpose(", "", ")");
2073 break;
2074 case EOpDeterminant:
2075 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2076 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002077 case EOpInverse:
2078 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002079 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002080 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002081
Jamie Madill8c46ab12015-12-07 16:39:19 -05002082 case EOpAny:
2083 outputTriplet(out, visit, "any(", "", ")");
2084 break;
2085 case EOpAll:
2086 outputTriplet(out, visit, "all(", "", ")");
2087 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 default: UNREACHABLE();
2089 }
2090
2091 return true;
2092}
2093
2094bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2095{
Jamie Madill32aab012015-01-27 14:12:26 -05002096 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098 switch (node->getOp())
2099 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002100 case EOpSequence:
2101 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002102 if (mInsideFunction)
2103 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002104 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002105 out << "{\n";
2106 }
2107
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002108 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002109 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002110 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002111
Olli Etuahoa6f22092015-05-08 18:31:10 +03002112 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002113
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002114 // Don't output ; after case labels, they're terminated by :
2115 // This is needed especially since outputting a ; after a case statement would turn empty
2116 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002117 // Also no need to output ; after selection (if) statements or sequences. This is done just
2118 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002119 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2120 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002121 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002122 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002123 }
2124
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002125 if (mInsideFunction)
2126 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002127 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002128 out << "}\n";
2129 }
2130
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002131 return false;
2132 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 case EOpDeclaration:
2134 if (visit == PreVisit)
2135 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002136 TIntermSequence *sequence = node->getSequence();
2137 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002138 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002140 if (variable &&
2141 (variable->getQualifier() == EvqTemporary ||
2142 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002143 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002144 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002145
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002146 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002148 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002150 out << "static ";
2151 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002152
Olli Etuahoa6f22092015-05-08 18:31:10 +03002153 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002154
Olli Etuahoa6f22092015-05-08 18:31:10 +03002155 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002156
Olli Etuahoa6f22092015-05-08 18:31:10 +03002157 if (symbol)
2158 {
2159 symbol->traverse(this);
2160 out << ArrayString(symbol->getType());
2161 out << " = " + initializer(symbol->getType());
2162 }
2163 else
2164 {
2165 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002168 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2169 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002170 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002171 }
2172 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173 }
Jamie Madill033dae62014-06-18 12:56:28 -04002174 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002175 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002176 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002177 {
2178 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2179
2180 if (symbol)
2181 {
2182 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2183 mReferencedVaryings[symbol->getSymbol()] = symbol;
2184 }
2185 else
2186 {
2187 (*sit)->traverse(this);
2188 }
2189 }
2190 }
2191
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002192 return false;
2193 }
2194 else if (visit == InVisit)
2195 {
2196 out << ", ";
2197 }
2198 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002199 case EOpInvariantDeclaration:
2200 // Do not do any translation
2201 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002202 case EOpPrototype:
2203 if (visit == PreVisit)
2204 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002205 size_t index = mCallDag.findIndex(node);
2206 // Skip the prototype if it is not implemented (and thus not used)
2207 if (index == CallDAG::InvalidIndex)
2208 {
2209 return false;
2210 }
2211
Olli Etuaho59f9a642015-08-06 20:38:26 +03002212 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2213 out << TypeString(node->getType()) << " " << name
2214 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002215
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002216 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002217
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002218 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002219 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002220 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002221
2222 if (symbol)
2223 {
2224 out << argumentString(symbol);
2225
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002226 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002227 {
2228 out << ", ";
2229 }
2230 }
2231 else UNREACHABLE();
2232 }
2233
2234 out << ");\n";
2235
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002236 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002237 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2238 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002239 {
2240 mOutputLod0Function = true;
2241 node->traverse(this);
2242 mOutputLod0Function = false;
2243 }
2244
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002245 return false;
2246 }
2247 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002248 case EOpComma:
2249 outputTriplet(out, visit, "(", ", ", ")");
2250 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002251 case EOpFunction:
2252 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002253 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002254 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255
Corentin Wallez1239ee92015-03-19 14:38:02 -07002256 size_t index = mCallDag.findIndex(node);
2257 ASSERT(index != CallDAG::InvalidIndex);
2258 mCurrentFunctionMetadata = &mASTMetadataList[index];
2259
Jamie Madill033dae62014-06-18 12:56:28 -04002260 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002261
2262 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002264 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002265 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002266 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002267 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002268 out << DecorateFunctionIfNeeded(node->getNameObj())
2269 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002270 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002271
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002272 TIntermSequence *sequence = node->getSequence();
2273 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002274
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002275 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002276 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002277 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002278
2279 if (symbol)
2280 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002281 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002282
2283 out << argumentString(symbol);
2284
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002285 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002286 {
2287 out << ", ";
2288 }
2289 }
2290 else UNREACHABLE();
2291 }
2292
Olli Etuaho4785fec2015-05-18 16:09:37 +03002293 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002294
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002295 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002296 {
2297 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002298 TIntermNode *body = (*sequence)[1];
2299 // The function body node will output braces.
2300 ASSERT(IsSequence(body));
2301 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002302 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002304 else
2305 {
2306 out << "{}\n";
2307 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002308
Corentin Wallez1239ee92015-03-19 14:38:02 -07002309 mCurrentFunctionMetadata = nullptr;
2310
2311 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2312 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002313 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002314 ASSERT(name != "main");
2315 mOutputLod0Function = true;
2316 node->traverse(this);
2317 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002318 }
2319
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002320 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321 }
2322 break;
2323 case EOpFunctionCall:
2324 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002325 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002326
Corentin Wallez1239ee92015-03-19 14:38:02 -07002327 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002328 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002330 if (node->isArray())
2331 {
2332 UNIMPLEMENTED();
2333 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002334 size_t index = mCallDag.findIndex(node);
2335 ASSERT(index != CallDAG::InvalidIndex);
2336 lod0 &= mASTMetadataList[index].mNeedsLod0;
2337
Olli Etuaho59f9a642015-08-06 20:38:26 +03002338 out << DecorateFunctionIfNeeded(node->getNameObj()) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339 }
2340 else
2341 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002342 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002343 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002344
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002345 TextureFunction textureFunction;
2346 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002347 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002348 textureFunction.method = TextureFunction::IMPLICIT;
2349 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002350 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002351
2352 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002353 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002354 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002355 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002356 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002357 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002358 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002359 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002360 }
Nicolas Capens46485082014-04-15 13:12:50 -04002361 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2362 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002363 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002364 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002365 }
Nicolas Capens46485082014-04-15 13:12:50 -04002366 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002367 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002368 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002369 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002370 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002371 else if (name == "textureSize")
2372 {
2373 textureFunction.method = TextureFunction::SIZE;
2374 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002375 else if (name == "textureOffset")
2376 {
2377 textureFunction.method = TextureFunction::IMPLICIT;
2378 textureFunction.offset = true;
2379 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002380 else if (name == "textureProjOffset")
2381 {
2382 textureFunction.method = TextureFunction::IMPLICIT;
2383 textureFunction.offset = true;
2384 textureFunction.proj = true;
2385 }
2386 else if (name == "textureLodOffset")
2387 {
2388 textureFunction.method = TextureFunction::LOD;
2389 textureFunction.offset = true;
2390 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002391 else if (name == "textureProjLodOffset")
2392 {
2393 textureFunction.method = TextureFunction::LOD;
2394 textureFunction.proj = true;
2395 textureFunction.offset = true;
2396 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002397 else if (name == "texelFetch")
2398 {
2399 textureFunction.method = TextureFunction::FETCH;
2400 }
2401 else if (name == "texelFetchOffset")
2402 {
2403 textureFunction.method = TextureFunction::FETCH;
2404 textureFunction.offset = true;
2405 }
Nicolas Capens46485082014-04-15 13:12:50 -04002406 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002407 {
2408 textureFunction.method = TextureFunction::GRAD;
2409 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002410 else if (name == "textureGradOffset")
2411 {
2412 textureFunction.method = TextureFunction::GRAD;
2413 textureFunction.offset = true;
2414 }
Nicolas Capens46485082014-04-15 13:12:50 -04002415 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002416 {
2417 textureFunction.method = TextureFunction::GRAD;
2418 textureFunction.proj = true;
2419 }
2420 else if (name == "textureProjGradOffset")
2421 {
2422 textureFunction.method = TextureFunction::GRAD;
2423 textureFunction.proj = true;
2424 textureFunction.offset = true;
2425 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002426 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002427
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002428 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002429 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002430 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2431
2432 if (textureFunction.offset)
2433 {
2434 mandatoryArgumentCount++;
2435 }
2436
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002437 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002438
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002439 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002440 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002441 if (bias)
2442 {
2443 textureFunction.method = TextureFunction::LOD0BIAS;
2444 }
2445 else
2446 {
2447 textureFunction.method = TextureFunction::LOD0;
2448 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002449 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002450 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002451 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002452 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002453 }
2454 }
2455
2456 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002457
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002458 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002459 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002460
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002461 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002462 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002463 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT &&
2464 IsSampler((*arg)->getAsTyped()->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002465 {
2466 out << "texture_";
2467 (*arg)->traverse(this);
2468 out << ", sampler_";
2469 }
2470
2471 (*arg)->traverse(this);
2472
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002473 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002474 {
2475 out << ", ";
2476 }
2477 }
2478
2479 out << ")";
2480
2481 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002482 }
2483 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002484 case EOpParameters:
2485 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2486 break;
2487 case EOpConstructFloat:
2488 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2489 break;
2490 case EOpConstructVec2:
2491 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2492 break;
2493 case EOpConstructVec3:
2494 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2495 break;
2496 case EOpConstructVec4:
2497 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2498 break;
2499 case EOpConstructBool:
2500 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2501 break;
2502 case EOpConstructBVec2:
2503 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2504 break;
2505 case EOpConstructBVec3:
2506 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2507 break;
2508 case EOpConstructBVec4:
2509 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2510 break;
2511 case EOpConstructInt:
2512 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2513 break;
2514 case EOpConstructIVec2:
2515 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2516 break;
2517 case EOpConstructIVec3:
2518 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2519 break;
2520 case EOpConstructIVec4:
2521 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2522 break;
2523 case EOpConstructUInt:
2524 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2525 break;
2526 case EOpConstructUVec2:
2527 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2528 break;
2529 case EOpConstructUVec3:
2530 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2531 break;
2532 case EOpConstructUVec4:
2533 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2534 break;
2535 case EOpConstructMat2:
2536 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2537 break;
2538 case EOpConstructMat2x3:
2539 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2540 break;
2541 case EOpConstructMat2x4:
2542 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2543 break;
2544 case EOpConstructMat3x2:
2545 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2546 break;
2547 case EOpConstructMat3:
2548 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2549 break;
2550 case EOpConstructMat3x4:
2551 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2552 break;
2553 case EOpConstructMat4x2:
2554 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2555 break;
2556 case EOpConstructMat4x3:
2557 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2558 break;
2559 case EOpConstructMat4:
2560 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2561 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002562 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002563 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002564 if (node->getType().isArray())
2565 {
2566 UNIMPLEMENTED();
2567 }
Jamie Madill033dae62014-06-18 12:56:28 -04002568 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002569 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002570 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002571 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002572 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002573 case EOpLessThan:
2574 outputTriplet(out, visit, "(", " < ", ")");
2575 break;
2576 case EOpGreaterThan:
2577 outputTriplet(out, visit, "(", " > ", ")");
2578 break;
2579 case EOpLessThanEqual:
2580 outputTriplet(out, visit, "(", " <= ", ")");
2581 break;
2582 case EOpGreaterThanEqual:
2583 outputTriplet(out, visit, "(", " >= ", ")");
2584 break;
2585 case EOpVectorEqual:
2586 outputTriplet(out, visit, "(", " == ", ")");
2587 break;
2588 case EOpVectorNotEqual:
2589 outputTriplet(out, visit, "(", " != ", ")");
2590 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002591 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002592 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002593 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002594 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002595 case EOpModf:
2596 outputTriplet(out, visit, "modf(", ", ", ")");
2597 break;
2598 case EOpPow:
2599 outputTriplet(out, visit, "pow(", ", ", ")");
2600 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002601 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002602 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002603 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002604 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002605 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002606 case EOpMin:
2607 outputTriplet(out, visit, "min(", ", ", ")");
2608 break;
2609 case EOpMax:
2610 outputTriplet(out, visit, "max(", ", ", ")");
2611 break;
2612 case EOpClamp:
2613 outputTriplet(out, visit, "clamp(", ", ", ")");
2614 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302615 case EOpMix:
2616 {
2617 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2618 if (lastParamNode->getType().getBasicType() == EbtBool)
2619 {
2620 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2621 // so use emulated version.
2622 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002623 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302624 }
2625 else
2626 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002627 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302628 }
2629 }
2630 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002631 case EOpStep:
2632 outputTriplet(out, visit, "step(", ", ", ")");
2633 break;
2634 case EOpSmoothStep:
2635 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2636 break;
2637 case EOpDistance:
2638 outputTriplet(out, visit, "distance(", ", ", ")");
2639 break;
2640 case EOpDot:
2641 outputTriplet(out, visit, "dot(", ", ", ")");
2642 break;
2643 case EOpCross:
2644 outputTriplet(out, visit, "cross(", ", ", ")");
2645 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002646 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002647 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002648 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002649 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002650 case EOpReflect:
2651 outputTriplet(out, visit, "reflect(", ", ", ")");
2652 break;
2653 case EOpRefract:
2654 outputTriplet(out, visit, "refract(", ", ", ")");
2655 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002656 case EOpOuterProduct:
2657 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002658 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002659 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002660 case EOpMul:
2661 outputTriplet(out, visit, "(", " * ", ")");
2662 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002663 default: UNREACHABLE();
2664 }
2665
2666 return true;
2667}
2668
Jamie Madill8c46ab12015-12-07 16:39:19 -05002669void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002670{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002671 out << "if (";
2672
2673 node->getCondition()->traverse(this);
2674
2675 out << ")\n";
2676
Jamie Madill8c46ab12015-12-07 16:39:19 -05002677 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002678
2679 bool discard = false;
2680
2681 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002682 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002683 // The trueBlock child node will output braces.
2684 ASSERT(IsSequence(node->getTrueBlock()));
2685
Olli Etuahoa6f22092015-05-08 18:31:10 +03002686 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002687
Olli Etuahoa6f22092015-05-08 18:31:10 +03002688 // Detect true discard
2689 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2690 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002691 else
2692 {
2693 // TODO(oetuaho): Check if the semicolon inside is necessary.
2694 // It's there as a result of conservative refactoring of the output.
2695 out << "{;}\n";
2696 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002697
Jamie Madill8c46ab12015-12-07 16:39:19 -05002698 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002699
Olli Etuahoa6f22092015-05-08 18:31:10 +03002700 if (node->getFalseBlock())
2701 {
2702 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002703
Jamie Madill8c46ab12015-12-07 16:39:19 -05002704 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002705
Olli Etuaho4785fec2015-05-18 16:09:37 +03002706 // Either this is "else if" or the falseBlock child node will output braces.
2707 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2708
Olli Etuahoa6f22092015-05-08 18:31:10 +03002709 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002710
Jamie Madill8c46ab12015-12-07 16:39:19 -05002711 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002712
Olli Etuahoa6f22092015-05-08 18:31:10 +03002713 // Detect false discard
2714 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2715 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002716
Olli Etuahoa6f22092015-05-08 18:31:10 +03002717 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002718 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002719 {
2720 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002721 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002722}
2723
2724bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2725{
2726 TInfoSinkBase &out = getInfoSink();
2727
2728 ASSERT(!node->usesTernaryOperator());
2729
2730 if (!mInsideFunction)
2731 {
2732 // This is part of unfolded global initialization.
2733 mDeferredGlobalInitializers.push_back(node);
2734 return false;
2735 }
2736
2737 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002738 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002739 {
2740 out << "FLATTEN ";
2741 }
2742
Jamie Madill8c46ab12015-12-07 16:39:19 -05002743 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002744
2745 return false;
2746}
2747
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002748bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002749{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002750 TInfoSinkBase &out = getInfoSink();
2751
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002752 if (node->getStatementList())
2753 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002754 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002755 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002756 // The curly braces get written when visiting the statementList aggregate
2757 }
2758 else
2759 {
2760 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002761 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002762 }
2763 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002764}
2765
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002766bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002767{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002768 TInfoSinkBase &out = getInfoSink();
2769
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002770 if (node->hasCondition())
2771 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002772 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002773 return true;
2774 }
2775 else
2776 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002777 out << "default:\n";
2778 return false;
2779 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002780}
2781
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002782void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2783{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002784 TInfoSinkBase &out = getInfoSink();
2785 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786}
2787
2788bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2789{
Nicolas Capens655fe362014-04-11 13:12:34 -04002790 mNestedLoopDepth++;
2791
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002792 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002793 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002794 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002795
Jamie Madill8c46ab12015-12-07 16:39:19 -05002796 TInfoSinkBase &out = getInfoSink();
2797
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002798 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002799 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002800 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002801 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002802 mInsideDiscontinuousLoop = wasDiscontinuous;
2803 mNestedLoopDepth--;
2804
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002805 return false;
2806 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002807 }
2808
Corentin Wallez1239ee92015-03-19 14:38:02 -07002809 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002810 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002811 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002812 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002813
Jamie Madill8c46ab12015-12-07 16:39:19 -05002814 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002815 }
2816 else
2817 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002818 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002819
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820 if (node->getInit())
2821 {
2822 node->getInit()->traverse(this);
2823 }
2824
2825 out << "; ";
2826
alokp@chromium.org52813552010-11-16 18:36:09 +00002827 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002828 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002829 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002830 }
2831
2832 out << "; ";
2833
alokp@chromium.org52813552010-11-16 18:36:09 +00002834 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002835 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002836 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002837 }
2838
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002839 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002840
Jamie Madill8c46ab12015-12-07 16:39:19 -05002841 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002842 }
2843
2844 if (node->getBody())
2845 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002846 // The loop body node will output braces.
2847 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002848 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002850 else
2851 {
2852 // TODO(oetuaho): Check if the semicolon inside is necessary.
2853 // It's there as a result of conservative refactoring of the output.
2854 out << "{;}\n";
2855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002856
Jamie Madill8c46ab12015-12-07 16:39:19 -05002857 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002858
alokp@chromium.org52813552010-11-16 18:36:09 +00002859 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002861 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002862 out << "while(\n";
2863
alokp@chromium.org52813552010-11-16 18:36:09 +00002864 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002865
daniel@transgaming.com73536982012-03-21 20:45:49 +00002866 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867 }
2868
daniel@transgaming.com73536982012-03-21 20:45:49 +00002869 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002871 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002872 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002873
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874 return false;
2875}
2876
2877bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2878{
Jamie Madill32aab012015-01-27 14:12:26 -05002879 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002880
2881 switch (node->getFlowOp())
2882 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002883 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002884 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002885 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002886 case EOpBreak:
2887 if (visit == PreVisit)
2888 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002889 if (mNestedLoopDepth > 1)
2890 {
2891 mUsesNestedBreak = true;
2892 }
2893
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002894 if (mExcessiveLoopIndex)
2895 {
2896 out << "{Break";
2897 mExcessiveLoopIndex->traverse(this);
2898 out << " = true; break;}\n";
2899 }
2900 else
2901 {
2902 out << "break;\n";
2903 }
2904 }
2905 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002906 case EOpContinue:
2907 outputTriplet(out, visit, "continue;\n", "", "");
2908 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002909 case EOpReturn:
2910 if (visit == PreVisit)
2911 {
2912 if (node->getExpression())
2913 {
2914 out << "return ";
2915 }
2916 else
2917 {
2918 out << "return;\n";
2919 }
2920 }
2921 else if (visit == PostVisit)
2922 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002923 if (node->getExpression())
2924 {
2925 out << ";\n";
2926 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002927 }
2928 break;
2929 default: UNREACHABLE();
2930 }
2931
2932 return true;
2933}
2934
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002935bool OutputHLSL::isSingleStatement(TIntermNode *node)
2936{
2937 TIntermAggregate *aggregate = node->getAsAggregate();
2938
2939 if (aggregate)
2940 {
2941 if (aggregate->getOp() == EOpSequence)
2942 {
2943 return false;
2944 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002945 else if (aggregate->getOp() == EOpDeclaration)
2946 {
2947 // Declaring multiple comma-separated variables must be considered multiple statements
2948 // because each individual declaration has side effects which are visible in the next.
2949 return false;
2950 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002951 else
2952 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002953 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002954 {
2955 if (!isSingleStatement(*sit))
2956 {
2957 return false;
2958 }
2959 }
2960
2961 return true;
2962 }
2963 }
2964
2965 return true;
2966}
2967
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002968// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2969// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002970bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002971{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002972 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002973
2974 // Parse loops of the form:
2975 // for(int index = initial; index [comparator] limit; index += increment)
2976 TIntermSymbol *index = NULL;
2977 TOperator comparator = EOpNull;
2978 int initial = 0;
2979 int limit = 0;
2980 int increment = 0;
2981
2982 // Parse index name and intial value
2983 if (node->getInit())
2984 {
2985 TIntermAggregate *init = node->getInit()->getAsAggregate();
2986
2987 if (init)
2988 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002989 TIntermSequence *sequence = init->getSequence();
2990 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002991
2992 if (variable && variable->getQualifier() == EvqTemporary)
2993 {
2994 TIntermBinary *assign = variable->getAsBinaryNode();
2995
2996 if (assign->getOp() == EOpInitialize)
2997 {
2998 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2999 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3000
3001 if (symbol && constant)
3002 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003003 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003004 {
3005 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003006 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003007 }
3008 }
3009 }
3010 }
3011 }
3012 }
3013
3014 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003015 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003016 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003017 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003018
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003019 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3020 {
3021 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3022
3023 if (constant)
3024 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003025 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003026 {
3027 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003028 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003029 }
3030 }
3031 }
3032 }
3033
3034 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003035 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003036 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003037 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3038 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003039
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003040 if (binaryTerminal)
3041 {
3042 TOperator op = binaryTerminal->getOp();
3043 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3044
3045 if (constant)
3046 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003047 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003048 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003049 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003050
3051 switch (op)
3052 {
3053 case EOpAddAssign: increment = value; break;
3054 case EOpSubAssign: increment = -value; break;
3055 default: UNIMPLEMENTED();
3056 }
3057 }
3058 }
3059 }
3060 else if (unaryTerminal)
3061 {
3062 TOperator op = unaryTerminal->getOp();
3063
3064 switch (op)
3065 {
3066 case EOpPostIncrement: increment = 1; break;
3067 case EOpPostDecrement: increment = -1; break;
3068 case EOpPreIncrement: increment = 1; break;
3069 case EOpPreDecrement: increment = -1; break;
3070 default: UNIMPLEMENTED();
3071 }
3072 }
3073 }
3074
3075 if (index != NULL && comparator != EOpNull && increment != 0)
3076 {
3077 if (comparator == EOpLessThanEqual)
3078 {
3079 comparator = EOpLessThan;
3080 limit += 1;
3081 }
3082
3083 if (comparator == EOpLessThan)
3084 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003085 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003086
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003087 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003088 {
3089 return false; // Not an excessive loop
3090 }
3091
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003092 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3093 mExcessiveLoopIndex = index;
3094
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003095 out << "{int ";
3096 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003097 out << ";\n"
3098 "bool Break";
3099 index->traverse(this);
3100 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003101
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003102 bool firstLoopFragment = true;
3103
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003104 while (iterations > 0)
3105 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003106 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003107
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003108 if (!firstLoopFragment)
3109 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003110 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003111 index->traverse(this);
3112 out << ") {\n";
3113 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003114
3115 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3116 {
3117 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3118 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003119
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003120 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003121 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003122
Corentin Wallez1239ee92015-03-19 14:38:02 -07003123 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003124 index->traverse(this);
3125 out << " = ";
3126 out << initial;
3127
3128 out << "; ";
3129 index->traverse(this);
3130 out << " < ";
3131 out << clampedLimit;
3132
3133 out << "; ";
3134 index->traverse(this);
3135 out << " += ";
3136 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003137 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003138
Jamie Madill8c46ab12015-12-07 16:39:19 -05003139 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003140 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003141
3142 if (node->getBody())
3143 {
3144 node->getBody()->traverse(this);
3145 }
3146
Jamie Madill8c46ab12015-12-07 16:39:19 -05003147 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003148 out << ";}\n";
3149
3150 if (!firstLoopFragment)
3151 {
3152 out << "}\n";
3153 }
3154
3155 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003156
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003157 initial += MAX_LOOP_ITERATIONS * increment;
3158 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003159 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003160
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003161 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003162
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003163 mExcessiveLoopIndex = restoreIndex;
3164
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003165 return true;
3166 }
3167 else UNIMPLEMENTED();
3168 }
3169
3170 return false; // Not handled as an excessive loop
3171}
3172
Jamie Madill8c46ab12015-12-07 16:39:19 -05003173void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3174 Visit visit,
3175 const char *preString,
3176 const char *inString,
3177 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003178{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003179 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003180 {
3181 out << preString;
3182 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003183 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003184 {
3185 out << inString;
3186 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003187 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003188 {
3189 out << postString;
3190 }
3191}
3192
Jamie Madill8c46ab12015-12-07 16:39:19 -05003193void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003194{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003195 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003196 {
Jamie Madill32aab012015-01-27 14:12:26 -05003197 out << "\n";
3198 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003199
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003200 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003201 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003202 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003203 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003204
Jamie Madill32aab012015-01-27 14:12:26 -05003205 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003206 }
3207}
3208
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003209TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3210{
3211 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003212 const TType &type = symbol->getType();
3213 const TName &name = symbol->getName();
3214 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003215
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003216 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003217 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003218 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003219 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003220 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003221 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003222 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003223 }
3224
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003225 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003226 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003227 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3228 {
3229 // Samplers are passed as indices to the sampler array.
3230 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3231 return "const uint " + nameStr + ArrayString(type);
3232 }
3233 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3234 {
3235 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3236 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3237 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3238 ArrayString(type);
3239 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003240 }
3241
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003242 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003243}
3244
3245TString OutputHLSL::initializer(const TType &type)
3246{
3247 TString string;
3248
Jamie Madill94bf7f22013-07-08 13:31:15 -04003249 size_t size = type.getObjectSize();
3250 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003251 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003252 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003253
Jamie Madill94bf7f22013-07-08 13:31:15 -04003254 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003255 {
3256 string += ", ";
3257 }
3258 }
3259
daniel@transgaming.comead23042010-04-29 03:35:36 +00003260 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003261}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003262
Jamie Madill8c46ab12015-12-07 16:39:19 -05003263void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3264 Visit visit,
3265 const TType &type,
3266 const char *name,
3267 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003268{
Olli Etuahof40319e2015-03-10 14:33:00 +02003269 if (type.isArray())
3270 {
3271 UNIMPLEMENTED();
3272 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003273
3274 if (visit == PreVisit)
3275 {
Jamie Madill8daaba12014-06-13 10:04:33 -04003276 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003277
Daniel Bratell29190082015-02-20 16:42:54 +01003278 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003279 }
3280 else if (visit == InVisit)
3281 {
3282 out << ", ";
3283 }
3284 else if (visit == PostVisit)
3285 {
3286 out << ")";
3287 }
3288}
3289
Jamie Madill8c46ab12015-12-07 16:39:19 -05003290const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3291 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003292 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003293{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003294 const TConstantUnion *constUnionIterated = constUnion;
3295
Jamie Madill98493dd2013-07-08 14:39:03 -04003296 const TStructure* structure = type.getStruct();
3297 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003298 {
Jamie Madill033dae62014-06-18 12:56:28 -04003299 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003300
Jamie Madill98493dd2013-07-08 14:39:03 -04003301 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003302
Jamie Madill98493dd2013-07-08 14:39:03 -04003303 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003304 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003305 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003306 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003307
Jamie Madill98493dd2013-07-08 14:39:03 -04003308 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003309 {
3310 out << ", ";
3311 }
3312 }
3313
3314 out << ")";
3315 }
3316 else
3317 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003318 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003319 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003320
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003321 if (writeType)
3322 {
Jamie Madill033dae62014-06-18 12:56:28 -04003323 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003324 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003325 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003326 if (writeType)
3327 {
3328 out << ")";
3329 }
3330 }
3331
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003332 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003333}
3334
Jamie Madill8c46ab12015-12-07 16:39:19 -05003335void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003336{
3337 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003338 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003339}
3340
Jamie Madill37997142015-01-28 10:06:34 -05003341bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3342{
3343 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3344 expression->traverse(&searchSymbol);
3345
3346 if (searchSymbol.foundMatch())
3347 {
3348 // Type already printed
3349 out << "t" + str(mUniqueIndex) + " = ";
3350 expression->traverse(this);
3351 out << ", ";
3352 symbolNode->traverse(this);
3353 out << " = t" + str(mUniqueIndex);
3354
3355 mUniqueIndex++;
3356 return true;
3357 }
3358
3359 return false;
3360}
3361
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003362bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3363{
3364 // We support writing constant unions and constructors that only take constant unions as
3365 // parameters as HLSL literals.
3366 if (expression->getAsConstantUnion())
3367 {
3368 return true;
3369 }
3370 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3371 !expression->getAsAggregate()->isConstructor())
3372 {
3373 return false;
3374 }
3375 TIntermAggregate *constructor = expression->getAsAggregate();
3376 for (TIntermNode *&node : *constructor->getSequence())
3377 {
3378 if (!node->getAsConstantUnion())
3379 return false;
3380 }
3381 return true;
3382}
3383
3384bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3385 TIntermSymbol *symbolNode,
3386 TIntermTyped *expression)
3387{
3388 if (canWriteAsHLSLLiteral(expression))
3389 {
3390 symbolNode->traverse(this);
3391 if (expression->getType().isArray())
3392 {
3393 out << "[" << expression->getType().getArraySize() << "]";
3394 }
3395 out << " = {";
3396 if (expression->getAsConstantUnion())
3397 {
3398 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3399 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3400 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3401 }
3402 else
3403 {
3404 TIntermAggregate *constructor = expression->getAsAggregate();
3405 ASSERT(constructor != nullptr);
3406 for (TIntermNode *&node : *constructor->getSequence())
3407 {
3408 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3409 ASSERT(nodeConst);
3410 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3411 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3412 if (node != constructor->getSequence()->back())
3413 {
3414 out << ", ";
3415 }
3416 }
3417 }
3418 out << "}";
3419 return true;
3420 }
3421 return false;
3422}
3423
Jamie Madill37997142015-01-28 10:06:34 -05003424void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3425{
3426 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3427 << "\n"
3428 << "void initializeDeferredGlobals()\n"
3429 << "{\n";
3430
3431 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3432 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003433 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3434 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3435 if (binary != nullptr)
3436 {
3437 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3438 TIntermTyped *expression = binary->getRight();
3439 ASSERT(symbol);
3440 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003441
Olli Etuahod81ed842015-05-12 12:46:35 +03003442 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003443
Olli Etuahod81ed842015-05-12 12:46:35 +03003444 if (!writeSameSymbolInitializer(out, symbol, expression))
3445 {
3446 ASSERT(mInfoSinkStack.top() == &out);
3447 expression->traverse(this);
3448 }
3449 out << ";\n";
3450 }
3451 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003452 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003453 writeSelection(out, selection);
Jamie Madill37997142015-01-28 10:06:34 -05003454 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003455 else
3456 {
3457 UNREACHABLE();
3458 }
Jamie Madill37997142015-01-28 10:06:34 -05003459 }
3460
3461 out << "}\n"
3462 << "\n";
3463}
3464
Jamie Madill55e79e02015-02-09 15:35:00 -05003465TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3466{
3467 const TFieldList &fields = structure.fields();
3468
3469 for (const auto &eqFunction : mStructEqualityFunctions)
3470 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003471 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003472 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003473 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003474 }
3475 }
3476
3477 const TString &structNameString = StructNameString(structure);
3478
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003479 StructEqualityFunction *function = new StructEqualityFunction();
3480 function->structure = &structure;
3481 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003482
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003483 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003484
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003485 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3486 << "{\n"
3487 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003488
3489 for (size_t i = 0; i < fields.size(); i++)
3490 {
3491 const TField *field = fields[i];
3492 const TType *fieldType = field->type();
3493
3494 const TString &fieldNameA = "a." + Decorate(field->name());
3495 const TString &fieldNameB = "b." + Decorate(field->name());
3496
3497 if (i > 0)
3498 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003499 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003500 }
3501
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003502 fnOut << "(";
3503 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3504 fnOut << fieldNameA;
3505 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3506 fnOut << fieldNameB;
3507 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3508 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003509 }
3510
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003511 fnOut << ";\n" << "}\n";
3512
3513 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003514
3515 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003516 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003517
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003518 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003519}
3520
Olli Etuaho7fb49552015-03-18 17:27:44 +02003521TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3522{
3523 for (const auto &eqFunction : mArrayEqualityFunctions)
3524 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003525 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003526 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003527 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003528 }
3529 }
3530
3531 const TString &typeName = TypeString(type);
3532
Olli Etuaho12690762015-03-31 12:55:28 +03003533 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003534 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003535
3536 TInfoSinkBase fnNameOut;
3537 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003538 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003539
3540 TType nonArrayType = type;
3541 nonArrayType.clearArrayness();
3542
3543 TInfoSinkBase fnOut;
3544
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003545 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003546 << typeName << " a[" << type.getArraySize() << "], "
3547 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003548 << "{\n"
3549 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3550 " {\n"
3551 " if (";
3552
3553 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3554 fnOut << "a[i]";
3555 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3556 fnOut << "b[i]";
3557 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3558
3559 fnOut << ") { return false; }\n"
3560 " }\n"
3561 " return true;\n"
3562 "}\n";
3563
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003564 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003565
3566 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003567 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003568
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003569 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003570}
3571
Olli Etuaho12690762015-03-31 12:55:28 +03003572TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3573{
3574 for (const auto &assignFunction : mArrayAssignmentFunctions)
3575 {
3576 if (assignFunction.type == type)
3577 {
3578 return assignFunction.functionName;
3579 }
3580 }
3581
3582 const TString &typeName = TypeString(type);
3583
3584 ArrayHelperFunction function;
3585 function.type = type;
3586
3587 TInfoSinkBase fnNameOut;
3588 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3589 function.functionName = fnNameOut.c_str();
3590
3591 TInfoSinkBase fnOut;
3592
3593 fnOut << "void " << function.functionName << "(out "
3594 << typeName << " a[" << type.getArraySize() << "], "
3595 << typeName << " b[" << type.getArraySize() << "])\n"
3596 << "{\n"
3597 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3598 " {\n"
3599 " a[i] = b[i];\n"
3600 " }\n"
3601 "}\n";
3602
3603 function.functionDefinition = fnOut.c_str();
3604
3605 mArrayAssignmentFunctions.push_back(function);
3606
3607 return function.functionName;
3608}
3609
Olli Etuaho9638c352015-04-01 14:34:52 +03003610TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3611{
3612 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3613 {
3614 if (constructIntoFunction.type == type)
3615 {
3616 return constructIntoFunction.functionName;
3617 }
3618 }
3619
3620 const TString &typeName = TypeString(type);
3621
3622 ArrayHelperFunction function;
3623 function.type = type;
3624
3625 TInfoSinkBase fnNameOut;
3626 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3627 function.functionName = fnNameOut.c_str();
3628
3629 TInfoSinkBase fnOut;
3630
3631 fnOut << "void " << function.functionName << "(out "
3632 << typeName << " a[" << type.getArraySize() << "]";
3633 for (int i = 0; i < type.getArraySize(); ++i)
3634 {
3635 fnOut << ", " << typeName << " b" << i;
3636 }
3637 fnOut << ")\n"
3638 "{\n";
3639
3640 for (int i = 0; i < type.getArraySize(); ++i)
3641 {
3642 fnOut << " a[" << i << "] = b" << i << ";\n";
3643 }
3644 fnOut << "}\n";
3645
3646 function.functionDefinition = fnOut.c_str();
3647
3648 mArrayConstructIntoFunctions.push_back(function);
3649
3650 return function.functionName;
3651}
3652
Jamie Madill2e295e22015-04-29 10:41:33 -04003653void OutputHLSL::ensureStructDefined(const TType &type)
3654{
3655 TStructure *structure = type.getStruct();
3656
3657 if (structure)
3658 {
3659 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3660 }
3661}
3662
3663
Olli Etuaho9638c352015-04-01 14:34:52 +03003664
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003665}