blob: 170875e2c455b2716adc6ddcf481a523d628b01a [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
508 out << "};\n";
509 }
510 else
511 {
512 if (mUsesDepthRange)
513 {
514 out << "uniform float3 dx_DepthRange : register(c0);";
515 }
516
517 if (mUsesFragCoord)
518 {
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000519 out << "uniform float4 dx_ViewCoords : register(c1);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000520 }
521
522 if (mUsesFragCoord || mUsesFrontFacing)
523 {
524 out << "uniform float3 dx_DepthFront : register(c2);\n";
525 }
526 }
527
528 out << "\n";
529
530 if (mUsesDepthRange)
531 {
532 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
533 "\n";
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000534 }
daniel@transgaming.com5024cc42010-04-20 18:52:04 +0000535
Jamie Madillf91ce812014-06-13 10:04:34 -0400536 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000537 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400538 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000539 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400540 out << flaggedStructs;
541 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000542 }
543
shannon.woods%transgaming.com@gtempaccount.comaa8b5cf2013-04-13 03:31:55 +0000544 if (usingMRTExtension && mNumRenderTargets > 1)
545 {
546 out << "#define GL_USES_MRT\n";
547 }
548
549 if (mUsesFragColor)
550 {
551 out << "#define GL_USES_FRAG_COLOR\n";
552 }
553
554 if (mUsesFragData)
555 {
556 out << "#define GL_USES_FRAG_DATA\n";
557 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000558 }
daniel@transgaming.comd25ab252010-03-30 03:36:26 +0000559 else // Vertex shader
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560 {
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000561 out << "// Attributes\n";
562 out << attributes;
563 out << "\n"
564 "static float4 gl_Position = float4(0, 0, 0, 0);\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400565
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000566 if (mUsesPointSize)
567 {
568 out << "static float gl_PointSize = float(1);\n";
569 }
570
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +0000571 if (mUsesInstanceID)
572 {
573 out << "static int gl_InstanceID;";
574 }
575
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +0000576 out << "\n"
577 "// Varyings\n";
578 out << varyings;
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000579 out << "\n";
580
581 if (mUsesDepthRange)
582 {
583 out << "struct gl_DepthRangeParameters\n"
584 "{\n"
585 " float near;\n"
586 " float far;\n"
587 " float diff;\n"
588 "};\n"
589 "\n";
590 }
591
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200592 if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000593 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800594 out << "cbuffer DriverConstants : register(b1)\n"
595 "{\n";
596
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000597 if (mUsesDepthRange)
598 {
Austin Kinross4fd18b12014-12-22 12:32:05 -0800599 out << " float3 dx_DepthRange : packoffset(c0);\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000600 }
Austin Kinross4fd18b12014-12-22 12:32:05 -0800601
Jamie Madilld1c46222016-02-08 14:51:18 +0000602 // dx_ViewAdjust and dx_ViewCoords will only be used in Feature Level 9 shaders.
603 // However, we declare it for all shaders (including Feature Level 10+).
604 // The bytecode is the same whether we declare it or not, since D3DCompiler removes it if it's unused.
Austin Kinross4fd18b12014-12-22 12:32:05 -0800605 out << " float4 dx_ViewAdjust : packoffset(c1);\n";
Cooper Partine6664f02015-01-09 16:22:24 -0800606 out << " float2 dx_ViewCoords : packoffset(c2);\n";
Austin Kinross4fd18b12014-12-22 12:32:05 -0800607
608 out << "};\n"
609 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000610 }
611 else
612 {
613 if (mUsesDepthRange)
614 {
615 out << "uniform float3 dx_DepthRange : register(c0);\n";
616 }
617
Cooper Partine6664f02015-01-09 16:22:24 -0800618 out << "uniform float4 dx_ViewAdjust : register(c1);\n";
619 out << "uniform float2 dx_ViewCoords : register(c2);\n"
shannon.woods@transgaming.coma14ecf32013-02-28 23:09:42 +0000620 "\n";
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000621 }
622
shannon.woods@transgaming.com46a5b872013-01-25 21:52:57 +0000623 if (mUsesDepthRange)
624 {
625 out << "static gl_DepthRangeParameters gl_DepthRange = {dx_DepthRange.x, dx_DepthRange.y, dx_DepthRange.z};\n"
626 "\n";
627 }
628
Jamie Madillf91ce812014-06-13 10:04:34 -0400629 if (!flaggedStructs.empty())
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000630 {
Jamie Madillf91ce812014-06-13 10:04:34 -0400631 out << "// Std140 Structures accessed by value\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000632 out << "\n";
Jamie Madillf91ce812014-06-13 10:04:34 -0400633 out << flaggedStructs;
634 out << "\n";
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000635 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400636 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +0000637
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400638 for (TextureFunctionSet::const_iterator textureFunction = mUsesTexture.begin(); textureFunction != mUsesTexture.end(); textureFunction++)
639 {
640 // Return type
Nicolas Capens75fb4752013-07-10 15:14:47 -0400641 if (textureFunction->method == TextureFunction::SIZE)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000642 {
Nicolas Capens75fb4752013-07-10 15:14:47 -0400643 switch(textureFunction->sampler)
644 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400645 case EbtSampler2D: out << "int2 "; break;
646 case EbtSampler3D: out << "int3 "; break;
647 case EbtSamplerCube: out << "int2 "; break;
648 case EbtSampler2DArray: out << "int3 "; break;
649 case EbtISampler2D: out << "int2 "; break;
650 case EbtISampler3D: out << "int3 "; break;
651 case EbtISamplerCube: out << "int2 "; break;
652 case EbtISampler2DArray: out << "int3 "; break;
653 case EbtUSampler2D: out << "int2 "; break;
654 case EbtUSampler3D: out << "int3 "; break;
655 case EbtUSamplerCube: out << "int2 "; break;
656 case EbtUSampler2DArray: out << "int3 "; break;
657 case EbtSampler2DShadow: out << "int2 "; break;
658 case EbtSamplerCubeShadow: out << "int2 "; break;
659 case EbtSampler2DArrayShadow: out << "int3 "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400660 default: UNREACHABLE();
661 }
662 }
663 else // Sampling function
664 {
665 switch(textureFunction->sampler)
666 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400667 case EbtSampler2D: out << "float4 "; break;
668 case EbtSampler3D: out << "float4 "; break;
669 case EbtSamplerCube: out << "float4 "; break;
670 case EbtSampler2DArray: out << "float4 "; break;
671 case EbtISampler2D: out << "int4 "; break;
672 case EbtISampler3D: out << "int4 "; break;
673 case EbtISamplerCube: out << "int4 "; break;
674 case EbtISampler2DArray: out << "int4 "; break;
675 case EbtUSampler2D: out << "uint4 "; break;
676 case EbtUSampler3D: out << "uint4 "; break;
677 case EbtUSamplerCube: out << "uint4 "; break;
678 case EbtUSampler2DArray: out << "uint4 "; break;
679 case EbtSampler2DShadow: out << "float "; break;
680 case EbtSamplerCubeShadow: out << "float "; break;
681 case EbtSampler2DArrayShadow: out << "float "; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400682 default: UNREACHABLE();
683 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000684 }
685
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400686 // Function name
687 out << textureFunction->name();
688
689 // Argument list
690 int hlslCoords = 4;
691
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200692 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000693 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400694 switch(textureFunction->sampler)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000695 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400696 case EbtSampler2D: out << "sampler2D s"; hlslCoords = 2; break;
697 case EbtSamplerCube: out << "samplerCUBE s"; hlslCoords = 3; break;
698 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000699 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400700
Nicolas Capens75fb4752013-07-10 15:14:47 -0400701 switch(textureFunction->method)
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000702 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400703 case TextureFunction::IMPLICIT: break;
704 case TextureFunction::BIAS: hlslCoords = 4; break;
705 case TextureFunction::LOD: hlslCoords = 4; break;
706 case TextureFunction::LOD0: hlslCoords = 4; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400707 case TextureFunction::LOD0BIAS: hlslCoords = 4; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400708 default: UNREACHABLE();
shannon.woods@transgaming.comfb256be2013-01-25 21:49:25 +0000709 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400710 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200711 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400712 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200713 hlslCoords = HLSLTextureCoordsCount(textureFunction->sampler);
714 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400715 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200716 out << TextureString(textureFunction->sampler) << " x, "
717 << SamplerString(textureFunction->sampler) << " s";
718 }
719 else
720 {
721 ASSERT(mOutputType == SH_HLSL_4_1_OUTPUT);
722 out << "const uint samplerIndex";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400723 }
724 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400725
Nicolas Capensfc014542014-02-18 14:47:13 -0500726 if (textureFunction->method == TextureFunction::FETCH) // Integer coordinates
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400727 {
Nicolas Capensfc014542014-02-18 14:47:13 -0500728 switch(textureFunction->coords)
729 {
730 case 2: out << ", int2 t"; break;
731 case 3: out << ", int3 t"; break;
732 default: UNREACHABLE();
733 }
734 }
735 else // Floating-point coordinates (except textureSize)
736 {
737 switch(textureFunction->coords)
738 {
739 case 1: out << ", int lod"; break; // textureSize()
740 case 2: out << ", float2 t"; break;
741 case 3: out << ", float3 t"; break;
742 case 4: out << ", float4 t"; break;
743 default: UNREACHABLE();
744 }
daniel@transgaming.com15795192011-05-11 15:36:20 +0000745 }
746
Nicolas Capensd11d5492014-02-19 17:06:10 -0500747 if (textureFunction->method == TextureFunction::GRAD)
748 {
749 switch(textureFunction->sampler)
750 {
751 case EbtSampler2D:
752 case EbtISampler2D:
753 case EbtUSampler2D:
754 case EbtSampler2DArray:
755 case EbtISampler2DArray:
756 case EbtUSampler2DArray:
757 case EbtSampler2DShadow:
758 case EbtSampler2DArrayShadow:
759 out << ", float2 ddx, float2 ddy";
760 break;
761 case EbtSampler3D:
762 case EbtISampler3D:
763 case EbtUSampler3D:
764 case EbtSamplerCube:
765 case EbtISamplerCube:
766 case EbtUSamplerCube:
767 case EbtSamplerCubeShadow:
768 out << ", float3 ddx, float3 ddy";
769 break;
770 default: UNREACHABLE();
771 }
772 }
773
Nicolas Capens75fb4752013-07-10 15:14:47 -0400774 switch(textureFunction->method)
daniel@transgaming.com15795192011-05-11 15:36:20 +0000775 {
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400776 case TextureFunction::IMPLICIT: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400777 case TextureFunction::BIAS: break; // Comes after the offset parameter
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400778 case TextureFunction::LOD: out << ", float lod"; break;
779 case TextureFunction::LOD0: break;
Nicolas Capens84cfa122014-04-14 13:48:45 -0400780 case TextureFunction::LOD0BIAS: break; // Comes after the offset parameter
Nicolas Capens75fb4752013-07-10 15:14:47 -0400781 case TextureFunction::SIZE: break;
Nicolas Capensfc014542014-02-18 14:47:13 -0500782 case TextureFunction::FETCH: out << ", int mip"; break;
Nicolas Capensd11d5492014-02-19 17:06:10 -0500783 case TextureFunction::GRAD: break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400784 default: UNREACHABLE();
daniel@transgaming.com15795192011-05-11 15:36:20 +0000785 }
786
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500787 if (textureFunction->offset)
788 {
789 switch(textureFunction->sampler)
790 {
791 case EbtSampler2D: out << ", int2 offset"; break;
792 case EbtSampler3D: out << ", int3 offset"; break;
793 case EbtSampler2DArray: out << ", int2 offset"; break;
794 case EbtISampler2D: out << ", int2 offset"; break;
795 case EbtISampler3D: out << ", int3 offset"; break;
796 case EbtISampler2DArray: out << ", int2 offset"; break;
797 case EbtUSampler2D: out << ", int2 offset"; break;
798 case EbtUSampler3D: out << ", int3 offset"; break;
799 case EbtUSampler2DArray: out << ", int2 offset"; break;
800 case EbtSampler2DShadow: out << ", int2 offset"; break;
Nicolas Capensbf7db102014-02-19 17:20:28 -0500801 case EbtSampler2DArrayShadow: out << ", int2 offset"; break;
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500802 default: UNREACHABLE();
803 }
804 }
805
Nicolas Capens84cfa122014-04-14 13:48:45 -0400806 if (textureFunction->method == TextureFunction::BIAS ||
807 textureFunction->method == TextureFunction::LOD0BIAS)
Nicolas Capensb1f45b72013-12-19 17:37:19 -0500808 {
809 out << ", float bias";
810 }
811
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400812 out << ")\n"
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400813 "{\n";
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400814
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200815 // In some cases we use a variable to store the texture/sampler objects, but to work around
816 // a D3D11 compiler bug related to discard inside a loop that is conditional on texture
817 // sampling we need to call the function directly on a reference to the array. The bug was
818 // found using dEQP-GLES3.functional.shaders.discard*loop_texture* tests.
819 TString textureReference("x");
820 TString samplerReference("s");
821 if (mOutputType == SH_HLSL_4_1_OUTPUT)
822 {
823 TString suffix = TextureGroupSuffix(textureFunction->sampler);
824 if (TextureGroup(textureFunction->sampler) == HLSL_TEXTURE_2D)
825 {
826 textureReference = TString("textures") + suffix + "[samplerIndex]";
827 samplerReference = TString("samplers") + suffix + "[samplerIndex]";
828 }
829 else
830 {
831 out << " const uint textureIndex = samplerIndex - textureIndexOffset" << suffix
832 << ";\n";
833 textureReference = TString("textures") + suffix + "[textureIndex]";
834 out << " const uint samplerArrayIndex = samplerIndex - samplerIndexOffset"
835 << suffix << ";\n";
836 samplerReference = TString("samplers") + suffix + "[samplerArrayIndex]";
837 }
838 }
839
Nicolas Capens75fb4752013-07-10 15:14:47 -0400840 if (textureFunction->method == TextureFunction::SIZE)
841 {
842 if (IsSampler2D(textureFunction->sampler) || IsSamplerCube(textureFunction->sampler))
843 {
844 if (IsSamplerArray(textureFunction->sampler))
845 {
846 out << " uint width; uint height; uint layers; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200847 << " " << textureReference
848 << ".GetDimensions(lod, width, height, layers, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400849 }
850 else
851 {
852 out << " uint width; uint height; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200853 << " " << textureReference
854 << ".GetDimensions(lod, width, height, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400855 }
856 }
857 else if (IsSampler3D(textureFunction->sampler))
858 {
859 out << " uint width; uint height; uint depth; uint numberOfLevels;\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200860 << " " << textureReference
861 << ".GetDimensions(lod, width, height, depth, numberOfLevels);\n";
Nicolas Capens75fb4752013-07-10 15:14:47 -0400862 }
863 else UNREACHABLE();
864
865 switch(textureFunction->sampler)
866 {
Nicolas Capenscb127d32013-07-15 17:26:18 -0400867 case EbtSampler2D: out << " return int2(width, height);"; break;
868 case EbtSampler3D: out << " return int3(width, height, depth);"; break;
869 case EbtSamplerCube: out << " return int2(width, height);"; break;
870 case EbtSampler2DArray: out << " return int3(width, height, layers);"; break;
871 case EbtISampler2D: out << " return int2(width, height);"; break;
872 case EbtISampler3D: out << " return int3(width, height, depth);"; break;
873 case EbtISamplerCube: out << " return int2(width, height);"; break;
874 case EbtISampler2DArray: out << " return int3(width, height, layers);"; break;
875 case EbtUSampler2D: out << " return int2(width, height);"; break;
876 case EbtUSampler3D: out << " return int3(width, height, depth);"; break;
877 case EbtUSamplerCube: out << " return int2(width, height);"; break;
878 case EbtUSampler2DArray: out << " return int3(width, height, layers);"; break;
879 case EbtSampler2DShadow: out << " return int2(width, height);"; break;
880 case EbtSamplerCubeShadow: out << " return int2(width, height);"; break;
881 case EbtSampler2DArrayShadow: out << " return int3(width, height, layers);"; break;
Nicolas Capens75fb4752013-07-10 15:14:47 -0400882 default: UNREACHABLE();
883 }
884 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400885 else
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400886 {
Nicolas Capens0027fa92014-02-20 14:26:42 -0500887 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
888 {
889 out << " float width; float height; float layers; float levels;\n";
890
891 out << " uint mip = 0;\n";
892
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200893 out << " " << textureReference
894 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens0027fa92014-02-20 14:26:42 -0500895
896 out << " bool xMajor = abs(t.x) > abs(t.y) && abs(t.x) > abs(t.z);\n";
897 out << " bool yMajor = abs(t.y) > abs(t.z) && abs(t.y) > abs(t.x);\n";
898 out << " bool zMajor = abs(t.z) > abs(t.x) && abs(t.z) > abs(t.y);\n";
899 out << " bool negative = (xMajor && t.x < 0.0f) || (yMajor && t.y < 0.0f) || (zMajor && t.z < 0.0f);\n";
900
901 // FACE_POSITIVE_X = 000b
902 // FACE_NEGATIVE_X = 001b
903 // FACE_POSITIVE_Y = 010b
904 // FACE_NEGATIVE_Y = 011b
905 // FACE_POSITIVE_Z = 100b
906 // FACE_NEGATIVE_Z = 101b
907 out << " int face = (int)negative + (int)yMajor * 2 + (int)zMajor * 4;\n";
908
909 out << " float u = xMajor ? -t.z : (yMajor && t.y < 0.0f ? -t.x : t.x);\n";
910 out << " float v = yMajor ? t.z : (negative ? t.y : -t.y);\n";
911 out << " float m = xMajor ? t.x : (yMajor ? t.y : t.z);\n";
912
913 out << " t.x = (u * 0.5f / m) + 0.5f;\n";
914 out << " t.y = (v * 0.5f / m) + 0.5f;\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500915
916 // Mip level computation.
917 if (textureFunction->method == TextureFunction::IMPLICIT)
918 {
919 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
920 " float2 dx = ddx(tSized);\n"
921 " float2 dy = ddy(tSized);\n"
922 " float lod = 0.5f * log2(max(dot(dx, dx), dot(dy, dy)));\n"
923 " mip = uint(min(max(round(lod), 0), levels - 1));\n"
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200924 << " " << textureReference
925 << ".GetDimensions(mip, width, height, layers, levels);\n";
Jamie Madill8d9f35f2015-11-24 16:10:20 -0500926 }
Nicolas Capens0027fa92014-02-20 14:26:42 -0500927 }
928 else if (IsIntegerSampler(textureFunction->sampler) &&
929 textureFunction->method != TextureFunction::FETCH)
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400930 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -0400931 if (IsSampler2D(textureFunction->sampler))
Nicolas Capense0ba27a2013-06-24 16:10:52 -0400932 {
Nicolas Capens93e50de2013-07-09 13:46:28 -0400933 if (IsSamplerArray(textureFunction->sampler))
934 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400935 out << " float width; float height; float layers; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400936
Nicolas Capens9edebd62013-08-06 10:59:10 -0400937 if (textureFunction->method == TextureFunction::LOD0)
938 {
939 out << " uint mip = 0;\n";
940 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400941 else if (textureFunction->method == TextureFunction::LOD0BIAS)
942 {
943 out << " uint mip = bias;\n";
944 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400945 else
946 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200947
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200948 out << " " << textureReference
949 << ".GetDimensions(0, width, height, layers, levels);\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400950 if (textureFunction->method == TextureFunction::IMPLICIT ||
951 textureFunction->method == TextureFunction::BIAS)
952 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200953 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400954 " float dx = length(ddx(tSized));\n"
955 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500956 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400957
958 if (textureFunction->method == TextureFunction::BIAS)
959 {
960 out << " lod += bias;\n";
961 }
962 }
Nicolas Capensd11d5492014-02-19 17:06:10 -0500963 else if (textureFunction->method == TextureFunction::GRAD)
964 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200965 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -0500966 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400967
968 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
969 }
970
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200971 out << " " << textureReference
972 << ".GetDimensions(mip, width, height, layers, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -0400973 }
974 else
975 {
Nicolas Capens9edebd62013-08-06 10:59:10 -0400976 out << " float width; float height; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -0400977
Nicolas Capens9edebd62013-08-06 10:59:10 -0400978 if (textureFunction->method == TextureFunction::LOD0)
979 {
980 out << " uint mip = 0;\n";
981 }
Nicolas Capens84cfa122014-04-14 13:48:45 -0400982 else if (textureFunction->method == TextureFunction::LOD0BIAS)
983 {
984 out << " uint mip = bias;\n";
985 }
Nicolas Capens9edebd62013-08-06 10:59:10 -0400986 else
987 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +0200988 out << " " << textureReference
989 << ".GetDimensions(0, width, height, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200990
Nicolas Capens9edebd62013-08-06 10:59:10 -0400991 if (textureFunction->method == TextureFunction::IMPLICIT ||
992 textureFunction->method == TextureFunction::BIAS)
993 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +0200994 out << " float2 tSized = float2(t.x * width, t.y * height);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -0400995 " float dx = length(ddx(tSized));\n"
996 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -0500997 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -0400998
999 if (textureFunction->method == TextureFunction::BIAS)
1000 {
1001 out << " lod += bias;\n";
1002 }
1003 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001004 else if (textureFunction->method == TextureFunction::GRAD)
1005 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001006 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001007 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001008
1009 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1010 }
1011
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001012 out << " " << textureReference
1013 << ".GetDimensions(mip, width, height, levels);\n";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001014 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001015 }
1016 else if (IsSampler3D(textureFunction->sampler))
1017 {
Nicolas Capens9edebd62013-08-06 10:59:10 -04001018 out << " float width; float height; float depth; float levels;\n";
Nicolas Capens84cfa122014-04-14 13:48:45 -04001019
Nicolas Capens9edebd62013-08-06 10:59:10 -04001020 if (textureFunction->method == TextureFunction::LOD0)
1021 {
1022 out << " uint mip = 0;\n";
1023 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04001024 else if (textureFunction->method == TextureFunction::LOD0BIAS)
1025 {
1026 out << " uint mip = bias;\n";
1027 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001028 else
1029 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001030 out << " " << textureReference
1031 << ".GetDimensions(0, width, height, depth, levels);\n";
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001032
Nicolas Capens9edebd62013-08-06 10:59:10 -04001033 if (textureFunction->method == TextureFunction::IMPLICIT ||
1034 textureFunction->method == TextureFunction::BIAS)
1035 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001036 out << " float3 tSized = float3(t.x * width, t.y * height, t.z * "
1037 "depth);\n"
Nicolas Capens9edebd62013-08-06 10:59:10 -04001038 " float dx = length(ddx(tSized));\n"
1039 " float dy = length(ddy(tSized));\n"
Jamie Madill03847b62013-11-13 19:42:39 -05001040 " float lod = log2(max(dx, dy));\n";
Nicolas Capens9edebd62013-08-06 10:59:10 -04001041
1042 if (textureFunction->method == TextureFunction::BIAS)
1043 {
1044 out << " lod += bias;\n";
1045 }
1046 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001047 else if (textureFunction->method == TextureFunction::GRAD)
1048 {
Olli Etuahoe8e4deb2015-11-18 17:15:38 +02001049 out << " float lod = log2(max(length(ddx), length(ddy)));\n";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001050 }
Nicolas Capens9edebd62013-08-06 10:59:10 -04001051
1052 out << " uint mip = uint(min(max(round(lod), 0), levels - 1));\n";
1053 }
1054
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001055 out << " " << textureReference
1056 << ".GetDimensions(mip, width, height, depth, levels);\n";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001057 }
1058 else UNREACHABLE();
1059 }
1060
1061 out << " return ";
1062
1063 // HLSL intrinsic
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001064 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001065 {
1066 switch(textureFunction->sampler)
1067 {
1068 case EbtSampler2D: out << "tex2D"; break;
1069 case EbtSamplerCube: out << "texCUBE"; break;
1070 default: UNREACHABLE();
1071 }
1072
Nicolas Capens75fb4752013-07-10 15:14:47 -04001073 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001074 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001075 case TextureFunction::IMPLICIT:
1076 out << "(" << samplerReference << ", ";
1077 break;
1078 case TextureFunction::BIAS:
1079 out << "bias(" << samplerReference << ", ";
1080 break;
1081 case TextureFunction::LOD:
1082 out << "lod(" << samplerReference << ", ";
1083 break;
1084 case TextureFunction::LOD0:
1085 out << "lod(" << samplerReference << ", ";
1086 break;
1087 case TextureFunction::LOD0BIAS:
1088 out << "lod(" << samplerReference << ", ";
1089 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001090 default: UNREACHABLE();
1091 }
1092 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001093 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001094 {
Nicolas Capensd11d5492014-02-19 17:06:10 -05001095 if (textureFunction->method == TextureFunction::GRAD)
1096 {
1097 if (IsIntegerSampler(textureFunction->sampler))
1098 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001099 out << "" << textureReference << ".Load(";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001100 }
1101 else if (IsShadowSampler(textureFunction->sampler))
1102 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001103 out << "" << textureReference << ".SampleCmpLevelZero(" << samplerReference
1104 << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001105 }
1106 else
1107 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001108 out << "" << textureReference << ".SampleGrad(" << samplerReference << ", ";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001109 }
1110 }
1111 else if (IsIntegerSampler(textureFunction->sampler) ||
1112 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001113 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001114 out << "" << textureReference << ".Load(";
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001115 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001116 else if (IsShadowSampler(textureFunction->sampler))
1117 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001118 switch(textureFunction->method)
1119 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001120 case TextureFunction::IMPLICIT:
1121 out << "" << textureReference << ".SampleCmp(" << samplerReference
1122 << ", ";
1123 break;
1124 case TextureFunction::BIAS:
1125 out << "" << textureReference << ".SampleCmp(" << samplerReference
1126 << ", ";
1127 break;
1128 case TextureFunction::LOD:
1129 out << "" << textureReference << ".SampleCmp(" << samplerReference
1130 << ", ";
1131 break;
1132 case TextureFunction::LOD0:
1133 out << "" << textureReference << ".SampleCmpLevelZero("
1134 << samplerReference << ", ";
1135 break;
1136 case TextureFunction::LOD0BIAS:
1137 out << "" << textureReference << ".SampleCmpLevelZero("
1138 << samplerReference << ", ";
1139 break;
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001140 default: UNREACHABLE();
1141 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001142 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001143 else
1144 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001145 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001146 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001147 case TextureFunction::IMPLICIT:
1148 out << "" << textureReference << ".Sample(" << samplerReference << ", ";
1149 break;
1150 case TextureFunction::BIAS:
1151 out << "" << textureReference << ".SampleBias(" << samplerReference
1152 << ", ";
1153 break;
1154 case TextureFunction::LOD:
1155 out << "" << textureReference << ".SampleLevel(" << samplerReference
1156 << ", ";
1157 break;
1158 case TextureFunction::LOD0:
1159 out << "" << textureReference << ".SampleLevel(" << samplerReference
1160 << ", ";
1161 break;
1162 case TextureFunction::LOD0BIAS:
1163 out << "" << textureReference << ".SampleLevel(" << samplerReference
1164 << ", ";
1165 break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001166 default: UNREACHABLE();
1167 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001168 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001169 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001170 else UNREACHABLE();
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001171
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001172 // Integer sampling requires integer addresses
1173 TString addressx = "";
1174 TString addressy = "";
1175 TString addressz = "";
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001176 TString close = "";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001177
Nicolas Capensfc014542014-02-18 14:47:13 -05001178 if (IsIntegerSampler(textureFunction->sampler) ||
1179 textureFunction->method == TextureFunction::FETCH)
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001180 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001181 switch(hlslCoords)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001182 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001183 case 2: out << "int3("; break;
1184 case 3: out << "int4("; break;
1185 default: UNREACHABLE();
1186 }
Jamie Madillf91ce812014-06-13 10:04:34 -04001187
Nicolas Capensfc014542014-02-18 14:47:13 -05001188 // Convert from normalized floating-point to integer
1189 if (textureFunction->method != TextureFunction::FETCH)
Nicolas Capens93e50de2013-07-09 13:46:28 -04001190 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001191 addressx = "int(floor(width * frac((";
1192 addressy = "int(floor(height * frac((";
Nicolas Capens93e50de2013-07-09 13:46:28 -04001193
Nicolas Capensfc014542014-02-18 14:47:13 -05001194 if (IsSamplerArray(textureFunction->sampler))
1195 {
1196 addressz = "int(max(0, min(layers - 1, floor(0.5 + ";
1197 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001198 else if (IsSamplerCube(textureFunction->sampler))
Nicolas Capens0027fa92014-02-20 14:26:42 -05001199 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001200 addressz = "((((";
Nicolas Capens0027fa92014-02-20 14:26:42 -05001201 }
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001202 else
1203 {
1204 addressz = "int(floor(depth * frac((";
1205 }
1206
1207 close = "))))";
Nicolas Capensfc014542014-02-18 14:47:13 -05001208 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001209 }
1210 else
1211 {
1212 switch(hlslCoords)
1213 {
1214 case 2: out << "float2("; break;
1215 case 3: out << "float3("; break;
1216 case 4: out << "float4("; break;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001217 default: UNREACHABLE();
1218 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001219 }
Nicolas Capens9fe6f982013-06-24 16:05:25 -04001220
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001221 TString proj = ""; // Only used for projected textures
Jamie Madillf91ce812014-06-13 10:04:34 -04001222
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001223 if (textureFunction->proj)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001224 {
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001225 switch(textureFunction->coords)
1226 {
1227 case 3: proj = " / t.z"; break;
1228 case 4: proj = " / t.w"; break;
1229 default: UNREACHABLE();
1230 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001231 }
daniel@transgaming.com15795192011-05-11 15:36:20 +00001232
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001233 out << addressx + ("t.x" + proj) + close + ", " + addressy + ("t.y" + proj) + close;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001234
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001235 if (mOutputType == SH_HLSL_3_0_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001236 {
1237 if (hlslCoords >= 3)
1238 {
1239 if (textureFunction->coords < 3)
1240 {
1241 out << ", 0";
1242 }
1243 else
1244 {
1245 out << ", t.z" + proj;
1246 }
1247 }
1248
1249 if (hlslCoords == 4)
1250 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001251 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001252 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04001253 case TextureFunction::BIAS: out << ", bias"; break;
1254 case TextureFunction::LOD: out << ", lod"; break;
1255 case TextureFunction::LOD0: out << ", 0"; break;
1256 case TextureFunction::LOD0BIAS: out << ", bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001257 default: UNREACHABLE();
1258 }
1259 }
1260
1261 out << "));\n";
1262 }
Olli Etuaho9b4e8622015-12-22 15:53:22 +02001263 else if (mOutputType == SH_HLSL_4_1_OUTPUT || mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001264 {
1265 if (hlslCoords >= 3)
1266 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001267 if (IsIntegerSampler(textureFunction->sampler) && IsSamplerCube(textureFunction->sampler))
1268 {
1269 out << ", face";
1270 }
1271 else
1272 {
Jamie Madill6c9b2ae2015-12-08 13:43:11 +00001273 out << ", " + addressz + ("t.z" + proj) + close;
Nicolas Capens0027fa92014-02-20 14:26:42 -05001274 }
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001275 }
1276
Nicolas Capensd11d5492014-02-19 17:06:10 -05001277 if (textureFunction->method == TextureFunction::GRAD)
1278 {
1279 if (IsIntegerSampler(textureFunction->sampler))
1280 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001281 out << ", mip)";
Nicolas Capensd11d5492014-02-19 17:06:10 -05001282 }
1283 else if (IsShadowSampler(textureFunction->sampler))
1284 {
Nicolas Capens0027fa92014-02-20 14:26:42 -05001285 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001286 if (textureFunction->proj)
Nicolas Capensd11d5492014-02-19 17:06:10 -05001287 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001288 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1289 // The resulting third component of P' in the shadow forms is used as Dref
1290 out << "), t.z" << proj;
1291 }
1292 else
1293 {
1294 switch(textureFunction->coords)
1295 {
1296 case 3: out << "), t.z"; break;
1297 case 4: out << "), t.w"; break;
1298 default: UNREACHABLE();
1299 }
Nicolas Capensd11d5492014-02-19 17:06:10 -05001300 }
1301 }
1302 else
1303 {
1304 out << "), ddx, ddy";
1305 }
1306 }
1307 else if (IsIntegerSampler(textureFunction->sampler) ||
1308 textureFunction->method == TextureFunction::FETCH)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001309 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001310 out << ", mip)";
Nicolas Capenscb127d32013-07-15 17:26:18 -04001311 }
1312 else if (IsShadowSampler(textureFunction->sampler))
1313 {
1314 // Compare value
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001315 if (textureFunction->proj)
Nicolas Capenscb127d32013-07-15 17:26:18 -04001316 {
Gregoire Payen de La Garanderie5cc9ac82015-02-20 11:27:56 +00001317 // According to ESSL 3.00.4 sec 8.8 p95 on textureProj:
1318 // The resulting third component of P' in the shadow forms is used as Dref
1319 out << "), t.z" << proj;
1320 }
1321 else
1322 {
1323 switch(textureFunction->coords)
1324 {
1325 case 3: out << "), t.z"; break;
1326 case 4: out << "), t.w"; break;
1327 default: UNREACHABLE();
1328 }
Nicolas Capenscb127d32013-07-15 17:26:18 -04001329 }
1330 }
1331 else
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001332 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04001333 switch(textureFunction->method)
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001334 {
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001335 case TextureFunction::IMPLICIT: out << ")"; break;
1336 case TextureFunction::BIAS: out << "), bias"; break;
1337 case TextureFunction::LOD: out << "), lod"; break;
1338 case TextureFunction::LOD0: out << "), 0"; break;
Nicolas Capens84cfa122014-04-14 13:48:45 -04001339 case TextureFunction::LOD0BIAS: out << "), bias"; break;
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001340 default: UNREACHABLE();
1341 }
1342 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05001343
1344 if (textureFunction->offset)
1345 {
1346 out << ", offset";
1347 }
1348
1349 out << ");";
Nicolas Capens6d232bb2013-07-08 15:56:38 -04001350 }
1351 else UNREACHABLE();
1352 }
1353
1354 out << "\n"
1355 "}\n"
Nicolas Capense0ba27a2013-06-24 16:10:52 -04001356 "\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001357 }
1358
daniel@transgaming.com4af7acc2010-05-14 17:30:53 +00001359 if (mUsesFragCoord)
1360 {
1361 out << "#define GL_USES_FRAG_COORD\n";
1362 }
1363
1364 if (mUsesPointCoord)
1365 {
1366 out << "#define GL_USES_POINT_COORD\n";
1367 }
1368
1369 if (mUsesFrontFacing)
1370 {
1371 out << "#define GL_USES_FRONT_FACING\n";
1372 }
1373
1374 if (mUsesPointSize)
1375 {
1376 out << "#define GL_USES_POINT_SIZE\n";
1377 }
1378
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001379 if (mUsesFragDepth)
1380 {
1381 out << "#define GL_USES_FRAG_DEPTH\n";
1382 }
1383
shannonwoods@chromium.org03299882013-05-30 00:05:26 +00001384 if (mUsesDepthRange)
1385 {
1386 out << "#define GL_USES_DEPTH_RANGE\n";
1387 }
1388
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001389 if (mUsesXor)
1390 {
1391 out << "bool xor(bool p, bool q)\n"
1392 "{\n"
1393 " return (p || q) && !(p && q);\n"
1394 "}\n"
1395 "\n";
1396 }
1397
Olli Etuaho95cd3c62015-03-03 16:45:32 +02001398 builtInFunctionEmulator->OutputEmulatedFunctions(out);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001399}
1400
1401void OutputHLSL::visitSymbol(TIntermSymbol *node)
1402{
Jamie Madill32aab012015-01-27 14:12:26 -05001403 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001404
Jamie Madill570e04d2013-06-21 09:15:33 -04001405 // Handle accessing std140 structs by value
1406 if (mFlaggedStructMappedNames.count(node) > 0)
1407 {
1408 out << mFlaggedStructMappedNames[node];
1409 return;
1410 }
1411
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001412 TString name = node->getSymbol();
1413
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001414 if (name == "gl_DepthRange")
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001415 {
1416 mUsesDepthRange = true;
1417 out << name;
1418 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001419 else
1420 {
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001421 TQualifier qualifier = node->getQualifier();
1422
1423 if (qualifier == EvqUniform)
1424 {
Jamie Madill2e295e22015-04-29 10:41:33 -04001425 const TType &nodeType = node->getType();
1426 const TInterfaceBlock *interfaceBlock = nodeType.getInterfaceBlock();
Jamie Madill98493dd2013-07-08 14:39:03 -04001427
1428 if (interfaceBlock)
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001429 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001430 mReferencedInterfaceBlocks[interfaceBlock->name()] = node;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001431 }
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001432 else
1433 {
1434 mReferencedUniforms[name] = node;
shannonwoods@chromium.org4a643ae2013-05-30 00:12:27 +00001435 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001436
Jamie Madill2e295e22015-04-29 10:41:33 -04001437 ensureStructDefined(nodeType);
1438
Jamie Madill033dae62014-06-18 12:56:28 -04001439 out << DecorateUniform(name, nodeType);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001440 }
Jamie Madill19571812013-08-12 15:26:34 -07001441 else if (qualifier == EvqAttribute || qualifier == EvqVertexIn)
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001442 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001443 mReferencedAttributes[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001444 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001445 }
Jamie Madill033dae62014-06-18 12:56:28 -04001446 else if (IsVarying(qualifier))
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001447 {
daniel@transgaming.com8803b852012-12-20 21:11:47 +00001448 mReferencedVaryings[name] = node;
Jamie Madill033dae62014-06-18 12:56:28 -04001449 out << Decorate(name);
daniel@transgaming.com86f7c9d2010-04-20 18:52:06 +00001450 }
Jamie Madill19571812013-08-12 15:26:34 -07001451 else if (qualifier == EvqFragmentOut)
Jamie Madill46131a32013-06-20 11:55:50 -04001452 {
1453 mReferencedOutputVariables[name] = node;
1454 out << "out_" << name;
1455 }
1456 else if (qualifier == EvqFragColor)
shannon.woods%transgaming.com@gtempaccount.come7d4a242013-04-13 03:38:33 +00001457 {
1458 out << "gl_Color[0]";
1459 mUsesFragColor = true;
1460 }
1461 else if (qualifier == EvqFragData)
1462 {
1463 out << "gl_Color";
1464 mUsesFragData = true;
1465 }
1466 else if (qualifier == EvqFragCoord)
1467 {
1468 mUsesFragCoord = true;
1469 out << name;
1470 }
1471 else if (qualifier == EvqPointCoord)
1472 {
1473 mUsesPointCoord = true;
1474 out << name;
1475 }
1476 else if (qualifier == EvqFrontFacing)
1477 {
1478 mUsesFrontFacing = true;
1479 out << name;
1480 }
1481 else if (qualifier == EvqPointSize)
1482 {
1483 mUsesPointSize = true;
1484 out << name;
1485 }
Gregoire Payen de La Garanderieb3dced22015-01-12 14:54:55 +00001486 else if (qualifier == EvqInstanceID)
1487 {
1488 mUsesInstanceID = true;
1489 out << name;
1490 }
Kimmo Kinnunen5f0246c2015-07-22 10:30:35 +03001491 else if (name == "gl_FragDepthEXT" || name == "gl_FragDepth")
Jamie Madill2aeb26a2013-07-08 14:02:55 -04001492 {
1493 mUsesFragDepth = true;
1494 out << "gl_Depth";
1495 }
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001496 else
1497 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03001498 out << DecorateIfNeeded(node->getName());
daniel@transgaming.comc72c6412011-09-20 16:09:17 +00001499 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001500 }
1501}
1502
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001503void OutputHLSL::visitRaw(TIntermRaw *node)
1504{
Jamie Madill32aab012015-01-27 14:12:26 -05001505 getInfoSink() << node->getRawText();
Jamie Madill4cfb1e82014-07-07 12:49:23 -04001506}
1507
Olli Etuaho7fb49552015-03-18 17:27:44 +02001508void OutputHLSL::outputEqual(Visit visit, const TType &type, TOperator op, TInfoSinkBase &out)
1509{
1510 if (type.isScalar() && !type.isArray())
1511 {
1512 if (op == EOpEqual)
1513 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001514 outputTriplet(out, visit, "(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001515 }
1516 else
1517 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001518 outputTriplet(out, visit, "(", " != ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001519 }
1520 }
1521 else
1522 {
1523 if (visit == PreVisit && op == EOpNotEqual)
1524 {
1525 out << "!";
1526 }
1527
1528 if (type.isArray())
1529 {
1530 const TString &functionName = addArrayEqualityFunction(type);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001531 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001532 }
1533 else if (type.getBasicType() == EbtStruct)
1534 {
1535 const TStructure &structure = *type.getStruct();
1536 const TString &functionName = addStructEqualityFunction(structure);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001537 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001538 }
1539 else
1540 {
1541 ASSERT(type.isMatrix() || type.isVector());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001542 outputTriplet(out, visit, "all(", " == ", ")");
Olli Etuaho7fb49552015-03-18 17:27:44 +02001543 }
1544 }
1545}
1546
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001547bool OutputHLSL::visitBinary(Visit visit, TIntermBinary *node)
1548{
Jamie Madill32aab012015-01-27 14:12:26 -05001549 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001550
Jamie Madill570e04d2013-06-21 09:15:33 -04001551 // Handle accessing std140 structs by value
1552 if (mFlaggedStructMappedNames.count(node) > 0)
1553 {
1554 out << mFlaggedStructMappedNames[node];
1555 return false;
1556 }
1557
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001558 switch (node->getOp())
1559 {
Olli Etuahoe79904c2015-03-18 16:56:42 +02001560 case EOpAssign:
1561 if (node->getLeft()->isArray())
1562 {
Olli Etuaho9638c352015-04-01 14:34:52 +03001563 TIntermAggregate *rightAgg = node->getRight()->getAsAggregate();
1564 if (rightAgg != nullptr && rightAgg->isConstructor())
1565 {
1566 const TString &functionName = addArrayConstructIntoFunction(node->getType());
1567 out << functionName << "(";
1568 node->getLeft()->traverse(this);
1569 TIntermSequence *seq = rightAgg->getSequence();
1570 for (auto &arrayElement : *seq)
1571 {
1572 out << ", ";
1573 arrayElement->traverse(this);
1574 }
1575 out << ")";
1576 return false;
1577 }
Olli Etuahoa8c414b2015-04-16 15:51:03 +03001578 // ArrayReturnValueToOutParameter should have eliminated expressions where a function call is assigned.
1579 ASSERT(rightAgg == nullptr || rightAgg->getOp() != EOpFunctionCall);
1580
1581 const TString &functionName = addArrayAssignmentFunction(node->getType());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001582 outputTriplet(out, visit, (functionName + "(").c_str(), ", ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001583 }
1584 else
1585 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001586 outputTriplet(out, visit, "(", " = ", ")");
Olli Etuahoe79904c2015-03-18 16:56:42 +02001587 }
1588 break;
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001589 case EOpInitialize:
1590 if (visit == PreVisit)
1591 {
1592 // GLSL allows to write things like "float x = x;" where a new variable x is defined
1593 // and the value of an existing variable x is assigned. HLSL uses C semantics (the
1594 // new variable is created before the assignment is evaluated), so we need to convert
1595 // this to "float t = x, x = t;".
1596
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001597 TIntermSymbol *symbolNode = node->getLeft()->getAsSymbolNode();
Jamie Madill37997142015-01-28 10:06:34 -05001598 ASSERT(symbolNode);
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001599 TIntermTyped *expression = node->getRight();
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001600
Jamie Madill37997142015-01-28 10:06:34 -05001601 // TODO (jmadill): do a 'deep' scan to know if an expression is statically const
1602 if (symbolNode->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst)
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001603 {
Jamie Madill37997142015-01-28 10:06:34 -05001604 // For variables which are not constant, defer their real initialization until
Olli Etuahod81ed842015-05-12 12:46:35 +03001605 // after we initialize uniforms.
1606 TIntermBinary *deferredInit = new TIntermBinary(EOpAssign);
1607 deferredInit->setLeft(node->getLeft());
1608 deferredInit->setRight(node->getRight());
1609 deferredInit->setType(node->getType());
1610 mDeferredGlobalInitializers.push_back(deferredInit);
Jamie Madill37997142015-01-28 10:06:34 -05001611 const TString &initString = initializer(node->getType());
1612 node->setRight(new TIntermRaw(node->getType(), initString));
1613 }
1614 else if (writeSameSymbolInitializer(out, symbolNode, expression))
1615 {
1616 // Skip initializing the rest of the expression
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001617 return false;
1618 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02001619 else if (writeConstantInitialization(out, symbolNode, expression))
1620 {
1621 return false;
1622 }
daniel@transgaming.combdfb2e52010-11-15 16:41:20 +00001623 }
1624 else if (visit == InVisit)
1625 {
1626 out << " = ";
daniel@transgaming.comb6ef8f12010-11-15 16:41:14 +00001627 }
1628 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001629 case EOpAddAssign:
1630 outputTriplet(out, visit, "(", " += ", ")");
1631 break;
1632 case EOpSubAssign:
1633 outputTriplet(out, visit, "(", " -= ", ")");
1634 break;
1635 case EOpMulAssign:
1636 outputTriplet(out, visit, "(", " *= ", ")");
1637 break;
1638 case EOpVectorTimesScalarAssign:
1639 outputTriplet(out, visit, "(", " *= ", ")");
1640 break;
1641 case EOpMatrixTimesScalarAssign:
1642 outputTriplet(out, visit, "(", " *= ", ")");
1643 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001644 case EOpVectorTimesMatrixAssign:
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001645 if (visit == PreVisit)
1646 {
1647 out << "(";
1648 }
1649 else if (visit == InVisit)
1650 {
1651 out << " = mul(";
1652 node->getLeft()->traverse(this);
Jamie Madillf91ce812014-06-13 10:04:34 -04001653 out << ", transpose(";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001654 }
1655 else
1656 {
daniel@transgaming.com3aa74202010-04-29 03:39:04 +00001657 out << ")))";
daniel@transgaming.com8976c1e2010-04-13 19:53:27 +00001658 }
1659 break;
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001660 case EOpMatrixTimesMatrixAssign:
1661 if (visit == PreVisit)
1662 {
1663 out << "(";
1664 }
1665 else if (visit == InVisit)
1666 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001667 out << " = transpose(mul(transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001668 node->getLeft()->traverse(this);
Olli Etuahofc7fab72015-03-06 12:03:18 +02001669 out << "), transpose(";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001670 }
1671 else
1672 {
Olli Etuahofc7fab72015-03-06 12:03:18 +02001673 out << "))))";
daniel@transgaming.com93a96c32010-03-28 19:36:13 +00001674 }
1675 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001676 case EOpDivAssign:
1677 outputTriplet(out, visit, "(", " /= ", ")");
1678 break;
1679 case EOpIModAssign:
1680 outputTriplet(out, visit, "(", " %= ", ")");
1681 break;
1682 case EOpBitShiftLeftAssign:
1683 outputTriplet(out, visit, "(", " <<= ", ")");
1684 break;
1685 case EOpBitShiftRightAssign:
1686 outputTriplet(out, visit, "(", " >>= ", ")");
1687 break;
1688 case EOpBitwiseAndAssign:
1689 outputTriplet(out, visit, "(", " &= ", ")");
1690 break;
1691 case EOpBitwiseXorAssign:
1692 outputTriplet(out, visit, "(", " ^= ", ")");
1693 break;
1694 case EOpBitwiseOrAssign:
1695 outputTriplet(out, visit, "(", " |= ", ")");
1696 break;
Jamie Madillb4e664b2013-06-20 11:55:54 -04001697 case EOpIndexDirect:
Jamie Madillb4e664b2013-06-20 11:55:54 -04001698 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001699 const TType& leftType = node->getLeft()->getType();
1700 if (leftType.isInterfaceBlock())
Jamie Madillb4e664b2013-06-20 11:55:54 -04001701 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001702 if (visit == PreVisit)
1703 {
1704 TInterfaceBlock* interfaceBlock = leftType.getInterfaceBlock();
1705 const int arrayIndex = node->getRight()->getAsConstantUnion()->getIConst(0);
Jamie Madill98493dd2013-07-08 14:39:03 -04001706 mReferencedInterfaceBlocks[interfaceBlock->instanceName()] = node->getLeft()->getAsSymbolNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04001707 out << mUniformHLSL->interfaceBlockInstanceString(*interfaceBlock, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04001708 return false;
1709 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001710 }
Jamie Madill98493dd2013-07-08 14:39:03 -04001711 else
1712 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001713 outputTriplet(out, visit, "", "[", "]");
Jamie Madill98493dd2013-07-08 14:39:03 -04001714 }
Jamie Madillb4e664b2013-06-20 11:55:54 -04001715 }
1716 break;
1717 case EOpIndexIndirect:
1718 // We do not currently support indirect references to interface blocks
1719 ASSERT(node->getLeft()->getBasicType() != EbtInterfaceBlock);
Jamie Madill8c46ab12015-12-07 16:39:19 -05001720 outputTriplet(out, visit, "", "[", "]");
Jamie Madillb4e664b2013-06-20 11:55:54 -04001721 break;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001722 case EOpIndexDirectStruct:
Jamie Madill98493dd2013-07-08 14:39:03 -04001723 if (visit == InVisit)
1724 {
1725 const TStructure* structure = node->getLeft()->getType().getStruct();
1726 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1727 const TField* field = structure->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001728 out << "." + DecorateField(field->name(), *structure);
Jamie Madill98493dd2013-07-08 14:39:03 -04001729
1730 return false;
1731 }
1732 break;
shannonwoods@chromium.org4430b0d2013-05-30 00:12:34 +00001733 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001734 if (visit == InVisit)
1735 {
Jamie Madill98493dd2013-07-08 14:39:03 -04001736 const TInterfaceBlock* interfaceBlock = node->getLeft()->getType().getInterfaceBlock();
1737 const TIntermConstantUnion* index = node->getRight()->getAsConstantUnion();
1738 const TField* field = interfaceBlock->fields()[index->getIConst(0)];
Jamie Madill033dae62014-06-18 12:56:28 -04001739 out << "." + Decorate(field->name());
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00001740
1741 return false;
1742 }
1743 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001744 case EOpVectorSwizzle:
1745 if (visit == InVisit)
1746 {
1747 out << ".";
1748
1749 TIntermAggregate *swizzle = node->getRight()->getAsAggregate();
1750
1751 if (swizzle)
1752 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001753 TIntermSequence *sequence = swizzle->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001754
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001755 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001756 {
1757 TIntermConstantUnion *element = (*sit)->getAsConstantUnion();
1758
1759 if (element)
1760 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00001761 int i = element->getIConst(0);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
1763 switch (i)
1764 {
1765 case 0: out << "x"; break;
1766 case 1: out << "y"; break;
1767 case 2: out << "z"; break;
1768 case 3: out << "w"; break;
1769 default: UNREACHABLE();
1770 }
1771 }
1772 else UNREACHABLE();
1773 }
1774 }
1775 else UNREACHABLE();
1776
1777 return false; // Fully processed
1778 }
1779 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001780 case EOpAdd:
1781 outputTriplet(out, visit, "(", " + ", ")");
1782 break;
1783 case EOpSub:
1784 outputTriplet(out, visit, "(", " - ", ")");
1785 break;
1786 case EOpMul:
1787 outputTriplet(out, visit, "(", " * ", ")");
1788 break;
1789 case EOpDiv:
1790 outputTriplet(out, visit, "(", " / ", ")");
1791 break;
1792 case EOpIMod:
1793 outputTriplet(out, visit, "(", " % ", ")");
1794 break;
1795 case EOpBitShiftLeft:
1796 outputTriplet(out, visit, "(", " << ", ")");
1797 break;
1798 case EOpBitShiftRight:
1799 outputTriplet(out, visit, "(", " >> ", ")");
1800 break;
1801 case EOpBitwiseAnd:
1802 outputTriplet(out, visit, "(", " & ", ")");
1803 break;
1804 case EOpBitwiseXor:
1805 outputTriplet(out, visit, "(", " ^ ", ")");
1806 break;
1807 case EOpBitwiseOr:
1808 outputTriplet(out, visit, "(", " | ", ")");
1809 break;
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001810 case EOpEqual:
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001811 case EOpNotEqual:
Olli Etuaho7fb49552015-03-18 17:27:44 +02001812 outputEqual(visit, node->getLeft()->getType(), node->getOp(), out);
daniel@transgaming.com49bce7e2010-03-17 03:58:51 +00001813 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001814 case EOpLessThan:
1815 outputTriplet(out, visit, "(", " < ", ")");
1816 break;
1817 case EOpGreaterThan:
1818 outputTriplet(out, visit, "(", " > ", ")");
1819 break;
1820 case EOpLessThanEqual:
1821 outputTriplet(out, visit, "(", " <= ", ")");
1822 break;
1823 case EOpGreaterThanEqual:
1824 outputTriplet(out, visit, "(", " >= ", ")");
1825 break;
1826 case EOpVectorTimesScalar:
1827 outputTriplet(out, visit, "(", " * ", ")");
1828 break;
1829 case EOpMatrixTimesScalar:
1830 outputTriplet(out, visit, "(", " * ", ")");
1831 break;
1832 case EOpVectorTimesMatrix:
1833 outputTriplet(out, visit, "mul(", ", transpose(", "))");
1834 break;
1835 case EOpMatrixTimesVector:
1836 outputTriplet(out, visit, "mul(transpose(", "), ", ")");
1837 break;
1838 case EOpMatrixTimesMatrix:
1839 outputTriplet(out, visit, "transpose(mul(transpose(", "), transpose(", ")))");
1840 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001841 case EOpLogicalOr:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001842 // HLSL doesn't short-circuit ||, so we assume that || affected by short-circuiting have been unfolded.
1843 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001844 outputTriplet(out, visit, "(", " || ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001845 return true;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001846 case EOpLogicalXor:
1847 mUsesXor = true;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001848 outputTriplet(out, visit, "xor(", ", ", ")");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00001849 break;
daniel@transgaming.com8915eba2012-04-28 00:34:44 +00001850 case EOpLogicalAnd:
Olli Etuahoa6f22092015-05-08 18:31:10 +03001851 // HLSL doesn't short-circuit &&, so we assume that && affected by short-circuiting have been unfolded.
1852 ASSERT(!node->getRight()->hasSideEffects());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001853 outputTriplet(out, visit, "(", " && ", ")");
Olli Etuahoa6f22092015-05-08 18:31:10 +03001854 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855 default: UNREACHABLE();
1856 }
1857
1858 return true;
1859}
1860
1861bool OutputHLSL::visitUnary(Visit visit, TIntermUnary *node)
1862{
Jamie Madill8c46ab12015-12-07 16:39:19 -05001863 TInfoSinkBase &out = getInfoSink();
1864
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001865 switch (node->getOp())
1866 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05001867 case EOpNegative:
1868 outputTriplet(out, visit, "(-", "", ")");
1869 break;
1870 case EOpPositive:
1871 outputTriplet(out, visit, "(+", "", ")");
1872 break;
1873 case EOpVectorLogicalNot:
1874 outputTriplet(out, visit, "(!", "", ")");
1875 break;
1876 case EOpLogicalNot:
1877 outputTriplet(out, visit, "(!", "", ")");
1878 break;
1879 case EOpBitwiseNot:
1880 outputTriplet(out, visit, "(~", "", ")");
1881 break;
1882 case EOpPostIncrement:
1883 outputTriplet(out, visit, "(", "", "++)");
1884 break;
1885 case EOpPostDecrement:
1886 outputTriplet(out, visit, "(", "", "--)");
1887 break;
1888 case EOpPreIncrement:
1889 outputTriplet(out, visit, "(++", "", ")");
1890 break;
1891 case EOpPreDecrement:
1892 outputTriplet(out, visit, "(--", "", ")");
1893 break;
1894 case EOpRadians:
1895 outputTriplet(out, visit, "radians(", "", ")");
1896 break;
1897 case EOpDegrees:
1898 outputTriplet(out, visit, "degrees(", "", ")");
1899 break;
1900 case EOpSin:
1901 outputTriplet(out, visit, "sin(", "", ")");
1902 break;
1903 case EOpCos:
1904 outputTriplet(out, visit, "cos(", "", ")");
1905 break;
1906 case EOpTan:
1907 outputTriplet(out, visit, "tan(", "", ")");
1908 break;
1909 case EOpAsin:
1910 outputTriplet(out, visit, "asin(", "", ")");
1911 break;
1912 case EOpAcos:
1913 outputTriplet(out, visit, "acos(", "", ")");
1914 break;
1915 case EOpAtan:
1916 outputTriplet(out, visit, "atan(", "", ")");
1917 break;
1918 case EOpSinh:
1919 outputTriplet(out, visit, "sinh(", "", ")");
1920 break;
1921 case EOpCosh:
1922 outputTriplet(out, visit, "cosh(", "", ")");
1923 break;
1924 case EOpTanh:
1925 outputTriplet(out, visit, "tanh(", "", ")");
1926 break;
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001927 case EOpAsinh:
1928 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001929 writeEmulatedFunctionTriplet(out, visit, "asinh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001930 break;
1931 case EOpAcosh:
1932 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001933 writeEmulatedFunctionTriplet(out, visit, "acosh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001934 break;
1935 case EOpAtanh:
1936 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001937 writeEmulatedFunctionTriplet(out, visit, "atanh(");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02001938 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001939 case EOpExp:
1940 outputTriplet(out, visit, "exp(", "", ")");
1941 break;
1942 case EOpLog:
1943 outputTriplet(out, visit, "log(", "", ")");
1944 break;
1945 case EOpExp2:
1946 outputTriplet(out, visit, "exp2(", "", ")");
1947 break;
1948 case EOpLog2:
1949 outputTriplet(out, visit, "log2(", "", ")");
1950 break;
1951 case EOpSqrt:
1952 outputTriplet(out, visit, "sqrt(", "", ")");
1953 break;
1954 case EOpInverseSqrt:
1955 outputTriplet(out, visit, "rsqrt(", "", ")");
1956 break;
1957 case EOpAbs:
1958 outputTriplet(out, visit, "abs(", "", ")");
1959 break;
1960 case EOpSign:
1961 outputTriplet(out, visit, "sign(", "", ")");
1962 break;
1963 case EOpFloor:
1964 outputTriplet(out, visit, "floor(", "", ")");
1965 break;
1966 case EOpTrunc:
1967 outputTriplet(out, visit, "trunc(", "", ")");
1968 break;
1969 case EOpRound:
1970 outputTriplet(out, visit, "round(", "", ")");
1971 break;
Qingqing Deng5dbece52015-02-27 20:35:38 -08001972 case EOpRoundEven:
1973 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05001974 writeEmulatedFunctionTriplet(out, visit, "roundEven(");
Qingqing Deng5dbece52015-02-27 20:35:38 -08001975 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001976 case EOpCeil:
1977 outputTriplet(out, visit, "ceil(", "", ")");
1978 break;
1979 case EOpFract:
1980 outputTriplet(out, visit, "frac(", "", ")");
1981 break;
Arun Patole44efa0b2015-03-04 17:11:05 +05301982 case EOpIsNan:
Jamie Madill8c46ab12015-12-07 16:39:19 -05001983 outputTriplet(out, visit, "isnan(", "", ")");
Arun Patole44efa0b2015-03-04 17:11:05 +05301984 mRequiresIEEEStrictCompiling = true;
1985 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05001986 case EOpIsInf:
1987 outputTriplet(out, visit, "isinf(", "", ")");
1988 break;
1989 case EOpFloatBitsToInt:
1990 outputTriplet(out, visit, "asint(", "", ")");
1991 break;
1992 case EOpFloatBitsToUint:
1993 outputTriplet(out, visit, "asuint(", "", ")");
1994 break;
1995 case EOpIntBitsToFloat:
1996 outputTriplet(out, visit, "asfloat(", "", ")");
1997 break;
1998 case EOpUintBitsToFloat:
1999 outputTriplet(out, visit, "asfloat(", "", ")");
2000 break;
Olli Etuaho7700ff62015-01-15 12:16:29 +02002001 case EOpPackSnorm2x16:
2002 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002003 writeEmulatedFunctionTriplet(out, visit, "packSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002004 break;
2005 case EOpPackUnorm2x16:
2006 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002007 writeEmulatedFunctionTriplet(out, visit, "packUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002008 break;
2009 case EOpPackHalf2x16:
2010 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002011 writeEmulatedFunctionTriplet(out, visit, "packHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002012 break;
2013 case EOpUnpackSnorm2x16:
2014 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002015 writeEmulatedFunctionTriplet(out, visit, "unpackSnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002016 break;
2017 case EOpUnpackUnorm2x16:
2018 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002019 writeEmulatedFunctionTriplet(out, visit, "unpackUnorm2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002020 break;
2021 case EOpUnpackHalf2x16:
2022 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002023 writeEmulatedFunctionTriplet(out, visit, "unpackHalf2x16(");
Olli Etuaho7700ff62015-01-15 12:16:29 +02002024 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002025 case EOpLength:
2026 outputTriplet(out, visit, "length(", "", ")");
2027 break;
2028 case EOpNormalize:
2029 outputTriplet(out, visit, "normalize(", "", ")");
2030 break;
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002031 case EOpDFdx:
2032 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2033 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002034 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002035 }
2036 else
2037 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002038 outputTriplet(out, visit, "ddx(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002039 }
2040 break;
2041 case EOpDFdy:
2042 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2043 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002044 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002045 }
2046 else
2047 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002048 outputTriplet(out, visit, "ddy(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002049 }
2050 break;
2051 case EOpFwidth:
2052 if(mInsideDiscontinuousLoop || mOutputLod0Function)
2053 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002054 outputTriplet(out, visit, "(", "", ", 0.0)");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002055 }
2056 else
2057 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002058 outputTriplet(out, visit, "fwidth(", "", ")");
daniel@transgaming.comddbb45d2012-05-31 01:21:41 +00002059 }
2060 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002061 case EOpTranspose:
2062 outputTriplet(out, visit, "transpose(", "", ")");
2063 break;
2064 case EOpDeterminant:
2065 outputTriplet(out, visit, "determinant(transpose(", "", "))");
2066 break;
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002067 case EOpInverse:
2068 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002069 writeEmulatedFunctionTriplet(out, visit, "inverse(");
Olli Etuahoabf6dad2015-01-14 14:45:16 +02002070 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002071
Jamie Madill8c46ab12015-12-07 16:39:19 -05002072 case EOpAny:
2073 outputTriplet(out, visit, "any(", "", ")");
2074 break;
2075 case EOpAll:
2076 outputTriplet(out, visit, "all(", "", ")");
2077 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078 default: UNREACHABLE();
2079 }
2080
2081 return true;
2082}
2083
2084bool OutputHLSL::visitAggregate(Visit visit, TIntermAggregate *node)
2085{
Jamie Madill32aab012015-01-27 14:12:26 -05002086 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002088 switch (node->getOp())
2089 {
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002090 case EOpSequence:
2091 {
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002092 if (mInsideFunction)
2093 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002094 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002095 out << "{\n";
2096 }
2097
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002098 for (TIntermSequence::iterator sit = node->getSequence()->begin(); sit != node->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002099 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002100 outputLineDirective(out, (*sit)->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002101
Olli Etuahoa6f22092015-05-08 18:31:10 +03002102 (*sit)->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002103
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002104 // Don't output ; after case labels, they're terminated by :
2105 // This is needed especially since outputting a ; after a case statement would turn empty
2106 // case statements into non-empty case statements, disallowing fall-through from them.
Olli Etuaho4785fec2015-05-18 16:09:37 +03002107 // Also no need to output ; after selection (if) statements or sequences. This is done just
2108 // for code clarity.
Olli Etuahoa6f22092015-05-08 18:31:10 +03002109 TIntermSelection *asSelection = (*sit)->getAsSelectionNode();
2110 ASSERT(asSelection == nullptr || !asSelection->usesTernaryOperator());
Olli Etuaho4785fec2015-05-18 16:09:37 +03002111 if ((*sit)->getAsCaseNode() == nullptr && asSelection == nullptr && !IsSequence(*sit))
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002112 out << ";\n";
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002113 }
2114
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002115 if (mInsideFunction)
2116 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002117 outputLineDirective(out, node->getLine().last_line);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002118 out << "}\n";
2119 }
2120
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002121 return false;
2122 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123 case EOpDeclaration:
2124 if (visit == PreVisit)
2125 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002126 TIntermSequence *sequence = node->getSequence();
2127 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
Olli Etuahoa6f22092015-05-08 18:31:10 +03002128 ASSERT(sequence->size() == 1);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002130 if (variable &&
2131 (variable->getQualifier() == EvqTemporary ||
2132 variable->getQualifier() == EvqGlobal || variable->getQualifier() == EvqConst))
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002133 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002134 ensureStructDefined(variable->getType());
daniel@transgaming.comead23042010-04-29 03:35:36 +00002135
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002136 if (!variable->getAsSymbolNode() || variable->getAsSymbolNode()->getSymbol() != "") // Variable declaration
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002138 if (!mInsideFunction)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139 {
Olli Etuahoa6f22092015-05-08 18:31:10 +03002140 out << "static ";
2141 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002142
Olli Etuahoa6f22092015-05-08 18:31:10 +03002143 out << TypeString(variable->getType()) + " ";
Nicolas Capensd974db42014-10-07 10:50:19 -04002144
Olli Etuahoa6f22092015-05-08 18:31:10 +03002145 TIntermSymbol *symbol = variable->getAsSymbolNode();
Nicolas Capensd974db42014-10-07 10:50:19 -04002146
Olli Etuahoa6f22092015-05-08 18:31:10 +03002147 if (symbol)
2148 {
2149 symbol->traverse(this);
2150 out << ArrayString(symbol->getType());
2151 out << " = " + initializer(symbol->getType());
2152 }
2153 else
2154 {
2155 variable->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002157 }
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002158 else if (variable->getAsSymbolNode() && variable->getAsSymbolNode()->getSymbol() == "") // Type (struct) declaration
2159 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00002160 // Already added to constructor map
daniel@transgaming.comfe565152010-04-10 05:29:07 +00002161 }
2162 else UNREACHABLE();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002163 }
Jamie Madill033dae62014-06-18 12:56:28 -04002164 else if (variable && IsVaryingOut(variable->getQualifier()))
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002165 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002166 for (TIntermSequence::iterator sit = sequence->begin(); sit != sequence->end(); sit++)
shannon.woods@transgaming.comcb332ab2013-02-28 23:12:18 +00002167 {
2168 TIntermSymbol *symbol = (*sit)->getAsSymbolNode();
2169
2170 if (symbol)
2171 {
2172 // Vertex (output) varyings which are declared but not written to should still be declared to allow successful linking
2173 mReferencedVaryings[symbol->getSymbol()] = symbol;
2174 }
2175 else
2176 {
2177 (*sit)->traverse(this);
2178 }
2179 }
2180 }
2181
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002182 return false;
2183 }
2184 else if (visit == InVisit)
2185 {
2186 out << ", ";
2187 }
2188 break;
Jamie Madill3b5c2da2014-08-19 15:23:32 -04002189 case EOpInvariantDeclaration:
2190 // Do not do any translation
2191 return false;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002192 case EOpPrototype:
2193 if (visit == PreVisit)
2194 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002195 size_t index = mCallDag.findIndex(node);
2196 // Skip the prototype if it is not implemented (and thus not used)
2197 if (index == CallDAG::InvalidIndex)
2198 {
2199 return false;
2200 }
2201
Olli Etuaho59f9a642015-08-06 20:38:26 +03002202 TString name = DecorateFunctionIfNeeded(node->getNameObj());
2203 out << TypeString(node->getType()) << " " << name
2204 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002205
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002206 TIntermSequence *arguments = node->getSequence();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002207
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002208 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002209 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002210 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002211
2212 if (symbol)
2213 {
2214 out << argumentString(symbol);
2215
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002216 if (i < arguments->size() - 1)
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002217 {
2218 out << ", ";
2219 }
2220 }
2221 else UNREACHABLE();
2222 }
2223
2224 out << ");\n";
2225
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002226 // Also prototype the Lod0 variant if needed
Corentin Wallez1239ee92015-03-19 14:38:02 -07002227 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2228 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com0e5bb402012-10-17 18:24:53 +00002229 {
2230 mOutputLod0Function = true;
2231 node->traverse(this);
2232 mOutputLod0Function = false;
2233 }
2234
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00002235 return false;
2236 }
2237 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002238 case EOpComma:
2239 outputTriplet(out, visit, "(", ", ", ")");
2240 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002241 case EOpFunction:
2242 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002243 ASSERT(mCurrentFunctionMetadata == nullptr);
Olli Etuaho59f9a642015-08-06 20:38:26 +03002244 TString name = TFunction::unmangleName(node->getNameObj().getString());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002245
Corentin Wallez1239ee92015-03-19 14:38:02 -07002246 size_t index = mCallDag.findIndex(node);
2247 ASSERT(index != CallDAG::InvalidIndex);
2248 mCurrentFunctionMetadata = &mASTMetadataList[index];
2249
Jamie Madill033dae62014-06-18 12:56:28 -04002250 out << TypeString(node->getType()) << " ";
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002251
2252 if (name == "main")
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002253 {
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002254 out << "gl_main(";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002256 else
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002257 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002258 out << DecorateFunctionIfNeeded(node->getNameObj())
2259 << (mOutputLod0Function ? "Lod0(" : "(");
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002260 }
daniel@transgaming.com63691862010-04-29 03:32:42 +00002261
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002262 TIntermSequence *sequence = node->getSequence();
2263 TIntermSequence *arguments = (*sequence)[0]->getAsAggregate()->getSequence();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002264
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002265 for (unsigned int i = 0; i < arguments->size(); i++)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002266 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002267 TIntermSymbol *symbol = (*arguments)[i]->getAsSymbolNode();
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002268
2269 if (symbol)
2270 {
Jamie Madill2e295e22015-04-29 10:41:33 -04002271 ensureStructDefined(symbol->getType());
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002272
2273 out << argumentString(symbol);
2274
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002275 if (i < arguments->size() - 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002276 {
2277 out << ", ";
2278 }
2279 }
2280 else UNREACHABLE();
2281 }
2282
Olli Etuaho4785fec2015-05-18 16:09:37 +03002283 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002284
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002285 if (sequence->size() > 1)
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002286 {
2287 mInsideFunction = true;
Olli Etuaho4785fec2015-05-18 16:09:37 +03002288 TIntermNode *body = (*sequence)[1];
2289 // The function body node will output braces.
2290 ASSERT(IsSequence(body));
2291 body->traverse(this);
daniel@transgaming.comf9ef1072010-04-22 13:35:16 +00002292 mInsideFunction = false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002293 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002294 else
2295 {
2296 out << "{}\n";
2297 }
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002298
Corentin Wallez1239ee92015-03-19 14:38:02 -07002299 mCurrentFunctionMetadata = nullptr;
2300
2301 bool needsLod0 = mASTMetadataList[index].mNeedsLod0;
2302 if (needsLod0 && !mOutputLod0Function && mShaderType == GL_FRAGMENT_SHADER)
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002303 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002304 ASSERT(name != "main");
2305 mOutputLod0Function = true;
2306 node->traverse(this);
2307 mOutputLod0Function = false;
daniel@transgaming.com89431aa2012-05-31 01:20:29 +00002308 }
2309
daniel@transgaming.com0e9704b2012-05-31 01:20:13 +00002310 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002311 }
2312 break;
2313 case EOpFunctionCall:
2314 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002315 TIntermSequence *arguments = node->getSequence();
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002316
Corentin Wallez1239ee92015-03-19 14:38:02 -07002317 bool lod0 = mInsideDiscontinuousLoop || mOutputLod0Function;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002318 if (node->isUserDefined())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319 {
Olli Etuahoa8c414b2015-04-16 15:51:03 +03002320 if (node->isArray())
2321 {
2322 UNIMPLEMENTED();
2323 }
Corentin Wallez1239ee92015-03-19 14:38:02 -07002324 size_t index = mCallDag.findIndex(node);
2325 ASSERT(index != CallDAG::InvalidIndex);
2326 lod0 &= mASTMetadataList[index].mNeedsLod0;
2327
Olli Etuaho59f9a642015-08-06 20:38:26 +03002328 out << DecorateFunctionIfNeeded(node->getNameObj()) << (lod0 ? "Lod0(" : "(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329 }
2330 else
2331 {
Olli Etuaho59f9a642015-08-06 20:38:26 +03002332 TString name = TFunction::unmangleName(node->getNameObj().getString());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002333 TBasicType samplerType = (*arguments)[0]->getAsTyped()->getType().getBasicType();
shannonwoods@chromium.orgc6ac65f2013-05-30 00:02:50 +00002334
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002335 TextureFunction textureFunction;
2336 textureFunction.sampler = samplerType;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002337 textureFunction.coords = (*arguments)[1]->getAsTyped()->getNominalSize();
Nicolas Capens75fb4752013-07-10 15:14:47 -04002338 textureFunction.method = TextureFunction::IMPLICIT;
2339 textureFunction.proj = false;
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002340 textureFunction.offset = false;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002341
2342 if (name == "texture2D" || name == "textureCube" || name == "texture")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002343 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002344 textureFunction.method = TextureFunction::IMPLICIT;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002345 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002346 else if (name == "texture2DProj" || name == "textureProj")
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002347 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002348 textureFunction.method = TextureFunction::IMPLICIT;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002349 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002350 }
Nicolas Capens46485082014-04-15 13:12:50 -04002351 else if (name == "texture2DLod" || name == "textureCubeLod" || name == "textureLod" ||
2352 name == "texture2DLodEXT" || name == "textureCubeLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002353 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002354 textureFunction.method = TextureFunction::LOD;
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002355 }
Nicolas Capens46485082014-04-15 13:12:50 -04002356 else if (name == "texture2DProjLod" || name == "textureProjLod" || name == "texture2DProjLodEXT")
Nicolas Capens9fe6f982013-06-24 16:05:25 -04002357 {
Nicolas Capens75fb4752013-07-10 15:14:47 -04002358 textureFunction.method = TextureFunction::LOD;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002359 textureFunction.proj = true;
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002360 }
Nicolas Capens75fb4752013-07-10 15:14:47 -04002361 else if (name == "textureSize")
2362 {
2363 textureFunction.method = TextureFunction::SIZE;
2364 }
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002365 else if (name == "textureOffset")
2366 {
2367 textureFunction.method = TextureFunction::IMPLICIT;
2368 textureFunction.offset = true;
2369 }
Nicolas Capensdf86c6b2014-02-14 20:09:17 -05002370 else if (name == "textureProjOffset")
2371 {
2372 textureFunction.method = TextureFunction::IMPLICIT;
2373 textureFunction.offset = true;
2374 textureFunction.proj = true;
2375 }
2376 else if (name == "textureLodOffset")
2377 {
2378 textureFunction.method = TextureFunction::LOD;
2379 textureFunction.offset = true;
2380 }
Nicolas Capens2adc2562014-02-14 23:50:59 -05002381 else if (name == "textureProjLodOffset")
2382 {
2383 textureFunction.method = TextureFunction::LOD;
2384 textureFunction.proj = true;
2385 textureFunction.offset = true;
2386 }
Nicolas Capensfc014542014-02-18 14:47:13 -05002387 else if (name == "texelFetch")
2388 {
2389 textureFunction.method = TextureFunction::FETCH;
2390 }
2391 else if (name == "texelFetchOffset")
2392 {
2393 textureFunction.method = TextureFunction::FETCH;
2394 textureFunction.offset = true;
2395 }
Nicolas Capens46485082014-04-15 13:12:50 -04002396 else if (name == "textureGrad" || name == "texture2DGradEXT")
Nicolas Capensd11d5492014-02-19 17:06:10 -05002397 {
2398 textureFunction.method = TextureFunction::GRAD;
2399 }
Nicolas Capensbf7db102014-02-19 17:20:28 -05002400 else if (name == "textureGradOffset")
2401 {
2402 textureFunction.method = TextureFunction::GRAD;
2403 textureFunction.offset = true;
2404 }
Nicolas Capens46485082014-04-15 13:12:50 -04002405 else if (name == "textureProjGrad" || name == "texture2DProjGradEXT" || name == "textureCubeGradEXT")
Nicolas Capensf7378e32014-02-19 17:29:32 -05002406 {
2407 textureFunction.method = TextureFunction::GRAD;
2408 textureFunction.proj = true;
2409 }
2410 else if (name == "textureProjGradOffset")
2411 {
2412 textureFunction.method = TextureFunction::GRAD;
2413 textureFunction.proj = true;
2414 textureFunction.offset = true;
2415 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002416 else UNREACHABLE();
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002417
Nicolas Capensb1f45b72013-12-19 17:37:19 -05002418 if (textureFunction.method == TextureFunction::IMPLICIT) // Could require lod 0 or have a bias argument
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002419 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002420 unsigned int mandatoryArgumentCount = 2; // All functions have sampler and coordinate arguments
2421
2422 if (textureFunction.offset)
2423 {
2424 mandatoryArgumentCount++;
2425 }
2426
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002427 bool bias = (arguments->size() > mandatoryArgumentCount); // Bias argument is optional
Nicolas Capens84cfa122014-04-14 13:48:45 -04002428
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02002429 if (lod0 || mShaderType == GL_VERTEX_SHADER)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002430 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002431 if (bias)
2432 {
2433 textureFunction.method = TextureFunction::LOD0BIAS;
2434 }
2435 else
2436 {
2437 textureFunction.method = TextureFunction::LOD0;
2438 }
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002439 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002440 else if (bias)
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002441 {
Nicolas Capens84cfa122014-04-14 13:48:45 -04002442 textureFunction.method = TextureFunction::BIAS;
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002443 }
2444 }
2445
2446 mUsesTexture.insert(textureFunction);
Nicolas Capens84cfa122014-04-14 13:48:45 -04002447
Nicolas Capense0ba27a2013-06-24 16:10:52 -04002448 out << textureFunction.name();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002449 }
Nicolas Capens84cfa122014-04-14 13:48:45 -04002450
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002451 for (TIntermSequence::iterator arg = arguments->begin(); arg != arguments->end(); arg++)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002452 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002453 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT &&
2454 IsSampler((*arg)->getAsTyped()->getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002455 {
2456 out << "texture_";
2457 (*arg)->traverse(this);
2458 out << ", sampler_";
2459 }
2460
2461 (*arg)->traverse(this);
2462
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002463 if (arg < arguments->end() - 1)
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00002464 {
2465 out << ", ";
2466 }
2467 }
2468
2469 out << ")";
2470
2471 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472 }
2473 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002474 case EOpParameters:
2475 outputTriplet(out, visit, "(", ", ", ")\n{\n");
2476 break;
2477 case EOpConstructFloat:
2478 outputConstructor(out, visit, node->getType(), "vec1", node->getSequence());
2479 break;
2480 case EOpConstructVec2:
2481 outputConstructor(out, visit, node->getType(), "vec2", node->getSequence());
2482 break;
2483 case EOpConstructVec3:
2484 outputConstructor(out, visit, node->getType(), "vec3", node->getSequence());
2485 break;
2486 case EOpConstructVec4:
2487 outputConstructor(out, visit, node->getType(), "vec4", node->getSequence());
2488 break;
2489 case EOpConstructBool:
2490 outputConstructor(out, visit, node->getType(), "bvec1", node->getSequence());
2491 break;
2492 case EOpConstructBVec2:
2493 outputConstructor(out, visit, node->getType(), "bvec2", node->getSequence());
2494 break;
2495 case EOpConstructBVec3:
2496 outputConstructor(out, visit, node->getType(), "bvec3", node->getSequence());
2497 break;
2498 case EOpConstructBVec4:
2499 outputConstructor(out, visit, node->getType(), "bvec4", node->getSequence());
2500 break;
2501 case EOpConstructInt:
2502 outputConstructor(out, visit, node->getType(), "ivec1", node->getSequence());
2503 break;
2504 case EOpConstructIVec2:
2505 outputConstructor(out, visit, node->getType(), "ivec2", node->getSequence());
2506 break;
2507 case EOpConstructIVec3:
2508 outputConstructor(out, visit, node->getType(), "ivec3", node->getSequence());
2509 break;
2510 case EOpConstructIVec4:
2511 outputConstructor(out, visit, node->getType(), "ivec4", node->getSequence());
2512 break;
2513 case EOpConstructUInt:
2514 outputConstructor(out, visit, node->getType(), "uvec1", node->getSequence());
2515 break;
2516 case EOpConstructUVec2:
2517 outputConstructor(out, visit, node->getType(), "uvec2", node->getSequence());
2518 break;
2519 case EOpConstructUVec3:
2520 outputConstructor(out, visit, node->getType(), "uvec3", node->getSequence());
2521 break;
2522 case EOpConstructUVec4:
2523 outputConstructor(out, visit, node->getType(), "uvec4", node->getSequence());
2524 break;
2525 case EOpConstructMat2:
2526 outputConstructor(out, visit, node->getType(), "mat2", node->getSequence());
2527 break;
2528 case EOpConstructMat2x3:
2529 outputConstructor(out, visit, node->getType(), "mat2x3", node->getSequence());
2530 break;
2531 case EOpConstructMat2x4:
2532 outputConstructor(out, visit, node->getType(), "mat2x4", node->getSequence());
2533 break;
2534 case EOpConstructMat3x2:
2535 outputConstructor(out, visit, node->getType(), "mat3x2", node->getSequence());
2536 break;
2537 case EOpConstructMat3:
2538 outputConstructor(out, visit, node->getType(), "mat3", node->getSequence());
2539 break;
2540 case EOpConstructMat3x4:
2541 outputConstructor(out, visit, node->getType(), "mat3x4", node->getSequence());
2542 break;
2543 case EOpConstructMat4x2:
2544 outputConstructor(out, visit, node->getType(), "mat4x2", node->getSequence());
2545 break;
2546 case EOpConstructMat4x3:
2547 outputConstructor(out, visit, node->getType(), "mat4x3", node->getSequence());
2548 break;
2549 case EOpConstructMat4:
2550 outputConstructor(out, visit, node->getType(), "mat4", node->getSequence());
2551 break;
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002552 case EOpConstructStruct:
Jamie Madillbfa91f42014-06-05 15:45:18 -04002553 {
Olli Etuahof40319e2015-03-10 14:33:00 +02002554 if (node->getType().isArray())
2555 {
2556 UNIMPLEMENTED();
2557 }
Jamie Madill033dae62014-06-18 12:56:28 -04002558 const TString &structName = StructNameString(*node->getType().getStruct());
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002559 mStructureHLSL->addConstructor(node->getType(), structName, node->getSequence());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002560 outputTriplet(out, visit, (structName + "_ctor(").c_str(), ", ", ")");
Jamie Madillbfa91f42014-06-05 15:45:18 -04002561 }
daniel@transgaming.com7a7003c2010-04-29 03:35:33 +00002562 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002563 case EOpLessThan:
2564 outputTriplet(out, visit, "(", " < ", ")");
2565 break;
2566 case EOpGreaterThan:
2567 outputTriplet(out, visit, "(", " > ", ")");
2568 break;
2569 case EOpLessThanEqual:
2570 outputTriplet(out, visit, "(", " <= ", ")");
2571 break;
2572 case EOpGreaterThanEqual:
2573 outputTriplet(out, visit, "(", " >= ", ")");
2574 break;
2575 case EOpVectorEqual:
2576 outputTriplet(out, visit, "(", " == ", ")");
2577 break;
2578 case EOpVectorNotEqual:
2579 outputTriplet(out, visit, "(", " != ", ")");
2580 break;
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002581 case EOpMod:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002582 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002583 writeEmulatedFunctionTriplet(out, visit, "mod(");
daniel@transgaming.comd7c98102010-05-14 17:30:48 +00002584 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002585 case EOpModf:
2586 outputTriplet(out, visit, "modf(", ", ", ")");
2587 break;
2588 case EOpPow:
2589 outputTriplet(out, visit, "pow(", ", ", ")");
2590 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002591 case EOpAtan:
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002592 ASSERT(node->getSequence()->size() == 2); // atan(x) is a unary operator
Olli Etuahoe17e3192015-01-02 12:47:59 +02002593 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002594 writeEmulatedFunctionTriplet(out, visit, "atan(");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002596 case EOpMin:
2597 outputTriplet(out, visit, "min(", ", ", ")");
2598 break;
2599 case EOpMax:
2600 outputTriplet(out, visit, "max(", ", ", ")");
2601 break;
2602 case EOpClamp:
2603 outputTriplet(out, visit, "clamp(", ", ", ")");
2604 break;
Arun Patoled94f6642015-05-18 16:25:12 +05302605 case EOpMix:
2606 {
2607 TIntermTyped *lastParamNode = (*(node->getSequence()))[2]->getAsTyped();
2608 if (lastParamNode->getType().getBasicType() == EbtBool)
2609 {
2610 // There is no HLSL equivalent for ESSL3 built-in "genType mix (genType x, genType y, genBType a)",
2611 // so use emulated version.
2612 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002613 writeEmulatedFunctionTriplet(out, visit, "mix(");
Arun Patoled94f6642015-05-18 16:25:12 +05302614 }
2615 else
2616 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002617 outputTriplet(out, visit, "lerp(", ", ", ")");
Arun Patoled94f6642015-05-18 16:25:12 +05302618 }
2619 }
2620 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002621 case EOpStep:
2622 outputTriplet(out, visit, "step(", ", ", ")");
2623 break;
2624 case EOpSmoothStep:
2625 outputTriplet(out, visit, "smoothstep(", ", ", ")");
2626 break;
2627 case EOpDistance:
2628 outputTriplet(out, visit, "distance(", ", ", ")");
2629 break;
2630 case EOpDot:
2631 outputTriplet(out, visit, "dot(", ", ", ")");
2632 break;
2633 case EOpCross:
2634 outputTriplet(out, visit, "cross(", ", ", ")");
2635 break;
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002636 case EOpFaceForward:
Olli Etuahoe17e3192015-01-02 12:47:59 +02002637 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002638 writeEmulatedFunctionTriplet(out, visit, "faceforward(");
daniel@transgaming.com0bbb0312010-04-26 15:33:39 +00002639 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002640 case EOpReflect:
2641 outputTriplet(out, visit, "reflect(", ", ", ")");
2642 break;
2643 case EOpRefract:
2644 outputTriplet(out, visit, "refract(", ", ", ")");
2645 break;
Olli Etuahoe39706d2014-12-30 16:40:36 +02002646 case EOpOuterProduct:
2647 ASSERT(node->getUseEmulatedFunction());
Jamie Madill8c46ab12015-12-07 16:39:19 -05002648 writeEmulatedFunctionTriplet(out, visit, "outerProduct(");
Olli Etuahoe39706d2014-12-30 16:40:36 +02002649 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002650 case EOpMul:
2651 outputTriplet(out, visit, "(", " * ", ")");
2652 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002653 default: UNREACHABLE();
2654 }
2655
2656 return true;
2657}
2658
Jamie Madill8c46ab12015-12-07 16:39:19 -05002659void OutputHLSL::writeSelection(TInfoSinkBase &out, TIntermSelection *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002660{
Olli Etuahoa6f22092015-05-08 18:31:10 +03002661 out << "if (";
2662
2663 node->getCondition()->traverse(this);
2664
2665 out << ")\n";
2666
Jamie Madill8c46ab12015-12-07 16:39:19 -05002667 outputLineDirective(out, node->getLine().first_line);
Olli Etuahoa6f22092015-05-08 18:31:10 +03002668
2669 bool discard = false;
2670
2671 if (node->getTrueBlock())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002672 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002673 // The trueBlock child node will output braces.
2674 ASSERT(IsSequence(node->getTrueBlock()));
2675
Olli Etuahoa6f22092015-05-08 18:31:10 +03002676 node->getTrueBlock()->traverse(this);
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002677
Olli Etuahoa6f22092015-05-08 18:31:10 +03002678 // Detect true discard
2679 discard = (discard || FindDiscard::search(node->getTrueBlock()));
2680 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002681 else
2682 {
2683 // TODO(oetuaho): Check if the semicolon inside is necessary.
2684 // It's there as a result of conservative refactoring of the output.
2685 out << "{;}\n";
2686 }
Corentin Wallez80bacde2014-11-10 12:07:37 -08002687
Jamie Madill8c46ab12015-12-07 16:39:19 -05002688 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002689
Olli Etuahoa6f22092015-05-08 18:31:10 +03002690 if (node->getFalseBlock())
2691 {
2692 out << "else\n";
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002693
Jamie Madill8c46ab12015-12-07 16:39:19 -05002694 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002695
Olli Etuaho4785fec2015-05-18 16:09:37 +03002696 // Either this is "else if" or the falseBlock child node will output braces.
2697 ASSERT(IsSequence(node->getFalseBlock()) || node->getFalseBlock()->getAsSelectionNode() != nullptr);
2698
Olli Etuahoa6f22092015-05-08 18:31:10 +03002699 node->getFalseBlock()->traverse(this);
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002700
Jamie Madill8c46ab12015-12-07 16:39:19 -05002701 outputLineDirective(out, node->getFalseBlock()->getLine().first_line);
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002702
Olli Etuahoa6f22092015-05-08 18:31:10 +03002703 // Detect false discard
2704 discard = (discard || FindDiscard::search(node->getFalseBlock()));
2705 }
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002706
Olli Etuahoa6f22092015-05-08 18:31:10 +03002707 // ANGLE issue 486: Detect problematic conditional discard
Olli Etuahofd3b9be2015-05-18 17:07:36 +03002708 if (discard)
Olli Etuahoa6f22092015-05-08 18:31:10 +03002709 {
2710 mUsesDiscardRewriting = true;
daniel@transgaming.com3d53fda2010-03-21 04:30:55 +00002711 }
Olli Etuahod81ed842015-05-12 12:46:35 +03002712}
2713
2714bool OutputHLSL::visitSelection(Visit visit, TIntermSelection *node)
2715{
2716 TInfoSinkBase &out = getInfoSink();
2717
2718 ASSERT(!node->usesTernaryOperator());
2719
2720 if (!mInsideFunction)
2721 {
2722 // This is part of unfolded global initialization.
2723 mDeferredGlobalInitializers.push_back(node);
2724 return false;
2725 }
2726
2727 // D3D errors when there is a gradient operation in a loop in an unflattened if.
Corentin Wallez477b2432015-08-31 10:41:16 -07002728 if (mShaderType == GL_FRAGMENT_SHADER && mCurrentFunctionMetadata->hasGradientLoop(node))
Olli Etuahod81ed842015-05-12 12:46:35 +03002729 {
2730 out << "FLATTEN ";
2731 }
2732
Jamie Madill8c46ab12015-12-07 16:39:19 -05002733 writeSelection(out, node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002734
2735 return false;
2736}
2737
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002738bool OutputHLSL::visitSwitch(Visit visit, TIntermSwitch *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002739{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002740 TInfoSinkBase &out = getInfoSink();
2741
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002742 if (node->getStatementList())
2743 {
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +02002744 node->setStatementList(RemoveSwitchFallThrough::removeFallThrough(node->getStatementList()));
Jamie Madill8c46ab12015-12-07 16:39:19 -05002745 outputTriplet(out, visit, "switch (", ") ", "");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002746 // The curly braces get written when visiting the statementList aggregate
2747 }
2748 else
2749 {
2750 // No statementList, so it won't output curly braces
Jamie Madill8c46ab12015-12-07 16:39:19 -05002751 outputTriplet(out, visit, "switch (", ") {", "}\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002752 }
2753 return true;
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002754}
2755
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002756bool OutputHLSL::visitCase(Visit visit, TIntermCase *node)
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002757{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002758 TInfoSinkBase &out = getInfoSink();
2759
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002760 if (node->hasCondition())
2761 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002762 outputTriplet(out, visit, "case (", "", "):\n");
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002763 return true;
2764 }
2765 else
2766 {
Olli Etuaho05ae50d2015-02-20 10:16:47 +02002767 out << "default:\n";
2768 return false;
2769 }
Olli Etuaho3c1dfb52015-02-20 11:34:03 +02002770}
2771
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002772void OutputHLSL::visitConstantUnion(TIntermConstantUnion *node)
2773{
Jamie Madill8c46ab12015-12-07 16:39:19 -05002774 TInfoSinkBase &out = getInfoSink();
2775 writeConstantUnion(out, node->getType(), node->getUnionArrayPointer());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002776}
2777
2778bool OutputHLSL::visitLoop(Visit visit, TIntermLoop *node)
2779{
Nicolas Capens655fe362014-04-11 13:12:34 -04002780 mNestedLoopDepth++;
2781
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002782 bool wasDiscontinuous = mInsideDiscontinuousLoop;
Corentin Wallez1239ee92015-03-19 14:38:02 -07002783 mInsideDiscontinuousLoop = mInsideDiscontinuousLoop ||
Corentin Walleza1884f22015-04-29 10:15:16 -07002784 mCurrentFunctionMetadata->mDiscontinuousLoops.count(node) > 0;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002785
Jamie Madill8c46ab12015-12-07 16:39:19 -05002786 TInfoSinkBase &out = getInfoSink();
2787
Olli Etuaho9b4e8622015-12-22 15:53:22 +02002788 if (mOutputType == SH_HLSL_3_0_OUTPUT)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002789 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002790 if (handleExcessiveLoop(out, node))
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002791 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002792 mInsideDiscontinuousLoop = wasDiscontinuous;
2793 mNestedLoopDepth--;
2794
shannon.woods@transgaming.com9cbce922013-02-28 23:14:24 +00002795 return false;
2796 }
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002797 }
2798
Corentin Wallez1239ee92015-03-19 14:38:02 -07002799 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
alokp@chromium.org52813552010-11-16 18:36:09 +00002800 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002801 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002802 out << "{" << unroll << " do\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002803
Jamie Madill8c46ab12015-12-07 16:39:19 -05002804 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002805 }
2806 else
2807 {
Corentin Wallez1239ee92015-03-19 14:38:02 -07002808 out << "{" << unroll << " for(";
Jamie Madillf91ce812014-06-13 10:04:34 -04002809
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002810 if (node->getInit())
2811 {
2812 node->getInit()->traverse(this);
2813 }
2814
2815 out << "; ";
2816
alokp@chromium.org52813552010-11-16 18:36:09 +00002817 if (node->getCondition())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002819 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002820 }
2821
2822 out << "; ";
2823
alokp@chromium.org52813552010-11-16 18:36:09 +00002824 if (node->getExpression())
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002825 {
alokp@chromium.org52813552010-11-16 18:36:09 +00002826 node->getExpression()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002827 }
2828
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002829 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04002830
Jamie Madill8c46ab12015-12-07 16:39:19 -05002831 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002832 }
2833
2834 if (node->getBody())
2835 {
Olli Etuaho4785fec2015-05-18 16:09:37 +03002836 // The loop body node will output braces.
2837 ASSERT(IsSequence(node->getBody()));
Olli Etuahoa6f22092015-05-08 18:31:10 +03002838 node->getBody()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002839 }
Olli Etuaho4785fec2015-05-18 16:09:37 +03002840 else
2841 {
2842 // TODO(oetuaho): Check if the semicolon inside is necessary.
2843 // It's there as a result of conservative refactoring of the output.
2844 out << "{;}\n";
2845 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002846
Jamie Madill8c46ab12015-12-07 16:39:19 -05002847 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002848
alokp@chromium.org52813552010-11-16 18:36:09 +00002849 if (node->getType() == ELoopDoWhile)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002850 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05002851 outputLineDirective(out, node->getCondition()->getLine().first_line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002852 out << "while(\n";
2853
alokp@chromium.org52813552010-11-16 18:36:09 +00002854 node->getCondition()->traverse(this);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002855
daniel@transgaming.com73536982012-03-21 20:45:49 +00002856 out << ");";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002857 }
2858
daniel@transgaming.com73536982012-03-21 20:45:49 +00002859 out << "}\n";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002860
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002861 mInsideDiscontinuousLoop = wasDiscontinuous;
Nicolas Capens655fe362014-04-11 13:12:34 -04002862 mNestedLoopDepth--;
daniel@transgaming.come11100c2012-05-31 01:20:32 +00002863
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002864 return false;
2865}
2866
2867bool OutputHLSL::visitBranch(Visit visit, TIntermBranch *node)
2868{
Jamie Madill32aab012015-01-27 14:12:26 -05002869 TInfoSinkBase &out = getInfoSink();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002870
2871 switch (node->getFlowOp())
2872 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002873 case EOpKill:
Jamie Madill8c46ab12015-12-07 16:39:19 -05002874 outputTriplet(out, visit, "discard;\n", "", "");
Jamie Madill3c9eeb92013-11-04 11:09:26 -05002875 break;
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002876 case EOpBreak:
2877 if (visit == PreVisit)
2878 {
Nicolas Capens655fe362014-04-11 13:12:34 -04002879 if (mNestedLoopDepth > 1)
2880 {
2881 mUsesNestedBreak = true;
2882 }
2883
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00002884 if (mExcessiveLoopIndex)
2885 {
2886 out << "{Break";
2887 mExcessiveLoopIndex->traverse(this);
2888 out << " = true; break;}\n";
2889 }
2890 else
2891 {
2892 out << "break;\n";
2893 }
2894 }
2895 break;
Jamie Madill8c46ab12015-12-07 16:39:19 -05002896 case EOpContinue:
2897 outputTriplet(out, visit, "continue;\n", "", "");
2898 break;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002899 case EOpReturn:
2900 if (visit == PreVisit)
2901 {
2902 if (node->getExpression())
2903 {
2904 out << "return ";
2905 }
2906 else
2907 {
2908 out << "return;\n";
2909 }
2910 }
2911 else if (visit == PostVisit)
2912 {
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00002913 if (node->getExpression())
2914 {
2915 out << ";\n";
2916 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002917 }
2918 break;
2919 default: UNREACHABLE();
2920 }
2921
2922 return true;
2923}
2924
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002925bool OutputHLSL::isSingleStatement(TIntermNode *node)
2926{
2927 TIntermAggregate *aggregate = node->getAsAggregate();
2928
2929 if (aggregate)
2930 {
2931 if (aggregate->getOp() == EOpSequence)
2932 {
2933 return false;
2934 }
Nicolas Capensfa41aa02014-10-06 17:40:13 -04002935 else if (aggregate->getOp() == EOpDeclaration)
2936 {
2937 // Declaring multiple comma-separated variables must be considered multiple statements
2938 // because each individual declaration has side effects which are visible in the next.
2939 return false;
2940 }
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002941 else
2942 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002943 for (TIntermSequence::iterator sit = aggregate->getSequence()->begin(); sit != aggregate->getSequence()->end(); sit++)
daniel@transgaming.comb5875982010-04-15 20:44:53 +00002944 {
2945 if (!isSingleStatement(*sit))
2946 {
2947 return false;
2948 }
2949 }
2950
2951 return true;
2952 }
2953 }
2954
2955 return true;
2956}
2957
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002958// Handle loops with more than 254 iterations (unsupported by D3D9) by splitting them
2959// (The D3D documentation says 255 iterations, but the compiler complains at anything more than 254).
Jamie Madill8c46ab12015-12-07 16:39:19 -05002960bool OutputHLSL::handleExcessiveLoop(TInfoSinkBase &out, TIntermLoop *node)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002961{
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00002962 const int MAX_LOOP_ITERATIONS = 254;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002963
2964 // Parse loops of the form:
2965 // for(int index = initial; index [comparator] limit; index += increment)
2966 TIntermSymbol *index = NULL;
2967 TOperator comparator = EOpNull;
2968 int initial = 0;
2969 int limit = 0;
2970 int increment = 0;
2971
2972 // Parse index name and intial value
2973 if (node->getInit())
2974 {
2975 TIntermAggregate *init = node->getInit()->getAsAggregate();
2976
2977 if (init)
2978 {
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002979 TIntermSequence *sequence = init->getSequence();
2980 TIntermTyped *variable = (*sequence)[0]->getAsTyped();
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002981
2982 if (variable && variable->getQualifier() == EvqTemporary)
2983 {
2984 TIntermBinary *assign = variable->getAsBinaryNode();
2985
2986 if (assign->getOp() == EOpInitialize)
2987 {
2988 TIntermSymbol *symbol = assign->getLeft()->getAsSymbolNode();
2989 TIntermConstantUnion *constant = assign->getRight()->getAsConstantUnion();
2990
2991 if (symbol && constant)
2992 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002993 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002994 {
2995 index = symbol;
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00002996 initial = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00002997 }
2998 }
2999 }
3000 }
3001 }
3002 }
3003
3004 // Parse comparator and limit value
alokp@chromium.org52813552010-11-16 18:36:09 +00003005 if (index != NULL && node->getCondition())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003006 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003007 TIntermBinary *test = node->getCondition()->getAsBinaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003008
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003009 if (test && test->getLeft()->getAsSymbolNode()->getId() == index->getId())
3010 {
3011 TIntermConstantUnion *constant = test->getRight()->getAsConstantUnion();
3012
3013 if (constant)
3014 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003015 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003016 {
3017 comparator = test->getOp();
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003018 limit = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003019 }
3020 }
3021 }
3022 }
3023
3024 // Parse increment
alokp@chromium.org52813552010-11-16 18:36:09 +00003025 if (index != NULL && comparator != EOpNull && node->getExpression())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003026 {
alokp@chromium.org52813552010-11-16 18:36:09 +00003027 TIntermBinary *binaryTerminal = node->getExpression()->getAsBinaryNode();
3028 TIntermUnary *unaryTerminal = node->getExpression()->getAsUnaryNode();
Jamie Madillf91ce812014-06-13 10:04:34 -04003029
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003030 if (binaryTerminal)
3031 {
3032 TOperator op = binaryTerminal->getOp();
3033 TIntermConstantUnion *constant = binaryTerminal->getRight()->getAsConstantUnion();
3034
3035 if (constant)
3036 {
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003037 if (constant->getBasicType() == EbtInt && constant->isScalar())
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003038 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +00003039 int value = constant->getIConst(0);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003040
3041 switch (op)
3042 {
3043 case EOpAddAssign: increment = value; break;
3044 case EOpSubAssign: increment = -value; break;
3045 default: UNIMPLEMENTED();
3046 }
3047 }
3048 }
3049 }
3050 else if (unaryTerminal)
3051 {
3052 TOperator op = unaryTerminal->getOp();
3053
3054 switch (op)
3055 {
3056 case EOpPostIncrement: increment = 1; break;
3057 case EOpPostDecrement: increment = -1; break;
3058 case EOpPreIncrement: increment = 1; break;
3059 case EOpPreDecrement: increment = -1; break;
3060 default: UNIMPLEMENTED();
3061 }
3062 }
3063 }
3064
3065 if (index != NULL && comparator != EOpNull && increment != 0)
3066 {
3067 if (comparator == EOpLessThanEqual)
3068 {
3069 comparator = EOpLessThan;
3070 limit += 1;
3071 }
3072
3073 if (comparator == EOpLessThan)
3074 {
daniel@transgaming.comf1f538e2011-02-09 16:30:01 +00003075 int iterations = (limit - initial) / increment;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003076
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003077 if (iterations <= MAX_LOOP_ITERATIONS)
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003078 {
3079 return false; // Not an excessive loop
3080 }
3081
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003082 TIntermSymbol *restoreIndex = mExcessiveLoopIndex;
3083 mExcessiveLoopIndex = index;
3084
daniel@transgaming.com0933b0c2012-07-11 20:37:28 +00003085 out << "{int ";
3086 index->traverse(this);
daniel@transgaming.com8c77f852012-07-11 20:37:35 +00003087 out << ";\n"
3088 "bool Break";
3089 index->traverse(this);
3090 out << " = false;\n";
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003091
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003092 bool firstLoopFragment = true;
3093
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003094 while (iterations > 0)
3095 {
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003096 int clampedLimit = initial + increment * std::min(MAX_LOOP_ITERATIONS, iterations);
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003097
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003098 if (!firstLoopFragment)
3099 {
Jamie Madill3c9eeb92013-11-04 11:09:26 -05003100 out << "if (!Break";
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003101 index->traverse(this);
3102 out << ") {\n";
3103 }
daniel@transgaming.com2fe20a82012-07-11 20:37:41 +00003104
3105 if (iterations <= MAX_LOOP_ITERATIONS) // Last loop fragment
3106 {
3107 mExcessiveLoopIndex = NULL; // Stops setting the Break flag
3108 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003109
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003110 // for(int index = initial; index < clampedLimit; index += increment)
Corentin Wallez1239ee92015-03-19 14:38:02 -07003111 const char *unroll = mCurrentFunctionMetadata->hasGradientInCallGraph(node) ? "LOOP" : "";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003112
Corentin Wallez1239ee92015-03-19 14:38:02 -07003113 out << unroll << " for(";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003114 index->traverse(this);
3115 out << " = ";
3116 out << initial;
3117
3118 out << "; ";
3119 index->traverse(this);
3120 out << " < ";
3121 out << clampedLimit;
3122
3123 out << "; ";
3124 index->traverse(this);
3125 out << " += ";
3126 out << increment;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003127 out << ")\n";
Jamie Madillf91ce812014-06-13 10:04:34 -04003128
Jamie Madill8c46ab12015-12-07 16:39:19 -05003129 outputLineDirective(out, node->getLine().first_line);
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003130 out << "{\n";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003131
3132 if (node->getBody())
3133 {
3134 node->getBody()->traverse(this);
3135 }
3136
Jamie Madill8c46ab12015-12-07 16:39:19 -05003137 outputLineDirective(out, node->getLine().first_line);
daniel@transgaming.com5b60f5e2012-07-11 20:37:38 +00003138 out << ";}\n";
3139
3140 if (!firstLoopFragment)
3141 {
3142 out << "}\n";
3143 }
3144
3145 firstLoopFragment = false;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003146
daniel@transgaming.com06eb0d42012-05-31 01:17:14 +00003147 initial += MAX_LOOP_ITERATIONS * increment;
3148 iterations -= MAX_LOOP_ITERATIONS;
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003149 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003150
daniel@transgaming.comc264de42012-07-11 20:37:25 +00003151 out << "}";
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003152
daniel@transgaming.come9b3f602012-07-11 20:37:31 +00003153 mExcessiveLoopIndex = restoreIndex;
3154
daniel@transgaming.com4a35ef22010-04-08 03:51:06 +00003155 return true;
3156 }
3157 else UNIMPLEMENTED();
3158 }
3159
3160 return false; // Not handled as an excessive loop
3161}
3162
Jamie Madill8c46ab12015-12-07 16:39:19 -05003163void OutputHLSL::outputTriplet(TInfoSinkBase &out,
3164 Visit visit,
3165 const char *preString,
3166 const char *inString,
3167 const char *postString)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003168{
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003169 if (visit == PreVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003170 {
3171 out << preString;
3172 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003173 else if (visit == InVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003174 {
3175 out << inString;
3176 }
daniel@transgaming.com67de6d62010-04-29 03:35:30 +00003177 else if (visit == PostVisit)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003178 {
3179 out << postString;
3180 }
3181}
3182
Jamie Madill8c46ab12015-12-07 16:39:19 -05003183void OutputHLSL::outputLineDirective(TInfoSinkBase &out, int line)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003184{
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003185 if ((mCompileOptions & SH_LINE_DIRECTIVES) && (line > 0))
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003186 {
Jamie Madill32aab012015-01-27 14:12:26 -05003187 out << "\n";
3188 out << "#line " << line;
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003189
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003190 if (mSourcePath)
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003191 {
Olli Etuahoa3a5cc62015-02-13 13:12:22 +02003192 out << " \"" << mSourcePath << "\"";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003193 }
Jamie Madillf91ce812014-06-13 10:04:34 -04003194
Jamie Madill32aab012015-01-27 14:12:26 -05003195 out << "\n";
apatrick@chromium.org0f4cefe2011-01-26 19:30:57 +00003196 }
3197}
3198
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003199TString OutputHLSL::argumentString(const TIntermSymbol *symbol)
3200{
3201 TQualifier qualifier = symbol->getQualifier();
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003202 const TType &type = symbol->getType();
3203 const TName &name = symbol->getName();
3204 TString nameStr;
daniel@transgaming.comd1acd1e2010-04-13 03:25:57 +00003205
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003206 if (name.getString().empty()) // HLSL demands named arguments, also for prototypes
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003207 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003208 nameStr = "x" + str(mUniqueIndex++);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003209 }
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003210 else
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003211 {
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003212 nameStr = DecorateIfNeeded(name);
daniel@transgaming.com005c7392010-04-15 20:45:27 +00003213 }
3214
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003215 if (IsSampler(type.getBasicType()))
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003216 {
Olli Etuaho9b4e8622015-12-22 15:53:22 +02003217 if (mOutputType == SH_HLSL_4_1_OUTPUT)
3218 {
3219 // Samplers are passed as indices to the sampler array.
3220 ASSERT(qualifier != EvqOut && qualifier != EvqInOut);
3221 return "const uint " + nameStr + ArrayString(type);
3222 }
3223 if (mOutputType == SH_HLSL_4_0_FL9_3_OUTPUT)
3224 {
3225 return QualifierString(qualifier) + " " + TextureString(type.getBasicType()) +
3226 " texture_" + nameStr + ArrayString(type) + ", " + QualifierString(qualifier) +
3227 " " + SamplerString(type.getBasicType()) + " sampler_" + nameStr +
3228 ArrayString(type);
3229 }
shannon.woods@transgaming.com01a5cf92013-02-28 23:13:51 +00003230 }
3231
Olli Etuahof5cfc8d2015-08-06 16:36:39 +03003232 return QualifierString(qualifier) + " " + TypeString(type) + " " + nameStr + ArrayString(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003233}
3234
3235TString OutputHLSL::initializer(const TType &type)
3236{
3237 TString string;
3238
Jamie Madill94bf7f22013-07-08 13:31:15 -04003239 size_t size = type.getObjectSize();
3240 for (size_t component = 0; component < size; component++)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003241 {
daniel@transgaming.comead23042010-04-29 03:35:36 +00003242 string += "0";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003243
Jamie Madill94bf7f22013-07-08 13:31:15 -04003244 if (component + 1 < size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003245 {
3246 string += ", ";
3247 }
3248 }
3249
daniel@transgaming.comead23042010-04-29 03:35:36 +00003250 return "{" + string + "}";
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003251}
daniel@transgaming.com72d0b522010-04-13 19:53:44 +00003252
Jamie Madill8c46ab12015-12-07 16:39:19 -05003253void OutputHLSL::outputConstructor(TInfoSinkBase &out,
3254 Visit visit,
3255 const TType &type,
3256 const char *name,
3257 const TIntermSequence *parameters)
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003258{
Olli Etuahof40319e2015-03-10 14:33:00 +02003259 if (type.isArray())
3260 {
3261 UNIMPLEMENTED();
3262 }
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003263
3264 if (visit == PreVisit)
3265 {
Jamie Madill8daaba12014-06-13 10:04:33 -04003266 mStructureHLSL->addConstructor(type, name, parameters);
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003267
Daniel Bratell29190082015-02-20 16:42:54 +01003268 out << name << "(";
Nicolas Capens1af18dc2014-06-11 11:07:32 -04003269 }
3270 else if (visit == InVisit)
3271 {
3272 out << ", ";
3273 }
3274 else if (visit == PostVisit)
3275 {
3276 out << ")";
3277 }
3278}
3279
Jamie Madill8c46ab12015-12-07 16:39:19 -05003280const TConstantUnion *OutputHLSL::writeConstantUnion(TInfoSinkBase &out,
3281 const TType &type,
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003282 const TConstantUnion *const constUnion)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003283{
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003284 const TConstantUnion *constUnionIterated = constUnion;
3285
Jamie Madill98493dd2013-07-08 14:39:03 -04003286 const TStructure* structure = type.getStruct();
3287 if (structure)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003288 {
Jamie Madill033dae62014-06-18 12:56:28 -04003289 out << StructNameString(*structure) + "_ctor(";
Jamie Madillf91ce812014-06-13 10:04:34 -04003290
Jamie Madill98493dd2013-07-08 14:39:03 -04003291 const TFieldList& fields = structure->fields();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003292
Jamie Madill98493dd2013-07-08 14:39:03 -04003293 for (size_t i = 0; i < fields.size(); i++)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003294 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003295 const TType *fieldType = fields[i]->type();
Jamie Madill8c46ab12015-12-07 16:39:19 -05003296 constUnionIterated = writeConstantUnion(out, *fieldType, constUnionIterated);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003297
Jamie Madill98493dd2013-07-08 14:39:03 -04003298 if (i != fields.size() - 1)
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003299 {
3300 out << ", ";
3301 }
3302 }
3303
3304 out << ")";
3305 }
3306 else
3307 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04003308 size_t size = type.getObjectSize();
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003309 bool writeType = size > 1;
Jamie Madillf91ce812014-06-13 10:04:34 -04003310
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003311 if (writeType)
3312 {
Jamie Madill033dae62014-06-18 12:56:28 -04003313 out << TypeString(type) << "(";
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003314 }
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003315 constUnionIterated = WriteConstantUnionArray(out, constUnionIterated, size);
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003316 if (writeType)
3317 {
3318 out << ")";
3319 }
3320 }
3321
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003322 return constUnionIterated;
daniel@transgaming.coma54da4e2010-05-07 13:03:28 +00003323}
3324
Jamie Madill8c46ab12015-12-07 16:39:19 -05003325void OutputHLSL::writeEmulatedFunctionTriplet(TInfoSinkBase &out, Visit visit, const char *preStr)
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003326{
3327 TString preString = BuiltInFunctionEmulator::GetEmulatedFunctionName(preStr);
Jamie Madill8c46ab12015-12-07 16:39:19 -05003328 outputTriplet(out, visit, preString.c_str(), ", ", ")");
Olli Etuaho5c9cd3d2014-12-18 13:04:25 +02003329}
3330
Jamie Madill37997142015-01-28 10:06:34 -05003331bool OutputHLSL::writeSameSymbolInitializer(TInfoSinkBase &out, TIntermSymbol *symbolNode, TIntermTyped *expression)
3332{
3333 sh::SearchSymbol searchSymbol(symbolNode->getSymbol());
3334 expression->traverse(&searchSymbol);
3335
3336 if (searchSymbol.foundMatch())
3337 {
3338 // Type already printed
3339 out << "t" + str(mUniqueIndex) + " = ";
3340 expression->traverse(this);
3341 out << ", ";
3342 symbolNode->traverse(this);
3343 out << " = t" + str(mUniqueIndex);
3344
3345 mUniqueIndex++;
3346 return true;
3347 }
3348
3349 return false;
3350}
3351
Olli Etuaho18b9deb2015-11-05 12:14:50 +02003352bool OutputHLSL::canWriteAsHLSLLiteral(TIntermTyped *expression)
3353{
3354 // We support writing constant unions and constructors that only take constant unions as
3355 // parameters as HLSL literals.
3356 if (expression->getAsConstantUnion())
3357 {
3358 return true;
3359 }
3360 if (expression->getQualifier() != EvqConst || !expression->getAsAggregate() ||
3361 !expression->getAsAggregate()->isConstructor())
3362 {
3363 return false;
3364 }
3365 TIntermAggregate *constructor = expression->getAsAggregate();
3366 for (TIntermNode *&node : *constructor->getSequence())
3367 {
3368 if (!node->getAsConstantUnion())
3369 return false;
3370 }
3371 return true;
3372}
3373
3374bool OutputHLSL::writeConstantInitialization(TInfoSinkBase &out,
3375 TIntermSymbol *symbolNode,
3376 TIntermTyped *expression)
3377{
3378 if (canWriteAsHLSLLiteral(expression))
3379 {
3380 symbolNode->traverse(this);
3381 if (expression->getType().isArray())
3382 {
3383 out << "[" << expression->getType().getArraySize() << "]";
3384 }
3385 out << " = {";
3386 if (expression->getAsConstantUnion())
3387 {
3388 TIntermConstantUnion *nodeConst = expression->getAsConstantUnion();
3389 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3390 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3391 }
3392 else
3393 {
3394 TIntermAggregate *constructor = expression->getAsAggregate();
3395 ASSERT(constructor != nullptr);
3396 for (TIntermNode *&node : *constructor->getSequence())
3397 {
3398 TIntermConstantUnion *nodeConst = node->getAsConstantUnion();
3399 ASSERT(nodeConst);
3400 const TConstantUnion *constUnion = nodeConst->getUnionArrayPointer();
3401 WriteConstantUnionArray(out, constUnion, nodeConst->getType().getObjectSize());
3402 if (node != constructor->getSequence()->back())
3403 {
3404 out << ", ";
3405 }
3406 }
3407 }
3408 out << "}";
3409 return true;
3410 }
3411 return false;
3412}
3413
Jamie Madill37997142015-01-28 10:06:34 -05003414void OutputHLSL::writeDeferredGlobalInitializers(TInfoSinkBase &out)
3415{
3416 out << "#define ANGLE_USES_DEFERRED_INIT\n"
3417 << "\n"
3418 << "void initializeDeferredGlobals()\n"
3419 << "{\n";
3420
3421 for (const auto &deferredGlobal : mDeferredGlobalInitializers)
3422 {
Olli Etuahod81ed842015-05-12 12:46:35 +03003423 TIntermBinary *binary = deferredGlobal->getAsBinaryNode();
3424 TIntermSelection *selection = deferredGlobal->getAsSelectionNode();
3425 if (binary != nullptr)
3426 {
3427 TIntermSymbol *symbol = binary->getLeft()->getAsSymbolNode();
3428 TIntermTyped *expression = binary->getRight();
3429 ASSERT(symbol);
3430 ASSERT(symbol->getQualifier() == EvqGlobal && expression->getQualifier() != EvqConst);
Jamie Madill37997142015-01-28 10:06:34 -05003431
Olli Etuahod81ed842015-05-12 12:46:35 +03003432 out << " " << Decorate(symbol->getSymbol()) << " = ";
Jamie Madill37997142015-01-28 10:06:34 -05003433
Olli Etuahod81ed842015-05-12 12:46:35 +03003434 if (!writeSameSymbolInitializer(out, symbol, expression))
3435 {
3436 ASSERT(mInfoSinkStack.top() == &out);
3437 expression->traverse(this);
3438 }
3439 out << ";\n";
3440 }
3441 else if (selection != nullptr)
Jamie Madill37997142015-01-28 10:06:34 -05003442 {
Jamie Madill8c46ab12015-12-07 16:39:19 -05003443 writeSelection(out, selection);
Jamie Madill37997142015-01-28 10:06:34 -05003444 }
Olli Etuahod81ed842015-05-12 12:46:35 +03003445 else
3446 {
3447 UNREACHABLE();
3448 }
Jamie Madill37997142015-01-28 10:06:34 -05003449 }
3450
3451 out << "}\n"
3452 << "\n";
3453}
3454
Jamie Madill55e79e02015-02-09 15:35:00 -05003455TString OutputHLSL::addStructEqualityFunction(const TStructure &structure)
3456{
3457 const TFieldList &fields = structure.fields();
3458
3459 for (const auto &eqFunction : mStructEqualityFunctions)
3460 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003461 if (eqFunction->structure == &structure)
Jamie Madill55e79e02015-02-09 15:35:00 -05003462 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003463 return eqFunction->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003464 }
3465 }
3466
3467 const TString &structNameString = StructNameString(structure);
3468
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003469 StructEqualityFunction *function = new StructEqualityFunction();
3470 function->structure = &structure;
3471 function->functionName = "angle_eq_" + structNameString;
Jamie Madill55e79e02015-02-09 15:35:00 -05003472
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003473 TInfoSinkBase fnOut;
Jamie Madill55e79e02015-02-09 15:35:00 -05003474
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003475 fnOut << "bool " << function->functionName << "(" << structNameString << " a, " << structNameString + " b)\n"
3476 << "{\n"
3477 " return ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003478
3479 for (size_t i = 0; i < fields.size(); i++)
3480 {
3481 const TField *field = fields[i];
3482 const TType *fieldType = field->type();
3483
3484 const TString &fieldNameA = "a." + Decorate(field->name());
3485 const TString &fieldNameB = "b." + Decorate(field->name());
3486
3487 if (i > 0)
3488 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003489 fnOut << " && ";
Jamie Madill55e79e02015-02-09 15:35:00 -05003490 }
3491
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003492 fnOut << "(";
3493 outputEqual(PreVisit, *fieldType, EOpEqual, fnOut);
3494 fnOut << fieldNameA;
3495 outputEqual(InVisit, *fieldType, EOpEqual, fnOut);
3496 fnOut << fieldNameB;
3497 outputEqual(PostVisit, *fieldType, EOpEqual, fnOut);
3498 fnOut << ")";
Jamie Madill55e79e02015-02-09 15:35:00 -05003499 }
3500
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003501 fnOut << ";\n" << "}\n";
3502
3503 function->functionDefinition = fnOut.c_str();
Jamie Madill55e79e02015-02-09 15:35:00 -05003504
3505 mStructEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003506 mEqualityFunctions.push_back(function);
Jamie Madill55e79e02015-02-09 15:35:00 -05003507
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003508 return function->functionName;
Jamie Madill55e79e02015-02-09 15:35:00 -05003509}
3510
Olli Etuaho7fb49552015-03-18 17:27:44 +02003511TString OutputHLSL::addArrayEqualityFunction(const TType& type)
3512{
3513 for (const auto &eqFunction : mArrayEqualityFunctions)
3514 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003515 if (eqFunction->type == type)
Olli Etuaho7fb49552015-03-18 17:27:44 +02003516 {
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003517 return eqFunction->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003518 }
3519 }
3520
3521 const TString &typeName = TypeString(type);
3522
Olli Etuaho12690762015-03-31 12:55:28 +03003523 ArrayHelperFunction *function = new ArrayHelperFunction();
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003524 function->type = type;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003525
3526 TInfoSinkBase fnNameOut;
3527 fnNameOut << "angle_eq_" << type.getArraySize() << "_" << typeName;
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003528 function->functionName = fnNameOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003529
3530 TType nonArrayType = type;
3531 nonArrayType.clearArrayness();
3532
3533 TInfoSinkBase fnOut;
3534
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003535 fnOut << "bool " << function->functionName << "("
Olli Etuahofc7cfd12015-03-31 14:46:18 +03003536 << typeName << " a[" << type.getArraySize() << "], "
3537 << typeName << " b[" << type.getArraySize() << "])\n"
Olli Etuaho7fb49552015-03-18 17:27:44 +02003538 << "{\n"
3539 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3540 " {\n"
3541 " if (";
3542
3543 outputEqual(PreVisit, nonArrayType, EOpNotEqual, fnOut);
3544 fnOut << "a[i]";
3545 outputEqual(InVisit, nonArrayType, EOpNotEqual, fnOut);
3546 fnOut << "b[i]";
3547 outputEqual(PostVisit, nonArrayType, EOpNotEqual, fnOut);
3548
3549 fnOut << ") { return false; }\n"
3550 " }\n"
3551 " return true;\n"
3552 "}\n";
3553
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003554 function->functionDefinition = fnOut.c_str();
Olli Etuaho7fb49552015-03-18 17:27:44 +02003555
3556 mArrayEqualityFunctions.push_back(function);
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003557 mEqualityFunctions.push_back(function);
Olli Etuaho7fb49552015-03-18 17:27:44 +02003558
Olli Etuahoae37a5c2015-03-20 16:50:15 +02003559 return function->functionName;
Olli Etuaho7fb49552015-03-18 17:27:44 +02003560}
3561
Olli Etuaho12690762015-03-31 12:55:28 +03003562TString OutputHLSL::addArrayAssignmentFunction(const TType& type)
3563{
3564 for (const auto &assignFunction : mArrayAssignmentFunctions)
3565 {
3566 if (assignFunction.type == type)
3567 {
3568 return assignFunction.functionName;
3569 }
3570 }
3571
3572 const TString &typeName = TypeString(type);
3573
3574 ArrayHelperFunction function;
3575 function.type = type;
3576
3577 TInfoSinkBase fnNameOut;
3578 fnNameOut << "angle_assign_" << type.getArraySize() << "_" << typeName;
3579 function.functionName = fnNameOut.c_str();
3580
3581 TInfoSinkBase fnOut;
3582
3583 fnOut << "void " << function.functionName << "(out "
3584 << typeName << " a[" << type.getArraySize() << "], "
3585 << typeName << " b[" << type.getArraySize() << "])\n"
3586 << "{\n"
3587 " for (int i = 0; i < " << type.getArraySize() << "; ++i)\n"
3588 " {\n"
3589 " a[i] = b[i];\n"
3590 " }\n"
3591 "}\n";
3592
3593 function.functionDefinition = fnOut.c_str();
3594
3595 mArrayAssignmentFunctions.push_back(function);
3596
3597 return function.functionName;
3598}
3599
Olli Etuaho9638c352015-04-01 14:34:52 +03003600TString OutputHLSL::addArrayConstructIntoFunction(const TType& type)
3601{
3602 for (const auto &constructIntoFunction : mArrayConstructIntoFunctions)
3603 {
3604 if (constructIntoFunction.type == type)
3605 {
3606 return constructIntoFunction.functionName;
3607 }
3608 }
3609
3610 const TString &typeName = TypeString(type);
3611
3612 ArrayHelperFunction function;
3613 function.type = type;
3614
3615 TInfoSinkBase fnNameOut;
3616 fnNameOut << "angle_construct_into_" << type.getArraySize() << "_" << typeName;
3617 function.functionName = fnNameOut.c_str();
3618
3619 TInfoSinkBase fnOut;
3620
3621 fnOut << "void " << function.functionName << "(out "
3622 << typeName << " a[" << type.getArraySize() << "]";
3623 for (int i = 0; i < type.getArraySize(); ++i)
3624 {
3625 fnOut << ", " << typeName << " b" << i;
3626 }
3627 fnOut << ")\n"
3628 "{\n";
3629
3630 for (int i = 0; i < type.getArraySize(); ++i)
3631 {
3632 fnOut << " a[" << i << "] = b" << i << ";\n";
3633 }
3634 fnOut << "}\n";
3635
3636 function.functionDefinition = fnOut.c_str();
3637
3638 mArrayConstructIntoFunctions.push_back(function);
3639
3640 return function.functionName;
3641}
3642
Jamie Madill2e295e22015-04-29 10:41:33 -04003643void OutputHLSL::ensureStructDefined(const TType &type)
3644{
3645 TStructure *structure = type.getStruct();
3646
3647 if (structure)
3648 {
3649 mStructureHLSL->addConstructor(type, StructNameString(*structure), nullptr);
3650 }
3651}
3652
3653
Olli Etuaho9638c352015-04-01 14:34:52 +03003654
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003655}