blob: 90aa734bb1d8035c4cf788e161295eea84c63c49 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/OutputHLSL.h"
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00008
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00009#include <algorithm>
shannon.woods@transgaming.comfff89b32013-02-28 23:20:15 +000010#include <cfloat>
11#include <stdio.h>
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +000012
Jamie Madill9e0478f2015-01-13 11:13:54 -050013#include "common/angleutils.h"
Olli Etuahod57e0db2015-04-24 15:05:08 +030014#include "common/debug.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050015#include "common/utilities.h"
Olli Etuaho8efc5ad2015-03-03 17:21:10 +020016#include "compiler/translator/BuiltInFunctionEmulator.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050017#include "compiler/translator/BuiltInFunctionEmulatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050018#include "compiler/translator/FlagStd140Structs.h"
19#include "compiler/translator/InfoSink.h"
20#include "compiler/translator/NodeSearch.h"
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +020021#include "compiler/translator/RemoveSwitchFallThrough.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050022#include "compiler/translator/SearchSymbol.h"
23#include "compiler/translator/StructureHLSL.h"
24#include "compiler/translator/TranslatorHLSL.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050025#include "compiler/translator/UniformHLSL.h"
26#include "compiler/translator/UtilsHLSL.h"
27#include "compiler/translator/blocklayout.h"
Jamie Madill9e0478f2015-01-13 11:13:54 -050028#include "compiler/translator/util.h"
29
Olli Etuaho4785fec2015-05-18 16:09:37 +030030namespace
31{
32
33bool IsSequence(TIntermNode *node)
34{
35 return node->getAsAggregate() != nullptr && node->getAsAggregate()->getOp() == EOpSequence;
36}
37
Olli Etuaho18b9deb2015-11-05 12:14:50 +020038void WriteSingleConstant(TInfoSinkBase &out, const TConstantUnion *const constUnion)
39{
40 ASSERT(constUnion != nullptr);
41 switch (constUnion->getType())
42 {
43 case EbtFloat:
44 out << std::min(FLT_MAX, std::max(-FLT_MAX, constUnion->getFConst()));
45 break;
46 case EbtInt:
47 out << constUnion->getIConst();
48 break;
49 case EbtUInt:
50 out << constUnion->getUConst();
51 break;
52 case EbtBool:
53 out << constUnion->getBConst();
54 break;
55 default:
56 UNREACHABLE();
57 }
58}
59
60const TConstantUnion *WriteConstantUnionArray(TInfoSinkBase &out,
61 const TConstantUnion *const constUnion,
62 const size_t size)
63{
64 const TConstantUnion *constUnionIterated = constUnion;
65 for (size_t i = 0; i < size; i++, constUnionIterated++)
66 {
67 WriteSingleConstant(out, constUnionIterated);
68
69 if (i != size - 1)
70 {
71 out << ", ";
72 }
73 }
74 return constUnionIterated;
75}
76
Olli Etuaho4785fec2015-05-18 16:09:37 +030077} // namespace
78
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079namespace sh
80{
daniel@transgaming.com005c7392010-04-15 20:45:27 +000081
Nicolas Capense0ba27a2013-06-24 16:10:52 -040082TString OutputHLSL::TextureFunction::name() const
83{
84 TString name = "gl_texture";
85
Olli Etuaho9b4e8622015-12-22 15:53:22 +020086 // We need to include full the sampler type in the function name to make the signature unique
87 // on D3D11, where samplers are passed to texture functions as indices.
88 name += TextureTypeSuffix(this->sampler);
Nicolas Capense0ba27a2013-06-24 16:10:52 -040089
90 if (proj)
91 {
92 name += "Proj";
93 }
94
Nicolas Capensb1f45b72013-12-19 17:37:19 -050095 if (offset)
96 {
97 name += "Offset";
98 }
99
Nicolas Capens75fb4752013-07-10 15:14:47 -0400100 switch(method)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400101 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500102 case IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400103 case BIAS: break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500104 case LOD: name += "Lod"; break;
105 case LOD0: name += "Lod0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400106 case LOD0BIAS: name += "Lod0"; break; // Extra parameter makes the signature unique
Nicolas Capensfc014542014-02-18 14:47:13 -0500107 case SIZE: name += "Size"; break;
108 case FETCH: name += "Fetch"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500109 case GRAD: name += "Grad"; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400110 default: UNREACHABLE();
111 }
112
113 return name + "(";
114}
115
116bool OutputHLSL::TextureFunction::operator<(const TextureFunction &rhs) const
117{
118 if (sampler < rhs.sampler) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400119 if (sampler > rhs.sampler) return false;
120
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400121 if (coords < rhs.coords) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400122 if (coords > rhs.coords) return false;
123
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400124 if (!proj && rhs.proj) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400125 if (proj && !rhs.proj) return false;
126
127 if (!offset && rhs.offset) return true;
128 if (offset && !rhs.offset) return false;
129
Nicolas Capens75fb4752013-07-10 15:14:47 -0400130 if (method < rhs.method) return true;
Nicolas Capens04296f82014-04-14 14:24:38 -0400131 if (method > rhs.method) return false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400132
133 return false;
134}
135
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200136OutputHLSL::OutputHLSL(sh::GLenum shaderType, int shaderVersion,
137 const TExtensionBehavior &extensionBehavior,
138 const char *sourcePath, ShShaderOutput outputType,
139 int numRenderTargets, const std::vector<Uniform> &uniforms,
140 int compileOptions)
Jamie Madill54ad4f82014-09-03 09:40:46 -0400141 : TIntermTraverser(true, true, true),
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200142 mShaderType(shaderType),
143 mShaderVersion(shaderVersion),
144 mExtensionBehavior(extensionBehavior),
145 mSourcePath(sourcePath),
146 mOutputType(outputType),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700147 mCompileOptions(compileOptions),
Sam McNally5a0edc62015-06-30 12:36:07 +1000148 mNumRenderTargets(numRenderTargets),
Corentin Wallez1239ee92015-03-19 14:38:02 -0700149 mCurrentFunctionMetadata(nullptr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150{
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +0000151 mInsideFunction = false;
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000152
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000153 mUsesFragColor = false;
154 mUsesFragData = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000155 mUsesDepthRange = false;
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000156 mUsesFragCoord = false;
157 mUsesPointCoord = false;
158 mUsesFrontFacing = false;
159 mUsesPointSize = false;
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000160 mUsesInstanceID = false;
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400161 mUsesFragDepth = false;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +0000162 mUsesXor = false;
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500163 mUsesDiscardRewriting = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400164 mUsesNestedBreak = false;
Arun Patole44efa0b2015-03-04 17:11:05 +0530165 mRequiresIEEEStrictCompiling = false;
daniel@transgaming.com005c7392010-04-15 20:45:27 +0000166
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +0000167 mUniqueIndex = 0;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000168
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000169 mOutputLod0Function = false;
daniel@transgaming.come11100c2012-05-31 01:20:32 +0000170 mInsideDiscontinuousLoop = false;
Nicolas Capens655fe362014-04-11 13:12:34 -0400171 mNestedLoopDepth = 0;
daniel@transgaming.come9b3f602012-07-11 20:37:31 +0000172
173 mExcessiveLoopIndex = NULL;
daniel@transgaming.com652468c2012-12-20 21:11:57 +0000174
Jamie Madill8daaba12014-06-13 10:04:33 -0400175 mStructureHLSL = new StructureHLSL;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200176 mUniformHLSL = new UniformHLSL(mStructureHLSL, outputType, uniforms);
Jamie Madill8daaba12014-06-13 10:04:33 -0400177
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200178 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000179 {
Arun Patole63419392015-03-13 11:51:07 +0530180 // Fragment shaders need dx_DepthRange, dx_ViewCoords and dx_DepthFront.
181 // Vertex shaders need a slightly different set: dx_DepthRange, dx_ViewCoords and dx_ViewAdjust.
182 // In both cases total 3 uniform registers need to be reserved.
183 mUniformHLSL->reserveUniformRegisters(3);
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000184 }
daniel@transgaming.coma390e1e2013-01-11 04:09:39 +0000185
Geoff Lang00140f42016-02-03 18:47:33 +0000186 // Reserve registers for the default uniform block and driver constants
187 mUniformHLSL->reserveInterfaceBlockRegisters(2);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000190OutputHLSL::~OutputHLSL()
191{
Jamie Madill8daaba12014-06-13 10:04:33 -0400192 SafeDelete(mStructureHLSL);
Jamie Madillf91ce812014-06-13 10:04:34 -0400193 SafeDelete(mUniformHLSL);
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200194 for (auto &eqFunction : mStructEqualityFunctions)
195 {
196 SafeDelete(eqFunction);
197 }
198 for (auto &eqFunction : mArrayEqualityFunctions)
199 {
200 SafeDelete(eqFunction);
201 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +0000202}
203
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200204void OutputHLSL::output(TIntermNode *treeRoot, TInfoSinkBase &objSink)
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000205{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200206 const std::vector<TIntermTyped*> &flaggedStructs = FlagStd140ValueStructs(treeRoot);
Jamie Madill570e04d2013-06-21 09:15:33 -0400207 makeFlaggedStructMaps(flaggedStructs);
daniel@transgaming.com89431aa2012-05-31 01:20:29 +0000208
Olli Etuaho8efc5ad2015-03-03 17:21:10 +0200209 BuiltInFunctionEmulator builtInFunctionEmulator;
210 InitBuiltInFunctionEmulatorForHLSL(&builtInFunctionEmulator);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200211 builtInFunctionEmulator.MarkBuiltInFunctionsForEmulation(treeRoot);
Jamie Madill32aab012015-01-27 14:12:26 -0500212
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700213 // Now that we are done changing the AST, do the analyses need for HLSL generation
Corentin Wallez1239ee92015-03-19 14:38:02 -0700214 CallDAG::InitResult success = mCallDag.init(treeRoot, &objSink);
215 ASSERT(success == CallDAG::INITDAG_SUCCESS);
Olli Etuahod57e0db2015-04-24 15:05:08 +0300216 UNUSED_ASSERTION_VARIABLE(success);
Corentin Wallez1239ee92015-03-19 14:38:02 -0700217 mASTMetadataList = CreateASTMetadataHLSL(treeRoot, mCallDag);
Corentin Wallezf4eab3b2015-03-18 12:55:45 -0700218
Jamie Madill37997142015-01-28 10:06:34 -0500219 // Output the body and footer first to determine what has to go in the header
Jamie Madill32aab012015-01-27 14:12:26 -0500220 mInfoSinkStack.push(&mBody);
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200221 treeRoot->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500222 mInfoSinkStack.pop();
223
Jamie Madill37997142015-01-28 10:06:34 -0500224 mInfoSinkStack.push(&mFooter);
225 if (!mDeferredGlobalInitializers.empty())
226 {
227 writeDeferredGlobalInitializers(mFooter);
228 }
229 mInfoSinkStack.pop();
230
Jamie Madill32aab012015-01-27 14:12:26 -0500231 mInfoSinkStack.push(&mHeader);
Jamie Madill8c46ab12015-12-07 16:39:19 -0500232 header(mHeader, &builtInFunctionEmulator);
Jamie Madill32aab012015-01-27 14:12:26 -0500233 mInfoSinkStack.pop();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000234
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200235 objSink << mHeader.c_str();
236 objSink << mBody.c_str();
237 objSink << mFooter.c_str();
Olli Etuahoe17e3192015-01-02 12:47:59 +0200238
239 builtInFunctionEmulator.Cleanup();
daniel@transgaming.com950f9932010-04-13 03:26:14 +0000240}
241
Jamie Madill570e04d2013-06-21 09:15:33 -0400242void OutputHLSL::makeFlaggedStructMaps(const std::vector<TIntermTyped *> &flaggedStructs)
243{
244 for (unsigned int structIndex = 0; structIndex < flaggedStructs.size(); structIndex++)
245 {
246 TIntermTyped *flaggedNode = flaggedStructs[structIndex];
247
Jamie Madill32aab012015-01-27 14:12:26 -0500248 TInfoSinkBase structInfoSink;
249 mInfoSinkStack.push(&structInfoSink);
250
Jamie Madill570e04d2013-06-21 09:15:33 -0400251 // This will mark the necessary block elements as referenced
252 flaggedNode->traverse(this);
Jamie Madill32aab012015-01-27 14:12:26 -0500253
254 TString structName(structInfoSink.c_str());
255 mInfoSinkStack.pop();
Jamie Madill570e04d2013-06-21 09:15:33 -0400256
257 mFlaggedStructOriginalNames[flaggedNode] = structName;
258
259 for (size_t pos = structName.find('.'); pos != std::string::npos; pos = structName.find('.'))
260 {
261 structName.erase(pos, 1);
262 }
263
264 mFlaggedStructMappedNames[flaggedNode] = "map" + structName;
265 }
266}
267
Jamie Madill4e1fd412014-07-10 17:50:10 -0400268const std::map<std::string, unsigned int> &OutputHLSL::getInterfaceBlockRegisterMap() const
269{
270 return mUniformHLSL->getInterfaceBlockRegisterMap();
271}
272
Jamie Madill9fe25e92014-07-18 10:33:08 -0400273const std::map<std::string, unsigned int> &OutputHLSL::getUniformRegisterMap() const
274{
275 return mUniformHLSL->getUniformRegisterMap();
276}
277
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000278int OutputHLSL::vectorSize(const TType &type) const
279{
shannonwoods@chromium.org9bd22fa2013-05-30 00:18:47 +0000280 int elementSize = type.isMatrix() ? type.getCols() : 1;
daniel@transgaming.com0b6b8342010-04-26 15:33:45 +0000281 int arraySize = type.isArray() ? type.getArraySize() : 1;
282
283 return elementSize * arraySize;
284}
285
Jamie Madill98493dd2013-07-08 14:39:03 -0400286TString OutputHLSL::structInitializerString(int indent, const TStructure &structure, const TString &rhsStructName)
Jamie Madill570e04d2013-06-21 09:15:33 -0400287{
288 TString init;
289
290 TString preIndentString;
291 TString fullIndentString;
292
293 for (int spaces = 0; spaces < (indent * 4); spaces++)
294 {
295 preIndentString += ' ';
296 }
297
298 for (int spaces = 0; spaces < ((indent+1) * 4); spaces++)
299 {
300 fullIndentString += ' ';
301 }
302
303 init += preIndentString + "{\n";
304
Jamie Madill98493dd2013-07-08 14:39:03 -0400305 const TFieldList &fields = structure.fields();
306 for (unsigned int fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400307 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400308 const TField &field = *fields[fieldIndex];
Jamie Madill033dae62014-06-18 12:56:28 -0400309 const TString &fieldName = rhsStructName + "." + Decorate(field.name());
Jamie Madill98493dd2013-07-08 14:39:03 -0400310 const TType &fieldType = *field.type();
Jamie Madill570e04d2013-06-21 09:15:33 -0400311
Jamie Madill98493dd2013-07-08 14:39:03 -0400312 if (fieldType.getStruct())
Jamie Madill570e04d2013-06-21 09:15:33 -0400313 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400314 init += structInitializerString(indent + 1, *fieldType.getStruct(), fieldName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400315 }
316 else
317 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400318 init += fullIndentString + fieldName + ",\n";
Jamie Madill570e04d2013-06-21 09:15:33 -0400319 }
320 }
321
322 init += preIndentString + "}" + (indent == 0 ? ";" : ",") + "\n";
323
324 return init;
325}
326
Jamie Madill8c46ab12015-12-07 16:39:19 -0500327void OutputHLSL::header(TInfoSinkBase &out, const BuiltInFunctionEmulator *builtInFunctionEmulator)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328{
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000329 TString varyings;
330 TString attributes;
Jamie Madill570e04d2013-06-21 09:15:33 -0400331 TString flaggedStructs;
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000332
Jamie Madill829f59e2013-11-13 19:40:54 -0500333 for (std::map<TIntermTyped*, TString>::const_iterator flaggedStructIt = mFlaggedStructMappedNames.begin(); flaggedStructIt != mFlaggedStructMappedNames.end(); flaggedStructIt++)
Jamie Madill570e04d2013-06-21 09:15:33 -0400334 {
335 TIntermTyped *structNode = flaggedStructIt->first;
336 const TString &mappedName = flaggedStructIt->second;
Jamie Madill98493dd2013-07-08 14:39:03 -0400337 const TStructure &structure = *structNode->getType().getStruct();
Jamie Madill570e04d2013-06-21 09:15:33 -0400338 const TString &originalName = mFlaggedStructOriginalNames[structNode];
339
Jamie Madill033dae62014-06-18 12:56:28 -0400340 flaggedStructs += "static " + Decorate(structure.name()) + " " + mappedName + " =\n";
Jamie Madill98493dd2013-07-08 14:39:03 -0400341 flaggedStructs += structInitializerString(0, structure, originalName);
Jamie Madill570e04d2013-06-21 09:15:33 -0400342 flaggedStructs += "\n";
343 }
344
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000345 for (ReferencedSymbols::const_iterator varying = mReferencedVaryings.begin(); varying != mReferencedVaryings.end(); varying++)
346 {
347 const TType &type = varying->second->getType();
348 const TString &name = varying->second->getSymbol();
349
350 // Program linking depends on this exact format
Jamie Madill033dae62014-06-18 12:56:28 -0400351 varyings += "static " + InterpolationString(type.getQualifier()) + " " + TypeString(type) + " " +
352 Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000353 }
354
355 for (ReferencedSymbols::const_iterator attribute = mReferencedAttributes.begin(); attribute != mReferencedAttributes.end(); attribute++)
356 {
357 const TType &type = attribute->second->getType();
358 const TString &name = attribute->second->getSymbol();
359
Jamie Madill033dae62014-06-18 12:56:28 -0400360 attributes += "static " + TypeString(type) + " " + Decorate(name) + ArrayString(type) + " = " + initializer(type) + ";\n";
daniel@transgaming.com8803b852012-12-20 21:11:47 +0000361 }
362
Jamie Madill8daaba12014-06-13 10:04:33 -0400363 out << mStructureHLSL->structsHeader();
Jamie Madill529077d2013-06-20 11:55:54 -0400364
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200365 mUniformHLSL->uniformsHeader(out, mOutputType, mReferencedUniforms);
Jamie Madillf91ce812014-06-13 10:04:34 -0400366 out << mUniformHLSL->interfaceBlocksHeader(mReferencedInterfaceBlocks);
367
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200368 if (!mEqualityFunctions.empty())
Jamie Madill55e79e02015-02-09 15:35:00 -0500369 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200370 out << "\n// Equality functions\n\n";
371 for (const auto &eqFunction : mEqualityFunctions)
Jamie Madill55e79e02015-02-09 15:35:00 -0500372 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +0200373 out << eqFunction->functionDefinition << "\n";
Olli Etuaho7fb49552015-03-18 17:27:44 +0200374 }
375 }
Olli Etuaho12690762015-03-31 12:55:28 +0300376 if (!mArrayAssignmentFunctions.empty())
377 {
378 out << "\n// Assignment functions\n\n";
379 for (const auto &assignmentFunction : mArrayAssignmentFunctions)
380 {
381 out << assignmentFunction.functionDefinition << "\n";
382 }
383 }
Olli Etuaho9638c352015-04-01 14:34:52 +0300384 if (!mArrayConstructIntoFunctions.empty())
385 {
386 out << "\n// Array constructor functions\n\n";
387 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
388 {
389 out << constructIntoFunction.functionDefinition << "\n";
390 }
391 }
Olli Etuaho7fb49552015-03-18 17:27:44 +0200392
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500393 if (mUsesDiscardRewriting)
394 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400395 out << "#define ANGLE_USES_DISCARD_REWRITING\n";
Jamie Madill3c9eeb92013-11-04 11:09:26 -0500396 }
397
Nicolas Capens655fe362014-04-11 13:12:34 -0400398 if (mUsesNestedBreak)
399 {
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400400 out << "#define ANGLE_USES_NESTED_BREAK\n";
Nicolas Capens655fe362014-04-11 13:12:34 -0400401 }
402
Arun Patole44efa0b2015-03-04 17:11:05 +0530403 if (mRequiresIEEEStrictCompiling)
404 {
405 out << "#define ANGLE_REQUIRES_IEEE_STRICT_COMPILING\n";
406 }
407
Nicolas Capens5e0c80a2014-10-10 10:11:54 -0400408 out << "#ifdef ANGLE_ENABLE_LOOP_FLATTEN\n"
409 "#define LOOP [loop]\n"
410 "#define FLATTEN [flatten]\n"
411 "#else\n"
412 "#define LOOP\n"
413 "#define FLATTEN\n"
414 "#endif\n";
415
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200416 if (mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200418 TExtensionBehavior::const_iterator iter = mExtensionBehavior.find("GL_EXT_draw_buffers");
419 const bool usingMRTExtension = (iter != mExtensionBehavior.end() && (iter->second == EBhEnable || iter->second == EBhRequire));
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000420
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000421 out << "// Varyings\n";
422 out << varyings;
Jamie Madill46131a32013-06-20 11:55:50 -0400423 out << "\n";
424
Olli Etuahoa3a5cc62015-02-13 13:12:22 +0200425 if (mShaderVersion >= 300)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000426 {
Jamie Madill829f59e2013-11-13 19:40:54 -0500427 for (ReferencedSymbols::const_iterator outputVariableIt = mReferencedOutputVariables.begin(); outputVariableIt != mReferencedOutputVariables.end(); outputVariableIt++)
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000428 {
Jamie Madill46131a32013-06-20 11:55:50 -0400429 const TString &variableName = outputVariableIt->first;
430 const TType &variableType = outputVariableIt->second->getType();
Jamie Madill46131a32013-06-20 11:55:50 -0400431
Jamie Madill033dae62014-06-18 12:56:28 -0400432 out << "static " + TypeString(variableType) + " out_" + variableName + ArrayString(variableType) +
Jamie Madill46131a32013-06-20 11:55:50 -0400433 " = " + initializer(variableType) + ";\n";
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000434 }
shannon.woods%transgaming.com@gtempaccount.coma28864c2013-04-13 03:32:03 +0000435 }
Jamie Madill46131a32013-06-20 11:55:50 -0400436 else
437 {
438 const unsigned int numColorValues = usingMRTExtension ? mNumRenderTargets : 1;
439
440 out << "static float4 gl_Color[" << numColorValues << "] =\n"
441 "{\n";
442 for (unsigned int i = 0; i < numColorValues; i++)
443 {
444 out << " float4(0, 0, 0, 0)";
445 if (i + 1 != numColorValues)
446 {
447 out << ",";
448 }
449 out << "\n";
450 }
451
452 out << "};\n";
453 }
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000454
Jamie Madill2aeb26a2013-07-08 14:02:55 -0400455 if (mUsesFragDepth)
456 {
457 out << "static float gl_Depth = 0.0;\n";
458 }
459
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000460 if (mUsesFragCoord)
461 {
462 out << "static float4 gl_FragCoord = float4(0, 0, 0, 0);\n";
463 }
464
465 if (mUsesPointCoord)
466 {
467 out << "static float2 gl_PointCoord = float2(0.5, 0.5);\n";
468 }
469
470 if (mUsesFrontFacing)
471 {
472 out << "static bool gl_FrontFacing = false;\n";
473 }
474
475 out << "\n";
476
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000477 if (mUsesDepthRange)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000478 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000479 out << "struct gl_DepthRangeParameters\n"
480 "{\n"
481 " float near;\n"
482 " float far;\n"
483 " float diff;\n"
484 "};\n"
485 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000486 }
487
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200488 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000489 {
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000490 out << "cbuffer DriverConstants : register(b1)\n"
491 "{\n";
492
493 if (mUsesDepthRange)
494 {
495 out << " float3 dx_DepthRange : packoffset(c0);\n";
496 }
497
498 if (mUsesFragCoord)
499 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000500 out << " float4 dx_ViewCoords : packoffset(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000501 }
502
503 if (mUsesFragCoord || mUsesFrontFacing)
504 {
505 out << " float3 dx_DepthFront : packoffset(c2);\n";
506 }
507
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800508 if (mUsesFragCoord)
509 {
510 // dx_ViewScale is only used in the fragment shader to correct
511 // the value for glFragCoord if necessary
512 out << " float2 dx_ViewScale : packoffset(c3);\n";
513 }
514
Olli Etuaho618bebc2016-01-15 16:40:00 +0200515 if (mOutputType == SH_HLSL_4_1_OUTPUT)
516 {
517 mUniformHLSL->samplerMetadataUniforms(out, "c4");
518 }
519
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000520 out << "};\n";
521 }
522 else
523 {
524 if (mUsesDepthRange)
525 {
526 out << "uniform float3 dx_DepthRange : register(c0);";
527 }
528
529 if (mUsesFragCoord)
530 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000531 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000532 }
533
534 if (mUsesFragCoord || mUsesFrontFacing)
535 {
536 out << "uniform float3 dx_DepthFront : register(c2);\n";
537 }
538 }
539
540 out << "\n";
541
542 if (mUsesDepthRange)
543 {
544 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
545 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000546 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000547
Jamie Madillf91ce812014-06-13 10:04:34 -0400548 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000549 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400550 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000551 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400552 out << flaggedStructs;
553 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000554 }
555
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000556 if (usingMRTExtension && mNumRenderTargets > 1)
557 {
558 out << "#define GL_USES_MRT\n";
559 }
560
561 if (mUsesFragColor)
562 {
563 out << "#define GL_USES_FRAG_COLOR\n";
564 }
565
566 if (mUsesFragData)
567 {
568 out << "#define GL_USES_FRAG_DATA\n";
569 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000571 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000573 out << "// Attributes\n";
574 out << attributes;
575 out << "\n"
576 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400577
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000578 if (mUsesPointSize)
579 {
580 out << "static float gl_PointSize = float(1);\n";
581 }
582
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000583 if (mUsesInstanceID)
584 {
585 out << "static int gl_InstanceID;";
586 }
587
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000588 out << "\n"
589 "// Varyings\n";
590 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000591 out << "\n";
592
593 if (mUsesDepthRange)
594 {
595 out << "struct gl_DepthRangeParameters\n"
596 "{\n"
597 " float near;\n"
598 " float far;\n"
599 " float diff;\n"
600 "};\n"
601 "\n";
602 }
603
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200604 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000605 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800606 out << "cbuffer DriverConstants : register(b1)\n"
607 "{\n";
608
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000609 if (mUsesDepthRange)
610 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800611 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000612 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800613
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800614 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9
615 // shaders. However, we declare it for all shaders (including Feature Level 10+).
616 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it
617 // if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800618 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800619 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross2a63b3f2016-02-08 12:29:08 -0800620 out << " float2 dx_ViewScale : packoffset(c3);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800621
Olli Etuaho618bebc2016-01-15 16:40:00 +0200622 if (mOutputType == SH_HLSL_4_1_OUTPUT)
623 {
624 mUniformHLSL->samplerMetadataUniforms(out, "c4");
625 }
626
Austin Kinross4fd18b12014-12-22 12:32:05 -0800627 out << "};\n"
628 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000629 }
630 else
631 {
632 if (mUsesDepthRange)
633 {
634 out << "uniform float3 dx_DepthRange : register(c0);\n";
635 }
636
Cooper Partine6664f02015-01-09 16:22:24 -0800637 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
638 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000639 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000640 }
641
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000642 if (mUsesDepthRange)
643 {
644 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
645 "\n";
646 }
647
Jamie Madillf91ce812014-06-13 10:04:34 -0400648 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000649 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400650 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000651 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400652 out << flaggedStructs;
653 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000654 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400655 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000656
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400657 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
658 {
659 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400660 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000661 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400662 switch(textureFunction->sampler)
663 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400664 case EbtSampler2D: out << "int2 "; break;
665 case EbtSampler3D: out << "int3 "; break;
666 case EbtSamplerCube: out << "int2 "; break;
667 case EbtSampler2DArray: out << "int3 "; break;
668 case EbtISampler2D: out << "int2 "; break;
669 case EbtISampler3D: out << "int3 "; break;
670 case EbtISamplerCube: out << "int2 "; break;
671 case EbtISampler2DArray: out << "int3 "; break;
672 case EbtUSampler2D: out << "int2 "; break;
673 case EbtUSampler3D: out << "int3 "; break;
674 case EbtUSamplerCube: out << "int2 "; break;
675 case EbtUSampler2DArray: out << "int3 "; break;
676 case EbtSampler2DShadow: out << "int2 "; break;
677 case EbtSamplerCubeShadow: out << "int2 "; break;
678 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400679 default: UNREACHABLE();
680 }
681 }
682 else // Sampling function
683 {
684 switch(textureFunction->sampler)
685 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400686 case EbtSampler2D: out << "float4 "; break;
687 case EbtSampler3D: out << "float4 "; break;
688 case EbtSamplerCube: out << "float4 "; break;
689 case EbtSampler2DArray: out << "float4 "; break;
690 case EbtISampler2D: out << "int4 "; break;
691 case EbtISampler3D: out << "int4 "; break;
692 case EbtISamplerCube: out << "int4 "; break;
693 case EbtISampler2DArray: out << "int4 "; break;
694 case EbtUSampler2D: out << "uint4 "; break;
695 case EbtUSampler3D: out << "uint4 "; break;
696 case EbtUSamplerCube: out << "uint4 "; break;
697 case EbtUSampler2DArray: out << "uint4 "; break;
698 case EbtSampler2DShadow: out << "float "; break;
699 case EbtSamplerCubeShadow: out << "float "; break;
700 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400701 default: UNREACHABLE();
702 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000703 }
704
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400705 // Function name
706 out << textureFunction->name();
707
708 // Argument list
709 int hlslCoords = 4;
710
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200711 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000712 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400713 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000714 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400715 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
716 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
717 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000718 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400719
Nicolas Capens75fb4752013-07-10 15:14:47 -0400720 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000721 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400722 case TextureFunction::IMPLICIT: break;
723 case TextureFunction::BIAS: hlslCoords = 4; break;
724 case TextureFunction::LOD: hlslCoords = 4; break;
725 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400726 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400727 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000728 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400729 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200730 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400731 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200732 hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler);
733 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400734 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200735 out << TextureString(textureFunction->sampler) << " x, "
736 << SamplerString(textureFunction->sampler) << " s";
737 }
738 else
739 {
740 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
741 out << "const uint samplerIndex";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400742 }
743 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400744
Nicolas Capensfc014542014-02-18 14:47:13 -0500745 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400746 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500747 switch(textureFunction->coords)
748 {
749 case 2: out << ", int2 t"; break;
750 case 3: out << ", int3 t"; break;
751 default: UNREACHABLE();
752 }
753 }
754 else // Floating-point coordinates (except textureSize)
755 {
756 switch(textureFunction->coords)
757 {
758 case 1: out << ", int lod"; break; // textureSize()
759 case 2: out << ", float2 t"; break;
760 case 3: out << ", float3 t"; break;
761 case 4: out << ", float4 t"; break;
762 default: UNREACHABLE();
763 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000764 }
765
Nicolas Capensd11d5492014-02-19 17:06:10 -0500766 if (textureFunction->method == TextureFunction::GRAD)
767 {
768 switch(textureFunction->sampler)
769 {
770 case EbtSampler2D:
771 case EbtISampler2D:
772 case EbtUSampler2D:
773 case EbtSampler2DArray:
774 case EbtISampler2DArray:
775 case EbtUSampler2DArray:
776 case EbtSampler2DShadow:
777 case EbtSampler2DArrayShadow:
778 out << ", float2 ddx, float2 ddy";
779 break;
780 case EbtSampler3D:
781 case EbtISampler3D:
782 case EbtUSampler3D:
783 case EbtSamplerCube:
784 case EbtISamplerCube:
785 case EbtUSamplerCube:
786 case EbtSamplerCubeShadow:
787 out << ", float3 ddx, float3 ddy";
788 break;
789 default: UNREACHABLE();
790 }
791 }
792
Nicolas Capens75fb4752013-07-10 15:14:47 -0400793 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000794 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400795 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400796 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400797 case TextureFunction::LOD: out << ", float lod"; break;
798 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400799 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400800 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500801 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500802 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400803 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000804 }
805
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500806 if (textureFunction->offset)
807 {
808 switch(textureFunction->sampler)
809 {
810 case EbtSampler2D: out << ", int2 offset"; break;
811 case EbtSampler3D: out << ", int3 offset"; break;
812 case EbtSampler2DArray: out << ", int2 offset"; break;
813 case EbtISampler2D: out << ", int2 offset"; break;
814 case EbtISampler3D: out << ", int3 offset"; break;
815 case EbtISampler2DArray: out << ", int2 offset"; break;
816 case EbtUSampler2D: out << ", int2 offset"; break;
817 case EbtUSampler3D: out << ", int3 offset"; break;
818 case EbtUSampler2DArray: out << ", int2 offset"; break;
819 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500820 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500821 default: UNREACHABLE();
822 }
823 }
824
Nicolas Capens84cfa122014-04-14 13:48:45 -0400825 if (textureFunction->method == TextureFunction::BIAS ||
826 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500827 {
828 out << ", float bias";
829 }
830
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400831 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400832 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400833
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200834 // In some cases we use a variable to store the texture/sampler objects, but to work around
835 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
836 // sampling we need to call the function directly on a reference to the array. The bug was
837 // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests.
838 TString textureReference("x");
839 TString samplerReference("s");
840 if (mOutputType == SH_HLSL_4_1_OUTPUT)
841 {
842 TString suffix = TextureGroupSuffix(textureFunction->sampler);
843 if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D)
844 {
845 textureReference = TString("textures") + suffix + "[samplerIndex]";
846 samplerReference = TString("samplers") + suffix + "[samplerIndex]";
847 }
848 else
849 {
850 out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix
851 << ";\n";
852 textureReference = TString("textures") + suffix + "[textureIndex]";
853 out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset"
854 << suffix << ";\n";
855 samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]";
856 }
857 }
858
Nicolas Capens75fb4752013-07-10 15:14:47 -0400859 if (textureFunction->method == TextureFunction::SIZE)
860 {
861 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
862 {
863 if (IsSamplerArray(textureFunction->sampler))
864 {
865 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200866 << " " << textureReference
867 << ".GetDimensions(lod, width, height, layers, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400868 }
869 else
870 {
871 out << " uint width; uint height; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200872 << " " << textureReference
873 << ".GetDimensions(lod, width, height, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400874 }
875 }
876 else if (IsSampler3D(textureFunction->sampler))
877 {
878 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200879 << " " << textureReference
880 << ".GetDimensions(lod, width, height, depth, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400881 }
882 else UNREACHABLE();
883
884 switch(textureFunction->sampler)
885 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400886 case EbtSampler2D: out << " return int2(width, height);"; break;
887 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
888 case EbtSamplerCube: out << " return int2(width, height);"; break;
889 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
890 case EbtISampler2D: out << " return int2(width, height);"; break;
891 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
892 case EbtISamplerCube: out << " return int2(width, height);"; break;
893 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
894 case EbtUSampler2D: out << " return int2(width, height);"; break;
895 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
896 case EbtUSamplerCube: out << " return int2(width, height);"; break;
897 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
898 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
899 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
900 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400901 default: UNREACHABLE();
902 }
903 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400904 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400905 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500906 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
907 {
908 out << " float width; float height; float layers; float levels;\n";
909
910 out << " uint mip = 0;\n";
911
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200912 out << " " << textureReference
913 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500914
915 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
916 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
917 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
918 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
919
920 // FACE_POSITIVE_X = 000b
921 // FACE_NEGATIVE_X = 001b
922 // FACE_POSITIVE_Y = 010b
923 // FACE_NEGATIVE_Y = 011b
924 // FACE_POSITIVE_Z = 100b
925 // FACE_NEGATIVE_Z = 101b
926 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
927
928 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
929 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
930 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
931
932 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
933 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500934
935 // Mip level computation.
936 if (textureFunction->method == TextureFunction::IMPLICIT)
937 {
938 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
939 " float2 dx = ddx(tSized);\n"
940 " float2 dy = ddy(tSized);\n"
941 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"
942 " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200943 << " " << textureReference
944 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500945 }
Nicolas Capens0027fa92014-02-20 14:26:42 -0500946 }
947 else if (IsIntegerSampler(textureFunction->sampler) &&
948 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400949 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400950 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400951 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400952 if (IsSamplerArray(textureFunction->sampler))
953 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400954 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400955
Nicolas Capens9edebd62013-08-06 10:59:10 -0400956 if (textureFunction->method == TextureFunction::LOD0)
957 {
958 out << " uint mip = 0;\n";
959 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400960 else if (textureFunction->method == TextureFunction::LOD0BIAS)
961 {
962 out << " uint mip = bias;\n";
963 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400964 else
965 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200966
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200967 out << " " << textureReference
968 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400969 if (textureFunction->method == TextureFunction::IMPLICIT ||
970 textureFunction->method == TextureFunction::BIAS)
971 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200972 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400973 " float dx = length(ddx(tSized));\n"
974 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500975 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400976
977 if (textureFunction->method == TextureFunction::BIAS)
978 {
979 out << " lod += bias;\n";
980 }
981 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500982 else if (textureFunction->method == TextureFunction::GRAD)
983 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200984 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500985 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400986
987 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
988 }
989
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200990 out << " " << textureReference
991 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400992 }
993 else
994 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400995 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400996
Nicolas Capens9edebd62013-08-06 10:59:10 -0400997 if (textureFunction->method == TextureFunction::LOD0)
998 {
999 out << " uint mip = 0;\n";
1000 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001001 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1002 {
1003 out << " uint mip = bias;\n";
1004 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001005 else
1006 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001007 out << " " << textureReference
1008 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001009
Nicolas Capens9edebd62013-08-06 10:59:10 -04001010 if (textureFunction->method == TextureFunction::IMPLICIT ||
1011 textureFunction->method == TextureFunction::BIAS)
1012 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001013 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001014 " float dx = length(ddx(tSized));\n"
1015 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001016 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001017
1018 if (textureFunction->method == TextureFunction::BIAS)
1019 {
1020 out << " lod += bias;\n";
1021 }
1022 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001023 else if (textureFunction->method == TextureFunction::GRAD)
1024 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001025 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001026 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001027
1028 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1029 }
1030
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001031 out << " " << textureReference
1032 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001033 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001034 }
1035 else if (IsSampler3D(textureFunction->sampler))
1036 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001037 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001038
Nicolas Capens9edebd62013-08-06 10:59:10 -04001039 if (textureFunction->method == TextureFunction::LOD0)
1040 {
1041 out << " uint mip = 0;\n";
1042 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001043 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1044 {
1045 out << " uint mip = bias;\n";
1046 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001047 else
1048 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001049 out << " " << textureReference
1050 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001051
Nicolas Capens9edebd62013-08-06 10:59:10 -04001052 if (textureFunction->method == TextureFunction::IMPLICIT ||
1053 textureFunction->method == TextureFunction::BIAS)
1054 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001055 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1056 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001057 " float dx = length(ddx(tSized));\n"
1058 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001059 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001060
1061 if (textureFunction->method == TextureFunction::BIAS)
1062 {
1063 out << " lod += bias;\n";
1064 }
1065 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001066 else if (textureFunction->method == TextureFunction::GRAD)
1067 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001068 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001069 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001070
1071 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1072 }
1073
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001074 out << " " << textureReference
1075 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001076 }
1077 else UNREACHABLE();
1078 }
1079
1080 out << " return ";
1081
1082 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001083 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001084 {
1085 switch(textureFunction->sampler)
1086 {
1087 case EbtSampler2D: out << "tex2D"; break;
1088 case EbtSamplerCube: out << "texCUBE"; break;
1089 default: UNREACHABLE();
1090 }
1091
Nicolas Capens75fb4752013-07-10 15:14:47 -04001092 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001093 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001094 case TextureFunction::IMPLICIT:
1095 out << "(" << samplerReference << ", ";
1096 break;
1097 case TextureFunction::BIAS:
1098 out << "bias(" << samplerReference << ", ";
1099 break;
1100 case TextureFunction::LOD:
1101 out << "lod(" << samplerReference << ", ";
1102 break;
1103 case TextureFunction::LOD0:
1104 out << "lod(" << samplerReference << ", ";
1105 break;
1106 case TextureFunction::LOD0BIAS:
1107 out << "lod(" << samplerReference << ", ";
1108 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001109 default: UNREACHABLE();
1110 }
1111 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001112 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001113 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001114 if (textureFunction->method == TextureFunction::GRAD)
1115 {
1116 if (IsIntegerSampler(textureFunction->sampler))
1117 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001118 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001119 }
1120 else if (IsShadowSampler(textureFunction->sampler))
1121 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001122 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1123 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001124 }
1125 else
1126 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001127 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001128 }
1129 }
1130 else if (IsIntegerSampler(textureFunction->sampler) ||
1131 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001132 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001133 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001134 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001135 else if (IsShadowSampler(textureFunction->sampler))
1136 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001137 switch(textureFunction->method)
1138 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001139 case TextureFunction::IMPLICIT:
1140 out << "" << textureReference << ".SampleCmp(" << samplerReference
1141 << ", ";
1142 break;
1143 case TextureFunction::BIAS:
1144 out << "" << textureReference << ".SampleCmp(" << samplerReference
1145 << ", ";
1146 break;
1147 case TextureFunction::LOD:
1148 out << "" << textureReference << ".SampleCmp(" << samplerReference
1149 << ", ";
1150 break;
1151 case TextureFunction::LOD0:
1152 out << "" << textureReference << ".SampleCmpLevelZero("
1153 << samplerReference << ", ";
1154 break;
1155 case TextureFunction::LOD0BIAS:
1156 out << "" << textureReference << ".SampleCmpLevelZero("
1157 << samplerReference << ", ";
1158 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001159 default: UNREACHABLE();
1160 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001161 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001162 else
1163 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001164 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001165 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001166 case TextureFunction::IMPLICIT:
1167 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1168 break;
1169 case TextureFunction::BIAS:
1170 out << "" << textureReference << ".SampleBias(" << samplerReference
1171 << ", ";
1172 break;
1173 case TextureFunction::LOD:
1174 out << "" << textureReference << ".SampleLevel(" << samplerReference
1175 << ", ";
1176 break;
1177 case TextureFunction::LOD0:
1178 out << "" << textureReference << ".SampleLevel(" << samplerReference
1179 << ", ";
1180 break;
1181 case TextureFunction::LOD0BIAS:
1182 out << "" << textureReference << ".SampleLevel(" << samplerReference
1183 << ", ";
1184 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001185 default: UNREACHABLE();
1186 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001187 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001188 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001189 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001190
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001191 // Integer sampling requires integer addresses
1192 TString addressx = "";
1193 TString addressy = "";
1194 TString addressz = "";
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001195 TString close = "";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001196
Nicolas Capensfc014542014-02-18 14:47:13 -05001197 if (IsIntegerSampler(textureFunction->sampler) ||
1198 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001199 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001200 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001201 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001202 case 2: out << "int3("; break;
1203 case 3: out << "int4("; break;
1204 default: UNREACHABLE();
1205 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001206
Nicolas Capensfc014542014-02-18 14:47:13 -05001207 // Convert from normalized floating-point to integer
1208 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001209 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001210 addressx = "int(floor(width * frac((";
1211 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001212
Nicolas Capensfc014542014-02-18 14:47:13 -05001213 if (IsSamplerArray(textureFunction->sampler))
1214 {
1215 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1216 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001217 else if (IsSamplerCube(textureFunction->sampler))
Nicolas Capens0027fa92014-02-20 14:26:42 -05001218 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001219 addressz = "((((";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001220 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001221 else
1222 {
1223 addressz = "int(floor(depth * frac((";
1224 }
1225
1226 close = "))))";
Nicolas Capensfc014542014-02-18 14:47:13 -05001227 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001228 }
1229 else
1230 {
1231 switch(hlslCoords)
1232 {
1233 case 2: out << "float2("; break;
1234 case 3: out << "float3("; break;
1235 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001236 default: UNREACHABLE();
1237 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001238 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001239
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001240 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001241
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001242 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001243 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001244 switch(textureFunction->coords)
1245 {
1246 case 3: proj = " / t.z"; break;
1247 case 4: proj = " / t.w"; break;
1248 default: UNREACHABLE();
1249 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001250 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001251
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001252 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001253
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001254 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001255 {
1256 if (hlslCoords >= 3)
1257 {
1258 if (textureFunction->coords < 3)
1259 {
1260 out << ", 0";
1261 }
1262 else
1263 {
1264 out << ", t.z" + proj;
1265 }
1266 }
1267
1268 if (hlslCoords == 4)
1269 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001270 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001271 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001272 case TextureFunction::BIAS: out << ", bias"; break;
1273 case TextureFunction::LOD: out << ", lod"; break;
1274 case TextureFunction::LOD0: out << ", 0"; break;
1275 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001276 default: UNREACHABLE();
1277 }
1278 }
1279
1280 out << "));\n";
1281 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001282 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001283 {
1284 if (hlslCoords >= 3)
1285 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001286 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1287 {
1288 out << ", face";
1289 }
1290 else
1291 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001292 out << ", " + addressz + ("t.z" + proj) + close;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001293 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001294 }
1295
Nicolas Capensd11d5492014-02-19 17:06:10 -05001296 if (textureFunction->method == TextureFunction::GRAD)
1297 {
1298 if (IsIntegerSampler(textureFunction->sampler))
1299 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001300 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001301 }
1302 else if (IsShadowSampler(textureFunction->sampler))
1303 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001304 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001305 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001306 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001307 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1308 // The resulting third component of P' in the shadow forms is used as Dref
1309 out << "), t.z" << proj;
1310 }
1311 else
1312 {
1313 switch(textureFunction->coords)
1314 {
1315 case 3: out << "), t.z"; break;
1316 case 4: out << "), t.w"; break;
1317 default: UNREACHABLE();
1318 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001319 }
1320 }
1321 else
1322 {
1323 out << "), ddx, ddy";
1324 }
1325 }
1326 else if (IsIntegerSampler(textureFunction->sampler) ||
1327 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001328 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001329 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001330 }
1331 else if (IsShadowSampler(textureFunction->sampler))
1332 {
1333 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001334 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001335 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001336 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1337 // The resulting third component of P' in the shadow forms is used as Dref
1338 out << "), t.z" << proj;
1339 }
1340 else
1341 {
1342 switch(textureFunction->coords)
1343 {
1344 case 3: out << "), t.z"; break;
1345 case 4: out << "), t.w"; break;
1346 default: UNREACHABLE();
1347 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001348 }
1349 }
1350 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001351 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001352 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001353 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001354 case TextureFunction::IMPLICIT: out << ")"; break;
1355 case TextureFunction::BIAS: out << "), bias"; break;
1356 case TextureFunction::LOD: out << "), lod"; break;
1357 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001358 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001359 default: UNREACHABLE();
1360 }
1361 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001362
1363 if (textureFunction->offset)
1364 {
1365 out << ", offset";
1366 }
1367
1368 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001369 }
1370 else UNREACHABLE();
1371 }
1372
1373 out << "\n"
1374 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001375 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376 }
1377
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001378 if (mUsesFragCoord)
1379 {
1380 out << "#define GL_USES_FRAG_COORD\n";
1381 }
1382
1383 if (mUsesPointCoord)
1384 {
1385 out << "#define GL_USES_POINT_COORD\n";
1386 }
1387
1388 if (mUsesFrontFacing)
1389 {
1390 out << "#define GL_USES_FRONT_FACING\n";
1391 }
1392
1393 if (mUsesPointSize)
1394 {
1395 out << "#define GL_USES_POINT_SIZE\n";
1396 }
1397
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001398 if (mUsesFragDepth)
1399 {
1400 out << "#define GL_USES_FRAG_DEPTH\n";
1401 }
1402
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001403 if (mUsesDepthRange)
1404 {
1405 out << "#define GL_USES_DEPTH_RANGE\n";
1406 }
1407
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001408 if (mUsesXor)
1409 {
1410 out << "bool xor(bool p, bool q)\n"
1411 "{\n"
1412 " return (p || q) && !(p && q);\n"
1413 "}\n"
1414 "\n";
1415 }
1416
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001417 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001418}
1419
1420void OutputHLSL::visitSymbol(TIntermSymbol *node)
1421{
Jamie Madill32aab012015-01-27 14:12:26 -05001422 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001423
Jamie Madill570e04d2013-06-21 09:15:33 -04001424 // Handle accessing std140 structs by value
1425 if (mFlaggedStructMappedNames.count(node) > 0)
1426 {
1427 out << mFlaggedStructMappedNames[node];
1428 return;
1429 }
1430
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001431 TString name = node->getSymbol();
1432
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001433 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001434 {
1435 mUsesDepthRange = true;
1436 out << name;
1437 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001438 else
1439 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001440 TQualifier qualifier = node->getQualifier();
1441
1442 if (qualifier == EvqUniform)
1443 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001444 const TType &nodeType = node->getType();
1445 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001446
1447 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001448 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001449 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001450 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001451 else
1452 {
1453 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001454 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001455
Jamie Madill2e295e22015-04-29 10:41:33 -04001456 ensureStructDefined(nodeType);
1457
Jamie Madill033dae62014-06-18 12:56:28 -04001458 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001459 }
Jamie Madill19571812013-08-12 15:26:34 -07001460 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001461 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001462 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001463 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001464 }
Jamie Madill033dae62014-06-18 12:56:28 -04001465 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001466 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001467 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001468 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001469 }
Jamie Madill19571812013-08-12 15:26:34 -07001470 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001471 {
1472 mReferencedOutputVariables[name] = node;
1473 out << "out_" << name;
1474 }
1475 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001476 {
1477 out << "gl_Color[0]";
1478 mUsesFragColor = true;
1479 }
1480 else if (qualifier == EvqFragData)
1481 {
1482 out << "gl_Color";
1483 mUsesFragData = true;
1484 }
1485 else if (qualifier == EvqFragCoord)
1486 {
1487 mUsesFragCoord = true;
1488 out << name;
1489 }
1490 else if (qualifier == EvqPointCoord)
1491 {
1492 mUsesPointCoord = true;
1493 out << name;
1494 }
1495 else if (qualifier == EvqFrontFacing)
1496 {
1497 mUsesFrontFacing = true;
1498 out << name;
1499 }
1500 else if (qualifier == EvqPointSize)
1501 {
1502 mUsesPointSize = true;
1503 out << name;
1504 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001505 else if (qualifier == EvqInstanceID)
1506 {
1507 mUsesInstanceID = true;
1508 out << name;
1509 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001510 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001511 {
1512 mUsesFragDepth = true;
1513 out << "gl_Depth";
1514 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001515 else
1516 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001517 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001519 }
1520}
1521
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001522void OutputHLSL::visitRaw(TIntermRaw *node)
1523{
Jamie Madill32aab012015-01-27 14:12:26 -05001524 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001525}
1526
Olli Etuaho7fb49552015-03-18 17:27:44 +02001527void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1528{
1529 if (type.isScalar() && !type.isArray())
1530 {
1531 if (op == EOpEqual)
1532 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001533 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001534 }
1535 else
1536 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001537 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001538 }
1539 }
1540 else
1541 {
1542 if (visit == PreVisit && op == EOpNotEqual)
1543 {
1544 out << "!";
1545 }
1546
1547 if (type.isArray())
1548 {
1549 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001550 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001551 }
1552 else if (type.getBasicType() == EbtStruct)
1553 {
1554 const TStructure &structure = *type.getStruct();
1555 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001556 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001557 }
1558 else
1559 {
1560 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001561 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001562 }
1563 }
1564}
1565
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001566bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1567{
Jamie Madill32aab012015-01-27 14:12:26 -05001568 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001569
Jamie Madill570e04d2013-06-21 09:15:33 -04001570 // Handle accessing std140 structs by value
1571 if (mFlaggedStructMappedNames.count(node) > 0)
1572 {
1573 out << mFlaggedStructMappedNames[node];
1574 return false;
1575 }
1576
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001577 switch (node->getOp())
1578 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001579 case EOpAssign:
1580 if (node->getLeft()->isArray())
1581 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001582 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1583 if (rightAgg != nullptr && rightAgg->isConstructor())
1584 {
1585 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1586 out << functionName << "(";
1587 node->getLeft()->traverse(this);
1588 TIntermSequence *seq = rightAgg->getSequence();
1589 for (auto &arrayElement : *seq)
1590 {
1591 out << ", ";
1592 arrayElement->traverse(this);
1593 }
1594 out << ")";
1595 return false;
1596 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001597 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1598 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1599
1600 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001601 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001602 }
1603 else
1604 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001605 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001606 }
1607 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001608 case EOpInitialize:
1609 if (visit == PreVisit)
1610 {
1611 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1612 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1613 // new variable is created before the assignment is evaluated), so we need to convert
1614 // this to "float t = x, x = t;".
1615
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001616 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001617 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001618 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001619
Jamie Madill37997142015-01-28 10:06:34 -05001620 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1621 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001622 {
Jamie Madill37997142015-01-28 10:06:34 -05001623 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001624 // after we initialize uniforms.
1625 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1626 deferredInit->setLeft(node->getLeft());
1627 deferredInit->setRight(node->getRight());
1628 deferredInit->setType(node->getType());
1629 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001630 const TString &initString = initializer(node->getType());
1631 node->setRight(new TIntermRaw(node->getType(), initString));
1632 }
1633 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1634 {
1635 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001636 return false;
1637 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001638 else if (writeConstantInitialization(out, symbolNode, expression))
1639 {
1640 return false;
1641 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001642 }
1643 else if (visit == InVisit)
1644 {
1645 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001646 }
1647 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001648 case EOpAddAssign:
1649 outputTriplet(out, visit, "(", " += ", ")");
1650 break;
1651 case EOpSubAssign:
1652 outputTriplet(out, visit, "(", " -= ", ")");
1653 break;
1654 case EOpMulAssign:
1655 outputTriplet(out, visit, "(", " *= ", ")");
1656 break;
1657 case EOpVectorTimesScalarAssign:
1658 outputTriplet(out, visit, "(", " *= ", ")");
1659 break;
1660 case EOpMatrixTimesScalarAssign:
1661 outputTriplet(out, visit, "(", " *= ", ")");
1662 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001663 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001664 if (visit == PreVisit)
1665 {
1666 out << "(";
1667 }
1668 else if (visit == InVisit)
1669 {
1670 out << " = mul(";
1671 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001672 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001673 }
1674 else
1675 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001676 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001677 }
1678 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001679 case EOpMatrixTimesMatrixAssign:
1680 if (visit == PreVisit)
1681 {
1682 out << "(";
1683 }
1684 else if (visit == InVisit)
1685 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001686 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001687 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001688 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001689 }
1690 else
1691 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001692 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001693 }
1694 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001695 case EOpDivAssign:
1696 outputTriplet(out, visit, "(", " /= ", ")");
1697 break;
1698 case EOpIModAssign:
1699 outputTriplet(out, visit, "(", " %= ", ")");
1700 break;
1701 case EOpBitShiftLeftAssign:
1702 outputTriplet(out, visit, "(", " <<= ", ")");
1703 break;
1704 case EOpBitShiftRightAssign:
1705 outputTriplet(out, visit, "(", " >>= ", ")");
1706 break;
1707 case EOpBitwiseAndAssign:
1708 outputTriplet(out, visit, "(", " &= ", ")");
1709 break;
1710 case EOpBitwiseXorAssign:
1711 outputTriplet(out, visit, "(", " ^= ", ")");
1712 break;
1713 case EOpBitwiseOrAssign:
1714 outputTriplet(out, visit, "(", " |= ", ")");
1715 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001716 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001717 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001718 const TType& leftType = node->getLeft()->getType();
1719 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001720 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001721 if (visit == PreVisit)
1722 {
1723 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1724 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001725 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001726 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001727 return false;
1728 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001729 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001730 else
1731 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001732 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001733 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001734 }
1735 break;
1736 case EOpIndexIndirect:
1737 // We do not currently support indirect references to interface blocks
1738 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001739 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001740 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001741 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001742 if (visit == InVisit)
1743 {
1744 const TStructure* structure = node->getLeft()->getType().getStruct();
1745 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1746 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001747 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001748
1749 return false;
1750 }
1751 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001752 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001753 if (visit == InVisit)
1754 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001755 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1756 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1757 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001758 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001759
1760 return false;
1761 }
1762 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001763 case EOpVectorSwizzle:
1764 if (visit == InVisit)
1765 {
1766 out << ".";
1767
1768 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1769
1770 if (swizzle)
1771 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001772 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001773
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001774 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001775 {
1776 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1777
1778 if (element)
1779 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001780 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781
1782 switch (i)
1783 {
1784 case 0: out << "x"; break;
1785 case 1: out << "y"; break;
1786 case 2: out << "z"; break;
1787 case 3: out << "w"; break;
1788 default: UNREACHABLE();
1789 }
1790 }
1791 else UNREACHABLE();
1792 }
1793 }
1794 else UNREACHABLE();
1795
1796 return false; // Fully processed
1797 }
1798 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001799 case EOpAdd:
1800 outputTriplet(out, visit, "(", " + ", ")");
1801 break;
1802 case EOpSub:
1803 outputTriplet(out, visit, "(", " - ", ")");
1804 break;
1805 case EOpMul:
1806 outputTriplet(out, visit, "(", " * ", ")");
1807 break;
1808 case EOpDiv:
1809 outputTriplet(out, visit, "(", " / ", ")");
1810 break;
1811 case EOpIMod:
1812 outputTriplet(out, visit, "(", " % ", ")");
1813 break;
1814 case EOpBitShiftLeft:
1815 outputTriplet(out, visit, "(", " << ", ")");
1816 break;
1817 case EOpBitShiftRight:
1818 outputTriplet(out, visit, "(", " >> ", ")");
1819 break;
1820 case EOpBitwiseAnd:
1821 outputTriplet(out, visit, "(", " & ", ")");
1822 break;
1823 case EOpBitwiseXor:
1824 outputTriplet(out, visit, "(", " ^ ", ")");
1825 break;
1826 case EOpBitwiseOr:
1827 outputTriplet(out, visit, "(", " | ", ")");
1828 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001829 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001830 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001831 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001832 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001833 case EOpLessThan:
1834 outputTriplet(out, visit, "(", " < ", ")");
1835 break;
1836 case EOpGreaterThan:
1837 outputTriplet(out, visit, "(", " > ", ")");
1838 break;
1839 case EOpLessThanEqual:
1840 outputTriplet(out, visit, "(", " <= ", ")");
1841 break;
1842 case EOpGreaterThanEqual:
1843 outputTriplet(out, visit, "(", " >= ", ")");
1844 break;
1845 case EOpVectorTimesScalar:
1846 outputTriplet(out, visit, "(", " * ", ")");
1847 break;
1848 case EOpMatrixTimesScalar:
1849 outputTriplet(out, visit, "(", " * ", ")");
1850 break;
1851 case EOpVectorTimesMatrix:
1852 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1853 break;
1854 case EOpMatrixTimesVector:
1855 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1856 break;
1857 case EOpMatrixTimesMatrix:
1858 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1859 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001860 case EOpLogicalOr:
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.comd7c98102010-05-14 17:30:48 +00001865 case EOpLogicalXor:
1866 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001867 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001868 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001869 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001870 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1871 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001872 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001873 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874 default: UNREACHABLE();
1875 }
1876
1877 return true;
1878}
1879
1880bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1881{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001882 TInfoSinkBase &out = getInfoSink();
1883
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001884 switch (node->getOp())
1885 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001886 case EOpNegative:
1887 outputTriplet(out, visit, "(-", "", ")");
1888 break;
1889 case EOpPositive:
1890 outputTriplet(out, visit, "(+", "", ")");
1891 break;
1892 case EOpVectorLogicalNot:
1893 outputTriplet(out, visit, "(!", "", ")");
1894 break;
1895 case EOpLogicalNot:
1896 outputTriplet(out, visit, "(!", "", ")");
1897 break;
1898 case EOpBitwiseNot:
1899 outputTriplet(out, visit, "(~", "", ")");
1900 break;
1901 case EOpPostIncrement:
1902 outputTriplet(out, visit, "(", "", "++)");
1903 break;
1904 case EOpPostDecrement:
1905 outputTriplet(out, visit, "(", "", "--)");
1906 break;
1907 case EOpPreIncrement:
1908 outputTriplet(out, visit, "(++", "", ")");
1909 break;
1910 case EOpPreDecrement:
1911 outputTriplet(out, visit, "(--", "", ")");
1912 break;
1913 case EOpRadians:
1914 outputTriplet(out, visit, "radians(", "", ")");
1915 break;
1916 case EOpDegrees:
1917 outputTriplet(out, visit, "degrees(", "", ")");
1918 break;
1919 case EOpSin:
1920 outputTriplet(out, visit, "sin(", "", ")");
1921 break;
1922 case EOpCos:
1923 outputTriplet(out, visit, "cos(", "", ")");
1924 break;
1925 case EOpTan:
1926 outputTriplet(out, visit, "tan(", "", ")");
1927 break;
1928 case EOpAsin:
1929 outputTriplet(out, visit, "asin(", "", ")");
1930 break;
1931 case EOpAcos:
1932 outputTriplet(out, visit, "acos(", "", ")");
1933 break;
1934 case EOpAtan:
1935 outputTriplet(out, visit, "atan(", "", ")");
1936 break;
1937 case EOpSinh:
1938 outputTriplet(out, visit, "sinh(", "", ")");
1939 break;
1940 case EOpCosh:
1941 outputTriplet(out, visit, "cosh(", "", ")");
1942 break;
1943 case EOpTanh:
1944 outputTriplet(out, visit, "tanh(", "", ")");
1945 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001946 case EOpAsinh:
1947 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001948 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001949 break;
1950 case EOpAcosh:
1951 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001952 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001953 break;
1954 case EOpAtanh:
1955 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001956 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001957 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001958 case EOpExp:
1959 outputTriplet(out, visit, "exp(", "", ")");
1960 break;
1961 case EOpLog:
1962 outputTriplet(out, visit, "log(", "", ")");
1963 break;
1964 case EOpExp2:
1965 outputTriplet(out, visit, "exp2(", "", ")");
1966 break;
1967 case EOpLog2:
1968 outputTriplet(out, visit, "log2(", "", ")");
1969 break;
1970 case EOpSqrt:
1971 outputTriplet(out, visit, "sqrt(", "", ")");
1972 break;
1973 case EOpInverseSqrt:
1974 outputTriplet(out, visit, "rsqrt(", "", ")");
1975 break;
1976 case EOpAbs:
1977 outputTriplet(out, visit, "abs(", "", ")");
1978 break;
1979 case EOpSign:
1980 outputTriplet(out, visit, "sign(", "", ")");
1981 break;
1982 case EOpFloor:
1983 outputTriplet(out, visit, "floor(", "", ")");
1984 break;
1985 case EOpTrunc:
1986 outputTriplet(out, visit, "trunc(", "", ")");
1987 break;
1988 case EOpRound:
1989 outputTriplet(out, visit, "round(", "", ")");
1990 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001991 case EOpRoundEven:
1992 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001993 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001994 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001995 case EOpCeil:
1996 outputTriplet(out, visit, "ceil(", "", ")");
1997 break;
1998 case EOpFract:
1999 outputTriplet(out, visit, "frac(", "", ")");
2000 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05302001 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002002 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05302003 mRequiresIEEEStrictCompiling = true;
2004 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002005 case EOpIsInf:
2006 outputTriplet(out, visit, "isinf(", "", ")");
2007 break;
2008 case EOpFloatBitsToInt:
2009 outputTriplet(out, visit, "asint(", "", ")");
2010 break;
2011 case EOpFloatBitsToUint:
2012 outputTriplet(out, visit, "asuint(", "", ")");
2013 break;
2014 case EOpIntBitsToFloat:
2015 outputTriplet(out, visit, "asfloat(", "", ")");
2016 break;
2017 case EOpUintBitsToFloat:
2018 outputTriplet(out, visit, "asfloat(", "", ")");
2019 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002020 case EOpPackSnorm2x16:
2021 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002022 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002023 break;
2024 case EOpPackUnorm2x16:
2025 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002026 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002027 break;
2028 case EOpPackHalf2x16:
2029 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002030 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002031 break;
2032 case EOpUnpackSnorm2x16:
2033 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002034 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002035 break;
2036 case EOpUnpackUnorm2x16:
2037 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002038 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002039 break;
2040 case EOpUnpackHalf2x16:
2041 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002042 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002043 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002044 case EOpLength:
2045 outputTriplet(out, visit, "length(", "", ")");
2046 break;
2047 case EOpNormalize:
2048 outputTriplet(out, visit, "normalize(", "", ")");
2049 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002050 case EOpDFdx:
2051 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2052 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002053 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002054 }
2055 else
2056 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002057 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002058 }
2059 break;
2060 case EOpDFdy:
2061 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2062 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002063 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002064 }
2065 else
2066 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002067 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002068 }
2069 break;
2070 case EOpFwidth:
2071 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2072 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002073 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002074 }
2075 else
2076 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002077 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002078 }
2079 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002080 case EOpTranspose:
2081 outputTriplet(out, visit, "transpose(", "", ")");
2082 break;
2083 case EOpDeterminant:
2084 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2085 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002086 case EOpInverse:
2087 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002088 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002089 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002090
Jamie Madill8c46ab12015-12-07 16:39:19 -05002091 case EOpAny:
2092 outputTriplet(out, visit, "any(", "", ")");
2093 break;
2094 case EOpAll:
2095 outputTriplet(out, visit, "all(", "", ")");
2096 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097 default: UNREACHABLE();
2098 }
2099
2100 return true;
2101}
2102
2103bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2104{
Jamie Madill32aab012015-01-27 14:12:26 -05002105 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002107 switch (node->getOp())
2108 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002109 case EOpSequence:
2110 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002111 if (mInsideFunction)
2112 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002113 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002114 out << "{\n";
2115 }
2116
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002117 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002118 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002119 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002120
Olli Etuahoa6f22092015-05-08 18:31:10 +03002121 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002122
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002123 // Don't output ; after case labels, they're terminated by :
2124 // This is needed especially since outputting a ; after a case statement would turn empty
2125 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002126 // Also no need to output ; after selection (if) statements or sequences. This is done just
2127 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002128 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2129 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002130 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002131 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002132 }
2133
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002134 if (mInsideFunction)
2135 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002136 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002137 out << "}\n";
2138 }
2139
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002140 return false;
2141 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142 case EOpDeclaration:
2143 if (visit == PreVisit)
2144 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002145 TIntermSequence *sequence = node->getSequence();
2146 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002147 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002149 if (variable &&
2150 (variable->getQualifier() == EvqTemporary ||
2151 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002153 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002154
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002155 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002157 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002158 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002159 out << "static ";
2160 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002161
Olli Etuahoa6f22092015-05-08 18:31:10 +03002162 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002163
Olli Etuahoa6f22092015-05-08 18:31:10 +03002164 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002165
Olli Etuahoa6f22092015-05-08 18:31:10 +03002166 if (symbol)
2167 {
2168 symbol->traverse(this);
2169 out << ArrayString(symbol->getType());
2170 out << " = " + initializer(symbol->getType());
2171 }
2172 else
2173 {
2174 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002176 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002177 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2178 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002179 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002180 }
2181 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002182 }
Jamie Madill033dae62014-06-18 12:56:28 -04002183 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002184 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002185 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002186 {
2187 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2188
2189 if (symbol)
2190 {
2191 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2192 mReferencedVaryings[symbol->getSymbol()] = symbol;
2193 }
2194 else
2195 {
2196 (*sit)->traverse(this);
2197 }
2198 }
2199 }
2200
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002201 return false;
2202 }
2203 else if (visit == InVisit)
2204 {
2205 out << ", ";
2206 }
2207 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002208 case EOpInvariantDeclaration:
2209 // Do not do any translation
2210 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002211 case EOpPrototype:
2212 if (visit == PreVisit)
2213 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002214 size_t index = mCallDag.findIndex(node);
2215 // Skip the prototype if it is not implemented (and thus not used)
2216 if (index == CallDAG::InvalidIndex)
2217 {
2218 return false;
2219 }
2220
Olli Etuaho59f9a642015-08-06 20:38:26 +03002221 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2222 out << TypeString(node->getType()) << " " << name
2223 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002224
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002225 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002226
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002227 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002228 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002229 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002230
2231 if (symbol)
2232 {
2233 out << argumentString(symbol);
2234
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002235 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002236 {
2237 out << ", ";
2238 }
2239 }
2240 else UNREACHABLE();
2241 }
2242
2243 out << ");\n";
2244
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002245 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002246 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2247 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002248 {
2249 mOutputLod0Function = true;
2250 node->traverse(this);
2251 mOutputLod0Function = false;
2252 }
2253
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002254 return false;
2255 }
2256 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002257 case EOpComma:
2258 outputTriplet(out, visit, "(", ", ", ")");
2259 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002260 case EOpFunction:
2261 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002262 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002263 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264
Corentin Wallez1239ee92015-03-19 14:38:02 -07002265 size_t index = mCallDag.findIndex(node);
2266 ASSERT(index != CallDAG::InvalidIndex);
2267 mCurrentFunctionMetadata = &mASTMetadataList[index];
2268
Jamie Madill033dae62014-06-18 12:56:28 -04002269 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002270
2271 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002272 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002273 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002275 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002276 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002277 out << DecorateFunctionIfNeeded(node->getNameObj())
2278 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002279 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002280
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002281 TIntermSequence *sequence = node->getSequence();
2282 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002283
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002284 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002285 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002286 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002287
2288 if (symbol)
2289 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002290 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002291
2292 out << argumentString(symbol);
2293
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002294 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002295 {
2296 out << ", ";
2297 }
2298 }
2299 else UNREACHABLE();
2300 }
2301
Olli Etuaho4785fec2015-05-18 16:09:37 +03002302 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002303
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002304 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002305 {
2306 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002307 TIntermNode *body = (*sequence)[1];
2308 // The function body node will output braces.
2309 ASSERT(IsSequence(body));
2310 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002311 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002312 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002313 else
2314 {
2315 out << "{}\n";
2316 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002317
Corentin Wallez1239ee92015-03-19 14:38:02 -07002318 mCurrentFunctionMetadata = nullptr;
2319
2320 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2321 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002322 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002323 ASSERT(name != "main");
2324 mOutputLod0Function = true;
2325 node->traverse(this);
2326 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002327 }
2328
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002329 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330 }
2331 break;
2332 case EOpFunctionCall:
2333 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002334 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002335
Corentin Wallez1239ee92015-03-19 14:38:02 -07002336 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002337 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002339 if (node->isArray())
2340 {
2341 UNIMPLEMENTED();
2342 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002343 size_t index = mCallDag.findIndex(node);
2344 ASSERT(index != CallDAG::InvalidIndex);
2345 lod0 &= mASTMetadataList[index].mNeedsLod0;
2346
Olli Etuaho59f9a642015-08-06 20:38:26 +03002347 out << DecorateFunctionIfNeeded(node->getNameObj()) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002348 }
2349 else
2350 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002351 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002352 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002353
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002354 TextureFunction textureFunction;
2355 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002356 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002357 textureFunction.method = TextureFunction::IMPLICIT;
2358 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002359 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002360
2361 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002362 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002363 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002364 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002365 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002366 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002367 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002368 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002369 }
Nicolas Capens46485082014-04-15 13:12:50 -04002370 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2371 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002372 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002373 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002374 }
Nicolas Capens46485082014-04-15 13:12:50 -04002375 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002376 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002377 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002378 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002379 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002380 else if (name == "textureSize")
2381 {
2382 textureFunction.method = TextureFunction::SIZE;
2383 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002384 else if (name == "textureOffset")
2385 {
2386 textureFunction.method = TextureFunction::IMPLICIT;
2387 textureFunction.offset = true;
2388 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002389 else if (name == "textureProjOffset")
2390 {
2391 textureFunction.method = TextureFunction::IMPLICIT;
2392 textureFunction.offset = true;
2393 textureFunction.proj = true;
2394 }
2395 else if (name == "textureLodOffset")
2396 {
2397 textureFunction.method = TextureFunction::LOD;
2398 textureFunction.offset = true;
2399 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002400 else if (name == "textureProjLodOffset")
2401 {
2402 textureFunction.method = TextureFunction::LOD;
2403 textureFunction.proj = true;
2404 textureFunction.offset = true;
2405 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002406 else if (name == "texelFetch")
2407 {
2408 textureFunction.method = TextureFunction::FETCH;
2409 }
2410 else if (name == "texelFetchOffset")
2411 {
2412 textureFunction.method = TextureFunction::FETCH;
2413 textureFunction.offset = true;
2414 }
Nicolas Capens46485082014-04-15 13:12:50 -04002415 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002416 {
2417 textureFunction.method = TextureFunction::GRAD;
2418 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002419 else if (name == "textureGradOffset")
2420 {
2421 textureFunction.method = TextureFunction::GRAD;
2422 textureFunction.offset = true;
2423 }
Nicolas Capens46485082014-04-15 13:12:50 -04002424 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002425 {
2426 textureFunction.method = TextureFunction::GRAD;
2427 textureFunction.proj = true;
2428 }
2429 else if (name == "textureProjGradOffset")
2430 {
2431 textureFunction.method = TextureFunction::GRAD;
2432 textureFunction.proj = true;
2433 textureFunction.offset = true;
2434 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002435 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002436
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002437 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002438 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002439 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2440
2441 if (textureFunction.offset)
2442 {
2443 mandatoryArgumentCount++;
2444 }
2445
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002446 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002447
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002448 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002449 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002450 if (bias)
2451 {
2452 textureFunction.method = TextureFunction::LOD0BIAS;
2453 }
2454 else
2455 {
2456 textureFunction.method = TextureFunction::LOD0;
2457 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002458 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002459 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002460 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002461 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002462 }
2463 }
2464
2465 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002466
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002467 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002469
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002470 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002471 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002472 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT &&
2473 IsSampler((*arg)->getAsTyped()->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002474 {
2475 out << "texture_";
2476 (*arg)->traverse(this);
2477 out << ", sampler_";
2478 }
2479
2480 (*arg)->traverse(this);
2481
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002482 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002483 {
2484 out << ", ";
2485 }
2486 }
2487
2488 out << ")";
2489
2490 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491 }
2492 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002493 case EOpParameters:
2494 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2495 break;
2496 case EOpConstructFloat:
2497 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2498 break;
2499 case EOpConstructVec2:
2500 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2501 break;
2502 case EOpConstructVec3:
2503 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2504 break;
2505 case EOpConstructVec4:
2506 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2507 break;
2508 case EOpConstructBool:
2509 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2510 break;
2511 case EOpConstructBVec2:
2512 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2513 break;
2514 case EOpConstructBVec3:
2515 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2516 break;
2517 case EOpConstructBVec4:
2518 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2519 break;
2520 case EOpConstructInt:
2521 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2522 break;
2523 case EOpConstructIVec2:
2524 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2525 break;
2526 case EOpConstructIVec3:
2527 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2528 break;
2529 case EOpConstructIVec4:
2530 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2531 break;
2532 case EOpConstructUInt:
2533 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2534 break;
2535 case EOpConstructUVec2:
2536 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2537 break;
2538 case EOpConstructUVec3:
2539 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2540 break;
2541 case EOpConstructUVec4:
2542 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2543 break;
2544 case EOpConstructMat2:
2545 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2546 break;
2547 case EOpConstructMat2x3:
2548 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2549 break;
2550 case EOpConstructMat2x4:
2551 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2552 break;
2553 case EOpConstructMat3x2:
2554 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2555 break;
2556 case EOpConstructMat3:
2557 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2558 break;
2559 case EOpConstructMat3x4:
2560 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2561 break;
2562 case EOpConstructMat4x2:
2563 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2564 break;
2565 case EOpConstructMat4x3:
2566 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2567 break;
2568 case EOpConstructMat4:
2569 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2570 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002571 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002572 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002573 if (node->getType().isArray())
2574 {
2575 UNIMPLEMENTED();
2576 }
Jamie Madill033dae62014-06-18 12:56:28 -04002577 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002578 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002579 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002580 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002581 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002582 case EOpLessThan:
2583 outputTriplet(out, visit, "(", " < ", ")");
2584 break;
2585 case EOpGreaterThan:
2586 outputTriplet(out, visit, "(", " > ", ")");
2587 break;
2588 case EOpLessThanEqual:
2589 outputTriplet(out, visit, "(", " <= ", ")");
2590 break;
2591 case EOpGreaterThanEqual:
2592 outputTriplet(out, visit, "(", " >= ", ")");
2593 break;
2594 case EOpVectorEqual:
2595 outputTriplet(out, visit, "(", " == ", ")");
2596 break;
2597 case EOpVectorNotEqual:
2598 outputTriplet(out, visit, "(", " != ", ")");
2599 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002600 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002601 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002602 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002603 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002604 case EOpModf:
2605 outputTriplet(out, visit, "modf(", ", ", ")");
2606 break;
2607 case EOpPow:
2608 outputTriplet(out, visit, "pow(", ", ", ")");
2609 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002610 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002611 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002612 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002613 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002614 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002615 case EOpMin:
2616 outputTriplet(out, visit, "min(", ", ", ")");
2617 break;
2618 case EOpMax:
2619 outputTriplet(out, visit, "max(", ", ", ")");
2620 break;
2621 case EOpClamp:
2622 outputTriplet(out, visit, "clamp(", ", ", ")");
2623 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302624 case EOpMix:
2625 {
2626 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2627 if (lastParamNode->getType().getBasicType() == EbtBool)
2628 {
2629 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2630 // so use emulated version.
2631 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002632 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302633 }
2634 else
2635 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002636 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302637 }
2638 }
2639 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002640 case EOpStep:
2641 outputTriplet(out, visit, "step(", ", ", ")");
2642 break;
2643 case EOpSmoothStep:
2644 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2645 break;
2646 case EOpDistance:
2647 outputTriplet(out, visit, "distance(", ", ", ")");
2648 break;
2649 case EOpDot:
2650 outputTriplet(out, visit, "dot(", ", ", ")");
2651 break;
2652 case EOpCross:
2653 outputTriplet(out, visit, "cross(", ", ", ")");
2654 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002655 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002656 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002657 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002658 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002659 case EOpReflect:
2660 outputTriplet(out, visit, "reflect(", ", ", ")");
2661 break;
2662 case EOpRefract:
2663 outputTriplet(out, visit, "refract(", ", ", ")");
2664 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002665 case EOpOuterProduct:
2666 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002667 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002668 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002669 case EOpMul:
2670 outputTriplet(out, visit, "(", " * ", ")");
2671 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002672 default: UNREACHABLE();
2673 }
2674
2675 return true;
2676}
2677
Jamie Madill8c46ab12015-12-07 16:39:19 -05002678void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002679{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002680 out << "if (";
2681
2682 node->getCondition()->traverse(this);
2683
2684 out << ")\n";
2685
Jamie Madill8c46ab12015-12-07 16:39:19 -05002686 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002687
2688 bool discard = false;
2689
2690 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002691 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002692 // The trueBlock child node will output braces.
2693 ASSERT(IsSequence(node->getTrueBlock()));
2694
Olli Etuahoa6f22092015-05-08 18:31:10 +03002695 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002696
Olli Etuahoa6f22092015-05-08 18:31:10 +03002697 // Detect true discard
2698 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2699 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002700 else
2701 {
2702 // TODO(oetuaho): Check if the semicolon inside is necessary.
2703 // It's there as a result of conservative refactoring of the output.
2704 out << "{;}\n";
2705 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002706
Jamie Madill8c46ab12015-12-07 16:39:19 -05002707 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002708
Olli Etuahoa6f22092015-05-08 18:31:10 +03002709 if (node->getFalseBlock())
2710 {
2711 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002712
Jamie Madill8c46ab12015-12-07 16:39:19 -05002713 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002714
Olli Etuaho4785fec2015-05-18 16:09:37 +03002715 // Either this is "else if" or the falseBlock child node will output braces.
2716 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2717
Olli Etuahoa6f22092015-05-08 18:31:10 +03002718 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002719
Jamie Madill8c46ab12015-12-07 16:39:19 -05002720 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002721
Olli Etuahoa6f22092015-05-08 18:31:10 +03002722 // Detect false discard
2723 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2724 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002725
Olli Etuahoa6f22092015-05-08 18:31:10 +03002726 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002727 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002728 {
2729 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002730 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002731}
2732
2733bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2734{
2735 TInfoSinkBase &out = getInfoSink();
2736
2737 ASSERT(!node->usesTernaryOperator());
2738
2739 if (!mInsideFunction)
2740 {
2741 // This is part of unfolded global initialization.
2742 mDeferredGlobalInitializers.push_back(node);
2743 return false;
2744 }
2745
2746 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002747 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002748 {
2749 out << "FLATTEN ";
2750 }
2751
Jamie Madill8c46ab12015-12-07 16:39:19 -05002752 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002753
2754 return false;
2755}
2756
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002757bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002758{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002759 TInfoSinkBase &out = getInfoSink();
2760
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002761 if (node->getStatementList())
2762 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002763 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002764 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002765 // The curly braces get written when visiting the statementList aggregate
2766 }
2767 else
2768 {
2769 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002770 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002771 }
2772 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002773}
2774
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002775bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002776{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002777 TInfoSinkBase &out = getInfoSink();
2778
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002779 if (node->hasCondition())
2780 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002781 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002782 return true;
2783 }
2784 else
2785 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002786 out << "default:\n";
2787 return false;
2788 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002789}
2790
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002791void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2792{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002793 TInfoSinkBase &out = getInfoSink();
2794 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002795}
2796
2797bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2798{
Nicolas Capens655fe362014-04-11 13:12:34 -04002799 mNestedLoopDepth++;
2800
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002801 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002802 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002803 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002804
Jamie Madill8c46ab12015-12-07 16:39:19 -05002805 TInfoSinkBase &out = getInfoSink();
2806
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002807 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002808 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002809 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002810 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002811 mInsideDiscontinuousLoop = wasDiscontinuous;
2812 mNestedLoopDepth--;
2813
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002814 return false;
2815 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002816 }
2817
Corentin Wallez1239ee92015-03-19 14:38:02 -07002818 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002819 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002821 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002822
Jamie Madill8c46ab12015-12-07 16:39:19 -05002823 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002824 }
2825 else
2826 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002827 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002828
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002829 if (node->getInit())
2830 {
2831 node->getInit()->traverse(this);
2832 }
2833
2834 out << "; ";
2835
alokp@chromium.org52813552010-11-16 18:36:09 +00002836 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002837 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002838 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839 }
2840
2841 out << "; ";
2842
alokp@chromium.org52813552010-11-16 18:36:09 +00002843 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002844 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002845 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002846 }
2847
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002848 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002849
Jamie Madill8c46ab12015-12-07 16:39:19 -05002850 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002851 }
2852
2853 if (node->getBody())
2854 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002855 // The loop body node will output braces.
2856 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002857 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002858 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002859 else
2860 {
2861 // TODO(oetuaho): Check if the semicolon inside is necessary.
2862 // It's there as a result of conservative refactoring of the output.
2863 out << "{;}\n";
2864 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002865
Jamie Madill8c46ab12015-12-07 16:39:19 -05002866 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002867
alokp@chromium.org52813552010-11-16 18:36:09 +00002868 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002869 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002870 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002871 out << "while(\n";
2872
alokp@chromium.org52813552010-11-16 18:36:09 +00002873 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002874
daniel@transgaming.com73536982012-03-21 20:45:49 +00002875 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002876 }
2877
daniel@transgaming.com73536982012-03-21 20:45:49 +00002878 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002879
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002880 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002881 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002882
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002883 return false;
2884}
2885
2886bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2887{
Jamie Madill32aab012015-01-27 14:12:26 -05002888 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002889
2890 switch (node->getFlowOp())
2891 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002892 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002893 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002894 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002895 case EOpBreak:
2896 if (visit == PreVisit)
2897 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002898 if (mNestedLoopDepth > 1)
2899 {
2900 mUsesNestedBreak = true;
2901 }
2902
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002903 if (mExcessiveLoopIndex)
2904 {
2905 out << "{Break";
2906 mExcessiveLoopIndex->traverse(this);
2907 out << " = true; break;}\n";
2908 }
2909 else
2910 {
2911 out << "break;\n";
2912 }
2913 }
2914 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002915 case EOpContinue:
2916 outputTriplet(out, visit, "continue;\n", "", "");
2917 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002918 case EOpReturn:
2919 if (visit == PreVisit)
2920 {
2921 if (node->getExpression())
2922 {
2923 out << "return ";
2924 }
2925 else
2926 {
2927 out << "return;\n";
2928 }
2929 }
2930 else if (visit == PostVisit)
2931 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002932 if (node->getExpression())
2933 {
2934 out << ";\n";
2935 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002936 }
2937 break;
2938 default: UNREACHABLE();
2939 }
2940
2941 return true;
2942}
2943
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002944bool OutputHLSL::isSingleStatement(TIntermNode *node)
2945{
2946 TIntermAggregate *aggregate = node->getAsAggregate();
2947
2948 if (aggregate)
2949 {
2950 if (aggregate->getOp() == EOpSequence)
2951 {
2952 return false;
2953 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002954 else if (aggregate->getOp() == EOpDeclaration)
2955 {
2956 // Declaring multiple comma-separated variables must be considered multiple statements
2957 // because each individual declaration has side effects which are visible in the next.
2958 return false;
2959 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002960 else
2961 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002962 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002963 {
2964 if (!isSingleStatement(*sit))
2965 {
2966 return false;
2967 }
2968 }
2969
2970 return true;
2971 }
2972 }
2973
2974 return true;
2975}
2976
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002977// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2978// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002979bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002980{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002981 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002982
2983 // Parse loops of the form:
2984 // for(int index = initial; index [comparator] limit; index += increment)
2985 TIntermSymbol *index = NULL;
2986 TOperator comparator = EOpNull;
2987 int initial = 0;
2988 int limit = 0;
2989 int increment = 0;
2990
2991 // Parse index name and intial value
2992 if (node->getInit())
2993 {
2994 TIntermAggregate *init = node->getInit()->getAsAggregate();
2995
2996 if (init)
2997 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002998 TIntermSequence *sequence = init->getSequence();
2999 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003000
3001 if (variable && variable->getQualifier() == EvqTemporary)
3002 {
3003 TIntermBinary *assign = variable->getAsBinaryNode();
3004
3005 if (assign->getOp() == EOpInitialize)
3006 {
3007 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
3008 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
3009
3010 if (symbol && constant)
3011 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003012 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003013 {
3014 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003015 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003016 }
3017 }
3018 }
3019 }
3020 }
3021 }
3022
3023 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003024 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003025 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003026 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003027
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003028 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3029 {
3030 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3031
3032 if (constant)
3033 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003034 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003035 {
3036 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003037 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003038 }
3039 }
3040 }
3041 }
3042
3043 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003044 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003045 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003046 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3047 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003048
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003049 if (binaryTerminal)
3050 {
3051 TOperator op = binaryTerminal->getOp();
3052 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3053
3054 if (constant)
3055 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003056 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003057 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003058 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003059
3060 switch (op)
3061 {
3062 case EOpAddAssign: increment = value; break;
3063 case EOpSubAssign: increment = -value; break;
3064 default: UNIMPLEMENTED();
3065 }
3066 }
3067 }
3068 }
3069 else if (unaryTerminal)
3070 {
3071 TOperator op = unaryTerminal->getOp();
3072
3073 switch (op)
3074 {
3075 case EOpPostIncrement: increment = 1; break;
3076 case EOpPostDecrement: increment = -1; break;
3077 case EOpPreIncrement: increment = 1; break;
3078 case EOpPreDecrement: increment = -1; break;
3079 default: UNIMPLEMENTED();
3080 }
3081 }
3082 }
3083
3084 if (index != NULL && comparator != EOpNull && increment != 0)
3085 {
3086 if (comparator == EOpLessThanEqual)
3087 {
3088 comparator = EOpLessThan;
3089 limit += 1;
3090 }
3091
3092 if (comparator == EOpLessThan)
3093 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003094 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003095
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003096 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003097 {
3098 return false; // Not an excessive loop
3099 }
3100
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003101 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3102 mExcessiveLoopIndex = index;
3103
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003104 out << "{int ";
3105 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003106 out << ";\n"
3107 "bool Break";
3108 index->traverse(this);
3109 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003110
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003111 bool firstLoopFragment = true;
3112
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003113 while (iterations > 0)
3114 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003115 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003116
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003117 if (!firstLoopFragment)
3118 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003119 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003120 index->traverse(this);
3121 out << ") {\n";
3122 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003123
3124 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3125 {
3126 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3127 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003128
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003129 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003130 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003131
Corentin Wallez1239ee92015-03-19 14:38:02 -07003132 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003133 index->traverse(this);
3134 out << " = ";
3135 out << initial;
3136
3137 out << "; ";
3138 index->traverse(this);
3139 out << " < ";
3140 out << clampedLimit;
3141
3142 out << "; ";
3143 index->traverse(this);
3144 out << " += ";
3145 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003146 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003147
Jamie Madill8c46ab12015-12-07 16:39:19 -05003148 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003149 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003150
3151 if (node->getBody())
3152 {
3153 node->getBody()->traverse(this);
3154 }
3155
Jamie Madill8c46ab12015-12-07 16:39:19 -05003156 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003157 out << ";}\n";
3158
3159 if (!firstLoopFragment)
3160 {
3161 out << "}\n";
3162 }
3163
3164 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003165
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003166 initial += MAX_LOOP_ITERATIONS * increment;
3167 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003168 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003169
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003170 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003171
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003172 mExcessiveLoopIndex = restoreIndex;
3173
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003174 return true;
3175 }
3176 else UNIMPLEMENTED();
3177 }
3178
3179 return false; // Not handled as an excessive loop
3180}
3181
Jamie Madill8c46ab12015-12-07 16:39:19 -05003182void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3183 Visit visit,
3184 const char *preString,
3185 const char *inString,
3186 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003187{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003188 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003189 {
3190 out << preString;
3191 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003192 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003193 {
3194 out << inString;
3195 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003196 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003197 {
3198 out << postString;
3199 }
3200}
3201
Jamie Madill8c46ab12015-12-07 16:39:19 -05003202void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003203{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003204 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003205 {
Jamie Madill32aab012015-01-27 14:12:26 -05003206 out << "\n";
3207 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003208
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003209 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003210 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003211 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003212 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003213
Jamie Madill32aab012015-01-27 14:12:26 -05003214 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003215 }
3216}
3217
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003218TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3219{
3220 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003221 const TType &type = symbol->getType();
3222 const TName &name = symbol->getName();
3223 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003224
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003225 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003226 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003227 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003228 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003229 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003230 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003231 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003232 }
3233
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003234 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003235 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003236 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3237 {
3238 // Samplers are passed as indices to the sampler array.
3239 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3240 return "const uint " + nameStr + ArrayString(type);
3241 }
3242 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3243 {
3244 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3245 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3246 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3247 ArrayString(type);
3248 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003249 }
3250
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003251 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003252}
3253
3254TString OutputHLSL::initializer(const TType &type)
3255{
3256 TString string;
3257
Jamie Madill94bf7f22013-07-08 13:31:15 -04003258 size_t size = type.getObjectSize();
3259 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003260 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003261 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003262
Jamie Madill94bf7f22013-07-08 13:31:15 -04003263 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003264 {
3265 string += ", ";
3266 }
3267 }
3268
daniel@transgaming.comead23042010-04-29 03:35:36 +00003269 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003270}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003271
Jamie Madill8c46ab12015-12-07 16:39:19 -05003272void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3273 Visit visit,
3274 const TType &type,
3275 const char *name,
3276 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003277{
Olli Etuahof40319e2015-03-10 14:33:00 +02003278 if (type.isArray())
3279 {
3280 UNIMPLEMENTED();
3281 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003282
3283 if (visit == PreVisit)
3284 {
Jamie Madill8daaba12014-06-13 10:04:33 -04003285 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003286
Daniel Bratell29190082015-02-20 16:42:54 +01003287 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003288 }
3289 else if (visit == InVisit)
3290 {
3291 out << ", ";
3292 }
3293 else if (visit == PostVisit)
3294 {
3295 out << ")";
3296 }
3297}
3298
Jamie Madill8c46ab12015-12-07 16:39:19 -05003299const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3300 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003301 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003302{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003303 const TConstantUnion *constUnionIterated = constUnion;
3304
Jamie Madill98493dd2013-07-08 14:39:03 -04003305 const TStructure* structure = type.getStruct();
3306 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003307 {
Jamie Madill033dae62014-06-18 12:56:28 -04003308 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003309
Jamie Madill98493dd2013-07-08 14:39:03 -04003310 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003311
Jamie Madill98493dd2013-07-08 14:39:03 -04003312 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003313 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003314 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003315 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003316
Jamie Madill98493dd2013-07-08 14:39:03 -04003317 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003318 {
3319 out << ", ";
3320 }
3321 }
3322
3323 out << ")";
3324 }
3325 else
3326 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003327 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003328 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003329
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003330 if (writeType)
3331 {
Jamie Madill033dae62014-06-18 12:56:28 -04003332 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003333 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003334 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003335 if (writeType)
3336 {
3337 out << ")";
3338 }
3339 }
3340
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003341 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003342}
3343
Jamie Madill8c46ab12015-12-07 16:39:19 -05003344void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003345{
3346 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003347 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003348}
3349
Jamie Madill37997142015-01-28 10:06:34 -05003350bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3351{
3352 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3353 expression->traverse(&searchSymbol);
3354
3355 if (searchSymbol.foundMatch())
3356 {
3357 // Type already printed
3358 out << "t" + str(mUniqueIndex) + " = ";
3359 expression->traverse(this);
3360 out << ", ";
3361 symbolNode->traverse(this);
3362 out << " = t" + str(mUniqueIndex);
3363
3364 mUniqueIndex++;
3365 return true;
3366 }
3367
3368 return false;
3369}
3370
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003371bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3372{
3373 // We support writing constant unions and constructors that only take constant unions as
3374 // parameters as HLSL literals.
3375 if (expression->getAsConstantUnion())
3376 {
3377 return true;
3378 }
3379 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3380 !expression->getAsAggregate()->isConstructor())
3381 {
3382 return false;
3383 }
3384 TIntermAggregate *constructor = expression->getAsAggregate();
3385 for (TIntermNode *&node : *constructor->getSequence())
3386 {
3387 if (!node->getAsConstantUnion())
3388 return false;
3389 }
3390 return true;
3391}
3392
3393bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3394 TIntermSymbol *symbolNode,
3395 TIntermTyped *expression)
3396{
3397 if (canWriteAsHLSLLiteral(expression))
3398 {
3399 symbolNode->traverse(this);
3400 if (expression->getType().isArray())
3401 {
3402 out << "[" << expression->getType().getArraySize() << "]";
3403 }
3404 out << " = {";
3405 if (expression->getAsConstantUnion())
3406 {
3407 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3408 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3409 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3410 }
3411 else
3412 {
3413 TIntermAggregate *constructor = expression->getAsAggregate();
3414 ASSERT(constructor != nullptr);
3415 for (TIntermNode *&node : *constructor->getSequence())
3416 {
3417 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3418 ASSERT(nodeConst);
3419 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3420 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3421 if (node != constructor->getSequence()->back())
3422 {
3423 out << ", ";
3424 }
3425 }
3426 }
3427 out << "}";
3428 return true;
3429 }
3430 return false;
3431}
3432
Jamie Madill37997142015-01-28 10:06:34 -05003433void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3434{
3435 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3436 << "\n"
3437 << "void initializeDeferredGlobals()\n"
3438 << "{\n";
3439
3440 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3441 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003442 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3443 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3444 if (binary != nullptr)
3445 {
3446 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3447 TIntermTyped *expression = binary->getRight();
3448 ASSERT(symbol);
3449 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003450
Olli Etuahod81ed842015-05-12 12:46:35 +03003451 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003452
Olli Etuahod81ed842015-05-12 12:46:35 +03003453 if (!writeSameSymbolInitializer(out, symbol, expression))
3454 {
3455 ASSERT(mInfoSinkStack.top() == &out);
3456 expression->traverse(this);
3457 }
3458 out << ";\n";
3459 }
3460 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003461 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003462 writeSelection(out, selection);
Jamie Madill37997142015-01-28 10:06:34 -05003463 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003464 else
3465 {
3466 UNREACHABLE();
3467 }
Jamie Madill37997142015-01-28 10:06:34 -05003468 }
3469
3470 out << "}\n"
3471 << "\n";
3472}
3473
Jamie Madill55e79e02015-02-09 15:35:00 -05003474TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3475{
3476 const TFieldList &fields = structure.fields();
3477
3478 for (const auto &eqFunction : mStructEqualityFunctions)
3479 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003480 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003481 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003482 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003483 }
3484 }
3485
3486 const TString &structNameString = StructNameString(structure);
3487
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003488 StructEqualityFunction *function = new StructEqualityFunction();
3489 function->structure = &structure;
3490 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003491
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003492 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003493
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003494 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3495 << "{\n"
3496 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003497
3498 for (size_t i = 0; i < fields.size(); i++)
3499 {
3500 const TField *field = fields[i];
3501 const TType *fieldType = field->type();
3502
3503 const TString &fieldNameA = "a." + Decorate(field->name());
3504 const TString &fieldNameB = "b." + Decorate(field->name());
3505
3506 if (i > 0)
3507 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003508 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003509 }
3510
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003511 fnOut << "(";
3512 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3513 fnOut << fieldNameA;
3514 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3515 fnOut << fieldNameB;
3516 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3517 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003518 }
3519
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003520 fnOut << ";\n" << "}\n";
3521
3522 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003523
3524 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003525 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003526
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003527 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003528}
3529
Olli Etuaho7fb49552015-03-18 17:27:44 +02003530TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3531{
3532 for (const auto &eqFunction : mArrayEqualityFunctions)
3533 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003534 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003535 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003536 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003537 }
3538 }
3539
3540 const TString &typeName = TypeString(type);
3541
Olli Etuaho12690762015-03-31 12:55:28 +03003542 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003543 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003544
3545 TInfoSinkBase fnNameOut;
3546 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003547 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003548
3549 TType nonArrayType = type;
3550 nonArrayType.clearArrayness();
3551
3552 TInfoSinkBase fnOut;
3553
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003554 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003555 << typeName << " a[" << type.getArraySize() << "], "
3556 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003557 << "{\n"
3558 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3559 " {\n"
3560 " if (";
3561
3562 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3563 fnOut << "a[i]";
3564 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3565 fnOut << "b[i]";
3566 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3567
3568 fnOut << ") { return false; }\n"
3569 " }\n"
3570 " return true;\n"
3571 "}\n";
3572
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003573 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003574
3575 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003576 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003577
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003578 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003579}
3580
Olli Etuaho12690762015-03-31 12:55:28 +03003581TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3582{
3583 for (const auto &assignFunction : mArrayAssignmentFunctions)
3584 {
3585 if (assignFunction.type == type)
3586 {
3587 return assignFunction.functionName;
3588 }
3589 }
3590
3591 const TString &typeName = TypeString(type);
3592
3593 ArrayHelperFunction function;
3594 function.type = type;
3595
3596 TInfoSinkBase fnNameOut;
3597 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3598 function.functionName = fnNameOut.c_str();
3599
3600 TInfoSinkBase fnOut;
3601
3602 fnOut << "void " << function.functionName << "(out "
3603 << typeName << " a[" << type.getArraySize() << "], "
3604 << typeName << " b[" << type.getArraySize() << "])\n"
3605 << "{\n"
3606 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3607 " {\n"
3608 " a[i] = b[i];\n"
3609 " }\n"
3610 "}\n";
3611
3612 function.functionDefinition = fnOut.c_str();
3613
3614 mArrayAssignmentFunctions.push_back(function);
3615
3616 return function.functionName;
3617}
3618
Olli Etuaho9638c352015-04-01 14:34:52 +03003619TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3620{
3621 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3622 {
3623 if (constructIntoFunction.type == type)
3624 {
3625 return constructIntoFunction.functionName;
3626 }
3627 }
3628
3629 const TString &typeName = TypeString(type);
3630
3631 ArrayHelperFunction function;
3632 function.type = type;
3633
3634 TInfoSinkBase fnNameOut;
3635 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3636 function.functionName = fnNameOut.c_str();
3637
3638 TInfoSinkBase fnOut;
3639
3640 fnOut << "void " << function.functionName << "(out "
3641 << typeName << " a[" << type.getArraySize() << "]";
3642 for (int i = 0; i < type.getArraySize(); ++i)
3643 {
3644 fnOut << ", " << typeName << " b" << i;
3645 }
3646 fnOut << ")\n"
3647 "{\n";
3648
3649 for (int i = 0; i < type.getArraySize(); ++i)
3650 {
3651 fnOut << " a[" << i << "] = b" << i << ";\n";
3652 }
3653 fnOut << "}\n";
3654
3655 function.functionDefinition = fnOut.c_str();
3656
3657 mArrayConstructIntoFunctions.push_back(function);
3658
3659 return function.functionName;
3660}
3661
Jamie Madill2e295e22015-04-29 10:41:33 -04003662void OutputHLSL::ensureStructDefined(const TType &type)
3663{
3664 TStructure *structure = type.getStruct();
3665
3666 if (structure)
3667 {
3668 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3669 }
3670}
3671
3672
Olli Etuaho9638c352015-04-01 14:34:52 +03003673
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003674}